Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / cp / parser.cc
blobf3abae716fe2f4579f5776a5dd029b8ca8c1daa7
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2023 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 "contracts.h"
50 #include "bitmap.h"
51 #include "builtins.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.cc) and the C++ parser. */
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_AUTO, /* auto */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
186 RT_CO_YIELD /* co_yield */
189 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
190 reverting it on destruction. */
192 class type_id_in_expr_sentinel
194 cp_parser *parser;
195 bool saved;
196 public:
197 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
198 : parser (parser),
199 saved (parser->in_type_id_in_expr_p)
200 { parser->in_type_id_in_expr_p = set; }
201 ~type_id_in_expr_sentinel ()
202 { parser->in_type_id_in_expr_p = saved; }
205 /* Prototypes. */
207 static cp_lexer *cp_lexer_new_main
208 (void);
209 static cp_lexer *cp_lexer_new_from_tokens
210 (cp_token_cache *tokens);
211 static void cp_lexer_destroy
212 (cp_lexer *);
213 static int cp_lexer_saving_tokens
214 (const cp_lexer *);
215 static cp_token *cp_lexer_token_at
216 (cp_lexer *, cp_token_position);
217 static void cp_lexer_get_preprocessor_token
218 (unsigned, cp_token *);
219 static inline cp_token *cp_lexer_peek_token
220 (cp_lexer *);
221 static cp_token *cp_lexer_peek_nth_token
222 (cp_lexer *, size_t);
223 static inline bool cp_lexer_next_token_is
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_not
226 (cp_lexer *, enum cpp_ttype);
227 static bool cp_lexer_next_token_is_keyword
228 (cp_lexer *, enum rid);
229 static cp_token *cp_lexer_consume_token
230 (cp_lexer *);
231 static void cp_lexer_purge_token
232 (cp_lexer *);
233 static void cp_lexer_purge_tokens_after
234 (cp_lexer *, cp_token_position);
235 static void cp_lexer_save_tokens
236 (cp_lexer *);
237 static void cp_lexer_commit_tokens
238 (cp_lexer *);
239 static void cp_lexer_rollback_tokens
240 (cp_lexer *);
241 static void cp_lexer_print_token
242 (FILE *, cp_token *);
243 static inline bool cp_lexer_debugging_p
244 (cp_lexer *);
245 static void cp_lexer_start_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static void cp_lexer_stop_debugging
248 (cp_lexer *) ATTRIBUTE_UNUSED;
250 static cp_token_cache *cp_token_cache_new
251 (cp_token *, cp_token *);
252 static tree cp_parser_late_noexcept_specifier
253 (cp_parser *, tree);
254 static void noexcept_override_late_checks
255 (tree);
257 static void cp_parser_initial_pragma
258 (cp_token *);
260 static bool cp_parser_omp_declare_reduction_exprs
261 (tree, cp_parser *);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
265 static void check_omp_intervening_code
266 (cp_parser *);
269 /* Manifest constants. */
270 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
271 #define CP_SAVED_TOKEN_STACK 5
273 /* Variables. */
275 /* The stream to which debugging output should be written. */
276 static FILE *cp_lexer_debug_stream;
278 /* Nonzero if we are parsing an unevaluated operand: an operand to
279 sizeof, typeof, or alignof. */
280 int cp_unevaluated_operand;
282 /* Dump up to NUM tokens in BUFFER to FILE starting with token
283 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
284 first token in BUFFER. If NUM is 0, dump all the tokens. If
285 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
286 highlighted by surrounding it in [[ ]]. */
288 static void
289 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
290 cp_token *start_token, unsigned num,
291 cp_token *curr_token)
293 unsigned i, nprinted;
294 cp_token *token;
295 bool do_print;
297 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
299 if (buffer == NULL)
300 return;
302 if (num == 0)
303 num = buffer->length ();
305 if (start_token == NULL)
306 start_token = buffer->address ();
308 if (start_token > buffer->address ())
310 cp_lexer_print_token (file, &(*buffer)[0]);
311 fprintf (file, " ... ");
314 do_print = false;
315 nprinted = 0;
316 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
318 if (token == start_token)
319 do_print = true;
321 if (!do_print)
322 continue;
324 nprinted++;
325 if (token == curr_token)
326 fprintf (file, "[[");
328 cp_lexer_print_token (file, token);
330 if (token == curr_token)
331 fprintf (file, "]]");
333 switch (token->type)
335 case CPP_SEMICOLON:
336 case CPP_OPEN_BRACE:
337 case CPP_CLOSE_BRACE:
338 case CPP_EOF:
339 fputc ('\n', file);
340 break;
342 default:
343 fputc (' ', file);
347 if (i == num && i < buffer->length ())
349 fprintf (file, " ... ");
350 cp_lexer_print_token (file, &buffer->last ());
353 fprintf (file, "\n");
357 /* Dump all tokens in BUFFER to stderr. */
359 void
360 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
362 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> &ref)
368 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
371 DEBUG_FUNCTION void
372 debug (vec<cp_token, va_gc> *ptr)
374 if (ptr)
375 debug (*ptr);
376 else
377 fprintf (stderr, "<nil>\n");
381 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
382 description for T. */
384 static void
385 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
387 if (t)
389 fprintf (file, "%s: ", desc);
390 print_node_brief (file, "", t, 0);
395 /* Dump parser context C to FILE. */
397 static void
398 cp_debug_print_context (FILE *file, cp_parser_context *c)
400 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
401 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
402 print_node_brief (file, "", c->object_type, 0);
403 fprintf (file, "}\n");
407 /* Print the stack of parsing contexts to FILE starting with FIRST. */
409 static void
410 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
412 unsigned i;
413 cp_parser_context *c;
415 fprintf (file, "Parsing context stack:\n");
416 for (i = 0, c = first; c; c = c->next, i++)
418 fprintf (file, "\t#%u: ", i);
419 cp_debug_print_context (file, c);
424 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
426 static void
427 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
429 if (flag)
430 fprintf (file, "%s: true\n", desc);
434 /* Print an unparsed function entry UF to FILE. */
436 static void
437 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
439 unsigned i;
440 cp_default_arg_entry *default_arg_fn;
441 tree fn;
443 fprintf (file, "\tFunctions with default args:\n");
444 for (i = 0;
445 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
446 i++)
448 fprintf (file, "\t\tClass type: ");
449 print_node_brief (file, "", default_arg_fn->class_type, 0);
450 fprintf (file, "\t\tDeclaration: ");
451 print_node_brief (file, "", default_arg_fn->decl, 0);
452 fprintf (file, "\n");
455 fprintf (file, "\n\tFunctions with definitions that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
464 fprintf (file, "\n\tNon-static data members with initializers that require "
465 "post-processing\n\t\t");
466 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
468 print_node_brief (file, "", fn, 0);
469 fprintf (file, " ");
471 fprintf (file, "\n");
475 /* Print the stack of unparsed member functions S to FILE. */
477 static void
478 cp_debug_print_unparsed_queues (FILE *file,
479 vec<cp_unparsed_functions_entry, va_gc> *s)
481 unsigned i;
482 cp_unparsed_functions_entry *uf;
484 fprintf (file, "Unparsed functions\n");
485 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
487 fprintf (file, "#%u:\n", i);
488 cp_debug_print_unparsed_function (file, uf);
493 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
494 the given PARSER. If FILE is NULL, the output is printed on stderr. */
496 static void
497 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
499 cp_token *next_token, *first_token, *start_token;
501 if (file == NULL)
502 file = stderr;
504 next_token = parser->lexer->next_token;
505 first_token = parser->lexer->buffer->address ();
506 start_token = (next_token > first_token + window_size / 2)
507 ? next_token - window_size / 2
508 : first_token;
509 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
510 next_token);
514 /* Dump debugging information for the given PARSER. If FILE is NULL,
515 the output is printed on stderr. */
517 void
518 cp_debug_parser (FILE *file, cp_parser *parser)
520 const size_t window_size = 20;
521 cp_token *token;
522 expanded_location eloc;
524 if (file == NULL)
525 file = stderr;
527 fprintf (file, "Parser state\n\n");
528 fprintf (file, "Number of tokens: %u\n",
529 vec_safe_length (parser->lexer->buffer));
530 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
531 cp_debug_print_tree_if_set (file, "Object scope",
532 parser->object_scope);
533 cp_debug_print_tree_if_set (file, "Qualifying scope",
534 parser->qualifying_scope);
535 cp_debug_print_context_stack (file, parser->context);
536 cp_debug_print_flag (file, "Allow GNU extensions",
537 parser->allow_gnu_extensions_p);
538 cp_debug_print_flag (file, "'>' token is greater-than",
539 parser->greater_than_is_operator_p);
540 cp_debug_print_flag (file, "Default args allowed in current "
541 "parameter list", parser->default_arg_ok_p);
542 cp_debug_print_flag (file, "Parsing integral constant-expression",
543 parser->integral_constant_expression_p);
544 cp_debug_print_flag (file, "Allow non-constant expression in current "
545 "constant-expression",
546 parser->allow_non_integral_constant_expression_p);
547 cp_debug_print_flag (file, "Seen non-constant expression",
548 parser->non_integral_constant_expression_p);
549 cp_debug_print_flag (file, "Local names forbidden in current context",
550 (parser->local_variables_forbidden_p
551 & LOCAL_VARS_FORBIDDEN));
552 cp_debug_print_flag (file, "'this' forbidden in current context",
553 (parser->local_variables_forbidden_p
554 & THIS_FORBIDDEN));
555 cp_debug_print_flag (file, "In unbraced linkage specification",
556 parser->in_unbraced_linkage_specification_p);
557 cp_debug_print_flag (file, "Parsing a declarator",
558 parser->in_declarator_p);
559 cp_debug_print_flag (file, "In template argument list",
560 parser->in_template_argument_list_p);
561 cp_debug_print_flag (file, "Parsing an iteration statement",
562 parser->in_statement & IN_ITERATION_STMT);
563 cp_debug_print_flag (file, "Parsing a switch statement",
564 parser->in_statement & IN_SWITCH_STMT);
565 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
566 parser->in_statement & IN_OMP_BLOCK);
567 cp_debug_print_flag (file, "Parsing an OpenMP loop",
568 parser->in_statement & IN_OMP_FOR);
569 cp_debug_print_flag (file, "Parsing an if statement",
570 parser->in_statement & IN_IF_STMT);
571 cp_debug_print_flag (file, "Parsing a type-id in an expression "
572 "context", parser->in_type_id_in_expr_p);
573 cp_debug_print_flag (file, "String expressions should be translated "
574 "to execution character set",
575 parser->translate_strings_p);
576 cp_debug_print_flag (file, "Parsing function body outside of a "
577 "local class", parser->in_function_body);
578 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
579 parser->colon_corrects_to_scope_p);
580 cp_debug_print_flag (file, "Colon doesn't start a class definition",
581 parser->colon_doesnt_start_class_def_p);
582 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
583 parser->objective_c_message_context_p);
584 if (parser->type_definition_forbidden_message)
585 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
586 parser->type_definition_forbidden_message,
587 parser->type_definition_forbidden_message_arg
588 ? parser->type_definition_forbidden_message_arg : "<none>");
589 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
590 fprintf (file, "Number of class definitions in progress: %u\n",
591 parser->num_classes_being_defined);
592 fprintf (file, "Number of template parameter lists for the current "
593 "declaration: %u\n", parser->num_template_parameter_lists);
594 cp_debug_parser_tokens (file, parser, window_size);
595 token = parser->lexer->next_token;
596 fprintf (file, "Next token to parse:\n");
597 fprintf (file, "\tToken: ");
598 cp_lexer_print_token (file, token);
599 eloc = expand_location (token->location);
600 fprintf (file, "\n\tFile: %s\n", eloc.file);
601 fprintf (file, "\tLine: %d\n", eloc.line);
602 fprintf (file, "\tColumn: %d\n", eloc.column);
605 DEBUG_FUNCTION void
606 debug (cp_parser &ref)
608 cp_debug_parser (stderr, &ref);
611 DEBUG_FUNCTION void
612 debug (cp_parser *ptr)
614 if (ptr)
615 debug (*ptr);
616 else
617 fprintf (stderr, "<nil>\n");
620 /* Allocate memory for a new lexer object and return it. */
622 static cp_lexer *
623 cp_lexer_alloc (void)
625 /* Allocate the memory. */
626 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
628 /* Initially we are not debugging. */
629 lexer->debugging_p = false;
631 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
633 /* Create the buffer. */
634 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
636 return lexer;
639 /* Return TRUE if token is the start of a module declaration that will be
640 terminated by a CPP_PRAGMA_EOL token. */
641 static inline bool
642 cp_token_is_module_directive (cp_token *token)
644 return token->keyword == RID__EXPORT
645 || token->keyword == RID__MODULE
646 || token->keyword == RID__IMPORT;
649 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
650 PRAGMA_NONE. */
652 static enum pragma_kind
653 cp_parser_pragma_kind (cp_token *token)
655 if (token->type != CPP_PRAGMA)
656 return PRAGMA_NONE;
657 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
658 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
661 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
662 during preprocessing for the case of preprocessing-related diagnostics. This
663 is called immediately after pushing the CPP_PRAGMA_EOL token onto
664 lexer->buffer. */
666 static void
667 cp_lexer_handle_early_pragma (cp_lexer *lexer)
669 const auto first_token = lexer->buffer->address ();
670 const auto last_token = first_token + lexer->buffer->length () - 1;
672 /* Back up to the start of the pragma so pragma_lex () can parse it when
673 c-pragma lib asks it to. */
674 auto begin = last_token;
675 gcc_assert (begin->type == CPP_PRAGMA_EOL);
676 while (begin->type != CPP_PRAGMA)
678 if (cp_token_is_module_directive (begin))
679 return;
680 gcc_assert (begin != first_token);
681 --begin;
683 gcc_assert (!lexer->next_token);
684 gcc_assert (!lexer->last_token);
685 lexer->next_token = begin;
686 lexer->last_token = last_token;
688 /* Dispatch it. */
689 const unsigned int id
690 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
691 if (id >= PRAGMA_FIRST_EXTERNAL)
692 c_invoke_early_pragma_handler (id);
694 /* Reset to normal state. */
695 lexer->next_token = lexer->last_token = nullptr;
698 /* The parser. */
699 static cp_parser *cp_parser_new (cp_lexer *);
700 static GTY (()) cp_parser *the_parser;
702 /* Create a new main C++ lexer, the lexer that gets tokens from the
703 preprocessor, and also create the main parser. */
705 static cp_lexer *
706 cp_lexer_new_main (void)
708 cp_token token;
710 /* It's possible that parsing the first pragma will load a PCH file,
711 which is a GC collection point. So we have to do that before
712 allocating any memory. */
713 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
714 cp_parser_initial_pragma (&token);
715 c_common_no_more_pch ();
717 cp_lexer *lexer = cp_lexer_alloc ();
718 /* Put the first token in the buffer. */
719 cp_token *tok = lexer->buffer->quick_push (token);
721 uintptr_t filter = 0;
722 if (modules_p ())
723 filter = module_token_cdtor (parse_in, filter);
725 /* Create the parser now, so we can use it to handle early pragmas. */
726 gcc_assert (!the_parser);
727 the_parser = cp_parser_new (lexer);
729 /* Get the remaining tokens from the preprocessor. */
730 while (tok->type != CPP_EOF)
732 if (filter)
733 /* Process the previous token. */
734 module_token_lang (tok->type, tok->keyword, tok->u.value,
735 tok->location, filter);
737 /* Check for early pragmas that need to be handled now. */
738 if (tok->type == CPP_PRAGMA_EOL)
739 cp_lexer_handle_early_pragma (lexer);
741 tok = vec_safe_push (lexer->buffer, cp_token ());
742 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
745 lexer->next_token = lexer->buffer->address ();
746 lexer->last_token = lexer->next_token
747 + lexer->buffer->length ()
748 - 1;
750 if (lexer->buffer->length () != 1)
752 /* Set the EOF token's location to be the just after the previous
753 token's range. That way 'at-eof' diagnostics point at something
754 meaninful. */
755 auto range = get_range_from_loc (line_table, tok[-1].location);
756 tok[0].location
757 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
760 if (filter)
761 module_token_cdtor (parse_in, filter);
763 /* Subsequent preprocessor diagnostics should use compiler
764 diagnostic functions to get the compiler source location. */
765 override_libcpp_locations = true;
767 maybe_check_all_macros (parse_in);
769 gcc_assert (!lexer->next_token->purged_p);
770 return lexer;
773 /* Create a lexer and parser to be used during preprocess-only mode.
774 This will be filled with tokens to parse when needed by pragma_lex (). */
775 void
776 c_init_preprocess ()
778 gcc_assert (!the_parser);
779 the_parser = cp_parser_new (cp_lexer_alloc ());
782 /* Create a new lexer whose token stream is primed with the tokens in
783 CACHE. When these tokens are exhausted, no new tokens will be read. */
785 static cp_lexer *
786 cp_lexer_new_from_tokens (cp_token_cache *cache)
788 cp_token *first = cache->first;
789 cp_token *last = cache->last;
790 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
792 /* We do not own the buffer. */
793 lexer->buffer = NULL;
795 /* Insert an EOF token. */
796 lexer->saved_type = last->type;
797 lexer->saved_keyword = last->keyword;
798 last->type = CPP_EOF;
799 last->keyword = RID_MAX;
801 lexer->next_token = first;
802 lexer->last_token = last;
804 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
806 /* Initially we are not debugging. */
807 lexer->debugging_p = false;
809 gcc_assert (!lexer->next_token->purged_p
810 && !lexer->last_token->purged_p);
811 return lexer;
814 /* Frees all resources associated with LEXER. */
816 static void
817 cp_lexer_destroy (cp_lexer *lexer)
819 if (lexer->buffer)
820 vec_free (lexer->buffer);
821 else
823 /* Restore the token we overwrite with EOF. */
824 lexer->last_token->type = lexer->saved_type;
825 lexer->last_token->keyword = lexer->saved_keyword;
827 lexer->saved_tokens.release ();
828 ggc_free (lexer);
831 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
832 be used. The point of this flag is to help the compiler to fold away calls
833 to cp_lexer_debugging_p within this source file at compile time, when the
834 lexer is not being debugged. */
836 #define LEXER_DEBUGGING_ENABLED_P false
838 /* Returns nonzero if debugging information should be output. */
840 static inline bool
841 cp_lexer_debugging_p (cp_lexer *lexer)
843 if (!LEXER_DEBUGGING_ENABLED_P)
844 return false;
846 return lexer->debugging_p;
850 static inline cp_token_position
851 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
853 return lexer->next_token - previous_p;
856 static inline cp_token *
857 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
859 return pos;
862 static inline void
863 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
865 lexer->next_token = cp_lexer_token_at (lexer, pos);
868 static inline cp_token_position
869 cp_lexer_previous_token_position (cp_lexer *lexer)
871 return cp_lexer_token_position (lexer, true);
874 static inline cp_token *
875 cp_lexer_previous_token (cp_lexer *lexer)
877 cp_token_position tp = cp_lexer_previous_token_position (lexer);
879 /* Skip past purged tokens. */
880 while (tp->purged_p)
882 gcc_assert (tp != vec_safe_address (lexer->buffer));
883 tp--;
886 return cp_lexer_token_at (lexer, tp);
889 /* Same as above, but return NULL when the lexer doesn't own the token
890 buffer or if the next_token is at the start of the token
891 vector or if all previous tokens are purged. */
893 static cp_token *
894 cp_lexer_safe_previous_token (cp_lexer *lexer)
896 if (lexer->buffer
897 && lexer->next_token != lexer->buffer->address ())
899 cp_token_position tp = cp_lexer_previous_token_position (lexer);
901 /* Skip past purged tokens. */
902 while (tp->purged_p)
904 if (tp == lexer->buffer->address ())
905 return NULL;
906 tp--;
908 return cp_lexer_token_at (lexer, tp);
911 return NULL;
914 /* Overload for make_location, taking the lexer to mean the location of the
915 previous token. */
917 static inline location_t
918 make_location (location_t caret, location_t start, cp_lexer *lexer)
920 cp_token *t = cp_lexer_previous_token (lexer);
921 return make_location (caret, start, t->location);
924 /* Overload for make_location taking tokens instead of locations. */
926 static inline location_t
927 make_location (cp_token *caret, cp_token *start, cp_token *end)
929 return make_location (caret->location, start->location, end->location);
932 /* nonzero if we are presently saving tokens. */
934 static inline int
935 cp_lexer_saving_tokens (const cp_lexer* lexer)
937 return lexer->saved_tokens.length () != 0;
940 /* Store the next token from the preprocessor in *TOKEN. Return true
941 if we reach EOF. If LEXER is NULL, assume we are handling an
942 initial #pragma pch_preprocess, and thus want the lexer to return
943 processed strings.
945 Diagnostics issued from this function must have their controlling option (if
946 any) in c.opt annotated as a libcpp option via the CppReason property. */
948 static void
949 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
951 static int is_extern_c = 0;
953 /* Get a new token from the preprocessor. */
954 token->type
955 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
956 flags);
957 token->keyword = RID_MAX;
958 token->purged_p = false;
959 token->error_reported = false;
960 token->tree_check_p = false;
961 /* Usually never see a zero, but just in case ... */
962 token->main_source_p = line_table->depth <= 1;
964 /* On some systems, some header files are surrounded by an
965 implicit extern "C" block. Set a flag in the token if it
966 comes from such a header. */
967 is_extern_c += pending_lang_change;
968 pending_lang_change = 0;
969 token->implicit_extern_c = is_extern_c > 0;
971 /* Check to see if this token is a keyword. */
972 if (token->type == CPP_NAME)
974 if (IDENTIFIER_KEYWORD_P (token->u.value))
976 /* Mark this token as a keyword. */
977 token->type = CPP_KEYWORD;
978 /* Record which keyword. */
979 token->keyword = C_RID_CODE (token->u.value);
981 else
983 if (warn_cxx11_compat
984 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
985 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
986 /* These are outside the CXX11 range. */
987 || C_RID_CODE (token->u.value) == RID_ALIGNOF
988 || C_RID_CODE (token->u.value) == RID_ALIGNAS
989 || C_RID_CODE (token->u.value)== RID_THREAD))
991 /* Warn about the C++11 keyword (but still treat it as
992 an identifier). */
993 warning_at (token->location, OPT_Wc__11_compat,
994 "identifier %qE is a keyword in C++11",
995 token->u.value);
997 /* Clear out the C_RID_CODE so we don't warn about this
998 particular identifier-turned-keyword again. */
999 C_SET_RID_CODE (token->u.value, RID_MAX);
1001 if (warn_cxx20_compat
1002 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1003 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1005 /* Warn about the C++20 keyword (but still treat it as
1006 an identifier). */
1007 warning_at (token->location, OPT_Wc__20_compat,
1008 "identifier %qE is a keyword in C++20",
1009 token->u.value);
1011 /* Clear out the C_RID_CODE so we don't warn about this
1012 particular identifier-turned-keyword again. */
1013 C_SET_RID_CODE (token->u.value, RID_MAX);
1016 token->keyword = RID_MAX;
1019 else if (token->type == CPP_AT_NAME)
1021 /* This only happens in Objective-C++; it must be a keyword. */
1022 token->type = CPP_KEYWORD;
1023 switch (C_RID_CODE (token->u.value))
1025 /* Replace 'class' with '@class', 'private' with '@private',
1026 etc. This prevents confusion with the C++ keyword
1027 'class', and makes the tokens consistent with other
1028 Objective-C 'AT' keywords. For example '@class' is
1029 reported as RID_AT_CLASS which is consistent with
1030 '@synchronized', which is reported as
1031 RID_AT_SYNCHRONIZED.
1033 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1034 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1035 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1036 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1037 case RID_THROW: token->keyword = RID_AT_THROW; break;
1038 case RID_TRY: token->keyword = RID_AT_TRY; break;
1039 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1040 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1041 default: token->keyword = C_RID_CODE (token->u.value);
1046 /* Update the globals input_location and the input file stack from TOKEN. */
1047 static inline void
1048 cp_lexer_set_source_position_from_token (cp_token *token)
1050 input_location = token->location;
1053 /* Update the globals input_location and the input file stack from LEXER. */
1054 static inline void
1055 cp_lexer_set_source_position (cp_lexer *lexer)
1057 cp_token *token = cp_lexer_peek_token (lexer);
1058 cp_lexer_set_source_position_from_token (token);
1061 /* Return a pointer to the next token in the token stream, but do not
1062 consume it. */
1064 static inline cp_token *
1065 cp_lexer_peek_token (cp_lexer *lexer)
1067 if (cp_lexer_debugging_p (lexer))
1069 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1070 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1071 putc ('\n', cp_lexer_debug_stream);
1073 return lexer->next_token;
1076 /* Return true if the next token has the indicated TYPE. */
1078 static inline bool
1079 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1081 return cp_lexer_peek_token (lexer)->type == type;
1084 /* Return true if the next token does not have the indicated TYPE. */
1086 static inline bool
1087 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1089 return !cp_lexer_next_token_is (lexer, type);
1092 /* Return true if the next token is the indicated KEYWORD. */
1094 static inline bool
1095 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1097 return cp_lexer_peek_token (lexer)->keyword == keyword;
1100 static inline bool
1101 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1103 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1106 static inline bool
1107 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1109 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1112 /* Return true if KEYWORD can start a decl-specifier. */
1114 bool
1115 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1117 switch (keyword)
1119 /* auto specifier: storage-class-specifier in C++,
1120 simple-type-specifier in C++0x. */
1121 case RID_AUTO:
1122 /* Storage classes. */
1123 case RID_REGISTER:
1124 case RID_STATIC:
1125 case RID_EXTERN:
1126 case RID_MUTABLE:
1127 case RID_THREAD:
1128 /* Elaborated type specifiers. */
1129 case RID_ENUM:
1130 case RID_CLASS:
1131 case RID_STRUCT:
1132 case RID_UNION:
1133 case RID_TYPENAME:
1134 /* Simple type specifiers. */
1135 case RID_CHAR:
1136 case RID_CHAR8:
1137 case RID_CHAR16:
1138 case RID_CHAR32:
1139 case RID_WCHAR:
1140 case RID_BOOL:
1141 case RID_SHORT:
1142 case RID_INT:
1143 case RID_LONG:
1144 case RID_SIGNED:
1145 case RID_UNSIGNED:
1146 case RID_FLOAT:
1147 case RID_DOUBLE:
1148 CASE_RID_FLOATN_NX:
1149 case RID_VOID:
1150 /* CV qualifiers. */
1151 case RID_CONST:
1152 case RID_VOLATILE:
1153 /* Function specifiers. */
1154 case RID_EXPLICIT:
1155 case RID_VIRTUAL:
1156 /* friend/typdef/inline specifiers. */
1157 case RID_FRIEND:
1158 case RID_TYPEDEF:
1159 case RID_INLINE:
1160 /* GNU extensions. */
1161 case RID_TYPEOF:
1162 /* C++11 extensions. */
1163 case RID_DECLTYPE:
1164 case RID_CONSTEXPR:
1165 /* C++20 extensions. */
1166 case RID_CONSTINIT:
1167 case RID_CONSTEVAL:
1168 return true;
1170 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
1171 case RID_##CODE:
1172 #include "cp-trait.def"
1173 #undef DEFTRAIT_TYPE
1174 return true;
1176 default:
1177 if (keyword >= RID_FIRST_INT_N
1178 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1179 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1180 return true;
1181 return false;
1185 /* Return true if the next token is a keyword for a decl-specifier. */
1187 static bool
1188 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1190 cp_token *token;
1192 token = cp_lexer_peek_token (lexer);
1193 return cp_keyword_starts_decl_specifier_p (token->keyword);
1196 /* Returns TRUE iff the token T begins a decltype type. */
1198 static bool
1199 token_is_decltype (cp_token *t)
1201 return (t->keyword == RID_DECLTYPE
1202 || t->type == CPP_DECLTYPE);
1205 /* Returns TRUE iff the next token begins a decltype type. */
1207 static bool
1208 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1210 cp_token *t = cp_lexer_peek_token (lexer);
1211 return token_is_decltype (t);
1214 /* Called when processing a token with tree_check_value; perform or defer the
1215 associated checks and return the value. */
1217 static tree
1218 saved_checks_value (struct tree_check *check_value)
1220 /* Perform any access checks that were deferred. */
1221 vec<deferred_access_check, va_gc> *checks;
1222 deferred_access_check *chk;
1223 checks = check_value->checks;
1224 if (checks)
1226 int i;
1227 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1228 perform_or_defer_access_check (chk->binfo,
1229 chk->decl,
1230 chk->diag_decl, tf_warning_or_error);
1232 /* Return the stored value. */
1233 return check_value->value;
1236 /* Return a pointer to the Nth token in the token stream. If N is 1,
1237 then this is precisely equivalent to cp_lexer_peek_token (except
1238 that it is not inline). One would like to disallow that case, but
1239 there is one case (cp_parser_nth_token_starts_template_id) where
1240 the caller passes a variable for N and it might be 1. */
1242 static cp_token *
1243 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1245 cp_token *token;
1247 /* N is 1-based, not zero-based. */
1248 gcc_assert (n > 0);
1250 if (cp_lexer_debugging_p (lexer))
1251 fprintf (cp_lexer_debug_stream,
1252 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1254 --n;
1255 token = lexer->next_token;
1256 while (n && token->type != CPP_EOF)
1258 ++token;
1259 if (!token->purged_p)
1260 --n;
1263 if (cp_lexer_debugging_p (lexer))
1265 cp_lexer_print_token (cp_lexer_debug_stream, token);
1266 putc ('\n', cp_lexer_debug_stream);
1269 return token;
1272 /* Return the next token, and advance the lexer's next_token pointer
1273 to point to the next non-purged token. */
1275 static cp_token *
1276 cp_lexer_consume_token (cp_lexer* lexer)
1278 cp_token *token = lexer->next_token;
1282 gcc_assert (token->type != CPP_EOF);
1283 lexer->next_token++;
1285 while (lexer->next_token->purged_p);
1287 cp_lexer_set_source_position_from_token (token);
1289 /* Provide debugging output. */
1290 if (cp_lexer_debugging_p (lexer))
1292 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1293 cp_lexer_print_token (cp_lexer_debug_stream, token);
1294 putc ('\n', cp_lexer_debug_stream);
1297 return token;
1300 /* Permanently remove the next token from the token stream, and
1301 advance the next_token pointer to refer to the next non-purged
1302 token. */
1304 static void
1305 cp_lexer_purge_token (cp_lexer *lexer)
1307 cp_token *tok = lexer->next_token;
1309 gcc_assert (tok->type != CPP_EOF);
1310 tok->purged_p = true;
1311 tok->location = UNKNOWN_LOCATION;
1312 tok->u.value = NULL_TREE;
1313 tok->keyword = RID_MAX;
1316 tok++;
1317 while (tok->purged_p);
1318 lexer->next_token = tok;
1321 /* Permanently remove all tokens after TOK, up to, but not
1322 including, the token that will be returned next by
1323 cp_lexer_peek_token. */
1325 static void
1326 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1328 cp_token *peek = lexer->next_token;
1330 gcc_assert (tok < peek);
1332 for (tok++; tok != peek; tok++)
1334 tok->purged_p = true;
1335 tok->location = UNKNOWN_LOCATION;
1336 tok->u.value = NULL_TREE;
1337 tok->keyword = RID_MAX;
1341 /* Begin saving tokens. All tokens consumed after this point will be
1342 preserved. */
1344 static void
1345 cp_lexer_save_tokens (cp_lexer* lexer)
1347 /* Provide debugging output. */
1348 if (cp_lexer_debugging_p (lexer))
1349 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1351 lexer->saved_tokens.safe_push (lexer->next_token);
1354 /* Commit to the portion of the token stream most recently saved. */
1356 static void
1357 cp_lexer_commit_tokens (cp_lexer* lexer)
1359 /* Provide debugging output. */
1360 if (cp_lexer_debugging_p (lexer))
1361 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1363 lexer->saved_tokens.pop ();
1366 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1367 to the token stream. Stop saving tokens. */
1369 static void
1370 cp_lexer_rollback_tokens (cp_lexer* lexer)
1372 /* Provide debugging output. */
1373 if (cp_lexer_debugging_p (lexer))
1374 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1376 lexer->next_token = lexer->saved_tokens.pop ();
1379 /* Determines what saved_token_sentinel does when going out of scope. */
1381 enum saved_token_sentinel_mode {
1382 STS_COMMIT,
1383 STS_ROLLBACK,
1384 STS_DONOTHING
1387 /* RAII wrapper around the above functions, with sanity checking (the token
1388 stream should be the same at the point of instantiation as it is at the
1389 point of destruction).
1391 Creating a variable saves tokens. MODE determines what happens when the
1392 object is destroyed. STS_COMMIT commits tokens (default),
1393 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1394 rollback() will immediately roll-back tokens and set MODE to
1395 STS_DONOTHING. */
1397 struct saved_token_sentinel
1399 cp_lexer *lexer;
1400 unsigned len;
1401 saved_token_sentinel_mode mode;
1402 saved_token_sentinel (cp_lexer *_lexer,
1403 saved_token_sentinel_mode _mode = STS_COMMIT)
1404 : lexer (_lexer), mode (_mode)
1406 len = lexer->saved_tokens.length ();
1407 cp_lexer_save_tokens (lexer);
1409 void rollback ()
1411 cp_lexer_rollback_tokens (lexer);
1412 cp_lexer_set_source_position_from_token
1413 (cp_lexer_previous_token (lexer));
1414 mode = STS_DONOTHING;
1416 ~saved_token_sentinel ()
1418 if (mode == STS_COMMIT)
1419 cp_lexer_commit_tokens (lexer);
1420 else if (mode == STS_ROLLBACK)
1421 rollback ();
1423 gcc_assert (lexer->saved_tokens.length () == len);
1427 /* Print a representation of the TOKEN on the STREAM. */
1429 static void
1430 cp_lexer_print_token (FILE * stream, cp_token *token)
1432 /* We don't use cpp_type2name here because the parser defines
1433 a few tokens of its own. */
1434 static const char *const token_names[] = {
1435 /* cpplib-defined token types */
1436 #define OP(e, s) #e,
1437 #define TK(e, s) #e,
1438 TTYPE_TABLE
1439 #undef OP
1440 #undef TK
1441 /* C++ parser token types - see "Manifest constants", above. */
1442 "KEYWORD",
1443 "TEMPLATE_ID",
1444 "NESTED_NAME_SPECIFIER",
1447 /* For some tokens, print the associated data. */
1448 switch (token->type)
1450 case CPP_KEYWORD:
1451 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1452 For example, `struct' is mapped to an INTEGER_CST. */
1453 if (!identifier_p (token->u.value))
1454 break;
1455 /* fall through */
1456 case CPP_NAME:
1457 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1458 break;
1460 case CPP_STRING:
1461 case CPP_STRING16:
1462 case CPP_STRING32:
1463 case CPP_WSTRING:
1464 case CPP_UTF8STRING:
1465 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1466 break;
1468 case CPP_NUMBER:
1469 print_generic_expr (stream, token->u.value);
1470 break;
1472 default:
1473 /* If we have a name for the token, print it out. Otherwise, we
1474 simply give the numeric code. */
1475 if (token->type < ARRAY_SIZE(token_names))
1476 fputs (token_names[token->type], stream);
1477 else
1478 fprintf (stream, "[%d]", token->type);
1479 break;
1483 DEBUG_FUNCTION void
1484 debug (cp_token &ref)
1486 cp_lexer_print_token (stderr, &ref);
1487 fprintf (stderr, "\n");
1490 DEBUG_FUNCTION void
1491 debug (cp_token *ptr)
1493 if (ptr)
1494 debug (*ptr);
1495 else
1496 fprintf (stderr, "<nil>\n");
1500 /* Start emitting debugging information. */
1502 static void
1503 cp_lexer_start_debugging (cp_lexer* lexer)
1505 if (!LEXER_DEBUGGING_ENABLED_P)
1506 fatal_error (input_location,
1507 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1509 lexer->debugging_p = true;
1510 cp_lexer_debug_stream = stderr;
1513 /* Stop emitting debugging information. */
1515 static void
1516 cp_lexer_stop_debugging (cp_lexer* lexer)
1518 if (!LEXER_DEBUGGING_ENABLED_P)
1519 fatal_error (input_location,
1520 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1522 lexer->debugging_p = false;
1523 cp_lexer_debug_stream = NULL;
1526 /* Create a new cp_token_cache, representing a range of tokens. */
1528 static cp_token_cache *
1529 cp_token_cache_new (cp_token *first, cp_token *last)
1531 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1532 cache->first = first;
1533 cache->last = last;
1534 return cache;
1537 /* Diagnose if #pragma omp declare simd isn't followed immediately
1538 by function declaration or definition. */
1540 static inline void
1541 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1543 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1545 error ("%<#pragma omp declare %s%> not immediately followed by "
1546 "function declaration or definition",
1547 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1548 parser->omp_declare_simd = NULL;
1552 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1553 and put that into "omp declare simd" attribute. */
1555 static inline void
1556 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1558 if (UNLIKELY (parser->omp_declare_simd != NULL))
1560 if (fndecl == error_mark_node)
1562 parser->omp_declare_simd = NULL;
1563 return;
1565 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1567 cp_ensure_no_omp_declare_simd (parser);
1568 return;
1573 /* Similarly, but for use in declaration parsing functions
1574 which call cp_parser_handle_directive_omp_attributes. */
1576 static inline void
1577 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1579 if (parser->omp_declare_simd != data)
1580 return;
1582 if (!parser->omp_declare_simd->error_seen
1583 && !parser->omp_declare_simd->fndecl_seen)
1584 error_at (parser->omp_declare_simd->loc,
1585 "%<declare %s%> directive not immediately followed by "
1586 "function declaration or definition",
1587 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1588 parser->omp_declare_simd = NULL;
1591 /* Diagnose if #pragma acc routine isn't followed immediately by function
1592 declaration or definition. */
1594 static inline void
1595 cp_ensure_no_oacc_routine (cp_parser *parser)
1597 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1599 error_at (parser->oacc_routine->loc,
1600 "%<#pragma acc routine%> not immediately followed by "
1601 "function declaration or definition");
1602 parser->oacc_routine = NULL;
1606 /* Decl-specifiers. */
1608 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1610 static void
1611 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1613 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1616 /* Declarators. */
1618 /* Nothing other than the parser should be creating declarators;
1619 declarators are a semi-syntactic representation of C++ entities.
1620 Other parts of the front end that need to create entities (like
1621 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1623 static cp_declarator *make_call_declarator
1624 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1625 tree, tree, tree, tree, tree, location_t);
1626 static cp_declarator *make_array_declarator
1627 (cp_declarator *, tree);
1628 static cp_declarator *make_pointer_declarator
1629 (cp_cv_quals, cp_declarator *, tree);
1630 static cp_declarator *make_reference_declarator
1631 (cp_cv_quals, cp_declarator *, bool, tree);
1632 static cp_declarator *make_ptrmem_declarator
1633 (cp_cv_quals, tree, cp_declarator *, tree);
1635 /* An erroneous declarator. */
1636 static cp_declarator *cp_error_declarator;
1638 /* The obstack on which declarators and related data structures are
1639 allocated. */
1640 static struct obstack declarator_obstack;
1642 /* Alloc BYTES from the declarator memory pool. */
1644 static inline void *
1645 alloc_declarator (size_t bytes)
1647 return obstack_alloc (&declarator_obstack, bytes);
1650 /* Allocate a declarator of the indicated KIND. Clear fields that are
1651 common to all declarators. */
1653 static cp_declarator *
1654 make_declarator (cp_declarator_kind kind)
1656 cp_declarator *declarator;
1658 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1659 declarator->kind = kind;
1660 declarator->parenthesized = UNKNOWN_LOCATION;
1661 declarator->attributes = NULL_TREE;
1662 declarator->std_attributes = NULL_TREE;
1663 declarator->declarator = NULL;
1664 declarator->parameter_pack_p = false;
1665 declarator->id_loc = UNKNOWN_LOCATION;
1666 declarator->init_loc = UNKNOWN_LOCATION;
1668 return declarator;
1671 /* Make a declarator for a generalized identifier. If
1672 QUALIFYING_SCOPE is non-NULL, the identifier is
1673 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1674 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1675 is, if any. */
1677 static cp_declarator *
1678 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1679 special_function_kind sfk, location_t id_location)
1681 cp_declarator *declarator;
1683 /* It is valid to write:
1685 class C { void f(); };
1686 typedef C D;
1687 void D::f();
1689 The standard is not clear about whether `typedef const C D' is
1690 legal; as of 2002-09-15 the committee is considering that
1691 question. EDG 3.0 allows that syntax. Therefore, we do as
1692 well. */
1693 if (qualifying_scope && TYPE_P (qualifying_scope))
1694 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1696 gcc_assert (identifier_p (unqualified_name)
1697 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1698 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1700 declarator = make_declarator (cdk_id);
1701 declarator->u.id.qualifying_scope = qualifying_scope;
1702 declarator->u.id.unqualified_name = unqualified_name;
1703 declarator->u.id.sfk = sfk;
1704 declarator->id_loc = id_location;
1706 return declarator;
1709 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1710 of modifiers such as const or volatile to apply to the pointer
1711 type, represented as identifiers. ATTRIBUTES represent the attributes that
1712 appertain to the pointer or reference. */
1714 cp_declarator *
1715 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1716 tree attributes)
1718 cp_declarator *declarator;
1720 declarator = make_declarator (cdk_pointer);
1721 declarator->declarator = target;
1722 declarator->u.pointer.qualifiers = cv_qualifiers;
1723 declarator->u.pointer.class_type = NULL_TREE;
1724 if (target)
1726 declarator->id_loc = target->id_loc;
1727 declarator->parameter_pack_p = target->parameter_pack_p;
1728 target->parameter_pack_p = false;
1730 else
1731 declarator->parameter_pack_p = false;
1733 declarator->std_attributes = attributes;
1735 return declarator;
1738 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1739 represent the attributes that appertain to the pointer or
1740 reference. */
1742 cp_declarator *
1743 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1744 bool rvalue_ref, tree attributes)
1746 cp_declarator *declarator;
1748 declarator = make_declarator (cdk_reference);
1749 declarator->declarator = target;
1750 declarator->u.reference.qualifiers = cv_qualifiers;
1751 declarator->u.reference.rvalue_ref = rvalue_ref;
1752 if (target)
1754 declarator->id_loc = target->id_loc;
1755 declarator->parameter_pack_p = target->parameter_pack_p;
1756 target->parameter_pack_p = false;
1758 else
1759 declarator->parameter_pack_p = false;
1761 declarator->std_attributes = attributes;
1763 return declarator;
1766 /* Like make_pointer_declarator -- but for a pointer to a non-static
1767 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1768 appertain to the pointer or reference. */
1770 cp_declarator *
1771 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1772 cp_declarator *pointee,
1773 tree attributes)
1775 cp_declarator *declarator;
1777 declarator = make_declarator (cdk_ptrmem);
1778 declarator->declarator = pointee;
1779 declarator->u.pointer.qualifiers = cv_qualifiers;
1780 declarator->u.pointer.class_type = class_type;
1782 if (pointee)
1784 declarator->parameter_pack_p = pointee->parameter_pack_p;
1785 pointee->parameter_pack_p = false;
1787 else
1788 declarator->parameter_pack_p = false;
1790 declarator->std_attributes = attributes;
1792 return declarator;
1795 /* Make a declarator for the function given by TARGET, with the
1796 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1797 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1798 indicates what exceptions can be thrown. STD_ATTRS contains
1799 attributes that appertain to the function type. */
1801 cp_declarator *
1802 make_call_declarator (cp_declarator *target,
1803 tree parms,
1804 cp_cv_quals cv_qualifiers,
1805 cp_virt_specifiers virt_specifiers,
1806 cp_ref_qualifier ref_qualifier,
1807 tree tx_qualifier,
1808 tree exception_specification,
1809 tree late_return_type,
1810 tree requires_clause,
1811 tree std_attrs,
1812 location_t parens_loc)
1814 cp_declarator *declarator;
1816 declarator = make_declarator (cdk_function);
1817 declarator->declarator = target;
1818 declarator->u.function.parameters = parms;
1819 declarator->u.function.qualifiers = cv_qualifiers;
1820 declarator->u.function.virt_specifiers = virt_specifiers;
1821 declarator->u.function.ref_qualifier = ref_qualifier;
1822 declarator->u.function.tx_qualifier = tx_qualifier;
1823 declarator->u.function.exception_specification = exception_specification;
1824 declarator->u.function.late_return_type = late_return_type;
1825 declarator->u.function.requires_clause = requires_clause;
1826 declarator->u.function.parens_loc = parens_loc;
1827 if (target)
1829 declarator->id_loc = target->id_loc;
1830 declarator->parameter_pack_p = target->parameter_pack_p;
1831 target->parameter_pack_p = false;
1833 else
1834 declarator->parameter_pack_p = false;
1836 declarator->std_attributes = std_attrs;
1838 return declarator;
1841 /* Make a declarator for an array of BOUNDS elements, each of which is
1842 defined by ELEMENT. */
1844 cp_declarator *
1845 make_array_declarator (cp_declarator *element, tree bounds)
1847 cp_declarator *declarator;
1849 declarator = make_declarator (cdk_array);
1850 declarator->declarator = element;
1851 declarator->u.array.bounds = bounds;
1852 if (element)
1854 declarator->id_loc = element->id_loc;
1855 declarator->parameter_pack_p = element->parameter_pack_p;
1856 element->parameter_pack_p = false;
1858 else
1859 declarator->parameter_pack_p = false;
1861 return declarator;
1864 /* Determine whether the declarator we've seen so far can be a
1865 parameter pack, when followed by an ellipsis. */
1866 static bool
1867 declarator_can_be_parameter_pack (cp_declarator *declarator)
1869 if (declarator && declarator->parameter_pack_p)
1870 /* We already saw an ellipsis. */
1871 return false;
1873 /* Search for a declarator name, or any other declarator that goes
1874 after the point where the ellipsis could appear in a parameter
1875 pack. If we find any of these, then this declarator cannot be
1876 made into a parameter pack. */
1877 bool found = false;
1878 while (declarator && !found)
1880 switch ((int)declarator->kind)
1882 case cdk_id:
1883 case cdk_array:
1884 case cdk_decomp:
1885 found = true;
1886 break;
1888 case cdk_error:
1889 return true;
1891 default:
1892 declarator = declarator->declarator;
1893 break;
1897 return !found;
1900 cp_parameter_declarator *no_parameters;
1902 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1903 DECLARATOR and DEFAULT_ARGUMENT. */
1905 cp_parameter_declarator *
1906 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1907 cp_declarator *declarator,
1908 tree default_argument,
1909 location_t loc,
1910 bool template_parameter_pack_p = false)
1912 cp_parameter_declarator *parameter;
1914 parameter = ((cp_parameter_declarator *)
1915 alloc_declarator (sizeof (cp_parameter_declarator)));
1916 parameter->next = NULL;
1917 if (decl_specifiers)
1918 parameter->decl_specifiers = *decl_specifiers;
1919 else
1920 clear_decl_specs (&parameter->decl_specifiers);
1921 parameter->declarator = declarator;
1922 parameter->default_argument = default_argument;
1923 parameter->template_parameter_pack_p = template_parameter_pack_p;
1924 parameter->loc = loc;
1926 return parameter;
1929 /* Returns true iff DECLARATOR is a declaration for a function. */
1931 static bool
1932 function_declarator_p (const cp_declarator *declarator)
1934 while (declarator)
1936 if (declarator->kind == cdk_function
1937 && declarator->declarator->kind == cdk_id)
1938 return true;
1939 if (declarator->kind == cdk_id
1940 || declarator->kind == cdk_decomp
1941 || declarator->kind == cdk_error)
1942 return false;
1943 declarator = declarator->declarator;
1945 return false;
1948 /* The parser. */
1950 /* Overview
1951 --------
1953 A cp_parser parses the token stream as specified by the C++
1954 grammar. Its job is purely parsing, not semantic analysis. For
1955 example, the parser breaks the token stream into declarators,
1956 expressions, statements, and other similar syntactic constructs.
1957 It does not check that the types of the expressions on either side
1958 of an assignment-statement are compatible, or that a function is
1959 not declared with a parameter of type `void'.
1961 The parser invokes routines elsewhere in the compiler to perform
1962 semantic analysis and to build up the abstract syntax tree for the
1963 code processed.
1965 The parser (and the template instantiation code, which is, in a
1966 way, a close relative of parsing) are the only parts of the
1967 compiler that should be calling push_scope and pop_scope, or
1968 related functions. The parser (and template instantiation code)
1969 keeps track of what scope is presently active; everything else
1970 should simply honor that. (The code that generates static
1971 initializers may also need to set the scope, in order to check
1972 access control correctly when emitting the initializers.)
1974 Methodology
1975 -----------
1977 The parser is of the standard recursive-descent variety. Upcoming
1978 tokens in the token stream are examined in order to determine which
1979 production to use when parsing a non-terminal. Some C++ constructs
1980 require arbitrary look ahead to disambiguate. For example, it is
1981 impossible, in the general case, to tell whether a statement is an
1982 expression or declaration without scanning the entire statement.
1983 Therefore, the parser is capable of "parsing tentatively." When the
1984 parser is not sure what construct comes next, it enters this mode.
1985 Then, while we attempt to parse the construct, the parser queues up
1986 error messages, rather than issuing them immediately, and saves the
1987 tokens it consumes. If the construct is parsed successfully, the
1988 parser "commits", i.e., it issues any queued error messages and
1989 the tokens that were being preserved are permanently discarded.
1990 If, however, the construct is not parsed successfully, the parser
1991 rolls back its state completely so that it can resume parsing using
1992 a different alternative.
1994 Future Improvements
1995 -------------------
1997 The performance of the parser could probably be improved substantially.
1998 We could often eliminate the need to parse tentatively by looking ahead
1999 a little bit. In some places, this approach might not entirely eliminate
2000 the need to parse tentatively, but it might still speed up the average
2001 case. */
2003 /* Flags that are passed to some parsing functions. These values can
2004 be bitwise-ored together. */
2006 enum
2008 /* No flags. */
2009 CP_PARSER_FLAGS_NONE = 0x0,
2010 /* The construct is optional. If it is not present, then no error
2011 should be issued. */
2012 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2013 /* When parsing a type-specifier, treat user-defined type-names
2014 as non-type identifiers. */
2015 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2016 /* When parsing a type-specifier, do not try to parse a class-specifier
2017 or enum-specifier. */
2018 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2019 /* When parsing a decl-specifier-seq, only allow type-specifier or
2020 constexpr. */
2021 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2022 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2023 for C++20 consteval or for C++23 static. */
2024 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2025 /* When parsing a decl-specifier-seq, allow missing typename. */
2026 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2027 /* When parsing of the noexcept-specifier should be delayed. */
2028 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2029 /* When parsing a consteval declarator. */
2030 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2033 /* This type is used for parameters and variables which hold
2034 combinations of the above flags. */
2035 typedef int cp_parser_flags;
2037 /* The different kinds of declarators we want to parse. */
2039 enum cp_parser_declarator_kind
2041 /* We want an abstract declarator. */
2042 CP_PARSER_DECLARATOR_ABSTRACT,
2043 /* We want a named declarator. */
2044 CP_PARSER_DECLARATOR_NAMED,
2045 /* We don't mind, but the name must be an unqualified-id. */
2046 CP_PARSER_DECLARATOR_EITHER
2049 /* The precedence values used to parse binary expressions. The minimum value
2050 of PREC must be 1, because zero is reserved to quickly discriminate
2051 binary operators from other tokens. */
2053 enum cp_parser_prec
2055 PREC_NOT_OPERATOR,
2056 PREC_LOGICAL_OR_EXPRESSION,
2057 PREC_LOGICAL_AND_EXPRESSION,
2058 PREC_INCLUSIVE_OR_EXPRESSION,
2059 PREC_EXCLUSIVE_OR_EXPRESSION,
2060 PREC_AND_EXPRESSION,
2061 PREC_EQUALITY_EXPRESSION,
2062 PREC_RELATIONAL_EXPRESSION,
2063 PREC_SPACESHIP_EXPRESSION,
2064 PREC_SHIFT_EXPRESSION,
2065 PREC_ADDITIVE_EXPRESSION,
2066 PREC_MULTIPLICATIVE_EXPRESSION,
2067 PREC_PM_EXPRESSION,
2068 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2071 /* A mapping from a token type to a corresponding tree node type, with a
2072 precedence value. */
2074 struct cp_parser_binary_operations_map_node
2076 /* The token type. */
2077 enum cpp_ttype token_type;
2078 /* The corresponding tree code. */
2079 enum tree_code tree_type;
2080 /* The precedence of this operator. */
2081 enum cp_parser_prec prec;
2084 struct cp_parser_expression_stack_entry
2086 /* Left hand side of the binary operation we are currently
2087 parsing. */
2088 cp_expr lhs;
2089 /* Original tree code for left hand side, if it was a binary
2090 expression itself (used for -Wparentheses). */
2091 enum tree_code lhs_type;
2092 /* Tree code for the binary operation we are parsing. */
2093 enum tree_code tree_type;
2094 /* Precedence of the binary operation we are parsing. */
2095 enum cp_parser_prec prec;
2096 /* Location of the binary operation we are parsing. */
2097 location_t loc;
2098 /* Flags from the operator token. */
2099 unsigned char flags;
2102 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2103 entries because precedence levels on the stack are monotonically
2104 increasing. */
2105 typedef struct cp_parser_expression_stack_entry
2106 cp_parser_expression_stack[NUM_PREC_VALUES];
2108 /* Used for parsing OMP for loops.
2110 Some notes on flags used for context:
2111 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2112 construct, except for the final-loop-body.
2113 The want_nested_loop flag is true if inside a {} sequence where
2114 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2115 but has not yet been seen. It's false when parsing intervening code
2116 statements or their substatements that cannot contain a loop-nest.
2117 The in_intervening_code flag is true when parsing any intervening code,
2118 including substatements, and whether or not want_nested_loop is true.
2120 And, about error handling:
2121 The saw_intervening_code flag is set if the loop is not perfectly
2122 nested, even in the usual case where this is not an error.
2123 perfect_nesting_fail is set if an error has been diagnosed because an
2124 imperfectly-nested loop was found where a perfectly-nested one is
2125 required (we diagnose this only once).
2126 fail is set if any kind of structural error in the loop nest
2127 has been found and diagnosed.
2129 struct omp_for_parse_data {
2130 enum tree_code code;
2131 tree declv, condv, incrv, initv;
2132 tree pre_body;
2133 tree orig_declv;
2134 auto_vec<tree, 4> orig_inits;
2135 int count; /* Expected nesting depth. */
2136 int depth; /* Current nesting depth. */
2137 location_t for_loc;
2138 releasing_vec init_blockv;
2139 releasing_vec body_blockv;
2140 releasing_vec init_placeholderv;
2141 releasing_vec body_placeholderv;
2142 bool ordered : 1;
2143 bool inscan : 1;
2144 bool want_nested_loop : 1;
2145 bool in_intervening_code : 1;
2146 bool saw_intervening_code : 1;
2147 bool perfect_nesting_fail : 1;
2148 bool fail : 1;
2149 tree clauses;
2150 tree *cclauses;
2151 tree ordered_cl;
2154 /* Prototypes. */
2156 /* Constructors and destructors. */
2158 static cp_parser_context *cp_parser_context_new
2159 (cp_parser_context *);
2161 /* Class variables. */
2163 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2165 /* The operator-precedence table used by cp_parser_binary_expression.
2166 Transformed into an associative array (binops_by_token) by
2167 cp_parser_new. */
2169 static const cp_parser_binary_operations_map_node binops[] = {
2170 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2171 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2173 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2174 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2175 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2177 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2178 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2180 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2181 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2183 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2185 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2186 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2187 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2188 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2190 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2191 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2193 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2195 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2197 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2199 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2201 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2204 /* The same as binops, but initialized by cp_parser_new so that
2205 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2206 for speed. */
2207 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2209 /* Constructors and destructors. */
2211 /* Construct a new context. The context below this one on the stack
2212 is given by NEXT. */
2214 static cp_parser_context *
2215 cp_parser_context_new (cp_parser_context* next)
2217 cp_parser_context *context;
2219 /* Allocate the storage. */
2220 if (cp_parser_context_free_list != NULL)
2222 /* Pull the first entry from the free list. */
2223 context = cp_parser_context_free_list;
2224 cp_parser_context_free_list = context->next;
2225 memset (context, 0, sizeof (*context));
2227 else
2228 context = ggc_cleared_alloc<cp_parser_context> ();
2230 /* No errors have occurred yet in this context. */
2231 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2232 /* If this is not the bottommost context, copy information that we
2233 need from the previous context. */
2234 if (next)
2236 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2237 expression, then we are parsing one in this context, too. */
2238 context->object_type = next->object_type;
2239 /* Thread the stack. */
2240 context->next = next;
2243 return context;
2246 /* Managing the unparsed function queues. */
2248 #define unparsed_funs_with_default_args \
2249 parser->unparsed_queues->last ().funs_with_default_args
2250 #define unparsed_funs_with_definitions \
2251 parser->unparsed_queues->last ().funs_with_definitions
2252 #define unparsed_nsdmis \
2253 parser->unparsed_queues->last ().nsdmis
2254 #define unparsed_noexcepts \
2255 parser->unparsed_queues->last ().noexcepts
2256 #define unparsed_contracts \
2257 parser->unparsed_queues->last ().contracts
2259 static void
2260 push_unparsed_function_queues (cp_parser *parser)
2262 cp_unparsed_functions_entry e
2263 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2264 vec_safe_push (parser->unparsed_queues, e);
2267 static void
2268 pop_unparsed_function_queues (cp_parser *parser)
2270 release_tree_vector (unparsed_funs_with_definitions);
2271 parser->unparsed_queues->pop ();
2274 /* Prototypes. */
2276 /* Routines to parse various constructs.
2278 Those that return `tree' will return the error_mark_node (rather
2279 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2280 Sometimes, they will return an ordinary node if error-recovery was
2281 attempted, even though a parse error occurred. So, to check
2282 whether or not a parse error occurred, you should always use
2283 cp_parser_error_occurred. If the construct is optional (indicated
2284 either by an `_opt' in the name of the function that does the
2285 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2286 the construct is not present. */
2288 /* Lexical conventions [gram.lex] */
2290 static tree finish_userdef_string_literal
2291 (tree);
2293 /* Basic concepts [gram.basic] */
2295 static void cp_parser_translation_unit (cp_parser *);
2297 /* Expressions [gram.expr] */
2299 static cp_expr cp_parser_primary_expression
2300 (cp_parser *, bool, bool, bool, cp_id_kind *);
2301 static cp_expr cp_parser_id_expression
2302 (cp_parser *, bool, bool, bool *, bool, bool);
2303 static cp_expr cp_parser_unqualified_id
2304 (cp_parser *, bool, bool, bool, bool);
2305 static tree cp_parser_nested_name_specifier_opt
2306 (cp_parser *, bool, bool, bool, bool, bool = false);
2307 static tree cp_parser_nested_name_specifier
2308 (cp_parser *, bool, bool, bool, bool);
2309 static tree cp_parser_qualifying_entity
2310 (cp_parser *, bool, bool, bool, bool, bool);
2311 static cp_expr cp_parser_postfix_expression
2312 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2313 static tree cp_parser_postfix_open_square_expression
2314 (cp_parser *, tree, bool, bool);
2315 static tree cp_parser_postfix_dot_deref_expression
2316 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2317 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2318 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2319 bool = false);
2320 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2321 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3 };
2322 static void cp_parser_pseudo_destructor_name
2323 (cp_parser *, tree, tree *, tree *);
2324 static cp_expr cp_parser_unary_expression
2325 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2326 static enum tree_code cp_parser_unary_operator
2327 (cp_token *);
2328 static tree cp_parser_has_attribute_expression
2329 (cp_parser *);
2330 static tree cp_parser_new_expression
2331 (cp_parser *);
2332 static vec<tree, va_gc> *cp_parser_new_placement
2333 (cp_parser *);
2334 static tree cp_parser_new_type_id
2335 (cp_parser *, tree *);
2336 static cp_declarator *cp_parser_new_declarator_opt
2337 (cp_parser *);
2338 static cp_declarator *cp_parser_direct_new_declarator
2339 (cp_parser *);
2340 static vec<tree, va_gc> *cp_parser_new_initializer
2341 (cp_parser *);
2342 static tree cp_parser_delete_expression
2343 (cp_parser *);
2344 static cp_expr cp_parser_cast_expression
2345 (cp_parser *, bool, bool, bool, cp_id_kind *);
2346 static cp_expr cp_parser_binary_expression
2347 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2348 static tree cp_parser_question_colon_clause
2349 (cp_parser *, cp_expr);
2350 static cp_expr cp_parser_conditional_expression (cp_parser *);
2351 static cp_expr cp_parser_assignment_expression
2352 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2353 static enum tree_code cp_parser_assignment_operator_opt
2354 (cp_parser *);
2355 static cp_expr cp_parser_expression
2356 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2357 static cp_expr cp_parser_constant_expression
2358 (cp_parser *, int = 0, bool * = NULL, bool = false);
2359 static cp_expr cp_parser_builtin_offsetof
2360 (cp_parser *);
2361 static cp_expr cp_parser_lambda_expression
2362 (cp_parser *);
2363 static void cp_parser_lambda_introducer
2364 (cp_parser *, tree);
2365 static bool cp_parser_lambda_declarator_opt
2366 (cp_parser *, tree);
2367 static void cp_parser_lambda_body
2368 (cp_parser *, tree);
2370 /* Statements [gram.stmt.stmt] */
2372 static void cp_parser_statement
2373 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2374 static void cp_parser_label_for_labeled_statement
2375 (cp_parser *, tree);
2376 static tree cp_parser_expression_statement
2377 (cp_parser *, tree);
2378 static tree cp_parser_compound_statement
2379 (cp_parser *, tree, int, bool);
2380 static void cp_parser_statement_seq_opt
2381 (cp_parser *, tree);
2382 static tree cp_parser_selection_statement
2383 (cp_parser *, bool *, vec<tree> *);
2384 static tree cp_parser_condition
2385 (cp_parser *);
2386 static tree cp_parser_iteration_statement
2387 (cp_parser *, bool *, bool, unsigned short, bool);
2388 static bool cp_parser_init_statement
2389 (cp_parser *, tree *decl);
2390 static tree cp_parser_for
2391 (cp_parser *, bool, unsigned short, bool);
2392 static tree cp_parser_c_for
2393 (cp_parser *, tree, tree, bool, unsigned short, bool);
2394 static tree cp_parser_range_for
2395 (cp_parser *, tree, tree, tree, bool, unsigned short, bool, bool);
2396 static void do_range_for_auto_deduction
2397 (tree, tree, cp_decomp *);
2398 static tree cp_parser_perform_range_for_lookup
2399 (tree, tree *, tree *);
2400 static tree cp_parser_range_for_member_function
2401 (tree, tree);
2402 static tree cp_parser_jump_statement
2403 (cp_parser *);
2404 static void cp_parser_declaration_statement
2405 (cp_parser *);
2407 static tree cp_parser_implicitly_scoped_statement
2408 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2409 static void cp_parser_already_scoped_statement
2410 (cp_parser *, bool *, const token_indent_info &);
2412 /* State of module-declaration parsing. */
2413 enum module_parse
2415 MP_NOT_MODULE, /* Not a module. */
2417 _MP_UNUSED,
2419 MP_FIRST, /* First declaration of TU. */
2420 MP_GLOBAL, /* Global Module Fragment. */
2422 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2423 MP_PURVIEW, /* Purview of a named module. */
2425 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2426 MP_PRIVATE, /* Private Module Fragment. */
2429 static module_parse cp_parser_module_declaration
2430 (cp_parser *parser, module_parse, bool exporting);
2431 static void cp_parser_import_declaration
2432 (cp_parser *parser, module_parse, bool exporting);
2434 /* Declarations [gram.dcl.dcl] */
2436 static void cp_parser_declaration_seq_opt
2437 (cp_parser *);
2438 static void cp_parser_declaration
2439 (cp_parser *, tree);
2440 static void cp_parser_toplevel_declaration
2441 (cp_parser *);
2442 static void cp_parser_block_declaration
2443 (cp_parser *, bool);
2444 static void cp_parser_simple_declaration
2445 (cp_parser *, bool, tree *);
2446 static void cp_parser_decl_specifier_seq
2447 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2448 static tree cp_parser_storage_class_specifier_opt
2449 (cp_parser *);
2450 static tree cp_parser_function_specifier_opt
2451 (cp_parser *, cp_decl_specifier_seq *);
2452 static tree cp_parser_type_specifier
2453 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2454 int *, bool *);
2455 static tree cp_parser_simple_type_specifier
2456 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2457 static tree cp_parser_placeholder_type_specifier
2458 (cp_parser *, location_t, tree, bool);
2459 static tree cp_parser_type_name
2460 (cp_parser *, bool);
2461 static tree cp_parser_nonclass_name
2462 (cp_parser* parser);
2463 static tree cp_parser_elaborated_type_specifier
2464 (cp_parser *, bool, bool);
2465 static tree cp_parser_enum_specifier
2466 (cp_parser *);
2467 static void cp_parser_enumerator_list
2468 (cp_parser *, tree);
2469 static void cp_parser_enumerator_definition
2470 (cp_parser *, tree);
2471 static tree cp_parser_namespace_name
2472 (cp_parser *);
2473 static void cp_parser_namespace_definition
2474 (cp_parser *);
2475 static void cp_parser_namespace_body
2476 (cp_parser *);
2477 static tree cp_parser_qualified_namespace_specifier
2478 (cp_parser *);
2479 static void cp_parser_namespace_alias_definition
2480 (cp_parser *);
2481 static bool cp_parser_using_declaration
2482 (cp_parser *, bool);
2483 static void cp_parser_using_directive
2484 (cp_parser *);
2485 static void cp_parser_using_enum
2486 (cp_parser *);
2487 static tree cp_parser_alias_declaration
2488 (cp_parser *);
2489 static void cp_parser_asm_definition
2490 (cp_parser *);
2491 static void cp_parser_linkage_specification
2492 (cp_parser *, tree);
2493 static void cp_parser_static_assert
2494 (cp_parser *, bool);
2495 static tree cp_parser_decltype
2496 (cp_parser *);
2497 static tree cp_parser_decomposition_declaration
2498 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2500 /* Declarators [gram.dcl.decl] */
2502 static tree cp_parser_init_declarator
2503 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2504 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2505 location_t *, tree *);
2506 static cp_declarator *cp_parser_declarator
2507 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2508 bool, bool, bool);
2509 static cp_declarator *cp_parser_direct_declarator
2510 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2511 bool);
2512 static enum tree_code cp_parser_ptr_operator
2513 (cp_parser *, tree *, cp_cv_quals *, tree *);
2514 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2515 (cp_parser *);
2516 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2517 (cp_parser *);
2518 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2519 (cp_parser *);
2520 static tree cp_parser_tx_qualifier_opt
2521 (cp_parser *);
2522 static tree cp_parser_late_return_type_opt
2523 (cp_parser *, cp_declarator *, tree &);
2524 static tree cp_parser_declarator_id
2525 (cp_parser *, bool);
2526 static tree cp_parser_type_id
2527 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2528 static tree cp_parser_template_type_arg
2529 (cp_parser *);
2530 static tree cp_parser_trailing_type_id (cp_parser *);
2531 static tree cp_parser_type_id_1
2532 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2533 static void cp_parser_type_specifier_seq
2534 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2535 static tree cp_parser_parameter_declaration_clause
2536 (cp_parser *, cp_parser_flags);
2537 static tree cp_parser_parameter_declaration_list
2538 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2539 static cp_parameter_declarator *cp_parser_parameter_declaration
2540 (cp_parser *, cp_parser_flags, bool, bool *);
2541 static tree cp_parser_default_argument
2542 (cp_parser *, bool);
2543 static void cp_parser_function_body
2544 (cp_parser *, bool);
2545 static tree cp_parser_initializer
2546 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2547 static cp_expr cp_parser_initializer_clause
2548 (cp_parser *, bool * = nullptr);
2549 static cp_expr cp_parser_braced_list
2550 (cp_parser*, bool * = nullptr);
2551 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2552 (cp_parser *, bool *, bool *);
2554 static void cp_parser_ctor_initializer_opt_and_function_body
2555 (cp_parser *, bool);
2557 static tree cp_parser_late_parsing_omp_declare_simd
2558 (cp_parser *, tree);
2560 static tree cp_parser_late_parsing_oacc_routine
2561 (cp_parser *, tree);
2563 static tree synthesize_implicit_template_parm
2564 (cp_parser *, tree);
2565 static tree finish_fully_implicit_template
2566 (cp_parser *, tree);
2567 static void abort_fully_implicit_template
2568 (cp_parser *);
2570 /* Classes [gram.class] */
2572 static tree cp_parser_class_name
2573 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2574 static tree cp_parser_class_specifier
2575 (cp_parser *);
2576 static tree cp_parser_class_head
2577 (cp_parser *, bool *);
2578 static enum tag_types cp_parser_class_key
2579 (cp_parser *);
2580 static void cp_parser_type_parameter_key
2581 (cp_parser* parser);
2582 static void cp_parser_member_specification_opt
2583 (cp_parser *);
2584 static void cp_parser_member_declaration
2585 (cp_parser *);
2586 static tree cp_parser_pure_specifier
2587 (cp_parser *);
2588 static tree cp_parser_constant_initializer
2589 (cp_parser *);
2591 /* Derived classes [gram.class.derived] */
2593 static tree cp_parser_base_clause
2594 (cp_parser *);
2595 static tree cp_parser_base_specifier
2596 (cp_parser *);
2598 /* Special member functions [gram.special] */
2600 static tree cp_parser_conversion_function_id
2601 (cp_parser *);
2602 static tree cp_parser_conversion_type_id
2603 (cp_parser *);
2604 static cp_declarator *cp_parser_conversion_declarator_opt
2605 (cp_parser *);
2606 static void cp_parser_ctor_initializer_opt
2607 (cp_parser *);
2608 static void cp_parser_mem_initializer_list
2609 (cp_parser *);
2610 static tree cp_parser_mem_initializer
2611 (cp_parser *);
2612 static tree cp_parser_mem_initializer_id
2613 (cp_parser *);
2615 /* Overloading [gram.over] */
2617 static cp_expr cp_parser_operator_function_id
2618 (cp_parser *);
2619 static cp_expr cp_parser_operator
2620 (cp_parser *, location_t);
2622 /* Templates [gram.temp] */
2624 static void cp_parser_template_declaration
2625 (cp_parser *, bool);
2626 static tree cp_parser_template_parameter_list
2627 (cp_parser *);
2628 static tree cp_parser_template_parameter
2629 (cp_parser *, bool *, bool *);
2630 static tree cp_parser_type_parameter
2631 (cp_parser *, bool *);
2632 static tree cp_parser_template_id
2633 (cp_parser *, bool, bool, enum tag_types, bool);
2634 static tree cp_parser_template_id_expr
2635 (cp_parser *, bool, bool, bool);
2636 static tree cp_parser_template_name
2637 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2638 static tree cp_parser_template_argument_list
2639 (cp_parser *);
2640 static tree cp_parser_template_argument
2641 (cp_parser *);
2642 static void cp_parser_explicit_instantiation
2643 (cp_parser *);
2644 static void cp_parser_explicit_specialization
2645 (cp_parser *);
2647 /* Exception handling [gram.except] */
2649 static tree cp_parser_try_block
2650 (cp_parser *);
2651 static void cp_parser_function_try_block
2652 (cp_parser *);
2653 static void cp_parser_handler_seq
2654 (cp_parser *);
2655 static void cp_parser_handler
2656 (cp_parser *);
2657 static tree cp_parser_exception_declaration
2658 (cp_parser *);
2659 static tree cp_parser_throw_expression
2660 (cp_parser *);
2661 static tree cp_parser_exception_specification_opt
2662 (cp_parser *, cp_parser_flags);
2663 static tree cp_parser_type_id_list
2664 (cp_parser *);
2665 static tree cp_parser_noexcept_specification_opt
2666 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2668 /* GNU Extensions */
2670 static tree cp_parser_asm_specification_opt
2671 (cp_parser *);
2672 static tree cp_parser_asm_operand_list
2673 (cp_parser *);
2674 static tree cp_parser_asm_clobber_list
2675 (cp_parser *);
2676 static tree cp_parser_asm_label_list
2677 (cp_parser *);
2678 static bool cp_next_tokens_can_be_attribute_p
2679 (cp_parser *);
2680 static bool cp_next_tokens_can_be_gnu_attribute_p
2681 (cp_parser *);
2682 static bool cp_next_tokens_can_be_std_attribute_p
2683 (cp_parser *);
2684 static bool cp_nth_tokens_can_be_std_attribute_p
2685 (cp_parser *, size_t);
2686 static bool cp_nth_tokens_can_be_gnu_attribute_p
2687 (cp_parser *, size_t);
2688 static bool cp_nth_tokens_can_be_attribute_p
2689 (cp_parser *, size_t);
2690 static tree cp_parser_attributes_opt
2691 (cp_parser *);
2692 static tree cp_parser_gnu_attributes_opt
2693 (cp_parser *);
2694 static tree cp_parser_gnu_attribute_list
2695 (cp_parser *, bool = false);
2696 static tree cp_parser_std_attribute
2697 (cp_parser *, tree);
2698 static tree cp_parser_std_attribute_spec
2699 (cp_parser *);
2700 static tree cp_parser_std_attribute_spec_seq
2701 (cp_parser *);
2702 static size_t cp_parser_skip_std_attribute_spec_seq
2703 (cp_parser *, size_t);
2704 static size_t cp_parser_skip_attributes_opt
2705 (cp_parser *, size_t);
2706 static bool cp_parser_extension_opt
2707 (cp_parser *, int *);
2708 static void cp_parser_label_declaration
2709 (cp_parser *);
2711 /* Concept Extensions */
2713 static tree cp_parser_concept_definition
2714 (cp_parser *);
2715 static tree cp_parser_constraint_expression
2716 (cp_parser *);
2717 static tree cp_parser_requires_clause_opt
2718 (cp_parser *, bool);
2719 static tree cp_parser_requires_expression
2720 (cp_parser *);
2721 static tree cp_parser_requirement_parameter_list
2722 (cp_parser *);
2723 static tree cp_parser_requirement_body
2724 (cp_parser *);
2725 static tree cp_parser_requirement_seq
2726 (cp_parser *);
2727 static tree cp_parser_requirement
2728 (cp_parser *);
2729 static tree cp_parser_simple_requirement
2730 (cp_parser *);
2731 static tree cp_parser_compound_requirement
2732 (cp_parser *);
2733 static tree cp_parser_type_requirement
2734 (cp_parser *);
2735 static tree cp_parser_nested_requirement
2736 (cp_parser *);
2738 /* Transactional Memory Extensions */
2740 static tree cp_parser_transaction
2741 (cp_parser *, cp_token *);
2742 static tree cp_parser_transaction_expression
2743 (cp_parser *, enum rid);
2744 static void cp_parser_function_transaction
2745 (cp_parser *, enum rid);
2746 static tree cp_parser_transaction_cancel
2747 (cp_parser *);
2749 /* Coroutine extensions. */
2751 static tree cp_parser_yield_expression
2752 (cp_parser *);
2754 /* Contracts */
2756 static void cp_parser_late_contract_condition
2757 (cp_parser *, tree, tree);
2759 enum pragma_context {
2760 pragma_external,
2761 pragma_member,
2762 pragma_objc_icode,
2763 pragma_stmt,
2764 pragma_compound
2766 static bool cp_parser_pragma
2767 (cp_parser *, enum pragma_context, bool *);
2769 /* Objective-C++ Productions */
2771 static tree cp_parser_objc_message_receiver
2772 (cp_parser *);
2773 static tree cp_parser_objc_message_args
2774 (cp_parser *);
2775 static tree cp_parser_objc_message_expression
2776 (cp_parser *);
2777 static cp_expr cp_parser_objc_encode_expression
2778 (cp_parser *);
2779 static tree cp_parser_objc_defs_expression
2780 (cp_parser *);
2781 static tree cp_parser_objc_protocol_expression
2782 (cp_parser *);
2783 static tree cp_parser_objc_selector_expression
2784 (cp_parser *);
2785 static cp_expr cp_parser_objc_expression
2786 (cp_parser *);
2787 static bool cp_parser_objc_selector_p
2788 (enum cpp_ttype);
2789 static tree cp_parser_objc_selector
2790 (cp_parser *);
2791 static tree cp_parser_objc_protocol_refs_opt
2792 (cp_parser *);
2793 static void cp_parser_objc_declaration
2794 (cp_parser *, tree);
2795 static tree cp_parser_objc_statement
2796 (cp_parser *);
2797 static bool cp_parser_objc_valid_prefix_attributes
2798 (cp_parser *, tree *);
2799 static void cp_parser_objc_at_property_declaration
2800 (cp_parser *) ;
2801 static void cp_parser_objc_at_synthesize_declaration
2802 (cp_parser *) ;
2803 static void cp_parser_objc_at_dynamic_declaration
2804 (cp_parser *) ;
2805 static tree cp_parser_objc_struct_declaration
2806 (cp_parser *) ;
2808 /* Utility Routines */
2810 static cp_expr cp_parser_lookup_name
2811 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2812 static tree cp_parser_lookup_name_simple
2813 (cp_parser *, tree, location_t);
2814 static tree cp_parser_maybe_treat_template_as_class
2815 (tree, bool);
2816 static bool cp_parser_check_declarator_template_parameters
2817 (cp_parser *, cp_declarator *, location_t);
2818 static bool cp_parser_check_template_parameters
2819 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2820 static cp_expr cp_parser_simple_cast_expression
2821 (cp_parser *);
2822 static tree cp_parser_global_scope_opt
2823 (cp_parser *, bool);
2824 static bool cp_parser_constructor_declarator_p
2825 (cp_parser *, cp_parser_flags, bool);
2826 static tree cp_parser_function_definition_from_specifiers_and_declarator
2827 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2828 static tree cp_parser_function_definition_after_declarator
2829 (cp_parser *, bool);
2830 static bool cp_parser_template_declaration_after_export
2831 (cp_parser *, bool);
2832 static void cp_parser_perform_template_parameter_access_checks
2833 (vec<deferred_access_check, va_gc> *);
2834 static tree cp_parser_single_declaration
2835 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2836 static cp_expr cp_parser_functional_cast
2837 (cp_parser *, tree);
2838 static tree cp_parser_save_member_function_body
2839 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2840 static tree cp_parser_save_nsdmi
2841 (cp_parser *);
2842 static tree cp_parser_enclosed_template_argument_list
2843 (cp_parser *);
2844 static void cp_parser_save_default_args
2845 (cp_parser *, tree);
2846 static void cp_parser_late_parsing_for_member
2847 (cp_parser *, tree);
2848 static tree cp_parser_late_parse_one_default_arg
2849 (cp_parser *, tree, tree, tree);
2850 static void cp_parser_late_parsing_nsdmi
2851 (cp_parser *, tree);
2852 static void cp_parser_late_parsing_default_args
2853 (cp_parser *, tree);
2854 static tree cp_parser_sizeof_operand
2855 (cp_parser *, enum rid);
2856 static cp_expr cp_parser_trait
2857 (cp_parser *, enum rid);
2858 static bool cp_parser_declares_only_class_p
2859 (cp_parser *);
2860 static void cp_parser_set_storage_class
2861 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2862 static void cp_parser_set_decl_spec_type
2863 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2864 static void set_and_check_decl_spec_loc
2865 (cp_decl_specifier_seq *decl_specs,
2866 cp_decl_spec ds, cp_token *);
2867 static bool cp_parser_friend_p
2868 (const cp_decl_specifier_seq *);
2869 static void cp_parser_required_error
2870 (cp_parser *, required_token, bool, location_t);
2871 static cp_token *cp_parser_require
2872 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2873 static cp_token *cp_parser_require_keyword
2874 (cp_parser *, enum rid, required_token);
2875 static bool cp_parser_token_starts_function_definition_p
2876 (cp_token *);
2877 static bool cp_parser_next_token_starts_class_definition_p
2878 (cp_parser *);
2879 static bool cp_parser_next_token_ends_template_argument_p
2880 (cp_parser *);
2881 static bool cp_parser_nth_token_starts_template_argument_list_p
2882 (cp_parser *, size_t);
2883 static enum tag_types cp_parser_token_is_class_key
2884 (cp_token *);
2885 static enum tag_types cp_parser_token_is_type_parameter_key
2886 (cp_token *);
2887 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2888 static void cp_parser_check_class_key
2889 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2890 static void cp_parser_check_access_in_redeclaration
2891 (tree type, location_t location);
2892 static bool cp_parser_optional_template_keyword
2893 (cp_parser *);
2894 static void cp_parser_pre_parsed_nested_name_specifier
2895 (cp_parser *);
2896 static bool cp_parser_cache_group
2897 (cp_parser *, enum cpp_ttype, unsigned);
2898 static tree cp_parser_cache_defarg
2899 (cp_parser *parser, bool nsdmi);
2900 static void cp_parser_parse_tentatively
2901 (cp_parser *);
2902 static void cp_parser_commit_to_tentative_parse
2903 (cp_parser *);
2904 static void cp_parser_commit_to_topmost_tentative_parse
2905 (cp_parser *);
2906 static void cp_parser_abort_tentative_parse
2907 (cp_parser *);
2908 static bool cp_parser_parse_definitely
2909 (cp_parser *);
2910 static inline bool cp_parser_parsing_tentatively
2911 (cp_parser *);
2912 static bool cp_parser_uncommitted_to_tentative_parse_p
2913 (cp_parser *);
2914 static void cp_parser_error
2915 (cp_parser *, const char *);
2916 static void cp_parser_name_lookup_error
2917 (cp_parser *, tree, tree, name_lookup_error, location_t);
2918 static bool cp_parser_simulate_error
2919 (cp_parser *);
2920 static bool cp_parser_check_type_definition
2921 (cp_parser *);
2922 static void cp_parser_check_for_definition_in_return_type
2923 (cp_declarator *, tree, location_t type_location);
2924 static void cp_parser_check_for_invalid_template_id
2925 (cp_parser *, tree, enum tag_types, location_t location);
2926 static bool cp_parser_non_integral_constant_expression
2927 (cp_parser *, non_integral_constant);
2928 static void cp_parser_diagnose_invalid_type_name
2929 (cp_parser *, tree, location_t);
2930 static bool cp_parser_parse_and_diagnose_invalid_type_name
2931 (cp_parser *);
2932 static int cp_parser_skip_to_closing_parenthesis
2933 (cp_parser *, bool, bool, bool);
2934 static void cp_parser_skip_to_end_of_statement
2935 (cp_parser *);
2936 static void cp_parser_consume_semicolon_at_end_of_statement
2937 (cp_parser *);
2938 static void cp_parser_skip_to_end_of_block_or_statement
2939 (cp_parser *);
2940 static bool cp_parser_skip_to_closing_brace
2941 (cp_parser *);
2942 static bool cp_parser_skip_entire_template_parameter_list
2943 (cp_parser *);
2944 static void cp_parser_require_end_of_template_parameter_list
2945 (cp_parser *);
2946 static bool cp_parser_skip_to_end_of_template_parameter_list
2947 (cp_parser *);
2948 static void cp_parser_skip_to_pragma_eol
2949 (cp_parser*, cp_token *);
2950 static bool cp_parser_error_occurred
2951 (cp_parser *);
2952 static bool cp_parser_allow_gnu_extensions_p
2953 (cp_parser *);
2954 static bool cp_parser_is_pure_string_literal
2955 (cp_token *);
2956 static bool cp_parser_is_string_literal
2957 (cp_token *);
2958 static bool cp_parser_is_keyword
2959 (cp_token *, enum rid);
2960 static tree cp_parser_make_typename_type
2961 (cp_parser *, tree, location_t location);
2962 static cp_declarator * cp_parser_make_indirect_declarator
2963 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2964 static bool cp_parser_compound_literal_p
2965 (cp_parser *);
2966 static bool cp_parser_array_designator_p
2967 (cp_parser *);
2968 static bool cp_parser_init_statement_p
2969 (cp_parser *);
2970 static bool cp_parser_skip_up_to_closing_square_bracket
2971 (cp_parser *);
2972 static bool cp_parser_skip_to_closing_square_bracket
2973 (cp_parser *);
2974 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2975 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
2977 // -------------------------------------------------------------------------- //
2978 // Unevaluated Operand Guard
2980 // Implementation of an RAII helper for unevaluated operand parsing.
2981 cp_unevaluated::cp_unevaluated ()
2983 ++cp_unevaluated_operand;
2984 ++c_inhibit_evaluation_warnings;
2987 cp_unevaluated::~cp_unevaluated ()
2989 --c_inhibit_evaluation_warnings;
2990 --cp_unevaluated_operand;
2993 // -------------------------------------------------------------------------- //
2994 // Tentative Parsing
2996 /* Returns nonzero if we are parsing tentatively. */
2998 static inline bool
2999 cp_parser_parsing_tentatively (cp_parser* parser)
3001 return parser->context->next != NULL;
3004 /* Returns nonzero if TOKEN is a string literal. */
3006 static bool
3007 cp_parser_is_pure_string_literal (cp_token* token)
3009 return (token->type == CPP_STRING ||
3010 token->type == CPP_STRING16 ||
3011 token->type == CPP_STRING32 ||
3012 token->type == CPP_WSTRING ||
3013 token->type == CPP_UTF8STRING);
3016 /* Returns nonzero if TOKEN is a string literal
3017 of a user-defined string literal. */
3019 static bool
3020 cp_parser_is_string_literal (cp_token* token)
3022 return (cp_parser_is_pure_string_literal (token) ||
3023 token->type == CPP_STRING_USERDEF ||
3024 token->type == CPP_STRING16_USERDEF ||
3025 token->type == CPP_STRING32_USERDEF ||
3026 token->type == CPP_WSTRING_USERDEF ||
3027 token->type == CPP_UTF8STRING_USERDEF);
3030 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3032 static bool
3033 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3035 return token->keyword == keyword;
3038 /* Helper function for cp_parser_error.
3039 Having peeked a token of kind TOK1_KIND that might signify
3040 a conflict marker, peek successor tokens to determine
3041 if we actually do have a conflict marker.
3042 Specifically, we consider a run of 7 '<', '=' or '>' characters
3043 at the start of a line as a conflict marker.
3044 These come through the lexer as three pairs and a single,
3045 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3046 If it returns true, *OUT_LOC is written to with the location/range
3047 of the marker. */
3049 static bool
3050 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3051 location_t *out_loc)
3053 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3054 if (token2->type != tok1_kind)
3055 return false;
3056 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3057 if (token3->type != tok1_kind)
3058 return false;
3059 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3060 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3061 return false;
3063 /* It must be at the start of the line. */
3064 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3065 if (LOCATION_COLUMN (start_loc) != 1)
3066 return false;
3068 /* We have a conflict marker. Construct a location of the form:
3069 <<<<<<<
3070 ^~~~~~~
3071 with start == caret, finishing at the end of the marker. */
3072 location_t finish_loc = get_finish (token4->location);
3073 *out_loc = make_location (start_loc, start_loc, finish_loc);
3075 return true;
3078 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3079 RT_CLOSE_PAREN. */
3081 static const char *
3082 get_matching_symbol (required_token token_desc)
3084 switch (token_desc)
3086 default:
3087 gcc_unreachable ();
3088 return "";
3089 case RT_CLOSE_BRACE:
3090 return "{";
3091 case RT_CLOSE_PAREN:
3092 return "(";
3096 /* Attempt to convert TOKEN_DESC from a required_token to an
3097 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3099 static enum cpp_ttype
3100 get_required_cpp_ttype (required_token token_desc)
3102 switch (token_desc)
3104 case RT_SEMICOLON:
3105 return CPP_SEMICOLON;
3106 case RT_OPEN_PAREN:
3107 return CPP_OPEN_PAREN;
3108 case RT_CLOSE_BRACE:
3109 return CPP_CLOSE_BRACE;
3110 case RT_OPEN_BRACE:
3111 return CPP_OPEN_BRACE;
3112 case RT_CLOSE_SQUARE:
3113 return CPP_CLOSE_SQUARE;
3114 case RT_OPEN_SQUARE:
3115 return CPP_OPEN_SQUARE;
3116 case RT_COMMA:
3117 return CPP_COMMA;
3118 case RT_COLON:
3119 return CPP_COLON;
3120 case RT_CLOSE_PAREN:
3121 return CPP_CLOSE_PAREN;
3123 default:
3124 /* Use CPP_EOF as a "no completions possible" code. */
3125 return CPP_EOF;
3130 /* Subroutine of cp_parser_error and cp_parser_required_error.
3132 Issue a diagnostic of the form
3133 FILE:LINE: MESSAGE before TOKEN
3134 where TOKEN is the next token in the input stream. MESSAGE
3135 (specified by the caller) is usually of the form "expected
3136 OTHER-TOKEN".
3138 This bypasses the check for tentative passing, and potentially
3139 adds material needed by cp_parser_required_error.
3141 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3142 suggesting insertion of the missing token.
3144 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3145 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3146 location. */
3148 static void
3149 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3150 required_token missing_token_desc,
3151 location_t matching_location)
3153 cp_token *token = cp_lexer_peek_token (parser->lexer);
3154 /* This diagnostic makes more sense if it is tagged to the line
3155 of the token we just peeked at. */
3156 cp_lexer_set_source_position_from_token (token);
3158 if (token->type == CPP_PRAGMA)
3160 error_at (token->location,
3161 "%<#pragma%> is not allowed here");
3162 cp_parser_skip_to_pragma_eol (parser, token);
3163 return;
3166 /* If this is actually a conflict marker, report it as such. */
3167 if (token->type == CPP_LSHIFT
3168 || token->type == CPP_RSHIFT
3169 || token->type == CPP_EQ_EQ)
3171 location_t loc;
3172 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3174 error_at (loc, "version control conflict marker in file");
3175 expanded_location token_exploc = expand_location (token->location);
3176 /* Consume tokens until the end of the source line. */
3177 for (;;)
3179 cp_lexer_consume_token (parser->lexer);
3180 cp_token *next = cp_lexer_peek_token (parser->lexer);
3181 if (next->type == CPP_EOF)
3182 break;
3183 if (next->location == UNKNOWN_LOCATION
3184 || loc == UNKNOWN_LOCATION)
3185 break;
3187 expanded_location next_exploc = expand_location (next->location);
3188 if (next_exploc.file != token_exploc.file)
3189 break;
3190 if (next_exploc.line != token_exploc.line)
3191 break;
3193 return;
3197 auto_diagnostic_group d;
3198 gcc_rich_location richloc (input_location);
3200 bool added_matching_location = false;
3202 if (missing_token_desc != RT_NONE)
3203 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3205 /* Potentially supply a fix-it hint, suggesting to add the
3206 missing token immediately after the *previous* token.
3207 This may move the primary location within richloc. */
3208 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3209 location_t prev_token_loc = prev_token->location;
3210 maybe_suggest_missing_token_insertion (&richloc, ttype,
3211 prev_token_loc);
3213 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3214 Attempt to consolidate diagnostics by printing it as a
3215 secondary range within the main diagnostic. */
3216 if (matching_location != UNKNOWN_LOCATION)
3217 added_matching_location
3218 = richloc.add_location_if_nearby (matching_location);
3221 /* If we were parsing a string-literal and there is an unknown name
3222 token right after, then check to see if that could also have been
3223 a literal string by checking the name against a list of known
3224 standard string literal constants defined in header files. If
3225 there is one, then add that as an hint to the error message. */
3226 name_hint h;
3227 if (token->type == CPP_NAME)
3228 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3229 if (cp_parser_is_string_literal (prev_token))
3231 tree name = token->u.value;
3232 const char *token_name = IDENTIFIER_POINTER (name);
3233 const char *header_hint
3234 = get_cp_stdlib_header_for_string_macro_name (token_name);
3235 if (header_hint != NULL)
3236 h = name_hint (NULL, new suggest_missing_header (token->location,
3237 token_name,
3238 header_hint));
3241 /* Actually emit the error. */
3242 c_parse_error (gmsgid,
3243 /* Because c_parser_error does not understand
3244 CPP_KEYWORD, keywords are treated like
3245 identifiers. */
3246 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3247 token->u.value, token->flags, &richloc);
3249 if (missing_token_desc != RT_NONE)
3251 /* If we weren't able to consolidate matching_location, then
3252 print it as a secondary diagnostic. */
3253 if (matching_location != UNKNOWN_LOCATION
3254 && !added_matching_location)
3255 inform (matching_location, "to match this %qs",
3256 get_matching_symbol (missing_token_desc));
3260 /* If not parsing tentatively, issue a diagnostic of the form
3261 FILE:LINE: MESSAGE before TOKEN
3262 where TOKEN is the next token in the input stream. MESSAGE
3263 (specified by the caller) is usually of the form "expected
3264 OTHER-TOKEN". */
3266 static void
3267 cp_parser_error (cp_parser* parser, const char* gmsgid)
3269 if (!cp_parser_simulate_error (parser))
3270 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3273 /* Issue an error about name-lookup failing. NAME is the
3274 IDENTIFIER_NODE DECL is the result of
3275 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3276 the thing that we hoped to find. */
3278 static void
3279 cp_parser_name_lookup_error (cp_parser* parser,
3280 tree name,
3281 tree decl,
3282 name_lookup_error desired,
3283 location_t location)
3285 /* If name lookup completely failed, tell the user that NAME was not
3286 declared. */
3287 if (decl == error_mark_node)
3289 if (parser->scope && parser->scope != global_namespace)
3290 error_at (location, "%<%E::%E%> has not been declared",
3291 parser->scope, name);
3292 else if (parser->scope == global_namespace)
3293 error_at (location, "%<::%E%> has not been declared", name);
3294 else if (parser->object_scope
3295 && !CLASS_TYPE_P (parser->object_scope))
3296 error_at (location, "request for member %qE in non-class type %qT",
3297 name, parser->object_scope);
3298 else if (parser->object_scope)
3299 error_at (location, "%<%T::%E%> has not been declared",
3300 parser->object_scope, name);
3301 else
3302 error_at (location, "%qE has not been declared", name);
3304 else if (parser->scope && parser->scope != global_namespace)
3306 switch (desired)
3308 case NLE_TYPE:
3309 error_at (location, "%<%E::%E%> is not a type",
3310 parser->scope, name);
3311 break;
3312 case NLE_CXX98:
3313 error_at (location, "%<%E::%E%> is not a class or namespace",
3314 parser->scope, name);
3315 break;
3316 case NLE_NOT_CXX98:
3317 error_at (location,
3318 "%<%E::%E%> is not a class, namespace, or enumeration",
3319 parser->scope, name);
3320 break;
3321 default:
3322 gcc_unreachable ();
3326 else if (parser->scope == global_namespace)
3328 switch (desired)
3330 case NLE_TYPE:
3331 error_at (location, "%<::%E%> is not a type", name);
3332 break;
3333 case NLE_CXX98:
3334 error_at (location, "%<::%E%> is not a class or namespace", name);
3335 break;
3336 case NLE_NOT_CXX98:
3337 error_at (location,
3338 "%<::%E%> is not a class, namespace, or enumeration",
3339 name);
3340 break;
3341 default:
3342 gcc_unreachable ();
3345 else
3347 switch (desired)
3349 case NLE_TYPE:
3350 error_at (location, "%qE is not a type", name);
3351 break;
3352 case NLE_CXX98:
3353 error_at (location, "%qE is not a class or namespace", name);
3354 break;
3355 case NLE_NOT_CXX98:
3356 error_at (location,
3357 "%qE is not a class, namespace, or enumeration", name);
3358 break;
3359 default:
3360 gcc_unreachable ();
3365 /* If we are parsing tentatively, remember that an error has occurred
3366 during this tentative parse. Returns true if the error was
3367 simulated; false if a message should be issued by the caller. */
3369 static bool
3370 cp_parser_simulate_error (cp_parser* parser)
3372 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3374 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3375 return true;
3377 return false;
3380 /* This function is called when a type is defined. If type
3381 definitions are forbidden at this point, an error message is
3382 issued. */
3384 static bool
3385 cp_parser_check_type_definition (cp_parser* parser)
3387 /* If types are forbidden here, issue a message. */
3388 if (parser->type_definition_forbidden_message)
3390 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3391 or %qs in the message need to be interpreted. */
3392 error (parser->type_definition_forbidden_message,
3393 parser->type_definition_forbidden_message_arg);
3394 return false;
3396 return true;
3399 /* This function is called when the DECLARATOR is processed. The TYPE
3400 was a type defined in the decl-specifiers. If it is invalid to
3401 define a type in the decl-specifiers for DECLARATOR, an error is
3402 issued. TYPE_LOCATION is the location of TYPE and is used
3403 for error reporting. */
3405 static void
3406 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3407 tree type, location_t type_location)
3409 /* [dcl.fct] forbids type definitions in return types.
3410 Unfortunately, it's not easy to know whether or not we are
3411 processing a return type until after the fact. */
3412 while (declarator
3413 && (declarator->kind == cdk_pointer
3414 || declarator->kind == cdk_reference
3415 || declarator->kind == cdk_ptrmem))
3416 declarator = declarator->declarator;
3417 if (declarator
3418 && declarator->kind == cdk_function)
3420 error_at (type_location,
3421 "new types may not be defined in a return type");
3422 inform (type_location,
3423 "(perhaps a semicolon is missing after the definition of %qT)",
3424 type);
3428 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3429 "<" in any valid C++ program. If the next token is indeed "<",
3430 issue a message warning the user about what appears to be an
3431 invalid attempt to form a template-id. LOCATION is the location
3432 of the type-specifier (TYPE) */
3434 static void
3435 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3436 tree type,
3437 enum tag_types tag_type,
3438 location_t location)
3440 cp_token_position start = 0;
3442 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3444 if (TREE_CODE (type) == TYPE_DECL)
3445 type = TREE_TYPE (type);
3446 if (TYPE_P (type) && !template_placeholder_p (type))
3447 error_at (location, "%qT is not a template", type);
3448 else if (identifier_p (type))
3450 if (tag_type != none_type)
3451 error_at (location, "%qE is not a class template", type);
3452 else
3453 error_at (location, "%qE is not a template", type);
3455 else
3456 error_at (location, "invalid template-id");
3457 /* Remember the location of the invalid "<". */
3458 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3459 start = cp_lexer_token_position (parser->lexer, true);
3460 /* Consume the "<". */
3461 cp_lexer_consume_token (parser->lexer);
3462 /* Parse the template arguments. */
3463 cp_parser_enclosed_template_argument_list (parser);
3464 /* Permanently remove the invalid template arguments so that
3465 this error message is not issued again. */
3466 if (start)
3467 cp_lexer_purge_tokens_after (parser->lexer, start);
3471 /* If parsing an integral constant-expression, issue an error message
3472 about the fact that THING appeared and return true. Otherwise,
3473 return false. In either case, set
3474 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3476 static bool
3477 cp_parser_non_integral_constant_expression (cp_parser *parser,
3478 non_integral_constant thing)
3480 parser->non_integral_constant_expression_p = true;
3481 if (parser->integral_constant_expression_p)
3483 if (!parser->allow_non_integral_constant_expression_p)
3485 const char *msg = NULL;
3486 switch (thing)
3488 case NIC_FLOAT:
3489 pedwarn (input_location, OPT_Wpedantic,
3490 "ISO C++ forbids using a floating-point literal "
3491 "in a constant-expression");
3492 return true;
3493 case NIC_CAST:
3494 error ("a cast to a type other than an integral or "
3495 "enumeration type cannot appear in a "
3496 "constant-expression");
3497 return true;
3498 case NIC_TYPEID:
3499 error ("%<typeid%> operator "
3500 "cannot appear in a constant-expression");
3501 return true;
3502 case NIC_NCC:
3503 error ("non-constant compound literals "
3504 "cannot appear in a constant-expression");
3505 return true;
3506 case NIC_FUNC_CALL:
3507 error ("a function call "
3508 "cannot appear in a constant-expression");
3509 return true;
3510 case NIC_INC:
3511 error ("an increment "
3512 "cannot appear in a constant-expression");
3513 return true;
3514 case NIC_DEC:
3515 error ("an decrement "
3516 "cannot appear in a constant-expression");
3517 return true;
3518 case NIC_ARRAY_REF:
3519 error ("an array reference "
3520 "cannot appear in a constant-expression");
3521 return true;
3522 case NIC_ADDR_LABEL:
3523 error ("the address of a label "
3524 "cannot appear in a constant-expression");
3525 return true;
3526 case NIC_OVERLOADED:
3527 error ("calls to overloaded operators "
3528 "cannot appear in a constant-expression");
3529 return true;
3530 case NIC_ASSIGNMENT:
3531 error ("an assignment cannot appear in a constant-expression");
3532 return true;
3533 case NIC_COMMA:
3534 error ("a comma operator "
3535 "cannot appear in a constant-expression");
3536 return true;
3537 case NIC_CONSTRUCTOR:
3538 error ("a call to a constructor "
3539 "cannot appear in a constant-expression");
3540 return true;
3541 case NIC_TRANSACTION:
3542 error ("a transaction expression "
3543 "cannot appear in a constant-expression");
3544 return true;
3545 case NIC_THIS:
3546 msg = "this";
3547 break;
3548 case NIC_FUNC_NAME:
3549 msg = "__FUNCTION__";
3550 break;
3551 case NIC_PRETTY_FUNC:
3552 msg = "__PRETTY_FUNCTION__";
3553 break;
3554 case NIC_C99_FUNC:
3555 msg = "__func__";
3556 break;
3557 case NIC_VA_ARG:
3558 msg = "va_arg";
3559 break;
3560 case NIC_ARROW:
3561 msg = "->";
3562 break;
3563 case NIC_POINT:
3564 msg = ".";
3565 break;
3566 case NIC_STAR:
3567 msg = "*";
3568 break;
3569 case NIC_ADDR:
3570 msg = "&";
3571 break;
3572 case NIC_PREINCREMENT:
3573 msg = "++";
3574 break;
3575 case NIC_PREDECREMENT:
3576 msg = "--";
3577 break;
3578 case NIC_NEW:
3579 msg = "new";
3580 break;
3581 case NIC_DEL:
3582 msg = "delete";
3583 break;
3584 default:
3585 gcc_unreachable ();
3587 if (msg)
3588 error ("%qs cannot appear in a constant-expression", msg);
3589 return true;
3592 return false;
3595 /* Emit a diagnostic for an invalid type name. This function commits
3596 to the current active tentative parse, if any. (Otherwise, the
3597 problematic construct might be encountered again later, resulting
3598 in duplicate error messages.) LOCATION is the location of ID. */
3600 static void
3601 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3602 location_t location)
3604 tree decl, ambiguous_decls;
3605 cp_parser_commit_to_tentative_parse (parser);
3606 /* Try to lookup the identifier. */
3607 decl = cp_parser_lookup_name (parser, id, none_type,
3608 /*is_template=*/false,
3609 /*is_namespace=*/false,
3610 /*check_dependency=*/true,
3611 &ambiguous_decls, location);
3612 if (ambiguous_decls)
3613 /* If the lookup was ambiguous, an error will already have
3614 been issued. */
3615 return;
3616 /* If the lookup found a template-name, it means that the user forgot
3617 to specify an argument list. Emit a useful error message. */
3618 if (DECL_TYPE_TEMPLATE_P (decl))
3620 auto_diagnostic_group d;
3621 error_at (location,
3622 "invalid use of template-name %qE without an argument list",
3623 decl);
3624 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3625 inform (location, "class template argument deduction is only available "
3626 "with %<-std=c++17%> or %<-std=gnu++17%>");
3627 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3629 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3630 error_at (location, "invalid use of destructor %qD as a type", id);
3631 else if (TREE_CODE (decl) == TYPE_DECL)
3632 /* Something like 'unsigned A a;' */
3633 error_at (location, "invalid combination of multiple type-specifiers");
3634 else if (!parser->scope)
3636 /* Issue an error message. */
3637 auto_diagnostic_group d;
3638 name_hint hint;
3639 if (TREE_CODE (id) == IDENTIFIER_NODE)
3640 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3641 if (const char *suggestion = hint.suggestion ())
3643 gcc_rich_location richloc (location);
3644 richloc.add_fixit_replace (suggestion);
3645 error_at (&richloc,
3646 "%qE does not name a type; did you mean %qs?",
3647 id, suggestion);
3649 else
3650 error_at (location, "%qE does not name a type", id);
3651 /* If we're in a template class, it's possible that the user was
3652 referring to a type from a base class. For example:
3654 template <typename T> struct A { typedef T X; };
3655 template <typename T> struct B : public A<T> { X x; };
3657 The user should have said "typename A<T>::X". */
3658 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3659 inform (location, "C++11 %<constexpr%> only available with "
3660 "%<-std=c++11%> or %<-std=gnu++11%>");
3661 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3662 inform (location, "C++11 %<noexcept%> only available with "
3663 "%<-std=c++11%> or %<-std=gnu++11%>");
3664 else if (TREE_CODE (id) == IDENTIFIER_NODE
3665 && (id_equal (id, "module") || id_equal (id, "import")))
3667 if (modules_p ())
3668 inform (location, "%qE is not recognized as a module control-line",
3669 id);
3670 else if (cxx_dialect < cxx20)
3671 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3672 id);
3673 else
3674 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3675 ", which is not yet enabled with %<-std=c++20%>", id);
3677 else if (cxx_dialect < cxx11
3678 && TREE_CODE (id) == IDENTIFIER_NODE
3679 && id_equal (id, "thread_local"))
3680 inform (location, "C++11 %<thread_local%> only available with "
3681 "%<-std=c++11%> or %<-std=gnu++11%>");
3682 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3683 inform (location, "C++20 %<constinit%> only available with "
3684 "%<-std=c++20%> or %<-std=gnu++20%>");
3685 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3686 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3687 "%<-fconcepts%>");
3688 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3689 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3690 "%<-fconcepts%>");
3691 else if (processing_template_decl && current_class_type
3692 && TYPE_BINFO (current_class_type))
3694 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3695 b; b = TREE_CHAIN (b))
3697 tree base_type = BINFO_TYPE (b);
3698 if (CLASS_TYPE_P (base_type)
3699 && dependent_type_p (base_type))
3701 /* Go from a particular instantiation of the
3702 template (which will have an empty TYPE_FIELDs),
3703 to the main version. */
3704 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3705 for (tree field = TYPE_FIELDS (base_type);
3706 field; field = DECL_CHAIN (field))
3707 if (TREE_CODE (field) == TYPE_DECL
3708 && DECL_NAME (field) == id)
3710 inform (location,
3711 "(perhaps %<typename %T::%E%> was intended)",
3712 BINFO_TYPE (b), id);
3713 goto found;
3717 found:;
3720 /* Here we diagnose qualified-ids where the scope is actually correct,
3721 but the identifier does not resolve to a valid type name. */
3722 else if (parser->scope != error_mark_node)
3724 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3726 auto_diagnostic_group d;
3727 name_hint hint;
3728 if (decl == error_mark_node)
3729 hint = suggest_alternative_in_explicit_scope (location, id,
3730 parser->scope);
3731 const char *suggestion = hint.suggestion ();
3732 gcc_rich_location richloc (location_of (id));
3733 if (suggestion)
3734 richloc.add_fixit_replace (suggestion);
3735 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3737 if (suggestion)
3738 error_at (&richloc,
3739 "%qE in namespace %qE does not name a template"
3740 " type; did you mean %qs?",
3741 id, parser->scope, suggestion);
3742 else
3743 error_at (&richloc,
3744 "%qE in namespace %qE does not name a template type",
3745 id, parser->scope);
3747 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3749 if (suggestion)
3750 error_at (&richloc,
3751 "%qE in namespace %qE does not name a template"
3752 " type; did you mean %qs?",
3753 TREE_OPERAND (id, 0), parser->scope, suggestion);
3754 else
3755 error_at (&richloc,
3756 "%qE in namespace %qE does not name a template"
3757 " type",
3758 TREE_OPERAND (id, 0), parser->scope);
3760 else
3762 if (suggestion)
3763 error_at (&richloc,
3764 "%qE in namespace %qE does not name a type"
3765 "; did you mean %qs?",
3766 id, parser->scope, suggestion);
3767 else
3768 error_at (&richloc,
3769 "%qE in namespace %qE does not name a type",
3770 id, parser->scope);
3772 if (DECL_P (decl))
3773 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3775 else if (CLASS_TYPE_P (parser->scope)
3776 && constructor_name_p (id, parser->scope))
3778 /* A<T>::A<T>() */
3779 auto_diagnostic_group d;
3780 error_at (location, "%<%T::%E%> names the constructor, not"
3781 " the type", parser->scope, id);
3782 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3783 error_at (location, "and %qT has no template constructors",
3784 parser->scope);
3786 else if (TYPE_P (parser->scope)
3787 && dependent_scope_p (parser->scope))
3789 gcc_rich_location richloc (location);
3790 richloc.add_fixit_insert_before ("typename ");
3791 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3792 error_at (&richloc,
3793 "need %<typename%> before %<%T::%D::%E%> because "
3794 "%<%T::%D%> is a dependent scope",
3795 TYPE_CONTEXT (parser->scope),
3796 TYPENAME_TYPE_FULLNAME (parser->scope),
3798 TYPE_CONTEXT (parser->scope),
3799 TYPENAME_TYPE_FULLNAME (parser->scope));
3800 else
3801 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3802 "%qT is a dependent scope",
3803 parser->scope, id, parser->scope);
3805 else if (TYPE_P (parser->scope))
3807 auto_diagnostic_group d;
3808 if (!COMPLETE_TYPE_P (parser->scope))
3809 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3810 parser->scope);
3811 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3812 error_at (location_of (id),
3813 "%qE in %q#T does not name a template type",
3814 id, parser->scope);
3815 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3816 error_at (location_of (id),
3817 "%qE in %q#T does not name a template type",
3818 TREE_OPERAND (id, 0), parser->scope);
3819 else
3820 error_at (location_of (id),
3821 "%qE in %q#T does not name a type",
3822 id, parser->scope);
3823 if (DECL_P (decl))
3824 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3826 else
3827 gcc_unreachable ();
3831 /* Check for a common situation where a type-name should be present,
3832 but is not, and issue a sensible error message. Returns true if an
3833 invalid type-name was detected.
3835 The situation handled by this function are variable declarations of the
3836 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3837 Usually, `ID' should name a type, but if we got here it means that it
3838 does not. We try to emit the best possible error message depending on
3839 how exactly the id-expression looks like. */
3841 static bool
3842 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3844 tree id;
3845 cp_token *token = cp_lexer_peek_token (parser->lexer);
3847 /* Avoid duplicate error about ambiguous lookup. */
3848 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3850 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3851 if (next->type == CPP_NAME && next->error_reported)
3852 goto out;
3855 cp_parser_parse_tentatively (parser);
3856 id = cp_parser_id_expression (parser,
3857 /*template_keyword_p=*/false,
3858 /*check_dependency_p=*/true,
3859 /*template_p=*/NULL,
3860 /*declarator_p=*/true,
3861 /*optional_p=*/false);
3862 /* If the next token is a (, this is a function with no explicit return
3863 type, i.e. constructor, destructor or conversion op. */
3864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3865 || TREE_CODE (id) == TYPE_DECL)
3867 cp_parser_abort_tentative_parse (parser);
3868 return false;
3870 if (!cp_parser_parse_definitely (parser))
3871 return false;
3873 /* Emit a diagnostic for the invalid type. */
3874 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3875 out:
3876 /* If we aren't in the middle of a declarator (i.e. in a
3877 parameter-declaration-clause), skip to the end of the declaration;
3878 there's no point in trying to process it. */
3879 if (!parser->in_declarator_p)
3880 cp_parser_skip_to_end_of_block_or_statement (parser);
3881 return true;
3884 /* Consume tokens up to, and including, the next non-nested closing `)'.
3885 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3886 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3887 found an unnested token of that type. */
3889 static int
3890 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3891 bool recovering,
3892 cpp_ttype or_ttype,
3893 bool consume_paren)
3895 unsigned paren_depth = 0;
3896 unsigned brace_depth = 0;
3897 unsigned square_depth = 0;
3898 unsigned condop_depth = 0;
3900 if (recovering && or_ttype == CPP_EOF
3901 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3902 return 0;
3904 while (true)
3906 cp_token * token = cp_lexer_peek_token (parser->lexer);
3908 /* Have we found what we're looking for before the closing paren? */
3909 if (token->type == or_ttype && or_ttype != CPP_EOF
3910 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3911 return -1;
3913 switch (token->type)
3915 case CPP_PRAGMA_EOL:
3916 if (!parser->lexer->in_pragma)
3917 break;
3918 /* FALLTHRU */
3919 case CPP_EOF:
3920 /* If we've run out of tokens, then there is no closing `)'. */
3921 return 0;
3923 /* This is good for lambda expression capture-lists. */
3924 case CPP_OPEN_SQUARE:
3925 ++square_depth;
3926 break;
3927 case CPP_CLOSE_SQUARE:
3928 if (!square_depth--)
3929 return 0;
3930 break;
3932 case CPP_SEMICOLON:
3933 /* This matches the processing in skip_to_end_of_statement. */
3934 if (!brace_depth)
3935 return 0;
3936 break;
3938 case CPP_OPEN_BRACE:
3939 ++brace_depth;
3940 break;
3941 case CPP_CLOSE_BRACE:
3942 if (!brace_depth--)
3943 return 0;
3944 break;
3946 case CPP_OPEN_PAREN:
3947 if (!brace_depth)
3948 ++paren_depth;
3949 break;
3951 case CPP_CLOSE_PAREN:
3952 if (!brace_depth && !paren_depth--)
3954 if (consume_paren)
3955 cp_lexer_consume_token (parser->lexer);
3956 return 1;
3958 break;
3960 case CPP_QUERY:
3961 if (!brace_depth && !paren_depth && !square_depth)
3962 ++condop_depth;
3963 break;
3965 case CPP_COLON:
3966 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3967 condop_depth--;
3968 break;
3970 case CPP_KEYWORD:
3971 if (!cp_token_is_module_directive (token))
3972 break;
3973 /* FALLTHROUGH */
3975 case CPP_PRAGMA:
3976 /* We fell into a pragma. Skip it, and continue. */
3977 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3978 continue;
3980 default:
3981 break;
3984 /* Consume the token. */
3985 cp_lexer_consume_token (parser->lexer);
3989 /* Consume tokens up to, and including, the next non-nested closing `)'.
3990 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3991 are doing error recovery. Returns -1 if OR_COMMA is true and we
3992 found an unnested token of that type. */
3994 static int
3995 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3996 bool recovering,
3997 bool or_comma,
3998 bool consume_paren)
4000 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4001 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4002 ttype, consume_paren);
4005 /* Consume tokens until we reach the end of the current statement.
4006 Normally, that will be just before consuming a `;'. However, if a
4007 non-nested `}' comes first, then we stop before consuming that. */
4009 static void
4010 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4012 unsigned nesting_depth = 0;
4014 /* Unwind generic function template scope if necessary. */
4015 if (parser->fully_implicit_function_template_p)
4016 abort_fully_implicit_template (parser);
4018 while (true)
4020 cp_token *token = cp_lexer_peek_token (parser->lexer);
4022 switch (token->type)
4024 case CPP_PRAGMA_EOL:
4025 if (!parser->lexer->in_pragma)
4026 break;
4027 /* FALLTHRU */
4028 case CPP_EOF:
4029 /* If we've run out of tokens, stop. */
4030 return;
4032 case CPP_SEMICOLON:
4033 /* If the next token is a `;', we have reached the end of the
4034 statement. */
4035 if (!nesting_depth)
4036 return;
4037 break;
4039 case CPP_CLOSE_BRACE:
4040 /* If this is a non-nested '}', stop before consuming it.
4041 That way, when confronted with something like:
4043 { 3 + }
4045 we stop before consuming the closing '}', even though we
4046 have not yet reached a `;'. */
4047 if (nesting_depth == 0)
4048 return;
4050 /* If it is the closing '}' for a block that we have
4051 scanned, stop -- but only after consuming the token.
4052 That way given:
4054 void f g () { ... }
4055 typedef int I;
4057 we will stop after the body of the erroneously declared
4058 function, but before consuming the following `typedef'
4059 declaration. */
4060 if (--nesting_depth == 0)
4062 cp_lexer_consume_token (parser->lexer);
4063 return;
4065 break;
4067 case CPP_OPEN_BRACE:
4068 ++nesting_depth;
4069 break;
4071 case CPP_KEYWORD:
4072 if (!cp_token_is_module_directive (token))
4073 break;
4074 /* FALLTHROUGH */
4076 case CPP_PRAGMA:
4077 /* We fell into a pragma. Skip it, and continue or return. */
4078 cp_parser_skip_to_pragma_eol (parser, token);
4079 if (!nesting_depth)
4080 return;
4081 continue;
4083 default:
4084 break;
4087 /* Consume the token. */
4088 cp_lexer_consume_token (parser->lexer);
4092 /* This function is called at the end of a statement or declaration.
4093 If the next token is a semicolon, it is consumed; otherwise, error
4094 recovery is attempted. */
4096 static void
4097 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4099 /* Look for the trailing `;'. */
4100 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4102 /* If there is additional (erroneous) input, skip to the end of
4103 the statement. */
4104 cp_parser_skip_to_end_of_statement (parser);
4105 /* If the next token is now a `;', consume it. */
4106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4107 cp_lexer_consume_token (parser->lexer);
4111 /* Skip tokens until we have consumed an entire block, or until we
4112 have consumed a non-nested `;'. */
4114 static void
4115 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4117 int nesting_depth = 0;
4119 /* Unwind generic function template scope if necessary. */
4120 if (parser->fully_implicit_function_template_p)
4121 abort_fully_implicit_template (parser);
4123 while (nesting_depth >= 0)
4125 cp_token *token = cp_lexer_peek_token (parser->lexer);
4127 switch (token->type)
4129 case CPP_PRAGMA_EOL:
4130 if (!parser->lexer->in_pragma)
4131 break;
4132 /* FALLTHRU */
4133 case CPP_EOF:
4134 /* If we've run out of tokens, stop. */
4135 return;
4137 case CPP_SEMICOLON:
4138 /* Stop if this is an unnested ';'. */
4139 if (!nesting_depth)
4140 nesting_depth = -1;
4141 break;
4143 case CPP_CLOSE_BRACE:
4144 /* Stop if this is an unnested '}', or closes the outermost
4145 nesting level. */
4146 nesting_depth--;
4147 if (nesting_depth < 0)
4148 return;
4149 if (!nesting_depth)
4150 nesting_depth = -1;
4151 break;
4153 case CPP_OPEN_BRACE:
4154 /* Nest. */
4155 nesting_depth++;
4156 break;
4158 case CPP_KEYWORD:
4159 if (!cp_token_is_module_directive (token))
4160 break;
4161 /* FALLTHROUGH */
4163 case CPP_PRAGMA:
4164 /* Skip it, and continue or return. */
4165 cp_parser_skip_to_pragma_eol (parser, token);
4166 if (!nesting_depth)
4167 return;
4168 continue;
4170 default:
4171 break;
4174 /* Consume the token. */
4175 cp_lexer_consume_token (parser->lexer);
4179 /* Skip tokens until a non-nested closing curly brace is the next
4180 token, or there are no more tokens. Return true in the first case,
4181 false otherwise. */
4183 static bool
4184 cp_parser_skip_to_closing_brace (cp_parser *parser)
4186 unsigned nesting_depth = 0;
4188 while (true)
4190 cp_token *token = cp_lexer_peek_token (parser->lexer);
4192 switch (token->type)
4194 case CPP_PRAGMA_EOL:
4195 if (!parser->lexer->in_pragma)
4196 break;
4197 /* FALLTHRU */
4198 case CPP_EOF:
4199 /* If we've run out of tokens, stop. */
4200 return false;
4202 case CPP_CLOSE_BRACE:
4203 /* If the next token is a non-nested `}', then we have reached
4204 the end of the current block. */
4205 if (nesting_depth-- == 0)
4206 return true;
4207 break;
4209 case CPP_OPEN_BRACE:
4210 /* If it the next token is a `{', then we are entering a new
4211 block. Consume the entire block. */
4212 ++nesting_depth;
4213 break;
4215 default:
4216 break;
4219 /* Consume the token. */
4220 cp_lexer_consume_token (parser->lexer);
4224 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4225 parameter is the PRAGMA token, allowing us to purge the entire pragma
4226 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4227 forwards (not error recovery). */
4229 static void
4230 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4232 cp_token *token;
4236 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4237 before an EOF token, even when the EOF is on the pragma line.
4238 We should never get here without being inside a deferred
4239 pragma. */
4240 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4241 token = cp_lexer_consume_token (parser->lexer);
4243 while (token->type != CPP_PRAGMA_EOL);
4245 if (pragma_tok)
4247 parser->lexer->in_pragma = false;
4248 if (parser->lexer->in_omp_attribute_pragma
4249 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4251 parser->lexer = parser->lexer->next;
4252 /* Put the current source position back where it was before this
4253 lexer was pushed. */
4254 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4259 /* Require pragma end of line, resyncing with it as necessary. The
4260 arguments are as for cp_parser_skip_to_pragma_eol. */
4262 static void
4263 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4265 parser->lexer->in_pragma = false;
4266 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4268 else if (parser->lexer->in_omp_attribute_pragma
4269 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4271 parser->lexer = parser->lexer->next;
4272 /* Put the current source position back where it was before this
4273 lexer was pushed. */
4274 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4278 /* This is a simple wrapper around make_typename_type. When the id is
4279 an unresolved identifier node, we can provide a superior diagnostic
4280 using cp_parser_diagnose_invalid_type_name. */
4282 static tree
4283 cp_parser_make_typename_type (cp_parser *parser, tree id,
4284 location_t id_location)
4286 tree result;
4287 if (identifier_p (id))
4289 result = make_typename_type (parser->scope, id, typename_type,
4290 /*complain=*/tf_none);
4291 if (result == error_mark_node)
4292 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4293 return result;
4295 return make_typename_type (parser->scope, id, typename_type, tf_error);
4298 /* This is a wrapper around the
4299 make_{pointer,ptrmem,reference}_declarator functions that decides
4300 which one to call based on the CODE and CLASS_TYPE arguments. The
4301 CODE argument should be one of the values returned by
4302 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4303 appertain to the pointer or reference. */
4305 static cp_declarator *
4306 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4307 cp_cv_quals cv_qualifiers,
4308 cp_declarator *target,
4309 tree attributes)
4311 if (code == ERROR_MARK || target == cp_error_declarator)
4312 return cp_error_declarator;
4314 if (code == INDIRECT_REF)
4315 if (class_type == NULL_TREE)
4316 return make_pointer_declarator (cv_qualifiers, target, attributes);
4317 else
4318 return make_ptrmem_declarator (cv_qualifiers, class_type,
4319 target, attributes);
4320 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4321 return make_reference_declarator (cv_qualifiers, target,
4322 false, attributes);
4323 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4324 return make_reference_declarator (cv_qualifiers, target,
4325 true, attributes);
4326 gcc_unreachable ();
4329 /* Create a new C++ parser. */
4331 static cp_parser *
4332 cp_parser_new (cp_lexer *lexer)
4334 /* Initialize the binops_by_token so that we can get the tree
4335 directly from the token. */
4336 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4337 binops_by_token[binops[i].token_type] = binops[i];
4339 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4340 parser->lexer = lexer;
4341 parser->context = cp_parser_context_new (NULL);
4343 /* For now, we always accept GNU extensions. */
4344 parser->allow_gnu_extensions_p = 1;
4346 /* The `>' token is a greater-than operator, not the end of a
4347 template-id. */
4348 parser->greater_than_is_operator_p = true;
4350 parser->default_arg_ok_p = true;
4352 /* We are not parsing a constant-expression. */
4353 parser->integral_constant_expression_p = false;
4354 parser->allow_non_integral_constant_expression_p = false;
4355 parser->non_integral_constant_expression_p = false;
4357 /* Local variable names are not forbidden. */
4358 parser->local_variables_forbidden_p = 0;
4360 /* We are not processing an `extern "C"' declaration. */
4361 parser->in_unbraced_linkage_specification_p = false;
4363 /* We are not processing a declarator. */
4364 parser->in_declarator_p = false;
4366 /* We are not processing a template-argument-list. */
4367 parser->in_template_argument_list_p = false;
4369 /* We are not in an iteration statement. */
4370 parser->in_statement = 0;
4372 /* We are not in a switch statement. */
4373 parser->in_switch_statement_p = false;
4375 /* We are not parsing a type-id inside an expression. */
4376 parser->in_type_id_in_expr_p = false;
4378 /* String literals should be translated to the execution character set. */
4379 parser->translate_strings_p = true;
4381 /* We are not parsing a function body. */
4382 parser->in_function_body = false;
4384 /* We can correct until told otherwise. */
4385 parser->colon_corrects_to_scope_p = true;
4387 /* The unparsed function queue is empty. */
4388 push_unparsed_function_queues (parser);
4390 /* There are no classes being defined. */
4391 parser->num_classes_being_defined = 0;
4393 /* No template parameters apply. */
4394 parser->num_template_parameter_lists = 0;
4396 /* Special parsing data structures. */
4397 parser->omp_declare_simd = NULL;
4398 parser->oacc_routine = NULL;
4400 /* Not declaring an implicit function template. */
4401 parser->auto_is_implicit_function_template_parm_p = false;
4402 parser->fully_implicit_function_template_p = false;
4403 parser->implicit_template_parms = 0;
4404 parser->implicit_template_scope = 0;
4406 /* Allow constrained-type-specifiers. */
4407 parser->prevent_constrained_type_specifiers = 0;
4409 /* We haven't yet seen an 'extern "C"'. */
4410 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4412 return parser;
4415 /* Create a cp_lexer structure which will emit the tokens in CACHE
4416 and push it onto the parser's lexer stack. This is used for delayed
4417 parsing of in-class method bodies and default arguments, and should
4418 not be confused with tentative parsing. */
4419 static void
4420 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4422 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4423 lexer->next = parser->lexer;
4424 parser->lexer = lexer;
4426 /* Move the current source position to that of the first token in the
4427 new lexer. */
4428 cp_lexer_set_source_position_from_token (lexer->next_token);
4431 /* Pop the top lexer off the parser stack. This is never used for the
4432 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4433 static void
4434 cp_parser_pop_lexer (cp_parser *parser)
4436 cp_lexer *lexer = parser->lexer;
4437 parser->lexer = lexer->next;
4438 cp_lexer_destroy (lexer);
4440 /* Put the current source position back where it was before this
4441 lexer was pushed. */
4442 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4445 /* Lexical conventions [gram.lex] */
4447 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4448 identifier. */
4450 static cp_expr
4451 cp_parser_identifier (cp_parser* parser)
4453 cp_token *token;
4455 /* Look for the identifier. */
4456 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4457 /* Return the value. */
4458 if (token)
4459 return cp_expr (token->u.value, token->location);
4460 else
4461 return error_mark_node;
4464 /* Worker for cp_parser_string_literal and cp_parser_userdef_string_literal.
4465 Do not call this directly; use either of the above.
4467 Parse a sequence of adjacent string constants. Return a
4468 TREE_STRING representing the combined, nul-terminated string
4469 constant. If TRANSLATE is true, translate the string to the
4470 execution character set. If WIDE_OK is true, a wide string is
4471 valid here. If UDL_OK is true, a string literal with user-defined
4472 suffix can be used in this context.
4474 C++98 [lex.string] says that if a narrow string literal token is
4475 adjacent to a wide string literal token, the behavior is undefined.
4476 However, C99 6.4.5p4 says that this results in a wide string literal.
4477 We follow C99 here, for consistency with the C front end.
4479 This code is largely lifted from lex_string() in c-lex.cc.
4481 FUTURE: ObjC++ will need to handle @-strings here. */
4483 static cp_expr
4484 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4485 bool wide_ok, bool udl_ok,
4486 bool lookup_udlit)
4488 tree value;
4489 size_t count;
4490 struct obstack str_ob;
4491 struct obstack loc_ob;
4492 cpp_string str, istr, *strs;
4493 cp_token *tok;
4494 enum cpp_ttype type, curr_type;
4495 int have_suffix_p = 0;
4496 tree string_tree;
4497 tree suffix_id = NULL_TREE;
4498 bool curr_tok_is_userdef_p = false;
4500 tok = cp_lexer_peek_token (parser->lexer);
4501 if (!cp_parser_is_string_literal (tok))
4503 cp_parser_error (parser, "expected string-literal");
4504 return error_mark_node;
4507 location_t loc = tok->location;
4509 if (cpp_userdef_string_p (tok->type))
4511 if (!udl_ok)
4513 error_at (loc, "string literal with user-defined suffix "
4514 "is invalid in this context");
4515 return error_mark_node;
4517 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4518 curr_type = cpp_userdef_string_remove_type (tok->type);
4519 curr_tok_is_userdef_p = true;
4521 else
4523 string_tree = tok->u.value;
4524 curr_type = tok->type;
4526 type = curr_type;
4528 /* Try to avoid the overhead of creating and destroying an obstack
4529 for the common case of just one string. */
4530 if (!cp_parser_is_string_literal
4531 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4533 cp_lexer_consume_token (parser->lexer);
4535 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4536 str.len = TREE_STRING_LENGTH (string_tree);
4537 count = 1;
4539 if (curr_tok_is_userdef_p)
4541 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4542 have_suffix_p = 1;
4543 curr_type = cpp_userdef_string_remove_type (tok->type);
4545 else
4546 curr_type = tok->type;
4548 strs = &str;
4550 else
4552 location_t last_tok_loc = tok->location;
4553 gcc_obstack_init (&str_ob);
4554 gcc_obstack_init (&loc_ob);
4555 count = 0;
4559 cp_lexer_consume_token (parser->lexer);
4560 count++;
4561 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4562 str.len = TREE_STRING_LENGTH (string_tree);
4564 if (curr_tok_is_userdef_p)
4566 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4567 if (have_suffix_p == 0)
4569 suffix_id = curr_suffix_id;
4570 have_suffix_p = 1;
4572 else if (have_suffix_p == 1
4573 && curr_suffix_id != suffix_id)
4575 error ("inconsistent user-defined literal suffixes"
4576 " %qD and %qD in string literal",
4577 suffix_id, curr_suffix_id);
4578 have_suffix_p = -1;
4580 curr_type = cpp_userdef_string_remove_type (tok->type);
4582 else
4583 curr_type = tok->type;
4585 if (type != curr_type)
4587 if (type == CPP_STRING)
4588 type = curr_type;
4589 else if (curr_type != CPP_STRING)
4591 rich_location rich_loc (line_table, tok->location);
4592 rich_loc.add_range (last_tok_loc);
4593 error_at (&rich_loc,
4594 "concatenation of string literals with "
4595 "conflicting encoding prefixes");
4599 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4600 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4602 last_tok_loc = tok->location;
4604 tok = cp_lexer_peek_token (parser->lexer);
4605 if (cpp_userdef_string_p (tok->type))
4607 if (!udl_ok)
4609 error_at (loc, "string literal with user-defined suffix "
4610 "is invalid in this context");
4611 return error_mark_node;
4613 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4614 curr_type = cpp_userdef_string_remove_type (tok->type);
4615 curr_tok_is_userdef_p = true;
4617 else
4619 string_tree = tok->u.value;
4620 curr_type = tok->type;
4621 curr_tok_is_userdef_p = false;
4624 while (cp_parser_is_string_literal (tok));
4626 /* A string literal built by concatenation has its caret=start at
4627 the start of the initial string, and its finish at the finish of
4628 the final string literal. */
4629 loc = make_location (loc, loc, get_finish (last_tok_loc));
4631 strs = (cpp_string *) obstack_finish (&str_ob);
4634 if (type != CPP_STRING && !wide_ok)
4636 cp_parser_error (parser, "a wide string is invalid in this context");
4637 type = CPP_STRING;
4640 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4641 (parse_in, strs, count, &istr, type))
4643 value = build_string (istr.len, (const char *)istr.text);
4644 free (CONST_CAST (unsigned char *, istr.text));
4645 if (count > 1)
4647 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4648 gcc_assert (g_string_concat_db);
4649 g_string_concat_db->record_string_concatenation (count, locs);
4652 switch (type)
4654 default:
4655 case CPP_STRING:
4656 TREE_TYPE (value) = char_array_type_node;
4657 break;
4658 case CPP_UTF8STRING:
4659 if (flag_char8_t)
4660 TREE_TYPE (value) = char8_array_type_node;
4661 else
4662 TREE_TYPE (value) = char_array_type_node;
4663 break;
4664 case CPP_STRING16:
4665 TREE_TYPE (value) = char16_array_type_node;
4666 break;
4667 case CPP_STRING32:
4668 TREE_TYPE (value) = char32_array_type_node;
4669 break;
4670 case CPP_WSTRING:
4671 TREE_TYPE (value) = wchar_array_type_node;
4672 break;
4675 value = fix_string_type (value);
4677 if (have_suffix_p)
4679 tree literal = build_userdef_literal (suffix_id, value,
4680 OT_NONE, NULL_TREE);
4681 if (lookup_udlit)
4682 value = finish_userdef_string_literal (literal);
4683 else
4684 value = literal;
4687 else
4688 /* cpp_interpret_string has issued an error. */
4689 value = error_mark_node;
4691 if (count > 1)
4693 obstack_free (&str_ob, 0);
4694 obstack_free (&loc_ob, 0);
4697 return cp_expr (value, loc);
4700 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4701 representing the combined, nul-terminated string constant. If
4702 TRANSLATE is true, translate the string to the execution character set.
4703 If WIDE_OK is true, a wide string is valid here.
4705 This function issues an error if a user defined string literal is
4706 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4708 static inline cp_expr
4709 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4711 return cp_parser_string_literal_common (parser, translate, wide_ok,
4712 /*udl_ok=*/false,
4713 /*lookup_udlit=*/false);
4716 /* Parse a string literal or user defined string literal.
4718 user-defined-string-literal :
4719 string-literal ud-suffix
4721 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4723 static inline cp_expr
4724 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4726 return cp_parser_string_literal_common (parser, /*translate=*/true,
4727 /*wide_ok=*/true, /*udl_ok=*/true,
4728 lookup_udlit);
4731 /* Look up a literal operator with the name and the exact arguments. */
4733 static tree
4734 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4736 tree decl = lookup_name (name);
4737 if (!decl || !is_overloaded_fn (decl))
4738 return error_mark_node;
4740 for (lkp_iterator iter (decl); iter; ++iter)
4742 tree fn = *iter;
4744 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4746 unsigned int ix;
4747 bool found = true;
4749 for (ix = 0;
4750 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4751 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4753 tree tparm = TREE_VALUE (parmtypes);
4754 tree targ = TREE_TYPE ((*args)[ix]);
4755 bool ptr = TYPE_PTR_P (tparm);
4756 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4757 if ((ptr || arr || !same_type_p (tparm, targ))
4758 && (!ptr || !arr
4759 || !same_type_p (TREE_TYPE (tparm),
4760 TREE_TYPE (targ))))
4761 found = false;
4764 if (found
4765 && ix == vec_safe_length (args)
4766 /* May be this should be sufficient_parms_p instead,
4767 depending on how exactly should user-defined literals
4768 work in presence of default arguments on the literal
4769 operator parameters. */
4770 && parmtypes == void_list_node)
4771 return decl;
4775 return error_mark_node;
4778 /* Parse a user-defined char constant. Returns a call to a user-defined
4779 literal operator taking the character as an argument. */
4781 static cp_expr
4782 cp_parser_userdef_char_literal (cp_parser *parser)
4784 cp_token *token = cp_lexer_consume_token (parser->lexer);
4785 tree literal = token->u.value;
4786 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4787 tree value = USERDEF_LITERAL_VALUE (literal);
4788 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4789 tree decl, result;
4791 /* Build up a call to the user-defined operator */
4792 /* Lookup the name we got back from the id-expression. */
4793 releasing_vec args;
4794 vec_safe_push (args, value);
4795 decl = lookup_literal_operator (name, args);
4796 if (!decl || decl == error_mark_node)
4798 error ("unable to find character literal operator %qD with %qT argument",
4799 name, TREE_TYPE (value));
4800 return error_mark_node;
4802 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4803 return result;
4806 /* A subroutine of cp_parser_userdef_numeric_literal to
4807 create a char... template parameter pack from a string node. */
4809 static tree
4810 make_char_string_pack (tree value)
4812 tree charvec;
4813 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4814 const unsigned char *str
4815 = (const unsigned char *) TREE_STRING_POINTER (value);
4816 int i, len = TREE_STRING_LENGTH (value) - 1;
4817 tree argvec = make_tree_vec (1);
4819 /* Fill in CHARVEC with all of the parameters. */
4820 charvec = make_tree_vec (len);
4821 for (i = 0; i < len; ++i)
4823 unsigned char s[3] = { '\'', str[i], '\'' };
4824 cpp_string in = { 3, s };
4825 cpp_string out = { 0, 0 };
4826 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4827 return NULL_TREE;
4828 gcc_assert (out.len == 2);
4829 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4830 out.text[0]);
4833 /* Build the argument packs. */
4834 ARGUMENT_PACK_ARGS (argpack) = charvec;
4836 TREE_VEC_ELT (argvec, 0) = argpack;
4838 return argvec;
4841 /* A subroutine of cp_parser_userdef_numeric_literal to
4842 create a char... template parameter pack from a string node. */
4844 static tree
4845 make_string_pack (tree value)
4847 tree charvec;
4848 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4849 const unsigned char *str
4850 = (const unsigned char *) TREE_STRING_POINTER (value);
4851 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4852 int len = TREE_STRING_LENGTH (value) / sz - 1;
4853 tree argvec = make_tree_vec (2);
4855 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4856 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4858 /* First template parm is character type. */
4859 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4861 /* Fill in CHARVEC with all of the parameters. */
4862 charvec = make_tree_vec (len);
4863 for (int i = 0; i < len; ++i)
4864 TREE_VEC_ELT (charvec, i)
4865 = double_int_to_tree (str_char_type_node,
4866 double_int::from_buffer (str + i * sz, sz));
4868 /* Build the argument packs. */
4869 ARGUMENT_PACK_ARGS (argpack) = charvec;
4871 TREE_VEC_ELT (argvec, 1) = argpack;
4873 return argvec;
4876 /* Parse a user-defined numeric constant. returns a call to a user-defined
4877 literal operator. */
4879 static cp_expr
4880 cp_parser_userdef_numeric_literal (cp_parser *parser)
4882 cp_token *token = cp_lexer_consume_token (parser->lexer);
4883 tree literal = token->u.value;
4884 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4885 tree value = USERDEF_LITERAL_VALUE (literal);
4886 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4887 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4888 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4889 tree decl, result;
4891 /* Look for a literal operator taking the exact type of numeric argument
4892 as the literal value. */
4893 releasing_vec args;
4894 vec_safe_push (args, value);
4895 decl = lookup_literal_operator (name, args);
4896 if (decl && decl != error_mark_node)
4898 result = finish_call_expr (decl, &args, false, true,
4899 tf_warning_or_error);
4901 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4903 warning_at (token->location, OPT_Woverflow,
4904 "integer literal exceeds range of %qT type",
4905 long_long_unsigned_type_node);
4907 else
4909 if (overflow > 0)
4910 warning_at (token->location, OPT_Woverflow,
4911 "floating literal exceeds range of %qT type",
4912 long_double_type_node);
4913 else if (overflow < 0)
4914 warning_at (token->location, OPT_Woverflow,
4915 "floating literal truncated to zero");
4918 return result;
4921 /* If the numeric argument didn't work, look for a raw literal
4922 operator taking a const char* argument consisting of the number
4923 in string format. */
4924 args->truncate (0);
4925 vec_safe_push (args, num_string);
4926 decl = lookup_literal_operator (name, args);
4927 if (decl && decl != error_mark_node)
4929 result = finish_call_expr (decl, &args, false, true,
4930 tf_warning_or_error);
4931 return result;
4934 /* If the raw literal didn't work, look for a non-type template
4935 function with parameter pack char.... Call the function with
4936 template parameter characters representing the number. */
4937 args->truncate (0);
4938 decl = lookup_literal_operator (name, args);
4939 if (decl && decl != error_mark_node)
4941 tree tmpl_args = make_char_string_pack (num_string);
4942 if (tmpl_args == NULL_TREE)
4944 error ("failed to translate literal to execution character set %qT",
4945 num_string);
4946 return error_mark_node;
4948 decl = lookup_template_function (decl, tmpl_args);
4949 result = finish_call_expr (decl, &args, false, true,
4950 tf_warning_or_error);
4951 return result;
4954 /* In C++14 the standard library defines complex number suffixes that
4955 conflict with GNU extensions. Prefer them if <complex> is #included. */
4956 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4957 bool i14 = (cxx_dialect > cxx11
4958 && (id_equal (suffix_id, "i")
4959 || id_equal (suffix_id, "if")
4960 || id_equal (suffix_id, "il")));
4961 diagnostic_t kind = DK_ERROR;
4962 int opt = 0;
4964 if (i14 && ext)
4966 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4967 LOOK_want::NORMAL, false);
4968 if (cxlit == error_mark_node)
4970 /* No <complex>, so pedwarn and use GNU semantics. */
4971 kind = DK_PEDWARN;
4972 opt = OPT_Wpedantic;
4976 bool complained
4977 = emit_diagnostic (kind, input_location, opt,
4978 "unable to find numeric literal operator %qD", name);
4980 if (!complained)
4981 /* Don't inform either. */;
4982 else if (i14)
4984 inform (token->location, "add %<using namespace std::complex_literals%> "
4985 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4986 "suffixes");
4987 if (ext)
4988 inform (token->location, "or use %<j%> instead of %<i%> for the "
4989 "GNU built-in suffix");
4991 else if (!ext)
4992 inform (token->location, "use %<-fext-numeric-literals%> "
4993 "to enable more built-in suffixes");
4995 if (kind == DK_ERROR)
4996 value = error_mark_node;
4997 else
4999 /* Use the built-in semantics. */
5000 tree type;
5001 if (id_equal (suffix_id, "i"))
5003 if (TREE_CODE (value) == INTEGER_CST)
5004 type = integer_type_node;
5005 else
5006 type = double_type_node;
5008 else if (id_equal (suffix_id, "if"))
5009 type = float_type_node;
5010 else /* if (id_equal (suffix_id, "il")) */
5011 type = long_double_type_node;
5013 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5014 build_zero_cst (type), fold_convert (type, value));
5017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5018 /* Avoid repeated diagnostics. */
5019 token->u.value = value;
5020 return value;
5023 /* Parse a user-defined string constant. Returns a call to a user-defined
5024 literal operator taking a character pointer and the length of the string
5025 as arguments. */
5027 static tree
5028 finish_userdef_string_literal (tree literal)
5030 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5031 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5032 tree value = USERDEF_LITERAL_VALUE (literal);
5033 int len = TREE_STRING_LENGTH (value)
5034 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5035 tree decl;
5037 /* Build up a call to the user-defined operator. */
5038 /* Lookup the name we got back from the id-expression. */
5039 releasing_vec args;
5040 vec_safe_push (args, value);
5041 vec_safe_push (args, build_int_cst (size_type_node, len));
5042 decl = lookup_literal_operator (name, args);
5044 if (decl && decl != error_mark_node)
5045 return finish_call_expr (decl, &args, false, true,
5046 tf_warning_or_error);
5048 /* Look for a suitable template function, either (C++20) with a single
5049 parameter of class type, or (N3599) with typename parameter CharT and
5050 parameter pack CharT... */
5051 args->truncate (0);
5052 decl = lookup_literal_operator (name, args);
5053 if (decl && decl != error_mark_node)
5055 /* Use resolve_nondeduced_context to try to choose one form of template
5056 or the other. */
5057 tree tmpl_args = make_tree_vec (1);
5058 TREE_VEC_ELT (tmpl_args, 0) = value;
5059 decl = lookup_template_function (decl, tmpl_args);
5060 tree res = resolve_nondeduced_context (decl, tf_none);
5061 if (DECL_P (res))
5062 decl = res;
5063 else
5065 TREE_OPERAND (decl, 1) = make_string_pack (value);
5066 res = resolve_nondeduced_context (decl, tf_none);
5067 if (DECL_P (res))
5068 decl = res;
5070 if (!DECL_P (decl) && cxx_dialect > cxx17)
5071 TREE_OPERAND (decl, 1) = tmpl_args;
5072 return finish_call_expr (decl, &args, false, true,
5073 tf_warning_or_error);
5076 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5077 name, TREE_TYPE (value), size_type_node);
5078 return error_mark_node;
5082 /* Basic concepts [gram.basic] */
5084 /* Parse a translation-unit.
5086 translation-unit:
5087 declaration-seq [opt] */
5089 static void
5090 cp_parser_translation_unit (cp_parser* parser)
5092 gcc_checking_assert (!cp_error_declarator);
5094 /* Create the declarator obstack. */
5095 gcc_obstack_init (&declarator_obstack);
5096 /* Create the error declarator. */
5097 cp_error_declarator = make_declarator (cdk_error);
5098 /* Create the empty parameter list. */
5099 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5100 UNKNOWN_LOCATION);
5101 /* Remember where the base of the declarator obstack lies. */
5102 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5104 push_deferring_access_checks (flag_access_control
5105 ? dk_no_deferred : dk_no_check);
5107 module_parse mp_state = MP_NOT_MODULE;
5108 if (modules_p () && !header_module_p ())
5109 mp_state = MP_FIRST;
5111 bool implicit_extern_c = false;
5113 /* Parse until EOF. */
5114 for (;;)
5116 cp_token *token = cp_lexer_peek_token (parser->lexer);
5118 /* If we're entering or exiting a region that's implicitly
5119 extern "C", modify the lang context appropriately. This is
5120 so horrible. Please die. */
5121 if (implicit_extern_c
5122 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5124 implicit_extern_c = !implicit_extern_c;
5125 if (implicit_extern_c)
5126 push_lang_context (lang_name_c);
5127 else
5128 pop_lang_context ();
5131 if (token->type == CPP_EOF)
5132 break;
5134 if (modules_p ())
5136 /* Top-level module declarations are ok, and change the
5137 portion of file we're in. Top-level import declarations
5138 are significant for the import portions. */
5140 cp_token *next = token;
5141 bool exporting = token->keyword == RID__EXPORT;
5142 if (exporting)
5144 cp_lexer_consume_token (parser->lexer);
5145 next = cp_lexer_peek_token (parser->lexer);
5147 if (next->keyword == RID__MODULE)
5149 mp_state
5150 = cp_parser_module_declaration (parser, mp_state, exporting);
5151 continue;
5153 else if (next->keyword == RID__IMPORT)
5155 if (mp_state == MP_FIRST)
5156 mp_state = MP_NOT_MODULE;
5157 cp_parser_import_declaration (parser, mp_state, exporting);
5158 continue;
5160 else
5161 gcc_checking_assert (!exporting);
5163 if (mp_state == MP_GLOBAL && token->main_source_p)
5165 static bool warned = false;
5166 if (!warned)
5168 warned = true;
5169 error_at (token->location,
5170 "global module fragment contents must be"
5171 " from preprocessor inclusion");
5176 /* This relies on the ordering of module_parse values. */
5177 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5178 /* We're no longer in the import portion of a named module. */
5179 mp_state = module_parse (mp_state + 1);
5180 else if (mp_state == MP_FIRST)
5181 mp_state = MP_NOT_MODULE;
5183 if (token->type == CPP_CLOSE_BRACE)
5185 cp_parser_error (parser, "expected declaration");
5186 cp_lexer_consume_token (parser->lexer);
5187 /* If the next token is now a `;', consume it. */
5188 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5189 cp_lexer_consume_token (parser->lexer);
5191 else
5192 cp_parser_toplevel_declaration (parser);
5195 /* Get rid of the token array; we don't need it any more. */
5196 cp_lexer_destroy (parser->lexer);
5197 parser->lexer = NULL;
5199 /* The EOF should have reset this. */
5200 gcc_checking_assert (!implicit_extern_c);
5202 /* Make sure the declarator obstack was fully cleaned up. */
5203 gcc_assert (obstack_next_free (&declarator_obstack)
5204 == declarator_obstack_base);
5207 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5208 decltype context. */
5210 static inline tsubst_flags_t
5211 complain_flags (bool decltype_p)
5213 tsubst_flags_t complain = tf_warning_or_error;
5214 if (decltype_p)
5215 complain |= tf_decltype;
5216 return complain;
5219 /* We're about to parse a collection of statements. If we're currently
5220 parsing tentatively, set up a firewall so that any nested
5221 cp_parser_commit_to_tentative_parse won't affect the current context. */
5223 static cp_token_position
5224 cp_parser_start_tentative_firewall (cp_parser *parser)
5226 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5227 return 0;
5229 cp_parser_parse_tentatively (parser);
5230 cp_parser_commit_to_topmost_tentative_parse (parser);
5231 return cp_lexer_token_position (parser->lexer, false);
5234 /* We've finished parsing the collection of statements. Wrap up the
5235 firewall and replace the relevant tokens with the parsed form. */
5237 static void
5238 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5239 tree expr)
5241 if (!start)
5242 return;
5244 /* Finish the firewall level. */
5245 cp_parser_parse_definitely (parser);
5246 /* And remember the result of the parse for when we try again. */
5247 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5248 token->type = CPP_PREPARSED_EXPR;
5249 token->u.value = expr;
5250 token->keyword = RID_MAX;
5251 cp_lexer_purge_tokens_after (parser->lexer, start);
5254 /* Like the above functions, but let the user modify the tokens. Used by
5255 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5256 later parses, so it makes sense to localize the effects of
5257 cp_parser_commit_to_tentative_parse. */
5259 struct tentative_firewall
5261 cp_parser *parser;
5262 bool set;
5264 tentative_firewall (cp_parser *p): parser(p)
5266 /* If we're currently parsing tentatively, start a committed level as a
5267 firewall and then an inner tentative parse. */
5268 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5270 cp_parser_parse_tentatively (parser);
5271 cp_parser_commit_to_topmost_tentative_parse (parser);
5272 cp_parser_parse_tentatively (parser);
5276 ~tentative_firewall()
5278 if (set)
5280 /* Finish the inner tentative parse and the firewall, propagating any
5281 uncommitted error state to the outer tentative parse. */
5282 bool err = cp_parser_error_occurred (parser);
5283 cp_parser_parse_definitely (parser);
5284 cp_parser_parse_definitely (parser);
5285 if (err)
5286 cp_parser_simulate_error (parser);
5291 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5292 This class is for tracking such a matching pair of symbols.
5293 In particular, it tracks the location of the first token,
5294 so that if the second token is missing, we can highlight the
5295 location of the first token when notifying the user about the
5296 problem. */
5298 template <typename traits_t>
5299 class token_pair
5301 public:
5302 /* token_pair's ctor. */
5303 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5305 /* If the next token is the opening symbol for this pair, consume it and
5306 return true.
5307 Otherwise, issue an error and return false.
5308 In either case, record the location of the opening token. */
5310 bool require_open (cp_parser *parser)
5312 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5313 return cp_parser_require (parser, traits_t::open_token_type,
5314 traits_t::required_token_open);
5317 /* Consume the next token from PARSER, recording its location as
5318 that of the opening token within the pair. */
5320 cp_token * consume_open (cp_parser *parser)
5322 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5323 gcc_assert (tok->type == traits_t::open_token_type);
5324 m_open_loc = tok->location;
5325 return tok;
5328 /* If the next token is the closing symbol for this pair, consume it
5329 and return it.
5330 Otherwise, issue an error, highlighting the location of the
5331 corresponding opening token, and return NULL. */
5333 cp_token *require_close (cp_parser *parser) const
5335 return cp_parser_require (parser, traits_t::close_token_type,
5336 traits_t::required_token_close,
5337 m_open_loc);
5340 location_t open_location () const { return m_open_loc; }
5342 private:
5343 location_t m_open_loc;
5346 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5348 struct matching_paren_traits
5350 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5351 static const enum required_token required_token_open = RT_OPEN_PAREN;
5352 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5353 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5356 /* "matching_parens" is a token_pair<T> class for tracking matching
5357 pairs of parentheses. */
5359 typedef token_pair<matching_paren_traits> matching_parens;
5361 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5363 struct matching_brace_traits
5365 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5366 static const enum required_token required_token_open = RT_OPEN_BRACE;
5367 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5368 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5371 /* "matching_braces" is a token_pair<T> class for tracking matching
5372 pairs of braces. */
5374 typedef token_pair<matching_brace_traits> matching_braces;
5377 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5378 enclosing parentheses. */
5380 static cp_expr
5381 cp_parser_statement_expr (cp_parser *parser)
5383 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5385 /* Consume the '('. */
5386 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5387 matching_parens parens;
5388 parens.consume_open (parser);
5389 /* Start the statement-expression. */
5390 tree expr = begin_stmt_expr ();
5391 /* Parse the compound-statement. */
5392 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5393 /* Finish up. */
5394 expr = finish_stmt_expr (expr, false);
5395 /* Consume the ')'. */
5396 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5397 if (!parens.require_close (parser))
5398 cp_parser_skip_to_end_of_statement (parser);
5400 cp_parser_end_tentative_firewall (parser, start, expr);
5401 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5402 return cp_expr (expr, combined_loc);
5405 /* Expressions [gram.expr] */
5407 /* Parse a fold-operator.
5409 fold-operator:
5410 - * / % ^ & | = < > << >>
5411 = -= *= /= %= ^= &= |= <<= >>=
5412 == != <= >= && || , .* ->*
5414 This returns the tree code corresponding to the matched operator
5415 as an int. When the current token matches a compound assignment
5416 operator, the resulting tree code is the negative value of the
5417 non-assignment operator. */
5419 static int
5420 cp_parser_fold_operator (cp_token *token)
5422 switch (token->type)
5424 case CPP_PLUS: return PLUS_EXPR;
5425 case CPP_MINUS: return MINUS_EXPR;
5426 case CPP_MULT: return MULT_EXPR;
5427 case CPP_DIV: return TRUNC_DIV_EXPR;
5428 case CPP_MOD: return TRUNC_MOD_EXPR;
5429 case CPP_XOR: return BIT_XOR_EXPR;
5430 case CPP_AND: return BIT_AND_EXPR;
5431 case CPP_OR: return BIT_IOR_EXPR;
5432 case CPP_LSHIFT: return LSHIFT_EXPR;
5433 case CPP_RSHIFT: return RSHIFT_EXPR;
5435 case CPP_EQ: return -NOP_EXPR;
5436 case CPP_PLUS_EQ: return -PLUS_EXPR;
5437 case CPP_MINUS_EQ: return -MINUS_EXPR;
5438 case CPP_MULT_EQ: return -MULT_EXPR;
5439 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5440 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5441 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5442 case CPP_AND_EQ: return -BIT_AND_EXPR;
5443 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5444 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5445 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5447 case CPP_EQ_EQ: return EQ_EXPR;
5448 case CPP_NOT_EQ: return NE_EXPR;
5449 case CPP_LESS: return LT_EXPR;
5450 case CPP_GREATER: return GT_EXPR;
5451 case CPP_LESS_EQ: return LE_EXPR;
5452 case CPP_GREATER_EQ: return GE_EXPR;
5454 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5455 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5457 case CPP_COMMA: return COMPOUND_EXPR;
5459 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5460 case CPP_DEREF_STAR: return MEMBER_REF;
5462 default: return ERROR_MARK;
5466 /* Returns true if CODE indicates a binary expression, which is not allowed in
5467 the LHS of a fold-expression. More codes will need to be added to use this
5468 function in other contexts. */
5470 static bool
5471 is_binary_op (tree_code code)
5473 switch (code)
5475 case PLUS_EXPR:
5476 case POINTER_PLUS_EXPR:
5477 case MINUS_EXPR:
5478 case MULT_EXPR:
5479 case TRUNC_DIV_EXPR:
5480 case TRUNC_MOD_EXPR:
5481 case BIT_XOR_EXPR:
5482 case BIT_AND_EXPR:
5483 case BIT_IOR_EXPR:
5484 case LSHIFT_EXPR:
5485 case RSHIFT_EXPR:
5487 case MODOP_EXPR:
5489 case EQ_EXPR:
5490 case NE_EXPR:
5491 case LE_EXPR:
5492 case GE_EXPR:
5493 case LT_EXPR:
5494 case GT_EXPR:
5496 case TRUTH_ANDIF_EXPR:
5497 case TRUTH_ORIF_EXPR:
5499 case COMPOUND_EXPR:
5501 case DOTSTAR_EXPR:
5502 case MEMBER_REF:
5503 return true;
5505 default:
5506 return false;
5510 /* If the next token is a suitable fold operator, consume it and return as
5511 the function above. */
5513 static int
5514 cp_parser_fold_operator (cp_parser *parser)
5516 cp_token* token = cp_lexer_peek_token (parser->lexer);
5517 int code = cp_parser_fold_operator (token);
5518 if (code != ERROR_MARK)
5519 cp_lexer_consume_token (parser->lexer);
5520 return code;
5523 /* Parse a fold-expression.
5525 fold-expression:
5526 ( ... folding-operator cast-expression)
5527 ( cast-expression folding-operator ... )
5528 ( cast-expression folding operator ... folding-operator cast-expression)
5530 Note that the '(' and ')' are matched in primary expression. */
5532 static cp_expr
5533 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5535 cp_id_kind pidk;
5537 // Left fold.
5538 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5540 if (expr1)
5541 return error_mark_node;
5542 cp_lexer_consume_token (parser->lexer);
5543 int op = cp_parser_fold_operator (parser);
5544 if (op == ERROR_MARK)
5546 cp_parser_error (parser, "expected binary operator");
5547 return error_mark_node;
5550 tree expr = cp_parser_cast_expression (parser, false, false,
5551 false, &pidk);
5552 if (expr == error_mark_node)
5553 return error_mark_node;
5554 return finish_left_unary_fold_expr (expr, op);
5557 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5558 int op = cp_parser_fold_operator (parser);
5559 if (op == ERROR_MARK)
5561 cp_parser_error (parser, "expected binary operator");
5562 return error_mark_node;
5565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5567 cp_parser_error (parser, "expected ...");
5568 return error_mark_node;
5570 cp_lexer_consume_token (parser->lexer);
5572 /* The operands of a fold-expression are cast-expressions, so binary or
5573 conditional expressions are not allowed. We check this here to avoid
5574 tentative parsing. */
5575 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5576 /* OK, the expression was parenthesized. */;
5577 else if (is_binary_op (TREE_CODE (expr1)))
5578 error_at (location_of (expr1),
5579 "binary expression in operand of fold-expression");
5580 else if (TREE_CODE (expr1) == COND_EXPR
5581 || (REFERENCE_REF_P (expr1)
5582 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5583 error_at (location_of (expr1),
5584 "conditional expression in operand of fold-expression");
5586 // Right fold.
5587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5588 return finish_right_unary_fold_expr (expr1, op);
5590 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5592 cp_parser_error (parser, "mismatched operator in fold-expression");
5593 return error_mark_node;
5595 cp_lexer_consume_token (parser->lexer);
5597 // Binary left or right fold.
5598 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5599 if (expr2 == error_mark_node)
5600 return error_mark_node;
5601 return finish_binary_fold_expr (expr1, expr2, op);
5604 /* Parse a primary-expression.
5606 primary-expression:
5607 literal
5608 this
5609 ( expression )
5610 id-expression
5611 lambda-expression (C++11)
5613 GNU Extensions:
5615 primary-expression:
5616 ( compound-statement )
5617 __builtin_va_arg ( assignment-expression , type-id )
5618 __builtin_offsetof ( type-id , offsetof-expression )
5620 C++ Extensions:
5621 __has_nothrow_assign ( type-id )
5622 __has_nothrow_constructor ( type-id )
5623 __has_nothrow_copy ( type-id )
5624 __has_trivial_assign ( type-id )
5625 __has_trivial_constructor ( type-id )
5626 __has_trivial_copy ( type-id )
5627 __has_trivial_destructor ( type-id )
5628 __has_virtual_destructor ( type-id )
5629 __is_abstract ( type-id )
5630 __is_base_of ( type-id , type-id )
5631 __is_class ( type-id )
5632 __is_empty ( type-id )
5633 __is_enum ( type-id )
5634 __is_final ( type-id )
5635 __is_literal_type ( type-id )
5636 __is_pod ( type-id )
5637 __is_polymorphic ( type-id )
5638 __is_std_layout ( type-id )
5639 __is_trivial ( type-id )
5640 __is_union ( type-id )
5642 Objective-C++ Extension:
5644 primary-expression:
5645 objc-expression
5647 literal:
5648 __null
5650 ADDRESS_P is true iff this expression was immediately preceded by
5651 "&" and therefore might denote a pointer-to-member. CAST_P is true
5652 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5653 true iff this expression is a template argument.
5655 Returns a representation of the expression. Upon return, *IDK
5656 indicates what kind of id-expression (if any) was present. */
5658 static cp_expr
5659 cp_parser_primary_expression (cp_parser *parser,
5660 bool address_p,
5661 bool cast_p,
5662 bool template_arg_p,
5663 bool decltype_p,
5664 cp_id_kind *idk)
5666 cp_token *token = NULL;
5668 /* Assume the primary expression is not an id-expression. */
5669 *idk = CP_ID_KIND_NONE;
5671 /* Peek at the next token. */
5672 token = cp_lexer_peek_token (parser->lexer);
5673 switch ((int) token->type)
5675 /* literal:
5676 integer-literal
5677 character-literal
5678 floating-literal
5679 string-literal
5680 boolean-literal
5681 pointer-literal
5682 user-defined-literal */
5683 case CPP_CHAR:
5684 case CPP_CHAR16:
5685 case CPP_CHAR32:
5686 case CPP_WCHAR:
5687 case CPP_UTF8CHAR:
5688 case CPP_NUMBER:
5689 case CPP_PREPARSED_EXPR:
5690 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5691 return cp_parser_userdef_numeric_literal (parser);
5692 token = cp_lexer_consume_token (parser->lexer);
5693 if (TREE_CODE (token->u.value) == FIXED_CST)
5695 error_at (token->location,
5696 "fixed-point types not supported in C++");
5697 return error_mark_node;
5699 /* Floating-point literals are only allowed in an integral
5700 constant expression if they are cast to an integral or
5701 enumeration type. */
5702 if ((TREE_CODE (token->u.value) == REAL_CST
5703 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5704 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5705 && parser->integral_constant_expression_p
5706 && pedantic)
5708 /* CAST_P will be set even in invalid code like "int(2.7 +
5709 ...)". Therefore, we have to check that the next token
5710 is sure to end the cast. */
5711 if (cast_p)
5713 cp_token *next_token;
5715 next_token = cp_lexer_peek_token (parser->lexer);
5716 if (/* The comma at the end of an
5717 enumerator-definition. */
5718 next_token->type != CPP_COMMA
5719 /* The curly brace at the end of an enum-specifier. */
5720 && next_token->type != CPP_CLOSE_BRACE
5721 /* The end of a statement. */
5722 && next_token->type != CPP_SEMICOLON
5723 /* The end of the cast-expression. */
5724 && next_token->type != CPP_CLOSE_PAREN
5725 /* The end of an array bound. */
5726 && next_token->type != CPP_CLOSE_SQUARE
5727 /* The closing ">" in a template-argument-list. */
5728 && (next_token->type != CPP_GREATER
5729 || parser->greater_than_is_operator_p)
5730 /* C++0x only: A ">>" treated like two ">" tokens,
5731 in a template-argument-list. */
5732 && (next_token->type != CPP_RSHIFT
5733 || (cxx_dialect == cxx98)
5734 || parser->greater_than_is_operator_p))
5735 cast_p = false;
5738 /* If we are within a cast, then the constraint that the
5739 cast is to an integral or enumeration type will be
5740 checked at that point. If we are not within a cast, then
5741 this code is invalid. */
5742 if (!cast_p)
5743 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5745 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5746 .maybe_add_location_wrapper ());
5748 case CPP_CHAR_USERDEF:
5749 case CPP_CHAR16_USERDEF:
5750 case CPP_CHAR32_USERDEF:
5751 case CPP_WCHAR_USERDEF:
5752 case CPP_UTF8CHAR_USERDEF:
5753 return cp_parser_userdef_char_literal (parser);
5755 case CPP_STRING:
5756 case CPP_STRING16:
5757 case CPP_STRING32:
5758 case CPP_WSTRING:
5759 case CPP_UTF8STRING:
5760 case CPP_STRING_USERDEF:
5761 case CPP_STRING16_USERDEF:
5762 case CPP_STRING32_USERDEF:
5763 case CPP_WSTRING_USERDEF:
5764 case CPP_UTF8STRING_USERDEF:
5765 /* ??? Should wide strings be allowed when parser->translate_strings_p
5766 is false (i.e. in attributes)? If not, we can kill the third
5767 argument to cp_parser_string_literal. */
5768 if (parser->translate_strings_p)
5769 return (cp_parser_userdef_string_literal (parser,
5770 /*lookup_udlit=*/true)
5771 .maybe_add_location_wrapper ());
5772 else
5773 return (cp_parser_string_literal (parser,
5774 /*translate=*/false,
5775 /*wide_ok=*/true)
5776 .maybe_add_location_wrapper ());
5778 case CPP_OPEN_PAREN:
5779 /* If we see `( { ' then we are looking at the beginning of
5780 a GNU statement-expression. */
5781 if (cp_parser_allow_gnu_extensions_p (parser)
5782 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5784 /* Statement-expressions are not allowed by the standard. */
5785 pedwarn (token->location, OPT_Wpedantic,
5786 "ISO C++ forbids braced-groups within expressions");
5788 /* And they're not allowed outside of a function-body; you
5789 cannot, for example, write:
5791 int i = ({ int j = 3; j + 1; });
5793 at class or namespace scope. */
5794 if (!parser->in_function_body
5795 || parser->in_template_argument_list_p)
5797 error_at (token->location,
5798 "statement-expressions are not allowed outside "
5799 "functions nor in template-argument lists");
5800 cp_parser_skip_to_end_of_block_or_statement (parser);
5801 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5802 cp_lexer_consume_token (parser->lexer);
5803 return error_mark_node;
5805 else
5806 return cp_parser_statement_expr (parser);
5808 /* Otherwise it's a normal parenthesized expression. */
5810 cp_expr expr;
5811 bool saved_greater_than_is_operator_p;
5813 location_t open_paren_loc = token->location;
5815 /* Consume the `('. */
5816 matching_parens parens;
5817 parens.consume_open (parser);
5818 /* Within a parenthesized expression, a `>' token is always
5819 the greater-than operator. */
5820 saved_greater_than_is_operator_p
5821 = parser->greater_than_is_operator_p;
5822 parser->greater_than_is_operator_p = true;
5824 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5825 /* Left fold expression. */
5826 expr = NULL_TREE;
5827 else
5828 /* Parse the parenthesized expression. */
5829 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5831 token = cp_lexer_peek_token (parser->lexer);
5832 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5834 expr = cp_parser_fold_expression (parser, expr);
5835 if (expr != error_mark_node
5836 && cxx_dialect < cxx17)
5837 pedwarn (input_location, OPT_Wc__17_extensions,
5838 "fold-expressions only available with %<-std=c++17%> "
5839 "or %<-std=gnu++17%>");
5841 else
5842 /* Let the front end know that this expression was
5843 enclosed in parentheses. This matters in case, for
5844 example, the expression is of the form `A::B', since
5845 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5846 not. */
5847 expr = finish_parenthesized_expr (expr);
5849 /* DR 705: Wrapping an unqualified name in parentheses
5850 suppresses arg-dependent lookup. We want to pass back
5851 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5852 (c++/37862), but none of the others. */
5853 if (*idk != CP_ID_KIND_QUALIFIED)
5854 *idk = CP_ID_KIND_NONE;
5856 /* The `>' token might be the end of a template-id or
5857 template-parameter-list now. */
5858 parser->greater_than_is_operator_p
5859 = saved_greater_than_is_operator_p;
5861 /* Consume the `)'. */
5862 token = cp_lexer_peek_token (parser->lexer);
5863 location_t close_paren_loc = token->location;
5864 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5865 expr.set_range (open_paren_loc, close_paren_loc);
5866 if (no_wparens)
5867 suppress_warning (expr, OPT_Wparentheses);
5868 if (!parens.require_close (parser)
5869 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5870 cp_parser_skip_to_end_of_statement (parser);
5872 return expr;
5875 case CPP_OPEN_SQUARE:
5877 if (c_dialect_objc ())
5879 /* We might have an Objective-C++ message. */
5880 cp_parser_parse_tentatively (parser);
5881 tree msg = cp_parser_objc_message_expression (parser);
5882 /* If that works out, we're done ... */
5883 if (cp_parser_parse_definitely (parser))
5884 return msg;
5885 /* ... else, fall though to see if it's a lambda. */
5887 cp_expr lam = cp_parser_lambda_expression (parser);
5888 /* Don't warn about a failed tentative parse. */
5889 if (cp_parser_error_occurred (parser))
5890 return error_mark_node;
5891 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5892 return lam;
5895 case CPP_OBJC_STRING:
5896 if (c_dialect_objc ())
5897 /* We have an Objective-C++ string literal. */
5898 return cp_parser_objc_expression (parser);
5899 cp_parser_error (parser, "expected primary-expression");
5900 return error_mark_node;
5902 case CPP_KEYWORD:
5903 switch (token->keyword)
5905 /* These two are the boolean literals. */
5906 case RID_TRUE:
5907 cp_lexer_consume_token (parser->lexer);
5908 return cp_expr (boolean_true_node, token->location);
5909 case RID_FALSE:
5910 cp_lexer_consume_token (parser->lexer);
5911 return cp_expr (boolean_false_node, token->location);
5913 /* The `__null' literal. */
5914 case RID_NULL:
5915 cp_lexer_consume_token (parser->lexer);
5916 return cp_expr (null_node, token->location);
5918 /* The `nullptr' literal. */
5919 case RID_NULLPTR:
5920 cp_lexer_consume_token (parser->lexer);
5921 return cp_expr (nullptr_node, token->location);
5923 /* Recognize the `this' keyword. */
5924 case RID_THIS:
5925 cp_lexer_consume_token (parser->lexer);
5926 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5928 error_at (token->location,
5929 "%<this%> may not be used in this context");
5930 return error_mark_node;
5932 /* Pointers cannot appear in constant-expressions. */
5933 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5934 return error_mark_node;
5935 return cp_expr (finish_this_expr (), token->location);
5937 /* The `operator' keyword can be the beginning of an
5938 id-expression. */
5939 case RID_OPERATOR:
5940 goto id_expression;
5942 case RID_FUNCTION_NAME:
5943 case RID_PRETTY_FUNCTION_NAME:
5944 case RID_C99_FUNCTION_NAME:
5946 non_integral_constant name;
5948 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5949 __func__ are the names of variables -- but they are
5950 treated specially. Therefore, they are handled here,
5951 rather than relying on the generic id-expression logic
5952 below. Grammatically, these names are id-expressions.
5954 Consume the token. */
5955 token = cp_lexer_consume_token (parser->lexer);
5957 switch (token->keyword)
5959 case RID_FUNCTION_NAME:
5960 name = NIC_FUNC_NAME;
5961 break;
5962 case RID_PRETTY_FUNCTION_NAME:
5963 name = NIC_PRETTY_FUNC;
5964 break;
5965 case RID_C99_FUNCTION_NAME:
5966 name = NIC_C99_FUNC;
5967 break;
5968 default:
5969 gcc_unreachable ();
5972 if (cp_parser_non_integral_constant_expression (parser, name))
5973 return error_mark_node;
5975 /* Look up the name. */
5976 return finish_fname (token->u.value);
5979 case RID_VA_ARG:
5981 tree expression;
5982 tree type;
5983 location_t type_location;
5984 location_t start_loc
5985 = cp_lexer_peek_token (parser->lexer)->location;
5986 /* The `__builtin_va_arg' construct is used to handle
5987 `va_arg'. Consume the `__builtin_va_arg' token. */
5988 cp_lexer_consume_token (parser->lexer);
5989 /* Look for the opening `('. */
5990 matching_parens parens;
5991 parens.require_open (parser);
5992 /* Now, parse the assignment-expression. */
5993 expression = cp_parser_assignment_expression (parser);
5994 /* Look for the `,'. */
5995 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5996 type_location = cp_lexer_peek_token (parser->lexer)->location;
5997 /* Parse the type-id. */
5999 type_id_in_expr_sentinel s (parser);
6000 type = cp_parser_type_id (parser);
6002 /* Look for the closing `)'. */
6003 location_t finish_loc
6004 = cp_lexer_peek_token (parser->lexer)->location;
6005 parens.require_close (parser);
6006 /* Using `va_arg' in a constant-expression is not
6007 allowed. */
6008 if (cp_parser_non_integral_constant_expression (parser,
6009 NIC_VA_ARG))
6010 return error_mark_node;
6011 /* Construct a location of the form:
6012 __builtin_va_arg (v, int)
6013 ~~~~~~~~~~~~~~~~~~~~~^~~~
6014 with the caret at the type, ranging from the start of the
6015 "__builtin_va_arg" token to the close paren. */
6016 location_t combined_loc
6017 = make_location (type_location, start_loc, finish_loc);
6018 return build_x_va_arg (combined_loc, expression, type);
6021 case RID_OFFSETOF:
6022 return cp_parser_builtin_offsetof (parser);
6024 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
6025 case RID_##CODE:
6026 #include "cp-trait.def"
6027 #undef DEFTRAIT_EXPR
6028 return cp_parser_trait (parser, token->keyword);
6030 // C++ concepts
6031 case RID_REQUIRES:
6032 return cp_parser_requires_expression (parser);
6034 /* Objective-C++ expressions. */
6035 case RID_AT_ENCODE:
6036 case RID_AT_PROTOCOL:
6037 case RID_AT_SELECTOR:
6038 return cp_parser_objc_expression (parser);
6040 case RID_OMP_ALL_MEMORY:
6041 gcc_assert (flag_openmp);
6042 cp_lexer_consume_token (parser->lexer);
6043 error_at (token->location,
6044 "%<omp_all_memory%> may only be used in OpenMP "
6045 "%<depend%> clause");
6046 return error_mark_node;
6048 case RID_TEMPLATE:
6049 if (parser->in_function_body
6050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6051 == CPP_LESS))
6053 error_at (token->location,
6054 "a template declaration cannot appear at block scope");
6055 cp_parser_skip_to_end_of_block_or_statement (parser);
6056 return error_mark_node;
6058 /* FALLTHRU */
6059 default:
6060 cp_parser_error (parser, "expected primary-expression");
6061 return error_mark_node;
6064 /* An id-expression can start with either an identifier, a
6065 `::' as the beginning of a qualified-id, or the "operator"
6066 keyword. */
6067 case CPP_NAME:
6068 case CPP_SCOPE:
6069 case CPP_TEMPLATE_ID:
6070 case CPP_NESTED_NAME_SPECIFIER:
6072 id_expression:
6073 cp_expr id_expression;
6074 cp_expr decl;
6075 const char *error_msg;
6076 bool template_p;
6077 bool done;
6078 cp_token *id_expr_token;
6080 /* Parse the id-expression. */
6081 id_expression
6082 = cp_parser_id_expression (parser,
6083 /*template_keyword_p=*/false,
6084 /*check_dependency_p=*/true,
6085 &template_p,
6086 /*declarator_p=*/false,
6087 /*optional_p=*/false);
6088 if (id_expression == error_mark_node)
6089 return error_mark_node;
6090 id_expr_token = token;
6091 token = cp_lexer_peek_token (parser->lexer);
6092 done = (token->type != CPP_OPEN_SQUARE
6093 && token->type != CPP_OPEN_PAREN
6094 && token->type != CPP_DOT
6095 && token->type != CPP_DEREF
6096 && token->type != CPP_PLUS_PLUS
6097 && token->type != CPP_MINUS_MINUS);
6098 /* If we have a template-id, then no further lookup is
6099 required. If the template-id was for a template-class, we
6100 will sometimes have a TYPE_DECL at this point. */
6101 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6102 || TREE_CODE (id_expression) == TYPE_DECL)
6103 decl = id_expression;
6104 /* Look up the name. */
6105 else
6107 tree ambiguous_decls;
6109 /* If we already know that this lookup is ambiguous, then
6110 we've already issued an error message; there's no reason
6111 to check again. */
6112 if (id_expr_token->type == CPP_NAME
6113 && id_expr_token->error_reported)
6115 cp_parser_simulate_error (parser);
6116 return error_mark_node;
6119 decl = cp_parser_lookup_name (parser, id_expression,
6120 none_type,
6121 template_p,
6122 /*is_namespace=*/false,
6123 /*check_dependency=*/true,
6124 &ambiguous_decls,
6125 id_expression.get_location ());
6126 /* If the lookup was ambiguous, an error will already have
6127 been issued. */
6128 if (ambiguous_decls)
6129 return error_mark_node;
6131 /* In Objective-C++, we may have an Objective-C 2.0
6132 dot-syntax for classes here. */
6133 if (c_dialect_objc ()
6134 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6135 && TREE_CODE (decl) == TYPE_DECL
6136 && objc_is_class_name (decl))
6138 tree component;
6139 cp_lexer_consume_token (parser->lexer);
6140 component = cp_parser_identifier (parser);
6141 if (component == error_mark_node)
6142 return error_mark_node;
6144 tree result = objc_build_class_component_ref (id_expression,
6145 component);
6146 /* Build a location of the form:
6147 expr.component
6148 ~~~~~^~~~~~~~~
6149 with caret at the start of the component name (at
6150 input_location), ranging from the start of the id_expression
6151 to the end of the component name. */
6152 location_t combined_loc
6153 = make_location (input_location, id_expression.get_start (),
6154 get_finish (input_location));
6155 protected_set_expr_location (result, combined_loc);
6156 return result;
6159 /* In Objective-C++, an instance variable (ivar) may be preferred
6160 to whatever cp_parser_lookup_name() found.
6161 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6162 rest of c-family, we have to do a little extra work to preserve
6163 any location information in cp_expr "decl". Given that
6164 objc_lookup_ivar is implemented in "c-family" and "objc", we
6165 have a trip through the pure "tree" type, rather than cp_expr.
6166 Naively copying it back to "decl" would implicitly give the
6167 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6168 store an EXPR_LOCATION. Hence we only update "decl" (and
6169 hence its location_t) if we get back a different tree node. */
6170 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6171 id_expression);
6172 if (decl_tree != decl.get_value ())
6173 decl = cp_expr (decl_tree);
6175 /* If name lookup gives us a SCOPE_REF, then the
6176 qualifying scope was dependent. */
6177 if (TREE_CODE (decl) == SCOPE_REF)
6179 /* At this point, we do not know if DECL is a valid
6180 integral constant expression. We assume that it is
6181 in fact such an expression, so that code like:
6183 template <int N> struct A {
6184 int a[B<N>::i];
6187 is accepted. At template-instantiation time, we
6188 will check that B<N>::i is actually a constant. */
6189 return decl;
6191 /* Check to see if DECL is a local variable in a context
6192 where that is forbidden. */
6193 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6194 && local_variable_p (decl)
6195 /* DR 2082 permits local variables in unevaluated contexts
6196 within a default argument. */
6197 && !cp_unevaluated_operand)
6199 const char *msg
6200 = (TREE_CODE (decl) == PARM_DECL
6201 ? _("parameter %qD may not appear in this context")
6202 : _("local variable %qD may not appear in this context"));
6203 error_at (id_expression.get_location (), msg,
6204 decl.get_value ());
6205 return error_mark_node;
6209 decl = (finish_id_expression
6210 (id_expression, decl, parser->scope,
6211 idk,
6212 parser->integral_constant_expression_p,
6213 parser->allow_non_integral_constant_expression_p,
6214 &parser->non_integral_constant_expression_p,
6215 template_p, done, address_p,
6216 template_arg_p,
6217 &error_msg,
6218 id_expression.get_location ()));
6219 if (error_msg)
6220 cp_parser_error (parser, error_msg);
6221 /* Build a location for an id-expression of the form:
6222 ::ns::id
6223 ~~~~~~^~
6227 i.e. from the start of the first token to the end of the final
6228 token, with the caret at the start of the unqualified-id. */
6229 location_t caret_loc = get_pure_location (id_expression.get_location ());
6230 location_t start_loc = get_start (id_expr_token->location);
6231 location_t finish_loc = get_finish (id_expression.get_location ());
6232 location_t combined_loc
6233 = make_location (caret_loc, start_loc, finish_loc);
6235 decl.set_location (combined_loc);
6236 return decl;
6239 /* Anything else is an error. */
6240 default:
6241 cp_parser_error (parser, "expected primary-expression");
6242 return error_mark_node;
6246 static inline cp_expr
6247 cp_parser_primary_expression (cp_parser *parser,
6248 bool address_p,
6249 bool cast_p,
6250 bool template_arg_p,
6251 cp_id_kind *idk)
6253 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6254 /*decltype*/false, idk);
6257 /* Complain about missing template keyword when naming a dependent
6258 member template. */
6260 static void
6261 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6263 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6264 return;
6266 gcc_rich_location richloc (loc);
6267 richloc.add_fixit_insert_before ("template");
6268 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6269 "expected %qs keyword before dependent "
6270 "template name", "template");
6271 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6274 /* Parse an id-expression.
6276 id-expression:
6277 unqualified-id
6278 qualified-id
6280 qualified-id:
6281 :: [opt] nested-name-specifier template [opt] unqualified-id
6282 :: identifier
6283 :: operator-function-id
6284 :: template-id
6286 Return a representation of the unqualified portion of the
6287 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6288 a `::' or nested-name-specifier.
6290 Often, if the id-expression was a qualified-id, the caller will
6291 want to make a SCOPE_REF to represent the qualified-id. This
6292 function does not do this in order to avoid wastefully creating
6293 SCOPE_REFs when they are not required.
6295 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6296 `template' keyword.
6298 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6299 uninstantiated templates.
6301 If *TEMPLATE_P is non-NULL, it is set to true iff the
6302 `template' keyword is used to explicitly indicate that the entity
6303 named is a template.
6305 If DECLARATOR_P is true, the id-expression is appearing as part of
6306 a declarator, rather than as part of an expression. */
6308 static cp_expr
6309 cp_parser_id_expression (cp_parser *parser,
6310 bool template_keyword_p,
6311 bool check_dependency_p,
6312 bool *template_p,
6313 bool declarator_p,
6314 bool optional_p)
6316 bool global_scope_p;
6317 bool nested_name_specifier_p;
6319 /* Assume the `template' keyword was not used. */
6320 if (template_p)
6321 *template_p = template_keyword_p;
6323 /* Look for the optional `::' operator. */
6324 global_scope_p
6325 = (!template_keyword_p
6326 && (cp_parser_global_scope_opt (parser,
6327 /*current_scope_valid_p=*/false)
6328 != NULL_TREE));
6330 /* Look for the optional nested-name-specifier. */
6331 nested_name_specifier_p
6332 = (cp_parser_nested_name_specifier_opt (parser,
6333 /*typename_keyword_p=*/false,
6334 check_dependency_p,
6335 /*type_p=*/false,
6336 declarator_p,
6337 template_keyword_p)
6338 != NULL_TREE);
6340 cp_expr id = NULL_TREE;
6341 tree scope = parser->scope;
6343 /* Peek at the next token. */
6344 cp_token *token = cp_lexer_peek_token (parser->lexer);
6346 /* If there is a nested-name-specifier, then we are looking at
6347 the first qualified-id production. */
6348 if (nested_name_specifier_p)
6350 tree saved_object_scope;
6351 tree saved_qualifying_scope;
6353 /* See if the next token is the `template' keyword. */
6354 if (!template_p)
6355 template_p = &template_keyword_p;
6356 *template_p = cp_parser_optional_template_keyword (parser);
6357 /* Name lookup we do during the processing of the
6358 unqualified-id might obliterate SCOPE. */
6359 saved_object_scope = parser->object_scope;
6360 saved_qualifying_scope = parser->qualifying_scope;
6361 /* Process the final unqualified-id. */
6362 id = cp_parser_unqualified_id (parser, *template_p,
6363 check_dependency_p,
6364 declarator_p,
6365 /*optional_p=*/false);
6366 /* Restore the SAVED_SCOPE for our caller. */
6367 parser->scope = scope;
6368 parser->object_scope = saved_object_scope;
6369 parser->qualifying_scope = saved_qualifying_scope;
6371 /* Otherwise, if we are in global scope, then we are looking at one
6372 of the other qualified-id productions. */
6373 else if (global_scope_p)
6375 /* If it's an identifier, and the next token is not a "<", then
6376 we can avoid the template-id case. This is an optimization
6377 for this common case. */
6378 if (token->type == CPP_NAME
6379 && !cp_parser_nth_token_starts_template_argument_list_p
6380 (parser, 2))
6381 return cp_parser_identifier (parser);
6383 cp_parser_parse_tentatively (parser);
6384 /* Try a template-id. */
6385 id = cp_parser_template_id_expr (parser,
6386 /*template_keyword_p=*/false,
6387 /*check_dependency_p=*/true,
6388 declarator_p);
6389 /* If that worked, we're done. */
6390 if (cp_parser_parse_definitely (parser))
6391 return id;
6393 /* Peek at the next token. (Changes in the token buffer may
6394 have invalidated the pointer obtained above.) */
6395 token = cp_lexer_peek_token (parser->lexer);
6397 switch (token->type)
6399 case CPP_NAME:
6400 id = cp_parser_identifier (parser);
6401 break;
6403 case CPP_KEYWORD:
6404 if (token->keyword == RID_OPERATOR)
6406 id = cp_parser_operator_function_id (parser);
6407 break;
6409 /* Fall through. */
6411 default:
6412 cp_parser_error (parser, "expected id-expression");
6413 return error_mark_node;
6416 else
6418 if (!scope)
6419 scope = parser->context->object_type;
6420 id = cp_parser_unqualified_id (parser, template_keyword_p,
6421 /*check_dependency_p=*/true,
6422 declarator_p,
6423 optional_p);
6426 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6427 && warn_missing_template_keyword
6428 && !template_keyword_p
6429 /* Don't warn if we're looking inside templates. */
6430 && check_dependency_p
6431 /* In a template argument list a > could be closing
6432 the enclosing targs. */
6433 && !parser->in_template_argument_list_p
6434 && scope && dependentish_scope_p (scope)
6435 /* Don't confuse an ill-formed constructor declarator for a missing
6436 template keyword in a return type. */
6437 && !(declarator_p && constructor_name_p (id, scope))
6438 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6439 && warning_enabled_at (token->location,
6440 OPT_Wmissing_template_keyword))
6442 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6443 if (cp_parser_skip_entire_template_parameter_list (parser)
6444 /* An operator after the > suggests that the > ends a
6445 template-id; a name or literal suggests that the > is an
6446 operator. */
6447 && (cp_lexer_peek_token (parser->lexer)->type
6448 <= CPP_LAST_PUNCTUATOR))
6449 missing_template_diag (token->location);
6452 return id;
6455 /* Parse an unqualified-id.
6457 unqualified-id:
6458 identifier
6459 operator-function-id
6460 conversion-function-id
6461 ~ class-name
6462 template-id
6464 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6465 keyword, in a construct like `A::template ...'.
6467 Returns a representation of unqualified-id. For the `identifier'
6468 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6469 production a BIT_NOT_EXPR is returned; the operand of the
6470 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6471 other productions, see the documentation accompanying the
6472 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6473 names are looked up in uninstantiated templates. If DECLARATOR_P
6474 is true, the unqualified-id is appearing as part of a declarator,
6475 rather than as part of an expression. */
6477 static cp_expr
6478 cp_parser_unqualified_id (cp_parser* parser,
6479 bool template_keyword_p,
6480 bool check_dependency_p,
6481 bool declarator_p,
6482 bool optional_p)
6484 cp_token *token;
6486 /* Peek at the next token. */
6487 token = cp_lexer_peek_token (parser->lexer);
6489 switch ((int) token->type)
6491 case CPP_NAME:
6493 tree id;
6495 /* We don't know yet whether or not this will be a
6496 template-id. */
6497 cp_parser_parse_tentatively (parser);
6498 /* Try a template-id. */
6499 id = cp_parser_template_id_expr (parser, template_keyword_p,
6500 check_dependency_p,
6501 declarator_p);
6502 /* If it worked, we're done. */
6503 if (cp_parser_parse_definitely (parser))
6504 return id;
6505 /* Otherwise, it's an ordinary identifier. */
6506 return cp_parser_identifier (parser);
6509 case CPP_TEMPLATE_ID:
6510 return cp_parser_template_id_expr (parser, template_keyword_p,
6511 check_dependency_p,
6512 declarator_p);
6514 case CPP_COMPL:
6516 tree type_decl;
6517 tree qualifying_scope;
6518 tree object_scope;
6519 tree scope;
6520 bool done;
6521 location_t tilde_loc = token->location;
6523 /* Consume the `~' token. */
6524 cp_lexer_consume_token (parser->lexer);
6525 /* Parse the class-name. The standard, as written, seems to
6526 say that:
6528 template <typename T> struct S { ~S (); };
6529 template <typename T> S<T>::~S() {}
6531 is invalid, since `~' must be followed by a class-name, but
6532 `S<T>' is dependent, and so not known to be a class.
6533 That's not right; we need to look in uninstantiated
6534 templates. A further complication arises from:
6536 template <typename T> void f(T t) {
6537 t.T::~T();
6540 Here, it is not possible to look up `T' in the scope of `T'
6541 itself. We must look in both the current scope, and the
6542 scope of the containing complete expression.
6544 Yet another issue is:
6546 struct S {
6547 int S;
6548 ~S();
6551 S::~S() {}
6553 The standard does not seem to say that the `S' in `~S'
6554 should refer to the type `S' and not the data member
6555 `S::S'. */
6557 /* DR 244 says that we look up the name after the "~" in the
6558 same scope as we looked up the qualifying name. That idea
6559 isn't fully worked out; it's more complicated than that. */
6560 scope = parser->scope;
6561 object_scope = parser->object_scope;
6562 qualifying_scope = parser->qualifying_scope;
6564 /* Check for invalid scopes. */
6565 if (scope == error_mark_node)
6567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6568 cp_lexer_consume_token (parser->lexer);
6569 return error_mark_node;
6571 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6573 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6574 error_at (token->location,
6575 "scope %qT before %<~%> is not a class-name",
6576 scope);
6577 cp_parser_simulate_error (parser);
6578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6579 cp_lexer_consume_token (parser->lexer);
6580 return error_mark_node;
6582 if (template_keyword_p)
6584 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6585 error_at (tilde_loc, "%<template%> keyword not permitted in "
6586 "destructor name");
6587 cp_parser_simulate_error (parser);
6588 return error_mark_node;
6591 gcc_assert (!scope || TYPE_P (scope));
6593 token = cp_lexer_peek_token (parser->lexer);
6595 /* Create a location with caret == start at the tilde,
6596 finishing at the end of the peeked token, e.g:
6597 ~token
6598 ^~~~~~. */
6599 location_t loc
6600 = make_location (tilde_loc, tilde_loc, token->location);
6602 /* If the name is of the form "X::~X" it's OK even if X is a
6603 typedef. */
6605 if (scope
6606 && token->type == CPP_NAME
6607 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6608 != CPP_LESS)
6609 && (token->u.value == TYPE_IDENTIFIER (scope)
6610 || (CLASS_TYPE_P (scope)
6611 && constructor_name_p (token->u.value, scope))))
6613 cp_lexer_consume_token (parser->lexer);
6614 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6617 /* ~auto means the destructor of whatever the object is. */
6618 if (cp_parser_is_keyword (token, RID_AUTO))
6620 if (cxx_dialect < cxx14)
6621 pedwarn (loc, OPT_Wc__14_extensions,
6622 "%<~auto%> only available with "
6623 "%<-std=c++14%> or %<-std=gnu++14%>");
6624 cp_lexer_consume_token (parser->lexer);
6625 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6628 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6629 declarator-id of a constructor or destructor. */
6630 if (token->type == CPP_TEMPLATE_ID && declarator_p
6631 && cxx_dialect >= cxx20)
6633 if (!cp_parser_simulate_error (parser))
6634 error_at (tilde_loc, "template-id not allowed for destructor");
6635 return error_mark_node;
6638 /* If there was an explicit qualification (S::~T), first look
6639 in the scope given by the qualification (i.e., S).
6641 Note: in the calls to cp_parser_class_name below we pass
6642 typename_type so that lookup finds the injected-class-name
6643 rather than the constructor. */
6644 done = false;
6645 type_decl = NULL_TREE;
6646 if (scope)
6648 cp_parser_parse_tentatively (parser);
6649 type_decl = cp_parser_class_name (parser,
6650 /*typename_keyword_p=*/false,
6651 /*template_keyword_p=*/false,
6652 typename_type,
6653 /*check_dependency=*/false,
6654 /*class_head_p=*/false,
6655 declarator_p);
6656 if (cp_parser_parse_definitely (parser))
6657 done = true;
6659 /* In "N::S::~S", look in "N" as well. */
6660 if (!done && scope && qualifying_scope)
6662 cp_parser_parse_tentatively (parser);
6663 parser->scope = qualifying_scope;
6664 parser->object_scope = NULL_TREE;
6665 parser->qualifying_scope = NULL_TREE;
6666 type_decl
6667 = cp_parser_class_name (parser,
6668 /*typename_keyword_p=*/false,
6669 /*template_keyword_p=*/false,
6670 typename_type,
6671 /*check_dependency=*/false,
6672 /*class_head_p=*/false,
6673 declarator_p);
6674 if (cp_parser_parse_definitely (parser))
6675 done = true;
6677 /* In "p->S::~T", look in the scope given by "*p" as well. */
6678 else if (!done && object_scope)
6680 cp_parser_parse_tentatively (parser);
6681 parser->scope = object_scope;
6682 parser->object_scope = NULL_TREE;
6683 parser->qualifying_scope = NULL_TREE;
6684 type_decl
6685 = cp_parser_class_name (parser,
6686 /*typename_keyword_p=*/false,
6687 /*template_keyword_p=*/false,
6688 typename_type,
6689 /*check_dependency=*/false,
6690 /*class_head_p=*/false,
6691 declarator_p);
6692 if (cp_parser_parse_definitely (parser))
6693 done = true;
6695 /* Look in the surrounding context. */
6696 if (!done)
6698 parser->scope = NULL_TREE;
6699 parser->object_scope = NULL_TREE;
6700 parser->qualifying_scope = NULL_TREE;
6701 if (processing_template_decl)
6702 cp_parser_parse_tentatively (parser);
6703 type_decl
6704 = cp_parser_class_name (parser,
6705 /*typename_keyword_p=*/false,
6706 /*template_keyword_p=*/false,
6707 typename_type,
6708 /*check_dependency=*/false,
6709 /*class_head_p=*/false,
6710 declarator_p);
6711 if (processing_template_decl
6712 && ! cp_parser_parse_definitely (parser))
6714 /* We couldn't find a type with this name. If we're parsing
6715 tentatively, fail and try something else. */
6716 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6718 cp_parser_simulate_error (parser);
6719 return error_mark_node;
6721 /* Otherwise, accept it and check for a match at instantiation
6722 time. */
6723 type_decl = cp_parser_identifier (parser);
6724 if (type_decl != error_mark_node)
6725 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6726 return type_decl;
6729 /* If an error occurred, assume that the name of the
6730 destructor is the same as the name of the qualifying
6731 class. That allows us to keep parsing after running
6732 into ill-formed destructor names. */
6733 if (type_decl == error_mark_node && scope)
6734 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6735 else if (type_decl == error_mark_node)
6736 return error_mark_node;
6738 /* Check that destructor name and scope match. */
6739 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6741 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6742 error_at (loc,
6743 "declaration of %<~%T%> as member of %qT",
6744 type_decl, scope);
6745 cp_parser_simulate_error (parser);
6746 return error_mark_node;
6749 /* [class.dtor]
6751 A typedef-name that names a class shall not be used as the
6752 identifier in the declarator for a destructor declaration. */
6753 if (declarator_p
6754 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6755 && !DECL_SELF_REFERENCE_P (type_decl)
6756 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6757 error_at (loc,
6758 "typedef-name %qD used as destructor declarator",
6759 type_decl);
6761 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6764 case CPP_KEYWORD:
6765 if (token->keyword == RID_OPERATOR)
6767 cp_expr id;
6769 /* This could be a template-id, so we try that first. */
6770 cp_parser_parse_tentatively (parser);
6771 /* Try a template-id. */
6772 id = cp_parser_template_id_expr (parser, template_keyword_p,
6773 /*check_dependency_p=*/true,
6774 declarator_p);
6775 /* If that worked, we're done. */
6776 if (cp_parser_parse_definitely (parser))
6777 return id;
6778 /* We still don't know whether we're looking at an
6779 operator-function-id or a conversion-function-id. */
6780 cp_parser_parse_tentatively (parser);
6781 /* Try an operator-function-id. */
6782 id = cp_parser_operator_function_id (parser);
6783 /* If that didn't work, try a conversion-function-id. */
6784 if (!cp_parser_parse_definitely (parser))
6785 id = cp_parser_conversion_function_id (parser);
6787 return id;
6789 /* Fall through. */
6791 default:
6792 if (optional_p)
6793 return NULL_TREE;
6794 cp_parser_error (parser, "expected unqualified-id");
6795 return error_mark_node;
6799 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6800 be a template-id or the name shall refer to a class template or an
6801 alias template. */
6803 static void
6804 check_template_keyword_in_nested_name_spec (tree name)
6806 if (CLASS_TYPE_P (name)
6807 && ((CLASSTYPE_USE_TEMPLATE (name)
6808 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6809 || CLASSTYPE_IS_TEMPLATE (name)))
6810 return;
6812 if (TREE_CODE (name) == TYPENAME_TYPE
6813 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6814 return;
6815 /* Alias templates are also OK. */
6816 else if (alias_template_specialization_p (name, nt_opaque))
6817 return;
6819 permerror (input_location, TYPE_P (name)
6820 ? G_("%qT is not a template")
6821 : G_("%qD is not a template"),
6822 name);
6825 /* Parse an (optional) nested-name-specifier.
6827 nested-name-specifier: [C++98]
6828 class-or-namespace-name :: nested-name-specifier [opt]
6829 class-or-namespace-name :: template nested-name-specifier [opt]
6831 nested-name-specifier: [C++0x]
6832 type-name ::
6833 namespace-name ::
6834 nested-name-specifier identifier ::
6835 nested-name-specifier template [opt] simple-template-id ::
6837 PARSER->SCOPE should be set appropriately before this function is
6838 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6839 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6840 in name lookups.
6842 Sets PARSER->SCOPE to the class (TYPE) or namespace
6843 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6844 it unchanged if there is no nested-name-specifier. Returns the new
6845 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6847 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6849 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6850 part of a declaration and/or decl-specifier. */
6852 static tree
6853 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6854 bool typename_keyword_p,
6855 bool check_dependency_p,
6856 bool type_p,
6857 bool is_declaration,
6858 bool template_keyword_p /* = false */)
6860 bool success = false;
6861 cp_token_position start = 0;
6862 cp_token *token;
6864 /* Remember where the nested-name-specifier starts. */
6865 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6866 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6868 start = cp_lexer_token_position (parser->lexer, false);
6869 push_deferring_access_checks (dk_deferred);
6872 while (true)
6874 tree new_scope;
6875 tree old_scope;
6876 tree saved_qualifying_scope;
6878 /* Spot cases that cannot be the beginning of a
6879 nested-name-specifier. */
6880 token = cp_lexer_peek_token (parser->lexer);
6882 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6883 the already parsed nested-name-specifier. */
6884 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6886 /* Grab the nested-name-specifier and continue the loop. */
6887 cp_parser_pre_parsed_nested_name_specifier (parser);
6888 /* If we originally encountered this nested-name-specifier
6889 with CHECK_DEPENDENCY_P set to true, we will not have
6890 resolved TYPENAME_TYPEs, so we must do so here. */
6891 if (is_declaration
6892 && !check_dependency_p
6893 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6895 new_scope = resolve_typename_type (parser->scope,
6896 /*only_current_p=*/false);
6897 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6898 parser->scope = new_scope;
6900 success = true;
6901 continue;
6904 /* Spot cases that cannot be the beginning of a
6905 nested-name-specifier. On the second and subsequent times
6906 through the loop, we look for the `template' keyword. */
6907 if (success && token->keyword == RID_TEMPLATE)
6909 /* A template-id can start a nested-name-specifier. */
6910 else if (token->type == CPP_TEMPLATE_ID)
6912 /* DR 743: decltype can be used in a nested-name-specifier. */
6913 else if (token_is_decltype (token))
6915 else
6917 /* If the next token is not an identifier, then it is
6918 definitely not a type-name or namespace-name. */
6919 if (token->type != CPP_NAME)
6920 break;
6921 /* If the following token is neither a `<' (to begin a
6922 template-id), nor a `::', then we are not looking at a
6923 nested-name-specifier. */
6924 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6926 if (token->type == CPP_COLON
6927 && parser->colon_corrects_to_scope_p
6928 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6929 /* name:name is a valid sequence in an Objective C message. */
6930 && !parser->objective_c_message_context_p)
6932 gcc_rich_location richloc (token->location);
6933 richloc.add_fixit_replace ("::");
6934 error_at (&richloc,
6935 "found %<:%> in nested-name-specifier, "
6936 "expected %<::%>");
6937 token->type = CPP_SCOPE;
6940 if (token->type != CPP_SCOPE
6941 && !cp_parser_nth_token_starts_template_argument_list_p
6942 (parser, 2))
6943 break;
6946 /* The nested-name-specifier is optional, so we parse
6947 tentatively. */
6948 cp_parser_parse_tentatively (parser);
6950 /* Look for the optional `template' keyword, if this isn't the
6951 first time through the loop. */
6952 if (success)
6954 template_keyword_p = cp_parser_optional_template_keyword (parser);
6955 /* DR1710: "In a qualified-id used as the name in
6956 a typename-specifier, elaborated-type-specifier, using-declaration,
6957 or class-or-decltype, an optional keyword template appearing at
6958 the top level is ignored." */
6959 if (!template_keyword_p
6960 && typename_keyword_p
6961 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6962 template_keyword_p = true;
6965 /* Save the old scope since the name lookup we are about to do
6966 might destroy it. */
6967 old_scope = parser->scope;
6968 saved_qualifying_scope = parser->qualifying_scope;
6969 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6970 look up names in "X<T>::I" in order to determine that "Y" is
6971 a template. So, if we have a typename at this point, we make
6972 an effort to look through it. */
6973 if (is_declaration
6974 && !check_dependency_p
6975 && !typename_keyword_p
6976 && parser->scope
6977 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6978 parser->scope = resolve_typename_type (parser->scope,
6979 /*only_current_p=*/false);
6980 /* Parse the qualifying entity. */
6981 new_scope
6982 = cp_parser_qualifying_entity (parser,
6983 typename_keyword_p,
6984 template_keyword_p,
6985 check_dependency_p,
6986 type_p,
6987 is_declaration);
6988 /* Look for the `::' token. */
6989 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6991 /* If we found what we wanted, we keep going; otherwise, we're
6992 done. */
6993 if (!cp_parser_parse_definitely (parser))
6995 bool error_p = false;
6997 /* Restore the OLD_SCOPE since it was valid before the
6998 failed attempt at finding the last
6999 class-or-namespace-name. */
7000 parser->scope = old_scope;
7001 parser->qualifying_scope = saved_qualifying_scope;
7003 /* If the next token is a decltype, and the one after that is a
7004 `::', then the decltype has failed to resolve to a class or
7005 enumeration type. Give this error even when parsing
7006 tentatively since it can't possibly be valid--and we're going
7007 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7008 won't get another chance.*/
7009 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7010 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7011 == CPP_SCOPE))
7013 token = cp_lexer_consume_token (parser->lexer);
7014 tree dtype = token->u.tree_check_value->value;
7015 if (dtype != error_mark_node)
7016 error_at (token->location, "%<decltype%> evaluates to %qT, "
7017 "which is not a class or enumeration type",
7018 dtype);
7019 parser->scope = error_mark_node;
7020 error_p = true;
7021 /* As below. */
7022 success = true;
7023 cp_lexer_consume_token (parser->lexer);
7026 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7027 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7029 /* If we have a non-type template-id followed by ::, it can't
7030 possibly be valid. */
7031 token = cp_lexer_peek_token (parser->lexer);
7032 tree tid = token->u.tree_check_value->value;
7033 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7034 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7036 tree tmpl = NULL_TREE;
7037 if (is_overloaded_fn (tid))
7039 tree fns = get_fns (tid);
7040 if (OVL_SINGLE_P (fns))
7041 tmpl = OVL_FIRST (fns);
7042 if (function_concept_p (fns))
7043 error_at (token->location, "concept-id %qD "
7044 "in nested-name-specifier", tid);
7045 else
7046 error_at (token->location, "function template-id "
7047 "%qD in nested-name-specifier", tid);
7049 else
7051 tmpl = TREE_OPERAND (tid, 0);
7052 if (variable_concept_p (tmpl)
7053 || standard_concept_p (tmpl))
7054 error_at (token->location, "concept-id %qD "
7055 "in nested-name-specifier", tid);
7056 else
7058 /* Variable template. */
7059 gcc_assert (variable_template_p (tmpl));
7060 error_at (token->location, "variable template-id "
7061 "%qD in nested-name-specifier", tid);
7064 if (tmpl)
7065 inform (DECL_SOURCE_LOCATION (tmpl),
7066 "%qD declared here", tmpl);
7068 parser->scope = error_mark_node;
7069 error_p = true;
7070 /* As below. */
7071 success = true;
7072 cp_lexer_consume_token (parser->lexer);
7073 cp_lexer_consume_token (parser->lexer);
7077 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7078 break;
7079 /* If the next token is an identifier, and the one after
7080 that is a `::', then any valid interpretation would have
7081 found a class-or-namespace-name. */
7082 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7083 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7084 == CPP_SCOPE)
7085 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7086 != CPP_COMPL))
7088 token = cp_lexer_consume_token (parser->lexer);
7089 if (!error_p)
7091 if (!token->error_reported)
7093 tree decl;
7094 tree ambiguous_decls;
7096 decl = cp_parser_lookup_name (parser, token->u.value,
7097 none_type,
7098 /*is_template=*/false,
7099 /*is_namespace=*/false,
7100 /*check_dependency=*/true,
7101 &ambiguous_decls,
7102 token->location);
7103 if (TREE_CODE (decl) == TEMPLATE_DECL)
7104 error_at (token->location,
7105 "%qD used without template arguments",
7106 decl);
7107 else if (ambiguous_decls)
7109 // cp_parser_lookup_name has the same diagnostic,
7110 // thus make sure to emit it at most once.
7111 if (cp_parser_uncommitted_to_tentative_parse_p
7112 (parser))
7114 error_at (token->location,
7115 "reference to %qD is ambiguous",
7116 token->u.value);
7117 print_candidates (ambiguous_decls);
7119 decl = error_mark_node;
7121 else
7123 if (cxx_dialect != cxx98)
7124 cp_parser_name_lookup_error
7125 (parser, token->u.value, decl, NLE_NOT_CXX98,
7126 token->location);
7127 else
7128 cp_parser_name_lookup_error
7129 (parser, token->u.value, decl, NLE_CXX98,
7130 token->location);
7133 parser->scope = error_mark_node;
7134 error_p = true;
7135 /* Treat this as a successful nested-name-specifier
7136 due to:
7138 [basic.lookup.qual]
7140 If the name found is not a class-name (clause
7141 _class_) or namespace-name (_namespace.def_), the
7142 program is ill-formed. */
7143 success = true;
7145 cp_lexer_consume_token (parser->lexer);
7147 break;
7149 /* We've found one valid nested-name-specifier. */
7150 success = true;
7151 /* Name lookup always gives us a DECL. */
7152 if (TREE_CODE (new_scope) == TYPE_DECL)
7153 new_scope = TREE_TYPE (new_scope);
7154 /* Uses of "template" must be followed by actual templates. */
7155 if (template_keyword_p)
7156 check_template_keyword_in_nested_name_spec (new_scope);
7157 /* If it is a class scope, try to complete it; we are about to
7158 be looking up names inside the class. */
7159 if (TYPE_P (new_scope)
7160 /* Since checking types for dependency can be expensive,
7161 avoid doing it if the type is already complete. */
7162 && !COMPLETE_TYPE_P (new_scope)
7163 /* Do not try to complete dependent types. */
7164 && !dependent_type_p (new_scope))
7166 new_scope = complete_type (new_scope);
7167 /* If it is a typedef to current class, use the current
7168 class instead, as the typedef won't have any names inside
7169 it yet. */
7170 if (!COMPLETE_TYPE_P (new_scope)
7171 && currently_open_class (new_scope))
7172 new_scope = TYPE_MAIN_VARIANT (new_scope);
7174 /* Make sure we look in the right scope the next time through
7175 the loop. */
7176 parser->scope = new_scope;
7179 /* If parsing tentatively, replace the sequence of tokens that makes
7180 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7181 token. That way, should we re-parse the token stream, we will
7182 not have to repeat the effort required to do the parse, nor will
7183 we issue duplicate error messages. */
7184 if (success && start)
7186 cp_token *token;
7188 token = cp_lexer_token_at (parser->lexer, start);
7189 /* Reset the contents of the START token. */
7190 token->type = CPP_NESTED_NAME_SPECIFIER;
7191 /* Retrieve any deferred checks. Do not pop this access checks yet
7192 so the memory will not be reclaimed during token replacing below. */
7193 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7194 token->tree_check_p = true;
7195 token->u.tree_check_value->value = parser->scope;
7196 token->u.tree_check_value->checks = get_deferred_access_checks ();
7197 token->u.tree_check_value->qualifying_scope =
7198 parser->qualifying_scope;
7199 token->keyword = RID_MAX;
7201 /* Purge all subsequent tokens. */
7202 cp_lexer_purge_tokens_after (parser->lexer, start);
7205 if (start)
7206 pop_to_parent_deferring_access_checks ();
7208 return success ? parser->scope : NULL_TREE;
7211 /* Parse a nested-name-specifier. See
7212 cp_parser_nested_name_specifier_opt for details. This function
7213 behaves identically, except that it will an issue an error if no
7214 nested-name-specifier is present. */
7216 static tree
7217 cp_parser_nested_name_specifier (cp_parser *parser,
7218 bool typename_keyword_p,
7219 bool check_dependency_p,
7220 bool type_p,
7221 bool is_declaration)
7223 tree scope;
7225 /* Look for the nested-name-specifier. */
7226 scope = cp_parser_nested_name_specifier_opt (parser,
7227 typename_keyword_p,
7228 check_dependency_p,
7229 type_p,
7230 is_declaration);
7231 /* If it was not present, issue an error message. */
7232 if (!scope)
7234 cp_parser_error (parser, "expected nested-name-specifier");
7235 parser->scope = NULL_TREE;
7238 return scope;
7241 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7242 this is either a class-name or a namespace-name (which corresponds
7243 to the class-or-namespace-name production in the grammar). For
7244 C++0x, it can also be a type-name that refers to an enumeration
7245 type or a simple-template-id.
7247 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7248 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7249 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7250 TYPE_P is TRUE iff the next name should be taken as a class-name,
7251 even the same name is declared to be another entity in the same
7252 scope.
7254 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7255 specified by the class-or-namespace-name. If neither is found the
7256 ERROR_MARK_NODE is returned. */
7258 static tree
7259 cp_parser_qualifying_entity (cp_parser *parser,
7260 bool typename_keyword_p,
7261 bool template_keyword_p,
7262 bool check_dependency_p,
7263 bool type_p,
7264 bool is_declaration)
7266 tree saved_scope;
7267 tree saved_qualifying_scope;
7268 tree saved_object_scope;
7269 tree scope;
7270 bool only_class_p;
7271 bool successful_parse_p;
7273 /* DR 743: decltype can appear in a nested-name-specifier. */
7274 if (cp_lexer_next_token_is_decltype (parser->lexer))
7276 scope = cp_parser_decltype (parser);
7277 if (TREE_CODE (scope) != ENUMERAL_TYPE
7278 && !MAYBE_CLASS_TYPE_P (scope))
7280 cp_parser_simulate_error (parser);
7281 return error_mark_node;
7283 if (TYPE_NAME (scope))
7284 scope = TYPE_NAME (scope);
7285 return scope;
7288 /* Before we try to parse the class-name, we must save away the
7289 current PARSER->SCOPE since cp_parser_class_name will destroy
7290 it. */
7291 saved_scope = parser->scope;
7292 saved_qualifying_scope = parser->qualifying_scope;
7293 saved_object_scope = parser->object_scope;
7294 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7295 there is no need to look for a namespace-name. */
7296 only_class_p = template_keyword_p
7297 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7298 if (!only_class_p)
7299 cp_parser_parse_tentatively (parser);
7300 scope = cp_parser_class_name (parser,
7301 typename_keyword_p,
7302 template_keyword_p,
7303 type_p ? class_type : none_type,
7304 check_dependency_p,
7305 /*class_head_p=*/false,
7306 is_declaration,
7307 /*enum_ok=*/cxx_dialect > cxx98);
7308 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7309 /* If that didn't work, try for a namespace-name. */
7310 if (!only_class_p && !successful_parse_p)
7312 /* Restore the saved scope. */
7313 parser->scope = saved_scope;
7314 parser->qualifying_scope = saved_qualifying_scope;
7315 parser->object_scope = saved_object_scope;
7316 /* If we are not looking at an identifier followed by the scope
7317 resolution operator, then this is not part of a
7318 nested-name-specifier. (Note that this function is only used
7319 to parse the components of a nested-name-specifier.) */
7320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7321 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7322 return error_mark_node;
7323 scope = cp_parser_namespace_name (parser);
7326 return scope;
7329 /* Return true if we are looking at a compound-literal, false otherwise. */
7331 static bool
7332 cp_parser_compound_literal_p (cp_parser *parser)
7334 cp_lexer_save_tokens (parser->lexer);
7336 /* Skip tokens until the next token is a closing parenthesis.
7337 If we find the closing `)', and the next token is a `{', then
7338 we are looking at a compound-literal. */
7339 bool compound_literal_p
7340 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7341 /*consume_paren=*/true)
7342 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7344 /* Roll back the tokens we skipped. */
7345 cp_lexer_rollback_tokens (parser->lexer);
7347 return compound_literal_p;
7350 /* Return true if EXPR is the integer constant zero or a complex constant
7351 of zero, without any folding, but ignoring location wrappers. */
7353 bool
7354 literal_integer_zerop (const_tree expr)
7356 return (location_wrapper_p (expr)
7357 && integer_zerop (TREE_OPERAND (expr, 0)));
7360 /* Parse a postfix-expression.
7362 postfix-expression:
7363 primary-expression
7364 postfix-expression [ expression ]
7365 postfix-expression ( expression-list [opt] )
7366 simple-type-specifier ( expression-list [opt] )
7367 typename :: [opt] nested-name-specifier identifier
7368 ( expression-list [opt] )
7369 typename :: [opt] nested-name-specifier template [opt] template-id
7370 ( expression-list [opt] )
7371 postfix-expression . template [opt] id-expression
7372 postfix-expression -> template [opt] id-expression
7373 postfix-expression . pseudo-destructor-name
7374 postfix-expression -> pseudo-destructor-name
7375 postfix-expression ++
7376 postfix-expression --
7377 dynamic_cast < type-id > ( expression )
7378 static_cast < type-id > ( expression )
7379 reinterpret_cast < type-id > ( expression )
7380 const_cast < type-id > ( expression )
7381 typeid ( expression )
7382 typeid ( type-id )
7384 GNU Extension:
7386 postfix-expression:
7387 ( type-id ) { initializer-list , [opt] }
7389 This extension is a GNU version of the C99 compound-literal
7390 construct. (The C99 grammar uses `type-name' instead of `type-id',
7391 but they are essentially the same concept.)
7393 If ADDRESS_P is true, the postfix expression is the operand of the
7394 `&' operator. CAST_P is true if this expression is the target of a
7395 cast.
7397 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7398 class member access expressions [expr.ref].
7400 Returns a representation of the expression. */
7402 static cp_expr
7403 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7404 bool member_access_only_p, bool decltype_p,
7405 cp_id_kind * pidk_return)
7407 cp_token *token;
7408 location_t loc;
7409 enum rid keyword;
7410 cp_id_kind idk = CP_ID_KIND_NONE;
7411 cp_expr postfix_expression = NULL_TREE;
7412 bool is_member_access = false;
7414 /* Peek at the next token. */
7415 token = cp_lexer_peek_token (parser->lexer);
7416 loc = token->location;
7417 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7419 /* Some of the productions are determined by keywords. */
7420 keyword = token->keyword;
7421 switch (keyword)
7423 case RID_DYNCAST:
7424 case RID_STATCAST:
7425 case RID_REINTCAST:
7426 case RID_CONSTCAST:
7428 tree type;
7429 cp_expr expression;
7430 const char *saved_message;
7431 bool saved_in_type_id_in_expr_p;
7433 /* All of these can be handled in the same way from the point
7434 of view of parsing. Begin by consuming the token
7435 identifying the cast. */
7436 cp_lexer_consume_token (parser->lexer);
7438 /* New types cannot be defined in the cast. */
7439 saved_message = parser->type_definition_forbidden_message;
7440 parser->type_definition_forbidden_message
7441 = G_("types may not be defined in casts");
7443 /* Look for the opening `<'. */
7444 cp_parser_require (parser, CPP_LESS, RT_LESS);
7445 /* Parse the type to which we are casting. */
7446 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7447 parser->in_type_id_in_expr_p = true;
7448 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7449 NULL);
7450 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7451 /* Look for the closing `>'. */
7452 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7453 /* Restore the old message. */
7454 parser->type_definition_forbidden_message = saved_message;
7456 bool saved_greater_than_is_operator_p
7457 = parser->greater_than_is_operator_p;
7458 parser->greater_than_is_operator_p = true;
7460 /* And the expression which is being cast. */
7461 matching_parens parens;
7462 parens.require_open (parser);
7463 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7464 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7465 RT_CLOSE_PAREN);
7466 location_t end_loc = close_paren ?
7467 close_paren->location : UNKNOWN_LOCATION;
7469 parser->greater_than_is_operator_p
7470 = saved_greater_than_is_operator_p;
7472 /* Only type conversions to integral or enumeration types
7473 can be used in constant-expressions. */
7474 if (!cast_valid_in_integral_constant_expression_p (type)
7475 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7477 postfix_expression = error_mark_node;
7478 break;
7481 /* Construct a location e.g. :
7482 reinterpret_cast <int *> (expr)
7483 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7484 ranging from the start of the "*_cast" token to the final closing
7485 paren, with the caret at the start. */
7486 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7488 switch (keyword)
7490 case RID_DYNCAST:
7491 postfix_expression
7492 = build_dynamic_cast (cp_cast_loc, type, expression,
7493 tf_warning_or_error);
7494 break;
7495 case RID_STATCAST:
7496 postfix_expression
7497 = build_static_cast (cp_cast_loc, type, expression,
7498 tf_warning_or_error);
7499 break;
7500 case RID_REINTCAST:
7501 postfix_expression
7502 = build_reinterpret_cast (cp_cast_loc, type, expression,
7503 tf_warning_or_error);
7504 break;
7505 case RID_CONSTCAST:
7506 postfix_expression
7507 = build_const_cast (cp_cast_loc, type, expression,
7508 tf_warning_or_error);
7509 break;
7510 default:
7511 gcc_unreachable ();
7514 break;
7516 case RID_TYPEID:
7518 tree type;
7519 const char *saved_message;
7520 bool saved_in_type_id_in_expr_p;
7522 /* Consume the `typeid' token. */
7523 cp_lexer_consume_token (parser->lexer);
7524 /* Look for the `(' token. */
7525 matching_parens parens;
7526 parens.require_open (parser);
7527 /* Types cannot be defined in a `typeid' expression. */
7528 saved_message = parser->type_definition_forbidden_message;
7529 parser->type_definition_forbidden_message
7530 = G_("types may not be defined in a %<typeid%> expression");
7531 /* We can't be sure yet whether we're looking at a type-id or an
7532 expression. */
7533 cp_parser_parse_tentatively (parser);
7534 /* Try a type-id first. */
7535 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7536 parser->in_type_id_in_expr_p = true;
7537 type = cp_parser_type_id (parser);
7538 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7539 /* Look for the `)' token. Otherwise, we can't be sure that
7540 we're not looking at an expression: consider `typeid (int
7541 (3))', for example. */
7542 cp_token *close_paren = parens.require_close (parser);
7543 /* If all went well, simply lookup the type-id. */
7544 if (cp_parser_parse_definitely (parser))
7545 postfix_expression = get_typeid (type, tf_warning_or_error);
7546 /* Otherwise, fall back to the expression variant. */
7547 else
7549 tree expression;
7551 /* Look for an expression. */
7552 expression = cp_parser_expression (parser, & idk);
7553 /* Compute its typeid. */
7554 postfix_expression = build_typeid (expression, tf_warning_or_error);
7555 /* Look for the `)' token. */
7556 close_paren = parens.require_close (parser);
7558 /* Restore the saved message. */
7559 parser->type_definition_forbidden_message = saved_message;
7560 /* `typeid' may not appear in an integral constant expression. */
7561 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7562 postfix_expression = error_mark_node;
7564 /* Construct a location e.g. :
7565 typeid (expr)
7566 ^~~~~~~~~~~~~
7567 ranging from the start of the "typeid" token to the final closing
7568 paren, with the caret at the start. */
7569 if (close_paren)
7571 location_t typeid_loc
7572 = make_location (start_loc, start_loc, close_paren->location);
7573 postfix_expression.set_location (typeid_loc);
7574 postfix_expression.maybe_add_location_wrapper ();
7577 break;
7579 case RID_TYPENAME:
7581 tree type;
7582 /* The syntax permitted here is the same permitted for an
7583 elaborated-type-specifier. */
7584 ++parser->prevent_constrained_type_specifiers;
7585 type = cp_parser_elaborated_type_specifier (parser,
7586 /*is_friend=*/false,
7587 /*is_declaration=*/false);
7588 --parser->prevent_constrained_type_specifiers;
7589 postfix_expression = cp_parser_functional_cast (parser, type);
7591 break;
7593 case RID_ADDRESSOF:
7594 case RID_BUILTIN_SHUFFLE:
7595 case RID_BUILTIN_SHUFFLEVECTOR:
7596 case RID_BUILTIN_LAUNDER:
7597 case RID_BUILTIN_ASSOC_BARRIER:
7599 vec<tree, va_gc> *vec;
7601 cp_lexer_consume_token (parser->lexer);
7602 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7603 /*cast_p=*/false, /*allow_expansion_p=*/true,
7604 /*non_constant_p=*/NULL);
7605 if (vec == NULL)
7607 postfix_expression = error_mark_node;
7608 break;
7611 for (tree p : *vec)
7612 mark_exp_read (p);
7614 switch (keyword)
7616 case RID_ADDRESSOF:
7617 if (vec->length () == 1)
7618 postfix_expression
7619 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7620 else
7622 error_at (loc, "wrong number of arguments to "
7623 "%<__builtin_addressof%>");
7624 postfix_expression = error_mark_node;
7626 break;
7628 case RID_BUILTIN_LAUNDER:
7629 if (vec->length () == 1)
7630 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7631 tf_warning_or_error);
7632 else
7634 error_at (loc, "wrong number of arguments to "
7635 "%<__builtin_launder%>");
7636 postfix_expression = error_mark_node;
7638 break;
7640 case RID_BUILTIN_ASSOC_BARRIER:
7641 if (vec->length () == 1)
7642 postfix_expression = build1_loc (loc, PAREN_EXPR,
7643 TREE_TYPE ((*vec)[0]),
7644 (*vec)[0]);
7645 else
7647 error_at (loc, "wrong number of arguments to "
7648 "%<__builtin_assoc_barrier%>");
7649 postfix_expression = error_mark_node;
7651 break;
7653 case RID_BUILTIN_SHUFFLE:
7654 if (vec->length () == 2)
7655 postfix_expression
7656 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7657 (*vec)[1], tf_warning_or_error);
7658 else if (vec->length () == 3)
7659 postfix_expression
7660 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7661 (*vec)[2], tf_warning_or_error);
7662 else
7664 error_at (loc, "wrong number of arguments to "
7665 "%<__builtin_shuffle%>");
7666 postfix_expression = error_mark_node;
7668 break;
7670 case RID_BUILTIN_SHUFFLEVECTOR:
7671 if (vec->length () < 3)
7673 error_at (loc, "wrong number of arguments to "
7674 "%<__builtin_shufflevector%>");
7675 postfix_expression = error_mark_node;
7677 else
7679 postfix_expression
7680 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7682 break;
7684 default:
7685 gcc_unreachable ();
7687 break;
7690 case RID_BUILTIN_CONVERTVECTOR:
7692 tree expression;
7693 tree type;
7694 /* Consume the `__builtin_convertvector' token. */
7695 cp_lexer_consume_token (parser->lexer);
7696 /* Look for the opening `('. */
7697 matching_parens parens;
7698 parens.require_open (parser);
7699 /* Now, parse the assignment-expression. */
7700 expression = cp_parser_assignment_expression (parser);
7701 /* Look for the `,'. */
7702 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7703 location_t type_location
7704 = cp_lexer_peek_token (parser->lexer)->location;
7705 /* Parse the type-id. */
7707 type_id_in_expr_sentinel s (parser);
7708 type = cp_parser_type_id (parser);
7710 /* Look for the closing `)'. */
7711 parens.require_close (parser);
7712 postfix_expression
7713 = cp_build_vec_convert (expression, type_location, type,
7714 tf_warning_or_error);
7715 break;
7718 case RID_BUILTIN_BIT_CAST:
7720 tree expression;
7721 tree type;
7722 /* Consume the `__builtin_bit_cast' token. */
7723 cp_lexer_consume_token (parser->lexer);
7724 /* Look for the opening `('. */
7725 matching_parens parens;
7726 parens.require_open (parser);
7727 location_t type_location
7728 = cp_lexer_peek_token (parser->lexer)->location;
7729 /* Parse the type-id. */
7731 type_id_in_expr_sentinel s (parser);
7732 type = cp_parser_type_id (parser);
7734 /* Look for the `,'. */
7735 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7736 /* Now, parse the assignment-expression. */
7737 expression = cp_parser_assignment_expression (parser);
7738 /* Look for the closing `)'. */
7739 parens.require_close (parser);
7740 postfix_expression
7741 = cp_build_bit_cast (type_location, type, expression,
7742 tf_warning_or_error);
7743 break;
7746 default:
7748 tree type;
7750 /* If the next thing is a simple-type-specifier, we may be
7751 looking at a functional cast. We could also be looking at
7752 an id-expression. So, we try the functional cast, and if
7753 that doesn't work we fall back to the primary-expression. */
7754 cp_parser_parse_tentatively (parser);
7755 /* Look for the simple-type-specifier. */
7756 ++parser->prevent_constrained_type_specifiers;
7757 type = cp_parser_simple_type_specifier (parser,
7758 /*decl_specs=*/NULL,
7759 CP_PARSER_FLAGS_NONE);
7760 --parser->prevent_constrained_type_specifiers;
7761 /* Parse the cast itself. */
7762 if (!cp_parser_error_occurred (parser))
7763 postfix_expression
7764 = cp_parser_functional_cast (parser, type);
7765 /* If that worked, we're done. */
7766 if (cp_parser_parse_definitely (parser))
7767 break;
7769 /* If the functional-cast didn't work out, try a
7770 compound-literal. */
7771 if (cp_parser_allow_gnu_extensions_p (parser)
7772 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7774 cp_expr initializer = NULL_TREE;
7776 cp_parser_parse_tentatively (parser);
7778 matching_parens parens;
7779 parens.consume_open (parser);
7781 /* Avoid calling cp_parser_type_id pointlessly, see comment
7782 in cp_parser_cast_expression about c++/29234. */
7783 if (!cp_parser_compound_literal_p (parser))
7784 cp_parser_simulate_error (parser);
7785 else
7787 /* Parse the type. */
7788 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7789 parser->in_type_id_in_expr_p = true;
7790 type = cp_parser_type_id (parser);
7791 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7792 parens.require_close (parser);
7795 /* If things aren't going well, there's no need to
7796 keep going. */
7797 if (!cp_parser_error_occurred (parser))
7798 /* Parse the brace-enclosed initializer list. */
7799 initializer = cp_parser_braced_list (parser);
7800 /* If that worked, we're definitely looking at a
7801 compound-literal expression. */
7802 if (cp_parser_parse_definitely (parser))
7804 /* Warn the user that a compound literal is not
7805 allowed in standard C++. */
7806 pedwarn (input_location, OPT_Wpedantic,
7807 "ISO C++ forbids compound-literals");
7808 /* For simplicity, we disallow compound literals in
7809 constant-expressions. We could
7810 allow compound literals of integer type, whose
7811 initializer was a constant, in constant
7812 expressions. Permitting that usage, as a further
7813 extension, would not change the meaning of any
7814 currently accepted programs. (Of course, as
7815 compound literals are not part of ISO C++, the
7816 standard has nothing to say.) */
7817 if (cp_parser_non_integral_constant_expression (parser,
7818 NIC_NCC))
7820 postfix_expression = error_mark_node;
7821 break;
7823 /* Form the representation of the compound-literal. */
7824 postfix_expression
7825 = finish_compound_literal (type, initializer,
7826 tf_warning_or_error, fcl_c99);
7827 postfix_expression.set_location (initializer.get_location ());
7828 break;
7832 /* It must be a primary-expression. */
7833 postfix_expression
7834 = cp_parser_primary_expression (parser, address_p, cast_p,
7835 /*template_arg_p=*/false,
7836 decltype_p,
7837 &idk);
7839 break;
7842 /* Note that we don't need to worry about calling build_cplus_new on a
7843 class-valued CALL_EXPR in decltype when it isn't the end of the
7844 postfix-expression; unary_complex_lvalue will take care of that for
7845 all these cases. */
7847 /* Keep looping until the postfix-expression is complete. */
7848 while (true)
7850 if (idk == CP_ID_KIND_UNQUALIFIED
7851 && identifier_p (postfix_expression)
7852 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7853 /* It is not a Koenig lookup function call. */
7854 postfix_expression
7855 = unqualified_name_lookup_error (postfix_expression);
7857 /* Peek at the next token. */
7858 token = cp_lexer_peek_token (parser->lexer);
7860 switch (token->type)
7862 case CPP_OPEN_SQUARE:
7863 if (cp_next_tokens_can_be_std_attribute_p (parser))
7865 cp_parser_error (parser,
7866 "two consecutive %<[%> shall "
7867 "only introduce an attribute");
7868 return error_mark_node;
7870 postfix_expression
7871 = cp_parser_postfix_open_square_expression (parser,
7872 postfix_expression,
7873 false,
7874 decltype_p);
7875 postfix_expression.set_range (start_loc,
7876 postfix_expression.get_location ());
7878 idk = CP_ID_KIND_NONE;
7879 is_member_access = false;
7880 break;
7882 case CPP_OPEN_PAREN:
7883 /* postfix-expression ( expression-list [opt] ) */
7885 bool koenig_p;
7886 bool is_builtin_constant_p;
7887 bool saved_integral_constant_expression_p = false;
7888 bool saved_non_integral_constant_expression_p = false;
7889 tsubst_flags_t complain = complain_flags (decltype_p);
7890 vec<tree, va_gc> *args;
7891 location_t close_paren_loc = UNKNOWN_LOCATION;
7892 location_t combined_loc = UNKNOWN_LOCATION;
7894 is_member_access = false;
7896 tree stripped_expression
7897 = tree_strip_any_location_wrapper (postfix_expression);
7898 is_builtin_constant_p
7899 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7900 if (is_builtin_constant_p)
7902 /* The whole point of __builtin_constant_p is to allow
7903 non-constant expressions to appear as arguments. */
7904 saved_integral_constant_expression_p
7905 = parser->integral_constant_expression_p;
7906 saved_non_integral_constant_expression_p
7907 = parser->non_integral_constant_expression_p;
7908 parser->integral_constant_expression_p = false;
7910 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
7911 && fndecl_built_in_p (stripped_expression,
7912 BUILT_IN_CLASSIFY_TYPE))
7914 /* __builtin_classify_type (type) */
7915 auto cl1 = make_temp_override
7916 (parser->type_definition_forbidden_message,
7917 G_("types may not be defined in "
7918 "%<__builtin_classify_type%> calls"));
7919 auto cl2 = make_temp_override
7920 (parser->type_definition_forbidden_message_arg,
7921 NULL);
7922 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
7923 true);
7924 cp_unevaluated uev;
7925 cp_parser_parse_tentatively (parser);
7926 matching_parens parens;
7927 parens.consume_open (parser);
7928 tree type = cp_parser_type_id (parser);
7929 parens.require_close (parser);
7930 if (cp_parser_parse_definitely (parser))
7932 if (dependent_type_p (type))
7934 postfix_expression = build_vl_exp (CALL_EXPR, 4);
7935 CALL_EXPR_FN (postfix_expression)
7936 = stripped_expression;
7937 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
7938 CALL_EXPR_ARG (postfix_expression, 0)
7939 = build_min (SIZEOF_EXPR, size_type_node, type);
7940 TREE_TYPE (postfix_expression) = integer_type_node;
7942 else
7944 postfix_expression
7945 = build_int_cst (integer_type_node,
7946 type_to_class (type));
7948 break;
7951 args = (cp_parser_parenthesized_expression_list
7952 (parser, non_attr,
7953 /*cast_p=*/false, /*allow_expansion_p=*/true,
7954 /*non_constant_p=*/NULL,
7955 /*close_paren_loc=*/&close_paren_loc,
7956 /*wrap_locations_p=*/true));
7957 if (is_builtin_constant_p)
7959 parser->integral_constant_expression_p
7960 = saved_integral_constant_expression_p;
7961 parser->non_integral_constant_expression_p
7962 = saved_non_integral_constant_expression_p;
7965 if (args == NULL)
7967 postfix_expression = error_mark_node;
7968 break;
7971 /* Function calls are not permitted in
7972 constant-expressions. */
7973 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7974 && cp_parser_non_integral_constant_expression (parser,
7975 NIC_FUNC_CALL))
7977 postfix_expression = error_mark_node;
7978 release_tree_vector (args);
7979 break;
7982 koenig_p = false;
7983 if (idk == CP_ID_KIND_UNQUALIFIED
7984 || idk == CP_ID_KIND_TEMPLATE_ID)
7986 if (identifier_p (postfix_expression)
7987 /* In C++20, we may need to perform ADL for a template
7988 name. */
7989 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7990 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7992 if (!args->is_empty ())
7994 koenig_p = true;
7995 if (!any_type_dependent_arguments_p (args))
7996 postfix_expression
7997 = perform_koenig_lookup (postfix_expression, args,
7998 complain);
8000 else
8001 postfix_expression
8002 = unqualified_fn_lookup_error (postfix_expression);
8004 /* We do not perform argument-dependent lookup if
8005 normal lookup finds a non-function, in accordance
8006 with the expected resolution of DR 218. */
8007 else if (!args->is_empty ()
8008 && is_overloaded_fn (postfix_expression))
8010 /* Do not do argument dependent lookup if regular
8011 lookup finds a member function or a block-scope
8012 function declaration. [basic.lookup.argdep]/3 */
8013 bool do_adl_p = true;
8014 tree fns = get_fns (postfix_expression);
8015 for (lkp_iterator iter (fns); iter; ++iter)
8017 tree fn = STRIP_TEMPLATE (*iter);
8018 if ((TREE_CODE (fn) == USING_DECL
8019 && DECL_DEPENDENT_P (fn))
8020 || DECL_FUNCTION_MEMBER_P (fn)
8021 || DECL_LOCAL_DECL_P (fn))
8023 do_adl_p = false;
8024 break;
8028 if (do_adl_p)
8030 koenig_p = true;
8031 if (!any_type_dependent_arguments_p (args))
8032 postfix_expression
8033 = perform_koenig_lookup (postfix_expression, args,
8034 complain);
8039 /* Temporarily set input_location to the combined location
8040 with call expression range, as e.g. build_out_target_exprs
8041 called from convert_default_arg relies on input_location,
8042 so updating it only when the call is fully built results
8043 in inconsistencies between location handling in templates
8044 and outside of templates. */
8045 if (close_paren_loc != UNKNOWN_LOCATION)
8046 combined_loc = make_location (token->location, start_loc,
8047 close_paren_loc);
8048 iloc_sentinel ils (combined_loc);
8050 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
8052 tree instance = TREE_OPERAND (postfix_expression, 0);
8053 tree fn = TREE_OPERAND (postfix_expression, 1);
8055 if (processing_template_decl
8056 && (type_dependent_object_expression_p (instance)
8057 || (!BASELINK_P (fn)
8058 && TREE_CODE (fn) != FIELD_DECL)
8059 || type_dependent_expression_p (fn)
8060 || any_type_dependent_arguments_p (args)))
8062 maybe_generic_this_capture (instance, fn);
8063 postfix_expression
8064 = build_min_nt_call_vec (postfix_expression, args);
8066 else if (BASELINK_P (fn))
8068 postfix_expression
8069 = (build_new_method_call
8070 (instance, fn, &args, NULL_TREE,
8071 (idk == CP_ID_KIND_QUALIFIED
8072 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
8073 : LOOKUP_NORMAL),
8074 /*fn_p=*/NULL,
8075 complain));
8077 else
8078 postfix_expression
8079 = finish_call_expr (postfix_expression, &args,
8080 /*disallow_virtual=*/false,
8081 /*koenig_p=*/false,
8082 complain);
8084 else if (TREE_CODE (postfix_expression) == OFFSET_REF
8085 || TREE_CODE (postfix_expression) == MEMBER_REF
8086 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8087 postfix_expression = (build_offset_ref_call_from_tree
8088 (postfix_expression, &args,
8089 complain));
8090 else if (idk == CP_ID_KIND_QUALIFIED)
8091 /* A call to a static class member, or a namespace-scope
8092 function. */
8093 postfix_expression
8094 = finish_call_expr (postfix_expression, &args,
8095 /*disallow_virtual=*/true,
8096 koenig_p,
8097 complain);
8098 else
8099 /* All other function calls. */
8101 if (DECL_P (postfix_expression)
8102 && parser->omp_for_parse_state
8103 && parser->omp_for_parse_state->in_intervening_code
8104 && omp_runtime_api_call (postfix_expression))
8106 error_at (loc, "calls to the OpenMP runtime API are "
8107 "not permitted in intervening code");
8108 parser->omp_for_parse_state->fail = true;
8110 postfix_expression
8111 = finish_call_expr (postfix_expression, &args,
8112 /*disallow_virtual=*/false,
8113 koenig_p,
8114 complain);
8116 if (close_paren_loc != UNKNOWN_LOCATION)
8117 postfix_expression.set_location (combined_loc);
8119 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8120 idk = CP_ID_KIND_NONE;
8122 release_tree_vector (args);
8124 break;
8126 case CPP_DOT:
8127 case CPP_DEREF:
8128 /* postfix-expression . template [opt] id-expression
8129 postfix-expression . pseudo-destructor-name
8130 postfix-expression -> template [opt] id-expression
8131 postfix-expression -> pseudo-destructor-name */
8133 /* Consume the `.' or `->' operator. */
8134 cp_lexer_consume_token (parser->lexer);
8136 postfix_expression
8137 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8138 postfix_expression,
8139 false, &idk, loc);
8141 is_member_access = true;
8142 break;
8144 case CPP_PLUS_PLUS:
8145 /* postfix-expression ++ */
8146 /* Consume the `++' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 /* Generate a representation for the complete expression. */
8149 postfix_expression
8150 = finish_increment_expr (postfix_expression,
8151 POSTINCREMENT_EXPR);
8152 /* Increments may not appear in constant-expressions. */
8153 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8154 postfix_expression = error_mark_node;
8155 idk = CP_ID_KIND_NONE;
8156 is_member_access = false;
8157 break;
8159 case CPP_MINUS_MINUS:
8160 /* postfix-expression -- */
8161 /* Consume the `--' token. */
8162 cp_lexer_consume_token (parser->lexer);
8163 /* Generate a representation for the complete expression. */
8164 postfix_expression
8165 = finish_increment_expr (postfix_expression,
8166 POSTDECREMENT_EXPR);
8167 /* Decrements may not appear in constant-expressions. */
8168 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8169 postfix_expression = error_mark_node;
8170 idk = CP_ID_KIND_NONE;
8171 is_member_access = false;
8172 break;
8174 default:
8175 if (pidk_return != NULL)
8176 * pidk_return = idk;
8177 if (member_access_only_p)
8178 return is_member_access
8179 ? postfix_expression
8180 : cp_expr (error_mark_node);
8181 else
8182 return postfix_expression;
8187 /* Helper function for cp_parser_parenthesized_expression_list and
8188 cp_parser_postfix_open_square_expression. Parse a single element
8189 of parenthesized expression list. */
8191 static cp_expr
8192 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8193 bool allow_expansion_p,
8194 bool *non_constant_p)
8196 cp_expr expr (NULL_TREE);
8197 bool expr_non_constant_p;
8199 /* Parse the next assignment-expression. */
8200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8202 /* A braced-init-list. */
8203 cp_lexer_set_source_position (parser->lexer);
8204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8205 expr = cp_parser_braced_list (parser,
8206 (non_constant_p != nullptr
8207 ? &expr_non_constant_p
8208 : nullptr));
8209 if (non_constant_p && expr_non_constant_p)
8210 *non_constant_p = true;
8212 else if (non_constant_p)
8214 expr = cp_parser_constant_expression (parser,
8215 /*allow_non_constant_p=*/true,
8216 &expr_non_constant_p);
8217 if (expr_non_constant_p)
8218 *non_constant_p = true;
8220 else
8221 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8223 /* If we have an ellipsis, then this is an expression expansion. */
8224 if (allow_expansion_p
8225 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8227 /* Consume the `...'. */
8228 cp_lexer_consume_token (parser->lexer);
8230 /* Build the argument pack. */
8231 expr = make_pack_expansion (expr);
8233 return expr;
8236 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8237 by cp_parser_builtin_offsetof. We're looking for
8239 postfix-expression [ expression ]
8240 postfix-expression [ braced-init-list ] (C++11)
8241 postfix-expression [ expression-list[opt] ] (C++23)
8243 FOR_OFFSETOF is set if we're being called in that context, which
8244 changes how we deal with integer constant expressions. */
8246 static tree
8247 cp_parser_postfix_open_square_expression (cp_parser *parser,
8248 tree postfix_expression,
8249 bool for_offsetof,
8250 bool decltype_p)
8252 tree index = NULL_TREE;
8253 releasing_vec expression_list = NULL;
8254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8255 bool saved_greater_than_is_operator_p;
8257 /* Consume the `[' token. */
8258 cp_lexer_consume_token (parser->lexer);
8260 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8261 parser->greater_than_is_operator_p = true;
8263 /* Parse the index expression. */
8264 /* ??? For offsetof, there is a question of what to allow here. If
8265 offsetof is not being used in an integral constant expression context,
8266 then we *could* get the right answer by computing the value at runtime.
8267 If we are in an integral constant expression context, then we might
8268 could accept any constant expression; hard to say without analysis.
8269 Rather than open the barn door too wide right away, allow only integer
8270 constant expressions here. */
8271 if (for_offsetof)
8272 index = cp_parser_constant_expression (parser);
8273 else
8275 if (cxx_dialect >= cxx23
8276 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8277 *&expression_list = make_tree_vector ();
8278 else if (cxx_dialect >= cxx23)
8280 while (true)
8282 cp_expr expr
8283 = cp_parser_parenthesized_expression_list_elt (parser,
8284 /*cast_p=*/
8285 false,
8286 /*allow_exp_p=*/
8287 true,
8288 /*non_cst_p=*/
8289 NULL);
8291 if (expr == error_mark_node)
8292 index = error_mark_node;
8293 else if (expression_list.get () == NULL
8294 && !PACK_EXPANSION_P (expr.get_value ()))
8295 index = expr.get_value ();
8296 else
8297 vec_safe_push (expression_list, expr.get_value ());
8299 /* If the next token isn't a `,', then we are done. */
8300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8301 break;
8303 if (expression_list.get () == NULL && index != error_mark_node)
8305 *&expression_list = make_tree_vector_single (index);
8306 index = NULL_TREE;
8309 /* Otherwise, consume the `,' and keep going. */
8310 cp_lexer_consume_token (parser->lexer);
8312 if (expression_list.get () && index == error_mark_node)
8313 expression_list.release ();
8315 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8317 cp_lexer_set_source_position (parser->lexer);
8318 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8319 index = cp_parser_braced_list (parser);
8321 else
8322 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8323 /*decltype_p=*/false,
8324 /*warn_comma_p=*/warn_comma_subscript);
8327 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8329 /* Look for the closing `]'. */
8330 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8332 /* Build the ARRAY_REF. */
8333 postfix_expression = grok_array_decl (loc, postfix_expression,
8334 index, &expression_list,
8335 tf_warning_or_error
8336 | (decltype_p ? tf_decltype : 0));
8338 /* When not doing offsetof, array references are not permitted in
8339 constant-expressions. */
8340 if (!for_offsetof
8341 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8342 postfix_expression = error_mark_node;
8344 return postfix_expression;
8347 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8348 dereference of incomplete type, returns true if error_mark_node should
8349 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8350 and *DEPENDENT_P. */
8352 bool
8353 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8354 bool *dependent_p)
8356 /* In a template, be permissive by treating an object expression
8357 of incomplete type as dependent (after a pedwarn). */
8358 diagnostic_t kind = (processing_template_decl
8359 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8361 switch (TREE_CODE (*postfix_expression))
8363 case CAST_EXPR:
8364 case REINTERPRET_CAST_EXPR:
8365 case CONST_CAST_EXPR:
8366 case STATIC_CAST_EXPR:
8367 case DYNAMIC_CAST_EXPR:
8368 case IMPLICIT_CONV_EXPR:
8369 case VIEW_CONVERT_EXPR:
8370 case NON_LVALUE_EXPR:
8371 kind = DK_ERROR;
8372 break;
8373 case OVERLOAD:
8374 /* Don't emit any diagnostic for OVERLOADs. */
8375 kind = DK_IGNORED;
8376 break;
8377 default:
8378 /* Avoid clobbering e.g. DECLs. */
8379 if (!EXPR_P (*postfix_expression))
8380 kind = DK_ERROR;
8381 break;
8384 if (kind == DK_IGNORED)
8385 return false;
8387 location_t exploc = location_of (*postfix_expression);
8388 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8389 if (!MAYBE_CLASS_TYPE_P (*scope))
8390 return true;
8391 if (kind == DK_ERROR)
8392 *scope = *postfix_expression = error_mark_node;
8393 else if (processing_template_decl)
8395 *dependent_p = true;
8396 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8398 return false;
8401 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8402 by cp_parser_builtin_offsetof. We're looking for
8404 postfix-expression . template [opt] id-expression
8405 postfix-expression . pseudo-destructor-name
8406 postfix-expression -> template [opt] id-expression
8407 postfix-expression -> pseudo-destructor-name
8409 FOR_OFFSETOF is set if we're being called in that context. That sorta
8410 limits what of the above we'll actually accept, but nevermind.
8411 TOKEN_TYPE is the "." or "->" token, which will already have been
8412 removed from the stream. */
8414 static tree
8415 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8416 enum cpp_ttype token_type,
8417 cp_expr postfix_expression,
8418 bool for_offsetof, cp_id_kind *idk,
8419 location_t location)
8421 tree name;
8422 bool dependent_p;
8423 bool pseudo_destructor_p;
8424 tree scope = NULL_TREE;
8425 location_t start_loc = postfix_expression.get_start ();
8427 /* If this is a `->' operator, dereference the pointer. */
8428 if (token_type == CPP_DEREF)
8429 postfix_expression = build_x_arrow (location, postfix_expression,
8430 tf_warning_or_error);
8431 /* Check to see whether or not the expression is type-dependent and
8432 not the current instantiation. */
8433 dependent_p = type_dependent_object_expression_p (postfix_expression);
8434 /* The identifier following the `->' or `.' is not qualified. */
8435 parser->scope = NULL_TREE;
8436 parser->qualifying_scope = NULL_TREE;
8437 parser->object_scope = NULL_TREE;
8438 *idk = CP_ID_KIND_NONE;
8440 /* Enter the scope corresponding to the type of the object
8441 given by the POSTFIX_EXPRESSION. */
8442 if (!dependent_p)
8444 scope = TREE_TYPE (postfix_expression);
8445 /* According to the standard, no expression should ever have
8446 reference type. Unfortunately, we do not currently match
8447 the standard in this respect in that our internal representation
8448 of an expression may have reference type even when the standard
8449 says it does not. Therefore, we have to manually obtain the
8450 underlying type here. */
8451 scope = non_reference (scope);
8452 /* The type of the POSTFIX_EXPRESSION must be complete. */
8453 /* Unlike the object expression in other contexts, *this is not
8454 required to be of complete type for purposes of class member
8455 access (5.2.5) outside the member function body. */
8456 if (postfix_expression != current_class_ref
8457 && scope != error_mark_node
8458 && !currently_open_class (scope))
8460 scope = complete_type (scope);
8461 if (!COMPLETE_TYPE_P (scope)
8462 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8463 &dependent_p))
8464 return error_mark_node;
8467 if (!dependent_p)
8469 /* Let the name lookup machinery know that we are processing a
8470 class member access expression. */
8471 parser->context->object_type = scope;
8472 /* If something went wrong, we want to be able to discern that case,
8473 as opposed to the case where there was no SCOPE due to the type
8474 of expression being dependent. */
8475 if (!scope)
8476 scope = error_mark_node;
8477 /* If the SCOPE was erroneous, make the various semantic analysis
8478 functions exit quickly -- and without issuing additional error
8479 messages. */
8480 if (scope == error_mark_node)
8481 postfix_expression = error_mark_node;
8485 if (dependent_p)
8487 tree type = TREE_TYPE (postfix_expression);
8488 /* If we don't have a (type-dependent) object of class type, use
8489 typeof to figure out the type of the object. */
8490 if (type == NULL_TREE || is_auto (type))
8491 type = finish_typeof (postfix_expression);
8492 parser->context->object_type = type;
8495 /* Assume this expression is not a pseudo-destructor access. */
8496 pseudo_destructor_p = false;
8498 /* If the SCOPE is a scalar type, then, if this is a valid program,
8499 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8500 is type dependent, it can be pseudo-destructor-name or something else.
8501 Try to parse it as pseudo-destructor-name first. */
8502 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8504 tree s;
8505 tree type;
8507 cp_parser_parse_tentatively (parser);
8508 /* Parse the pseudo-destructor-name. */
8509 s = NULL_TREE;
8510 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8511 &s, &type);
8512 if (dependent_p
8513 && (cp_parser_error_occurred (parser)
8514 || !SCALAR_TYPE_P (type)))
8515 cp_parser_abort_tentative_parse (parser);
8516 else if (cp_parser_parse_definitely (parser))
8518 pseudo_destructor_p = true;
8519 postfix_expression
8520 = finish_pseudo_destructor_expr (postfix_expression,
8521 s, type, location);
8525 if (!pseudo_destructor_p)
8527 /* If the SCOPE is not a scalar type, we are looking at an
8528 ordinary class member access expression, rather than a
8529 pseudo-destructor-name. */
8530 bool template_p;
8531 cp_token *token = cp_lexer_peek_token (parser->lexer);
8532 /* Parse the id-expression. */
8533 name = (cp_parser_id_expression
8534 (parser,
8535 cp_parser_optional_template_keyword (parser),
8536 /*check_dependency_p=*/true,
8537 &template_p,
8538 /*declarator_p=*/false,
8539 /*optional_p=*/false));
8540 /* In general, build a SCOPE_REF if the member name is qualified.
8541 However, if the name was not dependent and has already been
8542 resolved; there is no need to build the SCOPE_REF. For example;
8544 struct X { void f(); };
8545 template <typename T> void f(T* t) { t->X::f(); }
8547 Even though "t" is dependent, "X::f" is not and has been resolved
8548 to a BASELINK; there is no need to include scope information. */
8550 /* But we do need to remember that there was an explicit scope for
8551 virtual function calls. */
8552 if (parser->scope)
8553 *idk = CP_ID_KIND_QUALIFIED;
8555 /* If the name is a template-id that names a type, we will get a
8556 TYPE_DECL here. That is invalid code. */
8557 if (TREE_CODE (name) == TYPE_DECL)
8559 error_at (token->location, "invalid use of %qD", name);
8560 postfix_expression = error_mark_node;
8562 else
8564 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8566 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8568 error_at (token->location, "%<%D::%D%> is not a class member",
8569 parser->scope, name);
8570 postfix_expression = error_mark_node;
8572 else
8573 name = build_qualified_name (/*type=*/NULL_TREE,
8574 parser->scope,
8575 name,
8576 template_p);
8577 parser->scope = NULL_TREE;
8578 parser->qualifying_scope = NULL_TREE;
8579 parser->object_scope = NULL_TREE;
8581 if (parser->scope && name && BASELINK_P (name))
8582 adjust_result_of_qualified_name_lookup
8583 (name, parser->scope, scope);
8584 postfix_expression
8585 = finish_class_member_access_expr (postfix_expression, name,
8586 template_p,
8587 tf_warning_or_error);
8588 /* Build a location e.g.:
8589 ptr->access_expr
8590 ~~~^~~~~~~~~~~~~
8591 where the caret is at the deref token, ranging from
8592 the start of postfix_expression to the end of the access expr. */
8593 location_t combined_loc
8594 = make_location (input_location, start_loc, parser->lexer);
8595 protected_set_expr_location (postfix_expression, combined_loc);
8599 /* We no longer need to look up names in the scope of the object on
8600 the left-hand side of the `.' or `->' operator. */
8601 parser->context->object_type = NULL_TREE;
8603 /* Outside of offsetof, these operators may not appear in
8604 constant-expressions. */
8605 if (!for_offsetof
8606 && (cp_parser_non_integral_constant_expression
8607 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8608 postfix_expression = error_mark_node;
8610 return postfix_expression;
8613 /* Parse a parenthesized expression-list.
8615 expression-list:
8616 assignment-expression
8617 expression-list, assignment-expression
8619 attribute-list:
8620 expression-list
8621 identifier
8622 identifier, expression-list
8624 CAST_P is true if this expression is the target of a cast.
8626 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8627 argument pack.
8629 WRAP_LOCATIONS_P is true if expressions within this list for which
8630 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8631 their source locations.
8633 Returns a vector of trees. Each element is a representation of an
8634 assignment-expression. NULL is returned if the ( and or ) are
8635 missing. An empty, but allocated, vector is returned on no
8636 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8637 if we are parsing an attribute list for an attribute that wants a
8638 plain identifier argument, normal_attr for an attribute that wants
8639 an expression, or non_attr if we aren't parsing an attribute list. If
8640 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8641 not all of the expressions in the list were constant.
8642 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8643 will be written to with the location of the closing parenthesis. If
8644 an error occurs, it may or may not be written to. */
8646 static vec<tree, va_gc> *
8647 cp_parser_parenthesized_expression_list (cp_parser* parser,
8648 int is_attribute_list,
8649 bool cast_p,
8650 bool allow_expansion_p,
8651 bool *non_constant_p,
8652 location_t *close_paren_loc,
8653 bool wrap_locations_p)
8655 vec<tree, va_gc> *expression_list;
8656 bool saved_greater_than_is_operator_p;
8658 /* Assume all the expressions will be constant. */
8659 if (non_constant_p)
8660 *non_constant_p = false;
8662 matching_parens parens;
8663 if (!parens.require_open (parser))
8664 return NULL;
8666 expression_list = make_tree_vector ();
8668 /* Within a parenthesized expression, a `>' token is always
8669 the greater-than operator. */
8670 saved_greater_than_is_operator_p
8671 = parser->greater_than_is_operator_p;
8672 parser->greater_than_is_operator_p = true;
8674 cp_expr expr (NULL_TREE);
8676 /* Consume expressions until there are no more. */
8677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8678 while (true)
8680 /* At the beginning of attribute lists, check to see if the
8681 next token is an identifier. */
8682 if (is_attribute_list == id_attr
8683 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8684 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8685 else if (is_attribute_list == assume_attr)
8686 expr = cp_parser_conditional_expression (parser);
8687 else
8688 expr
8689 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8690 allow_expansion_p,
8691 non_constant_p);
8693 if (wrap_locations_p)
8694 expr.maybe_add_location_wrapper ();
8696 /* Add it to the list. We add error_mark_node
8697 expressions to the list, so that we can still tell if
8698 the correct form for a parenthesized expression-list
8699 is found. That gives better errors. */
8700 vec_safe_push (expression_list, expr.get_value ());
8702 if (expr == error_mark_node)
8703 goto skip_comma;
8705 /* After the first item, attribute lists look the same as
8706 expression lists. */
8707 is_attribute_list = non_attr;
8709 get_comma:;
8710 /* If the next token isn't a `,', then we are done. */
8711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8712 break;
8714 /* Otherwise, consume the `,' and keep going. */
8715 cp_lexer_consume_token (parser->lexer);
8718 if (close_paren_loc)
8719 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8721 if (!parens.require_close (parser))
8723 int ending;
8725 skip_comma:;
8726 /* We try and resync to an unnested comma, as that will give the
8727 user better diagnostics. */
8728 ending = cp_parser_skip_to_closing_parenthesis (parser,
8729 /*recovering=*/true,
8730 /*or_comma=*/true,
8731 /*consume_paren=*/true);
8732 if (ending < 0)
8733 goto get_comma;
8734 if (!ending)
8736 parser->greater_than_is_operator_p
8737 = saved_greater_than_is_operator_p;
8738 return NULL;
8742 parser->greater_than_is_operator_p
8743 = saved_greater_than_is_operator_p;
8745 return expression_list;
8748 /* Parse a pseudo-destructor-name.
8750 pseudo-destructor-name:
8751 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8752 :: [opt] nested-name-specifier template template-id :: ~ type-name
8753 :: [opt] nested-name-specifier [opt] ~ type-name
8755 If either of the first two productions is used, sets *SCOPE to the
8756 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8757 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8758 or ERROR_MARK_NODE if the parse fails. */
8760 static void
8761 cp_parser_pseudo_destructor_name (cp_parser* parser,
8762 tree object,
8763 tree* scope,
8764 tree* type)
8766 bool nested_name_specifier_p;
8768 /* Handle ~auto. */
8769 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8770 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8771 && !type_dependent_expression_p (object))
8773 if (cxx_dialect < cxx14)
8774 pedwarn (input_location, OPT_Wc__14_extensions,
8775 "%<~auto%> only available with "
8776 "%<-std=c++14%> or %<-std=gnu++14%>");
8777 cp_lexer_consume_token (parser->lexer);
8778 cp_lexer_consume_token (parser->lexer);
8779 *scope = NULL_TREE;
8780 *type = TREE_TYPE (object);
8781 return;
8784 /* Assume that things will not work out. */
8785 *type = error_mark_node;
8787 /* Look for the optional `::' operator. */
8788 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8789 /* Look for the optional nested-name-specifier. */
8790 nested_name_specifier_p
8791 = (cp_parser_nested_name_specifier_opt (parser,
8792 /*typename_keyword_p=*/false,
8793 /*check_dependency_p=*/true,
8794 /*type_p=*/false,
8795 /*is_declaration=*/false)
8796 != NULL_TREE);
8797 /* Now, if we saw a nested-name-specifier, we might be doing the
8798 second production. */
8799 if (nested_name_specifier_p
8800 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8802 /* Consume the `template' keyword. */
8803 cp_lexer_consume_token (parser->lexer);
8804 /* Parse the template-id. */
8805 cp_parser_template_id (parser,
8806 /*template_keyword_p=*/true,
8807 /*check_dependency_p=*/false,
8808 class_type,
8809 /*is_declaration=*/true);
8810 /* Look for the `::' token. */
8811 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8813 /* If the next token is not a `~', then there might be some
8814 additional qualification. */
8815 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8817 /* At this point, we're looking for "type-name :: ~". The type-name
8818 must not be a class-name, since this is a pseudo-destructor. So,
8819 it must be either an enum-name, or a typedef-name -- both of which
8820 are just identifiers. So, we peek ahead to check that the "::"
8821 and "~" tokens are present; if they are not, then we can avoid
8822 calling type_name. */
8823 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8824 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8825 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8827 cp_parser_error (parser, "non-scalar type");
8828 return;
8831 /* Look for the type-name. */
8832 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8833 if (*scope == error_mark_node)
8834 return;
8836 /* Look for the `::' token. */
8837 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8839 else
8840 *scope = NULL_TREE;
8842 /* Look for the `~'. */
8843 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8845 /* Once we see the ~, this has to be a pseudo-destructor. */
8846 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8847 cp_parser_commit_to_topmost_tentative_parse (parser);
8849 /* Look for the type-name again. We are not responsible for
8850 checking that it matches the first type-name. */
8851 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8854 /* Parse a unary-expression.
8856 unary-expression:
8857 postfix-expression
8858 ++ cast-expression
8859 -- cast-expression
8860 await-expression
8861 unary-operator cast-expression
8862 sizeof unary-expression
8863 sizeof ( type-id )
8864 alignof ( type-id ) [C++0x]
8865 new-expression
8866 delete-expression
8868 GNU Extensions:
8870 unary-expression:
8871 __extension__ cast-expression
8872 __alignof__ unary-expression
8873 __alignof__ ( type-id )
8874 alignof unary-expression [C++0x]
8875 __real__ cast-expression
8876 __imag__ cast-expression
8877 && identifier
8878 sizeof ( type-id ) { initializer-list , [opt] }
8879 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8880 __alignof__ ( type-id ) { initializer-list , [opt] }
8882 ADDRESS_P is true iff the unary-expression is appearing as the
8883 operand of the `&' operator. CAST_P is true if this expression is
8884 the target of a cast.
8886 Returns a representation of the expression. */
8888 static cp_expr
8889 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8890 bool address_p, bool cast_p, bool decltype_p)
8892 cp_token *token;
8893 enum tree_code unary_operator;
8895 /* Peek at the next token. */
8896 token = cp_lexer_peek_token (parser->lexer);
8897 /* Some keywords give away the kind of expression. */
8898 if (token->type == CPP_KEYWORD)
8900 enum rid keyword = token->keyword;
8902 switch (keyword)
8904 case RID_ALIGNOF:
8905 case RID_SIZEOF:
8907 tree operand, ret;
8908 enum tree_code op;
8909 location_t start_loc = token->location;
8911 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8912 bool std_alignof = id_equal (token->u.value, "alignof");
8914 /* Consume the token. */
8915 cp_lexer_consume_token (parser->lexer);
8916 /* Parse the operand. */
8917 operand = cp_parser_sizeof_operand (parser, keyword);
8919 /* Construct a location e.g. :
8920 alignof (expr)
8921 ^~~~~~~~~~~~~~
8922 with start == caret at the start of the "alignof"/"sizeof"
8923 token, with the endpoint at the final closing paren. */
8924 location_t compound_loc
8925 = make_location (start_loc, start_loc, parser->lexer);
8927 if (TYPE_P (operand))
8928 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8929 std_alignof, true);
8930 else
8932 /* ISO C++ defines alignof only with types, not with
8933 expressions. So pedwarn if alignof is used with a non-
8934 type expression. However, __alignof__ is ok. */
8935 if (std_alignof)
8936 pedwarn (token->location, OPT_Wpedantic,
8937 "ISO C++ does not allow %<alignof%> "
8938 "with a non-type");
8940 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8941 std_alignof, true);
8943 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8944 SIZEOF_EXPR with the original operand. */
8945 if (op == SIZEOF_EXPR && ret != error_mark_node)
8947 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8949 if (!processing_template_decl && TYPE_P (operand))
8951 ret = build_min (SIZEOF_EXPR, size_type_node,
8952 build1 (NOP_EXPR, operand,
8953 error_mark_node));
8954 SIZEOF_EXPR_TYPE_P (ret) = 1;
8956 else
8957 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8958 TREE_SIDE_EFFECTS (ret) = 0;
8959 TREE_READONLY (ret) = 1;
8960 SET_EXPR_LOCATION (ret, compound_loc);
8964 cp_expr ret_expr (ret, compound_loc);
8965 ret_expr = ret_expr.maybe_add_location_wrapper ();
8966 return ret_expr;
8969 case RID_BUILTIN_HAS_ATTRIBUTE:
8970 return cp_parser_has_attribute_expression (parser);
8972 case RID_NEW:
8973 return cp_parser_new_expression (parser);
8975 case RID_DELETE:
8976 return cp_parser_delete_expression (parser);
8978 case RID_EXTENSION:
8980 /* The saved value of the PEDANTIC flag. */
8981 int saved_pedantic;
8982 tree expr;
8984 /* Save away the PEDANTIC flag. */
8985 cp_parser_extension_opt (parser, &saved_pedantic);
8986 /* Parse the cast-expression. */
8987 expr = cp_parser_simple_cast_expression (parser);
8988 /* Restore the PEDANTIC flag. */
8989 pedantic = saved_pedantic;
8991 return expr;
8994 case RID_REALPART:
8995 case RID_IMAGPART:
8997 tree expression;
8999 /* Consume the `__real__' or `__imag__' token. */
9000 cp_lexer_consume_token (parser->lexer);
9001 /* Parse the cast-expression. */
9002 expression = cp_parser_simple_cast_expression (parser);
9003 /* Create the complete representation. */
9004 return build_x_unary_op (token->location,
9005 (keyword == RID_REALPART
9006 ? REALPART_EXPR : IMAGPART_EXPR),
9007 expression, NULL_TREE,
9008 tf_warning_or_error);
9010 break;
9012 case RID_TRANSACTION_ATOMIC:
9013 case RID_TRANSACTION_RELAXED:
9014 return cp_parser_transaction_expression (parser, keyword);
9016 case RID_NOEXCEPT:
9018 tree expr;
9019 const char *saved_message;
9020 bool saved_integral_constant_expression_p;
9021 bool saved_non_integral_constant_expression_p;
9022 bool saved_greater_than_is_operator_p;
9024 location_t start_loc = token->location;
9026 cp_lexer_consume_token (parser->lexer);
9027 matching_parens parens;
9028 parens.require_open (parser);
9030 saved_message = parser->type_definition_forbidden_message;
9031 parser->type_definition_forbidden_message
9032 = G_("types may not be defined in %<noexcept%> expressions");
9034 saved_integral_constant_expression_p
9035 = parser->integral_constant_expression_p;
9036 saved_non_integral_constant_expression_p
9037 = parser->non_integral_constant_expression_p;
9038 parser->integral_constant_expression_p = false;
9040 saved_greater_than_is_operator_p
9041 = parser->greater_than_is_operator_p;
9042 parser->greater_than_is_operator_p = true;
9044 ++cp_unevaluated_operand;
9045 ++c_inhibit_evaluation_warnings;
9046 ++cp_noexcept_operand;
9047 expr = cp_parser_expression (parser);
9048 --cp_noexcept_operand;
9049 --c_inhibit_evaluation_warnings;
9050 --cp_unevaluated_operand;
9052 parser->greater_than_is_operator_p
9053 = saved_greater_than_is_operator_p;
9055 parser->integral_constant_expression_p
9056 = saved_integral_constant_expression_p;
9057 parser->non_integral_constant_expression_p
9058 = saved_non_integral_constant_expression_p;
9060 parser->type_definition_forbidden_message = saved_message;
9062 parens.require_close (parser);
9064 /* Construct a location of the form:
9065 noexcept (expr)
9066 ^~~~~~~~~~~~~~~
9067 with start == caret, finishing at the close-paren. */
9068 location_t noexcept_loc
9069 = make_location (start_loc, start_loc, parser->lexer);
9071 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9072 noexcept_loc);
9075 case RID_CO_AWAIT:
9077 tree expr;
9078 location_t kw_loc = token->location;
9080 /* Consume the `co_await' token. */
9081 cp_lexer_consume_token (parser->lexer);
9082 /* Parse its cast-expression. */
9083 expr = cp_parser_simple_cast_expression (parser);
9084 if (expr == error_mark_node)
9085 return error_mark_node;
9087 /* Handle [expr.await]. */
9088 return cp_expr (finish_co_await_expr (kw_loc, expr));
9091 default:
9092 break;
9096 /* Look for the `:: new' and `:: delete', which also signal the
9097 beginning of a new-expression, or delete-expression,
9098 respectively. If the next token is `::', then it might be one of
9099 these. */
9100 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9102 enum rid keyword;
9104 /* See if the token after the `::' is one of the keywords in
9105 which we're interested. */
9106 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9107 /* If it's `new', we have a new-expression. */
9108 if (keyword == RID_NEW)
9109 return cp_parser_new_expression (parser);
9110 /* Similarly, for `delete'. */
9111 else if (keyword == RID_DELETE)
9112 return cp_parser_delete_expression (parser);
9115 /* Look for a unary operator. */
9116 unary_operator = cp_parser_unary_operator (token);
9117 /* The `++' and `--' operators can be handled similarly, even though
9118 they are not technically unary-operators in the grammar. */
9119 if (unary_operator == ERROR_MARK)
9121 if (token->type == CPP_PLUS_PLUS)
9122 unary_operator = PREINCREMENT_EXPR;
9123 else if (token->type == CPP_MINUS_MINUS)
9124 unary_operator = PREDECREMENT_EXPR;
9125 /* Handle the GNU address-of-label extension. */
9126 else if (cp_parser_allow_gnu_extensions_p (parser)
9127 && token->type == CPP_AND_AND)
9129 tree identifier;
9130 tree expression;
9131 location_t start_loc = token->location;
9133 /* Consume the '&&' token. */
9134 cp_lexer_consume_token (parser->lexer);
9135 /* Look for the identifier. */
9136 identifier = cp_parser_identifier (parser);
9137 /* Construct a location of the form:
9138 &&label
9139 ^~~~~~~
9140 with caret==start at the "&&", finish at the end of the label. */
9141 location_t combined_loc
9142 = make_location (start_loc, start_loc, parser->lexer);
9143 /* Create an expression representing the address. */
9144 expression = finish_label_address_expr (identifier, combined_loc);
9145 if (cp_parser_non_integral_constant_expression (parser,
9146 NIC_ADDR_LABEL))
9147 expression = error_mark_node;
9148 return expression;
9151 if (unary_operator != ERROR_MARK)
9153 cp_expr cast_expression;
9154 cp_expr expression = error_mark_node;
9155 non_integral_constant non_constant_p = NIC_NONE;
9156 location_t loc = token->location;
9157 tsubst_flags_t complain = complain_flags (decltype_p);
9159 /* Consume the operator token. */
9160 token = cp_lexer_consume_token (parser->lexer);
9161 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9163 /* Parse the cast-expression. */
9164 cast_expression
9165 = cp_parser_cast_expression (parser,
9166 unary_operator == ADDR_EXPR,
9167 /*cast_p=*/false,
9168 /*decltype*/false,
9169 pidk);
9171 /* Make a location:
9172 OP_TOKEN CAST_EXPRESSION
9173 ^~~~~~~~~~~~~~~~~~~~~~~~~
9174 with start==caret at the operator token, and
9175 extending to the end of the cast_expression. */
9176 loc = make_location (loc, loc, cast_expression.get_finish ());
9178 /* Now, build an appropriate representation. */
9179 switch (unary_operator)
9181 case INDIRECT_REF:
9182 non_constant_p = NIC_STAR;
9183 expression = build_x_indirect_ref (loc, cast_expression,
9184 RO_UNARY_STAR, NULL_TREE,
9185 complain);
9186 /* TODO: build_x_indirect_ref does not always honor the
9187 location, so ensure it is set. */
9188 expression.set_location (loc);
9189 break;
9191 case ADDR_EXPR:
9192 non_constant_p = NIC_ADDR;
9193 /* Fall through. */
9194 case BIT_NOT_EXPR:
9195 expression = build_x_unary_op (loc, unary_operator,
9196 cast_expression,
9197 NULL_TREE, complain);
9198 /* TODO: build_x_unary_op does not always honor the location,
9199 so ensure it is set. */
9200 expression.set_location (loc);
9201 break;
9203 case PREINCREMENT_EXPR:
9204 case PREDECREMENT_EXPR:
9205 non_constant_p = unary_operator == PREINCREMENT_EXPR
9206 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9207 /* Fall through. */
9208 case NEGATE_EXPR:
9209 /* Immediately fold negation of a constant, unless the constant is 0
9210 (since -0 == 0) or it would overflow. */
9211 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9213 tree stripped_expr
9214 = tree_strip_any_location_wrapper (cast_expression);
9215 if (CONSTANT_CLASS_P (stripped_expr)
9216 && !integer_zerop (stripped_expr)
9217 && !TREE_OVERFLOW (stripped_expr))
9219 tree folded = fold_build1 (unary_operator,
9220 TREE_TYPE (stripped_expr),
9221 stripped_expr);
9222 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9224 expression = maybe_wrap_with_location (folded, loc);
9225 break;
9229 /* Fall through. */
9230 case UNARY_PLUS_EXPR:
9231 case TRUTH_NOT_EXPR:
9232 expression = finish_unary_op_expr (loc, unary_operator,
9233 cast_expression, complain);
9234 break;
9236 default:
9237 gcc_unreachable ();
9240 if (non_constant_p != NIC_NONE
9241 && cp_parser_non_integral_constant_expression (parser,
9242 non_constant_p))
9243 expression = error_mark_node;
9245 return expression;
9248 return cp_parser_postfix_expression (parser, address_p, cast_p,
9249 /*member_access_only_p=*/false,
9250 decltype_p,
9251 pidk);
9254 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9255 unary-operator, the corresponding tree code is returned. */
9257 static enum tree_code
9258 cp_parser_unary_operator (cp_token* token)
9260 switch (token->type)
9262 case CPP_MULT:
9263 return INDIRECT_REF;
9265 case CPP_AND:
9266 return ADDR_EXPR;
9268 case CPP_PLUS:
9269 return UNARY_PLUS_EXPR;
9271 case CPP_MINUS:
9272 return NEGATE_EXPR;
9274 case CPP_NOT:
9275 return TRUTH_NOT_EXPR;
9277 case CPP_COMPL:
9278 return BIT_NOT_EXPR;
9280 default:
9281 return ERROR_MARK;
9285 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9286 Returns a representation of the expression. */
9288 static tree
9289 cp_parser_has_attribute_expression (cp_parser *parser)
9291 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9293 /* Consume the __builtin_has_attribute token. */
9294 cp_lexer_consume_token (parser->lexer);
9296 matching_parens parens;
9297 if (!parens.require_open (parser))
9298 return error_mark_node;
9300 /* Types cannot be defined in a `sizeof' expression. Save away the
9301 old message. */
9302 const char *saved_message = parser->type_definition_forbidden_message;
9303 const char *saved_message_arg
9304 = parser->type_definition_forbidden_message_arg;
9305 parser->type_definition_forbidden_message
9306 = G_("types may not be defined in %qs expressions");
9307 parser->type_definition_forbidden_message_arg
9308 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9310 /* The restrictions on constant-expressions do not apply inside
9311 sizeof expressions. */
9312 bool saved_integral_constant_expression_p
9313 = parser->integral_constant_expression_p;
9314 bool saved_non_integral_constant_expression_p
9315 = parser->non_integral_constant_expression_p;
9316 parser->integral_constant_expression_p = false;
9318 /* Do not actually evaluate the expression. */
9319 ++cp_unevaluated_operand;
9320 ++c_inhibit_evaluation_warnings;
9322 tree oper = NULL_TREE;
9324 /* We can't be sure yet whether we're looking at a type-id or an
9325 expression. */
9326 cp_parser_parse_tentatively (parser);
9328 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9329 parser->in_type_id_in_expr_p = true;
9330 /* Look for the type-id. */
9331 oper = cp_parser_type_id (parser);
9332 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9334 cp_parser_parse_definitely (parser);
9336 /* If the type-id production did not work out, then we must be
9337 looking at an expression. */
9338 if (!oper || oper == error_mark_node)
9339 oper = cp_parser_assignment_expression (parser);
9341 STRIP_ANY_LOCATION_WRAPPER (oper);
9343 /* Go back to evaluating expressions. */
9344 --cp_unevaluated_operand;
9345 --c_inhibit_evaluation_warnings;
9347 /* And restore the old one. */
9348 parser->type_definition_forbidden_message = saved_message;
9349 parser->type_definition_forbidden_message_arg = saved_message_arg;
9350 parser->integral_constant_expression_p
9351 = saved_integral_constant_expression_p;
9352 parser->non_integral_constant_expression_p
9353 = saved_non_integral_constant_expression_p;
9355 /* Consume the comma if it's there. */
9356 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9358 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9359 /*consume_paren=*/true);
9360 return error_mark_node;
9363 /* Parse the attribute specification. */
9364 bool ret = false;
9365 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9366 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9368 if (oper == error_mark_node)
9369 /* Nothing. */;
9370 else if (processing_template_decl && uses_template_parms (oper))
9371 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9372 "not supported yet");
9373 else
9375 /* Fold constant expressions used in attributes first. */
9376 cp_check_const_attributes (attr);
9378 /* Finally, see if OPER has been declared with ATTR. */
9379 ret = has_attribute (atloc, oper, attr, default_conversion);
9382 parens.require_close (parser);
9384 else
9386 error_at (atloc, "expected identifier");
9387 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9390 /* Construct a location e.g. :
9391 __builtin_has_attribute (oper, attr)
9392 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9393 with start == caret at the start of the built-in token,
9394 and with the endpoint at the final closing paren. */
9395 location_t compound_loc
9396 = make_location (start_loc, start_loc, parser->lexer);
9398 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9399 ret_expr.set_location (compound_loc);
9400 ret_expr = ret_expr.maybe_add_location_wrapper ();
9401 return ret_expr;
9404 /* Parse a new-expression.
9406 new-expression:
9407 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9408 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9410 Returns a representation of the expression. */
9412 static tree
9413 cp_parser_new_expression (cp_parser* parser)
9415 bool global_scope_p;
9416 vec<tree, va_gc> *placement;
9417 tree type;
9418 vec<tree, va_gc> *initializer;
9419 tree nelts = NULL_TREE;
9420 tree ret;
9422 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9424 /* Look for the optional `::' operator. */
9425 global_scope_p
9426 = (cp_parser_global_scope_opt (parser,
9427 /*current_scope_valid_p=*/false)
9428 != NULL_TREE);
9429 /* Look for the `new' operator. */
9430 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9431 /* There's no easy way to tell a new-placement from the
9432 `( type-id )' construct. */
9433 cp_parser_parse_tentatively (parser);
9434 /* Look for a new-placement. */
9435 placement = cp_parser_new_placement (parser);
9436 /* If that didn't work out, there's no new-placement. */
9437 if (!cp_parser_parse_definitely (parser))
9439 if (placement != NULL)
9440 release_tree_vector (placement);
9441 placement = NULL;
9444 /* If the next token is a `(', then we have a parenthesized
9445 type-id. */
9446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9448 cp_token *token;
9449 const char *saved_message = parser->type_definition_forbidden_message;
9451 /* Consume the `('. */
9452 matching_parens parens;
9453 parens.consume_open (parser);
9455 /* Parse the type-id. */
9456 parser->type_definition_forbidden_message
9457 = G_("types may not be defined in a new-expression");
9459 type_id_in_expr_sentinel s (parser);
9460 type = cp_parser_type_id (parser);
9462 parser->type_definition_forbidden_message = saved_message;
9464 /* Look for the closing `)'. */
9465 parens.require_close (parser);
9466 token = cp_lexer_peek_token (parser->lexer);
9467 /* There should not be a direct-new-declarator in this production,
9468 but GCC used to allowed this, so we check and emit a sensible error
9469 message for this case. */
9470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9472 error_at (token->location,
9473 "array bound forbidden after parenthesized type-id");
9474 inform (token->location,
9475 "try removing the parentheses around the type-id");
9476 cp_parser_direct_new_declarator (parser);
9479 /* Otherwise, there must be a new-type-id. */
9480 else
9481 type = cp_parser_new_type_id (parser, &nelts);
9483 /* If the next token is a `(' or '{', then we have a new-initializer. */
9484 cp_token *token = cp_lexer_peek_token (parser->lexer);
9485 if (token->type == CPP_OPEN_PAREN
9486 || token->type == CPP_OPEN_BRACE)
9487 initializer = cp_parser_new_initializer (parser);
9488 else
9489 initializer = NULL;
9491 /* A new-expression may not appear in an integral constant
9492 expression. */
9493 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9494 ret = error_mark_node;
9495 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9496 of a new-type-id or type-id of a new-expression, the new-expression shall
9497 contain a new-initializer of the form ( assignment-expression )".
9498 Additionally, consistently with the spirit of DR 1467, we want to accept
9499 'new auto { 2 }' too. */
9500 else if ((ret = type_uses_auto (type))
9501 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9502 && (vec_safe_length (initializer) != 1
9503 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9504 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9506 error_at (token->location,
9507 "initialization of new-expression for type %<auto%> "
9508 "requires exactly one element");
9509 ret = error_mark_node;
9511 else
9513 /* Construct a location e.g.:
9514 ptr = new int[100]
9515 ^~~~~~~~~~~~
9516 with caret == start at the start of the "new" token, and the end
9517 at the end of the final token we consumed. */
9518 location_t combined_loc = make_location (start_loc, start_loc,
9519 parser->lexer);
9520 /* Create a representation of the new-expression. */
9521 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9522 global_scope_p, tf_warning_or_error);
9525 if (placement != NULL)
9526 release_tree_vector (placement);
9527 if (initializer != NULL)
9528 release_tree_vector (initializer);
9530 return ret;
9533 /* Parse a new-placement.
9535 new-placement:
9536 ( expression-list )
9538 Returns the same representation as for an expression-list. */
9540 static vec<tree, va_gc> *
9541 cp_parser_new_placement (cp_parser* parser)
9543 vec<tree, va_gc> *expression_list;
9545 /* Parse the expression-list. */
9546 expression_list = (cp_parser_parenthesized_expression_list
9547 (parser, non_attr, /*cast_p=*/false,
9548 /*allow_expansion_p=*/true,
9549 /*non_constant_p=*/NULL));
9551 if (expression_list && expression_list->is_empty ())
9552 error ("expected expression-list or type-id");
9554 return expression_list;
9557 /* Parse a new-type-id.
9559 new-type-id:
9560 type-specifier-seq new-declarator [opt]
9562 Returns the TYPE allocated. If the new-type-id indicates an array
9563 type, *NELTS is set to the number of elements in the last array
9564 bound; the TYPE will not include the last array bound. */
9566 static tree
9567 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9569 cp_decl_specifier_seq type_specifier_seq;
9570 cp_declarator *new_declarator;
9571 cp_declarator *declarator;
9572 cp_declarator *outer_declarator;
9573 const char *saved_message;
9575 /* The type-specifier sequence must not contain type definitions.
9576 (It cannot contain declarations of new types either, but if they
9577 are not definitions we will catch that because they are not
9578 complete.) */
9579 saved_message = parser->type_definition_forbidden_message;
9580 parser->type_definition_forbidden_message
9581 = G_("types may not be defined in a new-type-id");
9582 /* Parse the type-specifier-seq. */
9583 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9584 /*is_declaration=*/false,
9585 /*is_trailing_return=*/false,
9586 &type_specifier_seq);
9587 /* Restore the old message. */
9588 parser->type_definition_forbidden_message = saved_message;
9590 if (type_specifier_seq.type == error_mark_node)
9591 return error_mark_node;
9593 /* Parse the new-declarator. */
9594 new_declarator = cp_parser_new_declarator_opt (parser);
9596 /* Determine the number of elements in the last array dimension, if
9597 any. */
9598 *nelts = NULL_TREE;
9599 /* Skip down to the last array dimension. */
9600 declarator = new_declarator;
9601 outer_declarator = NULL;
9602 while (declarator && (declarator->kind == cdk_pointer
9603 || declarator->kind == cdk_ptrmem))
9605 outer_declarator = declarator;
9606 declarator = declarator->declarator;
9608 while (declarator
9609 && declarator->kind == cdk_array
9610 && declarator->declarator
9611 && declarator->declarator->kind == cdk_array)
9613 outer_declarator = declarator;
9614 declarator = declarator->declarator;
9617 if (declarator && declarator->kind == cdk_array)
9619 *nelts = declarator->u.array.bounds;
9620 if (*nelts == error_mark_node)
9621 *nelts = integer_one_node;
9623 if (*nelts == NULL_TREE)
9624 /* Leave [] in the declarator. */;
9625 else if (outer_declarator)
9626 outer_declarator->declarator = declarator->declarator;
9627 else
9628 new_declarator = NULL;
9631 return groktypename (&type_specifier_seq, new_declarator, false);
9634 /* Parse an (optional) new-declarator.
9636 new-declarator:
9637 ptr-operator new-declarator [opt]
9638 direct-new-declarator
9640 Returns the declarator. */
9642 static cp_declarator *
9643 cp_parser_new_declarator_opt (cp_parser* parser)
9645 enum tree_code code;
9646 tree type, std_attributes = NULL_TREE;
9647 cp_cv_quals cv_quals;
9649 /* We don't know if there's a ptr-operator next, or not. */
9650 cp_parser_parse_tentatively (parser);
9651 /* Look for a ptr-operator. */
9652 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9653 /* If that worked, look for more new-declarators. */
9654 if (cp_parser_parse_definitely (parser))
9656 cp_declarator *declarator;
9658 /* Parse another optional declarator. */
9659 declarator = cp_parser_new_declarator_opt (parser);
9661 declarator = cp_parser_make_indirect_declarator
9662 (code, type, cv_quals, declarator, std_attributes);
9664 return declarator;
9667 /* If the next token is a `[', there is a direct-new-declarator. */
9668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9669 return cp_parser_direct_new_declarator (parser);
9671 return NULL;
9674 /* Parse a direct-new-declarator.
9676 direct-new-declarator:
9677 [ expression ]
9678 direct-new-declarator [constant-expression]
9682 static cp_declarator *
9683 cp_parser_direct_new_declarator (cp_parser* parser)
9685 cp_declarator *declarator = NULL;
9686 bool first_p = true;
9688 while (true)
9690 tree expression;
9691 cp_token *token;
9693 /* Look for the opening `['. */
9694 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9696 token = cp_lexer_peek_token (parser->lexer);
9697 if (token->type == CPP_CLOSE_SQUARE && first_p)
9698 expression = NULL_TREE;
9699 else
9700 expression = cp_parser_expression (parser);
9701 /* The standard requires that the expression have integral
9702 type. DR 74 adds enumeration types. We believe that the
9703 real intent is that these expressions be handled like the
9704 expression in a `switch' condition, which also allows
9705 classes with a single conversion to integral or
9706 enumeration type. */
9707 if (expression && !processing_template_decl)
9709 expression
9710 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9711 expression,
9712 /*complain=*/true);
9713 if (!expression)
9715 error_at (token->location,
9716 "expression in new-declarator must have integral "
9717 "or enumeration type");
9718 expression = error_mark_node;
9722 /* Look for the closing `]'. */
9723 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9725 /* Add this bound to the declarator. */
9726 declarator = make_array_declarator (declarator, expression);
9728 /* If the next token is not a `[', then there are no more
9729 bounds. */
9730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9731 break;
9732 first_p = false;
9735 return declarator;
9738 /* Parse a new-initializer.
9740 new-initializer:
9741 ( expression-list [opt] )
9742 braced-init-list
9744 Returns a representation of the expression-list. */
9746 static vec<tree, va_gc> *
9747 cp_parser_new_initializer (cp_parser* parser)
9749 vec<tree, va_gc> *expression_list;
9751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9753 cp_lexer_set_source_position (parser->lexer);
9754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9755 tree t = cp_parser_braced_list (parser);
9756 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9757 expression_list = make_tree_vector_single (t);
9759 else
9760 expression_list = (cp_parser_parenthesized_expression_list
9761 (parser, non_attr, /*cast_p=*/false,
9762 /*allow_expansion_p=*/true,
9763 /*non_constant_p=*/NULL));
9765 return expression_list;
9768 /* Parse a delete-expression.
9770 delete-expression:
9771 :: [opt] delete cast-expression
9772 :: [opt] delete [ ] cast-expression
9774 Returns a representation of the expression. */
9776 static tree
9777 cp_parser_delete_expression (cp_parser* parser)
9779 bool global_scope_p;
9780 bool array_p;
9781 tree expression;
9782 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9784 /* Look for the optional `::' operator. */
9785 global_scope_p
9786 = (cp_parser_global_scope_opt (parser,
9787 /*current_scope_valid_p=*/false)
9788 != NULL_TREE);
9789 /* Look for the `delete' keyword. */
9790 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9791 /* See if the array syntax is in use. */
9792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9794 /* Consume the `[' token. */
9795 cp_lexer_consume_token (parser->lexer);
9796 /* Look for the `]' token. */
9797 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9798 /* Remember that this is the `[]' construct. */
9799 array_p = true;
9801 else
9802 array_p = false;
9804 /* Parse the cast-expression. */
9805 expression = cp_parser_simple_cast_expression (parser);
9807 /* A delete-expression may not appear in an integral constant
9808 expression. */
9809 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9810 return error_mark_node;
9812 /* Construct a location e.g.:
9813 delete [ ] ptr
9814 ^~~~~~~~~~~~~~
9815 with caret == start at the start of the "delete" token, and
9816 the end at the end of the final token we consumed. */
9817 location_t combined_loc = make_location (start_loc, start_loc,
9818 parser->lexer);
9819 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9820 global_scope_p, tf_warning_or_error);
9822 return expression;
9825 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9826 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9827 0 otherwise. */
9829 static int
9830 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9832 cp_token *token = cp_lexer_peek_token (parser->lexer);
9833 switch (token->type)
9835 case CPP_COMMA:
9836 case CPP_SEMICOLON:
9837 case CPP_QUERY:
9838 case CPP_COLON:
9839 case CPP_CLOSE_SQUARE:
9840 case CPP_CLOSE_PAREN:
9841 case CPP_CLOSE_BRACE:
9842 case CPP_OPEN_BRACE:
9843 case CPP_DOT:
9844 case CPP_DOT_STAR:
9845 case CPP_DEREF:
9846 case CPP_DEREF_STAR:
9847 case CPP_DIV:
9848 case CPP_MOD:
9849 case CPP_LSHIFT:
9850 case CPP_RSHIFT:
9851 case CPP_LESS:
9852 case CPP_GREATER:
9853 case CPP_LESS_EQ:
9854 case CPP_GREATER_EQ:
9855 case CPP_EQ_EQ:
9856 case CPP_NOT_EQ:
9857 case CPP_EQ:
9858 case CPP_MULT_EQ:
9859 case CPP_DIV_EQ:
9860 case CPP_MOD_EQ:
9861 case CPP_PLUS_EQ:
9862 case CPP_MINUS_EQ:
9863 case CPP_RSHIFT_EQ:
9864 case CPP_LSHIFT_EQ:
9865 case CPP_AND_EQ:
9866 case CPP_XOR_EQ:
9867 case CPP_OR_EQ:
9868 case CPP_XOR:
9869 case CPP_OR:
9870 case CPP_OR_OR:
9871 case CPP_EOF:
9872 case CPP_ELLIPSIS:
9873 return 0;
9875 case CPP_OPEN_PAREN:
9876 /* In ((type ()) () the last () isn't a valid cast-expression,
9877 so the whole must be parsed as postfix-expression. */
9878 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9879 != CPP_CLOSE_PAREN;
9881 case CPP_OPEN_SQUARE:
9882 /* '[' may start a primary-expression in obj-c++ and in C++11,
9883 as a lambda-expression, eg, '(void)[]{}'. */
9884 if (cxx_dialect >= cxx11)
9885 return -1;
9886 return c_dialect_objc ();
9888 case CPP_PLUS_PLUS:
9889 case CPP_MINUS_MINUS:
9890 /* '++' and '--' may or may not start a cast-expression:
9892 struct T { void operator++(int); };
9893 void f() { (T())++; }
9897 int a;
9898 (int)++a; */
9899 return -1;
9901 default:
9902 return 1;
9906 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9907 in the order: const_cast, static_cast, reinterpret_cast.
9909 Don't suggest dynamic_cast.
9911 Return the first legal cast kind found, or NULL otherwise. */
9913 static const char *
9914 get_cast_suggestion (tree dst_type, tree orig_expr)
9916 tree trial;
9918 /* Reuse the parser logic by attempting to build the various kinds of
9919 cast, with "complain" disabled.
9920 Identify the first such cast that is valid. */
9922 /* Don't attempt to run such logic within template processing. */
9923 if (processing_template_decl)
9924 return NULL;
9926 /* First try const_cast. */
9927 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9928 if (trial != error_mark_node)
9929 return "const_cast";
9931 /* If that fails, try static_cast. */
9932 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9933 if (trial != error_mark_node)
9934 return "static_cast";
9936 /* Finally, try reinterpret_cast. */
9937 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9938 tf_none);
9939 if (trial != error_mark_node)
9940 return "reinterpret_cast";
9942 /* No such cast possible. */
9943 return NULL;
9946 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9947 suggesting how to convert a C-style cast of the form:
9949 (DST_TYPE)ORIG_EXPR
9951 to a C++-style cast.
9953 The primary range of RICHLOC is asssumed to be that of the original
9954 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9955 of the parens in the C-style cast. */
9957 static void
9958 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9959 location_t close_paren_loc, tree orig_expr,
9960 tree dst_type)
9962 /* This function is non-trivial, so bail out now if the warning isn't
9963 going to be emitted. */
9964 if (!warn_old_style_cast)
9965 return;
9967 /* Try to find a legal C++ cast, trying them in order:
9968 const_cast, static_cast, reinterpret_cast. */
9969 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9970 if (!cast_suggestion)
9971 return;
9973 /* Replace the open paren with "CAST_SUGGESTION<". */
9974 pretty_printer pp;
9975 pp_string (&pp, cast_suggestion);
9976 pp_less (&pp);
9977 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9979 /* Replace the close paren with "> (". */
9980 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9982 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9983 rich_loc->add_fixit_insert_after (")");
9987 /* Parse a cast-expression.
9989 cast-expression:
9990 unary-expression
9991 ( type-id ) cast-expression
9993 ADDRESS_P is true iff the unary-expression is appearing as the
9994 operand of the `&' operator. CAST_P is true if this expression is
9995 the target of a cast.
9997 Returns a representation of the expression. */
9999 static cp_expr
10000 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10001 bool decltype_p, cp_id_kind * pidk)
10003 /* If it's a `(', then we might be looking at a cast. */
10004 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10006 tree type = NULL_TREE;
10007 cp_expr expr (NULL_TREE);
10008 int cast_expression = 0;
10009 const char *saved_message;
10011 /* There's no way to know yet whether or not this is a cast.
10012 For example, `(int (3))' is a unary-expression, while `(int)
10013 3' is a cast. So, we resort to parsing tentatively. */
10014 cp_parser_parse_tentatively (parser);
10015 /* Types may not be defined in a cast. */
10016 saved_message = parser->type_definition_forbidden_message;
10017 parser->type_definition_forbidden_message
10018 = G_("types may not be defined in casts");
10019 /* Consume the `('. */
10020 matching_parens parens;
10021 cp_token *open_paren = parens.consume_open (parser);
10022 location_t open_paren_loc = open_paren->location;
10023 location_t close_paren_loc = UNKNOWN_LOCATION;
10025 /* A very tricky bit is that `(struct S) { 3 }' is a
10026 compound-literal (which we permit in C++ as an extension).
10027 But, that construct is not a cast-expression -- it is a
10028 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10029 is legal; if the compound-literal were a cast-expression,
10030 you'd need an extra set of parentheses.) But, if we parse
10031 the type-id, and it happens to be a class-specifier, then we
10032 will commit to the parse at that point, because we cannot
10033 undo the action that is done when creating a new class. So,
10034 then we cannot back up and do a postfix-expression.
10036 Another tricky case is the following (c++/29234):
10038 struct S { void operator () (); };
10040 void foo ()
10042 ( S()() );
10045 As a type-id we parse the parenthesized S()() as a function
10046 returning a function, groktypename complains and we cannot
10047 back up in this case either.
10049 Therefore, we scan ahead to the closing `)', and check to see
10050 if the tokens after the `)' can start a cast-expression. Otherwise
10051 we are dealing with an unary-expression, a postfix-expression
10052 or something else.
10054 Yet another tricky case, in C++11, is the following (c++/54891):
10056 (void)[]{};
10058 The issue is that usually, besides the case of lambda-expressions,
10059 the parenthesized type-id cannot be followed by '[', and, eg, we
10060 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10061 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10062 we don't commit, we try a cast-expression, then an unary-expression.
10064 Save tokens so that we can put them back. */
10065 cp_lexer_save_tokens (parser->lexer);
10067 /* We may be looking at a cast-expression. */
10068 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10069 /*consume_paren=*/true))
10070 cast_expression
10071 = cp_parser_tokens_start_cast_expression (parser);
10073 /* Roll back the tokens we skipped. */
10074 cp_lexer_rollback_tokens (parser->lexer);
10075 /* If we aren't looking at a cast-expression, simulate an error so
10076 that the call to cp_parser_error_occurred below returns true. */
10077 if (!cast_expression)
10078 cp_parser_simulate_error (parser);
10079 else
10081 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10082 parser->in_type_id_in_expr_p = true;
10083 /* Look for the type-id. */
10084 type = cp_parser_type_id (parser);
10085 /* Look for the closing `)'. */
10086 cp_token *close_paren = parens.require_close (parser);
10087 if (close_paren)
10088 close_paren_loc = close_paren->location;
10089 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10092 /* Restore the saved message. */
10093 parser->type_definition_forbidden_message = saved_message;
10095 /* At this point this can only be either a cast or a
10096 parenthesized ctor such as `(T ())' that looks like a cast to
10097 function returning T. */
10098 if (!cp_parser_error_occurred (parser))
10100 /* Only commit if the cast-expression doesn't start with
10101 '++', '--', or '[' in C++11. */
10102 if (cast_expression > 0)
10103 cp_parser_commit_to_topmost_tentative_parse (parser);
10105 expr = cp_parser_cast_expression (parser,
10106 /*address_p=*/false,
10107 /*cast_p=*/true,
10108 /*decltype_p=*/false,
10109 pidk);
10111 if (cp_parser_parse_definitely (parser))
10113 /* Warn about old-style casts, if so requested. */
10114 if (warn_old_style_cast
10115 && !in_system_header_at (input_location)
10116 && !VOID_TYPE_P (type)
10117 && current_lang_name != lang_name_c)
10119 gcc_rich_location rich_loc (input_location);
10120 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10121 expr, type);
10122 warning_at (&rich_loc, OPT_Wold_style_cast,
10123 "use of old-style cast to %q#T", type);
10126 /* Only type conversions to integral or enumeration types
10127 can be used in constant-expressions. */
10128 if (!cast_valid_in_integral_constant_expression_p (type)
10129 && cp_parser_non_integral_constant_expression (parser,
10130 NIC_CAST))
10131 return error_mark_node;
10133 /* Perform the cast. */
10134 /* Make a location:
10135 (TYPE) EXPR
10136 ^~~~~~~~~~~
10137 with start==caret at the open paren, extending to the
10138 end of "expr". */
10139 location_t cast_loc = make_location (open_paren_loc,
10140 open_paren_loc,
10141 expr.get_finish ());
10142 expr = build_c_cast (cast_loc, type, expr);
10143 return expr;
10146 else
10147 cp_parser_abort_tentative_parse (parser);
10150 /* If we get here, then it's not a cast, so it must be a
10151 unary-expression. */
10152 return cp_parser_unary_expression (parser, pidk, address_p,
10153 cast_p, decltype_p);
10156 /* Parse a binary expression of the general form:
10158 pm-expression:
10159 cast-expression
10160 pm-expression .* cast-expression
10161 pm-expression ->* cast-expression
10163 multiplicative-expression:
10164 pm-expression
10165 multiplicative-expression * pm-expression
10166 multiplicative-expression / pm-expression
10167 multiplicative-expression % pm-expression
10169 additive-expression:
10170 multiplicative-expression
10171 additive-expression + multiplicative-expression
10172 additive-expression - multiplicative-expression
10174 shift-expression:
10175 additive-expression
10176 shift-expression << additive-expression
10177 shift-expression >> additive-expression
10179 relational-expression:
10180 shift-expression
10181 relational-expression < shift-expression
10182 relational-expression > shift-expression
10183 relational-expression <= shift-expression
10184 relational-expression >= shift-expression
10186 GNU Extension:
10188 relational-expression:
10189 relational-expression <? shift-expression
10190 relational-expression >? shift-expression
10192 equality-expression:
10193 relational-expression
10194 equality-expression == relational-expression
10195 equality-expression != relational-expression
10197 and-expression:
10198 equality-expression
10199 and-expression & equality-expression
10201 exclusive-or-expression:
10202 and-expression
10203 exclusive-or-expression ^ and-expression
10205 inclusive-or-expression:
10206 exclusive-or-expression
10207 inclusive-or-expression | exclusive-or-expression
10209 logical-and-expression:
10210 inclusive-or-expression
10211 logical-and-expression && inclusive-or-expression
10213 logical-or-expression:
10214 logical-and-expression
10215 logical-or-expression || logical-and-expression
10217 All these are implemented with a single function like:
10219 binary-expression:
10220 simple-cast-expression
10221 binary-expression <token> binary-expression
10223 CAST_P is true if this expression is the target of a cast.
10225 The binops_by_token map is used to get the tree codes for each <token> type.
10226 binary-expressions are associated according to a precedence table. */
10228 #define TOKEN_PRECEDENCE(token) \
10229 (((token->type == CPP_GREATER \
10230 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10231 && !parser->greater_than_is_operator_p) \
10232 ? PREC_NOT_OPERATOR \
10233 : binops_by_token[token->type].prec)
10235 static cp_expr
10236 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10237 bool no_toplevel_fold_p,
10238 bool decltype_p,
10239 enum cp_parser_prec prec,
10240 cp_id_kind * pidk)
10242 cp_parser_expression_stack stack;
10243 cp_parser_expression_stack_entry *sp = &stack[0];
10244 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10245 cp_parser_expression_stack_entry current;
10246 cp_expr rhs;
10247 cp_token *token;
10248 enum tree_code rhs_type;
10249 enum cp_parser_prec new_prec, lookahead_prec;
10250 tree overload;
10252 /* Parse the first expression. */
10253 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10254 ? TRUTH_NOT_EXPR : ERROR_MARK);
10255 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10256 cast_p, decltype_p, pidk);
10257 current.prec = prec;
10259 if (cp_parser_error_occurred (parser))
10260 return error_mark_node;
10262 for (;;)
10264 /* Get an operator token. */
10265 token = cp_lexer_peek_token (parser->lexer);
10267 if (warn_cxx11_compat
10268 && token->type == CPP_RSHIFT
10269 && !parser->greater_than_is_operator_p)
10271 if (warning_at (token->location, OPT_Wc__11_compat,
10272 "%<>>%> operator is treated"
10273 " as two right angle brackets in C++11"))
10274 inform (token->location,
10275 "suggest parentheses around %<>>%> expression");
10278 new_prec = TOKEN_PRECEDENCE (token);
10279 if (new_prec != PREC_NOT_OPERATOR
10280 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10281 /* This is a fold-expression; handle it later. */
10282 new_prec = PREC_NOT_OPERATOR;
10284 /* Popping an entry off the stack means we completed a subexpression:
10285 - either we found a token which is not an operator (`>' where it is not
10286 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10287 will happen repeatedly;
10288 - or, we found an operator which has lower priority. This is the case
10289 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10290 parsing `3 * 4'. */
10291 if (new_prec <= current.prec)
10293 if (sp == stack)
10294 break;
10295 else
10296 goto pop;
10299 get_rhs:
10300 current.tree_type = binops_by_token[token->type].tree_type;
10301 current.loc = token->location;
10302 current.flags = token->flags;
10304 /* We used the operator token. */
10305 cp_lexer_consume_token (parser->lexer);
10307 /* For "false && x" or "true || x", x will never be executed;
10308 disable warnings while evaluating it. */
10309 if ((current.tree_type == TRUTH_ANDIF_EXPR
10310 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10311 || (current.tree_type == TRUTH_ORIF_EXPR
10312 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10314 disable_warnings_sp = sp;
10315 ++c_inhibit_evaluation_warnings;
10318 /* Extract another operand. It may be the RHS of this expression
10319 or the LHS of a new, higher priority expression. */
10320 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10321 ? TRUTH_NOT_EXPR : ERROR_MARK);
10322 rhs = cp_parser_simple_cast_expression (parser);
10324 /* Get another operator token. Look up its precedence to avoid
10325 building a useless (immediately popped) stack entry for common
10326 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10327 token = cp_lexer_peek_token (parser->lexer);
10328 lookahead_prec = TOKEN_PRECEDENCE (token);
10329 if (lookahead_prec != PREC_NOT_OPERATOR
10330 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10331 lookahead_prec = PREC_NOT_OPERATOR;
10332 if (lookahead_prec > new_prec)
10334 /* ... and prepare to parse the RHS of the new, higher priority
10335 expression. Since precedence levels on the stack are
10336 monotonically increasing, we do not have to care about
10337 stack overflows. */
10338 *sp = current;
10339 ++sp;
10340 current.lhs = rhs;
10341 current.lhs_type = rhs_type;
10342 current.prec = new_prec;
10343 new_prec = lookahead_prec;
10344 goto get_rhs;
10346 pop:
10347 lookahead_prec = new_prec;
10348 /* If the stack is not empty, we have parsed into LHS the right side
10349 (`4' in the example above) of an expression we had suspended.
10350 We can use the information on the stack to recover the LHS (`3')
10351 from the stack together with the tree code (`MULT_EXPR'), and
10352 the precedence of the higher level subexpression
10353 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10354 which will be used to actually build the additive expression. */
10355 rhs = current.lhs;
10356 rhs_type = current.lhs_type;
10357 --sp;
10358 current = *sp;
10361 /* Undo the disabling of warnings done above. */
10362 if (sp == disable_warnings_sp)
10364 disable_warnings_sp = NULL;
10365 --c_inhibit_evaluation_warnings;
10368 if (warn_logical_not_paren
10369 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10370 && current.lhs_type == TRUTH_NOT_EXPR
10371 /* Avoid warning for !!x == y. */
10372 && (TREE_CODE (current.lhs) != NE_EXPR
10373 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10374 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10375 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10376 /* Avoid warning for !b == y where b is boolean. */
10377 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10378 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10379 != BOOLEAN_TYPE))))
10380 /* Avoid warning for !!b == y where b is boolean. */
10381 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10382 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10383 && DECL_P (tree_strip_any_location_wrapper
10384 (TREE_OPERAND (current.lhs, 0)))))
10385 || TREE_TYPE (current.lhs) == NULL_TREE
10386 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10387 warn_logical_not_parentheses (current.loc, current.tree_type,
10388 current.lhs, maybe_constant_value (rhs));
10390 if (warn_xor_used_as_pow
10391 && current.tree_type == BIT_XOR_EXPR
10392 /* Don't warn for named "xor" (as opposed to '^'). */
10393 && !(current.flags & NAMED_OP)
10394 && current.lhs.decimal_p ()
10395 && rhs.decimal_p ())
10396 check_for_xor_used_as_pow
10397 (current.lhs.get_location (),
10398 tree_strip_any_location_wrapper (current.lhs),
10399 current.loc,
10400 rhs.get_location (),
10401 tree_strip_any_location_wrapper (rhs));
10403 overload = NULL;
10405 location_t combined_loc = make_location (current.loc,
10406 current.lhs.get_start (),
10407 rhs.get_finish ());
10409 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10410 ERROR_MARK for everything that is not a binary expression.
10411 This makes warn_about_parentheses miss some warnings that
10412 involve unary operators. For unary expressions we should
10413 pass the correct tree_code unless the unary expression was
10414 surrounded by parentheses.
10416 if (no_toplevel_fold_p
10417 && lookahead_prec <= current.prec
10418 && sp == stack)
10420 if (current.lhs == error_mark_node || rhs == error_mark_node)
10421 current.lhs = error_mark_node;
10422 else
10424 current.lhs.maybe_add_location_wrapper ();
10425 rhs.maybe_add_location_wrapper ();
10426 current.lhs
10427 = build_min (current.tree_type,
10428 TREE_CODE_CLASS (current.tree_type)
10429 == tcc_comparison
10430 ? boolean_type_node : TREE_TYPE (current.lhs),
10431 current.lhs.get_value (), rhs.get_value ());
10432 SET_EXPR_LOCATION (current.lhs, combined_loc);
10435 else
10437 op_location_t op_loc (current.loc, combined_loc);
10438 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10439 current.lhs, current.lhs_type,
10440 rhs, rhs_type, NULL_TREE, &overload,
10441 complain_flags (decltype_p));
10442 /* TODO: build_x_binary_op doesn't always honor the location. */
10443 current.lhs.set_location (combined_loc);
10445 current.lhs_type = current.tree_type;
10447 /* If the binary operator required the use of an overloaded operator,
10448 then this expression cannot be an integral constant-expression.
10449 An overloaded operator can be used even if both operands are
10450 otherwise permissible in an integral constant-expression if at
10451 least one of the operands is of enumeration type. */
10453 if (overload
10454 && cp_parser_non_integral_constant_expression (parser,
10455 NIC_OVERLOADED))
10456 return error_mark_node;
10459 return current.lhs;
10462 static cp_expr
10463 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10464 bool no_toplevel_fold_p,
10465 enum cp_parser_prec prec,
10466 cp_id_kind * pidk)
10468 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10469 /*decltype*/false, prec, pidk);
10472 /* Parse the `? expression : assignment-expression' part of a
10473 conditional-expression. The LOGICAL_OR_EXPR is the
10474 logical-or-expression that started the conditional-expression.
10475 Returns a representation of the entire conditional-expression.
10477 This routine is used by cp_parser_assignment_expression
10478 and cp_parser_conditional_expression.
10480 ? expression : assignment-expression
10482 GNU Extensions:
10484 ? : assignment-expression */
10486 static tree
10487 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10489 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10490 cp_expr assignment_expr;
10491 struct cp_token *token;
10492 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10494 /* Consume the `?' token. */
10495 cp_lexer_consume_token (parser->lexer);
10496 token = cp_lexer_peek_token (parser->lexer);
10497 if (cp_parser_allow_gnu_extensions_p (parser)
10498 && token->type == CPP_COLON)
10500 pedwarn (token->location, OPT_Wpedantic,
10501 "ISO C++ does not allow %<?:%> with omitted middle operand");
10502 /* Implicit true clause. */
10503 expr = NULL_TREE;
10504 c_inhibit_evaluation_warnings +=
10505 folded_logical_or_expr == truthvalue_true_node;
10506 warn_for_omitted_condop (token->location, logical_or_expr);
10508 else
10510 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10511 parser->colon_corrects_to_scope_p = false;
10512 /* Parse the expression. */
10513 c_inhibit_evaluation_warnings +=
10514 folded_logical_or_expr == truthvalue_false_node;
10515 expr = cp_parser_expression (parser);
10516 c_inhibit_evaluation_warnings +=
10517 ((folded_logical_or_expr == truthvalue_true_node)
10518 - (folded_logical_or_expr == truthvalue_false_node));
10519 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10522 /* The next token should be a `:'. */
10523 cp_parser_require (parser, CPP_COLON, RT_COLON);
10524 /* Parse the assignment-expression. */
10525 assignment_expr = cp_parser_assignment_expression (parser);
10526 c_inhibit_evaluation_warnings -=
10527 folded_logical_or_expr == truthvalue_true_node;
10529 /* Make a location:
10530 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10531 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10532 with the caret at the "?", ranging from the start of
10533 the logical_or_expr to the end of the assignment_expr. */
10534 loc = make_location (loc,
10535 logical_or_expr.get_start (),
10536 assignment_expr.get_finish ());
10538 /* Build the conditional-expression. */
10539 return build_x_conditional_expr (loc, logical_or_expr,
10540 expr,
10541 assignment_expr,
10542 tf_warning_or_error);
10545 /* Parse a conditional-expression.
10547 conditional-expression:
10548 logical-or-expression
10549 logical-or-expression ? expression : assignment-expression
10551 GNU Extensions:
10553 logical-or-expression ? : assignment-expression */
10555 static cp_expr
10556 cp_parser_conditional_expression (cp_parser *parser)
10558 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10559 PREC_NOT_OPERATOR, NULL);
10560 /* If the next token is a `?' then we're actually looking at
10561 a conditional-expression; otherwise we're done. */
10562 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10563 return cp_parser_question_colon_clause (parser, expr);
10564 return expr;
10567 /* Parse an assignment-expression.
10569 assignment-expression:
10570 conditional-expression
10571 logical-or-expression assignment-operator assignment_expression
10572 throw-expression
10573 yield-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 operand of decltype.
10578 Returns a representation for the expression. */
10580 static cp_expr
10581 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10582 bool cast_p, bool decltype_p)
10584 cp_expr expr;
10586 /* If the next token is the `throw' keyword, then we're looking at
10587 a throw-expression. */
10588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10589 expr = cp_parser_throw_expression (parser);
10590 /* If the next token is the `co_yield' keyword, then we're looking at
10591 a yield-expression. */
10592 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10593 expr = cp_parser_yield_expression (parser);
10594 /* Otherwise, it must be that we are looking at a
10595 logical-or-expression. */
10596 else
10598 /* Parse the binary expressions (logical-or-expression). */
10599 expr = cp_parser_binary_expression (parser, cast_p, false,
10600 decltype_p,
10601 PREC_NOT_OPERATOR, pidk);
10602 /* If the next token is a `?' then we're actually looking at a
10603 conditional-expression. */
10604 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10605 return cp_parser_question_colon_clause (parser, expr);
10606 else
10608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10610 /* If it's an assignment-operator, we're using the second
10611 production. */
10612 enum tree_code assignment_operator
10613 = cp_parser_assignment_operator_opt (parser);
10614 if (assignment_operator != ERROR_MARK)
10616 /* Parse the right-hand side of the assignment. */
10617 cp_expr rhs = cp_parser_initializer_clause (parser);
10619 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10620 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10622 /* An assignment may not appear in a
10623 constant-expression. */
10624 if (cp_parser_non_integral_constant_expression (parser,
10625 NIC_ASSIGNMENT))
10626 return error_mark_node;
10627 /* Build the assignment expression. Its default
10628 location:
10629 LHS = RHS
10630 ~~~~^~~~~
10631 is the location of the '=' token as the
10632 caret, ranging from the start of the lhs to the
10633 end of the rhs. */
10634 loc = make_location (loc,
10635 expr.get_start (),
10636 rhs.get_finish ());
10637 expr = build_x_modify_expr (loc, expr,
10638 assignment_operator,
10639 rhs, NULL_TREE,
10640 complain_flags (decltype_p));
10641 /* TODO: build_x_modify_expr doesn't honor the location,
10642 so we must set it here. */
10643 expr.set_location (loc);
10648 return expr;
10651 /* Parse an (optional) assignment-operator.
10653 assignment-operator: one of
10654 = *= /= %= += -= >>= <<= &= ^= |=
10656 GNU Extension:
10658 assignment-operator: one of
10659 <?= >?=
10661 If the next token is an assignment operator, the corresponding tree
10662 code is returned, and the token is consumed. For example, for
10663 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10664 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10665 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10666 operator, ERROR_MARK is returned. */
10668 static enum tree_code
10669 cp_parser_assignment_operator_opt (cp_parser* parser)
10671 enum tree_code op;
10672 cp_token *token;
10674 /* Peek at the next token. */
10675 token = cp_lexer_peek_token (parser->lexer);
10677 switch (token->type)
10679 case CPP_EQ:
10680 op = NOP_EXPR;
10681 break;
10683 case CPP_MULT_EQ:
10684 op = MULT_EXPR;
10685 break;
10687 case CPP_DIV_EQ:
10688 op = TRUNC_DIV_EXPR;
10689 break;
10691 case CPP_MOD_EQ:
10692 op = TRUNC_MOD_EXPR;
10693 break;
10695 case CPP_PLUS_EQ:
10696 op = PLUS_EXPR;
10697 break;
10699 case CPP_MINUS_EQ:
10700 op = MINUS_EXPR;
10701 break;
10703 case CPP_RSHIFT_EQ:
10704 op = RSHIFT_EXPR;
10705 break;
10707 case CPP_LSHIFT_EQ:
10708 op = LSHIFT_EXPR;
10709 break;
10711 case CPP_AND_EQ:
10712 op = BIT_AND_EXPR;
10713 break;
10715 case CPP_XOR_EQ:
10716 op = BIT_XOR_EXPR;
10717 break;
10719 case CPP_OR_EQ:
10720 op = BIT_IOR_EXPR;
10721 break;
10723 default:
10724 /* Nothing else is an assignment operator. */
10725 op = ERROR_MARK;
10728 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10729 if (op != ERROR_MARK
10730 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10731 op = ERROR_MARK;
10733 /* If it was an assignment operator, consume it. */
10734 if (op != ERROR_MARK)
10735 cp_lexer_consume_token (parser->lexer);
10737 return op;
10740 /* Parse an expression.
10742 expression:
10743 assignment-expression
10744 expression , assignment-expression
10746 CAST_P is true if this expression is the target of a cast.
10747 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10748 except possibly parenthesized or on the RHS of a comma (N3276).
10749 WARN_COMMA_P is true if a comma should be diagnosed.
10751 Returns a representation of the expression. */
10753 static cp_expr
10754 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10755 bool cast_p, bool decltype_p, bool warn_comma_p)
10757 cp_expr expression = NULL_TREE;
10758 location_t loc = UNKNOWN_LOCATION;
10760 while (true)
10762 cp_expr assignment_expression;
10764 /* Parse the next assignment-expression. */
10765 assignment_expression
10766 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10768 /* We don't create a temporary for a call that is the immediate operand
10769 of decltype or on the RHS of a comma. But when we see a comma, we
10770 need to create a temporary for a call on the LHS. */
10771 if (decltype_p && !processing_template_decl
10772 && TREE_CODE (assignment_expression) == CALL_EXPR
10773 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10774 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10775 assignment_expression
10776 = build_cplus_new (TREE_TYPE (assignment_expression),
10777 assignment_expression, tf_warning_or_error);
10779 /* If this is the first assignment-expression, we can just
10780 save it away. */
10781 if (!expression)
10782 expression = assignment_expression;
10783 else
10785 /* Create a location with caret at the comma, ranging
10786 from the start of the LHS to the end of the RHS. */
10787 loc = make_location (loc,
10788 expression.get_start (),
10789 assignment_expression.get_finish ());
10790 expression = build_x_compound_expr (loc, expression,
10791 assignment_expression, NULL_TREE,
10792 complain_flags (decltype_p));
10793 expression.set_location (loc);
10795 /* If the next token is not a comma, or we're in a fold-expression, then
10796 we are done with the expression. */
10797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10798 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10799 break;
10800 /* Consume the `,'. */
10801 loc = cp_lexer_peek_token (parser->lexer)->location;
10802 if (warn_comma_p)
10804 /* [depr.comma.subscript]: A comma expression appearing as
10805 the expr-or-braced-init-list of a subscripting expression
10806 is deprecated. A parenthesized comma expression is not
10807 deprecated. */
10808 warning_at (loc, OPT_Wcomma_subscript,
10809 "top-level comma expression in array subscript "
10810 "is deprecated");
10811 warn_comma_p = false;
10813 cp_lexer_consume_token (parser->lexer);
10814 /* A comma operator cannot appear in a constant-expression. */
10815 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10816 expression = error_mark_node;
10819 return expression;
10822 /* Parse a constant-expression.
10824 constant-expression:
10825 conditional-expression
10827 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10828 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10829 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10830 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10831 greater than 1, this isn't really a constant-expression, only a
10832 potentially constant-evaluated expression. If STRICT_P is true,
10833 only parse a conditional-expression, otherwise parse an
10834 assignment-expression. See below for rationale. */
10836 static cp_expr
10837 cp_parser_constant_expression (cp_parser* parser,
10838 int allow_non_constant_p /* = 0 */,
10839 bool *non_constant_p /* = NULL */,
10840 bool strict_p /* = false */)
10842 /* It might seem that we could simply parse the
10843 conditional-expression, and then check to see if it were
10844 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10845 one that the compiler can figure out is constant, possibly after
10846 doing some simplifications or optimizations. The standard has a
10847 precise definition of constant-expression, and we must honor
10848 that, even though it is somewhat more restrictive.
10850 For example:
10852 int i[(2, 3)];
10854 is not a legal declaration, because `(2, 3)' is not a
10855 constant-expression. The `,' operator is forbidden in a
10856 constant-expression. However, GCC's constant-folding machinery
10857 will fold this operation to an INTEGER_CST for `3'. */
10859 /* Save the old settings. */
10860 bool saved_integral_constant_expression_p
10861 = parser->integral_constant_expression_p;
10862 bool saved_allow_non_integral_constant_expression_p
10863 = parser->allow_non_integral_constant_expression_p;
10864 bool saved_non_integral_constant_expression_p
10865 = parser->non_integral_constant_expression_p;
10866 /* We are now parsing a constant-expression. */
10867 parser->integral_constant_expression_p = true;
10868 parser->allow_non_integral_constant_expression_p
10869 = (allow_non_constant_p || cxx_dialect >= cxx11);
10870 parser->non_integral_constant_expression_p = false;
10872 /* A manifestly constant-evaluated expression is evaluated even in an
10873 unevaluated operand. */
10874 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10876 /* Although the grammar says "conditional-expression", when not STRICT_P,
10877 we parse an "assignment-expression", which also permits
10878 "throw-expression" and the use of assignment operators. In the case
10879 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10880 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10881 actually essential that we look for an assignment-expression.
10882 For example, cp_parser_initializer_clauses uses this function to
10883 determine whether a particular assignment-expression is in fact
10884 constant. */
10885 cp_expr expression;
10886 if (strict_p)
10887 expression = cp_parser_conditional_expression (parser);
10888 else
10889 expression = cp_parser_assignment_expression (parser);
10890 /* Restore the old settings. */
10891 parser->integral_constant_expression_p
10892 = saved_integral_constant_expression_p;
10893 parser->allow_non_integral_constant_expression_p
10894 = saved_allow_non_integral_constant_expression_p;
10895 if (cxx_dialect >= cxx11
10896 && (!allow_non_constant_p || non_constant_p))
10898 /* Require an rvalue constant expression here; that's what our
10899 callers expect. Reference constant expressions are handled
10900 separately in e.g. cp_parser_template_argument. */
10901 tree decay = expression;
10902 if (TREE_TYPE (expression)
10903 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10904 decay = build_address (expression);
10905 bool is_const = is_rvalue_constant_expression (decay);
10906 parser->non_integral_constant_expression_p = !is_const;
10907 if (!is_const && !allow_non_constant_p)
10908 require_rvalue_constant_expression (decay);
10910 if (allow_non_constant_p && non_constant_p)
10911 *non_constant_p = parser->non_integral_constant_expression_p;
10912 parser->non_integral_constant_expression_p
10913 = saved_non_integral_constant_expression_p;
10915 return expression;
10918 /* Parse __builtin_offsetof.
10920 offsetof-expression:
10921 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10923 offsetof-member-designator:
10924 id-expression
10925 | offsetof-member-designator "." id-expression
10926 | offsetof-member-designator "[" expression "]"
10927 | offsetof-member-designator "->" id-expression */
10929 static cp_expr
10930 cp_parser_builtin_offsetof (cp_parser *parser)
10932 int save_ice_p, save_non_ice_p;
10933 tree type;
10934 cp_expr expr;
10935 cp_id_kind dummy;
10936 cp_token *token;
10937 location_t finish_loc;
10939 /* We're about to accept non-integral-constant things, but will
10940 definitely yield an integral constant expression. Save and
10941 restore these values around our local parsing. */
10942 save_ice_p = parser->integral_constant_expression_p;
10943 save_non_ice_p = parser->non_integral_constant_expression_p;
10945 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10947 /* Consume the "__builtin_offsetof" token. */
10948 cp_lexer_consume_token (parser->lexer);
10949 /* Consume the opening `('. */
10950 matching_parens parens;
10951 parens.require_open (parser);
10952 /* Parse the type-id. */
10953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10955 const char *saved_message = parser->type_definition_forbidden_message;
10956 parser->type_definition_forbidden_message
10957 = G_("types may not be defined within %<__builtin_offsetof%>");
10958 type = cp_parser_type_id (parser);
10959 parser->type_definition_forbidden_message = saved_message;
10961 /* Look for the `,'. */
10962 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10963 token = cp_lexer_peek_token (parser->lexer);
10965 /* Build the (type *)null that begins the traditional offsetof macro. */
10966 tree object_ptr
10967 = build_static_cast (input_location, build_pointer_type (type),
10968 null_pointer_node, tf_warning_or_error);
10970 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10971 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10972 true, &dummy, token->location);
10973 while (true)
10975 token = cp_lexer_peek_token (parser->lexer);
10976 switch (token->type)
10978 case CPP_OPEN_SQUARE:
10979 /* offsetof-member-designator "[" expression "]" */
10980 expr = cp_parser_postfix_open_square_expression (parser, expr,
10981 true, false);
10982 break;
10984 case CPP_DEREF:
10985 /* offsetof-member-designator "->" identifier */
10986 expr = grok_array_decl (token->location, expr, integer_zero_node,
10987 NULL, tf_warning_or_error);
10988 /* FALLTHRU */
10990 case CPP_DOT:
10991 /* offsetof-member-designator "." identifier */
10992 cp_lexer_consume_token (parser->lexer);
10993 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10994 expr, true, &dummy,
10995 token->location);
10996 break;
10998 case CPP_CLOSE_PAREN:
10999 /* Consume the ")" token. */
11000 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11001 cp_lexer_consume_token (parser->lexer);
11002 goto success;
11004 default:
11005 /* Error. We know the following require will fail, but
11006 that gives the proper error message. */
11007 parens.require_close (parser);
11008 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11009 expr = error_mark_node;
11010 goto failure;
11014 success:
11015 /* Make a location of the form:
11016 __builtin_offsetof (struct s, f)
11017 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11018 with caret at the type-id, ranging from the start of the
11019 "_builtin_offsetof" token to the close paren. */
11020 loc = make_location (loc, start_loc, finish_loc);
11021 /* The result will be an INTEGER_CST, so we need to explicitly
11022 preserve the location. */
11023 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11025 failure:
11026 parser->integral_constant_expression_p = save_ice_p;
11027 parser->non_integral_constant_expression_p = save_non_ice_p;
11029 expr = expr.maybe_add_location_wrapper ();
11030 return expr;
11033 /* Parse a builtin trait expression or type. */
11035 static cp_expr
11036 cp_parser_trait (cp_parser* parser, enum rid keyword)
11038 cp_trait_kind kind;
11039 tree type1, type2 = NULL_TREE;
11040 bool binary = false;
11041 bool variadic = false;
11042 bool type = false;
11044 switch (keyword)
11046 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
11047 case RID_##CODE: \
11048 kind = CPTK_##CODE; \
11049 binary = (ARITY == 2); \
11050 variadic = (ARITY == -1); \
11051 type = (TCC == tcc_type); \
11052 break;
11053 #include "cp-trait.def"
11054 #undef DEFTRAIT
11055 default:
11056 gcc_unreachable ();
11059 /* Get location of initial token. */
11060 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11062 /* Consume the token. */
11063 cp_lexer_consume_token (parser->lexer);
11065 matching_parens parens;
11066 if (kind == CPTK_TYPE_PACK_ELEMENT)
11067 cp_parser_require (parser, CPP_LESS, RT_LESS);
11068 else
11069 parens.require_open (parser);
11071 if (kind == CPTK_IS_DEDUCIBLE)
11073 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11074 type1 = cp_parser_id_expression (parser,
11075 /*template_keyword_p=*/false,
11076 /*check_dependency_p=*/true,
11077 nullptr,
11078 /*declarator_p=*/false,
11079 /*optional_p=*/false);
11080 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11082 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11083 /* __type_pack_element takes an expression as its first argument and uses
11084 template-id syntax instead of function call syntax (for consistency
11085 with Clang). We special case these properties of __type_pack_element
11086 here and elsewhere. */
11087 type1 = cp_parser_constant_expression (parser);
11088 else
11090 type_id_in_expr_sentinel s (parser);
11091 type1 = cp_parser_type_id (parser);
11094 if (type1 == error_mark_node)
11095 return error_mark_node;
11097 if (kind == CPTK_TYPE_PACK_ELEMENT)
11099 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11100 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11101 for (tree elt : tree_vec_range (trailing))
11103 if (!TYPE_P (elt))
11105 error_at (cp_expr_loc_or_input_loc (elt),
11106 "trailing argument to %<__type_pack_element%> "
11107 "is not a type");
11108 return error_mark_node;
11111 type2 = trailing;
11113 else if (binary)
11115 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11118 type_id_in_expr_sentinel s (parser);
11119 type2 = cp_parser_type_id (parser);
11122 if (type2 == error_mark_node)
11123 return error_mark_node;
11125 else if (variadic)
11127 auto_vec<tree, 4> trailing;
11128 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11130 cp_lexer_consume_token (parser->lexer);
11131 tree elt = cp_parser_type_id (parser);
11132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11134 cp_lexer_consume_token (parser->lexer);
11135 elt = make_pack_expansion (elt);
11137 if (elt == error_mark_node)
11138 return error_mark_node;
11139 trailing.safe_push (elt);
11141 type2 = make_tree_vec (trailing.length ());
11142 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11143 TREE_VEC_ELT (type2, i) = trailing[i];
11146 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11147 if (kind == CPTK_TYPE_PACK_ELEMENT)
11148 /* cp_parser_enclosed_template_argument_list above already took care
11149 of parsing the closing '>'. */;
11150 else
11151 parens.require_close (parser);
11153 /* Construct a location of the form:
11154 __is_trivially_copyable(_Tp)
11155 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11156 with start == caret, finishing at the close-paren. */
11157 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11159 /* Complete the trait expression, which may mean either processing
11160 the trait expr now or saving it for template instantiation. */
11161 switch (kind)
11163 case CPTK_BASES:
11164 return cp_expr (finish_bases (type1, false), trait_loc);
11165 case CPTK_DIRECT_BASES:
11166 return cp_expr (finish_bases (type1, true), trait_loc);
11167 default:
11168 if (type)
11169 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11170 else
11171 return finish_trait_expr (trait_loc, kind, type1, type2);
11175 /* Parse a lambda expression.
11177 lambda-expression:
11178 lambda-introducer lambda-declarator [opt] compound-statement
11179 lambda-introducer < template-parameter-list > requires-clause [opt]
11180 lambda-declarator [opt] compound-statement
11182 Returns a representation of the expression. */
11184 static cp_expr
11185 cp_parser_lambda_expression (cp_parser* parser)
11187 tree lambda_expr = build_lambda_expr ();
11188 tree type;
11189 bool ok = true;
11190 cp_token *token = cp_lexer_peek_token (parser->lexer);
11191 cp_token_position start = 0;
11193 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11195 if (cxx_dialect >= cxx20)
11197 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11198 non-type parameter is nonsensical.
11200 Distinguish a lambda in the parameter type from a lambda in the
11201 default argument by looking at local_variables_forbidden_p, which is
11202 only set in default arguments. */
11203 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11205 error_at (token->location,
11206 "lambda-expression in template parameter type");
11207 token->error_reported = true;
11208 ok = false;
11211 else if (cp_unevaluated_operand)
11213 if (!token->error_reported)
11215 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11216 "lambda-expression in unevaluated context"
11217 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11218 token->error_reported = true;
11220 ok = false;
11222 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11224 if (!token->error_reported)
11226 error_at (token->location, "lambda-expression in template-argument"
11227 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11228 token->error_reported = true;
11230 ok = false;
11233 /* We may be in the middle of deferred access check. Disable
11234 it now. */
11235 push_deferring_access_checks (dk_no_deferred);
11237 cp_parser_lambda_introducer (parser, lambda_expr);
11238 if (cp_parser_error_occurred (parser))
11239 return error_mark_node;
11241 type = begin_lambda_type (lambda_expr);
11242 if (type == error_mark_node)
11243 return error_mark_node;
11245 record_lambda_scope (lambda_expr);
11246 record_lambda_scope_discriminator (lambda_expr);
11248 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11249 determine_visibility (TYPE_NAME (type));
11251 /* Now that we've started the type, add the capture fields for any
11252 explicit captures. */
11253 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11256 /* Inside the class, surrounding template-parameter-lists do not apply. */
11257 unsigned int saved_num_template_parameter_lists
11258 = parser->num_template_parameter_lists;
11259 unsigned char in_statement = parser->in_statement;
11260 bool in_switch_statement_p = parser->in_switch_statement_p;
11261 bool fully_implicit_function_template_p
11262 = parser->fully_implicit_function_template_p;
11263 tree implicit_template_parms = parser->implicit_template_parms;
11264 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11265 bool auto_is_implicit_function_template_parm_p
11266 = parser->auto_is_implicit_function_template_parm_p;
11268 parser->num_template_parameter_lists = 0;
11269 parser->in_statement = 0;
11270 parser->in_switch_statement_p = false;
11271 parser->fully_implicit_function_template_p = false;
11272 parser->implicit_template_parms = 0;
11273 parser->implicit_template_scope = 0;
11274 parser->auto_is_implicit_function_template_parm_p = false;
11276 /* The body of a lambda in a discarded statement is not discarded. */
11277 bool discarded = in_discarded_stmt;
11278 in_discarded_stmt = 0;
11280 /* Similarly the body of a lambda in immediate function context is not
11281 in immediate function context. */
11282 bool save_in_consteval_if_p = in_consteval_if_p;
11283 in_consteval_if_p = false;
11285 /* By virtue of defining a local class, a lambda expression has access to
11286 the private variables of enclosing classes. */
11288 if (cp_parser_start_tentative_firewall (parser))
11289 start = token;
11291 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11293 if (ok && cp_parser_error_occurred (parser))
11294 ok = false;
11296 if (ok)
11297 cp_parser_lambda_body (parser, lambda_expr);
11298 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11300 if (cp_parser_skip_to_closing_brace (parser))
11301 cp_lexer_consume_token (parser->lexer);
11304 /* The capture list was built up in reverse order; fix that now. */
11305 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11306 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11308 if (ok)
11309 maybe_add_lambda_conv_op (type);
11311 finish_struct (type, /*attributes=*/NULL_TREE);
11313 in_consteval_if_p = save_in_consteval_if_p;
11314 in_discarded_stmt = discarded;
11316 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11317 parser->in_statement = in_statement;
11318 parser->in_switch_statement_p = in_switch_statement_p;
11319 parser->fully_implicit_function_template_p
11320 = fully_implicit_function_template_p;
11321 parser->implicit_template_parms = implicit_template_parms;
11322 parser->implicit_template_scope = implicit_template_scope;
11323 parser->auto_is_implicit_function_template_parm_p
11324 = auto_is_implicit_function_template_parm_p;
11327 /* This field is only used during parsing of the lambda. */
11328 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11330 /* This lambda shouldn't have any proxies left at this point. */
11331 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11332 /* And now that we're done, push proxies for an enclosing lambda. */
11333 insert_pending_capture_proxies ();
11335 /* Update the lambda expression to a range. */
11336 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11337 token->location,
11338 parser->lexer);
11340 if (ok)
11341 lambda_expr = build_lambda_object (lambda_expr);
11342 else
11343 lambda_expr = error_mark_node;
11345 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11347 pop_deferring_access_checks ();
11349 return lambda_expr;
11352 /* Parse the beginning of a lambda expression.
11354 lambda-introducer:
11355 [ lambda-capture [opt] ]
11357 LAMBDA_EXPR is the current representation of the lambda expression. */
11359 static void
11360 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11362 /* Need commas after the first capture. */
11363 bool first = true;
11365 /* Eat the leading `['. */
11366 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11368 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11369 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11370 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11371 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11372 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11373 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11374 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11375 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11377 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11379 cp_lexer_consume_token (parser->lexer);
11380 first = false;
11382 if (!(at_function_scope_p () || parsing_nsdmi ()))
11383 error ("non-local lambda expression cannot have a capture-default");
11386 hash_set<tree, true> ids;
11387 tree first_capture_id = NULL_TREE;
11388 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11390 cp_token* capture_token;
11391 tree capture_id;
11392 tree capture_init_expr;
11393 cp_id_kind idk = CP_ID_KIND_NONE;
11394 bool explicit_init_p = false;
11396 enum capture_kind_type
11398 BY_COPY,
11399 BY_REFERENCE
11401 enum capture_kind_type capture_kind = BY_COPY;
11403 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11405 error ("expected end of capture-list");
11406 return;
11409 if (first)
11410 first = false;
11411 else
11412 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11414 /* Possibly capture `this'. */
11415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11417 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11418 if (cxx_dialect < cxx20 && pedantic
11419 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11420 pedwarn (loc, OPT_Wc__20_extensions,
11421 "explicit by-copy capture of %<this%> "
11422 "with by-copy capture default only available with "
11423 "%<-std=c++20%> or %<-std=gnu++20%>");
11424 cp_lexer_consume_token (parser->lexer);
11425 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11426 pedwarn (input_location, 0,
11427 "already captured %qD in lambda expression",
11428 this_identifier);
11429 else
11430 add_capture (lambda_expr, /*id=*/this_identifier,
11431 /*initializer=*/finish_this_expr (),
11432 /*by_reference_p=*/true, explicit_init_p);
11433 continue;
11436 /* Possibly capture `*this'. */
11437 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11438 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11441 if (cxx_dialect < cxx17)
11442 pedwarn (loc, OPT_Wc__17_extensions,
11443 "%<*this%> capture only available with "
11444 "%<-std=c++17%> or %<-std=gnu++17%>");
11445 cp_lexer_consume_token (parser->lexer);
11446 cp_lexer_consume_token (parser->lexer);
11447 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11448 pedwarn (input_location, 0,
11449 "already captured %qD in lambda expression",
11450 this_identifier);
11451 else
11452 add_capture (lambda_expr, /*id=*/this_identifier,
11453 /*initializer=*/finish_this_expr (),
11454 /*by_reference_p=*/false, explicit_init_p);
11455 continue;
11458 /* But reject `&this'. */
11459 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11460 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11462 error_at (cp_lexer_peek_token (parser->lexer)->location,
11463 "%<this%> cannot be captured by reference");
11464 cp_lexer_consume_token (parser->lexer);
11465 cp_lexer_consume_token (parser->lexer);
11466 continue;
11469 /* Remember whether we want to capture as a reference or not. */
11470 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11472 capture_kind = BY_REFERENCE;
11473 cp_lexer_consume_token (parser->lexer);
11476 bool init_pack_expansion = false;
11477 location_t ellipsis_loc = UNKNOWN_LOCATION;
11478 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11480 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11481 if (cxx_dialect < cxx20)
11482 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11483 "pack init-capture only available with "
11484 "%<-std=c++20%> or %<-std=gnu++20%>");
11485 cp_lexer_consume_token (parser->lexer);
11486 init_pack_expansion = true;
11489 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11490 if (init_pack_expansion && capture_kind != BY_REFERENCE
11491 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11493 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11494 0, "%<&%> should come before %<...%>");
11495 capture_kind = BY_REFERENCE;
11496 cp_lexer_consume_token (parser->lexer);
11499 /* Get the identifier. */
11500 capture_token = cp_lexer_peek_token (parser->lexer);
11501 capture_id = cp_parser_identifier (parser);
11503 if (capture_id == error_mark_node)
11504 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11505 delimiters, but I modified this to stop on unnested ']' as well. It
11506 was already changed to stop on unnested '}', so the
11507 "closing_parenthesis" name is no more misleading with my change. */
11509 cp_parser_skip_to_closing_parenthesis (parser,
11510 /*recovering=*/true,
11511 /*or_comma=*/true,
11512 /*consume_paren=*/true);
11513 break;
11516 /* Find the initializer for this capture. */
11517 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11518 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11519 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11521 /* An explicit initializer exists. */
11522 if (cxx_dialect < cxx14)
11523 pedwarn (input_location, OPT_Wc__14_extensions,
11524 "lambda capture initializers "
11525 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11526 capture_init_expr = cp_parser_initializer (parser,
11527 /*direct_init=*/nullptr,
11528 /*non_constant=*/nullptr,
11529 /*subexpression_p=*/true);
11530 explicit_init_p = true;
11531 if (capture_init_expr == NULL_TREE)
11533 error ("empty initializer for lambda init-capture");
11534 capture_init_expr = error_mark_node;
11536 if (init_pack_expansion)
11537 capture_init_expr = make_pack_expansion (capture_init_expr);
11539 else
11541 const char* error_msg;
11543 /* Turn the identifier into an id-expression. */
11544 capture_init_expr
11545 = cp_parser_lookup_name_simple (parser, capture_id,
11546 capture_token->location);
11548 if (capture_init_expr == error_mark_node)
11550 unqualified_name_lookup_error (capture_id);
11551 continue;
11553 else if (!VAR_P (capture_init_expr)
11554 && TREE_CODE (capture_init_expr) != PARM_DECL)
11556 error_at (capture_token->location,
11557 "capture of non-variable %qE",
11558 capture_init_expr);
11559 if (DECL_P (capture_init_expr))
11560 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11561 "%q#D declared here", capture_init_expr);
11562 continue;
11564 if (VAR_P (capture_init_expr)
11565 && decl_storage_duration (capture_init_expr) != dk_auto)
11567 if (pedwarn (capture_token->location, 0, "capture of variable "
11568 "%qD with non-automatic storage duration",
11569 capture_init_expr))
11570 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11571 "%q#D declared here", capture_init_expr);
11572 continue;
11575 capture_init_expr
11576 = finish_id_expression
11577 (capture_id,
11578 capture_init_expr,
11579 parser->scope,
11580 &idk,
11581 /*integral_constant_expression_p=*/false,
11582 /*allow_non_integral_constant_expression_p=*/false,
11583 /*non_integral_constant_expression_p=*/NULL,
11584 /*template_p=*/false,
11585 /*done=*/true,
11586 /*address_p=*/false,
11587 /*template_arg_p=*/false,
11588 &error_msg,
11589 capture_token->location);
11591 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11593 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11594 cp_lexer_consume_token (parser->lexer);
11595 capture_init_expr = make_pack_expansion (capture_init_expr);
11596 if (init_pack_expansion)
11598 /* If what follows is an initializer, the second '...' is
11599 invalid. But for cases like [...xs...], the first one
11600 is invalid. */
11601 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11602 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11603 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11604 ellipsis_loc = loc;
11605 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11606 continue;
11611 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11612 && !explicit_init_p)
11614 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11615 && capture_kind == BY_COPY)
11616 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11617 "of %qD redundant with by-copy capture default",
11618 capture_id);
11619 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11620 && capture_kind == BY_REFERENCE)
11621 pedwarn (capture_token->location, 0, "explicit by-reference "
11622 "capture of %qD redundant with by-reference capture "
11623 "default", capture_id);
11626 /* Check for duplicates.
11627 Optimize for the zero or one explicit captures cases and only create
11628 the hash_set after adding second capture. */
11629 bool found = false;
11630 if (!ids.is_empty ())
11631 found = ids.add (capture_id);
11632 else if (first_capture_id == NULL_TREE)
11633 first_capture_id = capture_id;
11634 else if (capture_id == first_capture_id)
11635 found = true;
11636 else
11638 ids.add (first_capture_id);
11639 ids.add (capture_id);
11641 if (found)
11642 pedwarn (input_location, 0,
11643 "already captured %qD in lambda expression", capture_id);
11644 else
11645 add_capture (lambda_expr, capture_id, capture_init_expr,
11646 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11647 explicit_init_p);
11649 /* If there is any qualification still in effect, clear it
11650 now; we will be starting fresh with the next capture. */
11651 parser->scope = NULL_TREE;
11652 parser->qualifying_scope = NULL_TREE;
11653 parser->object_scope = NULL_TREE;
11656 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11659 /* Parse the (optional) middle of a lambda expression.
11661 lambda-declarator:
11662 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11663 lambda-specifiers (C++23)
11665 lambda-specifiers:
11666 decl-specifier-seq [opt] noexcept-specifier [opt]
11667 attribute-specifier-seq [opt] trailing-return-type [opt]
11669 LAMBDA_EXPR is the current representation of the lambda expression. */
11671 static bool
11672 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11674 /* 5.1.1.4 of the standard says:
11675 If a lambda-expression does not include a lambda-declarator, it is as if
11676 the lambda-declarator were ().
11677 This means an empty parameter list, no attributes, and no exception
11678 specification. */
11679 tree param_list = void_list_node;
11680 tree std_attrs = NULL_TREE;
11681 tree gnu_attrs = NULL_TREE;
11682 tree exception_spec = NULL_TREE;
11683 tree template_param_list = NULL_TREE;
11684 tree tx_qual = NULL_TREE;
11685 tree return_type = NULL_TREE;
11686 tree trailing_requires_clause = NULL_TREE;
11687 bool has_param_list = false;
11688 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11689 cp_decl_specifier_seq lambda_specs;
11690 clear_decl_specs (&lambda_specs);
11691 /* A lambda op() is const unless explicitly 'mutable'. */
11692 cp_cv_quals quals = TYPE_QUAL_CONST;
11694 /* The template-parameter-list is optional, but must begin with
11695 an opening angle if present. */
11696 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11698 if (cxx_dialect < cxx20
11699 && (pedantic || cxx_dialect < cxx14))
11700 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11701 "lambda templates are only available with "
11702 "%<-std=c++20%> or %<-std=gnu++20%>");
11704 cp_lexer_consume_token (parser->lexer);
11706 template_param_list = cp_parser_template_parameter_list (parser);
11707 cp_parser_require_end_of_template_parameter_list (parser);
11709 /* We may have a constrained generic lambda; parse the requires-clause
11710 immediately after the template-parameter-list and combine with any
11711 shorthand constraints present. */
11712 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11713 if (flag_concepts)
11715 tree reqs = get_shorthand_constraints (current_template_parms);
11716 if (dreqs)
11717 reqs = combine_constraint_expressions (reqs, dreqs);
11718 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11721 /* We just processed one more parameter list. */
11722 ++parser->num_template_parameter_lists;
11725 /* Committee discussion supports allowing attributes here. */
11726 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11728 /* The parameter-declaration-clause is optional (unless
11729 template-parameter-list was given), but must begin with an
11730 opening parenthesis if present. */
11731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11733 matching_parens parens;
11734 parens.consume_open (parser);
11736 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11738 /* Parse parameters. */
11739 param_list
11740 = cp_parser_parameter_declaration_clause
11741 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11743 /* Default arguments shall not be specified in the
11744 parameter-declaration-clause of a lambda-declarator. */
11745 if (pedantic && cxx_dialect < cxx14)
11746 for (tree t = param_list; t; t = TREE_CHAIN (t))
11747 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11748 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11749 OPT_Wc__14_extensions,
11750 "default argument specified for lambda parameter");
11752 parens.require_close (parser);
11753 has_param_list = true;
11755 else if (cxx_dialect < cxx23)
11756 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11758 /* In the decl-specifier-seq of the lambda-declarator, each
11759 decl-specifier shall either be mutable or constexpr. */
11760 int declares_class_or_enum;
11761 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11762 cp_parser_decl_specifier_seq (parser,
11763 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11764 &lambda_specs, &declares_class_or_enum);
11766 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11768 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11769 "parameter declaration before lambda declaration "
11770 "specifiers only optional with %<-std=c++2b%> or "
11771 "%<-std=gnu++2b%>");
11772 omitted_parms_loc = UNKNOWN_LOCATION;
11775 if (lambda_specs.storage_class == sc_mutable)
11777 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11778 quals = TYPE_UNQUALIFIED;
11780 else if (lambda_specs.storage_class == sc_static)
11782 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11783 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11784 error_at (lambda_specs.locations[ds_storage_class],
11785 "%<static%> lambda specifier with lambda capture");
11786 else
11788 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11789 quals = TYPE_UNQUALIFIED;
11793 tx_qual = cp_parser_tx_qualifier_opt (parser);
11794 if (omitted_parms_loc && tx_qual)
11796 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11797 "parameter declaration before lambda transaction "
11798 "qualifier only optional with %<-std=c++2b%> or "
11799 "%<-std=gnu++2b%>");
11800 omitted_parms_loc = UNKNOWN_LOCATION;
11803 /* Parse optional exception specification. */
11804 exception_spec
11805 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11807 if (omitted_parms_loc && exception_spec)
11809 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11810 "parameter declaration before lambda exception "
11811 "specification only optional with %<-std=c++2b%> or "
11812 "%<-std=gnu++2b%>");
11813 omitted_parms_loc = UNKNOWN_LOCATION;
11816 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11817 attributes that appertain to the function type. */
11818 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11819 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11820 else
11821 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11823 /* Parse optional trailing return type. */
11824 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11826 if (omitted_parms_loc)
11827 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11828 "parameter declaration before lambda trailing "
11829 "return type only optional with %<-std=c++2b%> or "
11830 "%<-std=gnu++2b%>");
11831 cp_lexer_consume_token (parser->lexer);
11832 return_type = cp_parser_trailing_type_id (parser);
11835 /* Also allow GNU attributes at the very end of the declaration, the usual
11836 place for GNU attributes. */
11837 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11838 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11840 if (has_param_list)
11842 /* Parse optional trailing requires clause. */
11843 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11845 /* The function parameters must be in scope all the way until after the
11846 trailing-return-type in case of decltype. */
11847 pop_bindings_and_leave_scope ();
11850 /* Create the function call operator.
11852 Messing with declarators like this is no uglier than building up the
11853 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11854 other code. */
11856 cp_decl_specifier_seq return_type_specs;
11857 cp_declarator* declarator;
11858 tree fco;
11859 void *p;
11861 clear_decl_specs (&return_type_specs);
11862 return_type_specs.type = make_auto ();
11864 if (lambda_specs.locations[ds_constexpr])
11866 if (cxx_dialect >= cxx17)
11867 return_type_specs.locations[ds_constexpr]
11868 = lambda_specs.locations[ds_constexpr];
11869 else
11870 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11871 "lambda only available with %<-std=c++17%> or "
11872 "%<-std=gnu++17%>");
11874 if (lambda_specs.locations[ds_consteval])
11875 return_type_specs.locations[ds_consteval]
11876 = lambda_specs.locations[ds_consteval];
11877 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11879 return_type_specs.storage_class = sc_static;
11880 return_type_specs.locations[ds_storage_class]
11881 = lambda_specs.locations[ds_storage_class];
11884 p = obstack_alloc (&declarator_obstack, 0);
11886 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11887 LAMBDA_EXPR_LOCATION (lambda_expr));
11889 declarator = make_call_declarator (declarator, param_list, quals,
11890 VIRT_SPEC_UNSPECIFIED,
11891 REF_QUAL_NONE,
11892 tx_qual,
11893 exception_spec,
11894 return_type,
11895 trailing_requires_clause,
11896 std_attrs,
11897 UNKNOWN_LOCATION);
11899 fco = grokmethod (&return_type_specs,
11900 declarator,
11901 chainon (gnu_attrs, lambda_specs.attributes));
11902 if (fco != error_mark_node)
11904 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11905 DECL_ARTIFICIAL (fco) = 1;
11906 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
11907 /* Give the object parameter a different name. */
11908 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11909 DECL_SET_LAMBDA_FUNCTION (fco, true);
11911 if (template_param_list)
11913 fco = finish_member_template_decl (fco);
11914 finish_template_decl (template_param_list);
11915 --parser->num_template_parameter_lists;
11917 else if (parser->fully_implicit_function_template_p)
11918 fco = finish_fully_implicit_template (parser, fco);
11920 finish_member_declaration (fco);
11921 record_lambda_scope_sig_discriminator (lambda_expr, fco);
11923 obstack_free (&declarator_obstack, p);
11925 return (fco != error_mark_node);
11929 /* Parse the body of a lambda expression, which is simply
11931 compound-statement
11933 but which requires special handling.
11934 LAMBDA_EXPR is the current representation of the lambda expression. */
11936 static void
11937 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11939 bool nested = (current_function_decl != NULL_TREE);
11940 unsigned char local_variables_forbidden_p
11941 = parser->local_variables_forbidden_p;
11942 bool in_function_body = parser->in_function_body;
11944 /* The body of a lambda-expression is not a subexpression of the enclosing
11945 expression. */
11946 cp_evaluated ev;
11948 if (nested)
11949 push_function_context ();
11950 else
11951 /* Still increment function_depth so that we don't GC in the
11952 middle of an expression. */
11953 ++function_depth;
11955 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11956 auto ord = make_temp_override (parser->oacc_routine, NULL);
11957 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11958 vec<tree> omp_privatization_save;
11959 save_omp_privatization_clauses (omp_privatization_save);
11960 /* Clear this in case we're in the middle of a default argument. */
11961 parser->local_variables_forbidden_p = 0;
11962 parser->in_function_body = true;
11965 local_specialization_stack s (lss_copy);
11966 tree fco = lambda_function (lambda_expr);
11967 tree body = start_lambda_function (fco, lambda_expr);
11969 /* Originally C++11 required us to peek for 'return expr'; and
11970 process it specially here to deduce the return type. N3638
11971 removed the need for that. */
11972 cp_parser_function_body (parser, false);
11974 finish_lambda_function (body);
11977 restore_omp_privatization_clauses (omp_privatization_save);
11978 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11979 parser->in_function_body = in_function_body;
11980 if (nested)
11981 pop_function_context();
11982 else
11983 --function_depth;
11986 /* Statements [gram.stmt.stmt] */
11988 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11990 static void
11991 add_debug_begin_stmt (location_t loc)
11993 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11994 return;
11995 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11996 /* A concept is never expanded normally. */
11997 return;
11999 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12000 SET_EXPR_LOCATION (stmt, loc);
12001 add_stmt (stmt);
12004 struct cp_omp_attribute_data
12006 cp_token_cache *tokens;
12007 const c_omp_directive *dir;
12008 c_omp_directive_kind kind;
12011 /* Handle omp::directive and omp::sequence attributes in ATTRS
12012 (if any) at the start of a statement or in attribute-declaration. */
12014 static tree
12015 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12017 if (!flag_openmp && !flag_openmp_simd)
12018 return attrs;
12020 auto_vec<cp_omp_attribute_data, 16> vec;
12021 int cnt = 0;
12022 int tokens = 0;
12023 bool bad = false;
12024 for (tree *pa = &attrs; *pa; )
12025 if (get_attribute_namespace (*pa) == omp_identifier
12026 && is_attribute_p ("directive", get_attribute_name (*pa)))
12028 cnt++;
12029 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12031 tree d = TREE_VALUE (a);
12032 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12033 cp_token *first = DEFPARSE_TOKENS (d)->first;
12034 cp_token *last = DEFPARSE_TOKENS (d)->last;
12035 if (parser->omp_attrs_forbidden_p)
12037 error_at (first->location,
12038 "mixing OpenMP directives with attribute and pragma "
12039 "syntax on the same statement");
12040 parser->omp_attrs_forbidden_p = false;
12041 bad = true;
12043 else if (TREE_PUBLIC (d))
12045 error_at (first->location,
12046 "OpenMP %<omp::decl%> attribute on a statement");
12047 bad = true;
12049 const char *directive[3] = {};
12050 for (int i = 0; i < 3; i++)
12052 tree id = NULL_TREE;
12053 if (first + i == last)
12054 break;
12055 if (first[i].type == CPP_NAME)
12056 id = first[i].u.value;
12057 else if (first[i].type == CPP_KEYWORD)
12058 id = ridpointers[(int) first[i].keyword];
12059 else
12060 break;
12061 directive[i] = IDENTIFIER_POINTER (id);
12063 const c_omp_directive *dir = NULL;
12064 if (directive[0])
12065 dir = c_omp_categorize_directive (directive[0], directive[1],
12066 directive[2]);
12067 if (dir == NULL)
12069 error_at (first->location,
12070 "unknown OpenMP directive name in %qs attribute "
12071 "argument",
12072 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12073 continue;
12075 c_omp_directive_kind kind = dir->kind;
12076 if (dir->id == PRAGMA_OMP_ORDERED)
12078 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12079 depend/doacross clause. */
12080 if (directive[1]
12081 && (strcmp (directive[1], "depend") == 0
12082 || strcmp (directive[1], "doacross") == 0))
12083 kind = C_OMP_DIR_STANDALONE;
12084 else if (first + 2 < last
12085 && first[1].type == CPP_COMMA
12086 && first[2].type == CPP_NAME
12087 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12088 "depend") == 0
12089 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12090 "doacross") == 0))
12091 kind = C_OMP_DIR_STANDALONE;
12093 else if (dir->id == PRAGMA_OMP_ERROR)
12095 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12096 int paren_depth = 0;
12097 for (int i = 1; first + i < last; i++)
12098 if (first[i].type == CPP_OPEN_PAREN)
12099 paren_depth++;
12100 else if (first[i].type == CPP_CLOSE_PAREN)
12101 paren_depth--;
12102 else if (paren_depth == 0
12103 && first + i + 2 < last
12104 && first[i].type == CPP_NAME
12105 && first[i + 1].type == CPP_OPEN_PAREN
12106 && first[i + 2].type == CPP_NAME
12107 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12108 "at")
12109 && !strcmp (IDENTIFIER_POINTER (first[i
12110 + 2].u.value),
12111 "execution"))
12113 kind = C_OMP_DIR_STANDALONE;
12114 break;
12117 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12118 vec.safe_push (v);
12119 if (flag_openmp || dir->simd)
12120 tokens += (last - first) + 1;
12122 cp_omp_attribute_data v = {};
12123 vec.safe_push (v);
12124 *pa = TREE_CHAIN (*pa);
12126 else
12127 pa = &TREE_CHAIN (*pa);
12129 if (bad)
12130 return attrs;
12132 unsigned int i;
12133 cp_omp_attribute_data *v;
12134 cp_omp_attribute_data *construct_seen = nullptr;
12135 cp_omp_attribute_data *standalone_seen = nullptr;
12136 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12137 FOR_EACH_VEC_ELT (vec, i, v)
12138 if (v->tokens)
12140 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12141 construct_seen = v;
12142 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12143 standalone_seen = v;
12145 else
12147 if (standalone_seen && !prev_standalone_seen)
12149 prev_standalone_seen = standalone_seen;
12150 standalone_seen = nullptr;
12154 if (cnt > 1 && construct_seen)
12156 error_at (construct_seen->tokens->first->location,
12157 "OpenMP construct among %<omp::directive%> attributes"
12158 " requires all %<omp::directive%> attributes on the"
12159 " same statement to be in the same %<omp::sequence%>");
12160 return attrs;
12162 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12164 error_at (standalone_seen->tokens->first->location,
12165 "multiple OpenMP standalone directives among"
12166 " %<omp::directive%> attributes must be all within the"
12167 " same %<omp::sequence%>");
12168 return attrs;
12171 if (prev_standalone_seen)
12172 standalone_seen = prev_standalone_seen;
12173 if (standalone_seen
12174 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12176 error_at (standalone_seen->tokens->first->location,
12177 "standalone OpenMP directives in %<omp::directive%> attribute"
12178 " can only appear on an empty statement");
12179 return attrs;
12181 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12183 cp_token *token = cp_lexer_peek_token (parser->lexer);
12184 enum pragma_kind kind = cp_parser_pragma_kind (token);
12185 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12187 error_at (token->location,
12188 "mixing OpenMP directives with attribute and pragma "
12189 "syntax on the same statement");
12190 return attrs;
12194 if (!tokens)
12195 return attrs;
12196 tokens++;
12197 cp_lexer *lexer = cp_lexer_alloc ();
12198 lexer->debugging_p = parser->lexer->debugging_p;
12199 vec_safe_reserve (lexer->buffer, tokens, true);
12200 FOR_EACH_VEC_ELT (vec, i, v)
12202 if (!v->tokens)
12203 continue;
12204 if (!flag_openmp && !v->dir->simd)
12205 continue;
12206 cp_token *first = v->tokens->first;
12207 cp_token *last = v->tokens->last;
12208 cp_token tok = {};
12209 tok.type = CPP_PRAGMA;
12210 tok.keyword = RID_MAX;
12211 tok.u.value = build_int_cst (NULL, v->dir->id);
12212 tok.location = first->location;
12213 lexer->buffer->quick_push (tok);
12214 while (++first < last)
12215 lexer->buffer->quick_push (*first);
12216 tok = {};
12217 tok.type = CPP_PRAGMA_EOL;
12218 tok.keyword = RID_MAX;
12219 tok.location = last->location;
12220 lexer->buffer->quick_push (tok);
12222 cp_token tok = {};
12223 tok.type = CPP_EOF;
12224 tok.keyword = RID_MAX;
12225 tok.location = lexer->buffer->last ().location;
12226 lexer->buffer->quick_push (tok);
12227 lexer->next = parser->lexer;
12228 lexer->next_token = lexer->buffer->address ();
12229 lexer->last_token = lexer->next_token
12230 + lexer->buffer->length ()
12231 - 1;
12232 lexer->in_omp_attribute_pragma = true;
12233 parser->lexer = lexer;
12234 /* Move the current source position to that of the first token in the
12235 new lexer. */
12236 cp_lexer_set_source_position_from_token (lexer->next_token);
12237 return attrs;
12240 /* True if and only if the name is one of the contract types. */
12242 static bool
12243 contract_attribute_p (const_tree id)
12245 return is_attribute_p ("assert", id)
12246 || is_attribute_p ("pre", id)
12247 || is_attribute_p ("post", id);
12250 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12251 (if any) at the start or after declaration-id of a declaration. */
12253 static void
12254 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12255 cp_omp_declare_simd_data *data,
12256 bool start)
12258 if (!flag_openmp && !flag_openmp_simd)
12259 return;
12261 int cnt = 0;
12262 bool bad = false;
12263 bool variant_p = false;
12264 location_t loc = UNKNOWN_LOCATION;
12265 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12266 if (get_attribute_namespace (pa) == omp_identifier
12267 && is_attribute_p ("directive", get_attribute_name (pa)))
12269 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12271 tree d = TREE_VALUE (a);
12272 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12273 cp_token *first = DEFPARSE_TOKENS (d)->first;
12274 cp_token *last = DEFPARSE_TOKENS (d)->last;
12275 const char *directive[3] = {};
12276 for (int i = 0; i < 3; i++)
12278 tree id = NULL_TREE;
12279 if (first + i == last)
12280 break;
12281 if (first[i].type == CPP_NAME)
12282 id = first[i].u.value;
12283 else if (first[i].type == CPP_KEYWORD)
12284 id = ridpointers[(int) first[i].keyword];
12285 else
12286 break;
12287 directive[i] = IDENTIFIER_POINTER (id);
12289 const c_omp_directive *dir = NULL;
12290 if (directive[0])
12291 dir = c_omp_categorize_directive (directive[0], directive[1],
12292 directive[2]);
12293 if (dir == NULL)
12294 continue;
12295 if (dir->id == PRAGMA_OMP_DECLARE
12296 && (strcmp (directive[1], "simd") == 0
12297 || strcmp (directive[1], "variant") == 0))
12299 if (cnt++ == 0)
12301 variant_p = strcmp (directive[1], "variant") == 0;
12302 loc = first->location;
12304 if (start && parser->omp_declare_simd && !bad)
12306 error_at (first->location,
12307 "mixing OpenMP directives with attribute and "
12308 "pragma syntax on the same declaration");
12309 bad = true;
12315 if (bad)
12317 for (tree *pa = pattrs; *pa; )
12318 if (get_attribute_namespace (*pa) == omp_identifier
12319 && is_attribute_p ("directive", get_attribute_name (*pa)))
12320 *pa = TREE_CHAIN (*pa);
12321 else
12322 pa = &TREE_CHAIN (*pa);
12323 return;
12325 if (cnt == 0)
12326 return;
12328 if (parser->omp_declare_simd == NULL)
12330 data->error_seen = false;
12331 data->fndecl_seen = false;
12332 data->variant_p = variant_p;
12333 data->loc = loc;
12334 data->tokens = vNULL;
12335 data->attribs[0] = NULL;
12336 data->attribs[1] = NULL;
12337 parser->omp_declare_simd = data;
12339 parser->omp_declare_simd->attribs[!start] = pattrs;
12342 /* Parse a statement.
12344 statement:
12345 labeled-statement
12346 expression-statement
12347 compound-statement
12348 selection-statement
12349 iteration-statement
12350 jump-statement
12351 declaration-statement
12352 try-block
12354 C++11:
12356 statement:
12357 labeled-statement
12358 attribute-specifier-seq (opt) expression-statement
12359 attribute-specifier-seq (opt) compound-statement
12360 attribute-specifier-seq (opt) selection-statement
12361 attribute-specifier-seq (opt) iteration-statement
12362 attribute-specifier-seq (opt) jump-statement
12363 declaration-statement
12364 attribute-specifier-seq (opt) try-block
12366 init-statement:
12367 expression-statement
12368 simple-declaration
12369 alias-declaration
12371 TM Extension:
12373 statement:
12374 atomic-statement
12376 IN_COMPOUND is true when the statement is nested inside a
12377 cp_parser_compound_statement.
12379 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12380 is a (possibly labeled) if statement which is not enclosed in braces
12381 and has an else clause. This is used to implement -Wparentheses.
12383 CHAIN is a vector of if-else-if conditions.
12385 Note that this version of parsing restricts assertions to be attached to
12386 empty statements. */
12388 static void
12389 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12390 const bool in_compound, bool *if_p, vec<tree> *chain,
12391 location_t *loc_after_labels)
12393 tree statement, std_attrs = NULL_TREE;
12394 cp_token *token;
12395 location_t statement_location, attrs_loc;
12396 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12397 bool has_std_attrs;
12398 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12399 This matters for certain pragmas. */
12400 bool in_compound_for_pragma = in_compound;
12402 restart:
12403 if (if_p != NULL)
12404 *if_p = false;
12405 /* There is no statement yet. */
12406 statement = NULL_TREE;
12408 saved_token_sentinel saved_tokens (parser->lexer);
12409 token = cp_lexer_peek_token (parser->lexer);
12410 attrs_loc = token->location;
12411 if (c_dialect_objc ())
12412 /* In obj-c++, seeing '[[' might be the either the beginning of
12413 c++11 attributes, or a nested objc-message-expression. So
12414 let's parse the c++11 attributes tentatively. */
12415 cp_parser_parse_tentatively (parser);
12416 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12417 if (std_attrs)
12418 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12419 if (c_dialect_objc ())
12421 if (!cp_parser_parse_definitely (parser))
12422 std_attrs = NULL_TREE;
12424 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12426 /* Peek at the next token. */
12427 token = cp_lexer_peek_token (parser->lexer);
12429 /* If we have contracts, check that they're valid in this context. */
12430 if (std_attrs != error_mark_node)
12432 if (tree pre = lookup_attribute ("pre", std_attrs))
12433 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12434 "preconditions cannot be statements");
12435 else if (tree post = lookup_attribute ("post", std_attrs))
12436 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12437 "postconditions cannot be statements");
12439 /* Check that assertions are null statements. */
12440 if (cp_contract_assertion_p (std_attrs))
12441 if (token->type != CPP_SEMICOLON)
12442 error_at (token->location, "assertions must be followed by %<;%>");
12445 bool omp_attrs_forbidden_p;
12446 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12448 if (std_attrs && (flag_openmp || flag_openmp_simd))
12450 bool handle_omp_attribs = false;
12451 if (token->type == CPP_KEYWORD)
12452 switch (token->keyword)
12454 case RID_IF:
12455 case RID_SWITCH:
12456 case RID_WHILE:
12457 case RID_DO:
12458 case RID_FOR:
12459 case RID_BREAK:
12460 case RID_CONTINUE:
12461 case RID_RETURN:
12462 case RID_CO_RETURN:
12463 case RID_GOTO:
12464 case RID_AT_TRY:
12465 case RID_AT_CATCH:
12466 case RID_AT_FINALLY:
12467 case RID_AT_SYNCHRONIZED:
12468 case RID_AT_THROW:
12469 case RID_TRY:
12470 case RID_TRANSACTION_ATOMIC:
12471 case RID_TRANSACTION_RELAXED:
12472 case RID_SYNCHRONIZED:
12473 case RID_ATOMIC_NOEXCEPT:
12474 case RID_ATOMIC_CANCEL:
12475 case RID_TRANSACTION_CANCEL:
12476 handle_omp_attribs = true;
12477 break;
12478 default:
12479 break;
12481 else if (token->type == CPP_SEMICOLON
12482 || token->type == CPP_OPEN_BRACE
12483 || token->type == CPP_PRAGMA)
12484 handle_omp_attribs = true;
12485 if (handle_omp_attribs)
12487 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12488 std_attrs);
12489 token = cp_lexer_peek_token (parser->lexer);
12492 parser->omp_attrs_forbidden_p = false;
12494 /* Remember the location of the first token in the statement. */
12495 cp_token *statement_token = token;
12496 statement_location = token->location;
12497 add_debug_begin_stmt (statement_location);
12498 /* If this is a keyword, then that will often determine what kind of
12499 statement we have. */
12500 if (token->type == CPP_KEYWORD)
12502 enum rid keyword = token->keyword;
12504 switch (keyword)
12506 case RID_CASE:
12507 case RID_DEFAULT:
12508 /* Looks like a labeled-statement with a case label.
12509 Parse the label, and then use tail recursion to parse
12510 the statement. */
12511 cp_parser_label_for_labeled_statement (parser, std_attrs);
12512 in_compound_for_pragma = false;
12513 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12514 goto restart;
12516 case RID_IF:
12517 case RID_SWITCH:
12518 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12519 statement = cp_parser_selection_statement (parser, if_p, chain);
12520 break;
12522 case RID_WHILE:
12523 case RID_DO:
12524 case RID_FOR:
12525 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12526 statement = cp_parser_iteration_statement (parser, if_p, false, 0,
12527 false);
12528 break;
12530 case RID_BREAK:
12531 case RID_CONTINUE:
12532 case RID_RETURN:
12533 case RID_CO_RETURN:
12534 case RID_GOTO:
12535 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12536 statement = cp_parser_jump_statement (parser);
12537 break;
12539 /* Objective-C++ exception-handling constructs. */
12540 case RID_AT_TRY:
12541 case RID_AT_CATCH:
12542 case RID_AT_FINALLY:
12543 case RID_AT_SYNCHRONIZED:
12544 case RID_AT_THROW:
12545 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12546 statement = cp_parser_objc_statement (parser);
12547 break;
12549 case RID_TRY:
12550 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12551 statement = cp_parser_try_block (parser);
12552 break;
12554 case RID_NAMESPACE:
12555 /* This must be a namespace alias definition. */
12556 if (has_std_attrs)
12558 /* Attributes should be parsed as part of the
12559 declaration, so let's un-parse them. */
12560 saved_tokens.rollback();
12561 std_attrs = NULL_TREE;
12563 cp_parser_declaration_statement (parser);
12564 return;
12566 case RID_TRANSACTION_ATOMIC:
12567 case RID_TRANSACTION_RELAXED:
12568 case RID_SYNCHRONIZED:
12569 case RID_ATOMIC_NOEXCEPT:
12570 case RID_ATOMIC_CANCEL:
12571 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12572 statement = cp_parser_transaction (parser, token);
12573 break;
12574 case RID_TRANSACTION_CANCEL:
12575 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12576 statement = cp_parser_transaction_cancel (parser);
12577 break;
12579 default:
12580 /* It might be a keyword like `int' that can start a
12581 declaration-statement. */
12582 break;
12585 else if (token->type == CPP_NAME)
12587 /* If the next token is a `:', then we are looking at a
12588 labeled-statement. */
12589 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12590 if (token->type == CPP_COLON)
12592 /* Looks like a labeled-statement with an ordinary label.
12593 Parse the label, and then use tail recursion to parse
12594 the statement. */
12596 cp_parser_label_for_labeled_statement (parser, std_attrs);
12598 /* If there's no statement, it's not a labeled-statement, just
12599 a label. That's allowed in C++23, but only if we're at the
12600 end of a compound-statement. */
12601 if (in_compound
12602 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12604 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12605 if (cxx_dialect < cxx23)
12606 pedwarn (loc, OPT_Wc__23_extensions,
12607 "label at end of compound statement only available "
12608 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12609 return;
12611 in_compound_for_pragma = false;
12612 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12613 goto restart;
12616 /* Anything that starts with a `{' must be a compound-statement. */
12617 else if (token->type == CPP_OPEN_BRACE)
12619 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12620 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12622 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12623 a statement all its own. */
12624 else if (token->type == CPP_PRAGMA)
12626 do_pragma:;
12627 cp_lexer *lexer = parser->lexer;
12628 bool do_restart = false;
12629 /* Only certain OpenMP pragmas are attached to statements, and thus
12630 are considered statements themselves. All others are not. In
12631 the context of a compound, accept the pragma as a "statement" and
12632 return so that we can check for a close brace. Otherwise we
12633 require a real statement and must go back and read one. */
12634 if (in_compound_for_pragma)
12636 if (cp_parser_pragma (parser, pragma_compound, if_p)
12637 && parser->omp_for_parse_state)
12638 check_omp_intervening_code (parser);
12640 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12641 do_restart = true;
12642 else if (parser->omp_for_parse_state)
12643 check_omp_intervening_code (parser);
12644 if (parser->lexer != lexer
12645 && lexer->in_omp_attribute_pragma
12646 && (!in_omp_attribute_pragma || lexer->orphan_p))
12648 if (saved_tokens.lexer == lexer)
12650 if (saved_tokens.mode == STS_COMMIT)
12651 cp_lexer_commit_tokens (lexer);
12652 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12653 saved_tokens.lexer = parser->lexer;
12654 saved_tokens.mode = STS_DONOTHING;
12655 saved_tokens.len = parser->lexer->saved_tokens.length ();
12657 cp_lexer_destroy (lexer);
12658 lexer = parser->lexer;
12660 if (do_restart)
12661 goto restart;
12662 if (parser->lexer == lexer
12663 && lexer->in_omp_attribute_pragma
12664 && !in_omp_attribute_pragma)
12665 parser->lexer->orphan_p = true;
12666 return;
12668 else if (token->type == CPP_EOF)
12670 cp_parser_error (parser, "expected statement");
12671 return;
12674 /* Everything else must be a declaration-statement or an
12675 expression-statement. Try for the declaration-statement
12676 first, unless we are looking at a `;', in which case we know that
12677 we have an expression-statement. */
12678 if (!statement)
12680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12682 if (has_std_attrs)
12683 /* Attributes should be parsed as part of the declaration,
12684 so let's un-parse them. */
12685 saved_tokens.rollback();
12687 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12688 cp_parser_parse_tentatively (parser);
12689 /* Try to parse the declaration-statement. */
12690 cp_parser_declaration_statement (parser);
12691 parser->omp_attrs_forbidden_p = false;
12692 /* If that worked, we're done. */
12693 if (cp_parser_parse_definitely (parser))
12694 return;
12695 /* It didn't work, restore the post-attribute position. */
12696 if (has_std_attrs)
12698 cp_lexer_set_token_position (parser->lexer, statement_token);
12699 if (flag_openmp || flag_openmp_simd)
12701 size_t i = 1;
12702 bool handle_omp_attribs = true;
12703 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12704 == RID_EXTENSION)
12705 i++;
12706 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12708 case RID_ASM:
12709 case RID_NAMESPACE:
12710 case RID_USING:
12711 case RID_LABEL:
12712 case RID_STATIC_ASSERT:
12713 /* Don't handle OpenMP attribs on keywords that
12714 always start a declaration statement but don't
12715 accept attribute before it and therefore
12716 the tentative cp_parser_declaration_statement
12717 fails to parse because of that. */
12718 handle_omp_attribs = false;
12719 break;
12720 default:
12721 break;
12724 if (handle_omp_attribs)
12726 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12727 std_attrs
12728 = cp_parser_handle_statement_omp_attributes
12729 (parser, std_attrs);
12730 parser->omp_attrs_forbidden_p = false;
12731 token = cp_lexer_peek_token (parser->lexer);
12732 if (token->type == CPP_PRAGMA)
12733 goto do_pragma;
12738 /* All preceding labels have been parsed at this point. */
12739 if (loc_after_labels != NULL)
12740 *loc_after_labels = statement_location;
12742 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12744 /* Look for an expression-statement instead. */
12745 statement = cp_parser_expression_statement (parser, in_statement_expr);
12747 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12748 attrs_loc);
12750 /* Handle [[fallthrough]];. */
12751 if (attribute_fallthrough_p (std_attrs))
12753 /* The next token after the fallthrough attribute is ';'. */
12754 if (statement == NULL_TREE)
12756 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12757 statement = build_call_expr_internal_loc (statement_location,
12758 IFN_FALLTHROUGH,
12759 void_type_node, 0);
12760 finish_expr_stmt (statement);
12762 else
12763 warning_at (statement_location, OPT_Wattributes,
12764 "%<fallthrough%> attribute not followed by %<;%>");
12765 std_attrs = NULL_TREE;
12768 /* Handle [[assert: ...]]; */
12769 if (cp_contract_assertion_p (std_attrs))
12771 /* Add the assertion as a statement in the current block. */
12772 gcc_assert (!statement || statement == error_mark_node);
12773 emit_assertion (std_attrs);
12774 std_attrs = NULL_TREE;
12778 /* Set the line number for the statement. */
12779 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12780 SET_EXPR_LOCATION (statement, statement_location);
12782 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12783 if (std_attrs != NULL_TREE)
12784 warning_at (attrs_loc,
12785 OPT_Wattributes,
12786 "attributes at the beginning of statement are ignored");
12789 /* Append ATTR to attribute list ATTRS. */
12791 tree
12792 attr_chainon (tree attrs, tree attr)
12794 if (attrs == error_mark_node)
12795 return error_mark_node;
12796 if (attr == error_mark_node)
12797 return error_mark_node;
12798 return chainon (attrs, attr);
12801 /* Parse the label for a labeled-statement, i.e.
12803 label:
12804 attribute-specifier-seq[opt] identifier :
12805 attribute-specifier-seq[opt] case constant-expression :
12806 attribute-specifier-seq[opt] default :
12808 labeled-statement:
12809 label statement
12811 GNU Extension:
12812 case constant-expression ... constant-expression : statement
12814 When a label is parsed without errors, the label is added to the
12815 parse tree by the finish_* functions, so this function doesn't
12816 have to return the label. */
12818 static void
12819 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12821 cp_token *token;
12822 tree label = NULL_TREE;
12823 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12825 /* The next token should be an identifier. */
12826 token = cp_lexer_peek_token (parser->lexer);
12827 if (token->type != CPP_NAME
12828 && token->type != CPP_KEYWORD)
12830 cp_parser_error (parser, "expected labeled-statement");
12831 return;
12834 /* Remember whether this case or a user-defined label is allowed to fall
12835 through to. */
12836 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12838 parser->colon_corrects_to_scope_p = false;
12839 switch (token->keyword)
12841 case RID_CASE:
12843 tree expr, expr_hi;
12844 cp_token *ellipsis;
12846 /* Consume the `case' token. */
12847 cp_lexer_consume_token (parser->lexer);
12848 /* Parse the constant-expression. */
12849 expr = cp_parser_constant_expression (parser);
12850 if (check_for_bare_parameter_packs (expr))
12851 expr = error_mark_node;
12853 ellipsis = cp_lexer_peek_token (parser->lexer);
12854 if (ellipsis->type == CPP_ELLIPSIS)
12856 /* Consume the `...' token. */
12857 cp_lexer_consume_token (parser->lexer);
12858 expr_hi = cp_parser_constant_expression (parser);
12859 if (check_for_bare_parameter_packs (expr_hi))
12860 expr_hi = error_mark_node;
12862 /* We don't need to emit warnings here, as the common code
12863 will do this for us. */
12865 else
12866 expr_hi = NULL_TREE;
12868 if (parser->in_switch_statement_p)
12870 tree l = finish_case_label (token->location, expr, expr_hi);
12871 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12873 label = CASE_LABEL (l);
12874 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12877 else
12878 error_at (token->location,
12879 "case label %qE not within a switch statement",
12880 expr);
12882 break;
12884 case RID_DEFAULT:
12885 /* Consume the `default' token. */
12886 cp_lexer_consume_token (parser->lexer);
12888 if (parser->in_switch_statement_p)
12890 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12891 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12893 label = CASE_LABEL (l);
12894 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12897 else
12898 error_at (token->location, "case label not within a switch statement");
12899 break;
12901 default:
12902 /* Anything else must be an ordinary label. */
12903 label = finish_label_stmt (cp_parser_identifier (parser));
12904 if (label && TREE_CODE (label) == LABEL_DECL)
12905 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12906 break;
12909 /* Require the `:' token. */
12910 cp_parser_require (parser, CPP_COLON, RT_COLON);
12912 /* An ordinary label may optionally be followed by attributes.
12913 However, this is only permitted if the attributes are then
12914 followed by a semicolon. This is because, for backward
12915 compatibility, when parsing
12916 lab: __attribute__ ((unused)) int i;
12917 we want the attribute to attach to "i", not "lab". */
12918 if (label != NULL_TREE
12919 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12921 tree attrs;
12922 cp_parser_parse_tentatively (parser);
12923 attrs = cp_parser_gnu_attributes_opt (parser);
12924 if (attrs == NULL_TREE
12925 /* And fallthrough always binds to the expression-statement. */
12926 || attribute_fallthrough_p (attrs)
12927 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12928 cp_parser_abort_tentative_parse (parser);
12929 else if (!cp_parser_parse_definitely (parser))
12931 else
12932 attributes = attr_chainon (attributes, attrs);
12935 if (attributes != NULL_TREE)
12936 cplus_decl_attributes (&label, attributes, 0);
12938 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12941 /* Parse an expression-statement.
12943 expression-statement:
12944 expression [opt] ;
12946 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12947 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12948 indicates whether this expression-statement is part of an
12949 expression statement. */
12951 static tree
12952 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12954 tree statement = NULL_TREE;
12955 cp_token *token = cp_lexer_peek_token (parser->lexer);
12956 location_t loc = token->location;
12958 /* There might be attribute fallthrough. */
12959 tree attr = cp_parser_gnu_attributes_opt (parser);
12961 /* If the next token is a ';', then there is no expression
12962 statement. */
12963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12965 statement = cp_parser_expression (parser);
12966 if (statement == error_mark_node
12967 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12969 cp_parser_skip_to_end_of_block_or_statement (parser);
12970 return error_mark_node;
12974 attr = process_stmt_assume_attribute (attr, statement, loc);
12976 /* Handle [[fallthrough]];. */
12977 if (attribute_fallthrough_p (attr))
12979 /* The next token after the fallthrough attribute is ';'. */
12980 if (statement == NULL_TREE)
12981 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12982 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12983 void_type_node, 0);
12984 else
12985 warning_at (loc, OPT_Wattributes,
12986 "%<fallthrough%> attribute not followed by %<;%>");
12987 attr = NULL_TREE;
12990 /* Allow "[[fallthrough]];", but warn otherwise. */
12991 if (attr != NULL_TREE)
12992 warning_at (loc, OPT_Wattributes,
12993 "attributes at the beginning of statement are ignored");
12995 /* Give a helpful message for "A<T>::type t;" and the like. */
12996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12997 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12999 if (TREE_CODE (statement) == SCOPE_REF)
13000 error_at (token->location, "need %<typename%> before %qE because "
13001 "%qT is a dependent scope",
13002 statement, TREE_OPERAND (statement, 0));
13003 else if (is_overloaded_fn (statement)
13004 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13006 /* A::A a; */
13007 tree fn = get_first_fn (statement);
13008 error_at (token->location,
13009 "%<%T::%D%> names the constructor, not the type",
13010 DECL_CONTEXT (fn), DECL_NAME (fn));
13014 /* Consume the final `;'. */
13015 cp_parser_consume_semicolon_at_end_of_statement (parser);
13017 if (in_statement_expr
13018 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13019 /* This is the final expression statement of a statement
13020 expression. */
13021 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13022 else if (statement)
13023 statement = finish_expr_stmt (statement);
13025 return statement;
13028 /* Parse a compound-statement.
13030 compound-statement:
13031 { statement-seq [opt] label-seq [opt] }
13033 label-seq:
13034 label
13035 label-seq label
13037 GNU extension:
13039 compound-statement:
13040 { label-declaration-seq [opt] statement-seq [opt] }
13042 label-declaration-seq:
13043 label-declaration
13044 label-declaration-seq label-declaration
13046 Returns a tree representing the statement. */
13048 static tree
13049 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13050 int bcs_flags, bool function_body)
13052 tree compound_stmt;
13053 matching_braces braces;
13055 /* Consume the `{'. */
13056 if (!braces.require_open (parser))
13057 return error_mark_node;
13058 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13059 && !function_body && cxx_dialect < cxx14)
13060 pedwarn (input_location, OPT_Wpedantic,
13061 "compound-statement in %<constexpr%> function");
13062 /* Begin the compound-statement. */
13063 compound_stmt = begin_compound_stmt (bcs_flags);
13064 /* If the next keyword is `__label__' we have a label declaration. */
13065 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13066 cp_parser_label_declaration (parser);
13067 /* Parse an (optional) statement-seq. */
13068 cp_parser_statement_seq_opt (parser, in_statement_expr);
13070 /* Consume the `}'. */
13071 braces.require_close (parser);
13073 /* Finish the compound-statement. */
13074 finish_compound_stmt (compound_stmt);
13076 return compound_stmt;
13079 /* Diagnose errors related to imperfectly nested loops in an OMP
13080 loop construct. This function is called when such code is seen.
13081 Only issue one such diagnostic no matter how much invalid
13082 intervening code there is in the loop.
13083 FIXME: maybe the location associated with the diagnostic should
13084 be the current parser token instead of the location of the outer loop
13085 nest. */
13087 static void
13088 check_omp_intervening_code (cp_parser *parser)
13090 struct omp_for_parse_data *omp_for_parse_state
13091 = parser->omp_for_parse_state;
13092 gcc_assert (omp_for_parse_state);
13094 if (!omp_for_parse_state->in_intervening_code)
13095 return;
13096 omp_for_parse_state->saw_intervening_code = true;
13098 /* Only diagnose errors related to perfect nesting once. */
13099 if (!omp_for_parse_state->perfect_nesting_fail)
13101 if (omp_for_parse_state->code == OACC_LOOP)
13103 error_at (omp_for_parse_state->for_loc,
13104 "inner loops must be perfectly nested in "
13105 "%<#pragma acc loop%>");
13106 omp_for_parse_state->perfect_nesting_fail = true;
13108 else if (omp_for_parse_state->ordered)
13110 error_at (omp_for_parse_state->for_loc,
13111 "inner loops must be perfectly nested with "
13112 "%<ordered%> clause");
13113 omp_for_parse_state->perfect_nesting_fail = true;
13115 else if (omp_for_parse_state->inscan)
13117 error_at (omp_for_parse_state->for_loc,
13118 "inner loops must be perfectly nested with "
13119 "%<reduction%> %<inscan%> clause");
13120 omp_for_parse_state->perfect_nesting_fail = true;
13122 /* TODO: Also reject loops with TILE directive. */
13123 if (omp_for_parse_state->perfect_nesting_fail)
13124 omp_for_parse_state->fail = true;
13128 /* Parse an (optional) statement-seq.
13130 statement-seq:
13131 statement
13132 statement-seq [opt] statement */
13134 static void
13135 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13137 struct omp_for_parse_data *omp_for_parse_state
13138 = parser->omp_for_parse_state;
13139 bool in_omp_loop_block
13140 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13142 /* Scan statements until there aren't any more. */
13143 while (true)
13145 cp_token *token = cp_lexer_peek_token (parser->lexer);
13147 /* If we are looking at a `}', then we have run out of
13148 statements; the same is true if we have reached the end
13149 of file, or have stumbled upon a stray '@end'. */
13150 if (token->type == CPP_CLOSE_BRACE
13151 || token->type == CPP_EOF
13152 || token->type == CPP_PRAGMA_EOL
13153 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13154 break;
13156 /* If we are in a compound statement and find 'else' then
13157 something went wrong. */
13158 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13160 if (parser->in_statement & IN_IF_STMT)
13161 break;
13162 else
13164 token = cp_lexer_consume_token (parser->lexer);
13165 error_at (token->location, "%<else%> without a previous %<if%>");
13169 /* Handle special cases for OMP FOR canonical loop syntax. */
13170 else if (in_omp_loop_block)
13172 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13173 if (want_nested_loop
13174 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13176 /* Found the nested loop. */
13177 omp_for_parse_state->depth++;
13178 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13179 omp_for_parse_state->depth--;
13181 else if (token->type == CPP_SEMICOLON)
13183 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13184 loop form, GCC used to accept an empty statements as not
13185 being intervening code. Continue to do that, as an
13186 extension. */
13187 /* FIXME: Maybe issue a warning or something here? */
13188 cp_lexer_consume_token (parser->lexer);
13190 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13191 /* The nested compound statement may contain the next loop, or
13192 it might just be intervening code. */
13194 cp_parser_statement (parser, in_statement_expr, true, NULL);
13195 if (omp_for_parse_state->want_nested_loop)
13196 check_omp_intervening_code (parser);
13198 else
13200 /* This must be intervening code. */
13201 omp_for_parse_state->want_nested_loop = false;
13202 /* Defer calling check_omp_intervening_code on pragmas until
13203 cp_parser_statement, because we can't know until we parse
13204 it whether or not the pragma is a statement. */
13205 if (token->type != CPP_PRAGMA)
13206 check_omp_intervening_code (parser);
13207 cp_parser_statement (parser, in_statement_expr, true, NULL);
13208 omp_for_parse_state->want_nested_loop = want_nested_loop;
13210 continue;
13213 /* Parse the statement. */
13214 cp_parser_statement (parser, in_statement_expr, true, NULL);
13218 /* Return true if this is the C++20 version of range-based-for with
13219 init-statement. */
13221 static bool
13222 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13224 bool r = false;
13226 /* Save tokens so that we can put them back. */
13227 cp_lexer_save_tokens (parser->lexer);
13229 /* There has to be an unnested ; followed by an unnested :. */
13230 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13231 /*recovering=*/false,
13232 CPP_SEMICOLON,
13233 /*consume_paren=*/false) != -1)
13234 goto out;
13236 /* We found the semicolon, eat it now. */
13237 cp_lexer_consume_token (parser->lexer);
13239 /* Now look for ':' that is not nested in () or {}. */
13240 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13241 /*recovering=*/false,
13242 CPP_COLON,
13243 /*consume_paren=*/false) == -1);
13245 out:
13246 /* Roll back the tokens we skipped. */
13247 cp_lexer_rollback_tokens (parser->lexer);
13249 return r;
13252 /* Return true if we're looking at (init; cond), false otherwise. */
13254 static bool
13255 cp_parser_init_statement_p (cp_parser *parser)
13257 /* Save tokens so that we can put them back. */
13258 cp_lexer_save_tokens (parser->lexer);
13260 /* Look for ';' that is not nested in () or {}. */
13261 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13262 /*recovering=*/false,
13263 CPP_SEMICOLON,
13264 /*consume_paren=*/false);
13266 /* Roll back the tokens we skipped. */
13267 cp_lexer_rollback_tokens (parser->lexer);
13269 return ret == -1;
13272 /* Parse a selection-statement.
13274 selection-statement:
13275 if ( init-statement [opt] condition ) statement
13276 if ( init-statement [opt] condition ) statement else statement
13277 switch ( init-statement [opt] condition ) statement
13279 Returns the new IF_STMT or SWITCH_STMT.
13281 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13282 is a (possibly labeled) if statement which is not enclosed in
13283 braces and has an else clause. This is used to implement
13284 -Wparentheses.
13286 CHAIN is a vector of if-else-if conditions. This is used to implement
13287 -Wduplicated-cond. */
13289 static tree
13290 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13291 vec<tree> *chain)
13293 cp_token *token;
13294 enum rid keyword;
13295 token_indent_info guard_tinfo;
13297 if (if_p != NULL)
13298 *if_p = false;
13300 /* Peek at the next token. */
13301 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13302 guard_tinfo = get_token_indent_info (token);
13304 /* See what kind of keyword it is. */
13305 keyword = token->keyword;
13306 switch (keyword)
13308 case RID_IF:
13309 case RID_SWITCH:
13311 tree statement;
13312 tree condition;
13314 bool cx = false;
13315 if (keyword == RID_IF
13316 && cp_lexer_next_token_is_keyword (parser->lexer,
13317 RID_CONSTEXPR))
13319 cx = true;
13320 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13321 if (cxx_dialect < cxx17)
13322 pedwarn (tok->location, OPT_Wc__17_extensions,
13323 "%<if constexpr%> only available with "
13324 "%<-std=c++17%> or %<-std=gnu++17%>");
13326 int ce = 0;
13327 if (keyword == RID_IF && !cx)
13329 if (cp_lexer_next_token_is_keyword (parser->lexer,
13330 RID_CONSTEVAL))
13331 ce = 1;
13332 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13333 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13334 RID_CONSTEVAL))
13336 ce = -1;
13337 cp_lexer_consume_token (parser->lexer);
13340 if (ce)
13342 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13343 if (cxx_dialect < cxx23)
13344 pedwarn (tok->location, OPT_Wc__23_extensions,
13345 "%<if consteval%> only available with "
13346 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13348 bool save_in_consteval_if_p = in_consteval_if_p;
13349 statement = begin_if_stmt ();
13350 IF_STMT_CONSTEVAL_P (statement) = true;
13351 condition = finish_if_stmt_cond (boolean_false_node, statement);
13353 gcc_rich_location richloc (tok->location);
13354 bool non_compound_stmt_p = false;
13355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13357 non_compound_stmt_p = true;
13358 richloc.add_fixit_insert_after (tok->location, "{");
13361 in_consteval_if_p |= ce > 0;
13362 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13364 if (non_compound_stmt_p)
13366 location_t before_loc
13367 = cp_lexer_peek_token (parser->lexer)->location;
13368 richloc.add_fixit_insert_before (before_loc, "}");
13369 error_at (&richloc,
13370 "%<if consteval%> requires compound statement");
13371 non_compound_stmt_p = false;
13374 finish_then_clause (statement);
13376 /* If the next token is `else', parse the else-clause. */
13377 if (cp_lexer_next_token_is_keyword (parser->lexer,
13378 RID_ELSE))
13380 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13381 gcc_rich_location else_richloc (else_tok->location);
13382 guard_tinfo = get_token_indent_info (else_tok);
13383 /* Consume the `else' keyword. */
13384 cp_lexer_consume_token (parser->lexer);
13386 begin_else_clause (statement);
13388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13390 non_compound_stmt_p = true;
13391 else_richloc.add_fixit_insert_after (else_tok->location,
13392 "{");
13395 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13396 cp_parser_implicitly_scoped_statement (parser, NULL,
13397 guard_tinfo);
13399 if (non_compound_stmt_p)
13401 location_t before_loc
13402 = cp_lexer_peek_token (parser->lexer)->location;
13403 else_richloc.add_fixit_insert_before (before_loc, "}");
13404 error_at (&else_richloc,
13405 "%<if consteval%> requires compound statement");
13408 finish_else_clause (statement);
13411 in_consteval_if_p = save_in_consteval_if_p;
13412 if (ce < 0)
13414 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13415 if (THEN_CLAUSE (statement) == NULL_TREE)
13416 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13419 finish_if_stmt (statement);
13420 return statement;
13423 /* Look for the `('. */
13424 matching_parens parens;
13425 if (!parens.require_open (parser))
13427 cp_parser_skip_to_end_of_statement (parser);
13428 return error_mark_node;
13431 /* Begin the selection-statement. */
13432 if (keyword == RID_IF)
13434 statement = begin_if_stmt ();
13435 IF_STMT_CONSTEXPR_P (statement) = cx;
13437 else
13438 statement = begin_switch_stmt ();
13440 /* Parse the optional init-statement. */
13441 if (cp_parser_init_statement_p (parser))
13443 tree decl;
13444 if (cxx_dialect < cxx17)
13445 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13446 OPT_Wc__17_extensions,
13447 "init-statement in selection statements only available "
13448 "with %<-std=c++17%> or %<-std=gnu++17%>");
13449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13450 /* A non-empty init-statement can have arbitrary side
13451 effects. */
13452 vec_free (chain);
13453 cp_parser_init_statement (parser, &decl);
13456 /* Parse the condition. */
13457 condition = cp_parser_condition (parser);
13458 /* Look for the `)'. */
13459 if (!parens.require_close (parser))
13460 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13461 /*consume_paren=*/true);
13463 if (keyword == RID_IF)
13465 bool nested_if;
13466 unsigned char in_statement;
13468 /* Add the condition. */
13469 condition = finish_if_stmt_cond (condition, statement);
13471 if (warn_duplicated_cond)
13472 warn_duplicated_cond_add_or_warn (token->location, condition,
13473 &chain);
13475 /* Parse the then-clause. */
13476 in_statement = parser->in_statement;
13477 parser->in_statement |= IN_IF_STMT;
13479 /* Outside a template, the non-selected branch of a constexpr
13480 if is a 'discarded statement', i.e. unevaluated. */
13481 bool was_discarded = in_discarded_stmt;
13482 bool discard_then = (cx && !processing_template_decl
13483 && integer_zerop (condition));
13484 if (discard_then)
13486 in_discarded_stmt = true;
13487 ++c_inhibit_evaluation_warnings;
13490 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13491 guard_tinfo);
13493 parser->in_statement = in_statement;
13495 finish_then_clause (statement);
13497 if (discard_then)
13499 THEN_CLAUSE (statement) = NULL_TREE;
13500 in_discarded_stmt = was_discarded;
13501 --c_inhibit_evaluation_warnings;
13504 /* If the next token is `else', parse the else-clause. */
13505 if (cp_lexer_next_token_is_keyword (parser->lexer,
13506 RID_ELSE))
13508 bool discard_else = (cx && !processing_template_decl
13509 && integer_nonzerop (condition));
13510 if (discard_else)
13512 in_discarded_stmt = true;
13513 ++c_inhibit_evaluation_warnings;
13516 guard_tinfo
13517 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13518 /* Consume the `else' keyword. */
13519 cp_lexer_consume_token (parser->lexer);
13520 if (warn_duplicated_cond)
13522 if (cp_lexer_next_token_is_keyword (parser->lexer,
13523 RID_IF)
13524 && chain == NULL)
13526 /* We've got "if (COND) else if (COND2)". Start
13527 the condition chain and add COND as the first
13528 element. */
13529 chain = new vec<tree> ();
13530 if (!CONSTANT_CLASS_P (condition)
13531 && !TREE_SIDE_EFFECTS (condition))
13533 /* Wrap it in a NOP_EXPR so that we can set the
13534 location of the condition. */
13535 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13536 condition);
13537 SET_EXPR_LOCATION (e, token->location);
13538 chain->safe_push (e);
13541 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13542 RID_IF))
13543 /* This is if-else without subsequent if. Zap the
13544 condition chain; we would have already warned at
13545 this point. */
13546 vec_free (chain);
13548 begin_else_clause (statement);
13549 /* Parse the else-clause. */
13550 cp_parser_implicitly_scoped_statement (parser, NULL,
13551 guard_tinfo, chain);
13553 finish_else_clause (statement);
13555 /* If we are currently parsing a then-clause, then
13556 IF_P will not be NULL. We set it to true to
13557 indicate that this if statement has an else clause.
13558 This may trigger the Wparentheses warning below
13559 when we get back up to the parent if statement. */
13560 if (if_p != NULL)
13561 *if_p = true;
13563 if (discard_else)
13565 ELSE_CLAUSE (statement) = NULL_TREE;
13566 in_discarded_stmt = was_discarded;
13567 --c_inhibit_evaluation_warnings;
13570 else
13572 /* This if statement does not have an else clause. If
13573 NESTED_IF is true, then the then-clause has an if
13574 statement which does have an else clause. We warn
13575 about the potential ambiguity. */
13576 if (nested_if)
13577 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13578 "suggest explicit braces to avoid ambiguous"
13579 " %<else%>");
13580 if (warn_duplicated_cond)
13581 /* We don't need the condition chain anymore. */
13582 vec_free (chain);
13585 /* Now we're all done with the if-statement. */
13586 finish_if_stmt (statement);
13588 else
13590 bool in_switch_statement_p;
13591 unsigned char in_statement;
13593 /* Add the condition. */
13594 finish_switch_cond (condition, statement);
13596 /* Parse the body of the switch-statement. */
13597 in_switch_statement_p = parser->in_switch_statement_p;
13598 in_statement = parser->in_statement;
13599 parser->in_switch_statement_p = true;
13600 parser->in_statement |= IN_SWITCH_STMT;
13601 cp_parser_implicitly_scoped_statement (parser, if_p,
13602 guard_tinfo);
13603 parser->in_switch_statement_p = in_switch_statement_p;
13604 parser->in_statement = in_statement;
13606 /* Now we're all done with the switch-statement. */
13607 finish_switch_stmt (statement);
13610 return statement;
13612 break;
13614 default:
13615 cp_parser_error (parser, "expected selection-statement");
13616 return error_mark_node;
13620 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13621 If we have seen at least one decl-specifier, and the next token is not
13622 a parenthesis (after "int (" we might be looking at a functional cast)
13623 neither we are dealing with a concept-check expression then we must be
13624 looking at a declaration. */
13626 static void
13627 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13628 cp_decl_specifier_seq *decl_specs)
13630 if (decl_specs->any_specifiers_p
13631 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13632 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13633 && !cp_parser_error_occurred (parser)
13634 && !(decl_specs->type
13635 && TREE_CODE (decl_specs->type) == TYPE_DECL
13636 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13637 cp_parser_commit_to_tentative_parse (parser);
13640 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13641 The declarator shall not specify a function or an array. Returns
13642 TRUE if the declarator is valid, FALSE otherwise. */
13644 static bool
13645 cp_parser_check_condition_declarator (cp_parser* parser,
13646 cp_declarator *declarator,
13647 location_t loc)
13649 if (declarator == cp_error_declarator
13650 || function_declarator_p (declarator)
13651 || declarator->kind == cdk_array)
13653 if (declarator == cp_error_declarator)
13654 /* Already complained. */;
13655 else if (declarator->kind == cdk_array)
13656 error_at (loc, "condition declares an array");
13657 else
13658 error_at (loc, "condition declares a function");
13659 if (parser->fully_implicit_function_template_p)
13660 abort_fully_implicit_template (parser);
13661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13662 /*or_comma=*/false,
13663 /*consume_paren=*/false);
13664 return false;
13666 else
13667 return true;
13670 /* Parse a condition.
13672 condition:
13673 expression
13674 type-specifier-seq declarator = initializer-clause
13675 type-specifier-seq declarator braced-init-list
13677 GNU Extension:
13679 condition:
13680 type-specifier-seq declarator asm-specification [opt]
13681 attributes [opt] = assignment-expression
13683 Returns the expression that should be tested. */
13685 static tree
13686 cp_parser_condition (cp_parser* parser)
13688 cp_decl_specifier_seq type_specifiers;
13689 const char *saved_message;
13690 int declares_class_or_enum;
13692 /* Try the declaration first. */
13693 cp_parser_parse_tentatively (parser);
13694 /* New types are not allowed in the type-specifier-seq for a
13695 condition. */
13696 saved_message = parser->type_definition_forbidden_message;
13697 parser->type_definition_forbidden_message
13698 = G_("types may not be defined in conditions");
13699 /* Parse the type-specifier-seq. */
13700 cp_parser_decl_specifier_seq (parser,
13701 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13702 &type_specifiers,
13703 &declares_class_or_enum);
13704 /* Restore the saved message. */
13705 parser->type_definition_forbidden_message = saved_message;
13707 /* Gather the attributes that were provided with the
13708 decl-specifiers. */
13709 tree prefix_attributes = type_specifiers.attributes;
13711 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13713 /* If all is well, we might be looking at a declaration. */
13714 if (!cp_parser_error_occurred (parser))
13716 tree decl;
13717 tree asm_specification;
13718 tree attributes;
13719 cp_declarator *declarator;
13720 tree initializer = NULL_TREE;
13721 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13723 /* Parse the declarator. */
13724 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13725 CP_PARSER_FLAGS_NONE,
13726 /*ctor_dtor_or_conv_p=*/NULL,
13727 /*parenthesized_p=*/NULL,
13728 /*member_p=*/false,
13729 /*friend_p=*/false,
13730 /*static_p=*/false);
13731 /* Parse the attributes. */
13732 attributes = cp_parser_attributes_opt (parser);
13733 /* Parse the asm-specification. */
13734 asm_specification = cp_parser_asm_specification_opt (parser);
13735 /* If the next token is not an `=' or '{', then we might still be
13736 looking at an expression. For example:
13738 if (A(a).x)
13740 looks like a decl-specifier-seq and a declarator -- but then
13741 there is no `=', so this is an expression. */
13742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13743 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13744 cp_parser_simulate_error (parser);
13746 /* If we did see an `=' or '{', then we are looking at a declaration
13747 for sure. */
13748 if (cp_parser_parse_definitely (parser))
13750 tree pushed_scope;
13751 bool non_constant_p = false;
13752 int flags = LOOKUP_ONLYCONVERTING;
13754 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13755 return error_mark_node;
13757 /* Create the declaration. */
13758 decl = start_decl (declarator, &type_specifiers,
13759 /*initialized_p=*/true,
13760 attributes, prefix_attributes,
13761 &pushed_scope);
13763 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13764 /* Parse the initializer. */
13765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13767 initializer = cp_parser_braced_list (parser, &non_constant_p);
13768 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13769 flags = 0;
13771 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13773 /* Consume the `='. */
13774 cp_lexer_consume_token (parser->lexer);
13775 initializer = cp_parser_initializer_clause (parser,
13776 &non_constant_p);
13778 else
13780 cp_parser_error (parser, "expected initializer");
13781 initializer = error_mark_node;
13783 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13786 /* Process the initializer. */
13787 cp_finish_decl (decl,
13788 initializer, !non_constant_p,
13789 asm_specification,
13790 flags);
13792 if (pushed_scope)
13793 pop_scope (pushed_scope);
13795 return convert_from_reference (decl);
13798 /* If we didn't even get past the declarator successfully, we are
13799 definitely not looking at a declaration. */
13800 else
13801 cp_parser_abort_tentative_parse (parser);
13803 /* Otherwise, we are looking at an expression. */
13804 return cp_parser_expression (parser);
13807 /* Parses a for-statement or range-for-statement until the closing ')',
13808 not included. */
13810 static tree
13811 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll,
13812 bool novector)
13814 tree init, scope, decl;
13815 bool is_range_for;
13817 /* Begin the for-statement. */
13818 scope = begin_for_scope (&init);
13820 /* Maybe parse the optional init-statement in a range-based for loop. */
13821 if (cp_parser_range_based_for_with_init_p (parser)
13822 /* Checked for diagnostic purposes only. */
13823 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13825 tree dummy;
13826 cp_parser_init_statement (parser, &dummy);
13827 if (cxx_dialect < cxx20)
13829 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13830 OPT_Wc__20_extensions,
13831 "range-based %<for%> loops with initializer only "
13832 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13833 decl = error_mark_node;
13837 /* Parse the initialization. */
13838 is_range_for = cp_parser_init_statement (parser, &decl);
13840 if (is_range_for)
13841 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13842 novector, false);
13843 else
13844 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
13847 static tree
13848 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13849 unsigned short unroll, bool novector)
13851 /* Normal for loop */
13852 tree condition = NULL_TREE;
13853 tree expression = NULL_TREE;
13854 tree stmt;
13856 stmt = begin_for_stmt (scope, init);
13857 /* The init-statement has already been parsed in
13858 cp_parser_init_statement, so no work is needed here. */
13859 finish_init_stmt (stmt);
13861 /* If there's a condition, process it. */
13862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13863 condition = cp_parser_condition (parser);
13864 else if (ivdep)
13866 cp_parser_error (parser, "missing loop condition in loop with "
13867 "%<GCC ivdep%> pragma");
13868 condition = error_mark_node;
13870 else if (unroll)
13872 cp_parser_error (parser, "missing loop condition in loop with "
13873 "%<GCC unroll%> pragma");
13874 condition = error_mark_node;
13876 finish_for_cond (condition, stmt, ivdep, unroll, novector);
13877 /* Look for the `;'. */
13878 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13880 /* If there's an expression, process it. */
13881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13882 expression = cp_parser_expression (parser);
13883 finish_for_expr (expression, stmt);
13885 return stmt;
13888 /* Tries to parse a range-based for-statement:
13890 range-based-for:
13891 decl-specifier-seq declarator : expression
13893 The decl-specifier-seq declarator and the `:' are already parsed by
13894 cp_parser_init_statement. If processing_template_decl it returns a
13895 newly created RANGE_FOR_STMT; if not, it is converted to a
13896 regular FOR_STMT. */
13898 static tree
13899 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13900 bool ivdep, unsigned short unroll, bool novector,
13901 bool is_omp)
13903 tree stmt, range_expr;
13904 auto_vec <cxx_binding *, 16> bindings;
13905 auto_vec <tree, 16> names;
13906 cp_decomp decomp_d, *decomp = NULL;
13908 /* Get the range declaration momentarily out of the way so that
13909 the range expression doesn't clash with it. */
13910 if (range_decl != error_mark_node)
13912 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13914 tree v = DECL_VALUE_EXPR (range_decl);
13915 /* For decomposition declaration get all of the corresponding
13916 declarations out of the way. */
13917 if (TREE_CODE (v) == ARRAY_REF
13918 && VAR_P (TREE_OPERAND (v, 0))
13919 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13921 tree d = range_decl;
13922 range_decl = TREE_OPERAND (v, 0);
13923 decomp = &decomp_d;
13924 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13925 decomp->decl = d;
13926 for (unsigned int i = 0; i < decomp->count;
13927 i++, d = DECL_CHAIN (d))
13929 tree name = DECL_NAME (d);
13930 names.safe_push (name);
13931 bindings.safe_push (IDENTIFIER_BINDING (name));
13932 IDENTIFIER_BINDING (name)
13933 = IDENTIFIER_BINDING (name)->previous;
13937 if (names.is_empty ())
13939 tree name = DECL_NAME (range_decl);
13940 names.safe_push (name);
13941 bindings.safe_push (IDENTIFIER_BINDING (name));
13942 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13947 range_expr = cp_parser_braced_list (parser);
13948 else
13949 range_expr = cp_parser_expression (parser);
13951 /* Put the range declaration(s) back into scope. */
13952 for (unsigned int i = 0; i < names.length (); i++)
13954 cxx_binding *binding = bindings[i];
13955 binding->previous = IDENTIFIER_BINDING (names[i]);
13956 IDENTIFIER_BINDING (names[i]) = binding;
13959 /* finish_omp_for has its own code for the following, so just
13960 return the range_expr instead. */
13961 if (is_omp)
13962 return range_expr;
13964 /* If in template, STMT is converted to a normal for-statement
13965 at instantiation. If not, it is done just ahead. */
13966 if (processing_template_decl)
13968 if (check_for_bare_parameter_packs (range_expr))
13969 range_expr = error_mark_node;
13970 stmt = begin_range_for_stmt (scope, init);
13971 if (ivdep)
13972 RANGE_FOR_IVDEP (stmt) = 1;
13973 if (unroll)
13974 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13975 if (novector)
13976 RANGE_FOR_NOVECTOR (stmt) = 1;
13977 finish_range_for_decl (stmt, range_decl, range_expr);
13978 if (!type_dependent_expression_p (range_expr)
13979 /* do_auto_deduction doesn't mess with template init-lists. */
13980 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13981 do_range_for_auto_deduction (range_decl, range_expr, decomp);
13983 else
13985 stmt = begin_for_stmt (scope, init);
13986 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
13987 ivdep, unroll, novector);
13989 return stmt;
13992 /* Subroutine of cp_convert_range_for: given the initializer expression,
13993 builds up the range temporary. */
13995 static tree
13996 build_range_temp (tree range_expr)
13998 /* Find out the type deduced by the declaration
13999 `auto &&__range = range_expr'. */
14000 tree auto_node = make_auto ();
14001 tree range_type = cp_build_reference_type (auto_node, true);
14002 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14004 /* Create the __range variable. */
14005 tree range_temp = build_decl (input_location, VAR_DECL,
14006 for_range__identifier, range_type);
14007 TREE_USED (range_temp) = 1;
14008 DECL_ARTIFICIAL (range_temp) = 1;
14010 return range_temp;
14013 /* Used by cp_parser_range_for in template context: we aren't going to
14014 do a full conversion yet, but we still need to resolve auto in the
14015 type of the for-range-declaration if present. This is basically
14016 a shortcut version of cp_convert_range_for. */
14018 static void
14019 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14021 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14022 if (auto_node)
14024 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14025 range_temp = convert_from_reference (build_range_temp (range_expr));
14026 iter_type = (cp_parser_perform_range_for_lookup
14027 (range_temp, &begin_dummy, &end_dummy));
14028 if (iter_type)
14030 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14031 iter_type);
14032 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14033 RO_UNARY_STAR, NULL_TREE,
14034 tf_warning_or_error);
14035 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14036 iter_decl, auto_node,
14037 tf_warning_or_error,
14038 adc_variable_type);
14039 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
14040 cp_finish_decomp (decl, decomp);
14045 /* Warns when the loop variable should be changed to a reference type to
14046 avoid unnecessary copying. I.e., from
14048 for (const auto x : range)
14050 where range returns a reference, to
14052 for (const auto &x : range)
14054 if this version doesn't make a copy.
14056 This function also warns when the loop variable is initialized with
14057 a value of a different type resulting in a copy:
14059 int arr[10];
14060 for (const double &x : arr)
14062 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14063 This function is never called when processing_template_decl is on. */
14065 static void
14066 warn_for_range_copy (tree decl, tree expr)
14068 if (!warn_range_loop_construct
14069 || decl == error_mark_node)
14070 return;
14072 location_t loc = DECL_SOURCE_LOCATION (decl);
14073 tree type = TREE_TYPE (decl);
14075 if (from_macro_expansion_at (loc))
14076 return;
14078 if (TYPE_REF_P (type))
14080 if (glvalue_p (expr)
14081 && ref_conv_binds_to_temporary (type, expr).is_true ())
14083 auto_diagnostic_group d;
14084 if (warning_at (loc, OPT_Wrange_loop_construct,
14085 "loop variable %qD of type %qT binds to a temporary "
14086 "constructed from type %qT", decl, type,
14087 TREE_TYPE (expr)))
14089 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14090 TYPE_QUAL_CONST);
14091 ref = cp_build_reference_type (ref, /*rval*/false);
14092 inform (loc, "use non-reference type %qT to make the copy "
14093 "explicit or %qT to prevent copying",
14094 non_reference (type), ref);
14097 return;
14099 else if (!CP_TYPE_CONST_P (type))
14100 return;
14102 /* Since small trivially copyable types are cheap to copy, we suppress the
14103 warning for them. 64B is a common size of a cache line. */
14104 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14105 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14106 && trivially_copyable_p (type)))
14107 return;
14109 /* If we can initialize a reference directly, suggest that to avoid the
14110 copy. */
14111 tree rtype = cp_build_reference_type (type, /*rval*/false);
14112 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14114 auto_diagnostic_group d;
14115 if (warning_at (loc, OPT_Wrange_loop_construct,
14116 "loop variable %qD creates a copy from type %qT",
14117 decl, type))
14119 gcc_rich_location richloc (loc);
14120 richloc.add_fixit_insert_before ("&");
14121 inform (&richloc, "use reference type to prevent copying");
14126 /* Converts a range-based for-statement into a normal
14127 for-statement, as per the definition.
14129 for (RANGE_DECL : RANGE_EXPR)
14130 BLOCK
14132 should be equivalent to:
14135 auto &&__range = RANGE_EXPR;
14136 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14137 __begin != __end;
14138 ++__begin)
14140 RANGE_DECL = *__begin;
14141 BLOCK
14145 If RANGE_EXPR is an array:
14146 BEGIN_EXPR = __range
14147 END_EXPR = __range + ARRAY_SIZE(__range)
14148 Else if RANGE_EXPR has a member 'begin' or 'end':
14149 BEGIN_EXPR = __range.begin()
14150 END_EXPR = __range.end()
14151 Else:
14152 BEGIN_EXPR = begin(__range)
14153 END_EXPR = end(__range);
14155 If __range has a member 'begin' but not 'end', or vice versa, we must
14156 still use the second alternative (it will surely fail, however).
14157 When calling begin()/end() in the third alternative we must use
14158 argument dependent lookup, but always considering 'std' as an associated
14159 namespace. */
14161 tree
14162 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14163 cp_decomp *decomp, bool ivdep, unsigned short unroll,
14164 bool novector)
14166 tree begin, end;
14167 tree iter_type, begin_expr, end_expr;
14168 tree condition, expression;
14170 range_expr = mark_lvalue_use (range_expr);
14172 if (range_decl == error_mark_node || range_expr == error_mark_node)
14173 /* If an error happened previously do nothing or else a lot of
14174 unhelpful errors would be issued. */
14175 begin_expr = end_expr = iter_type = error_mark_node;
14176 else
14178 tree range_temp;
14180 if (VAR_P (range_expr)
14181 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14182 /* Can't bind a reference to an array of runtime bound. */
14183 range_temp = range_expr;
14184 else
14186 range_temp = build_range_temp (range_expr);
14187 pushdecl (range_temp);
14188 cp_finish_decl (range_temp, range_expr,
14189 /*is_constant_init*/false, NULL_TREE,
14190 LOOKUP_ONLYCONVERTING);
14191 range_temp = convert_from_reference (range_temp);
14193 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14194 &begin_expr, &end_expr);
14197 /* The new for initialization statement. */
14198 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14199 iter_type);
14200 TREE_USED (begin) = 1;
14201 DECL_ARTIFICIAL (begin) = 1;
14202 pushdecl (begin);
14203 cp_finish_decl (begin, begin_expr,
14204 /*is_constant_init*/false, NULL_TREE,
14205 LOOKUP_ONLYCONVERTING);
14207 if (cxx_dialect >= cxx17)
14208 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14209 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14210 TREE_USED (end) = 1;
14211 DECL_ARTIFICIAL (end) = 1;
14212 pushdecl (end);
14213 cp_finish_decl (end, end_expr,
14214 /*is_constant_init*/false, NULL_TREE,
14215 LOOKUP_ONLYCONVERTING);
14217 finish_init_stmt (statement);
14219 /* The new for condition. */
14220 condition = build_x_binary_op (input_location, NE_EXPR,
14221 begin, ERROR_MARK,
14222 end, ERROR_MARK,
14223 NULL_TREE, NULL, tf_warning_or_error);
14224 finish_for_cond (condition, statement, ivdep, unroll, novector);
14226 /* The new increment expression. */
14227 expression = finish_unary_op_expr (input_location,
14228 PREINCREMENT_EXPR, begin,
14229 tf_warning_or_error);
14230 finish_for_expr (expression, statement);
14232 /* The declaration is initialized with *__begin inside the loop body. */
14233 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14234 NULL_TREE, tf_warning_or_error);
14235 cp_finish_decl (range_decl, deref_begin,
14236 /*is_constant_init*/false, NULL_TREE,
14237 LOOKUP_ONLYCONVERTING, decomp);
14238 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14239 cp_finish_decomp (range_decl, decomp);
14241 warn_for_range_copy (range_decl, deref_begin);
14243 return statement;
14246 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14247 We need to solve both at the same time because the method used
14248 depends on the existence of members begin or end.
14249 Returns the type deduced for the iterator expression. */
14251 static tree
14252 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14254 if (error_operand_p (range))
14256 *begin = *end = error_mark_node;
14257 return error_mark_node;
14260 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14262 error ("range-based %<for%> expression of type %qT "
14263 "has incomplete type", TREE_TYPE (range));
14264 *begin = *end = error_mark_node;
14265 return error_mark_node;
14267 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14269 /* If RANGE is an array, we will use pointer arithmetic. */
14270 *begin = decay_conversion (range, tf_warning_or_error);
14271 *end = build_binary_op (input_location, PLUS_EXPR,
14272 range,
14273 array_type_nelts_top (TREE_TYPE (range)),
14274 false);
14275 return TREE_TYPE (*begin);
14277 else
14279 /* If it is not an array, we must do a bit of magic. */
14280 tree id_begin, id_end;
14281 tree member_begin, member_end;
14283 *begin = *end = error_mark_node;
14285 id_begin = get_identifier ("begin");
14286 id_end = get_identifier ("end");
14287 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14288 /*protect=*/2, /*want_type=*/false,
14289 tf_warning_or_error);
14290 member_end = lookup_member (TREE_TYPE (range), id_end,
14291 /*protect=*/2, /*want_type=*/false,
14292 tf_warning_or_error);
14294 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14296 /* Use the member functions. */
14297 *begin = cp_parser_range_for_member_function (range, id_begin);
14298 *end = cp_parser_range_for_member_function (range, id_end);
14300 else
14302 /* Use global functions with ADL. */
14303 releasing_vec vec;
14305 vec_safe_push (vec, range);
14307 member_begin = perform_koenig_lookup (id_begin, vec,
14308 tf_warning_or_error);
14309 *begin = finish_call_expr (member_begin, &vec, false, true,
14310 tf_warning_or_error);
14311 member_end = perform_koenig_lookup (id_end, vec,
14312 tf_warning_or_error);
14313 *end = finish_call_expr (member_end, &vec, false, true,
14314 tf_warning_or_error);
14317 /* Last common checks. */
14318 if (*begin == error_mark_node || *end == error_mark_node)
14320 /* If one of the expressions is an error do no more checks. */
14321 *begin = *end = error_mark_node;
14322 return error_mark_node;
14324 else if (type_dependent_expression_p (*begin)
14325 || type_dependent_expression_p (*end))
14326 /* Can happen, when, eg, in a template context, Koenig lookup
14327 can't resolve begin/end (c++/58503). */
14328 return NULL_TREE;
14329 else
14331 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14332 /* The unqualified type of the __begin and __end temporaries should
14333 be the same, as required by the multiple auto declaration. */
14334 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14336 if (cxx_dialect >= cxx17
14337 && (build_x_binary_op (input_location, NE_EXPR,
14338 *begin, ERROR_MARK,
14339 *end, ERROR_MARK,
14340 NULL_TREE, NULL, tf_none)
14341 != error_mark_node))
14342 /* P0184R0 allows __begin and __end to have different types,
14343 but make sure they are comparable so we can give a better
14344 diagnostic. */;
14345 else
14346 error ("inconsistent begin/end types in range-based %<for%> "
14347 "statement: %qT and %qT",
14348 TREE_TYPE (*begin), TREE_TYPE (*end));
14350 return iter_type;
14355 /* Helper function for cp_parser_perform_range_for_lookup.
14356 Builds a tree for RANGE.IDENTIFIER(). */
14358 static tree
14359 cp_parser_range_for_member_function (tree range, tree identifier)
14361 tree member, res;
14363 member = finish_class_member_access_expr (range, identifier,
14364 false, tf_warning_or_error);
14365 if (member == error_mark_node)
14366 return error_mark_node;
14368 releasing_vec vec;
14369 res = finish_call_expr (member, &vec,
14370 /*disallow_virtual=*/false,
14371 /*koenig_p=*/false,
14372 tf_warning_or_error);
14373 return res;
14376 /* Parse an iteration-statement.
14378 iteration-statement:
14379 while ( condition ) statement
14380 do statement while ( expression ) ;
14381 for ( init-statement condition [opt] ; expression [opt] )
14382 statement
14384 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14386 static tree
14387 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14388 unsigned short unroll, bool novector)
14390 cp_token *token;
14391 enum rid keyword;
14392 tree statement;
14393 unsigned char in_statement;
14394 token_indent_info guard_tinfo;
14396 /* Peek at the next token. */
14397 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14398 if (!token)
14399 return error_mark_node;
14401 guard_tinfo = get_token_indent_info (token);
14403 /* Remember whether or not we are already within an iteration
14404 statement. */
14405 in_statement = parser->in_statement;
14407 /* Special case for OMP loop intervening code. Parsing of permitted
14408 collapsed loop nests is handled elsewhere. */
14409 if (parser->omp_for_parse_state)
14411 error_at (token->location,
14412 "loop not permitted in intervening code in OpenMP loop body");
14413 parser->omp_for_parse_state->fail = true;
14416 /* See what kind of keyword it is. */
14417 keyword = token->keyword;
14418 switch (keyword)
14420 case RID_WHILE:
14422 tree condition;
14424 /* Begin the while-statement. */
14425 statement = begin_while_stmt ();
14426 /* Look for the `('. */
14427 matching_parens parens;
14428 parens.require_open (parser);
14429 /* Parse the condition. */
14430 condition = cp_parser_condition (parser);
14431 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14432 /* Look for the `)'. */
14433 parens.require_close (parser);
14434 /* Parse the dependent statement. */
14435 parser->in_statement = IN_ITERATION_STMT;
14436 bool prev = note_iteration_stmt_body_start ();
14437 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14438 note_iteration_stmt_body_end (prev);
14439 parser->in_statement = in_statement;
14440 /* We're done with the while-statement. */
14441 finish_while_stmt (statement);
14443 break;
14445 case RID_DO:
14447 tree expression;
14449 /* Begin the do-statement. */
14450 statement = begin_do_stmt ();
14451 /* Parse the body of the do-statement. */
14452 parser->in_statement = IN_ITERATION_STMT;
14453 bool prev = note_iteration_stmt_body_start ();
14454 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14455 note_iteration_stmt_body_end (prev);
14456 parser->in_statement = in_statement;
14457 finish_do_body (statement);
14458 /* Look for the `while' keyword. */
14459 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14460 /* Look for the `('. */
14461 matching_parens parens;
14462 parens.require_open (parser);
14463 /* Parse the expression. */
14464 expression = cp_parser_expression (parser);
14465 /* We're done with the do-statement. */
14466 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14467 /* Look for the `)'. */
14468 parens.require_close (parser);
14469 /* Look for the `;'. */
14470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14472 break;
14474 case RID_FOR:
14476 /* Look for the `('. */
14477 matching_parens parens;
14478 parens.require_open (parser);
14480 statement = cp_parser_for (parser, ivdep, unroll, novector);
14482 /* Look for the `)'. */
14483 parens.require_close (parser);
14485 /* Parse the body of the for-statement. */
14486 parser->in_statement = IN_ITERATION_STMT;
14487 bool prev = note_iteration_stmt_body_start ();
14488 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14489 note_iteration_stmt_body_end (prev);
14490 parser->in_statement = in_statement;
14492 /* We're done with the for-statement. */
14493 finish_for_stmt (statement);
14495 break;
14497 default:
14498 cp_parser_error (parser, "expected iteration-statement");
14499 statement = error_mark_node;
14500 break;
14503 return statement;
14506 /* Parse an init-statement or the declarator of a range-based-for.
14507 Returns true if a range-based-for declaration is seen.
14509 init-statement:
14510 expression-statement
14511 simple-declaration
14512 alias-declaration */
14514 static bool
14515 cp_parser_init_statement (cp_parser *parser, tree *decl)
14517 /* If the next token is a `;', then we have an empty
14518 expression-statement. Grammatically, this is also a
14519 simple-declaration, but an invalid one, because it does not
14520 declare anything. Therefore, if we did not handle this case
14521 specially, we would issue an error message about an invalid
14522 declaration. */
14523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14525 bool is_range_for = false;
14526 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14528 /* A colon is used in range-based for. */
14529 parser->colon_corrects_to_scope_p = false;
14531 /* We're going to speculatively look for a declaration, falling back
14532 to an expression, if necessary. */
14533 cp_parser_parse_tentatively (parser);
14534 bool expect_semicolon_p = true;
14535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14537 cp_parser_alias_declaration (parser);
14538 expect_semicolon_p = false;
14539 if (cxx_dialect < cxx23
14540 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14541 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14542 OPT_Wc__23_extensions,
14543 "alias-declaration in init-statement only "
14544 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14546 else
14547 /* Parse the declaration. */
14548 cp_parser_simple_declaration (parser,
14549 /*function_definition_allowed_p=*/false,
14550 decl);
14551 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14552 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14554 /* It is a range-for, consume the ':'. */
14555 cp_lexer_consume_token (parser->lexer);
14556 is_range_for = true;
14557 if (cxx_dialect < cxx11)
14558 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14559 OPT_Wc__11_extensions,
14560 "range-based %<for%> loops only available with "
14561 "%<-std=c++11%> or %<-std=gnu++11%>");
14563 else if (expect_semicolon_p)
14564 /* The ';' is not consumed yet because we told
14565 cp_parser_simple_declaration not to. */
14566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14568 if (cp_parser_parse_definitely (parser))
14569 return is_range_for;
14570 /* If the tentative parse failed, then we shall need to look for an
14571 expression-statement. */
14573 /* If we are here, it is an expression-statement. */
14574 cp_parser_expression_statement (parser, NULL_TREE);
14575 return false;
14578 /* Parse a jump-statement.
14580 jump-statement:
14581 break ;
14582 continue ;
14583 return expression [opt] ;
14584 return braced-init-list ;
14585 coroutine-return-statement;
14586 goto identifier ;
14588 GNU extension:
14590 jump-statement:
14591 goto * expression ;
14593 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14595 static tree
14596 cp_parser_jump_statement (cp_parser* parser)
14598 tree statement = error_mark_node;
14599 cp_token *token;
14600 enum rid keyword;
14601 unsigned char in_statement;
14603 /* Peek at the next token. */
14604 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14605 if (!token)
14606 return error_mark_node;
14608 /* See what kind of keyword it is. */
14609 keyword = token->keyword;
14610 switch (keyword)
14612 case RID_BREAK:
14613 in_statement = parser->in_statement & ~IN_IF_STMT;
14614 switch (in_statement)
14616 case 0:
14617 error_at (token->location, "break statement not within loop or switch");
14618 break;
14619 default:
14620 gcc_assert ((in_statement & IN_SWITCH_STMT)
14621 || in_statement == IN_ITERATION_STMT);
14622 statement = finish_break_stmt ();
14623 if (in_statement == IN_ITERATION_STMT)
14624 break_maybe_infinite_loop ();
14625 break;
14626 case IN_OMP_BLOCK:
14627 error_at (token->location, "invalid exit from OpenMP structured block");
14628 break;
14629 case IN_OMP_FOR:
14630 error_at (token->location, "break statement used with OpenMP for loop");
14631 break;
14633 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14634 break;
14636 case RID_CONTINUE:
14637 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14639 case 0:
14640 error_at (token->location, "continue statement not within a loop");
14641 break;
14642 /* Fall through. */
14643 case IN_ITERATION_STMT:
14644 case IN_OMP_FOR:
14645 statement = finish_continue_stmt ();
14646 break;
14647 case IN_OMP_BLOCK:
14648 error_at (token->location, "invalid exit from OpenMP structured block");
14649 break;
14650 default:
14651 gcc_unreachable ();
14653 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14654 break;
14656 case RID_CO_RETURN:
14657 case RID_RETURN:
14659 tree expr;
14661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14663 cp_lexer_set_source_position (parser->lexer);
14664 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14665 expr = cp_parser_braced_list (parser);
14667 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14668 expr = cp_parser_expression (parser);
14669 else
14670 /* If the next token is a `;', then there is no
14671 expression. */
14672 expr = NULL_TREE;
14673 /* Build the return-statement, check co-return first, since type
14674 deduction is not valid there. */
14675 if (keyword == RID_CO_RETURN)
14676 statement = finish_co_return_stmt (token->location, expr);
14677 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14678 /* Don't deduce from a discarded return statement. */;
14679 else
14680 statement = finish_return_stmt (expr);
14681 /* Look for the final `;'. */
14682 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14684 break;
14686 case RID_GOTO:
14687 if (parser->in_function_body
14688 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14689 && cxx_dialect < cxx23)
14691 error ("%<goto%> in %<constexpr%> function only available with "
14692 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14693 cp_function_chain->invalid_constexpr = true;
14696 /* Create the goto-statement. */
14697 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14699 /* Issue a warning about this use of a GNU extension. */
14700 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14701 /* Consume the '*' token. */
14702 cp_lexer_consume_token (parser->lexer);
14703 /* Parse the dependent expression. */
14704 finish_goto_stmt (cp_parser_expression (parser));
14706 else
14707 finish_goto_stmt (cp_parser_identifier (parser));
14708 /* Look for the final `;'. */
14709 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14710 break;
14712 default:
14713 cp_parser_error (parser, "expected jump-statement");
14714 break;
14717 return statement;
14720 /* Parse a declaration-statement.
14722 declaration-statement:
14723 block-declaration */
14725 static void
14726 cp_parser_declaration_statement (cp_parser* parser)
14728 void *p;
14730 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14731 p = obstack_alloc (&declarator_obstack, 0);
14733 /* Parse the block-declaration. */
14734 cp_parser_block_declaration (parser, /*statement_p=*/true);
14736 /* Free any declarators allocated. */
14737 obstack_free (&declarator_obstack, p);
14740 /* Some dependent statements (like `if (cond) statement'), are
14741 implicitly in their own scope. In other words, if the statement is
14742 a single statement (as opposed to a compound-statement), it is
14743 none-the-less treated as if it were enclosed in braces. Any
14744 declarations appearing in the dependent statement are out of scope
14745 after control passes that point. This function parses a statement,
14746 but ensures that is in its own scope, even if it is not a
14747 compound-statement.
14749 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14750 is a (possibly labeled) if statement which is not enclosed in
14751 braces and has an else clause. This is used to implement
14752 -Wparentheses.
14754 CHAIN is a vector of if-else-if conditions. This is used to implement
14755 -Wduplicated-cond.
14757 Returns the new statement. */
14759 static tree
14760 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14761 const token_indent_info &guard_tinfo,
14762 vec<tree> *chain)
14764 tree statement;
14765 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14766 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14767 token_indent_info body_tinfo
14768 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14770 if (if_p != NULL)
14771 *if_p = false;
14773 /* Mark if () ; with a special NOP_EXPR. */
14774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14776 cp_lexer_consume_token (parser->lexer);
14777 statement = add_stmt (build_empty_stmt (body_loc));
14779 if (guard_tinfo.keyword == RID_IF
14780 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14781 warning_at (body_loc, OPT_Wempty_body,
14782 "suggest braces around empty body in an %<if%> statement");
14783 else if (guard_tinfo.keyword == RID_ELSE)
14784 warning_at (body_loc, OPT_Wempty_body,
14785 "suggest braces around empty body in an %<else%> statement");
14787 /* if a compound is opened, we simply parse the statement directly. */
14788 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14789 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14790 /* If the token is not a `{', then we must take special action. */
14791 else
14793 /* Create a compound-statement. */
14794 statement = begin_compound_stmt (0);
14795 /* Parse the dependent-statement. */
14796 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14797 &body_loc_after_labels);
14798 /* Finish the dummy compound-statement. */
14799 finish_compound_stmt (statement);
14802 token_indent_info next_tinfo
14803 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14804 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14806 if (body_loc_after_labels != UNKNOWN_LOCATION
14807 && next_tinfo.type != CPP_SEMICOLON)
14808 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14809 guard_tinfo.location, guard_tinfo.keyword);
14811 /* Return the statement. */
14812 return statement;
14815 /* For some dependent statements (like `while (cond) statement'), we
14816 have already created a scope. Therefore, even if the dependent
14817 statement is a compound-statement, we do not want to create another
14818 scope. */
14820 static void
14821 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14822 const token_indent_info &guard_tinfo)
14824 /* If the token is a `{', then we must take special action. */
14825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14827 token_indent_info body_tinfo
14828 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14829 location_t loc_after_labels = UNKNOWN_LOCATION;
14831 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14832 &loc_after_labels);
14833 token_indent_info next_tinfo
14834 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14835 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14837 if (loc_after_labels != UNKNOWN_LOCATION
14838 && next_tinfo.type != CPP_SEMICOLON)
14839 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14840 guard_tinfo.location,
14841 guard_tinfo.keyword);
14843 else
14845 /* Avoid calling cp_parser_compound_statement, so that we
14846 don't create a new scope. Do everything else by hand. */
14847 matching_braces braces;
14848 braces.require_open (parser);
14849 /* If the next keyword is `__label__' we have a label declaration. */
14850 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14851 cp_parser_label_declaration (parser);
14852 /* Parse an (optional) statement-seq. */
14853 cp_parser_statement_seq_opt (parser, NULL_TREE);
14854 braces.require_close (parser);
14858 /* Modules */
14860 /* Parse a module-name,
14861 identifier
14862 module-name . identifier
14863 header-name
14865 Returns a pointer to module object, NULL. */
14867 static module_state *
14868 cp_parser_module_name (cp_parser *parser)
14870 cp_token *token = cp_lexer_peek_token (parser->lexer);
14871 if (token->type == CPP_HEADER_NAME)
14873 cp_lexer_consume_token (parser->lexer);
14875 return get_module (token->u.value);
14878 module_state *parent = NULL;
14879 bool partitioned = false;
14880 if (token->type == CPP_COLON && named_module_p ())
14882 partitioned = true;
14883 cp_lexer_consume_token (parser->lexer);
14886 for (;;)
14888 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14890 cp_parser_error (parser, "expected module-name");
14891 break;
14894 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14895 parent = get_module (name, parent, partitioned);
14896 token = cp_lexer_peek_token (parser->lexer);
14897 if (!partitioned && token->type == CPP_COLON)
14898 partitioned = true;
14899 else if (token->type != CPP_DOT)
14900 break;
14902 cp_lexer_consume_token (parser->lexer);
14905 return parent;
14908 /* Named module-declaration
14909 __module ; PRAGMA_EOL
14910 __module private ; PRAGMA_EOL (unimplemented)
14911 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14914 static module_parse
14915 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14916 bool exporting)
14918 /* We're a pseudo pragma. */
14919 parser->lexer->in_pragma = true;
14920 cp_token *token = cp_lexer_consume_token (parser->lexer);
14922 if (flag_header_unit)
14924 error_at (token->location,
14925 "module-declaration not permitted in header-unit");
14926 goto skip_eol;
14928 else if (mp_state == MP_FIRST && !exporting
14929 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14931 /* Start global module fragment. */
14932 cp_lexer_consume_token (parser->lexer);
14933 module_kind = MK_NAMED;
14934 mp_state = MP_GLOBAL;
14935 cp_parser_require_pragma_eol (parser, token);
14937 else if (!exporting
14938 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14939 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14940 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14942 cp_lexer_consume_token (parser->lexer);
14943 cp_lexer_consume_token (parser->lexer);
14944 cp_lexer_consume_token (parser->lexer);
14945 cp_parser_require_pragma_eol (parser, token);
14947 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14948 && module_has_cmi_p ())
14950 mp_state = MP_PRIVATE_IMPORTS;
14951 sorry_at (token->location, "private module fragment");
14953 else
14954 error_at (token->location,
14955 "private module fragment only permitted in purview"
14956 " of module interface or partition");
14958 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14960 /* Neither the first declaration, nor in a GMF. */
14961 error_at (token->location, "module-declaration only permitted as first"
14962 " declaration, or ending a global module fragment");
14963 skip_eol:
14964 cp_parser_skip_to_pragma_eol (parser, token);
14966 else
14968 module_state *mod = cp_parser_module_name (parser);
14969 tree attrs = cp_parser_attributes_opt (parser);
14971 mp_state = MP_PURVIEW_IMPORTS;
14972 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14973 goto skip_eol;
14975 declare_module (mod, token->location, exporting, attrs, parse_in);
14976 cp_parser_require_pragma_eol (parser, token);
14979 return mp_state;
14982 /* Import-declaration
14983 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14985 static void
14986 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14987 bool exporting)
14989 /* We're a pseudo pragma. */
14990 parser->lexer->in_pragma = true;
14991 cp_token *token = cp_lexer_consume_token (parser->lexer);
14993 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14995 error_at (token->location, "post-module-declaration"
14996 " imports must be contiguous");
14997 note_lexer:
14998 inform (token->location, "perhaps insert a line break, or other"
14999 " disambiguation, to prevent this being considered a"
15000 " module control-line");
15001 skip_eol:
15002 cp_parser_skip_to_pragma_eol (parser, token);
15004 else if (current_scope () != global_namespace)
15006 error_at (token->location, "import-declaration must be at global scope");
15007 goto note_lexer;
15009 else
15011 module_state *mod = cp_parser_module_name (parser);
15012 tree attrs = cp_parser_attributes_opt (parser);
15014 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15015 goto skip_eol;
15016 cp_parser_require_pragma_eol (parser, token);
15018 if (parser->in_unbraced_linkage_specification_p)
15019 error_at (token->location, "import cannot appear directly in"
15020 " a linkage-specification");
15022 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15024 /* Module-purview imports must not be from source inclusion
15025 [cpp.import]/7 */
15026 if (attrs
15027 && private_lookup_attribute ("__translated",
15028 strlen ("__translated"), attrs))
15029 error_at (token->location, "post-module-declaration imports"
15030 " must not be include-translated");
15031 else if (!token->main_source_p)
15032 error_at (token->location, "post-module-declaration imports"
15033 " must not be from header inclusion");
15036 import_module (mod, token->location, exporting, attrs, parse_in);
15040 /* export-declaration.
15042 export declaration
15043 export { declaration-seq-opt } */
15045 static void
15046 cp_parser_module_export (cp_parser *parser)
15048 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15049 cp_token *token = cp_lexer_consume_token (parser->lexer);
15051 if (!module_interface_p ())
15052 error_at (token->location,
15053 "%qE may only occur after a module interface declaration",
15054 token->u.value);
15056 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15058 unsigned mk = module_kind;
15059 if (module_exporting_p ())
15060 error_at (token->location,
15061 "%qE may only occur once in an export declaration",
15062 token->u.value);
15063 module_kind |= MK_EXPORTING;
15065 if (braced)
15067 cp_ensure_no_omp_declare_simd (parser);
15068 cp_ensure_no_oacc_routine (parser);
15070 cp_lexer_consume_token (parser->lexer);
15071 cp_parser_declaration_seq_opt (parser);
15072 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15074 else
15076 /* Explicitly check if the next tokens might be a
15077 module-directive line, so we can give a clearer error message
15078 about why the directive will be rejected. */
15079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15080 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15081 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15082 error_at (token->location, "%<export%> not part of following"
15083 " module-directive");
15084 cp_parser_declaration (parser, NULL_TREE);
15087 module_kind = mk;
15090 /* Declarations [gram.dcl.dcl] */
15092 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15093 is the top-level declaration sequence. That affects whether we
15094 deal with module-preamble.
15096 declaration-seq:
15097 declaration
15098 declaration-seq declaration */
15100 static void
15101 cp_parser_declaration_seq_opt (cp_parser* parser)
15103 while (true)
15105 cp_token *token = cp_lexer_peek_token (parser->lexer);
15107 if (token->type == CPP_CLOSE_BRACE
15108 || token->type == CPP_EOF)
15109 break;
15110 else
15111 cp_parser_toplevel_declaration (parser);
15115 /* Parse a declaration.
15117 declaration:
15118 block-declaration
15119 function-definition
15120 template-declaration
15121 explicit-instantiation
15122 explicit-specialization
15123 linkage-specification
15124 namespace-definition
15126 C++17:
15127 deduction-guide
15129 modules:
15130 (all these are only allowed at the outermost level, check
15131 that semantically, for better diagnostics)
15132 module-declaration
15133 module-export-declaration
15134 module-import-declaration
15135 export-declaration
15137 GNU extension:
15139 declaration:
15140 __extension__ declaration */
15142 static void
15143 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15145 int saved_pedantic;
15147 /* Check for the `__extension__' keyword. */
15148 if (cp_parser_extension_opt (parser, &saved_pedantic))
15150 /* Parse the qualified declaration. */
15151 cp_parser_declaration (parser, prefix_attrs);
15152 /* Restore the PEDANTIC flag. */
15153 pedantic = saved_pedantic;
15155 return;
15158 /* Try to figure out what kind of declaration is present. */
15159 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15160 cp_token *token2 = (token1->type == CPP_EOF
15161 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15163 if (token1->type == CPP_SEMICOLON)
15165 cp_lexer_consume_token (parser->lexer);
15166 /* A declaration consisting of a single semicolon is invalid
15167 * before C++11. Allow it unless we're being pedantic. */
15168 if (cxx_dialect < cxx11)
15169 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15170 return;
15172 else if (cp_lexer_nth_token_is (parser->lexer,
15173 cp_parser_skip_std_attribute_spec_seq (parser,
15175 CPP_SEMICOLON))
15177 location_t attrs_loc = token1->location;
15178 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15180 if (std_attrs && (flag_openmp || flag_openmp_simd))
15182 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15183 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15184 std_attrs);
15185 if (parser->lexer->in_omp_attribute_pragma)
15187 cp_lexer *lexer = parser->lexer;
15188 while (parser->lexer->in_omp_attribute_pragma)
15190 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15191 CPP_PRAGMA));
15192 cp_parser_pragma (parser, pragma_external, NULL);
15194 cp_lexer_destroy (lexer);
15198 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
15199 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15200 OPT_Wattributes, "attribute ignored");
15201 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15202 cp_lexer_consume_token (parser->lexer);
15203 return;
15206 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15207 void *p = obstack_alloc (&declarator_obstack, 0);
15209 tree attributes = NULL_TREE;
15211 /* Conditionally, allow attributes to precede a linkage specification. */
15212 if (token1->keyword == RID_ATTRIBUTE)
15214 cp_lexer_save_tokens (parser->lexer);
15215 attributes = cp_parser_attributes_opt (parser);
15216 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15217 cp_token *t2 = (t1->type == CPP_EOF
15218 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15219 if (t1->keyword == RID_EXTERN
15220 && cp_parser_is_pure_string_literal (t2))
15222 cp_lexer_commit_tokens (parser->lexer);
15223 /* We might have already been here. */
15224 if (!c_dialect_objc ())
15226 location_t where = get_finish (t2->location);
15227 warning_at (token1->location, OPT_Wattributes, "attributes are"
15228 " not permitted in this position");
15229 where = linemap_position_for_loc_and_offset (line_table,
15230 where, 1);
15231 inform (where, "attributes may be inserted here");
15232 attributes = NULL_TREE;
15234 token1 = t1;
15235 token2 = t2;
15237 else
15239 cp_lexer_rollback_tokens (parser->lexer);
15240 attributes = NULL_TREE;
15243 /* If we already had some attributes, and we've added more, then prepend.
15244 Otherwise attributes just contains any that we just read. */
15245 if (prefix_attrs)
15247 if (attributes)
15248 TREE_CHAIN (prefix_attrs) = attributes;
15249 attributes = prefix_attrs;
15252 /* If the next token is `extern' and the following token is a string
15253 literal, then we have a linkage specification. */
15254 if (token1->keyword == RID_EXTERN
15255 && cp_parser_is_pure_string_literal (token2))
15256 cp_parser_linkage_specification (parser, attributes);
15257 /* If the next token is `template', then we have either a template
15258 declaration, an explicit instantiation, or an explicit
15259 specialization. */
15260 else if (token1->keyword == RID_TEMPLATE)
15262 /* `template <>' indicates a template specialization. */
15263 if (token2->type == CPP_LESS
15264 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15265 cp_parser_explicit_specialization (parser);
15266 /* `template <' indicates a template declaration. */
15267 else if (token2->type == CPP_LESS)
15268 cp_parser_template_declaration (parser, /*member_p=*/false);
15269 /* Anything else must be an explicit instantiation. */
15270 else
15271 cp_parser_explicit_instantiation (parser);
15273 /* If the next token is `export', it's new-style modules or
15274 old-style template. */
15275 else if (token1->keyword == RID_EXPORT)
15277 if (!modules_p ())
15278 cp_parser_template_declaration (parser, /*member_p=*/false);
15279 else
15280 cp_parser_module_export (parser);
15282 else if (cp_token_is_module_directive (token1))
15284 bool exporting = token1->keyword == RID__EXPORT;
15285 cp_token *next = exporting ? token2 : token1;
15286 if (exporting)
15287 cp_lexer_consume_token (parser->lexer);
15288 // In module purview this will be ill-formed.
15289 auto state = (!named_module_p () ? MP_NOT_MODULE
15290 : module_purview_p () ? MP_PURVIEW
15291 : MP_GLOBAL);
15292 if (next->keyword == RID__MODULE)
15293 cp_parser_module_declaration (parser, state, exporting);
15294 else
15295 cp_parser_import_declaration (parser, state, exporting);
15297 /* If the next token is `extern', 'static' or 'inline' and the one
15298 after that is `template', we have a GNU extended explicit
15299 instantiation directive. */
15300 else if (cp_parser_allow_gnu_extensions_p (parser)
15301 && token2->keyword == RID_TEMPLATE
15302 && (token1->keyword == RID_EXTERN
15303 || token1->keyword == RID_STATIC
15304 || token1->keyword == RID_INLINE))
15305 cp_parser_explicit_instantiation (parser);
15306 /* If the next token is `namespace', check for a named or unnamed
15307 namespace definition. */
15308 else if (token1->keyword == RID_NAMESPACE
15309 && (/* A named namespace definition. */
15310 (token2->type == CPP_NAME
15311 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15312 != CPP_EQ))
15313 || (token2->type == CPP_OPEN_SQUARE
15314 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15315 == CPP_OPEN_SQUARE)
15316 /* An unnamed namespace definition. */
15317 || token2->type == CPP_OPEN_BRACE
15318 || token2->keyword == RID_ATTRIBUTE))
15319 cp_parser_namespace_definition (parser);
15320 /* An inline (associated) namespace definition. */
15321 else if (token2->keyword == RID_NAMESPACE
15322 && token1->keyword == RID_INLINE)
15323 cp_parser_namespace_definition (parser);
15324 /* Objective-C++ declaration/definition. */
15325 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15326 cp_parser_objc_declaration (parser, attributes);
15327 else if (c_dialect_objc ()
15328 && token1->keyword == RID_ATTRIBUTE
15329 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15330 cp_parser_objc_declaration (parser, attributes);
15331 /* At this point we may have a template declared by a concept
15332 introduction. */
15333 else if (flag_concepts
15334 && cp_parser_template_declaration_after_export (parser,
15335 /*member_p=*/false))
15336 /* We did. */;
15337 else
15338 /* Try to parse a block-declaration, or a function-definition. */
15339 cp_parser_block_declaration (parser, /*statement_p=*/false);
15341 /* Free any declarators allocated. */
15342 obstack_free (&declarator_obstack, p);
15345 /* Parse a namespace-scope declaration. */
15347 static void
15348 cp_parser_toplevel_declaration (cp_parser* parser)
15350 cp_token *token = cp_lexer_peek_token (parser->lexer);
15352 if (token->type == CPP_PRAGMA)
15353 /* A top-level declaration can consist solely of a #pragma. A
15354 nested declaration cannot, so this is done here and not in
15355 cp_parser_declaration. (A #pragma at block scope is
15356 handled in cp_parser_statement.) */
15357 cp_parser_pragma (parser, pragma_external, NULL);
15358 else
15359 /* Parse the declaration itself. */
15360 cp_parser_declaration (parser, NULL_TREE);
15363 /* Parse a block-declaration.
15365 block-declaration:
15366 simple-declaration
15367 asm-definition
15368 namespace-alias-definition
15369 using-declaration
15370 using-directive
15372 GNU Extension:
15374 block-declaration:
15375 __extension__ block-declaration
15377 C++0x Extension:
15379 block-declaration:
15380 static_assert-declaration
15382 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15383 part of a declaration-statement. */
15385 static void
15386 cp_parser_block_declaration (cp_parser *parser,
15387 bool statement_p)
15389 int saved_pedantic;
15391 /* Check for the `__extension__' keyword. */
15392 if (cp_parser_extension_opt (parser, &saved_pedantic))
15394 /* Parse the qualified declaration. */
15395 cp_parser_block_declaration (parser, statement_p);
15396 /* Restore the PEDANTIC flag. */
15397 pedantic = saved_pedantic;
15399 return;
15402 /* Peek at the next token to figure out which kind of declaration is
15403 present. */
15404 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15405 size_t attr_idx;
15407 /* If the next keyword is `asm', we have an asm-definition. */
15408 if (token1->keyword == RID_ASM)
15410 if (statement_p)
15411 cp_parser_commit_to_tentative_parse (parser);
15412 cp_parser_asm_definition (parser);
15414 /* If the next keyword is `namespace', we have a
15415 namespace-alias-definition. */
15416 else if (token1->keyword == RID_NAMESPACE)
15417 cp_parser_namespace_alias_definition (parser);
15418 /* If the next keyword is `using', we have a
15419 using-declaration, a using-directive, or an alias-declaration. */
15420 else if (token1->keyword == RID_USING)
15422 cp_token *token2;
15424 if (statement_p)
15425 cp_parser_commit_to_tentative_parse (parser);
15426 /* If the token after `using' is `namespace', then we have a
15427 using-directive. */
15428 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15429 if (token2->keyword == RID_NAMESPACE)
15430 cp_parser_using_directive (parser);
15431 else if (token2->keyword == RID_ENUM)
15432 cp_parser_using_enum (parser);
15433 /* If the second token after 'using' is '=', then we have an
15434 alias-declaration. */
15435 else if (cxx_dialect >= cxx11
15436 && token2->type == CPP_NAME
15437 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15438 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15439 cp_parser_alias_declaration (parser);
15440 /* Otherwise, it's a using-declaration. */
15441 else
15442 cp_parser_using_declaration (parser,
15443 /*access_declaration_p=*/false);
15445 /* If the next keyword is `__label__' we have a misplaced label
15446 declaration. */
15447 else if (token1->keyword == RID_LABEL)
15449 cp_lexer_consume_token (parser->lexer);
15450 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15451 cp_parser_skip_to_end_of_statement (parser);
15452 /* If the next token is now a `;', consume it. */
15453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15454 cp_lexer_consume_token (parser->lexer);
15456 /* If the next token is `static_assert' we have a static assertion. */
15457 else if (token1->keyword == RID_STATIC_ASSERT)
15458 cp_parser_static_assert (parser, /*member_p=*/false);
15459 /* If the next tokens after attributes is `using namespace', then we have
15460 a using-directive. */
15461 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15462 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15463 RID_USING)
15464 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15465 RID_NAMESPACE))
15467 if (statement_p)
15468 cp_parser_commit_to_tentative_parse (parser);
15469 cp_parser_using_directive (parser);
15471 /* Anything else must be a simple-declaration. */
15472 else
15473 cp_parser_simple_declaration (parser, !statement_p,
15474 /*maybe_range_for_decl*/NULL);
15477 /* Parse a simple-declaration.
15479 simple-declaration:
15480 decl-specifier-seq [opt] init-declarator-list [opt] ;
15481 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15482 brace-or-equal-initializer ;
15484 init-declarator-list:
15485 init-declarator
15486 init-declarator-list , init-declarator
15488 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15489 function-definition as a simple-declaration.
15491 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15492 parsed declaration if it is an uninitialized single declarator not followed
15493 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15494 if present, will not be consumed. */
15496 static void
15497 cp_parser_simple_declaration (cp_parser* parser,
15498 bool function_definition_allowed_p,
15499 tree *maybe_range_for_decl)
15501 cp_decl_specifier_seq decl_specifiers;
15502 int declares_class_or_enum;
15503 bool saw_declarator;
15504 location_t comma_loc = UNKNOWN_LOCATION;
15505 location_t init_loc = UNKNOWN_LOCATION;
15507 if (maybe_range_for_decl)
15508 *maybe_range_for_decl = NULL_TREE;
15510 /* Defer access checks until we know what is being declared; the
15511 checks for names appearing in the decl-specifier-seq should be
15512 done as if we were in the scope of the thing being declared. */
15513 push_deferring_access_checks (dk_deferred);
15515 /* Parse the decl-specifier-seq. We have to keep track of whether
15516 or not the decl-specifier-seq declares a named class or
15517 enumeration type, since that is the only case in which the
15518 init-declarator-list is allowed to be empty.
15520 [dcl.dcl]
15522 In a simple-declaration, the optional init-declarator-list can be
15523 omitted only when declaring a class or enumeration, that is when
15524 the decl-specifier-seq contains either a class-specifier, an
15525 elaborated-type-specifier, or an enum-specifier. */
15526 cp_parser_decl_specifier_seq (parser,
15527 CP_PARSER_FLAGS_OPTIONAL,
15528 &decl_specifiers,
15529 &declares_class_or_enum);
15530 /* We no longer need to defer access checks. */
15531 stop_deferring_access_checks ();
15533 cp_omp_declare_simd_data odsd;
15534 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15535 cp_parser_handle_directive_omp_attributes (parser,
15536 &decl_specifiers.attributes,
15537 &odsd, true);
15539 /* In a block scope, a valid declaration must always have a
15540 decl-specifier-seq. By not trying to parse declarators, we can
15541 resolve the declaration/expression ambiguity more quickly. */
15542 if (!function_definition_allowed_p
15543 && !decl_specifiers.any_specifiers_p)
15545 cp_parser_error (parser, "expected declaration");
15546 goto done;
15549 /* If the next two tokens are both identifiers, the code is
15550 erroneous. The usual cause of this situation is code like:
15552 T t;
15554 where "T" should name a type -- but does not. */
15555 if (!decl_specifiers.any_type_specifiers_p
15556 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15558 /* If parsing tentatively, we should commit; we really are
15559 looking at a declaration. */
15560 cp_parser_commit_to_tentative_parse (parser);
15561 /* Give up. */
15562 goto done;
15565 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15567 /* Look for C++17 decomposition declaration. */
15568 for (size_t n = 1; ; n++)
15569 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15570 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15571 continue;
15572 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15573 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15574 && decl_specifiers.any_specifiers_p)
15576 tree decl
15577 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15578 maybe_range_for_decl,
15579 &init_loc);
15581 /* The next token should be either a `,' or a `;'. */
15582 cp_token *token = cp_lexer_peek_token (parser->lexer);
15583 /* If it's a `;', we are done. */
15584 if (token->type == CPP_SEMICOLON)
15585 goto finish;
15586 else if (maybe_range_for_decl)
15588 if (*maybe_range_for_decl == NULL_TREE)
15589 *maybe_range_for_decl = error_mark_node;
15590 goto finish;
15592 /* Anything else is an error. */
15593 else
15595 /* If we have already issued an error message we don't need
15596 to issue another one. */
15597 if ((decl != error_mark_node
15598 && DECL_INITIAL (decl) != error_mark_node)
15599 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15600 cp_parser_error (parser, "expected %<;%>");
15601 /* Skip tokens until we reach the end of the statement. */
15602 cp_parser_skip_to_end_of_statement (parser);
15603 /* If the next token is now a `;', consume it. */
15604 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15605 cp_lexer_consume_token (parser->lexer);
15606 goto done;
15609 else
15610 break;
15612 tree last_type;
15613 bool auto_specifier_p;
15614 /* NULL_TREE if both variable and function declaration are allowed,
15615 error_mark_node if function declaration are not allowed and
15616 a FUNCTION_DECL that should be diagnosed if it is followed by
15617 variable declarations. */
15618 tree auto_function_declaration;
15620 last_type = NULL_TREE;
15621 auto_specifier_p
15622 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15623 auto_function_declaration = NULL_TREE;
15625 /* Keep going until we hit the `;' at the end of the simple
15626 declaration. */
15627 saw_declarator = false;
15628 while (cp_lexer_next_token_is_not (parser->lexer,
15629 CPP_SEMICOLON))
15631 cp_token *token;
15632 bool function_definition_p;
15633 tree decl;
15634 tree auto_result = NULL_TREE;
15636 if (saw_declarator)
15638 /* If we are processing next declarator, comma is expected */
15639 token = cp_lexer_peek_token (parser->lexer);
15640 gcc_assert (token->type == CPP_COMMA);
15641 cp_lexer_consume_token (parser->lexer);
15642 if (maybe_range_for_decl)
15644 *maybe_range_for_decl = error_mark_node;
15645 if (comma_loc == UNKNOWN_LOCATION)
15646 comma_loc = token->location;
15649 else
15650 saw_declarator = true;
15652 /* Parse the init-declarator. */
15653 decl = cp_parser_init_declarator (parser,
15654 CP_PARSER_FLAGS_NONE,
15655 &decl_specifiers,
15656 /*checks=*/NULL,
15657 function_definition_allowed_p,
15658 /*member_p=*/false,
15659 declares_class_or_enum,
15660 &function_definition_p,
15661 maybe_range_for_decl,
15662 &init_loc,
15663 &auto_result);
15664 /* If an error occurred while parsing tentatively, exit quickly.
15665 (That usually happens when in the body of a function; each
15666 statement is treated as a declaration-statement until proven
15667 otherwise.) */
15668 if (cp_parser_error_occurred (parser))
15669 goto done;
15671 if (auto_specifier_p && cxx_dialect >= cxx14)
15673 /* If the init-declarator-list contains more than one
15674 init-declarator, they shall all form declarations of
15675 variables. */
15676 if (auto_function_declaration == NULL_TREE)
15677 auto_function_declaration
15678 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15679 else if (TREE_CODE (decl) == FUNCTION_DECL
15680 || auto_function_declaration != error_mark_node)
15682 error_at (decl_specifiers.locations[ds_type_spec],
15683 "non-variable %qD in declaration with more than one "
15684 "declarator with placeholder type",
15685 TREE_CODE (decl) == FUNCTION_DECL
15686 ? decl : auto_function_declaration);
15687 auto_function_declaration = error_mark_node;
15691 if (auto_result
15692 && (!processing_template_decl || !type_uses_auto (auto_result)))
15694 if (last_type
15695 && last_type != error_mark_node
15696 && !same_type_p (auto_result, last_type))
15698 /* If the list of declarators contains more than one declarator,
15699 the type of each declared variable is determined as described
15700 above. If the type deduced for the template parameter U is not
15701 the same in each deduction, the program is ill-formed. */
15702 error_at (decl_specifiers.locations[ds_type_spec],
15703 "inconsistent deduction for %qT: %qT and then %qT",
15704 decl_specifiers.type, last_type, auto_result);
15705 last_type = error_mark_node;
15707 else
15708 last_type = auto_result;
15711 /* Handle function definitions specially. */
15712 if (function_definition_p)
15714 /* If the next token is a `,', then we are probably
15715 processing something like:
15717 void f() {}, *p;
15719 which is erroneous. */
15720 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15722 cp_token *token = cp_lexer_peek_token (parser->lexer);
15723 error_at (token->location,
15724 "mixing"
15725 " declarations and function-definitions is forbidden");
15727 /* Otherwise, we're done with the list of declarators. */
15728 else
15730 pop_deferring_access_checks ();
15731 cp_finalize_omp_declare_simd (parser, &odsd);
15732 return;
15735 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15736 *maybe_range_for_decl = decl;
15737 /* The next token should be either a `,' or a `;'. */
15738 token = cp_lexer_peek_token (parser->lexer);
15739 /* If it's a `,', there are more declarators to come. */
15740 if (token->type == CPP_COMMA)
15741 /* will be consumed next time around */;
15742 /* If it's a `;', we are done. */
15743 else if (token->type == CPP_SEMICOLON)
15744 break;
15745 else if (maybe_range_for_decl)
15747 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15748 permerror (decl_specifiers.locations[ds_type_spec],
15749 "types may not be defined in a for-range-declaration");
15750 break;
15752 /* Anything else is an error. */
15753 else
15755 /* If we have already issued an error message we don't need
15756 to issue another one. */
15757 if ((decl != error_mark_node
15758 && DECL_INITIAL (decl) != error_mark_node)
15759 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15760 cp_parser_error (parser, "expected %<,%> or %<;%>");
15761 /* Skip tokens until we reach the end of the statement. */
15762 cp_parser_skip_to_end_of_statement (parser);
15763 /* If the next token is now a `;', consume it. */
15764 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15765 cp_lexer_consume_token (parser->lexer);
15766 goto done;
15768 /* After the first time around, a function-definition is not
15769 allowed -- even if it was OK at first. For example:
15771 int i, f() {}
15773 is not valid. */
15774 function_definition_allowed_p = false;
15777 /* Issue an error message if no declarators are present, and the
15778 decl-specifier-seq does not itself declare a class or
15779 enumeration: [dcl.dcl]/3. */
15780 if (!saw_declarator)
15782 if (cp_parser_declares_only_class_p (parser))
15784 if (!declares_class_or_enum
15785 && decl_specifiers.type
15786 && OVERLOAD_TYPE_P (decl_specifiers.type))
15787 /* Ensure an error is issued anyway when finish_decltype_type,
15788 called via cp_parser_decl_specifier_seq, returns a class or
15789 an enumeration (c++/51786). */
15790 decl_specifiers.type = NULL_TREE;
15791 shadow_tag (&decl_specifiers);
15793 /* Perform any deferred access checks. */
15794 perform_deferred_access_checks (tf_warning_or_error);
15797 /* Consume the `;'. */
15798 finish:
15799 if (!maybe_range_for_decl)
15800 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15801 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15803 if (init_loc != UNKNOWN_LOCATION)
15804 error_at (init_loc, "initializer in range-based %<for%> loop");
15805 if (comma_loc != UNKNOWN_LOCATION)
15806 error_at (comma_loc,
15807 "multiple declarations in range-based %<for%> loop");
15810 done:
15811 pop_deferring_access_checks ();
15812 cp_finalize_omp_declare_simd (parser, &odsd);
15815 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15816 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15817 initializer ; */
15819 static tree
15820 cp_parser_decomposition_declaration (cp_parser *parser,
15821 cp_decl_specifier_seq *decl_specifiers,
15822 tree *maybe_range_for_decl,
15823 location_t *init_loc)
15825 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15826 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15827 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15829 /* Parse the identifier-list. */
15830 auto_vec<cp_expr, 10> v;
15831 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15832 while (true)
15834 cp_expr e = cp_parser_identifier (parser);
15835 if (e.get_value () == error_mark_node)
15836 break;
15837 v.safe_push (e);
15838 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15839 break;
15840 cp_lexer_consume_token (parser->lexer);
15843 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15844 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15846 end_loc = UNKNOWN_LOCATION;
15847 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15848 false);
15849 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15850 cp_lexer_consume_token (parser->lexer);
15851 else
15853 cp_parser_skip_to_end_of_statement (parser);
15854 return error_mark_node;
15858 if (cxx_dialect < cxx17)
15859 pedwarn (loc, OPT_Wc__17_extensions,
15860 "structured bindings only available with "
15861 "%<-std=c++17%> or %<-std=gnu++17%>");
15863 tree pushed_scope;
15864 cp_declarator *declarator = make_declarator (cdk_decomp);
15865 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15866 declarator->id_loc = loc;
15867 if (ref_qual != REF_QUAL_NONE)
15868 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15869 ref_qual == REF_QUAL_RVALUE,
15870 NULL_TREE);
15871 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15872 NULL_TREE, decl_specifiers->attributes,
15873 &pushed_scope);
15874 tree orig_decl = decl;
15876 unsigned int i;
15877 cp_expr e;
15878 cp_decl_specifier_seq decl_specs;
15879 clear_decl_specs (&decl_specs);
15880 decl_specs.type = make_auto ();
15881 tree prev = decl;
15882 FOR_EACH_VEC_ELT (v, i, e)
15884 if (i == 0)
15885 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15886 sfk_none, e.get_location ());
15887 else
15889 declarator->u.id.unqualified_name = e.get_value ();
15890 declarator->id_loc = e.get_location ();
15892 tree elt_pushed_scope;
15893 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15894 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15895 if (decl2 == error_mark_node)
15896 decl = error_mark_node;
15897 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15899 /* Ensure we've diagnosed redeclaration if we aren't creating
15900 a new VAR_DECL. */
15901 gcc_assert (errorcount);
15902 decl = error_mark_node;
15904 else
15905 prev = decl2;
15906 if (elt_pushed_scope)
15907 pop_scope (elt_pushed_scope);
15910 if (v.is_empty ())
15912 error_at (loc, "empty structured binding declaration");
15913 decl = error_mark_node;
15916 if (maybe_range_for_decl == NULL
15917 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15919 bool non_constant_p = false, is_direct_init = false;
15920 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15921 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15922 &non_constant_p);
15923 if (initializer == NULL_TREE
15924 || (TREE_CODE (initializer) == TREE_LIST
15925 && TREE_CHAIN (initializer))
15926 || (is_direct_init
15927 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15928 && CONSTRUCTOR_NELTS (initializer) != 1))
15930 error_at (loc, "invalid initializer for structured binding "
15931 "declaration");
15932 initializer = error_mark_node;
15935 if (decl != error_mark_node)
15937 cp_decomp decomp = { prev, v.length () };
15938 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15939 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
15940 &decomp);
15941 cp_finish_decomp (decl, &decomp);
15944 else if (decl != error_mark_node)
15946 *maybe_range_for_decl = prev;
15947 cp_decomp decomp = { prev, v.length () };
15948 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15949 the underlying DECL. */
15950 cp_finish_decomp (decl, &decomp);
15953 if (pushed_scope)
15954 pop_scope (pushed_scope);
15956 if (decl == error_mark_node && DECL_P (orig_decl))
15958 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15959 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15962 return decl;
15965 /* Names of storage classes. */
15967 static const char *const
15968 cp_storage_class_name[] = {
15969 "", "auto", "register", "static", "extern", "mutable"
15972 /* Parse a decl-specifier-seq.
15974 decl-specifier-seq:
15975 decl-specifier-seq [opt] decl-specifier
15976 decl-specifier attribute-specifier-seq [opt] (C++11)
15978 decl-specifier:
15979 storage-class-specifier
15980 type-specifier
15981 function-specifier
15982 friend
15983 typedef
15985 GNU Extension:
15987 decl-specifier:
15988 attributes
15990 Concepts Extension:
15992 decl-specifier:
15993 concept
15995 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15997 The parser flags FLAGS is used to control type-specifier parsing.
15999 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16000 flags:
16002 1: one of the decl-specifiers is an elaborated-type-specifier
16003 (i.e., a type declaration)
16004 2: one of the decl-specifiers is an enum-specifier or a
16005 class-specifier (i.e., a type definition)
16009 static void
16010 cp_parser_decl_specifier_seq (cp_parser* parser,
16011 cp_parser_flags flags,
16012 cp_decl_specifier_seq *decl_specs,
16013 int* declares_class_or_enum)
16015 bool constructor_possible_p = !parser->in_declarator_p;
16016 bool found_decl_spec = false;
16017 cp_token *start_token = NULL;
16018 cp_decl_spec ds;
16020 /* Clear DECL_SPECS. */
16021 clear_decl_specs (decl_specs);
16023 /* Assume no class or enumeration type is declared. */
16024 *declares_class_or_enum = 0;
16026 /* Keep reading specifiers until there are no more to read. */
16027 while (true)
16029 bool constructor_p;
16030 cp_token *token;
16031 ds = ds_last;
16033 /* Peek at the next token. */
16034 token = cp_lexer_peek_token (parser->lexer);
16036 /* Save the first token of the decl spec list for error
16037 reporting. */
16038 if (!start_token)
16039 start_token = token;
16040 /* Handle attributes. */
16041 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16042 && cp_next_tokens_can_be_attribute_p (parser))
16044 /* Parse the attributes. */
16045 tree attrs = cp_parser_attributes_opt (parser);
16047 /* In a sequence of declaration specifiers, c++11 attributes
16048 appertain to the type that precede them. In that case
16049 [dcl.spec]/1 says:
16051 The attribute-specifier-seq affects the type only for
16052 the declaration it appears in, not other declarations
16053 involving the same type.
16055 But for now let's force the user to position the
16056 attribute either at the beginning of the declaration or
16057 after the declarator-id, which would clearly mean that it
16058 applies to the declarator. */
16059 if (cxx11_attribute_p (attrs))
16061 if (!found_decl_spec)
16062 /* The c++11 attribute is at the beginning of the
16063 declaration. It appertains to the entity being
16064 declared. */;
16065 else
16067 if (find_contract (attrs))
16069 diagnose_misapplied_contracts (attrs);
16070 attrs = NULL_TREE;
16072 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16074 /* This is an attribute following a
16075 class-specifier. */
16076 if (decl_specs->type_definition_p)
16077 warn_misplaced_attr_for_class_type (token->location,
16078 decl_specs->type);
16079 attrs = NULL_TREE;
16081 else
16083 decl_specs->std_attributes
16084 = attr_chainon (decl_specs->std_attributes, attrs);
16085 if (decl_specs->locations[ds_std_attribute] == 0)
16086 decl_specs->locations[ds_std_attribute] = token->location;
16088 continue;
16092 decl_specs->attributes
16093 = attr_chainon (decl_specs->attributes, attrs);
16094 if (decl_specs->locations[ds_attribute] == 0)
16095 decl_specs->locations[ds_attribute] = token->location;
16096 continue;
16098 /* Assume we will find a decl-specifier keyword. */
16099 found_decl_spec = true;
16100 /* If the next token is an appropriate keyword, we can simply
16101 add it to the list. */
16102 switch (token->keyword)
16104 /* decl-specifier:
16105 friend
16106 constexpr
16107 constinit */
16108 case RID_FRIEND:
16109 if (!at_class_scope_p ())
16111 gcc_rich_location richloc (token->location);
16112 richloc.add_fixit_remove ();
16113 error_at (&richloc, "%<friend%> used outside of class");
16114 cp_lexer_purge_token (parser->lexer);
16116 else
16118 ds = ds_friend;
16119 /* Consume the token. */
16120 cp_lexer_consume_token (parser->lexer);
16122 break;
16124 case RID_CONSTEXPR:
16125 ds = ds_constexpr;
16126 cp_lexer_consume_token (parser->lexer);
16127 break;
16129 case RID_CONSTINIT:
16130 ds = ds_constinit;
16131 cp_lexer_consume_token (parser->lexer);
16132 break;
16134 case RID_CONSTEVAL:
16135 ds = ds_consteval;
16136 cp_lexer_consume_token (parser->lexer);
16137 break;
16139 case RID_CONCEPT:
16140 ds = ds_concept;
16141 cp_lexer_consume_token (parser->lexer);
16143 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16144 break;
16146 /* Warn for concept as a decl-specifier. We'll rewrite these as
16147 concept declarations later. */
16148 if (!flag_concepts_ts)
16150 cp_token *next = cp_lexer_peek_token (parser->lexer);
16151 if (next->keyword == RID_BOOL)
16152 permerror (next->location, "the %<bool%> keyword is not "
16153 "allowed in a C++20 concept definition");
16154 else
16155 error_at (token->location, "C++20 concept definition syntax "
16156 "is %<concept <name> = <expr>%>");
16159 /* In C++20 a concept definition is just 'concept name = expr;'
16160 Support that syntax as a TS extension by pretending we've seen
16161 the 'bool' specifier. */
16162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16163 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16164 && !decl_specs->any_type_specifiers_p)
16166 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16167 token, /*type_definition*/false);
16168 decl_specs->any_type_specifiers_p = true;
16170 break;
16172 /* function-specifier:
16173 inline
16174 virtual
16175 explicit */
16176 case RID_INLINE:
16177 case RID_VIRTUAL:
16178 case RID_EXPLICIT:
16179 cp_parser_function_specifier_opt (parser, decl_specs);
16180 break;
16182 /* decl-specifier:
16183 typedef */
16184 case RID_TYPEDEF:
16185 ds = ds_typedef;
16186 /* Consume the token. */
16187 cp_lexer_consume_token (parser->lexer);
16189 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16190 break;
16192 /* A constructor declarator cannot appear in a typedef. */
16193 constructor_possible_p = false;
16194 /* The "typedef" keyword can only occur in a declaration; we
16195 may as well commit at this point. */
16196 cp_parser_commit_to_tentative_parse (parser);
16198 if (decl_specs->storage_class != sc_none)
16200 if (decl_specs->conflicting_specifiers_p)
16201 break;
16202 gcc_rich_location richloc (token->location);
16203 location_t oloc = decl_specs->locations[ds_storage_class];
16204 richloc.add_location_if_nearby (oloc);
16205 error_at (&richloc,
16206 "%<typedef%> specifier conflicts with %qs",
16207 cp_storage_class_name[decl_specs->storage_class]);
16208 decl_specs->conflicting_specifiers_p = true;
16210 break;
16212 /* storage-class-specifier:
16213 auto
16214 register
16215 static
16216 extern
16217 mutable
16219 GNU Extension:
16220 thread */
16221 case RID_AUTO:
16222 if (cxx_dialect == cxx98)
16224 /* Consume the token. */
16225 cp_lexer_consume_token (parser->lexer);
16227 /* Complain about `auto' as a storage specifier, if
16228 we're complaining about C++0x compatibility. */
16229 gcc_rich_location richloc (token->location);
16230 richloc.add_fixit_remove ();
16231 warning_at (&richloc, OPT_Wc__11_compat,
16232 "%<auto%> changes meaning in C++11; "
16233 "please remove it");
16235 /* Set the storage class anyway. */
16236 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16237 token);
16239 else
16240 /* C++0x auto type-specifier. */
16241 found_decl_spec = false;
16242 break;
16244 case RID_REGISTER:
16245 case RID_STATIC:
16246 case RID_EXTERN:
16247 case RID_MUTABLE:
16248 /* Consume the token. */
16249 cp_lexer_consume_token (parser->lexer);
16250 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16251 token);
16252 break;
16253 case RID_THREAD:
16254 /* Consume the token. */
16255 ds = ds_thread;
16256 cp_lexer_consume_token (parser->lexer);
16257 break;
16259 default:
16260 /* We did not yet find a decl-specifier yet. */
16261 found_decl_spec = false;
16262 break;
16265 if (found_decl_spec
16266 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16267 && token->keyword != RID_CONSTEXPR)
16268 error ("%qD invalid in condition", ridpointers[token->keyword]);
16270 if (found_decl_spec
16271 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16272 && token->keyword != RID_MUTABLE
16273 && token->keyword != RID_CONSTEXPR
16274 && token->keyword != RID_CONSTEVAL)
16276 if (token->keyword != RID_STATIC)
16277 error_at (token->location, "%qD invalid in lambda",
16278 ridpointers[token->keyword]);
16279 else if (cxx_dialect < cxx23)
16280 pedwarn (token->location, OPT_Wc__23_extensions,
16281 "%qD only valid in lambda with %<-std=c++23%> or "
16282 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16285 if (ds != ds_last)
16286 set_and_check_decl_spec_loc (decl_specs, ds, token);
16288 /* Constructors are a special case. The `S' in `S()' is not a
16289 decl-specifier; it is the beginning of the declarator. */
16290 constructor_p
16291 = (!found_decl_spec
16292 && constructor_possible_p
16293 && (cp_parser_constructor_declarator_p
16294 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16295 ds_friend))));
16297 /* If we don't have a DECL_SPEC yet, then we must be looking at
16298 a type-specifier. */
16299 if (!found_decl_spec && !constructor_p)
16301 int decl_spec_declares_class_or_enum;
16302 bool is_cv_qualifier;
16303 tree type_spec;
16305 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16306 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16308 type_spec
16309 = cp_parser_type_specifier (parser, flags,
16310 decl_specs,
16311 /*is_declaration=*/true,
16312 &decl_spec_declares_class_or_enum,
16313 &is_cv_qualifier);
16314 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16316 /* If this type-specifier referenced a user-defined type
16317 (a typedef, class-name, etc.), then we can't allow any
16318 more such type-specifiers henceforth.
16320 [dcl.spec]
16322 The longest sequence of decl-specifiers that could
16323 possibly be a type name is taken as the
16324 decl-specifier-seq of a declaration. The sequence shall
16325 be self-consistent as described below.
16327 [dcl.type]
16329 As a general rule, at most one type-specifier is allowed
16330 in the complete decl-specifier-seq of a declaration. The
16331 only exceptions are the following:
16333 -- const or volatile can be combined with any other
16334 type-specifier.
16336 -- signed or unsigned can be combined with char, long,
16337 short, or int.
16339 -- ..
16341 Example:
16343 typedef char* Pc;
16344 void g (const int Pc);
16346 Here, Pc is *not* part of the decl-specifier seq; it's
16347 the declarator. Therefore, once we see a type-specifier
16348 (other than a cv-qualifier), we forbid any additional
16349 user-defined types. We *do* still allow things like `int
16350 int' to be considered a decl-specifier-seq, and issue the
16351 error message later. */
16352 if (type_spec && !is_cv_qualifier)
16353 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16354 /* A constructor declarator cannot follow a type-specifier. */
16355 if (type_spec)
16357 constructor_possible_p = false;
16358 found_decl_spec = true;
16359 if (!is_cv_qualifier)
16360 decl_specs->any_type_specifiers_p = true;
16362 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16363 error_at (token->location, "type-specifier invalid in lambda");
16367 /* If we still do not have a DECL_SPEC, then there are no more
16368 decl-specifiers. */
16369 if (!found_decl_spec)
16370 break;
16372 if (decl_specs->std_attributes)
16374 error_at (decl_specs->locations[ds_std_attribute],
16375 "standard attributes in middle of decl-specifiers");
16376 inform (decl_specs->locations[ds_std_attribute],
16377 "standard attributes must precede the decl-specifiers to "
16378 "apply to the declaration, or follow them to apply to "
16379 "the type");
16382 decl_specs->any_specifiers_p = true;
16383 /* After we see one decl-specifier, further decl-specifiers are
16384 always optional. */
16385 flags |= CP_PARSER_FLAGS_OPTIONAL;
16388 /* Don't allow a friend specifier with a class definition. */
16389 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16390 && (*declares_class_or_enum & 2))
16391 error_at (decl_specs->locations[ds_friend],
16392 "class definition may not be declared a friend");
16395 /* Parse an (optional) storage-class-specifier.
16397 storage-class-specifier:
16398 auto
16399 register
16400 static
16401 extern
16402 mutable
16404 GNU Extension:
16406 storage-class-specifier:
16407 thread
16409 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16411 static tree
16412 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16414 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16416 case RID_AUTO:
16417 if (cxx_dialect != cxx98)
16418 return NULL_TREE;
16419 /* Fall through for C++98. */
16420 gcc_fallthrough ();
16422 case RID_REGISTER:
16423 case RID_STATIC:
16424 case RID_EXTERN:
16425 case RID_MUTABLE:
16426 case RID_THREAD:
16427 /* Consume the token. */
16428 return cp_lexer_consume_token (parser->lexer)->u.value;
16430 default:
16431 return NULL_TREE;
16435 /* Parse an (optional) function-specifier.
16437 function-specifier:
16438 inline
16439 virtual
16440 explicit
16442 C++20 Extension:
16443 explicit(constant-expression)
16445 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16446 Updates DECL_SPECS, if it is non-NULL. */
16448 static tree
16449 cp_parser_function_specifier_opt (cp_parser* parser,
16450 cp_decl_specifier_seq *decl_specs)
16452 cp_token *token = cp_lexer_peek_token (parser->lexer);
16453 switch (token->keyword)
16455 case RID_INLINE:
16456 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16457 break;
16459 case RID_VIRTUAL:
16460 /* 14.5.2.3 [temp.mem]
16462 A member function template shall not be virtual. */
16463 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16464 && current_class_type)
16465 error_at (token->location, "templates may not be %<virtual%>");
16466 else
16467 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16468 break;
16470 case RID_EXPLICIT:
16472 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16473 /* If we see '(', it's C++20 explicit(bool). */
16474 tree expr;
16475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16477 matching_parens parens;
16478 parens.consume_open (parser);
16480 /* New types are not allowed in an explicit-specifier. */
16481 const char *saved_message
16482 = parser->type_definition_forbidden_message;
16483 parser->type_definition_forbidden_message
16484 = G_("types may not be defined in explicit-specifier");
16486 if (cxx_dialect < cxx20)
16487 pedwarn (token->location, OPT_Wc__20_extensions,
16488 "%<explicit(bool)%> only available with %<-std=c++20%> "
16489 "or %<-std=gnu++20%>");
16491 /* Parse the constant-expression. */
16492 expr = cp_parser_constant_expression (parser);
16494 /* Restore the saved message. */
16495 parser->type_definition_forbidden_message = saved_message;
16496 parens.require_close (parser);
16498 else
16499 /* The explicit-specifier explicit without a constant-expression is
16500 equivalent to the explicit-specifier explicit(true). */
16501 expr = boolean_true_node;
16503 /* [dcl.fct.spec]
16504 "the constant-expression, if supplied, shall be a contextually
16505 converted constant expression of type bool." */
16506 expr = build_explicit_specifier (expr, tf_warning_or_error);
16507 /* We could evaluate it -- mark the decl as appropriate. */
16508 if (expr == boolean_true_node)
16509 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16510 else if (expr == boolean_false_node)
16511 /* Don't mark the decl as explicit. */;
16512 else if (decl_specs)
16513 /* The expression was value-dependent. Remember it so that we can
16514 substitute it later. */
16515 decl_specs->explicit_specifier = expr;
16516 return id;
16519 default:
16520 return NULL_TREE;
16523 /* Consume the token. */
16524 return cp_lexer_consume_token (parser->lexer)->u.value;
16527 /* Parse a linkage-specification.
16529 linkage-specification:
16530 extern string-literal { declaration-seq [opt] }
16531 extern string-literal declaration */
16533 static void
16534 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16536 /* Look for the `extern' keyword. */
16537 cp_token *extern_token
16538 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16540 /* Look for the string-literal. */
16541 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16542 tree linkage = cp_parser_string_literal (parser, /*translate=*/false,
16543 /*wide_ok=*/false);
16545 /* Transform the literal into an identifier. If the literal is a
16546 wide-character string, or contains embedded NULs, then we can't
16547 handle it as the user wants. */
16548 if (linkage == error_mark_node
16549 || strlen (TREE_STRING_POINTER (linkage))
16550 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16552 cp_parser_error (parser, "invalid linkage-specification");
16553 /* Assume C++ linkage. */
16554 linkage = lang_name_cplusplus;
16556 else
16557 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16559 /* We're now using the new linkage. */
16560 unsigned saved_module = module_kind;
16561 module_kind &= ~MK_ATTACH;
16562 push_lang_context (linkage);
16564 /* Preserve the location of the innermost linkage specification,
16565 tracking the locations of nested specifications via a local. */
16566 location_t saved_location
16567 = parser->innermost_linkage_specification_location;
16568 /* Construct a location ranging from the start of the "extern" to
16569 the end of the string-literal, with the caret at the start, e.g.:
16570 extern "C" {
16571 ^~~~~~~~~~
16573 parser->innermost_linkage_specification_location
16574 = make_location (extern_token->location,
16575 extern_token->location,
16576 get_finish (string_token->location));
16578 /* If the next token is a `{', then we're using the first
16579 production. */
16580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16582 cp_ensure_no_omp_declare_simd (parser);
16583 cp_ensure_no_oacc_routine (parser);
16585 /* Consume the `{' token. */
16586 matching_braces braces;
16587 braces.consume_open (parser);
16588 /* Parse the declarations. */
16589 cp_parser_declaration_seq_opt (parser);
16590 /* Look for the closing `}'. */
16591 braces.require_close (parser);
16593 /* Otherwise, there's just one declaration. */
16594 else
16596 bool saved_in_unbraced_linkage_specification_p;
16598 saved_in_unbraced_linkage_specification_p
16599 = parser->in_unbraced_linkage_specification_p;
16600 parser->in_unbraced_linkage_specification_p = true;
16601 cp_parser_declaration (parser, prefix_attr);
16602 parser->in_unbraced_linkage_specification_p
16603 = saved_in_unbraced_linkage_specification_p;
16606 /* We're done with the linkage-specification. */
16607 pop_lang_context ();
16608 module_kind = saved_module;
16610 /* Restore location of parent linkage specification, if any. */
16611 parser->innermost_linkage_specification_location = saved_location;
16614 /* Parse a static_assert-declaration.
16616 static_assert-declaration:
16617 static_assert ( constant-expression , string-literal ) ;
16618 static_assert ( constant-expression ) ; (C++17)
16620 If MEMBER_P, this static_assert is a class member. */
16622 static void
16623 cp_parser_static_assert (cp_parser *parser, bool member_p)
16625 cp_expr condition;
16626 location_t token_loc;
16627 tree message;
16629 /* Peek at the `static_assert' token so we can keep track of exactly
16630 where the static assertion started. */
16631 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16633 /* Look for the `static_assert' keyword. */
16634 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16635 RT_STATIC_ASSERT))
16636 return;
16638 /* We know we are in a static assertion; commit to any tentative
16639 parse. */
16640 if (cp_parser_parsing_tentatively (parser))
16641 cp_parser_commit_to_tentative_parse (parser);
16643 /* Parse the `(' starting the static assertion condition. */
16644 matching_parens parens;
16645 parens.require_open (parser);
16647 /* Parse the constant-expression. Allow a non-constant expression
16648 here in order to give better diagnostics in finish_static_assert. */
16649 condition =
16650 cp_parser_constant_expression (parser,
16651 /*allow_non_constant_p=*/true,
16652 /*non_constant_p=*/nullptr);
16654 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16656 if (pedantic && cxx_dialect < cxx17)
16657 pedwarn (input_location, OPT_Wc__17_extensions,
16658 "%<static_assert%> without a message "
16659 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16660 /* Eat the ')' */
16661 cp_lexer_consume_token (parser->lexer);
16662 message = build_string (1, "");
16663 TREE_TYPE (message) = char_array_type_node;
16664 fix_string_type (message);
16666 else
16668 /* Parse the separating `,'. */
16669 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16671 /* Parse the string-literal message. */
16672 message = cp_parser_string_literal (parser, /*translate=*/false,
16673 /*wide_ok=*/true);
16675 /* A `)' completes the static assertion. */
16676 if (!parens.require_close (parser))
16677 cp_parser_skip_to_closing_parenthesis (parser,
16678 /*recovering=*/true,
16679 /*or_comma=*/false,
16680 /*consume_paren=*/true);
16683 /* A semicolon terminates the declaration. */
16684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16686 /* Get the location for the static assertion. Use that of the
16687 condition if available, otherwise, use that of the "static_assert"
16688 token. */
16689 location_t assert_loc = condition.get_location ();
16690 if (assert_loc == UNKNOWN_LOCATION)
16691 assert_loc = token_loc;
16693 /* Complete the static assertion, which may mean either processing
16694 the static assert now or saving it for template instantiation. */
16695 finish_static_assert (condition, message, assert_loc, member_p,
16696 /*show_expr_p=*/false);
16699 /* Parse the expression in decltype ( expression ). */
16701 static tree
16702 cp_parser_decltype_expr (cp_parser *parser,
16703 bool &id_expression_or_member_access_p)
16705 cp_token *id_expr_start_token;
16706 tree expr;
16708 /* First, try parsing an id-expression. */
16709 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16710 cp_parser_parse_tentatively (parser);
16711 expr = cp_parser_id_expression (parser,
16712 /*template_keyword_p=*/false,
16713 /*check_dependency_p=*/true,
16714 /*template_p=*/NULL,
16715 /*declarator_p=*/false,
16716 /*optional_p=*/false);
16718 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16720 bool non_integral_constant_expression_p = false;
16721 tree id_expression = expr;
16722 cp_id_kind idk;
16723 const char *error_msg;
16725 if (identifier_p (expr))
16726 /* Lookup the name we got back from the id-expression. */
16727 expr = cp_parser_lookup_name_simple (parser, expr,
16728 id_expr_start_token->location);
16730 if (expr
16731 && expr != error_mark_node
16732 && TREE_CODE (expr) != TYPE_DECL
16733 && (TREE_CODE (expr) != BIT_NOT_EXPR
16734 || !TYPE_P (TREE_OPERAND (expr, 0)))
16735 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16737 /* Complete lookup of the id-expression. */
16738 expr = (finish_id_expression
16739 (id_expression, expr, parser->scope, &idk,
16740 /*integral_constant_expression_p=*/false,
16741 /*allow_non_integral_constant_expression_p=*/true,
16742 &non_integral_constant_expression_p,
16743 /*template_p=*/false,
16744 /*done=*/true,
16745 /*address_p=*/false,
16746 /*template_arg_p=*/false,
16747 &error_msg,
16748 id_expr_start_token->location));
16750 if (error_msg)
16752 /* We found an id-expression, but it was something that we
16753 should not have found. This is an error, not something
16754 we can recover from, so report the error we found and
16755 we'll recover as gracefully as possible. */
16756 cp_parser_parse_definitely (parser);
16757 cp_parser_error (parser, error_msg);
16758 id_expression_or_member_access_p = true;
16759 return error_mark_node;
16763 if (expr
16764 && expr != error_mark_node
16765 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16766 /* We have an id-expression. */
16767 id_expression_or_member_access_p = true;
16770 if (!id_expression_or_member_access_p)
16772 /* Abort the id-expression parse. */
16773 cp_parser_abort_tentative_parse (parser);
16775 /* Parsing tentatively, again. */
16776 cp_parser_parse_tentatively (parser);
16778 /* Parse a class member access. */
16779 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16780 /*cast_p=*/false, /*decltype*/true,
16781 /*member_access_only_p=*/true, NULL);
16783 if (expr
16784 && expr != error_mark_node
16785 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16786 /* We have an id-expression. */
16787 id_expression_or_member_access_p = true;
16790 if (id_expression_or_member_access_p)
16791 /* We have parsed the complete id-expression or member access. */
16792 cp_parser_parse_definitely (parser);
16793 else
16795 /* Abort our attempt to parse an id-expression or member access
16796 expression. */
16797 cp_parser_abort_tentative_parse (parser);
16799 /* Parse a full expression. */
16800 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16801 /*decltype_p=*/true);
16804 return expr;
16807 /* Parse a `decltype' type. Returns the type.
16809 decltype-specifier:
16810 decltype ( expression )
16811 C++14:
16812 decltype ( auto ) */
16814 static tree
16815 cp_parser_decltype (cp_parser *parser)
16817 bool id_expression_or_member_access_p = false;
16818 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16820 if (start_token->type == CPP_DECLTYPE)
16822 /* Already parsed. */
16823 cp_lexer_consume_token (parser->lexer);
16824 return saved_checks_value (start_token->u.tree_check_value);
16827 /* Look for the `decltype' token. */
16828 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16829 return error_mark_node;
16831 /* Parse the opening `('. */
16832 matching_parens parens;
16833 if (!parens.require_open (parser))
16834 return error_mark_node;
16836 /* Since we're going to preserve any side-effects from this parse, set up a
16837 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16838 in the expression. */
16839 tentative_firewall firewall (parser);
16841 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16842 Otherwise, commit now for better diagnostics. */
16843 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16844 && !parser->in_declarator_p)
16845 cp_parser_commit_to_topmost_tentative_parse (parser);
16847 push_deferring_access_checks (dk_deferred);
16849 tree expr = NULL_TREE;
16851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16852 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16854 /* decltype (auto) */
16855 cp_lexer_consume_token (parser->lexer);
16856 if (cxx_dialect < cxx14)
16858 error_at (start_token->location,
16859 "%<decltype(auto)%> type specifier only available with "
16860 "%<-std=c++14%> or %<-std=gnu++14%>");
16861 expr = error_mark_node;
16864 else
16866 /* decltype (expression) */
16868 /* Types cannot be defined in a `decltype' expression. Save away the
16869 old message and set the new one. */
16870 const char *saved_message = parser->type_definition_forbidden_message;
16871 parser->type_definition_forbidden_message
16872 = G_("types may not be defined in %<decltype%> expressions");
16874 /* The restrictions on constant-expressions do not apply inside
16875 decltype expressions. */
16876 bool saved_integral_constant_expression_p
16877 = parser->integral_constant_expression_p;
16878 bool saved_non_integral_constant_expression_p
16879 = parser->non_integral_constant_expression_p;
16880 parser->integral_constant_expression_p = false;
16882 /* Within a parenthesized expression, a `>' token is always
16883 the greater-than operator. */
16884 bool saved_greater_than_is_operator_p
16885 = parser->greater_than_is_operator_p;
16886 parser->greater_than_is_operator_p = true;
16888 /* Don't synthesize an implicit template type parameter here. This
16889 could happen with C++23 code like
16891 void f(decltype(new auto{0}));
16893 where we want to deduce the auto right away so that the parameter
16894 is of type 'int *'. */
16895 auto cleanup = make_temp_override
16896 (parser->auto_is_implicit_function_template_parm_p, false);
16898 /* Do not actually evaluate the expression. */
16899 ++cp_unevaluated_operand;
16901 /* Do not warn about problems with the expression. */
16902 ++c_inhibit_evaluation_warnings;
16904 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16905 STRIP_ANY_LOCATION_WRAPPER (expr);
16907 /* Go back to evaluating expressions. */
16908 --cp_unevaluated_operand;
16909 --c_inhibit_evaluation_warnings;
16911 /* The `>' token might be the end of a template-id or
16912 template-parameter-list now. */
16913 parser->greater_than_is_operator_p
16914 = saved_greater_than_is_operator_p;
16916 /* Restore the old message and the integral constant expression
16917 flags. */
16918 parser->type_definition_forbidden_message = saved_message;
16919 parser->integral_constant_expression_p
16920 = saved_integral_constant_expression_p;
16921 parser->non_integral_constant_expression_p
16922 = saved_non_integral_constant_expression_p;
16925 /* Parse to the closing `)'. */
16926 if (expr == error_mark_node || !parens.require_close (parser))
16928 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16929 /*consume_paren=*/true);
16930 expr = error_mark_node;
16933 /* If we got a parse error while tentative, bail out now. */
16934 if (cp_parser_error_occurred (parser))
16936 pop_deferring_access_checks ();
16937 return error_mark_node;
16940 if (!expr)
16941 /* Build auto. */
16942 expr = make_decltype_auto ();
16943 else
16944 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16945 tf_warning_or_error);
16947 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16948 it again. */
16949 start_token->type = CPP_DECLTYPE;
16950 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16951 start_token->tree_check_p = true;
16952 start_token->u.tree_check_value->value = expr;
16953 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16954 start_token->keyword = RID_MAX;
16956 location_t loc = start_token->location;
16957 loc = make_location (loc, loc, parser->lexer);
16958 start_token->location = loc;
16960 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16962 pop_to_parent_deferring_access_checks ();
16964 return expr;
16967 /* Special member functions [gram.special] */
16969 /* Parse a conversion-function-id.
16971 conversion-function-id:
16972 operator conversion-type-id
16974 Returns an IDENTIFIER_NODE representing the operator. */
16976 static tree
16977 cp_parser_conversion_function_id (cp_parser* parser)
16979 tree type;
16980 tree saved_scope;
16981 tree saved_qualifying_scope;
16982 tree saved_object_scope;
16983 tree pushed_scope = NULL_TREE;
16985 /* Look for the `operator' token. */
16986 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16987 return error_mark_node;
16988 /* When we parse the conversion-type-id, the current scope will be
16989 reset. However, we need that information in able to look up the
16990 conversion function later, so we save it here. */
16991 saved_scope = parser->scope;
16992 saved_qualifying_scope = parser->qualifying_scope;
16993 saved_object_scope = parser->object_scope;
16994 /* We must enter the scope of the class so that the names of
16995 entities declared within the class are available in the
16996 conversion-type-id. For example, consider:
16998 struct S {
16999 typedef int I;
17000 operator I();
17003 S::operator I() { ... }
17005 In order to see that `I' is a type-name in the definition, we
17006 must be in the scope of `S'. */
17007 if (saved_scope)
17008 pushed_scope = push_scope (saved_scope);
17009 /* Parse the conversion-type-id. */
17010 type = cp_parser_conversion_type_id (parser);
17011 /* Leave the scope of the class, if any. */
17012 if (pushed_scope)
17013 pop_scope (pushed_scope);
17014 /* Restore the saved scope. */
17015 parser->scope = saved_scope;
17016 parser->qualifying_scope = saved_qualifying_scope;
17017 parser->object_scope = saved_object_scope;
17018 /* If the TYPE is invalid, indicate failure. */
17019 if (type == error_mark_node)
17020 return error_mark_node;
17021 return make_conv_op_name (type);
17024 /* Parse a conversion-type-id:
17026 conversion-type-id:
17027 type-specifier-seq conversion-declarator [opt]
17029 Returns the TYPE specified. */
17031 static tree
17032 cp_parser_conversion_type_id (cp_parser* parser)
17034 tree attributes;
17035 cp_decl_specifier_seq type_specifiers;
17036 cp_declarator *declarator;
17037 tree type_specified;
17038 const char *saved_message;
17040 /* Parse the attributes. */
17041 attributes = cp_parser_attributes_opt (parser);
17043 saved_message = parser->type_definition_forbidden_message;
17044 parser->type_definition_forbidden_message
17045 = G_("types may not be defined in a conversion-type-id");
17047 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17048 optional in conversion-type-id. */
17049 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17050 /*is_declaration=*/false,
17051 /*is_trailing_return=*/false,
17052 &type_specifiers);
17054 parser->type_definition_forbidden_message = saved_message;
17056 /* If that didn't work, stop. */
17057 if (type_specifiers.type == error_mark_node)
17058 return error_mark_node;
17059 /* Parse the conversion-declarator. */
17060 declarator = cp_parser_conversion_declarator_opt (parser);
17062 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17063 /*initialized=*/0, &attributes);
17064 if (attributes)
17065 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17067 /* Don't give this error when parsing tentatively. This happens to
17068 work because we always parse this definitively once. */
17069 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17070 && type_uses_auto (type_specified))
17072 if (cxx_dialect < cxx14)
17074 error ("invalid use of %<auto%> in conversion operator");
17075 return error_mark_node;
17077 else if (template_parm_scope_p ())
17078 warning (0, "use of %<auto%> in member template "
17079 "conversion operator can never be deduced");
17082 return type_specified;
17085 /* Parse an (optional) conversion-declarator.
17087 conversion-declarator:
17088 ptr-operator conversion-declarator [opt]
17092 static cp_declarator *
17093 cp_parser_conversion_declarator_opt (cp_parser* parser)
17095 enum tree_code code;
17096 tree class_type, std_attributes = NULL_TREE;
17097 cp_cv_quals cv_quals;
17099 /* We don't know if there's a ptr-operator next, or not. */
17100 cp_parser_parse_tentatively (parser);
17101 /* Try the ptr-operator. */
17102 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17103 &std_attributes);
17104 /* If it worked, look for more conversion-declarators. */
17105 if (cp_parser_parse_definitely (parser))
17107 cp_declarator *declarator;
17109 /* Parse another optional declarator. */
17110 declarator = cp_parser_conversion_declarator_opt (parser);
17112 declarator = cp_parser_make_indirect_declarator
17113 (code, class_type, cv_quals, declarator, std_attributes);
17115 return declarator;
17118 return NULL;
17121 /* Parse an (optional) ctor-initializer.
17123 ctor-initializer:
17124 : mem-initializer-list */
17126 static void
17127 cp_parser_ctor_initializer_opt (cp_parser* parser)
17129 /* If the next token is not a `:', then there is no
17130 ctor-initializer. */
17131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17133 /* Do default initialization of any bases and members. */
17134 if (DECL_CONSTRUCTOR_P (current_function_decl))
17135 finish_mem_initializers (NULL_TREE);
17136 return;
17139 /* Consume the `:' token. */
17140 cp_lexer_consume_token (parser->lexer);
17141 /* And the mem-initializer-list. */
17142 cp_parser_mem_initializer_list (parser);
17145 /* Parse a mem-initializer-list.
17147 mem-initializer-list:
17148 mem-initializer ... [opt]
17149 mem-initializer ... [opt] , mem-initializer-list */
17151 static void
17152 cp_parser_mem_initializer_list (cp_parser* parser)
17154 tree mem_initializer_list = NULL_TREE;
17155 tree target_ctor = error_mark_node;
17156 cp_token *token = cp_lexer_peek_token (parser->lexer);
17158 /* Let the semantic analysis code know that we are starting the
17159 mem-initializer-list. */
17160 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17161 error_at (token->location,
17162 "only constructors take member initializers");
17164 /* Loop through the list. */
17165 while (true)
17167 tree mem_initializer;
17169 token = cp_lexer_peek_token (parser->lexer);
17170 /* Parse the mem-initializer. */
17171 mem_initializer = cp_parser_mem_initializer (parser);
17172 /* If the next token is a `...', we're expanding member initializers. */
17173 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17174 if (ellipsis
17175 || (mem_initializer != error_mark_node
17176 && check_for_bare_parameter_packs (TREE_PURPOSE
17177 (mem_initializer))))
17179 /* Consume the `...'. */
17180 if (ellipsis)
17181 cp_lexer_consume_token (parser->lexer);
17183 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17184 can be expanded but members cannot. */
17185 if (mem_initializer != error_mark_node
17186 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17188 error_at (token->location,
17189 "cannot expand initializer for member %qD",
17190 TREE_PURPOSE (mem_initializer));
17191 mem_initializer = error_mark_node;
17194 /* Construct the pack expansion type. */
17195 if (mem_initializer != error_mark_node)
17196 mem_initializer = make_pack_expansion (mem_initializer);
17198 if (target_ctor != error_mark_node
17199 && mem_initializer != error_mark_node)
17201 error ("mem-initializer for %qD follows constructor delegation",
17202 TREE_PURPOSE (mem_initializer));
17203 mem_initializer = error_mark_node;
17205 /* Look for a target constructor. */
17206 if (mem_initializer != error_mark_node
17207 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17208 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17210 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17211 if (mem_initializer_list)
17213 error ("constructor delegation follows mem-initializer for %qD",
17214 TREE_PURPOSE (mem_initializer_list));
17215 mem_initializer = error_mark_node;
17217 target_ctor = mem_initializer;
17219 /* Add it to the list, unless it was erroneous. */
17220 if (mem_initializer != error_mark_node)
17222 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17223 mem_initializer_list = mem_initializer;
17225 /* If the next token is not a `,', we're done. */
17226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17227 break;
17228 /* Consume the `,' token. */
17229 cp_lexer_consume_token (parser->lexer);
17232 /* Perform semantic analysis. */
17233 if (DECL_CONSTRUCTOR_P (current_function_decl))
17234 finish_mem_initializers (mem_initializer_list);
17237 /* Parse a mem-initializer.
17239 mem-initializer:
17240 mem-initializer-id ( expression-list [opt] )
17241 mem-initializer-id braced-init-list
17243 GNU extension:
17245 mem-initializer:
17246 ( expression-list [opt] )
17248 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17249 class) or FIELD_DECL (for a non-static data member) to initialize;
17250 the TREE_VALUE is the expression-list. An empty initialization
17251 list is represented by void_list_node. */
17253 static tree
17254 cp_parser_mem_initializer (cp_parser* parser)
17256 tree mem_initializer_id;
17257 tree expression_list;
17258 tree member;
17259 cp_token *token = cp_lexer_peek_token (parser->lexer);
17261 /* Find out what is being initialized. */
17262 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17264 permerror (token->location,
17265 "anachronistic old-style base class initializer");
17266 mem_initializer_id = NULL_TREE;
17268 else
17270 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17271 if (mem_initializer_id == error_mark_node)
17272 return mem_initializer_id;
17274 member = expand_member_init (mem_initializer_id);
17275 if (member && !DECL_P (member))
17276 in_base_initializer = 1;
17278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17280 cp_lexer_set_source_position (parser->lexer);
17281 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17282 expression_list = cp_parser_braced_list (parser);
17283 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17284 expression_list = build_tree_list (NULL_TREE, expression_list);
17286 else
17288 vec<tree, va_gc> *vec;
17289 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17290 /*cast_p=*/false,
17291 /*allow_expansion_p=*/true,
17292 /*non_constant_p=*/NULL,
17293 /*close_paren_loc=*/NULL,
17294 /*wrap_locations_p=*/true);
17295 if (vec == NULL)
17296 return error_mark_node;
17297 expression_list = build_tree_list_vec (vec);
17298 release_tree_vector (vec);
17301 if (expression_list == error_mark_node)
17302 return error_mark_node;
17303 if (!expression_list)
17304 expression_list = void_type_node;
17306 in_base_initializer = 0;
17308 if (!member)
17309 return error_mark_node;
17310 tree node = build_tree_list (member, expression_list);
17312 /* We can't attach the source location of this initializer directly to
17313 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17314 within the TREE_TYPE of the list node. */
17315 location_t loc
17316 = make_location (token->location, token->location, parser->lexer);
17317 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17318 SET_EXPR_LOCATION (dummy, loc);
17319 TREE_TYPE (node) = dummy;
17321 return node;
17324 /* Parse a mem-initializer-id.
17326 mem-initializer-id:
17327 :: [opt] nested-name-specifier [opt] class-name
17328 decltype-specifier (C++11)
17329 identifier
17331 Returns a TYPE indicating the class to be initialized for the first
17332 production (and the second in C++11). Returns an IDENTIFIER_NODE
17333 indicating the data member to be initialized for the last production. */
17335 static tree
17336 cp_parser_mem_initializer_id (cp_parser* parser)
17338 bool global_scope_p;
17339 bool nested_name_specifier_p;
17340 bool template_p = false;
17341 tree id;
17343 cp_token *token = cp_lexer_peek_token (parser->lexer);
17345 /* `typename' is not allowed in this context ([temp.res]). */
17346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17348 error_at (token->location,
17349 "keyword %<typename%> not allowed in this context (a qualified "
17350 "member initializer is implicitly a type)");
17351 cp_lexer_consume_token (parser->lexer);
17353 /* Look for the optional `::' operator. */
17354 global_scope_p
17355 = (cp_parser_global_scope_opt (parser,
17356 /*current_scope_valid_p=*/false)
17357 != NULL_TREE);
17358 /* Look for the optional nested-name-specifier. The simplest way to
17359 implement:
17361 [temp.res]
17363 The keyword `typename' is not permitted in a base-specifier or
17364 mem-initializer; in these contexts a qualified name that
17365 depends on a template-parameter is implicitly assumed to be a
17366 type name.
17368 is to assume that we have seen the `typename' keyword at this
17369 point. */
17370 nested_name_specifier_p
17371 = (cp_parser_nested_name_specifier_opt (parser,
17372 /*typename_keyword_p=*/true,
17373 /*check_dependency_p=*/true,
17374 /*type_p=*/true,
17375 /*is_declaration=*/true)
17376 != NULL_TREE);
17377 if (nested_name_specifier_p)
17378 template_p = cp_parser_optional_template_keyword (parser);
17379 /* If there is a `::' operator or a nested-name-specifier, then we
17380 are definitely looking for a class-name. */
17381 if (global_scope_p || nested_name_specifier_p)
17382 return cp_parser_class_name (parser,
17383 /*typename_keyword_p=*/true,
17384 /*template_keyword_p=*/template_p,
17385 typename_type,
17386 /*check_dependency_p=*/true,
17387 /*class_head_p=*/false,
17388 /*is_declaration=*/true);
17389 /* Otherwise, we could also be looking for an ordinary identifier. */
17390 cp_parser_parse_tentatively (parser);
17391 if (cp_lexer_next_token_is_decltype (parser->lexer))
17392 /* Try a decltype-specifier. */
17393 id = cp_parser_decltype (parser);
17394 else
17395 /* Otherwise, try a class-name. */
17396 id = cp_parser_class_name (parser,
17397 /*typename_keyword_p=*/true,
17398 /*template_keyword_p=*/false,
17399 none_type,
17400 /*check_dependency_p=*/true,
17401 /*class_head_p=*/false,
17402 /*is_declaration=*/true);
17403 /* If we found one, we're done. */
17404 if (cp_parser_parse_definitely (parser))
17405 return id;
17406 /* Otherwise, look for an ordinary identifier. */
17407 return cp_parser_identifier (parser);
17410 /* Overloading [gram.over] */
17412 /* Parse an operator-function-id.
17414 operator-function-id:
17415 operator operator
17417 Returns an IDENTIFIER_NODE for the operator which is a
17418 human-readable spelling of the identifier, e.g., `operator +'. */
17420 static cp_expr
17421 cp_parser_operator_function_id (cp_parser* parser)
17423 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17424 /* Look for the `operator' keyword. */
17425 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17426 return error_mark_node;
17427 /* And then the name of the operator itself. */
17428 return cp_parser_operator (parser, start_loc);
17431 /* Return an identifier node for a user-defined literal operator.
17432 The suffix identifier is chained to the operator name identifier. */
17434 tree
17435 cp_literal_operator_id (const char* name)
17437 tree identifier;
17438 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17439 + strlen (name) + 10);
17440 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17441 identifier = get_identifier (buffer);
17442 XDELETEVEC (buffer);
17444 return identifier;
17447 /* Parse an operator.
17449 operator:
17450 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17451 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17452 || ++ -- , ->* -> () []
17454 GNU Extensions:
17456 operator:
17457 <? >? <?= >?=
17459 Returns an IDENTIFIER_NODE for the operator which is a
17460 human-readable spelling of the identifier, e.g., `operator +'. */
17462 static cp_expr
17463 cp_parser_operator (cp_parser* parser, location_t start_loc)
17465 tree id = NULL_TREE;
17466 cp_token *token;
17467 bool utf8 = false;
17469 /* Peek at the next token. */
17470 token = cp_lexer_peek_token (parser->lexer);
17472 location_t end_loc = token->location;
17474 /* Figure out which operator we have. */
17475 enum tree_code op = ERROR_MARK;
17476 bool assop = false;
17477 bool consumed = false;
17478 switch (token->type)
17480 case CPP_KEYWORD:
17482 /* The keyword should be either `new', `delete' or `co_await'. */
17483 if (token->keyword == RID_NEW)
17484 op = NEW_EXPR;
17485 else if (token->keyword == RID_DELETE)
17486 op = DELETE_EXPR;
17487 else if (token->keyword == RID_CO_AWAIT)
17488 op = CO_AWAIT_EXPR;
17489 else
17490 break;
17492 /* Consume the `new', `delete' or co_await token. */
17493 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17495 /* Peek at the next token. */
17496 token = cp_lexer_peek_token (parser->lexer);
17497 /* If it's a `[' token then this is the array variant of the
17498 operator. */
17499 if (token->type == CPP_OPEN_SQUARE
17500 && op != CO_AWAIT_EXPR)
17502 /* Consume the `[' token. */
17503 cp_lexer_consume_token (parser->lexer);
17504 /* Look for the `]' token. */
17505 if (cp_token *close_token
17506 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17507 end_loc = close_token->location;
17508 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17510 consumed = true;
17511 break;
17514 case CPP_PLUS:
17515 op = PLUS_EXPR;
17516 break;
17518 case CPP_MINUS:
17519 op = MINUS_EXPR;
17520 break;
17522 case CPP_MULT:
17523 op = MULT_EXPR;
17524 break;
17526 case CPP_DIV:
17527 op = TRUNC_DIV_EXPR;
17528 break;
17530 case CPP_MOD:
17531 op = TRUNC_MOD_EXPR;
17532 break;
17534 case CPP_XOR:
17535 op = BIT_XOR_EXPR;
17536 break;
17538 case CPP_AND:
17539 op = BIT_AND_EXPR;
17540 break;
17542 case CPP_OR:
17543 op = BIT_IOR_EXPR;
17544 break;
17546 case CPP_COMPL:
17547 op = BIT_NOT_EXPR;
17548 break;
17550 case CPP_NOT:
17551 op = TRUTH_NOT_EXPR;
17552 break;
17554 case CPP_EQ:
17555 assop = true;
17556 op = NOP_EXPR;
17557 break;
17559 case CPP_LESS:
17560 op = LT_EXPR;
17561 break;
17563 case CPP_GREATER:
17564 op = GT_EXPR;
17565 break;
17567 case CPP_PLUS_EQ:
17568 assop = true;
17569 op = PLUS_EXPR;
17570 break;
17572 case CPP_MINUS_EQ:
17573 assop = true;
17574 op = MINUS_EXPR;
17575 break;
17577 case CPP_MULT_EQ:
17578 assop = true;
17579 op = MULT_EXPR;
17580 break;
17582 case CPP_DIV_EQ:
17583 assop = true;
17584 op = TRUNC_DIV_EXPR;
17585 break;
17587 case CPP_MOD_EQ:
17588 assop = true;
17589 op = TRUNC_MOD_EXPR;
17590 break;
17592 case CPP_XOR_EQ:
17593 assop = true;
17594 op = BIT_XOR_EXPR;
17595 break;
17597 case CPP_AND_EQ:
17598 assop = true;
17599 op = BIT_AND_EXPR;
17600 break;
17602 case CPP_OR_EQ:
17603 assop = true;
17604 op = BIT_IOR_EXPR;
17605 break;
17607 case CPP_LSHIFT:
17608 op = LSHIFT_EXPR;
17609 break;
17611 case CPP_RSHIFT:
17612 op = RSHIFT_EXPR;
17613 break;
17615 case CPP_LSHIFT_EQ:
17616 assop = true;
17617 op = LSHIFT_EXPR;
17618 break;
17620 case CPP_RSHIFT_EQ:
17621 assop = true;
17622 op = RSHIFT_EXPR;
17623 break;
17625 case CPP_EQ_EQ:
17626 op = EQ_EXPR;
17627 break;
17629 case CPP_NOT_EQ:
17630 op = NE_EXPR;
17631 break;
17633 case CPP_LESS_EQ:
17634 op = LE_EXPR;
17635 break;
17637 case CPP_GREATER_EQ:
17638 op = GE_EXPR;
17639 break;
17641 case CPP_SPACESHIP:
17642 op = SPACESHIP_EXPR;
17643 break;
17645 case CPP_AND_AND:
17646 op = TRUTH_ANDIF_EXPR;
17647 break;
17649 case CPP_OR_OR:
17650 op = TRUTH_ORIF_EXPR;
17651 break;
17653 case CPP_PLUS_PLUS:
17654 op = POSTINCREMENT_EXPR;
17655 break;
17657 case CPP_MINUS_MINUS:
17658 op = PREDECREMENT_EXPR;
17659 break;
17661 case CPP_COMMA:
17662 op = COMPOUND_EXPR;
17663 break;
17665 case CPP_DEREF_STAR:
17666 op = MEMBER_REF;
17667 break;
17669 case CPP_DEREF:
17670 op = COMPONENT_REF;
17671 break;
17673 case CPP_QUERY:
17674 op = COND_EXPR;
17675 /* Consume the `?'. */
17676 cp_lexer_consume_token (parser->lexer);
17677 /* Look for the matching `:'. */
17678 cp_parser_require (parser, CPP_COLON, RT_COLON);
17679 consumed = true;
17680 break;
17682 case CPP_OPEN_PAREN:
17684 /* Consume the `('. */
17685 matching_parens parens;
17686 parens.consume_open (parser);
17687 /* Look for the matching `)'. */
17688 token = parens.require_close (parser);
17689 if (token)
17690 end_loc = token->location;
17691 op = CALL_EXPR;
17692 consumed = true;
17693 break;
17696 case CPP_OPEN_SQUARE:
17697 /* Consume the `['. */
17698 cp_lexer_consume_token (parser->lexer);
17699 /* Look for the matching `]'. */
17700 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17701 if (token)
17702 end_loc = token->location;
17703 op = ARRAY_REF;
17704 consumed = true;
17705 break;
17707 case CPP_UTF8STRING:
17708 case CPP_UTF8STRING_USERDEF:
17709 utf8 = true;
17710 /* FALLTHRU */
17711 case CPP_STRING:
17712 case CPP_WSTRING:
17713 case CPP_STRING16:
17714 case CPP_STRING32:
17715 case CPP_STRING_USERDEF:
17716 case CPP_WSTRING_USERDEF:
17717 case CPP_STRING16_USERDEF:
17718 case CPP_STRING32_USERDEF:
17720 tree string_tree;
17721 int sz, len;
17723 if (cxx_dialect == cxx98)
17724 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17726 /* Consume the string. */
17727 cp_expr str = cp_parser_userdef_string_literal (parser,
17728 /*lookup_udlit=*/false);
17729 if (str == error_mark_node)
17730 return error_mark_node;
17731 else if (TREE_CODE (str) == USERDEF_LITERAL)
17733 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17734 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17735 end_loc = str.get_location ();
17737 else
17739 string_tree = str;
17740 /* Look for the suffix identifier. */
17741 token = cp_lexer_peek_token (parser->lexer);
17742 if (token->type == CPP_NAME)
17744 id = cp_parser_identifier (parser);
17745 end_loc = token->location;
17747 else if (token->type == CPP_KEYWORD)
17749 error ("unexpected keyword;"
17750 " remove space between quotes and suffix identifier");
17751 return error_mark_node;
17753 else
17755 error ("expected suffix identifier");
17756 return error_mark_node;
17759 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17760 (TREE_TYPE (TREE_TYPE (string_tree))));
17761 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17762 if (len != 0)
17764 error ("expected empty string after %<operator%> keyword");
17765 return error_mark_node;
17767 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17768 != char_type_node)
17770 error ("invalid encoding prefix in literal operator");
17771 return error_mark_node;
17773 if (id != error_mark_node)
17775 const char *name = IDENTIFIER_POINTER (id);
17776 id = cp_literal_operator_id (name);
17778 /* Generate a location of the form:
17779 "" _suffix_identifier
17780 ^~~~~~~~~~~~~~~~~~~~~
17781 with caret == start at the start token, finish at the end of the
17782 suffix identifier. */
17783 location_t combined_loc
17784 = make_location (start_loc, start_loc, parser->lexer);
17785 return cp_expr (id, combined_loc);
17788 default:
17789 /* Anything else is an error. */
17790 break;
17793 /* If we have selected an identifier, we need to consume the
17794 operator token. */
17795 if (op != ERROR_MARK)
17797 id = ovl_op_identifier (assop, op);
17798 if (!consumed)
17799 cp_lexer_consume_token (parser->lexer);
17801 /* Otherwise, no valid operator name was present. */
17802 else
17804 cp_parser_error (parser, "expected operator");
17805 id = error_mark_node;
17808 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17809 return cp_expr (id, start_loc);
17812 /* Parse a template-declaration.
17814 template-declaration:
17815 export [opt] template < template-parameter-list > declaration
17817 If MEMBER_P is TRUE, this template-declaration occurs within a
17818 class-specifier.
17820 The grammar rule given by the standard isn't correct. What
17821 is really meant is:
17823 template-declaration:
17824 export [opt] template-parameter-list-seq
17825 decl-specifier-seq [opt] init-declarator [opt] ;
17826 export [opt] template-parameter-list-seq
17827 function-definition
17829 template-parameter-list-seq:
17830 template-parameter-list-seq [opt]
17831 template < template-parameter-list >
17833 Concept Extensions:
17835 template-parameter-list-seq:
17836 template < template-parameter-list > requires-clause [opt]
17838 requires-clause:
17839 requires logical-or-expression */
17841 static void
17842 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17844 /* Check for `export'. */
17845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17847 /* Consume the `export' token. */
17848 cp_lexer_consume_token (parser->lexer);
17849 /* Warn that this use of export is deprecated. */
17850 if (cxx_dialect < cxx11)
17851 warning (0, "keyword %<export%> not implemented, and will be ignored");
17852 else if (cxx_dialect < cxx20)
17853 warning (0, "keyword %<export%> is deprecated, and is ignored");
17854 else
17855 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17858 cp_parser_template_declaration_after_export (parser, member_p);
17861 /* Parse a template-parameter-list.
17863 template-parameter-list:
17864 template-parameter
17865 template-parameter-list , template-parameter
17867 Returns a TREE_LIST. Each node represents a template parameter.
17868 The nodes are connected via their TREE_CHAINs. */
17870 static tree
17871 cp_parser_template_parameter_list (cp_parser* parser)
17873 tree parameter_list = NULL_TREE;
17875 /* Don't create wrapper nodes within a template-parameter-list,
17876 since we don't want to have different types based on the
17877 spelling location of constants and decls within them. */
17878 auto_suppress_location_wrappers sentinel;
17880 begin_template_parm_list ();
17882 /* The loop below parses the template parms. We first need to know
17883 the total number of template parms to be able to compute proper
17884 canonical types of each dependent type. So after the loop, when
17885 we know the total number of template parms,
17886 end_template_parm_list computes the proper canonical types and
17887 fixes up the dependent types accordingly. */
17888 while (true)
17890 tree parameter;
17891 bool is_non_type;
17892 bool is_parameter_pack;
17893 location_t parm_loc;
17895 /* Parse the template-parameter. */
17896 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17897 parameter = cp_parser_template_parameter (parser,
17898 &is_non_type,
17899 &is_parameter_pack);
17900 /* Add it to the list. */
17901 if (parameter != error_mark_node)
17902 parameter_list = process_template_parm (parameter_list,
17903 parm_loc,
17904 parameter,
17905 is_non_type,
17906 is_parameter_pack);
17907 else
17909 tree err_parm = build_tree_list (parameter, parameter);
17910 parameter_list = chainon (parameter_list, err_parm);
17913 /* If the next token is not a `,', we're done. */
17914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17915 break;
17916 /* Otherwise, consume the `,' token. */
17917 cp_lexer_consume_token (parser->lexer);
17920 return end_template_parm_list (parameter_list);
17923 /* Parse a introduction-list.
17925 introduction-list:
17926 introduced-parameter
17927 introduction-list , introduced-parameter
17929 introduced-parameter:
17930 ...[opt] identifier
17932 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17933 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17934 WILDCARD_DECL will also have DECL_NAME set and token location in
17935 DECL_SOURCE_LOCATION. */
17937 static tree
17938 cp_parser_introduction_list (cp_parser *parser)
17940 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17942 while (true)
17944 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17945 if (is_pack)
17946 cp_lexer_consume_token (parser->lexer);
17948 tree identifier = cp_parser_identifier (parser);
17949 if (identifier == error_mark_node)
17950 break;
17952 /* Build placeholder. */
17953 tree parm = build_nt (WILDCARD_DECL);
17954 DECL_SOURCE_LOCATION (parm)
17955 = cp_lexer_peek_token (parser->lexer)->location;
17956 DECL_NAME (parm) = identifier;
17957 WILDCARD_PACK_P (parm) = is_pack;
17958 vec_safe_push (introduction_vec, parm);
17960 /* If the next token is not a `,', we're done. */
17961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17962 break;
17963 /* Otherwise, consume the `,' token. */
17964 cp_lexer_consume_token (parser->lexer);
17967 /* Convert the vec into a TREE_VEC. */
17968 tree introduction_list = make_tree_vec (introduction_vec->length ());
17969 unsigned int n;
17970 tree parm;
17971 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17972 TREE_VEC_ELT (introduction_list, n) = parm;
17974 release_tree_vector (introduction_vec);
17975 return introduction_list;
17978 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17979 is an abstract declarator. */
17981 static inline cp_declarator*
17982 get_id_declarator (cp_declarator *declarator)
17984 cp_declarator *d = declarator;
17985 while (d && d->kind != cdk_id)
17986 d = d->declarator;
17987 return d;
17990 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17991 is an abstract declarator. */
17993 static inline tree
17994 get_unqualified_id (cp_declarator *declarator)
17996 declarator = get_id_declarator (declarator);
17997 if (declarator)
17998 return declarator->u.id.unqualified_name;
17999 else
18000 return NULL_TREE;
18003 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18005 static inline bool
18006 is_constrained_parameter (tree type)
18008 return (type
18009 && TREE_CODE (type) == TYPE_DECL
18010 && CONSTRAINED_PARM_CONCEPT (type)
18011 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18014 /* Returns true if PARM declares a constrained-parameter. */
18016 static inline bool
18017 is_constrained_parameter (cp_parameter_declarator *parm)
18019 return is_constrained_parameter (parm->decl_specifiers.type);
18022 /* Check that the type parameter is only a declarator-id, and that its
18023 type is not cv-qualified. */
18025 bool
18026 cp_parser_check_constrained_type_parm (cp_parser *parser,
18027 cp_parameter_declarator *parm)
18029 if (!parm->declarator)
18030 return true;
18032 if (parm->declarator->kind != cdk_id)
18034 cp_parser_error (parser, "invalid constrained type parameter");
18035 return false;
18038 /* Don't allow cv-qualified type parameters. */
18039 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18040 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18042 cp_parser_error (parser, "cv-qualified type parameter");
18043 return false;
18046 return true;
18049 /* Finish parsing/processing a template type parameter and checking
18050 various restrictions. */
18052 static inline tree
18053 cp_parser_constrained_type_template_parm (cp_parser *parser,
18054 tree id,
18055 cp_parameter_declarator* parmdecl)
18057 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18058 return finish_template_type_parm (class_type_node, id);
18059 else
18060 return error_mark_node;
18063 static tree
18064 finish_constrained_template_template_parm (tree proto, tree id)
18066 /* FIXME: This should probably be copied, and we may need to adjust
18067 the template parameter depths. */
18068 tree saved_parms = current_template_parms;
18069 begin_template_parm_list ();
18070 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18071 end_template_parm_list ();
18073 tree parm = finish_template_template_parm (class_type_node, id);
18074 current_template_parms = saved_parms;
18076 return parm;
18079 /* Finish parsing/processing a template template parameter by borrowing
18080 the template parameter list from the prototype parameter. */
18082 static tree
18083 cp_parser_constrained_template_template_parm (cp_parser *parser,
18084 tree proto,
18085 tree id,
18086 cp_parameter_declarator *parmdecl)
18088 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18089 return error_mark_node;
18090 return finish_constrained_template_template_parm (proto, id);
18093 /* Create a new non-type template parameter from the given PARM
18094 declarator. */
18096 static tree
18097 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18098 cp_parameter_declarator *parm)
18100 *is_non_type = true;
18101 cp_declarator *decl = parm->declarator;
18102 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18103 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18104 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18107 /* Build a constrained template parameter based on the PARMDECL
18108 declarator. The type of PARMDECL is the constrained type, which
18109 refers to the prototype template parameter that ultimately
18110 specifies the type of the declared parameter. */
18112 static tree
18113 finish_constrained_parameter (cp_parser *parser,
18114 cp_parameter_declarator *parmdecl,
18115 bool *is_non_type)
18117 tree decl = parmdecl->decl_specifiers.type;
18118 tree id = get_unqualified_id (parmdecl->declarator);
18119 tree def = parmdecl->default_argument;
18120 tree proto = DECL_INITIAL (decl);
18122 /* Build the parameter. Return an error if the declarator was invalid. */
18123 tree parm;
18124 if (TREE_CODE (proto) == TYPE_DECL)
18125 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18126 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18127 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18128 parmdecl);
18129 else
18130 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18131 if (parm == error_mark_node)
18132 return error_mark_node;
18134 /* Finish the parameter decl and create a node attaching the
18135 default argument and constraint. */
18136 parm = build_tree_list (def, parm);
18137 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18139 return parm;
18142 /* Returns true if the parsed type actually represents the declaration
18143 of a type template-parameter. */
18145 static bool
18146 declares_constrained_type_template_parameter (tree type)
18148 return (is_constrained_parameter (type)
18149 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18152 /* Returns true if the parsed type actually represents the declaration of
18153 a template template-parameter. */
18155 static bool
18156 declares_constrained_template_template_parameter (tree type)
18158 return (is_constrained_parameter (type)
18159 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18162 /* Parse a default argument for a type template-parameter.
18163 Note that diagnostics are handled in cp_parser_template_parameter. */
18165 static tree
18166 cp_parser_default_type_template_argument (cp_parser *parser)
18168 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18170 /* Consume the `=' token. */
18171 cp_lexer_consume_token (parser->lexer);
18173 cp_token *token = cp_lexer_peek_token (parser->lexer);
18175 /* Tell cp_parser_lambda_expression this is a default argument. */
18176 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18177 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18179 /* Parse the default-argument. */
18180 push_deferring_access_checks (dk_no_deferred);
18181 tree default_argument = cp_parser_type_id (parser,
18182 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18183 NULL);
18184 pop_deferring_access_checks ();
18186 if (flag_concepts && type_uses_auto (default_argument))
18188 error_at (token->location,
18189 "invalid use of %<auto%> in default template argument");
18190 return error_mark_node;
18193 return default_argument;
18196 /* Parse a default argument for a template template-parameter. */
18198 static tree
18199 cp_parser_default_template_template_argument (cp_parser *parser)
18201 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18203 bool is_template;
18205 /* Consume the `='. */
18206 cp_lexer_consume_token (parser->lexer);
18207 /* Parse the id-expression. */
18208 push_deferring_access_checks (dk_no_deferred);
18209 /* save token before parsing the id-expression, for error
18210 reporting */
18211 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18212 tree default_argument
18213 = cp_parser_id_expression (parser,
18214 /*template_keyword_p=*/false,
18215 /*check_dependency_p=*/true,
18216 /*template_p=*/&is_template,
18217 /*declarator_p=*/false,
18218 /*optional_p=*/false);
18219 if (TREE_CODE (default_argument) == TYPE_DECL)
18220 /* If the id-expression was a template-id that refers to
18221 a template-class, we already have the declaration here,
18222 so no further lookup is needed. */
18224 else
18225 /* Look up the name. */
18226 default_argument
18227 = cp_parser_lookup_name (parser, default_argument,
18228 none_type,
18229 /*is_template=*/is_template,
18230 /*is_namespace=*/false,
18231 /*check_dependency=*/true,
18232 /*ambiguous_decls=*/NULL,
18233 token->location);
18234 /* See if the default argument is valid. */
18235 default_argument = check_template_template_default_arg (default_argument);
18236 pop_deferring_access_checks ();
18237 return default_argument;
18240 /* Parse a template-parameter.
18242 template-parameter:
18243 type-parameter
18244 parameter-declaration
18246 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18247 the parameter. The TREE_PURPOSE is the default value, if any.
18248 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18249 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18250 set to true iff this parameter is a parameter pack. */
18252 static tree
18253 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18254 bool *is_parameter_pack)
18256 cp_token *token;
18257 cp_parameter_declarator *parameter_declarator;
18258 tree parm;
18260 /* Assume it is a type parameter or a template parameter. */
18261 *is_non_type = false;
18262 /* Assume it not a parameter pack. */
18263 *is_parameter_pack = false;
18264 /* Peek at the next token. */
18265 token = cp_lexer_peek_token (parser->lexer);
18266 /* If it is `template', we have a type-parameter. */
18267 if (token->keyword == RID_TEMPLATE)
18268 return cp_parser_type_parameter (parser, is_parameter_pack);
18269 /* If it is `class' or `typename' we do not know yet whether it is a
18270 type parameter or a non-type parameter. Consider:
18272 template <typename T, typename T::X X> ...
18276 template <class C, class D*> ...
18278 Here, the first parameter is a type parameter, and the second is
18279 a non-type parameter. We can tell by looking at the token after
18280 the identifier -- if it is a `,', `=', or `>' then we have a type
18281 parameter. */
18282 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18284 /* Peek at the token after `class' or `typename'. */
18285 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18286 /* If it's an ellipsis, we have a template type parameter
18287 pack. */
18288 if (token->type == CPP_ELLIPSIS)
18289 return cp_parser_type_parameter (parser, is_parameter_pack);
18290 /* If it's an identifier, skip it. */
18291 if (token->type == CPP_NAME)
18292 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18293 /* Now, see if the token looks like the end of a template
18294 parameter. */
18295 if (token->type == CPP_COMMA
18296 || token->type == CPP_EQ
18297 || token->type == CPP_GREATER)
18298 return cp_parser_type_parameter (parser, is_parameter_pack);
18301 /* Otherwise, it is a non-type parameter or a constrained parameter.
18303 [temp.param]
18305 When parsing a default template-argument for a non-type
18306 template-parameter, the first non-nested `>' is taken as the end
18307 of the template parameter-list rather than a greater-than
18308 operator. */
18309 parameter_declarator
18310 = cp_parser_parameter_declaration (parser,
18311 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18312 /*template_parm_p=*/true,
18313 /*parenthesized_p=*/NULL);
18315 if (!parameter_declarator)
18316 return error_mark_node;
18318 /* If the parameter declaration is marked as a parameter pack, set
18319 *IS_PARAMETER_PACK to notify the caller. */
18320 if (parameter_declarator->template_parameter_pack_p)
18321 *is_parameter_pack = true;
18323 if (parameter_declarator->default_argument)
18325 /* Can happen in some cases of erroneous input (c++/34892). */
18326 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18327 /* Consume the `...' for better error recovery. */
18328 cp_lexer_consume_token (parser->lexer);
18331 /* The parameter may have been constrained type parameter. */
18332 if (is_constrained_parameter (parameter_declarator))
18333 return finish_constrained_parameter (parser,
18334 parameter_declarator,
18335 is_non_type);
18337 // Now we're sure that the parameter is a non-type parameter.
18338 *is_non_type = true;
18340 parm = grokdeclarator (parameter_declarator->declarator,
18341 &parameter_declarator->decl_specifiers,
18342 TPARM, /*initialized=*/0,
18343 /*attrlist=*/NULL);
18344 if (parm == error_mark_node)
18345 return error_mark_node;
18347 return build_tree_list (parameter_declarator->default_argument, parm);
18350 /* Parse a type-parameter.
18352 type-parameter:
18353 class identifier [opt]
18354 class identifier [opt] = type-id
18355 typename identifier [opt]
18356 typename identifier [opt] = type-id
18357 template < template-parameter-list > class identifier [opt]
18358 template < template-parameter-list > class identifier [opt]
18359 = id-expression
18361 GNU Extension (variadic templates):
18363 type-parameter:
18364 class ... identifier [opt]
18365 typename ... identifier [opt]
18367 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18368 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18369 the declaration of the parameter.
18371 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18373 static tree
18374 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18376 cp_token *token;
18377 tree parameter;
18379 /* Look for a keyword to tell us what kind of parameter this is. */
18380 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18381 if (!token)
18382 return error_mark_node;
18384 switch (token->keyword)
18386 case RID_CLASS:
18387 case RID_TYPENAME:
18389 tree identifier;
18390 tree default_argument;
18392 /* If the next token is an ellipsis, we have a template
18393 argument pack. */
18394 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18396 /* Consume the `...' token. */
18397 cp_lexer_consume_token (parser->lexer);
18398 maybe_warn_variadic_templates ();
18400 *is_parameter_pack = true;
18403 /* If the next token is an identifier, then it names the
18404 parameter. */
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18406 identifier = cp_parser_identifier (parser);
18407 else
18408 identifier = NULL_TREE;
18410 /* Create the parameter. */
18411 parameter = finish_template_type_parm (class_type_node, identifier);
18413 /* If the next token is an `=', we have a default argument. */
18414 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18416 default_argument
18417 = cp_parser_default_type_template_argument (parser);
18419 /* Template parameter packs cannot have default
18420 arguments. */
18421 if (*is_parameter_pack)
18423 if (identifier)
18424 error_at (token->location,
18425 "template parameter pack %qD cannot have a "
18426 "default argument", identifier);
18427 else
18428 error_at (token->location,
18429 "template parameter packs cannot have "
18430 "default arguments");
18431 default_argument = NULL_TREE;
18433 else if (check_for_bare_parameter_packs (default_argument))
18434 default_argument = error_mark_node;
18436 else
18437 default_argument = NULL_TREE;
18439 /* Create the combined representation of the parameter and the
18440 default argument. */
18441 parameter = build_tree_list (default_argument, parameter);
18443 break;
18445 case RID_TEMPLATE:
18447 tree identifier;
18448 tree default_argument;
18450 /* Look for the `<'. */
18451 cp_parser_require (parser, CPP_LESS, RT_LESS);
18452 /* Parse the template-parameter-list. */
18453 cp_parser_template_parameter_list (parser);
18454 /* Look for the `>'. */
18455 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18457 /* If template requirements are present, parse them. */
18458 if (flag_concepts)
18460 tree reqs = get_shorthand_constraints (current_template_parms);
18461 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18462 reqs = combine_constraint_expressions (reqs, dreqs);
18463 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18466 /* Look for the `class' or 'typename' keywords. */
18467 cp_parser_type_parameter_key (parser);
18468 /* If the next token is an ellipsis, we have a template
18469 argument pack. */
18470 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18472 /* Consume the `...' token. */
18473 cp_lexer_consume_token (parser->lexer);
18474 maybe_warn_variadic_templates ();
18476 *is_parameter_pack = true;
18478 /* If the next token is an `=', then there is a
18479 default-argument. If the next token is a `>', we are at
18480 the end of the parameter-list. If the next token is a `,',
18481 then we are at the end of this parameter. */
18482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18483 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18484 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18486 identifier = cp_parser_identifier (parser);
18487 /* Treat invalid names as if the parameter were nameless. */
18488 if (identifier == error_mark_node)
18489 identifier = NULL_TREE;
18491 else
18492 identifier = NULL_TREE;
18494 /* Create the template parameter. */
18495 parameter = finish_template_template_parm (class_type_node,
18496 identifier);
18498 /* If the next token is an `=', then there is a
18499 default-argument. */
18500 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18502 default_argument
18503 = cp_parser_default_template_template_argument (parser);
18505 /* Template parameter packs cannot have default
18506 arguments. */
18507 if (*is_parameter_pack)
18509 if (identifier)
18510 error_at (token->location,
18511 "template parameter pack %qD cannot "
18512 "have a default argument",
18513 identifier);
18514 else
18515 error_at (token->location, "template parameter packs cannot "
18516 "have default arguments");
18517 default_argument = NULL_TREE;
18520 else
18521 default_argument = NULL_TREE;
18523 /* Create the combined representation of the parameter and the
18524 default argument. */
18525 parameter = build_tree_list (default_argument, parameter);
18527 break;
18529 default:
18530 gcc_unreachable ();
18531 break;
18534 return parameter;
18537 /* Parse a template-id.
18539 template-id:
18540 template-name < template-argument-list [opt] >
18542 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18543 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18544 returned. Otherwise, if the template-name names a function, or set
18545 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18546 names a class, returns a TYPE_DECL for the specialization.
18548 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18549 uninstantiated templates. */
18551 static tree
18552 cp_parser_template_id (cp_parser *parser,
18553 bool template_keyword_p,
18554 bool check_dependency_p,
18555 enum tag_types tag_type,
18556 bool is_declaration)
18558 tree templ;
18559 tree arguments;
18560 tree template_id;
18561 cp_token_position start_of_id = 0;
18562 cp_token *next_token = NULL, *next_token_2 = NULL;
18563 bool is_identifier;
18565 /* If the next token corresponds to a template-id, there is no need
18566 to reparse it. */
18567 cp_token *token = cp_lexer_peek_token (parser->lexer);
18569 if (token->type == CPP_TEMPLATE_ID)
18571 cp_lexer_consume_token (parser->lexer);
18572 return saved_checks_value (token->u.tree_check_value);
18575 /* Avoid performing name lookup if there is no possibility of
18576 finding a template-id. */
18577 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18578 || (token->type == CPP_NAME
18579 && !cp_parser_nth_token_starts_template_argument_list_p
18580 (parser, 2)))
18582 cp_parser_error (parser, "expected template-id");
18583 return error_mark_node;
18586 /* Remember where the template-id starts. */
18587 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18588 start_of_id = cp_lexer_token_position (parser->lexer, false);
18590 push_deferring_access_checks (dk_deferred);
18592 /* Parse the template-name. */
18593 is_identifier = false;
18594 templ = cp_parser_template_name (parser, template_keyword_p,
18595 check_dependency_p,
18596 is_declaration,
18597 tag_type,
18598 &is_identifier);
18600 /* Push any access checks inside the firewall we're about to create. */
18601 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18602 pop_deferring_access_checks ();
18603 if (templ == error_mark_node || is_identifier)
18604 return templ;
18606 /* Since we're going to preserve any side-effects from this parse, set up a
18607 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18608 in the template arguments. */
18609 tentative_firewall firewall (parser);
18610 reopen_deferring_access_checks (checks);
18612 /* If we find the sequence `[:' after a template-name, it's probably
18613 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18614 parse correctly the argument list. */
18615 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18616 == CPP_OPEN_SQUARE)
18617 && next_token->flags & DIGRAPH
18618 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18619 == CPP_COLON)
18620 && !(next_token_2->flags & PREV_WHITE))
18622 cp_parser_parse_tentatively (parser);
18623 /* Change `:' into `::'. */
18624 next_token_2->type = CPP_SCOPE;
18625 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18626 CPP_LESS. */
18627 cp_lexer_consume_token (parser->lexer);
18629 /* Parse the arguments. */
18630 arguments = cp_parser_enclosed_template_argument_list (parser);
18631 if (!cp_parser_parse_definitely (parser))
18633 /* If we couldn't parse an argument list, then we revert our changes
18634 and return simply an error. Maybe this is not a template-id
18635 after all. */
18636 next_token_2->type = CPP_COLON;
18637 cp_parser_error (parser, "expected %<<%>");
18638 pop_deferring_access_checks ();
18639 return error_mark_node;
18641 /* Otherwise, emit an error about the invalid digraph, but continue
18642 parsing because we got our argument list. */
18643 if (permerror (next_token->location,
18644 "%<<::%> cannot begin a template-argument list"))
18646 static bool hint = false;
18647 inform (next_token->location,
18648 "%<<:%> is an alternate spelling for %<[%>."
18649 " Insert whitespace between %<<%> and %<::%>");
18650 if (!hint && !flag_permissive)
18652 inform (next_token->location, "(if you use %<-fpermissive%> "
18653 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18654 "accept your code)");
18655 hint = true;
18659 else
18661 /* Look for the `<' that starts the template-argument-list. */
18662 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18664 pop_deferring_access_checks ();
18665 return error_mark_node;
18667 /* Parse the arguments. */
18668 arguments = cp_parser_enclosed_template_argument_list (parser);
18670 if ((cxx_dialect > cxx17)
18671 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18672 && !template_keyword_p
18673 && (cp_parser_error_occurred (parser)
18674 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18676 /* This didn't go well. */
18677 if (TREE_CODE (templ) == FUNCTION_DECL)
18679 /* C++20 says that "function-name < a;" is now ill-formed. */
18680 if (cp_parser_error_occurred (parser))
18682 error_at (token->location, "invalid template-argument-list");
18683 inform (token->location, "function name as the left hand "
18684 "operand of %<<%> is ill-formed in C++20; wrap the "
18685 "function name in %<()%>");
18687 else
18688 /* We expect "f<targs>" to be followed by "(args)". */
18689 error_at (cp_lexer_peek_token (parser->lexer)->location,
18690 "expected %<(%> after template-argument-list");
18691 if (start_of_id)
18692 /* Purge all subsequent tokens. */
18693 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18695 else
18696 cp_parser_simulate_error (parser);
18697 pop_deferring_access_checks ();
18698 return error_mark_node;
18702 /* Set the location to be of the form:
18703 template-name < template-argument-list [opt] >
18704 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18705 with caret == start at the start of the template-name,
18706 ranging until the closing '>'. */
18707 location_t combined_loc
18708 = make_location (token->location, token->location, parser->lexer);
18710 /* Check for concepts autos where they don't belong. We could
18711 identify types in some cases of identifier TEMPL, looking ahead
18712 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18713 types. We reject them in functions, but if what we have is an
18714 identifier, even with none_type we can't conclude it's NOT a
18715 type, we have to wait for template substitution. */
18716 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18717 template_id = error_mark_node;
18718 /* Build a representation of the specialization. */
18719 else if (identifier_p (templ))
18720 template_id = build_min_nt_loc (combined_loc,
18721 TEMPLATE_ID_EXPR,
18722 templ, arguments);
18723 else if (DECL_TYPE_TEMPLATE_P (templ)
18724 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18726 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18727 template (rather than some instantiation thereof) only if
18728 is not nested within some other construct. For example, in
18729 "template <typename T> void f(T) { A<T>::", A<T> is just an
18730 instantiation of A. */
18731 bool entering_scope
18732 = (template_parm_scope_p ()
18733 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18734 template_id
18735 = finish_template_type (templ, arguments, entering_scope);
18737 else if (concept_definition_p (templ))
18739 /* The caller will decide whether this is a concept check or type
18740 constraint. */
18741 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18742 boolean_type_node, templ, arguments);
18744 else if (variable_template_p (templ))
18746 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
18747 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18748 SET_EXPR_LOCATION (template_id, combined_loc);
18750 else if (TREE_CODE (templ) == TYPE_DECL
18751 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18753 /* Some type template in dependent scope. */
18754 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18755 name = build_min_nt_loc (combined_loc,
18756 TEMPLATE_ID_EXPR,
18757 name, arguments);
18758 template_id = templ;
18760 else
18762 /* If it's not a class-template or a template-template, it should be
18763 a function-template. */
18764 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18766 template_id = lookup_template_function (templ, arguments);
18767 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18768 SET_EXPR_LOCATION (template_id, combined_loc);
18771 /* If parsing tentatively, replace the sequence of tokens that makes
18772 up the template-id with a CPP_TEMPLATE_ID token. That way,
18773 should we re-parse the token stream, we will not have to repeat
18774 the effort required to do the parse, nor will we issue duplicate
18775 error messages about problems during instantiation of the
18776 template. */
18777 if (start_of_id
18778 /* Don't do this if we had a parse error in a declarator; re-parsing
18779 might succeed if a name changes meaning (60361). */
18780 && !(cp_parser_error_occurred (parser)
18781 && cp_parser_parsing_tentatively (parser)
18782 && parser->in_declarator_p))
18784 /* Reset the contents of the START_OF_ID token. */
18785 token->type = CPP_TEMPLATE_ID;
18786 token->location = combined_loc;
18788 /* Retrieve any deferred checks. Do not pop this access checks yet
18789 so the memory will not be reclaimed during token replacing below. */
18790 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18791 token->tree_check_p = true;
18792 token->u.tree_check_value->value = template_id;
18793 token->u.tree_check_value->checks = get_deferred_access_checks ();
18794 token->keyword = RID_MAX;
18796 /* Purge all subsequent tokens. */
18797 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18799 /* ??? Can we actually assume that, if template_id ==
18800 error_mark_node, we will have issued a diagnostic to the
18801 user, as opposed to simply marking the tentative parse as
18802 failed? */
18803 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18804 error_at (token->location, "parse error in template argument list");
18807 pop_to_parent_deferring_access_checks ();
18808 return template_id;
18811 /* Like cp_parser_template_id, called in non-type context. */
18813 static tree
18814 cp_parser_template_id_expr (cp_parser *parser,
18815 bool template_keyword_p,
18816 bool check_dependency_p,
18817 bool is_declaration)
18819 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18820 none_type, is_declaration);
18821 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18822 && concept_check_p (x))
18823 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18824 return build_concept_id (x);
18825 return x;
18828 /* Parse a template-name.
18830 template-name:
18831 identifier
18833 The standard should actually say:
18835 template-name:
18836 identifier
18837 operator-function-id
18839 A defect report has been filed about this issue.
18841 A conversion-function-id cannot be a template name because they cannot
18842 be part of a template-id. In fact, looking at this code:
18844 a.operator K<int>()
18846 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18847 It is impossible to call a templated conversion-function-id with an
18848 explicit argument list, since the only allowed template parameter is
18849 the type to which it is converting.
18851 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18852 `template' keyword, in a construction like:
18854 T::template f<3>()
18856 In that case `f' is taken to be a template-name, even though there
18857 is no way of knowing for sure.
18859 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18860 name refers to a set of overloaded functions, at least one of which
18861 is a template, or an IDENTIFIER_NODE with the name of the template,
18862 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18863 names are looked up inside uninstantiated templates. */
18865 static tree
18866 cp_parser_template_name (cp_parser* parser,
18867 bool template_keyword_p,
18868 bool check_dependency_p,
18869 bool is_declaration,
18870 enum tag_types tag_type,
18871 bool *is_identifier)
18873 tree identifier;
18874 tree decl;
18875 cp_token *token = cp_lexer_peek_token (parser->lexer);
18877 /* If the next token is `operator', then we have either an
18878 operator-function-id or a conversion-function-id. */
18879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18881 /* We don't know whether we're looking at an
18882 operator-function-id or a conversion-function-id. */
18883 cp_parser_parse_tentatively (parser);
18884 /* Try an operator-function-id. */
18885 identifier = cp_parser_operator_function_id (parser);
18886 /* If that didn't work, try a conversion-function-id. */
18887 if (!cp_parser_parse_definitely (parser))
18889 cp_parser_error (parser, "expected template-name");
18890 return error_mark_node;
18893 /* Look for the identifier. */
18894 else
18895 identifier = cp_parser_identifier (parser);
18897 /* If we didn't find an identifier, we don't have a template-id. */
18898 if (identifier == error_mark_node)
18899 return error_mark_node;
18901 /* If the name immediately followed the `template' keyword, then it
18902 is a template-name. However, if the next token is not `<', then
18903 we do not treat it as a template-name, since it is not being used
18904 as part of a template-id. This enables us to handle constructs
18905 like:
18907 template <typename T> struct S { S(); };
18908 template <typename T> S<T>::S();
18910 correctly. We would treat `S' as a template -- if it were `S<T>'
18911 -- but we do not if there is no `<'. */
18913 if (processing_template_decl
18914 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18916 /* In a declaration, in a dependent context, we pretend that the
18917 "template" keyword was present in order to improve error
18918 recovery. For example, given:
18920 template <typename T> void f(T::X<int>);
18922 we want to treat "X<int>" as a template-id. */
18923 if (is_declaration
18924 && !template_keyword_p
18925 && parser->scope && TYPE_P (parser->scope)
18926 && check_dependency_p
18927 && dependent_scope_p (parser->scope)
18928 /* Do not do this for dtors (or ctors), since they never
18929 need the template keyword before their name. */
18930 && !constructor_name_p (identifier, parser->scope))
18932 cp_token_position start = 0;
18934 /* Explain what went wrong. */
18935 error_at (token->location, "non-template %qD used as template",
18936 identifier);
18937 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18938 parser->scope, identifier);
18939 /* If parsing tentatively, find the location of the "<" token. */
18940 if (cp_parser_simulate_error (parser))
18941 start = cp_lexer_token_position (parser->lexer, true);
18942 /* Parse the template arguments so that we can issue error
18943 messages about them. */
18944 cp_lexer_consume_token (parser->lexer);
18945 cp_parser_enclosed_template_argument_list (parser);
18946 /* Skip tokens until we find a good place from which to
18947 continue parsing. */
18948 cp_parser_skip_to_closing_parenthesis (parser,
18949 /*recovering=*/true,
18950 /*or_comma=*/true,
18951 /*consume_paren=*/false);
18952 /* If parsing tentatively, permanently remove the
18953 template argument list. That will prevent duplicate
18954 error messages from being issued about the missing
18955 "template" keyword. */
18956 if (start)
18957 cp_lexer_purge_tokens_after (parser->lexer, start);
18958 if (is_identifier)
18959 *is_identifier = true;
18960 parser->context->object_type = NULL_TREE;
18961 return identifier;
18964 /* If the "template" keyword is present, then there is generally
18965 no point in doing name-lookup, so we just return IDENTIFIER.
18966 But, if the qualifying scope is non-dependent then we can
18967 (and must) do name-lookup normally. */
18968 if (template_keyword_p)
18970 tree scope = (parser->scope ? parser->scope
18971 : parser->context->object_type);
18972 if (scope && TYPE_P (scope)
18973 && (!CLASS_TYPE_P (scope)
18974 || (check_dependency_p && dependent_scope_p (scope))))
18976 /* We're optimizing away the call to cp_parser_lookup_name, but
18977 we still need to do this. */
18978 parser->object_scope = parser->context->object_type;
18979 parser->context->object_type = NULL_TREE;
18980 return identifier;
18985 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18986 tree scope = (parser->scope ? parser->scope
18987 : parser->context->object_type);
18989 /* Look up the name. */
18990 decl = cp_parser_lookup_name (parser, identifier,
18991 tag_type,
18992 /*is_template=*/1 + template_keyword_p,
18993 /*is_namespace=*/false,
18994 check_dependency_p,
18995 /*ambiguous_decls=*/NULL,
18996 token->location);
18998 decl = strip_using_decl (decl);
19000 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19001 template-argument-list if it follows a name that is not a
19002 conversion-function-id and
19003 - that follows the keyword template or a ~ after a nested-name-specifier or
19004 in a class member access expression, or
19005 - for which name lookup finds the injected-class-name of a class template
19006 or finds any declaration of a template, or
19007 - that is an unqualified name for which name lookup either finds one or
19008 more functions or finds nothing, or
19009 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19010 (9.3.4), or in a type-only context other than a nested-name-specifier
19011 (13.8). */
19013 /* Handle injected-class-name. */
19014 decl = maybe_get_template_decl_from_type_decl (decl);
19016 /* If DECL is a template, then the name was a template-name. */
19017 if (TREE_CODE (decl) == TEMPLATE_DECL)
19019 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19020 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19022 tree d = DECL_TEMPLATE_RESULT (decl);
19023 tree attr;
19024 if (TREE_CODE (d) == TYPE_DECL)
19025 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19026 else
19027 attr = DECL_ATTRIBUTES (d);
19028 if (TREE_UNAVAILABLE (decl))
19030 attr = lookup_attribute ("unavailable", attr);
19031 error_unavailable_use (decl, attr);
19033 else if (TREE_DEPRECATED (decl)
19034 && deprecated_state != DEPRECATED_SUPPRESS)
19036 attr = lookup_attribute ("deprecated", attr);
19037 warn_deprecated_use (decl, attr);
19041 else
19043 /* Look through an overload set for any templates. */
19044 bool found = false;
19046 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19047 !found && iter; ++iter)
19048 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19049 found = true;
19051 /* "an unqualified name for which name lookup either finds one or more
19052 functions or finds nothing". */
19053 if (!found
19054 && (cxx_dialect > cxx17)
19055 && !scope
19056 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19057 && tag_type == none_type)
19059 /* The "more functions" case. Just use the OVERLOAD as normally.
19060 We don't use is_overloaded_fn here to avoid considering
19061 BASELINKs. */
19062 if (TREE_CODE (decl) == OVERLOAD
19063 /* Name lookup found one function. */
19064 || TREE_CODE (decl) == FUNCTION_DECL
19065 /* Name lookup found nothing. */
19066 || decl == error_mark_node)
19067 found = true;
19070 /* "that follows the keyword template"..."in a type-only context" */
19071 if (!found && scope
19072 && (template_keyword_p || tag_type != none_type)
19073 && dependentish_scope_p (scope)
19074 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19075 found = true;
19077 if (!found)
19079 /* The name does not name a template. */
19080 cp_parser_error (parser, "expected template-name");
19081 return error_mark_node;
19083 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19084 || TREE_CODE (decl) == USING_DECL
19085 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19086 || (TREE_CODE (decl) == TYPE_DECL
19087 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19088 /* Repeat the lookup at instantiation time. */
19089 decl = identifier;
19092 return decl;
19095 /* Parse a template-argument-list.
19097 template-argument-list:
19098 template-argument ... [opt]
19099 template-argument-list , template-argument ... [opt]
19101 Returns a TREE_VEC containing the arguments. */
19103 static tree
19104 cp_parser_template_argument_list (cp_parser* parser)
19106 bool saved_in_template_argument_list_p;
19107 bool saved_ice_p;
19108 bool saved_non_ice_p;
19110 /* Don't create location wrapper nodes within a template-argument-list. */
19111 auto_suppress_location_wrappers sentinel;
19113 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19114 parser->in_template_argument_list_p = true;
19115 /* Even if the template-id appears in an integral
19116 constant-expression, the contents of the argument list do
19117 not. */
19118 saved_ice_p = parser->integral_constant_expression_p;
19119 parser->integral_constant_expression_p = false;
19120 saved_non_ice_p = parser->non_integral_constant_expression_p;
19121 parser->non_integral_constant_expression_p = false;
19123 /* Parse the arguments. */
19124 auto_vec<tree, 10> args;
19127 if (!args.is_empty ())
19128 /* Consume the comma. */
19129 cp_lexer_consume_token (parser->lexer);
19131 /* Parse the template-argument. */
19132 tree argument = cp_parser_template_argument (parser);
19134 /* If the next token is an ellipsis, we're expanding a template
19135 argument pack. */
19136 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19138 if (argument == error_mark_node)
19140 cp_token *token = cp_lexer_peek_token (parser->lexer);
19141 error_at (token->location,
19142 "expected parameter pack before %<...%>");
19144 /* Consume the `...' token. */
19145 cp_lexer_consume_token (parser->lexer);
19147 /* Make the argument into a TYPE_PACK_EXPANSION or
19148 EXPR_PACK_EXPANSION. */
19149 argument = make_pack_expansion (argument);
19152 args.safe_push (argument);
19154 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19156 int n_args = args.length ();
19157 tree vec = make_tree_vec (n_args);
19159 for (int i = 0; i < n_args; i++)
19160 TREE_VEC_ELT (vec, i) = args[i];
19162 parser->non_integral_constant_expression_p = saved_non_ice_p;
19163 parser->integral_constant_expression_p = saved_ice_p;
19164 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19165 if (CHECKING_P)
19166 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19167 return vec;
19170 /* Parse a template-argument.
19172 template-argument:
19173 assignment-expression
19174 type-id
19175 id-expression
19177 The representation is that of an assignment-expression, type-id, or
19178 id-expression -- except that the qualified id-expression is
19179 evaluated, so that the value returned is either a DECL or an
19180 OVERLOAD.
19182 Although the standard says "assignment-expression", it forbids
19183 throw-expressions or assignments in the template argument.
19184 Therefore, we use "conditional-expression" instead. */
19186 static tree
19187 cp_parser_template_argument (cp_parser* parser)
19189 tree argument;
19190 bool template_p;
19191 bool address_p;
19192 bool maybe_type_id = false;
19193 cp_token *token = NULL, *argument_start_token = NULL;
19194 location_t loc = 0;
19195 cp_id_kind idk;
19197 /* There's really no way to know what we're looking at, so we just
19198 try each alternative in order.
19200 [temp.arg]
19202 In a template-argument, an ambiguity between a type-id and an
19203 expression is resolved to a type-id, regardless of the form of
19204 the corresponding template-parameter.
19206 Therefore, we try a type-id first. */
19207 cp_parser_parse_tentatively (parser);
19208 argument = cp_parser_template_type_arg (parser);
19209 /* If there was no error parsing the type-id but the next token is a
19210 '>>', our behavior depends on which dialect of C++ we're
19211 parsing. In C++98, we probably found a typo for '> >'. But there
19212 are type-id which are also valid expressions. For instance:
19214 struct X { int operator >> (int); };
19215 template <int V> struct Foo {};
19216 Foo<X () >> 5> r;
19218 Here 'X()' is a valid type-id of a function type, but the user just
19219 wanted to write the expression "X() >> 5". Thus, we remember that we
19220 found a valid type-id, but we still try to parse the argument as an
19221 expression to see what happens.
19223 In C++0x, the '>>' will be considered two separate '>'
19224 tokens. */
19225 if (!cp_parser_error_occurred (parser)
19226 && ((cxx_dialect == cxx98
19227 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19228 /* Similarly for >= which
19229 cp_parser_next_token_ends_template_argument_p treats for
19230 diagnostics purposes as mistyped > =, but can be valid
19231 after a type-id. */
19232 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19234 maybe_type_id = true;
19235 cp_parser_abort_tentative_parse (parser);
19237 else
19239 /* If the next token isn't a `,' or a `>', then this argument wasn't
19240 really finished. This means that the argument is not a valid
19241 type-id. */
19242 if (!cp_parser_next_token_ends_template_argument_p (parser))
19243 cp_parser_error (parser, "expected template-argument");
19244 /* If that worked, we're done. */
19245 if (cp_parser_parse_definitely (parser))
19246 return argument;
19248 /* We're still not sure what the argument will be. */
19249 cp_parser_parse_tentatively (parser);
19250 /* Try a template. */
19251 argument_start_token = cp_lexer_peek_token (parser->lexer);
19252 argument = cp_parser_id_expression (parser,
19253 /*template_keyword_p=*/false,
19254 /*check_dependency_p=*/true,
19255 &template_p,
19256 /*declarator_p=*/false,
19257 /*optional_p=*/false);
19258 /* If the next token isn't a `,' or a `>', then this argument wasn't
19259 really finished. */
19260 if (!cp_parser_next_token_ends_template_argument_p (parser))
19261 cp_parser_error (parser, "expected template-argument");
19262 if (!cp_parser_error_occurred (parser))
19264 /* Figure out what is being referred to. If the id-expression
19265 was for a class template specialization, then we will have a
19266 TYPE_DECL at this point. There is no need to do name lookup
19267 at this point in that case. */
19268 if (TREE_CODE (argument) != TYPE_DECL)
19269 argument = cp_parser_lookup_name (parser, argument,
19270 none_type,
19271 /*is_template=*/template_p,
19272 /*is_namespace=*/false,
19273 /*check_dependency=*/true,
19274 /*ambiguous_decls=*/NULL,
19275 argument_start_token->location);
19276 if (TREE_CODE (argument) != TEMPLATE_DECL
19277 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19278 cp_parser_error (parser, "expected template-name");
19280 if (cp_parser_parse_definitely (parser))
19282 if (TREE_UNAVAILABLE (argument))
19283 error_unavailable_use (argument, NULL_TREE);
19284 else if (TREE_DEPRECATED (argument))
19285 warn_deprecated_use (argument, NULL_TREE);
19286 return argument;
19288 /* It must be a non-type argument. In C++17 any constant-expression is
19289 allowed. */
19290 if (cxx_dialect > cxx14)
19291 goto general_expr;
19293 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19295 -- an integral constant-expression of integral or enumeration
19296 type; or
19298 -- the name of a non-type template-parameter; or
19300 -- the name of an object or function with external linkage...
19302 -- the address of an object or function with external linkage...
19304 -- a pointer to member... */
19305 /* Look for a non-type template parameter. */
19306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19308 cp_parser_parse_tentatively (parser);
19309 argument = cp_parser_primary_expression (parser,
19310 /*address_p=*/false,
19311 /*cast_p=*/false,
19312 /*template_arg_p=*/true,
19313 &idk);
19314 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19315 || !cp_parser_next_token_ends_template_argument_p (parser))
19316 cp_parser_simulate_error (parser);
19317 if (cp_parser_parse_definitely (parser))
19318 return argument;
19321 /* If the next token is "&", the argument must be the address of an
19322 object or function with external linkage. */
19323 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19324 if (address_p)
19326 loc = cp_lexer_peek_token (parser->lexer)->location;
19327 cp_lexer_consume_token (parser->lexer);
19329 /* See if we might have an id-expression. */
19330 token = cp_lexer_peek_token (parser->lexer);
19331 if (token->type == CPP_NAME
19332 || token->keyword == RID_OPERATOR
19333 || token->type == CPP_SCOPE
19334 || token->type == CPP_TEMPLATE_ID
19335 || token->type == CPP_NESTED_NAME_SPECIFIER)
19337 cp_parser_parse_tentatively (parser);
19338 argument = cp_parser_primary_expression (parser,
19339 address_p,
19340 /*cast_p=*/false,
19341 /*template_arg_p=*/true,
19342 &idk);
19343 if (cp_parser_error_occurred (parser)
19344 || !cp_parser_next_token_ends_template_argument_p (parser))
19345 cp_parser_abort_tentative_parse (parser);
19346 else
19348 tree probe;
19350 if (INDIRECT_REF_P (argument))
19352 /* Strip the dereference temporarily. */
19353 gcc_assert (REFERENCE_REF_P (argument));
19354 argument = TREE_OPERAND (argument, 0);
19357 /* If we're in a template, we represent a qualified-id referring
19358 to a static data member as a SCOPE_REF even if the scope isn't
19359 dependent so that we can check access control later. */
19360 probe = argument;
19361 if (TREE_CODE (probe) == SCOPE_REF)
19362 probe = TREE_OPERAND (probe, 1);
19363 if (VAR_P (probe))
19365 /* A variable without external linkage might still be a
19366 valid constant-expression, so no error is issued here
19367 if the external-linkage check fails. */
19368 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19369 cp_parser_simulate_error (parser);
19371 else if (is_overloaded_fn (argument))
19372 /* All overloaded functions are allowed; if the external
19373 linkage test does not pass, an error will be issued
19374 later. */
19376 else if (address_p
19377 && (TREE_CODE (argument) == OFFSET_REF
19378 || TREE_CODE (argument) == SCOPE_REF))
19379 /* A pointer-to-member. */
19381 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19383 else
19384 cp_parser_simulate_error (parser);
19386 if (cp_parser_parse_definitely (parser))
19388 if (address_p)
19389 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19390 NULL_TREE, tf_warning_or_error);
19391 else
19392 argument = convert_from_reference (argument);
19393 return argument;
19397 /* If the argument started with "&", there are no other valid
19398 alternatives at this point. */
19399 if (address_p)
19401 cp_parser_error (parser, "invalid non-type template argument");
19402 return error_mark_node;
19405 general_expr:
19406 /* If the argument wasn't successfully parsed as a type-id followed
19407 by '>>', the argument can only be a constant expression now.
19408 Otherwise, we try parsing the constant-expression tentatively,
19409 because the argument could really be a type-id. */
19410 if (maybe_type_id)
19411 cp_parser_parse_tentatively (parser);
19413 if (cxx_dialect <= cxx14)
19414 argument = cp_parser_constant_expression (parser);
19415 else
19417 /* In C++20, we can encounter a braced-init-list. */
19418 if (cxx_dialect >= cxx20
19419 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19420 return cp_parser_braced_list (parser);
19422 /* With C++17 generalized non-type template arguments we need to handle
19423 lvalue constant expressions, too. */
19424 argument = cp_parser_assignment_expression (parser);
19425 require_potential_constant_expression (argument);
19428 if (!maybe_type_id)
19429 return argument;
19430 if (!cp_parser_next_token_ends_template_argument_p (parser))
19431 cp_parser_error (parser, "expected template-argument");
19432 if (cp_parser_parse_definitely (parser))
19433 return argument;
19434 /* We did our best to parse the argument as a non type-id, but that
19435 was the only alternative that matched (albeit with a '>' after
19436 it). We can assume it's just a typo from the user, and a
19437 diagnostic will then be issued. */
19438 return cp_parser_template_type_arg (parser);
19441 /* Parse an explicit-instantiation.
19443 explicit-instantiation:
19444 template declaration
19446 Although the standard says `declaration', what it really means is:
19448 explicit-instantiation:
19449 template decl-specifier-seq [opt] declarator [opt] ;
19451 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19452 supposed to be allowed. A defect report has been filed about this
19453 issue.
19455 GNU Extension:
19457 explicit-instantiation:
19458 storage-class-specifier template
19459 decl-specifier-seq [opt] declarator [opt] ;
19460 function-specifier template
19461 decl-specifier-seq [opt] declarator [opt] ; */
19463 static void
19464 cp_parser_explicit_instantiation (cp_parser* parser)
19466 int declares_class_or_enum;
19467 cp_decl_specifier_seq decl_specifiers;
19468 tree extension_specifier = NULL_TREE;
19470 auto_timevar tv (TV_TEMPLATE_INST);
19472 /* Look for an (optional) storage-class-specifier or
19473 function-specifier. */
19474 if (cp_parser_allow_gnu_extensions_p (parser))
19476 extension_specifier
19477 = cp_parser_storage_class_specifier_opt (parser);
19478 if (!extension_specifier)
19479 extension_specifier
19480 = cp_parser_function_specifier_opt (parser,
19481 /*decl_specs=*/NULL);
19484 /* Look for the `template' keyword. */
19485 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19486 /* Let the front end know that we are processing an explicit
19487 instantiation. */
19488 begin_explicit_instantiation ();
19489 /* [temp.explicit] says that we are supposed to ignore access
19490 control while processing explicit instantiation directives. */
19491 push_deferring_access_checks (dk_no_check);
19492 /* Parse a decl-specifier-seq. */
19493 cp_parser_decl_specifier_seq (parser,
19494 CP_PARSER_FLAGS_OPTIONAL,
19495 &decl_specifiers,
19496 &declares_class_or_enum);
19498 cp_omp_declare_simd_data odsd;
19499 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19500 cp_parser_handle_directive_omp_attributes (parser,
19501 &decl_specifiers.attributes,
19502 &odsd, true);
19504 /* If there was exactly one decl-specifier, and it declared a class,
19505 and there's no declarator, then we have an explicit type
19506 instantiation. */
19507 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19509 tree type = check_tag_decl (&decl_specifiers,
19510 /*explicit_type_instantiation_p=*/true);
19511 /* Turn access control back on for names used during
19512 template instantiation. */
19513 pop_deferring_access_checks ();
19514 if (type)
19515 do_type_instantiation (type, extension_specifier,
19516 /*complain=*/tf_error);
19518 else
19520 cp_declarator *declarator;
19521 tree decl;
19523 /* Parse the declarator. */
19524 declarator
19525 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19526 CP_PARSER_FLAGS_NONE,
19527 /*ctor_dtor_or_conv_p=*/NULL,
19528 /*parenthesized_p=*/NULL,
19529 /*member_p=*/false,
19530 /*friend_p=*/false,
19531 /*static_p=*/false);
19532 if (declares_class_or_enum & 2)
19533 cp_parser_check_for_definition_in_return_type (declarator,
19534 decl_specifiers.type,
19535 decl_specifiers.locations[ds_type_spec]);
19536 if (declarator != cp_error_declarator)
19538 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19539 permerror (decl_specifiers.locations[ds_inline],
19540 "explicit instantiation shall not use"
19541 " %<inline%> specifier");
19542 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19543 permerror (decl_specifiers.locations[ds_constexpr],
19544 "explicit instantiation shall not use"
19545 " %<constexpr%> specifier");
19546 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19547 permerror (decl_specifiers.locations[ds_consteval],
19548 "explicit instantiation shall not use"
19549 " %<consteval%> specifier");
19551 decl = grokdeclarator (declarator, &decl_specifiers,
19552 NORMAL, 0, &decl_specifiers.attributes);
19553 /* Turn access control back on for names used during
19554 template instantiation. */
19555 pop_deferring_access_checks ();
19556 /* Do the explicit instantiation. */
19557 do_decl_instantiation (decl, extension_specifier);
19559 else
19561 pop_deferring_access_checks ();
19562 /* Skip the body of the explicit instantiation. */
19563 cp_parser_skip_to_end_of_statement (parser);
19566 /* We're done with the instantiation. */
19567 end_explicit_instantiation ();
19569 cp_parser_consume_semicolon_at_end_of_statement (parser);
19571 cp_finalize_omp_declare_simd (parser, &odsd);
19574 /* Parse an explicit-specialization.
19576 explicit-specialization:
19577 template < > declaration
19579 Although the standard says `declaration', what it really means is:
19581 explicit-specialization:
19582 template <> decl-specifier [opt] init-declarator [opt] ;
19583 template <> function-definition
19584 template <> explicit-specialization
19585 template <> template-declaration */
19587 static void
19588 cp_parser_explicit_specialization (cp_parser* parser)
19590 cp_token *token = cp_lexer_peek_token (parser->lexer);
19592 /* Look for the `template' keyword. */
19593 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19594 /* Look for the `<'. */
19595 cp_parser_require (parser, CPP_LESS, RT_LESS);
19596 /* Look for the `>'. */
19597 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19598 /* We have processed another parameter list. */
19599 ++parser->num_template_parameter_lists;
19601 /* [temp]
19603 A template ... explicit specialization ... shall not have C
19604 linkage. */
19605 bool need_lang_pop = current_lang_name == lang_name_c;
19606 if (need_lang_pop)
19608 error_at (token->location, "template specialization with C linkage");
19609 maybe_show_extern_c_location ();
19611 /* Give it C++ linkage to avoid confusing other parts of the
19612 front end. */
19613 push_lang_context (lang_name_cplusplus);
19616 /* Let the front end know that we are beginning a specialization. */
19617 if (begin_specialization ())
19619 /* If the next keyword is `template', we need to figure out
19620 whether or not we're looking a template-declaration. */
19621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19623 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19624 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19625 cp_parser_template_declaration_after_export (parser,
19626 /*member_p=*/false);
19627 else
19628 cp_parser_explicit_specialization (parser);
19630 else
19631 /* Parse the dependent declaration. */
19632 cp_parser_single_declaration (parser,
19633 /*checks=*/NULL,
19634 /*member_p=*/false,
19635 /*explicit_specialization_p=*/true,
19636 /*friend_p=*/NULL);
19639 /* We're done with the specialization. */
19640 end_specialization ();
19642 /* For the erroneous case of a template with C linkage, we pushed an
19643 implicit C++ linkage scope; exit that scope now. */
19644 if (need_lang_pop)
19645 pop_lang_context ();
19647 /* We're done with this parameter list. */
19648 --parser->num_template_parameter_lists;
19651 /* Preserve the attributes across a garbage collect (by making it a GC
19652 root), which can occur when parsing a member function. */
19654 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19656 /* Parse a type-specifier.
19658 type-specifier:
19659 simple-type-specifier
19660 class-specifier
19661 enum-specifier
19662 elaborated-type-specifier
19663 cv-qualifier
19665 GNU Extension:
19667 type-specifier:
19668 __complex__
19670 Returns a representation of the type-specifier. For a
19671 class-specifier, enum-specifier, or elaborated-type-specifier, a
19672 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19674 The parser flags FLAGS is used to control type-specifier parsing.
19676 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19677 in a decl-specifier-seq.
19679 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19680 class-specifier, enum-specifier, or elaborated-type-specifier, then
19681 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19682 if a type is declared; 2 if it is defined. Otherwise, it is set to
19683 zero.
19685 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19686 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19687 is set to FALSE. */
19689 static tree
19690 cp_parser_type_specifier (cp_parser* parser,
19691 cp_parser_flags flags,
19692 cp_decl_specifier_seq *decl_specs,
19693 bool is_declaration,
19694 int* declares_class_or_enum,
19695 bool* is_cv_qualifier)
19697 tree type_spec = NULL_TREE;
19698 cp_token *token;
19699 enum rid keyword;
19700 cp_decl_spec ds = ds_last;
19702 /* Assume this type-specifier does not declare a new type. */
19703 if (declares_class_or_enum)
19704 *declares_class_or_enum = 0;
19705 /* And that it does not specify a cv-qualifier. */
19706 if (is_cv_qualifier)
19707 *is_cv_qualifier = false;
19708 /* Peek at the next token. */
19709 token = cp_lexer_peek_token (parser->lexer);
19711 /* If we're looking at a keyword, we can use that to guide the
19712 production we choose. */
19713 keyword = token->keyword;
19714 switch (keyword)
19716 case RID_ENUM:
19717 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19718 goto elaborated_type_specifier;
19720 /* Look for the enum-specifier. */
19721 type_spec = cp_parser_enum_specifier (parser);
19722 /* If that worked, we're done. */
19723 if (type_spec)
19725 if (declares_class_or_enum)
19726 *declares_class_or_enum = 2;
19727 if (decl_specs)
19728 cp_parser_set_decl_spec_type (decl_specs,
19729 type_spec,
19730 token,
19731 /*type_definition_p=*/true);
19732 return type_spec;
19734 else
19735 goto elaborated_type_specifier;
19737 /* Any of these indicate either a class-specifier, or an
19738 elaborated-type-specifier. */
19739 case RID_CLASS:
19740 case RID_STRUCT:
19741 case RID_UNION:
19742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19743 goto elaborated_type_specifier;
19745 /* Parse tentatively so that we can back up if we don't find a
19746 class-specifier. */
19747 cp_parser_parse_tentatively (parser);
19748 if (decl_specs->attributes)
19749 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19750 /* Look for the class-specifier. */
19751 type_spec = cp_parser_class_specifier (parser);
19752 if (decl_specs->attributes)
19753 cp_parser_decl_specs_attrs->pop ();
19754 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19755 /* If that worked, we're done. */
19756 if (cp_parser_parse_definitely (parser))
19758 if (declares_class_or_enum)
19759 *declares_class_or_enum = 2;
19760 if (decl_specs)
19761 cp_parser_set_decl_spec_type (decl_specs,
19762 type_spec,
19763 token,
19764 /*type_definition_p=*/true);
19765 return type_spec;
19768 /* Fall through. */
19769 elaborated_type_specifier:
19770 /* We're declaring (not defining) a class or enum. */
19771 if (declares_class_or_enum)
19772 *declares_class_or_enum = 1;
19774 /* Fall through. */
19775 case RID_TYPENAME:
19776 /* Look for an elaborated-type-specifier. */
19777 type_spec
19778 = (cp_parser_elaborated_type_specifier
19779 (parser,
19780 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19781 is_declaration));
19782 if (decl_specs)
19783 cp_parser_set_decl_spec_type (decl_specs,
19784 type_spec,
19785 token,
19786 /*type_definition_p=*/false);
19787 return type_spec;
19789 case RID_CONST:
19790 ds = ds_const;
19791 if (is_cv_qualifier)
19792 *is_cv_qualifier = true;
19793 break;
19795 case RID_VOLATILE:
19796 ds = ds_volatile;
19797 if (is_cv_qualifier)
19798 *is_cv_qualifier = true;
19799 break;
19801 case RID_RESTRICT:
19802 ds = ds_restrict;
19803 if (is_cv_qualifier)
19804 *is_cv_qualifier = true;
19805 break;
19807 case RID_COMPLEX:
19808 /* The `__complex__' keyword is a GNU extension. */
19809 ds = ds_complex;
19810 break;
19812 default:
19813 break;
19816 /* Handle simple keywords. */
19817 if (ds != ds_last)
19819 if (decl_specs)
19821 set_and_check_decl_spec_loc (decl_specs, ds, token);
19822 decl_specs->any_specifiers_p = true;
19824 return cp_lexer_consume_token (parser->lexer)->u.value;
19827 /* If we do not already have a type-specifier, assume we are looking
19828 at a simple-type-specifier. */
19829 type_spec = cp_parser_simple_type_specifier (parser,
19830 decl_specs,
19831 flags);
19833 /* If we didn't find a type-specifier, and a type-specifier was not
19834 optional in this context, issue an error message. */
19835 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19837 cp_parser_error (parser, "expected type specifier");
19838 return error_mark_node;
19841 return type_spec;
19844 /* Parse a simple-type-specifier.
19846 simple-type-specifier:
19847 :: [opt] nested-name-specifier [opt] type-name
19848 :: [opt] nested-name-specifier template template-id
19849 char
19850 wchar_t
19851 bool
19852 short
19854 long
19855 signed
19856 unsigned
19857 float
19858 double
19859 void
19861 C++11 Extension:
19863 simple-type-specifier:
19864 auto
19865 decltype ( expression )
19866 char16_t
19867 char32_t
19868 __underlying_type ( type-id )
19870 C++17 extension:
19872 nested-name-specifier(opt) template-name
19874 GNU Extension:
19876 simple-type-specifier:
19877 __int128
19878 __typeof__ unary-expression
19879 __typeof__ ( type-id )
19880 __typeof__ ( type-id ) { initializer-list , [opt] }
19882 Concepts Extension:
19884 simple-type-specifier:
19885 constrained-type-specifier
19887 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19888 appropriately updated. */
19890 static tree
19891 cp_parser_simple_type_specifier (cp_parser* parser,
19892 cp_decl_specifier_seq *decl_specs,
19893 cp_parser_flags flags)
19895 tree type = NULL_TREE;
19896 cp_token *token;
19897 int idx;
19899 /* Peek at the next token. */
19900 token = cp_lexer_peek_token (parser->lexer);
19902 /* If we're looking at a keyword, things are easy. */
19903 switch (token->keyword)
19905 case RID_CHAR:
19906 if (decl_specs)
19907 decl_specs->explicit_char_p = true;
19908 type = char_type_node;
19909 break;
19910 case RID_CHAR8:
19911 type = char8_type_node;
19912 break;
19913 case RID_CHAR16:
19914 type = char16_type_node;
19915 break;
19916 case RID_CHAR32:
19917 type = char32_type_node;
19918 break;
19919 case RID_WCHAR:
19920 type = wchar_type_node;
19921 break;
19922 case RID_BOOL:
19923 type = boolean_type_node;
19924 break;
19925 case RID_SHORT:
19926 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19927 type = short_integer_type_node;
19928 break;
19929 case RID_INT:
19930 if (decl_specs)
19931 decl_specs->explicit_int_p = true;
19932 type = integer_type_node;
19933 break;
19934 case RID_INT_N_0:
19935 case RID_INT_N_1:
19936 case RID_INT_N_2:
19937 case RID_INT_N_3:
19938 idx = token->keyword - RID_INT_N_0;
19939 if (! int_n_enabled_p [idx])
19940 break;
19941 if (decl_specs)
19943 decl_specs->explicit_intN_p = true;
19944 decl_specs->int_n_idx = idx;
19945 /* Check if the alternate "__intN__" form has been used instead of
19946 "__intN". */
19947 if (startswith (IDENTIFIER_POINTER (token->u.value)
19948 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19949 decl_specs->int_n_alt = true;
19951 type = int_n_trees [idx].signed_type;
19952 break;
19953 case RID_LONG:
19954 if (decl_specs)
19955 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19956 type = long_integer_type_node;
19957 break;
19958 case RID_SIGNED:
19959 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19960 type = integer_type_node;
19961 break;
19962 case RID_UNSIGNED:
19963 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19964 type = unsigned_type_node;
19965 break;
19966 case RID_FLOAT:
19967 type = float_type_node;
19968 break;
19969 case RID_DOUBLE:
19970 type = double_type_node;
19971 break;
19972 CASE_RID_FLOATN_NX:
19973 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
19974 if (type == NULL_TREE)
19975 error ("%<_Float%d%s%> is not supported on this target",
19976 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
19977 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
19978 ? "x" : "");
19979 break;
19980 case RID_VOID:
19981 type = void_type_node;
19982 break;
19984 case RID_AUTO:
19985 maybe_warn_cpp0x (CPP0X_AUTO);
19986 if (parser->auto_is_implicit_function_template_parm_p)
19988 /* The 'auto' might be the placeholder return type for a function decl
19989 with trailing return type. */
19990 bool have_trailing_return_fn_decl = false;
19992 cp_parser_parse_tentatively (parser);
19993 cp_lexer_consume_token (parser->lexer);
19994 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19995 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19996 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19997 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20001 cp_lexer_consume_token (parser->lexer);
20002 cp_parser_skip_to_closing_parenthesis (parser,
20003 /*recovering*/false,
20004 /*or_comma*/false,
20005 /*consume_paren*/true);
20006 continue;
20009 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20011 have_trailing_return_fn_decl = true;
20012 break;
20015 cp_lexer_consume_token (parser->lexer);
20017 cp_parser_abort_tentative_parse (parser);
20019 if (have_trailing_return_fn_decl)
20021 type = make_auto ();
20022 break;
20025 if (cxx_dialect >= cxx14)
20027 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20028 type = TREE_TYPE (type);
20030 else
20031 type = error_mark_node;
20033 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20035 if (cxx_dialect < cxx14)
20036 error_at (token->location,
20037 "use of %<auto%> in lambda parameter declaration "
20038 "only available with "
20039 "%<-std=c++14%> or %<-std=gnu++14%>");
20041 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20042 pedwarn (token->location, 0,
20043 "use of %<auto%> in template argument "
20044 "only available with %<-fconcepts-ts%>");
20045 else if (!flag_concepts)
20046 pedwarn (token->location, 0,
20047 "use of %<auto%> in parameter declaration "
20048 "only available with %<-std=c++20%> or %<-fconcepts%>");
20049 else if (cxx_dialect < cxx14)
20050 error_at (token->location,
20051 "use of %<auto%> in parameter declaration "
20052 "only available with "
20053 "%<-std=c++14%> or %<-std=gnu++14%>");
20055 else
20056 type = make_auto ();
20057 break;
20059 case RID_DECLTYPE:
20060 /* Since DR 743, decltype can either be a simple-type-specifier by
20061 itself or begin a nested-name-specifier. Parsing it will replace
20062 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20063 handling below decide what to do. */
20064 cp_parser_decltype (parser);
20065 cp_lexer_set_token_position (parser->lexer, token);
20066 break;
20068 case RID_TYPEOF:
20069 /* Consume the `typeof' token. */
20070 cp_lexer_consume_token (parser->lexer);
20071 /* Parse the operand to `typeof'. */
20072 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20073 /* If it is not already a TYPE, take its type. */
20074 if (!TYPE_P (type))
20075 type = finish_typeof (type);
20077 if (decl_specs)
20078 cp_parser_set_decl_spec_type (decl_specs, type,
20079 token,
20080 /*type_definition_p=*/false);
20082 return type;
20084 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
20085 case RID_##CODE:
20086 #include "cp-trait.def"
20087 #undef DEFTRAIT_TYPE
20088 type = cp_parser_trait (parser, token->keyword);
20089 if (decl_specs)
20090 cp_parser_set_decl_spec_type (decl_specs, type,
20091 token,
20092 /*type_definition_p=*/false);
20094 return type;
20096 default:
20097 break;
20100 /* If token is an already-parsed decltype not followed by ::,
20101 it's a simple-type-specifier. */
20102 if (token->type == CPP_DECLTYPE
20103 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20105 type = saved_checks_value (token->u.tree_check_value);
20106 if (decl_specs)
20108 cp_parser_set_decl_spec_type (decl_specs, type,
20109 token,
20110 /*type_definition_p=*/false);
20111 /* Remember that we are handling a decltype in order to
20112 implement the resolution of DR 1510 when the argument
20113 isn't instantiation dependent. */
20114 decl_specs->decltype_p = true;
20116 cp_lexer_consume_token (parser->lexer);
20117 return type;
20120 /* If the type-specifier was for a built-in type, we're done. */
20121 if (type)
20123 /* Record the type. */
20124 if (decl_specs
20125 && (token->keyword != RID_SIGNED
20126 && token->keyword != RID_UNSIGNED
20127 && token->keyword != RID_SHORT
20128 && token->keyword != RID_LONG))
20129 cp_parser_set_decl_spec_type (decl_specs,
20130 type,
20131 token,
20132 /*type_definition_p=*/false);
20133 if (decl_specs)
20134 decl_specs->any_specifiers_p = true;
20136 /* Consume the token. */
20137 cp_lexer_consume_token (parser->lexer);
20139 if (type == error_mark_node)
20140 return error_mark_node;
20142 /* There is no valid C++ program where a non-template type is
20143 followed by a "<". That usually indicates that the user thought
20144 that the type was a template. */
20145 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20146 token->location);
20148 return TYPE_NAME (type);
20151 /* The type-specifier must be a user-defined type. */
20152 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20154 bool qualified_p;
20155 bool global_p;
20156 const bool typename_p = (cxx_dialect >= cxx20
20157 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20159 /* Don't gobble tokens or issue error messages if this is an
20160 optional type-specifier. */
20161 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20162 cp_parser_parse_tentatively (parser);
20164 /* Remember current tentative parsing state -- if we know we need
20165 a type, we can give better diagnostics here. */
20166 bool tent = cp_parser_parsing_tentatively (parser);
20168 token = cp_lexer_peek_token (parser->lexer);
20170 /* Look for the optional `::' operator. */
20171 global_p
20172 = (cp_parser_global_scope_opt (parser,
20173 /*current_scope_valid_p=*/false)
20174 != NULL_TREE);
20175 /* Look for the nested-name specifier. */
20176 qualified_p
20177 = (cp_parser_nested_name_specifier_opt (parser,
20178 /*typename_keyword_p=*/false,
20179 /*check_dependency_p=*/true,
20180 /*type_p=*/false,
20181 /*is_declaration=*/false)
20182 != NULL_TREE);
20183 /* If we have seen a nested-name-specifier, and the next token
20184 is `template', then we are using the template-id production. */
20185 if (parser->scope
20186 && cp_parser_optional_template_keyword (parser))
20188 /* Look for the template-id. */
20189 type = cp_parser_template_id (parser,
20190 /*template_keyword_p=*/true,
20191 /*check_dependency_p=*/true,
20192 none_type,
20193 /*is_declaration=*/false);
20194 /* If the template-id did not name a type, we are out of
20195 luck. */
20196 if (TREE_CODE (type) != TYPE_DECL)
20198 /* ...unless we pretend we have seen 'typename'. */
20199 if (typename_p)
20200 type = cp_parser_make_typename_type (parser, type,
20201 token->location);
20202 else
20204 cp_parser_error (parser, "expected template-id for type");
20205 type = error_mark_node;
20209 /* DR 1812: A < following a qualified-id in a typename-specifier
20210 could safely be assumed to begin a template argument list, so
20211 the template keyword should be optional. */
20212 else if (parser->scope
20213 && qualified_p
20214 && typename_p
20215 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20217 cp_parser_parse_tentatively (parser);
20219 type = cp_parser_template_id (parser,
20220 /*template_keyword_p=*/true,
20221 /*check_dependency_p=*/true,
20222 none_type,
20223 /*is_declaration=*/false);
20224 /* This is handled below, so back off. */
20225 if (type && concept_check_p (type))
20226 cp_parser_simulate_error (parser);
20228 if (!cp_parser_parse_definitely (parser))
20229 type = NULL_TREE;
20230 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20231 type = make_typename_type (parser->scope, type, typename_type,
20232 /*complain=*/tf_error);
20233 else if (TREE_CODE (type) != TYPE_DECL)
20234 type = NULL_TREE;
20237 /* Otherwise, look for a type-name. */
20238 if (!type)
20240 if (cxx_dialect >= cxx17 || flag_concepts)
20241 cp_parser_parse_tentatively (parser);
20243 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20245 if ((cxx_dialect >= cxx17 || flag_concepts)
20246 && !cp_parser_parse_definitely (parser))
20247 type = NULL_TREE;
20250 if (!type && flag_concepts && decl_specs)
20252 /* Try for a type-constraint with template arguments. We check
20253 decl_specs here to avoid trying this for a functional cast. */
20255 cp_parser_parse_tentatively (parser);
20257 type = cp_parser_template_id (parser,
20258 /*template_keyword_p=*/false,
20259 /*check_dependency_p=*/true,
20260 none_type,
20261 /*is_declaration=*/false);
20262 if (type && concept_check_p (type))
20264 location_t loc = EXPR_LOCATION (type);
20265 type = cp_parser_placeholder_type_specifier (parser, loc,
20266 type, tent);
20267 if (tent && type == error_mark_node)
20268 /* Perhaps it's a concept-check expression. */
20269 cp_parser_simulate_error (parser);
20271 else
20272 cp_parser_simulate_error (parser);
20274 if (!cp_parser_parse_definitely (parser))
20275 type = NULL_TREE;
20278 if (!type && cxx_dialect >= cxx17)
20280 /* Try class template argument deduction or type-constraint without
20281 template arguments. */
20282 tree name = cp_parser_identifier (parser);
20283 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20284 && parser->scope != error_mark_node)
20286 location_t loc
20287 = cp_lexer_previous_token (parser->lexer)->location;
20288 tree tmpl = cp_parser_lookup_name (parser, name,
20289 none_type,
20290 /*is_template=*/false,
20291 /*is_namespace=*/false,
20292 /*check_dependency=*/true,
20293 /*ambiguous_decls=*/NULL,
20294 token->location);
20295 if (tmpl && tmpl != error_mark_node
20296 && ctad_template_p (tmpl))
20297 type = make_template_placeholder (tmpl);
20298 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20299 type = cp_parser_placeholder_type_specifier (parser, loc,
20300 tmpl, tent);
20301 else
20303 type = error_mark_node;
20304 if (!cp_parser_simulate_error (parser))
20305 cp_parser_name_lookup_error (parser, name, tmpl,
20306 NLE_TYPE, token->location);
20309 else
20310 type = error_mark_node;
20313 /* If it didn't work out, we don't have a TYPE. */
20314 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20315 && !cp_parser_parse_definitely (parser))
20316 type = NULL_TREE;
20318 /* Keep track of all name-lookups performed in class scopes. */
20319 if (type
20320 && !global_p
20321 && !qualified_p
20322 && TREE_CODE (type) == TYPE_DECL
20323 && identifier_p (DECL_NAME (type)))
20324 maybe_note_name_used_in_class (DECL_NAME (type), type);
20326 if (type && decl_specs)
20327 cp_parser_set_decl_spec_type (decl_specs, type,
20328 token,
20329 /*type_definition_p=*/false);
20332 /* If we didn't get a type-name, issue an error message. */
20333 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20335 cp_parser_error (parser, "expected type-name");
20336 return error_mark_node;
20339 if (type && type != error_mark_node)
20341 /* See if TYPE is an Objective-C type, and if so, parse and
20342 accept any protocol references following it. Do this before
20343 the cp_parser_check_for_invalid_template_id() call, because
20344 Objective-C types can be followed by '<...>' which would
20345 enclose protocol names rather than template arguments, and so
20346 everything is fine. */
20347 if (c_dialect_objc () && !parser->scope
20348 && (objc_is_id (type) || objc_is_class_name (type)))
20350 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20351 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20353 /* Clobber the "unqualified" type previously entered into
20354 DECL_SPECS with the new, improved protocol-qualified version. */
20355 if (decl_specs)
20356 decl_specs->type = qual_type;
20358 return qual_type;
20361 /* There is no valid C++ program where a non-template type is
20362 followed by a "<". That usually indicates that the user
20363 thought that the type was a template. */
20364 cp_parser_check_for_invalid_template_id (parser, type,
20365 none_type,
20366 token->location);
20369 return type;
20372 /* Parse the remainder of a placholder-type-specifier.
20374 placeholder-type-specifier:
20375 type-constraint_opt auto
20376 type-constraint_opt decltype(auto)
20378 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20379 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20380 parses the placeholder type, and performs some contextual syntactic analysis.
20382 LOC provides the location of the template name.
20384 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20385 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20386 might actually be a concept-check,
20388 Note that the Concepts TS allows the auto or decltype(auto) to be
20389 omitted in a constrained-type-specifier. */
20391 static tree
20392 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20393 tree tmpl, bool tentative)
20395 if (tmpl == error_mark_node)
20396 return error_mark_node;
20398 tree orig_tmpl = tmpl;
20400 /* Get the arguments as written for subsequent analysis. */
20401 tree args = NULL_TREE;
20402 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20404 args = TREE_OPERAND (tmpl, 1);
20405 tmpl = TREE_OPERAND (tmpl, 0);
20407 else
20408 /* A concept-name with no arguments can't be an expression. */
20409 tentative = false;
20411 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20413 /* Get the concept and prototype parameter for the constraint. */
20414 tree_pair info = finish_type_constraints (tmpl, args, complain);
20415 tree con = info.first;
20416 tree proto = info.second;
20417 if (con == error_mark_node)
20418 return error_mark_node;
20420 /* As per the standard, require auto or decltype(auto), except in some
20421 cases (template parameter lists, -fconcepts-ts enabled). */
20422 cp_token *placeholder = NULL, *close_paren = NULL;
20423 if (cxx_dialect >= cxx20)
20425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20426 placeholder = cp_lexer_consume_token (parser->lexer);
20427 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20429 placeholder = cp_lexer_consume_token (parser->lexer);
20430 matching_parens parens;
20431 parens.require_open (parser);
20432 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20433 close_paren = parens.require_close (parser);
20437 /* A type constraint constrains a contextually determined type or type
20438 parameter pack. However, the Concepts TS does allow concepts
20439 to introduce non-type and template template parameters. */
20440 if (TREE_CODE (proto) != TYPE_DECL)
20442 if (!flag_concepts_ts
20443 || !processing_template_parmlist)
20445 if (!tentative)
20447 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20448 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20450 return error_mark_node;
20454 /* In a template parameter list, a type-parameter can be introduced
20455 by type-constraints alone. */
20456 if (processing_template_parmlist && !placeholder)
20458 /* In a default argument we may not be creating new parameters. */
20459 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20461 /* If this assert turns out to be false, do error() instead. */
20462 gcc_assert (tentative);
20463 return error_mark_node;
20465 return build_constrained_parameter (con, proto, args);
20468 /* Diagnose issues placeholder issues. */
20469 if (!flag_concepts_ts
20470 && !parser->in_result_type_constraint_p
20471 && !placeholder)
20473 if (tentative)
20474 /* Perhaps it's a concept-check expression (c++/91073). */
20475 return error_mark_node;
20477 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20478 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20479 error_at (input_location,
20480 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20481 /* Fall through. This is an error of omission. */
20483 else if (parser->in_result_type_constraint_p && placeholder)
20485 /* A trailing return type only allows type-constraints. */
20486 error_at (input_location,
20487 "unexpected placeholder in constrained result type");
20490 /* In a parameter-declaration-clause, a placeholder-type-specifier
20491 results in an invented template parameter. */
20492 if (parser->auto_is_implicit_function_template_parm_p)
20494 if (close_paren)
20496 location_t loc = make_location (placeholder->location,
20497 placeholder->location,
20498 close_paren->location);
20499 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20500 return error_mark_node;
20502 tree parm = build_constrained_parameter (con, proto, args);
20503 return synthesize_implicit_template_parm (parser, parm);
20506 /* Determine if the type should be deduced using template argument
20507 deduction or decltype deduction. Note that the latter is always
20508 used for type-constraints in trailing return types. */
20509 bool decltype_p = placeholder
20510 ? placeholder->keyword == RID_DECLTYPE
20511 : parser->in_result_type_constraint_p;
20513 /* Otherwise, this is the type of a variable or return type. */
20514 if (decltype_p)
20515 return make_constrained_decltype_auto (con, args);
20516 else
20517 return make_constrained_auto (con, args);
20520 /* Parse a type-name.
20522 type-name:
20523 class-name
20524 enum-name
20525 typedef-name
20526 simple-template-id [in c++0x]
20528 enum-name:
20529 identifier
20531 typedef-name:
20532 identifier
20534 Concepts:
20536 type-name:
20537 concept-name
20538 partial-concept-id
20540 concept-name:
20541 identifier
20543 Returns a TYPE_DECL for the type. */
20545 static tree
20546 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20548 tree type_decl;
20550 /* We can't know yet whether it is a class-name or not. */
20551 cp_parser_parse_tentatively (parser);
20552 /* Try a class-name. */
20553 type_decl = cp_parser_class_name (parser,
20554 typename_keyword_p,
20555 /*template_keyword_p=*/false,
20556 none_type,
20557 /*check_dependency_p=*/true,
20558 /*class_head_p=*/false,
20559 /*is_declaration=*/false);
20560 /* If it's not a class-name, keep looking. */
20561 if (!cp_parser_parse_definitely (parser))
20563 if (cxx_dialect < cxx11)
20564 /* It must be a typedef-name or an enum-name. */
20565 return cp_parser_nonclass_name (parser);
20567 cp_parser_parse_tentatively (parser);
20568 /* It is either a simple-template-id representing an
20569 instantiation of an alias template... */
20570 type_decl = cp_parser_template_id (parser,
20571 /*template_keyword_p=*/false,
20572 /*check_dependency_p=*/true,
20573 none_type,
20574 /*is_declaration=*/false);
20575 /* Note that this must be an instantiation of an alias template
20576 because [temp.names]/6 says:
20578 A template-id that names an alias template specialization
20579 is a type-name.
20581 Whereas [temp.names]/7 says:
20583 A simple-template-id that names a class template
20584 specialization is a class-name.
20586 With concepts, this could also be a partial-concept-id that
20587 declares a non-type template parameter. */
20588 if (type_decl != NULL_TREE
20589 && TREE_CODE (type_decl) == TYPE_DECL
20590 && TYPE_DECL_ALIAS_P (type_decl))
20591 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20592 else
20593 cp_parser_simulate_error (parser);
20595 if (!cp_parser_parse_definitely (parser))
20596 /* ... Or a typedef-name or an enum-name. */
20597 return cp_parser_nonclass_name (parser);
20600 return type_decl;
20603 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20604 or a concept-name.
20606 enum-name:
20607 identifier
20609 typedef-name:
20610 identifier
20612 concept-name:
20613 identifier
20615 Returns a TYPE_DECL for the type. */
20617 static tree
20618 cp_parser_nonclass_name (cp_parser* parser)
20620 tree type_decl;
20621 tree identifier;
20623 cp_token *token = cp_lexer_peek_token (parser->lexer);
20624 identifier = cp_parser_identifier (parser);
20625 if (identifier == error_mark_node)
20626 return error_mark_node;
20628 /* Look up the type-name. */
20629 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20631 type_decl = strip_using_decl (type_decl);
20633 if (TREE_CODE (type_decl) != TYPE_DECL
20634 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20636 /* See if this is an Objective-C type. */
20637 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20638 tree type = objc_get_protocol_qualified_type (identifier, protos);
20639 if (type)
20640 type_decl = TYPE_NAME (type);
20643 /* Issue an error if we did not find a type-name. */
20644 if (TREE_CODE (type_decl) != TYPE_DECL
20645 /* In Objective-C, we have the complication that class names are
20646 normally type names and start declarations (eg, the
20647 "NSObject" in "NSObject *object;"), but can be used in an
20648 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20649 is an expression. So, a classname followed by a dot is not a
20650 valid type-name. */
20651 || (objc_is_class_name (TREE_TYPE (type_decl))
20652 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20654 if (!cp_parser_simulate_error (parser))
20655 cp_parser_name_lookup_error (parser, identifier, type_decl,
20656 NLE_TYPE, token->location);
20657 return error_mark_node;
20659 /* Remember that the name was used in the definition of the
20660 current class so that we can check later to see if the
20661 meaning would have been different after the class was
20662 entirely defined. */
20663 else if (type_decl != error_mark_node
20664 && !parser->scope)
20665 maybe_note_name_used_in_class (identifier, type_decl);
20667 return type_decl;
20670 /* Parse an elaborated-type-specifier. Note that the grammar given
20671 here incorporates the resolution to DR68.
20673 elaborated-type-specifier:
20674 class-key :: [opt] nested-name-specifier [opt] identifier
20675 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20676 enum-key :: [opt] nested-name-specifier [opt] identifier
20677 typename :: [opt] nested-name-specifier identifier
20678 typename :: [opt] nested-name-specifier template [opt]
20679 template-id
20681 GNU extension:
20683 elaborated-type-specifier:
20684 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20685 class-key attributes :: [opt] nested-name-specifier [opt]
20686 template [opt] template-id
20687 enum attributes :: [opt] nested-name-specifier [opt] identifier
20689 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20690 declared `friend'. If IS_DECLARATION is TRUE, then this
20691 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20692 something is being declared.
20694 Returns the TYPE specified. */
20696 static tree
20697 cp_parser_elaborated_type_specifier (cp_parser* parser,
20698 bool is_friend,
20699 bool is_declaration)
20701 enum tag_types tag_type;
20702 tree identifier;
20703 tree type = NULL_TREE;
20704 tree attributes = NULL_TREE;
20705 tree globalscope;
20706 cp_token *token = NULL;
20708 /* For class and enum types the location of the class-key or enum-key. */
20709 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20710 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20711 rid scoped_key = RID_MAX;
20713 /* See if we're looking at the `enum' keyword. */
20714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20716 /* Consume the `enum' token. */
20717 cp_lexer_consume_token (parser->lexer);
20718 /* Remember that it's an enumeration type. */
20719 tag_type = enum_type;
20720 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20721 enums) is used here. */
20722 cp_token *token = cp_lexer_peek_token (parser->lexer);
20723 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20724 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20726 location_t loc = token->location;
20727 gcc_rich_location richloc (loc);
20728 richloc.add_range (input_location);
20729 richloc.add_fixit_remove ();
20730 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20731 "a scoped enum must not use the %qD keyword",
20732 token->u.value);
20733 /* Consume the `struct' or `class' and parse it anyway. */
20734 cp_lexer_consume_token (parser->lexer);
20735 /* Create a combined location for the whole scoped-enum-key. */
20736 key_loc = make_location (key_loc, key_loc, loc);
20738 else
20739 scoped_key = RID_MAX;
20741 /* Parse the attributes. */
20742 attributes = cp_parser_attributes_opt (parser);
20744 /* Or, it might be `typename'. */
20745 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20746 RID_TYPENAME))
20748 /* Consume the `typename' token. */
20749 cp_lexer_consume_token (parser->lexer);
20750 /* Remember that it's a `typename' type. */
20751 tag_type = typename_type;
20753 /* Otherwise it must be a class-key. */
20754 else
20756 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20757 tag_type = cp_parser_class_key (parser);
20758 if (tag_type == none_type)
20759 return error_mark_node;
20760 /* Parse the attributes. */
20761 attributes = cp_parser_attributes_opt (parser);
20764 /* Look for the `::' operator. */
20765 globalscope = cp_parser_global_scope_opt (parser,
20766 /*current_scope_valid_p=*/false);
20767 /* Look for the nested-name-specifier. */
20768 tree nested_name_specifier;
20769 if (tag_type == typename_type && !globalscope)
20771 nested_name_specifier
20772 = cp_parser_nested_name_specifier (parser,
20773 /*typename_keyword_p=*/true,
20774 /*check_dependency_p=*/true,
20775 /*type_p=*/true,
20776 is_declaration);
20777 if (!nested_name_specifier)
20778 return error_mark_node;
20780 else
20781 /* Even though `typename' is not present, the proposed resolution
20782 to Core Issue 180 says that in `class A<T>::B', `B' should be
20783 considered a type-name, even if `A<T>' is dependent. */
20784 nested_name_specifier
20785 = cp_parser_nested_name_specifier_opt (parser,
20786 /*typename_keyword_p=*/true,
20787 /*check_dependency_p=*/true,
20788 /*type_p=*/true,
20789 is_declaration);
20790 /* For everything but enumeration types, consider a template-id.
20791 For an enumeration type, consider only a plain identifier. */
20792 if (tag_type != enum_type)
20794 bool template_p = false;
20795 tree decl;
20797 /* Allow the `template' keyword. */
20798 template_p = cp_parser_optional_template_keyword (parser);
20799 /* If we didn't see `template', we don't know if there's a
20800 template-id or not. */
20801 if (!template_p)
20802 cp_parser_parse_tentatively (parser);
20803 /* The `template' keyword must follow a nested-name-specifier. */
20804 else if (!nested_name_specifier && !globalscope)
20806 cp_parser_error (parser, "%<template%> must follow a nested-"
20807 "name-specifier");
20808 return error_mark_node;
20811 /* Parse the template-id. */
20812 token = cp_lexer_peek_token (parser->lexer);
20813 decl = cp_parser_template_id (parser, template_p,
20814 /*check_dependency_p=*/true,
20815 tag_type,
20816 is_declaration);
20817 /* If we didn't find a template-id, look for an ordinary
20818 identifier. */
20819 if (!template_p && !cp_parser_parse_definitely (parser))
20821 /* We can get here when cp_parser_template_id, called by
20822 cp_parser_class_name with tag_type == none_type, succeeds
20823 and caches a BASELINK. Then, when called again here,
20824 instead of failing and returning an error_mark_node
20825 returns it (see template/typename17.C in C++11).
20826 ??? Could we diagnose this earlier? */
20827 else if (tag_type == typename_type && BASELINK_P (decl))
20829 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20830 type = error_mark_node;
20832 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20833 in effect, then we must assume that, upon instantiation, the
20834 template will correspond to a class. */
20835 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20836 && tag_type == typename_type)
20837 type = make_typename_type (parser->scope, decl,
20838 typename_type,
20839 /*complain=*/tf_error);
20840 /* If the `typename' keyword is in effect and DECL is not a type
20841 decl, then type is non existent. */
20842 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20844 else if (TREE_CODE (decl) == TYPE_DECL)
20846 type = check_elaborated_type_specifier (tag_type, decl,
20847 /*allow_template_p=*/true);
20849 /* If the next token is a semicolon, this must be a specialization,
20850 instantiation, or friend declaration. Check the scope while we
20851 still know whether or not we had a nested-name-specifier. */
20852 if (type != error_mark_node
20853 && !nested_name_specifier && !is_friend
20854 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20855 check_unqualified_spec_or_inst (type, token->location);
20857 else if (decl == error_mark_node)
20858 type = error_mark_node;
20861 if (!type)
20863 token = cp_lexer_peek_token (parser->lexer);
20864 identifier = cp_parser_identifier (parser);
20866 if (identifier == error_mark_node)
20868 parser->scope = NULL_TREE;
20869 return error_mark_node;
20872 /* For a `typename', we needn't call xref_tag. */
20873 if (tag_type == typename_type
20874 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20875 return cp_parser_make_typename_type (parser, identifier,
20876 token->location);
20878 /* Template parameter lists apply only if we are not within a
20879 function parameter list. */
20880 bool template_parm_lists_apply
20881 = parser->num_template_parameter_lists;
20882 if (template_parm_lists_apply)
20883 for (cp_binding_level *s = current_binding_level;
20884 s && s->kind != sk_template_parms;
20885 s = s->level_chain)
20886 if (s->kind == sk_function_parms)
20887 template_parm_lists_apply = false;
20889 /* Look up a qualified name in the usual way. */
20890 if (parser->scope)
20892 tree decl;
20893 tree ambiguous_decls;
20895 decl = cp_parser_lookup_name (parser, identifier,
20896 tag_type,
20897 /*is_template=*/false,
20898 /*is_namespace=*/false,
20899 /*check_dependency=*/true,
20900 &ambiguous_decls,
20901 token->location);
20903 /* If the lookup was ambiguous, an error will already have been
20904 issued. */
20905 if (ambiguous_decls)
20906 return error_mark_node;
20908 /* If we are parsing friend declaration, DECL may be a
20909 TEMPLATE_DECL tree node here. However, we need to check
20910 whether this TEMPLATE_DECL results in valid code. Consider
20911 the following example:
20913 namespace N {
20914 template <class T> class C {};
20916 class X {
20917 template <class T> friend class N::C; // #1, valid code
20919 template <class T> class Y {
20920 friend class N::C; // #2, invalid code
20923 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20924 name lookup of `N::C'. We see that friend declaration must
20925 be template for the code to be valid. Note that
20926 processing_template_decl does not work here since it is
20927 always 1 for the above two cases. */
20929 decl = (cp_parser_maybe_treat_template_as_class
20930 (decl, /*tag_name_p=*/is_friend
20931 && template_parm_lists_apply));
20933 if (TREE_CODE (decl) != TYPE_DECL)
20935 cp_parser_diagnose_invalid_type_name (parser,
20936 identifier,
20937 token->location);
20938 return error_mark_node;
20941 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20943 bool allow_template = (template_parm_lists_apply
20944 || DECL_SELF_REFERENCE_P (decl));
20945 type = check_elaborated_type_specifier (tag_type, decl,
20946 allow_template);
20948 if (type == error_mark_node)
20949 return error_mark_node;
20952 /* Forward declarations of nested types, such as
20954 class C1::C2;
20955 class C1::C2::C3;
20957 are invalid unless all components preceding the final '::'
20958 are complete. If all enclosing types are complete, these
20959 declarations become merely pointless.
20961 Invalid forward declarations of nested types are errors
20962 caught elsewhere in parsing. Those that are pointless arrive
20963 here. */
20965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20966 && !is_friend && is_declaration
20967 && !processing_explicit_instantiation)
20968 warning (0, "declaration %qD does not declare anything", decl);
20970 type = TREE_TYPE (decl);
20972 else
20974 /* An elaborated-type-specifier sometimes introduces a new type and
20975 sometimes names an existing type. Normally, the rule is that it
20976 introduces a new type only if there is not an existing type of
20977 the same name already in scope. For example, given:
20979 struct S {};
20980 void f() { struct S s; }
20982 the `struct S' in the body of `f' is the same `struct S' as in
20983 the global scope; the existing definition is used. However, if
20984 there were no global declaration, this would introduce a new
20985 local class named `S'.
20987 An exception to this rule applies to the following code:
20989 namespace N { struct S; }
20991 Here, the elaborated-type-specifier names a new type
20992 unconditionally; even if there is already an `S' in the
20993 containing scope this declaration names a new type.
20994 This exception only applies if the elaborated-type-specifier
20995 forms the complete declaration:
20997 [class.name]
20999 A declaration consisting solely of `class-key identifier ;' is
21000 either a redeclaration of the name in the current scope or a
21001 forward declaration of the identifier as a class name. It
21002 introduces the name into the current scope.
21004 We are in this situation precisely when the next token is a `;'.
21006 An exception to the exception is that a `friend' declaration does
21007 *not* name a new type; i.e., given:
21009 struct S { friend struct T; };
21011 `T' is not a new type in the scope of `S'.
21013 Also, `new struct S' or `sizeof (struct S)' never results in the
21014 definition of a new type; a new type can only be declared in a
21015 declaration context. */
21017 TAG_how how;
21019 if (is_friend)
21020 /* Friends have special name lookup rules. */
21021 how = TAG_how::HIDDEN_FRIEND;
21022 else if (is_declaration
21023 && cp_lexer_next_token_is (parser->lexer,
21024 CPP_SEMICOLON))
21025 /* This is a `class-key identifier ;' */
21026 how = TAG_how::CURRENT_ONLY;
21027 else
21028 how = TAG_how::GLOBAL;
21030 bool template_p =
21031 (template_parm_lists_apply
21032 && (cp_parser_next_token_starts_class_definition_p (parser)
21033 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21034 /* An unqualified name was used to reference this type, so
21035 there were no qualifying templates. */
21036 if (template_parm_lists_apply
21037 && !cp_parser_check_template_parameters (parser,
21038 /*num_templates=*/0,
21039 /*template_id*/false,
21040 token->location,
21041 /*declarator=*/NULL))
21042 return error_mark_node;
21044 type = xref_tag (tag_type, identifier, how, template_p);
21048 if (type == error_mark_node)
21049 return error_mark_node;
21051 /* Allow attributes on forward declarations of classes. */
21052 if (attributes)
21054 if (TREE_CODE (type) == TYPENAME_TYPE)
21055 warning (OPT_Wattributes,
21056 "attributes ignored on uninstantiated type");
21057 else if (tag_type != enum_type
21058 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21059 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21060 && ! processing_explicit_instantiation)
21061 warning (OPT_Wattributes,
21062 "attributes ignored on template instantiation");
21063 else if (is_friend && cxx11_attribute_p (attributes))
21065 if (warning (OPT_Wattributes, "attribute ignored"))
21066 inform (input_location, "an attribute that appertains to a friend "
21067 "declaration that is not a definition is ignored");
21069 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21070 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21071 else
21072 warning (OPT_Wattributes,
21073 "attributes ignored on elaborated-type-specifier that is "
21074 "not a forward declaration");
21077 if (tag_type == enum_type)
21078 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21079 else
21081 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21082 for alias definition. */
21083 bool decl_class = (is_declaration
21084 && cp_parser_declares_only_class_p (parser));
21085 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21086 decl_class);
21088 /* Indicate whether this class was declared as a `class' or as a
21089 `struct'. */
21090 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21091 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21094 /* A "<" cannot follow an elaborated type specifier. If that
21095 happens, the user was probably trying to form a template-id. */
21096 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21097 token->location);
21099 return type;
21102 /* Parse an enum-specifier.
21104 enum-specifier:
21105 enum-head { enumerator-list [opt] }
21106 enum-head { enumerator-list , } [C++0x]
21108 enum-head:
21109 enum-key identifier [opt] enum-base [opt]
21110 enum-key nested-name-specifier identifier enum-base [opt]
21112 enum-key:
21113 enum
21114 enum class [C++0x]
21115 enum struct [C++0x]
21117 enum-base: [C++0x]
21118 : type-specifier-seq
21120 opaque-enum-specifier:
21121 enum-key identifier enum-base [opt] ;
21123 GNU Extensions:
21124 enum-key attributes[opt] identifier [opt] enum-base [opt]
21125 { enumerator-list [opt] }attributes[opt]
21126 enum-key attributes[opt] identifier [opt] enum-base [opt]
21127 { enumerator-list, }attributes[opt] [C++0x]
21129 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21130 if the token stream isn't an enum-specifier after all. */
21132 static tree
21133 cp_parser_enum_specifier (cp_parser* parser)
21135 tree identifier;
21136 tree type = NULL_TREE;
21137 tree prev_scope;
21138 tree nested_name_specifier = NULL_TREE;
21139 tree attributes;
21140 bool scoped_enum_p = false;
21141 bool has_underlying_type = false;
21142 bool nested_being_defined = false;
21143 bool new_value_list = false;
21144 bool is_new_type = false;
21145 bool is_unnamed = false;
21146 tree underlying_type = NULL_TREE;
21147 cp_token *type_start_token = NULL;
21148 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21150 /* Parse tentatively so that we can back up if we don't find a
21151 enum-specifier. */
21152 cp_parser_parse_tentatively (parser);
21154 /* Caller guarantees that the current token is 'enum', an identifier
21155 possibly follows, and the token after that is an opening brace.
21156 If we don't have an identifier, fabricate an anonymous name for
21157 the enumeration being defined. */
21158 cp_lexer_consume_token (parser->lexer);
21160 /* Parse the "class" or "struct", which indicates a scoped
21161 enumeration type in C++0x. */
21162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21163 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21165 if (cxx_dialect < cxx11)
21166 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21168 /* Consume the `struct' or `class' token. */
21169 cp_lexer_consume_token (parser->lexer);
21171 scoped_enum_p = true;
21174 attributes = cp_parser_attributes_opt (parser);
21176 /* Clear the qualification. */
21177 parser->scope = NULL_TREE;
21178 parser->qualifying_scope = NULL_TREE;
21179 parser->object_scope = NULL_TREE;
21181 /* Figure out in what scope the declaration is being placed. */
21182 prev_scope = current_scope ();
21184 type_start_token = cp_lexer_peek_token (parser->lexer);
21186 push_deferring_access_checks (dk_no_check);
21187 nested_name_specifier
21188 = cp_parser_nested_name_specifier_opt (parser,
21189 /*typename_keyword_p=*/true,
21190 /*check_dependency_p=*/false,
21191 /*type_p=*/false,
21192 /*is_declaration=*/false);
21194 if (nested_name_specifier)
21196 tree name;
21198 identifier = cp_parser_identifier (parser);
21199 name = cp_parser_lookup_name (parser, identifier,
21200 enum_type,
21201 /*is_template=*/false,
21202 /*is_namespace=*/false,
21203 /*check_dependency=*/true,
21204 /*ambiguous_decls=*/NULL,
21205 input_location);
21206 if (name && name != error_mark_node)
21208 type = TREE_TYPE (name);
21209 if (TREE_CODE (type) == TYPENAME_TYPE)
21211 /* Are template enums allowed in ISO? */
21212 if (template_parm_scope_p ())
21213 pedwarn (type_start_token->location, OPT_Wpedantic,
21214 "%qD is an enumeration template", name);
21215 /* ignore a typename reference, for it will be solved by name
21216 in start_enum. */
21217 type = NULL_TREE;
21220 else if (nested_name_specifier == error_mark_node)
21221 /* We already issued an error. */;
21222 else
21224 error_at (type_start_token->location,
21225 "%qD does not name an enumeration in %qT",
21226 identifier, nested_name_specifier);
21227 nested_name_specifier = error_mark_node;
21230 else
21232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21233 identifier = cp_parser_identifier (parser);
21234 else
21236 identifier = make_anon_name ();
21237 is_unnamed = true;
21238 if (scoped_enum_p)
21239 error_at (type_start_token->location,
21240 "unnamed scoped enum is not allowed");
21243 pop_deferring_access_checks ();
21245 /* Check for the `:' that denotes a specified underlying type in C++0x.
21246 Note that a ':' could also indicate a bitfield width, however. */
21247 location_t colon_loc = UNKNOWN_LOCATION;
21248 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21250 cp_decl_specifier_seq type_specifiers;
21252 /* Consume the `:'. */
21253 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21254 cp_lexer_consume_token (parser->lexer);
21256 auto tdf
21257 = make_temp_override (parser->type_definition_forbidden_message,
21258 G_("types may not be defined in enum-base"));
21260 /* Parse the type-specifier-seq. */
21261 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21262 /*is_declaration=*/false,
21263 /*is_trailing_return=*/false,
21264 &type_specifiers);
21266 /* At this point this is surely not elaborated type specifier. */
21267 if (!cp_parser_parse_definitely (parser))
21268 return NULL_TREE;
21270 if (cxx_dialect < cxx11)
21271 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21273 has_underlying_type = true;
21275 /* If that didn't work, stop. */
21276 if (type_specifiers.type != error_mark_node)
21278 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21279 /*initialized=*/0, NULL);
21280 if (underlying_type == error_mark_node
21281 || check_for_bare_parameter_packs (underlying_type))
21282 underlying_type = NULL_TREE;
21286 /* Look for the `{' but don't consume it yet. */
21287 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21289 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21291 if (has_underlying_type)
21292 cp_parser_commit_to_tentative_parse (parser);
21293 cp_parser_error (parser, "expected %<{%>");
21294 if (has_underlying_type)
21295 return error_mark_node;
21297 /* An opaque-enum-specifier must have a ';' here. */
21298 if ((scoped_enum_p || underlying_type)
21299 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21301 if (has_underlying_type)
21302 pedwarn (colon_loc,
21303 OPT_Welaborated_enum_base,
21304 "declaration of enumeration with "
21305 "fixed underlying type and no enumerator list is "
21306 "only permitted as a standalone declaration");
21307 else
21308 cp_parser_error (parser, "expected %<;%> or %<{%>");
21312 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21313 return NULL_TREE;
21315 if (nested_name_specifier)
21317 if (CLASS_TYPE_P (nested_name_specifier))
21319 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21320 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21321 push_scope (nested_name_specifier);
21323 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21324 push_nested_namespace (nested_name_specifier);
21327 /* Issue an error message if type-definitions are forbidden here. */
21328 if (!cp_parser_check_type_definition (parser))
21329 type = error_mark_node;
21330 else
21331 /* Create the new type. We do this before consuming the opening
21332 brace so the enum will be recorded as being on the line of its
21333 tag (or the 'enum' keyword, if there is no tag). */
21334 type = start_enum (identifier, type, underlying_type,
21335 attributes, scoped_enum_p, &is_new_type);
21337 /* If the next token is not '{' it is an opaque-enum-specifier or an
21338 elaborated-type-specifier. */
21339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21341 auto_timevar tv (TV_PARSE_ENUM);
21343 if (nested_name_specifier
21344 && nested_name_specifier != error_mark_node)
21346 /* The following catches invalid code such as:
21347 enum class S<int>::E { A, B, C }; */
21348 if (!processing_specialization
21349 && CLASS_TYPE_P (nested_name_specifier)
21350 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21351 error_at (type_start_token->location, "cannot add an enumerator "
21352 "list to a template instantiation");
21354 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21356 error_at (type_start_token->location,
21357 "%<%T::%E%> has not been declared",
21358 TYPE_CONTEXT (nested_name_specifier),
21359 nested_name_specifier);
21360 type = error_mark_node;
21362 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21363 && !CLASS_TYPE_P (nested_name_specifier))
21365 error_at (type_start_token->location, "nested name specifier "
21366 "%qT for enum declaration does not name a class "
21367 "or namespace", nested_name_specifier);
21368 type = error_mark_node;
21370 /* If that scope does not contain the scope in which the
21371 class was originally declared, the program is invalid. */
21372 else if (prev_scope && !is_ancestor (prev_scope,
21373 nested_name_specifier))
21375 if (at_namespace_scope_p ())
21376 error_at (type_start_token->location,
21377 "declaration of %qD in namespace %qD which does not "
21378 "enclose %qD",
21379 type, prev_scope, nested_name_specifier);
21380 else
21381 error_at (type_start_token->location,
21382 "declaration of %qD in %qD which does not "
21383 "enclose %qD",
21384 type, prev_scope, nested_name_specifier);
21385 type = error_mark_node;
21387 /* If that scope is the scope where the declaration is being placed
21388 the program is invalid. */
21389 else if (CLASS_TYPE_P (nested_name_specifier)
21390 && CLASS_TYPE_P (prev_scope)
21391 && same_type_p (nested_name_specifier, prev_scope))
21393 permerror (type_start_token->location,
21394 "extra qualification not allowed");
21395 nested_name_specifier = NULL_TREE;
21399 if (scoped_enum_p)
21400 begin_scope (sk_scoped_enum, type);
21402 /* Consume the opening brace. */
21403 matching_braces braces;
21404 braces.consume_open (parser);
21406 if (type == error_mark_node)
21407 ; /* Nothing to add */
21408 else if (OPAQUE_ENUM_P (type)
21409 || (cxx_dialect > cxx98 && processing_specialization))
21411 new_value_list = true;
21412 SET_OPAQUE_ENUM_P (type, false);
21413 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21415 else
21417 error_at (type_start_token->location,
21418 "multiple definition of %q#T", type);
21419 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21420 "previous definition here");
21421 type = error_mark_node;
21424 if (type == error_mark_node)
21425 cp_parser_skip_to_end_of_block_or_statement (parser);
21426 /* If the next token is not '}', then there are some enumerators. */
21427 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21429 if (is_unnamed && !scoped_enum_p
21430 /* Don't warn for enum {} a; here. */
21431 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21432 pedwarn (type_start_token->location, OPT_Wpedantic,
21433 "ISO C++ forbids empty unnamed enum");
21435 else
21437 /* We've seen a '{' so we know we're in an enum-specifier.
21438 Commit to any tentative parse to get syntax errors. */
21439 cp_parser_commit_to_tentative_parse (parser);
21440 cp_parser_enumerator_list (parser, type);
21443 /* Consume the final '}'. */
21444 braces.require_close (parser);
21446 if (scoped_enum_p)
21447 finish_scope ();
21449 else
21451 /* If a ';' follows, then it is an opaque-enum-specifier
21452 and additional restrictions apply. */
21453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21455 if (is_unnamed)
21456 error_at (type_start_token->location,
21457 "opaque-enum-specifier without name");
21458 else if (nested_name_specifier)
21459 error_at (type_start_token->location,
21460 "opaque-enum-specifier must use a simple identifier");
21464 /* Look for trailing attributes to apply to this enumeration, and
21465 apply them if appropriate. */
21466 if (cp_parser_allow_gnu_extensions_p (parser))
21468 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21469 cplus_decl_attributes (&type,
21470 trailing_attr,
21471 (int) ATTR_FLAG_TYPE_IN_PLACE);
21474 /* Finish up the enumeration. */
21475 if (type != error_mark_node)
21477 if (new_value_list)
21478 finish_enum_value_list (type);
21479 if (is_new_type)
21480 finish_enum (type);
21483 if (nested_name_specifier)
21485 if (CLASS_TYPE_P (nested_name_specifier))
21487 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21488 pop_scope (nested_name_specifier);
21490 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21491 pop_nested_namespace (nested_name_specifier);
21493 return type;
21496 /* Parse an enumerator-list. The enumerators all have the indicated
21497 TYPE.
21499 enumerator-list:
21500 enumerator-definition
21501 enumerator-list , enumerator-definition */
21503 static void
21504 cp_parser_enumerator_list (cp_parser* parser, tree type)
21506 while (true)
21508 /* Parse an enumerator-definition. */
21509 cp_parser_enumerator_definition (parser, type);
21511 /* If the next token is not a ',', we've reached the end of
21512 the list. */
21513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21514 break;
21515 /* Otherwise, consume the `,' and keep going. */
21516 cp_lexer_consume_token (parser->lexer);
21517 /* If the next token is a `}', there is a trailing comma. */
21518 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21520 if (cxx_dialect < cxx11)
21521 pedwarn (input_location, OPT_Wpedantic,
21522 "comma at end of enumerator list");
21523 break;
21528 /* Parse an enumerator-definition. The enumerator has the indicated
21529 TYPE.
21531 enumerator-definition:
21532 enumerator
21533 enumerator = constant-expression
21535 enumerator:
21536 identifier
21538 GNU Extensions:
21540 enumerator-definition:
21541 enumerator attributes [opt]
21542 enumerator attributes [opt] = constant-expression */
21544 static void
21545 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21547 tree identifier;
21548 tree value;
21549 location_t loc;
21551 /* Save the input location because we are interested in the location
21552 of the identifier and not the location of the explicit value. */
21553 loc = cp_lexer_peek_token (parser->lexer)->location;
21555 /* Look for the identifier. */
21556 identifier = cp_parser_identifier (parser);
21557 if (identifier == error_mark_node)
21558 return;
21560 /* Parse any specified attributes. */
21561 tree attrs = cp_parser_attributes_opt (parser);
21563 /* If the next token is an '=', then there is an explicit value. */
21564 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21566 /* Consume the `=' token. */
21567 cp_lexer_consume_token (parser->lexer);
21568 /* Parse the value. */
21569 value = cp_parser_constant_expression (parser);
21571 else
21572 value = NULL_TREE;
21574 /* If we are processing a template, make sure the initializer of the
21575 enumerator doesn't contain any bare template parameter pack. */
21576 if (current_lambda_expr ())
21578 /* In a lambda it should work, but doesn't currently. */
21579 if (uses_parameter_packs (value))
21581 sorry ("unexpanded parameter pack in enumerator in lambda");
21582 value = error_mark_node;
21585 else if (check_for_bare_parameter_packs (value))
21586 value = error_mark_node;
21588 /* Create the enumerator. */
21589 build_enumerator (identifier, value, type, attrs, loc);
21592 /* Parse a namespace-name.
21594 namespace-name:
21595 original-namespace-name
21596 namespace-alias
21598 Returns the NAMESPACE_DECL for the namespace. */
21600 static tree
21601 cp_parser_namespace_name (cp_parser* parser)
21603 tree identifier;
21604 tree namespace_decl;
21606 cp_token *token = cp_lexer_peek_token (parser->lexer);
21608 /* Get the name of the namespace. */
21609 identifier = cp_parser_identifier (parser);
21610 if (identifier == error_mark_node)
21611 return error_mark_node;
21613 /* Look up the identifier in the currently active scope. Look only
21614 for namespaces, due to:
21616 [basic.lookup.udir]
21618 When looking up a namespace-name in a using-directive or alias
21619 definition, only namespace names are considered.
21621 And:
21623 [basic.lookup.qual]
21625 During the lookup of a name preceding the :: scope resolution
21626 operator, object, function, and enumerator names are ignored.
21628 (Note that cp_parser_qualifying_entity only calls this
21629 function if the token after the name is the scope resolution
21630 operator.) */
21631 namespace_decl = cp_parser_lookup_name (parser, identifier,
21632 none_type,
21633 /*is_template=*/false,
21634 /*is_namespace=*/true,
21635 /*check_dependency=*/true,
21636 /*ambiguous_decls=*/NULL,
21637 token->location);
21638 /* If it's not a namespace, issue an error. */
21639 if (namespace_decl == error_mark_node
21640 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21642 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21644 auto_diagnostic_group d;
21645 name_hint hint;
21646 if (namespace_decl == error_mark_node
21647 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21648 hint = suggest_alternative_in_explicit_scope (token->location,
21649 identifier,
21650 parser->scope);
21651 if (const char *suggestion = hint.suggestion ())
21653 gcc_rich_location richloc (token->location);
21654 richloc.add_fixit_replace (suggestion);
21655 error_at (&richloc,
21656 "%qD is not a namespace-name; did you mean %qs?",
21657 identifier, suggestion);
21659 else
21660 error_at (token->location, "%qD is not a namespace-name",
21661 identifier);
21663 else
21664 cp_parser_error (parser, "expected namespace-name");
21665 namespace_decl = error_mark_node;
21668 return namespace_decl;
21671 /* Parse a namespace-definition.
21673 namespace-definition:
21674 named-namespace-definition
21675 unnamed-namespace-definition
21677 named-namespace-definition:
21678 original-namespace-definition
21679 extension-namespace-definition
21681 original-namespace-definition:
21682 namespace identifier { namespace-body }
21684 extension-namespace-definition:
21685 namespace original-namespace-name { namespace-body }
21687 unnamed-namespace-definition:
21688 namespace { namespace-body } */
21690 static void
21691 cp_parser_namespace_definition (cp_parser* parser)
21693 tree identifier;
21694 int nested_definition_count = 0;
21696 cp_ensure_no_omp_declare_simd (parser);
21697 cp_ensure_no_oacc_routine (parser);
21699 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21700 const bool topmost_inline_p = is_inline;
21702 if (is_inline)
21704 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21705 cp_lexer_consume_token (parser->lexer);
21708 /* Look for the `namespace' keyword. */
21709 cp_token* token
21710 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21712 /* Parse any specified attributes before the identifier. */
21713 tree attribs = cp_parser_attributes_opt (parser);
21715 for (;;)
21717 identifier = NULL_TREE;
21719 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21720 RID_INLINE);
21721 if (nested_inline_p && nested_definition_count != 0)
21723 if (pedantic && cxx_dialect < cxx20)
21724 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21725 OPT_Wc__20_extensions, "nested inline namespace "
21726 "definitions only available with %<-std=c++20%> or "
21727 "%<-std=gnu++20%>");
21728 cp_lexer_consume_token (parser->lexer);
21731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21733 identifier = cp_parser_identifier (parser);
21735 if (cp_next_tokens_can_be_std_attribute_p (parser))
21736 pedwarn (input_location, OPT_Wpedantic,
21737 "standard attributes on namespaces must precede "
21738 "the namespace name");
21740 /* Parse any attributes specified after the identifier. */
21741 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21746 /* Don't forget that the innermost namespace might have been
21747 marked as inline. Use |= because we cannot overwrite
21748 IS_INLINE in case the outermost namespace is inline, but
21749 there are no nested inlines. */
21750 is_inline |= nested_inline_p;
21751 break;
21754 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21755 pedwarn (input_location, OPT_Wc__17_extensions,
21756 "nested namespace definitions only available with "
21757 "%<-std=c++17%> or %<-std=gnu++17%>");
21759 /* Nested namespace names can create new namespaces (unlike
21760 other qualified-ids). */
21761 if (int count = (identifier
21762 ? push_namespace (identifier, nested_inline_p)
21763 : 0))
21764 nested_definition_count += count;
21765 else
21766 cp_parser_error (parser, "nested namespace name required");
21767 cp_lexer_consume_token (parser->lexer);
21770 if (nested_definition_count && !identifier)
21771 cp_parser_error (parser, "namespace name required");
21773 if (nested_definition_count && attribs)
21774 error_at (token->location,
21775 "a nested namespace definition cannot have attributes");
21776 if (nested_definition_count && topmost_inline_p)
21777 error_at (token->location,
21778 "a nested namespace definition cannot be inline");
21780 /* Start the namespace. */
21781 nested_definition_count += push_namespace (identifier, is_inline);
21783 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21785 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21787 /* Look for the `{' to validate starting the namespace. */
21788 matching_braces braces;
21789 if (braces.require_open (parser))
21791 /* Parse the body of the namespace. */
21792 cp_parser_namespace_body (parser);
21794 /* Look for the final `}'. */
21795 braces.require_close (parser);
21798 if (has_visibility)
21799 pop_visibility (1);
21801 /* Pop the nested namespace definitions. */
21802 while (nested_definition_count--)
21803 pop_namespace ();
21806 /* Parse a namespace-body.
21808 namespace-body:
21809 declaration-seq [opt] */
21811 static void
21812 cp_parser_namespace_body (cp_parser* parser)
21814 cp_parser_declaration_seq_opt (parser);
21817 /* Parse a namespace-alias-definition.
21819 namespace-alias-definition:
21820 namespace identifier = qualified-namespace-specifier ; */
21822 static void
21823 cp_parser_namespace_alias_definition (cp_parser* parser)
21825 tree identifier;
21826 tree namespace_specifier;
21828 cp_token *token = cp_lexer_peek_token (parser->lexer);
21830 /* Look for the `namespace' keyword. */
21831 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21832 /* Look for the identifier. */
21833 identifier = cp_parser_identifier (parser);
21834 if (identifier == error_mark_node)
21835 return;
21836 /* Look for the `=' token. */
21837 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21838 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21840 error_at (token->location, "%<namespace%> definition is not allowed here");
21841 /* Skip the definition. */
21842 cp_lexer_consume_token (parser->lexer);
21843 if (cp_parser_skip_to_closing_brace (parser))
21844 cp_lexer_consume_token (parser->lexer);
21845 return;
21847 cp_parser_require (parser, CPP_EQ, RT_EQ);
21848 /* Look for the qualified-namespace-specifier. */
21849 namespace_specifier
21850 = cp_parser_qualified_namespace_specifier (parser);
21851 cp_warn_deprecated_use_scopes (namespace_specifier);
21852 /* Look for the `;' token. */
21853 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21855 /* Register the alias in the symbol table. */
21856 do_namespace_alias (identifier, namespace_specifier);
21859 /* Parse a qualified-namespace-specifier.
21861 qualified-namespace-specifier:
21862 :: [opt] nested-name-specifier [opt] namespace-name
21864 Returns a NAMESPACE_DECL corresponding to the specified
21865 namespace. */
21867 static tree
21868 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21870 /* Look for the optional `::'. */
21871 cp_parser_global_scope_opt (parser,
21872 /*current_scope_valid_p=*/false);
21874 /* Look for the optional nested-name-specifier. */
21875 cp_parser_nested_name_specifier_opt (parser,
21876 /*typename_keyword_p=*/false,
21877 /*check_dependency_p=*/true,
21878 /*type_p=*/false,
21879 /*is_declaration=*/true);
21881 return cp_parser_namespace_name (parser);
21884 /* Subroutine of cp_parser_using_declaration. */
21886 static tree
21887 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21889 tree decl = NULL_TREE;
21890 if (at_class_scope_p ())
21892 /* Create the USING_DECL. */
21893 decl = do_class_using_decl (qscope, identifier);
21895 if (check_for_bare_parameter_packs (decl))
21896 return error_mark_node;
21898 if (decl && typename_p)
21899 USING_DECL_TYPENAME_P (decl) = 1;
21901 /* Add it to the list of members in this class. */
21902 finish_member_declaration (decl);
21904 else
21905 finish_nonmember_using_decl (qscope, identifier);
21906 return decl;
21909 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21910 access declaration.
21912 using-declaration:
21913 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21914 using :: unqualified-id ;
21916 access-declaration:
21917 qualified-id ;
21921 static bool
21922 cp_parser_using_declaration (cp_parser* parser,
21923 bool access_declaration_p)
21925 cp_token *token;
21926 bool typename_p = false;
21927 bool global_scope_p;
21928 tree identifier;
21929 tree qscope;
21930 int oldcount = errorcount;
21931 cp_token *diag_token = NULL;
21933 if (access_declaration_p)
21935 diag_token = cp_lexer_peek_token (parser->lexer);
21936 cp_parser_parse_tentatively (parser);
21938 else
21940 /* Look for the `using' keyword. */
21941 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21943 again:
21944 /* Peek at the next token. */
21945 token = cp_lexer_peek_token (parser->lexer);
21946 /* See if it's `typename'. */
21947 if (token->keyword == RID_TYPENAME)
21949 /* Remember that we've seen it. */
21950 typename_p = true;
21951 /* Consume the `typename' token. */
21952 cp_lexer_consume_token (parser->lexer);
21956 /* Look for the optional global scope qualification. */
21957 global_scope_p
21958 = (cp_parser_global_scope_opt (parser,
21959 /*current_scope_valid_p=*/false)
21960 != NULL_TREE);
21962 /* If we saw `typename', or didn't see `::', then there must be a
21963 nested-name-specifier present. */
21964 if (typename_p || !global_scope_p)
21966 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21967 /*check_dependency_p=*/true,
21968 /*type_p=*/false,
21969 /*is_declaration=*/true);
21970 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21972 cp_parser_skip_to_end_of_block_or_statement (parser);
21973 return false;
21976 /* Otherwise, we could be in either of the two productions. In that
21977 case, treat the nested-name-specifier as optional. */
21978 else
21979 qscope = cp_parser_nested_name_specifier_opt (parser,
21980 /*typename_keyword_p=*/false,
21981 /*check_dependency_p=*/true,
21982 /*type_p=*/false,
21983 /*is_declaration=*/true);
21984 if (!qscope)
21985 qscope = global_namespace;
21987 cp_warn_deprecated_use_scopes (qscope);
21989 if (access_declaration_p
21990 && !MAYBE_CLASS_TYPE_P (qscope)
21991 && TREE_CODE (qscope) != ENUMERAL_TYPE)
21992 /* If the qualifying scope of an access-declaration isn't a class
21993 or enumeration type then it can't be valid. */
21994 cp_parser_simulate_error (parser);
21996 if (access_declaration_p && cp_parser_error_occurred (parser))
21997 /* Something has already gone wrong; there's no need to parse
21998 further. Since an error has occurred, the return value of
21999 cp_parser_parse_definitely will be false, as required. */
22000 return cp_parser_parse_definitely (parser);
22002 token = cp_lexer_peek_token (parser->lexer);
22003 /* Parse the unqualified-id. */
22004 identifier = cp_parser_unqualified_id (parser,
22005 /*template_keyword_p=*/false,
22006 /*check_dependency_p=*/true,
22007 /*declarator_p=*/true,
22008 /*optional_p=*/false);
22010 if (access_declaration_p)
22012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22013 cp_parser_simulate_error (parser);
22014 if (!cp_parser_parse_definitely (parser))
22015 return false;
22017 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22019 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22020 if (cxx_dialect < cxx17)
22021 pedwarn (ell->location, OPT_Wc__17_extensions,
22022 "pack expansion in using-declaration only available "
22023 "with %<-std=c++17%> or %<-std=gnu++17%>");
22025 /* A parameter pack can appear in the qualifying scope, and/or in the
22026 terminal name (if naming a conversion function). Logically they're
22027 part of a single pack expansion of the overall USING_DECL, but we
22028 express them as separate pack expansions within the USING_DECL since
22029 we can't create a pack expansion over a USING_DECL. */
22030 bool saw_parm_pack = false;
22031 if (uses_parameter_packs (qscope))
22033 qscope = make_pack_expansion (qscope);
22034 saw_parm_pack = true;
22036 if (identifier_p (identifier)
22037 && IDENTIFIER_CONV_OP_P (identifier)
22038 && uses_parameter_packs (TREE_TYPE (identifier)))
22040 identifier = make_conv_op_name (make_pack_expansion
22041 (TREE_TYPE (identifier)));
22042 saw_parm_pack = true;
22044 if (!saw_parm_pack)
22046 /* Issue an error in terms using a SCOPE_REF that includes both
22047 components. */
22048 tree name
22049 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22050 make_pack_expansion (name);
22051 gcc_assert (seen_error ());
22052 qscope = identifier = error_mark_node;
22056 /* The function we call to handle a using-declaration is different
22057 depending on what scope we are in. */
22058 if (qscope == error_mark_node || identifier == error_mark_node)
22060 else if (!identifier_p (identifier)
22061 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22062 /* [namespace.udecl]
22064 A using declaration shall not name a template-id. */
22065 error_at (token->location,
22066 "a template-id may not appear in a using-declaration");
22067 else
22069 tree decl = finish_using_decl (qscope, identifier, typename_p);
22071 if (decl == error_mark_node)
22073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22074 return false;
22078 if (!access_declaration_p
22079 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22081 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22082 if (cxx_dialect < cxx17)
22083 pedwarn (comma->location, OPT_Wc__17_extensions,
22084 "comma-separated list in using-declaration only available "
22085 "with %<-std=c++17%> or %<-std=gnu++17%>");
22086 goto again;
22089 /* Look for the final `;'. */
22090 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22092 if (access_declaration_p && errorcount == oldcount)
22093 warning_at (diag_token->location, OPT_Wdeprecated,
22094 "access declarations are deprecated "
22095 "in favour of using-declarations; "
22096 "suggestion: add the %<using%> keyword");
22098 return true;
22101 /* C++20 using enum declaration.
22103 using-enum-declaration :
22104 using elaborated-enum-specifier ; */
22106 static void
22107 cp_parser_using_enum (cp_parser *parser)
22109 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22111 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22112 breaks one of the motivating examples in using-enum-5.C.
22113 cp_parser_simple_type_specifier seems to be closer to what we actually
22114 want, though that hasn't been properly specified yet. */
22116 /* Consume 'enum'. */
22117 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22118 cp_lexer_consume_token (parser->lexer);
22120 cp_token *start = cp_lexer_peek_token (parser->lexer);
22122 tree type = (cp_parser_simple_type_specifier
22123 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22125 cp_token *end = cp_lexer_previous_token (parser->lexer);
22127 if (type == error_mark_node
22128 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22130 cp_parser_skip_to_end_of_block_or_statement (parser);
22131 return;
22133 if (TREE_CODE (type) == TYPE_DECL)
22134 type = TREE_TYPE (type);
22136 /* The elaborated-enum-specifier shall not name a dependent type and the type
22137 shall have a reachable enum-specifier. */
22138 const char *msg = nullptr;
22139 if (cxx_dialect < cxx20)
22140 msg = _("%<using enum%> "
22141 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22142 else if (dependent_type_p (type))
22143 msg = _("%<using enum%> of dependent type %qT");
22144 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22145 msg = _("%<using enum%> of non-enumeration type %q#T");
22146 else if (!COMPLETE_TYPE_P (type))
22147 msg = _("%<using enum%> of incomplete type %qT");
22148 else if (OPAQUE_ENUM_P (type))
22149 msg = _("%<using enum%> of %qT before its enum-specifier");
22150 if (msg)
22152 location_t loc = make_location (start, start, end);
22153 auto_diagnostic_group g;
22154 error_at (loc, msg, type);
22155 loc = location_of (type);
22156 if (cxx_dialect < cxx20 || loc == input_location)
22158 else if (OPAQUE_ENUM_P (type))
22159 inform (loc, "opaque-enum-declaration here");
22160 else
22161 inform (loc, "declared here");
22164 /* A using-enum-declaration introduces the enumerator names of the named
22165 enumeration as if by a using-declaration for each enumerator. */
22166 if (TREE_CODE (type) == ENUMERAL_TYPE)
22167 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22168 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22171 /* Parse an alias-declaration.
22173 alias-declaration:
22174 using identifier attribute-specifier-seq [opt] = type-id */
22176 static tree
22177 cp_parser_alias_declaration (cp_parser* parser)
22179 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22180 location_t id_location, type_location;
22181 cp_declarator *declarator;
22182 cp_decl_specifier_seq decl_specs;
22183 bool member_p;
22184 const char *saved_message = NULL;
22186 /* Look for the `using' keyword. */
22187 cp_token *using_token
22188 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22189 if (using_token == NULL)
22190 return error_mark_node;
22192 id_location = cp_lexer_peek_token (parser->lexer)->location;
22193 id = cp_parser_identifier (parser);
22194 if (id == error_mark_node)
22195 return error_mark_node;
22197 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22198 attributes = cp_parser_attributes_opt (parser);
22199 if (attributes == error_mark_node)
22200 return error_mark_node;
22202 cp_parser_require (parser, CPP_EQ, RT_EQ);
22204 if (cp_parser_error_occurred (parser))
22205 return error_mark_node;
22207 cp_parser_commit_to_tentative_parse (parser);
22209 /* Now we are going to parse the type-id of the declaration. */
22212 [dcl.type]/3 says:
22214 "A type-specifier-seq shall not define a class or enumeration
22215 unless it appears in the type-id of an alias-declaration (7.1.3) that
22216 is not the declaration of a template-declaration."
22218 In other words, if we currently are in an alias template, the
22219 type-id should not define a type.
22221 So let's set parser->type_definition_forbidden_message in that
22222 case; cp_parser_check_type_definition (called by
22223 cp_parser_class_specifier) will then emit an error if a type is
22224 defined in the type-id. */
22225 if (parser->num_template_parameter_lists)
22227 saved_message = parser->type_definition_forbidden_message;
22228 parser->type_definition_forbidden_message =
22229 G_("types may not be defined in alias template declarations");
22232 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22233 &type_location);
22235 /* Restore the error message if need be. */
22236 if (parser->num_template_parameter_lists)
22237 parser->type_definition_forbidden_message = saved_message;
22239 if (type == error_mark_node
22240 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22242 cp_parser_skip_to_end_of_block_or_statement (parser);
22243 return error_mark_node;
22246 /* A typedef-name can also be introduced by an alias-declaration. The
22247 identifier following the using keyword becomes a typedef-name. It has
22248 the same semantics as if it were introduced by the typedef
22249 specifier. In particular, it does not define a new type and it shall
22250 not appear in the type-id. */
22252 clear_decl_specs (&decl_specs);
22253 decl_specs.type = type;
22254 if (attributes != NULL_TREE)
22256 decl_specs.attributes = attributes;
22257 set_and_check_decl_spec_loc (&decl_specs,
22258 ds_attribute,
22259 attrs_token);
22261 set_and_check_decl_spec_loc (&decl_specs,
22262 ds_typedef,
22263 using_token);
22264 set_and_check_decl_spec_loc (&decl_specs,
22265 ds_alias,
22266 using_token);
22267 decl_specs.locations[ds_type_spec] = type_location;
22269 if (parser->num_template_parameter_lists
22270 && !cp_parser_check_template_parameters (parser,
22271 /*num_templates=*/0,
22272 /*template_id*/false,
22273 id_location,
22274 /*declarator=*/NULL))
22275 return error_mark_node;
22277 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22279 member_p = at_class_scope_p ();
22280 if (member_p)
22281 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22282 NULL_TREE, attributes);
22283 else
22284 decl = start_decl (declarator, &decl_specs, 0,
22285 attributes, NULL_TREE, &pushed_scope);
22286 if (decl == error_mark_node)
22287 return decl;
22289 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22291 if (pushed_scope)
22292 pop_scope (pushed_scope);
22294 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22295 added into the symbol table; otherwise, return the TYPE_DECL. */
22296 if (DECL_LANG_SPECIFIC (decl)
22297 && DECL_TEMPLATE_INFO (decl)
22298 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22300 decl = DECL_TI_TEMPLATE (decl);
22301 if (member_p)
22302 check_member_template (decl);
22305 return decl;
22308 /* Parse a using-directive.
22310 using-directive:
22311 attribute-specifier-seq [opt] using namespace :: [opt]
22312 nested-name-specifier [opt] namespace-name ; */
22314 static void
22315 cp_parser_using_directive (cp_parser* parser)
22317 tree namespace_decl;
22318 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22321 /* Error during attribute parsing that resulted in skipping
22322 to next semicolon. */
22323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22324 return;
22327 /* Look for the `using' keyword. */
22328 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22329 /* And the `namespace' keyword. */
22330 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22331 /* Look for the optional `::' operator. */
22332 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22333 /* And the optional nested-name-specifier. */
22334 cp_parser_nested_name_specifier_opt (parser,
22335 /*typename_keyword_p=*/false,
22336 /*check_dependency_p=*/true,
22337 /*type_p=*/false,
22338 /*is_declaration=*/true);
22339 /* Get the namespace being used. */
22340 namespace_decl = cp_parser_namespace_name (parser);
22341 cp_warn_deprecated_use_scopes (namespace_decl);
22342 /* And any specified GNU attributes. */
22343 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22344 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22346 /* Update the symbol table. */
22347 finish_using_directive (namespace_decl, attribs);
22349 /* Look for the final `;'. */
22350 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22353 /* Parse an asm-definition.
22355 asm-qualifier:
22356 volatile
22357 inline
22358 goto
22360 asm-qualifier-list:
22361 asm-qualifier
22362 asm-qualifier-list asm-qualifier
22364 asm-definition:
22365 asm ( string-literal ) ;
22367 GNU Extension:
22369 asm-definition:
22370 asm asm-qualifier-list [opt] ( string-literal ) ;
22371 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22372 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22373 : asm-operand-list [opt] ) ;
22374 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22375 : asm-operand-list [opt]
22376 : asm-clobber-list [opt] ) ;
22377 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22378 : asm-clobber-list [opt]
22379 : asm-goto-list ) ;
22381 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22382 contains goto, and is the only allowed form in that case. No duplicates are
22383 allowed in an asm-qualifier-list. */
22385 static void
22386 cp_parser_asm_definition (cp_parser* parser)
22388 tree outputs = NULL_TREE;
22389 tree inputs = NULL_TREE;
22390 tree clobbers = NULL_TREE;
22391 tree labels = NULL_TREE;
22392 tree asm_stmt;
22393 bool extended_p = false;
22394 bool invalid_inputs_p = false;
22395 bool invalid_outputs_p = false;
22396 required_token missing = RT_NONE;
22397 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22399 /* Look for the `asm' keyword. */
22400 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22402 /* In C++20, unevaluated inline assembly is permitted in constexpr
22403 functions. */
22404 if (parser->in_function_body
22405 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22406 && cxx_dialect < cxx20)
22407 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22408 "function only available with %<-std=c++20%> or "
22409 "%<-std=gnu++20%>");
22411 /* Handle the asm-qualifier-list. */
22412 location_t volatile_loc = UNKNOWN_LOCATION;
22413 location_t inline_loc = UNKNOWN_LOCATION;
22414 location_t goto_loc = UNKNOWN_LOCATION;
22415 location_t first_loc = UNKNOWN_LOCATION;
22417 if (cp_parser_allow_gnu_extensions_p (parser))
22418 for (;;)
22420 cp_token *token = cp_lexer_peek_token (parser->lexer);
22421 location_t loc = token->location;
22422 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22424 case RID_VOLATILE:
22425 if (volatile_loc)
22427 error_at (loc, "duplicate %<asm%> qualifier %qT",
22428 token->u.value);
22429 inform (volatile_loc, "first seen here");
22431 else
22433 if (!parser->in_function_body)
22434 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22435 "outside of function body", token->u.value);
22436 volatile_loc = loc;
22438 cp_lexer_consume_token (parser->lexer);
22439 continue;
22441 case RID_INLINE:
22442 if (inline_loc)
22444 error_at (loc, "duplicate %<asm%> qualifier %qT",
22445 token->u.value);
22446 inform (inline_loc, "first seen here");
22448 else
22449 inline_loc = loc;
22450 if (!first_loc)
22451 first_loc = loc;
22452 cp_lexer_consume_token (parser->lexer);
22453 continue;
22455 case RID_GOTO:
22456 if (goto_loc)
22458 error_at (loc, "duplicate %<asm%> qualifier %qT",
22459 token->u.value);
22460 inform (goto_loc, "first seen here");
22462 else
22463 goto_loc = loc;
22464 if (!first_loc)
22465 first_loc = loc;
22466 cp_lexer_consume_token (parser->lexer);
22467 continue;
22469 case RID_CONST:
22470 case RID_RESTRICT:
22471 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22472 cp_lexer_consume_token (parser->lexer);
22473 continue;
22475 default:
22476 break;
22478 break;
22481 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22482 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22483 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22485 if (!parser->in_function_body && (inline_p || goto_p))
22487 error_at (first_loc, "%<asm%> qualifier outside of function body");
22488 inline_p = goto_p = false;
22491 /* Look for the opening `('. */
22492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22493 return;
22494 /* Look for the string. */
22495 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22496 /*wide_ok=*/false);
22497 if (string == error_mark_node)
22499 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22500 /*consume_paren=*/true);
22501 return;
22504 /* If we're allowing GNU extensions, check for the extended assembly
22505 syntax. Unfortunately, the `:' tokens need not be separated by
22506 a space in C, and so, for compatibility, we tolerate that here
22507 too. Doing that means that we have to treat the `::' operator as
22508 two `:' tokens. */
22509 if (cp_parser_allow_gnu_extensions_p (parser)
22510 && parser->in_function_body
22511 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22512 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22514 bool inputs_p = false;
22515 bool clobbers_p = false;
22516 bool labels_p = false;
22518 /* The extended syntax was used. */
22519 extended_p = true;
22521 /* Look for outputs. */
22522 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22524 /* Consume the `:'. */
22525 cp_lexer_consume_token (parser->lexer);
22526 /* Parse the output-operands. */
22527 if (cp_lexer_next_token_is_not (parser->lexer,
22528 CPP_COLON)
22529 && cp_lexer_next_token_is_not (parser->lexer,
22530 CPP_SCOPE)
22531 && cp_lexer_next_token_is_not (parser->lexer,
22532 CPP_CLOSE_PAREN))
22534 outputs = cp_parser_asm_operand_list (parser);
22535 if (outputs == error_mark_node)
22536 invalid_outputs_p = true;
22539 /* If the next token is `::', there are no outputs, and the
22540 next token is the beginning of the inputs. */
22541 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22542 /* The inputs are coming next. */
22543 inputs_p = true;
22545 /* Look for inputs. */
22546 if (inputs_p
22547 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22549 /* Consume the `:' or `::'. */
22550 cp_lexer_consume_token (parser->lexer);
22551 /* Parse the output-operands. */
22552 if (cp_lexer_next_token_is_not (parser->lexer,
22553 CPP_COLON)
22554 && cp_lexer_next_token_is_not (parser->lexer,
22555 CPP_SCOPE)
22556 && cp_lexer_next_token_is_not (parser->lexer,
22557 CPP_CLOSE_PAREN))
22559 inputs = cp_parser_asm_operand_list (parser);
22560 if (inputs == error_mark_node)
22561 invalid_inputs_p = true;
22564 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22565 /* The clobbers are coming next. */
22566 clobbers_p = true;
22568 /* Look for clobbers. */
22569 if (clobbers_p
22570 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22572 clobbers_p = true;
22573 /* Consume the `:' or `::'. */
22574 cp_lexer_consume_token (parser->lexer);
22575 /* Parse the clobbers. */
22576 if (cp_lexer_next_token_is_not (parser->lexer,
22577 CPP_COLON)
22578 && cp_lexer_next_token_is_not (parser->lexer,
22579 CPP_CLOSE_PAREN))
22580 clobbers = cp_parser_asm_clobber_list (parser);
22582 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22583 /* The labels are coming next. */
22584 labels_p = true;
22586 /* Look for labels. */
22587 if (labels_p
22588 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22590 labels_p = true;
22591 /* Consume the `:' or `::'. */
22592 cp_lexer_consume_token (parser->lexer);
22593 /* Parse the labels. */
22594 labels = cp_parser_asm_label_list (parser);
22597 if (goto_p && !labels_p)
22598 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22600 else if (goto_p)
22601 missing = RT_COLON_SCOPE;
22603 /* Look for the closing `)'. */
22604 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22605 missing ? missing : RT_CLOSE_PAREN))
22606 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22607 /*consume_paren=*/true);
22608 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22610 if (!invalid_inputs_p && !invalid_outputs_p)
22612 /* Create the ASM_EXPR. */
22613 if (parser->in_function_body)
22615 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22616 inputs, clobbers, labels, inline_p);
22617 /* If the extended syntax was not used, mark the ASM_EXPR. */
22618 if (!extended_p)
22620 tree temp = asm_stmt;
22621 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22622 temp = TREE_OPERAND (temp, 0);
22624 ASM_INPUT_P (temp) = 1;
22627 else
22628 symtab->finalize_toplevel_asm (string);
22632 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22633 type that comes from the decl-specifier-seq. */
22635 static tree
22636 strip_declarator_types (tree type, cp_declarator *declarator)
22638 for (cp_declarator *d = declarator; d;)
22639 switch (d->kind)
22641 case cdk_id:
22642 case cdk_decomp:
22643 case cdk_error:
22644 d = NULL;
22645 break;
22647 default:
22648 if (TYPE_PTRMEMFUNC_P (type))
22649 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22650 type = TREE_TYPE (type);
22651 d = d->declarator;
22652 break;
22655 return type;
22658 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22659 a construct looks like a variable definition but is actually a function
22660 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22661 is the declarator for this function declaration. */
22663 static void
22664 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22665 const cp_declarator *declarator)
22667 /* Only warn if we are declaring a function at block scope. */
22668 if (!at_function_scope_p ())
22669 return;
22671 /* And only if there is no storage class specified. */
22672 if (decl_specifiers->storage_class != sc_none
22673 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22674 return;
22676 if (declarator->kind != cdk_function
22677 || !declarator->declarator
22678 || declarator->declarator->kind != cdk_id
22679 || !identifier_p (get_unqualified_id
22680 (const_cast<cp_declarator *>(declarator))))
22681 return;
22683 /* Don't warn when the whole declarator (not just the declarator-id!)
22684 was parenthesized. That is, don't warn for int(n()) but do warn
22685 for int(f)(). */
22686 if (declarator->parenthesized != UNKNOWN_LOCATION)
22687 return;
22689 tree type;
22690 if (decl_specifiers->type)
22692 type = decl_specifiers->type;
22693 if (TREE_CODE (type) == TYPE_DECL)
22694 type = TREE_TYPE (type);
22696 /* If the return type is void there is no ambiguity. */
22697 if (same_type_p (type, void_type_node))
22698 return;
22700 else if (decl_specifiers->any_type_specifiers_p)
22701 /* Code like long f(); will have null ->type. If we have any
22702 type-specifiers, pretend we've seen int. */
22703 type = integer_type_node;
22704 else
22705 return;
22707 auto_diagnostic_group d;
22708 location_t loc = declarator->u.function.parens_loc;
22709 tree params = declarator->u.function.parameters;
22710 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22712 /* The T t() case. */
22713 if (params == void_list_node)
22715 if (warning_at (loc, OPT_Wvexing_parse,
22716 "empty parentheses were disambiguated as a function "
22717 "declaration"))
22719 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22720 means value-initialization or aggregate-initialization, nothing
22721 means default-initialization. We can only suggest removing the
22722 parentheses/adding {} if T has a default constructor. */
22723 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22725 gcc_rich_location iloc (loc);
22726 iloc.add_fixit_remove ();
22727 inform (&iloc, "remove parentheses to default-initialize "
22728 "a variable");
22729 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22731 if (CP_AGGREGATE_TYPE_P (type))
22732 inform (loc, "or replace parentheses with braces to "
22733 "aggregate-initialize a variable");
22734 else
22735 inform (loc, "or replace parentheses with braces to "
22736 "value-initialize a variable");
22740 return;
22743 /* If we had (...) or the parameter-list wasn't parenthesized,
22744 we're done. */
22745 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22746 return;
22748 /* The T t(X()) case. */
22749 if (list_length (params) == 2)
22751 if (warning_at (loc, OPT_Wvexing_parse,
22752 "parentheses were disambiguated as a function "
22753 "declaration"))
22755 gcc_rich_location iloc (loc);
22756 /* {}-initialization means that we can use an initializer-list
22757 constructor if no default constructor is available, so don't
22758 suggest using {} for classes that have an initializer_list
22759 constructor. */
22760 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22762 iloc.add_fixit_replace (get_start (loc), "{");
22763 iloc.add_fixit_replace (get_finish (loc), "}");
22764 inform (&iloc, "replace parentheses with braces to declare a "
22765 "variable");
22767 else
22769 iloc.add_fixit_insert_after (get_start (loc), "(");
22770 iloc.add_fixit_insert_before (get_finish (loc), ")");
22771 inform (&iloc, "add parentheses to declare a variable");
22775 /* The T t(X(), X()) case. */
22776 else if (warning_at (loc, OPT_Wvexing_parse,
22777 "parentheses were disambiguated as a function "
22778 "declaration"))
22780 gcc_rich_location iloc (loc);
22781 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22783 iloc.add_fixit_replace (get_start (loc), "{");
22784 iloc.add_fixit_replace (get_finish (loc), "}");
22785 inform (&iloc, "replace parentheses with braces to declare a "
22786 "variable");
22791 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22792 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22793 has the same meaning as in cp_parser_declarator. */
22795 static void
22796 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22797 cp_decl_specifier_seq *decl_specs,
22798 cp_declarator *declarator,
22799 int *ctor_dtor_or_conv_p)
22801 if (cxx_dialect >= cxx17
22802 && *ctor_dtor_or_conv_p <= 0
22803 && !decl_specs->type
22804 && !decl_specs->any_type_specifiers_p
22805 && function_declarator_p (declarator))
22807 cp_declarator *id = get_id_declarator (declarator);
22808 tree name = id->u.id.unqualified_name;
22809 parser->scope = id->u.id.qualifying_scope;
22810 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22811 if (tmpl
22812 && (DECL_CLASS_TEMPLATE_P (tmpl)
22813 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22815 id->u.id.unqualified_name = dguide_name (tmpl);
22816 id->u.id.sfk = sfk_deduction_guide;
22817 *ctor_dtor_or_conv_p = 1;
22822 /* Declarators [gram.dcl.decl] */
22824 /* Parse an init-declarator.
22826 init-declarator:
22827 declarator initializer [opt]
22829 GNU Extension:
22831 init-declarator:
22832 declarator asm-specification [opt] attributes [opt] initializer [opt]
22834 function-definition:
22835 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22836 function-body
22837 decl-specifier-seq [opt] declarator function-try-block
22839 GNU Extension:
22841 function-definition:
22842 __extension__ function-definition
22844 TM Extension:
22846 function-definition:
22847 decl-specifier-seq [opt] declarator function-transaction-block
22849 The parser flags FLAGS is used to control type-specifier parsing.
22851 The DECL_SPECIFIERS apply to this declarator. Returns a
22852 representation of the entity declared. If MEMBER_P is TRUE, then
22853 this declarator appears in a class scope. The new DECL created by
22854 this declarator is returned.
22856 The CHECKS are access checks that should be performed once we know
22857 what entity is being declared (and, therefore, what classes have
22858 befriended it).
22860 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22861 for a function-definition here as well. If the declarator is a
22862 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22863 be TRUE upon return. By that point, the function-definition will
22864 have been completely parsed.
22866 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22867 is FALSE.
22869 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22870 parsed declaration if it is an uninitialized single declarator not followed
22871 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22872 if present, will not be consumed. If returned, this declarator will be
22873 created with SD_INITIALIZED but will not call cp_finish_decl.
22875 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22876 and there is an initializer, the pointed location_t is set to the
22877 location of the '=' or `(', or '{' in C++11 token introducing the
22878 initializer. */
22880 static tree
22881 cp_parser_init_declarator (cp_parser* parser,
22882 cp_parser_flags flags,
22883 cp_decl_specifier_seq *decl_specifiers,
22884 vec<deferred_access_check, va_gc> *checks,
22885 bool function_definition_allowed_p,
22886 bool member_p,
22887 int declares_class_or_enum,
22888 bool* function_definition_p,
22889 tree* maybe_range_for_decl,
22890 location_t* init_loc,
22891 tree* auto_result)
22893 cp_token *token = NULL, *asm_spec_start_token = NULL,
22894 *attributes_start_token = NULL;
22895 cp_declarator *declarator;
22896 tree prefix_attributes;
22897 tree attributes = NULL;
22898 tree asm_specification;
22899 tree initializer;
22900 tree decl = NULL_TREE;
22901 tree scope;
22902 int is_initialized;
22903 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22904 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22905 "(...)". */
22906 enum cpp_ttype initialization_kind;
22907 bool is_direct_init = false;
22908 bool is_non_constant_init;
22909 int ctor_dtor_or_conv_p;
22910 bool friend_p = cp_parser_friend_p (decl_specifiers);
22911 bool static_p = decl_specifiers->storage_class == sc_static;
22912 tree pushed_scope = NULL_TREE;
22913 bool range_for_decl_p = false;
22914 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22915 location_t tmp_init_loc = UNKNOWN_LOCATION;
22917 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22918 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22920 /* Assume that this is not the declarator for a function
22921 definition. */
22922 if (function_definition_p)
22923 *function_definition_p = false;
22925 /* Default arguments are only permitted for function parameters. */
22926 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22927 parser->default_arg_ok_p = false;
22929 /* Defer access checks while parsing the declarator; we cannot know
22930 what names are accessible until we know what is being
22931 declared. */
22932 resume_deferring_access_checks ();
22934 token = cp_lexer_peek_token (parser->lexer);
22936 /* Parse the declarator. */
22937 declarator
22938 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22939 flags, &ctor_dtor_or_conv_p,
22940 /*parenthesized_p=*/NULL,
22941 member_p, friend_p, static_p);
22942 /* Gather up the deferred checks. */
22943 stop_deferring_access_checks ();
22945 parser->default_arg_ok_p = saved_default_arg_ok_p;
22947 /* If the DECLARATOR was erroneous, there's no need to go
22948 further. */
22949 if (declarator == cp_error_declarator)
22950 return error_mark_node;
22952 /* Check that the number of template-parameter-lists is OK. */
22953 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22954 token->location))
22955 return error_mark_node;
22957 if (declares_class_or_enum & 2)
22958 cp_parser_check_for_definition_in_return_type (declarator,
22959 decl_specifiers->type,
22960 decl_specifiers->locations[ds_type_spec]);
22962 /* Figure out what scope the entity declared by the DECLARATOR is
22963 located in. `grokdeclarator' sometimes changes the scope, so
22964 we compute it now. */
22965 scope = get_scope_of_declarator (declarator);
22967 /* Perform any lookups in the declared type which were thought to be
22968 dependent, but are not in the scope of the declarator. */
22969 decl_specifiers->type
22970 = maybe_update_decl_type (decl_specifiers->type, scope);
22972 /* If we're allowing GNU extensions, look for an
22973 asm-specification. */
22974 if (cp_parser_allow_gnu_extensions_p (parser))
22976 /* Look for an asm-specification. */
22977 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22978 asm_specification = cp_parser_asm_specification_opt (parser);
22980 else
22981 asm_specification = NULL_TREE;
22983 /* Gather the attributes that were provided with the
22984 decl-specifiers. */
22985 prefix_attributes = decl_specifiers->attributes;
22987 /* Look for attributes. */
22988 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22989 attributes = cp_parser_attributes_opt (parser);
22991 /* Peek at the next token. */
22992 token = cp_lexer_peek_token (parser->lexer);
22994 bool bogus_implicit_tmpl = false;
22996 if (function_declarator_p (declarator))
22998 /* Handle C++17 deduction guides. Note that class-scope
22999 non-template deduction guides are instead handled in
23000 cp_parser_member_declaration. */
23001 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23002 decl_specifiers,
23003 declarator,
23004 &ctor_dtor_or_conv_p);
23006 if (!member_p && !cp_parser_error_occurred (parser))
23007 warn_about_ambiguous_parse (decl_specifiers, declarator);
23009 /* Check to see if the token indicates the start of a
23010 function-definition. */
23011 if (cp_parser_token_starts_function_definition_p (token))
23013 if (!function_definition_allowed_p)
23015 /* If a function-definition should not appear here, issue an
23016 error message. */
23017 cp_parser_error (parser,
23018 "a function-definition is not allowed here");
23019 return error_mark_node;
23022 location_t func_brace_location
23023 = cp_lexer_peek_token (parser->lexer)->location;
23025 /* Neither attributes nor an asm-specification are allowed
23026 on a function-definition. */
23027 if (asm_specification)
23028 error_at (asm_spec_start_token->location,
23029 "an %<asm%> specification is not allowed "
23030 "on a function-definition");
23031 if (attributes)
23032 error_at (attributes_start_token->location,
23033 "attributes are not allowed "
23034 "on a function-definition");
23035 /* This is a function-definition. */
23036 *function_definition_p = true;
23038 /* Parse the function definition. */
23039 if (member_p)
23040 decl = cp_parser_save_member_function_body (parser,
23041 decl_specifiers,
23042 declarator,
23043 prefix_attributes);
23044 else
23045 decl =
23046 (cp_parser_function_definition_from_specifiers_and_declarator
23047 (parser, decl_specifiers, prefix_attributes, declarator));
23049 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23051 /* This is where the prologue starts... */
23052 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23053 = func_brace_location;
23056 return decl;
23059 else if (parser->fully_implicit_function_template_p)
23061 /* A non-template declaration involving a function parameter list
23062 containing an implicit template parameter will be made into a
23063 template. If the resulting declaration is not going to be an
23064 actual function then finish the template scope here to prevent it.
23065 An error message will be issued once we have a decl to talk about.
23067 FIXME probably we should do type deduction rather than create an
23068 implicit template, but the standard currently doesn't allow it. */
23069 bogus_implicit_tmpl = true;
23070 finish_fully_implicit_template (parser, NULL_TREE);
23073 /* [dcl.dcl]
23075 Only in function declarations for constructors, destructors, type
23076 conversions, and deduction guides can the decl-specifier-seq be omitted.
23078 We explicitly postpone this check past the point where we handle
23079 function-definitions because we tolerate function-definitions
23080 that are missing their return types in some modes. */
23081 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23083 cp_parser_error (parser,
23084 "expected constructor, destructor, or type conversion");
23085 return error_mark_node;
23088 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23089 an initializer as well. */
23090 if (token->type == CPP_EQ
23091 || token->type == CPP_OPEN_PAREN
23092 || token->type == CPP_OPEN_BRACE)
23094 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23095 It isn't; it's an expression. (Here '(i)' would have already been
23096 parsed as a declarator.) */
23097 if (token->type == CPP_OPEN_PAREN
23098 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23100 cp_lexer_save_tokens (parser->lexer);
23101 cp_lexer_consume_token (parser->lexer);
23102 cp_parser_skip_to_closing_parenthesis (parser,
23103 /*recovering*/false,
23104 /*or_comma*/false,
23105 /*consume_paren*/true);
23106 /* If this is an initializer, only a ',' or ';' can follow: either
23107 we have another init-declarator, or we're at the end of an
23108 init-declarator-list which can only be followed by a ';'. */
23109 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23110 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23111 cp_lexer_rollback_tokens (parser->lexer);
23112 if (UNLIKELY (!ok))
23113 /* Not an init-declarator. */
23114 return error_mark_node;
23116 is_initialized = SD_INITIALIZED;
23117 initialization_kind = token->type;
23118 declarator->init_loc = token->location;
23119 if (maybe_range_for_decl)
23120 *maybe_range_for_decl = error_mark_node;
23121 tmp_init_loc = token->location;
23122 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23123 *init_loc = tmp_init_loc;
23125 if (token->type == CPP_EQ
23126 && function_declarator_p (declarator))
23128 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23129 if (t2->keyword == RID_DEFAULT)
23130 is_initialized = SD_DEFAULTED;
23131 else if (t2->keyword == RID_DELETE)
23132 is_initialized = SD_DELETED;
23135 else
23137 /* If the init-declarator isn't initialized and isn't followed by a
23138 `,' or `;', it's not a valid init-declarator. */
23139 if (token->type != CPP_COMMA
23140 && token->type != CPP_SEMICOLON)
23142 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23143 range_for_decl_p = true;
23144 else
23146 if (!maybe_range_for_decl)
23147 cp_parser_error (parser, "expected initializer");
23148 return error_mark_node;
23151 is_initialized = SD_UNINITIALIZED;
23152 initialization_kind = CPP_EOF;
23155 /* Because start_decl has side-effects, we should only call it if we
23156 know we're going ahead. By this point, we know that we cannot
23157 possibly be looking at any other construct. */
23158 cp_parser_commit_to_tentative_parse (parser);
23160 /* Enter the newly declared entry in the symbol table. If we're
23161 processing a declaration in a class-specifier, we wait until
23162 after processing the initializer. */
23163 if (!member_p)
23165 if (parser->in_unbraced_linkage_specification_p)
23166 decl_specifiers->storage_class = sc_extern;
23167 decl = start_decl (declarator, decl_specifiers,
23168 range_for_decl_p? SD_INITIALIZED : is_initialized,
23169 attributes, prefix_attributes, &pushed_scope);
23170 cp_finalize_omp_declare_simd (parser, decl);
23171 cp_finalize_oacc_routine (parser, decl, false);
23172 /* Adjust location of decl if declarator->id_loc is more appropriate:
23173 set, and decl wasn't merged with another decl, in which case its
23174 location would be different from input_location, and more accurate. */
23175 if (DECL_P (decl)
23176 && declarator->id_loc != UNKNOWN_LOCATION
23177 && DECL_SOURCE_LOCATION (decl) == input_location)
23178 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23180 else if (scope)
23181 /* Enter the SCOPE. That way unqualified names appearing in the
23182 initializer will be looked up in SCOPE. */
23183 pushed_scope = push_scope (scope);
23185 /* Perform deferred access control checks, now that we know in which
23186 SCOPE the declared entity resides. */
23187 if (!member_p && decl)
23189 tree saved_current_function_decl = NULL_TREE;
23191 /* If the entity being declared is a function, pretend that we
23192 are in its scope. If it is a `friend', it may have access to
23193 things that would not otherwise be accessible. */
23194 if (TREE_CODE (decl) == FUNCTION_DECL)
23196 saved_current_function_decl = current_function_decl;
23197 current_function_decl = decl;
23200 /* Perform access checks for template parameters. */
23201 cp_parser_perform_template_parameter_access_checks (checks);
23203 /* Perform the access control checks for the declarator and the
23204 decl-specifiers. */
23205 perform_deferred_access_checks (tf_warning_or_error);
23207 /* Restore the saved value. */
23208 if (TREE_CODE (decl) == FUNCTION_DECL)
23209 current_function_decl = saved_current_function_decl;
23212 /* Parse the initializer. */
23213 initializer = NULL_TREE;
23214 is_direct_init = false;
23215 is_non_constant_init = true;
23216 if (is_initialized)
23218 if (function_declarator_p (declarator))
23220 if (initialization_kind == CPP_EQ)
23221 initializer = cp_parser_pure_specifier (parser);
23222 else
23224 /* If the declaration was erroneous, we don't really
23225 know what the user intended, so just silently
23226 consume the initializer. */
23227 if (decl != error_mark_node)
23228 error_at (tmp_init_loc, "initializer provided for function");
23229 cp_parser_skip_to_closing_parenthesis (parser,
23230 /*recovering=*/true,
23231 /*or_comma=*/false,
23232 /*consume_paren=*/true);
23235 else
23237 /* We want to record the extra mangling scope for in-class
23238 initializers of class members and initializers of static
23239 data member templates and namespace-scope initializers.
23240 The former involves deferring parsing of the initializer
23241 until end of class as with default arguments. So right
23242 here we only handle the latter two. */
23243 bool has_lambda_scope = false;
23245 if (decl != error_mark_node
23246 && !member_p
23247 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23248 has_lambda_scope = true;
23250 if (has_lambda_scope)
23251 start_lambda_scope (decl);
23252 initializer = cp_parser_initializer (parser,
23253 &is_direct_init,
23254 &is_non_constant_init);
23255 if (has_lambda_scope)
23256 finish_lambda_scope ();
23257 if (initializer == error_mark_node)
23258 cp_parser_skip_to_end_of_statement (parser);
23262 /* The old parser allows attributes to appear after a parenthesized
23263 initializer. Mark Mitchell proposed removing this functionality
23264 on the GCC mailing lists on 2002-08-13. This parser accepts the
23265 attributes -- but ignores them. Made a permerror in GCC 8. */
23266 if (cp_parser_allow_gnu_extensions_p (parser)
23267 && initialization_kind == CPP_OPEN_PAREN
23268 && cp_parser_attributes_opt (parser)
23269 && permerror (input_location,
23270 "attributes after parenthesized initializer ignored"))
23272 static bool hint;
23273 if (flag_permissive && !hint)
23275 hint = true;
23276 inform (input_location,
23277 "this flexibility is deprecated and will be removed");
23281 /* And now complain about a non-function implicit template. */
23282 if (bogus_implicit_tmpl && decl != error_mark_node)
23283 error_at (DECL_SOURCE_LOCATION (decl),
23284 "non-function %qD declared as implicit template", decl);
23286 /* For an in-class declaration, use `grokfield' to create the
23287 declaration. */
23288 if (member_p)
23290 if (pushed_scope)
23292 pop_scope (pushed_scope);
23293 pushed_scope = NULL_TREE;
23295 decl = grokfield (declarator, decl_specifiers,
23296 initializer, !is_non_constant_init,
23297 /*asmspec=*/NULL_TREE,
23298 attr_chainon (attributes, prefix_attributes));
23299 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23300 cp_parser_save_default_args (parser, decl);
23301 cp_finalize_omp_declare_simd (parser, decl);
23302 cp_finalize_oacc_routine (parser, decl, false);
23305 /* Finish processing the declaration. But, skip member
23306 declarations. */
23307 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23309 cp_finish_decl (decl,
23310 initializer, !is_non_constant_init,
23311 asm_specification,
23312 /* If the initializer is in parentheses, then this is
23313 a direct-initialization, which means that an
23314 `explicit' constructor is OK. Otherwise, an
23315 `explicit' constructor cannot be used. */
23316 ((is_direct_init || !is_initialized)
23317 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23319 else if ((cxx_dialect != cxx98) && friend_p
23320 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23321 /* Core issue #226 (C++0x only): A default template-argument
23322 shall not be specified in a friend class template
23323 declaration. */
23324 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23325 /*is_partial=*/false, /*is_friend_decl=*/1);
23327 if (!friend_p && pushed_scope)
23328 pop_scope (pushed_scope);
23330 if (function_declarator_p (declarator)
23331 && parser->fully_implicit_function_template_p)
23333 if (member_p)
23334 decl = finish_fully_implicit_template (parser, decl);
23335 else
23336 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23339 if (auto_result && is_initialized && decl_specifiers->type
23340 && type_uses_auto (decl_specifiers->type))
23341 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23343 return decl;
23346 /* Parse a declarator.
23348 declarator:
23349 direct-declarator
23350 ptr-operator declarator
23352 abstract-declarator:
23353 ptr-operator abstract-declarator [opt]
23354 direct-abstract-declarator
23356 GNU Extensions:
23358 declarator:
23359 attributes [opt] direct-declarator
23360 attributes [opt] ptr-operator declarator
23362 abstract-declarator:
23363 attributes [opt] ptr-operator abstract-declarator [opt]
23364 attributes [opt] direct-abstract-declarator
23366 The parser flags FLAGS is used to control type-specifier parsing.
23368 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23369 detect constructors, destructors, deduction guides, or conversion operators.
23370 It is set to -1 if the declarator is a name, and +1 if it is a
23371 function. Otherwise it is set to zero. Usually you just want to
23372 test for >0, but internally the negative value is used.
23374 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23375 a decl-specifier-seq unless it declares a constructor, destructor,
23376 or conversion. It might seem that we could check this condition in
23377 semantic analysis, rather than parsing, but that makes it difficult
23378 to handle something like `f()'. We want to notice that there are
23379 no decl-specifiers, and therefore realize that this is an
23380 expression, not a declaration.)
23382 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23383 the declarator is a direct-declarator of the form "(...)".
23385 MEMBER_P is true iff this declarator is a member-declarator.
23387 FRIEND_P is true iff this declarator is a friend.
23389 STATIC_P is true iff the keyword static was seen. */
23391 static cp_declarator *
23392 cp_parser_declarator (cp_parser* parser,
23393 cp_parser_declarator_kind dcl_kind,
23394 cp_parser_flags flags,
23395 int* ctor_dtor_or_conv_p,
23396 bool* parenthesized_p,
23397 bool member_p, bool friend_p, bool static_p)
23399 cp_declarator *declarator;
23400 enum tree_code code;
23401 cp_cv_quals cv_quals;
23402 tree class_type;
23403 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23405 /* Assume this is not a constructor, destructor, or type-conversion
23406 operator. */
23407 if (ctor_dtor_or_conv_p)
23408 *ctor_dtor_or_conv_p = 0;
23410 if (cp_parser_allow_gnu_extensions_p (parser))
23411 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23413 /* Check for the ptr-operator production. */
23414 cp_parser_parse_tentatively (parser);
23415 /* Parse the ptr-operator. */
23416 code = cp_parser_ptr_operator (parser,
23417 &class_type,
23418 &cv_quals,
23419 &std_attributes);
23421 /* If that worked, then we have a ptr-operator. */
23422 if (cp_parser_parse_definitely (parser))
23424 /* If a ptr-operator was found, then this declarator was not
23425 parenthesized. */
23426 if (parenthesized_p)
23427 *parenthesized_p = false;
23428 /* The dependent declarator is optional if we are parsing an
23429 abstract-declarator. */
23430 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23431 cp_parser_parse_tentatively (parser);
23433 /* Parse the dependent declarator. */
23434 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23435 /*ctor_dtor_or_conv_p=*/NULL,
23436 /*parenthesized_p=*/NULL,
23437 member_p, friend_p, static_p);
23439 /* If we are parsing an abstract-declarator, we must handle the
23440 case where the dependent declarator is absent. */
23441 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23442 && !cp_parser_parse_definitely (parser))
23443 declarator = NULL;
23445 declarator = cp_parser_make_indirect_declarator
23446 (code, class_type, cv_quals, declarator, std_attributes);
23448 /* Everything else is a direct-declarator. */
23449 else
23451 if (parenthesized_p)
23452 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23453 CPP_OPEN_PAREN);
23454 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23455 flags, ctor_dtor_or_conv_p,
23456 member_p, friend_p, static_p);
23459 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23460 declarator->attributes = gnu_attributes;
23461 return declarator;
23464 /* Parse a direct-declarator or direct-abstract-declarator.
23466 direct-declarator:
23467 declarator-id
23468 direct-declarator ( parameter-declaration-clause )
23469 cv-qualifier-seq [opt]
23470 ref-qualifier [opt]
23471 exception-specification [opt]
23472 direct-declarator [ constant-expression [opt] ]
23473 ( declarator )
23475 direct-abstract-declarator:
23476 direct-abstract-declarator [opt]
23477 ( parameter-declaration-clause )
23478 cv-qualifier-seq [opt]
23479 ref-qualifier [opt]
23480 exception-specification [opt]
23481 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23482 ( abstract-declarator )
23484 Returns a representation of the declarator. DCL_KIND is
23485 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23486 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23487 we are parsing a direct-declarator. It is
23488 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23489 of ambiguity we prefer an abstract declarator, as per
23490 [dcl.ambig.res].
23491 The parser flags FLAGS is used to control type-specifier parsing.
23492 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23493 as for cp_parser_declarator. */
23495 static cp_declarator *
23496 cp_parser_direct_declarator (cp_parser* parser,
23497 cp_parser_declarator_kind dcl_kind,
23498 cp_parser_flags flags,
23499 int* ctor_dtor_or_conv_p,
23500 bool member_p, bool friend_p, bool static_p)
23502 cp_token *token;
23503 cp_declarator *declarator = NULL;
23504 tree scope = NULL_TREE;
23505 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23506 bool saved_in_declarator_p = parser->in_declarator_p;
23507 bool first = true;
23508 tree pushed_scope = NULL_TREE;
23509 cp_token *open_paren = NULL, *close_paren = NULL;
23511 while (true)
23513 /* Peek at the next token. */
23514 token = cp_lexer_peek_token (parser->lexer);
23515 if (token->type == CPP_OPEN_PAREN)
23517 /* This is either a parameter-declaration-clause, or a
23518 parenthesized declarator. When we know we are parsing a
23519 named declarator, it must be a parenthesized declarator
23520 if FIRST is true. For instance, `(int)' is a
23521 parameter-declaration-clause, with an omitted
23522 direct-abstract-declarator. But `((*))', is a
23523 parenthesized abstract declarator. Finally, when T is a
23524 template parameter `(T)' is a
23525 parameter-declaration-clause, and not a parenthesized
23526 named declarator.
23528 We first try and parse a parameter-declaration-clause,
23529 and then try a nested declarator (if FIRST is true).
23531 It is not an error for it not to be a
23532 parameter-declaration-clause, even when FIRST is
23533 false. Consider,
23535 int i (int);
23536 int i (3);
23538 The first is the declaration of a function while the
23539 second is the definition of a variable, including its
23540 initializer.
23542 Having seen only the parenthesis, we cannot know which of
23543 these two alternatives should be selected. Even more
23544 complex are examples like:
23546 int i (int (a));
23547 int i (int (3));
23549 The former is a function-declaration; the latter is a
23550 variable initialization.
23552 Thus again, we try a parameter-declaration-clause, and if
23553 that fails, we back out and return. */
23555 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23557 tree params;
23558 bool is_declarator = false;
23560 open_paren = NULL;
23562 /* In a member-declarator, the only valid interpretation
23563 of a parenthesis is the start of a
23564 parameter-declaration-clause. (It is invalid to
23565 initialize a static data member with a parenthesized
23566 initializer; only the "=" form of initialization is
23567 permitted.) */
23568 if (!member_p)
23569 cp_parser_parse_tentatively (parser);
23571 /* Consume the `('. */
23572 const location_t parens_start = token->location;
23573 matching_parens parens;
23574 parens.consume_open (parser);
23575 if (first)
23577 /* If this is going to be an abstract declarator, we're
23578 in a declarator and we can't have default args. */
23579 parser->default_arg_ok_p = false;
23580 parser->in_declarator_p = true;
23583 begin_scope (sk_function_parms, NULL_TREE);
23585 /* Parse the parameter-declaration-clause. */
23586 params
23587 = cp_parser_parameter_declaration_clause (parser, flags);
23588 const location_t parens_end
23589 = cp_lexer_peek_token (parser->lexer)->location;
23591 /* Consume the `)'. */
23592 parens.require_close (parser);
23594 /* If all went well, parse the cv-qualifier-seq,
23595 ref-qualifier and the exception-specification. */
23596 if (member_p || cp_parser_parse_definitely (parser))
23598 cp_cv_quals cv_quals;
23599 cp_virt_specifiers virt_specifiers;
23600 cp_ref_qualifier ref_qual;
23601 tree exception_specification;
23602 tree late_return;
23603 tree attrs;
23604 bool memfn = (member_p || (pushed_scope
23605 && CLASS_TYPE_P (pushed_scope)));
23606 unsigned char local_variables_forbidden_p
23607 = parser->local_variables_forbidden_p;
23608 /* 'this' is not allowed in static member functions. */
23609 if (static_p || friend_p)
23610 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23612 is_declarator = true;
23614 if (ctor_dtor_or_conv_p)
23615 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23616 first = false;
23618 /* Parse the cv-qualifier-seq. */
23619 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23620 /* Parse the ref-qualifier. */
23621 ref_qual = cp_parser_ref_qualifier_opt (parser);
23622 /* Parse the tx-qualifier. */
23623 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23625 tree save_ccp = current_class_ptr;
23626 tree save_ccr = current_class_ref;
23627 if (memfn && !friend_p && !static_p)
23628 /* DR 1207: 'this' is in scope after the cv-quals. */
23629 inject_this_parameter (current_class_type, cv_quals);
23631 /* If it turned out that this is e.g. a pointer to a
23632 function, we don't want to delay noexcept parsing. */
23633 if (declarator == NULL || declarator->kind != cdk_id)
23634 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23636 /* Parse the exception-specification. */
23637 exception_specification
23638 = cp_parser_exception_specification_opt (parser,
23639 flags);
23641 attrs = cp_parser_std_attribute_spec_seq (parser);
23643 cp_omp_declare_simd_data odsd;
23644 if ((flag_openmp || flag_openmp_simd)
23645 && declarator
23646 && declarator->std_attributes
23647 && declarator->kind == cdk_id)
23649 tree *pa = &declarator->std_attributes;
23650 cp_parser_handle_directive_omp_attributes (parser, pa,
23651 &odsd, false);
23654 /* In here, we handle cases where attribute is used after
23655 the function declaration. For example:
23656 void func (int x) __attribute__((vector(..))); */
23657 tree gnu_attrs = NULL_TREE;
23658 tree requires_clause = NULL_TREE;
23659 late_return
23660 = cp_parser_late_return_type_opt (parser, declarator,
23661 requires_clause);
23663 cp_finalize_omp_declare_simd (parser, &odsd);
23665 /* Parse the virt-specifier-seq. */
23666 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23668 location_t parens_loc = make_location (parens_start,
23669 parens_start,
23670 parens_end);
23671 /* Create the function-declarator. */
23672 declarator = make_call_declarator (declarator,
23673 params,
23674 cv_quals,
23675 virt_specifiers,
23676 ref_qual,
23677 tx_qual,
23678 exception_specification,
23679 late_return,
23680 requires_clause,
23681 attrs,
23682 parens_loc);
23683 declarator->attributes = gnu_attrs;
23684 /* Any subsequent parameter lists are to do with
23685 return type, so are not those of the declared
23686 function. */
23687 parser->default_arg_ok_p = false;
23689 current_class_ptr = save_ccp;
23690 current_class_ref = save_ccr;
23692 /* Restore the state of local_variables_forbidden_p. */
23693 parser->local_variables_forbidden_p
23694 = local_variables_forbidden_p;
23697 /* Remove the function parms from scope. */
23698 pop_bindings_and_leave_scope ();
23700 if (is_declarator)
23701 /* Repeat the main loop. */
23702 continue;
23705 /* If this is the first, we can try a parenthesized
23706 declarator. */
23707 if (first)
23709 bool saved_in_type_id_in_expr_p;
23711 parser->default_arg_ok_p = saved_default_arg_ok_p;
23712 parser->in_declarator_p = saved_in_declarator_p;
23714 open_paren = token;
23715 /* Consume the `('. */
23716 matching_parens parens;
23717 parens.consume_open (parser);
23718 /* Parse the nested declarator. */
23719 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23720 parser->in_type_id_in_expr_p = true;
23721 declarator
23722 = cp_parser_declarator (parser, dcl_kind, flags,
23723 ctor_dtor_or_conv_p,
23724 /*parenthesized_p=*/NULL,
23725 member_p, friend_p,
23726 /*static_p=*/false);
23727 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23728 first = false;
23729 /* Expect a `)'. */
23730 close_paren = cp_lexer_peek_token (parser->lexer);
23731 if (!parens.require_close (parser))
23732 declarator = cp_error_declarator;
23733 if (declarator == cp_error_declarator)
23734 break;
23736 goto handle_declarator;
23738 /* Otherwise, we must be done. */
23739 else
23740 break;
23742 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23743 && token->type == CPP_OPEN_SQUARE
23744 && !cp_next_tokens_can_be_attribute_p (parser))
23746 /* Parse an array-declarator. */
23747 tree bounds, attrs;
23749 if (ctor_dtor_or_conv_p)
23750 *ctor_dtor_or_conv_p = 0;
23752 open_paren = NULL;
23753 first = false;
23754 parser->default_arg_ok_p = false;
23755 parser->in_declarator_p = true;
23756 /* Consume the `['. */
23757 cp_lexer_consume_token (parser->lexer);
23758 /* Peek at the next token. */
23759 token = cp_lexer_peek_token (parser->lexer);
23760 /* If the next token is `]', then there is no
23761 constant-expression. */
23762 if (token->type != CPP_CLOSE_SQUARE)
23764 bool non_constant_p;
23765 bounds
23766 = cp_parser_constant_expression (parser,
23767 /*allow_non_constant=*/true,
23768 &non_constant_p);
23769 if (!non_constant_p)
23770 /* OK */;
23771 else if (error_operand_p (bounds))
23772 /* Already gave an error. */;
23773 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23774 /* Let compute_array_index_type diagnose this. */;
23775 else if (!parser->in_function_body
23776 || parsing_function_declarator ())
23778 /* Normally, the array bound must be an integral constant
23779 expression. However, as an extension, we allow VLAs
23780 in function scopes as long as they aren't part of a
23781 parameter declaration. */
23782 cp_parser_error (parser,
23783 "array bound is not an integer constant");
23784 bounds = error_mark_node;
23786 else if (processing_template_decl
23787 && !type_dependent_expression_p (bounds))
23789 /* Remember this wasn't a constant-expression. */
23790 bounds = build_nop (TREE_TYPE (bounds), bounds);
23791 TREE_SIDE_EFFECTS (bounds) = 1;
23794 else
23795 bounds = NULL_TREE;
23796 /* Look for the closing `]'. */
23797 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23799 declarator = cp_error_declarator;
23800 break;
23803 attrs = cp_parser_std_attribute_spec_seq (parser);
23804 declarator = make_array_declarator (declarator, bounds);
23805 declarator->std_attributes = attrs;
23807 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23810 tree qualifying_scope;
23811 tree unqualified_name;
23812 tree attrs;
23813 special_function_kind sfk;
23814 bool abstract_ok;
23815 bool pack_expansion_p = false;
23816 cp_token *declarator_id_start_token;
23818 /* Parse a declarator-id */
23819 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23820 if (abstract_ok)
23822 cp_parser_parse_tentatively (parser);
23824 /* If we see an ellipsis, we should be looking at a
23825 parameter pack. */
23826 if (token->type == CPP_ELLIPSIS)
23828 /* Consume the `...' */
23829 cp_lexer_consume_token (parser->lexer);
23831 pack_expansion_p = true;
23835 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23836 unqualified_name
23837 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23838 qualifying_scope = parser->scope;
23839 if (abstract_ok)
23841 bool okay = false;
23843 if (!unqualified_name && pack_expansion_p)
23845 /* Check whether an error occurred. */
23846 okay = !cp_parser_error_occurred (parser);
23848 /* We already consumed the ellipsis to mark a
23849 parameter pack, but we have no way to report it,
23850 so abort the tentative parse. We will be exiting
23851 immediately anyway. */
23852 cp_parser_abort_tentative_parse (parser);
23854 else
23855 okay = cp_parser_parse_definitely (parser);
23857 if (!okay)
23858 unqualified_name = error_mark_node;
23859 else if (unqualified_name
23860 && (qualifying_scope
23861 || (!identifier_p (unqualified_name))))
23863 cp_parser_error (parser, "expected unqualified-id");
23864 unqualified_name = error_mark_node;
23868 if (!unqualified_name)
23869 return NULL;
23870 if (unqualified_name == error_mark_node)
23872 declarator = cp_error_declarator;
23873 pack_expansion_p = false;
23874 declarator->parameter_pack_p = false;
23875 break;
23878 attrs = cp_parser_std_attribute_spec_seq (parser);
23880 if (qualifying_scope && at_namespace_scope_p ()
23881 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23883 /* In the declaration of a member of a template class
23884 outside of the class itself, the SCOPE will sometimes
23885 be a TYPENAME_TYPE. For example, given:
23887 template <typename T>
23888 int S<T>::R::i = 3;
23890 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23891 this context, we must resolve S<T>::R to an ordinary
23892 type, rather than a typename type.
23894 The reason we normally avoid resolving TYPENAME_TYPEs
23895 is that a specialization of `S' might render
23896 `S<T>::R' not a type. However, if `S' is
23897 specialized, then this `i' will not be used, so there
23898 is no harm in resolving the types here. */
23899 tree type;
23901 /* Resolve the TYPENAME_TYPE. */
23902 type = resolve_typename_type (qualifying_scope,
23903 /*only_current_p=*/false);
23904 /* If that failed, the declarator is invalid. */
23905 if (TREE_CODE (type) == TYPENAME_TYPE)
23907 if (typedef_variant_p (type))
23908 error_at (declarator_id_start_token->location,
23909 "cannot define member of dependent typedef "
23910 "%qT", type);
23911 else
23912 error_at (declarator_id_start_token->location,
23913 "%<%T::%E%> is not a type",
23914 TYPE_CONTEXT (qualifying_scope),
23915 TYPE_IDENTIFIER (qualifying_scope));
23917 qualifying_scope = type;
23920 sfk = sfk_none;
23922 if (unqualified_name)
23924 tree class_type;
23926 if (qualifying_scope
23927 && CLASS_TYPE_P (qualifying_scope))
23928 class_type = qualifying_scope;
23929 else
23930 class_type = current_class_type;
23932 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23934 tree name_type = TREE_TYPE (unqualified_name);
23936 if (!class_type || !same_type_p (name_type, class_type))
23938 /* We do not attempt to print the declarator
23939 here because we do not have enough
23940 information about its original syntactic
23941 form. */
23942 cp_parser_error (parser, "invalid declarator");
23943 declarator = cp_error_declarator;
23944 break;
23946 else if (qualifying_scope
23947 && CLASSTYPE_USE_TEMPLATE (name_type))
23949 error_at (declarator_id_start_token->location,
23950 "invalid use of constructor as a template");
23951 inform (declarator_id_start_token->location,
23952 "use %<%T::%D%> instead of %<%T::%D%> to "
23953 "name the constructor in a qualified name",
23954 class_type,
23955 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23956 class_type, name_type);
23957 declarator = cp_error_declarator;
23958 break;
23960 unqualified_name = constructor_name (class_type);
23963 if (class_type)
23965 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23966 sfk = sfk_destructor;
23967 else if (identifier_p (unqualified_name)
23968 && IDENTIFIER_CONV_OP_P (unqualified_name))
23969 sfk = sfk_conversion;
23970 else if (/* There's no way to declare a constructor
23971 for an unnamed type, even if the type
23972 got a name for linkage purposes. */
23973 !TYPE_WAS_UNNAMED (class_type)
23974 /* Handle correctly (c++/19200):
23976 struct S {
23977 struct T{};
23978 friend void S(T);
23981 and also:
23983 namespace N {
23984 void S();
23987 struct S {
23988 friend void N::S();
23989 }; */
23990 && (!friend_p || class_type == qualifying_scope)
23991 && constructor_name_p (unqualified_name,
23992 class_type))
23993 sfk = sfk_constructor;
23994 else if (is_overloaded_fn (unqualified_name)
23995 && DECL_CONSTRUCTOR_P (get_first_fn
23996 (unqualified_name)))
23997 sfk = sfk_constructor;
23999 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24000 *ctor_dtor_or_conv_p = -1;
24003 declarator = make_id_declarator (qualifying_scope,
24004 unqualified_name,
24005 sfk, token->location);
24006 declarator->std_attributes = attrs;
24007 declarator->parameter_pack_p = pack_expansion_p;
24009 if (pack_expansion_p)
24010 maybe_warn_variadic_templates ();
24012 /* We're looking for this case in [temp.res]:
24013 A qualified-id is assumed to name a type if [...]
24014 - it is a decl-specifier of the decl-specifier-seq of a
24015 parameter-declaration in a declarator of a function or
24016 function template declaration, ... */
24017 if (cxx_dialect >= cxx20
24018 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24019 && declarator->kind == cdk_id
24020 && !at_class_scope_p ()
24021 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24023 /* ...whose declarator-id is qualified. If it isn't, never
24024 assume the parameters to refer to types. */
24025 if (qualifying_scope == NULL_TREE)
24026 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24027 else
24029 /* Now we have something like
24030 template <typename T> int C::x(S::p);
24031 which can be a function template declaration or a
24032 variable template definition. If name lookup for
24033 the declarator-id C::x finds one or more function
24034 templates, assume S::p to name a type. Otherwise,
24035 don't. */
24036 tree decl
24037 = cp_parser_lookup_name (parser, unqualified_name,
24038 none_type,
24039 /*is_template=*/false,
24040 /*is_namespace=*/false,
24041 /*check_dependency=*/false,
24042 /*ambiguous_decls=*/NULL,
24043 token->location);
24045 if (!is_overloaded_fn (decl)
24046 /* Allow
24047 template<typename T>
24048 A<T>::A(T::type) { } */
24049 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24050 && constructor_name_p (unqualified_name,
24051 qualifying_scope)))
24052 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24057 handle_declarator:;
24058 scope = get_scope_of_declarator (declarator);
24059 if (scope)
24061 /* Any names that appear after the declarator-id for a
24062 member are looked up in the containing scope. */
24063 if (at_function_scope_p ())
24065 /* But declarations with qualified-ids can't appear in a
24066 function. */
24067 cp_parser_error (parser, "qualified-id in declaration");
24068 declarator = cp_error_declarator;
24069 break;
24071 pushed_scope = push_scope (scope);
24073 parser->in_declarator_p = true;
24074 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24075 || (declarator && declarator->kind == cdk_id))
24076 /* Default args are only allowed on function
24077 declarations. */
24078 parser->default_arg_ok_p = saved_default_arg_ok_p;
24079 else
24080 parser->default_arg_ok_p = false;
24082 first = false;
24084 /* We're done. */
24085 else
24086 break;
24089 /* For an abstract declarator, we might wind up with nothing at this
24090 point. That's an error; the declarator is not optional. */
24091 if (!declarator)
24092 cp_parser_error (parser, "expected declarator");
24093 else if (open_paren)
24095 /* Record overly parenthesized declarator so we can give a
24096 diagnostic about confusing decl/expr disambiguation. */
24097 if (declarator->kind == cdk_array)
24099 /* If the open and close parens are on different lines, this
24100 is probably a formatting thing, so ignore. */
24101 expanded_location open = expand_location (open_paren->location);
24102 expanded_location close = expand_location (close_paren->location);
24103 if (open.line != close.line || open.file != close.file)
24104 open_paren = NULL;
24106 if (open_paren)
24107 declarator->parenthesized = make_location (open_paren->location,
24108 open_paren->location,
24109 close_paren->location);
24112 /* If we entered a scope, we must exit it now. */
24113 if (pushed_scope)
24114 pop_scope (pushed_scope);
24116 parser->default_arg_ok_p = saved_default_arg_ok_p;
24117 parser->in_declarator_p = saved_in_declarator_p;
24119 return declarator;
24122 /* Parse a ptr-operator.
24124 ptr-operator:
24125 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24126 * cv-qualifier-seq [opt]
24128 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24129 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24131 GNU Extension:
24133 ptr-operator:
24134 & cv-qualifier-seq [opt]
24136 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24137 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24138 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24139 filled in with the TYPE containing the member. *CV_QUALS is
24140 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24141 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24142 Note that the tree codes returned by this function have nothing
24143 to do with the types of trees that will be eventually be created
24144 to represent the pointer or reference type being parsed. They are
24145 just constants with suggestive names. */
24146 static enum tree_code
24147 cp_parser_ptr_operator (cp_parser* parser,
24148 tree* type,
24149 cp_cv_quals *cv_quals,
24150 tree *attributes)
24152 enum tree_code code = ERROR_MARK;
24153 cp_token *token;
24154 tree attrs = NULL_TREE;
24156 /* Assume that it's not a pointer-to-member. */
24157 *type = NULL_TREE;
24158 /* And that there are no cv-qualifiers. */
24159 *cv_quals = TYPE_UNQUALIFIED;
24161 /* Peek at the next token. */
24162 token = cp_lexer_peek_token (parser->lexer);
24164 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24165 if (token->type == CPP_MULT)
24166 code = INDIRECT_REF;
24167 else if (token->type == CPP_AND)
24168 code = ADDR_EXPR;
24169 else if ((cxx_dialect != cxx98) &&
24170 token->type == CPP_AND_AND) /* C++0x only */
24171 code = NON_LVALUE_EXPR;
24173 if (code != ERROR_MARK)
24175 /* Consume the `*', `&' or `&&'. */
24176 cp_lexer_consume_token (parser->lexer);
24178 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24179 `&', if we are allowing GNU extensions. (The only qualifier
24180 that can legally appear after `&' is `restrict', but that is
24181 enforced during semantic analysis. */
24182 if (code == INDIRECT_REF
24183 || cp_parser_allow_gnu_extensions_p (parser))
24184 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24186 attrs = cp_parser_std_attribute_spec_seq (parser);
24187 if (attributes != NULL)
24188 *attributes = attrs;
24190 else
24192 /* Try the pointer-to-member case. */
24193 cp_parser_parse_tentatively (parser);
24194 /* Look for the optional `::' operator. */
24195 cp_parser_global_scope_opt (parser,
24196 /*current_scope_valid_p=*/false);
24197 /* Look for the nested-name specifier. */
24198 token = cp_lexer_peek_token (parser->lexer);
24199 cp_parser_nested_name_specifier (parser,
24200 /*typename_keyword_p=*/false,
24201 /*check_dependency_p=*/true,
24202 /*type_p=*/false,
24203 /*is_declaration=*/false);
24204 /* If we found it, and the next token is a `*', then we are
24205 indeed looking at a pointer-to-member operator. */
24206 if (!cp_parser_error_occurred (parser)
24207 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24209 /* Indicate that the `*' operator was used. */
24210 code = INDIRECT_REF;
24212 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24213 error_at (token->location, "%qD is a namespace", parser->scope);
24214 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24215 error_at (token->location, "cannot form pointer to member of "
24216 "non-class %q#T", parser->scope);
24217 else
24219 /* The type of which the member is a member is given by the
24220 current SCOPE. */
24221 *type = parser->scope;
24222 /* The next name will not be qualified. */
24223 parser->scope = NULL_TREE;
24224 parser->qualifying_scope = NULL_TREE;
24225 parser->object_scope = NULL_TREE;
24226 /* Look for optional c++11 attributes. */
24227 attrs = cp_parser_std_attribute_spec_seq (parser);
24228 if (attributes != NULL)
24229 *attributes = attrs;
24230 /* Look for the optional cv-qualifier-seq. */
24231 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24234 /* If that didn't work we don't have a ptr-operator. */
24235 if (!cp_parser_parse_definitely (parser))
24236 cp_parser_error (parser, "expected ptr-operator");
24239 return code;
24242 /* Parse an (optional) cv-qualifier-seq.
24244 cv-qualifier-seq:
24245 cv-qualifier cv-qualifier-seq [opt]
24247 cv-qualifier:
24248 const
24249 volatile
24251 GNU Extension:
24253 cv-qualifier:
24254 __restrict__
24256 Returns a bitmask representing the cv-qualifiers. */
24258 static cp_cv_quals
24259 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24261 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24263 while (true)
24265 cp_token *token;
24266 cp_cv_quals cv_qualifier;
24268 /* Peek at the next token. */
24269 token = cp_lexer_peek_token (parser->lexer);
24270 /* See if it's a cv-qualifier. */
24271 switch (token->keyword)
24273 case RID_CONST:
24274 cv_qualifier = TYPE_QUAL_CONST;
24275 break;
24277 case RID_VOLATILE:
24278 cv_qualifier = TYPE_QUAL_VOLATILE;
24279 break;
24281 case RID_RESTRICT:
24282 cv_qualifier = TYPE_QUAL_RESTRICT;
24283 break;
24285 default:
24286 cv_qualifier = TYPE_UNQUALIFIED;
24287 break;
24290 if (!cv_qualifier)
24291 break;
24293 if (cv_quals & cv_qualifier)
24295 gcc_rich_location richloc (token->location);
24296 richloc.add_fixit_remove ();
24297 error_at (&richloc, "duplicate cv-qualifier");
24298 cp_lexer_purge_token (parser->lexer);
24300 else
24302 cp_lexer_consume_token (parser->lexer);
24303 cv_quals |= cv_qualifier;
24307 return cv_quals;
24310 /* Parse an (optional) ref-qualifier
24312 ref-qualifier:
24316 Returns cp_ref_qualifier representing ref-qualifier. */
24318 static cp_ref_qualifier
24319 cp_parser_ref_qualifier_opt (cp_parser* parser)
24321 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24323 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24324 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24325 return ref_qual;
24327 while (true)
24329 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24330 cp_token *token = cp_lexer_peek_token (parser->lexer);
24332 switch (token->type)
24334 case CPP_AND:
24335 curr_ref_qual = REF_QUAL_LVALUE;
24336 break;
24338 case CPP_AND_AND:
24339 curr_ref_qual = REF_QUAL_RVALUE;
24340 break;
24342 default:
24343 curr_ref_qual = REF_QUAL_NONE;
24344 break;
24347 if (!curr_ref_qual)
24348 break;
24349 else if (ref_qual)
24351 error_at (token->location, "multiple ref-qualifiers");
24352 cp_lexer_purge_token (parser->lexer);
24354 else
24356 ref_qual = curr_ref_qual;
24357 cp_lexer_consume_token (parser->lexer);
24361 return ref_qual;
24364 /* Parse an optional tx-qualifier.
24366 tx-qualifier:
24367 transaction_safe
24368 transaction_safe_dynamic */
24370 static tree
24371 cp_parser_tx_qualifier_opt (cp_parser *parser)
24373 cp_token *token = cp_lexer_peek_token (parser->lexer);
24374 if (token->type == CPP_NAME)
24376 tree name = token->u.value;
24377 const char *p = IDENTIFIER_POINTER (name);
24378 const int len = strlen ("transaction_safe");
24379 if (startswith (p, "transaction_safe"))
24381 p += len;
24382 if (*p == '\0'
24383 || !strcmp (p, "_dynamic"))
24385 cp_lexer_consume_token (parser->lexer);
24386 if (!flag_tm)
24388 error ("%qE requires %<-fgnu-tm%>", name);
24389 return NULL_TREE;
24391 else
24392 return name;
24396 return NULL_TREE;
24399 /* Parse an (optional) virt-specifier-seq.
24401 virt-specifier-seq:
24402 virt-specifier virt-specifier-seq [opt]
24404 virt-specifier:
24405 override
24406 final
24408 Returns a bitmask representing the virt-specifiers. */
24410 static cp_virt_specifiers
24411 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24413 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24415 while (true)
24417 cp_token *token;
24418 cp_virt_specifiers virt_specifier;
24420 /* Peek at the next token. */
24421 token = cp_lexer_peek_token (parser->lexer);
24422 /* See if it's a virt-specifier-qualifier. */
24423 if (token->type != CPP_NAME)
24424 break;
24425 if (id_equal (token->u.value, "override"))
24427 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24428 virt_specifier = VIRT_SPEC_OVERRIDE;
24430 else if (id_equal (token->u.value, "final"))
24432 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24433 virt_specifier = VIRT_SPEC_FINAL;
24435 else if (id_equal (token->u.value, "__final"))
24437 virt_specifier = VIRT_SPEC_FINAL;
24439 else
24440 break;
24442 if (virt_specifiers & virt_specifier)
24444 gcc_rich_location richloc (token->location);
24445 richloc.add_fixit_remove ();
24446 error_at (&richloc, "duplicate virt-specifier");
24447 cp_lexer_purge_token (parser->lexer);
24449 else
24451 cp_lexer_consume_token (parser->lexer);
24452 virt_specifiers |= virt_specifier;
24455 return virt_specifiers;
24458 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24459 is in scope even though it isn't real. */
24461 void
24462 inject_this_parameter (tree ctype, cp_cv_quals quals)
24464 tree this_parm;
24466 if (current_class_ptr)
24468 /* We don't clear this between NSDMIs. Is it already what we want? */
24469 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24470 if (DECL_P (current_class_ptr)
24471 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24472 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24473 && cp_type_quals (type) == quals)
24474 return;
24477 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24478 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24479 current_class_ptr = NULL_TREE;
24480 current_class_ref
24481 = cp_build_fold_indirect_ref (this_parm);
24482 current_class_ptr = this_parm;
24485 /* Return true iff our current scope is a non-static data member
24486 initializer. */
24488 bool
24489 parsing_nsdmi (void)
24491 /* We recognize NSDMI context by the context-less 'this' pointer set up
24492 by the function above. */
24493 if (current_class_ptr
24494 && TREE_CODE (current_class_ptr) == PARM_DECL
24495 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24496 return true;
24497 return false;
24500 /* True if we're parsing a function declarator. */
24502 bool
24503 parsing_function_declarator ()
24505 /* this_entity is NULL for a function parameter scope while parsing the
24506 declarator; it is set when parsing the body of the function. */
24507 return (current_binding_level->kind == sk_function_parms
24508 && !current_binding_level->this_entity);
24511 /* Parse a late-specified return type, if any. This is not a separate
24512 non-terminal, but part of a function declarator, which looks like
24514 -> trailing-type-specifier-seq abstract-declarator(opt)
24516 Returns the type indicated by the type-id.
24518 In addition to this, parse any queued up #pragma omp declare simd
24519 clauses, and #pragma acc routine clauses.
24521 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24522 function. */
24524 static tree
24525 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24526 tree& requires_clause)
24528 cp_token *token;
24529 tree type = NULL_TREE;
24530 bool declare_simd_p = (parser->omp_declare_simd
24531 && declarator
24532 && declarator->kind == cdk_id);
24534 bool oacc_routine_p = (parser->oacc_routine
24535 && declarator
24536 && declarator->kind == cdk_id);
24538 /* Peek at the next token. */
24539 token = cp_lexer_peek_token (parser->lexer);
24540 /* A late-specified return type is indicated by an initial '->'. */
24541 if (token->type != CPP_DEREF
24542 && token->keyword != RID_REQUIRES
24543 && !(token->type == CPP_NAME
24544 && token->u.value == ridpointers[RID_REQUIRES])
24545 && !(declare_simd_p || oacc_routine_p))
24546 return NULL_TREE;
24548 if (token->type == CPP_DEREF)
24550 /* Consume the ->. */
24551 cp_lexer_consume_token (parser->lexer);
24553 type = cp_parser_trailing_type_id (parser);
24556 /* Function declarations may be followed by a trailing
24557 requires-clause. */
24558 requires_clause = cp_parser_requires_clause_opt (parser, false);
24560 if (declare_simd_p)
24561 declarator->attributes
24562 = cp_parser_late_parsing_omp_declare_simd (parser,
24563 declarator->attributes);
24564 if (oacc_routine_p)
24565 declarator->attributes
24566 = cp_parser_late_parsing_oacc_routine (parser,
24567 declarator->attributes);
24569 return type;
24572 /* Parse a declarator-id.
24574 declarator-id:
24575 id-expression
24576 :: [opt] nested-name-specifier [opt] type-name
24578 In the `id-expression' case, the value returned is as for
24579 cp_parser_id_expression if the id-expression was an unqualified-id.
24580 If the id-expression was a qualified-id, then a SCOPE_REF is
24581 returned. The first operand is the scope (either a NAMESPACE_DECL
24582 or TREE_TYPE), but the second is still just a representation of an
24583 unqualified-id. */
24585 static tree
24586 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24588 tree id;
24589 /* The expression must be an id-expression. Assume that qualified
24590 names are the names of types so that:
24592 template <class T>
24593 int S<T>::R::i = 3;
24595 will work; we must treat `S<T>::R' as the name of a type.
24596 Similarly, assume that qualified names are templates, where
24597 required, so that:
24599 template <class T>
24600 int S<T>::R<T>::i = 3;
24602 will work, too. */
24603 id = cp_parser_id_expression (parser,
24604 /*template_keyword_p=*/false,
24605 /*check_dependency_p=*/false,
24606 /*template_p=*/NULL,
24607 /*declarator_p=*/true,
24608 optional_p);
24609 if (id && BASELINK_P (id))
24610 id = BASELINK_FUNCTIONS (id);
24611 return id;
24614 /* Parse a type-id.
24616 type-id:
24617 type-specifier-seq abstract-declarator [opt]
24619 The parser flags FLAGS is used to control type-specifier parsing.
24621 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24623 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24624 i.e. we've just seen "->".
24626 Returns the TYPE specified. */
24628 static tree
24629 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24630 bool is_template_arg, bool is_trailing_return,
24631 location_t *type_location)
24633 cp_decl_specifier_seq type_specifier_seq;
24634 cp_declarator *abstract_declarator;
24636 /* Parse the type-specifier-seq. */
24637 cp_parser_type_specifier_seq (parser, flags,
24638 /*is_declaration=*/false,
24639 is_trailing_return,
24640 &type_specifier_seq);
24641 if (type_location)
24642 *type_location = type_specifier_seq.locations[ds_type_spec];
24644 if (is_template_arg && type_specifier_seq.type
24645 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24646 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24647 /* A bare template name as a template argument is a template template
24648 argument, not a placeholder, so fail parsing it as a type argument. */
24650 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24651 cp_parser_simulate_error (parser);
24652 return error_mark_node;
24654 if (type_specifier_seq.type == error_mark_node)
24655 return error_mark_node;
24657 /* There might or might not be an abstract declarator. */
24658 cp_parser_parse_tentatively (parser);
24659 /* Look for the declarator. */
24660 abstract_declarator
24661 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24662 CP_PARSER_FLAGS_NONE, NULL,
24663 /*parenthesized_p=*/NULL,
24664 /*member_p=*/false,
24665 /*friend_p=*/false,
24666 /*static_p=*/false);
24667 /* Check to see if there really was a declarator. */
24668 if (!cp_parser_parse_definitely (parser))
24669 abstract_declarator = NULL;
24671 bool auto_typeid_ok = false;
24672 /* The concepts TS allows 'auto' as a type-id. */
24673 if (flag_concepts_ts)
24674 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24675 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24676 outside the template-argument-list context here only for the sake of
24677 diagnostic: grokdeclarator then can emit a better error message for
24678 e.g. using T = auto. */
24679 else if (flag_concepts)
24680 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24681 && !parser->in_template_argument_list_p);
24683 if (type_specifier_seq.type
24684 && !auto_typeid_ok
24685 /* None of the valid uses of 'auto' in C++14 involve the type-id
24686 nonterminal, but it is valid in a trailing-return-type. */
24687 && !(cxx_dialect >= cxx14 && is_trailing_return))
24688 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24690 /* A type-id with type 'auto' is only ok if the abstract declarator
24691 is a function declarator with a late-specified return type.
24693 A type-id with 'auto' is also valid in a trailing-return-type
24694 in a compound-requirement. */
24695 if (abstract_declarator
24696 && abstract_declarator->kind == cdk_function
24697 && abstract_declarator->u.function.late_return_type)
24698 /* OK */;
24699 else if (parser->in_result_type_constraint_p)
24700 /* OK */;
24701 else
24703 if (!cp_parser_simulate_error (parser))
24705 location_t loc = type_specifier_seq.locations[ds_type_spec];
24706 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24708 auto_diagnostic_group g;
24709 gcc_rich_location richloc (loc);
24710 richloc.add_fixit_insert_after ("<>");
24711 error_at (&richloc, "missing template arguments after %qE",
24712 tmpl);
24713 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24714 tmpl);
24716 else if (parser->in_template_argument_list_p)
24717 error_at (loc, "%qT not permitted in template argument",
24718 auto_node);
24719 else
24720 error_at (loc, "invalid use of %qT", auto_node);
24722 return error_mark_node;
24726 return groktypename (&type_specifier_seq, abstract_declarator,
24727 is_template_arg);
24730 /* Wrapper for cp_parser_type_id_1. */
24732 static tree
24733 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24734 location_t *type_location)
24736 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24739 /* Wrapper for cp_parser_type_id_1. */
24741 static tree
24742 cp_parser_template_type_arg (cp_parser *parser)
24744 tree r;
24745 const char *saved_message = parser->type_definition_forbidden_message;
24746 parser->type_definition_forbidden_message
24747 = G_("types may not be defined in template arguments");
24748 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24749 parser->type_definition_forbidden_message = saved_message;
24750 return r;
24753 /* Wrapper for cp_parser_type_id_1. */
24755 static tree
24756 cp_parser_trailing_type_id (cp_parser *parser)
24758 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24759 false, true, NULL);
24762 /* Parse a type-specifier-seq.
24764 type-specifier-seq:
24765 type-specifier type-specifier-seq [opt]
24767 GNU extension:
24769 type-specifier-seq:
24770 attributes type-specifier-seq [opt]
24772 The parser flags FLAGS is used to control type-specifier parsing.
24774 If IS_DECLARATION is true, we are at the start of a "condition" or
24775 exception-declaration, so we might be followed by a declarator-id.
24777 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24778 i.e. we've just seen "->".
24780 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24782 static void
24783 cp_parser_type_specifier_seq (cp_parser* parser,
24784 cp_parser_flags flags,
24785 bool is_declaration,
24786 bool is_trailing_return,
24787 cp_decl_specifier_seq *type_specifier_seq)
24789 bool seen_type_specifier = false;
24790 cp_token *start_token = NULL;
24792 /* Clear the TYPE_SPECIFIER_SEQ. */
24793 clear_decl_specs (type_specifier_seq);
24795 flags |= CP_PARSER_FLAGS_OPTIONAL;
24796 /* In the context of a trailing return type, enum E { } is an
24797 elaborated-type-specifier followed by a function-body, not an
24798 enum-specifier. */
24799 if (is_trailing_return)
24800 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24802 /* Parse the type-specifiers and attributes. */
24803 while (true)
24805 tree type_specifier;
24806 bool is_cv_qualifier;
24808 /* Check for attributes first. */
24809 if (cp_next_tokens_can_be_attribute_p (parser))
24811 /* GNU attributes at the end of a declaration apply to the
24812 declaration as a whole, not to the trailing return type. So look
24813 ahead to see if these attributes are at the end. */
24814 if (seen_type_specifier && is_trailing_return
24815 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24817 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24818 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24819 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24820 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24821 break;
24823 type_specifier_seq->attributes
24824 = attr_chainon (type_specifier_seq->attributes,
24825 cp_parser_attributes_opt (parser));
24826 continue;
24829 /* record the token of the beginning of the type specifier seq,
24830 for error reporting purposes*/
24831 if (!start_token)
24832 start_token = cp_lexer_peek_token (parser->lexer);
24834 /* Look for the type-specifier. */
24835 type_specifier = cp_parser_type_specifier (parser,
24836 flags,
24837 type_specifier_seq,
24838 /*is_declaration=*/false,
24839 NULL,
24840 &is_cv_qualifier);
24841 if (!type_specifier)
24843 /* If the first type-specifier could not be found, this is not a
24844 type-specifier-seq at all. */
24845 if (!seen_type_specifier)
24847 /* Set in_declarator_p to avoid skipping to the semicolon. */
24848 int in_decl = parser->in_declarator_p;
24849 parser->in_declarator_p = true;
24851 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24852 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24853 cp_parser_error (parser, "expected type-specifier");
24855 parser->in_declarator_p = in_decl;
24857 type_specifier_seq->type = error_mark_node;
24858 return;
24860 /* If subsequent type-specifiers could not be found, the
24861 type-specifier-seq is complete. */
24862 break;
24865 seen_type_specifier = true;
24866 /* The standard says that a condition can be:
24868 type-specifier-seq declarator = assignment-expression
24870 However, given:
24872 struct S {};
24873 if (int S = ...)
24875 we should treat the "S" as a declarator, not as a
24876 type-specifier. The standard doesn't say that explicitly for
24877 type-specifier-seq, but it does say that for
24878 decl-specifier-seq in an ordinary declaration. Perhaps it
24879 would be clearer just to allow a decl-specifier-seq here, and
24880 then add a semantic restriction that if any decl-specifiers
24881 that are not type-specifiers appear, the program is invalid. */
24882 if (is_declaration && !is_cv_qualifier)
24883 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24887 /* Return whether the function currently being declared has an associated
24888 template parameter list. */
24890 static bool
24891 function_being_declared_is_template_p (cp_parser* parser)
24893 if (!current_template_parms || processing_template_parmlist)
24894 return false;
24896 if (parser->implicit_template_scope)
24897 return true;
24899 if (at_class_scope_p ()
24900 && TYPE_BEING_DEFINED (current_class_type))
24901 return parser->num_template_parameter_lists != 0;
24903 return ((int) parser->num_template_parameter_lists > template_class_depth
24904 (current_class_type));
24907 /* Parse a parameter-declaration-clause.
24909 parameter-declaration-clause:
24910 parameter-declaration-list [opt] ... [opt]
24911 parameter-declaration-list , ...
24913 The parser flags FLAGS is used to control type-specifier parsing.
24915 Returns a representation for the parameter declarations. A return
24916 value of NULL indicates a parameter-declaration-clause consisting
24917 only of an ellipsis. */
24919 static tree
24920 cp_parser_parameter_declaration_clause (cp_parser* parser,
24921 cp_parser_flags flags)
24923 tree parameters;
24924 cp_token *token;
24925 bool ellipsis_p;
24927 auto cleanup = make_temp_override
24928 (parser->auto_is_implicit_function_template_parm_p);
24930 if (!processing_specialization
24931 && !processing_template_parmlist
24932 && !processing_explicit_instantiation
24933 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24934 actual function or a random abstract declarator. */
24935 && parser->default_arg_ok_p)
24936 if (!current_function_decl
24937 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24938 parser->auto_is_implicit_function_template_parm_p = true;
24940 /* Peek at the next token. */
24941 token = cp_lexer_peek_token (parser->lexer);
24942 /* Check for trivial parameter-declaration-clauses. */
24943 if (token->type == CPP_ELLIPSIS)
24945 /* Consume the `...' token. */
24946 cp_lexer_consume_token (parser->lexer);
24947 return NULL_TREE;
24949 else if (token->type == CPP_CLOSE_PAREN)
24950 /* There are no parameters. */
24951 return void_list_node;
24952 /* Check for `(void)', too, which is a special case. */
24953 else if (token->keyword == RID_VOID
24954 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24955 == CPP_CLOSE_PAREN))
24957 /* Consume the `void' token. */
24958 cp_lexer_consume_token (parser->lexer);
24959 /* There are no parameters. */
24960 return explicit_void_list_node;
24963 /* A vector of parameters that haven't been pushed yet. */
24964 auto_vec<tree> pending_decls;
24966 /* Parse the parameter-declaration-list. */
24967 parameters = cp_parser_parameter_declaration_list (parser, flags,
24968 &pending_decls);
24969 /* If a parse error occurred while parsing the
24970 parameter-declaration-list, then the entire
24971 parameter-declaration-clause is erroneous. */
24972 if (parameters == error_mark_node)
24973 return NULL_TREE;
24975 /* Peek at the next token. */
24976 token = cp_lexer_peek_token (parser->lexer);
24977 /* If it's a `,', the clause should terminate with an ellipsis. */
24978 if (token->type == CPP_COMMA)
24980 /* Consume the `,'. */
24981 cp_lexer_consume_token (parser->lexer);
24982 /* Expect an ellipsis. */
24983 ellipsis_p
24984 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24986 /* It might also be `...' if the optional trailing `,' was
24987 omitted. */
24988 else if (token->type == CPP_ELLIPSIS)
24990 /* Consume the `...' token. */
24991 cp_lexer_consume_token (parser->lexer);
24992 /* And remember that we saw it. */
24993 ellipsis_p = true;
24995 else
24996 ellipsis_p = false;
24998 /* A valid parameter-declaration-clause can only be followed by a ')'.
24999 So it's time to push all the parameters we have seen now that we
25000 know we have a valid declaration. Note that here we may not have
25001 committed yet, nor should we. Pushing here will detect the error
25002 of redefining a parameter. */
25003 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25004 for (tree p : pending_decls)
25005 pushdecl (p);
25007 /* Finish the parameter list. */
25008 if (!ellipsis_p)
25009 parameters = chainon (parameters, void_list_node);
25011 return parameters;
25014 /* Parse a parameter-declaration-list.
25016 parameter-declaration-list:
25017 parameter-declaration
25018 parameter-declaration-list , parameter-declaration
25020 The parser flags FLAGS is used to control type-specifier parsing.
25021 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25023 Returns a representation of the parameter-declaration-list, as for
25024 cp_parser_parameter_declaration_clause. However, the
25025 `void_list_node' is never appended to the list. */
25027 static tree
25028 cp_parser_parameter_declaration_list (cp_parser* parser,
25029 cp_parser_flags flags,
25030 auto_vec<tree> *pending_decls)
25032 tree parameters = NULL_TREE;
25033 tree *tail = &parameters;
25034 bool saved_in_unbraced_linkage_specification_p;
25035 int index = 0;
25037 /* The special considerations that apply to a function within an
25038 unbraced linkage specifications do not apply to the parameters
25039 to the function. */
25040 saved_in_unbraced_linkage_specification_p
25041 = parser->in_unbraced_linkage_specification_p;
25042 parser->in_unbraced_linkage_specification_p = false;
25044 /* Look for more parameters. */
25045 while (true)
25047 cp_parameter_declarator *parameter;
25048 tree decl = error_mark_node;
25049 bool parenthesized_p = false;
25051 /* Parse the parameter. */
25052 parameter
25053 = cp_parser_parameter_declaration (parser, flags,
25054 /*template_parm_p=*/false,
25055 &parenthesized_p);
25057 /* We don't know yet if the enclosing context is unavailable or deprecated,
25058 so wait and deal with it in grokparms if appropriate. */
25059 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25061 if (parameter && !cp_parser_error_occurred (parser))
25063 decl = grokdeclarator (parameter->declarator,
25064 &parameter->decl_specifiers,
25065 PARM,
25066 parameter->default_argument != NULL_TREE,
25067 &parameter->decl_specifiers.attributes);
25068 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25069 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25072 deprecated_state = DEPRECATED_NORMAL;
25074 /* If a parse error occurred parsing the parameter declaration,
25075 then the entire parameter-declaration-list is erroneous. */
25076 if (decl == error_mark_node)
25078 parameters = error_mark_node;
25079 break;
25082 if (parameter->decl_specifiers.attributes)
25083 cplus_decl_attributes (&decl,
25084 parameter->decl_specifiers.attributes,
25086 if (DECL_NAME (decl))
25088 /* We cannot always pushdecl while parsing tentatively because
25089 it may have side effects and we can't be sure yet if we're
25090 parsing a declaration, e.g.:
25092 S foo(int(x), int(x), int{x});
25094 where it's not clear if we're dealing with a constructor call
25095 or a function declaration until we've seen the last argument
25096 which breaks it up.
25097 It's safe to pushdecl so long as it doesn't result in a clash
25098 with an already-pushed parameter. But we don't delay pushing
25099 different parameters to handle
25101 S foo(int(i), decltype(i) j = 42);
25103 which is valid. */
25104 if (pending_decls
25105 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25106 /* See if PARAMETERS already contains a parameter with the same
25107 DECL_NAME as DECL. */
25108 && [parameters, decl] {
25109 for (tree p = parameters; p; p = TREE_CHAIN (p))
25110 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25111 return true;
25112 return false;
25113 }())
25114 pending_decls->safe_push (decl);
25115 else
25116 decl = pushdecl (decl);
25119 if (decl != error_mark_node)
25121 retrofit_lang_decl (decl);
25122 DECL_PARM_INDEX (decl) = ++index;
25123 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25126 /* Add the new parameter to the list. */
25127 *tail = build_tree_list (parameter->default_argument, decl);
25128 tail = &TREE_CHAIN (*tail);
25130 /* If the parameters were parenthesized, it's the case of
25131 T foo(X(x)) which looks like a variable definition but
25132 is a function declaration. */
25133 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25134 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25136 /* Peek at the next token. */
25137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25138 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25139 /* These are for Objective-C++ */
25140 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25141 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25142 /* The parameter-declaration-list is complete. */
25143 break;
25144 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25146 cp_token *token;
25148 /* Peek at the next token. */
25149 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25150 /* If it's an ellipsis, then the list is complete. */
25151 if (token->type == CPP_ELLIPSIS)
25152 break;
25153 /* Otherwise, there must be more parameters. Consume the
25154 `,'. */
25155 cp_lexer_consume_token (parser->lexer);
25156 /* When parsing something like:
25158 int i(float f, double d)
25160 we can tell after seeing the declaration for "f" that we
25161 are not looking at an initialization of a variable "i",
25162 but rather at the declaration of a function "i".
25164 Due to the fact that the parsing of template arguments
25165 (as specified to a template-id) requires backtracking we
25166 cannot use this technique when inside a template argument
25167 list. */
25168 if (!parser->in_template_argument_list_p
25169 && !parser->in_type_id_in_expr_p
25170 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25171 /* However, a parameter-declaration of the form
25172 "float(f)" (which is a valid declaration of a
25173 parameter "f") can also be interpreted as an
25174 expression (the conversion of "f" to "float"). */
25175 && !parenthesized_p)
25176 cp_parser_commit_to_tentative_parse (parser);
25178 else
25180 cp_parser_error (parser, "expected %<,%> or %<...%>");
25181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25182 cp_parser_skip_to_closing_parenthesis (parser,
25183 /*recovering=*/true,
25184 /*or_comma=*/false,
25185 /*consume_paren=*/false);
25186 break;
25190 parser->in_unbraced_linkage_specification_p
25191 = saved_in_unbraced_linkage_specification_p;
25193 /* Reset implicit_template_scope if we are about to leave the function
25194 parameter list that introduced it. Note that for out-of-line member
25195 definitions, there will be one or more class scopes before we get to
25196 the template parameter scope. */
25198 if (cp_binding_level *its = parser->implicit_template_scope)
25199 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25201 while (maybe_its->kind == sk_class)
25202 maybe_its = maybe_its->level_chain;
25203 if (maybe_its == its)
25205 parser->implicit_template_parms = 0;
25206 parser->implicit_template_scope = 0;
25210 return parameters;
25213 /* Parse a parameter declaration.
25215 parameter-declaration:
25216 decl-specifier-seq ... [opt] declarator
25217 decl-specifier-seq declarator = assignment-expression
25218 decl-specifier-seq ... [opt] abstract-declarator [opt]
25219 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25221 The parser flags FLAGS is used to control type-specifier parsing.
25223 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25224 declares a template parameter. (In that case, a non-nested `>'
25225 token encountered during the parsing of the assignment-expression
25226 is not interpreted as a greater-than operator.)
25228 Returns a representation of the parameter, or NULL if an error
25229 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25230 true iff the declarator is of the form "(p)". */
25232 static cp_parameter_declarator *
25233 cp_parser_parameter_declaration (cp_parser *parser,
25234 cp_parser_flags flags,
25235 bool template_parm_p,
25236 bool *parenthesized_p)
25238 int declares_class_or_enum;
25239 cp_decl_specifier_seq decl_specifiers;
25240 cp_declarator *declarator;
25241 tree default_argument;
25242 cp_token *token = NULL, *declarator_token_start = NULL;
25243 const char *saved_message;
25244 bool template_parameter_pack_p = false;
25246 /* In a template parameter, `>' is not an operator.
25248 [temp.param]
25250 When parsing a default template-argument for a non-type
25251 template-parameter, the first non-nested `>' is taken as the end
25252 of the template parameter-list rather than a greater-than
25253 operator. */
25255 /* Type definitions may not appear in parameter types. */
25256 saved_message = parser->type_definition_forbidden_message;
25257 parser->type_definition_forbidden_message
25258 = G_("types may not be defined in parameter types");
25260 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25261 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25262 (current_template_parms)) : 0);
25264 /* Parse the declaration-specifiers. */
25265 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25266 cp_parser_decl_specifier_seq (parser,
25267 flags,
25268 &decl_specifiers,
25269 &declares_class_or_enum);
25271 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25272 type-constraint opt auto can be used as a decl-specifier of the
25273 decl-specifier-seq of a parameter-declaration of a function declaration
25274 or lambda-expression..." but we must not synthesize an implicit template
25275 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25276 we want to synthesize only the first auto. */
25277 auto cleanup = make_temp_override
25278 (parser->auto_is_implicit_function_template_parm_p, false);
25280 /* Complain about missing 'typename' or other invalid type names. */
25281 if (!decl_specifiers.any_type_specifiers_p
25282 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25283 decl_specifiers.type = error_mark_node;
25285 /* If an error occurred, there's no reason to attempt to parse the
25286 rest of the declaration. */
25287 if (cp_parser_error_occurred (parser))
25289 parser->type_definition_forbidden_message = saved_message;
25290 return NULL;
25293 /* Peek at the next token. */
25294 token = cp_lexer_peek_token (parser->lexer);
25296 /* If the next token is a `)', `,', `=', `>', or `...', then there
25297 is no declarator. However, when variadic templates are enabled,
25298 there may be a declarator following `...'. */
25299 if (token->type == CPP_CLOSE_PAREN
25300 || token->type == CPP_COMMA
25301 || token->type == CPP_EQ
25302 || token->type == CPP_GREATER)
25304 declarator = NULL;
25305 if (parenthesized_p)
25306 *parenthesized_p = false;
25308 /* Otherwise, there should be a declarator. */
25309 else
25311 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25312 parser->default_arg_ok_p = false;
25314 /* After seeing a decl-specifier-seq, if the next token is not a
25315 "(" or "{", there is no possibility that the code is a valid
25316 expression. Therefore, if parsing tentatively, we commit at
25317 this point. */
25318 if (!parser->in_template_argument_list_p
25319 /* In an expression context, having seen:
25321 (int((char ...
25323 we cannot be sure whether we are looking at a
25324 function-type (taking a "char" as a parameter) or a cast
25325 of some object of type "char" to "int". */
25326 && !parser->in_type_id_in_expr_p
25327 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25328 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25332 if (decl_specifiers.type
25333 && template_placeholder_p (decl_specifiers.type))
25334 /* This is a CTAD expression, not a parameter declaration. */
25335 cp_parser_simulate_error (parser);
25337 else
25338 cp_parser_commit_to_tentative_parse (parser);
25340 /* Parse the declarator. */
25341 declarator_token_start = token;
25342 declarator = cp_parser_declarator (parser,
25343 CP_PARSER_DECLARATOR_EITHER,
25344 CP_PARSER_FLAGS_NONE,
25345 /*ctor_dtor_or_conv_p=*/NULL,
25346 parenthesized_p,
25347 /*member_p=*/false,
25348 /*friend_p=*/false,
25349 /*static_p=*/false);
25350 parser->default_arg_ok_p = saved_default_arg_ok_p;
25351 /* After the declarator, allow more attributes. */
25352 decl_specifiers.attributes
25353 = attr_chainon (decl_specifiers.attributes,
25354 cp_parser_attributes_opt (parser));
25356 /* If the declarator is a template parameter pack, remember that and
25357 clear the flag in the declarator itself so we don't get errors
25358 from grokdeclarator. */
25359 if (template_parm_p && declarator && declarator->parameter_pack_p)
25361 declarator->parameter_pack_p = false;
25362 template_parameter_pack_p = true;
25366 /* If the next token is an ellipsis, and we have not seen a declarator
25367 name, and if either the type of the declarator contains parameter
25368 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25369 for, eg, abbreviated integral type names), then we actually have a
25370 parameter pack expansion expression. Otherwise, leave the ellipsis
25371 for a C-style variadic function. */
25372 token = cp_lexer_peek_token (parser->lexer);
25374 /* If a function parameter pack was specified and an implicit template
25375 parameter was introduced during cp_parser_parameter_declaration,
25376 change any implicit parameters introduced into packs. */
25377 if (parser->implicit_template_parms
25378 && ((token->type == CPP_ELLIPSIS
25379 && declarator_can_be_parameter_pack (declarator))
25380 || (declarator && declarator->parameter_pack_p)))
25382 int latest_template_parm_idx = TREE_VEC_LENGTH
25383 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25385 if (latest_template_parm_idx != template_parm_idx)
25386 decl_specifiers.type = convert_generic_types_to_packs
25387 (decl_specifiers.type,
25388 template_parm_idx, latest_template_parm_idx);
25391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25393 tree type = decl_specifiers.type;
25395 if (type && DECL_P (type))
25396 type = TREE_TYPE (type);
25398 if (((type
25399 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25400 && (template_parm_p || uses_parameter_packs (type)))
25401 || (!type && template_parm_p))
25402 && declarator_can_be_parameter_pack (declarator))
25404 /* Consume the `...'. */
25405 cp_lexer_consume_token (parser->lexer);
25406 maybe_warn_variadic_templates ();
25408 /* Build a pack expansion type */
25409 if (template_parm_p)
25410 template_parameter_pack_p = true;
25411 else if (declarator)
25412 declarator->parameter_pack_p = true;
25413 else
25414 decl_specifiers.type = make_pack_expansion (type);
25418 /* The restriction on defining new types applies only to the type
25419 of the parameter, not to the default argument. */
25420 parser->type_definition_forbidden_message = saved_message;
25422 /* If the next token is `=', then process a default argument. */
25423 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25425 tree type = decl_specifiers.type;
25426 token = cp_lexer_peek_token (parser->lexer);
25427 if (declarator)
25428 declarator->init_loc = token->location;
25429 /* If we are defining a class, then the tokens that make up the
25430 default argument must be saved and processed later. */
25431 if (!template_parm_p && at_class_scope_p ()
25432 && TYPE_BEING_DEFINED (current_class_type)
25433 && !LAMBDA_TYPE_P (current_class_type))
25434 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25436 /* A constrained-type-specifier may declare a type
25437 template-parameter. */
25438 else if (declares_constrained_type_template_parameter (type))
25439 default_argument
25440 = cp_parser_default_type_template_argument (parser);
25442 /* A constrained-type-specifier may declare a
25443 template-template-parameter. */
25444 else if (declares_constrained_template_template_parameter (type))
25445 default_argument
25446 = cp_parser_default_template_template_argument (parser);
25448 /* Outside of a class definition, we can just parse the
25449 assignment-expression. */
25450 else
25451 default_argument
25452 = cp_parser_default_argument (parser, template_parm_p);
25454 if (!parser->default_arg_ok_p)
25456 permerror (token->location,
25457 "default arguments are only "
25458 "permitted for function parameters");
25460 else if ((declarator && declarator->parameter_pack_p)
25461 || template_parameter_pack_p
25462 || (decl_specifiers.type
25463 && PACK_EXPANSION_P (decl_specifiers.type)))
25465 /* Find the name of the parameter pack. */
25466 cp_declarator *id_declarator = declarator;
25467 while (id_declarator && id_declarator->kind != cdk_id)
25468 id_declarator = id_declarator->declarator;
25470 if (id_declarator && id_declarator->kind == cdk_id)
25471 error_at (declarator_token_start->location,
25472 template_parm_p
25473 ? G_("template parameter pack %qD "
25474 "cannot have a default argument")
25475 : G_("parameter pack %qD cannot have "
25476 "a default argument"),
25477 id_declarator->u.id.unqualified_name);
25478 else
25479 error_at (declarator_token_start->location,
25480 template_parm_p
25481 ? G_("template parameter pack cannot have "
25482 "a default argument")
25483 : G_("parameter pack cannot have a "
25484 "default argument"));
25486 default_argument = NULL_TREE;
25489 else
25490 default_argument = NULL_TREE;
25492 if (default_argument)
25493 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25495 /* Generate a location for the parameter, ranging from the start of the
25496 initial token to the end of the final token (using input_location for
25497 the latter, set up by cp_lexer_set_source_position_from_token when
25498 consuming tokens).
25500 If we have a identifier, then use it for the caret location, e.g.
25502 extern int callee (int one, int (*two)(int, int), float three);
25503 ~~~~~~^~~~~~~~~~~~~~
25505 otherwise, reuse the start location for the caret location e.g.:
25507 extern int callee (int one, int (*)(int, int), float three);
25508 ^~~~~~~~~~~~~~~~~
25511 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25512 ? declarator->id_loc
25513 : decl_spec_token_start->location);
25514 location_t param_loc = make_location (caret_loc,
25515 decl_spec_token_start->location,
25516 input_location);
25518 return make_parameter_declarator (&decl_specifiers,
25519 declarator,
25520 default_argument,
25521 param_loc,
25522 template_parameter_pack_p);
25525 /* Parse a default argument and return it.
25527 TEMPLATE_PARM_P is true if this is a default argument for a
25528 non-type template parameter. */
25529 static tree
25530 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25532 tree default_argument = NULL_TREE;
25533 bool saved_greater_than_is_operator_p;
25534 unsigned char saved_local_variables_forbidden_p;
25536 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25537 set correctly. */
25538 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25539 parser->greater_than_is_operator_p = !template_parm_p;
25540 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25541 auto ord = make_temp_override (parser->oacc_routine, NULL);
25542 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25544 /* Local variable names (and the `this' keyword) may not
25545 appear in a default argument. */
25546 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25547 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25548 /* Parse the assignment-expression. */
25549 if (template_parm_p)
25550 push_deferring_access_checks (dk_no_deferred);
25551 tree saved_class_ptr = NULL_TREE;
25552 tree saved_class_ref = NULL_TREE;
25553 /* The "this" pointer is not valid in a default argument. */
25554 if (cfun)
25556 saved_class_ptr = current_class_ptr;
25557 cp_function_chain->x_current_class_ptr = NULL_TREE;
25558 saved_class_ref = current_class_ref;
25559 cp_function_chain->x_current_class_ref = NULL_TREE;
25561 default_argument = cp_parser_initializer (parser);
25562 /* Restore the "this" pointer. */
25563 if (cfun)
25565 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25566 cp_function_chain->x_current_class_ref = saved_class_ref;
25568 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25570 if (template_parm_p)
25571 pop_deferring_access_checks ();
25572 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25573 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25575 return default_argument;
25578 /* Parse a function-body.
25580 function-body:
25581 compound_statement */
25583 static void
25584 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25586 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25587 ? BCS_TRY_BLOCK : BCS_NORMAL),
25588 true);
25591 /* Parse a ctor-initializer-opt followed by a function-body. Return
25592 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25593 is true we are parsing a function-try-block. */
25595 static void
25596 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25597 bool in_function_try_block)
25599 tree body, list;
25600 const bool check_body_p
25601 = (DECL_CONSTRUCTOR_P (current_function_decl)
25602 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25603 tree last = NULL;
25605 if (in_function_try_block
25606 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25607 && cxx_dialect < cxx20)
25609 if (DECL_CONSTRUCTOR_P (current_function_decl))
25610 pedwarn (input_location, OPT_Wc__20_extensions,
25611 "function-try-block body of %<constexpr%> constructor only "
25612 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25613 else
25614 pedwarn (input_location, OPT_Wc__20_extensions,
25615 "function-try-block body of %<constexpr%> function only "
25616 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25619 /* Begin the function body. */
25620 body = begin_function_body ();
25621 /* Parse the optional ctor-initializer. */
25622 cp_parser_ctor_initializer_opt (parser);
25624 /* If we're parsing a constexpr constructor definition, we need
25625 to check that the constructor body is indeed empty. However,
25626 before we get to cp_parser_function_body lot of junk has been
25627 generated, so we can't just check that we have an empty block.
25628 Rather we take a snapshot of the outermost block, and check whether
25629 cp_parser_function_body changed its state. */
25630 if (check_body_p)
25632 list = cur_stmt_list;
25633 if (STATEMENT_LIST_TAIL (list))
25634 last = STATEMENT_LIST_TAIL (list)->stmt;
25636 /* Parse the function-body. */
25637 cp_parser_function_body (parser, in_function_try_block);
25638 if (check_body_p)
25639 check_constexpr_ctor_body (last, list, /*complain=*/true);
25640 /* Finish the function body. */
25641 finish_function_body (body);
25644 /* Parse an initializer.
25646 initializer:
25647 = initializer-clause
25648 ( expression-list )
25650 Returns an expression representing the initializer. If no
25651 initializer is present, NULL_TREE is returned.
25653 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25654 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25655 set to TRUE if there is no initializer present. If there is an
25656 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25657 is set to true; otherwise it is set to false. */
25659 static tree
25660 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
25661 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
25663 cp_token *token;
25664 tree init;
25666 /* Peek at the next token. */
25667 token = cp_lexer_peek_token (parser->lexer);
25669 /* Let our caller know whether or not this initializer was
25670 parenthesized. */
25671 if (is_direct_init)
25672 *is_direct_init = (token->type != CPP_EQ);
25673 /* Assume that the initializer is constant. */
25674 if (non_constant_p)
25675 *non_constant_p = false;
25677 if (token->type == CPP_EQ)
25679 /* Consume the `='. */
25680 cp_lexer_consume_token (parser->lexer);
25681 /* Parse the initializer-clause. */
25682 init = cp_parser_initializer_clause (parser, non_constant_p);
25684 else if (token->type == CPP_OPEN_PAREN)
25686 vec<tree, va_gc> *vec;
25687 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25688 /*cast_p=*/false,
25689 /*allow_expansion_p=*/true,
25690 non_constant_p);
25691 if (vec == NULL)
25692 return error_mark_node;
25693 init = build_tree_list_vec (vec);
25694 release_tree_vector (vec);
25696 else if (token->type == CPP_OPEN_BRACE)
25698 cp_lexer_set_source_position (parser->lexer);
25699 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25700 init = cp_parser_braced_list (parser, non_constant_p);
25701 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25703 else
25705 /* Anything else is an error. */
25706 cp_parser_error (parser, "expected initializer");
25707 init = error_mark_node;
25710 if (!subexpression_p && check_for_bare_parameter_packs (init))
25711 init = error_mark_node;
25713 return init;
25716 /* Parse an initializer-clause.
25718 initializer-clause:
25719 assignment-expression
25720 braced-init-list
25722 Returns an expression representing the initializer.
25724 If the `assignment-expression' production is used the value
25725 returned is simply a representation for the expression.
25727 Otherwise, calls cp_parser_braced_list. */
25729 static cp_expr
25730 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25732 cp_expr initializer;
25734 /* Assume the expression is constant. */
25735 if (non_constant_p)
25736 *non_constant_p = false;
25738 /* If it is not a `{', then we are looking at an
25739 assignment-expression. */
25740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25742 initializer
25743 = cp_parser_constant_expression (parser,
25744 /*allow_non_constant_p=*/2,
25745 non_constant_p);
25747 else
25748 initializer = cp_parser_braced_list (parser, non_constant_p);
25750 return initializer;
25753 /* Parse a brace-enclosed initializer list.
25755 braced-init-list:
25756 { initializer-list , [opt] }
25757 { designated-initializer-list , [opt] }
25760 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25761 the elements of the initializer-list (or NULL, if the last
25762 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25763 NULL_TREE. There is no way to detect whether or not the optional
25764 trailing `,' was provided. NON_CONSTANT_P is as for
25765 cp_parser_initializer. */
25767 static cp_expr
25768 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
25770 tree initializer;
25771 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25773 /* Consume the `{' token. */
25774 matching_braces braces;
25775 braces.require_open (parser);
25776 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25777 initializer = make_node (CONSTRUCTOR);
25778 /* If it's not a `}', then there is a non-trivial initializer. */
25779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25781 bool designated;
25782 /* Parse the initializer list. */
25783 CONSTRUCTOR_ELTS (initializer)
25784 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25785 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25786 /* A trailing `,' token is allowed. */
25787 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25788 cp_lexer_consume_token (parser->lexer);
25790 else if (non_constant_p)
25791 *non_constant_p = false;
25792 /* Now, there should be a trailing `}'. */
25793 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25794 braces.require_close (parser);
25795 TREE_TYPE (initializer) = init_list_type_node;
25796 recompute_constructor_flags (initializer);
25798 cp_expr result (initializer);
25799 /* Build a location of the form:
25800 { ... }
25801 ^~~~~~~
25802 with caret==start at the open brace, finish at the close brace. */
25803 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25804 result.set_location (combined_loc);
25805 return result;
25808 /* Consume tokens up to, but not including, the next non-nested closing `]'.
25809 Returns true iff we found a closing `]'. */
25811 static bool
25812 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
25814 unsigned square_depth = 0;
25816 while (true)
25818 cp_token * token = cp_lexer_peek_token (parser->lexer);
25820 switch (token->type)
25822 case CPP_PRAGMA_EOL:
25823 if (!parser->lexer->in_pragma)
25824 break;
25825 /* FALLTHRU */
25827 case CPP_EOF:
25828 /* If we've run out of tokens, then there is no closing `]'. */
25829 return false;
25831 case CPP_OPEN_SQUARE:
25832 ++square_depth;
25833 break;
25835 case CPP_CLOSE_SQUARE:
25836 if (!square_depth--)
25837 return true;
25838 break;
25840 default:
25841 break;
25844 /* Consume the current token, skipping it. */
25845 cp_lexer_consume_token (parser->lexer);
25849 /* Consume tokens up to, and including, the next non-nested closing `]'.
25850 Returns true iff we found a closing `]'. */
25852 static bool
25853 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25855 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
25856 if (found)
25857 cp_lexer_consume_token (parser->lexer);
25858 return found;
25861 /* Return true if we are looking at an array-designator, false otherwise. */
25863 static bool
25864 cp_parser_array_designator_p (cp_parser *parser)
25866 /* Consume the `['. */
25867 cp_lexer_consume_token (parser->lexer);
25869 cp_lexer_save_tokens (parser->lexer);
25871 /* Skip tokens until the next token is a closing square bracket.
25872 If we find the closing `]', and the next token is a `=', then
25873 we are looking at an array designator. */
25874 bool array_designator_p
25875 = (cp_parser_skip_to_closing_square_bracket (parser)
25876 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25878 /* Roll back the tokens we skipped. */
25879 cp_lexer_rollback_tokens (parser->lexer);
25881 return array_designator_p;
25884 /* Parse an initializer-list.
25886 initializer-list:
25887 initializer-clause ... [opt]
25888 initializer-list , initializer-clause ... [opt]
25890 C++20 Extension:
25892 designated-initializer-list:
25893 designated-initializer-clause
25894 designated-initializer-list , designated-initializer-clause
25896 designated-initializer-clause:
25897 designator brace-or-equal-initializer
25899 designator:
25900 . identifier
25902 GNU Extension:
25904 initializer-list:
25905 designation initializer-clause ...[opt]
25906 initializer-list , designation initializer-clause ...[opt]
25908 designation:
25909 . identifier =
25910 identifier :
25911 [ constant-expression ] =
25913 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25914 for the initializer. If the INDEX of the elt is non-NULL, it is the
25915 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25916 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25917 are any designators. */
25919 static vec<constructor_elt, va_gc> *
25920 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25921 bool *designated)
25923 vec<constructor_elt, va_gc> *v = NULL;
25924 bool first_p = true;
25925 tree first_designator = NULL_TREE;
25927 /* Assume all of the expressions are constant. */
25928 if (non_constant_p)
25929 *non_constant_p = false;
25931 unsigned nelts = 0;
25932 int suppress = suppress_location_wrappers;
25934 /* Parse the rest of the list. */
25935 while (true)
25937 cp_token *token;
25938 tree designator;
25939 tree initializer;
25940 bool clause_non_constant_p;
25941 bool direct_p = false;
25942 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25944 /* Handle the C++20 syntax, '. id ='. */
25945 if ((cxx_dialect >= cxx20
25946 || cp_parser_allow_gnu_extensions_p (parser))
25947 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25948 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25949 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25950 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25951 == CPP_OPEN_BRACE)))
25953 if (pedantic && cxx_dialect < cxx20)
25954 pedwarn (loc, OPT_Wc__20_extensions,
25955 "C++ designated initializers only available with "
25956 "%<-std=c++20%> or %<-std=gnu++20%>");
25957 /* Consume the `.'. */
25958 cp_lexer_consume_token (parser->lexer);
25959 /* Consume the identifier. */
25960 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25961 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25962 /* Consume the `='. */
25963 cp_lexer_consume_token (parser->lexer);
25964 else
25965 direct_p = true;
25967 /* Also, if the next token is an identifier and the following one is a
25968 colon, we are looking at the GNU designated-initializer
25969 syntax. */
25970 else if (cp_parser_allow_gnu_extensions_p (parser)
25971 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25973 == CPP_COLON))
25975 /* Warn the user that they are using an extension. */
25976 pedwarn (loc, OPT_Wpedantic,
25977 "ISO C++ does not allow GNU designated initializers");
25978 /* Consume the identifier. */
25979 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25980 /* Consume the `:'. */
25981 cp_lexer_consume_token (parser->lexer);
25983 /* Also handle C99 array designators, '[ const ] ='. */
25984 else if (cp_parser_allow_gnu_extensions_p (parser)
25985 && !c_dialect_objc ()
25986 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25988 /* In C++11, [ could start a lambda-introducer. */
25989 bool non_const = false;
25991 cp_parser_parse_tentatively (parser);
25993 if (!cp_parser_array_designator_p (parser))
25995 cp_parser_simulate_error (parser);
25996 designator = NULL_TREE;
25998 else
26000 designator = cp_parser_constant_expression (parser, true,
26001 &non_const);
26002 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26003 cp_parser_require (parser, CPP_EQ, RT_EQ);
26006 if (!cp_parser_parse_definitely (parser))
26007 designator = NULL_TREE;
26008 else if (non_const
26009 && (!require_potential_rvalue_constant_expression
26010 (designator)))
26011 designator = NULL_TREE;
26012 if (designator)
26013 /* Warn the user that they are using an extension. */
26014 pedwarn (loc, OPT_Wpedantic,
26015 "ISO C++ does not allow C99 designated initializers");
26017 else
26018 designator = NULL_TREE;
26020 if (first_p)
26022 first_designator = designator;
26023 first_p = false;
26025 else if (cxx_dialect >= cxx20
26026 && first_designator != error_mark_node
26027 && (!first_designator != !designator))
26029 error_at (loc, "either all initializer clauses should be designated "
26030 "or none of them should be");
26031 first_designator = error_mark_node;
26033 else if (cxx_dialect < cxx20 && !first_designator)
26034 first_designator = designator;
26036 /* Parse the initializer. */
26037 initializer = cp_parser_initializer_clause (parser,
26038 (non_constant_p != nullptr
26039 ? &clause_non_constant_p
26040 : nullptr));
26041 /* If any clause is non-constant, so is the entire initializer. */
26042 if (non_constant_p && clause_non_constant_p)
26043 *non_constant_p = true;
26045 if (TREE_CODE (initializer) == CONSTRUCTOR)
26046 /* This uses |= rather than = because C_I_D_I could have been set in
26047 cp_parser_functional_cast so we must be careful not to clear the
26048 flag. */
26049 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26051 /* If we have an ellipsis, this is an initializer pack
26052 expansion. */
26053 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26055 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26057 /* Consume the `...'. */
26058 cp_lexer_consume_token (parser->lexer);
26060 if (designator && cxx_dialect >= cxx20)
26061 error_at (loc,
26062 "%<...%> not allowed in designated initializer list");
26064 /* Turn the initializer into an initializer expansion. */
26065 initializer = make_pack_expansion (initializer);
26068 /* Add it to the vector. */
26069 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26071 /* If the next token is not a comma, we have reached the end of
26072 the list. */
26073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26074 break;
26076 /* Peek at the next token. */
26077 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26078 /* If the next token is a `}', then we're still done. An
26079 initializer-clause can have a trailing `,' after the
26080 initializer-list and before the closing `}'. */
26081 if (token->type == CPP_CLOSE_BRACE)
26082 break;
26084 /* Suppress location wrappers in a long initializer to save memory
26085 (14179). The cutoff is chosen arbitrarily. */
26086 const unsigned loc_max = 256;
26087 unsigned incr = 1;
26088 if (TREE_CODE (initializer) == CONSTRUCTOR)
26089 /* Look one level down because it's easy. Looking deeper would require
26090 passing down a nelts pointer, and I don't think multi-level massive
26091 initializers are common enough to justify this. */
26092 incr = CONSTRUCTOR_NELTS (initializer);
26093 nelts += incr;
26094 if (nelts >= loc_max && (nelts - incr) < loc_max)
26095 ++suppress_location_wrappers;
26097 /* Consume the `,' token. */
26098 cp_lexer_consume_token (parser->lexer);
26101 /* The same identifier shall not appear in multiple designators
26102 of a designated-initializer-list. */
26103 if (first_designator)
26105 unsigned int i;
26106 tree designator, val;
26107 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26108 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26110 if (IDENTIFIER_MARKED (designator))
26112 error_at (cp_expr_loc_or_input_loc (val),
26113 "%<.%s%> designator used multiple times in "
26114 "the same initializer list",
26115 IDENTIFIER_POINTER (designator));
26116 (*v)[i].index = error_mark_node;
26118 else
26119 IDENTIFIER_MARKED (designator) = 1;
26121 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26122 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26123 IDENTIFIER_MARKED (designator) = 0;
26126 suppress_location_wrappers = suppress;
26128 *designated = first_designator != NULL_TREE;
26129 return v;
26132 /* Classes [gram.class] */
26134 /* Parse a class-name.
26136 class-name:
26137 identifier
26138 template-id
26140 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26141 to indicate that names looked up in dependent types should be
26142 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26143 keyword has been used to indicate that the name that appears next
26144 is a template. TAG_TYPE indicates the explicit tag given before
26145 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26146 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26147 is the class being defined in a class-head. If ENUM_OK is TRUE,
26148 enum-names are also accepted.
26150 Returns the TYPE_DECL representing the class. */
26152 static tree
26153 cp_parser_class_name (cp_parser *parser,
26154 bool typename_keyword_p,
26155 bool template_keyword_p,
26156 enum tag_types tag_type,
26157 bool check_dependency_p,
26158 bool class_head_p,
26159 bool is_declaration,
26160 bool enum_ok)
26162 tree decl;
26163 tree identifier = NULL_TREE;
26165 /* All class-names start with an identifier. */
26166 cp_token *token = cp_lexer_peek_token (parser->lexer);
26167 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26169 cp_parser_error (parser, "expected class-name");
26170 return error_mark_node;
26173 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26174 to a template-id, so we save it here. Consider object scope too,
26175 so that make_typename_type below can use it (cp_parser_template_name
26176 considers object scope also). This may happen with code like
26178 p->template A<T>::a()
26180 where we first want to look up A<T>::a in the class of the object
26181 expression, as per [basic.lookup.classref]. */
26182 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26183 /* This only checks parser->scope to avoid duplicate errors; if
26184 ->object_type is erroneous, go on to give a parse error. */
26185 if (parser->scope == error_mark_node)
26186 return error_mark_node;
26188 /* Any name names a type if we're following the `typename' keyword
26189 in a qualified name where the enclosing scope is type-dependent. */
26190 const bool typename_p = (typename_keyword_p
26191 && parser->scope
26192 && TYPE_P (parser->scope)
26193 && dependent_scope_p (parser->scope));
26194 /* Handle the common case (an identifier, but not a template-id)
26195 efficiently. */
26196 if (token->type == CPP_NAME
26197 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26199 cp_token *identifier_token;
26200 bool ambiguous_p;
26202 /* Look for the identifier. */
26203 identifier_token = cp_lexer_peek_token (parser->lexer);
26204 ambiguous_p = identifier_token->error_reported;
26205 identifier = cp_parser_identifier (parser);
26206 /* If the next token isn't an identifier, we are certainly not
26207 looking at a class-name. */
26208 if (identifier == error_mark_node)
26209 decl = error_mark_node;
26210 /* If we know this is a type-name, there's no need to look it
26211 up. */
26212 else if (typename_p)
26213 decl = identifier;
26214 else
26216 tree ambiguous_decls;
26217 /* If we already know that this lookup is ambiguous, then
26218 we've already issued an error message; there's no reason
26219 to check again. */
26220 if (ambiguous_p)
26222 cp_parser_simulate_error (parser);
26223 return error_mark_node;
26225 /* If the next token is a `::', then the name must be a type
26226 name.
26228 [basic.lookup.qual]
26230 During the lookup for a name preceding the :: scope
26231 resolution operator, object, function, and enumerator
26232 names are ignored. */
26233 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26234 tag_type = scope_type;
26235 /* Look up the name. */
26236 decl = cp_parser_lookup_name (parser, identifier,
26237 tag_type,
26238 /*is_template=*/false,
26239 /*is_namespace=*/false,
26240 check_dependency_p,
26241 &ambiguous_decls,
26242 identifier_token->location);
26243 if (ambiguous_decls)
26245 if (cp_parser_parsing_tentatively (parser))
26246 cp_parser_simulate_error (parser);
26247 return error_mark_node;
26251 else
26253 /* Try a template-id. */
26254 decl = cp_parser_template_id (parser, template_keyword_p,
26255 check_dependency_p,
26256 tag_type,
26257 is_declaration);
26258 if (decl == error_mark_node)
26259 return error_mark_node;
26262 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26264 /* If this is a typename, create a TYPENAME_TYPE. */
26265 if (typename_p && decl != error_mark_node)
26267 decl = make_typename_type (scope, decl, typename_type,
26268 /*complain=*/tf_error);
26269 if (decl != error_mark_node)
26270 decl = TYPE_NAME (decl);
26273 decl = strip_using_decl (decl);
26275 /* Check to see that it is really the name of a class. */
26276 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26277 && identifier_p (TREE_OPERAND (decl, 0))
26278 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26279 /* Situations like this:
26281 template <typename T> struct A {
26282 typename T::template X<int>::I i;
26285 are problematic. Is `T::template X<int>' a class-name? The
26286 standard does not seem to be definitive, but there is no other
26287 valid interpretation of the following `::'. Therefore, those
26288 names are considered class-names. */
26290 decl = make_typename_type (scope, decl, tag_type, tf_error);
26291 if (decl != error_mark_node)
26292 decl = TYPE_NAME (decl);
26294 else if (TREE_CODE (decl) != TYPE_DECL
26295 || TREE_TYPE (decl) == error_mark_node
26296 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26297 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26298 /* In Objective-C 2.0, a classname followed by '.' starts a
26299 dot-syntax expression, and it's not a type-name. */
26300 || (c_dialect_objc ()
26301 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26302 && objc_is_class_name (decl)))
26303 decl = error_mark_node;
26305 if (decl == error_mark_node)
26306 cp_parser_error (parser, "expected class-name");
26307 else if (identifier && !parser->scope)
26308 maybe_note_name_used_in_class (identifier, decl);
26310 return decl;
26313 /* Make sure that any member-function parameters are in scope.
26314 For instance, a function's noexcept-specifier can use the function's
26315 parameters:
26317 struct S {
26318 void fn (int p) noexcept(noexcept(p));
26321 so we need to make sure name lookup can find them. This is used
26322 when we delay parsing of the noexcept-specifier. */
26324 static void
26325 inject_parm_decls (tree decl)
26327 begin_scope (sk_function_parms, decl);
26328 tree args = DECL_ARGUMENTS (decl);
26330 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26332 if (args && is_this_parameter (args))
26334 gcc_checking_assert (current_class_ptr == NULL_TREE);
26335 current_class_ref = cp_build_fold_indirect_ref (args);
26336 current_class_ptr = args;
26340 /* Undo the effects of inject_parm_decls. */
26342 static void
26343 pop_injected_parms (void)
26345 pop_bindings_and_leave_scope ();
26346 current_class_ptr = current_class_ref = NULL_TREE;
26349 /* Parse a class-specifier.
26351 class-specifier:
26352 class-head { member-specification [opt] }
26354 Returns the TREE_TYPE representing the class. */
26356 tree
26357 cp_parser_class_specifier (cp_parser* parser)
26359 auto_timevar tv (TV_PARSE_STRUCT);
26361 tree type;
26362 tree attributes = NULL_TREE;
26363 bool nested_name_specifier_p;
26364 unsigned saved_num_template_parameter_lists;
26365 bool saved_in_function_body;
26366 unsigned char in_statement;
26367 bool in_switch_statement_p;
26368 bool saved_in_unbraced_linkage_specification_p;
26369 tree old_scope = NULL_TREE;
26370 tree scope = NULL_TREE;
26371 cp_token *closing_brace;
26373 push_deferring_access_checks (dk_no_deferred);
26375 /* Parse the class-head. */
26376 type = cp_parser_class_head (parser,
26377 &nested_name_specifier_p);
26378 /* If the class-head was a semantic disaster, skip the entire body
26379 of the class. */
26380 if (!type)
26382 cp_parser_skip_to_end_of_block_or_statement (parser);
26383 pop_deferring_access_checks ();
26384 return error_mark_node;
26387 /* Look for the `{'. */
26388 matching_braces braces;
26389 if (!braces.require_open (parser))
26391 pop_deferring_access_checks ();
26392 return error_mark_node;
26395 cp_ensure_no_omp_declare_simd (parser);
26396 cp_ensure_no_oacc_routine (parser);
26398 /* Issue an error message if type-definitions are forbidden here. */
26399 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26400 /* Remember that we are defining one more class. */
26401 ++parser->num_classes_being_defined;
26402 /* Inside the class, surrounding template-parameter-lists do not
26403 apply. */
26404 saved_num_template_parameter_lists
26405 = parser->num_template_parameter_lists;
26406 parser->num_template_parameter_lists = 0;
26407 /* We are not in a function body. */
26408 saved_in_function_body = parser->in_function_body;
26409 parser->in_function_body = false;
26410 /* Or in a loop. */
26411 in_statement = parser->in_statement;
26412 parser->in_statement = 0;
26413 /* Or in a switch. */
26414 in_switch_statement_p = parser->in_switch_statement_p;
26415 parser->in_switch_statement_p = false;
26416 /* We are not immediately inside an extern "lang" block. */
26417 saved_in_unbraced_linkage_specification_p
26418 = parser->in_unbraced_linkage_specification_p;
26419 parser->in_unbraced_linkage_specification_p = false;
26420 /* 'this' from an enclosing non-static member function is unavailable. */
26421 tree saved_ccp = current_class_ptr;
26422 tree saved_ccr = current_class_ref;
26423 current_class_ptr = NULL_TREE;
26424 current_class_ref = NULL_TREE;
26426 /* Start the class. */
26427 if (nested_name_specifier_p)
26429 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26430 /* SCOPE must be a scope nested inside current scope. */
26431 if (is_nested_namespace (current_namespace,
26432 decl_namespace_context (scope)))
26433 old_scope = push_inner_scope (scope);
26434 else
26435 nested_name_specifier_p = false;
26437 type = begin_class_definition (type);
26439 if (type == error_mark_node)
26440 /* If the type is erroneous, skip the entire body of the class. */
26441 cp_parser_skip_to_closing_brace (parser);
26442 else
26443 /* Parse the member-specification. */
26444 cp_parser_member_specification_opt (parser);
26446 /* Look for the trailing `}'. */
26447 closing_brace = braces.require_close (parser);
26448 /* Look for trailing attributes to apply to this class. */
26449 if (cp_parser_allow_gnu_extensions_p (parser))
26450 attributes = cp_parser_gnu_attributes_opt (parser);
26451 if (type != error_mark_node)
26452 type = finish_struct (type, attributes);
26453 if (nested_name_specifier_p)
26454 pop_inner_scope (old_scope, scope);
26456 /* We've finished a type definition. Check for the common syntax
26457 error of forgetting a semicolon after the definition. We need to
26458 be careful, as we can't just check for not-a-semicolon and be done
26459 with it; the user might have typed:
26461 class X { } c = ...;
26462 class X { } *p = ...;
26464 and so forth. Instead, enumerate all the possible tokens that
26465 might follow this production; if we don't see one of them, then
26466 complain and silently insert the semicolon. */
26468 cp_token *token = cp_lexer_peek_token (parser->lexer);
26469 bool want_semicolon = true;
26471 if (cp_next_tokens_can_be_std_attribute_p (parser))
26472 /* Don't try to parse c++11 attributes here. As per the
26473 grammar, that should be a task for
26474 cp_parser_decl_specifier_seq. */
26475 want_semicolon = false;
26477 switch (token->type)
26479 case CPP_NAME:
26480 case CPP_SEMICOLON:
26481 case CPP_MULT:
26482 case CPP_AND:
26483 case CPP_OPEN_PAREN:
26484 case CPP_CLOSE_PAREN:
26485 case CPP_COMMA:
26486 case CPP_SCOPE:
26487 want_semicolon = false;
26488 break;
26490 /* While it's legal for type qualifiers and storage class
26491 specifiers to follow type definitions in the grammar, only
26492 compiler testsuites contain code like that. Assume that if
26493 we see such code, then what we're really seeing is a case
26494 like:
26496 class X { }
26497 const <type> var = ...;
26501 class Y { }
26502 static <type> func (...) ...
26504 i.e. the qualifier or specifier applies to the next
26505 declaration. To do so, however, we need to look ahead one
26506 more token to see if *that* token is a type specifier.
26508 This code could be improved to handle:
26510 class Z { }
26511 static const <type> var = ...; */
26512 case CPP_KEYWORD:
26513 if (keyword_is_decl_specifier (token->keyword))
26515 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26517 /* Handling user-defined types here would be nice, but very
26518 tricky. */
26519 want_semicolon
26520 = (lookahead->type == CPP_KEYWORD
26521 && keyword_begins_type_specifier (lookahead->keyword));
26523 break;
26524 default:
26525 break;
26528 /* If we don't have a type, then something is very wrong and we
26529 shouldn't try to do anything clever. Likewise for not seeing the
26530 closing brace. */
26531 if (closing_brace && TYPE_P (type) && want_semicolon)
26533 /* Locate the closing brace. */
26534 cp_token_position prev
26535 = cp_lexer_previous_token_position (parser->lexer);
26536 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26537 location_t loc = prev_token->location;
26539 /* We want to suggest insertion of a ';' immediately *after* the
26540 closing brace, so, if we can, offset the location by 1 column. */
26541 location_t next_loc = loc;
26542 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26543 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26545 rich_location richloc (line_table, next_loc);
26547 /* If we successfully offset the location, suggest the fix-it. */
26548 if (next_loc != loc)
26549 richloc.add_fixit_insert_before (next_loc, ";");
26551 if (CLASSTYPE_DECLARED_CLASS (type))
26552 error_at (&richloc,
26553 "expected %<;%> after class definition");
26554 else if (TREE_CODE (type) == RECORD_TYPE)
26555 error_at (&richloc,
26556 "expected %<;%> after struct definition");
26557 else if (TREE_CODE (type) == UNION_TYPE)
26558 error_at (&richloc,
26559 "expected %<;%> after union definition");
26560 else
26561 gcc_unreachable ();
26563 /* Unget one token and smash it to look as though we encountered
26564 a semicolon in the input stream. */
26565 cp_lexer_set_token_position (parser->lexer, prev);
26566 token = cp_lexer_peek_token (parser->lexer);
26567 token->type = CPP_SEMICOLON;
26568 token->keyword = RID_MAX;
26572 /* If this class is not itself within the scope of another class,
26573 then we need to parse the bodies of all of the queued function
26574 definitions. Note that the queued functions defined in a class
26575 are not always processed immediately following the
26576 class-specifier for that class. Consider:
26578 struct A {
26579 struct B { void f() { sizeof (A); } };
26582 If `f' were processed before the processing of `A' were
26583 completed, there would be no way to compute the size of `A'.
26584 Note that the nesting we are interested in here is lexical --
26585 not the semantic nesting given by TYPE_CONTEXT. In particular,
26586 for:
26588 struct A { struct B; };
26589 struct A::B { void f() { } };
26591 there is no need to delay the parsing of `A::B::f'. */
26592 if (--parser->num_classes_being_defined == 0)
26594 tree decl;
26595 tree class_type = NULL_TREE;
26596 tree pushed_scope = NULL_TREE;
26597 unsigned ix;
26598 cp_default_arg_entry *e;
26600 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26602 /* Skip default arguments, NSDMIs, etc, in order to improve
26603 error recovery (c++/71169, c++/71832). */
26604 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26605 vec_safe_truncate (unparsed_nsdmis, 0);
26606 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26609 /* In a first pass, parse default arguments to the functions.
26610 Then, in a second pass, parse the bodies of the functions.
26611 This two-phased approach handles cases like:
26613 struct S {
26614 void f() { g(); }
26615 void g(int i = 3);
26619 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26621 decl = e->decl;
26622 /* If there are default arguments that have not yet been processed,
26623 take care of them now. */
26624 if (class_type != e->class_type)
26626 if (pushed_scope)
26627 pop_scope (pushed_scope);
26628 class_type = e->class_type;
26629 pushed_scope = push_scope (class_type);
26631 /* Make sure that any template parameters are in scope. */
26632 maybe_begin_member_template_processing (decl);
26633 /* Parse the default argument expressions. */
26634 cp_parser_late_parsing_default_args (parser, decl);
26635 /* Remove any template parameters from the symbol table. */
26636 maybe_end_member_template_processing ();
26638 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26640 /* If there are noexcept-specifiers that have not yet been processed,
26641 take care of them now. Do this before processing NSDMIs as they
26642 may depend on noexcept-specifiers already having been processed. */
26643 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26645 tree ctx = DECL_CONTEXT (decl);
26646 if (class_type != ctx)
26648 if (pushed_scope)
26649 pop_scope (pushed_scope);
26650 class_type = ctx;
26651 pushed_scope = push_scope (class_type);
26654 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26655 def_parse = TREE_PURPOSE (def_parse);
26657 /* Make sure that any template parameters are in scope. */
26658 maybe_begin_member_template_processing (decl);
26660 /* Make sure that any member-function parameters are in scope.
26661 This function doesn't expect ccp to be set. */
26662 current_class_ptr = current_class_ref = NULL_TREE;
26663 inject_parm_decls (decl);
26665 /* 'this' is not allowed in static member functions. */
26666 unsigned char local_variables_forbidden_p
26667 = parser->local_variables_forbidden_p;
26668 if (DECL_THIS_STATIC (decl))
26669 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26671 /* Now we can parse the noexcept-specifier. */
26672 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26674 if (spec == error_mark_node)
26675 spec = NULL_TREE;
26677 /* Update the fn's type directly -- it might have escaped
26678 beyond this decl :( */
26679 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26680 /* Update any instantiations we've already created. We must
26681 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26682 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26683 in the pattern. */
26684 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26685 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26686 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26688 /* Restore the state of local_variables_forbidden_p. */
26689 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26691 /* The finish_struct call above performed various override checking,
26692 but it skipped unparsed noexcept-specifier operands. Now that we
26693 have resolved them, check again. */
26694 noexcept_override_late_checks (decl);
26696 /* Remove any member-function parameters from the symbol table. */
26697 pop_injected_parms ();
26699 /* Remove any template parameters from the symbol table. */
26700 maybe_end_member_template_processing ();
26702 vec_safe_truncate (unparsed_noexcepts, 0);
26704 /* Now parse any NSDMIs. */
26705 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26707 tree ctx = type_context_for_name_lookup (decl);
26708 if (class_type != ctx)
26710 if (pushed_scope)
26711 pop_scope (pushed_scope);
26712 class_type = ctx;
26713 pushed_scope = push_scope (class_type);
26715 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26716 cp_parser_late_parsing_nsdmi (parser, decl);
26718 vec_safe_truncate (unparsed_nsdmis, 0);
26720 /* Now contract attributes. */
26721 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26723 tree ctx = DECL_CONTEXT (decl);
26724 if (class_type != ctx)
26726 if (pushed_scope)
26727 pop_scope (pushed_scope);
26728 class_type = ctx;
26729 pushed_scope = push_scope (class_type);
26732 temp_override<tree> cfd(current_function_decl, decl);
26734 /* Make sure that any template parameters are in scope. */
26735 maybe_begin_member_template_processing (decl);
26737 /* Make sure that any member-function parameters are in scope.
26738 This function doesn't expect ccp to be set. */
26739 current_class_ptr = current_class_ref = NULL_TREE;
26740 inject_parm_decls (decl);
26742 /* 'this' is not allowed in static member functions. */
26743 unsigned char local_variables_forbidden_p
26744 = parser->local_variables_forbidden_p;
26745 if (DECL_THIS_STATIC (decl))
26746 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26748 /* Now we can parse contract conditions. */
26749 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26751 if (cxx_contract_attribute_p (a))
26752 cp_parser_late_contract_condition (parser, decl, a);
26755 /* Restore the state of local_variables_forbidden_p. */
26756 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26758 /* Remove any member-function parameters from the symbol table. */
26759 pop_injected_parms ();
26761 /* Remove any template parameters from the symbol table. */
26762 maybe_end_member_template_processing ();
26764 /* Perform any deferred contract matching. */
26765 match_deferred_contracts (decl);
26767 vec_safe_truncate (unparsed_contracts, 0);
26769 current_class_ptr = NULL_TREE;
26770 current_class_ref = NULL_TREE;
26771 if (pushed_scope)
26772 pop_scope (pushed_scope);
26774 /* Now parse the body of the functions. */
26775 if (flag_openmp)
26777 /* OpenMP UDRs need to be parsed before all other functions. */
26778 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26779 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26780 cp_parser_late_parsing_for_member (parser, decl);
26781 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26782 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26783 cp_parser_late_parsing_for_member (parser, decl);
26785 else
26786 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26787 cp_parser_late_parsing_for_member (parser, decl);
26788 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26791 /* Put back any saved access checks. */
26792 pop_deferring_access_checks ();
26794 /* Restore saved state. */
26795 parser->in_switch_statement_p = in_switch_statement_p;
26796 parser->in_statement = in_statement;
26797 parser->in_function_body = saved_in_function_body;
26798 parser->num_template_parameter_lists
26799 = saved_num_template_parameter_lists;
26800 parser->in_unbraced_linkage_specification_p
26801 = saved_in_unbraced_linkage_specification_p;
26802 current_class_ptr = saved_ccp;
26803 current_class_ref = saved_ccr;
26805 return type;
26808 /* Parse a class-head.
26810 class-head:
26811 class-key identifier [opt] base-clause [opt]
26812 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26813 class-key nested-name-specifier [opt] template-id
26814 base-clause [opt]
26816 class-virt-specifier:
26817 final
26819 GNU Extensions:
26820 class-key attributes identifier [opt] base-clause [opt]
26821 class-key attributes nested-name-specifier identifier base-clause [opt]
26822 class-key attributes nested-name-specifier [opt] template-id
26823 base-clause [opt]
26825 Upon return BASES is initialized to the list of base classes (or
26826 NULL, if there are none) in the same form returned by
26827 cp_parser_base_clause.
26829 Returns the TYPE of the indicated class. Sets
26830 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26831 involving a nested-name-specifier was used, and FALSE otherwise.
26833 Returns error_mark_node if this is not a class-head.
26835 Returns NULL_TREE if the class-head is syntactically valid, but
26836 semantically invalid in a way that means we should skip the entire
26837 body of the class. */
26839 static tree
26840 cp_parser_class_head (cp_parser* parser,
26841 bool* nested_name_specifier_p)
26843 tree nested_name_specifier;
26844 enum tag_types class_key;
26845 tree id = NULL_TREE;
26846 tree type = NULL_TREE;
26847 tree attributes;
26848 tree bases;
26849 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26850 bool template_id_p = false;
26851 bool qualified_p = false;
26852 bool invalid_nested_name_p = false;
26853 bool invalid_explicit_specialization_p = false;
26854 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26855 tree pushed_scope = NULL_TREE;
26856 unsigned num_templates;
26857 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26858 /* Assume no nested-name-specifier will be present. */
26859 *nested_name_specifier_p = false;
26860 /* Assume no template parameter lists will be used in defining the
26861 type. */
26862 num_templates = 0;
26863 parser->colon_corrects_to_scope_p = false;
26865 /* Look for the class-key. */
26866 class_key = cp_parser_class_key (parser);
26867 if (class_key == none_type)
26868 return error_mark_node;
26870 location_t class_head_start_location = input_location;
26872 /* Parse the attributes. */
26873 attributes = cp_parser_attributes_opt (parser);
26874 if (find_contract (attributes))
26875 diagnose_misapplied_contracts (attributes);
26877 /* If the next token is `::', that is invalid -- but sometimes
26878 people do try to write:
26880 struct ::S {};
26882 Handle this gracefully by accepting the extra qualifier, and then
26883 issuing an error about it later if this really is a
26884 class-head. If it turns out just to be an elaborated type
26885 specifier, remain silent. */
26886 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26887 qualified_p = true;
26889 /* It is OK to define an inaccessible class; for example:
26891 class A { class B; };
26892 class A::B {};
26894 So we want to ignore access when parsing the class name.
26895 However, we might be tentatively parsing what is really an
26896 elaborated-type-specifier naming a template-id, e.g.
26898 struct C<&D::m> c;
26900 In this case the tentative parse as a class-head will fail, but not
26901 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
26902 Since dk_no_check is sticky, we must instead use dk_deferred so that
26903 any such CPP_TEMPLATE_ID token created during this tentative parse
26904 will correctly capture the access checks imposed by the template-id . */
26905 push_deferring_access_checks (dk_deferred);
26907 /* Determine the name of the class. Begin by looking for an
26908 optional nested-name-specifier. */
26909 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26910 nested_name_specifier
26911 = cp_parser_nested_name_specifier_opt (parser,
26912 /*typename_keyword_p=*/false,
26913 /*check_dependency_p=*/false,
26914 /*type_p=*/true,
26915 /*is_declaration=*/false);
26916 /* If there was a nested-name-specifier, then there *must* be an
26917 identifier. */
26919 cp_token *bad_template_keyword = NULL;
26921 if (nested_name_specifier)
26923 type_start_token = cp_lexer_peek_token (parser->lexer);
26924 /* Although the grammar says `identifier', it really means
26925 `class-name' or `template-name'. You are only allowed to
26926 define a class that has already been declared with this
26927 syntax.
26929 The proposed resolution for Core Issue 180 says that wherever
26930 you see `class T::X' you should treat `X' as a type-name.
26932 We do not know if we will see a class-name, or a
26933 template-name. We look for a class-name first, in case the
26934 class-name is a template-id; if we looked for the
26935 template-name first we would stop after the template-name. */
26936 cp_parser_parse_tentatively (parser);
26937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26938 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26939 type = cp_parser_class_name (parser,
26940 /*typename_keyword_p=*/false,
26941 /*template_keyword_p=*/false,
26942 class_type,
26943 /*check_dependency_p=*/false,
26944 /*class_head_p=*/true,
26945 /*is_declaration=*/false);
26946 /* If that didn't work, ignore the nested-name-specifier. */
26947 if (!cp_parser_parse_definitely (parser))
26949 invalid_nested_name_p = true;
26950 type_start_token = cp_lexer_peek_token (parser->lexer);
26951 id = cp_parser_identifier (parser);
26952 if (id == error_mark_node)
26953 id = NULL_TREE;
26955 /* If we could not find a corresponding TYPE, treat this
26956 declaration like an unqualified declaration. */
26957 if (type == error_mark_node)
26958 nested_name_specifier = NULL_TREE;
26959 /* Otherwise, count the number of templates used in TYPE and its
26960 containing scopes. */
26961 else
26962 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26964 /* Otherwise, the identifier is optional. */
26965 else
26967 /* We don't know whether what comes next is a template-id,
26968 an identifier, or nothing at all. */
26969 cp_parser_parse_tentatively (parser);
26970 /* Check for a template-id. */
26971 type_start_token = cp_lexer_peek_token (parser->lexer);
26972 id = cp_parser_template_id (parser,
26973 /*template_keyword_p=*/false,
26974 /*check_dependency_p=*/true,
26975 class_key,
26976 /*is_declaration=*/true);
26977 /* If that didn't work, it could still be an identifier. */
26978 if (!cp_parser_parse_definitely (parser))
26980 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26982 type_start_token = cp_lexer_peek_token (parser->lexer);
26983 id = cp_parser_identifier (parser);
26985 else
26986 id = NULL_TREE;
26988 else
26990 template_id_p = true;
26991 ++num_templates;
26995 pop_deferring_access_checks ();
26997 if (id)
26999 cp_parser_check_for_invalid_template_id (parser, id,
27000 class_key,
27001 type_start_token->location);
27003 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27005 /* If it's not a `:' or a `{' then we can't really be looking at a
27006 class-head, since a class-head only appears as part of a
27007 class-specifier. We have to detect this situation before calling
27008 xref_tag, since that has irreversible side-effects. */
27009 if (!cp_parser_next_token_starts_class_definition_p (parser))
27011 cp_parser_error (parser, "expected %<{%> or %<:%>");
27012 type = error_mark_node;
27013 goto out;
27016 /* At this point, we're going ahead with the class-specifier, even
27017 if some other problem occurs. */
27018 cp_parser_commit_to_tentative_parse (parser);
27019 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27021 cp_parser_error (parser,
27022 "cannot specify %<override%> for a class");
27023 type = error_mark_node;
27024 goto out;
27026 /* Issue the error about the overly-qualified name now. */
27027 if (qualified_p)
27029 cp_parser_error (parser,
27030 "global qualification of class name is invalid");
27031 type = error_mark_node;
27032 goto out;
27034 else if (invalid_nested_name_p)
27036 cp_parser_error (parser,
27037 "qualified name does not name a class");
27038 type = error_mark_node;
27039 goto out;
27041 else if (nested_name_specifier)
27043 tree scope;
27045 if (bad_template_keyword)
27046 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27047 keyword template shall not appear at the top level. */
27048 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27049 "keyword %<template%> not allowed in class-head-name");
27051 /* Reject typedef-names in class heads. */
27052 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27054 error_at (type_start_token->location,
27055 "invalid class name in declaration of %qD",
27056 type);
27057 type = NULL_TREE;
27058 goto done;
27061 /* Figure out in what scope the declaration is being placed. */
27062 scope = current_scope ();
27063 /* If that scope does not contain the scope in which the
27064 class was originally declared, the program is invalid. */
27065 if (scope && !is_ancestor (scope, nested_name_specifier))
27067 if (at_namespace_scope_p ())
27068 error_at (type_start_token->location,
27069 "declaration of %qD in namespace %qD which does not "
27070 "enclose %qD",
27071 type, scope, nested_name_specifier);
27072 else
27073 error_at (type_start_token->location,
27074 "declaration of %qD in %qD which does not enclose %qD",
27075 type, scope, nested_name_specifier);
27076 type = NULL_TREE;
27077 goto done;
27079 /* [dcl.meaning]
27081 A declarator-id shall not be qualified except for the
27082 definition of a ... nested class outside of its class
27083 ... [or] the definition or explicit instantiation of a
27084 class member of a namespace outside of its namespace. */
27085 if (scope == nested_name_specifier)
27086 permerror (nested_name_specifier_token_start->location,
27087 "extra qualification not allowed");
27089 /* An explicit-specialization must be preceded by "template <>". If
27090 it is not, try to recover gracefully. */
27091 if (at_namespace_scope_p ()
27092 && parser->num_template_parameter_lists == 0
27093 && !processing_template_parmlist
27094 && template_id_p)
27096 /* Build a location of this form:
27097 struct typename <ARGS>
27098 ^~~~~~~~~~~~~~~~~~~~~~
27099 with caret==start at the start token, and
27100 finishing at the end of the type. */
27101 location_t reported_loc
27102 = make_location (class_head_start_location,
27103 class_head_start_location,
27104 get_finish (type_start_token->location));
27105 rich_location richloc (line_table, reported_loc);
27106 richloc.add_fixit_insert_before (class_head_start_location,
27107 "template <> ");
27108 error_at (&richloc,
27109 "an explicit specialization must be preceded by"
27110 " %<template <>%>");
27111 invalid_explicit_specialization_p = true;
27112 /* Take the same action that would have been taken by
27113 cp_parser_explicit_specialization. */
27114 ++parser->num_template_parameter_lists;
27115 begin_specialization ();
27117 /* There must be no "return" statements between this point and the
27118 end of this function; set "type "to the correct return value and
27119 use "goto done;" to return. */
27120 /* Make sure that the right number of template parameters were
27121 present. */
27122 if (!cp_parser_check_template_parameters (parser, num_templates,
27123 template_id_p,
27124 type_start_token->location,
27125 /*declarator=*/NULL))
27127 /* If something went wrong, there is no point in even trying to
27128 process the class-definition. */
27129 type = NULL_TREE;
27130 goto done;
27133 /* Look up the type. */
27134 if (template_id_p)
27136 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27137 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27138 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27140 error_at (type_start_token->location,
27141 "function template %qD redeclared as a class template", id);
27142 type = error_mark_node;
27144 else
27146 type = TREE_TYPE (id);
27147 type = maybe_process_partial_specialization (type);
27149 /* Check the scope while we still know whether or not we had a
27150 nested-name-specifier. */
27151 if (type != error_mark_node)
27152 check_unqualified_spec_or_inst (type, type_start_token->location);
27154 if (nested_name_specifier)
27155 pushed_scope = push_scope (nested_name_specifier);
27157 else if (nested_name_specifier)
27159 type = TREE_TYPE (type);
27161 /* Given:
27163 template <typename T> struct S { struct T };
27164 template <typename T> struct S<T>::T { };
27166 we will get a TYPENAME_TYPE when processing the definition of
27167 `S::T'. We need to resolve it to the actual type before we
27168 try to define it. */
27169 if (TREE_CODE (type) == TYPENAME_TYPE)
27171 type = resolve_typename_type (type, /*only_current_p=*/false);
27172 if (TREE_CODE (type) == TYPENAME_TYPE)
27174 cp_parser_error (parser, "could not resolve typename type");
27175 type = error_mark_node;
27179 type = maybe_process_partial_specialization (type);
27180 if (type == error_mark_node)
27182 type = NULL_TREE;
27183 goto done;
27186 /* Enter the scope indicated by the nested-name-specifier. */
27187 pushed_scope = push_scope (nested_name_specifier);
27188 /* Get the canonical version of this type. */
27189 type = TYPE_MAIN_DECL (type);
27190 /* Call push_template_decl if it seems like we should be defining a
27191 template either from the template headers or the type we're
27192 defining, so that we diagnose both extra and missing headers. */
27193 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27194 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27195 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27197 type = push_template_decl (type);
27198 if (type == error_mark_node)
27200 type = NULL_TREE;
27201 goto done;
27205 type = TREE_TYPE (type);
27206 *nested_name_specifier_p = true;
27208 else /* The name is not a nested name. */
27210 /* If the class was unnamed, create a dummy name. */
27211 if (!id)
27212 id = make_anon_name ();
27213 TAG_how how = (parser->in_type_id_in_expr_p
27214 ? TAG_how::INNERMOST_NON_CLASS
27215 : TAG_how::CURRENT_ONLY);
27216 type = xref_tag (class_key, id, how,
27217 parser->num_template_parameter_lists);
27220 /* Diagnose class/struct/union mismatches. */
27221 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27222 true, true);
27224 /* Indicate whether this class was declared as a `class' or as a
27225 `struct'. */
27226 if (TREE_CODE (type) == RECORD_TYPE)
27227 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27229 /* If this type was already complete, and we see another definition,
27230 that's an error. Likewise if the type is already being defined:
27231 this can happen, eg, when it's defined from within an expression
27232 (c++/84605). */
27233 if (type != error_mark_node
27234 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27236 error_at (type_start_token->location, "redefinition of %q#T",
27237 type);
27238 inform (location_of (type), "previous definition of %q#T",
27239 type);
27240 type = NULL_TREE;
27241 goto done;
27243 else if (type == error_mark_node)
27244 type = NULL_TREE;
27246 if (type)
27248 if (current_lambda_expr ()
27249 && uses_parameter_packs (attributes))
27251 /* In a lambda this should work, but doesn't currently. */
27252 sorry ("unexpanded parameter pack in local class in lambda");
27253 attributes = NULL_TREE;
27256 /* Apply attributes now, before any use of the class as a template
27257 argument in its base list. */
27258 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27259 fixup_attribute_variants (type);
27262 /* Associate constraints with the type. */
27263 if (flag_concepts)
27264 type = associate_classtype_constraints (type);
27266 /* We will have entered the scope containing the class; the names of
27267 base classes should be looked up in that context. For example:
27269 struct A { struct B {}; struct C; };
27270 struct A::C : B {};
27272 is valid. */
27274 /* Get the list of base-classes, if there is one. Defer access checking
27275 until the entire list has been seen, as per [class.access.general]. */
27276 push_deferring_access_checks (dk_deferred);
27277 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27279 if (type)
27280 pushclass (type);
27281 bases = cp_parser_base_clause (parser);
27282 if (type)
27283 popclass ();
27285 else
27286 bases = NULL_TREE;
27288 /* If we're really defining a class, process the base classes.
27289 If they're invalid, fail. */
27290 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27291 xref_basetypes (type, bases);
27293 /* Now that all bases have been seen and attached to the class, check
27294 accessibility of the types named in the base-clause. This must be
27295 done relative to the class scope, so that we accept e.g.
27297 struct A { protected: struct B {}; };
27298 struct C : A::B, A {}; // OK: A::B is accessible via base A
27300 as per [class.access.general]. */
27301 if (type)
27302 pushclass (type);
27303 pop_to_parent_deferring_access_checks ();
27304 if (type)
27305 popclass ();
27307 done:
27308 /* Leave the scope given by the nested-name-specifier. We will
27309 enter the class scope itself while processing the members. */
27310 if (pushed_scope)
27311 pop_scope (pushed_scope);
27313 if (invalid_explicit_specialization_p)
27315 end_specialization ();
27316 --parser->num_template_parameter_lists;
27319 if (type)
27320 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27321 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27322 CLASSTYPE_FINAL (type) = 1;
27323 out:
27324 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27325 return type;
27328 /* Parse a class-key.
27330 class-key:
27331 class
27332 struct
27333 union
27335 Returns the kind of class-key specified, or none_type to indicate
27336 error. */
27338 static enum tag_types
27339 cp_parser_class_key (cp_parser* parser)
27341 cp_token *token;
27342 enum tag_types tag_type;
27344 /* Look for the class-key. */
27345 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27346 if (!token)
27347 return none_type;
27349 /* Check to see if the TOKEN is a class-key. */
27350 tag_type = cp_parser_token_is_class_key (token);
27351 if (!tag_type)
27352 cp_parser_error (parser, "expected class-key");
27353 return tag_type;
27356 /* Parse a type-parameter-key.
27358 type-parameter-key:
27359 class
27360 typename
27363 static void
27364 cp_parser_type_parameter_key (cp_parser* parser)
27366 /* Look for the type-parameter-key. */
27367 enum tag_types tag_type = none_type;
27368 cp_token *token = cp_lexer_peek_token (parser->lexer);
27369 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27371 cp_lexer_consume_token (parser->lexer);
27372 if (pedantic && tag_type == typename_type
27373 && cxx_dialect < cxx17)
27374 /* typename is not allowed in a template template parameter
27375 by the standard until C++17. */
27376 pedwarn (token->location, OPT_Wc__17_extensions,
27377 "ISO C++ forbids typename key in template template parameter;"
27378 " use %<-std=c++17%> or %<-std=gnu++17%>");
27380 else
27381 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27383 return;
27386 /* Parse an (optional) member-specification.
27388 member-specification:
27389 member-declaration member-specification [opt]
27390 access-specifier : member-specification [opt] */
27392 static void
27393 cp_parser_member_specification_opt (cp_parser* parser)
27395 while (true)
27397 cp_token *token;
27398 enum rid keyword;
27400 /* Peek at the next token. */
27401 token = cp_lexer_peek_token (parser->lexer);
27402 /* If it's a `}', or EOF then we've seen all the members. */
27403 if (token->type == CPP_CLOSE_BRACE
27404 || token->type == CPP_EOF
27405 || token->type == CPP_PRAGMA_EOL)
27406 break;
27408 /* See if this token is a keyword. */
27409 keyword = token->keyword;
27410 switch (keyword)
27412 case RID_PUBLIC:
27413 case RID_PROTECTED:
27414 case RID_PRIVATE:
27415 /* Consume the access-specifier. */
27416 cp_lexer_consume_token (parser->lexer);
27417 /* Remember which access-specifier is active. */
27418 current_access_specifier = token->u.value;
27419 /* Look for the `:'. */
27420 cp_parser_require (parser, CPP_COLON, RT_COLON);
27421 break;
27423 default:
27424 /* Accept #pragmas at class scope. */
27425 if (token->type == CPP_PRAGMA)
27427 cp_parser_pragma (parser, pragma_member, NULL);
27428 break;
27431 /* Otherwise, the next construction must be a
27432 member-declaration. */
27433 cp_parser_member_declaration (parser);
27438 /* Parse a member-declaration.
27440 member-declaration:
27441 decl-specifier-seq [opt] member-declarator-list [opt] ;
27442 function-definition ; [opt]
27443 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27444 using-declaration
27445 template-declaration
27446 alias-declaration
27448 member-declarator-list:
27449 member-declarator
27450 member-declarator-list , member-declarator
27452 member-declarator:
27453 declarator pure-specifier [opt]
27454 declarator constant-initializer [opt]
27455 identifier [opt] : constant-expression
27457 GNU Extensions:
27459 member-declaration:
27460 __extension__ member-declaration
27462 member-declarator:
27463 declarator attributes [opt] pure-specifier [opt]
27464 declarator attributes [opt] constant-initializer [opt]
27465 identifier [opt] attributes [opt] : constant-expression
27467 C++0x Extensions:
27469 member-declaration:
27470 static_assert-declaration */
27472 static void
27473 cp_parser_member_declaration (cp_parser* parser)
27475 cp_decl_specifier_seq decl_specifiers;
27476 tree prefix_attributes;
27477 tree decl;
27478 int declares_class_or_enum;
27479 bool friend_p;
27480 cp_token *token = NULL;
27481 cp_token *decl_spec_token_start = NULL;
27482 cp_token *initializer_token_start = NULL;
27483 int saved_pedantic;
27484 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27486 /* Check for the `__extension__' keyword. */
27487 if (cp_parser_extension_opt (parser, &saved_pedantic))
27489 /* Recurse. */
27490 cp_parser_member_declaration (parser);
27491 /* Restore the old value of the PEDANTIC flag. */
27492 pedantic = saved_pedantic;
27494 return;
27497 /* Check for a template-declaration. */
27498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27500 /* An explicit specialization here is an error condition, and we
27501 expect the specialization handler to detect and report this. */
27502 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27503 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27504 cp_parser_explicit_specialization (parser);
27505 else
27506 cp_parser_template_declaration (parser, /*member_p=*/true);
27508 return;
27510 /* Check for a template introduction. */
27511 else if (cp_parser_template_declaration_after_export (parser, true))
27512 return;
27514 /* Check for a using-declaration. */
27515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27517 if (cxx_dialect < cxx11)
27518 /* Parse the using-declaration. */
27519 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27520 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27521 cp_parser_using_enum (parser);
27522 else
27524 tree decl;
27525 bool alias_decl_expected;
27526 cp_parser_parse_tentatively (parser);
27527 decl = cp_parser_alias_declaration (parser);
27528 /* Note that if we actually see the '=' token after the
27529 identifier, cp_parser_alias_declaration commits the
27530 tentative parse. In that case, we really expect an
27531 alias-declaration. Otherwise, we expect a using
27532 declaration. */
27533 alias_decl_expected =
27534 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27535 cp_parser_parse_definitely (parser);
27537 if (alias_decl_expected)
27538 finish_member_declaration (decl);
27539 else
27540 cp_parser_using_declaration (parser,
27541 /*access_declaration_p=*/false);
27543 return;
27546 /* Check for @defs. */
27547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27549 tree ivar, member;
27550 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27551 ivar = ivar_chains;
27552 while (ivar)
27554 member = ivar;
27555 ivar = TREE_CHAIN (member);
27556 TREE_CHAIN (member) = NULL_TREE;
27557 finish_member_declaration (member);
27559 return;
27562 /* If the next token is `static_assert' we have a static assertion. */
27563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27565 cp_parser_static_assert (parser, /*member_p=*/true);
27566 return;
27569 parser->colon_corrects_to_scope_p = false;
27571 cp_omp_declare_simd_data odsd;
27572 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27573 goto out;
27575 /* Parse the decl-specifier-seq. */
27576 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27577 cp_parser_decl_specifier_seq (parser,
27578 (CP_PARSER_FLAGS_OPTIONAL
27579 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27580 &decl_specifiers,
27581 &declares_class_or_enum);
27583 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27584 cp_parser_handle_directive_omp_attributes (parser,
27585 &decl_specifiers.attributes,
27586 &odsd, true);
27588 /* Check for an invalid type-name. */
27589 if (!decl_specifiers.any_type_specifiers_p
27590 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27591 goto out;
27592 /* If there is no declarator, then the decl-specifier-seq should
27593 specify a type. */
27594 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27596 /* If there was no decl-specifier-seq, and the next token is a
27597 `;', then we have something like:
27599 struct S { ; };
27601 [class.mem]
27603 Each member-declaration shall declare at least one member
27604 name of the class. */
27605 if (!decl_specifiers.any_specifiers_p)
27607 cp_token *token = cp_lexer_peek_token (parser->lexer);
27608 if (!in_system_header_at (token->location))
27610 gcc_rich_location richloc (token->location);
27611 richloc.add_fixit_remove ();
27612 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27615 else
27617 /* See if this declaration is a friend. */
27618 friend_p = cp_parser_friend_p (&decl_specifiers);
27619 /* If there were decl-specifiers, check to see if there was
27620 a class-declaration. */
27621 tree type = check_tag_decl (&decl_specifiers,
27622 /*explicit_type_instantiation_p=*/false);
27623 /* Nested classes have already been added to the class, but
27624 a `friend' needs to be explicitly registered. */
27625 if (friend_p)
27627 /* If the `friend' keyword was present, the friend must
27628 be introduced with a class-key. */
27629 if (!declares_class_or_enum && cxx_dialect < cxx11)
27630 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27631 "in C++03 a class-key must be used "
27632 "when declaring a friend");
27633 /* In this case:
27635 template <typename T> struct A {
27636 friend struct A<T>::B;
27639 A<T>::B will be represented by a TYPENAME_TYPE, and
27640 therefore not recognized by check_tag_decl. */
27641 if (!type)
27643 type = decl_specifiers.type;
27644 if (type && TREE_CODE (type) == TYPE_DECL)
27645 type = TREE_TYPE (type);
27647 /* Warn if an attribute cannot appear here, as per
27648 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27649 we ignore attributes in elaborated-type-specifiers. */
27650 if (!declares_class_or_enum
27651 && cxx11_attribute_p (decl_specifiers.attributes))
27653 decl_specifiers.attributes = NULL_TREE;
27654 if (warning_at (decl_spec_token_start->location,
27655 OPT_Wattributes, "attribute ignored"))
27656 inform (decl_spec_token_start->location, "an attribute "
27657 "that appertains to a friend declaration that "
27658 "is not a definition is ignored");
27660 if (!type || !TYPE_P (type))
27661 error_at (decl_spec_token_start->location,
27662 "friend declaration does not name a class or "
27663 "function");
27664 else
27665 make_friend_class (current_class_type, type,
27666 /*complain=*/true);
27668 /* If there is no TYPE, an error message will already have
27669 been issued. */
27670 else if (!type || type == error_mark_node)
27672 /* An anonymous aggregate has to be handled specially; such
27673 a declaration really declares a data member (with a
27674 particular type), as opposed to a nested class. */
27675 else if (ANON_AGGR_TYPE_P (type))
27677 /* C++11 9.5/6. */
27678 if (decl_specifiers.storage_class != sc_none)
27679 error_at (decl_spec_token_start->location,
27680 "a storage class on an anonymous aggregate "
27681 "in class scope is not allowed");
27683 /* Remove constructors and such from TYPE, now that we
27684 know it is an anonymous aggregate. */
27685 fixup_anonymous_aggr (type);
27686 /* And make the corresponding data member. */
27687 decl = build_decl (decl_spec_token_start->location,
27688 FIELD_DECL, NULL_TREE, type);
27689 /* Add it to the class. */
27690 finish_member_declaration (decl);
27692 else
27693 cp_parser_check_access_in_redeclaration
27694 (TYPE_NAME (type),
27695 decl_spec_token_start->location);
27698 else
27700 bool assume_semicolon = false;
27702 /* Clear attributes from the decl_specifiers but keep them
27703 around as prefix attributes that apply them to the entity
27704 being declared. */
27705 prefix_attributes = decl_specifiers.attributes;
27706 decl_specifiers.attributes = NULL_TREE;
27707 if (parser->omp_declare_simd
27708 && (parser->omp_declare_simd->attribs[0]
27709 == &decl_specifiers.attributes))
27710 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27712 /* See if these declarations will be friends. */
27713 friend_p = cp_parser_friend_p (&decl_specifiers);
27715 /* Keep going until we hit the `;' at the end of the
27716 declaration. */
27717 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27719 tree attributes = NULL_TREE;
27720 tree first_attribute;
27721 tree initializer;
27722 bool named_bitfld = false;
27724 /* Peek at the next token. */
27725 token = cp_lexer_peek_token (parser->lexer);
27727 /* The following code wants to know early if it is a bit-field
27728 or some other declaration. Attributes can appear before
27729 the `:' token. Skip over them without consuming any tokens
27730 to peek if they are followed by `:'. */
27731 if (cp_next_tokens_can_be_attribute_p (parser)
27732 || (token->type == CPP_NAME
27733 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27734 && (named_bitfld = true)))
27736 size_t n
27737 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27738 token = cp_lexer_peek_nth_token (parser->lexer, n);
27741 /* Check for a bitfield declaration. */
27742 if (token->type == CPP_COLON
27743 || (token->type == CPP_NAME
27744 && token == cp_lexer_peek_token (parser->lexer)
27745 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27746 && (named_bitfld = true)))
27748 tree identifier;
27749 tree width;
27750 tree late_attributes = NULL_TREE;
27751 location_t id_location
27752 = cp_lexer_peek_token (parser->lexer)->location;
27754 if (named_bitfld)
27755 identifier = cp_parser_identifier (parser);
27756 else
27757 identifier = NULL_TREE;
27759 /* Look for attributes that apply to the bitfield. */
27760 attributes = cp_parser_attributes_opt (parser);
27762 /* Consume the `:' token. */
27763 cp_lexer_consume_token (parser->lexer);
27765 /* Get the width of the bitfield. */
27766 width = cp_parser_constant_expression (parser, false, NULL,
27767 cxx_dialect >= cxx11);
27769 /* In C++20 and as extension for C++11 and above we allow
27770 default member initializers for bit-fields. */
27771 initializer = NULL_TREE;
27772 if (cxx_dialect >= cxx11
27773 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27774 || cp_lexer_next_token_is (parser->lexer,
27775 CPP_OPEN_BRACE)))
27777 location_t loc
27778 = cp_lexer_peek_token (parser->lexer)->location;
27779 if (cxx_dialect < cxx20
27780 && identifier != NULL_TREE)
27781 pedwarn (loc, OPT_Wc__20_extensions,
27782 "default member initializers for bit-fields "
27783 "only available with %<-std=c++20%> or "
27784 "%<-std=gnu++20%>");
27786 initializer = cp_parser_save_nsdmi (parser);
27787 if (identifier == NULL_TREE)
27789 error_at (loc, "default member initializer for "
27790 "unnamed bit-field");
27791 initializer = NULL_TREE;
27794 else
27796 /* Look for attributes that apply to the bitfield after
27797 the `:' token and width. This is where GCC used to
27798 parse attributes in the past, pedwarn if there is
27799 a std attribute. */
27800 if (cp_next_tokens_can_be_std_attribute_p (parser))
27801 pedwarn (input_location, OPT_Wpedantic,
27802 "ISO C++ allows bit-field attributes only "
27803 "before the %<:%> token");
27805 late_attributes = cp_parser_attributes_opt (parser);
27808 attributes = attr_chainon (attributes, late_attributes);
27810 /* Remember which attributes are prefix attributes and
27811 which are not. */
27812 first_attribute = attributes;
27813 /* Combine the attributes. */
27814 attributes = attr_chainon (prefix_attributes, attributes);
27816 /* Create the bitfield declaration. */
27817 decl = grokbitfield (identifier
27818 ? make_id_declarator (NULL_TREE,
27819 identifier,
27820 sfk_none,
27821 id_location)
27822 : NULL,
27823 &decl_specifiers,
27824 width, initializer,
27825 attributes);
27827 else
27829 cp_declarator *declarator;
27830 tree asm_specification;
27831 int ctor_dtor_or_conv_p;
27832 bool static_p = (decl_specifiers.storage_class == sc_static);
27833 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27834 /* We can't delay parsing for friends,
27835 alias-declarations, and typedefs, even though the
27836 standard seems to require it. */
27837 if (!friend_p
27838 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27839 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27841 /* Parse the declarator. */
27842 declarator
27843 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27844 flags,
27845 &ctor_dtor_or_conv_p,
27846 /*parenthesized_p=*/NULL,
27847 /*member_p=*/true,
27848 friend_p, static_p);
27850 /* If something went wrong parsing the declarator, make sure
27851 that we at least consume some tokens. */
27852 if (declarator == cp_error_declarator)
27854 /* Skip to the end of the statement. */
27855 cp_parser_skip_to_end_of_statement (parser);
27856 /* If the next token is not a semicolon, that is
27857 probably because we just skipped over the body of
27858 a function. So, we consume a semicolon if
27859 present, but do not issue an error message if it
27860 is not present. */
27861 if (cp_lexer_next_token_is (parser->lexer,
27862 CPP_SEMICOLON))
27863 cp_lexer_consume_token (parser->lexer);
27864 goto out;
27867 /* Handle class-scope non-template C++17 deduction guides. */
27868 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27869 &decl_specifiers,
27870 declarator,
27871 &ctor_dtor_or_conv_p);
27873 if (declares_class_or_enum & 2)
27874 cp_parser_check_for_definition_in_return_type
27875 (declarator, decl_specifiers.type,
27876 decl_specifiers.locations[ds_type_spec]);
27878 /* Look for an asm-specification. */
27879 asm_specification = cp_parser_asm_specification_opt (parser);
27880 /* Look for attributes that apply to the declaration. */
27881 attributes = cp_parser_attributes_opt (parser);
27882 /* Remember which attributes are prefix attributes and
27883 which are not. */
27884 first_attribute = attributes;
27885 /* Combine the attributes. */
27886 attributes = attr_chainon (prefix_attributes, attributes);
27888 /* If it's an `=', then we have a constant-initializer or a
27889 pure-specifier. It is not correct to parse the
27890 initializer before registering the member declaration
27891 since the member declaration should be in scope while
27892 its initializer is processed. However, the rest of the
27893 front end does not yet provide an interface that allows
27894 us to handle this correctly. */
27895 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27897 /* In [class.mem]:
27899 A pure-specifier shall be used only in the declaration of
27900 a virtual function.
27902 A member-declarator can contain a constant-initializer
27903 only if it declares a static member of integral or
27904 enumeration type.
27906 Therefore, if the DECLARATOR is for a function, we look
27907 for a pure-specifier; otherwise, we look for a
27908 constant-initializer. When we call `grokfield', it will
27909 perform more stringent semantics checks. */
27910 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27911 declarator->init_loc = initializer_token_start->location;
27912 if (function_declarator_p (declarator)
27913 || (decl_specifiers.type
27914 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27915 && declarator->kind == cdk_id
27916 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27917 == FUNCTION_TYPE)))
27918 initializer = cp_parser_pure_specifier (parser);
27919 else if (decl_specifiers.storage_class != sc_static)
27920 initializer = cp_parser_save_nsdmi (parser);
27921 else if (cxx_dialect >= cxx11)
27923 /* Don't require a constant rvalue in C++11, since we
27924 might want a reference constant. We'll enforce
27925 constancy later. */
27926 cp_lexer_consume_token (parser->lexer);
27927 /* Parse the initializer. */
27928 initializer = cp_parser_initializer_clause (parser);
27930 else
27931 /* Parse the initializer. */
27932 initializer = cp_parser_constant_initializer (parser);
27934 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27935 && !function_declarator_p (declarator))
27937 declarator->init_loc
27938 = cp_lexer_peek_token (parser->lexer)->location;
27939 if (decl_specifiers.storage_class != sc_static)
27940 initializer = cp_parser_save_nsdmi (parser);
27941 else
27942 initializer = cp_parser_initializer (parser);
27944 /* Detect invalid bit-field cases such as
27946 int *p : 4;
27947 int &&r : 3;
27949 and similar. */
27950 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27951 /* If there were no type specifiers, it was a
27952 constructor. */
27953 && decl_specifiers.any_type_specifiers_p)
27955 /* This is called for a decent diagnostic only. */
27956 tree d = grokdeclarator (declarator, &decl_specifiers,
27957 BITFIELD, /*initialized=*/false,
27958 &attributes);
27959 if (!error_operand_p (d))
27960 error_at (DECL_SOURCE_LOCATION (d),
27961 "bit-field %qD has non-integral type %qT",
27962 d, TREE_TYPE (d));
27963 cp_parser_skip_to_end_of_statement (parser);
27964 /* Avoid "extra ;" pedwarns. */
27965 if (cp_lexer_next_token_is (parser->lexer,
27966 CPP_SEMICOLON))
27967 cp_lexer_consume_token (parser->lexer);
27968 goto out;
27970 /* Otherwise, there is no initializer. */
27971 else
27972 initializer = NULL_TREE;
27974 /* See if we are probably looking at a function
27975 definition. We are certainly not looking at a
27976 member-declarator. Calling `grokfield' has
27977 side-effects, so we must not do it unless we are sure
27978 that we are looking at a member-declarator. */
27979 if (cp_parser_token_starts_function_definition_p
27980 (cp_lexer_peek_token (parser->lexer)))
27982 /* The grammar does not allow a pure-specifier to be
27983 used when a member function is defined. (It is
27984 possible that this fact is an oversight in the
27985 standard, since a pure function may be defined
27986 outside of the class-specifier. */
27987 if (initializer && initializer_token_start)
27988 error_at (initializer_token_start->location,
27989 "pure-specifier on function-definition");
27990 decl = cp_parser_save_member_function_body (parser,
27991 &decl_specifiers,
27992 declarator,
27993 attributes);
27995 if (parser->fully_implicit_function_template_p)
27996 decl = finish_fully_implicit_template (parser, decl);
27997 /* If the member was not a friend, declare it here. */
27998 if (!friend_p)
27999 finish_member_declaration (decl);
28000 /* Peek at the next token. */
28001 token = cp_lexer_peek_token (parser->lexer);
28002 /* If the next token is a semicolon, consume it. */
28003 if (token->type == CPP_SEMICOLON)
28005 location_t semicolon_loc
28006 = cp_lexer_consume_token (parser->lexer)->location;
28007 gcc_rich_location richloc (semicolon_loc);
28008 richloc.add_fixit_remove ();
28009 warning_at (&richloc, OPT_Wextra_semi,
28010 "extra %<;%> after in-class "
28011 "function definition");
28013 goto out;
28015 else
28016 if (declarator->kind == cdk_function)
28017 declarator->id_loc = token->location;
28019 /* Create the declaration. */
28020 decl = grokfield (declarator, &decl_specifiers,
28021 initializer, /*init_const_expr_p=*/true,
28022 asm_specification, attributes);
28024 if (parser->fully_implicit_function_template_p)
28026 if (friend_p)
28027 finish_fully_implicit_template (parser, 0);
28028 else
28029 decl = finish_fully_implicit_template (parser, decl);
28033 cp_finalize_omp_declare_simd (parser, decl);
28034 cp_finalize_oacc_routine (parser, decl, false);
28036 /* Reset PREFIX_ATTRIBUTES. */
28037 if (attributes != error_mark_node)
28039 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28040 attributes = TREE_CHAIN (attributes);
28041 if (attributes)
28042 TREE_CHAIN (attributes) = NULL_TREE;
28045 /* If there is any qualification still in effect, clear it
28046 now; we will be starting fresh with the next declarator. */
28047 parser->scope = NULL_TREE;
28048 parser->qualifying_scope = NULL_TREE;
28049 parser->object_scope = NULL_TREE;
28050 /* If it's a `,', then there are more declarators. */
28051 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28053 cp_lexer_consume_token (parser->lexer);
28054 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28056 cp_token *token = cp_lexer_previous_token (parser->lexer);
28057 gcc_rich_location richloc (token->location);
28058 richloc.add_fixit_remove ();
28059 error_at (&richloc, "stray %<,%> at end of "
28060 "member declaration");
28063 /* If the next token isn't a `;', then we have a parse error. */
28064 else if (cp_lexer_next_token_is_not (parser->lexer,
28065 CPP_SEMICOLON))
28067 /* The next token might be a ways away from where the
28068 actual semicolon is missing. Find the previous token
28069 and use that for our error position. */
28070 cp_token *token = cp_lexer_previous_token (parser->lexer);
28071 gcc_rich_location richloc (token->location);
28072 richloc.add_fixit_insert_after (";");
28073 error_at (&richloc, "expected %<;%> at end of "
28074 "member declaration");
28076 /* Assume that the user meant to provide a semicolon. If
28077 we were to cp_parser_skip_to_end_of_statement, we might
28078 skip to a semicolon inside a member function definition
28079 and issue nonsensical error messages. */
28080 assume_semicolon = true;
28083 if (decl)
28085 /* Add DECL to the list of members. */
28086 if (!friend_p
28087 /* Explicitly include, eg, NSDMIs, for better error
28088 recovery (c++/58650). */
28089 || !DECL_DECLARES_FUNCTION_P (decl))
28090 finish_member_declaration (decl);
28092 if (DECL_DECLARES_FUNCTION_P (decl))
28093 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28094 else if (TREE_CODE (decl) == FIELD_DECL
28095 && DECL_INITIAL (decl))
28096 /* Add DECL to the queue of NSDMI to be parsed later. */
28097 vec_safe_push (unparsed_nsdmis, decl);
28100 if (assume_semicolon)
28101 goto out;
28105 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28106 out:
28107 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28108 cp_finalize_omp_declare_simd (parser, &odsd);
28111 /* Parse a pure-specifier.
28113 pure-specifier:
28116 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28117 Otherwise, ERROR_MARK_NODE is returned. */
28119 static tree
28120 cp_parser_pure_specifier (cp_parser* parser)
28122 cp_token *token;
28124 /* Look for the `=' token. */
28125 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28126 return error_mark_node;
28127 /* Look for the `0' token. */
28128 token = cp_lexer_peek_token (parser->lexer);
28130 if (token->type == CPP_EOF
28131 || token->type == CPP_PRAGMA_EOL)
28132 return error_mark_node;
28134 cp_lexer_consume_token (parser->lexer);
28136 /* Accept = default or = delete in c++0x mode. */
28137 if (token->keyword == RID_DEFAULT
28138 || token->keyword == RID_DELETE)
28140 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28141 return token->u.value;
28144 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28145 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28147 cp_parser_error (parser,
28148 "invalid pure specifier (only %<= 0%> is allowed)");
28149 cp_parser_skip_to_end_of_statement (parser);
28150 return error_mark_node;
28152 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28154 error_at (token->location, "templates may not be %<virtual%>");
28155 return error_mark_node;
28158 return integer_zero_node;
28161 /* Parse a constant-initializer.
28163 constant-initializer:
28164 = constant-expression
28166 Returns a representation of the constant-expression. */
28168 static tree
28169 cp_parser_constant_initializer (cp_parser* parser)
28171 /* Look for the `=' token. */
28172 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28173 return error_mark_node;
28175 /* It is invalid to write:
28177 struct S { static const int i = { 7 }; };
28180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28182 cp_parser_error (parser,
28183 "a brace-enclosed initializer is not allowed here");
28184 /* Consume the opening brace. */
28185 matching_braces braces;
28186 braces.consume_open (parser);
28187 /* Skip the initializer. */
28188 cp_parser_skip_to_closing_brace (parser);
28189 /* Look for the trailing `}'. */
28190 braces.require_close (parser);
28192 return error_mark_node;
28195 return cp_parser_constant_expression (parser);
28198 /* Derived classes [gram.class.derived] */
28200 /* Parse a base-clause.
28202 base-clause:
28203 : base-specifier-list
28205 base-specifier-list:
28206 base-specifier ... [opt]
28207 base-specifier-list , base-specifier ... [opt]
28209 Returns a TREE_LIST representing the base-classes, in the order in
28210 which they were declared. The representation of each node is as
28211 described by cp_parser_base_specifier.
28213 In the case that no bases are specified, this function will return
28214 NULL_TREE, not ERROR_MARK_NODE. */
28216 static tree
28217 cp_parser_base_clause (cp_parser* parser)
28219 tree bases = NULL_TREE;
28221 /* Look for the `:' that begins the list. */
28222 cp_parser_require (parser, CPP_COLON, RT_COLON);
28224 /* Scan the base-specifier-list. */
28225 while (true)
28227 cp_token *token;
28228 tree base;
28229 bool pack_expansion_p = false;
28231 /* Look for the base-specifier. */
28232 base = cp_parser_base_specifier (parser);
28233 /* Look for the (optional) ellipsis. */
28234 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28236 /* Consume the `...'. */
28237 cp_lexer_consume_token (parser->lexer);
28239 pack_expansion_p = true;
28242 /* Add BASE to the front of the list. */
28243 if (base && base != error_mark_node)
28245 if (pack_expansion_p)
28246 /* Make this a pack expansion type. */
28247 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28249 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28251 TREE_CHAIN (base) = bases;
28252 bases = base;
28255 /* Peek at the next token. */
28256 token = cp_lexer_peek_token (parser->lexer);
28257 /* If it's not a comma, then the list is complete. */
28258 if (token->type != CPP_COMMA)
28259 break;
28260 /* Consume the `,'. */
28261 cp_lexer_consume_token (parser->lexer);
28264 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28265 base class had a qualified name. However, the next name that
28266 appears is certainly not qualified. */
28267 parser->scope = NULL_TREE;
28268 parser->qualifying_scope = NULL_TREE;
28269 parser->object_scope = NULL_TREE;
28271 return nreverse (bases);
28274 /* Parse a base-specifier.
28276 base-specifier:
28277 :: [opt] nested-name-specifier [opt] class-name
28278 virtual access-specifier [opt] :: [opt] nested-name-specifier
28279 [opt] class-name
28280 access-specifier virtual [opt] :: [opt] nested-name-specifier
28281 [opt] class-name
28283 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28284 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28285 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28286 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28288 static tree
28289 cp_parser_base_specifier (cp_parser* parser)
28291 cp_token *token;
28292 bool done = false;
28293 bool virtual_p = false;
28294 bool duplicate_virtual_error_issued_p = false;
28295 bool duplicate_access_error_issued_p = false;
28296 bool class_scope_p, template_p;
28297 tree access = access_default_node;
28298 tree type;
28300 /* Process the optional `virtual' and `access-specifier'. */
28301 while (!done)
28303 /* Peek at the next token. */
28304 token = cp_lexer_peek_token (parser->lexer);
28305 /* Process `virtual'. */
28306 switch (token->keyword)
28308 case RID_VIRTUAL:
28309 /* If `virtual' appears more than once, issue an error. */
28310 if (virtual_p && !duplicate_virtual_error_issued_p)
28312 cp_parser_error (parser,
28313 "%<virtual%> specified more than once in base-specifier");
28314 duplicate_virtual_error_issued_p = true;
28317 virtual_p = true;
28319 /* Consume the `virtual' token. */
28320 cp_lexer_consume_token (parser->lexer);
28322 break;
28324 case RID_PUBLIC:
28325 case RID_PROTECTED:
28326 case RID_PRIVATE:
28327 /* If more than one access specifier appears, issue an
28328 error. */
28329 if (access != access_default_node
28330 && !duplicate_access_error_issued_p)
28332 cp_parser_error (parser,
28333 "more than one access specifier in base-specifier");
28334 duplicate_access_error_issued_p = true;
28337 access = ridpointers[(int) token->keyword];
28339 /* Consume the access-specifier. */
28340 cp_lexer_consume_token (parser->lexer);
28342 break;
28344 default:
28345 done = true;
28346 break;
28349 /* It is not uncommon to see programs mechanically, erroneously, use
28350 the 'typename' keyword to denote (dependent) qualified types
28351 as base classes. */
28352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28354 token = cp_lexer_peek_token (parser->lexer);
28355 if (!processing_template_decl)
28356 error_at (token->location,
28357 "keyword %<typename%> not allowed outside of templates");
28358 else
28359 error_at (token->location,
28360 "keyword %<typename%> not allowed in this context "
28361 "(the base class is implicitly a type)");
28362 cp_lexer_consume_token (parser->lexer);
28365 /* Look for the optional `::' operator. */
28366 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28367 /* Look for the nested-name-specifier. The simplest way to
28368 implement:
28370 [temp.res]
28372 The keyword `typename' is not permitted in a base-specifier or
28373 mem-initializer; in these contexts a qualified name that
28374 depends on a template-parameter is implicitly assumed to be a
28375 type name.
28377 is to pretend that we have seen the `typename' keyword at this
28378 point. */
28379 cp_parser_nested_name_specifier_opt (parser,
28380 /*typename_keyword_p=*/true,
28381 /*check_dependency_p=*/true,
28382 /*type_p=*/true,
28383 /*is_declaration=*/true);
28384 /* If the base class is given by a qualified name, assume that names
28385 we see are type names or templates, as appropriate. */
28386 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28387 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28389 if (!parser->scope
28390 && cp_lexer_next_token_is_decltype (parser->lexer))
28391 /* DR 950 allows decltype as a base-specifier. */
28392 type = cp_parser_decltype (parser);
28393 else
28395 /* Otherwise, look for the class-name. */
28396 type = cp_parser_class_name (parser,
28397 class_scope_p,
28398 template_p,
28399 typename_type,
28400 /*check_dependency_p=*/true,
28401 /*class_head_p=*/false,
28402 /*is_declaration=*/true);
28403 type = TREE_TYPE (type);
28406 if (type == error_mark_node)
28407 return error_mark_node;
28409 return finish_base_specifier (type, access, virtual_p);
28412 /* Exception handling [gram.exception] */
28414 /* Save the tokens that make up the noexcept-specifier for a member-function.
28415 Returns a DEFERRED_PARSE. */
28417 static tree
28418 cp_parser_save_noexcept (cp_parser *parser)
28420 cp_token *first = parser->lexer->next_token;
28421 /* We want everything up to, including, the final ')'. */
28422 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28423 cp_token *last = parser->lexer->next_token;
28425 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28426 to carry the information we will need. */
28427 tree expr = make_node (DEFERRED_PARSE);
28428 /* Save away the noexcept-specifier; we will process it when the
28429 class is complete. */
28430 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28431 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28432 expr = build_tree_list (expr, NULL_TREE);
28433 return expr;
28436 /* Used for late processing of noexcept-specifiers of member-functions.
28437 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28438 we saved for later; parse it now. DECL is the declaration of the
28439 member function. */
28441 static tree
28442 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28444 /* Make sure we've gotten something that hasn't been parsed yet. */
28445 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28447 push_unparsed_function_queues (parser);
28449 /* Push the saved tokens for the noexcept-specifier onto the parser's
28450 lexer stack. */
28451 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28452 cp_parser_push_lexer_for_tokens (parser, tokens);
28454 /* Parse the cached noexcept-specifier. */
28455 tree parsed_arg
28456 = cp_parser_noexcept_specification_opt (parser,
28457 CP_PARSER_FLAGS_NONE,
28458 /*require_constexpr=*/true,
28459 /*consumed_expr=*/NULL,
28460 /*return_cond=*/false);
28462 /* Revert to the main lexer. */
28463 cp_parser_pop_lexer (parser);
28465 /* Restore the queue. */
28466 pop_unparsed_function_queues (parser);
28468 /* And we're done. */
28469 return parsed_arg;
28472 /* Perform late checking of overriding function with respect to their
28473 noexcept-specifiers. FNDECL is the member function that potentially
28474 overrides some virtual function with the same signature. */
28476 static void
28477 noexcept_override_late_checks (tree fndecl)
28479 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28480 tree base_binfo;
28482 if (DECL_STATIC_FUNCTION_P (fndecl))
28483 return;
28485 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28487 tree basetype = BINFO_TYPE (base_binfo);
28489 if (!TYPE_POLYMORPHIC_P (basetype))
28490 continue;
28492 tree fn = look_for_overrides_here (basetype, fndecl);
28493 if (fn)
28494 maybe_check_overriding_exception_spec (fndecl, fn);
28498 /* Parse an (optional) noexcept-specification.
28500 noexcept-specification:
28501 noexcept ( constant-expression ) [opt]
28503 If no noexcept-specification is present, returns NULL_TREE.
28504 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28505 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28506 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28507 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28508 in which case a boolean condition is returned instead. The parser flags
28509 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28510 the (member) function is `const'. */
28512 static tree
28513 cp_parser_noexcept_specification_opt (cp_parser* parser,
28514 cp_parser_flags flags,
28515 bool require_constexpr,
28516 bool* consumed_expr,
28517 bool return_cond)
28519 cp_token *token;
28520 const char *saved_message;
28522 /* Peek at the next token. */
28523 token = cp_lexer_peek_token (parser->lexer);
28525 /* Is it a noexcept-specification? */
28526 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28528 tree expr;
28530 /* [class.mem]/6 says that a noexcept-specifer (within the
28531 member-specification of the class) is a complete-class context of
28532 a class. So, if the noexcept-specifier has the optional expression,
28533 just save the tokens, and reparse this after we're done with the
28534 class. */
28536 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28537 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28538 /* No need to delay parsing for a number literal or true/false. */
28539 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28540 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28541 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28542 && at_class_scope_p ()
28543 && TYPE_BEING_DEFINED (current_class_type)
28544 && !LAMBDA_TYPE_P (current_class_type))
28545 return cp_parser_save_noexcept (parser);
28547 cp_lexer_consume_token (parser->lexer);
28549 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28551 matching_parens parens;
28552 parens.consume_open (parser);
28554 if (require_constexpr)
28556 /* Types may not be defined in an exception-specification. */
28557 saved_message = parser->type_definition_forbidden_message;
28558 parser->type_definition_forbidden_message
28559 = G_("types may not be defined in an exception-specification");
28561 bool non_constant_p;
28562 expr
28563 = cp_parser_constant_expression (parser,
28564 /*allow_non_constant=*/true,
28565 &non_constant_p);
28566 if (non_constant_p
28567 && !require_potential_rvalue_constant_expression (expr))
28569 expr = NULL_TREE;
28570 return_cond = true;
28573 /* Restore the saved message. */
28574 parser->type_definition_forbidden_message = saved_message;
28576 else
28578 expr = cp_parser_expression (parser);
28579 *consumed_expr = true;
28582 parens.require_close (parser);
28584 else
28586 expr = boolean_true_node;
28587 if (!require_constexpr)
28588 *consumed_expr = false;
28591 /* We cannot build a noexcept-spec right away because this will check
28592 that expr is a constexpr. */
28593 if (!return_cond)
28594 return build_noexcept_spec (expr, tf_warning_or_error);
28595 else
28596 return expr;
28598 else
28599 return NULL_TREE;
28602 /* Parse an (optional) exception-specification.
28604 exception-specification:
28605 throw ( type-id-list [opt] )
28607 Returns a TREE_LIST representing the exception-specification. The
28608 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28609 control parsing. QUALS are qualifiers indicating whether the (member)
28610 function is `const'. */
28612 static tree
28613 cp_parser_exception_specification_opt (cp_parser* parser,
28614 cp_parser_flags flags)
28616 cp_token *token;
28617 tree type_id_list;
28618 const char *saved_message;
28620 /* Peek at the next token. */
28621 token = cp_lexer_peek_token (parser->lexer);
28623 /* Is it a noexcept-specification? */
28624 type_id_list
28625 = cp_parser_noexcept_specification_opt (parser, flags,
28626 /*require_constexpr=*/true,
28627 /*consumed_expr=*/NULL,
28628 /*return_cond=*/false);
28629 if (type_id_list != NULL_TREE)
28630 return type_id_list;
28632 /* If it's not `throw', then there's no exception-specification. */
28633 if (!cp_parser_is_keyword (token, RID_THROW))
28634 return NULL_TREE;
28636 location_t loc = token->location;
28638 /* Consume the `throw'. */
28639 cp_lexer_consume_token (parser->lexer);
28641 /* Look for the `('. */
28642 matching_parens parens;
28643 parens.require_open (parser);
28645 /* Peek at the next token. */
28646 token = cp_lexer_peek_token (parser->lexer);
28647 /* If it's not a `)', then there is a type-id-list. */
28648 if (token->type != CPP_CLOSE_PAREN)
28650 /* Types may not be defined in an exception-specification. */
28651 saved_message = parser->type_definition_forbidden_message;
28652 parser->type_definition_forbidden_message
28653 = G_("types may not be defined in an exception-specification");
28654 /* Parse the type-id-list. */
28655 type_id_list = cp_parser_type_id_list (parser);
28656 /* Restore the saved message. */
28657 parser->type_definition_forbidden_message = saved_message;
28659 if (cxx_dialect >= cxx17)
28661 error_at (loc, "ISO C++17 does not allow dynamic exception "
28662 "specifications");
28663 type_id_list = NULL_TREE;
28665 else if (cxx_dialect >= cxx11)
28666 warning_at (loc, OPT_Wdeprecated,
28667 "dynamic exception specifications are deprecated in "
28668 "C++11");
28670 /* In C++17, throw() is equivalent to noexcept (true). throw()
28671 is deprecated in C++11 and above as well, but is still widely used,
28672 so don't warn about it yet. */
28673 else if (cxx_dialect >= cxx17)
28674 type_id_list = noexcept_true_spec;
28675 else
28676 type_id_list = empty_except_spec;
28678 /* Look for the `)'. */
28679 parens.require_close (parser);
28681 return type_id_list;
28684 /* Parse an (optional) type-id-list.
28686 type-id-list:
28687 type-id ... [opt]
28688 type-id-list , type-id ... [opt]
28690 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28691 in the order that the types were presented. */
28693 static tree
28694 cp_parser_type_id_list (cp_parser* parser)
28696 tree types = NULL_TREE;
28698 while (true)
28700 cp_token *token;
28701 tree type;
28703 token = cp_lexer_peek_token (parser->lexer);
28705 /* Get the next type-id. */
28706 type = cp_parser_type_id (parser);
28707 /* Check for invalid 'auto'. */
28708 if (flag_concepts && type_uses_auto (type))
28710 error_at (token->location,
28711 "invalid use of %<auto%> in exception-specification");
28712 type = error_mark_node;
28714 /* Parse the optional ellipsis. */
28715 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28717 /* Consume the `...'. */
28718 cp_lexer_consume_token (parser->lexer);
28720 /* Turn the type into a pack expansion expression. */
28721 type = make_pack_expansion (type);
28723 /* Add it to the list. */
28724 types = add_exception_specifier (types, type, /*complain=*/1);
28725 /* Peek at the next token. */
28726 token = cp_lexer_peek_token (parser->lexer);
28727 /* If it is not a `,', we are done. */
28728 if (token->type != CPP_COMMA)
28729 break;
28730 /* Consume the `,'. */
28731 cp_lexer_consume_token (parser->lexer);
28734 return nreverse (types);
28737 /* Parse a try-block.
28739 try-block:
28740 try compound-statement handler-seq */
28742 static tree
28743 cp_parser_try_block (cp_parser* parser)
28745 tree try_block;
28747 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28748 if (parser->in_function_body
28749 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28750 && cxx_dialect < cxx20)
28751 pedwarn (input_location, OPT_Wc__20_extensions,
28752 "%<try%> in %<constexpr%> function only "
28753 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28755 try_block = begin_try_block ();
28756 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28757 finish_try_block (try_block);
28758 cp_parser_handler_seq (parser);
28759 finish_handler_sequence (try_block);
28761 return try_block;
28764 /* Parse a function-try-block.
28766 function-try-block:
28767 try ctor-initializer [opt] function-body handler-seq */
28769 static void
28770 cp_parser_function_try_block (cp_parser* parser)
28772 tree compound_stmt;
28773 tree try_block;
28775 /* Look for the `try' keyword. */
28776 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28777 return;
28778 /* Let the rest of the front end know where we are. */
28779 try_block = begin_function_try_block (&compound_stmt);
28780 /* Parse the function-body. */
28781 cp_parser_ctor_initializer_opt_and_function_body
28782 (parser, /*in_function_try_block=*/true);
28783 /* We're done with the `try' part. */
28784 finish_function_try_block (try_block);
28785 /* Parse the handlers. */
28786 cp_parser_handler_seq (parser);
28787 /* We're done with the handlers. */
28788 finish_function_handler_sequence (try_block, compound_stmt);
28791 /* Parse a handler-seq.
28793 handler-seq:
28794 handler handler-seq [opt] */
28796 static void
28797 cp_parser_handler_seq (cp_parser* parser)
28799 while (true)
28801 cp_token *token;
28803 /* Parse the handler. */
28804 cp_parser_handler (parser);
28805 /* Peek at the next token. */
28806 token = cp_lexer_peek_token (parser->lexer);
28807 /* If it's not `catch' then there are no more handlers. */
28808 if (!cp_parser_is_keyword (token, RID_CATCH))
28809 break;
28813 /* Parse a handler.
28815 handler:
28816 catch ( exception-declaration ) compound-statement */
28818 static void
28819 cp_parser_handler (cp_parser* parser)
28821 tree handler;
28822 tree declaration;
28824 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28825 handler = begin_handler ();
28826 matching_parens parens;
28827 parens.require_open (parser);
28828 declaration = cp_parser_exception_declaration (parser);
28829 finish_handler_parms (declaration, handler);
28830 parens.require_close (parser);
28831 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28832 finish_handler (handler);
28835 /* Parse an exception-declaration.
28837 exception-declaration:
28838 type-specifier-seq declarator
28839 type-specifier-seq abstract-declarator
28840 type-specifier-seq
28843 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28844 ellipsis variant is used. */
28846 static tree
28847 cp_parser_exception_declaration (cp_parser* parser)
28849 cp_decl_specifier_seq type_specifiers;
28850 cp_declarator *declarator;
28851 const char *saved_message;
28853 /* If it's an ellipsis, it's easy to handle. */
28854 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28856 /* Consume the `...' token. */
28857 cp_lexer_consume_token (parser->lexer);
28858 return NULL_TREE;
28861 /* Types may not be defined in exception-declarations. */
28862 saved_message = parser->type_definition_forbidden_message;
28863 parser->type_definition_forbidden_message
28864 = G_("types may not be defined in exception-declarations");
28866 /* Parse the type-specifier-seq. */
28867 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28868 /*is_declaration=*/true,
28869 /*is_trailing_return=*/false,
28870 &type_specifiers);
28871 /* If it's a `)', then there is no declarator. */
28872 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28873 declarator = NULL;
28874 else
28875 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28876 CP_PARSER_FLAGS_NONE,
28877 /*ctor_dtor_or_conv_p=*/NULL,
28878 /*parenthesized_p=*/NULL,
28879 /*member_p=*/false,
28880 /*friend_p=*/false,
28881 /*static_p=*/false);
28883 /* Restore the saved message. */
28884 parser->type_definition_forbidden_message = saved_message;
28886 if (!type_specifiers.any_specifiers_p)
28887 return error_mark_node;
28889 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28892 /* Parse a throw-expression.
28894 throw-expression:
28895 throw assignment-expression [opt]
28897 Returns a THROW_EXPR representing the throw-expression. */
28899 static tree
28900 cp_parser_throw_expression (cp_parser* parser)
28902 tree expression;
28903 cp_token* token;
28904 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28906 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28907 token = cp_lexer_peek_token (parser->lexer);
28908 /* Figure out whether or not there is an assignment-expression
28909 following the "throw" keyword. */
28910 if (token->type == CPP_COMMA
28911 || token->type == CPP_SEMICOLON
28912 || token->type == CPP_CLOSE_PAREN
28913 || token->type == CPP_CLOSE_SQUARE
28914 || token->type == CPP_CLOSE_BRACE
28915 || token->type == CPP_COLON)
28916 expression = NULL_TREE;
28917 else
28918 expression = cp_parser_assignment_expression (parser);
28920 /* Construct a location e.g.:
28921 throw x
28922 ^~~~~~~
28923 with caret == start at the start of the "throw" token, and
28924 the end at the end of the final token we consumed. */
28925 location_t combined_loc = make_location (start_loc, start_loc,
28926 parser->lexer);
28927 expression = build_throw (combined_loc, expression);
28929 return expression;
28932 /* Parse a yield-expression.
28934 yield-expression:
28935 co_yield assignment-expression
28936 co_yield braced-init-list
28938 Returns a CO_YIELD_EXPR representing the yield-expression. */
28940 static tree
28941 cp_parser_yield_expression (cp_parser* parser)
28943 tree expr;
28945 cp_token *token = cp_lexer_peek_token (parser->lexer);
28946 location_t kw_loc = token->location; /* Save for later. */
28948 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28952 cp_lexer_set_source_position (parser->lexer);
28953 /* ??? : probably a moot point? */
28954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28955 expr = cp_parser_braced_list (parser);
28957 else
28958 expr = cp_parser_assignment_expression (parser);
28960 if (expr == error_mark_node)
28961 return expr;
28963 return finish_co_yield_expr (kw_loc, expr);
28966 /* GNU Extensions */
28968 /* Parse an (optional) asm-specification.
28970 asm-specification:
28971 asm ( string-literal )
28973 If the asm-specification is present, returns a STRING_CST
28974 corresponding to the string-literal. Otherwise, returns
28975 NULL_TREE. */
28977 static tree
28978 cp_parser_asm_specification_opt (cp_parser* parser)
28980 /* Peek at the next token. */
28981 cp_token *token = cp_lexer_peek_token (parser->lexer);
28982 /* If the next token isn't the `asm' keyword, then there's no
28983 asm-specification. */
28984 if (!cp_parser_is_keyword (token, RID_ASM))
28985 return NULL_TREE;
28987 /* Consume the `asm' token. */
28988 cp_lexer_consume_token (parser->lexer);
28989 /* Look for the `('. */
28990 matching_parens parens;
28991 parens.require_open (parser);
28993 /* Look for the string-literal. */
28994 tree asm_specification = cp_parser_string_literal (parser,
28995 /*translate=*/false,
28996 /*wide_ok=*/false);
28998 /* Look for the `)'. */
28999 parens.require_close (parser);
29001 return asm_specification;
29004 /* Parse an asm-operand-list.
29006 asm-operand-list:
29007 asm-operand
29008 asm-operand-list , asm-operand
29010 asm-operand:
29011 string-literal ( expression )
29012 [ string-literal ] string-literal ( expression )
29014 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29015 each node is the expression. The TREE_PURPOSE is itself a
29016 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29017 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29018 is a STRING_CST for the string literal before the parenthesis. Returns
29019 ERROR_MARK_NODE if any of the operands are invalid. */
29021 static tree
29022 cp_parser_asm_operand_list (cp_parser* parser)
29024 tree asm_operands = NULL_TREE;
29025 bool invalid_operands = false;
29027 while (true)
29029 tree name;
29031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29033 /* Consume the `[' token. */
29034 cp_lexer_consume_token (parser->lexer);
29035 /* Read the operand name. */
29036 name = cp_parser_identifier (parser);
29037 if (name != error_mark_node)
29038 name = build_string (IDENTIFIER_LENGTH (name),
29039 IDENTIFIER_POINTER (name));
29040 /* Look for the closing `]'. */
29041 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29043 else
29044 name = NULL_TREE;
29045 /* Look for the string-literal. */
29046 tree string_literal = cp_parser_string_literal (parser,
29047 /*translate=*/false,
29048 /*wide_ok=*/false);
29050 /* Look for the `('. */
29051 matching_parens parens;
29052 parens.require_open (parser);
29053 /* Parse the expression. */
29054 tree expression = cp_parser_expression (parser);
29055 /* Look for the `)'. */
29056 parens.require_close (parser);
29058 if (name == error_mark_node
29059 || string_literal == error_mark_node
29060 || expression == error_mark_node)
29061 invalid_operands = true;
29063 /* Add this operand to the list. */
29064 asm_operands = tree_cons (build_tree_list (name, string_literal),
29065 expression,
29066 asm_operands);
29067 /* If the next token is not a `,', there are no more
29068 operands. */
29069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29070 break;
29071 /* Consume the `,'. */
29072 cp_lexer_consume_token (parser->lexer);
29075 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29078 /* Parse an asm-clobber-list.
29080 asm-clobber-list:
29081 string-literal
29082 asm-clobber-list , string-literal
29084 Returns a TREE_LIST, indicating the clobbers in the order that they
29085 appeared. The TREE_VALUE of each node is a STRING_CST. */
29087 static tree
29088 cp_parser_asm_clobber_list (cp_parser* parser)
29090 tree clobbers = NULL_TREE;
29092 while (true)
29094 /* Look for the string literal. */
29095 tree string_literal = cp_parser_string_literal (parser,
29096 /*translate=*/false,
29097 /*wide_ok=*/false);
29098 /* Add it to the list. */
29099 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29100 /* If the next token is not a `,', then the list is
29101 complete. */
29102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29103 break;
29104 /* Consume the `,' token. */
29105 cp_lexer_consume_token (parser->lexer);
29108 return clobbers;
29111 /* Parse an asm-label-list.
29113 asm-label-list:
29114 identifier
29115 asm-label-list , identifier
29117 Returns a TREE_LIST, indicating the labels in the order that they
29118 appeared. The TREE_VALUE of each node is a label. */
29120 static tree
29121 cp_parser_asm_label_list (cp_parser* parser)
29123 tree labels = NULL_TREE;
29125 while (true)
29127 tree identifier, label, name;
29129 /* Look for the identifier. */
29130 identifier = cp_parser_identifier (parser);
29131 if (!error_operand_p (identifier))
29133 label = lookup_label (identifier);
29134 if (TREE_CODE (label) == LABEL_DECL)
29136 TREE_USED (label) = 1;
29137 check_goto (label);
29138 name = build_string (IDENTIFIER_LENGTH (identifier),
29139 IDENTIFIER_POINTER (identifier));
29140 labels = tree_cons (name, label, labels);
29143 /* If the next token is not a `,', then the list is
29144 complete. */
29145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29146 break;
29147 /* Consume the `,' token. */
29148 cp_lexer_consume_token (parser->lexer);
29151 return nreverse (labels);
29154 /* Return TRUE iff the next tokens in the stream are possibly the
29155 beginning of a GNU extension attribute. */
29157 static bool
29158 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29160 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29163 /* Return TRUE iff the next tokens in the stream are possibly the
29164 beginning of a standard C++-11 attribute specifier. */
29166 static bool
29167 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29169 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29172 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29173 beginning of a standard C++-11 attribute specifier. */
29175 static bool
29176 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29178 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29180 return (cxx_dialect >= cxx11
29181 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29182 || (token->type == CPP_OPEN_SQUARE
29183 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29184 && token->type == CPP_OPEN_SQUARE)));
29187 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29188 beginning of a GNU extension attribute. */
29190 static bool
29191 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29193 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29195 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29198 /* Return true iff the next tokens can be the beginning of either a
29199 GNU attribute list, or a standard C++11 attribute sequence. */
29201 static bool
29202 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29204 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29205 || cp_next_tokens_can_be_std_attribute_p (parser));
29208 /* Return true iff the next Nth tokens can be the beginning of either
29209 a GNU attribute list, or a standard C++11 attribute sequence. */
29211 static bool
29212 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29214 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29215 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29218 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29219 of GNU attributes, or return NULL. */
29221 static tree
29222 cp_parser_attributes_opt (cp_parser *parser)
29224 tree attrs = NULL_TREE;
29225 while (true)
29227 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29228 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29229 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29230 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29231 else
29232 break;
29234 return attrs;
29237 /* Parse an (optional) series of attributes.
29239 attributes:
29240 attributes attribute
29242 attribute:
29243 __attribute__ (( attribute-list [opt] ))
29245 The return value is as for cp_parser_gnu_attribute_list. */
29247 static tree
29248 cp_parser_gnu_attributes_opt (cp_parser* parser)
29250 tree attributes = NULL_TREE;
29252 auto cleanup = make_temp_override
29253 (parser->auto_is_implicit_function_template_parm_p, false);
29255 while (true)
29257 cp_token *token;
29258 tree attribute_list;
29259 bool ok = true;
29261 /* Peek at the next token. */
29262 token = cp_lexer_peek_token (parser->lexer);
29263 /* If it's not `__attribute__', then we're done. */
29264 if (token->keyword != RID_ATTRIBUTE)
29265 break;
29267 /* Consume the `__attribute__' keyword. */
29268 cp_lexer_consume_token (parser->lexer);
29269 /* Look for the two `(' tokens. */
29270 matching_parens outer_parens;
29271 if (!outer_parens.require_open (parser))
29272 ok = false;
29273 matching_parens inner_parens;
29274 if (!inner_parens.require_open (parser))
29275 ok = false;
29277 /* Peek at the next token. */
29278 token = cp_lexer_peek_token (parser->lexer);
29279 if (token->type != CPP_CLOSE_PAREN)
29280 /* Parse the attribute-list. */
29281 attribute_list = cp_parser_gnu_attribute_list (parser);
29282 else
29283 /* If the next token is a `)', then there is no attribute
29284 list. */
29285 attribute_list = NULL;
29287 /* Look for the two `)' tokens. */
29288 if (!inner_parens.require_close (parser))
29289 ok = false;
29290 if (!outer_parens.require_close (parser))
29291 ok = false;
29292 if (!ok)
29293 cp_parser_skip_to_end_of_statement (parser);
29295 /* Add these new attributes to the list. */
29296 attributes = attr_chainon (attributes, attribute_list);
29299 return attributes;
29302 /* Parse a GNU attribute-list.
29304 attribute-list:
29305 attribute
29306 attribute-list , attribute
29308 attribute:
29309 identifier
29310 identifier ( identifier )
29311 identifier ( identifier , expression-list )
29312 identifier ( expression-list )
29314 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29315 to an attribute. The TREE_PURPOSE of each node is the identifier
29316 indicating which attribute is in use. The TREE_VALUE represents
29317 the arguments, if any. */
29319 static tree
29320 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29322 tree attribute_list = NULL_TREE;
29323 bool save_translate_strings_p = parser->translate_strings_p;
29325 /* Don't create wrapper nodes within attributes: the
29326 handlers don't know how to handle them. */
29327 auto_suppress_location_wrappers sentinel;
29329 parser->translate_strings_p = false;
29330 while (true)
29332 cp_token *token;
29333 tree identifier;
29334 tree attribute;
29336 /* Look for the identifier. We also allow keywords here; for
29337 example `__attribute__ ((const))' is legal. */
29338 token = cp_lexer_peek_token (parser->lexer);
29339 if (token->type == CPP_NAME
29340 || token->type == CPP_KEYWORD)
29342 tree arguments = NULL_TREE;
29344 /* Consume the token, but save it since we need it for the
29345 SIMD enabled function parsing. */
29346 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29348 /* Save away the identifier that indicates which attribute
29349 this is. */
29350 identifier = (token->type == CPP_KEYWORD)
29351 /* For keywords, use the canonical spelling, not the
29352 parsed identifier. */
29353 ? ridpointers[(int) token->keyword]
29354 : id_token->u.value;
29356 identifier = canonicalize_attr_name (identifier);
29357 attribute = build_tree_list (identifier, NULL_TREE);
29359 /* Peek at the next token. */
29360 token = cp_lexer_peek_token (parser->lexer);
29361 /* If it's an `(', then parse the attribute arguments. */
29362 if (token->type == CPP_OPEN_PAREN)
29364 vec<tree, va_gc> *vec;
29365 int attr_flag = (attribute_takes_identifier_p (identifier)
29366 ? id_attr : normal_attr);
29367 if (is_attribute_p ("assume", identifier))
29368 attr_flag = assume_attr;
29369 vec = cp_parser_parenthesized_expression_list
29370 (parser, attr_flag, /*cast_p=*/false,
29371 /*allow_expansion_p=*/false,
29372 /*non_constant_p=*/NULL);
29373 if (vec == NULL)
29374 arguments = error_mark_node;
29375 else
29377 arguments = build_tree_list_vec (vec);
29378 release_tree_vector (vec);
29380 /* Save the arguments away. */
29381 TREE_VALUE (attribute) = arguments;
29384 if (arguments != error_mark_node)
29386 /* Add this attribute to the list. */
29387 TREE_CHAIN (attribute) = attribute_list;
29388 attribute_list = attribute;
29391 token = cp_lexer_peek_token (parser->lexer);
29393 /* Unless EXACTLY_ONE is set look for more attributes.
29394 If the next token isn't a `,', we're done. */
29395 if (exactly_one || token->type != CPP_COMMA)
29396 break;
29398 /* Consume the comma and keep going. */
29399 cp_lexer_consume_token (parser->lexer);
29401 parser->translate_strings_p = save_translate_strings_p;
29403 /* We built up the list in reverse order. */
29404 return nreverse (attribute_list);
29407 /* Parse arguments of omp::directive attribute.
29409 ( directive-name ,[opt] clause-list[opt] )
29411 For directive just remember the first/last tokens for subsequent
29412 parsing. */
29414 static void
29415 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
29417 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29418 if (first->type == CPP_CLOSE_PAREN)
29420 cp_lexer_consume_token (parser->lexer);
29421 error_at (first->location, "expected OpenMP directive name");
29422 cp_lexer_consume_token (parser->lexer);
29423 TREE_VALUE (attribute) = NULL_TREE;
29424 return;
29426 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29427 if (n == 1)
29429 cp_lexer_consume_token (parser->lexer);
29430 error_at (first->location, "expected attribute argument as balanced "
29431 "token sequence");
29432 TREE_VALUE (attribute) = NULL_TREE;
29433 return;
29435 for (n = n - 2; n; --n)
29436 cp_lexer_consume_token (parser->lexer);
29437 cp_token *last = cp_lexer_peek_token (parser->lexer);
29438 cp_lexer_consume_token (parser->lexer);
29439 tree arg = make_node (DEFERRED_PARSE);
29440 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29441 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29442 if (decl_p)
29443 TREE_PUBLIC (arg) = 1;
29444 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29447 /* Parse arguments of omp::sequence attribute.
29449 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29451 static void
29452 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29454 matching_parens parens;
29455 parens.consume_open (parser);
29458 cp_token *token = cp_lexer_peek_token (parser->lexer);
29459 if (token->type == CPP_NAME
29460 && token->u.value == omp_identifier
29461 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29463 cp_lexer_consume_token (parser->lexer);
29464 cp_lexer_consume_token (parser->lexer);
29465 token = cp_lexer_peek_token (parser->lexer);
29467 bool directive = false;
29468 const char *p;
29469 if (token->type != CPP_NAME)
29470 p = "";
29471 else
29472 p = IDENTIFIER_POINTER (token->u.value);
29473 if (strcmp (p, "directive") == 0)
29474 directive = true;
29475 else if (strcmp (p, "sequence") != 0)
29477 error_at (token->location, "expected %<directive%> or %<sequence%>");
29478 cp_parser_skip_to_closing_parenthesis (parser,
29479 /*recovering=*/true,
29480 /*or_comma=*/true,
29481 /*consume_paren=*/false);
29482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29483 break;
29484 cp_lexer_consume_token (parser->lexer);
29486 cp_lexer_consume_token (parser->lexer);
29487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29488 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29489 UNKNOWN_LOCATION);
29490 else if (directive)
29491 cp_parser_omp_directive_args (parser, attribute, false);
29492 else
29493 cp_parser_omp_sequence_args (parser, attribute);
29494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29495 break;
29496 cp_lexer_consume_token (parser->lexer);
29498 while (1);
29499 if (!parens.require_close (parser))
29500 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29501 /*consume_paren=*/true);
29504 /* Parse a standard C++11 attribute.
29506 The returned representation is a TREE_LIST which TREE_PURPOSE is
29507 the scoped name of the attribute, and the TREE_VALUE is its
29508 arguments list.
29510 Note that the scoped name of the attribute is itself a TREE_LIST
29511 which TREE_PURPOSE is the namespace of the attribute, and
29512 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29513 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29514 and which TREE_PURPOSE is directly the attribute name.
29516 Clients of the attribute code should use get_attribute_namespace
29517 and get_attribute_name to get the actual namespace and name of
29518 attributes, regardless of their being GNU or C++11 attributes.
29520 attribute:
29521 attribute-token attribute-argument-clause [opt]
29523 attribute-token:
29524 identifier
29525 attribute-scoped-token
29527 attribute-scoped-token:
29528 attribute-namespace :: identifier
29530 attribute-namespace:
29531 identifier
29533 attribute-argument-clause:
29534 ( balanced-token-seq )
29536 balanced-token-seq:
29537 balanced-token [opt]
29538 balanced-token-seq balanced-token
29540 balanced-token:
29541 ( balanced-token-seq )
29542 [ balanced-token-seq ]
29543 { balanced-token-seq }. */
29545 static tree
29546 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29548 tree attribute, attr_id = NULL_TREE, arguments;
29549 cp_token *token;
29551 auto cleanup = make_temp_override
29552 (parser->auto_is_implicit_function_template_parm_p, false);
29554 /* First, parse name of the attribute, a.k.a attribute-token. */
29556 token = cp_lexer_peek_token (parser->lexer);
29557 if (token->type == CPP_NAME)
29558 attr_id = token->u.value;
29559 else if (token->type == CPP_KEYWORD)
29560 attr_id = ridpointers[(int) token->keyword];
29561 else if (token->flags & NAMED_OP)
29562 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29564 if (attr_id == NULL_TREE)
29565 return NULL_TREE;
29567 cp_lexer_consume_token (parser->lexer);
29569 token = cp_lexer_peek_token (parser->lexer);
29570 if (token->type == CPP_SCOPE)
29572 /* We are seeing a scoped attribute token. */
29574 cp_lexer_consume_token (parser->lexer);
29575 if (attr_ns)
29576 error_at (token->location, "attribute using prefix used together "
29577 "with scoped attribute token");
29578 attr_ns = attr_id;
29580 token = cp_lexer_peek_token (parser->lexer);
29581 if (token->type == CPP_NAME)
29582 attr_id = token->u.value;
29583 else if (token->type == CPP_KEYWORD)
29584 attr_id = ridpointers[(int) token->keyword];
29585 else if (token->flags & NAMED_OP)
29586 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29587 else
29589 error_at (token->location,
29590 "expected an identifier for the attribute name");
29591 return error_mark_node;
29593 cp_lexer_consume_token (parser->lexer);
29595 attr_ns = canonicalize_attr_name (attr_ns);
29596 attr_id = canonicalize_attr_name (attr_id);
29597 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29598 NULL_TREE);
29599 token = cp_lexer_peek_token (parser->lexer);
29601 else if (attr_ns)
29603 attr_ns = canonicalize_attr_name (attr_ns);
29604 attr_id = canonicalize_attr_name (attr_id);
29605 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29606 NULL_TREE);
29608 else
29610 attr_id = canonicalize_attr_name (attr_id);
29611 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29612 NULL_TREE);
29614 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29615 but no longer: we have to be able to tell [[noreturn]] and
29616 __attribute__((noreturn)) apart. */
29617 /* C++14 deprecated attribute is equivalent to GNU's. */
29618 if (is_attribute_p ("deprecated", attr_id))
29619 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29620 /* C++17 fallthrough attribute is equivalent to GNU's. */
29621 else if (is_attribute_p ("fallthrough", attr_id))
29622 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29623 /* C++23 assume attribute is equivalent to GNU's. */
29624 else if (is_attribute_p ("assume", attr_id))
29625 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29626 /* Transactional Memory TS optimize_for_synchronized attribute is
29627 equivalent to GNU transaction_callable. */
29628 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29629 TREE_PURPOSE (attribute)
29630 = get_identifier ("transaction_callable");
29631 /* Transactional Memory attributes are GNU attributes. */
29632 else if (tm_attr_to_mask (attr_id))
29633 TREE_PURPOSE (attribute) = attr_id;
29636 /* Now parse the optional argument clause of the attribute. */
29638 if (token->type != CPP_OPEN_PAREN)
29640 if ((flag_openmp || flag_openmp_simd)
29641 && attr_ns == omp_identifier
29642 && (is_attribute_p ("directive", attr_id)
29643 || is_attribute_p ("sequence", attr_id)
29644 || is_attribute_p ("decl", attr_id)))
29646 error_at (token->location, "%<omp::%E%> attribute requires argument",
29647 attr_id);
29648 return NULL_TREE;
29650 return attribute;
29654 vec<tree, va_gc> *vec;
29655 int attr_flag = normal_attr;
29657 /* Maybe we don't expect to see any arguments for this attribute. */
29658 const attribute_spec *as
29659 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29660 if (as && as->max_length == 0)
29662 error_at (token->location, "%qE attribute does not take any arguments",
29663 attr_id);
29664 cp_parser_skip_to_closing_parenthesis (parser,
29665 /*recovering=*/true,
29666 /*or_comma=*/false,
29667 /*consume_paren=*/true);
29668 return error_mark_node;
29671 if (is_attribute_p ("assume", attr_id)
29672 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29673 /* The assume attribute needs special handling of the argument. */
29674 attr_flag = assume_attr;
29675 else if (attr_ns == gnu_identifier
29676 && attribute_takes_identifier_p (attr_id))
29677 /* A GNU attribute that takes an identifier in parameter. */
29678 attr_flag = id_attr;
29680 /* If this is a fake attribute created to handle -Wno-attributes,
29681 we must skip parsing the arguments. */
29682 if (as == NULL || attribute_ignored_p (as))
29684 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29686 if (is_attribute_p ("directive", attr_id))
29688 cp_parser_omp_directive_args (parser, attribute, false);
29689 return attribute;
29691 else if (is_attribute_p ("decl", attr_id))
29693 TREE_VALUE (TREE_PURPOSE (attribute))
29694 = get_identifier ("directive");
29695 cp_parser_omp_directive_args (parser, attribute, true);
29696 return attribute;
29698 else if (is_attribute_p ("sequence", attr_id))
29700 TREE_VALUE (TREE_PURPOSE (attribute))
29701 = get_identifier ("directive");
29702 cp_parser_omp_sequence_args (parser, attribute);
29703 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29704 return attribute;
29708 /* For unknown attributes, just skip balanced tokens instead of
29709 trying to parse the arguments. Set TREE_VALUE (attribute) to
29710 error_mark_node to distinguish skipped arguments from attributes
29711 with no arguments. */
29712 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29713 cp_lexer_consume_token (parser->lexer);
29714 TREE_VALUE (attribute) = error_mark_node;
29715 return attribute;
29718 vec = cp_parser_parenthesized_expression_list
29719 (parser, attr_flag, /*cast_p=*/false,
29720 /*allow_expansion_p=*/true,
29721 /*non_constant_p=*/NULL);
29722 if (vec == NULL)
29723 arguments = error_mark_node;
29724 else
29726 if (vec->is_empty ())
29727 /* e.g. [[attr()]]. */
29728 error_at (token->location, "parentheses must be omitted if "
29729 "%qE attribute argument list is empty",
29730 attr_id);
29731 arguments = build_tree_list_vec (vec);
29732 release_tree_vector (vec);
29735 if (arguments == error_mark_node)
29736 attribute = error_mark_node;
29737 else
29738 TREE_VALUE (attribute) = arguments;
29741 return attribute;
29744 /* Warn if the attribute ATTRIBUTE appears more than once in the
29745 attribute-list ATTRIBUTES. This used to be enforced for certain
29746 attributes, but the restriction was removed in P2156.
29747 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29748 found in ATTRIBUTES. */
29750 static bool
29751 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29753 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29754 "likely", "unlikely", "fallthrough",
29755 "no_unique_address", "carries_dependency" };
29756 if (attributes)
29757 for (const auto &a : alist)
29758 if (is_attribute_p (a, get_attribute_name (attribute))
29759 && is_attribute_namespace_p ("", attribute)
29760 && lookup_attribute ("", a, attributes))
29762 if (!from_macro_expansion_at (loc))
29763 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29764 "multiple times", a);
29765 return false;
29767 return true;
29770 /* Parse a list of standard C++-11 attributes.
29772 attribute-list:
29773 attribute [opt]
29774 attribute-list , attribute[opt]
29775 attribute ...
29776 attribute-list , attribute ...
29779 static tree
29780 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29782 tree attributes = NULL_TREE, attribute = NULL_TREE;
29783 cp_token *token = NULL;
29785 while (true)
29787 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29788 attribute = cp_parser_std_attribute (parser, attr_ns);
29789 if (attribute == error_mark_node)
29790 break;
29791 if (attribute != NULL_TREE)
29793 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29795 TREE_CHAIN (attribute) = attributes;
29796 attributes = attribute;
29799 token = cp_lexer_peek_token (parser->lexer);
29800 if (token->type == CPP_ELLIPSIS)
29802 cp_lexer_consume_token (parser->lexer);
29803 if (attribute == NULL_TREE)
29804 error_at (token->location,
29805 "expected attribute before %<...%>");
29806 else if (TREE_VALUE (attribute) == NULL_TREE)
29808 error_at (token->location, "attribute with no arguments "
29809 "contains no parameter packs");
29810 return error_mark_node;
29812 else if (TREE_VALUE (attribute) != error_mark_node)
29814 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29815 if (pack == error_mark_node)
29816 return error_mark_node;
29817 TREE_VALUE (attribute) = pack;
29819 token = cp_lexer_peek_token (parser->lexer);
29821 if (token->type != CPP_COMMA)
29822 break;
29823 cp_lexer_consume_token (parser->lexer);
29825 attributes = nreverse (attributes);
29826 return attributes;
29829 /* Optionally parse a C++20 contract role. A NULL return means that no
29830 contract role was specified.
29832 contract-role:
29833 % default
29834 % identifier
29836 If the identifier does not name a known contract role, it will
29837 be assumed to be default. Returns the identifier for the role
29838 token. */
29840 static tree
29841 cp_parser_contract_role (cp_parser *parser)
29843 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
29844 cp_lexer_consume_token (parser->lexer);
29846 cp_token *token = cp_lexer_peek_token (parser->lexer);
29847 tree role_id = NULL_TREE;
29848 if (token->type == CPP_NAME)
29849 role_id = token->u.value;
29850 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29851 role_id = get_identifier ("default");
29852 else
29854 error_at (token->location, "expected contract-role");
29855 return error_mark_node;
29857 cp_lexer_consume_token (parser->lexer);
29859 /* FIXME: Warn about invalid/unknown roles? */
29860 return role_id;
29863 /* Parse an optional contract mode.
29865 contract-mode:
29866 contract-semantic
29867 [contract-level] [contract-role]
29869 contract-semantic:
29870 check_never_continue
29871 check_maybe_continue
29872 check_always_continue
29874 contract-level:
29875 default
29876 audit
29877 axiom
29879 contract-role:
29880 default
29881 identifier
29883 This grammar is taken from P1332R0. During parsing, this sets options
29884 on the MODE object to determine the configuration of the contract.
29886 Returns a tree containing the identifiers used in the configuration.
29887 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
29888 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
29889 contract-role, if any. NULL_TREE is returned if no information is
29890 given (i.e., all defaults selected). */
29892 static tree
29893 cp_parser_contract_mode_opt (cp_parser *parser,
29894 bool postcondition_p)
29896 /* The mode is empty; the level and role are default. */
29897 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29898 return NULL_TREE;
29900 /* There is only a role; the level is default. */
29901 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29903 tree role_id = cp_parser_contract_role (parser);
29904 return build_tree_list (role_id, get_identifier ("default"));
29907 /* Otherwise, match semantic or level. */
29908 cp_token *token = cp_lexer_peek_token (parser->lexer);
29909 contract_level level = CONTRACT_INVALID;
29910 contract_semantic semantic = CCS_INVALID;
29911 tree config_id;
29912 if (token->type == CPP_NAME)
29914 config_id = token->u.value;
29916 /* Either a named level, a concrete semantic, or an identifier
29917 for a postcondition. */
29918 const char *ident = IDENTIFIER_POINTER (token->u.value);
29919 level = map_contract_level (ident);
29920 semantic = map_contract_semantic (ident);
29922 /* The identifier is the return value for a postcondition. */
29923 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
29924 && postcondition_p)
29925 return NULL_TREE;
29927 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29929 config_id = get_identifier ("default");
29930 level = CONTRACT_DEFAULT;
29932 else
29934 /* We got some other token other than a ':'. */
29935 error_at (token->location, "expected contract semantic or level");
29936 return NULL_TREE;
29939 /* Consume the literal semantic or level token. */
29940 cp_lexer_consume_token (parser->lexer);
29942 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
29944 error_at (token->location,
29945 "expected contract level: "
29946 "%<default%>, %<audit%>, or %<axiom%>");
29947 return NULL_TREE;
29950 /* We matched an explicit semantic. */
29951 if (semantic != CCS_INVALID)
29953 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29955 error ("invalid use of contract role for explicit semantic");
29956 cp_lexer_consume_token (parser->lexer);
29957 cp_lexer_consume_token (parser->lexer);
29959 return config_id;
29962 /* We matched a level, there may be a role; otherwise this is default. */
29963 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29965 tree role_id = cp_parser_contract_role (parser);
29966 return build_tree_list (role_id, config_id);
29969 return build_tree_list (NULL_TREE, config_id);
29972 static tree
29973 find_error (tree *tp, int *, void *)
29975 if (*tp == error_mark_node)
29976 return *tp;
29977 return NULL_TREE;
29980 static bool
29981 contains_error_p (tree t)
29983 return walk_tree (&t, find_error, NULL, NULL);
29986 /* Parse a standard C++20 contract attribute specifier.
29988 contract-attribute-specifier:
29989 [ [ assert contract-level [opt] : conditional-expression ] ]
29990 [ [ pre contract-level [opt] : conditional-expression ] ]
29991 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
29993 For free functions, we cannot determine the type of the postcondition
29994 identifier because the we haven't called grokdeclarator yet. In those
29995 cases we parse the postcondition as if the identifier was declared as
29996 'auto <identifier>'. We then instantiate the postcondition once the
29997 return type is known.
29999 For member functions, contracts are in the complete-class context, so the
30000 parse is deferred. We also have the return type avaialable (unless it's
30001 deduced), so we don't need to parse the postcondition in terms of a
30002 placeholder. */
30004 static tree
30005 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30007 gcc_assert (contract_attribute_p (attribute));
30008 cp_token *token = cp_lexer_consume_token (parser->lexer);
30009 location_t loc = token->location;
30011 bool assertion_p = is_attribute_p ("assert", attribute);
30012 bool postcondition_p = is_attribute_p ("post", attribute);
30014 /* Parse the optional mode. */
30015 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30017 /* Check for postcondition identifiers. */
30018 cp_expr identifier;
30019 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30020 identifier = cp_parser_identifier (parser);
30021 if (identifier == error_mark_node)
30022 return error_mark_node;
30024 cp_parser_require (parser, CPP_COLON, RT_COLON);
30026 /* Defer the parsing of pre/post contracts inside class definitions. */
30027 tree contract;
30028 if (!assertion_p &&
30029 current_class_type &&
30030 TYPE_BEING_DEFINED (current_class_type))
30032 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30033 that doesn't close the attribute, return an error and let the attribute
30034 handling code emit an error for missing ']]'. */
30035 cp_token *first = cp_lexer_peek_token (parser->lexer);
30036 cp_parser_skip_to_closing_parenthesis_1 (parser,
30037 /*recovering=*/false,
30038 CPP_CLOSE_SQUARE,
30039 /*consume_paren=*/false);
30040 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30041 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30042 return error_mark_node;
30043 cp_token *last = cp_lexer_peek_token (parser->lexer);
30045 /* Build a deferred-parse node. */
30046 tree condition = make_node (DEFERRED_PARSE);
30047 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30048 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30050 /* And its corresponding contract. */
30051 contract = grok_contract (attribute, mode, identifier, condition, loc);
30053 else
30055 /* Enable location wrappers when parsing contracts. */
30056 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30058 /* Build a fake variable for the result identifier. */
30059 tree result = NULL_TREE;
30060 if (identifier)
30062 begin_scope (sk_block, NULL_TREE);
30063 result = make_postcondition_variable (identifier);
30064 ++processing_template_decl;
30067 /* Parse the condition, ensuring that parameters or the return variable
30068 aren't flagged for use outside the body of a function. */
30069 ++processing_contract_condition;
30070 cp_expr condition = cp_parser_conditional_expression (parser);
30071 --processing_contract_condition;
30073 /* Try to recover from errors by scanning up to the end of the
30074 attribute. Sometimes we get partially parsed expressions, so
30075 we need to search the condition for errors. */
30076 if (contains_error_p (condition))
30077 cp_parser_skip_up_to_closing_square_bracket (parser);
30079 /* Build the contract. */
30080 contract = grok_contract (attribute, mode, result, condition, loc);
30082 /* Leave our temporary scope for the postcondition result. */
30083 if (result)
30085 --processing_template_decl;
30086 pop_bindings_and_leave_scope ();
30090 if (!flag_contracts)
30092 error_at (loc, "contracts are only available with %<-fcontracts%>");
30093 return error_mark_node;
30096 return finish_contract_attribute (attribute, contract);
30099 /* Parse a contract condition for a deferred contract. */
30101 void cp_parser_late_contract_condition (cp_parser *parser,
30102 tree fn,
30103 tree attribute)
30105 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30107 /* Make sure we've gotten something that hasn't been parsed yet or that
30108 we're not parsing an invalid contract. */
30109 tree condition = CONTRACT_CONDITION (contract);
30110 if (TREE_CODE (condition) != DEFERRED_PARSE)
30111 return;
30113 tree identifier = NULL_TREE;
30114 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30115 identifier = POSTCONDITION_IDENTIFIER (contract);
30117 /* Build a fake variable for the result identifier. */
30118 tree result = NULL_TREE;
30119 if (identifier)
30121 /* TODO: Can we guarantee that the identifier has a location? */
30122 location_t loc = cp_expr_location (contract);
30123 tree type = TREE_TYPE (TREE_TYPE (fn));
30124 if (!check_postcondition_result (fn, type, loc))
30126 invalidate_contract (contract);
30127 return;
30130 begin_scope (sk_block, NULL_TREE);
30131 result = make_postcondition_variable (identifier, type);
30132 ++processing_template_decl;
30135 /* 'this' is not allowed in preconditions of constructors or in postconditions
30136 of destructors. Note that the previous value of this variable is
30137 established by the calling function, so we need to save it here. */
30138 tree saved_ccr = current_class_ref;
30139 tree saved_ccp = current_class_ptr;
30140 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30141 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30143 current_class_ref = current_class_ptr = NULL_TREE;
30144 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30147 push_unparsed_function_queues (parser);
30149 /* Push the saved tokens onto the parser's lexer stack. */
30150 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30151 cp_parser_push_lexer_for_tokens (parser, tokens);
30153 /* Parse the condition, ensuring that parameters or the return variable
30154 aren't flagged for use outside the body of a function. */
30155 ++processing_contract_condition;
30156 condition = cp_parser_conditional_expression (parser);
30157 --processing_contract_condition;
30159 /* Revert to the main lexer. */
30160 cp_parser_pop_lexer (parser);
30162 /* Restore the queue. */
30163 pop_unparsed_function_queues (parser);
30165 current_class_ref = saved_ccr;
30166 current_class_ptr = saved_ccp;
30168 /* Commit to changes. */
30169 update_late_contract (contract, result, condition);
30171 /* Leave our temporary scope for the postcondition result. */
30172 if (result)
30174 --processing_template_decl;
30175 pop_bindings_and_leave_scope ();
30179 /* Parse a standard C++-11 attribute specifier.
30181 attribute-specifier:
30182 [ [ attribute-using-prefix [opt] attribute-list ] ]
30183 contract-attribute-specifier
30184 alignment-specifier
30186 attribute-using-prefix:
30187 using attribute-namespace :
30189 alignment-specifier:
30190 alignas ( type-id ... [opt] )
30191 alignas ( alignment-expression ... [opt] ).
30193 Extensions for contracts:
30195 contract-attribute-specifier:
30196 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30197 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30198 [ [ post : contract-mode [opt] identifier [opt] :
30199 conditional-expression ] ] */
30201 static tree
30202 cp_parser_std_attribute_spec (cp_parser *parser)
30204 tree attributes = NULL_TREE;
30205 cp_token *token = cp_lexer_peek_token (parser->lexer);
30207 if (token->type == CPP_OPEN_SQUARE
30208 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30210 tree attr_ns = NULL_TREE;
30211 tree attr_name = NULL_TREE;
30213 cp_lexer_consume_token (parser->lexer);
30214 cp_lexer_consume_token (parser->lexer);
30216 token = cp_lexer_peek_token (parser->lexer);
30217 if (token->type == CPP_NAME)
30219 attr_name = token->u.value;
30220 attr_name = canonicalize_attr_name (attr_name);
30223 /* Handle contract-attribute-specs specially. */
30224 if (attr_name && contract_attribute_p (attr_name))
30226 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30227 if (attrs != error_mark_node)
30228 attributes = attrs;
30229 goto finish_attrs;
30232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30234 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30235 if (token->type == CPP_NAME)
30236 attr_ns = token->u.value;
30237 else if (token->type == CPP_KEYWORD)
30238 attr_ns = ridpointers[(int) token->keyword];
30239 else if (token->flags & NAMED_OP)
30240 attr_ns = get_identifier (cpp_type2name (token->type,
30241 token->flags));
30242 if (attr_ns
30243 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30245 if (cxx_dialect < cxx17)
30246 pedwarn (input_location, OPT_Wc__17_extensions,
30247 "attribute using prefix only available "
30248 "with %<-std=c++17%> or %<-std=gnu++17%>");
30250 cp_lexer_consume_token (parser->lexer);
30251 cp_lexer_consume_token (parser->lexer);
30252 cp_lexer_consume_token (parser->lexer);
30254 else
30255 attr_ns = NULL_TREE;
30258 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30260 finish_attrs:
30261 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30262 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30263 cp_parser_skip_to_end_of_statement (parser);
30264 else
30265 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30266 when we are sure that we have actually parsed them. */
30267 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30269 else
30271 tree alignas_expr;
30273 /* Look for an alignment-specifier. */
30275 token = cp_lexer_peek_token (parser->lexer);
30277 if (token->type != CPP_KEYWORD
30278 || token->keyword != RID_ALIGNAS)
30279 return NULL_TREE;
30281 cp_lexer_consume_token (parser->lexer);
30282 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30284 matching_parens parens;
30285 if (!parens.require_open (parser))
30286 return error_mark_node;
30288 cp_parser_parse_tentatively (parser);
30289 alignas_expr = cp_parser_type_id (parser);
30291 if (!cp_parser_parse_definitely (parser))
30293 alignas_expr = cp_parser_assignment_expression (parser);
30294 if (alignas_expr == error_mark_node)
30295 cp_parser_skip_to_end_of_statement (parser);
30296 if (alignas_expr == NULL_TREE
30297 || alignas_expr == error_mark_node)
30298 return alignas_expr;
30301 alignas_expr = cxx_alignas_expr (alignas_expr);
30302 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30304 /* Handle alignas (pack...). */
30305 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30307 cp_lexer_consume_token (parser->lexer);
30308 alignas_expr = make_pack_expansion (alignas_expr);
30311 /* Something went wrong, so don't build the attribute. */
30312 if (alignas_expr == error_mark_node)
30313 return error_mark_node;
30315 /* Missing ')' means the code cannot possibly be valid; go ahead
30316 and commit to make sure we issue a hard error. */
30317 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30318 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30319 cp_parser_commit_to_tentative_parse (parser);
30321 if (!parens.require_close (parser))
30322 return error_mark_node;
30324 /* Build the C++-11 representation of an 'aligned'
30325 attribute. */
30326 attributes
30327 = build_tree_list (build_tree_list (gnu_identifier,
30328 aligned_identifier), alignas_expr);
30331 return attributes;
30334 /* Parse a standard C++-11 attribute-specifier-seq.
30336 attribute-specifier-seq:
30337 attribute-specifier-seq [opt] attribute-specifier */
30339 static tree
30340 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30342 tree attr_specs = NULL_TREE;
30343 tree attr_last = NULL_TREE;
30345 /* Don't create wrapper nodes within attributes: the
30346 handlers don't know how to handle them. */
30347 auto_suppress_location_wrappers sentinel;
30349 while (true)
30351 tree attr_spec = cp_parser_std_attribute_spec (parser);
30352 if (attr_spec == NULL_TREE)
30353 break;
30354 if (attr_spec == error_mark_node)
30355 return error_mark_node;
30357 if (attr_last)
30358 TREE_CHAIN (attr_last) = attr_spec;
30359 else
30360 attr_specs = attr_last = attr_spec;
30361 attr_last = tree_last (attr_last);
30364 return attr_specs;
30367 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30368 return index of the first token after balanced-token, or N on failure. */
30370 static size_t
30371 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30373 size_t orig_n = n;
30374 int nparens = 0, nbraces = 0, nsquares = 0;
30376 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30378 case CPP_PRAGMA_EOL:
30379 if (!parser->lexer->in_pragma)
30380 break;
30381 /* FALLTHRU */
30382 case CPP_EOF:
30383 /* Ran out of tokens. */
30384 return orig_n;
30385 case CPP_OPEN_PAREN:
30386 ++nparens;
30387 break;
30388 case CPP_OPEN_BRACE:
30389 ++nbraces;
30390 break;
30391 case CPP_OPEN_SQUARE:
30392 ++nsquares;
30393 break;
30394 case CPP_CLOSE_PAREN:
30395 --nparens;
30396 break;
30397 case CPP_CLOSE_BRACE:
30398 --nbraces;
30399 break;
30400 case CPP_CLOSE_SQUARE:
30401 --nsquares;
30402 break;
30403 default:
30404 break;
30406 while (nparens || nbraces || nsquares);
30407 return n;
30410 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30411 return index of the first token after the GNU attribute tokens, or N on
30412 failure. */
30414 static size_t
30415 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30417 while (true)
30419 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30420 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30421 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30422 break;
30424 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30425 if (n2 == n + 2)
30426 break;
30427 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30428 break;
30429 n = n2 + 1;
30431 return n;
30434 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30435 next token), return index of the first token after the standard C++11
30436 attribute tokens, or N on failure. */
30438 static size_t
30439 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30441 while (true)
30443 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30444 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30446 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30447 if (n2 == n + 1)
30448 break;
30449 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30450 break;
30451 n = n2 + 1;
30453 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30454 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30456 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30457 if (n2 == n + 1)
30458 break;
30459 n = n2;
30461 else
30462 break;
30464 return n;
30467 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30468 as the next token), return index of the first token after the attribute
30469 tokens, or N on failure. */
30471 static size_t
30472 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30474 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30475 return cp_parser_skip_gnu_attributes_opt (parser, n);
30476 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30479 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30480 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30481 current value of the PEDANTIC flag, regardless of whether or not
30482 the `__extension__' keyword is present. The caller is responsible
30483 for restoring the value of the PEDANTIC flag. */
30485 static bool
30486 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30488 /* Save the old value of the PEDANTIC flag. */
30489 *saved_pedantic = pedantic;
30491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30493 /* Consume the `__extension__' token. */
30494 cp_lexer_consume_token (parser->lexer);
30495 /* We're not being pedantic while the `__extension__' keyword is
30496 in effect. */
30497 pedantic = 0;
30499 return true;
30502 return false;
30505 /* Parse a label declaration.
30507 label-declaration:
30508 __label__ label-declarator-seq ;
30510 label-declarator-seq:
30511 identifier , label-declarator-seq
30512 identifier */
30514 static void
30515 cp_parser_label_declaration (cp_parser* parser)
30517 /* Look for the `__label__' keyword. */
30518 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30520 while (true)
30522 tree identifier;
30524 /* Look for an identifier. */
30525 identifier = cp_parser_identifier (parser);
30526 /* If we failed, stop. */
30527 if (identifier == error_mark_node)
30528 break;
30529 /* Declare it as a label. */
30530 finish_label_decl (identifier);
30531 /* If the next token is a `;', stop. */
30532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30533 break;
30534 /* Look for the `,' separating the label declarations. */
30535 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30538 /* Look for the final `;'. */
30539 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30542 // -------------------------------------------------------------------------- //
30543 // Concept definitions
30545 static tree
30546 cp_parser_concept_definition (cp_parser *parser)
30548 /* A concept definition is an unevaluated context. */
30549 cp_unevaluated u;
30551 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30552 cp_lexer_consume_token (parser->lexer);
30554 cp_expr id = cp_parser_identifier (parser);
30555 if (id == error_mark_node)
30557 cp_parser_skip_to_end_of_statement (parser);
30558 cp_parser_consume_semicolon_at_end_of_statement (parser);
30559 return NULL_TREE;
30562 tree attrs = cp_parser_attributes_opt (parser);
30564 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30566 cp_parser_skip_to_end_of_statement (parser);
30567 cp_parser_consume_semicolon_at_end_of_statement (parser);
30568 return error_mark_node;
30571 processing_constraint_expression_sentinel parsing_constraint;
30572 tree init = cp_parser_constraint_expression (parser);
30573 if (init == error_mark_node)
30574 cp_parser_skip_to_end_of_statement (parser);
30576 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30577 but continue as if it were. */
30578 cp_parser_consume_semicolon_at_end_of_statement (parser);
30580 return finish_concept_definition (id, init, attrs);
30583 // -------------------------------------------------------------------------- //
30584 // Requires Clause
30586 /* Diagnose an expression that should appear in ()'s within a requires-clause
30587 and suggest where to place those parentheses. */
30589 static void
30590 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30592 error_at (loc, "expression must be enclosed in parentheses");
30595 static void
30596 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30598 gcc_rich_location richloc (loc);
30599 richloc.add_fixit_insert_before ("(");
30600 richloc.add_fixit_insert_after (")");
30601 error_at (&richloc, "expression must be enclosed in parentheses");
30604 /* Characterizes the likely kind of expression intended by a mis-written
30605 primary constraint. */
30606 enum primary_constraint_error
30608 pce_ok,
30609 pce_maybe_operator,
30610 pce_maybe_postfix
30613 /* Returns true if the token(s) following a primary-expression in a
30614 constraint-logical-* expression would require parentheses. */
30616 static primary_constraint_error
30617 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30619 cp_token *token = cp_lexer_peek_token (parser->lexer);
30620 switch (token->type)
30622 default:
30623 return pce_ok;
30625 case CPP_EQ:
30627 /* An equal sign may be part of the definition of a function,
30628 and not an assignment operator, when parsing the expression
30629 for a trailing requires-clause. For example:
30631 template<typename T>
30632 struct S {
30633 S() requires C<T> = default;
30636 Don't try to reparse this a binary operator. */
30637 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30638 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30639 return pce_ok;
30641 gcc_fallthrough ();
30644 /* Arithmetic operators. */
30645 case CPP_PLUS:
30646 case CPP_MINUS:
30647 case CPP_MULT:
30648 case CPP_DIV:
30649 case CPP_MOD:
30650 /* Bitwise operators. */
30651 case CPP_AND:
30652 case CPP_OR:
30653 case CPP_XOR:
30654 case CPP_RSHIFT:
30655 case CPP_LSHIFT:
30656 /* Relational operators. */
30657 case CPP_EQ_EQ:
30658 case CPP_NOT_EQ:
30659 case CPP_LESS:
30660 case CPP_GREATER:
30661 case CPP_LESS_EQ:
30662 case CPP_GREATER_EQ:
30663 case CPP_SPACESHIP:
30664 /* Pointer-to-member. */
30665 case CPP_DOT_STAR:
30666 case CPP_DEREF_STAR:
30667 /* Assignment operators. */
30668 case CPP_PLUS_EQ:
30669 case CPP_MINUS_EQ:
30670 case CPP_MULT_EQ:
30671 case CPP_DIV_EQ:
30672 case CPP_MOD_EQ:
30673 case CPP_AND_EQ:
30674 case CPP_OR_EQ:
30675 case CPP_XOR_EQ:
30676 case CPP_RSHIFT_EQ:
30677 case CPP_LSHIFT_EQ:
30678 /* Conditional operator */
30679 case CPP_QUERY:
30680 /* Unenclosed binary or conditional operator. */
30681 return pce_maybe_operator;
30683 case CPP_OPEN_PAREN:
30685 /* A primary constraint that precedes the parameter-list of a
30686 lambda expression is followed by an open paren.
30688 []<typename T> requires C (T a, T b) { ... }
30690 Don't try to re-parse this as a postfix expression. */
30691 if (lambda_p)
30692 return pce_ok;
30694 gcc_fallthrough ();
30696 case CPP_OPEN_SQUARE:
30698 /* A primary-constraint-expression followed by a '[[' is not a
30699 postfix expression. */
30700 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30701 return pce_ok;
30703 gcc_fallthrough ();
30705 case CPP_PLUS_PLUS:
30706 case CPP_MINUS_MINUS:
30707 case CPP_DOT:
30708 /* Unenclosed postfix operator. */
30709 return pce_maybe_postfix;
30711 case CPP_DEREF:
30712 /* A primary constraint that precedes the lambda-declarator of a
30713 lambda expression is followed by trailing return type.
30715 []<typename T> requires C -> void {}
30717 Don't try to re-parse this as a postfix expression in
30718 C++23 and later. In C++20 ( needs to come in between but we
30719 allow it to be omitted with pedwarn. */
30720 if (lambda_p)
30721 return pce_ok;
30722 /* Unenclosed postfix operator. */
30723 return pce_maybe_postfix;
30727 /* Returns true if the next token begins a unary expression, preceded by
30728 an operator or keyword. */
30730 static bool
30731 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30733 cp_token *token = cp_lexer_peek_token (parser->lexer);
30734 switch (token->type)
30736 case CPP_NOT:
30737 case CPP_PLUS:
30738 case CPP_MINUS:
30739 case CPP_MULT:
30740 case CPP_COMPL:
30741 case CPP_PLUS_PLUS:
30742 case CPP_MINUS_MINUS:
30743 return true;
30745 case CPP_KEYWORD:
30747 switch (token->keyword)
30749 case RID_STATCAST:
30750 case RID_DYNCAST:
30751 case RID_REINTCAST:
30752 case RID_CONSTCAST:
30753 case RID_TYPEID:
30754 case RID_SIZEOF:
30755 case RID_ALIGNOF:
30756 case RID_NOEXCEPT:
30757 case RID_NEW:
30758 case RID_DELETE:
30759 case RID_THROW:
30760 return true;
30762 default:
30763 break;
30767 default:
30768 break;
30771 return false;
30774 /* Parse a primary expression within a constraint. */
30776 static cp_expr
30777 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
30779 /* If this looks like a unary expression, parse it as such, but diagnose
30780 it as ill-formed; it requires parens. */
30781 if (cp_parser_unary_constraint_requires_parens (parser))
30783 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
30784 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
30785 return e;
30788 cp_lexer_save_tokens (parser->lexer);
30789 cp_id_kind idk;
30790 location_t loc = input_location;
30791 cp_expr expr = cp_parser_primary_expression (parser,
30792 /*address_p=*/false,
30793 /*cast_p=*/false,
30794 /*template_arg_p=*/false,
30795 &idk);
30796 expr.maybe_add_location_wrapper ();
30798 primary_constraint_error pce = pce_ok;
30799 if (expr != error_mark_node)
30801 /* The primary-expression could be part of an unenclosed non-logical
30802 compound expression. */
30803 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
30805 if (pce == pce_ok)
30807 cp_lexer_commit_tokens (parser->lexer);
30808 return finish_constraint_primary_expr (expr);
30811 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30812 error, but return the expression as if it were valid. */
30813 cp_lexer_rollback_tokens (parser->lexer);
30814 cp_parser_parse_tentatively (parser);
30815 if (pce == pce_maybe_operator)
30816 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30817 else
30818 expr = cp_parser_simple_cast_expression (parser);
30819 if (cp_parser_parse_definitely (parser))
30821 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
30822 return expr;
30825 /* Otherwise, something has gone very wrong, and we can't generate a more
30826 meaningful diagnostic or recover. */
30827 cp_parser_diagnose_ungrouped_constraint_plain (loc);
30828 return error_mark_node;
30831 /* Parse a constraint-logical-and-expression.
30833 constraint-logical-and-expression:
30834 primary-expression
30835 constraint-logical-and-expression '&&' primary-expression */
30837 static cp_expr
30838 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
30840 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30841 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
30843 cp_token *op = cp_lexer_consume_token (parser->lexer);
30844 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30845 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
30847 return lhs;
30850 /* Parse a constraint-logical-or-expression.
30852 constraint-logical-or-expression:
30853 constraint-logical-and-expression
30854 constraint-logical-or-expression '||' constraint-logical-and-expression */
30856 static cp_expr
30857 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
30859 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30860 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
30862 cp_token *op = cp_lexer_consume_token (parser->lexer);
30863 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30864 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
30866 return lhs;
30869 /* Parse the expression after a requires-clause. This has a different grammar
30870 than that in the concepts TS. */
30872 static tree
30873 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
30875 processing_constraint_expression_sentinel parsing_constraint;
30876 ++processing_template_decl;
30877 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
30878 --processing_template_decl;
30879 if (check_for_bare_parameter_packs (expr))
30880 expr = error_mark_node;
30881 return expr;
30884 /* Parse a expression after a requires clause.
30886 constraint-expression:
30887 logical-or-expression
30889 The required logical-or-expression must be a constant expression. Note
30890 that we don't check that the expression is constepxr here. We defer until
30891 we analyze constraints and then, we only check atomic constraints. */
30893 static tree
30894 cp_parser_constraint_expression (cp_parser *parser)
30896 processing_constraint_expression_sentinel parsing_constraint;
30897 ++processing_template_decl;
30898 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30899 PREC_NOT_OPERATOR, NULL);
30900 --processing_template_decl;
30901 if (check_for_bare_parameter_packs (expr))
30902 expr = error_mark_node;
30903 expr.maybe_add_location_wrapper ();
30904 return expr;
30907 /* Optionally parse a requires clause:
30909 requires-clause:
30910 `requires` constraint-logical-or-expression.
30911 [ConceptsTS]
30912 `requires constraint-expression.
30914 LAMBDA_P is true when the requires-clause is parsed before the
30915 parameter-list of a lambda-declarator. */
30917 static tree
30918 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30920 /* A requires clause is an unevaluated context. */
30921 cp_unevaluated u;
30923 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30924 if (tok->keyword != RID_REQUIRES)
30926 if (!flag_concepts && tok->type == CPP_NAME
30927 && tok->u.value == ridpointers[RID_REQUIRES])
30929 error_at (cp_lexer_peek_token (parser->lexer)->location,
30930 "%<requires%> only available with "
30931 "%<-std=c++20%> or %<-fconcepts%>");
30932 /* Parse and discard the requires-clause. */
30933 cp_lexer_consume_token (parser->lexer);
30934 cp_parser_constraint_expression (parser);
30936 return NULL_TREE;
30939 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30940 if (tok2->type == CPP_OPEN_BRACE)
30942 /* An opening brace following the start of a requires-clause is
30943 ill-formed; the user likely forgot the second `requires' that
30944 would start a requires-expression. */
30945 gcc_rich_location richloc (tok2->location);
30946 richloc.add_fixit_insert_after (tok->location, " requires");
30947 error_at (&richloc, "missing additional %<requires%> to start "
30948 "a requires-expression");
30949 /* Don't consume the `requires', so that it's reused as the start of a
30950 requires-expression. */
30952 else
30953 cp_lexer_consume_token (parser->lexer);
30955 if (!flag_concepts_ts)
30956 return cp_parser_requires_clause_expression (parser, lambda_p);
30957 else
30958 return cp_parser_constraint_expression (parser);
30961 /*---------------------------------------------------------------------------
30962 Requires expressions
30963 ---------------------------------------------------------------------------*/
30965 /* Parse a requires expression
30967 requirement-expression:
30968 'requires' requirement-parameter-list [opt] requirement-body */
30970 static tree
30971 cp_parser_requires_expression (cp_parser *parser)
30973 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30974 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30976 /* Avoid committing to outer tentative parse. */
30977 tentative_firewall firewall (parser);
30979 /* This is definitely a requires-expression. */
30980 cp_parser_commit_to_tentative_parse (parser);
30982 tree parms, reqs;
30984 /* Local parameters are delared as variables within the scope
30985 of the expression. They are not visible past the end of
30986 the expression. Expressions within the requires-expression
30987 are unevaluated. */
30988 struct scope_sentinel
30990 scope_sentinel ()
30992 ++cp_unevaluated_operand;
30993 begin_scope (sk_function_parms, NULL_TREE);
30994 current_binding_level->requires_expression = true;
30997 ~scope_sentinel ()
30999 pop_bindings_and_leave_scope ();
31000 --cp_unevaluated_operand;
31002 } s;
31004 /* Parse the optional parameter list. */
31005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31007 parms = cp_parser_requirement_parameter_list (parser);
31008 if (parms == error_mark_node)
31009 return error_mark_node;
31011 else
31012 parms = NULL_TREE;
31014 /* Parse the requirement body. */
31015 ++processing_template_decl;
31016 reqs = cp_parser_requirement_body (parser);
31017 --processing_template_decl;
31018 if (reqs == error_mark_node)
31019 return error_mark_node;
31022 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31023 the parm chain. */
31024 grokparms (parms, &parms);
31025 loc = make_location (loc, loc, parser->lexer);
31026 tree expr = finish_requires_expr (loc, parms, reqs);
31027 if (!processing_template_decl)
31029 /* Perform semantic processing now to diagnose any invalid types and
31030 expressions. */
31031 int saved_errorcount = errorcount;
31032 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31033 if (errorcount > saved_errorcount)
31034 return error_mark_node;
31036 return expr;
31039 /* Parse a parameterized requirement.
31041 requirement-parameter-list:
31042 '(' parameter-declaration-clause ')' */
31044 static tree
31045 cp_parser_requirement_parameter_list (cp_parser *parser)
31047 matching_parens parens;
31048 if (!parens.require_open (parser))
31049 return error_mark_node;
31051 tree parms = (cp_parser_parameter_declaration_clause
31052 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31054 if (!parens.require_close (parser))
31055 return error_mark_node;
31057 /* Modify the declared parameters by removing their context
31058 so they don't refer to the enclosing scope and explicitly
31059 indicating that they are constraint variables. */
31060 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31062 if (parm == void_list_node || parm == explicit_void_list_node)
31063 break;
31064 tree decl = TREE_VALUE (parm);
31065 if (decl != error_mark_node)
31067 DECL_CONTEXT (decl) = NULL_TREE;
31068 CONSTRAINT_VAR_P (decl) = true;
31072 return parms;
31075 /* Parse the body of a requirement.
31077 requirement-body:
31078 '{' requirement-list '}' */
31079 static tree
31080 cp_parser_requirement_body (cp_parser *parser)
31082 matching_braces braces;
31083 if (!braces.require_open (parser))
31084 return error_mark_node;
31086 tree reqs = cp_parser_requirement_seq (parser);
31088 if (!braces.require_close (parser))
31089 return error_mark_node;
31091 return reqs;
31094 /* Parse a sequence of requirements.
31096 requirement-seq:
31097 requirement
31098 requirement-seq requirement */
31100 static tree
31101 cp_parser_requirement_seq (cp_parser *parser)
31103 tree result = NULL_TREE;
31106 tree req = cp_parser_requirement (parser);
31107 if (req != error_mark_node)
31108 result = tree_cons (NULL_TREE, req, result);
31110 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31111 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31113 /* If there are no valid requirements, this is not a valid expression. */
31114 if (!result)
31115 return error_mark_node;
31117 /* Reverse the order of requirements so they are analyzed in order. */
31118 return nreverse (result);
31121 /* Parse a syntactic requirement or type requirement.
31123 requirement:
31124 simple-requirement
31125 compound-requirement
31126 type-requirement
31127 nested-requirement */
31129 static tree
31130 cp_parser_requirement (cp_parser *parser)
31132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31133 return cp_parser_compound_requirement (parser);
31134 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31136 /* It's probably a type-requirement. */
31137 cp_parser_parse_tentatively (parser);
31138 tree req = cp_parser_type_requirement (parser);
31139 if (cp_parser_parse_definitely (parser))
31140 return req;
31141 /* No, maybe it's something like typename T::type(); */
31142 cp_parser_parse_tentatively (parser);
31143 req = cp_parser_simple_requirement (parser);
31144 if (cp_parser_parse_definitely (parser))
31145 return req;
31146 /* Non-tentative for the error. */
31147 return cp_parser_type_requirement (parser);
31149 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31150 return cp_parser_nested_requirement (parser);
31151 else
31152 return cp_parser_simple_requirement (parser);
31155 /* Parse a simple requirement.
31157 simple-requirement:
31158 expression ';' */
31160 static tree
31161 cp_parser_simple_requirement (cp_parser *parser)
31163 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31164 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31165 if (expr == error_mark_node)
31166 cp_parser_skip_to_end_of_statement (parser);
31168 cp_parser_consume_semicolon_at_end_of_statement (parser);
31170 if (!expr || expr == error_mark_node)
31171 return error_mark_node;
31173 /* Sometimes we don't get locations, so use the cached token location
31174 as a reasonable approximation. */
31175 if (expr.get_location() == UNKNOWN_LOCATION)
31176 expr.set_location (start);
31178 for (tree t = expr; ; )
31180 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31181 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31183 t = TREE_OPERAND (t, 0);
31184 continue;
31186 if (concept_check_p (t))
31188 gcc_rich_location richloc (get_start (start));
31189 richloc.add_fixit_insert_before (start, "requires ");
31190 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31191 "if a concept-id is a valid expression; add "
31192 "%<requires%> to check satisfaction");
31194 break;
31197 return finish_simple_requirement (expr.get_location (), expr);
31200 /* Parse a type requirement
31202 type-requirement
31203 nested-name-specifier [opt] required-type-name ';'
31205 required-type-name:
31206 type-name
31207 'template' [opt] simple-template-id */
31209 static tree
31210 cp_parser_type_requirement (cp_parser *parser)
31212 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31213 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31215 // Save the scope before parsing name specifiers.
31216 tree saved_scope = parser->scope;
31217 tree saved_object_scope = parser->object_scope;
31218 tree saved_qualifying_scope = parser->qualifying_scope;
31219 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31220 cp_parser_nested_name_specifier_opt (parser,
31221 /*typename_keyword_p=*/true,
31222 /*check_dependency_p=*/true,
31223 /*type_p=*/true,
31224 /*is_declaration=*/false);
31226 tree type;
31227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31229 cp_lexer_consume_token (parser->lexer);
31230 type = cp_parser_template_id (parser,
31231 /*template_keyword_p=*/true,
31232 /*check_dependency_p=*/true,
31233 /*tag_type=*/none_type,
31234 /*is_declaration=*/false);
31235 type = make_typename_type (parser->scope, type, typename_type,
31236 /*complain=*/tf_error);
31238 else
31239 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31241 if (TREE_CODE (type) == TYPE_DECL)
31242 type = TREE_TYPE (type);
31244 parser->scope = saved_scope;
31245 parser->object_scope = saved_object_scope;
31246 parser->qualifying_scope = saved_qualifying_scope;
31248 if (type == error_mark_node)
31249 cp_parser_skip_to_end_of_statement (parser);
31251 cp_parser_consume_semicolon_at_end_of_statement (parser);
31253 if (type == error_mark_node)
31254 return error_mark_node;
31256 loc = make_location (loc, start_tok->location, parser->lexer);
31257 return finish_type_requirement (loc, type);
31260 /* Parse a compound requirement
31262 compound-requirement:
31263 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31265 static tree
31266 cp_parser_compound_requirement (cp_parser *parser)
31268 /* Parse an expression enclosed in '{ }'s. */
31269 matching_braces braces;
31270 if (!braces.require_open (parser))
31271 return error_mark_node;
31273 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31275 tree expr = cp_parser_expression (parser, NULL, false, false);
31276 if (expr == error_mark_node)
31277 cp_parser_skip_to_closing_brace (parser);
31279 if (!braces.require_close (parser))
31281 cp_parser_skip_to_end_of_statement (parser);
31282 cp_parser_consume_semicolon_at_end_of_statement (parser);
31283 return error_mark_node;
31286 /* If the expression was invalid, skip the remainder of the requirement. */
31287 if (!expr || expr == error_mark_node)
31289 cp_parser_skip_to_end_of_statement (parser);
31290 cp_parser_consume_semicolon_at_end_of_statement (parser);
31291 return error_mark_node;
31294 /* Parse the optional noexcept. */
31295 bool noexcept_p = false;
31296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31298 cp_lexer_consume_token (parser->lexer);
31299 noexcept_p = true;
31302 /* Parse the optional trailing return type. */
31303 tree type = NULL_TREE;
31304 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31306 cp_lexer_consume_token (parser->lexer);
31307 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31309 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31310 parser->in_result_type_constraint_p = true;
31311 /* C++20 allows either a type-id or a type-constraint. Parsing
31312 a type-id will subsume the parsing for a type-constraint but
31313 allow for more syntactic forms (e.g., const C<T>*). */
31314 type = cp_parser_trailing_type_id (parser);
31315 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31316 if (type == error_mark_node)
31317 return error_mark_node;
31319 location_t type_loc = make_location (tok->location, tok->location,
31320 parser->lexer);
31322 /* Check that we haven't written something like 'const C<T>*'. */
31323 if (type_uses_auto (type))
31325 if (!is_auto (type))
31327 error_at (type_loc,
31328 "result type is not a plain type-constraint");
31329 cp_parser_consume_semicolon_at_end_of_statement (parser);
31330 return error_mark_node;
31333 else if (!flag_concepts_ts)
31334 /* P1452R2 removed the trailing-return-type option. */
31335 error_at (type_loc,
31336 "return-type-requirement is not a type-constraint");
31339 location_t loc = make_location (expr_token->location,
31340 braces.open_location (),
31341 parser->lexer);
31343 cp_parser_consume_semicolon_at_end_of_statement (parser);
31345 if (expr == error_mark_node || type == error_mark_node)
31346 return error_mark_node;
31348 return finish_compound_requirement (loc, expr, type, noexcept_p);
31351 /* Parse a nested requirement. This is the same as a requires clause.
31353 nested-requirement:
31354 requires-clause */
31356 static tree
31357 cp_parser_nested_requirement (cp_parser *parser)
31359 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31360 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31361 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31362 tree req = cp_parser_constraint_expression (parser);
31363 if (req == error_mark_node)
31364 cp_parser_skip_to_end_of_statement (parser);
31365 loc = make_location (loc, tok->location, parser->lexer);
31366 cp_parser_consume_semicolon_at_end_of_statement (parser);
31367 if (req == error_mark_node)
31368 return error_mark_node;
31369 return finish_nested_requirement (loc, req);
31372 /* Support Functions */
31374 /* Return the appropriate prefer_type argument for lookup_name based on
31375 tag_type. */
31377 static inline LOOK_want
31378 prefer_type_arg (tag_types tag_type)
31380 switch (tag_type)
31382 case none_type: return LOOK_want::NORMAL; // No preference.
31383 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31384 default: return LOOK_want::TYPE; // Type only.
31388 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31389 NAME should have one of the representations used for an
31390 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31391 is returned. If PARSER->SCOPE is a dependent type, then a
31392 SCOPE_REF is returned.
31394 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31395 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31396 was formed. Abstractly, such entities should not be passed to this
31397 function, because they do not need to be looked up, but it is
31398 simpler to check for this special case here, rather than at the
31399 call-sites.
31401 In cases not explicitly covered above, this function returns a
31402 DECL, OVERLOAD, or baselink representing the result of the lookup.
31403 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31404 is returned.
31406 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31407 (e.g., "struct") that was used. In that case bindings that do not
31408 refer to types are ignored.
31410 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31411 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31413 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31414 are ignored.
31416 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31417 types.
31419 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31420 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31421 NULL_TREE otherwise. */
31423 static cp_expr
31424 cp_parser_lookup_name (cp_parser *parser, tree name,
31425 enum tag_types tag_type,
31426 int is_template,
31427 bool is_namespace,
31428 bool check_dependency,
31429 tree *ambiguous_decls,
31430 location_t name_location)
31432 tree decl;
31433 tree object_type = parser->context->object_type;
31435 /* Assume that the lookup will be unambiguous. */
31436 if (ambiguous_decls)
31437 *ambiguous_decls = NULL_TREE;
31439 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31440 no longer valid. Note that if we are parsing tentatively, and
31441 the parse fails, OBJECT_TYPE will be automatically restored. */
31442 parser->context->object_type = NULL_TREE;
31444 if (name == error_mark_node)
31445 return error_mark_node;
31447 /* A template-id has already been resolved; there is no lookup to
31448 do. */
31449 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31450 return name;
31451 if (BASELINK_P (name))
31453 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31454 == TEMPLATE_ID_EXPR);
31455 return name;
31458 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31459 it should already have been checked to make sure that the name
31460 used matches the type being destroyed. */
31461 if (TREE_CODE (name) == BIT_NOT_EXPR)
31463 tree type;
31465 /* Figure out to which type this destructor applies. */
31466 if (parser->scope)
31467 type = parser->scope;
31468 else if (object_type)
31469 type = object_type;
31470 else
31471 type = current_class_type;
31472 /* If that's not a class type, there is no destructor. */
31473 if (!type || !CLASS_TYPE_P (type))
31474 return error_mark_node;
31476 /* In a non-static member function, check implicit this->. */
31477 if (current_class_ref)
31478 return lookup_destructor (current_class_ref, parser->scope, name,
31479 tf_warning_or_error);
31481 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31482 lazily_declare_fn (sfk_destructor, type);
31484 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31485 return dtor;
31487 return error_mark_node;
31490 /* By this point, the NAME should be an ordinary identifier. If
31491 the id-expression was a qualified name, the qualifying scope is
31492 stored in PARSER->SCOPE at this point. */
31493 gcc_assert (identifier_p (name));
31495 /* Perform the lookup. */
31496 if (parser->scope)
31498 bool dependent_p;
31500 if (parser->scope == error_mark_node)
31501 return error_mark_node;
31503 /* If the SCOPE is dependent, the lookup must be deferred until
31504 the template is instantiated -- unless we are explicitly
31505 looking up names in uninstantiated templates. Even then, we
31506 cannot look up the name if the scope is not a class type; it
31507 might, for example, be a template type parameter. */
31508 dependent_p = (TYPE_P (parser->scope)
31509 && dependent_scope_p (parser->scope));
31510 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31511 && dependent_p)
31512 /* Defer lookup. */
31513 decl = error_mark_node;
31514 else
31516 tree pushed_scope = NULL_TREE;
31518 /* If PARSER->SCOPE is a dependent type, then it must be a
31519 class type, and we must not be checking dependencies;
31520 otherwise, we would have processed this lookup above. So
31521 that PARSER->SCOPE is not considered a dependent base by
31522 lookup_member, we must enter the scope here. */
31523 if (dependent_p)
31524 pushed_scope = push_scope (parser->scope);
31526 /* If the PARSER->SCOPE is a template specialization, it
31527 may be instantiated during name lookup. In that case,
31528 errors may be issued. Even if we rollback the current
31529 tentative parse, those errors are valid. */
31530 decl = lookup_qualified_name (parser->scope, name,
31531 prefer_type_arg (tag_type),
31532 /*complain=*/true);
31534 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31535 lookup result and the nested-name-specifier nominates a class C:
31536 * if the name specified after the nested-name-specifier, when
31537 looked up in C, is the injected-class-name of C (Clause 9), or
31538 * if the name specified after the nested-name-specifier is the
31539 same as the identifier or the simple-template-id's template-
31540 name in the last component of the nested-name-specifier,
31541 the name is instead considered to name the constructor of
31542 class C. [ Note: for example, the constructor is not an
31543 acceptable lookup result in an elaborated-type-specifier so
31544 the constructor would not be used in place of the
31545 injected-class-name. --end note ] Such a constructor name
31546 shall be used only in the declarator-id of a declaration that
31547 names a constructor or in a using-declaration. */
31548 if (tag_type == none_type
31549 && DECL_SELF_REFERENCE_P (decl)
31550 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31551 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31552 prefer_type_arg (tag_type),
31553 /*complain=*/true);
31555 if (pushed_scope)
31556 pop_scope (pushed_scope);
31559 /* If the scope is a dependent type and either we deferred lookup or
31560 we did lookup but didn't find the name, rememeber the name. */
31561 if (decl == error_mark_node && TYPE_P (parser->scope)
31562 && dependent_type_p (parser->scope))
31564 if (tag_type)
31566 tree type;
31568 /* The resolution to Core Issue 180 says that `struct
31569 A::B' should be considered a type-name, even if `A'
31570 is dependent. */
31571 type = make_typename_type (parser->scope, name, tag_type,
31572 /*complain=*/tf_error);
31573 if (type != error_mark_node)
31574 decl = TYPE_NAME (type);
31576 else if (is_template
31577 && (cp_parser_next_token_ends_template_argument_p (parser)
31578 || cp_lexer_next_token_is (parser->lexer,
31579 CPP_CLOSE_PAREN)))
31580 decl = make_unbound_class_template (parser->scope,
31581 name, NULL_TREE,
31582 /*complain=*/tf_error);
31583 else
31584 decl = build_qualified_name (/*type=*/NULL_TREE,
31585 parser->scope, name,
31586 is_template);
31588 parser->qualifying_scope = parser->scope;
31589 parser->object_scope = NULL_TREE;
31591 else if (object_type)
31593 bool dep = dependent_scope_p (object_type);
31595 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31596 OBJECT_TYPE is not a class. */
31597 if (!dep && CLASS_TYPE_P (object_type))
31598 /* If the OBJECT_TYPE is a template specialization, it may
31599 be instantiated during name lookup. In that case, errors
31600 may be issued. Even if we rollback the current tentative
31601 parse, those errors are valid. */
31602 decl = lookup_member (object_type,
31603 name,
31604 /*protect=*/0,
31605 /*prefer_type=*/tag_type != none_type,
31606 tf_warning_or_error);
31607 else
31608 decl = NULL_TREE;
31610 /* If we didn't find a member and have dependent bases, the member lookup
31611 is now dependent. */
31612 if (!dep && !decl && any_dependent_bases_p (object_type))
31613 dep = true;
31615 if (dep && is_template == 2)
31616 /* The template keyword specifies a dependent template. */;
31617 else if (!decl)
31618 /* Look it up in the enclosing context. DR 141: When looking for a
31619 template-name after -> or ., only consider class templates. */
31620 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31621 /* DR 141: When looking in the
31622 current enclosing context for a
31623 template-name after -> or ., only
31624 consider class templates. */
31625 : is_template ? LOOK_want::TYPE
31626 : prefer_type_arg (tag_type));
31628 /* If we did unqualified lookup of a dependent member-qualified name and
31629 found something, do we want to use it? P1787 clarified that we need
31630 to look in the object scope first even if it's dependent, but for now
31631 let's still use it in some cases.
31632 FIXME remember unqualified lookup result to use if member lookup fails
31633 at instantiation time. */
31634 if (decl && dep && is_template)
31636 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31637 /* Only use the unqualified class template lookup if we're actually
31638 looking at a template arg list. */
31639 if (!cp_parser_skip_entire_template_parameter_list (parser))
31640 decl = NULL_TREE;
31643 /* If we know we're looking for a type (e.g. A in p->A::x),
31644 mock up a typename. */
31645 if (!decl && dep && tag_type != none_type)
31647 tree type = build_typename_type (object_type, name, name,
31648 typename_type);
31649 decl = TYPE_NAME (type);
31652 parser->object_scope = object_type;
31653 parser->qualifying_scope = NULL_TREE;
31655 else
31657 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31658 : prefer_type_arg (tag_type));
31659 parser->qualifying_scope = NULL_TREE;
31660 parser->object_scope = NULL_TREE;
31663 /* If the lookup failed, let our caller know. */
31664 if (!decl || decl == error_mark_node)
31665 return error_mark_node;
31667 /* If we have resolved the name of a member declaration, check to
31668 see if the declaration is accessible. When the name resolves to
31669 set of overloaded functions, accessibility is checked when
31670 overload resolution is done. If we have a TREE_LIST, then the lookup
31671 is either ambiguous or it found multiple injected-class-names, the
31672 accessibility of which is trivially satisfied.
31674 During an explicit instantiation, access is not checked at all,
31675 as per [temp.explicit]. */
31676 if (DECL_P (decl))
31677 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31678 tf_warning_or_error);
31680 /* Pull out the template from an injected-class-name (or multiple). */
31681 if (is_template)
31682 decl = maybe_get_template_decl_from_type_decl (decl);
31684 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31685 if (TREE_CODE (decl) == TREE_LIST)
31687 if (ambiguous_decls)
31688 *ambiguous_decls = decl;
31689 /* The error message we have to print is too complicated for
31690 cp_parser_error, so we incorporate its actions directly. */
31691 if (!cp_parser_simulate_error (parser))
31693 error_at (name_location, "reference to %qD is ambiguous",
31694 name);
31695 print_candidates (decl);
31697 return error_mark_node;
31700 gcc_assert (DECL_P (decl)
31701 || TREE_CODE (decl) == OVERLOAD
31702 || TREE_CODE (decl) == SCOPE_REF
31703 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31704 || BASELINK_P (decl));
31706 maybe_record_typedef_use (decl);
31708 return cp_expr (decl, name_location);
31711 /* Like cp_parser_lookup_name, but for use in the typical case where
31712 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31713 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31715 static tree
31716 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31718 return cp_parser_lookup_name (parser, name,
31719 none_type,
31720 /*is_template=*/false,
31721 /*is_namespace=*/false,
31722 /*check_dependency=*/true,
31723 /*ambiguous_decls=*/NULL,
31724 location);
31727 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31728 the current context, return the TYPE_DECL. If TAG_NAME_P is
31729 true, the DECL indicates the class being defined in a class-head,
31730 or declared in an elaborated-type-specifier.
31732 Otherwise, return DECL. */
31734 static tree
31735 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31737 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31738 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
31740 struct A {
31741 template <typename T> struct B;
31744 template <typename T> struct A::B {};
31746 Similarly, in an elaborated-type-specifier:
31748 namespace N { struct X{}; }
31750 struct A {
31751 template <typename T> friend struct N::X;
31754 However, if the DECL refers to a class type, and we are in
31755 the scope of the class, then the name lookup automatically
31756 finds the TYPE_DECL created by build_self_reference rather
31757 than a TEMPLATE_DECL. For example, in:
31759 template <class T> struct S {
31760 S s;
31763 there is no need to handle such case. */
31765 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
31766 return DECL_TEMPLATE_RESULT (decl);
31768 return decl;
31771 /* If too many, or too few, template-parameter lists apply to the
31772 declarator, issue an error message. Returns TRUE if all went well,
31773 and FALSE otherwise. */
31775 static bool
31776 cp_parser_check_declarator_template_parameters (cp_parser* parser,
31777 cp_declarator *declarator,
31778 location_t declarator_location)
31780 switch (declarator->kind)
31782 case cdk_id:
31784 unsigned num_templates = 0;
31785 tree scope = declarator->u.id.qualifying_scope;
31786 bool template_id_p = false;
31788 if (scope)
31789 num_templates = num_template_headers_for_class (scope);
31790 else if (TREE_CODE (declarator->u.id.unqualified_name)
31791 == TEMPLATE_ID_EXPR)
31793 /* If the DECLARATOR has the form `X<y>' then it uses one
31794 additional level of template parameters. */
31795 ++num_templates;
31796 template_id_p = true;
31799 return cp_parser_check_template_parameters
31800 (parser, num_templates, template_id_p, declarator_location,
31801 declarator);
31804 case cdk_function:
31805 case cdk_array:
31806 case cdk_pointer:
31807 case cdk_reference:
31808 case cdk_ptrmem:
31809 return (cp_parser_check_declarator_template_parameters
31810 (parser, declarator->declarator, declarator_location));
31812 case cdk_decomp:
31813 case cdk_error:
31814 return true;
31816 default:
31817 gcc_unreachable ();
31819 return false;
31822 /* NUM_TEMPLATES were used in the current declaration. If that is
31823 invalid, return FALSE and issue an error messages. Otherwise,
31824 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31825 declarator and we can print more accurate diagnostics. */
31827 static bool
31828 cp_parser_check_template_parameters (cp_parser* parser,
31829 unsigned num_templates,
31830 bool template_id_p,
31831 location_t location,
31832 cp_declarator *declarator)
31834 /* If there are the same number of template classes and parameter
31835 lists, that's OK. */
31836 if (parser->num_template_parameter_lists == num_templates)
31837 return true;
31838 /* If there are more, but only one more, and the name ends in an identifier,
31839 then we are declaring a primary template. That's OK too. */
31840 if (!template_id_p
31841 && parser->num_template_parameter_lists == num_templates + 1)
31842 return true;
31844 if (cp_parser_simulate_error (parser))
31845 return false;
31847 /* If there are more template classes than parameter lists, we have
31848 something like:
31850 template <class T> void S<T>::R<T>::f (); */
31851 if (parser->num_template_parameter_lists < num_templates)
31853 if (declarator && !current_function_decl)
31854 error_at (location, "specializing member %<%T::%E%> "
31855 "requires %<template<>%> syntax",
31856 declarator->u.id.qualifying_scope,
31857 declarator->u.id.unqualified_name);
31858 else if (declarator)
31859 error_at (location, "invalid declaration of %<%T::%E%>",
31860 declarator->u.id.qualifying_scope,
31861 declarator->u.id.unqualified_name);
31862 else
31863 error_at (location, "too few template-parameter-lists");
31864 return false;
31866 /* Otherwise, there are too many template parameter lists. We have
31867 something like:
31869 template <class T> template <class U> void S::f(); */
31870 error_at (location, "too many template-parameter-lists");
31871 return false;
31874 /* Parse an optional `::' token indicating that the following name is
31875 from the global namespace. If so, PARSER->SCOPE is set to the
31876 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31877 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31878 Returns the new value of PARSER->SCOPE, if the `::' token is
31879 present, and NULL_TREE otherwise. */
31881 static tree
31882 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
31884 cp_token *token;
31886 /* Peek at the next token. */
31887 token = cp_lexer_peek_token (parser->lexer);
31888 /* If we're looking at a `::' token then we're starting from the
31889 global namespace, not our current location. */
31890 if (token->type == CPP_SCOPE)
31892 /* Consume the `::' token. */
31893 cp_lexer_consume_token (parser->lexer);
31894 /* Set the SCOPE so that we know where to start the lookup. */
31895 parser->scope = global_namespace;
31896 parser->qualifying_scope = global_namespace;
31897 parser->object_scope = NULL_TREE;
31899 return parser->scope;
31901 else if (!current_scope_valid_p)
31903 parser->scope = NULL_TREE;
31904 parser->qualifying_scope = NULL_TREE;
31905 parser->object_scope = NULL_TREE;
31908 return NULL_TREE;
31911 /* Returns TRUE if the upcoming token sequence is the start of a
31912 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31913 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31914 is used to control type-specifier parsing. */
31916 static bool
31917 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31918 bool friend_p)
31920 bool constructor_p;
31921 bool outside_class_specifier_p;
31922 tree nested_name_specifier;
31923 cp_token *next_token;
31925 /* The common case is that this is not a constructor declarator, so
31926 try to avoid doing lots of work if at all possible. It's not
31927 valid declare a constructor at function scope. */
31928 if (parser->in_function_body)
31929 return false;
31930 /* And only certain tokens can begin a constructor declarator. */
31931 next_token = cp_lexer_peek_token (parser->lexer);
31932 if (next_token->type != CPP_NAME
31933 && next_token->type != CPP_SCOPE
31934 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31935 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31936 declarator-id of a constructor or destructor. */
31937 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31938 return false;
31940 /* Parse tentatively; we are going to roll back all of the tokens
31941 consumed here. */
31942 cp_parser_parse_tentatively (parser);
31943 /* Assume that we are looking at a constructor declarator. */
31944 constructor_p = true;
31946 /* Look for the optional `::' operator. */
31947 cp_parser_global_scope_opt (parser,
31948 /*current_scope_valid_p=*/false);
31949 /* Look for the nested-name-specifier. */
31950 nested_name_specifier
31951 = (cp_parser_nested_name_specifier_opt (parser,
31952 /*typename_keyword_p=*/false,
31953 /*check_dependency_p=*/false,
31954 /*type_p=*/false,
31955 /*is_declaration=*/false));
31957 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31958 if (nested_name_specifier
31959 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31961 tree s = resolve_typename_type (nested_name_specifier,
31962 /*only_current_p=*/false);
31963 if (TREE_CODE (s) != TYPENAME_TYPE)
31964 nested_name_specifier = s;
31967 outside_class_specifier_p = (!at_class_scope_p ()
31968 || !TYPE_BEING_DEFINED (current_class_type)
31969 || friend_p);
31971 /* Outside of a class-specifier, there must be a
31972 nested-name-specifier. Except in C++17 mode, where we
31973 might be declaring a guiding declaration. */
31974 if (!nested_name_specifier && outside_class_specifier_p
31975 && cxx_dialect < cxx17)
31976 constructor_p = false;
31977 else if (nested_name_specifier == error_mark_node)
31978 constructor_p = false;
31980 /* If we have a class scope, this is easy; DR 147 says that S::S always
31981 names the constructor, and no other qualified name could. */
31982 if (constructor_p && nested_name_specifier
31983 && CLASS_TYPE_P (nested_name_specifier))
31985 tree id = cp_parser_unqualified_id (parser,
31986 /*template_keyword_p=*/false,
31987 /*check_dependency_p=*/false,
31988 /*declarator_p=*/true,
31989 /*optional_p=*/false);
31990 if (is_overloaded_fn (id))
31991 id = DECL_NAME (get_first_fn (id));
31992 if (!constructor_name_p (id, nested_name_specifier))
31993 constructor_p = false;
31995 /* If we still think that this might be a constructor-declarator,
31996 look for a class-name. */
31997 else if (constructor_p)
31999 /* If we have:
32001 template <typename T> struct S {
32002 S();
32005 we must recognize that the nested `S' names a class. */
32006 if (cxx_dialect >= cxx17)
32007 cp_parser_parse_tentatively (parser);
32009 tree type_decl;
32010 type_decl = cp_parser_class_name (parser,
32011 /*typename_keyword_p=*/false,
32012 /*template_keyword_p=*/false,
32013 none_type,
32014 /*check_dependency_p=*/false,
32015 /*class_head_p=*/false,
32016 /*is_declaration=*/false);
32018 if (cxx_dialect >= cxx17
32019 && !cp_parser_parse_definitely (parser))
32021 type_decl = NULL_TREE;
32022 tree tmpl = cp_parser_template_name (parser,
32023 /*template_keyword*/false,
32024 /*check_dependency_p*/false,
32025 /*is_declaration*/false,
32026 none_type,
32027 /*is_identifier*/NULL);
32028 if (DECL_CLASS_TEMPLATE_P (tmpl)
32029 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32030 /* It's a deduction guide, return true. */;
32031 else
32032 cp_parser_simulate_error (parser);
32035 /* If there was no class-name, then this is not a constructor.
32036 Otherwise, if we are in a class-specifier and we aren't
32037 handling a friend declaration, check that its type matches
32038 current_class_type (c++/38313). Note: error_mark_node
32039 is left alone for error recovery purposes. */
32040 constructor_p = (!cp_parser_error_occurred (parser)
32041 && (outside_class_specifier_p
32042 || type_decl == NULL_TREE
32043 || type_decl == error_mark_node
32044 || same_type_p (current_class_type,
32045 TREE_TYPE (type_decl))));
32047 /* If we're still considering a constructor, we have to see a `(',
32048 to begin the parameter-declaration-clause, followed by either a
32049 `)', an `...', or a decl-specifier. We need to check for a
32050 type-specifier to avoid being fooled into thinking that:
32052 S (f) (int);
32054 is a constructor. (It is actually a function named `f' that
32055 takes one parameter (of type `int') and returns a value of type
32056 `S'. */
32057 if (constructor_p
32058 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32059 constructor_p = false;
32061 if (constructor_p
32062 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32063 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32064 /* A parameter declaration begins with a decl-specifier,
32065 which is either the "attribute" keyword, a storage class
32066 specifier, or (usually) a type-specifier. */
32067 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32068 /* GNU attributes can actually appear both at the start of
32069 a parameter and parenthesized declarator.
32070 S (__attribute__((unused)) int);
32071 is a constructor, but
32072 S (__attribute__((unused)) foo) (int);
32073 is a function declaration. [[attribute]] can appear in the
32074 first form too, but not in the second form. */
32075 && !cp_next_tokens_can_be_std_attribute_p (parser))
32077 tree type;
32078 tree pushed_scope = NULL_TREE;
32079 unsigned saved_num_template_parameter_lists;
32081 if (cp_parser_allow_gnu_extensions_p (parser)
32082 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32084 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32085 while (--n)
32086 cp_lexer_consume_token (parser->lexer);
32089 /* Names appearing in the type-specifier should be looked up
32090 in the scope of the class. */
32091 if (current_class_type)
32092 type = NULL_TREE;
32093 else if (type_decl)
32095 type = TREE_TYPE (type_decl);
32096 if (TREE_CODE (type) == TYPENAME_TYPE)
32098 type = resolve_typename_type (type,
32099 /*only_current_p=*/false);
32100 if (TREE_CODE (type) == TYPENAME_TYPE)
32102 cp_parser_abort_tentative_parse (parser);
32103 return false;
32106 pushed_scope = push_scope (type);
32109 /* Inside the constructor parameter list, surrounding
32110 template-parameter-lists do not apply. */
32111 saved_num_template_parameter_lists
32112 = parser->num_template_parameter_lists;
32113 parser->num_template_parameter_lists = 0;
32115 /* Look for the type-specifier. It's not optional, but its typename
32116 might be. Unless this is a friend declaration; we don't want to
32117 treat
32119 friend S (T::fn)(int);
32121 as a constructor, but with P0634, we might assume a type when
32122 looking for the type-specifier. It is actually a function named
32123 `T::fn' that takes one parameter (of type `int') and returns a
32124 value of type `S'. Constructors can be friends, but they must
32125 use a qualified name.
32127 Parse with an empty set of declaration specifiers since we're
32128 trying to match a decl-specifier-seq of the first parameter.
32129 This must be non-null so that cp_parser_simple_type_specifier
32130 will recognize a constrained placeholder type such as:
32131 'C<int> auto' where C is a type concept. */
32132 cp_decl_specifier_seq ctor_specs;
32133 clear_decl_specs (&ctor_specs);
32134 cp_parser_type_specifier (parser,
32135 (friend_p ? CP_PARSER_FLAGS_NONE
32136 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32137 /*decl_specs=*/&ctor_specs,
32138 /*is_declarator=*/true,
32139 /*declares_class_or_enum=*/NULL,
32140 /*is_cv_qualifier=*/NULL);
32142 parser->num_template_parameter_lists
32143 = saved_num_template_parameter_lists;
32145 /* Leave the scope of the class. */
32146 if (pushed_scope)
32147 pop_scope (pushed_scope);
32149 constructor_p = !cp_parser_error_occurred (parser);
32153 /* We did not really want to consume any tokens. */
32154 cp_parser_abort_tentative_parse (parser);
32156 return constructor_p;
32159 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32160 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32161 they must be performed once we are in the scope of the function.
32163 Returns the function defined. */
32165 static tree
32166 cp_parser_function_definition_from_specifiers_and_declarator
32167 (cp_parser* parser,
32168 cp_decl_specifier_seq *decl_specifiers,
32169 tree attributes,
32170 const cp_declarator *declarator)
32172 tree fn;
32173 bool success_p;
32175 /* Begin the function-definition. */
32176 success_p = start_function (decl_specifiers, declarator, attributes);
32178 /* The things we're about to see are not directly qualified by any
32179 template headers we've seen thus far. */
32180 reset_specialization ();
32182 /* If there were names looked up in the decl-specifier-seq that we
32183 did not check, check them now. We must wait until we are in the
32184 scope of the function to perform the checks, since the function
32185 might be a friend. */
32186 perform_deferred_access_checks (tf_warning_or_error);
32188 if (success_p)
32190 cp_finalize_omp_declare_simd (parser, current_function_decl);
32191 parser->omp_declare_simd = NULL;
32192 cp_finalize_oacc_routine (parser, current_function_decl, true);
32193 parser->oacc_routine = NULL;
32196 if (!success_p)
32198 /* Skip the entire function. */
32199 cp_parser_skip_to_end_of_block_or_statement (parser);
32200 fn = error_mark_node;
32202 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32204 /* Seen already, skip it. An error message has already been output. */
32205 cp_parser_skip_to_end_of_block_or_statement (parser);
32206 fn = current_function_decl;
32207 current_function_decl = NULL_TREE;
32208 /* If this is a function from a class, pop the nested class. */
32209 if (current_class_name)
32210 pop_nested_class ();
32212 else
32214 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32215 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32216 fn = cp_parser_function_definition_after_declarator (parser,
32217 /*inline_p=*/false);
32220 return fn;
32223 /* Parse the part of a function-definition that follows the
32224 declarator. INLINE_P is TRUE iff this function is an inline
32225 function defined within a class-specifier.
32227 Returns the function defined. */
32229 static tree
32230 cp_parser_function_definition_after_declarator (cp_parser* parser,
32231 bool inline_p)
32233 tree fn;
32234 bool saved_in_unbraced_linkage_specification_p;
32235 bool saved_in_function_body;
32236 unsigned saved_num_template_parameter_lists;
32237 cp_token *token;
32238 bool fully_implicit_function_template_p
32239 = parser->fully_implicit_function_template_p;
32240 parser->fully_implicit_function_template_p = false;
32241 tree implicit_template_parms
32242 = parser->implicit_template_parms;
32243 parser->implicit_template_parms = 0;
32244 cp_binding_level* implicit_template_scope
32245 = parser->implicit_template_scope;
32246 parser->implicit_template_scope = 0;
32248 saved_in_function_body = parser->in_function_body;
32249 parser->in_function_body = true;
32250 /* If the next token is `return', then the code may be trying to
32251 make use of the "named return value" extension that G++ used to
32252 support. */
32253 token = cp_lexer_peek_token (parser->lexer);
32254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32256 /* Consume the `return' keyword. */
32257 cp_lexer_consume_token (parser->lexer);
32258 /* Look for the identifier that indicates what value is to be
32259 returned. */
32260 cp_parser_identifier (parser);
32261 /* Issue an error message. */
32262 error_at (token->location,
32263 "named return values are no longer supported");
32264 /* Skip tokens until we reach the start of the function body. */
32265 while (true)
32267 cp_token *token = cp_lexer_peek_token (parser->lexer);
32268 if (token->type == CPP_OPEN_BRACE
32269 || token->type == CPP_EOF
32270 || token->type == CPP_PRAGMA_EOL)
32271 break;
32272 cp_lexer_consume_token (parser->lexer);
32275 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32276 anything declared inside `f'. */
32277 saved_in_unbraced_linkage_specification_p
32278 = parser->in_unbraced_linkage_specification_p;
32279 parser->in_unbraced_linkage_specification_p = false;
32280 /* Inside the function, surrounding template-parameter-lists do not
32281 apply. */
32282 saved_num_template_parameter_lists
32283 = parser->num_template_parameter_lists;
32284 parser->num_template_parameter_lists = 0;
32286 /* If the next token is `try', `__transaction_atomic', or
32287 `__transaction_relaxed`, then we are looking at either function-try-block
32288 or function-transaction-block. Note that all of these include the
32289 function-body. */
32290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32291 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32292 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32293 RID_TRANSACTION_RELAXED))
32294 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32295 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32296 cp_parser_function_try_block (parser);
32297 else
32298 cp_parser_ctor_initializer_opt_and_function_body
32299 (parser, /*in_function_try_block=*/false);
32301 /* Finish the function. */
32302 fn = finish_function (inline_p);
32304 if (modules_p ()
32305 && !inline_p
32306 && TYPE_P (DECL_CONTEXT (fn))
32307 && (DECL_DECLARED_INLINE_P (fn)
32308 || processing_template_decl))
32309 set_defining_module (fn);
32311 /* Generate code for it, if necessary. */
32312 expand_or_defer_fn (fn);
32314 /* Restore the saved values. */
32315 parser->in_unbraced_linkage_specification_p
32316 = saved_in_unbraced_linkage_specification_p;
32317 parser->num_template_parameter_lists
32318 = saved_num_template_parameter_lists;
32319 parser->in_function_body = saved_in_function_body;
32321 parser->fully_implicit_function_template_p
32322 = fully_implicit_function_template_p;
32323 parser->implicit_template_parms
32324 = implicit_template_parms;
32325 parser->implicit_template_scope
32326 = implicit_template_scope;
32328 if (parser->fully_implicit_function_template_p)
32329 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32331 return fn;
32334 /* Parse a template-declaration body (following argument list). */
32336 static void
32337 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32338 tree parameter_list,
32339 bool member_p)
32341 tree decl = NULL_TREE;
32342 bool friend_p = false;
32344 /* We just processed one more parameter list. */
32345 ++parser->num_template_parameter_lists;
32347 /* Get the deferred access checks from the parameter list. These
32348 will be checked once we know what is being declared, as for a
32349 member template the checks must be performed in the scope of the
32350 class containing the member. */
32351 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32353 /* Tentatively parse for a new template parameter list, which can either be
32354 the template keyword or a template introduction. */
32355 if (cp_parser_template_declaration_after_export (parser, member_p))
32356 /* OK */;
32357 else if (cxx_dialect >= cxx11
32358 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32359 decl = cp_parser_alias_declaration (parser);
32360 else if (flag_concepts
32361 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32362 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32363 /* -fconcept-ts 'concept bool' syntax is handled below, in
32364 cp_parser_single_declaration. */
32365 decl = cp_parser_concept_definition (parser);
32366 else
32368 cp_token *token = cp_lexer_peek_token (parser->lexer);
32369 decl = cp_parser_single_declaration (parser,
32370 checks,
32371 member_p,
32372 /*explicit_specialization_p=*/false,
32373 &friend_p);
32375 /* If this is a member template declaration, let the front
32376 end know. */
32377 if (member_p && !friend_p && decl)
32379 if (TREE_CODE (decl) == TYPE_DECL)
32380 cp_parser_check_access_in_redeclaration (decl, token->location);
32382 decl = finish_member_template_decl (decl);
32384 else if (friend_p && decl
32385 && DECL_DECLARES_TYPE_P (decl))
32386 make_friend_class (current_class_type, TREE_TYPE (decl),
32387 /*complain=*/true);
32389 /* We are done with the current parameter list. */
32390 --parser->num_template_parameter_lists;
32392 pop_deferring_access_checks ();
32394 /* Finish up. */
32395 finish_template_decl (parameter_list);
32397 /* Check the template arguments for a literal operator template. */
32398 if (decl
32399 && DECL_DECLARES_FUNCTION_P (decl)
32400 && UDLIT_OPER_P (DECL_NAME (decl)))
32402 bool ok = true;
32403 if (parameter_list == NULL_TREE)
32404 ok = false;
32405 else
32407 int num_parms = TREE_VEC_LENGTH (parameter_list);
32408 if (num_parms == 1)
32410 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32411 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32412 if (TREE_CODE (parm) != PARM_DECL)
32413 ok = false;
32414 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32415 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32416 /* OK, C++20 string literal operator template. We don't need
32417 to warn in lower dialects here because we will have already
32418 warned about the template parameter. */;
32419 else if (TREE_TYPE (parm) != char_type_node
32420 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32421 ok = false;
32423 else if (num_parms == 2 && cxx_dialect >= cxx14)
32425 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32426 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32427 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32428 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32429 if (TREE_CODE (parm) != PARM_DECL
32430 || TREE_TYPE (parm) != TREE_TYPE (type)
32431 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32432 ok = false;
32433 else
32434 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32435 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32436 "ISO C++ did not adopt string literal operator templa"
32437 "tes taking an argument pack of characters");
32439 else
32440 ok = false;
32442 if (!ok)
32444 if (cxx_dialect > cxx17)
32445 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32446 "template %qD has invalid parameter list; expected "
32447 "non-type template parameter pack %<<char...>%> or "
32448 "single non-type parameter of class type",
32449 decl);
32450 else
32451 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32452 "template %qD has invalid parameter list; expected "
32453 "non-type template parameter pack %<<char...>%>",
32454 decl);
32458 /* Register member declarations. */
32459 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32460 finish_member_declaration (decl);
32461 /* If DECL is a function template, we must return to parse it later.
32462 (Even though there is no definition, there might be default
32463 arguments that need handling.) */
32464 if (member_p && decl
32465 && DECL_DECLARES_FUNCTION_P (decl))
32466 vec_safe_push (unparsed_funs_with_definitions, decl);
32469 /* Parse a template introduction header for a template-declaration. Returns
32470 false if tentative parse fails. */
32472 static bool
32473 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32475 cp_parser_parse_tentatively (parser);
32477 tree saved_scope = parser->scope;
32478 tree saved_object_scope = parser->object_scope;
32479 tree saved_qualifying_scope = parser->qualifying_scope;
32480 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32482 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32484 /* In classes don't parse valid unnamed bitfields as invalid
32485 template introductions. */
32486 if (member_p)
32487 parser->colon_corrects_to_scope_p = false;
32489 /* Look for the optional `::' operator. */
32490 cp_parser_global_scope_opt (parser,
32491 /*current_scope_valid_p=*/false);
32492 /* Look for the nested-name-specifier. */
32493 cp_parser_nested_name_specifier_opt (parser,
32494 /*typename_keyword_p=*/false,
32495 /*check_dependency_p=*/true,
32496 /*type_p=*/false,
32497 /*is_declaration=*/false);
32499 cp_token *token = cp_lexer_peek_token (parser->lexer);
32500 tree concept_name = cp_parser_identifier (parser);
32502 /* Look up the concept for which we will be matching
32503 template parameters. */
32504 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32505 token->location);
32506 parser->scope = saved_scope;
32507 parser->object_scope = saved_object_scope;
32508 parser->qualifying_scope = saved_qualifying_scope;
32509 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32511 if (concept_name == error_mark_node
32512 || (seen_error () && !concept_definition_p (tmpl_decl)))
32513 cp_parser_simulate_error (parser);
32515 /* Look for opening brace for introduction. */
32516 matching_braces braces;
32517 braces.require_open (parser);
32518 location_t open_loc = input_location;
32520 if (!cp_parser_parse_definitely (parser))
32521 return false;
32523 push_deferring_access_checks (dk_deferred);
32525 /* Build vector of placeholder parameters and grab
32526 matching identifiers. */
32527 tree introduction_list = cp_parser_introduction_list (parser);
32529 /* Look for closing brace for introduction. */
32530 if (!braces.require_close (parser))
32531 return true;
32533 /* The introduction-list shall not be empty. */
32534 int nargs = TREE_VEC_LENGTH (introduction_list);
32535 if (nargs == 0)
32537 /* In cp_parser_introduction_list we have already issued an error. */
32538 return true;
32541 if (tmpl_decl == error_mark_node)
32543 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32544 token->location);
32545 return true;
32548 /* Build and associate the constraint. */
32549 location_t introduction_loc = make_location (open_loc,
32550 start_token->location,
32551 parser->lexer);
32552 tree parms = finish_template_introduction (tmpl_decl,
32553 introduction_list,
32554 introduction_loc);
32555 if (parms && parms != error_mark_node)
32557 if (!flag_concepts_ts)
32558 pedwarn (introduction_loc, 0, "template-introductions"
32559 " are not part of C++20 concepts; use %qs to enable",
32560 "-fconcepts-ts");
32562 cp_parser_template_declaration_after_parameters (parser, parms,
32563 member_p);
32564 return true;
32567 if (parms == NULL_TREE)
32568 error_at (token->location, "no matching concept for template-introduction");
32570 return true;
32573 /* Parse a normal template-declaration following the template keyword. */
32575 static void
32576 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32578 tree parameter_list;
32579 bool need_lang_pop;
32580 location_t location = input_location;
32582 /* Look for the `<' token. */
32583 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32584 return;
32585 if (at_class_scope_p () && current_function_decl)
32587 /* 14.5.2.2 [temp.mem]
32589 A local class shall not have member templates. */
32590 error_at (location,
32591 "invalid declaration of member template in local class");
32592 cp_parser_skip_to_end_of_block_or_statement (parser);
32593 return;
32595 /* [temp]
32597 A template ... shall not have C linkage. */
32598 if (current_lang_name == lang_name_c)
32600 error_at (location, "template with C linkage");
32601 maybe_show_extern_c_location ();
32602 /* Give it C++ linkage to avoid confusing other parts of the
32603 front end. */
32604 push_lang_context (lang_name_cplusplus);
32605 need_lang_pop = true;
32607 else
32608 need_lang_pop = false;
32610 /* We cannot perform access checks on the template parameter
32611 declarations until we know what is being declared, just as we
32612 cannot check the decl-specifier list. */
32613 push_deferring_access_checks (dk_deferred);
32615 /* If the next token is `>', then we have an invalid
32616 specialization. Rather than complain about an invalid template
32617 parameter, issue an error message here. */
32618 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32620 cp_parser_error (parser, "invalid explicit specialization");
32621 begin_specialization ();
32622 parameter_list = NULL_TREE;
32624 else
32626 /* Parse the template parameters. */
32627 parameter_list = cp_parser_template_parameter_list (parser);
32630 /* Look for the `>'. */
32631 cp_parser_require_end_of_template_parameter_list (parser);
32633 /* Manage template requirements */
32634 if (flag_concepts)
32636 tree reqs = get_shorthand_constraints (current_template_parms);
32637 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32638 reqs = combine_constraint_expressions (reqs, treqs);
32639 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32642 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32643 member_p);
32645 /* For the erroneous case of a template with C linkage, we pushed an
32646 implicit C++ linkage scope; exit that scope now. */
32647 if (need_lang_pop)
32648 pop_lang_context ();
32651 /* Parse a template-declaration, assuming that the `export' (and
32652 `extern') keywords, if present, has already been scanned. MEMBER_P
32653 is as for cp_parser_template_declaration. */
32655 static bool
32656 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32660 cp_lexer_consume_token (parser->lexer);
32661 cp_parser_explicit_template_declaration (parser, member_p);
32662 return true;
32664 else if (flag_concepts)
32665 return cp_parser_template_introduction (parser, member_p);
32667 return false;
32670 /* Perform the deferred access checks from a template-parameter-list.
32671 CHECKS is a TREE_LIST of access checks, as returned by
32672 get_deferred_access_checks. */
32674 static void
32675 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32677 ++processing_template_parmlist;
32678 perform_access_checks (checks, tf_warning_or_error);
32679 --processing_template_parmlist;
32682 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32683 `function-definition' sequence that follows a template header.
32684 If MEMBER_P is true, this declaration appears in a class scope.
32686 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32687 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32689 static tree
32690 cp_parser_single_declaration (cp_parser* parser,
32691 vec<deferred_access_check, va_gc> *checks,
32692 bool member_p,
32693 bool explicit_specialization_p,
32694 bool* friend_p)
32696 int declares_class_or_enum;
32697 tree decl = NULL_TREE;
32698 cp_decl_specifier_seq decl_specifiers;
32699 bool function_definition_p = false;
32700 cp_token *decl_spec_token_start;
32702 /* This function is only used when processing a template
32703 declaration. */
32704 gcc_assert (innermost_scope_kind () == sk_template_parms
32705 || innermost_scope_kind () == sk_template_spec);
32707 /* Defer access checks until we know what is being declared. */
32708 push_deferring_access_checks (dk_deferred);
32710 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32711 alternative. */
32712 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32713 cp_parser_decl_specifier_seq (parser,
32714 (CP_PARSER_FLAGS_OPTIONAL
32715 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32716 &decl_specifiers,
32717 &declares_class_or_enum);
32719 cp_omp_declare_simd_data odsd;
32720 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32721 cp_parser_handle_directive_omp_attributes (parser,
32722 &decl_specifiers.attributes,
32723 &odsd, true);
32725 if (friend_p)
32726 *friend_p = cp_parser_friend_p (&decl_specifiers);
32728 /* There are no template typedefs. */
32729 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32731 error_at (decl_spec_token_start->location,
32732 "template declaration of %<typedef%>");
32733 decl = error_mark_node;
32736 /* Gather up the access checks that occurred the
32737 decl-specifier-seq. */
32738 stop_deferring_access_checks ();
32740 /* Check for the declaration of a template class. */
32741 if (declares_class_or_enum)
32743 if (cp_parser_declares_only_class_p (parser)
32744 || (declares_class_or_enum & 2))
32746 decl = shadow_tag (&decl_specifiers);
32748 /* In this case:
32750 struct C {
32751 friend template <typename T> struct A<T>::B;
32754 A<T>::B will be represented by a TYPENAME_TYPE, and
32755 therefore not recognized by shadow_tag. */
32756 if (friend_p && *friend_p
32757 && !decl
32758 && decl_specifiers.type
32759 && TYPE_P (decl_specifiers.type))
32760 decl = decl_specifiers.type;
32762 if (decl && decl != error_mark_node)
32763 decl = TYPE_NAME (decl);
32764 else
32765 decl = error_mark_node;
32767 /* If this is a declaration, but not a definition, associate
32768 any constraints with the type declaration. Constraints
32769 are associated with definitions in cp_parser_class_specifier. */
32770 if (declares_class_or_enum == 1)
32771 associate_classtype_constraints (TREE_TYPE (decl));
32773 /* Perform access checks for template parameters. */
32774 cp_parser_perform_template_parameter_access_checks (checks);
32776 /* Give a helpful diagnostic for
32777 template <class T> struct A { } a;
32778 if we aren't already recovering from an error. */
32779 if (!cp_parser_declares_only_class_p (parser)
32780 && !seen_error ())
32782 error_at (cp_lexer_peek_token (parser->lexer)->location,
32783 "a class template declaration must not declare "
32784 "anything else");
32785 cp_parser_skip_to_end_of_block_or_statement (parser);
32786 goto out;
32791 /* Complain about missing 'typename' or other invalid type names. */
32792 if (!decl_specifiers.any_type_specifiers_p
32793 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
32795 /* cp_parser_parse_and_diagnose_invalid_type_name calls
32796 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
32797 the rest of this declaration. */
32798 decl = error_mark_node;
32799 goto out;
32802 /* If it's not a template class, try for a template function. If
32803 the next token is a `;', then this declaration does not declare
32804 anything. But, if there were errors in the decl-specifiers, then
32805 the error might well have come from an attempted class-specifier.
32806 In that case, there's no need to warn about a missing declarator. */
32807 if (!decl
32808 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
32809 || decl_specifiers.type != error_mark_node))
32811 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
32812 /* We don't delay parsing for friends, though CWG 2510 may change
32813 that. */
32814 if (member_p && !(friend_p && *friend_p))
32815 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
32816 decl = cp_parser_init_declarator (parser,
32817 flags,
32818 &decl_specifiers,
32819 checks,
32820 /*function_definition_allowed_p=*/true,
32821 member_p,
32822 declares_class_or_enum,
32823 &function_definition_p,
32824 NULL, NULL, NULL);
32826 /* 7.1.1-1 [dcl.stc]
32828 A storage-class-specifier shall not be specified in an explicit
32829 specialization... */
32830 if (decl
32831 && explicit_specialization_p
32832 && decl_specifiers.storage_class != sc_none)
32834 error_at (decl_spec_token_start->location,
32835 "explicit template specialization cannot have a storage class");
32836 decl = error_mark_node;
32839 if (decl && VAR_P (decl))
32840 check_template_variable (decl);
32843 /* Look for a trailing `;' after the declaration. */
32844 if (!function_definition_p
32845 && (decl == error_mark_node
32846 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
32847 cp_parser_skip_to_end_of_block_or_statement (parser);
32849 out:
32850 pop_deferring_access_checks ();
32852 /* Clear any current qualification; whatever comes next is the start
32853 of something new. */
32854 parser->scope = NULL_TREE;
32855 parser->qualifying_scope = NULL_TREE;
32856 parser->object_scope = NULL_TREE;
32858 cp_finalize_omp_declare_simd (parser, &odsd);
32860 return decl;
32863 /* Parse a cast-expression that is not the operand of a unary "&". */
32865 static cp_expr
32866 cp_parser_simple_cast_expression (cp_parser *parser)
32868 return cp_parser_cast_expression (parser, /*address_p=*/false,
32869 /*cast_p=*/false, /*decltype*/false, NULL);
32872 /* Parse a functional cast to TYPE. Returns an expression
32873 representing the cast. */
32875 static cp_expr
32876 cp_parser_functional_cast (cp_parser* parser, tree type)
32878 vec<tree, va_gc> *vec;
32879 tree expression_list;
32880 cp_expr cast;
32882 location_t start_loc = input_location;
32884 if (!type)
32885 type = error_mark_node;
32887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32889 cp_lexer_set_source_position (parser->lexer);
32890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32891 expression_list = cp_parser_braced_list (parser);
32892 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
32893 if (TREE_CODE (type) == TYPE_DECL)
32894 type = TREE_TYPE (type);
32896 cast = finish_compound_literal (type, expression_list,
32897 tf_warning_or_error, fcl_functional);
32898 /* Create a location of the form:
32899 type_name{i, f}
32900 ^~~~~~~~~~~~~~~
32901 with caret == start at the start of the type name,
32902 finishing at the closing brace. */
32903 location_t combined_loc = make_location (start_loc, start_loc,
32904 parser->lexer);
32905 cast.set_location (combined_loc);
32906 return cast;
32910 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
32911 /*cast_p=*/true,
32912 /*allow_expansion_p=*/true,
32913 /*non_constant_p=*/NULL);
32914 if (vec == NULL)
32915 expression_list = error_mark_node;
32916 else
32918 expression_list = build_tree_list_vec (vec);
32919 release_tree_vector (vec);
32922 /* Create a location of the form:
32923 float(i)
32924 ^~~~~~~~
32925 with caret == start at the start of the type name,
32926 finishing at the closing paren. */
32927 location_t combined_loc = make_location (start_loc, start_loc,
32928 parser->lexer);
32929 cast = build_functional_cast (combined_loc, type, expression_list,
32930 tf_warning_or_error);
32932 /* [expr.const]/1: In an integral constant expression "only type
32933 conversions to integral or enumeration type can be used". */
32934 if (TREE_CODE (type) == TYPE_DECL)
32935 type = TREE_TYPE (type);
32936 if (cast != error_mark_node
32937 && !cast_valid_in_integral_constant_expression_p (type)
32938 && cp_parser_non_integral_constant_expression (parser,
32939 NIC_CONSTRUCTOR))
32940 return error_mark_node;
32942 return cast;
32945 /* Save the tokens that make up the body of a member function defined
32946 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32947 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32948 specifiers applied to the declaration. Returns the FUNCTION_DECL
32949 for the member function. */
32951 static tree
32952 cp_parser_save_member_function_body (cp_parser* parser,
32953 cp_decl_specifier_seq *decl_specifiers,
32954 cp_declarator *declarator,
32955 tree attributes)
32957 cp_token *first;
32958 cp_token *last;
32959 tree fn;
32960 bool function_try_block = false;
32962 /* Create the FUNCTION_DECL. */
32963 fn = grokmethod (decl_specifiers, declarator, attributes);
32964 cp_finalize_omp_declare_simd (parser, fn);
32965 cp_finalize_oacc_routine (parser, fn, true);
32966 /* If something went badly wrong, bail out now. */
32967 if (fn == error_mark_node)
32969 /* If there's a function-body, skip it. */
32970 if (cp_parser_token_starts_function_definition_p
32971 (cp_lexer_peek_token (parser->lexer)))
32972 cp_parser_skip_to_end_of_block_or_statement (parser);
32973 return error_mark_node;
32976 /* Remember it, if there are default args to post process. */
32977 cp_parser_save_default_args (parser, fn);
32979 /* Save away the tokens that make up the body of the
32980 function. */
32981 first = parser->lexer->next_token;
32983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32984 cp_lexer_consume_token (parser->lexer);
32985 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32986 RID_TRANSACTION_ATOMIC))
32988 cp_lexer_consume_token (parser->lexer);
32989 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32991 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32992 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32993 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32994 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32995 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32997 cp_lexer_consume_token (parser->lexer);
32998 cp_lexer_consume_token (parser->lexer);
32999 cp_lexer_consume_token (parser->lexer);
33000 cp_lexer_consume_token (parser->lexer);
33001 cp_lexer_consume_token (parser->lexer);
33003 else
33004 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33005 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33007 cp_lexer_consume_token (parser->lexer);
33008 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33009 break;
33013 /* Handle function try blocks. */
33014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33016 cp_lexer_consume_token (parser->lexer);
33017 function_try_block = true;
33019 /* We can have braced-init-list mem-initializers before the fn body. */
33020 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33022 cp_lexer_consume_token (parser->lexer);
33023 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33025 /* cache_group will stop after an un-nested { } pair, too. */
33026 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33027 break;
33029 /* variadic mem-inits have ... after the ')'. */
33030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33031 cp_lexer_consume_token (parser->lexer);
33034 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33035 /* Handle function try blocks. */
33036 if (function_try_block)
33037 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33039 last = parser->lexer->next_token;
33041 /* Save away the inline definition; we will process it when the
33042 class is complete. */
33043 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33044 DECL_PENDING_INLINE_P (fn) = 1;
33046 /* We need to know that this was defined in the class, so that
33047 friend templates are handled correctly. */
33048 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33050 /* Add FN to the queue of functions to be parsed later. */
33051 vec_safe_push (unparsed_funs_with_definitions, fn);
33053 return fn;
33056 /* Save the tokens that make up the in-class initializer for a non-static
33057 data member. Returns a DEFERRED_PARSE. */
33059 static tree
33060 cp_parser_save_nsdmi (cp_parser* parser)
33062 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33065 /* Parse a template-argument-list, as well as the trailing ">" (but
33066 not the opening "<"). See cp_parser_template_argument_list for the
33067 return value. */
33069 static tree
33070 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33072 tree arguments;
33073 tree saved_scope;
33074 tree saved_qualifying_scope;
33075 tree saved_object_scope;
33076 bool saved_greater_than_is_operator_p;
33078 /* [temp.names]
33080 When parsing a template-id, the first non-nested `>' is taken as
33081 the end of the template-argument-list rather than a greater-than
33082 operator. */
33083 saved_greater_than_is_operator_p
33084 = parser->greater_than_is_operator_p;
33085 parser->greater_than_is_operator_p = false;
33086 /* Parsing the argument list may modify SCOPE, so we save it
33087 here. */
33088 saved_scope = parser->scope;
33089 saved_qualifying_scope = parser->qualifying_scope;
33090 saved_object_scope = parser->object_scope;
33091 /* We need to evaluate the template arguments, even though this
33092 template-id may be nested within a "sizeof". */
33093 cp_evaluated ev;
33094 /* Parse the template-argument-list itself. */
33095 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33096 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33097 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33098 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33100 arguments = make_tree_vec (0);
33101 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33103 else
33104 arguments = cp_parser_template_argument_list (parser);
33105 /* Look for the `>' that ends the template-argument-list. If we find
33106 a '>>' instead, it's probably just a typo. */
33107 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33109 if (cxx_dialect != cxx98)
33111 /* In C++0x, a `>>' in a template argument list or cast
33112 expression is considered to be two separate `>'
33113 tokens. So, change the current token to a `>', but don't
33114 consume it: it will be consumed later when the outer
33115 template argument list (or cast expression) is parsed.
33116 Note that this replacement of `>' for `>>' is necessary
33117 even if we are parsing tentatively: in the tentative
33118 case, after calling
33119 cp_parser_enclosed_template_argument_list we will always
33120 throw away all of the template arguments and the first
33121 closing `>', either because the template argument list
33122 was erroneous or because we are replacing those tokens
33123 with a CPP_TEMPLATE_ID token. The second `>' (which will
33124 not have been thrown away) is needed either to close an
33125 outer template argument list or to complete a new-style
33126 cast. */
33127 cp_token *token = cp_lexer_peek_token (parser->lexer);
33128 token->type = CPP_GREATER;
33130 else if (!saved_greater_than_is_operator_p)
33132 /* If we're in a nested template argument list, the '>>' has
33133 to be a typo for '> >'. We emit the error message, but we
33134 continue parsing and we push a '>' as next token, so that
33135 the argument list will be parsed correctly. Note that the
33136 global source location is still on the token before the
33137 '>>', so we need to say explicitly where we want it. */
33138 cp_token *token = cp_lexer_peek_token (parser->lexer);
33139 gcc_rich_location richloc (token->location);
33140 richloc.add_fixit_replace ("> >");
33141 error_at (&richloc, "%<>>%> should be %<> >%> "
33142 "within a nested template argument list");
33144 token->type = CPP_GREATER;
33146 else
33148 /* If this is not a nested template argument list, the '>>'
33149 is a typo for '>'. Emit an error message and continue.
33150 Same deal about the token location, but here we can get it
33151 right by consuming the '>>' before issuing the diagnostic. */
33152 cp_token *token = cp_lexer_consume_token (parser->lexer);
33153 error_at (token->location,
33154 "spurious %<>>%>, use %<>%> to terminate "
33155 "a template argument list");
33158 /* Similarly for >>= and >=. */
33159 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33160 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33162 cp_token *token = cp_lexer_consume_token (parser->lexer);
33163 gcc_rich_location richloc (token->location);
33164 enum cpp_ttype new_type;
33165 const char *replacement;
33166 if (token->type == CPP_GREATER_EQ)
33168 replacement = "> =";
33169 new_type = CPP_EQ;
33171 else if (!saved_greater_than_is_operator_p)
33173 if (cxx_dialect != cxx98)
33174 replacement = ">> =";
33175 else
33176 replacement = "> > =";
33177 new_type = CPP_GREATER;
33179 else
33181 replacement = "> >=";
33182 new_type = CPP_GREATER_EQ;
33184 richloc.add_fixit_replace (replacement);
33185 error_at (&richloc, "%qs should be %qs to terminate a template "
33186 "argument list",
33187 cpp_type2name (token->type, token->flags), replacement);
33188 token->type = new_type;
33190 else
33191 cp_parser_require_end_of_template_parameter_list (parser);
33192 /* The `>' token might be a greater-than operator again now. */
33193 parser->greater_than_is_operator_p
33194 = saved_greater_than_is_operator_p;
33195 /* Restore the SAVED_SCOPE. */
33196 parser->scope = saved_scope;
33197 parser->qualifying_scope = saved_qualifying_scope;
33198 parser->object_scope = saved_object_scope;
33200 return arguments;
33203 /* MEMBER_FUNCTION is a member function, or a friend. If default
33204 arguments, or the body of the function have not yet been parsed,
33205 parse them now. */
33207 static void
33208 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33210 auto_timevar tv (TV_PARSE_INMETH);
33212 /* If this member is a template, get the underlying
33213 FUNCTION_DECL. */
33214 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33215 member_function = DECL_TEMPLATE_RESULT (member_function);
33217 /* There should not be any class definitions in progress at this
33218 point; the bodies of members are only parsed outside of all class
33219 definitions. */
33220 gcc_assert (parser->num_classes_being_defined == 0);
33221 /* While we're parsing the member functions we might encounter more
33222 classes. We want to handle them right away, but we don't want
33223 them getting mixed up with functions that are currently in the
33224 queue. */
33225 push_unparsed_function_queues (parser);
33227 /* Make sure that any template parameters are in scope. */
33228 maybe_begin_member_template_processing (member_function);
33230 /* If the body of the function has not yet been parsed, parse it
33231 now. Except if the tokens have been purged (PR c++/39751). */
33232 if (DECL_PENDING_INLINE_P (member_function)
33233 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33235 tree function_scope;
33236 cp_token_cache *tokens;
33238 /* The function is no longer pending; we are processing it. */
33239 tokens = DECL_PENDING_INLINE_INFO (member_function);
33240 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33241 DECL_PENDING_INLINE_P (member_function) = 0;
33243 /* If this is a local class, enter the scope of the containing
33244 function. */
33245 function_scope = current_function_decl;
33246 if (function_scope)
33247 push_function_context ();
33249 /* Push the body of the function onto the lexer stack. */
33250 cp_parser_push_lexer_for_tokens (parser, tokens);
33252 /* Let the front end know that we going to be defining this
33253 function. */
33254 start_preparsed_function (member_function, NULL_TREE,
33255 SF_PRE_PARSED | SF_INCLASS_INLINE);
33257 /* #pragma omp declare reduction needs special parsing. */
33258 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33260 parser->lexer->in_pragma = true;
33261 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33262 finish_function (/*inline_p=*/true);
33263 cp_check_omp_declare_reduction (member_function);
33265 else
33266 /* Now, parse the body of the function. */
33267 cp_parser_function_definition_after_declarator (parser,
33268 /*inline_p=*/true);
33270 /* Leave the scope of the containing function. */
33271 if (function_scope)
33272 pop_function_context ();
33273 cp_parser_pop_lexer (parser);
33276 /* Remove any template parameters from the symbol table. */
33277 maybe_end_member_template_processing ();
33279 /* Restore the queue. */
33280 pop_unparsed_function_queues (parser);
33283 /* If DECL contains any default args, remember it on the unparsed
33284 functions queue. */
33286 static void
33287 cp_parser_save_default_args (cp_parser* parser, tree decl)
33289 tree probe;
33291 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33292 probe;
33293 probe = TREE_CHAIN (probe))
33294 if (TREE_PURPOSE (probe))
33296 cp_default_arg_entry entry = {current_class_type, decl};
33297 vec_safe_push (unparsed_funs_with_default_args, entry);
33298 break;
33301 /* Remember if there is a noexcept-specifier to post process. */
33302 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33303 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33304 vec_safe_push (unparsed_noexcepts, decl);
33306 /* Contracts are deferred. */
33307 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33308 if (cxx_contract_attribute_p (attr))
33310 vec_safe_push (unparsed_contracts, decl);
33311 break;
33315 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33316 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33317 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33318 from the parameter-type-list. */
33320 static tree
33321 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33322 tree default_arg, tree parmtype)
33324 cp_token_cache *tokens;
33325 tree parsed_arg;
33327 if (default_arg == error_mark_node)
33328 return error_mark_node;
33330 /* Push the saved tokens for the default argument onto the parser's
33331 lexer stack. */
33332 tokens = DEFPARSE_TOKENS (default_arg);
33333 cp_parser_push_lexer_for_tokens (parser, tokens);
33335 start_lambda_scope (decl);
33337 /* Parse the default argument. */
33338 parsed_arg = cp_parser_initializer (parser);
33339 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33340 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33342 finish_lambda_scope ();
33344 if (parsed_arg == error_mark_node)
33345 cp_parser_skip_to_end_of_statement (parser);
33347 if (!processing_template_decl)
33349 /* In a non-template class, check conversions now. In a template,
33350 we'll wait and instantiate these as needed. */
33351 if (TREE_CODE (decl) == PARM_DECL)
33352 parsed_arg = check_default_argument (parmtype, parsed_arg,
33353 tf_warning_or_error);
33354 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33355 parsed_arg = error_mark_node;
33356 else
33357 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33360 /* If the token stream has not been completely used up, then
33361 there was extra junk after the end of the default
33362 argument. */
33363 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33365 if (TREE_CODE (decl) == PARM_DECL)
33366 cp_parser_error (parser, "expected %<,%>");
33367 else
33368 cp_parser_error (parser, "expected %<;%>");
33371 /* Revert to the main lexer. */
33372 cp_parser_pop_lexer (parser);
33374 return parsed_arg;
33377 /* FIELD is a non-static data member with an initializer which we saved for
33378 later; parse it now. */
33380 static void
33381 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33383 tree def;
33385 maybe_begin_member_template_processing (field);
33387 push_unparsed_function_queues (parser);
33388 def = cp_parser_late_parse_one_default_arg (parser, field,
33389 DECL_INITIAL (field),
33390 NULL_TREE);
33391 pop_unparsed_function_queues (parser);
33393 maybe_end_member_template_processing ();
33395 DECL_INITIAL (field) = def;
33398 /* FN is a FUNCTION_DECL which may contains a parameter with an
33399 unparsed DEFERRED_PARSE. Parse the default args now. This function
33400 assumes that the current scope is the scope in which the default
33401 argument should be processed. */
33403 static void
33404 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33406 unsigned char saved_local_variables_forbidden_p;
33408 /* While we're parsing the default args, we might (due to the
33409 statement expression extension) encounter more classes. We want
33410 to handle them right away, but we don't want them getting mixed
33411 up with default args that are currently in the queue. */
33412 push_unparsed_function_queues (parser);
33414 /* Local variable names (and the `this' keyword) may not appear
33415 in a default argument. */
33416 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33417 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33419 push_defarg_context (fn);
33421 begin_scope (sk_function_parms, fn);
33423 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33424 pushdecl clears DECL_CHAIN. */
33425 releasing_vec parms;
33426 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33427 parmdecl = DECL_CHAIN (parmdecl))
33428 vec_safe_push (parms, parmdecl);
33430 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33431 for (int i = 0;
33432 parm && parm != void_list_node;
33433 parm = TREE_CHAIN (parm),
33434 ++i)
33436 tree default_arg = TREE_PURPOSE (parm);
33437 tree parsed_arg;
33439 tree parmdecl = parms[i];
33440 pushdecl (parmdecl);
33442 if (!default_arg)
33443 continue;
33445 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33446 /* This can happen for a friend declaration for a function
33447 already declared with default arguments. */
33448 continue;
33450 parsed_arg
33451 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33452 default_arg,
33453 TREE_VALUE (parm));
33454 TREE_PURPOSE (parm) = parsed_arg;
33456 /* Update any instantiations we've already created. */
33457 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33458 TREE_PURPOSE (copy) = parsed_arg;
33461 pop_bindings_and_leave_scope ();
33463 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33464 parm = NULL_TREE;
33465 for (int i = parms->length () - 1; i >= 0; --i)
33467 DECL_CHAIN (parms[i]) = parm;
33468 parm = parms[i];
33471 pop_defarg_context ();
33473 /* Make sure no default arg is missing. */
33474 check_default_args (fn);
33476 /* Restore the state of local_variables_forbidden_p. */
33477 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33479 /* Restore the queue. */
33480 pop_unparsed_function_queues (parser);
33483 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33485 sizeof ... ( identifier )
33487 where the 'sizeof' token has already been consumed. */
33489 static tree
33490 cp_parser_sizeof_pack (cp_parser *parser)
33492 /* Consume the `...'. */
33493 cp_lexer_consume_token (parser->lexer);
33494 maybe_warn_variadic_templates ();
33496 matching_parens parens;
33497 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33498 if (paren)
33499 parens.consume_open (parser);
33500 else
33501 permerror (cp_lexer_peek_token (parser->lexer)->location,
33502 "%<sizeof...%> argument must be surrounded by parentheses");
33504 cp_token *token = cp_lexer_peek_token (parser->lexer);
33505 tree name = cp_parser_identifier (parser);
33506 if (name == error_mark_node)
33507 return error_mark_node;
33508 /* The name is not qualified. */
33509 parser->scope = NULL_TREE;
33510 parser->qualifying_scope = NULL_TREE;
33511 parser->object_scope = NULL_TREE;
33512 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33513 if (expr == error_mark_node)
33514 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33515 token->location);
33516 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33517 expr = TREE_TYPE (expr);
33518 else if (TREE_CODE (expr) == CONST_DECL)
33519 expr = DECL_INITIAL (expr);
33520 expr = make_pack_expansion (expr);
33521 if (expr != error_mark_node)
33522 PACK_EXPANSION_SIZEOF_P (expr) = true;
33524 if (paren)
33525 parens.require_close (parser);
33527 return expr;
33530 /* Parse the operand of `sizeof' (or a similar operator). Returns
33531 either a TYPE or an expression, depending on the form of the
33532 input. The KEYWORD indicates which kind of expression we have
33533 encountered. */
33535 static tree
33536 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33538 tree expr = NULL_TREE;
33539 const char *saved_message;
33540 const char *saved_message_arg;
33541 bool saved_integral_constant_expression_p;
33542 bool saved_non_integral_constant_expression_p;
33544 /* If it's a `...', then we are computing the length of a parameter
33545 pack. */
33546 if (keyword == RID_SIZEOF
33547 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33548 return cp_parser_sizeof_pack (parser);
33550 /* Types cannot be defined in a `sizeof' expression. Save away the
33551 old message. */
33552 saved_message = parser->type_definition_forbidden_message;
33553 saved_message_arg = parser->type_definition_forbidden_message_arg;
33554 parser->type_definition_forbidden_message
33555 = G_("types may not be defined in %qs expressions");
33556 parser->type_definition_forbidden_message_arg
33557 = IDENTIFIER_POINTER (ridpointers[keyword]);
33559 /* The restrictions on constant-expressions do not apply inside
33560 sizeof expressions. */
33561 saved_integral_constant_expression_p
33562 = parser->integral_constant_expression_p;
33563 saved_non_integral_constant_expression_p
33564 = parser->non_integral_constant_expression_p;
33565 parser->integral_constant_expression_p = false;
33567 auto cleanup = make_temp_override
33568 (parser->auto_is_implicit_function_template_parm_p, false);
33570 /* Do not actually evaluate the expression. */
33571 ++cp_unevaluated_operand;
33572 ++c_inhibit_evaluation_warnings;
33573 /* If it's a `(', then we might be looking at the type-id
33574 construction. */
33575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33577 tree type = NULL_TREE;
33579 tentative_firewall firewall (parser);
33581 /* We can't be sure yet whether we're looking at a type-id or an
33582 expression. */
33583 cp_parser_parse_tentatively (parser);
33585 matching_parens parens;
33586 parens.consume_open (parser);
33588 /* Note: as a GNU Extension, compound literals are considered
33589 postfix-expressions as they are in C99, so they are valid
33590 arguments to sizeof. See comment in cp_parser_cast_expression
33591 for details. */
33592 if (cp_parser_compound_literal_p (parser))
33593 cp_parser_simulate_error (parser);
33594 else
33596 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33597 parser->in_type_id_in_expr_p = true;
33598 /* Look for the type-id. */
33599 type = cp_parser_type_id (parser);
33600 /* Look for the closing `)'. */
33601 parens.require_close (parser);
33602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33605 /* If all went well, then we're done. */
33606 if (cp_parser_parse_definitely (parser))
33607 expr = type;
33608 else
33610 /* Commit to the tentative_firewall so we get syntax errors. */
33611 cp_parser_commit_to_tentative_parse (parser);
33613 expr = cp_parser_unary_expression (parser);
33616 else
33617 expr = cp_parser_unary_expression (parser);
33619 /* Go back to evaluating expressions. */
33620 --cp_unevaluated_operand;
33621 --c_inhibit_evaluation_warnings;
33623 /* And restore the old one. */
33624 parser->type_definition_forbidden_message = saved_message;
33625 parser->type_definition_forbidden_message_arg = saved_message_arg;
33626 parser->integral_constant_expression_p
33627 = saved_integral_constant_expression_p;
33628 parser->non_integral_constant_expression_p
33629 = saved_non_integral_constant_expression_p;
33631 return expr;
33634 /* If the current declaration has no declarator, return true. */
33636 static bool
33637 cp_parser_declares_only_class_p (cp_parser *parser)
33639 /* If the next token is a `;' or a `,' then there is no
33640 declarator. */
33641 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33642 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33645 /* Update the DECL_SPECS to reflect the storage class indicated by
33646 KEYWORD. */
33648 static void
33649 cp_parser_set_storage_class (cp_parser *parser,
33650 cp_decl_specifier_seq *decl_specs,
33651 enum rid keyword,
33652 cp_token *token)
33654 cp_storage_class storage_class;
33656 switch (keyword)
33658 case RID_AUTO:
33659 storage_class = sc_auto;
33660 break;
33661 case RID_REGISTER:
33662 storage_class = sc_register;
33663 break;
33664 case RID_STATIC:
33665 storage_class = sc_static;
33666 break;
33667 case RID_EXTERN:
33668 storage_class = sc_extern;
33669 break;
33670 case RID_MUTABLE:
33671 storage_class = sc_mutable;
33672 break;
33673 default:
33674 gcc_unreachable ();
33677 if (parser->in_unbraced_linkage_specification_p)
33679 error_at (token->location, "invalid use of %qD in linkage specification",
33680 ridpointers[keyword]);
33681 return;
33683 else if (decl_specs->storage_class != sc_none)
33685 if (decl_specs->conflicting_specifiers_p)
33686 return;
33687 gcc_rich_location richloc (token->location);
33688 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33689 if (decl_specs->storage_class == storage_class)
33690 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33691 else
33692 error_at (&richloc,
33693 "%qD specifier conflicts with %qs",
33694 ridpointers[keyword],
33695 cp_storage_class_name[decl_specs->storage_class]);
33696 decl_specs->conflicting_specifiers_p = true;
33697 return;
33700 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33701 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33702 && decl_specs->gnu_thread_keyword_p)
33704 pedwarn (decl_specs->locations[ds_thread], 0,
33705 "%<__thread%> before %qD", ridpointers[keyword]);
33708 decl_specs->storage_class = storage_class;
33709 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33711 /* A storage class specifier cannot be applied alongside a typedef
33712 specifier. If there is a typedef specifier present then set
33713 conflicting_specifiers_p which will trigger an error later
33714 on in grokdeclarator. */
33715 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33716 && !decl_specs->conflicting_specifiers_p)
33718 gcc_rich_location richloc (token->location);
33719 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33720 error_at (&richloc,
33721 "%qD specifier conflicts with %<typedef%>",
33722 ridpointers[keyword]);
33723 decl_specs->conflicting_specifiers_p = true;
33727 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33728 is true, the type is a class or enum definition. */
33730 static void
33731 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33732 tree type_spec,
33733 cp_token *token,
33734 bool type_definition_p)
33736 decl_specs->any_specifiers_p = true;
33738 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33739 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
33740 this is what happened. In system headers, we ignore these
33741 declarations so that G++ can work with system headers that are not
33742 C++-safe. */
33743 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33744 && !type_definition_p
33745 && TYPE_P (type_spec)
33746 && (type_spec == boolean_type_node
33747 || type_spec == char8_type_node
33748 || type_spec == char16_type_node
33749 || type_spec == char32_type_node
33750 || extended_float_type_p (type_spec)
33751 || type_spec == wchar_type_node)
33752 && (decl_specs->type
33753 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
33754 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
33755 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
33756 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
33758 decl_specs->redefined_builtin_type = type_spec;
33759 set_and_check_decl_spec_loc (decl_specs,
33760 ds_redefined_builtin_type_spec,
33761 token);
33762 if (!decl_specs->type)
33764 decl_specs->type = type_spec;
33765 decl_specs->type_definition_p = false;
33766 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
33769 else if (decl_specs->type)
33770 decl_specs->multiple_types_p = true;
33771 else
33773 decl_specs->type = type_spec;
33774 decl_specs->type_definition_p = type_definition_p;
33775 decl_specs->redefined_builtin_type = NULL_TREE;
33776 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
33780 /* True iff TOKEN is the GNU keyword __thread. */
33782 static bool
33783 token_is__thread (cp_token *token)
33785 gcc_assert (token->keyword == RID_THREAD);
33786 return id_equal (token->u.value, "__thread");
33789 /* Set the location for a declarator specifier and check if it is
33790 duplicated.
33792 DECL_SPECS is the sequence of declarator specifiers onto which to
33793 set the location.
33795 DS is the single declarator specifier to set which location is to
33796 be set onto the existing sequence of declarators.
33798 LOCATION is the location for the declarator specifier to
33799 consider. */
33801 static void
33802 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
33803 cp_decl_spec ds, cp_token *token)
33805 gcc_assert (ds < ds_last);
33807 if (decl_specs == NULL)
33808 return;
33810 location_t location = token->location;
33812 if (decl_specs->locations[ds] == 0)
33814 decl_specs->locations[ds] = location;
33815 if (ds == ds_thread)
33816 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
33818 else
33820 if (ds == ds_long)
33822 if (decl_specs->locations[ds_long_long] != 0)
33823 error_at (location,
33824 "%<long long long%> is too long for GCC");
33825 else
33827 decl_specs->locations[ds_long_long] = location;
33828 pedwarn_cxx98 (location,
33829 OPT_Wlong_long,
33830 "ISO C++ 1998 does not support %<long long%>");
33833 else if (ds == ds_thread)
33835 bool gnu = token_is__thread (token);
33836 gcc_rich_location richloc (location);
33837 if (gnu != decl_specs->gnu_thread_keyword_p)
33839 richloc.add_range (decl_specs->locations[ds_thread]);
33840 error_at (&richloc,
33841 "both %<__thread%> and %<thread_local%> specified");
33843 else
33845 richloc.add_fixit_remove ();
33846 error_at (&richloc, "duplicate %qD", token->u.value);
33849 else
33851 static const char *const decl_spec_names[] = {
33852 "signed",
33853 "unsigned",
33854 "short",
33855 "long",
33856 "const",
33857 "volatile",
33858 "restrict",
33859 "inline",
33860 "virtual",
33861 "explicit",
33862 "friend",
33863 "typedef",
33864 "using",
33865 "constexpr",
33866 "__complex",
33867 "constinit",
33868 "consteval"
33870 gcc_rich_location richloc (location);
33871 richloc.add_fixit_remove ();
33872 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
33877 /* Return true iff the declarator specifier DS is present in the
33878 sequence of declarator specifiers DECL_SPECS. */
33880 bool
33881 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
33882 cp_decl_spec ds)
33884 gcc_assert (ds < ds_last);
33886 if (decl_specs == NULL)
33887 return false;
33889 return decl_specs->locations[ds] != 0;
33892 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33893 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33895 static bool
33896 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
33898 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
33901 /* Issue an error message indicating that TOKEN_DESC was expected.
33902 If KEYWORD is true, it indicated this function is called by
33903 cp_parser_require_keword and the required token can only be
33904 a indicated keyword.
33906 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33907 within any error as the location of an "opening" token matching
33908 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33909 RT_CLOSE_PAREN). */
33911 static void
33912 cp_parser_required_error (cp_parser *parser,
33913 required_token token_desc,
33914 bool keyword,
33915 location_t matching_location)
33917 if (cp_parser_simulate_error (parser))
33918 return;
33920 const char *gmsgid = NULL;
33921 switch (token_desc)
33923 case RT_NEW:
33924 gmsgid = G_("expected %<new%>");
33925 break;
33926 case RT_DELETE:
33927 gmsgid = G_("expected %<delete%>");
33928 break;
33929 case RT_RETURN:
33930 gmsgid = G_("expected %<return%>");
33931 break;
33932 case RT_WHILE:
33933 gmsgid = G_("expected %<while%>");
33934 break;
33935 case RT_EXTERN:
33936 gmsgid = G_("expected %<extern%>");
33937 break;
33938 case RT_STATIC_ASSERT:
33939 gmsgid = G_("expected %<static_assert%>");
33940 break;
33941 case RT_DECLTYPE:
33942 gmsgid = G_("expected %<decltype%>");
33943 break;
33944 case RT_OPERATOR:
33945 gmsgid = G_("expected %<operator%>");
33946 break;
33947 case RT_CLASS:
33948 gmsgid = G_("expected %<class%>");
33949 break;
33950 case RT_TEMPLATE:
33951 gmsgid = G_("expected %<template%>");
33952 break;
33953 case RT_NAMESPACE:
33954 gmsgid = G_("expected %<namespace%>");
33955 break;
33956 case RT_USING:
33957 gmsgid = G_("expected %<using%>");
33958 break;
33959 case RT_ASM:
33960 gmsgid = G_("expected %<asm%>");
33961 break;
33962 case RT_TRY:
33963 gmsgid = G_("expected %<try%>");
33964 break;
33965 case RT_CATCH:
33966 gmsgid = G_("expected %<catch%>");
33967 break;
33968 case RT_THROW:
33969 gmsgid = G_("expected %<throw%>");
33970 break;
33971 case RT_AUTO:
33972 gmsgid = G_("expected %<auto%>");
33973 break;
33974 case RT_LABEL:
33975 gmsgid = G_("expected %<__label__%>");
33976 break;
33977 case RT_AT_TRY:
33978 gmsgid = G_("expected %<@try%>");
33979 break;
33980 case RT_AT_SYNCHRONIZED:
33981 gmsgid = G_("expected %<@synchronized%>");
33982 break;
33983 case RT_AT_THROW:
33984 gmsgid = G_("expected %<@throw%>");
33985 break;
33986 case RT_TRANSACTION_ATOMIC:
33987 gmsgid = G_("expected %<__transaction_atomic%>");
33988 break;
33989 case RT_TRANSACTION_RELAXED:
33990 gmsgid = G_("expected %<__transaction_relaxed%>");
33991 break;
33992 case RT_CO_YIELD:
33993 gmsgid = G_("expected %<co_yield%>");
33994 break;
33995 default:
33996 break;
33999 if (!gmsgid && !keyword)
34001 switch (token_desc)
34003 case RT_SEMICOLON:
34004 gmsgid = G_("expected %<;%>");
34005 break;
34006 case RT_OPEN_PAREN:
34007 gmsgid = G_("expected %<(%>");
34008 break;
34009 case RT_CLOSE_BRACE:
34010 gmsgid = G_("expected %<}%>");
34011 break;
34012 case RT_OPEN_BRACE:
34013 gmsgid = G_("expected %<{%>");
34014 break;
34015 case RT_CLOSE_SQUARE:
34016 gmsgid = G_("expected %<]%>");
34017 break;
34018 case RT_OPEN_SQUARE:
34019 gmsgid = G_("expected %<[%>");
34020 break;
34021 case RT_COMMA:
34022 gmsgid = G_("expected %<,%>");
34023 break;
34024 case RT_SCOPE:
34025 gmsgid = G_("expected %<::%>");
34026 break;
34027 case RT_LESS:
34028 gmsgid = G_("expected %<<%>");
34029 break;
34030 case RT_GREATER:
34031 gmsgid = G_("expected %<>%>");
34032 break;
34033 case RT_EQ:
34034 gmsgid = G_("expected %<=%>");
34035 break;
34036 case RT_ELLIPSIS:
34037 gmsgid = G_("expected %<...%>");
34038 break;
34039 case RT_MULT:
34040 gmsgid = G_("expected %<*%>");
34041 break;
34042 case RT_COMPL:
34043 gmsgid = G_("expected %<~%>");
34044 break;
34045 case RT_COLON:
34046 gmsgid = G_("expected %<:%>");
34047 break;
34048 case RT_COLON_SCOPE:
34049 gmsgid = G_("expected %<:%> or %<::%>");
34050 break;
34051 case RT_CLOSE_PAREN:
34052 gmsgid = G_("expected %<)%>");
34053 break;
34054 case RT_COMMA_CLOSE_PAREN:
34055 gmsgid = G_("expected %<,%> or %<)%>");
34056 break;
34057 case RT_PRAGMA_EOL:
34058 gmsgid = G_("expected end of line");
34059 break;
34060 case RT_NAME:
34061 gmsgid = G_("expected identifier");
34062 break;
34063 case RT_SELECT:
34064 gmsgid = G_("expected selection-statement");
34065 break;
34066 case RT_ITERATION:
34067 gmsgid = G_("expected iteration-statement");
34068 break;
34069 case RT_JUMP:
34070 gmsgid = G_("expected jump-statement");
34071 break;
34072 case RT_CLASS_KEY:
34073 gmsgid = G_("expected class-key");
34074 break;
34075 case RT_CLASS_TYPENAME_TEMPLATE:
34076 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34077 break;
34078 default:
34079 gcc_unreachable ();
34083 if (gmsgid)
34084 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34088 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34089 issue an error message indicating that TOKEN_DESC was expected.
34091 Returns the token consumed, if the token had the appropriate type.
34092 Otherwise, returns NULL.
34094 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34095 within any error as the location of an "opening" token matching
34096 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34097 RT_CLOSE_PAREN). */
34099 static cp_token *
34100 cp_parser_require (cp_parser* parser,
34101 enum cpp_ttype type,
34102 required_token token_desc,
34103 location_t matching_location)
34105 if (cp_lexer_next_token_is (parser->lexer, type))
34106 return cp_lexer_consume_token (parser->lexer);
34107 else
34109 /* Output the MESSAGE -- unless we're parsing tentatively. */
34110 if (!cp_parser_simulate_error (parser))
34111 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34112 matching_location);
34113 return NULL;
34117 /* Skip an entire parameter list from start to finish. The next token must
34118 be the initial "<" of the parameter list. Returns true on success and
34119 false otherwise. */
34121 static bool
34122 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34124 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34125 requires it. */
34126 cp_lexer_consume_token (parser->lexer);
34127 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34130 /* Ensure we are at the end of a template parameter list. If we are, return.
34131 If we are not, something has gone wrong, in which case issue an error and
34132 skip to end of the parameter list. */
34134 static void
34135 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34137 /* Are we ready, yet? If not, issue error message. */
34138 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34139 return;
34141 cp_parser_skip_to_end_of_template_parameter_list (parser);
34144 /* You should only call this function from inside a template parameter list
34145 (i.e. the current token should at least be the initial "<" of the
34146 parameter list). If you are skipping the entire list, it may be better to
34147 use cp_parser_skip_entire_template_parameter_list.
34149 Tokens are skipped until the final ">" is found, or if we see
34150 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34152 Returns true if we successfully reached the end, and false if
34153 something unexpected happened (e.g. end of file). */
34155 static bool
34156 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34158 /* Current level of '< ... >'. */
34159 unsigned level = 0;
34160 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34161 unsigned nesting_depth = 0;
34163 /* Skip tokens until the desired token is found. */
34164 while (true)
34166 /* Peek at the next token. */
34167 switch (cp_lexer_peek_token (parser->lexer)->type)
34169 case CPP_LESS:
34170 if (!nesting_depth)
34171 ++level;
34172 break;
34174 case CPP_RSHIFT:
34175 if (cxx_dialect == cxx98)
34176 /* C++0x views the `>>' operator as two `>' tokens, but
34177 C++98 does not. */
34178 break;
34179 else if (!nesting_depth && level-- == 0)
34181 /* We've hit a `>>' where the first `>' closes the
34182 template argument list, and the second `>' is
34183 spurious. Just consume the `>>' and stop; we've
34184 already produced at least one error. */
34185 cp_lexer_consume_token (parser->lexer);
34186 return false;
34188 /* Fall through for C++0x, so we handle the second `>' in
34189 the `>>'. */
34190 gcc_fallthrough ();
34192 case CPP_GREATER:
34193 if (!nesting_depth && level-- == 0)
34195 /* We've reached the token we want, consume it and stop. */
34196 cp_lexer_consume_token (parser->lexer);
34197 return true;
34199 break;
34201 case CPP_OPEN_PAREN:
34202 case CPP_OPEN_SQUARE:
34203 ++nesting_depth;
34204 break;
34206 case CPP_CLOSE_PAREN:
34207 case CPP_CLOSE_SQUARE:
34208 if (nesting_depth-- == 0)
34209 return false;
34210 break;
34212 case CPP_EOF:
34213 case CPP_PRAGMA_EOL:
34214 case CPP_SEMICOLON:
34215 case CPP_OPEN_BRACE:
34216 case CPP_CLOSE_BRACE:
34217 /* The '>' was probably forgotten, don't look further. */
34218 return false;
34220 default:
34221 break;
34224 /* Consume this token. */
34225 cp_lexer_consume_token (parser->lexer);
34229 /* If the next token is the indicated keyword, consume it. Otherwise,
34230 issue an error message indicating that TOKEN_DESC was expected.
34232 Returns the token consumed, if the token had the appropriate type.
34233 Otherwise, returns NULL. */
34235 static cp_token *
34236 cp_parser_require_keyword (cp_parser* parser,
34237 enum rid keyword,
34238 required_token token_desc)
34240 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34242 if (token && token->keyword != keyword)
34244 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34245 UNKNOWN_LOCATION);
34246 return NULL;
34249 return token;
34252 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34253 function-definition. */
34255 static bool
34256 cp_parser_token_starts_function_definition_p (cp_token* token)
34258 return (/* An ordinary function-body begins with an `{'. */
34259 token->type == CPP_OPEN_BRACE
34260 /* A ctor-initializer begins with a `:'. */
34261 || token->type == CPP_COLON
34262 /* A function-try-block begins with `try'. */
34263 || token->keyword == RID_TRY
34264 /* A function-transaction-block begins with `__transaction_atomic'
34265 or `__transaction_relaxed'. */
34266 || token->keyword == RID_TRANSACTION_ATOMIC
34267 || token->keyword == RID_TRANSACTION_RELAXED
34268 /* The named return value extension begins with `return'. */
34269 || token->keyword == RID_RETURN);
34272 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34273 definition. */
34275 static bool
34276 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34278 cp_token *token;
34280 token = cp_lexer_peek_token (parser->lexer);
34281 return (token->type == CPP_OPEN_BRACE
34282 || (token->type == CPP_COLON
34283 && !parser->colon_doesnt_start_class_def_p));
34286 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34287 C++0x) ending a template-argument. */
34289 static bool
34290 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34292 cp_token *token;
34294 token = cp_lexer_peek_token (parser->lexer);
34295 return (token->type == CPP_COMMA
34296 || token->type == CPP_GREATER
34297 || token->type == CPP_ELLIPSIS
34298 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34299 /* For better diagnostics, treat >>= like that too, that
34300 shouldn't appear non-nested in template arguments. */
34301 || token->type == CPP_RSHIFT_EQ);
34304 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34305 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34307 static bool
34308 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34309 size_t n)
34311 cp_token *token;
34313 token = cp_lexer_peek_nth_token (parser->lexer, n);
34314 if (token->type == CPP_LESS)
34315 return true;
34316 /* Check for the sequence `<::' in the original code. It would be lexed as
34317 `[:', where `[' is a digraph, and there is no whitespace before
34318 `:'. */
34319 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34321 cp_token *token2;
34322 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34323 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34324 return true;
34326 return false;
34329 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34330 or none_type otherwise. */
34332 static enum tag_types
34333 cp_parser_token_is_class_key (cp_token* token)
34335 switch (token->keyword)
34337 case RID_CLASS:
34338 return class_type;
34339 case RID_STRUCT:
34340 return record_type;
34341 case RID_UNION:
34342 return union_type;
34344 default:
34345 return none_type;
34349 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34350 or none_type otherwise or if the token is null. */
34352 static enum tag_types
34353 cp_parser_token_is_type_parameter_key (cp_token* token)
34355 if (!token)
34356 return none_type;
34358 switch (token->keyword)
34360 case RID_CLASS:
34361 return class_type;
34362 case RID_TYPENAME:
34363 return typename_type;
34365 default:
34366 return none_type;
34370 /* Diagnose redundant enum-keys. */
34372 static void
34373 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34374 tree type, rid scoped_key)
34376 if (!warn_redundant_tags)
34377 return;
34379 tree type_decl = TYPE_MAIN_DECL (type);
34380 tree name = DECL_NAME (type_decl);
34381 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34382 push_deferring_access_checks (dk_no_check);
34383 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34384 pop_deferring_access_checks ();
34386 /* The enum-key is redundant for uses of the TYPE that are not
34387 declarations and for which name lookup returns just the type
34388 itself. */
34389 if (decl != type_decl)
34390 return;
34392 if (scoped_key != RID_CLASS
34393 && scoped_key != RID_STRUCT
34394 && current_lang_name != lang_name_cplusplus
34395 && current_namespace == global_namespace)
34397 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34398 enum tag in shared C/C++ code in files (such as headers) included
34399 in the main source file. */
34400 const line_map_ordinary *map = NULL;
34401 linemap_resolve_location (line_table, key_loc,
34402 LRK_MACRO_DEFINITION_LOCATION,
34403 &map);
34404 if (!MAIN_FILE_P (map))
34405 return;
34408 gcc_rich_location richloc (key_loc);
34409 richloc.add_fixit_remove (key_loc);
34410 warning_at (&richloc, OPT_Wredundant_tags,
34411 "redundant enum-key %<enum%s%> in reference to %q#T",
34412 (scoped_key == RID_CLASS ? " class"
34413 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34416 /* Describes the set of declarations of a struct, class, or class template
34417 or its specializations. Used for -Wmismatched-tags. */
34419 class class_decl_loc_t
34421 public:
34423 class_decl_loc_t ()
34424 : locvec (), idxdef (), def_class_key ()
34426 locvec.create (4);
34429 /* Constructs an object for a single declaration of a class with
34430 CLASS_KEY at the current location in the current function (or
34431 at another scope). KEY_REDUNDANT is true if the class-key may
34432 be omitted in the current context without an ambiguity with
34433 another symbol with the same name.
34434 DEF_P is true for a class declaration that is a definition.
34435 CURLOC is the associated location. */
34436 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34437 location_t curloc = input_location)
34438 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34440 locvec.create (4);
34441 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34442 key_redundant);
34443 locvec.quick_push (ckl);
34446 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34447 isn't safely copyable and assignable and doesn't release storage
34448 on its own. */
34449 class_decl_loc_t (const class_decl_loc_t &rhs)
34450 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34451 def_class_key (rhs.def_class_key)
34454 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34456 if (this == &rhs)
34457 return *this;
34458 locvec.release ();
34459 locvec = rhs.locvec.copy ();
34460 idxdef = rhs.idxdef;
34461 def_class_key = rhs.def_class_key;
34462 return *this;
34465 ~class_decl_loc_t ()
34467 locvec.release ();
34470 /* Issues -Wmismatched-tags for a single class. */
34471 void diag_mismatched_tags (tree);
34473 /* Issues -Wmismatched-tags for all classes. */
34474 static void diag_mismatched_tags ();
34476 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34477 redundant tags (if -Wredundant-tags is enabled). */
34478 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34480 /* Either adds this decl to the collection of class decls
34481 or diagnoses it, whichever is appropriate. */
34482 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34484 private:
34486 tree function (unsigned i) const
34488 return locvec[i].func;
34491 location_t location (unsigned i) const
34493 return locvec[i].loc;
34496 bool key_redundant (unsigned i) const
34498 return locvec[i].key_redundant;
34501 tag_types class_key (unsigned i) const
34503 return locvec[i].class_key;
34506 /* True if a definition for the class has been seen. */
34507 bool def_p () const
34509 return idxdef < locvec.length ();
34512 /* The location of a single mention of a class type with the given
34513 class-key. */
34514 struct class_key_loc_t
34516 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34517 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34520 /* The function the type is mentioned in. */
34521 tree func;
34522 /* The exact location. */
34523 location_t loc;
34524 /* The class-key used in the mention of the type. */
34525 tag_types class_key;
34526 /* True when the class-key could be omitted at this location
34527 without an ambiguity with another symbol of the same name. */
34528 bool key_redundant;
34530 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34531 vec <class_key_loc_t> locvec;
34532 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34533 unsigned idxdef;
34534 /* The class-key the class was last declared with or none_type when
34535 it has been declared with a mismatched key. */
34536 tag_types def_class_key;
34538 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34539 description above. */
34540 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34541 static class_to_loc_map_t class2loc;
34544 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34546 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34547 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34548 is set for a declaration of class TYPE and clear for a reference to
34549 it that is not a declaration of it. */
34551 static void
34552 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34553 tag_types class_key, tree type, bool def_p,
34554 bool decl_p)
34556 if (type == error_mark_node)
34557 return;
34559 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34560 if (seen_as_union != (class_key == union_type))
34562 if (permerror (input_location, "%qs tag used in naming %q#T",
34563 class_key == union_type ? "union"
34564 : class_key == record_type ? "struct" : "class",
34565 type))
34566 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34567 "%q#T was previously declared here", type);
34568 return;
34571 if (!warn_mismatched_tags && !warn_redundant_tags)
34572 return;
34574 /* Only consider the true class-keys below and ignore typename_type,
34575 etc. that are not C++ class-keys. */
34576 if (class_key != class_type
34577 && class_key != record_type
34578 && class_key != union_type)
34579 return;
34581 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34584 /* Returns the template or specialization of one to which the RECORD_TYPE
34585 TYPE corresponds. */
34587 static tree
34588 specialization_of (tree type)
34590 tree ret = type;
34592 /* Determine the template or its partial specialization to which TYPE
34593 corresponds. */
34594 if (tree ti = most_specialized_partial_spec (type, tf_none))
34595 if (ti != error_mark_node)
34596 ret = TREE_TYPE (TI_TEMPLATE (ti));
34598 if (ret == type)
34599 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34601 return TYPE_MAIN_DECL (ret);
34605 /* Adds the class TYPE to the collection of class decls and diagnoses
34606 redundant tags (if -Wredundant-tags is enabled).
34607 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34608 is set for a (likely, based on syntactic context) declaration of class
34609 TYPE and clear for a reference to it that is not a declaration of it. */
34611 void
34612 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34613 tag_types class_key, tree type, bool def_p, bool decl_p)
34615 tree type_decl = TYPE_MAIN_DECL (type);
34616 tree name = DECL_NAME (type_decl);
34617 /* Look up the NAME to see if it unambiguously refers to the TYPE
34618 and set KEY_REDUNDANT if so. */
34619 push_deferring_access_checks (dk_no_check);
34620 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34621 pop_deferring_access_checks ();
34623 /* The class-key is redundant for uses of the CLASS_TYPE that are
34624 neither definitions of it nor declarations, and for which name
34625 lookup returns just the type itself. */
34626 bool key_redundant = (!def_p && !decl_p
34627 && (decl == type_decl
34628 || TREE_CODE (decl) == TEMPLATE_DECL
34629 || (CLASS_TYPE_P (type)
34630 && TYPE_BEING_DEFINED (type))));
34632 if (key_redundant
34633 && class_key != class_type
34634 && current_lang_name != lang_name_cplusplus
34635 && current_namespace == global_namespace)
34637 /* Avoid issuing the diagnostic for apparently redundant struct
34638 and union class-keys in shared C/C++ code in files (such as
34639 headers) included in the main source file. */
34640 const line_map_ordinary *map = NULL;
34641 linemap_resolve_location (line_table, key_loc,
34642 LRK_MACRO_DEFINITION_LOCATION,
34643 &map);
34644 if (!MAIN_FILE_P (map))
34645 key_redundant = false;
34648 /* Set if a declaration of TYPE has previously been seen or if it must
34649 exist in a precompiled header. */
34650 bool exist;
34651 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34652 if (!exist)
34654 tree type = TREE_TYPE (type_decl);
34655 if (def_p || !COMPLETE_TYPE_P (type))
34657 /* TYPE_DECL is the first declaration or definition of the type
34658 (outside precompiled headers -- see below). Just create
34659 a new entry for it and return unless it's a declaration
34660 involving a template that may need to be diagnosed by
34661 -Wredundant-tags. */
34662 *rdl = class_decl_loc_t (class_key, false, def_p);
34663 if (TREE_CODE (decl) != TEMPLATE_DECL)
34664 return;
34666 else
34668 /* TYPE was previously defined in some unknown precompiled header.
34669 Simply add a record of its definition at an unknown location and
34670 proceed below to add a reference to it at the current location.
34671 (Declarations in precompiled headers that are not definitions
34672 are ignored.) */
34673 tag_types def_key
34674 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34675 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34676 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34677 exist = true;
34681 /* A prior declaration of TYPE_DECL has been seen. */
34683 if (key_redundant)
34685 gcc_rich_location richloc (key_loc);
34686 richloc.add_fixit_remove (key_loc);
34687 warning_at (&richloc, OPT_Wredundant_tags,
34688 "redundant class-key %qs in reference to %q#T",
34689 class_key == union_type ? "union"
34690 : class_key == record_type ? "struct" : "class",
34691 type);
34694 if (!exist)
34695 /* Do nothing if this is the first declaration of the type. */
34696 return;
34698 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34699 /* Do nothing if the class-key in this declaration matches
34700 the definition. */
34701 return;
34703 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34704 def_p);
34707 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34708 of class decls or diagnoses it, whichever is appropriate. */
34710 void
34711 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34712 tag_types class_key,
34713 bool redundant,
34714 bool def_p)
34716 /* Reset the CLASS_KEY associated with this type on mismatch.
34717 This is an optimization that lets the diagnostic code skip
34718 over classes that use the same class-key in all declarations. */
34719 if (def_class_key != class_key)
34720 def_class_key = none_type;
34722 /* Set IDXDEF to the index of the vector corresponding to
34723 the definition. */
34724 if (def_p)
34725 idxdef = locvec.length ();
34727 /* Append a record of this declaration to the vector. */
34728 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34729 redundant);
34730 locvec.safe_push (ckl);
34732 if (idxdef == UINT_MAX)
34733 return;
34735 /* As a space optimization diagnose declarations of a class
34736 whose definition has been seen and purge the LOCVEC of
34737 all entries except the definition. */
34738 diag_mismatched_tags (type_decl);
34739 if (idxdef)
34741 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
34742 locvec.release ();
34743 locvec.reserve (2);
34744 locvec.safe_push (ent);
34745 idxdef = 0;
34747 else
34748 /* Pop the entry pushed above for this declaration. */
34749 locvec.pop ();
34752 /* Issues -Wmismatched-tags for a single class. */
34754 void
34755 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
34757 if (!warn_mismatched_tags)
34758 return;
34760 /* Number of uses of the class. */
34761 const unsigned ndecls = locvec.length ();
34763 /* The class (or template) declaration guiding the decisions about
34764 the diagnostic. For ordinary classes it's the same as THIS. For
34765 uses of instantiations of templates other than their declarations
34766 it points to the record for the declaration of the corresponding
34767 primary template or partial specialization. */
34768 class_decl_loc_t *cdlguide = this;
34770 tree type = TREE_TYPE (type_decl);
34771 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
34773 /* For implicit instantiations of a primary template look up
34774 the primary or partial specialization and use it as
34775 the expected class-key rather than using the class-key of
34776 the first reference to the instantiation. The primary must
34777 be (and inevitably is) at index zero. */
34778 tree spec = specialization_of (type);
34779 cdlguide = class2loc.get (spec);
34780 /* It's possible that we didn't find SPEC. Consider:
34782 template<typename T> struct A {
34783 template<typename U> struct W { };
34785 struct A<int>::W<int> w; // #1
34787 where while parsing A and #1 we've stashed
34788 A<T>
34789 A<T>::W<U>
34790 A<int>::W<int>
34791 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
34792 will yield A<int>::W<U> which may be in CLASS2LOC if we had
34793 an A<int> class specialization, but otherwise won't be in it.
34794 So try to look up A<T>::W<U>. */
34795 if (!cdlguide)
34797 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
34798 cdlguide = class2loc.get (spec);
34800 /* Now we really should have found something. */
34801 gcc_assert (cdlguide != NULL);
34803 /* Skip declarations that consistently use the same class-key. */
34804 else if (def_class_key != none_type)
34805 return;
34807 /* Set if a definition for the class has been seen. */
34808 const bool def_p = cdlguide->def_p ();
34810 /* The index of the declaration whose class-key this declaration
34811 is expected to match. It's either the class-key of the class
34812 definition if one exists or the first declaration otherwise. */
34813 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
34815 /* The class-key the class is expected to be declared with: it's
34816 either the key used in its definition or the first declaration
34817 if no definition has been provided.
34818 For implicit instantiations of a primary template it's
34819 the class-key used to declare the primary with. The primary
34820 must be at index zero. */
34821 const tag_types xpect_key = cdlguide->class_key (idxguide);
34823 unsigned idx = 0;
34824 /* Advance IDX to the first declaration that either is not
34825 a definition or that doesn't match the first declaration
34826 if no definition is provided. */
34827 while (class_key (idx) == xpect_key)
34828 if (++idx == ndecls)
34829 return;
34831 /* Save the current function before changing it below. */
34832 tree save_func = current_function_decl;
34833 /* Set the function declaration to print in diagnostic context. */
34834 current_function_decl = function (idx);
34836 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
34837 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
34839 location_t loc = location (idx);
34840 bool key_redundant_p = key_redundant (idx);
34841 auto_diagnostic_group d;
34842 /* Issue a warning for the first mismatched declaration.
34843 Avoid using "%#qT" since the class-key for the same type will
34844 be the same regardless of which one was used in the declaraion. */
34845 if (warning_at (loc, OPT_Wmismatched_tags,
34846 "%qT declared with a mismatched class-key %qs",
34847 type_decl, xmatchkstr))
34849 /* Suggest how to avoid the warning for each instance since
34850 the guidance may be different depending on context. */
34851 inform (loc,
34852 (key_redundant_p
34853 ? G_("remove the class-key or replace it with %qs")
34854 : G_("replace the class-key with %qs")),
34855 xpectkstr);
34857 /* Also point to the first declaration or definition that guided
34858 the decision to issue the warning above. */
34859 inform (cdlguide->location (idxguide),
34860 (def_p
34861 ? G_("%qT defined as %qs here")
34862 : G_("%qT first declared as %qs here")),
34863 type_decl, xpectkstr);
34866 /* Issue warnings for the remaining inconsistent declarations. */
34867 for (unsigned i = idx + 1; i != ndecls; ++i)
34869 tag_types clskey = class_key (i);
34870 /* Skip over the declarations that match either the definition
34871 if one was provided or the first declaration. */
34872 if (clskey == xpect_key)
34873 continue;
34875 loc = location (i);
34876 key_redundant_p = key_redundant (i);
34877 /* Set the function declaration to print in diagnostic context. */
34878 current_function_decl = function (i);
34879 if (warning_at (loc, OPT_Wmismatched_tags,
34880 "%qT declared with a mismatched class-key %qs",
34881 type_decl, xmatchkstr))
34882 /* Suggest how to avoid the warning for each instance since
34883 the guidance may be different depending on context. */
34884 inform (loc,
34885 (key_redundant_p
34886 ? G_("remove the class-key or replace it with %qs")
34887 : G_("replace the class-key with %qs")),
34888 xpectkstr);
34891 /* Restore the current function in case it was replaced above. */
34892 current_function_decl = save_func;
34895 /* Issues -Wmismatched-tags for all classes. Called at the end
34896 of processing a translation unit, after declarations of all class
34897 types and their uses have been recorded. */
34899 void
34900 class_decl_loc_t::diag_mismatched_tags ()
34902 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34903 -Wredundant-tags are disabled. */
34904 gcc_assert (warn_mismatched_tags
34905 || warn_redundant_tags
34906 || class2loc.is_empty ());
34908 /* Save the current function before changing on return. It should
34909 be null at this point. */
34910 temp_override<tree> cleanup (current_function_decl);
34912 if (warn_mismatched_tags)
34914 /* Iterate over the collected class/struct/template declarations. */
34915 typedef class_to_loc_map_t::iterator iter_t;
34916 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
34918 tree type_decl = (*it).first;
34919 class_decl_loc_t &recloc = (*it).second;
34920 recloc.diag_mismatched_tags (type_decl);
34924 class2loc.empty ();
34927 /* Issue an error message if DECL is redeclared with different
34928 access than its original declaration [class.access.spec/3].
34929 This applies to nested classes, nested class templates and
34930 enumerations [class.mem/1]. */
34932 static void
34933 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
34935 if (!decl
34936 || (!CLASS_TYPE_P (TREE_TYPE (decl))
34937 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
34938 return;
34940 if ((TREE_PRIVATE (decl)
34941 != (current_access_specifier == access_private_node))
34942 || (TREE_PROTECTED (decl)
34943 != (current_access_specifier == access_protected_node)))
34944 error_at (location, "%qD redeclared with different access", decl);
34947 /* Look for the `template' keyword, as a syntactic disambiguator.
34948 Return TRUE iff it is present, in which case it will be
34949 consumed. */
34951 static bool
34952 cp_parser_optional_template_keyword (cp_parser *parser)
34954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34956 /* In C++98 the `template' keyword can only be used within templates;
34957 outside templates the parser can always figure out what is a
34958 template and what is not. In C++11, per the resolution of DR 468,
34959 `template' is allowed in cases where it is not strictly necessary. */
34960 if (!processing_template_decl
34961 && pedantic && cxx_dialect == cxx98)
34963 cp_token *token = cp_lexer_peek_token (parser->lexer);
34964 pedwarn (token->location, OPT_Wpedantic,
34965 "in C++98 %<template%> (as a disambiguator) is only "
34966 "allowed within templates");
34967 /* If this part of the token stream is rescanned, the same
34968 error message would be generated. So, we purge the token
34969 from the stream. */
34970 cp_lexer_purge_token (parser->lexer);
34971 return false;
34973 else
34975 /* Consume the `template' keyword. */
34976 cp_lexer_consume_token (parser->lexer);
34977 return true;
34980 return false;
34983 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34984 set PARSER->SCOPE, and perform other related actions. */
34986 static void
34987 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34989 struct tree_check *check_value;
34991 /* Get the stored value. */
34992 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34993 /* Set the scope from the stored value. */
34994 parser->scope = saved_checks_value (check_value);
34995 parser->qualifying_scope = check_value->qualifying_scope;
34996 parser->object_scope = parser->context->object_type;
34997 parser->context->object_type = NULL_TREE;
35000 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35001 encounter the end of a block before what we were looking for. */
35003 static bool
35004 cp_parser_cache_group (cp_parser *parser,
35005 enum cpp_ttype end,
35006 unsigned depth)
35008 while (true)
35010 cp_token *token = cp_lexer_peek_token (parser->lexer);
35012 /* Abort a parenthesized expression if we encounter a semicolon. */
35013 if ((end == CPP_CLOSE_PAREN || depth == 0)
35014 && token->type == CPP_SEMICOLON)
35015 return true;
35016 /* If we've reached the end of the file, stop. */
35017 if (token->type == CPP_EOF
35018 || (end != CPP_PRAGMA_EOL
35019 && token->type == CPP_PRAGMA_EOL))
35020 return true;
35021 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35022 /* We've hit the end of an enclosing block, so there's been some
35023 kind of syntax error. */
35024 return true;
35026 /* Consume the token. */
35027 cp_lexer_consume_token (parser->lexer);
35028 /* See if it starts a new group. */
35029 if (token->type == CPP_OPEN_BRACE)
35031 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35032 /* In theory this should probably check end == '}', but
35033 cp_parser_save_member_function_body needs it to exit
35034 after either '}' or ')' when called with ')'. */
35035 if (depth == 0)
35036 return false;
35038 else if (token->type == CPP_OPEN_PAREN)
35040 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35041 if (depth == 0 && end == CPP_CLOSE_PAREN)
35042 return false;
35044 else if (token->type == CPP_PRAGMA)
35045 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35046 else if (token->type == end)
35047 return false;
35051 /* Like above, for caching a default argument or NSDMI. Both of these are
35052 terminated by a non-nested comma, but it can be unclear whether or not a
35053 comma is nested in a template argument list unless we do more parsing.
35054 In order to handle this ambiguity, when we encounter a ',' after a '<'
35055 we try to parse what follows as a parameter-declaration-list (in the
35056 case of a default argument) or a member-declarator (in the case of an
35057 NSDMI). If that succeeds, then we stop caching. */
35059 static tree
35060 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35062 unsigned depth = 0;
35063 int maybe_template_id = 0;
35064 cp_token *first_token;
35065 cp_token *token;
35066 tree default_argument;
35068 /* Add tokens until we have processed the entire default
35069 argument. We add the range [first_token, token). */
35070 first_token = cp_lexer_peek_token (parser->lexer);
35071 if (first_token->type == CPP_OPEN_BRACE)
35073 /* For list-initialization, this is straightforward. */
35074 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35075 token = cp_lexer_peek_token (parser->lexer);
35077 else while (true)
35079 bool done = false;
35081 /* Peek at the next token. */
35082 token = cp_lexer_peek_token (parser->lexer);
35083 /* What we do depends on what token we have. */
35084 switch (token->type)
35086 /* In valid code, a default argument must be
35087 immediately followed by a `,' `)', or `...'. */
35088 case CPP_COMMA:
35089 if (depth == 0 && maybe_template_id)
35091 /* If we've seen a '<', we might be in a
35092 template-argument-list. Until Core issue 325 is
35093 resolved, we don't know how this situation ought
35094 to be handled, so try to DTRT. We check whether
35095 what comes after the comma is a valid parameter
35096 declaration list. If it is, then the comma ends
35097 the default argument; otherwise the default
35098 argument continues. */
35099 bool error = false;
35100 cp_token *peek;
35102 /* Set ITALP so cp_parser_parameter_declaration_list
35103 doesn't decide to commit to this parse. */
35104 bool saved_italp = parser->in_template_argument_list_p;
35105 parser->in_template_argument_list_p = true;
35107 cp_parser_parse_tentatively (parser);
35109 if (nsdmi)
35111 /* Parse declarators until we reach a non-comma or
35112 somthing that cannot be an initializer.
35113 Just checking whether we're looking at a single
35114 declarator is insufficient. Consider:
35115 int var = tuple<T,U>::x;
35116 The template parameter 'U' looks exactly like a
35117 declarator. */
35120 int ctor_dtor_or_conv_p;
35121 cp_lexer_consume_token (parser->lexer);
35122 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35123 CP_PARSER_FLAGS_NONE,
35124 &ctor_dtor_or_conv_p,
35125 /*parenthesized_p=*/NULL,
35126 /*member_p=*/true,
35127 /*friend_p=*/false,
35128 /*static_p=*/false);
35129 peek = cp_lexer_peek_token (parser->lexer);
35130 if (cp_parser_error_occurred (parser))
35131 break;
35133 while (peek->type == CPP_COMMA);
35134 /* If we met an '=' or ';' then the original comma
35135 was the end of the NSDMI. Otherwise assume
35136 we're still in the NSDMI. */
35137 error = (peek->type != CPP_EQ
35138 && peek->type != CPP_SEMICOLON);
35140 else
35142 cp_lexer_consume_token (parser->lexer);
35143 begin_scope (sk_function_parms, NULL_TREE);
35144 tree t = cp_parser_parameter_declaration_list
35145 (parser, CP_PARSER_FLAGS_NONE,
35146 /*pending_decls*/nullptr);
35147 if (t == error_mark_node)
35148 error = true;
35149 pop_bindings_and_leave_scope ();
35151 if (!cp_parser_error_occurred (parser) && !error)
35152 done = true;
35153 cp_parser_abort_tentative_parse (parser);
35155 parser->in_template_argument_list_p = saved_italp;
35156 break;
35158 /* FALLTHRU */
35159 case CPP_CLOSE_PAREN:
35160 case CPP_ELLIPSIS:
35161 /* If we run into a non-nested `;', `}', or `]',
35162 then the code is invalid -- but the default
35163 argument is certainly over. */
35164 case CPP_SEMICOLON:
35165 case CPP_CLOSE_BRACE:
35166 case CPP_CLOSE_SQUARE:
35167 if (depth == 0
35168 /* Handle correctly int n = sizeof ... ( p ); */
35169 && token->type != CPP_ELLIPSIS)
35170 done = true;
35171 /* Update DEPTH, if necessary. */
35172 else if (token->type == CPP_CLOSE_PAREN
35173 || token->type == CPP_CLOSE_BRACE
35174 || token->type == CPP_CLOSE_SQUARE)
35175 --depth;
35176 break;
35178 case CPP_OPEN_PAREN:
35179 case CPP_OPEN_SQUARE:
35180 case CPP_OPEN_BRACE:
35181 ++depth;
35182 break;
35184 case CPP_LESS:
35185 if (depth == 0)
35186 /* This might be the comparison operator, or it might
35187 start a template argument list. */
35188 ++maybe_template_id;
35189 break;
35191 case CPP_RSHIFT:
35192 if (cxx_dialect == cxx98)
35193 break;
35194 /* Fall through for C++0x, which treats the `>>'
35195 operator like two `>' tokens in certain
35196 cases. */
35197 gcc_fallthrough ();
35199 case CPP_GREATER:
35200 if (depth == 0)
35202 /* This might be an operator, or it might close a
35203 template argument list. But if a previous '<'
35204 started a template argument list, this will have
35205 closed it, so we can't be in one anymore. */
35206 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35207 if (maybe_template_id < 0)
35208 maybe_template_id = 0;
35210 break;
35212 /* If we run out of tokens, issue an error message. */
35213 case CPP_EOF:
35214 case CPP_PRAGMA_EOL:
35215 error_at (token->location, "file ends in default argument");
35216 return error_mark_node;
35218 case CPP_NAME:
35219 case CPP_SCOPE:
35220 /* In these cases, we should look for template-ids.
35221 For example, if the default argument is
35222 `X<int, double>()', we need to do name lookup to
35223 figure out whether or not `X' is a template; if
35224 so, the `,' does not end the default argument.
35226 That is not yet done. */
35227 break;
35229 default:
35230 break;
35233 /* If we've reached the end, stop. */
35234 if (done)
35235 break;
35237 /* Add the token to the token block. */
35238 token = cp_lexer_consume_token (parser->lexer);
35241 /* Create a DEFERRED_PARSE to represent the unparsed default
35242 argument. */
35243 default_argument = make_node (DEFERRED_PARSE);
35244 DEFPARSE_TOKENS (default_argument)
35245 = cp_token_cache_new (first_token, token);
35246 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35248 return default_argument;
35251 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35253 location_t
35254 defparse_location (tree default_argument)
35256 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35257 location_t start = tokens->first->location;
35258 location_t end = tokens->last->location;
35259 return make_location (start, start, end);
35262 /* Begin parsing tentatively. We always save tokens while parsing
35263 tentatively so that if the tentative parsing fails we can restore the
35264 tokens. */
35266 static void
35267 cp_parser_parse_tentatively (cp_parser* parser)
35269 /* Enter a new parsing context. */
35270 parser->context = cp_parser_context_new (parser->context);
35271 /* Begin saving tokens. */
35272 cp_lexer_save_tokens (parser->lexer);
35273 /* In order to avoid repetitive access control error messages,
35274 access checks are queued up until we are no longer parsing
35275 tentatively. */
35276 push_deferring_access_checks (dk_deferred);
35279 /* Commit to the currently active tentative parse. */
35281 static void
35282 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35284 cp_parser_context *context;
35285 cp_lexer *lexer;
35287 /* Mark all of the levels as committed. */
35288 lexer = parser->lexer;
35289 for (context = parser->context; context->next; context = context->next)
35291 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35292 break;
35293 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35294 while (!cp_lexer_saving_tokens (lexer))
35295 lexer = lexer->next;
35296 cp_lexer_commit_tokens (lexer);
35300 /* Commit to the topmost currently active tentative parse.
35302 Note that this function shouldn't be called when there are
35303 irreversible side-effects while in a tentative state. For
35304 example, we shouldn't create a permanent entry in the symbol
35305 table, or issue an error message that might not apply if the
35306 tentative parse is aborted. */
35308 static void
35309 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35311 cp_parser_context *context = parser->context;
35312 cp_lexer *lexer = parser->lexer;
35314 if (context)
35316 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35317 return;
35318 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35320 while (!cp_lexer_saving_tokens (lexer))
35321 lexer = lexer->next;
35322 cp_lexer_commit_tokens (lexer);
35326 /* Abort the currently active tentative parse. All consumed tokens
35327 will be rolled back, and no diagnostics will be issued. */
35329 static void
35330 cp_parser_abort_tentative_parse (cp_parser* parser)
35332 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35333 || errorcount > 0);
35334 cp_parser_simulate_error (parser);
35335 /* Now, pretend that we want to see if the construct was
35336 successfully parsed. */
35337 cp_parser_parse_definitely (parser);
35340 /* Stop parsing tentatively. If a parse error has occurred, restore the
35341 token stream. Otherwise, commit to the tokens we have consumed.
35342 Returns true if no error occurred; false otherwise. */
35344 static bool
35345 cp_parser_parse_definitely (cp_parser* parser)
35347 bool error_occurred;
35348 cp_parser_context *context;
35350 /* Remember whether or not an error occurred, since we are about to
35351 destroy that information. */
35352 error_occurred = cp_parser_error_occurred (parser);
35353 /* Remove the topmost context from the stack. */
35354 context = parser->context;
35355 parser->context = context->next;
35356 /* If no parse errors occurred, commit to the tentative parse. */
35357 if (!error_occurred)
35359 /* Commit to the tokens read tentatively, unless that was
35360 already done. */
35361 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35362 cp_lexer_commit_tokens (parser->lexer);
35364 pop_to_parent_deferring_access_checks ();
35366 /* Otherwise, if errors occurred, roll back our state so that things
35367 are just as they were before we began the tentative parse. */
35368 else
35370 cp_lexer_rollback_tokens (parser->lexer);
35371 pop_deferring_access_checks ();
35373 /* Add the context to the front of the free list. */
35374 context->next = cp_parser_context_free_list;
35375 cp_parser_context_free_list = context;
35377 return !error_occurred;
35380 /* Returns true if we are parsing tentatively and are not committed to
35381 this tentative parse. */
35383 static bool
35384 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35386 return (cp_parser_parsing_tentatively (parser)
35387 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35390 /* Returns nonzero iff an error has occurred during the most recent
35391 tentative parse. */
35393 static bool
35394 cp_parser_error_occurred (cp_parser* parser)
35396 return (cp_parser_parsing_tentatively (parser)
35397 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35400 /* Returns nonzero if GNU extensions are allowed. */
35402 static bool
35403 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35405 return parser->allow_gnu_extensions_p;
35408 /* Objective-C++ Productions */
35411 /* Parse an Objective-C expression, which feeds into a primary-expression
35412 above.
35414 objc-expression:
35415 objc-message-expression
35416 objc-string-literal
35417 objc-encode-expression
35418 objc-protocol-expression
35419 objc-selector-expression
35421 Returns a tree representation of the expression. */
35423 static cp_expr
35424 cp_parser_objc_expression (cp_parser* parser)
35426 /* Try to figure out what kind of declaration is present. */
35427 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35429 switch (kwd->type)
35431 case CPP_OPEN_SQUARE:
35432 return cp_parser_objc_message_expression (parser);
35434 case CPP_OBJC_STRING:
35435 kwd = cp_lexer_consume_token (parser->lexer);
35436 return objc_build_string_object (kwd->u.value);
35438 case CPP_KEYWORD:
35439 switch (kwd->keyword)
35441 case RID_AT_ENCODE:
35442 return cp_parser_objc_encode_expression (parser);
35444 case RID_AT_PROTOCOL:
35445 return cp_parser_objc_protocol_expression (parser);
35447 case RID_AT_SELECTOR:
35448 return cp_parser_objc_selector_expression (parser);
35450 default:
35451 break;
35453 /* FALLTHRU */
35454 default:
35455 error_at (kwd->location,
35456 "misplaced %<@%D%> Objective-C++ construct",
35457 kwd->u.value);
35458 cp_parser_skip_to_end_of_block_or_statement (parser);
35461 return error_mark_node;
35464 /* Parse an Objective-C message expression.
35466 objc-message-expression:
35467 [ objc-message-receiver objc-message-args ]
35469 Returns a representation of an Objective-C message. */
35471 static tree
35472 cp_parser_objc_message_expression (cp_parser* parser)
35474 tree receiver, messageargs;
35476 parser->objective_c_message_context_p = true;
35477 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35478 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35479 receiver = cp_parser_objc_message_receiver (parser);
35480 messageargs = cp_parser_objc_message_args (parser);
35481 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35484 tree result = objc_build_message_expr (receiver, messageargs);
35486 /* Construct a location e.g.
35487 [self func1:5]
35488 ^~~~~~~~~~~~~~
35489 ranging from the '[' to the ']', with the caret at the start. */
35490 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35491 protected_set_expr_location (result, combined_loc);
35493 parser->objective_c_message_context_p = false;
35494 return result;
35497 /* Parse an objc-message-receiver.
35499 objc-message-receiver:
35500 expression
35501 simple-type-specifier
35503 Returns a representation of the type or expression. */
35505 static tree
35506 cp_parser_objc_message_receiver (cp_parser* parser)
35508 tree rcv;
35510 /* An Objective-C message receiver may be either (1) a type
35511 or (2) an expression. */
35512 cp_parser_parse_tentatively (parser);
35513 rcv = cp_parser_expression (parser);
35515 /* If that worked out, fine. */
35516 if (cp_parser_parse_definitely (parser))
35517 return rcv;
35519 cp_parser_parse_tentatively (parser);
35520 rcv = cp_parser_simple_type_specifier (parser,
35521 /*decl_specs=*/NULL,
35522 CP_PARSER_FLAGS_NONE);
35524 if (cp_parser_parse_definitely (parser))
35525 return objc_get_class_reference (rcv);
35527 cp_parser_error (parser, "objective-c++ message receiver expected");
35528 return error_mark_node;
35531 /* Parse the arguments and selectors comprising an Objective-C message.
35533 objc-message-args:
35534 objc-selector
35535 objc-selector-args
35536 objc-selector-args , objc-comma-args
35538 objc-selector-args:
35539 objc-selector [opt] : assignment-expression
35540 objc-selector-args objc-selector [opt] : assignment-expression
35542 objc-comma-args:
35543 assignment-expression
35544 objc-comma-args , assignment-expression
35546 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35547 selector arguments and TREE_VALUE containing a list of comma
35548 arguments. */
35550 static tree
35551 cp_parser_objc_message_args (cp_parser* parser)
35553 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35554 bool maybe_unary_selector_p = true;
35555 cp_token *token = cp_lexer_peek_token (parser->lexer);
35557 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35559 tree selector = NULL_TREE, arg;
35561 if (token->type != CPP_COLON)
35562 selector = cp_parser_objc_selector (parser);
35564 /* Detect if we have a unary selector. */
35565 if (maybe_unary_selector_p
35566 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35567 return build_tree_list (selector, NULL_TREE);
35569 maybe_unary_selector_p = false;
35570 cp_parser_require (parser, CPP_COLON, RT_COLON);
35571 arg = cp_parser_assignment_expression (parser);
35573 sel_args
35574 = chainon (sel_args,
35575 build_tree_list (selector, arg));
35577 token = cp_lexer_peek_token (parser->lexer);
35580 /* Handle non-selector arguments, if any. */
35581 while (token->type == CPP_COMMA)
35583 tree arg;
35585 cp_lexer_consume_token (parser->lexer);
35586 arg = cp_parser_assignment_expression (parser);
35588 addl_args
35589 = chainon (addl_args,
35590 build_tree_list (NULL_TREE, arg));
35592 token = cp_lexer_peek_token (parser->lexer);
35595 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35597 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35598 return build_tree_list (error_mark_node, error_mark_node);
35601 return build_tree_list (sel_args, addl_args);
35604 /* Parse an Objective-C encode expression.
35606 objc-encode-expression:
35607 @encode objc-typename
35609 Returns an encoded representation of the type argument. */
35611 static cp_expr
35612 cp_parser_objc_encode_expression (cp_parser* parser)
35614 tree type;
35615 cp_token *token;
35616 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35618 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35619 matching_parens parens;
35620 parens.require_open (parser);
35621 token = cp_lexer_peek_token (parser->lexer);
35622 type = complete_type (cp_parser_type_id (parser));
35623 parens.require_close (parser);
35625 if (!type)
35627 error_at (token->location,
35628 "%<@encode%> must specify a type as an argument");
35629 return error_mark_node;
35632 /* This happens if we find @encode(T) (where T is a template
35633 typename or something dependent on a template typename) when
35634 parsing a template. In that case, we can't compile it
35635 immediately, but we rather create an AT_ENCODE_EXPR which will
35636 need to be instantiated when the template is used.
35638 if (dependent_type_p (type))
35640 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35641 TREE_READONLY (value) = 1;
35642 return value;
35646 /* Build a location of the form:
35647 @encode(int)
35648 ^~~~~~~~~~~~
35649 with caret==start at the @ token, finishing at the close paren. */
35650 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35652 return cp_expr (objc_build_encode_expr (type), combined_loc);
35655 /* Parse an Objective-C @defs expression. */
35657 static tree
35658 cp_parser_objc_defs_expression (cp_parser *parser)
35660 tree name;
35662 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35663 matching_parens parens;
35664 parens.require_open (parser);
35665 name = cp_parser_identifier (parser);
35666 parens.require_close (parser);
35668 return objc_get_class_ivars (name);
35671 /* Parse an Objective-C protocol expression.
35673 objc-protocol-expression:
35674 @protocol ( identifier )
35676 Returns a representation of the protocol expression. */
35678 static tree
35679 cp_parser_objc_protocol_expression (cp_parser* parser)
35681 tree proto;
35682 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35684 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35685 matching_parens parens;
35686 parens.require_open (parser);
35687 proto = cp_parser_identifier (parser);
35688 parens.require_close (parser);
35690 /* Build a location of the form:
35691 @protocol(prot)
35692 ^~~~~~~~~~~~~~~
35693 with caret==start at the @ token, finishing at the close paren. */
35694 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35695 tree result = objc_build_protocol_expr (proto);
35696 protected_set_expr_location (result, combined_loc);
35697 return result;
35700 /* Parse an Objective-C selector expression.
35702 objc-selector-expression:
35703 @selector ( objc-method-signature )
35705 objc-method-signature:
35706 objc-selector
35707 objc-selector-seq
35709 objc-selector-seq:
35710 objc-selector :
35711 objc-selector-seq objc-selector :
35713 Returns a representation of the method selector. */
35715 static tree
35716 cp_parser_objc_selector_expression (cp_parser* parser)
35718 tree sel_seq = NULL_TREE;
35719 bool maybe_unary_selector_p = true;
35720 cp_token *token;
35721 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35723 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35724 matching_parens parens;
35725 parens.require_open (parser);
35726 token = cp_lexer_peek_token (parser->lexer);
35728 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35729 || token->type == CPP_SCOPE)
35731 tree selector = NULL_TREE;
35733 if (token->type != CPP_COLON
35734 || token->type == CPP_SCOPE)
35735 selector = cp_parser_objc_selector (parser);
35737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
35738 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
35740 /* Detect if we have a unary selector. */
35741 if (maybe_unary_selector_p)
35743 sel_seq = selector;
35744 goto finish_selector;
35746 else
35748 cp_parser_error (parser, "expected %<:%>");
35751 maybe_unary_selector_p = false;
35752 token = cp_lexer_consume_token (parser->lexer);
35754 if (token->type == CPP_SCOPE)
35756 sel_seq
35757 = chainon (sel_seq,
35758 build_tree_list (selector, NULL_TREE));
35759 sel_seq
35760 = chainon (sel_seq,
35761 build_tree_list (NULL_TREE, NULL_TREE));
35763 else
35764 sel_seq
35765 = chainon (sel_seq,
35766 build_tree_list (selector, NULL_TREE));
35768 token = cp_lexer_peek_token (parser->lexer);
35771 finish_selector:
35772 parens.require_close (parser);
35775 /* Build a location of the form:
35776 @selector(func)
35777 ^~~~~~~~~~~~~~~
35778 with caret==start at the @ token, finishing at the close paren. */
35779 location_t combined_loc = make_location (loc, loc, parser->lexer);
35780 tree result = objc_build_selector_expr (combined_loc, sel_seq);
35781 /* TODO: objc_build_selector_expr doesn't always honor the location. */
35782 protected_set_expr_location (result, combined_loc);
35783 return result;
35786 /* Parse a list of identifiers.
35788 objc-identifier-list:
35789 identifier
35790 objc-identifier-list , identifier
35792 Returns a TREE_LIST of identifier nodes. */
35794 static tree
35795 cp_parser_objc_identifier_list (cp_parser* parser)
35797 tree identifier;
35798 tree list;
35799 cp_token *sep;
35801 identifier = cp_parser_identifier (parser);
35802 if (identifier == error_mark_node)
35803 return error_mark_node;
35805 list = build_tree_list (NULL_TREE, identifier);
35806 sep = cp_lexer_peek_token (parser->lexer);
35808 while (sep->type == CPP_COMMA)
35810 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35811 identifier = cp_parser_identifier (parser);
35812 if (identifier == error_mark_node)
35813 return list;
35815 list = chainon (list, build_tree_list (NULL_TREE,
35816 identifier));
35817 sep = cp_lexer_peek_token (parser->lexer);
35820 return list;
35823 /* Parse an Objective-C alias declaration.
35825 objc-alias-declaration:
35826 @compatibility_alias identifier identifier ;
35828 This function registers the alias mapping with the Objective-C front end.
35829 It returns nothing. */
35831 static void
35832 cp_parser_objc_alias_declaration (cp_parser* parser)
35834 tree alias, orig;
35836 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
35837 alias = cp_parser_identifier (parser);
35838 orig = cp_parser_identifier (parser);
35839 objc_declare_alias (alias, orig);
35840 cp_parser_consume_semicolon_at_end_of_statement (parser);
35843 /* Parse an Objective-C class forward-declaration.
35845 objc-class-declaration:
35846 @class objc-identifier-list ;
35848 The function registers the forward declarations with the Objective-C
35849 front end. It returns nothing. */
35851 static void
35852 cp_parser_objc_class_declaration (cp_parser* parser)
35854 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
35855 while (true)
35857 tree id;
35859 id = cp_parser_identifier (parser);
35860 if (id == error_mark_node)
35861 break;
35863 objc_declare_class (id);
35865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35866 cp_lexer_consume_token (parser->lexer);
35867 else
35868 break;
35870 cp_parser_consume_semicolon_at_end_of_statement (parser);
35873 /* Parse a list of Objective-C protocol references.
35875 objc-protocol-refs-opt:
35876 objc-protocol-refs [opt]
35878 objc-protocol-refs:
35879 < objc-identifier-list >
35881 Returns a TREE_LIST of identifiers, if any. */
35883 static tree
35884 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
35886 tree protorefs = NULL_TREE;
35888 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
35890 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
35891 protorefs = cp_parser_objc_identifier_list (parser);
35892 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
35895 return protorefs;
35898 /* Parse a Objective-C visibility specification. */
35900 static void
35901 cp_parser_objc_visibility_spec (cp_parser* parser)
35903 cp_token *vis = cp_lexer_peek_token (parser->lexer);
35905 switch (vis->keyword)
35907 case RID_AT_PRIVATE:
35908 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
35909 break;
35910 case RID_AT_PROTECTED:
35911 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
35912 break;
35913 case RID_AT_PUBLIC:
35914 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
35915 break;
35916 case RID_AT_PACKAGE:
35917 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
35918 break;
35919 default:
35920 return;
35923 /* Eat '@private'/'@protected'/'@public'. */
35924 cp_lexer_consume_token (parser->lexer);
35927 /* Parse an Objective-C method type. Return 'true' if it is a class
35928 (+) method, and 'false' if it is an instance (-) method. */
35930 static inline bool
35931 cp_parser_objc_method_type (cp_parser* parser)
35933 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
35934 return true;
35935 else
35936 return false;
35939 /* Parse an Objective-C protocol qualifier. */
35941 static tree
35942 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35944 tree quals = NULL_TREE, node;
35945 cp_token *token = cp_lexer_peek_token (parser->lexer);
35947 node = token->u.value;
35949 while (node && identifier_p (node)
35950 && (node == ridpointers [(int) RID_IN]
35951 || node == ridpointers [(int) RID_OUT]
35952 || node == ridpointers [(int) RID_INOUT]
35953 || node == ridpointers [(int) RID_BYCOPY]
35954 || node == ridpointers [(int) RID_BYREF]
35955 || node == ridpointers [(int) RID_ONEWAY]))
35957 quals = tree_cons (NULL_TREE, node, quals);
35958 cp_lexer_consume_token (parser->lexer);
35959 token = cp_lexer_peek_token (parser->lexer);
35960 node = token->u.value;
35963 return quals;
35966 /* Parse an Objective-C typename. */
35968 static tree
35969 cp_parser_objc_typename (cp_parser* parser)
35971 tree type_name = NULL_TREE;
35973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35975 tree proto_quals, cp_type = NULL_TREE;
35977 matching_parens parens;
35978 parens.consume_open (parser); /* Eat '('. */
35979 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35981 /* An ObjC type name may consist of just protocol qualifiers, in which
35982 case the type shall default to 'id'. */
35983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35985 cp_type = cp_parser_type_id (parser);
35987 /* If the type could not be parsed, an error has already
35988 been produced. For error recovery, behave as if it had
35989 not been specified, which will use the default type
35990 'id'. */
35991 if (cp_type == error_mark_node)
35993 cp_type = NULL_TREE;
35994 /* We need to skip to the closing parenthesis as
35995 cp_parser_type_id() does not seem to do it for
35996 us. */
35997 cp_parser_skip_to_closing_parenthesis (parser,
35998 /*recovering=*/true,
35999 /*or_comma=*/false,
36000 /*consume_paren=*/false);
36004 parens.require_close (parser);
36005 type_name = build_tree_list (proto_quals, cp_type);
36008 return type_name;
36011 /* Check to see if TYPE refers to an Objective-C selector name. */
36013 static bool
36014 cp_parser_objc_selector_p (enum cpp_ttype type)
36016 return (type == CPP_NAME || type == CPP_KEYWORD
36017 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36018 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36019 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36020 || type == CPP_XOR || type == CPP_XOR_EQ);
36023 /* Parse an Objective-C selector. */
36025 static tree
36026 cp_parser_objc_selector (cp_parser* parser)
36028 cp_token *token = cp_lexer_consume_token (parser->lexer);
36030 if (!cp_parser_objc_selector_p (token->type))
36032 error_at (token->location, "invalid Objective-C++ selector name");
36033 return error_mark_node;
36036 /* C++ operator names are allowed to appear in ObjC selectors. */
36037 switch (token->type)
36039 case CPP_AND_AND: return get_identifier ("and");
36040 case CPP_AND_EQ: return get_identifier ("and_eq");
36041 case CPP_AND: return get_identifier ("bitand");
36042 case CPP_OR: return get_identifier ("bitor");
36043 case CPP_COMPL: return get_identifier ("compl");
36044 case CPP_NOT: return get_identifier ("not");
36045 case CPP_NOT_EQ: return get_identifier ("not_eq");
36046 case CPP_OR_OR: return get_identifier ("or");
36047 case CPP_OR_EQ: return get_identifier ("or_eq");
36048 case CPP_XOR: return get_identifier ("xor");
36049 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36050 default: return token->u.value;
36054 /* Parse an Objective-C params list. */
36056 static tree
36057 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36059 tree params = NULL_TREE;
36060 bool maybe_unary_selector_p = true;
36061 cp_token *token = cp_lexer_peek_token (parser->lexer);
36063 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36065 tree selector = NULL_TREE, type_name, identifier;
36066 tree parm_attr = NULL_TREE;
36068 if (token->keyword == RID_ATTRIBUTE)
36069 break;
36071 if (token->type != CPP_COLON)
36072 selector = cp_parser_objc_selector (parser);
36074 /* Detect if we have a unary selector. */
36075 if (maybe_unary_selector_p
36076 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36078 params = selector; /* Might be followed by attributes. */
36079 break;
36082 maybe_unary_selector_p = false;
36083 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36085 /* Something went quite wrong. There should be a colon
36086 here, but there is not. Stop parsing parameters. */
36087 break;
36089 type_name = cp_parser_objc_typename (parser);
36090 /* New ObjC allows attributes on parameters too. */
36091 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36092 parm_attr = cp_parser_attributes_opt (parser);
36093 identifier = cp_parser_identifier (parser);
36095 params
36096 = chainon (params,
36097 objc_build_keyword_decl (selector,
36098 type_name,
36099 identifier,
36100 parm_attr));
36102 token = cp_lexer_peek_token (parser->lexer);
36105 if (params == NULL_TREE)
36107 cp_parser_error (parser, "objective-c++ method declaration is expected");
36108 return error_mark_node;
36111 /* We allow tail attributes for the method. */
36112 if (token->keyword == RID_ATTRIBUTE)
36114 *attributes = cp_parser_attributes_opt (parser);
36115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36116 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36117 return params;
36118 cp_parser_error (parser,
36119 "method attributes must be specified at the end");
36120 return error_mark_node;
36123 if (params == NULL_TREE)
36125 cp_parser_error (parser, "objective-c++ method declaration is expected");
36126 return error_mark_node;
36128 return params;
36131 /* Parse the non-keyword Objective-C params. */
36133 static tree
36134 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36135 tree* attributes)
36137 tree params = make_node (TREE_LIST);
36138 cp_token *token = cp_lexer_peek_token (parser->lexer);
36139 *ellipsisp = false; /* Initially, assume no ellipsis. */
36141 while (token->type == CPP_COMMA)
36143 cp_parameter_declarator *parmdecl;
36144 tree parm;
36146 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36147 token = cp_lexer_peek_token (parser->lexer);
36149 if (token->type == CPP_ELLIPSIS)
36151 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36152 *ellipsisp = true;
36153 token = cp_lexer_peek_token (parser->lexer);
36154 break;
36157 /* TODO: parse attributes for tail parameters. */
36158 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36159 false, NULL);
36160 parm = grokdeclarator (parmdecl->declarator,
36161 &parmdecl->decl_specifiers,
36162 PARM, /*initialized=*/0,
36163 /*attrlist=*/NULL);
36165 chainon (params, build_tree_list (NULL_TREE, parm));
36166 token = cp_lexer_peek_token (parser->lexer);
36169 /* We allow tail attributes for the method. */
36170 if (token->keyword == RID_ATTRIBUTE)
36172 if (*attributes == NULL_TREE)
36174 *attributes = cp_parser_attributes_opt (parser);
36175 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36176 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36177 return params;
36179 else
36180 /* We have an error, but parse the attributes, so that we can
36181 carry on. */
36182 *attributes = cp_parser_attributes_opt (parser);
36184 cp_parser_error (parser,
36185 "method attributes must be specified at the end");
36186 return error_mark_node;
36189 return params;
36192 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36194 static void
36195 cp_parser_objc_interstitial_code (cp_parser* parser)
36197 cp_token *token = cp_lexer_peek_token (parser->lexer);
36199 /* If the next token is `extern' and the following token is a string
36200 literal, then we have a linkage specification. */
36201 if (token->keyword == RID_EXTERN
36202 && cp_parser_is_pure_string_literal
36203 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36204 cp_parser_linkage_specification (parser, NULL_TREE);
36205 /* Handle #pragma, if any. */
36206 else if (token->type == CPP_PRAGMA)
36207 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36208 /* Allow stray semicolons. */
36209 else if (token->type == CPP_SEMICOLON)
36210 cp_lexer_consume_token (parser->lexer);
36211 /* Mark methods as optional or required, when building protocols. */
36212 else if (token->keyword == RID_AT_OPTIONAL)
36214 cp_lexer_consume_token (parser->lexer);
36215 objc_set_method_opt (true);
36217 else if (token->keyword == RID_AT_REQUIRED)
36219 cp_lexer_consume_token (parser->lexer);
36220 objc_set_method_opt (false);
36222 else if (token->keyword == RID_NAMESPACE)
36223 cp_parser_namespace_definition (parser);
36224 /* Other stray characters must generate errors. */
36225 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36227 cp_lexer_consume_token (parser->lexer);
36228 error ("stray %qs between Objective-C++ methods",
36229 token->type == CPP_OPEN_BRACE ? "{" : "}");
36231 /* Finally, try to parse a block-declaration, or a function-definition. */
36232 else
36233 cp_parser_block_declaration (parser, /*statement_p=*/false);
36236 /* Parse a method signature. */
36238 static tree
36239 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36241 tree rettype, kwdparms, optparms;
36242 bool ellipsis = false;
36243 bool is_class_method;
36245 is_class_method = cp_parser_objc_method_type (parser);
36246 rettype = cp_parser_objc_typename (parser);
36247 *attributes = NULL_TREE;
36248 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36249 if (kwdparms == error_mark_node)
36250 return error_mark_node;
36251 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36252 if (optparms == error_mark_node)
36253 return error_mark_node;
36255 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36258 static bool
36259 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36261 tree tattr;
36262 cp_lexer_save_tokens (parser->lexer);
36263 tattr = cp_parser_attributes_opt (parser);
36264 gcc_assert (tattr) ;
36266 /* If the attributes are followed by a method introducer, this is not allowed.
36267 Dump the attributes and flag the situation. */
36268 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36269 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36270 return true;
36272 /* Otherwise, the attributes introduce some interstitial code, possibly so
36273 rewind to allow that check. */
36274 cp_lexer_rollback_tokens (parser->lexer);
36275 return false;
36278 /* Parse an Objective-C method prototype list. */
36280 static void
36281 cp_parser_objc_method_prototype_list (cp_parser* parser)
36283 cp_token *token = cp_lexer_peek_token (parser->lexer);
36285 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36287 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36289 tree attributes, sig;
36290 bool is_class_method;
36291 if (token->type == CPP_PLUS)
36292 is_class_method = true;
36293 else
36294 is_class_method = false;
36295 sig = cp_parser_objc_method_signature (parser, &attributes);
36296 if (sig == error_mark_node)
36298 cp_parser_skip_to_end_of_block_or_statement (parser);
36299 token = cp_lexer_peek_token (parser->lexer);
36300 continue;
36302 objc_add_method_declaration (is_class_method, sig, attributes);
36303 cp_parser_consume_semicolon_at_end_of_statement (parser);
36305 else if (token->keyword == RID_AT_PROPERTY)
36306 cp_parser_objc_at_property_declaration (parser);
36307 else if (token->keyword == RID_ATTRIBUTE
36308 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36309 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36310 OPT_Wattributes,
36311 "prefix attributes are ignored for methods");
36312 else
36313 /* Allow for interspersed non-ObjC++ code. */
36314 cp_parser_objc_interstitial_code (parser);
36316 token = cp_lexer_peek_token (parser->lexer);
36319 if (token->type != CPP_EOF)
36320 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36321 else
36322 cp_parser_error (parser, "expected %<@end%>");
36324 objc_finish_interface ();
36327 /* Parse an Objective-C method definition list. */
36329 static void
36330 cp_parser_objc_method_definition_list (cp_parser* parser)
36332 for (;;)
36334 cp_token *token = cp_lexer_peek_token (parser->lexer);
36336 if (token->keyword == RID_AT_END)
36338 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36339 break;
36341 else if (token->type == CPP_EOF)
36343 cp_parser_error (parser, "expected %<@end%>");
36344 break;
36346 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36348 bool is_class_method = token->type == CPP_PLUS;
36350 push_deferring_access_checks (dk_deferred);
36351 tree attribute;
36352 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36353 if (sig == error_mark_node)
36354 cp_parser_skip_to_end_of_block_or_statement (parser);
36355 else
36357 objc_start_method_definition (is_class_method, sig,
36358 attribute, NULL_TREE);
36360 /* For historical reasons, we accept an optional semicolon. */
36361 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36362 cp_lexer_consume_token (parser->lexer);
36364 perform_deferred_access_checks (tf_warning_or_error);
36365 stop_deferring_access_checks ();
36366 tree meth
36367 = cp_parser_function_definition_after_declarator (parser, false);
36368 pop_deferring_access_checks ();
36369 objc_finish_method_definition (meth);
36372 /* The following case will be removed once @synthesize is
36373 completely implemented. */
36374 else if (token->keyword == RID_AT_PROPERTY)
36375 cp_parser_objc_at_property_declaration (parser);
36376 else if (token->keyword == RID_AT_SYNTHESIZE)
36377 cp_parser_objc_at_synthesize_declaration (parser);
36378 else if (token->keyword == RID_AT_DYNAMIC)
36379 cp_parser_objc_at_dynamic_declaration (parser);
36380 else if (token->keyword == RID_ATTRIBUTE
36381 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36382 warning_at (token->location, OPT_Wattributes,
36383 "prefix attributes are ignored for methods");
36384 else
36385 /* Allow for interspersed non-ObjC++ code. */
36386 cp_parser_objc_interstitial_code (parser);
36389 objc_finish_implementation ();
36392 /* Parse Objective-C ivars. */
36394 static void
36395 cp_parser_objc_class_ivars (cp_parser* parser)
36397 cp_token *token = cp_lexer_peek_token (parser->lexer);
36399 if (token->type != CPP_OPEN_BRACE)
36400 return; /* No ivars specified. */
36402 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36403 token = cp_lexer_peek_token (parser->lexer);
36405 while (token->type != CPP_CLOSE_BRACE
36406 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36408 cp_decl_specifier_seq declspecs;
36409 int decl_class_or_enum_p;
36410 tree prefix_attributes;
36412 cp_parser_objc_visibility_spec (parser);
36414 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36415 break;
36417 cp_parser_decl_specifier_seq (parser,
36418 CP_PARSER_FLAGS_OPTIONAL,
36419 &declspecs,
36420 &decl_class_or_enum_p);
36422 /* auto, register, static, extern, mutable. */
36423 if (declspecs.storage_class != sc_none)
36425 cp_parser_error (parser, "invalid type for instance variable");
36426 declspecs.storage_class = sc_none;
36429 /* thread_local. */
36430 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36432 cp_parser_error (parser, "invalid type for instance variable");
36433 declspecs.locations[ds_thread] = 0;
36436 /* typedef. */
36437 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36439 cp_parser_error (parser, "invalid type for instance variable");
36440 declspecs.locations[ds_typedef] = 0;
36443 prefix_attributes = declspecs.attributes;
36444 declspecs.attributes = NULL_TREE;
36446 /* Keep going until we hit the `;' at the end of the
36447 declaration. */
36448 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36450 tree width = NULL_TREE, attributes, first_attribute, decl;
36451 cp_declarator *declarator = NULL;
36452 int ctor_dtor_or_conv_p;
36454 /* Check for a (possibly unnamed) bitfield declaration. */
36455 token = cp_lexer_peek_token (parser->lexer);
36456 if (token->type == CPP_COLON)
36457 goto eat_colon;
36459 if (token->type == CPP_NAME
36460 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36461 == CPP_COLON))
36463 /* Get the name of the bitfield. */
36464 declarator = make_id_declarator (NULL_TREE,
36465 cp_parser_identifier (parser),
36466 sfk_none, token->location);
36468 eat_colon:
36469 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36470 /* Get the width of the bitfield. */
36471 width
36472 = cp_parser_constant_expression (parser);
36474 else
36476 /* Parse the declarator. */
36477 declarator
36478 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36479 CP_PARSER_FLAGS_NONE,
36480 &ctor_dtor_or_conv_p,
36481 /*parenthesized_p=*/NULL,
36482 /*member_p=*/false,
36483 /*friend_p=*/false,
36484 /*static_p=*/false);
36487 /* Look for attributes that apply to the ivar. */
36488 attributes = cp_parser_attributes_opt (parser);
36489 /* Remember which attributes are prefix attributes and
36490 which are not. */
36491 first_attribute = attributes;
36492 /* Combine the attributes. */
36493 attributes = attr_chainon (prefix_attributes, attributes);
36495 if (width)
36496 /* Create the bitfield declaration. */
36497 decl = grokbitfield (declarator, &declspecs,
36498 width, NULL_TREE, attributes);
36499 else
36500 decl = grokfield (declarator, &declspecs,
36501 NULL_TREE, /*init_const_expr_p=*/false,
36502 NULL_TREE, attributes);
36504 /* Add the instance variable. */
36505 if (decl != error_mark_node && decl != NULL_TREE)
36506 objc_add_instance_variable (decl);
36508 /* Reset PREFIX_ATTRIBUTES. */
36509 if (attributes != error_mark_node)
36511 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36512 attributes = TREE_CHAIN (attributes);
36513 if (attributes)
36514 TREE_CHAIN (attributes) = NULL_TREE;
36517 token = cp_lexer_peek_token (parser->lexer);
36519 if (token->type == CPP_COMMA)
36521 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36522 continue;
36524 break;
36527 cp_parser_consume_semicolon_at_end_of_statement (parser);
36528 token = cp_lexer_peek_token (parser->lexer);
36531 if (token->keyword == RID_AT_END)
36532 cp_parser_error (parser, "expected %<}%>");
36534 /* Do not consume the RID_AT_END, so it will be read again as terminating
36535 the @interface of @implementation. */
36536 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36537 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36539 /* For historical reasons, we accept an optional semicolon. */
36540 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36541 cp_lexer_consume_token (parser->lexer);
36544 /* Parse an Objective-C protocol declaration. */
36546 static void
36547 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36549 tree proto, protorefs;
36550 cp_token *tok;
36552 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36555 tok = cp_lexer_peek_token (parser->lexer);
36556 error_at (tok->location, "identifier expected after %<@protocol%>");
36557 cp_parser_consume_semicolon_at_end_of_statement (parser);
36558 return;
36561 /* See if we have a forward declaration or a definition. */
36562 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36564 /* Try a forward declaration first. */
36565 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36567 while (true)
36569 tree id;
36571 id = cp_parser_identifier (parser);
36572 if (id == error_mark_node)
36573 break;
36575 objc_declare_protocol (id, attributes);
36577 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36578 cp_lexer_consume_token (parser->lexer);
36579 else
36580 break;
36582 cp_parser_consume_semicolon_at_end_of_statement (parser);
36585 /* Ok, we got a full-fledged definition (or at least should). */
36586 else
36588 proto = cp_parser_identifier (parser);
36589 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36590 objc_start_protocol (proto, protorefs, attributes);
36591 cp_parser_objc_method_prototype_list (parser);
36595 /* Parse an Objective-C superclass or category. */
36597 static void
36598 cp_parser_objc_superclass_or_category (cp_parser *parser,
36599 bool iface_p,
36600 tree *super,
36601 tree *categ, bool *is_class_extension)
36603 cp_token *next = cp_lexer_peek_token (parser->lexer);
36605 *super = *categ = NULL_TREE;
36606 *is_class_extension = false;
36607 if (next->type == CPP_COLON)
36609 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36610 *super = cp_parser_identifier (parser);
36612 else if (next->type == CPP_OPEN_PAREN)
36614 matching_parens parens;
36615 parens.consume_open (parser); /* Eat '('. */
36617 /* If there is no category name, and this is an @interface, we
36618 have a class extension. */
36619 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36621 *categ = NULL_TREE;
36622 *is_class_extension = true;
36624 else
36625 *categ = cp_parser_identifier (parser);
36627 parens.require_close (parser);
36631 /* Parse an Objective-C class interface. */
36633 static void
36634 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36636 tree name, super, categ, protos;
36637 bool is_class_extension;
36639 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36640 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36641 name = cp_parser_identifier (parser);
36642 if (name == error_mark_node)
36644 /* It's hard to recover because even if valid @interface stuff
36645 is to follow, we can't compile it (or validate it) if we
36646 don't even know which class it refers to. Let's assume this
36647 was a stray '@interface' token in the stream and skip it.
36649 return;
36651 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36652 &is_class_extension);
36653 protos = cp_parser_objc_protocol_refs_opt (parser);
36655 /* We have either a class or a category on our hands. */
36656 if (categ || is_class_extension)
36657 objc_start_category_interface (name, categ, protos, attributes);
36658 else
36660 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36661 /* Handle instance variable declarations, if any. */
36662 cp_parser_objc_class_ivars (parser);
36663 objc_continue_interface ();
36666 cp_parser_objc_method_prototype_list (parser);
36669 /* Parse an Objective-C class implementation. */
36671 static void
36672 cp_parser_objc_class_implementation (cp_parser* parser)
36674 tree name, super, categ;
36675 bool is_class_extension;
36677 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36678 name = cp_parser_identifier (parser);
36679 if (name == error_mark_node)
36681 /* It's hard to recover because even if valid @implementation
36682 stuff is to follow, we can't compile it (or validate it) if
36683 we don't even know which class it refers to. Let's assume
36684 this was a stray '@implementation' token in the stream and
36685 skip it.
36687 return;
36689 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36690 &is_class_extension);
36692 /* We have either a class or a category on our hands. */
36693 if (categ)
36694 objc_start_category_implementation (name, categ);
36695 else
36697 objc_start_class_implementation (name, super);
36698 /* Handle instance variable declarations, if any. */
36699 cp_parser_objc_class_ivars (parser);
36700 objc_continue_implementation ();
36703 cp_parser_objc_method_definition_list (parser);
36706 /* Consume the @end token and finish off the implementation. */
36708 static void
36709 cp_parser_objc_end_implementation (cp_parser* parser)
36711 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36712 objc_finish_implementation ();
36715 /* Parse an Objective-C declaration. */
36717 static void
36718 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36720 /* Try to figure out what kind of declaration is present. */
36721 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36723 if (attributes)
36724 switch (kwd->keyword)
36726 case RID_AT_ALIAS:
36727 case RID_AT_CLASS:
36728 case RID_AT_END:
36729 error_at (kwd->location, "attributes may not be specified before"
36730 " the %<@%D%> Objective-C++ keyword",
36731 kwd->u.value);
36732 attributes = NULL;
36733 break;
36734 case RID_AT_IMPLEMENTATION:
36735 warning_at (kwd->location, OPT_Wattributes,
36736 "prefix attributes are ignored before %<@%D%>",
36737 kwd->u.value);
36738 attributes = NULL;
36739 default:
36740 break;
36743 switch (kwd->keyword)
36745 case RID_AT_ALIAS:
36746 cp_parser_objc_alias_declaration (parser);
36747 break;
36748 case RID_AT_CLASS:
36749 cp_parser_objc_class_declaration (parser);
36750 break;
36751 case RID_AT_PROTOCOL:
36752 cp_parser_objc_protocol_declaration (parser, attributes);
36753 break;
36754 case RID_AT_INTERFACE:
36755 cp_parser_objc_class_interface (parser, attributes);
36756 break;
36757 case RID_AT_IMPLEMENTATION:
36758 cp_parser_objc_class_implementation (parser);
36759 break;
36760 case RID_AT_END:
36761 cp_parser_objc_end_implementation (parser);
36762 break;
36763 default:
36764 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36765 kwd->u.value);
36766 cp_parser_skip_to_end_of_block_or_statement (parser);
36770 /* Parse an Objective-C try-catch-finally statement.
36772 objc-try-catch-finally-stmt:
36773 @try compound-statement objc-catch-clause-seq [opt]
36774 objc-finally-clause [opt]
36776 objc-catch-clause-seq:
36777 objc-catch-clause objc-catch-clause-seq [opt]
36779 objc-catch-clause:
36780 @catch ( objc-exception-declaration ) compound-statement
36782 objc-finally-clause:
36783 @finally compound-statement
36785 objc-exception-declaration:
36786 parameter-declaration
36787 '...'
36789 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
36791 Returns NULL_TREE.
36793 PS: This function is identical to c_parser_objc_try_catch_finally_statement
36794 for C. Keep them in sync. */
36796 static tree
36797 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
36799 location_t location;
36800 tree stmt;
36802 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
36803 location = cp_lexer_peek_token (parser->lexer)->location;
36804 objc_maybe_warn_exceptions (location);
36805 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
36806 node, lest it get absorbed into the surrounding block. */
36807 stmt = push_stmt_list ();
36808 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36809 objc_begin_try_stmt (location, pop_stmt_list (stmt));
36811 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
36813 cp_parameter_declarator *parm;
36814 tree parameter_declaration = error_mark_node;
36815 bool seen_open_paren = false;
36816 matching_parens parens;
36818 cp_lexer_consume_token (parser->lexer);
36819 if (parens.require_open (parser))
36820 seen_open_paren = true;
36821 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
36823 /* We have "@catch (...)" (where the '...' are literally
36824 what is in the code). Skip the '...'.
36825 parameter_declaration is set to NULL_TREE, and
36826 objc_being_catch_clauses() knows that that means
36827 '...'. */
36828 cp_lexer_consume_token (parser->lexer);
36829 parameter_declaration = NULL_TREE;
36831 else
36833 /* We have "@catch (NSException *exception)" or something
36834 like that. Parse the parameter declaration. */
36835 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36836 false, NULL);
36837 if (parm == NULL)
36838 parameter_declaration = error_mark_node;
36839 else
36840 parameter_declaration = grokdeclarator (parm->declarator,
36841 &parm->decl_specifiers,
36842 PARM, /*initialized=*/0,
36843 /*attrlist=*/NULL);
36845 if (seen_open_paren)
36846 parens.require_close (parser);
36847 else
36849 /* If there was no open parenthesis, we are recovering from
36850 an error, and we are trying to figure out what mistake
36851 the user has made. */
36853 /* If there is an immediate closing parenthesis, the user
36854 probably forgot the opening one (ie, they typed "@catch
36855 NSException *e)". Parse the closing parenthesis and keep
36856 going. */
36857 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36858 cp_lexer_consume_token (parser->lexer);
36860 /* If these is no immediate closing parenthesis, the user
36861 probably doesn't know that parenthesis are required at
36862 all (ie, they typed "@catch NSException *e"). So, just
36863 forget about the closing parenthesis and keep going. */
36865 objc_begin_catch_clause (parameter_declaration);
36866 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36867 objc_finish_catch_clause ();
36869 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
36871 cp_lexer_consume_token (parser->lexer);
36872 location = cp_lexer_peek_token (parser->lexer)->location;
36873 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36874 node, lest it get absorbed into the surrounding block. */
36875 stmt = push_stmt_list ();
36876 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36877 objc_build_finally_clause (location, pop_stmt_list (stmt));
36880 return objc_finish_try_stmt ();
36883 /* Parse an Objective-C synchronized statement.
36885 objc-synchronized-stmt:
36886 @synchronized ( expression ) compound-statement
36888 Returns NULL_TREE. */
36890 static tree
36891 cp_parser_objc_synchronized_statement (cp_parser *parser)
36893 location_t location;
36894 tree lock, stmt;
36896 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
36898 location = cp_lexer_peek_token (parser->lexer)->location;
36899 objc_maybe_warn_exceptions (location);
36900 matching_parens parens;
36901 parens.require_open (parser);
36902 lock = cp_parser_expression (parser);
36903 parens.require_close (parser);
36905 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36906 node, lest it get absorbed into the surrounding block. */
36907 stmt = push_stmt_list ();
36908 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36910 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
36913 /* Parse an Objective-C throw statement.
36915 objc-throw-stmt:
36916 @throw assignment-expression [opt] ;
36918 Returns a constructed '@throw' statement. */
36920 static tree
36921 cp_parser_objc_throw_statement (cp_parser *parser)
36923 tree expr = NULL_TREE;
36924 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36926 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
36928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36929 expr = cp_parser_expression (parser);
36931 cp_parser_consume_semicolon_at_end_of_statement (parser);
36933 return objc_build_throw_stmt (loc, expr);
36936 /* Parse an Objective-C statement. */
36938 static tree
36939 cp_parser_objc_statement (cp_parser * parser)
36941 /* Try to figure out what kind of declaration is present. */
36942 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36944 switch (kwd->keyword)
36946 case RID_AT_TRY:
36947 return cp_parser_objc_try_catch_finally_statement (parser);
36948 case RID_AT_SYNCHRONIZED:
36949 return cp_parser_objc_synchronized_statement (parser);
36950 case RID_AT_THROW:
36951 return cp_parser_objc_throw_statement (parser);
36952 default:
36953 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36954 kwd->u.value);
36955 cp_parser_skip_to_end_of_block_or_statement (parser);
36958 return error_mark_node;
36961 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36962 look ahead to see if an objc keyword follows the attributes. This
36963 is to detect the use of prefix attributes on ObjC @interface and
36964 @protocol. */
36966 static bool
36967 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36969 cp_lexer_save_tokens (parser->lexer);
36970 tree addon = cp_parser_attributes_opt (parser);
36971 if (addon
36972 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36974 cp_lexer_commit_tokens (parser->lexer);
36975 if (*attrib)
36976 TREE_CHAIN (*attrib) = addon;
36977 else
36978 *attrib = addon;
36979 return true;
36981 cp_lexer_rollback_tokens (parser->lexer);
36982 return false;
36985 /* This routine is a minimal replacement for
36986 c_parser_struct_declaration () used when parsing the list of
36987 types/names or ObjC++ properties. For example, when parsing the
36988 code
36990 @property (readonly) int a, b, c;
36992 this function is responsible for parsing "int a, int b, int c" and
36993 returning the declarations as CHAIN of DECLs.
36995 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36996 similar parsing. */
36997 static tree
36998 cp_parser_objc_struct_declaration (cp_parser *parser)
37000 tree decls = NULL_TREE;
37001 cp_decl_specifier_seq declspecs;
37002 int decl_class_or_enum_p;
37003 tree prefix_attributes;
37005 cp_parser_decl_specifier_seq (parser,
37006 CP_PARSER_FLAGS_NONE,
37007 &declspecs,
37008 &decl_class_or_enum_p);
37010 if (declspecs.type == error_mark_node)
37011 return error_mark_node;
37013 /* auto, register, static, extern, mutable. */
37014 if (declspecs.storage_class != sc_none)
37016 cp_parser_error (parser, "invalid type for property");
37017 declspecs.storage_class = sc_none;
37020 /* thread_local. */
37021 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37023 cp_parser_error (parser, "invalid type for property");
37024 declspecs.locations[ds_thread] = 0;
37027 /* typedef. */
37028 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37030 cp_parser_error (parser, "invalid type for property");
37031 declspecs.locations[ds_typedef] = 0;
37034 prefix_attributes = declspecs.attributes;
37035 declspecs.attributes = NULL_TREE;
37037 /* Keep going until we hit the `;' at the end of the declaration. */
37038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37040 tree attributes, first_attribute, decl;
37041 cp_declarator *declarator;
37042 cp_token *token;
37044 /* Parse the declarator. */
37045 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37046 CP_PARSER_FLAGS_NONE,
37047 NULL, NULL, false, false, false);
37049 /* Look for attributes that apply to the ivar. */
37050 attributes = cp_parser_attributes_opt (parser);
37051 /* Remember which attributes are prefix attributes and
37052 which are not. */
37053 first_attribute = attributes;
37054 /* Combine the attributes. */
37055 attributes = attr_chainon (prefix_attributes, attributes);
37057 decl = grokfield (declarator, &declspecs,
37058 NULL_TREE, /*init_const_expr_p=*/false,
37059 NULL_TREE, attributes);
37061 if (decl == error_mark_node || decl == NULL_TREE)
37062 return error_mark_node;
37064 /* Reset PREFIX_ATTRIBUTES. */
37065 if (attributes != error_mark_node)
37067 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37068 attributes = TREE_CHAIN (attributes);
37069 if (attributes)
37070 TREE_CHAIN (attributes) = NULL_TREE;
37073 DECL_CHAIN (decl) = decls;
37074 decls = decl;
37076 token = cp_lexer_peek_token (parser->lexer);
37077 if (token->type == CPP_COMMA)
37079 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37080 continue;
37082 else
37083 break;
37085 return decls;
37088 /* Parse an Objective-C @property declaration. The syntax is:
37090 objc-property-declaration:
37091 '@property' objc-property-attributes[opt] struct-declaration ;
37093 objc-property-attributes:
37094 '(' objc-property-attribute-list ')'
37096 objc-property-attribute-list:
37097 objc-property-attribute
37098 objc-property-attribute-list, objc-property-attribute
37100 objc-property-attribute
37101 'getter' = identifier
37102 'setter' = identifier
37103 'readonly'
37104 'readwrite'
37105 'assign'
37106 'retain'
37107 'copy'
37108 'nonatomic'
37110 For example:
37111 @property NSString *name;
37112 @property (readonly) id object;
37113 @property (retain, nonatomic, getter=getTheName) id name;
37114 @property int a, b, c;
37116 PS: This function is identical to
37117 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37118 static void
37119 cp_parser_objc_at_property_declaration (cp_parser *parser)
37121 /* Parse the optional attribute list.
37123 A list of parsed, but not verified, attributes. */
37124 auto_delete_vec<property_attribute_info> prop_attr_list;
37125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37127 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37129 /* Parse the optional attribute list... */
37130 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37132 /* Eat the '('. */
37133 matching_parens parens;
37134 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37135 parens.consume_open (parser);
37136 bool syntax_error = false;
37138 /* Allow empty @property attribute lists, but with a warning. */
37139 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37140 location_t attr_comb;
37141 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37143 attr_comb = make_location (attr_end, attr_start, attr_end);
37144 warning_at (attr_comb, OPT_Wattributes,
37145 "empty property attribute list");
37147 else
37148 while (true)
37150 cp_token *token = cp_lexer_peek_token (parser->lexer);
37151 attr_start = token->location;
37152 attr_end = get_finish (token->location);
37153 attr_comb = make_location (attr_start, attr_start, attr_end);
37155 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37157 warning_at (attr_comb, OPT_Wattributes,
37158 "missing property attribute");
37159 if (token->type == CPP_CLOSE_PAREN)
37160 break;
37161 cp_lexer_consume_token (parser->lexer);
37162 continue;
37165 tree attr_name = NULL_TREE;
37166 if (identifier_p (token->u.value))
37167 attr_name = token->u.value;
37169 enum rid keyword;
37170 if (token->type == CPP_NAME)
37171 keyword = C_RID_CODE (token->u.value);
37172 else if (token->type == CPP_KEYWORD
37173 && token->keyword == RID_CLASS)
37174 /* Account for accepting the 'class' keyword in this context. */
37175 keyword = RID_CLASS;
37176 else
37177 keyword = RID_MAX; /* By definition, an unknown property. */
37178 cp_lexer_consume_token (parser->lexer);
37180 enum objc_property_attribute_kind prop_kind
37181 = objc_prop_attr_kind_for_rid (keyword);
37182 property_attribute_info *prop
37183 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37184 prop_attr_list.safe_push (prop);
37186 tree meth_name;
37187 switch (prop->prop_kind)
37189 default: break;
37190 case OBJC_PROPERTY_ATTR_UNKNOWN:
37191 if (attr_name)
37192 error_at (attr_start, "unknown property attribute %qE",
37193 attr_name);
37194 else
37195 error_at (attr_start, "unknown property attribute");
37196 prop->parse_error = syntax_error = true;
37197 break;
37199 case OBJC_PROPERTY_ATTR_GETTER:
37200 case OBJC_PROPERTY_ATTR_SETTER:
37201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37203 attr_comb = make_location (attr_end, attr_start, attr_end);
37204 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37205 attr_name);
37206 prop->parse_error = syntax_error = true;
37207 break;
37210 token = cp_lexer_peek_token (parser->lexer);
37211 attr_end = token->location;
37212 cp_lexer_consume_token (parser->lexer); /* eat the = */
37214 if (!cp_parser_objc_selector_p
37215 (cp_lexer_peek_token (parser->lexer)->type))
37217 attr_comb = make_location (attr_end, attr_start, attr_end);
37218 error_at (attr_comb, "expected %qE selector name",
37219 attr_name);
37220 prop->parse_error = syntax_error = true;
37221 break;
37224 /* Get the end of the method name, and consume the name. */
37225 token = cp_lexer_peek_token (parser->lexer);
37226 attr_end = get_finish (token->location);
37227 /* Because method names may contain C++ keywords, we have a
37228 routine to fetch them (this also consumes the token). */
37229 meth_name = cp_parser_objc_selector (parser);
37231 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37235 attr_comb = make_location (attr_end, attr_start,
37236 attr_end);
37237 error_at (attr_comb, "setter method names must"
37238 " terminate with %<:%>");
37239 prop->parse_error = syntax_error = true;
37241 else
37243 attr_end = get_finish (cp_lexer_peek_token
37244 (parser->lexer)->location);
37245 cp_lexer_consume_token (parser->lexer);
37247 attr_comb = make_location (attr_start, attr_start,
37248 attr_end);
37250 else
37251 attr_comb = make_location (attr_start, attr_start,
37252 attr_end);
37253 prop->ident = meth_name;
37254 /* Updated location including all that was successfully
37255 parsed. */
37256 prop->prop_loc = attr_comb;
37257 break;
37260 /* If we see a comma here, then keep going - even if we already
37261 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37262 this makes a more useful output and avoid spurious warnings
37263 about missing attributes that are, in fact, specified after the
37264 one with the syntax error. */
37265 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37266 cp_lexer_consume_token (parser->lexer);
37267 else
37268 break;
37271 if (syntax_error || !parens.require_close (parser))
37272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37273 /*or_comma=*/false,
37274 /*consume_paren=*/true);
37277 /* 'properties' is the list of properties that we read. Usually a
37278 single one, but maybe more (eg, in "@property int a, b, c;" there
37279 are three).
37280 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37281 that we can issue a meaningful and consistent (between C/C++) error
37282 message from objc_add_property_declaration (). */
37283 tree properties = cp_parser_objc_struct_declaration (parser);
37285 if (properties == error_mark_node)
37286 cp_parser_skip_to_end_of_statement (parser);
37287 else if (properties == NULL_TREE)
37288 cp_parser_error (parser, "expected identifier");
37289 else
37291 /* Comma-separated properties are chained together in reverse order;
37292 add them one by one. */
37293 properties = nreverse (properties);
37294 for (; properties; properties = TREE_CHAIN (properties))
37295 objc_add_property_declaration (loc, copy_node (properties),
37296 prop_attr_list);
37299 cp_parser_consume_semicolon_at_end_of_statement (parser);
37302 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37304 objc-synthesize-declaration:
37305 @synthesize objc-synthesize-identifier-list ;
37307 objc-synthesize-identifier-list:
37308 objc-synthesize-identifier
37309 objc-synthesize-identifier-list, objc-synthesize-identifier
37311 objc-synthesize-identifier
37312 identifier
37313 identifier = identifier
37315 For example:
37316 @synthesize MyProperty;
37317 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37319 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37320 for C. Keep them in sync.
37322 static void
37323 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37325 tree list = NULL_TREE;
37326 location_t loc;
37327 loc = cp_lexer_peek_token (parser->lexer)->location;
37329 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37330 while (true)
37332 tree property, ivar;
37333 property = cp_parser_identifier (parser);
37334 if (property == error_mark_node)
37336 cp_parser_consume_semicolon_at_end_of_statement (parser);
37337 return;
37339 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37341 cp_lexer_consume_token (parser->lexer);
37342 ivar = cp_parser_identifier (parser);
37343 if (ivar == error_mark_node)
37345 cp_parser_consume_semicolon_at_end_of_statement (parser);
37346 return;
37349 else
37350 ivar = NULL_TREE;
37351 list = chainon (list, build_tree_list (ivar, property));
37352 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37353 cp_lexer_consume_token (parser->lexer);
37354 else
37355 break;
37357 cp_parser_consume_semicolon_at_end_of_statement (parser);
37358 objc_add_synthesize_declaration (loc, list);
37361 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37363 objc-dynamic-declaration:
37364 @dynamic identifier-list ;
37366 For example:
37367 @dynamic MyProperty;
37368 @dynamic MyProperty, AnotherProperty;
37370 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37371 for C. Keep them in sync.
37373 static void
37374 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37376 tree list = NULL_TREE;
37377 location_t loc;
37378 loc = cp_lexer_peek_token (parser->lexer)->location;
37380 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37381 while (true)
37383 tree property;
37384 property = cp_parser_identifier (parser);
37385 if (property == error_mark_node)
37387 cp_parser_consume_semicolon_at_end_of_statement (parser);
37388 return;
37390 list = chainon (list, build_tree_list (NULL, property));
37391 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37392 cp_lexer_consume_token (parser->lexer);
37393 else
37394 break;
37396 cp_parser_consume_semicolon_at_end_of_statement (parser);
37397 objc_add_dynamic_declaration (loc, list);
37401 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37403 /* Returns name of the next clause.
37404 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37405 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37406 returned and the token is consumed. */
37408 static pragma_omp_clause
37409 cp_parser_omp_clause_name (cp_parser *parser)
37411 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37414 result = PRAGMA_OACC_CLAUSE_AUTO;
37415 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37416 result = PRAGMA_OMP_CLAUSE_IF;
37417 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37418 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37419 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37420 result = PRAGMA_OACC_CLAUSE_DELETE;
37421 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37422 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37423 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37424 result = PRAGMA_OMP_CLAUSE_FOR;
37425 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37428 const char *p = IDENTIFIER_POINTER (id);
37430 switch (p[0])
37432 case 'a':
37433 if (!strcmp ("affinity", p))
37434 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37435 else if (!strcmp ("aligned", p))
37436 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37437 else if (!strcmp ("allocate", p))
37438 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37439 else if (!strcmp ("async", p))
37440 result = PRAGMA_OACC_CLAUSE_ASYNC;
37441 else if (!strcmp ("attach", p))
37442 result = PRAGMA_OACC_CLAUSE_ATTACH;
37443 break;
37444 case 'b':
37445 if (!strcmp ("bind", p))
37446 result = PRAGMA_OMP_CLAUSE_BIND;
37447 break;
37448 case 'c':
37449 if (!strcmp ("collapse", p))
37450 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37451 else if (!strcmp ("copy", p))
37452 result = PRAGMA_OACC_CLAUSE_COPY;
37453 else if (!strcmp ("copyin", p))
37454 result = PRAGMA_OMP_CLAUSE_COPYIN;
37455 else if (!strcmp ("copyout", p))
37456 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37457 else if (!strcmp ("copyprivate", p))
37458 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37459 else if (!strcmp ("create", p))
37460 result = PRAGMA_OACC_CLAUSE_CREATE;
37461 break;
37462 case 'd':
37463 if (!strcmp ("defaultmap", p))
37464 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37465 else if (!strcmp ("depend", p))
37466 result = PRAGMA_OMP_CLAUSE_DEPEND;
37467 else if (!strcmp ("detach", p))
37468 result = PRAGMA_OACC_CLAUSE_DETACH;
37469 else if (!strcmp ("device", p))
37470 result = PRAGMA_OMP_CLAUSE_DEVICE;
37471 else if (!strcmp ("deviceptr", p))
37472 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37473 else if (!strcmp ("device_resident", p))
37474 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37475 else if (!strcmp ("device_type", p))
37476 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37477 else if (!strcmp ("dist_schedule", p))
37478 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37479 else if (!strcmp ("doacross", p))
37480 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37481 break;
37482 case 'e':
37483 if (!strcmp ("enter", p))
37484 result = PRAGMA_OMP_CLAUSE_ENTER;
37485 break;
37486 case 'f':
37487 if (!strcmp ("filter", p))
37488 result = PRAGMA_OMP_CLAUSE_FILTER;
37489 else if (!strcmp ("final", p))
37490 result = PRAGMA_OMP_CLAUSE_FINAL;
37491 else if (!strcmp ("finalize", p))
37492 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37493 else if (!strcmp ("firstprivate", p))
37494 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37495 else if (!strcmp ("from", p))
37496 result = PRAGMA_OMP_CLAUSE_FROM;
37497 break;
37498 case 'g':
37499 if (!strcmp ("gang", p))
37500 result = PRAGMA_OACC_CLAUSE_GANG;
37501 else if (!strcmp ("grainsize", p))
37502 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37503 break;
37504 case 'h':
37505 if (!strcmp ("has_device_addr", p))
37506 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37507 else if (!strcmp ("hint", p))
37508 result = PRAGMA_OMP_CLAUSE_HINT;
37509 else if (!strcmp ("host", p))
37510 result = PRAGMA_OACC_CLAUSE_HOST;
37511 break;
37512 case 'i':
37513 if (!strcmp ("if_present", p))
37514 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37515 else if (!strcmp ("in_reduction", p))
37516 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37517 else if (!strcmp ("inbranch", p))
37518 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37519 else if (!strcmp ("independent", p))
37520 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37521 else if (!strcmp ("is_device_ptr", p))
37522 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37523 break;
37524 case 'l':
37525 if (!strcmp ("lastprivate", p))
37526 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37527 else if (!strcmp ("linear", p))
37528 result = PRAGMA_OMP_CLAUSE_LINEAR;
37529 else if (!strcmp ("link", p))
37530 result = PRAGMA_OMP_CLAUSE_LINK;
37531 break;
37532 case 'm':
37533 if (!strcmp ("map", p))
37534 result = PRAGMA_OMP_CLAUSE_MAP;
37535 else if (!strcmp ("mergeable", p))
37536 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37537 break;
37538 case 'n':
37539 if (!strcmp ("no_create", p))
37540 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37541 else if (!strcmp ("nogroup", p))
37542 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37543 else if (!strcmp ("nohost", p))
37544 result = PRAGMA_OACC_CLAUSE_NOHOST;
37545 else if (!strcmp ("nontemporal", p))
37546 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37547 else if (!strcmp ("notinbranch", p))
37548 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37549 else if (!strcmp ("nowait", p))
37550 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37551 else if (!strcmp ("num_gangs", p))
37552 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37553 else if (!strcmp ("num_tasks", p))
37554 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37555 else if (!strcmp ("num_teams", p))
37556 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37557 else if (!strcmp ("num_threads", p))
37558 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37559 else if (!strcmp ("num_workers", p))
37560 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37561 break;
37562 case 'o':
37563 if (!strcmp ("ordered", p))
37564 result = PRAGMA_OMP_CLAUSE_ORDERED;
37565 else if (!strcmp ("order", p))
37566 result = PRAGMA_OMP_CLAUSE_ORDER;
37567 break;
37568 case 'p':
37569 if (!strcmp ("parallel", p))
37570 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37571 else if (!strcmp ("present", p))
37572 result = PRAGMA_OACC_CLAUSE_PRESENT;
37573 else if (!strcmp ("present_or_copy", p)
37574 || !strcmp ("pcopy", p))
37575 result = PRAGMA_OACC_CLAUSE_COPY;
37576 else if (!strcmp ("present_or_copyin", p)
37577 || !strcmp ("pcopyin", p))
37578 result = PRAGMA_OACC_CLAUSE_COPYIN;
37579 else if (!strcmp ("present_or_copyout", p)
37580 || !strcmp ("pcopyout", p))
37581 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37582 else if (!strcmp ("present_or_create", p)
37583 || !strcmp ("pcreate", p))
37584 result = PRAGMA_OACC_CLAUSE_CREATE;
37585 else if (!strcmp ("priority", p))
37586 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37587 else if (!strcmp ("proc_bind", p))
37588 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37589 break;
37590 case 'r':
37591 if (!strcmp ("reduction", p))
37592 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37593 break;
37594 case 's':
37595 if (!strcmp ("safelen", p))
37596 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37597 else if (!strcmp ("schedule", p))
37598 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37599 else if (!strcmp ("sections", p))
37600 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37601 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
37602 result = PRAGMA_OACC_CLAUSE_HOST;
37603 else if (!strcmp ("seq", p))
37604 result = PRAGMA_OACC_CLAUSE_SEQ;
37605 else if (!strcmp ("shared", p))
37606 result = PRAGMA_OMP_CLAUSE_SHARED;
37607 else if (!strcmp ("simd", p))
37608 result = PRAGMA_OMP_CLAUSE_SIMD;
37609 else if (!strcmp ("simdlen", p))
37610 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37611 break;
37612 case 't':
37613 if (!strcmp ("task_reduction", p))
37614 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37615 else if (!strcmp ("taskgroup", p))
37616 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37617 else if (!strcmp ("thread_limit", p))
37618 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37619 else if (!strcmp ("threads", p))
37620 result = PRAGMA_OMP_CLAUSE_THREADS;
37621 else if (!strcmp ("tile", p))
37622 result = PRAGMA_OACC_CLAUSE_TILE;
37623 else if (!strcmp ("to", p))
37624 result = PRAGMA_OMP_CLAUSE_TO;
37625 break;
37626 case 'u':
37627 if (!strcmp ("uniform", p))
37628 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37629 else if (!strcmp ("untied", p))
37630 result = PRAGMA_OMP_CLAUSE_UNTIED;
37631 else if (!strcmp ("use_device", p))
37632 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37633 else if (!strcmp ("use_device_addr", p))
37634 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37635 else if (!strcmp ("use_device_ptr", p))
37636 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37637 break;
37638 case 'v':
37639 if (!strcmp ("vector", p))
37640 result = PRAGMA_OACC_CLAUSE_VECTOR;
37641 else if (!strcmp ("vector_length", p))
37642 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37643 break;
37644 case 'w':
37645 if (!strcmp ("wait", p))
37646 result = PRAGMA_OACC_CLAUSE_WAIT;
37647 else if (!strcmp ("worker", p))
37648 result = PRAGMA_OACC_CLAUSE_WORKER;
37649 break;
37653 if (result != PRAGMA_OMP_CLAUSE_NONE)
37654 cp_lexer_consume_token (parser->lexer);
37656 return result;
37659 /* Validate that a clause of the given type does not already exist. */
37661 static void
37662 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37663 const char *name, location_t location)
37665 if (omp_find_clause (clauses, code))
37666 error_at (location, "too many %qs clauses", name);
37669 /* OpenMP 2.5:
37670 variable-list:
37671 identifier
37672 variable-list , identifier
37674 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37675 colon). An opening parenthesis will have been consumed by the caller.
37677 If KIND is nonzero, create the appropriate node and install the decl
37678 in OMP_CLAUSE_DECL and add the node to the head of the list.
37680 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37681 return the list created.
37683 COLON can be NULL if only closing parenthesis should end the list,
37684 or pointer to bool which will receive false if the list is terminated
37685 by closing parenthesis or true if the list is terminated by colon.
37687 The optional ALLOW_DEREF argument is true if list items can use the deref
37688 (->) operator. */
37690 struct omp_dim
37692 tree low_bound, length;
37693 location_t loc;
37694 bool no_colon;
37695 omp_dim (tree lb, tree len, location_t lo, bool nc)
37696 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37699 static tree
37700 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37701 tree list, bool *colon,
37702 bool allow_deref = false)
37704 auto_vec<omp_dim> dims;
37705 bool array_section_p;
37706 cp_token *token;
37707 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37708 if (colon)
37710 parser->colon_corrects_to_scope_p = false;
37711 *colon = false;
37713 while (1)
37715 tree name, decl;
37717 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37718 cp_parser_parse_tentatively (parser);
37719 token = cp_lexer_peek_token (parser->lexer);
37720 if (kind != 0
37721 && cp_parser_is_keyword (token, RID_THIS))
37723 decl = finish_this_expr ();
37724 if (TREE_CODE (decl) == NON_LVALUE_EXPR
37725 || CONVERT_EXPR_P (decl))
37726 decl = TREE_OPERAND (decl, 0);
37727 cp_lexer_consume_token (parser->lexer);
37729 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
37730 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
37731 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
37733 cp_id_kind idk;
37734 decl = cp_parser_primary_expression (parser, false, false, false,
37735 &idk);
37737 else if (kind == OMP_CLAUSE_DEPEND
37738 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
37739 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37740 || cp_lexer_nth_token_is (parser->lexer, 2,
37741 CPP_CLOSE_PAREN)))
37743 decl = ridpointers[RID_OMP_ALL_MEMORY];
37744 cp_lexer_consume_token (parser->lexer);
37746 else
37748 name = cp_parser_id_expression (parser, /*template_p=*/false,
37749 /*check_dependency_p=*/true,
37750 /*template_p=*/NULL,
37751 /*declarator_p=*/false,
37752 /*optional_p=*/false);
37753 if (name == error_mark_node)
37755 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37756 && cp_parser_simulate_error (parser))
37757 goto depend_lvalue;
37758 goto skip_comma;
37761 if (identifier_p (name))
37762 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37763 else
37764 decl = name;
37765 if (decl == error_mark_node)
37767 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37768 && cp_parser_simulate_error (parser))
37769 goto depend_lvalue;
37770 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37771 token->location);
37774 if (outer_automatic_var_p (decl))
37775 decl = process_outer_var_ref (decl, tf_warning_or_error);
37776 if (decl == error_mark_node)
37778 else if (kind != 0)
37780 switch (kind)
37782 case OMP_CLAUSE__CACHE_:
37783 /* The OpenACC cache directive explicitly only allows "array
37784 elements or subarrays". */
37785 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
37787 error_at (token->location, "expected %<[%>");
37788 decl = error_mark_node;
37789 break;
37791 /* FALLTHROUGH. */
37792 case OMP_CLAUSE_MAP:
37793 case OMP_CLAUSE_FROM:
37794 case OMP_CLAUSE_TO:
37795 start_component_ref:
37796 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37797 || (allow_deref
37798 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
37800 cpp_ttype ttype
37801 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37802 ? CPP_DOT : CPP_DEREF;
37803 location_t loc
37804 = cp_lexer_peek_token (parser->lexer)->location;
37805 cp_id_kind idk = CP_ID_KIND_NONE;
37806 cp_lexer_consume_token (parser->lexer);
37807 decl = convert_from_reference (decl);
37808 decl = (cp_parser_postfix_dot_deref_expression
37809 (parser, ttype, cp_expr (decl, token->location),
37810 false, &idk, loc));
37812 /* FALLTHROUGH. */
37813 case OMP_CLAUSE_AFFINITY:
37814 case OMP_CLAUSE_DEPEND:
37815 case OMP_CLAUSE_REDUCTION:
37816 case OMP_CLAUSE_IN_REDUCTION:
37817 case OMP_CLAUSE_TASK_REDUCTION:
37818 case OMP_CLAUSE_HAS_DEVICE_ADDR:
37819 array_section_p = false;
37820 dims.truncate (0);
37821 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
37823 location_t loc = UNKNOWN_LOCATION;
37824 tree low_bound = NULL_TREE, length = NULL_TREE;
37825 bool no_colon = false;
37827 parser->colon_corrects_to_scope_p = false;
37828 cp_lexer_consume_token (parser->lexer);
37829 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37831 loc = cp_lexer_peek_token (parser->lexer)->location;
37832 low_bound = cp_parser_expression (parser);
37833 /* Later handling is not prepared to see through these. */
37834 gcc_checking_assert (!location_wrapper_p (low_bound));
37836 if (!colon)
37837 parser->colon_corrects_to_scope_p
37838 = saved_colon_corrects_to_scope_p;
37839 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
37841 length = integer_one_node;
37842 no_colon = true;
37844 else
37846 /* Look for `:'. */
37847 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37849 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37850 && cp_parser_simulate_error (parser))
37851 goto depend_lvalue;
37852 goto skip_comma;
37854 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37855 cp_parser_commit_to_tentative_parse (parser);
37856 else
37857 array_section_p = true;
37858 if (!cp_lexer_next_token_is (parser->lexer,
37859 CPP_CLOSE_SQUARE))
37861 length = cp_parser_expression (parser);
37862 /* Later handling is not prepared to see through these. */
37863 gcc_checking_assert (!location_wrapper_p (length));
37866 /* Look for the closing `]'. */
37867 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
37868 RT_CLOSE_SQUARE))
37870 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37871 && cp_parser_simulate_error (parser))
37872 goto depend_lvalue;
37873 goto skip_comma;
37876 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
37879 if ((kind == OMP_CLAUSE_MAP
37880 || kind == OMP_CLAUSE_FROM
37881 || kind == OMP_CLAUSE_TO)
37882 && !array_section_p
37883 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37884 || (allow_deref
37885 && cp_lexer_next_token_is (parser->lexer,
37886 CPP_DEREF))))
37888 for (unsigned i = 0; i < dims.length (); i++)
37890 gcc_assert (dims[i].length == integer_one_node);
37891 decl = build_array_ref (dims[i].loc,
37892 decl, dims[i].low_bound);
37894 goto start_component_ref;
37896 else
37897 for (unsigned i = 0; i < dims.length (); i++)
37898 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
37900 break;
37901 default:
37902 break;
37905 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
37908 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
37909 && cp_parser_simulate_error (parser))
37911 depend_lvalue:
37912 cp_parser_abort_tentative_parse (parser);
37913 decl = cp_parser_assignment_expression (parser, NULL,
37914 false, false);
37916 else
37917 cp_parser_parse_definitely (parser);
37920 tree u = build_omp_clause (token->location, kind);
37921 OMP_CLAUSE_DECL (u) = decl;
37922 OMP_CLAUSE_CHAIN (u) = list;
37923 list = u;
37925 else
37926 list = tree_cons (decl, NULL_TREE, list);
37928 get_comma:
37929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37930 break;
37931 cp_lexer_consume_token (parser->lexer);
37934 if (colon)
37935 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37937 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37939 *colon = true;
37940 cp_parser_require (parser, CPP_COLON, RT_COLON);
37941 return list;
37944 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37946 int ending;
37948 /* Try to resync to an unnested comma. Copied from
37949 cp_parser_parenthesized_expression_list. */
37950 skip_comma:
37951 if (colon)
37952 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37953 ending = cp_parser_skip_to_closing_parenthesis (parser,
37954 /*recovering=*/true,
37955 /*or_comma=*/true,
37956 /*consume_paren=*/true);
37957 if (ending < 0)
37958 goto get_comma;
37961 return list;
37964 /* Similarly, but expect leading and trailing parenthesis. This is a very
37965 common case for omp clauses. */
37967 static tree
37968 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37969 bool allow_deref = false)
37971 if (parser->lexer->in_omp_decl_attribute)
37973 if (kind)
37975 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37976 tree u = build_omp_clause (loc, kind);
37977 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
37978 OMP_CLAUSE_CHAIN (u) = list;
37979 return u;
37981 else
37982 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
37983 list);
37986 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37987 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37988 allow_deref);
37989 return list;
37992 /* OpenACC 2.0:
37993 copy ( variable-list )
37994 copyin ( variable-list )
37995 copyout ( variable-list )
37996 create ( variable-list )
37997 delete ( variable-list )
37998 present ( variable-list )
38000 OpenACC 2.6:
38001 no_create ( variable-list )
38002 attach ( variable-list )
38003 detach ( variable-list ) */
38005 static tree
38006 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38007 tree list)
38009 enum gomp_map_kind kind;
38010 switch (c_kind)
38012 case PRAGMA_OACC_CLAUSE_ATTACH:
38013 kind = GOMP_MAP_ATTACH;
38014 break;
38015 case PRAGMA_OACC_CLAUSE_COPY:
38016 kind = GOMP_MAP_TOFROM;
38017 break;
38018 case PRAGMA_OACC_CLAUSE_COPYIN:
38019 kind = GOMP_MAP_TO;
38020 break;
38021 case PRAGMA_OACC_CLAUSE_COPYOUT:
38022 kind = GOMP_MAP_FROM;
38023 break;
38024 case PRAGMA_OACC_CLAUSE_CREATE:
38025 kind = GOMP_MAP_ALLOC;
38026 break;
38027 case PRAGMA_OACC_CLAUSE_DELETE:
38028 kind = GOMP_MAP_RELEASE;
38029 break;
38030 case PRAGMA_OACC_CLAUSE_DETACH:
38031 kind = GOMP_MAP_DETACH;
38032 break;
38033 case PRAGMA_OACC_CLAUSE_DEVICE:
38034 kind = GOMP_MAP_FORCE_TO;
38035 break;
38036 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38037 kind = GOMP_MAP_DEVICE_RESIDENT;
38038 break;
38039 case PRAGMA_OACC_CLAUSE_HOST:
38040 kind = GOMP_MAP_FORCE_FROM;
38041 break;
38042 case PRAGMA_OACC_CLAUSE_LINK:
38043 kind = GOMP_MAP_LINK;
38044 break;
38045 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38046 kind = GOMP_MAP_IF_PRESENT;
38047 break;
38048 case PRAGMA_OACC_CLAUSE_PRESENT:
38049 kind = GOMP_MAP_FORCE_PRESENT;
38050 break;
38051 default:
38052 gcc_unreachable ();
38054 tree nl, c;
38055 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
38057 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38058 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38060 return nl;
38063 /* OpenACC 2.0:
38064 deviceptr ( variable-list ) */
38066 static tree
38067 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38069 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38070 tree vars, t;
38072 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38073 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38074 variable-list must only allow for pointer variables. */
38075 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38076 for (t = vars; t; t = TREE_CHAIN (t))
38078 tree v = TREE_PURPOSE (t);
38079 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38080 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38081 OMP_CLAUSE_DECL (u) = v;
38082 OMP_CLAUSE_CHAIN (u) = list;
38083 list = u;
38086 return list;
38089 /* OpenACC 2.5:
38090 auto
38091 finalize
38092 independent
38093 nohost
38094 seq */
38096 static tree
38097 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38098 tree list)
38100 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38102 tree c = build_omp_clause (loc, code);
38103 OMP_CLAUSE_CHAIN (c) = list;
38105 return c;
38108 /* OpenACC:
38109 num_gangs ( expression )
38110 num_workers ( expression )
38111 vector_length ( expression ) */
38113 static tree
38114 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38115 const char *str, tree list)
38117 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38119 matching_parens parens;
38120 if (!parens.require_open (parser))
38121 return list;
38123 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38125 if (t == error_mark_node
38126 || !parens.require_close (parser))
38128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38129 /*or_comma=*/false,
38130 /*consume_paren=*/true);
38131 return list;
38134 check_no_duplicate_clause (list, code, str, loc);
38136 tree c = build_omp_clause (loc, code);
38137 OMP_CLAUSE_OPERAND (c, 0) = t;
38138 OMP_CLAUSE_CHAIN (c) = list;
38139 return c;
38142 /* OpenACC:
38144 gang [( gang-arg-list )]
38145 worker [( [num:] int-expr )]
38146 vector [( [length:] int-expr )]
38148 where gang-arg is one of:
38150 [num:] int-expr
38151 static: size-expr
38153 and size-expr may be:
38156 int-expr
38159 static tree
38160 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38161 omp_clause_code kind,
38162 const char *str, tree list)
38164 const char *id = "num";
38165 cp_lexer *lexer = parser->lexer;
38166 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38168 if (kind == OMP_CLAUSE_VECTOR)
38169 id = "length";
38171 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38173 matching_parens parens;
38174 parens.consume_open (parser);
38178 cp_token *next = cp_lexer_peek_token (lexer);
38179 int idx = 0;
38181 /* Gang static argument. */
38182 if (kind == OMP_CLAUSE_GANG
38183 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38185 cp_lexer_consume_token (lexer);
38187 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38188 goto cleanup_error;
38190 idx = 1;
38191 if (ops[idx] != NULL)
38193 cp_parser_error (parser, "too many %<static%> arguments");
38194 goto cleanup_error;
38197 /* Check for the '*' argument. */
38198 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38199 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38200 || cp_lexer_nth_token_is (parser->lexer, 2,
38201 CPP_CLOSE_PAREN)))
38203 cp_lexer_consume_token (lexer);
38204 ops[idx] = integer_minus_one_node;
38206 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38208 cp_lexer_consume_token (lexer);
38209 continue;
38211 else break;
38214 /* Worker num: argument and vector length: arguments. */
38215 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38216 && id_equal (next->u.value, id)
38217 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38219 cp_lexer_consume_token (lexer); /* id */
38220 cp_lexer_consume_token (lexer); /* ':' */
38223 /* Now collect the actual argument. */
38224 if (ops[idx] != NULL_TREE)
38226 cp_parser_error (parser, "unexpected argument");
38227 goto cleanup_error;
38230 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38231 false);
38232 if (expr == error_mark_node)
38233 goto cleanup_error;
38235 mark_exp_read (expr);
38236 ops[idx] = expr;
38238 if (kind == OMP_CLAUSE_GANG
38239 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38241 cp_lexer_consume_token (lexer);
38242 continue;
38244 break;
38246 while (1);
38248 if (!parens.require_close (parser))
38249 goto cleanup_error;
38252 check_no_duplicate_clause (list, kind, str, loc);
38254 c = build_omp_clause (loc, kind);
38256 if (ops[1])
38257 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38259 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38260 OMP_CLAUSE_CHAIN (c) = list;
38262 return c;
38264 cleanup_error:
38265 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38266 return list;
38269 /* OpenACC 2.0:
38270 tile ( size-expr-list ) */
38272 static tree
38273 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38275 tree c, expr = error_mark_node;
38276 tree tile = NULL_TREE;
38278 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38279 so, but the spec authors never considered such a case and have
38280 differing opinions on what it might mean, including 'not
38281 allowed'.) */
38282 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38283 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38284 clause_loc);
38286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38287 return list;
38291 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38292 return list;
38294 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38295 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38296 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38298 cp_lexer_consume_token (parser->lexer);
38299 expr = integer_zero_node;
38301 else
38302 expr = cp_parser_constant_expression (parser);
38304 tile = tree_cons (NULL_TREE, expr, tile);
38306 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38308 /* Consume the trailing ')'. */
38309 cp_lexer_consume_token (parser->lexer);
38311 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38312 tile = nreverse (tile);
38313 OMP_CLAUSE_TILE_LIST (c) = tile;
38314 OMP_CLAUSE_CHAIN (c) = list;
38315 return c;
38318 /* OpenACC 2.0
38319 Parse wait clause or directive parameters. */
38321 static tree
38322 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38324 vec<tree, va_gc> *args;
38325 tree t, args_tree;
38327 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38328 /*cast_p=*/false,
38329 /*allow_expansion_p=*/true,
38330 /*non_constant_p=*/NULL);
38332 if (args == NULL || args->length () == 0)
38334 if (args != NULL)
38336 cp_parser_error (parser, "expected integer expression list");
38337 release_tree_vector (args);
38339 return list;
38342 args_tree = build_tree_list_vec (args);
38344 release_tree_vector (args);
38346 for (t = args_tree; t; t = TREE_CHAIN (t))
38348 tree targ = TREE_VALUE (t);
38350 if (targ != error_mark_node)
38352 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38353 error ("%<wait%> expression must be integral");
38354 else
38356 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38358 targ = mark_rvalue_use (targ);
38359 OMP_CLAUSE_DECL (c) = targ;
38360 OMP_CLAUSE_CHAIN (c) = list;
38361 list = c;
38366 return list;
38369 /* OpenACC:
38370 wait [( int-expr-list )] */
38372 static tree
38373 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38375 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38377 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38378 list = cp_parser_oacc_wait_list (parser, location, list);
38379 else
38381 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38383 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38384 OMP_CLAUSE_CHAIN (c) = list;
38385 list = c;
38388 return list;
38391 /* OpenMP 3.0:
38392 collapse ( constant-expression ) */
38394 static tree
38395 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38397 tree c, num;
38398 location_t loc;
38399 HOST_WIDE_INT n;
38401 loc = cp_lexer_peek_token (parser->lexer)->location;
38402 matching_parens parens;
38403 if (!parens.require_open (parser))
38404 return list;
38406 num = cp_parser_constant_expression (parser);
38408 if (!parens.require_close (parser))
38409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38410 /*or_comma=*/false,
38411 /*consume_paren=*/true);
38413 if (num == error_mark_node)
38414 return list;
38415 num = fold_non_dependent_expr (num);
38416 if (!tree_fits_shwi_p (num)
38417 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38418 || (n = tree_to_shwi (num)) <= 0
38419 || (int) n != n)
38421 error_at (loc, "collapse argument needs positive constant integer expression");
38422 return list;
38425 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38426 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38427 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38428 OMP_CLAUSE_CHAIN (c) = list;
38429 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38431 return c;
38434 /* OpenMP 2.5:
38435 default ( none | shared )
38437 OpenMP 5.1:
38438 default ( private | firstprivate )
38440 OpenACC:
38441 default ( none | present ) */
38443 static tree
38444 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38445 location_t location, bool is_oacc)
38447 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38448 tree c;
38450 matching_parens parens;
38451 if (!parens.require_open (parser))
38452 return list;
38453 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38455 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38456 cp_lexer_consume_token (parser->lexer);
38458 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38460 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38461 const char *p = IDENTIFIER_POINTER (id);
38463 switch (p[0])
38465 case 'n':
38466 if (strcmp ("none", p) != 0)
38467 goto invalid_kind;
38468 kind = OMP_CLAUSE_DEFAULT_NONE;
38469 break;
38471 case 'p':
38472 if (strcmp ("present", p) != 0 || !is_oacc)
38473 goto invalid_kind;
38474 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38475 break;
38477 case 'f':
38478 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38479 goto invalid_kind;
38480 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38481 break;
38483 case 's':
38484 if (strcmp ("shared", p) != 0 || is_oacc)
38485 goto invalid_kind;
38486 kind = OMP_CLAUSE_DEFAULT_SHARED;
38487 break;
38489 default:
38490 goto invalid_kind;
38493 cp_lexer_consume_token (parser->lexer);
38495 else
38497 invalid_kind:
38498 if (is_oacc)
38499 cp_parser_error (parser, "expected %<none%> or %<present%>");
38500 else
38501 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38502 "%<private%> or %<firstprivate%>");
38505 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38506 || !parens.require_close (parser))
38507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38508 /*or_comma=*/false,
38509 /*consume_paren=*/true);
38511 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38512 return list;
38514 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38515 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38516 OMP_CLAUSE_CHAIN (c) = list;
38517 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38519 return c;
38522 /* OpenMP 3.1:
38523 final ( expression ) */
38525 static tree
38526 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38528 tree t, c;
38530 matching_parens parens;
38531 if (!parens.require_open (parser))
38532 return list;
38534 t = cp_parser_assignment_expression (parser);
38536 if (t == error_mark_node
38537 || !parens.require_close (parser))
38538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38539 /*or_comma=*/false,
38540 /*consume_paren=*/true);
38542 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38544 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38545 OMP_CLAUSE_FINAL_EXPR (c) = t;
38546 OMP_CLAUSE_CHAIN (c) = list;
38548 return c;
38551 /* OpenMP 2.5:
38552 if ( expression )
38554 OpenMP 4.5:
38555 if ( directive-name-modifier : expression )
38557 directive-name-modifier:
38558 parallel | task | taskloop | target data | target | target update
38559 | target enter data | target exit data
38561 OpenMP 5.0:
38562 directive-name-modifier:
38563 ... | simd | cancel */
38565 static tree
38566 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38567 bool is_omp)
38569 tree t, c;
38570 enum tree_code if_modifier = ERROR_MARK;
38572 matching_parens parens;
38573 if (!parens.require_open (parser))
38574 return list;
38576 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38578 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38579 const char *p = IDENTIFIER_POINTER (id);
38580 int n = 2;
38582 if (strcmp ("cancel", p) == 0)
38583 if_modifier = VOID_CST;
38584 else if (strcmp ("parallel", p) == 0)
38585 if_modifier = OMP_PARALLEL;
38586 else if (strcmp ("simd", p) == 0)
38587 if_modifier = OMP_SIMD;
38588 else if (strcmp ("task", p) == 0)
38589 if_modifier = OMP_TASK;
38590 else if (strcmp ("taskloop", p) == 0)
38591 if_modifier = OMP_TASKLOOP;
38592 else if (strcmp ("target", p) == 0)
38594 if_modifier = OMP_TARGET;
38595 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38597 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
38598 p = IDENTIFIER_POINTER (id);
38599 if (strcmp ("data", p) == 0)
38600 if_modifier = OMP_TARGET_DATA;
38601 else if (strcmp ("update", p) == 0)
38602 if_modifier = OMP_TARGET_UPDATE;
38603 else if (strcmp ("enter", p) == 0)
38604 if_modifier = OMP_TARGET_ENTER_DATA;
38605 else if (strcmp ("exit", p) == 0)
38606 if_modifier = OMP_TARGET_EXIT_DATA;
38607 if (if_modifier != OMP_TARGET)
38608 n = 3;
38609 else
38611 location_t loc
38612 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
38613 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
38614 "or %<exit%>");
38615 if_modifier = ERROR_MARK;
38617 if (if_modifier == OMP_TARGET_ENTER_DATA
38618 || if_modifier == OMP_TARGET_EXIT_DATA)
38620 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
38622 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
38623 p = IDENTIFIER_POINTER (id);
38624 if (strcmp ("data", p) == 0)
38625 n = 4;
38627 if (n != 4)
38629 location_t loc
38630 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
38631 error_at (loc, "expected %<data%>");
38632 if_modifier = ERROR_MARK;
38637 if (if_modifier != ERROR_MARK)
38639 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
38641 while (n-- > 0)
38642 cp_lexer_consume_token (parser->lexer);
38644 else
38646 if (n > 2)
38648 location_t loc
38649 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
38650 error_at (loc, "expected %<:%>");
38652 if_modifier = ERROR_MARK;
38657 t = cp_parser_assignment_expression (parser);
38659 if (t == error_mark_node
38660 || !parens.require_close (parser))
38661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38662 /*or_comma=*/false,
38663 /*consume_paren=*/true);
38665 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38666 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
38668 if (if_modifier != ERROR_MARK
38669 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38671 const char *p = NULL;
38672 switch (if_modifier)
38674 case VOID_CST: p = "cancel"; break;
38675 case OMP_PARALLEL: p = "parallel"; break;
38676 case OMP_SIMD: p = "simd"; break;
38677 case OMP_TASK: p = "task"; break;
38678 case OMP_TASKLOOP: p = "taskloop"; break;
38679 case OMP_TARGET_DATA: p = "target data"; break;
38680 case OMP_TARGET: p = "target"; break;
38681 case OMP_TARGET_UPDATE: p = "target update"; break;
38682 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
38683 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
38684 default: gcc_unreachable ();
38686 error_at (location, "too many %<if%> clauses with %qs modifier",
38688 return list;
38690 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38692 if (!is_omp)
38693 error_at (location, "too many %<if%> clauses");
38694 else
38695 error_at (location, "too many %<if%> clauses without modifier");
38696 return list;
38698 else if (if_modifier == ERROR_MARK
38699 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
38701 error_at (location, "if any %<if%> clause has modifier, then all "
38702 "%<if%> clauses have to use modifier");
38703 return list;
38707 c = build_omp_clause (location, OMP_CLAUSE_IF);
38708 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
38709 OMP_CLAUSE_IF_EXPR (c) = t;
38710 OMP_CLAUSE_CHAIN (c) = list;
38712 return c;
38715 /* OpenMP 3.1:
38716 mergeable */
38718 static tree
38719 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
38720 tree list, location_t location)
38722 tree c;
38724 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
38725 location);
38727 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
38728 OMP_CLAUSE_CHAIN (c) = list;
38729 return c;
38732 /* OpenMP 2.5:
38733 nowait */
38735 static tree
38736 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
38737 tree list, location_t location)
38739 tree c;
38741 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
38743 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
38744 OMP_CLAUSE_CHAIN (c) = list;
38745 return c;
38748 /* OpenMP 2.5:
38749 num_threads ( expression ) */
38751 static tree
38752 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
38753 location_t location)
38755 tree t, c;
38757 matching_parens parens;
38758 if (!parens.require_open (parser))
38759 return list;
38761 t = cp_parser_assignment_expression (parser);
38763 if (t == 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_THREADS,
38770 "num_threads", location);
38772 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
38773 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
38774 OMP_CLAUSE_CHAIN (c) = list;
38776 return c;
38779 /* OpenMP 4.5:
38780 num_tasks ( expression )
38782 OpenMP 5.1:
38783 num_tasks ( strict : expression ) */
38785 static tree
38786 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
38787 location_t location)
38789 tree t, c;
38791 matching_parens parens;
38792 if (!parens.require_open (parser))
38793 return list;
38795 bool strict = false;
38796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38797 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38800 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38802 strict = true;
38803 cp_lexer_consume_token (parser->lexer);
38804 cp_lexer_consume_token (parser->lexer);
38808 t = cp_parser_assignment_expression (parser);
38810 if (t == error_mark_node
38811 || !parens.require_close (parser))
38812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38813 /*or_comma=*/false,
38814 /*consume_paren=*/true);
38816 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
38817 "num_tasks", location);
38819 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
38820 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
38821 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
38822 OMP_CLAUSE_CHAIN (c) = list;
38824 return c;
38827 /* OpenMP 4.5:
38828 grainsize ( expression )
38830 OpenMP 5.1:
38831 grainsize ( strict : expression ) */
38833 static tree
38834 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
38835 location_t location)
38837 tree t, c;
38839 matching_parens parens;
38840 if (!parens.require_open (parser))
38841 return list;
38843 bool strict = false;
38844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38845 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38848 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38850 strict = true;
38851 cp_lexer_consume_token (parser->lexer);
38852 cp_lexer_consume_token (parser->lexer);
38856 t = cp_parser_assignment_expression (parser);
38858 if (t == error_mark_node
38859 || !parens.require_close (parser))
38860 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38861 /*or_comma=*/false,
38862 /*consume_paren=*/true);
38864 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
38865 "grainsize", location);
38867 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
38868 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
38869 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
38870 OMP_CLAUSE_CHAIN (c) = list;
38872 return c;
38875 /* OpenMP 4.5:
38876 priority ( expression ) */
38878 static tree
38879 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
38880 location_t location)
38882 tree t, c;
38884 matching_parens parens;
38885 if (!parens.require_open (parser))
38886 return list;
38888 t = cp_parser_assignment_expression (parser);
38890 if (t == error_mark_node
38891 || !parens.require_close (parser))
38892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38893 /*or_comma=*/false,
38894 /*consume_paren=*/true);
38896 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
38897 "priority", location);
38899 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
38900 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
38901 OMP_CLAUSE_CHAIN (c) = list;
38903 return c;
38906 /* OpenMP 4.5:
38907 hint ( expression ) */
38909 static tree
38910 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
38912 tree t, c;
38914 matching_parens parens;
38915 if (!parens.require_open (parser))
38916 return list;
38918 t = cp_parser_assignment_expression (parser);
38920 if (t != error_mark_node)
38922 t = fold_non_dependent_expr (t);
38923 if (!value_dependent_expression_p (t)
38924 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
38925 || !tree_fits_shwi_p (t)
38926 || tree_int_cst_sgn (t) == -1))
38927 error_at (location, "expected constant integer expression with "
38928 "valid sync-hint value");
38930 if (t == error_mark_node
38931 || !parens.require_close (parser))
38932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38933 /*or_comma=*/false,
38934 /*consume_paren=*/true);
38935 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
38937 c = build_omp_clause (location, OMP_CLAUSE_HINT);
38938 OMP_CLAUSE_HINT_EXPR (c) = t;
38939 OMP_CLAUSE_CHAIN (c) = list;
38941 return c;
38944 /* OpenMP 5.1:
38945 filter ( integer-expression ) */
38947 static tree
38948 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
38950 tree t, c;
38952 matching_parens parens;
38953 if (!parens.require_open (parser))
38954 return list;
38956 t = cp_parser_assignment_expression (parser);
38958 if (t == error_mark_node
38959 || !parens.require_close (parser))
38960 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38961 /*or_comma=*/false,
38962 /*consume_paren=*/true);
38963 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38965 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38966 OMP_CLAUSE_FILTER_EXPR (c) = t;
38967 OMP_CLAUSE_CHAIN (c) = list;
38969 return c;
38972 /* OpenMP 4.5:
38973 defaultmap ( tofrom : scalar )
38975 OpenMP 5.0:
38976 defaultmap ( implicit-behavior [ : variable-category ] ) */
38978 static tree
38979 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38980 location_t location)
38982 tree c, id;
38983 const char *p;
38984 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38985 enum omp_clause_defaultmap_kind category
38986 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38988 matching_parens parens;
38989 if (!parens.require_open (parser))
38990 return list;
38992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38993 p = "default";
38994 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38996 invalid_behavior:
38997 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38998 "%<tofrom%>, %<firstprivate%>, %<none%> "
38999 "or %<default%>");
39000 goto out_err;
39002 else
39004 id = cp_lexer_peek_token (parser->lexer)->u.value;
39005 p = IDENTIFIER_POINTER (id);
39008 switch (p[0])
39010 case 'a':
39011 if (strcmp ("alloc", p) == 0)
39012 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39013 else
39014 goto invalid_behavior;
39015 break;
39017 case 'd':
39018 if (strcmp ("default", p) == 0)
39019 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39020 else
39021 goto invalid_behavior;
39022 break;
39024 case 'f':
39025 if (strcmp ("firstprivate", p) == 0)
39026 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39027 else if (strcmp ("from", p) == 0)
39028 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39029 else
39030 goto invalid_behavior;
39031 break;
39033 case 'n':
39034 if (strcmp ("none", p) == 0)
39035 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39036 else
39037 goto invalid_behavior;
39038 break;
39040 case 'p':
39041 if (strcmp ("present", p) == 0)
39042 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39043 else
39044 goto invalid_behavior;
39045 break;
39047 case 't':
39048 if (strcmp ("tofrom", p) == 0)
39049 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39050 else if (strcmp ("to", p) == 0)
39051 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39052 else
39053 goto invalid_behavior;
39054 break;
39056 default:
39057 goto invalid_behavior;
39059 cp_lexer_consume_token (parser->lexer);
39061 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39063 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39064 goto out_err;
39066 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39068 invalid_category:
39069 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39070 "%<all%>");
39071 goto out_err;
39073 id = cp_lexer_peek_token (parser->lexer)->u.value;
39074 p = IDENTIFIER_POINTER (id);
39076 switch (p[0])
39078 case 'a':
39079 if (strcmp ("aggregate", p) == 0)
39080 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39081 else if (strcmp ("all", p) == 0)
39082 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39083 else
39084 goto invalid_category;
39085 break;
39087 case 'p':
39088 if (strcmp ("pointer", p) == 0)
39089 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39090 else
39091 goto invalid_category;
39092 break;
39094 case 's':
39095 if (strcmp ("scalar", p) == 0)
39096 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39097 else
39098 goto invalid_category;
39099 break;
39101 default:
39102 goto invalid_category;
39105 cp_lexer_consume_token (parser->lexer);
39107 if (!parens.require_close (parser))
39108 goto out_err;
39110 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39111 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39112 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39113 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39114 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39115 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39116 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39117 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39118 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39120 enum omp_clause_defaultmap_kind cat = category;
39121 location_t loc = OMP_CLAUSE_LOCATION (c);
39122 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39123 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39124 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39125 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39126 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39127 p = NULL;
39128 switch (cat)
39130 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39131 p = NULL;
39132 break;
39133 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39134 p = "all";
39135 break;
39136 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39137 p = "aggregate";
39138 break;
39139 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39140 p = "pointer";
39141 break;
39142 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39143 p = "scalar";
39144 break;
39145 default:
39146 gcc_unreachable ();
39148 if (p)
39149 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39151 else
39152 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39153 "category");
39154 break;
39157 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39158 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39159 OMP_CLAUSE_CHAIN (c) = list;
39160 return c;
39162 out_err:
39163 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39164 /*or_comma=*/false,
39165 /*consume_paren=*/true);
39166 return list;
39169 /* OpenMP 5.0:
39170 order ( concurrent )
39172 OpenMP 5.1:
39173 order ( order-modifier : concurrent )
39175 order-modifier:
39176 reproducible
39177 unconstrained */
39179 static tree
39180 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39182 tree c, id;
39183 const char *p;
39184 bool unconstrained = false;
39185 bool reproducible = false;
39187 matching_parens parens;
39188 if (!parens.require_open (parser))
39189 return list;
39191 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39192 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39194 id = cp_lexer_peek_token (parser->lexer)->u.value;
39195 p = IDENTIFIER_POINTER (id);
39196 if (strcmp (p, "unconstrained") == 0)
39197 unconstrained = true;
39198 else if (strcmp (p, "reproducible") == 0)
39199 reproducible = true;
39200 else
39202 cp_parser_error (parser, "expected %<reproducible%> or "
39203 "%<unconstrained%>");
39204 goto out_err;
39206 cp_lexer_consume_token (parser->lexer);
39207 cp_lexer_consume_token (parser->lexer);
39209 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39211 cp_parser_error (parser, "expected %<concurrent%>");
39212 goto out_err;
39214 else
39216 id = cp_lexer_peek_token (parser->lexer)->u.value;
39217 p = IDENTIFIER_POINTER (id);
39219 if (strcmp (p, "concurrent") != 0)
39221 cp_parser_error (parser, "expected %<concurrent%>");
39222 goto out_err;
39224 cp_lexer_consume_token (parser->lexer);
39225 if (!parens.require_close (parser))
39226 goto out_err;
39228 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39229 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39230 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39231 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39232 OMP_CLAUSE_CHAIN (c) = list;
39233 return c;
39235 out_err:
39236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39237 /*or_comma=*/false,
39238 /*consume_paren=*/true);
39239 return list;
39242 /* OpenMP 5.0:
39243 bind ( teams | parallel | thread ) */
39245 static tree
39246 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39247 location_t location)
39249 tree c;
39250 const char *p;
39251 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39253 matching_parens parens;
39254 if (!parens.require_open (parser))
39255 return list;
39257 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39259 invalid:
39260 cp_parser_error (parser,
39261 "expected %<teams%>, %<parallel%> or %<thread%>");
39262 goto out_err;
39264 else
39266 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39267 p = IDENTIFIER_POINTER (id);
39269 if (strcmp (p, "teams") == 0)
39270 kind = OMP_CLAUSE_BIND_TEAMS;
39271 else if (strcmp (p, "parallel") == 0)
39272 kind = OMP_CLAUSE_BIND_PARALLEL;
39273 else if (strcmp (p, "thread") != 0)
39274 goto invalid;
39275 cp_lexer_consume_token (parser->lexer);
39276 if (!parens.require_close (parser))
39277 goto out_err;
39279 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39280 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39281 OMP_CLAUSE_BIND_KIND (c) = kind;
39282 OMP_CLAUSE_CHAIN (c) = list;
39283 return c;
39285 out_err:
39286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39287 /*or_comma=*/false,
39288 /*consume_paren=*/true);
39289 return list;
39292 /* OpenMP 2.5:
39293 ordered
39295 OpenMP 4.5:
39296 ordered ( constant-expression ) */
39298 static tree
39299 cp_parser_omp_clause_ordered (cp_parser *parser,
39300 tree list, location_t location)
39302 tree c, num = NULL_TREE;
39303 HOST_WIDE_INT n;
39305 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39306 "ordered", location);
39308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39310 matching_parens parens;
39311 parens.consume_open (parser);
39313 num = cp_parser_constant_expression (parser);
39315 if (!parens.require_close (parser))
39316 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39317 /*or_comma=*/false,
39318 /*consume_paren=*/true);
39320 if (num == error_mark_node)
39321 return list;
39322 num = fold_non_dependent_expr (num);
39323 if (!tree_fits_shwi_p (num)
39324 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39325 || (n = tree_to_shwi (num)) <= 0
39326 || (int) n != n)
39328 error_at (location,
39329 "ordered argument needs positive constant integer "
39330 "expression");
39331 return list;
39335 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39336 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39337 OMP_CLAUSE_CHAIN (c) = list;
39338 return c;
39341 /* OpenMP 2.5:
39342 reduction ( reduction-operator : variable-list )
39344 reduction-operator:
39345 One of: + * - & ^ | && ||
39347 OpenMP 3.1:
39349 reduction-operator:
39350 One of: + * - & ^ | && || min max
39352 OpenMP 4.0:
39354 reduction-operator:
39355 One of: + * - & ^ | && ||
39356 id-expression
39358 OpenMP 5.0:
39359 reduction ( reduction-modifier, reduction-operator : variable-list )
39360 in_reduction ( reduction-operator : variable-list )
39361 task_reduction ( reduction-operator : variable-list ) */
39363 static tree
39364 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39365 bool is_omp, tree list)
39367 enum tree_code code = ERROR_MARK;
39368 tree nlist, c, id = NULL_TREE;
39369 bool task = false;
39370 bool inscan = false;
39372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39373 return list;
39375 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39378 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39380 cp_lexer_consume_token (parser->lexer);
39381 cp_lexer_consume_token (parser->lexer);
39383 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39384 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39387 const char *p = IDENTIFIER_POINTER (id);
39388 if (strcmp (p, "task") == 0)
39389 task = true;
39390 else if (strcmp (p, "inscan") == 0)
39391 inscan = true;
39392 if (task || inscan)
39394 cp_lexer_consume_token (parser->lexer);
39395 cp_lexer_consume_token (parser->lexer);
39400 switch (cp_lexer_peek_token (parser->lexer)->type)
39402 case CPP_PLUS: code = PLUS_EXPR; break;
39403 case CPP_MULT: code = MULT_EXPR; break;
39404 case CPP_MINUS: code = MINUS_EXPR; break;
39405 case CPP_AND: code = BIT_AND_EXPR; break;
39406 case CPP_XOR: code = BIT_XOR_EXPR; break;
39407 case CPP_OR: code = BIT_IOR_EXPR; break;
39408 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39409 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39410 default: break;
39413 if (code != ERROR_MARK)
39414 cp_lexer_consume_token (parser->lexer);
39415 else
39417 bool saved_colon_corrects_to_scope_p;
39418 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39419 parser->colon_corrects_to_scope_p = false;
39420 id = cp_parser_id_expression (parser, /*template_p=*/false,
39421 /*check_dependency_p=*/true,
39422 /*template_p=*/NULL,
39423 /*declarator_p=*/false,
39424 /*optional_p=*/false);
39425 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39426 if (identifier_p (id))
39428 const char *p = IDENTIFIER_POINTER (id);
39430 if (strcmp (p, "min") == 0)
39431 code = MIN_EXPR;
39432 else if (strcmp (p, "max") == 0)
39433 code = MAX_EXPR;
39434 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39435 code = PLUS_EXPR;
39436 else if (id == ovl_op_identifier (false, MULT_EXPR))
39437 code = MULT_EXPR;
39438 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39439 code = MINUS_EXPR;
39440 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39441 code = BIT_AND_EXPR;
39442 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39443 code = BIT_IOR_EXPR;
39444 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39445 code = BIT_XOR_EXPR;
39446 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39447 code = TRUTH_ANDIF_EXPR;
39448 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39449 code = TRUTH_ORIF_EXPR;
39450 id = omp_reduction_id (code, id, NULL_TREE);
39451 tree scope = parser->scope;
39452 if (scope)
39453 id = build_qualified_name (NULL_TREE, scope, id, false);
39454 parser->scope = NULL_TREE;
39455 parser->qualifying_scope = NULL_TREE;
39456 parser->object_scope = NULL_TREE;
39458 else
39460 error ("invalid reduction-identifier");
39461 resync_fail:
39462 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39463 /*or_comma=*/false,
39464 /*consume_paren=*/true);
39465 return list;
39469 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39470 goto resync_fail;
39472 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39473 NULL);
39474 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39476 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39477 if (task)
39478 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39479 else if (inscan)
39480 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39481 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39484 return nlist;
39487 /* OpenMP 2.5:
39488 schedule ( schedule-kind )
39489 schedule ( schedule-kind , expression )
39491 schedule-kind:
39492 static | dynamic | guided | runtime | auto
39494 OpenMP 4.5:
39495 schedule ( schedule-modifier : schedule-kind )
39496 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39498 schedule-modifier:
39499 simd
39500 monotonic
39501 nonmonotonic */
39503 static tree
39504 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39506 tree c, t;
39507 int modifiers = 0, nmodifiers = 0;
39509 matching_parens parens;
39510 if (!parens.require_open (parser))
39511 return list;
39513 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39515 location_t comma = UNKNOWN_LOCATION;
39516 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39518 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39519 const char *p = IDENTIFIER_POINTER (id);
39520 if (strcmp ("simd", p) == 0)
39521 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39522 else if (strcmp ("monotonic", p) == 0)
39523 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39524 else if (strcmp ("nonmonotonic", p) == 0)
39525 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39526 else
39527 break;
39528 comma = UNKNOWN_LOCATION;
39529 cp_lexer_consume_token (parser->lexer);
39530 if (nmodifiers++ == 0
39531 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39533 comma = cp_lexer_peek_token (parser->lexer)->location;
39534 cp_lexer_consume_token (parser->lexer);
39536 else
39538 cp_parser_require (parser, CPP_COLON, RT_COLON);
39539 break;
39542 if (comma != UNKNOWN_LOCATION)
39543 error_at (comma, "expected %<:%>");
39545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39548 const char *p = IDENTIFIER_POINTER (id);
39550 switch (p[0])
39552 case 'd':
39553 if (strcmp ("dynamic", p) != 0)
39554 goto invalid_kind;
39555 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39556 break;
39558 case 'g':
39559 if (strcmp ("guided", p) != 0)
39560 goto invalid_kind;
39561 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39562 break;
39564 case 'r':
39565 if (strcmp ("runtime", p) != 0)
39566 goto invalid_kind;
39567 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39568 break;
39570 default:
39571 goto invalid_kind;
39574 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39575 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39576 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39577 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39578 else
39579 goto invalid_kind;
39580 cp_lexer_consume_token (parser->lexer);
39582 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39583 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39584 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39585 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39587 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39588 "specified");
39589 modifiers = 0;
39592 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39594 cp_token *token;
39595 cp_lexer_consume_token (parser->lexer);
39597 token = cp_lexer_peek_token (parser->lexer);
39598 t = cp_parser_assignment_expression (parser);
39600 if (t == error_mark_node)
39601 goto resync_fail;
39602 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
39603 error_at (token->location, "schedule %<runtime%> does not take "
39604 "a %<chunk_size%> parameter");
39605 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
39606 error_at (token->location, "schedule %<auto%> does not take "
39607 "a %<chunk_size%> parameter");
39608 else
39609 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
39611 if (!parens.require_close (parser))
39612 goto resync_fail;
39614 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39615 goto resync_fail;
39617 OMP_CLAUSE_SCHEDULE_KIND (c)
39618 = (enum omp_clause_schedule_kind)
39619 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
39621 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
39622 OMP_CLAUSE_CHAIN (c) = list;
39623 return c;
39625 invalid_kind:
39626 cp_parser_error (parser, "invalid schedule kind");
39627 resync_fail:
39628 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39629 /*or_comma=*/false,
39630 /*consume_paren=*/true);
39631 return list;
39634 /* OpenMP 3.0:
39635 untied */
39637 static tree
39638 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
39639 tree list, location_t location)
39641 tree c;
39643 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
39645 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
39646 OMP_CLAUSE_CHAIN (c) = list;
39647 return c;
39650 /* OpenMP 4.0:
39651 inbranch
39652 notinbranch */
39654 static tree
39655 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
39656 tree list, location_t location)
39658 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39659 tree c = build_omp_clause (location, code);
39660 OMP_CLAUSE_CHAIN (c) = list;
39661 return c;
39664 /* OpenMP 4.0:
39665 parallel
39667 sections
39668 taskgroup */
39670 static tree
39671 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
39672 enum omp_clause_code code,
39673 tree list, location_t location)
39675 tree c = build_omp_clause (location, code);
39676 OMP_CLAUSE_CHAIN (c) = list;
39677 return c;
39680 /* OpenMP 4.5:
39681 nogroup */
39683 static tree
39684 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
39685 tree list, location_t location)
39687 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
39688 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
39689 OMP_CLAUSE_CHAIN (c) = list;
39690 return c;
39693 /* OpenMP 4.5:
39694 simd
39695 threads */
39697 static tree
39698 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
39699 enum omp_clause_code code,
39700 tree list, location_t location)
39702 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39703 tree c = build_omp_clause (location, code);
39704 OMP_CLAUSE_CHAIN (c) = list;
39705 return c;
39708 /* OpenMP 4.0:
39709 num_teams ( expression )
39711 OpenMP 5.1:
39712 num_teams ( expression : expression ) */
39714 static tree
39715 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
39716 location_t location)
39718 tree upper, lower = NULL_TREE, c;
39720 matching_parens parens;
39721 if (!parens.require_open (parser))
39722 return list;
39724 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39725 parser->colon_corrects_to_scope_p = false;
39726 upper = cp_parser_assignment_expression (parser);
39727 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39729 if (upper != error_mark_node
39730 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39732 lower = upper;
39733 cp_lexer_consume_token (parser->lexer);
39734 upper = cp_parser_assignment_expression (parser);
39737 if (upper == error_mark_node
39738 || !parens.require_close (parser))
39739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39740 /*or_comma=*/false,
39741 /*consume_paren=*/true);
39743 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
39744 "num_teams", location);
39746 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
39747 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
39748 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
39749 OMP_CLAUSE_CHAIN (c) = list;
39751 return c;
39754 /* OpenMP 4.0:
39755 thread_limit ( expression ) */
39757 static tree
39758 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
39759 location_t location)
39761 tree t, c;
39763 matching_parens parens;
39764 if (!parens.require_open (parser))
39765 return list;
39767 t = cp_parser_assignment_expression (parser);
39769 if (t == error_mark_node
39770 || !parens.require_close (parser))
39771 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39772 /*or_comma=*/false,
39773 /*consume_paren=*/true);
39775 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
39776 "thread_limit", location);
39778 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
39779 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
39780 OMP_CLAUSE_CHAIN (c) = list;
39782 return c;
39785 /* OpenMP 4.0:
39786 aligned ( variable-list )
39787 aligned ( variable-list : constant-expression ) */
39789 static tree
39790 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
39792 tree nlist, c, alignment = NULL_TREE;
39793 bool colon;
39795 matching_parens parens;
39796 if (!parens.require_open (parser))
39797 return list;
39799 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
39800 &colon);
39802 if (colon)
39804 alignment = cp_parser_constant_expression (parser);
39806 if (!parens.require_close (parser))
39807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39808 /*or_comma=*/false,
39809 /*consume_paren=*/true);
39811 if (alignment == error_mark_node)
39812 alignment = NULL_TREE;
39815 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39816 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
39818 return nlist;
39821 /* OpenMP 5.0:
39822 allocate ( variable-list )
39823 allocate ( expression : variable-list )
39825 OpenMP 5.1:
39826 allocate ( allocator-modifier : variable-list )
39827 allocate ( allocator-modifier , allocator-modifier : variable-list )
39829 allocator-modifier:
39830 allocator ( expression )
39831 align ( expression ) */
39833 static tree
39834 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
39836 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
39837 bool colon, has_modifiers = false;
39839 matching_parens parens;
39840 if (!parens.require_open (parser))
39841 return list;
39843 cp_parser_parse_tentatively (parser);
39844 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39845 parser->colon_corrects_to_scope_p = false;
39846 for (int mod = 0; mod < 2; mod++)
39847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39848 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39851 const char *p = IDENTIFIER_POINTER (id);
39852 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
39853 break;
39854 cp_lexer_consume_token (parser->lexer);
39855 matching_parens parens2;
39856 if (!parens2.require_open (parser))
39857 break;
39858 if (strcmp (p, "allocator") == 0)
39860 if (allocator != NULL_TREE)
39861 break;
39862 allocator = cp_parser_assignment_expression (parser);
39864 else
39866 if (align != NULL_TREE)
39867 break;
39868 align = cp_parser_assignment_expression (parser);
39870 if (!parens2.require_close (parser))
39871 break;
39872 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39874 has_modifiers = true;
39875 break;
39877 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
39878 break;
39879 cp_lexer_consume_token (parser->lexer);
39881 else
39882 break;
39883 if (!has_modifiers)
39885 cp_parser_abort_tentative_parse (parser);
39886 align = NULL_TREE;
39887 allocator = NULL_TREE;
39888 cp_parser_parse_tentatively (parser);
39889 allocator = cp_parser_assignment_expression (parser);
39891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39892 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39894 cp_parser_parse_definitely (parser);
39895 cp_lexer_consume_token (parser->lexer);
39896 if (allocator == error_mark_node)
39897 allocator = NULL_TREE;
39898 if (align == error_mark_node)
39899 align = NULL_TREE;
39901 else
39903 cp_parser_abort_tentative_parse (parser);
39904 allocator = NULL_TREE;
39905 align = NULL_TREE;
39908 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
39909 &colon);
39911 if (allocator || align)
39912 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39914 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
39915 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
39918 return nlist;
39921 /* OpenMP 2.5:
39922 lastprivate ( variable-list )
39924 OpenMP 5.0:
39925 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39927 static tree
39928 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
39930 bool conditional = false;
39932 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39933 return list;
39935 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39936 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39938 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39939 const char *p = IDENTIFIER_POINTER (id);
39941 if (strcmp ("conditional", p) == 0)
39943 conditional = true;
39944 cp_lexer_consume_token (parser->lexer);
39945 cp_lexer_consume_token (parser->lexer);
39949 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
39950 list, NULL);
39952 if (conditional)
39953 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39954 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
39955 return nlist;
39958 /* OpenMP 4.0:
39959 linear ( variable-list )
39960 linear ( variable-list : expression )
39962 OpenMP 4.5:
39963 linear ( modifier ( variable-list ) )
39964 linear ( modifier ( variable-list ) : expression )
39966 modifier:
39969 uval
39971 OpenMP 5.2:
39972 linear ( variable-list : modifiers-list )
39974 modifiers:
39977 uval
39978 step ( expression ) */
39980 static tree
39981 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39982 bool declare_simd)
39984 tree nlist, c, step = integer_one_node;
39985 bool colon;
39986 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39987 bool old_linear_modifier = false;
39989 matching_parens parens;
39990 if (!parens.require_open (parser))
39991 return list;
39993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39995 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39996 const char *p = IDENTIFIER_POINTER (id);
39998 if (strcmp ("ref", p) == 0)
39999 kind = OMP_CLAUSE_LINEAR_REF;
40000 else if (strcmp ("val", p) == 0)
40001 kind = OMP_CLAUSE_LINEAR_VAL;
40002 else if (strcmp ("uval", p) == 0)
40003 kind = OMP_CLAUSE_LINEAR_UVAL;
40004 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40006 cp_lexer_consume_token (parser->lexer);
40007 old_linear_modifier = true;
40009 else
40010 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40013 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40014 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40015 &colon);
40016 else
40018 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40019 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40020 if (colon)
40021 cp_parser_require (parser, CPP_COLON, RT_COLON);
40022 else if (!parens.require_close (parser))
40023 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40024 /*or_comma=*/false,
40025 /*consume_paren=*/true);
40028 if (colon)
40030 bool has_modifiers = false;
40031 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40032 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40034 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40035 const char *p = IDENTIFIER_POINTER (id);
40036 size_t pos = 0;
40037 if (strcmp ("ref", p) == 0
40038 || strcmp ("val", p) == 0
40039 || strcmp ("uval", p) == 0)
40040 pos = 2;
40041 else if (strcmp ("step", p) == 0
40042 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40044 pos = cp_parser_skip_balanced_tokens (parser, 2);
40045 if (pos == 2)
40046 pos = 0;
40048 if (pos != 0
40049 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
40050 || cp_lexer_nth_token_is (parser->lexer, pos,
40051 CPP_CLOSE_PAREN)))
40052 has_modifiers = true;
40055 step = NULL_TREE;
40056 if (has_modifiers)
40058 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40061 const char *p = IDENTIFIER_POINTER (id);
40062 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
40063 if (strcmp ("ref", p) == 0)
40064 nkind = OMP_CLAUSE_LINEAR_REF;
40065 else if (strcmp ("val", p) == 0)
40066 nkind = OMP_CLAUSE_LINEAR_VAL;
40067 else if (strcmp ("uval", p) == 0)
40068 nkind = OMP_CLAUSE_LINEAR_UVAL;
40069 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
40071 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
40072 error_at (cp_lexer_peek_token (parser->lexer)->location,
40073 "multiple linear modifiers");
40074 kind = nkind;
40075 cp_lexer_consume_token (parser->lexer);
40077 else if (strcmp ("step", p) == 0)
40079 location_t step_loc
40080 = cp_lexer_peek_token (parser->lexer)->location;
40081 cp_lexer_consume_token (parser->lexer);
40082 matching_parens parens2;
40083 if (parens2.require_open (parser))
40085 if (step)
40086 error_at (step_loc, "multiple %<step%> modifiers");
40087 if (declare_simd
40088 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40089 && cp_lexer_nth_token_is (parser->lexer, 2,
40090 CPP_CLOSE_PAREN))
40092 cp_token *token
40093 = cp_lexer_peek_token (parser->lexer);
40094 location_t tok_loc = token->location;
40095 cp_parser_parse_tentatively (parser);
40096 step = cp_parser_id_expression (parser, false, true,
40097 NULL, false, false);
40098 if (step != error_mark_node)
40099 step = cp_parser_lookup_name_simple (parser, step,
40100 tok_loc);
40101 if (step == error_mark_node)
40103 step = NULL_TREE;
40104 cp_parser_abort_tentative_parse (parser);
40106 else if (!cp_parser_parse_definitely (parser))
40107 step = NULL_TREE;
40109 if (!step)
40110 step = cp_parser_assignment_expression (parser);
40111 if (!parens2.require_close (parser))
40112 cp_parser_skip_to_closing_parenthesis (parser, true,
40113 false, true);
40115 else
40116 break;
40118 else
40119 break;
40120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40122 cp_lexer_consume_token (parser->lexer);
40123 continue;
40125 break;
40127 if (!step)
40128 step = integer_one_node;
40130 else if (declare_simd
40131 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40132 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40134 cp_token *token = cp_lexer_peek_token (parser->lexer);
40135 cp_parser_parse_tentatively (parser);
40136 step = cp_parser_id_expression (parser, /*template_p=*/false,
40137 /*check_dependency_p=*/true,
40138 /*template_p=*/NULL,
40139 /*declarator_p=*/false,
40140 /*optional_p=*/false);
40141 if (step != error_mark_node)
40142 step = cp_parser_lookup_name_simple (parser, step, token->location);
40143 if (step == error_mark_node)
40145 step = NULL_TREE;
40146 cp_parser_abort_tentative_parse (parser);
40148 else if (!cp_parser_parse_definitely (parser))
40149 step = NULL_TREE;
40151 if (!step)
40152 step = cp_parser_assignment_expression (parser);
40154 if (!parens.require_close (parser))
40155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40156 /*or_comma=*/false,
40157 /*consume_paren=*/true);
40159 if (step == error_mark_node)
40160 return list;
40163 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40165 OMP_CLAUSE_LINEAR_STEP (c) = step;
40166 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40167 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40170 return nlist;
40173 /* OpenMP 4.0:
40174 safelen ( constant-expression ) */
40176 static tree
40177 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40178 location_t location)
40180 tree t, c;
40182 matching_parens parens;
40183 if (!parens.require_open (parser))
40184 return list;
40186 t = cp_parser_constant_expression (parser);
40188 if (t == error_mark_node
40189 || !parens.require_close (parser))
40190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40191 /*or_comma=*/false,
40192 /*consume_paren=*/true);
40194 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40196 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40197 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40198 OMP_CLAUSE_CHAIN (c) = list;
40200 return c;
40203 /* OpenMP 4.0:
40204 simdlen ( constant-expression ) */
40206 static tree
40207 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40208 location_t location)
40210 tree t, c;
40212 matching_parens parens;
40213 if (!parens.require_open (parser))
40214 return list;
40216 t = cp_parser_constant_expression (parser);
40218 if (t == error_mark_node
40219 || !parens.require_close (parser))
40220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40221 /*or_comma=*/false,
40222 /*consume_paren=*/true);
40224 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40226 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40227 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40228 OMP_CLAUSE_CHAIN (c) = list;
40230 return c;
40233 /* OpenMP 4.5:
40234 vec:
40235 identifier [+/- integer]
40236 vec , identifier [+/- integer]
40239 static tree
40240 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40241 tree list, bool depend_p)
40243 tree vec = NULL;
40245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40247 cp_parser_error (parser, "expected identifier");
40248 return list;
40251 if (!depend_p)
40253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40254 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40255 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40256 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40257 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40259 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40260 if (integer_onep (val))
40262 cp_lexer_consume_token (parser->lexer);
40263 cp_lexer_consume_token (parser->lexer);
40264 cp_lexer_consume_token (parser->lexer);
40265 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40266 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40267 OMP_CLAUSE_CHAIN (u) = list;
40268 return u;
40273 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40275 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40276 tree t, identifier = cp_parser_identifier (parser);
40277 tree addend = NULL;
40279 if (identifier == error_mark_node)
40280 t = error_mark_node;
40281 else
40283 t = cp_parser_lookup_name_simple
40284 (parser, identifier,
40285 cp_lexer_peek_token (parser->lexer)->location);
40286 if (t == error_mark_node)
40287 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40288 id_loc);
40291 bool neg = false;
40292 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40293 neg = true;
40294 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40296 addend = integer_zero_node;
40297 goto add_to_vector;
40299 cp_lexer_consume_token (parser->lexer);
40301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40303 cp_parser_error (parser, "expected integer");
40304 return list;
40307 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40308 if (TREE_CODE (addend) != INTEGER_CST)
40310 cp_parser_error (parser, "expected integer");
40311 return list;
40313 cp_lexer_consume_token (parser->lexer);
40315 add_to_vector:
40316 if (t != error_mark_node)
40318 vec = tree_cons (addend, t, vec);
40319 if (neg)
40320 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40324 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40325 break;
40327 cp_lexer_consume_token (parser->lexer);
40330 if (vec)
40332 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40333 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40334 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40335 OMP_CLAUSE_DECL (u) = nreverse (vec);
40336 OMP_CLAUSE_CHAIN (u) = list;
40337 return u;
40339 return list;
40342 /* OpenMP 5.0:
40343 detach ( event-handle ) */
40345 static tree
40346 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40348 matching_parens parens;
40350 if (!parens.require_open (parser))
40351 return list;
40353 cp_token *token;
40354 tree name, decl;
40356 token = cp_lexer_peek_token (parser->lexer);
40357 name = cp_parser_id_expression (parser, /*template_p=*/false,
40358 /*check_dependency_p=*/true,
40359 /*template_p=*/NULL,
40360 /*declarator_p=*/false,
40361 /*optional_p=*/false);
40362 if (name == error_mark_node)
40363 decl = error_mark_node;
40364 else
40366 if (identifier_p (name))
40367 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40368 else
40369 decl = name;
40370 if (decl == error_mark_node)
40371 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40372 token->location);
40375 if (decl == error_mark_node
40376 || !parens.require_close (parser))
40377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40378 /*or_comma=*/false,
40379 /*consume_paren=*/true);
40381 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40382 OMP_CLAUSE_DECL (u) = decl;
40383 OMP_CLAUSE_CHAIN (u) = list;
40385 return u;
40388 /* OpenMP 5.0:
40389 iterators ( iterators-definition )
40391 iterators-definition:
40392 iterator-specifier
40393 iterator-specifier , iterators-definition
40395 iterator-specifier:
40396 identifier = range-specification
40397 iterator-type identifier = range-specification
40399 range-specification:
40400 begin : end
40401 begin : end : step */
40403 static tree
40404 cp_parser_omp_iterators (cp_parser *parser)
40406 tree ret = NULL_TREE, *last = &ret;
40407 cp_lexer_consume_token (parser->lexer);
40409 matching_parens parens;
40410 if (!parens.require_open (parser))
40411 return error_mark_node;
40413 bool saved_colon_corrects_to_scope_p
40414 = parser->colon_corrects_to_scope_p;
40415 bool saved_colon_doesnt_start_class_def_p
40416 = parser->colon_doesnt_start_class_def_p;
40420 tree iter_type;
40421 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40423 iter_type = integer_type_node;
40424 else
40426 const char *saved_message
40427 = parser->type_definition_forbidden_message;
40428 parser->type_definition_forbidden_message
40429 = G_("types may not be defined in iterator type");
40431 iter_type = cp_parser_type_id (parser);
40433 parser->type_definition_forbidden_message = saved_message;
40436 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40439 cp_parser_error (parser, "expected identifier");
40440 break;
40443 tree id = cp_parser_identifier (parser);
40444 if (id == error_mark_node)
40445 break;
40447 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40448 break;
40450 parser->colon_corrects_to_scope_p = false;
40451 parser->colon_doesnt_start_class_def_p = true;
40452 tree begin = cp_parser_assignment_expression (parser);
40454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40455 break;
40457 tree end = cp_parser_assignment_expression (parser);
40459 tree step = integer_one_node;
40460 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40462 cp_lexer_consume_token (parser->lexer);
40463 step = cp_parser_assignment_expression (parser);
40466 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40467 DECL_ARTIFICIAL (iter_var) = 1;
40468 DECL_CONTEXT (iter_var) = current_function_decl;
40469 pushdecl (iter_var);
40471 *last = make_tree_vec (6);
40472 TREE_VEC_ELT (*last, 0) = iter_var;
40473 TREE_VEC_ELT (*last, 1) = begin;
40474 TREE_VEC_ELT (*last, 2) = end;
40475 TREE_VEC_ELT (*last, 3) = step;
40476 last = &TREE_CHAIN (*last);
40478 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40480 cp_lexer_consume_token (parser->lexer);
40481 continue;
40483 break;
40485 while (1);
40487 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40488 parser->colon_doesnt_start_class_def_p
40489 = saved_colon_doesnt_start_class_def_p;
40491 if (!parens.require_close (parser))
40492 cp_parser_skip_to_closing_parenthesis (parser,
40493 /*recovering=*/true,
40494 /*or_comma=*/false,
40495 /*consume_paren=*/true);
40497 return ret ? ret : error_mark_node;
40500 /* OpenMP 5.0:
40501 affinity ( [aff-modifier :] variable-list )
40502 aff-modifier:
40503 iterator ( iterators-definition ) */
40505 static tree
40506 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40508 tree nlist, c, iterators = NULL_TREE;
40510 matching_parens parens;
40511 if (!parens.require_open (parser))
40512 return list;
40514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40517 const char *p = IDENTIFIER_POINTER (id);
40518 bool parse_iter = ((strcmp ("iterator", p) == 0)
40519 && (cp_lexer_nth_token_is (parser->lexer, 2,
40520 CPP_OPEN_PAREN)));
40521 if (parse_iter)
40523 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40524 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40526 if (parse_iter)
40528 begin_scope (sk_omp, NULL);
40529 iterators = cp_parser_omp_iterators (parser);
40530 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40532 if (iterators)
40533 poplevel (0, 1, 0);
40534 cp_parser_skip_to_closing_parenthesis (parser,
40535 /*recovering=*/true,
40536 /*or_comma=*/false,
40537 /*consume_paren=*/true);
40538 return list;
40542 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40543 list, NULL);
40544 if (iterators)
40546 tree block = poplevel (1, 1, 0);
40547 if (iterators != error_mark_node)
40549 TREE_VEC_ELT (iterators, 5) = block;
40550 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40551 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40552 OMP_CLAUSE_DECL (c));
40555 return nlist;
40558 /* OpenMP 4.0:
40559 depend ( depend-kind : variable-list )
40561 depend-kind:
40562 in | out | inout
40564 OpenMP 4.5:
40565 depend ( source )
40567 depend ( sink : vec )
40569 OpenMP 5.0:
40570 depend ( depend-modifier , depend-kind: variable-list )
40572 depend-kind:
40573 in | out | inout | mutexinoutset | depobj
40575 depend-modifier:
40576 iterator ( iterators-definition ) */
40578 static tree
40579 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40581 tree nlist, c, iterators = NULL_TREE;
40582 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40583 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40585 matching_parens parens;
40586 if (!parens.require_open (parser))
40587 return list;
40591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40592 goto invalid_kind;
40594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40595 const char *p = IDENTIFIER_POINTER (id);
40597 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
40599 begin_scope (sk_omp, NULL);
40600 iterators = cp_parser_omp_iterators (parser);
40601 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
40602 continue;
40604 if (strcmp ("in", p) == 0)
40605 kind = OMP_CLAUSE_DEPEND_IN;
40606 else if (strcmp ("inout", p) == 0)
40607 kind = OMP_CLAUSE_DEPEND_INOUT;
40608 else if (strcmp ("inoutset", p) == 0)
40609 kind = OMP_CLAUSE_DEPEND_INOUTSET;
40610 else if (strcmp ("mutexinoutset", p) == 0)
40611 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
40612 else if (strcmp ("out", p) == 0)
40613 kind = OMP_CLAUSE_DEPEND_OUT;
40614 else if (strcmp ("depobj", p) == 0)
40615 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
40616 else if (strcmp ("sink", p) == 0)
40617 dkind = OMP_CLAUSE_DOACROSS_SINK;
40618 else if (strcmp ("source", p) == 0)
40619 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
40620 else
40621 goto invalid_kind;
40622 break;
40624 while (1);
40626 cp_lexer_consume_token (parser->lexer);
40628 if (iterators
40629 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
40630 || dkind == OMP_CLAUSE_DOACROSS_SINK))
40632 poplevel (0, 1, 0);
40633 error_at (loc, "%<iterator%> modifier incompatible with %qs",
40634 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
40635 iterators = NULL_TREE;
40638 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
40640 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40641 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
40642 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
40643 OMP_CLAUSE_DECL (c) = NULL_TREE;
40644 OMP_CLAUSE_CHAIN (c) = list;
40645 if (!parens.require_close (parser))
40646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40647 /*or_comma=*/false,
40648 /*consume_paren=*/true);
40649 return c;
40652 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40653 goto resync_fail;
40655 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
40657 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
40658 if (!parens.require_close (parser))
40659 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40660 /*or_comma=*/false,
40661 /*consume_paren=*/true);
40663 else
40665 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
40666 list, NULL);
40668 if (iterators)
40670 tree block = poplevel (1, 1, 0);
40671 if (iterators == error_mark_node)
40672 iterators = NULL_TREE;
40673 else
40674 TREE_VEC_ELT (iterators, 5) = block;
40677 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40679 OMP_CLAUSE_DEPEND_KIND (c) = kind;
40680 if (iterators)
40681 OMP_CLAUSE_DECL (c)
40682 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
40685 return nlist;
40687 invalid_kind:
40688 cp_parser_error (parser, "invalid depend kind");
40689 resync_fail:
40690 if (iterators)
40691 poplevel (0, 1, 0);
40692 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40693 /*or_comma=*/false,
40694 /*consume_paren=*/true);
40695 return list;
40698 /* OpenMP 5.2:
40699 doacross ( source : )
40700 doacross ( source : omp_cur_iteration )
40702 doacross ( sink : vec )
40703 doacross ( sink : omp_cur_iteration - logical_iteration ) */
40705 static tree
40706 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
40708 tree nlist;
40709 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
40711 matching_parens parens;
40712 if (!parens.require_open (parser))
40713 return list;
40715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40717 invalid_kind:
40718 cp_parser_error (parser, "invalid doacross kind");
40719 resync_fail:
40720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40721 /*or_comma=*/false,
40722 /*consume_paren=*/true);
40723 return list;
40726 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40727 const char *p = IDENTIFIER_POINTER (id);
40729 if (strcmp ("sink", p) == 0)
40730 kind = OMP_CLAUSE_DOACROSS_SINK;
40731 else if (strcmp ("source", p) == 0)
40732 kind = OMP_CLAUSE_DOACROSS_SOURCE;
40733 else
40734 goto invalid_kind;
40736 cp_lexer_consume_token (parser->lexer);
40738 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40739 goto resync_fail;
40741 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
40743 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40745 id = cp_lexer_peek_token (parser->lexer)->u.value;
40746 p = IDENTIFIER_POINTER (id);
40747 if (strcmp (p, "omp_cur_iteration") == 0)
40748 cp_lexer_consume_token (parser->lexer);
40750 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40751 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
40752 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
40753 OMP_CLAUSE_CHAIN (nlist) = list;
40755 else
40756 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
40758 if (!parens.require_close (parser))
40759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40760 /*or_comma=*/false,
40761 /*consume_paren=*/true);
40762 return nlist;
40765 /* OpenMP 4.0:
40766 from ( variable-list )
40767 to ( variable-list )
40769 OpenMP 5.1:
40770 from ( [present :] variable-list )
40771 to ( [present :] variable-list ) */
40773 static tree
40774 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
40775 tree list)
40777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40778 return list;
40780 bool present = false;
40781 cp_token *token = cp_lexer_peek_token (parser->lexer);
40783 if (token->type == CPP_NAME
40784 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
40785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40787 present = true;
40788 cp_lexer_consume_token (parser->lexer);
40789 cp_lexer_consume_token (parser->lexer);
40792 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
40793 if (present)
40794 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
40795 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
40797 return nl;
40800 /* OpenMP 4.0:
40801 map ( map-kind : variable-list )
40802 map ( variable-list )
40804 map-kind:
40805 alloc | to | from | tofrom
40807 OpenMP 4.5:
40808 map-kind:
40809 alloc | to | from | tofrom | release | delete
40811 map ( always [,] map-kind: variable-list )
40813 OpenMP 5.0:
40814 map ( [map-type-modifier[,] ...] map-kind: variable-list )
40816 map-type-modifier:
40817 always | close */
40819 static tree
40820 cp_parser_omp_clause_map (cp_parser *parser, tree list)
40822 tree nlist, c;
40823 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
40825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40826 return list;
40828 int pos = 1;
40829 int map_kind_pos = 0;
40830 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
40831 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
40833 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
40835 map_kind_pos = pos;
40836 break;
40839 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
40840 pos++;
40841 pos++;
40844 bool always_modifier = false;
40845 bool close_modifier = false;
40846 bool present_modifier = false;
40847 for (int pos = 1; pos < map_kind_pos; ++pos)
40849 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40850 if (tok->type == CPP_COMMA)
40852 cp_lexer_consume_token (parser->lexer);
40853 continue;
40856 const char *p = IDENTIFIER_POINTER (tok->u.value);
40857 if (strcmp ("always", p) == 0)
40859 if (always_modifier)
40861 cp_parser_error (parser, "too many %<always%> modifiers");
40862 cp_parser_skip_to_closing_parenthesis (parser,
40863 /*recovering=*/true,
40864 /*or_comma=*/false,
40865 /*consume_paren=*/true);
40866 return list;
40868 always_modifier = true;
40870 else if (strcmp ("close", p) == 0)
40872 if (close_modifier)
40874 cp_parser_error (parser, "too many %<close%> modifiers");
40875 cp_parser_skip_to_closing_parenthesis (parser,
40876 /*recovering=*/true,
40877 /*or_comma=*/false,
40878 /*consume_paren=*/true);
40879 return list;
40881 close_modifier = true;
40883 else if (strcmp ("present", p) == 0)
40885 if (present_modifier)
40887 cp_parser_error (parser, "too many %<present%> modifiers");
40888 cp_parser_skip_to_closing_parenthesis (parser,
40889 /*recovering=*/true,
40890 /*or_comma=*/false,
40891 /*consume_paren=*/true);
40892 return list;
40894 present_modifier = true;
40896 else
40898 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
40899 " than %<always%>, %<close%> or %<present%>");
40900 cp_parser_skip_to_closing_parenthesis (parser,
40901 /*recovering=*/true,
40902 /*or_comma=*/false,
40903 /*consume_paren=*/true);
40904 return list;
40907 cp_lexer_consume_token (parser->lexer);
40910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40911 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40914 const char *p = IDENTIFIER_POINTER (id);
40915 int always_present_modifier = always_modifier && present_modifier;
40917 if (strcmp ("alloc", p) == 0)
40918 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
40919 else if (strcmp ("to", p) == 0)
40920 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
40921 : present_modifier ? GOMP_MAP_PRESENT_TO
40922 : always_modifier ? GOMP_MAP_ALWAYS_TO
40923 : GOMP_MAP_TO);
40924 else if (strcmp ("from", p) == 0)
40925 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
40926 : present_modifier ? GOMP_MAP_PRESENT_FROM
40927 : always_modifier ? GOMP_MAP_ALWAYS_FROM
40928 : GOMP_MAP_FROM);
40929 else if (strcmp ("tofrom", p) == 0)
40930 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
40931 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
40932 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
40933 : GOMP_MAP_TOFROM);
40934 else if (strcmp ("release", p) == 0)
40935 kind = GOMP_MAP_RELEASE;
40936 else
40938 cp_parser_error (parser, "invalid map kind");
40939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40940 /*or_comma=*/false,
40941 /*consume_paren=*/true);
40942 return list;
40944 cp_lexer_consume_token (parser->lexer);
40945 cp_lexer_consume_token (parser->lexer);
40947 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
40948 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40950 kind = GOMP_MAP_DELETE;
40951 cp_lexer_consume_token (parser->lexer);
40952 cp_lexer_consume_token (parser->lexer);
40955 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
40956 NULL, true);
40958 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40959 OMP_CLAUSE_SET_MAP_KIND (c, kind);
40961 return nlist;
40964 /* OpenMP 4.0:
40965 device ( expression )
40967 OpenMP 5.0:
40968 device ( [device-modifier :] integer-expression )
40970 device-modifier:
40971 ancestor | device_num */
40973 static tree
40974 cp_parser_omp_clause_device (cp_parser *parser, tree list,
40975 location_t location)
40977 tree t, c;
40978 bool ancestor = false;
40980 matching_parens parens;
40981 if (!parens.require_open (parser))
40982 return list;
40984 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40985 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40987 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40988 const char *p = IDENTIFIER_POINTER (tok->u.value);
40989 if (strcmp ("ancestor", p) == 0)
40991 ancestor = true;
40993 /* A requires directive with the reverse_offload clause must be
40994 specified. */
40995 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
40997 error_at (tok->location, "%<ancestor%> device modifier not "
40998 "preceded by %<requires%> directive "
40999 "with %<reverse_offload%> clause");
41000 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41001 return list;
41004 else if (strcmp ("device_num", p) == 0)
41006 else
41008 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41009 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41010 return list;
41012 cp_lexer_consume_token (parser->lexer);
41013 cp_lexer_consume_token (parser->lexer);
41016 t = cp_parser_assignment_expression (parser);
41018 if (t == error_mark_node
41019 || !parens.require_close (parser))
41020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41021 /*or_comma=*/false,
41022 /*consume_paren=*/true);
41024 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41025 "device", location);
41027 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41028 OMP_CLAUSE_DEVICE_ID (c) = t;
41029 OMP_CLAUSE_CHAIN (c) = list;
41030 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
41032 return c;
41035 /* OpenMP 4.0:
41036 dist_schedule ( static )
41037 dist_schedule ( static , expression ) */
41039 static tree
41040 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
41041 location_t location)
41043 tree c, t;
41045 matching_parens parens;
41046 if (!parens.require_open (parser))
41047 return list;
41049 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
41051 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
41052 goto invalid_kind;
41053 cp_lexer_consume_token (parser->lexer);
41055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41057 cp_lexer_consume_token (parser->lexer);
41059 t = cp_parser_assignment_expression (parser);
41061 if (t == error_mark_node)
41062 goto resync_fail;
41063 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
41065 if (!parens.require_close (parser))
41066 goto resync_fail;
41068 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41069 goto resync_fail;
41071 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
41072 "dist_schedule", location); */
41073 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
41074 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
41075 OMP_CLAUSE_CHAIN (c) = list;
41076 return c;
41078 invalid_kind:
41079 cp_parser_error (parser, "invalid dist_schedule kind");
41080 resync_fail:
41081 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41082 /*or_comma=*/false,
41083 /*consume_paren=*/true);
41084 return list;
41087 /* OpenMP 4.0:
41088 proc_bind ( proc-bind-kind )
41090 proc-bind-kind:
41091 primary | master | close | spread
41092 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41094 static tree
41095 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41096 location_t location)
41098 tree c;
41099 enum omp_clause_proc_bind_kind kind;
41101 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41102 return list;
41104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41106 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41107 const char *p = IDENTIFIER_POINTER (id);
41109 if (strcmp ("primary", p) == 0)
41110 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41111 else if (strcmp ("master", p) == 0)
41112 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41113 else if (strcmp ("close", p) == 0)
41114 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41115 else if (strcmp ("spread", p) == 0)
41116 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41117 else
41118 goto invalid_kind;
41120 else
41121 goto invalid_kind;
41123 cp_lexer_consume_token (parser->lexer);
41124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41125 goto resync_fail;
41127 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41128 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41129 location);
41130 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41131 OMP_CLAUSE_CHAIN (c) = list;
41132 return c;
41134 invalid_kind:
41135 cp_parser_error (parser, "invalid depend kind");
41136 resync_fail:
41137 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41138 /*or_comma=*/false,
41139 /*consume_paren=*/true);
41140 return list;
41143 /* OpenMP 5.0:
41144 device_type ( host | nohost | any ) */
41146 static tree
41147 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41148 location_t location)
41150 tree c;
41151 enum omp_clause_device_type_kind kind;
41153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41154 return list;
41156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41158 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41159 const char *p = IDENTIFIER_POINTER (id);
41161 if (strcmp ("host", p) == 0)
41162 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41163 else if (strcmp ("nohost", p) == 0)
41164 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41165 else if (strcmp ("any", p) == 0)
41166 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41167 else
41168 goto invalid_kind;
41170 else
41171 goto invalid_kind;
41173 cp_lexer_consume_token (parser->lexer);
41174 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41175 goto resync_fail;
41177 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41178 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41179 location);
41180 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41181 OMP_CLAUSE_CHAIN (c) = list;
41182 return c;
41184 invalid_kind:
41185 cp_parser_error (parser, "invalid depend kind");
41186 resync_fail:
41187 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41188 /*or_comma=*/false,
41189 /*consume_paren=*/true);
41190 return list;
41193 /* OpenACC:
41194 async [( int-expr )] */
41196 static tree
41197 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41199 tree c, t;
41200 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41202 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41204 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41206 matching_parens parens;
41207 parens.consume_open (parser);
41209 t = cp_parser_assignment_expression (parser);
41210 if (t == error_mark_node
41211 || !parens.require_close (parser))
41212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41213 /*or_comma=*/false,
41214 /*consume_paren=*/true);
41217 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41219 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41220 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41221 OMP_CLAUSE_CHAIN (c) = list;
41222 list = c;
41224 return list;
41227 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41228 is a bitmask in MASK. Return the list of clauses found. */
41230 static tree
41231 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41232 const char *where, cp_token *pragma_tok,
41233 bool finish_p = true)
41235 tree clauses = NULL;
41236 bool first = true;
41238 /* Don't create location wrapper nodes within OpenACC clauses. */
41239 auto_suppress_location_wrappers sentinel;
41241 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41243 location_t here;
41244 pragma_omp_clause c_kind;
41245 omp_clause_code code;
41246 const char *c_name;
41247 tree prev = clauses;
41249 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41250 cp_lexer_consume_token (parser->lexer);
41252 here = cp_lexer_peek_token (parser->lexer)->location;
41253 c_kind = cp_parser_omp_clause_name (parser);
41255 switch (c_kind)
41257 case PRAGMA_OACC_CLAUSE_ASYNC:
41258 clauses = cp_parser_oacc_clause_async (parser, clauses);
41259 c_name = "async";
41260 break;
41261 case PRAGMA_OACC_CLAUSE_AUTO:
41262 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
41263 clauses);
41264 c_name = "auto";
41265 break;
41266 case PRAGMA_OACC_CLAUSE_ATTACH:
41267 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41268 c_name = "attach";
41269 break;
41270 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41271 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41272 c_name = "collapse";
41273 break;
41274 case PRAGMA_OACC_CLAUSE_COPY:
41275 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41276 c_name = "copy";
41277 break;
41278 case PRAGMA_OACC_CLAUSE_COPYIN:
41279 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41280 c_name = "copyin";
41281 break;
41282 case PRAGMA_OACC_CLAUSE_COPYOUT:
41283 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41284 c_name = "copyout";
41285 break;
41286 case PRAGMA_OACC_CLAUSE_CREATE:
41287 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41288 c_name = "create";
41289 break;
41290 case PRAGMA_OACC_CLAUSE_DELETE:
41291 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41292 c_name = "delete";
41293 break;
41294 case PRAGMA_OMP_CLAUSE_DEFAULT:
41295 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41296 c_name = "default";
41297 break;
41298 case PRAGMA_OACC_CLAUSE_DETACH:
41299 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41300 c_name = "detach";
41301 break;
41302 case PRAGMA_OACC_CLAUSE_DEVICE:
41303 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41304 c_name = "device";
41305 break;
41306 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41307 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41308 c_name = "deviceptr";
41309 break;
41310 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41311 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41312 c_name = "device_resident";
41313 break;
41314 case PRAGMA_OACC_CLAUSE_FINALIZE:
41315 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41316 clauses);
41317 c_name = "finalize";
41318 break;
41319 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41321 clauses);
41322 c_name = "firstprivate";
41323 break;
41324 case PRAGMA_OACC_CLAUSE_GANG:
41325 c_name = "gang";
41326 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41327 c_name, clauses);
41328 break;
41329 case PRAGMA_OACC_CLAUSE_HOST:
41330 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41331 c_name = "host";
41332 break;
41333 case PRAGMA_OACC_CLAUSE_IF:
41334 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41335 c_name = "if";
41336 break;
41337 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41338 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41339 clauses);
41340 c_name = "if_present";
41341 break;
41342 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41343 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41344 clauses);
41345 c_name = "independent";
41346 break;
41347 case PRAGMA_OACC_CLAUSE_LINK:
41348 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41349 c_name = "link";
41350 break;
41351 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41352 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41353 c_name = "no_create";
41354 break;
41355 case PRAGMA_OACC_CLAUSE_NOHOST:
41356 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41357 clauses);
41358 c_name = "nohost";
41359 break;
41360 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41361 code = OMP_CLAUSE_NUM_GANGS;
41362 c_name = "num_gangs";
41363 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41364 clauses);
41365 break;
41366 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41367 c_name = "num_workers";
41368 code = OMP_CLAUSE_NUM_WORKERS;
41369 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41370 clauses);
41371 break;
41372 case PRAGMA_OACC_CLAUSE_PRESENT:
41373 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41374 c_name = "present";
41375 break;
41376 case PRAGMA_OACC_CLAUSE_PRIVATE:
41377 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41378 clauses);
41379 c_name = "private";
41380 break;
41381 case PRAGMA_OACC_CLAUSE_REDUCTION:
41382 clauses
41383 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41384 false, clauses);
41385 c_name = "reduction";
41386 break;
41387 case PRAGMA_OACC_CLAUSE_SEQ:
41388 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41389 clauses);
41390 c_name = "seq";
41391 break;
41392 case PRAGMA_OACC_CLAUSE_TILE:
41393 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41394 c_name = "tile";
41395 break;
41396 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41397 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41398 clauses);
41399 c_name = "use_device";
41400 break;
41401 case PRAGMA_OACC_CLAUSE_VECTOR:
41402 c_name = "vector";
41403 clauses = cp_parser_oacc_shape_clause (parser, here,
41404 OMP_CLAUSE_VECTOR,
41405 c_name, clauses);
41406 break;
41407 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41408 c_name = "vector_length";
41409 code = OMP_CLAUSE_VECTOR_LENGTH;
41410 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41411 clauses);
41412 break;
41413 case PRAGMA_OACC_CLAUSE_WAIT:
41414 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41415 c_name = "wait";
41416 break;
41417 case PRAGMA_OACC_CLAUSE_WORKER:
41418 c_name = "worker";
41419 clauses = cp_parser_oacc_shape_clause (parser, here,
41420 OMP_CLAUSE_WORKER,
41421 c_name, clauses);
41422 break;
41423 default:
41424 cp_parser_error (parser, "expected an OpenACC clause");
41425 goto saw_error;
41428 first = false;
41430 if (((mask >> c_kind) & 1) == 0)
41432 /* Remove the invalid clause(s) from the list to avoid
41433 confusing the rest of the compiler. */
41434 clauses = prev;
41435 error_at (here, "%qs is not valid for %qs", c_name, where);
41439 saw_error:
41440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41442 if (finish_p)
41443 return finish_omp_clauses (clauses, C_ORT_ACC);
41445 return clauses;
41448 /* Parse all OpenMP clauses. The set clauses allowed by the directive
41449 is a bitmask in MASK. Return the list of clauses found.
41450 FINISH_P set if finish_omp_clauses should be called.
41451 NESTED non-zero if clauses should be terminated by closing paren instead
41452 of end of pragma. If it is 2, additionally commas are required in between
41453 the clauses. */
41455 static tree
41456 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
41457 const char *where, cp_token *pragma_tok,
41458 bool finish_p = true, int nested = 0)
41460 tree clauses = NULL;
41461 bool first = true;
41462 cp_token *token = NULL;
41464 /* Don't create location wrapper nodes within OpenMP clauses. */
41465 auto_suppress_location_wrappers sentinel;
41467 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41469 pragma_omp_clause c_kind;
41470 const char *c_name;
41471 tree prev = clauses;
41473 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
41474 break;
41476 if (!first || nested != 2)
41478 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41479 cp_lexer_consume_token (parser->lexer);
41480 else if (nested == 2)
41481 error_at (cp_lexer_peek_token (parser->lexer)->location,
41482 "clauses in %<simd%> trait should be separated "
41483 "by %<,%>");
41486 token = cp_lexer_peek_token (parser->lexer);
41487 c_kind = cp_parser_omp_clause_name (parser);
41489 switch (c_kind)
41491 case PRAGMA_OMP_CLAUSE_BIND:
41492 clauses = cp_parser_omp_clause_bind (parser, clauses,
41493 token->location);
41494 c_name = "bind";
41495 break;
41496 case PRAGMA_OMP_CLAUSE_COLLAPSE:
41497 clauses = cp_parser_omp_clause_collapse (parser, clauses,
41498 token->location);
41499 c_name = "collapse";
41500 break;
41501 case PRAGMA_OMP_CLAUSE_COPYIN:
41502 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
41503 c_name = "copyin";
41504 break;
41505 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41506 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41507 clauses);
41508 c_name = "copyprivate";
41509 break;
41510 case PRAGMA_OMP_CLAUSE_DEFAULT:
41511 clauses = cp_parser_omp_clause_default (parser, clauses,
41512 token->location, false);
41513 c_name = "default";
41514 break;
41515 case PRAGMA_OMP_CLAUSE_FILTER:
41516 clauses = cp_parser_omp_clause_filter (parser, clauses,
41517 token->location);
41518 c_name = "filter";
41519 break;
41520 case PRAGMA_OMP_CLAUSE_FINAL:
41521 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41522 c_name = "final";
41523 break;
41524 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41525 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41526 clauses);
41527 c_name = "firstprivate";
41528 break;
41529 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41530 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41531 token->location);
41532 c_name = "grainsize";
41533 break;
41534 case PRAGMA_OMP_CLAUSE_HINT:
41535 clauses = cp_parser_omp_clause_hint (parser, clauses,
41536 token->location);
41537 c_name = "hint";
41538 break;
41539 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41540 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41541 token->location);
41542 c_name = "defaultmap";
41543 break;
41544 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
41545 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41546 clauses);
41547 c_name = "use_device_ptr";
41548 break;
41549 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
41550 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
41551 clauses);
41552 c_name = "use_device_addr";
41553 break;
41554 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
41555 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
41556 clauses);
41557 c_name = "is_device_ptr";
41558 break;
41559 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
41560 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
41561 clauses);
41562 c_name = "has_device_addr";
41563 break;
41564 case PRAGMA_OMP_CLAUSE_IF:
41565 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
41566 true);
41567 c_name = "if";
41568 break;
41569 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
41570 clauses
41571 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
41572 true, clauses);
41573 c_name = "in_reduction";
41574 break;
41575 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
41576 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
41577 c_name = "lastprivate";
41578 break;
41579 case PRAGMA_OMP_CLAUSE_MERGEABLE:
41580 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
41581 token->location);
41582 c_name = "mergeable";
41583 break;
41584 case PRAGMA_OMP_CLAUSE_NOWAIT:
41585 clauses = cp_parser_omp_clause_nowait (parser, clauses,
41586 token->location);
41587 c_name = "nowait";
41588 break;
41589 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
41590 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
41591 token->location);
41592 c_name = "num_tasks";
41593 break;
41594 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
41595 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
41596 token->location);
41597 c_name = "num_threads";
41598 break;
41599 case PRAGMA_OMP_CLAUSE_ORDER:
41600 clauses = cp_parser_omp_clause_order (parser, clauses,
41601 token->location);
41602 c_name = "order";
41603 break;
41604 case PRAGMA_OMP_CLAUSE_ORDERED:
41605 clauses = cp_parser_omp_clause_ordered (parser, clauses,
41606 token->location);
41607 c_name = "ordered";
41608 break;
41609 case PRAGMA_OMP_CLAUSE_PRIORITY:
41610 clauses = cp_parser_omp_clause_priority (parser, clauses,
41611 token->location);
41612 c_name = "priority";
41613 break;
41614 case PRAGMA_OMP_CLAUSE_PRIVATE:
41615 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41616 clauses);
41617 c_name = "private";
41618 break;
41619 case PRAGMA_OMP_CLAUSE_REDUCTION:
41620 clauses
41621 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41622 true, clauses);
41623 c_name = "reduction";
41624 break;
41625 case PRAGMA_OMP_CLAUSE_SCHEDULE:
41626 clauses = cp_parser_omp_clause_schedule (parser, clauses,
41627 token->location);
41628 c_name = "schedule";
41629 break;
41630 case PRAGMA_OMP_CLAUSE_SHARED:
41631 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
41632 clauses);
41633 c_name = "shared";
41634 break;
41635 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
41636 clauses
41637 = cp_parser_omp_clause_reduction (parser,
41638 OMP_CLAUSE_TASK_REDUCTION,
41639 true, clauses);
41640 c_name = "task_reduction";
41641 break;
41642 case PRAGMA_OMP_CLAUSE_UNTIED:
41643 clauses = cp_parser_omp_clause_untied (parser, clauses,
41644 token->location);
41645 c_name = "untied";
41646 break;
41647 case PRAGMA_OMP_CLAUSE_INBRANCH:
41648 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
41649 clauses, token->location);
41650 c_name = "inbranch";
41651 break;
41652 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
41653 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
41654 clauses);
41655 c_name = "nontemporal";
41656 break;
41657 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41658 clauses = cp_parser_omp_clause_branch (parser,
41659 OMP_CLAUSE_NOTINBRANCH,
41660 clauses, token->location);
41661 c_name = "notinbranch";
41662 break;
41663 case PRAGMA_OMP_CLAUSE_PARALLEL:
41664 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
41665 clauses, token->location);
41666 c_name = "parallel";
41667 if (!first)
41669 clause_not_first:
41670 error_at (token->location, "%qs must be the first clause of %qs",
41671 c_name, where);
41672 clauses = prev;
41674 break;
41675 case PRAGMA_OMP_CLAUSE_FOR:
41676 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
41677 clauses, token->location);
41678 c_name = "for";
41679 if (!first)
41680 goto clause_not_first;
41681 break;
41682 case PRAGMA_OMP_CLAUSE_SECTIONS:
41683 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
41684 clauses, token->location);
41685 c_name = "sections";
41686 if (!first)
41687 goto clause_not_first;
41688 break;
41689 case PRAGMA_OMP_CLAUSE_TASKGROUP:
41690 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
41691 clauses, token->location);
41692 c_name = "taskgroup";
41693 if (!first)
41694 goto clause_not_first;
41695 break;
41696 case PRAGMA_OMP_CLAUSE_LINK:
41697 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
41698 c_name = "link";
41699 break;
41700 case PRAGMA_OMP_CLAUSE_TO:
41701 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
41703 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41704 clauses);
41705 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
41706 OMP_CLAUSE_ENTER_TO (c) = 1;
41707 clauses = nl;
41709 else
41710 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
41711 clauses);
41712 c_name = "to";
41713 break;
41714 case PRAGMA_OMP_CLAUSE_FROM:
41715 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
41716 clauses);
41717 c_name = "from";
41718 break;
41719 case PRAGMA_OMP_CLAUSE_UNIFORM:
41720 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
41721 clauses);
41722 c_name = "uniform";
41723 break;
41724 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
41725 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
41726 token->location);
41727 c_name = "num_teams";
41728 break;
41729 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
41730 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
41731 token->location);
41732 c_name = "thread_limit";
41733 break;
41734 case PRAGMA_OMP_CLAUSE_ALIGNED:
41735 clauses = cp_parser_omp_clause_aligned (parser, clauses);
41736 c_name = "aligned";
41737 break;
41738 case PRAGMA_OMP_CLAUSE_ALLOCATE:
41739 clauses = cp_parser_omp_clause_allocate (parser, clauses);
41740 c_name = "allocate";
41741 break;
41742 case PRAGMA_OMP_CLAUSE_LINEAR:
41744 bool declare_simd = false;
41745 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
41746 declare_simd = true;
41747 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
41749 c_name = "linear";
41750 break;
41751 case PRAGMA_OMP_CLAUSE_AFFINITY:
41752 clauses = cp_parser_omp_clause_affinity (parser, clauses);
41753 c_name = "affinity";
41754 break;
41755 case PRAGMA_OMP_CLAUSE_DEPEND:
41756 clauses = cp_parser_omp_clause_depend (parser, clauses,
41757 token->location);
41758 c_name = "depend";
41759 break;
41760 case PRAGMA_OMP_CLAUSE_DOACROSS:
41761 clauses = cp_parser_omp_clause_doacross (parser, clauses,
41762 token->location);
41763 c_name = "doacross";
41764 break;
41765 case PRAGMA_OMP_CLAUSE_DETACH:
41766 clauses = cp_parser_omp_clause_detach (parser, clauses);
41767 c_name = "detach";
41768 break;
41769 case PRAGMA_OMP_CLAUSE_MAP:
41770 clauses = cp_parser_omp_clause_map (parser, clauses);
41771 c_name = "map";
41772 break;
41773 case PRAGMA_OMP_CLAUSE_DEVICE:
41774 clauses = cp_parser_omp_clause_device (parser, clauses,
41775 token->location);
41776 c_name = "device";
41777 break;
41778 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
41779 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
41780 token->location);
41781 c_name = "dist_schedule";
41782 break;
41783 case PRAGMA_OMP_CLAUSE_PROC_BIND:
41784 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
41785 token->location);
41786 c_name = "proc_bind";
41787 break;
41788 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
41789 clauses = cp_parser_omp_clause_device_type (parser, clauses,
41790 token->location);
41791 c_name = "device_type";
41792 break;
41793 case PRAGMA_OMP_CLAUSE_SAFELEN:
41794 clauses = cp_parser_omp_clause_safelen (parser, clauses,
41795 token->location);
41796 c_name = "safelen";
41797 break;
41798 case PRAGMA_OMP_CLAUSE_SIMDLEN:
41799 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
41800 token->location);
41801 c_name = "simdlen";
41802 break;
41803 case PRAGMA_OMP_CLAUSE_NOGROUP:
41804 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
41805 token->location);
41806 c_name = "nogroup";
41807 break;
41808 case PRAGMA_OMP_CLAUSE_THREADS:
41809 clauses
41810 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
41811 clauses, token->location);
41812 c_name = "threads";
41813 break;
41814 case PRAGMA_OMP_CLAUSE_SIMD:
41815 clauses
41816 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
41817 clauses, token->location);
41818 c_name = "simd";
41819 break;
41820 case PRAGMA_OMP_CLAUSE_ENTER:
41821 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41822 clauses);
41823 c_name = "enter";
41824 break;
41825 default:
41826 cp_parser_error (parser, "expected an OpenMP clause");
41827 goto saw_error;
41830 first = false;
41832 if (((mask >> c_kind) & 1) == 0)
41834 /* Remove the invalid clause(s) from the list to avoid
41835 confusing the rest of the compiler. */
41836 clauses = prev;
41837 error_at (token->location, "%qs is not valid for %qs", c_name, where);
41840 saw_error:
41841 if (!nested)
41842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41843 if (finish_p)
41845 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
41846 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
41847 else
41848 return finish_omp_clauses (clauses, C_ORT_OMP);
41850 return clauses;
41853 /* OpenMP 2.5:
41854 structured-block:
41855 statement
41857 In practice, we're also interested in adding the statement to an
41858 outer node. So it is convenient if we work around the fact that
41859 cp_parser_statement calls add_stmt. */
41861 static unsigned
41862 cp_parser_begin_omp_structured_block (cp_parser *parser)
41864 unsigned save = parser->in_statement;
41866 /* Only move the values to IN_OMP_BLOCK if they weren't false.
41867 This preserves the "not within loop or switch" style error messages
41868 for nonsense cases like
41869 void foo() {
41870 #pragma omp single
41871 break;
41874 if (parser->in_statement)
41875 parser->in_statement = IN_OMP_BLOCK;
41877 return save;
41880 static void
41881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
41883 parser->in_statement = save;
41886 static tree
41887 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
41889 tree stmt = begin_omp_structured_block ();
41890 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41892 parser->omp_attrs_forbidden_p = true;
41893 cp_parser_statement (parser, NULL_TREE, false, if_p);
41895 cp_parser_end_omp_structured_block (parser, save);
41896 return finish_omp_structured_block (stmt);
41899 /* OpenMP 5.x:
41900 # pragma omp allocate (list) clauses
41902 OpenMP 5.0 clause:
41903 allocator (omp_allocator_handle_t expression)
41905 OpenMP 5.1 additional clause:
41906 align (constant-expression)] */
41908 static void
41909 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
41911 tree allocator = NULL_TREE;
41912 tree alignment = NULL_TREE;
41913 location_t loc = pragma_tok->location;
41914 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
41918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41919 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41920 cp_lexer_consume_token (parser->lexer);
41922 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41923 break;
41924 matching_parens parens;
41925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41926 const char *p = IDENTIFIER_POINTER (id);
41927 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41928 cp_lexer_consume_token (parser->lexer);
41929 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
41931 error_at (cloc, "expected %<allocator%> or %<align%>");
41932 break;
41934 if (!parens.require_open (parser))
41935 break;
41936 tree expr = cp_parser_assignment_expression (parser);
41937 if (p[2] == 'i' && alignment)
41939 error_at (cloc, "too many %qs clauses", "align");
41940 break;
41942 else if (p[2] == 'i')
41944 if (expr != error_mark_node)
41945 alignment = expr;
41946 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
41947 if (alignment
41948 && !type_dependent_expression_p (alignment)
41949 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
41951 error_at (cloc, "%<align%> clause argument needs to be "
41952 "positive constant power of two integer "
41953 "expression");
41954 alignment = NULL_TREE;
41956 else if (alignment)
41958 alignment = mark_rvalue_use (alignment);
41959 if (!processing_template_decl)
41961 alignment = maybe_constant_value (alignment);
41962 if (TREE_CODE (alignment) != INTEGER_CST
41963 || !tree_fits_uhwi_p (alignment)
41964 || !integer_pow2p (alignment))
41966 error_at (cloc, "%<align%> clause argument needs to be "
41967 "positive constant power of two integer "
41968 "expression");
41969 alignment = NULL_TREE;
41974 else if (allocator)
41976 error_at (cloc, "too many %qs clauses", "allocator");
41977 break;
41979 else
41981 if (expr != error_mark_node)
41982 allocator = expr;
41984 parens.require_close (parser);
41985 } while (true);
41986 cp_parser_require_pragma_eol (parser, pragma_tok);
41988 if (allocator || alignment)
41989 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
41991 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
41992 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
41995 /* FIXME: When implementing properly, delete the align/allocate expr error
41996 check above and add one in semantics.cc (to properly handle templates).
41997 Base this on the allocator/align modifiers check for the 'allocate' clause
41998 in semantics.cc's finish_omp_clauses. */
41999 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
42002 /* OpenMP 2.5:
42003 # pragma omp atomic new-line
42004 expression-stmt
42006 expression-stmt:
42007 x binop= expr | x++ | ++x | x-- | --x
42008 binop:
42009 +, *, -, /, &, ^, |, <<, >>
42011 where x is an lvalue expression with scalar type.
42013 OpenMP 3.1:
42014 # pragma omp atomic new-line
42015 update-stmt
42017 # pragma omp atomic read new-line
42018 read-stmt
42020 # pragma omp atomic write new-line
42021 write-stmt
42023 # pragma omp atomic update new-line
42024 update-stmt
42026 # pragma omp atomic capture new-line
42027 capture-stmt
42029 # pragma omp atomic capture new-line
42030 capture-block
42032 read-stmt:
42033 v = x
42034 write-stmt:
42035 x = expr
42036 update-stmt:
42037 expression-stmt | x = x binop expr
42038 capture-stmt:
42039 v = expression-stmt
42040 capture-block:
42041 { v = x; update-stmt; } | { update-stmt; v = x; }
42043 OpenMP 4.0:
42044 update-stmt:
42045 expression-stmt | x = x binop expr | x = expr binop x
42046 capture-stmt:
42047 v = update-stmt
42048 capture-block:
42049 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
42051 OpenMP 5.1:
42052 # pragma omp atomic compare new-line
42053 conditional-update-atomic
42055 # pragma omp atomic compare capture new-line
42056 conditional-update-capture-atomic
42058 conditional-update-atomic:
42059 cond-expr-stmt | cond-update-stmt
42060 cond-expr-stmt:
42061 x = expr ordop x ? expr : x;
42062 x = x ordop expr ? expr : x;
42063 x = x == e ? d : x;
42064 cond-update-stmt:
42065 if (expr ordop x) { x = expr; }
42066 if (x ordop expr) { x = expr; }
42067 if (x == e) { x = d; }
42068 ordop:
42069 <, >
42070 conditional-update-capture-atomic:
42071 v = cond-expr-stmt
42072 { v = x; cond-expr-stmt }
42073 { cond-expr-stmt v = x; }
42074 { v = x; cond-update-stmt }
42075 { cond-update-stmt v = x; }
42076 if (x == e) { x = d; } else { v = x; }
42077 { r = x == e; if (r) { x = d; } }
42078 { r = x == e; if (r) { x = d; } else { v = x; } }
42080 where x, r and v are lvalue expressions with scalar type,
42081 expr, e and d are expressions with scalar type and e might be
42082 the same as v. */
42084 static void
42085 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42087 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42088 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42089 location_t loc = pragma_tok->location;
42090 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42091 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42092 bool structured_block = false;
42093 tree clauses = NULL_TREE;
42094 bool capture = false;
42095 bool compare = false;
42096 bool weak = false;
42097 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42098 bool no_semicolon = false;
42099 bool extra_scope = false;
42101 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42103 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42104 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42105 cp_lexer_consume_token (parser->lexer);
42107 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42109 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42110 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42111 const char *p = IDENTIFIER_POINTER (id);
42112 enum tree_code new_code = ERROR_MARK;
42113 enum omp_memory_order new_memory_order
42114 = OMP_MEMORY_ORDER_UNSPECIFIED;
42115 bool new_capture = false;
42116 bool new_compare = false;
42117 bool new_weak = false;
42118 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42120 if (!strcmp (p, "read"))
42121 new_code = OMP_ATOMIC_READ;
42122 else if (!strcmp (p, "write"))
42123 new_code = NOP_EXPR;
42124 else if (!strcmp (p, "update"))
42125 new_code = OMP_ATOMIC;
42126 else if (openacc && !strcmp (p, "capture"))
42127 new_code = OMP_ATOMIC_CAPTURE_NEW;
42128 else if (openacc)
42130 p = NULL;
42131 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42132 "or %<capture%> clause");
42134 else if (!strcmp (p, "capture"))
42135 new_capture = true;
42136 else if (!strcmp (p, "compare"))
42137 new_compare = true;
42138 else if (!strcmp (p, "weak"))
42139 new_weak = true;
42140 else if (!strcmp (p, "fail"))
42142 matching_parens parens;
42144 cp_lexer_consume_token (parser->lexer);
42145 if (!parens.require_open (parser))
42146 continue;
42148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42150 id = cp_lexer_peek_token (parser->lexer)->u.value;
42151 const char *q = IDENTIFIER_POINTER (id);
42153 if (!strcmp (q, "seq_cst"))
42154 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42155 else if (!strcmp (q, "acquire"))
42156 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42157 else if (!strcmp (q, "relaxed"))
42158 new_fail = OMP_MEMORY_ORDER_RELAXED;
42161 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42163 cp_lexer_consume_token (parser->lexer);
42164 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42165 error_at (cloc, "too many %qs clauses", "fail");
42166 else
42167 fail = new_fail;
42169 else
42170 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42171 "or %<relaxed%>");
42172 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42173 || !parens.require_close (parser))
42174 cp_parser_skip_to_closing_parenthesis (parser,
42175 /*recovering=*/true,
42176 /*or_comma=*/false,
42177 /*consume_paren=*/true);
42178 continue;
42180 else if (!strcmp (p, "seq_cst"))
42181 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42182 else if (!strcmp (p, "acq_rel"))
42183 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42184 else if (!strcmp (p, "release"))
42185 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42186 else if (!strcmp (p, "acquire"))
42187 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42188 else if (!strcmp (p, "relaxed"))
42189 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42190 else if (!strcmp (p, "hint"))
42192 cp_lexer_consume_token (parser->lexer);
42193 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42194 continue;
42196 else
42198 p = NULL;
42199 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42200 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42201 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42202 "%<relaxed%> or %<hint%> clause");
42204 if (p)
42206 if (new_code != ERROR_MARK)
42208 /* OpenACC permits 'update capture'. */
42209 if (openacc
42210 && code == OMP_ATOMIC
42211 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42212 code = new_code;
42213 else if (code != ERROR_MARK)
42214 error_at (cloc, "too many atomic clauses");
42215 else
42216 code = new_code;
42218 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42220 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42221 error_at (cloc, "too many memory order clauses");
42222 else
42223 memory_order = new_memory_order;
42225 else if (new_capture)
42227 if (capture)
42228 error_at (cloc, "too many %qs clauses", "capture");
42229 else
42230 capture = true;
42232 else if (new_compare)
42234 if (compare)
42235 error_at (cloc, "too many %qs clauses", "compare");
42236 else
42237 compare = true;
42239 else if (new_weak)
42241 if (weak)
42242 error_at (cloc, "too many %qs clauses", "weak");
42243 else
42244 weak = true;
42246 cp_lexer_consume_token (parser->lexer);
42247 continue;
42250 break;
42252 cp_parser_require_pragma_eol (parser, pragma_tok);
42254 if (code == ERROR_MARK)
42255 code = OMP_ATOMIC;
42256 if (capture)
42258 if (code != OMP_ATOMIC)
42259 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42260 "clauses", "capture");
42261 else
42262 code = OMP_ATOMIC_CAPTURE_NEW;
42264 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42266 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42267 "clauses", "compare");
42268 compare = false;
42270 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42272 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42273 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42275 if (weak && !compare)
42277 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42278 weak = false;
42280 if (openacc)
42281 memory_order = OMP_MEMORY_ORDER_RELAXED;
42282 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42284 omp_requires_mask
42285 = (enum omp_requires) (omp_requires_mask
42286 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42287 switch ((enum omp_memory_order)
42288 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42290 case OMP_MEMORY_ORDER_UNSPECIFIED:
42291 case OMP_MEMORY_ORDER_RELAXED:
42292 memory_order = OMP_MEMORY_ORDER_RELAXED;
42293 break;
42294 case OMP_MEMORY_ORDER_SEQ_CST:
42295 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42296 break;
42297 case OMP_MEMORY_ORDER_ACQ_REL:
42298 switch (code)
42300 case OMP_ATOMIC_READ:
42301 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42302 break;
42303 case NOP_EXPR: /* atomic write */
42304 memory_order = OMP_MEMORY_ORDER_RELEASE;
42305 break;
42306 default:
42307 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42308 break;
42310 break;
42311 default:
42312 gcc_unreachable ();
42315 else
42316 switch (code)
42318 case OMP_ATOMIC_READ:
42319 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42321 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42322 "%<release%> clause");
42323 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42325 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42326 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42327 break;
42328 case NOP_EXPR: /* atomic write */
42329 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42331 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42332 "%<acquire%> clause");
42333 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42335 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42336 memory_order = OMP_MEMORY_ORDER_RELEASE;
42337 break;
42338 default:
42339 break;
42341 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42342 memory_order
42343 = (enum omp_memory_order) (memory_order
42344 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42346 switch (code)
42348 case OMP_ATOMIC_READ:
42349 case NOP_EXPR: /* atomic write */
42350 v = cp_parser_unary_expression (parser);
42351 if (v == error_mark_node)
42352 goto saw_error;
42353 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42354 goto saw_error;
42355 if (code == NOP_EXPR)
42356 lhs = cp_parser_expression (parser);
42357 else
42358 lhs = cp_parser_unary_expression (parser);
42359 if (lhs == error_mark_node)
42360 goto saw_error;
42361 if (code == NOP_EXPR)
42363 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42364 opcode. */
42365 code = OMP_ATOMIC;
42366 rhs = lhs;
42367 lhs = v;
42368 v = NULL_TREE;
42370 goto done;
42371 case OMP_ATOMIC_CAPTURE_NEW:
42372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42374 cp_lexer_consume_token (parser->lexer);
42375 structured_block = true;
42377 else if (compare
42378 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42379 break;
42380 else
42382 v = cp_parser_unary_expression (parser);
42383 if (v == error_mark_node)
42384 goto saw_error;
42385 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42386 goto saw_error;
42387 if (compare
42388 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42390 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42391 error_at (eloc, "expected expression");
42392 goto saw_error;
42395 default:
42396 break;
42399 restart:
42400 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42402 cp_lexer_consume_token (parser->lexer);
42404 matching_parens parens;
42405 if (!parens.require_open (parser))
42406 goto saw_error;
42407 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42408 tree cmp_expr;
42409 if (r)
42410 cmp_expr = cp_parser_unary_expression (parser);
42411 else
42412 cmp_expr = cp_parser_binary_expression (parser, false, true,
42413 PREC_NOT_OPERATOR, NULL);
42414 if (!parens.require_close (parser))
42415 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42416 if (cmp_expr == error_mark_node)
42417 goto saw_error;
42418 if (r)
42420 if (!cp_tree_equal (cmp_expr, r))
42421 goto bad_if;
42422 cmp_expr = rhs;
42423 rhs = NULL_TREE;
42424 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
42426 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42428 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42430 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42431 "expected %<==%> comparison in %<if%> condition");
42432 goto saw_error;
42434 else if (TREE_CODE (cmp_expr) != GT_EXPR
42435 && TREE_CODE (cmp_expr) != LT_EXPR)
42437 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42438 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
42439 "condition");
42440 goto saw_error;
42442 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42443 goto saw_error;
42445 extra_scope = true;
42446 eloc = cp_lexer_peek_token (parser->lexer)->location;
42447 lhs = cp_parser_unary_expression (parser);
42448 orig_lhs = lhs;
42449 if (lhs == error_mark_node)
42450 goto saw_error;
42451 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42453 cp_parser_error (parser, "expected %<=%>");
42454 goto saw_error;
42456 cp_lexer_consume_token (parser->lexer);
42457 eloc = cp_lexer_peek_token (parser->lexer)->location;
42458 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42459 rhs1 = cp_parser_expression (parser);
42460 else
42461 rhs1 = cp_parser_simple_cast_expression (parser);
42463 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42464 goto saw_error;
42466 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42467 goto saw_error;
42469 extra_scope = false;
42470 no_semicolon = true;
42472 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
42474 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42476 opcode = COND_EXPR;
42477 rhs = TREE_OPERAND (cmp_expr, 1);
42479 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
42481 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42482 ? MIN_EXPR : MAX_EXPR);
42483 rhs = rhs1;
42484 rhs1 = TREE_OPERAND (cmp_expr, 0);
42486 else
42487 goto bad_if;
42489 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
42490 goto bad_if;
42491 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
42492 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
42494 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42495 ? MAX_EXPR : MIN_EXPR);
42496 rhs = rhs1;
42497 rhs1 = TREE_OPERAND (cmp_expr, 1);
42499 else
42501 bad_if:
42502 cp_parser_error (parser,
42503 "invalid form of %<#pragma omp atomic compare%>");
42504 goto saw_error;
42507 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
42509 if (code != OMP_ATOMIC_CAPTURE_NEW
42510 || (structured_block && r == NULL_TREE)
42511 || TREE_CODE (cmp_expr) != EQ_EXPR)
42513 eloc = cp_lexer_peek_token (parser->lexer)->location;
42514 error_at (eloc, "unexpected %<else%>");
42515 goto saw_error;
42518 cp_lexer_consume_token (parser->lexer);
42520 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42521 goto saw_error;
42523 extra_scope = true;
42524 v = cp_parser_unary_expression (parser);
42525 if (v == error_mark_node)
42526 goto saw_error;
42527 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42528 goto saw_error;
42530 tree expr = cp_parser_simple_cast_expression (parser);
42532 if (!cp_tree_equal (expr, lhs))
42533 goto bad_if;
42535 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42536 goto saw_error;
42538 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42539 goto saw_error;
42541 extra_scope = false;
42542 code = OMP_ATOMIC_CAPTURE_OLD;
42543 if (r == NULL_TREE)
42544 /* Signal to c_finish_omp_atomic that in
42545 if (x == e) { x = d; } else { v = x; }
42546 case the store to v should be conditional. */
42547 r = void_list_node;
42549 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42551 cp_parser_error (parser, "expected %<else%>");
42552 goto saw_error;
42554 else if (code == OMP_ATOMIC_CAPTURE_NEW
42555 && r != NULL_TREE
42556 && v == NULL_TREE)
42557 code = OMP_ATOMIC;
42558 goto stmt_done;
42560 lhs = cp_parser_unary_expression (parser);
42561 orig_lhs = lhs;
42562 switch (TREE_CODE (lhs))
42564 case ERROR_MARK:
42565 goto saw_error;
42567 case POSTINCREMENT_EXPR:
42568 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42569 code = OMP_ATOMIC_CAPTURE_OLD;
42570 /* FALLTHROUGH */
42571 case PREINCREMENT_EXPR:
42572 lhs = TREE_OPERAND (lhs, 0);
42573 opcode = PLUS_EXPR;
42574 rhs = integer_one_node;
42575 if (compare)
42576 goto invalid_compare;
42577 break;
42579 case POSTDECREMENT_EXPR:
42580 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42581 code = OMP_ATOMIC_CAPTURE_OLD;
42582 /* FALLTHROUGH */
42583 case PREDECREMENT_EXPR:
42584 lhs = TREE_OPERAND (lhs, 0);
42585 opcode = MINUS_EXPR;
42586 rhs = integer_one_node;
42587 if (compare)
42588 goto invalid_compare;
42589 break;
42591 case COMPOUND_EXPR:
42592 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
42593 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
42594 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
42595 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
42596 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
42597 (TREE_OPERAND (lhs, 1), 0), 0)))
42598 == BOOLEAN_TYPE)
42599 /* Undo effects of boolean_increment for post {in,de}crement. */
42600 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
42601 /* FALLTHRU */
42602 case MODIFY_EXPR:
42603 if (TREE_CODE (lhs) == MODIFY_EXPR
42604 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
42606 /* Undo effects of boolean_increment. */
42607 if (integer_onep (TREE_OPERAND (lhs, 1)))
42609 /* This is pre or post increment. */
42610 rhs = TREE_OPERAND (lhs, 1);
42611 lhs = TREE_OPERAND (lhs, 0);
42612 opcode = NOP_EXPR;
42613 if (code == OMP_ATOMIC_CAPTURE_NEW
42614 && !structured_block
42615 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
42616 code = OMP_ATOMIC_CAPTURE_OLD;
42617 if (compare)
42618 goto invalid_compare;
42619 break;
42622 /* FALLTHRU */
42623 default:
42624 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42626 cp_parser_error (parser, "expected %<=%>");
42627 goto saw_error;
42629 switch (cp_lexer_peek_token (parser->lexer)->type)
42631 case CPP_MULT_EQ:
42632 opcode = MULT_EXPR;
42633 break;
42634 case CPP_DIV_EQ:
42635 opcode = TRUNC_DIV_EXPR;
42636 break;
42637 case CPP_PLUS_EQ:
42638 opcode = PLUS_EXPR;
42639 break;
42640 case CPP_MINUS_EQ:
42641 opcode = MINUS_EXPR;
42642 break;
42643 case CPP_LSHIFT_EQ:
42644 opcode = LSHIFT_EXPR;
42645 break;
42646 case CPP_RSHIFT_EQ:
42647 opcode = RSHIFT_EXPR;
42648 break;
42649 case CPP_AND_EQ:
42650 opcode = BIT_AND_EXPR;
42651 break;
42652 case CPP_OR_EQ:
42653 opcode = BIT_IOR_EXPR;
42654 break;
42655 case CPP_XOR_EQ:
42656 opcode = BIT_XOR_EXPR;
42657 break;
42658 case CPP_EQ:
42659 enum cp_parser_prec oprec;
42660 cp_token *token;
42661 cp_lexer_consume_token (parser->lexer);
42662 cp_parser_parse_tentatively (parser);
42663 rhs1 = cp_parser_simple_cast_expression (parser);
42664 if (rhs1 == error_mark_node)
42666 cp_parser_abort_tentative_parse (parser);
42667 cp_parser_simple_cast_expression (parser);
42668 goto saw_error;
42670 token = cp_lexer_peek_token (parser->lexer);
42671 if (token->type != CPP_SEMICOLON
42672 && (!compare || token->type != CPP_QUERY)
42673 && !cp_tree_equal (lhs, rhs1))
42675 cp_parser_abort_tentative_parse (parser);
42676 cp_parser_parse_tentatively (parser);
42677 rhs = cp_parser_binary_expression (parser, false, true,
42678 PREC_NOT_OPERATOR, NULL);
42679 if (rhs == error_mark_node)
42681 cp_parser_abort_tentative_parse (parser);
42682 cp_parser_binary_expression (parser, false, true,
42683 PREC_NOT_OPERATOR, NULL);
42684 goto saw_error;
42686 switch (TREE_CODE (rhs))
42688 case MULT_EXPR:
42689 case TRUNC_DIV_EXPR:
42690 case RDIV_EXPR:
42691 case PLUS_EXPR:
42692 case MINUS_EXPR:
42693 case LSHIFT_EXPR:
42694 case RSHIFT_EXPR:
42695 case BIT_AND_EXPR:
42696 case BIT_IOR_EXPR:
42697 case BIT_XOR_EXPR:
42698 if (compare)
42699 break;
42700 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
42702 if (cp_parser_parse_definitely (parser))
42704 opcode = TREE_CODE (rhs);
42705 rhs1 = TREE_OPERAND (rhs, 0);
42706 rhs = TREE_OPERAND (rhs, 1);
42707 goto stmt_done;
42709 else
42710 goto saw_error;
42712 break;
42713 case EQ_EXPR:
42714 if (!compare
42715 || code != OMP_ATOMIC_CAPTURE_NEW
42716 || !structured_block
42717 || v
42718 || r)
42719 break;
42720 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
42721 && cp_lexer_nth_token_is_keyword (parser->lexer,
42722 2, RID_IF))
42724 if (cp_parser_parse_definitely (parser))
42726 r = lhs;
42727 lhs = NULL_TREE;
42728 rhs1 = NULL_TREE;
42729 cp_lexer_consume_token (parser->lexer);
42730 goto restart;
42733 break;
42734 case GT_EXPR:
42735 case LT_EXPR:
42736 if (compare
42737 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
42738 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
42739 && cp_parser_parse_definitely (parser))
42741 opcode = TREE_CODE (rhs);
42742 rhs1 = TREE_OPERAND (rhs, 0);
42743 rhs = TREE_OPERAND (rhs, 1);
42744 cond_expr:
42745 cp_lexer_consume_token (parser->lexer);
42746 bool saved_colon_corrects_to_scope_p
42747 = parser->colon_corrects_to_scope_p;
42748 parser->colon_corrects_to_scope_p = false;
42749 tree e1 = cp_parser_expression (parser);
42750 parser->colon_corrects_to_scope_p
42751 = saved_colon_corrects_to_scope_p;
42752 cp_parser_require (parser, CPP_COLON, RT_COLON);
42753 tree e2 = cp_parser_simple_cast_expression (parser);
42754 if (cp_tree_equal (lhs, e2))
42756 if (cp_tree_equal (lhs, rhs1))
42758 if (opcode == EQ_EXPR)
42760 opcode = COND_EXPR;
42761 rhs1 = e1;
42762 goto stmt_done;
42764 if (cp_tree_equal (rhs, e1))
42766 opcode
42767 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
42768 rhs = e1;
42769 goto stmt_done;
42772 else
42774 gcc_assert (opcode != EQ_EXPR);
42775 if (cp_tree_equal (rhs1, e1))
42777 opcode
42778 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
42779 rhs1 = rhs;
42780 rhs = e1;
42781 goto stmt_done;
42785 cp_parser_error (parser,
42786 "invalid form of "
42787 "%<#pragma omp atomic compare%>");
42788 goto saw_error;
42790 break;
42791 default:
42792 break;
42794 cp_parser_abort_tentative_parse (parser);
42795 if (structured_block
42796 && code == OMP_ATOMIC_CAPTURE_OLD
42797 && !compare)
42799 rhs = cp_parser_expression (parser);
42800 if (rhs == error_mark_node)
42801 goto saw_error;
42802 opcode = NOP_EXPR;
42803 rhs1 = NULL_TREE;
42804 goto stmt_done;
42806 cp_parser_error (parser,
42807 "invalid form of %<#pragma omp atomic%>");
42808 goto saw_error;
42810 if (!cp_parser_parse_definitely (parser))
42811 goto saw_error;
42812 switch (token->type)
42814 case CPP_SEMICOLON:
42815 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42817 code = OMP_ATOMIC_CAPTURE_OLD;
42818 v = lhs;
42819 lhs = NULL_TREE;
42820 lhs1 = rhs1;
42821 rhs1 = NULL_TREE;
42822 cp_lexer_consume_token (parser->lexer);
42823 goto restart;
42825 else if (structured_block && !compare)
42827 opcode = NOP_EXPR;
42828 rhs = rhs1;
42829 rhs1 = NULL_TREE;
42830 goto stmt_done;
42832 cp_parser_error (parser,
42833 "invalid form of %<#pragma omp atomic%>");
42834 goto saw_error;
42835 case CPP_MULT:
42836 opcode = MULT_EXPR;
42837 break;
42838 case CPP_DIV:
42839 opcode = TRUNC_DIV_EXPR;
42840 break;
42841 case CPP_PLUS:
42842 opcode = PLUS_EXPR;
42843 break;
42844 case CPP_MINUS:
42845 opcode = MINUS_EXPR;
42846 break;
42847 case CPP_LSHIFT:
42848 opcode = LSHIFT_EXPR;
42849 break;
42850 case CPP_RSHIFT:
42851 opcode = RSHIFT_EXPR;
42852 break;
42853 case CPP_AND:
42854 opcode = BIT_AND_EXPR;
42855 break;
42856 case CPP_OR:
42857 opcode = BIT_IOR_EXPR;
42858 break;
42859 case CPP_XOR:
42860 opcode = BIT_XOR_EXPR;
42861 break;
42862 case CPP_EQ_EQ:
42863 opcode = EQ_EXPR;
42864 break;
42865 case CPP_GREATER:
42866 opcode = GT_EXPR;
42867 break;
42868 case CPP_LESS:
42869 opcode = LT_EXPR;
42870 break;
42871 default:
42872 cp_parser_error (parser,
42873 "invalid operator for %<#pragma omp atomic%>");
42874 goto saw_error;
42876 if (compare
42877 && TREE_CODE_CLASS (opcode) != tcc_comparison)
42879 cp_parser_error (parser,
42880 "invalid form of "
42881 "%<#pragma omp atomic compare%>");
42882 goto saw_error;
42884 oprec = TOKEN_PRECEDENCE (token);
42885 gcc_assert (oprec != PREC_NOT_OPERATOR);
42886 if (commutative_tree_code (opcode))
42887 oprec = (enum cp_parser_prec) (oprec - 1);
42888 cp_lexer_consume_token (parser->lexer);
42889 rhs = cp_parser_binary_expression (parser, false, false,
42890 oprec, NULL);
42891 if (rhs == error_mark_node)
42892 goto saw_error;
42893 if (compare)
42895 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
42897 cp_parser_error (parser,
42898 "invalid form of "
42899 "%<#pragma omp atomic compare%>");
42900 goto saw_error;
42902 goto cond_expr;
42904 goto stmt_done;
42905 default:
42906 cp_parser_error (parser,
42907 "invalid operator for %<#pragma omp atomic%>");
42908 goto saw_error;
42910 cp_lexer_consume_token (parser->lexer);
42912 rhs = cp_parser_expression (parser);
42913 if (rhs == error_mark_node)
42914 goto saw_error;
42915 break;
42917 stmt_done:
42918 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
42920 if (!no_semicolon
42921 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42922 goto saw_error;
42923 no_semicolon = false;
42924 v = cp_parser_unary_expression (parser);
42925 if (v == error_mark_node)
42926 goto saw_error;
42927 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42928 goto saw_error;
42929 lhs1 = cp_parser_unary_expression (parser);
42930 if (lhs1 == error_mark_node)
42931 goto saw_error;
42933 if (structured_block)
42935 if (!no_semicolon)
42936 cp_parser_consume_semicolon_at_end_of_statement (parser);
42937 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
42939 done:
42940 if (weak && opcode != COND_EXPR)
42942 error_at (loc, "%<weak%> clause requires atomic equality comparison");
42943 weak = false;
42945 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42946 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
42947 rhs1, r, clauses, memory_order, weak);
42948 if (!structured_block && !no_semicolon)
42949 cp_parser_consume_semicolon_at_end_of_statement (parser);
42950 return;
42952 invalid_compare:
42953 error ("invalid form of %<pragma omp atomic compare%>");
42954 /* FALLTHRU */
42955 saw_error:
42956 cp_parser_skip_to_end_of_block_or_statement (parser);
42957 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42958 cp_lexer_consume_token (parser->lexer);
42959 if (structured_block)
42961 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42962 cp_lexer_consume_token (parser->lexer);
42963 else if (code == OMP_ATOMIC_CAPTURE_NEW)
42965 cp_parser_skip_to_end_of_block_or_statement (parser);
42966 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42967 cp_lexer_consume_token (parser->lexer);
42973 /* OpenMP 2.5:
42974 # pragma omp barrier new-line */
42976 static void
42977 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
42979 cp_parser_require_pragma_eol (parser, pragma_tok);
42980 finish_omp_barrier ();
42983 /* OpenMP 2.5:
42984 # pragma omp critical [(name)] new-line
42985 structured-block
42987 OpenMP 4.5:
42988 # pragma omp critical [(name) [hint(expression)]] new-line
42989 structured-block */
42991 #define OMP_CRITICAL_CLAUSE_MASK \
42992 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
42994 static tree
42995 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42997 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
42999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43001 matching_parens parens;
43002 parens.consume_open (parser);
43004 name = cp_parser_identifier (parser);
43006 if (name == error_mark_node
43007 || !parens.require_close (parser))
43008 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43009 /*or_comma=*/false,
43010 /*consume_paren=*/true);
43011 if (name == error_mark_node)
43012 name = NULL;
43014 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43015 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43016 cp_lexer_consume_token (parser->lexer);
43019 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
43020 "#pragma omp critical", pragma_tok);
43022 stmt = cp_parser_omp_structured_block (parser, if_p);
43023 return c_finish_omp_critical (input_location, stmt, name, clauses);
43026 /* OpenMP 5.0:
43027 # pragma omp depobj ( depobj ) depobj-clause new-line
43029 depobj-clause:
43030 depend (dependence-type : locator)
43031 destroy
43032 update (dependence-type)
43034 dependence-type:
43037 inout
43038 mutexinout */
43040 static void
43041 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
43043 location_t loc = pragma_tok->location;
43044 matching_parens parens;
43045 if (!parens.require_open (parser))
43047 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43048 return;
43051 tree depobj = cp_parser_assignment_expression (parser);
43053 if (!parens.require_close (parser))
43054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43055 /*or_comma=*/false,
43056 /*consume_paren=*/true);
43058 tree clause = NULL_TREE;
43059 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
43060 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43061 cp_lexer_consume_token (parser->lexer);
43062 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
43063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43066 const char *p = IDENTIFIER_POINTER (id);
43068 cp_lexer_consume_token (parser->lexer);
43069 if (!strcmp ("depend", p))
43071 /* Don't create location wrapper nodes within the depend clause. */
43072 auto_suppress_location_wrappers sentinel;
43073 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
43074 if (clause)
43075 clause = finish_omp_clauses (clause, C_ORT_OMP);
43076 if (!clause)
43077 clause = error_mark_node;
43079 else if (!strcmp ("destroy", p))
43080 kind = OMP_CLAUSE_DEPEND_LAST;
43081 else if (!strcmp ("update", p))
43083 matching_parens c_parens;
43084 if (c_parens.require_open (parser))
43086 location_t c2_loc
43087 = cp_lexer_peek_token (parser->lexer)->location;
43088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43090 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43091 const char *p2 = IDENTIFIER_POINTER (id2);
43093 cp_lexer_consume_token (parser->lexer);
43094 if (!strcmp ("in", p2))
43095 kind = OMP_CLAUSE_DEPEND_IN;
43096 else if (!strcmp ("out", p2))
43097 kind = OMP_CLAUSE_DEPEND_OUT;
43098 else if (!strcmp ("inout", p2))
43099 kind = OMP_CLAUSE_DEPEND_INOUT;
43100 else if (!strcmp ("mutexinoutset", p2))
43101 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43102 else if (!strcmp ("inoutset", p2))
43103 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43105 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43107 clause = error_mark_node;
43108 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43109 "%<mutexinoutset%> or %<inoutset%>");
43111 if (!c_parens.require_close (parser))
43112 cp_parser_skip_to_closing_parenthesis (parser,
43113 /*recovering=*/true,
43114 /*or_comma=*/false,
43115 /*consume_paren=*/true);
43117 else
43118 clause = error_mark_node;
43121 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43123 clause = error_mark_node;
43124 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43126 cp_parser_require_pragma_eol (parser, pragma_tok);
43128 finish_omp_depobj (loc, depobj, kind, clause);
43132 /* OpenMP 2.5:
43133 # pragma omp flush flush-vars[opt] new-line
43135 flush-vars:
43136 ( variable-list )
43138 OpenMP 5.0:
43139 # pragma omp flush memory-order-clause new-line */
43141 static void
43142 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43144 enum memmodel mo = MEMMODEL_LAST;
43145 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43146 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43147 cp_lexer_consume_token (parser->lexer);
43148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43151 const char *p = IDENTIFIER_POINTER (id);
43152 if (!strcmp (p, "seq_cst"))
43153 mo = MEMMODEL_SEQ_CST;
43154 else if (!strcmp (p, "acq_rel"))
43155 mo = MEMMODEL_ACQ_REL;
43156 else if (!strcmp (p, "release"))
43157 mo = MEMMODEL_RELEASE;
43158 else if (!strcmp (p, "acquire"))
43159 mo = MEMMODEL_ACQUIRE;
43160 else
43161 error_at (cp_lexer_peek_token (parser->lexer)->location,
43162 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43163 "%<acquire%>");
43164 cp_lexer_consume_token (parser->lexer);
43166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43168 if (mo != MEMMODEL_LAST)
43169 error_at (cp_lexer_peek_token (parser->lexer)->location,
43170 "%<flush%> list specified together with memory order "
43171 "clause");
43172 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43174 cp_parser_require_pragma_eol (parser, pragma_tok);
43176 finish_omp_flush (mo);
43179 /* Helper function, to parse omp for increment expression. */
43181 static tree
43182 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43184 tree cond = cp_parser_binary_expression (parser, false, true,
43185 PREC_NOT_OPERATOR, NULL);
43186 if (cond == error_mark_node
43187 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43189 cp_parser_skip_to_end_of_statement (parser);
43190 return error_mark_node;
43193 switch (TREE_CODE (cond))
43195 case GT_EXPR:
43196 case GE_EXPR:
43197 case LT_EXPR:
43198 case LE_EXPR:
43199 break;
43200 case NE_EXPR:
43201 if (code != OACC_LOOP)
43202 break;
43203 gcc_fallthrough ();
43204 default:
43205 return error_mark_node;
43208 /* If decl is an iterator, preserve LHS and RHS of the relational
43209 expr until finish_omp_for. */
43210 if (decl
43211 && (type_dependent_expression_p (decl)
43212 || CLASS_TYPE_P (TREE_TYPE (decl))))
43213 return cond;
43215 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
43216 TREE_CODE (cond),
43217 TREE_OPERAND (cond, 0), ERROR_MARK,
43218 TREE_OPERAND (cond, 1), ERROR_MARK,
43219 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
43222 /* Helper function, to parse omp for increment expression. */
43224 static tree
43225 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
43227 cp_token *token = cp_lexer_peek_token (parser->lexer);
43228 enum tree_code op;
43229 tree lhs, rhs;
43230 cp_id_kind idk;
43231 bool decl_first;
43233 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43235 op = (token->type == CPP_PLUS_PLUS
43236 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
43237 cp_lexer_consume_token (parser->lexer);
43238 lhs = cp_parser_simple_cast_expression (parser);
43239 if (lhs != decl
43240 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43241 return error_mark_node;
43242 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43245 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
43246 if (lhs != decl
43247 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43248 return error_mark_node;
43250 token = cp_lexer_peek_token (parser->lexer);
43251 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43253 op = (token->type == CPP_PLUS_PLUS
43254 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
43255 cp_lexer_consume_token (parser->lexer);
43256 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43259 op = cp_parser_assignment_operator_opt (parser);
43260 if (op == ERROR_MARK)
43261 return error_mark_node;
43263 if (op != NOP_EXPR)
43265 rhs = cp_parser_assignment_expression (parser);
43266 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43267 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43270 lhs = cp_parser_binary_expression (parser, false, false,
43271 PREC_ADDITIVE_EXPRESSION, NULL);
43272 token = cp_lexer_peek_token (parser->lexer);
43273 decl_first = (lhs == decl
43274 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43275 if (decl_first)
43276 lhs = NULL_TREE;
43277 if (token->type != CPP_PLUS
43278 && token->type != CPP_MINUS)
43279 return error_mark_node;
43283 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43284 cp_lexer_consume_token (parser->lexer);
43285 rhs = cp_parser_binary_expression (parser, false, false,
43286 PREC_ADDITIVE_EXPRESSION, NULL);
43287 token = cp_lexer_peek_token (parser->lexer);
43288 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43290 if (lhs == NULL_TREE)
43292 if (op == PLUS_EXPR)
43293 lhs = rhs;
43294 else
43295 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43296 NULL_TREE, tf_warning_or_error);
43298 else
43299 lhs = build_x_binary_op (input_location, op,
43300 lhs, ERROR_MARK,
43301 rhs, ERROR_MARK,
43302 NULL_TREE, NULL, tf_warning_or_error);
43305 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43307 if (!decl_first)
43309 if ((rhs != decl
43310 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43311 || op == MINUS_EXPR)
43312 return error_mark_node;
43313 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43315 else
43316 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43318 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43321 /* Parse the initialization statement of an OpenMP for loop. Range-for
43322 is handled separately in cp_convert_omp_range_for.
43324 On entry SL is the current statement list. Parsing of some forms
43325 of initialization pops this list and stores its contents in either INIT
43326 or THIS_PRE_BODY, and sets SL to null. Initialization for class
43327 iterators is added directly to SL and it is not popped until later.
43329 On return, DECL is set if the initialization is by binding the
43330 iteration variable. If the initialization is by assignment, REAL_DECL
43331 is set to point to a variable in an outer scope. ORIG_INIT is set
43332 if the iteration variable is of class type; this is a copy saved for
43333 error checking in finish_omp_for.
43335 Return true if the resulting construct should have an
43336 OMP_CLAUSE_PRIVATE added to it. */
43338 static tree
43339 cp_parser_omp_for_loop_init (cp_parser *parser,
43340 tree &this_pre_body,
43341 tree &sl,
43342 tree &init,
43343 tree &orig_init,
43344 tree &decl,
43345 tree &real_decl)
43347 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43348 return NULL_TREE;
43350 tree add_private_clause = NULL_TREE;
43352 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43354 init-expr:
43355 var = lb
43356 integer-type var = lb
43357 random-access-iterator-type var = lb
43358 pointer-type var = lb
43360 cp_decl_specifier_seq type_specifiers;
43362 /* First, try to parse as an initialized declaration. See
43363 cp_parser_condition, from whence the bulk of this is copied. */
43365 cp_parser_parse_tentatively (parser);
43366 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43367 /*is_declaration=*/true,
43368 /*is_trailing_return=*/false,
43369 &type_specifiers);
43370 if (cp_parser_parse_definitely (parser))
43372 /* If parsing a type specifier seq succeeded, then this
43373 MUST be a initialized declaration. */
43374 tree asm_specification, attributes;
43375 cp_declarator *declarator;
43377 declarator = cp_parser_declarator (parser,
43378 CP_PARSER_DECLARATOR_NAMED,
43379 CP_PARSER_FLAGS_NONE,
43380 /*ctor_dtor_or_conv_p=*/NULL,
43381 /*parenthesized_p=*/NULL,
43382 /*member_p=*/false,
43383 /*friend_p=*/false,
43384 /*static_p=*/false);
43385 attributes = cp_parser_attributes_opt (parser);
43386 asm_specification = cp_parser_asm_specification_opt (parser);
43388 if (declarator == cp_error_declarator)
43389 cp_parser_skip_to_end_of_statement (parser);
43391 else
43393 tree pushed_scope, auto_node;
43395 decl = start_decl (declarator, &type_specifiers,
43396 SD_INITIALIZED, attributes,
43397 /*prefix_attributes=*/NULL_TREE,
43398 &pushed_scope);
43400 auto_node = type_uses_auto (TREE_TYPE (decl));
43401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
43403 if (cp_lexer_next_token_is (parser->lexer,
43404 CPP_OPEN_PAREN))
43405 error ("parenthesized initialization is not allowed in "
43406 "OpenMP %<for%> loop");
43407 else
43408 /* Trigger an error. */
43409 cp_parser_require (parser, CPP_EQ, RT_EQ);
43411 init = error_mark_node;
43412 cp_parser_skip_to_end_of_statement (parser);
43414 else if (CLASS_TYPE_P (TREE_TYPE (decl))
43415 || type_dependent_expression_p (decl)
43416 || auto_node)
43418 bool is_non_constant_init;
43420 init = cp_parser_initializer (parser,
43421 /*is_direct_init=*/nullptr,
43422 &is_non_constant_init);
43424 if (auto_node)
43426 TREE_TYPE (decl)
43427 = do_auto_deduction (TREE_TYPE (decl), init,
43428 auto_node);
43430 if (!CLASS_TYPE_P (TREE_TYPE (decl))
43431 && !type_dependent_expression_p (decl))
43432 goto non_class;
43435 cp_finish_decl (decl, init, !is_non_constant_init,
43436 asm_specification,
43437 LOOKUP_ONLYCONVERTING);
43438 orig_init = init;
43440 /* In the case of a class iterator, do not pop sl here.
43441 Both class initialization and finalization must happen in
43442 the enclosing init block scope. For now set the init
43443 expression to null; it'll be filled in properly in
43444 finish_omp_for before stuffing it in the OMP_FOR. */
43445 if (CLASS_TYPE_P (TREE_TYPE (decl)))
43446 init = NULL_TREE;
43447 else /* It is a parameterized type. */
43449 init = pop_stmt_list (sl);
43450 sl = NULL_TREE;
43451 if (init && TREE_CODE (init) == STATEMENT_LIST)
43453 tree_stmt_iterator i = tsi_start (init);
43454 /* Move lambda DECL_EXPRs to the enclosing block. */
43455 while (!tsi_end_p (i))
43457 tree t = tsi_stmt (i);
43458 if (TREE_CODE (t) == DECL_EXPR
43459 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
43461 tsi_delink (&i);
43462 add_stmt (t);
43463 continue;
43465 break;
43467 if (tsi_one_before_end_p (i))
43469 tree t = tsi_stmt (i);
43470 tsi_delink (&i);
43471 free_stmt_list (init);
43472 init = t;
43477 else
43478 /* This is an initialized declaration of non-class,
43479 non-parameterized type iteration variable. */
43481 /* Consume '='. */
43482 cp_lexer_consume_token (parser->lexer);
43483 init = cp_parser_assignment_expression (parser);
43485 non_class:
43486 if (TYPE_REF_P (TREE_TYPE (decl)))
43487 init = error_mark_node;
43488 else
43489 cp_finish_decl (decl, NULL_TREE,
43490 /*init_const_expr_p=*/false,
43491 asm_specification,
43492 LOOKUP_ONLYCONVERTING);
43493 this_pre_body = pop_stmt_list (sl);
43494 sl = NULL_TREE;
43497 if (pushed_scope)
43498 pop_scope (pushed_scope);
43501 else
43503 cp_id_kind idk;
43504 /* If parsing a type specifier sequence failed, then
43505 this MUST be a simple expression. */
43506 cp_parser_parse_tentatively (parser);
43507 decl = cp_parser_primary_expression (parser, false, false,
43508 false, &idk);
43509 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
43510 if (!cp_parser_error_occurred (parser)
43511 && decl
43512 && (TREE_CODE (decl) == COMPONENT_REF
43513 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
43515 cp_parser_abort_tentative_parse (parser);
43516 cp_parser_parse_tentatively (parser);
43517 cp_token *token = cp_lexer_peek_token (parser->lexer);
43518 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
43519 /*check_dependency_p=*/true,
43520 /*template_p=*/NULL,
43521 /*declarator_p=*/false,
43522 /*optional_p=*/false);
43523 if (name != error_mark_node
43524 && last_tok == cp_lexer_peek_token (parser->lexer))
43526 decl = cp_parser_lookup_name_simple (parser, name,
43527 token->location);
43528 if (TREE_CODE (decl) == FIELD_DECL)
43529 add_private_clause = omp_privatize_field (decl, false);
43531 cp_parser_abort_tentative_parse (parser);
43532 cp_parser_parse_tentatively (parser);
43533 decl = cp_parser_primary_expression (parser, false, false,
43534 false, &idk);
43536 if (!cp_parser_error_occurred (parser)
43537 && decl
43538 && DECL_P (decl)
43539 && CLASS_TYPE_P (TREE_TYPE (decl)))
43541 tree rhs;
43543 cp_parser_parse_definitely (parser);
43544 cp_parser_require (parser, CPP_EQ, RT_EQ);
43545 rhs = cp_parser_assignment_expression (parser);
43546 orig_init = rhs;
43547 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
43548 decl, NOP_EXPR,
43549 rhs, NULL_TREE,
43550 tf_warning_or_error));
43551 if (!add_private_clause)
43552 add_private_clause = decl;
43554 else
43556 decl = NULL;
43557 cp_parser_abort_tentative_parse (parser);
43558 init = cp_parser_expression (parser);
43559 if (init)
43561 if (TREE_CODE (init) == MODIFY_EXPR
43562 || TREE_CODE (init) == MODOP_EXPR)
43563 real_decl = TREE_OPERAND (init, 0);
43566 this_pre_body = pop_stmt_list (sl);
43567 sl = NULL_TREE;
43569 return add_private_clause;
43572 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
43573 including introducing new temporaries for the range start and end,
43574 doing auto deduction, and processing decomposition variables.
43576 This function is also called from pt.cc during template instantiation.
43577 In that case SL is NULL_TREE, otherwise it is the current statement
43578 list. */
43579 void
43580 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
43581 tree &decl, tree &orig_decl, tree &init,
43582 tree &orig_init, tree &cond, tree &incr)
43584 tree begin, end, range_temp_decl = NULL_TREE;
43585 tree iter_type, begin_expr, end_expr;
43586 bool clear_has_value_expr = false;
43588 if (processing_template_decl)
43590 if (check_for_bare_parameter_packs (init))
43591 init = error_mark_node;
43592 if (!type_dependent_expression_p (init)
43593 /* do_auto_deduction doesn't mess with template init-lists. */
43594 && !BRACE_ENCLOSED_INITIALIZER_P (init))
43596 tree d = decl;
43597 cp_decomp decomp_d, *decomp = NULL;
43598 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
43600 tree v = DECL_VALUE_EXPR (decl);
43601 if (TREE_CODE (v) == ARRAY_REF
43602 && VAR_P (TREE_OPERAND (v, 0))
43603 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43605 d = TREE_OPERAND (v, 0);
43606 decomp = &decomp_d;
43607 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43608 decomp->decl = decl;
43611 do_range_for_auto_deduction (d, init, decomp);
43613 cond = global_namespace;
43614 incr = NULL_TREE;
43615 orig_init = init;
43616 if (sl)
43618 this_pre_body = pop_stmt_list (sl);
43619 sl = NULL_TREE;
43621 return;
43624 init = mark_lvalue_use (init);
43626 if (decl == error_mark_node || init == error_mark_node)
43627 /* If an error happened previously do nothing or else a lot of
43628 unhelpful errors would be issued. */
43629 begin_expr = end_expr = iter_type = error_mark_node;
43630 else
43632 tree range_temp;
43634 if (VAR_P (init)
43635 && array_of_runtime_bound_p (TREE_TYPE (init)))
43636 /* Can't bind a reference to an array of runtime bound. */
43637 range_temp = init;
43638 else
43640 range_temp = build_range_temp (init);
43641 DECL_NAME (range_temp) = NULL_TREE;
43642 pushdecl (range_temp);
43643 cp_finish_decl (range_temp, init,
43644 /*is_constant_init*/false, NULL_TREE,
43645 LOOKUP_ONLYCONVERTING);
43646 range_temp_decl = range_temp;
43647 range_temp = convert_from_reference (range_temp);
43649 iter_type = cp_parser_perform_range_for_lookup (range_temp,
43650 &begin_expr, &end_expr);
43653 tree end_iter_type = iter_type;
43654 if (cxx_dialect >= cxx17)
43655 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
43656 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
43657 TREE_USED (end) = 1;
43658 DECL_ARTIFICIAL (end) = 1;
43659 pushdecl (end);
43660 cp_finish_decl (end, end_expr,
43661 /*is_constant_init*/false, NULL_TREE,
43662 LOOKUP_ONLYCONVERTING);
43664 /* The new for initialization statement. */
43665 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
43666 TREE_USED (begin) = 1;
43667 DECL_ARTIFICIAL (begin) = 1;
43668 pushdecl (begin);
43669 orig_init = init;
43670 if (CLASS_TYPE_P (iter_type))
43671 init = NULL_TREE;
43672 else
43674 init = begin_expr;
43675 begin_expr = NULL_TREE;
43677 cp_finish_decl (begin, begin_expr,
43678 /*is_constant_init*/false, NULL_TREE,
43679 LOOKUP_ONLYCONVERTING);
43681 /* The new for condition. */
43682 if (CLASS_TYPE_P (iter_type))
43683 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
43684 else
43685 cond = build_x_binary_op (input_location, NE_EXPR,
43686 begin, ERROR_MARK,
43687 end, ERROR_MARK,
43688 NULL_TREE, NULL, tf_warning_or_error);
43690 /* The new increment expression. */
43691 if (CLASS_TYPE_P (iter_type))
43692 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
43693 else
43694 incr = finish_unary_op_expr (input_location,
43695 PREINCREMENT_EXPR, begin,
43696 tf_warning_or_error);
43698 orig_decl = decl;
43699 decl = begin;
43700 /* Defer popping sl here. */
43702 cp_decomp decomp_d, *decomp = NULL;
43703 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
43705 tree v = DECL_VALUE_EXPR (orig_decl);
43706 if (TREE_CODE (v) == ARRAY_REF
43707 && VAR_P (TREE_OPERAND (v, 0))
43708 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43710 tree d = orig_decl;
43711 orig_decl = TREE_OPERAND (v, 0);
43712 decomp = &decomp_d;
43713 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43714 decomp->decl = d;
43718 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
43719 if (auto_node)
43721 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43722 NULL_TREE, tf_none);
43723 if (!error_operand_p (t))
43725 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
43726 t, auto_node);
43727 if (decomp)
43729 ++processing_template_decl;
43730 cp_finish_decomp (orig_decl, decomp);
43731 --processing_template_decl;
43732 if (!processing_template_decl)
43733 clear_has_value_expr = true;
43738 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
43739 that holds decls for variables implementing the iterator, represented
43740 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
43741 of the vector are decls of scratch variables for the range start and
43742 end that will eventually be bound in the implicit scope surrounding
43743 the whole loop nest. The remaining elements are decls of derived
43744 decomposition variables that are bound inside the loop body. This
43745 structure is further mangled by finish_omp_for into the form required
43746 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
43747 unsigned decomp_cnt = decomp ? decomp->count : 0;
43748 tree v = make_tree_vec (decomp_cnt + 3);
43749 TREE_VEC_ELT (v, 0) = range_temp_decl;
43750 TREE_VEC_ELT (v, 1) = end;
43751 TREE_VEC_ELT (v, 2) = orig_decl;
43752 if (clear_has_value_expr)
43753 TREE_PUBLIC (v) = 1;
43754 for (unsigned i = 0; i < decomp_cnt; i++)
43756 if (clear_has_value_expr)
43758 /* If cp_finish_decomp was called with processing_template_decl
43759 temporarily set to 1, then decomp names will have deduced
43760 name but the DECL_VALUE_EXPR will be dependent. Hide those
43761 from folding of other loop initializers e.g. for warning
43762 purposes until cp_finish_omp_range_for. */
43763 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
43764 || (TREE_TYPE (decomp->decl)
43765 == error_mark_node));
43766 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
43768 TREE_VEC_ELT (v, i + 3) = decomp->decl;
43769 decomp->decl = DECL_CHAIN (decomp->decl);
43771 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
43774 /* Helper for cp_parser_omp_for_loop, finalize part of range for
43775 inside of the collapsed body. */
43777 void
43778 cp_finish_omp_range_for (tree orig, tree begin)
43780 gcc_assert (TREE_CODE (orig) == TREE_LIST
43781 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
43782 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
43783 cp_decomp decomp_d, *decomp = NULL;
43785 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43787 decomp = &decomp_d;
43788 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
43789 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
43790 if (TREE_PUBLIC (TREE_CHAIN (orig)))
43792 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
43793 by cp_convert_omp_range_for above. */
43794 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
43795 tree d = decomp_d.decl;
43796 for (unsigned i = 0; i < decomp_d.count; i++)
43798 if (TREE_TYPE (d) != error_mark_node)
43799 DECL_HAS_VALUE_EXPR_P (d) = 1;
43800 d = DECL_CHAIN (d);
43805 /* The declaration is initialized with *__begin inside the loop body. */
43806 cp_finish_decl (decl,
43807 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43808 NULL_TREE, tf_warning_or_error),
43809 /*is_constant_init*/false, NULL_TREE,
43810 LOOKUP_ONLYCONVERTING, decomp);
43811 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43812 cp_finish_decomp (decl, decomp);
43815 /* Return true if next tokens contain a standard attribute that contains
43816 omp::directive (DIRECTIVE). */
43818 static bool
43819 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
43820 bool tentative)
43822 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
43823 if (n < 10)
43824 return false;
43825 for (i = 5; i < n - 4; i++)
43826 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
43827 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
43828 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
43830 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
43831 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
43832 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
43833 continue;
43834 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
43835 break;
43837 if (i == n - 4)
43838 return false;
43839 cp_parser_parse_tentatively (parser);
43840 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
43841 location_t last_loc
43842 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
43843 location_t middle_loc = UNKNOWN_LOCATION;
43844 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
43845 int cnt = 0;
43846 bool seen = false;
43847 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
43848 if (get_attribute_namespace (attr) == omp_identifier
43849 && is_attribute_p ("directive", get_attribute_name (attr)))
43851 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
43853 tree d = TREE_VALUE (a);
43854 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
43855 cp_token *first = DEFPARSE_TOKENS (d)->first;
43856 cnt++;
43857 if (first->type == CPP_NAME
43858 && strcmp (IDENTIFIER_POINTER (first->u.value),
43859 directive) == 0)
43861 seen = true;
43862 if (middle_loc == UNKNOWN_LOCATION)
43863 middle_loc = first->location;
43867 if (!seen || tentative)
43869 cp_parser_abort_tentative_parse (parser);
43870 return seen;
43872 if (cnt != 1 || TREE_CHAIN (std_attrs))
43874 error_at (make_location (first_loc, last_loc, middle_loc),
43875 "%<[[omp::directive(%s)]]%> must be the only specified "
43876 "attribute on a statement", directive);
43877 cp_parser_abort_tentative_parse (parser);
43878 return false;
43880 if (!cp_parser_parse_definitely (parser))
43881 return false;
43882 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
43883 return true;
43886 /* Parse an OpenMP structured block sequence. KIND is the corresponding
43887 separating directive. */
43889 static tree
43890 cp_parser_omp_structured_block_sequence (cp_parser *parser,
43891 enum pragma_kind kind)
43893 tree stmt = begin_omp_structured_block ();
43894 unsigned int save = cp_parser_begin_omp_structured_block (parser);
43896 cp_parser_statement (parser, NULL_TREE, false, NULL);
43897 while (true)
43899 cp_token *token = cp_lexer_peek_token (parser->lexer);
43901 if (token->type == CPP_CLOSE_BRACE
43902 || token->type == CPP_EOF
43903 || token->type == CPP_PRAGMA_EOL
43904 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
43905 || (kind != PRAGMA_NONE
43906 && cp_parser_pragma_kind (token) == kind))
43907 break;
43909 if (kind != PRAGMA_NONE
43910 && cp_parser_omp_section_scan (parser,
43911 kind == PRAGMA_OMP_SCAN
43912 ? "scan" : "section", false))
43913 break;
43915 cp_parser_statement (parser, NULL_TREE, false, NULL);
43918 cp_parser_end_omp_structured_block (parser, save);
43919 return finish_omp_structured_block (stmt);
43923 /* OpenMP 5.0:
43925 scan-loop-body:
43926 { structured-block scan-directive structured-block } */
43928 static void
43929 cp_parser_omp_scan_loop_body (cp_parser *parser)
43931 tree substmt, clauses = NULL_TREE;
43932 bool found_scan = false;
43934 matching_braces braces;
43935 if (!braces.require_open (parser))
43936 return;
43938 cp_token *tok = cp_lexer_peek_token (parser->lexer);
43939 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
43940 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
43941 else
43943 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero preceding "
43944 "executable statements");
43945 substmt = build_empty_stmt (tok->location);
43947 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
43948 add_stmt (substmt);
43950 tok = cp_lexer_peek_token (parser->lexer);
43951 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
43953 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
43954 found_scan = true;
43956 cp_lexer_consume_token (parser->lexer);
43958 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43959 cp_lexer_consume_token (parser->lexer);
43961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43964 const char *p = IDENTIFIER_POINTER (id);
43965 if (strcmp (p, "inclusive") == 0)
43966 clause = OMP_CLAUSE_INCLUSIVE;
43967 else if (strcmp (p, "exclusive") == 0)
43968 clause = OMP_CLAUSE_EXCLUSIVE;
43970 if (clause != OMP_CLAUSE_ERROR)
43972 cp_lexer_consume_token (parser->lexer);
43973 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
43975 else
43976 cp_parser_error (parser, "expected %<inclusive%> or "
43977 "%<exclusive%> clause");
43979 cp_parser_require_pragma_eol (parser, tok);
43981 else
43982 error ("expected %<#pragma omp scan%>");
43984 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43985 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43986 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
43987 else
43989 if (found_scan)
43990 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero "
43991 "succeeding executable statements");
43992 substmt = build_empty_stmt (tok->location);
43994 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
43995 clauses);
43996 add_stmt (substmt);
43998 braces.require_close (parser);
44002 /* This function parses a single level of a loop nest, invoking itself
44003 recursively if necessary.
44005 loop-nest :: for (...) loop-body
44006 loop-body :: loop-nest
44007 | { [intervening-code] loop-body [intervening-code] }
44008 | final-loop-body
44009 intervening-code :: structured-block-sequence
44010 final-loop-body :: structured-block
44012 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
44013 all the iterator information from the inner loops into vectors in the
44014 parser->omp_for_parse_state structure.
44016 In the "range for" case, it is transformed into a regular "for" iterator
44017 by introducing some temporary variables for the begin/end,
44018 as well as bindings of the actual iteration variables which are
44019 injected into the body of the loop.
44021 Initialization code for iterator variables may end up either in the
44022 init vector (simple assignments), in omp_for_parse_state->pre_body
44023 (decl_exprs for iterators bound in the for statement), or in the
44024 scope surrounding this level of loop initialization.
44026 The scopes of class iterator variables and their finalizers need to
44027 be adjusted after parsing so that all of the initialization happens
44028 in a scope surrounding all of the intervening and body code. For
44029 this reason we separately store the initialization and body blocks
44030 for each level of loops in the omp_for_parse_state structure and
44031 reassemble/reorder them in cp_parser_omp_for. See additional
44032 comments there about the use of placeholders, etc. */
44034 static tree
44035 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
44037 tree decl, cond, incr, init;
44038 tree orig_init, real_decl, orig_decl;
44039 tree init_block, body_block;
44040 tree init_placeholder, body_placeholder;
44041 tree init_scope;
44042 tree this_pre_body = NULL_TREE;
44043 bool moreloops;
44044 unsigned char save_in_statement;
44045 tree add_private_clause = NULL_TREE;
44046 location_t loc;
44047 bool is_range_for = false;
44048 tree sl = NULL_TREE;
44049 struct omp_for_parse_data *omp_for_parse_state
44050 = parser->omp_for_parse_state;
44051 gcc_assert (omp_for_parse_state);
44052 int depth = omp_for_parse_state->depth;
44054 /* We have already matched the FOR token but not consumed it yet. */
44055 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
44056 loc = cp_lexer_consume_token (parser->lexer)->location;
44058 /* Forbid break/continue in the loop initializer, condition, and
44059 increment expressions. */
44060 save_in_statement = parser->in_statement;
44061 parser->in_statement = IN_OMP_BLOCK;
44063 /* We are not in intervening code now. */
44064 omp_for_parse_state->in_intervening_code = false;
44066 /* Don't create location wrapper nodes within an OpenMP "for"
44067 statement. */
44068 auto_suppress_location_wrappers sentinel;
44070 matching_parens parens;
44071 if (!parens.require_open (parser))
44072 return NULL;
44074 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44076 init_placeholder = build_stmt (input_location, EXPR_STMT,
44077 integer_zero_node);
44078 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44080 /* The init_block acts as a container for this level of loop goo. */
44081 init_block = push_stmt_list ();
44082 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44084 /* Wrap a scope around this entire level of loop to hold bindings
44085 of loop iteration variables. We can't insert them directly
44086 in the containing scope because that would cause their visibility to
44087 be incorrect with respect to intervening code after this loop.
44088 We will combine the nested init_scopes in postprocessing after the
44089 entire loop is parsed. */
44090 init_scope = begin_compound_stmt (0);
44092 /* Now we need another level of statement list container to capture the
44093 initialization (and possible finalization) bits. In some cases this
44094 container may be popped off during initializer parsing to store code in
44095 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44096 we have a class iterator we will pop it at the end of parsing this
44097 level, so the cleanups are handled correctly. */
44098 sl = push_stmt_list ();
44100 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44102 /* Save tokens so that we can put them back. */
44103 cp_lexer_save_tokens (parser->lexer);
44105 /* Look for ':' that is not nested in () or {}. */
44106 is_range_for
44107 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44108 /*recovering=*/false,
44109 CPP_COLON,
44110 /*consume_paren=*/
44111 false) == -1);
44113 /* Roll back the tokens we skipped. */
44114 cp_lexer_rollback_tokens (parser->lexer);
44116 if (is_range_for)
44118 bool saved_colon_corrects_to_scope_p
44119 = parser->colon_corrects_to_scope_p;
44121 /* A colon is used in range-based for. */
44122 parser->colon_corrects_to_scope_p = false;
44124 /* Parse the declaration. */
44125 cp_parser_simple_declaration (parser,
44126 /*function_definition_allowed_p=*/
44127 false, &decl);
44128 parser->colon_corrects_to_scope_p
44129 = saved_colon_corrects_to_scope_p;
44131 cp_parser_require (parser, CPP_COLON, RT_COLON);
44133 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44134 false, 0, false, true);
44136 cp_convert_omp_range_for (this_pre_body, sl, decl,
44137 orig_decl, init, orig_init,
44138 cond, incr);
44140 if (omp_for_parse_state->ordered_cl)
44141 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44142 "%<ordered%> clause with parameter on "
44143 "range-based %<for%> loop");
44145 goto parse_close_paren;
44149 add_private_clause
44150 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44151 init, orig_init, decl, real_decl);
44153 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44155 /* If the iteration variable was introduced via a declaration in the
44156 for statement, DECL points at it. Otherwise DECL is null and
44157 REAL_DECL is a variable previously declared in an outer scope.
44158 Make REAL_DECL point at the iteration variable no matter where it
44159 was introduced. */
44160 if (decl)
44161 real_decl = decl;
44163 /* Some clauses treat iterator variables specially. */
44164 if (omp_for_parse_state->cclauses != NULL
44165 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44166 && real_decl != NULL_TREE
44167 && omp_for_parse_state->code != OMP_LOOP)
44169 tree *c;
44170 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44171 *c ; )
44172 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44173 && OMP_CLAUSE_DECL (*c) == real_decl)
44175 error_at (loc, "iteration variable %qD"
44176 " should not be firstprivate", real_decl);
44177 *c = OMP_CLAUSE_CHAIN (*c);
44179 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44180 && OMP_CLAUSE_DECL (*c) == real_decl)
44182 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44183 tree l = *c;
44184 *c = OMP_CLAUSE_CHAIN (*c);
44185 if (omp_for_parse_state->code == OMP_SIMD)
44187 OMP_CLAUSE_CHAIN (l)
44188 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44189 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44191 else
44193 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
44194 omp_for_parse_state->clauses = l;
44196 add_private_clause = NULL_TREE;
44198 else
44200 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
44201 && OMP_CLAUSE_DECL (*c) == real_decl)
44202 add_private_clause = NULL_TREE;
44203 c = &OMP_CLAUSE_CHAIN (*c);
44207 if (add_private_clause)
44209 tree c;
44210 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
44212 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
44213 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
44214 && OMP_CLAUSE_DECL (c) == decl)
44215 break;
44216 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
44217 && OMP_CLAUSE_DECL (c) == decl)
44218 error_at (loc, "iteration variable %qD "
44219 "should not be firstprivate",
44220 decl);
44221 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
44222 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44223 && OMP_CLAUSE_DECL (c) == decl)
44224 error_at (loc, "iteration variable %qD should not be reduction",
44225 decl);
44227 if (c == NULL)
44229 if ((omp_for_parse_state->code == OMP_SIMD
44230 && omp_for_parse_state->count != 1)
44231 || omp_for_parse_state->code == OMP_LOOP)
44232 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
44233 else if (omp_for_parse_state->code != OMP_SIMD)
44234 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
44235 else
44236 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
44237 OMP_CLAUSE_DECL (c) = add_private_clause;
44238 c = finish_omp_clauses (c, C_ORT_OMP);
44239 if (c)
44241 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
44242 omp_for_parse_state->clauses = c;
44243 /* For linear, signal that we need to fill up
44244 the so far unknown linear step. */
44245 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
44246 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
44251 cond = NULL;
44252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44253 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
44254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44256 incr = NULL;
44257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
44259 /* If decl is an iterator, preserve the operator on decl
44260 until finish_omp_for. */
44261 if (real_decl
44262 && ((processing_template_decl
44263 && (TREE_TYPE (real_decl) == NULL_TREE
44264 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
44265 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
44266 incr = cp_parser_omp_for_incr (parser, real_decl);
44267 else
44268 incr = cp_parser_expression (parser);
44269 protected_set_expr_location_if_unset (incr, input_location);
44272 parse_close_paren:
44273 if (!parens.require_close (parser))
44274 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44275 /*or_comma=*/false,
44276 /*consume_paren=*/true);
44278 /* We've parsed all the for (...) stuff now. Store the bits. */
44279 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
44280 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
44281 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
44282 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
44283 if (orig_init)
44285 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
44286 omp_for_parse_state->orig_inits[depth] = orig_init;
44288 if (orig_decl)
44290 if (!omp_for_parse_state->orig_declv)
44291 omp_for_parse_state->orig_declv
44292 = copy_node (omp_for_parse_state->declv);
44293 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
44295 else if (omp_for_parse_state->orig_declv)
44296 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
44297 if (this_pre_body)
44298 append_to_statement_list_force (this_pre_body,
44299 &(omp_for_parse_state->pre_body));
44301 /* Start a nested block for the loop body. */
44302 body_placeholder = build_stmt (input_location, EXPR_STMT,
44303 integer_zero_node);
44304 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
44305 body_block = push_stmt_list ();
44306 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
44308 moreloops = depth < omp_for_parse_state->count - 1;
44309 omp_for_parse_state->want_nested_loop = moreloops;
44310 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44312 omp_for_parse_state->depth++;
44313 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
44314 omp_for_parse_state->depth--;
44316 else if (moreloops
44317 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44319 /* This is the open brace in the loop-body grammar production. Rather
44320 than trying to special-case braces, just parse it as a compound
44321 statement and handle the nested loop-body case there. Note that
44322 when we see a further open brace inside the compound statement
44323 loop-body, we don't know whether it is the start of intervening
44324 code that is a compound statement, or a level of braces
44325 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
44326 bit to ensure we have only one nested loop at each level. */
44328 omp_for_parse_state->in_intervening_code = true;
44329 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
44330 omp_for_parse_state->in_intervening_code = false;
44332 if (omp_for_parse_state->want_nested_loop)
44334 /* We have already parsed the whole loop body and not found a
44335 nested loop. */
44336 error_at (omp_for_parse_state->for_loc,
44337 "not enough nested loops");
44338 omp_for_parse_state->fail = true;
44340 if_p = NULL;
44342 else
44344 /* This is the final-loop-body case in the grammar: we have something
44345 that is not a FOR and not an open brace. */
44346 if (moreloops)
44348 /* If we were expecting a nested loop, give an error and mark
44349 that parsing has failed, and try to recover by parsing the
44350 body as regular code without further collapsing. */
44351 error_at (omp_for_parse_state->for_loc,
44352 "not enough nested loops");
44353 omp_for_parse_state->fail = true;
44355 parser->in_statement = IN_OMP_FOR;
44357 /* Generate the parts of range for that belong in the loop body,
44358 to be executed on every iteration. This includes setting the
44359 user-declared decomposition variables from the compiler-generated
44360 temporaries that are the real iteration variables for OMP_FOR.
44361 FIXME: Not sure if this is correct with respect to visibility
44362 of the variables from intervening code. However, putting this
44363 code in each level of loop instead of all around the innermost
44364 body also makes the decomposition variables visible to the
44365 inner for init/bound/step exressions, which is not supposed to
44366 happen and causes test failures. */
44367 if (omp_for_parse_state->orig_declv)
44368 for (int i = 0; i < omp_for_parse_state->count; i++)
44370 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
44371 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
44372 if (o != d)
44373 cp_finish_omp_range_for (o, d);
44376 /* Now parse the final-loop-body for the innermost loop. */
44377 parser->omp_for_parse_state = NULL;
44378 if (omp_for_parse_state->inscan)
44379 cp_parser_omp_scan_loop_body (parser);
44380 else
44381 cp_parser_statement (parser, NULL_TREE, false, if_p);
44382 parser->omp_for_parse_state = omp_for_parse_state;
44384 parser->in_statement = save_in_statement;
44385 omp_for_parse_state->want_nested_loop = false;
44386 omp_for_parse_state->in_intervening_code = true;
44388 /* Pop and remember the body block. Add the body placeholder
44389 to the surrounding statement list instead. This is just a unique
44390 token that will be replaced when we reassemble the generated
44391 code for the entire omp for statement. */
44392 body_block = pop_stmt_list (body_block);
44393 omp_for_parse_state->body_blockv[depth] = body_block;
44394 add_stmt (body_placeholder);
44396 /* Pop and remember the init block. */
44397 if (sl)
44398 add_stmt (pop_stmt_list (sl));
44399 finish_compound_stmt (init_scope);
44400 init_block = pop_stmt_list (init_block);
44401 omp_for_parse_state->init_blockv[depth] = init_block;
44403 /* Return the init placeholder rather than the remembered init block.
44404 Again, this is just a unique cookie that will be used to reassemble
44405 code pieces when the entire omp for statement has been parsed. */
44406 return init_placeholder;
44409 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
44410 and ITER is the element that is or contains a nested loop. This
44411 function moves the statements before and after ITER into
44412 OMP_STRUCTURED_BLOCKs and modifies *TP. */
44413 static void
44414 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
44416 tree sl = push_stmt_list ();
44417 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
44418 if (i == iter)
44420 sl = pop_stmt_list (sl);
44421 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44422 tsi_link_before (&i,
44423 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44424 TSI_SAME_STMT);
44425 i++;
44426 sl = push_stmt_list ();
44428 else
44430 tree s = tsi_stmt (i);
44431 tsi_delink (&i); /* Advances i to next statement. */
44432 add_stmt (s);
44434 sl = pop_stmt_list (sl);
44435 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44436 tsi_link_after (&iter,
44437 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44438 TSI_SAME_STMT);
44441 /* Helper to find and mark structured blocks in intervening code for a
44442 single loop level with markers for later error checking. *TP is the
44443 piece of code to be marked and INNER is the inner loop placeholder.
44444 Returns true if INNER was found (recursively) in *TP. */
44445 static bool
44446 find_structured_blocks (tree *tp, tree inner)
44448 if (*tp == inner)
44449 return true;
44450 else if (TREE_CODE (*tp) == BIND_EXPR)
44451 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
44452 else if (TREE_CODE (*tp) == STATEMENT_LIST)
44454 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
44456 tree *p = tsi_stmt_ptr (i);
44457 /* The normal case is that there is no intervening code and we
44458 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
44459 if (find_structured_blocks (p, inner))
44461 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
44462 insert_structured_blocks (tp, i);
44463 return true;
44466 return false;
44468 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
44469 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
44470 else if (TREE_CODE (*tp) == CLEANUP_STMT)
44471 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
44472 else
44473 return false;
44476 /* Helpers used for relinking tree structures: In tree rooted at
44477 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
44478 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
44480 struct sit_data {
44481 tree orig;
44482 tree repl;
44483 bool flatten;
44486 static tree
44487 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
44488 void *dp)
44490 struct sit_data *sit = (struct sit_data *)dp;
44491 if (*tp == sit->orig)
44493 *tp = sit->repl;
44494 return *tp;
44496 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
44497 trying to flatten. */
44498 else if (TREE_CODE (*tp) == BIND_EXPR
44499 && BIND_EXPR_BODY (*tp) == sit->orig
44500 && !BIND_EXPR_VARS (*tp)
44501 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
44503 *tp = sit->repl;
44504 return *tp;
44506 else if (sit->flatten
44507 && TREE_CODE (*tp) == BIND_EXPR
44508 && TREE_CODE (sit->repl) == BIND_EXPR)
44510 if (BIND_EXPR_BODY (*tp) == sit->orig)
44512 /* Merge binding lists for two directly nested BIND_EXPRs,
44513 keeping the outer one. */
44514 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
44515 BIND_EXPR_VARS (sit->repl));
44516 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
44517 return *tp;
44519 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
44520 /* There might be a statement list containing cleanup_points
44521 etc between the two levels of BIND_EXPR. We can still merge
44522 them, again keeping the outer BIND_EXPR. */
44523 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
44524 !tsi_end_p (i); ++i)
44526 tree *p = tsi_stmt_ptr (i);
44527 if (*p == sit->orig)
44529 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
44530 BIND_EXPR_VARS (sit->repl));
44531 *p = BIND_EXPR_BODY (sit->repl);
44532 return *tp;
44536 return NULL;
44539 static void
44540 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
44542 struct sit_data data;
44544 gcc_assert (*context && orig && repl);
44545 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
44546 repl = BIND_EXPR_BODY (repl);
44547 data.orig = orig;
44548 data.repl = repl;
44549 data.flatten = flatten;
44551 tree result = cp_walk_tree (context, substitute_in_tree_walker,
44552 (void *)&data, NULL);
44553 gcc_assert (result != NULL_TREE);
44556 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
44557 *DP is is the parent block. */
44559 static tree
44560 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
44562 tree superblock = *(tree *)dp;
44564 /* BIND_EXPR_BLOCK may be null if the expression is not a
44565 full-expression; if there's no block, no patching is necessary
44566 for this node. */
44567 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
44569 tree block = BIND_EXPR_BLOCK (*tp);
44570 if (superblock)
44572 BLOCK_SUPERCONTEXT (block) = superblock;
44573 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
44574 BLOCK_SUBBLOCKS (superblock) = block;
44576 BLOCK_SUBBLOCKS (block) = NULL_TREE;
44577 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
44578 (void *)&block, NULL);
44579 *walk_subtrees = 0;
44582 return NULL;
44585 /* Parse the restricted form of the for statement allowed by OpenMP. */
44587 static tree
44588 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
44589 tree *cclauses, bool *if_p)
44591 tree ret;
44592 tree cl, ordered_cl = NULL_TREE;
44593 int collapse = 1, ordered = 0;
44594 unsigned int count;
44595 bool tiling = false;
44596 bool inscan = false;
44597 struct omp_for_parse_data data;
44598 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
44599 tree result;
44600 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
44602 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
44603 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
44604 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
44605 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
44607 tiling = true;
44608 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
44610 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
44611 && OMP_CLAUSE_ORDERED_EXPR (cl))
44613 ordered_cl = cl;
44614 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
44616 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
44617 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
44618 && (code == OMP_SIMD || code == OMP_FOR))
44619 inscan = true;
44621 if (ordered && ordered < collapse)
44623 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
44624 "%<ordered%> clause parameter is less than %<collapse%>");
44625 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
44626 = build_int_cst (NULL_TREE, collapse);
44627 ordered = collapse;
44630 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
44631 count = ordered ? ordered : collapse;
44633 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44635 cp_parser_error (parser, "for statement expected");
44636 return NULL;
44639 /* Initialize parse state for recursive descent. */
44640 data.declv = make_tree_vec (count);
44641 data.initv = make_tree_vec (count);
44642 data.condv = make_tree_vec (count);
44643 data.incrv = make_tree_vec (count);
44644 data.pre_body = NULL_TREE;
44645 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
44646 data.count = count;
44647 data.depth = 0;
44648 data.want_nested_loop = true;
44649 data.ordered = ordered > 0;
44650 data.in_intervening_code = false;
44651 data.perfect_nesting_fail = false;
44652 data.fail = false;
44653 data.inscan = inscan;
44654 data.saw_intervening_code = false;
44655 data.code = code;
44656 data.orig_declv = NULL_TREE;
44657 data.clauses = clauses;
44658 data.cclauses = cclauses;
44659 data.ordered_cl = ordered_cl;
44660 parser->omp_for_parse_state = &data;
44662 cp_parser_omp_loop_nest (parser, if_p);
44664 /* Bomb out early if there was an error (not enough loops, etc). */
44665 if (data.fail || data.declv == NULL_TREE)
44667 parser->omp_for_parse_state = save_data;
44668 return NULL_TREE;
44671 /* Relink the init and body blocks that were built during parsing. At
44672 this point we have a structure nested like
44673 init 0
44674 body 0
44675 init 1
44676 body 1
44677 init 2
44678 body 2
44679 and we want to turn it into
44680 init 0
44681 init 1
44682 init 2
44683 omp_for
44684 body 0
44685 body 1
44686 body 2
44687 We also need to flatten the init blocks, as some code for later
44688 processing of combined directives gets confused otherwise. */
44690 gcc_assert (vec_safe_length (data.init_blockv) == count);
44691 gcc_assert (vec_safe_length (data.body_blockv) == count);
44692 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
44693 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
44695 /* First insert markers for structured blocks for intervening code in
44696 the loop bodies. */
44697 for (unsigned int i = 0; i < count - 1; i++)
44699 bool good = find_structured_blocks (&(data.body_blockv[i]),
44700 data.init_placeholderv[i+1]);
44701 gcc_assert (good);
44704 /* Do the substitution from the inside out. */
44705 for (unsigned int i = count - 1; i > 0; i--)
44707 substitute_in_tree (&(data.body_blockv[i-1]),
44708 data.init_placeholderv[i],
44709 data.body_blockv[i], false);
44710 substitute_in_tree (&(data.init_blockv[i-1]),
44711 data.body_placeholderv[i-1],
44712 data.init_blockv[i], true);
44715 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
44716 (and possibly other stuff) to the current statement list but
44717 returns a pointer to the OMP_FOR itself, or null in case of error. */
44718 result = push_stmt_list ();
44719 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
44720 data.initv, data.condv, data.incrv,
44721 data.body_blockv[0],
44722 data.pre_body, &data.orig_inits, data.clauses);
44723 result = pop_stmt_list (result);
44725 /* Check for errors involving lb/ub/incr expressions referencing
44726 variables declared in intervening code. */
44727 if (data.saw_intervening_code
44728 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
44729 ret = NULL_TREE;
44731 if (ret)
44733 /* Splice the omp_for into the nest of init blocks. */
44734 substitute_in_tree (&(data.init_blockv[0]),
44735 data.body_placeholderv[count - 1],
44736 result, true);
44738 /* Some later processing for combined directives assumes
44739 that the BIND_EXPR containing range for variables appears
44740 at top level in the OMP_FOR body. Fix that up if it's
44741 not the case, e.g. because there is intervening code. */
44742 if (code != OACC_LOOP)
44743 finish_omp_for_block (data.init_blockv[0], ret);
44745 /* Clean up the block subblock/superblock links. Per comment in
44746 begin_compound_stmt, "we don't build BLOCK nodes when processing
44747 templates", so skip this step in that case. */
44748 if (!processing_template_decl)
44750 tree superblock = NULL_TREE;
44751 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
44752 (void *)&superblock, NULL);
44755 /* Finally record the result. */
44756 add_stmt (data.init_blockv[0]);
44759 parser->omp_for_parse_state = save_data;
44760 return ret;
44763 /* Helper function for OpenMP parsing, split clauses and call
44764 finish_omp_clauses on each of the set of clauses afterwards. */
44766 static void
44767 cp_omp_split_clauses (location_t loc, enum tree_code code,
44768 omp_clause_mask mask, tree clauses, tree *cclauses)
44770 int i;
44771 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
44772 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
44773 if (cclauses[i])
44774 cclauses[i] = finish_omp_clauses (cclauses[i],
44775 i == C_OMP_CLAUSE_SPLIT_TARGET
44776 ? C_ORT_OMP_TARGET : C_ORT_OMP);
44779 /* OpenMP 5.0:
44780 #pragma omp loop loop-clause[optseq] new-line
44781 for-loop */
44783 #define OMP_LOOP_CLAUSE_MASK \
44784 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
44789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44791 static tree
44792 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
44793 char *p_name, omp_clause_mask mask, tree *cclauses,
44794 bool *if_p)
44796 tree clauses, sb, ret;
44797 unsigned int save;
44798 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44800 strcat (p_name, " loop");
44801 mask |= OMP_LOOP_CLAUSE_MASK;
44803 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44804 cclauses == NULL);
44805 if (cclauses)
44807 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
44808 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
44811 keep_next_level (true);
44812 sb = begin_omp_structured_block ();
44813 save = cp_parser_begin_omp_structured_block (parser);
44815 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
44817 cp_parser_end_omp_structured_block (parser, save);
44818 add_stmt (finish_omp_structured_block (sb));
44820 return ret;
44823 /* OpenMP 4.0:
44824 #pragma omp simd simd-clause[optseq] new-line
44825 for-loop */
44827 #define OMP_SIMD_CLAUSE_MASK \
44828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
44829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
44838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44840 static tree
44841 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
44842 char *p_name, omp_clause_mask mask, tree *cclauses,
44843 bool *if_p)
44845 tree clauses, sb, ret;
44846 unsigned int save;
44847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44849 strcat (p_name, " simd");
44850 mask |= OMP_SIMD_CLAUSE_MASK;
44852 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44853 cclauses == NULL);
44854 if (cclauses)
44856 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
44857 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
44860 keep_next_level (true);
44861 sb = begin_omp_structured_block ();
44862 save = cp_parser_begin_omp_structured_block (parser);
44864 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
44866 cp_parser_end_omp_structured_block (parser, save);
44867 add_stmt (finish_omp_structured_block (sb));
44869 return ret;
44872 /* OpenMP 2.5:
44873 #pragma omp for for-clause[optseq] new-line
44874 for-loop
44876 OpenMP 4.0:
44877 #pragma omp for simd for-simd-clause[optseq] new-line
44878 for-loop */
44880 #define OMP_FOR_CLAUSE_MASK \
44881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
44887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
44888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44893 static tree
44894 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
44895 char *p_name, omp_clause_mask mask, tree *cclauses,
44896 bool *if_p)
44898 tree clauses, sb, ret;
44899 unsigned int save;
44900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44902 strcat (p_name, " for");
44903 mask |= OMP_FOR_CLAUSE_MASK;
44904 /* parallel for{, simd} disallows nowait clause, but for
44905 target {teams distribute ,}parallel for{, simd} it should be accepted. */
44906 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
44907 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44908 /* Composite distribute parallel for{, simd} disallows ordered clause. */
44909 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44910 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
44912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44914 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44915 const char *p = IDENTIFIER_POINTER (id);
44917 if (strcmp (p, "simd") == 0)
44919 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44920 if (cclauses == NULL)
44921 cclauses = cclauses_buf;
44923 cp_lexer_consume_token (parser->lexer);
44924 if (!flag_openmp) /* flag_openmp_simd */
44925 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44926 cclauses, if_p);
44927 sb = begin_omp_structured_block ();
44928 save = cp_parser_begin_omp_structured_block (parser);
44929 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44930 cclauses, if_p);
44931 cp_parser_end_omp_structured_block (parser, save);
44932 tree body = finish_omp_structured_block (sb);
44933 if (ret == NULL)
44934 return ret;
44935 ret = make_node (OMP_FOR);
44936 TREE_TYPE (ret) = void_type_node;
44937 OMP_FOR_BODY (ret) = body;
44938 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44939 SET_EXPR_LOCATION (ret, loc);
44940 add_stmt (ret);
44941 return ret;
44944 if (!flag_openmp) /* flag_openmp_simd */
44946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44947 return NULL_TREE;
44950 /* Composite distribute parallel for disallows linear clause. */
44951 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44952 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
44954 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44955 cclauses == NULL);
44956 if (cclauses)
44958 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
44959 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44962 keep_next_level (true);
44963 sb = begin_omp_structured_block ();
44964 save = cp_parser_begin_omp_structured_block (parser);
44966 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
44968 cp_parser_end_omp_structured_block (parser, save);
44969 add_stmt (finish_omp_structured_block (sb));
44971 return ret;
44974 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
44975 omp_clause_mask, tree *, bool *);
44977 /* OpenMP 2.5:
44978 # pragma omp master new-line
44979 structured-block */
44981 static tree
44982 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
44983 char *p_name, omp_clause_mask mask, tree *cclauses,
44984 bool *if_p)
44986 tree clauses, sb, ret;
44987 unsigned int save;
44988 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44990 strcat (p_name, " master");
44992 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44994 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44995 const char *p = IDENTIFIER_POINTER (id);
44997 if (strcmp (p, "taskloop") == 0)
44999 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45000 if (cclauses == NULL)
45001 cclauses = cclauses_buf;
45003 cp_lexer_consume_token (parser->lexer);
45004 if (!flag_openmp) /* flag_openmp_simd */
45005 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45006 cclauses, if_p);
45007 sb = begin_omp_structured_block ();
45008 save = cp_parser_begin_omp_structured_block (parser);
45009 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45010 cclauses, if_p);
45011 cp_parser_end_omp_structured_block (parser, save);
45012 tree body = finish_omp_structured_block (sb);
45013 if (ret == NULL)
45014 return ret;
45015 ret = c_finish_omp_master (loc, body);
45016 OMP_MASTER_COMBINED (ret) = 1;
45017 return ret;
45020 if (!flag_openmp) /* flag_openmp_simd */
45022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45023 return NULL_TREE;
45026 if (cclauses)
45028 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45029 false);
45030 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45032 else
45033 cp_parser_require_pragma_eol (parser, pragma_tok);
45035 return c_finish_omp_master (loc,
45036 cp_parser_omp_structured_block (parser, if_p));
45039 /* OpenMP 5.1:
45040 # pragma omp masked masked-clauses new-line
45041 structured-block */
45043 #define OMP_MASKED_CLAUSE_MASK \
45044 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
45046 static tree
45047 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
45048 char *p_name, omp_clause_mask mask, tree *cclauses,
45049 bool *if_p)
45051 tree clauses, sb, ret;
45052 unsigned int save;
45053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45055 strcat (p_name, " masked");
45056 mask |= OMP_MASKED_CLAUSE_MASK;
45058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45061 const char *p = IDENTIFIER_POINTER (id);
45063 if (strcmp (p, "taskloop") == 0)
45065 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45066 if (cclauses == NULL)
45067 cclauses = cclauses_buf;
45069 cp_lexer_consume_token (parser->lexer);
45070 if (!flag_openmp) /* flag_openmp_simd */
45071 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45072 cclauses, if_p);
45073 sb = begin_omp_structured_block ();
45074 save = cp_parser_begin_omp_structured_block (parser);
45075 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45076 cclauses, if_p);
45077 cp_parser_end_omp_structured_block (parser, save);
45078 tree body = finish_omp_structured_block (sb);
45079 if (ret == NULL)
45080 return ret;
45081 ret = c_finish_omp_masked (loc, body,
45082 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45083 OMP_MASKED_COMBINED (ret) = 1;
45084 return ret;
45087 if (!flag_openmp) /* flag_openmp_simd */
45089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45090 return NULL_TREE;
45093 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45094 cclauses == NULL);
45095 if (cclauses)
45097 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45098 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45101 return c_finish_omp_masked (loc,
45102 cp_parser_omp_structured_block (parser, if_p),
45103 clauses);
45106 /* OpenMP 2.5:
45107 # pragma omp ordered new-line
45108 structured-block
45110 OpenMP 4.5:
45111 # pragma omp ordered ordered-clauses new-line
45112 structured-block */
45114 #define OMP_ORDERED_CLAUSE_MASK \
45115 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45118 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45122 static bool
45123 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45124 enum pragma_context context, bool *if_p)
45126 location_t loc = pragma_tok->location;
45127 int n = 1;
45129 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45130 n = 2;
45132 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45134 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45135 const char *p = IDENTIFIER_POINTER (id);
45137 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45139 if (!flag_openmp) /* flag_openmp_simd */
45141 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45142 return false;
45144 if (context == pragma_stmt)
45146 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45147 "%qs clause may only be used in compound "
45148 "statements", p);
45149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45150 return true;
45152 tree clauses
45153 = cp_parser_omp_all_clauses (parser,
45154 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45155 "#pragma omp ordered", pragma_tok);
45156 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45157 return false;
45161 tree clauses
45162 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45163 "#pragma omp ordered", pragma_tok);
45165 if (!flag_openmp /* flag_openmp_simd */
45166 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45167 return false;
45169 c_finish_omp_ordered (loc, clauses,
45170 cp_parser_omp_structured_block (parser, if_p));
45171 return true;
45174 /* OpenMP 2.5:
45176 section-scope:
45177 { section-sequence }
45179 section-sequence:
45180 section-directive[opt] structured-block
45181 section-sequence section-directive structured-block */
45183 static tree
45184 cp_parser_omp_sections_scope (cp_parser *parser)
45186 tree stmt, substmt;
45187 bool error_suppress = false;
45188 cp_token *tok;
45190 matching_braces braces;
45191 if (!braces.require_open (parser))
45192 return NULL_TREE;
45194 stmt = push_stmt_list ();
45196 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45197 != PRAGMA_OMP_SECTION
45198 && !cp_parser_omp_section_scan (parser, "section", true))
45200 substmt = cp_parser_omp_structured_block_sequence (parser,
45201 PRAGMA_OMP_SECTION);
45202 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45203 add_stmt (substmt);
45206 while (1)
45208 tok = cp_lexer_peek_token (parser->lexer);
45209 if (tok->type == CPP_CLOSE_BRACE)
45210 break;
45211 if (tok->type == CPP_EOF)
45212 break;
45214 if (cp_parser_omp_section_scan (parser, "section", false))
45215 tok = cp_lexer_peek_token (parser->lexer);
45216 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
45218 cp_lexer_consume_token (parser->lexer);
45219 cp_parser_require_pragma_eol (parser, tok);
45220 error_suppress = false;
45222 else if (!error_suppress)
45224 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
45225 error_suppress = true;
45228 substmt = cp_parser_omp_structured_block_sequence (parser,
45229 PRAGMA_OMP_SECTION);
45230 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45231 add_stmt (substmt);
45233 braces.require_close (parser);
45235 substmt = pop_stmt_list (stmt);
45237 stmt = make_node (OMP_SECTIONS);
45238 TREE_TYPE (stmt) = void_type_node;
45239 OMP_SECTIONS_BODY (stmt) = substmt;
45241 add_stmt (stmt);
45242 return stmt;
45245 /* OpenMP 2.5:
45246 # pragma omp sections sections-clause[optseq] newline
45247 sections-scope */
45249 #define OMP_SECTIONS_CLAUSE_MASK \
45250 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45257 static tree
45258 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
45259 char *p_name, omp_clause_mask mask, tree *cclauses)
45261 tree clauses, ret;
45262 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45264 strcat (p_name, " sections");
45265 mask |= OMP_SECTIONS_CLAUSE_MASK;
45266 if (cclauses)
45267 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45269 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45270 cclauses == NULL);
45271 if (cclauses)
45273 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
45274 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
45277 ret = cp_parser_omp_sections_scope (parser);
45278 if (ret)
45279 OMP_SECTIONS_CLAUSES (ret) = clauses;
45281 return ret;
45284 /* OpenMP 2.5:
45285 # pragma omp parallel parallel-clause[optseq] new-line
45286 structured-block
45287 # pragma omp parallel for parallel-for-clause[optseq] new-line
45288 structured-block
45289 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
45290 structured-block
45292 OpenMP 4.0:
45293 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
45294 structured-block */
45296 #define OMP_PARALLEL_CLAUSE_MASK \
45297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
45303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
45305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
45308 static tree
45309 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
45310 char *p_name, omp_clause_mask mask, tree *cclauses,
45311 bool *if_p)
45313 tree stmt, clauses, block;
45314 unsigned int save;
45315 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45317 strcat (p_name, " parallel");
45318 mask |= OMP_PARALLEL_CLAUSE_MASK;
45319 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
45320 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
45321 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
45322 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
45324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45326 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45327 if (cclauses == NULL)
45328 cclauses = cclauses_buf;
45330 cp_lexer_consume_token (parser->lexer);
45331 if (!flag_openmp) /* flag_openmp_simd */
45332 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45333 if_p);
45334 block = begin_omp_parallel ();
45335 save = cp_parser_begin_omp_structured_block (parser);
45336 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45337 if_p);
45338 cp_parser_end_omp_structured_block (parser, save);
45339 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45340 block);
45341 if (ret == NULL_TREE)
45342 return ret;
45343 OMP_PARALLEL_COMBINED (stmt) = 1;
45344 return stmt;
45346 /* When combined with distribute, parallel has to be followed by for.
45347 #pragma omp target parallel is allowed though. */
45348 else if (cclauses
45349 && (mask & (OMP_CLAUSE_MASK_1
45350 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45352 error_at (loc, "expected %<for%> after %qs", p_name);
45353 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45354 return NULL_TREE;
45356 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45359 const char *p = IDENTIFIER_POINTER (id);
45360 if (cclauses == NULL && strcmp (p, "masked") == 0)
45362 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45363 cclauses = cclauses_buf;
45365 cp_lexer_consume_token (parser->lexer);
45366 if (!flag_openmp) /* flag_openmp_simd */
45367 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45368 cclauses, if_p);
45369 block = begin_omp_parallel ();
45370 save = cp_parser_begin_omp_structured_block (parser);
45371 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45372 cclauses, if_p);
45373 cp_parser_end_omp_structured_block (parser, save);
45374 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45375 block);
45376 if (ret == NULL_TREE)
45377 return ret;
45378 /* masked does have just filter clause, but during gimplification
45379 isn't represented by a gimplification omp context, so for
45380 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
45381 so that
45382 #pragma omp parallel masked
45383 #pragma omp taskloop simd lastprivate (x)
45384 isn't confused with
45385 #pragma omp parallel masked taskloop simd lastprivate (x) */
45386 if (OMP_MASKED_COMBINED (ret))
45387 OMP_PARALLEL_COMBINED (stmt) = 1;
45388 return stmt;
45390 else if (cclauses == NULL && strcmp (p, "master") == 0)
45392 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45393 cclauses = cclauses_buf;
45395 cp_lexer_consume_token (parser->lexer);
45396 if (!flag_openmp) /* flag_openmp_simd */
45397 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45398 cclauses, if_p);
45399 block = begin_omp_parallel ();
45400 save = cp_parser_begin_omp_structured_block (parser);
45401 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45402 cclauses, if_p);
45403 cp_parser_end_omp_structured_block (parser, save);
45404 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45405 block);
45406 if (ret == NULL_TREE)
45407 return ret;
45408 /* master doesn't have any clauses and during gimplification
45409 isn't represented by a gimplification omp context, so for
45410 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
45411 so that
45412 #pragma omp parallel master
45413 #pragma omp taskloop simd lastprivate (x)
45414 isn't confused with
45415 #pragma omp parallel master taskloop simd lastprivate (x) */
45416 if (OMP_MASTER_COMBINED (ret))
45417 OMP_PARALLEL_COMBINED (stmt) = 1;
45418 return stmt;
45420 else if (strcmp (p, "loop") == 0)
45422 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45423 if (cclauses == NULL)
45424 cclauses = cclauses_buf;
45426 cp_lexer_consume_token (parser->lexer);
45427 if (!flag_openmp) /* flag_openmp_simd */
45428 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45429 cclauses, if_p);
45430 block = begin_omp_parallel ();
45431 save = cp_parser_begin_omp_structured_block (parser);
45432 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45433 cclauses, if_p);
45434 cp_parser_end_omp_structured_block (parser, save);
45435 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45436 block);
45437 if (ret == NULL_TREE)
45438 return ret;
45439 OMP_PARALLEL_COMBINED (stmt) = 1;
45440 return stmt;
45442 else if (!flag_openmp) /* flag_openmp_simd */
45444 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45445 return NULL_TREE;
45447 else if (cclauses == NULL && strcmp (p, "sections") == 0)
45449 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45450 cclauses = cclauses_buf;
45452 cp_lexer_consume_token (parser->lexer);
45453 block = begin_omp_parallel ();
45454 save = cp_parser_begin_omp_structured_block (parser);
45455 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
45456 cp_parser_end_omp_structured_block (parser, save);
45457 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45458 block);
45459 OMP_PARALLEL_COMBINED (stmt) = 1;
45460 return stmt;
45463 else if (!flag_openmp) /* flag_openmp_simd */
45465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45466 return NULL_TREE;
45469 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45470 cclauses == NULL);
45471 if (cclauses)
45473 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
45474 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
45477 block = begin_omp_parallel ();
45478 save = cp_parser_begin_omp_structured_block (parser);
45479 parser->omp_attrs_forbidden_p = true;
45480 cp_parser_statement (parser, NULL_TREE, false, if_p);
45481 cp_parser_end_omp_structured_block (parser, save);
45482 stmt = finish_omp_parallel (clauses, block);
45483 return stmt;
45486 /* OpenMP 2.5:
45487 # pragma omp single single-clause[optseq] new-line
45488 structured-block */
45490 #define OMP_SINGLE_CLAUSE_MASK \
45491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
45494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45497 static tree
45498 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45500 tree stmt = make_node (OMP_SINGLE);
45501 TREE_TYPE (stmt) = void_type_node;
45502 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45504 OMP_SINGLE_CLAUSES (stmt)
45505 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
45506 "#pragma omp single", pragma_tok);
45507 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45509 return add_stmt (stmt);
45512 /* OpenMP 5.1:
45513 # pragma omp scope scope-clause[optseq] new-line
45514 structured-block */
45516 #define OMP_SCOPE_CLAUSE_MASK \
45517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45523 static tree
45524 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45526 tree stmt = make_node (OMP_SCOPE);
45527 TREE_TYPE (stmt) = void_type_node;
45528 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45530 OMP_SCOPE_CLAUSES (stmt)
45531 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
45532 "#pragma omp scope", pragma_tok);
45533 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45535 return add_stmt (stmt);
45538 /* OpenMP 3.0:
45539 # pragma omp task task-clause[optseq] new-line
45540 structured-block */
45542 #define OMP_TASK_CLAUSE_MASK \
45543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
45545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
45550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
45551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
45553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
45555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
45556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
45558 static tree
45559 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45561 tree clauses, block;
45562 unsigned int save;
45564 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
45565 "#pragma omp task", pragma_tok);
45566 block = begin_omp_task ();
45567 save = cp_parser_begin_omp_structured_block (parser);
45568 parser->omp_attrs_forbidden_p = true;
45569 cp_parser_statement (parser, NULL_TREE, false, if_p);
45570 cp_parser_end_omp_structured_block (parser, save);
45571 return finish_omp_task (clauses, block);
45574 /* OpenMP 3.0:
45575 # pragma omp taskwait new-line
45577 OpenMP 5.0:
45578 # pragma omp taskwait taskwait-clause[opt] new-line */
45580 #define OMP_TASKWAIT_CLAUSE_MASK \
45581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45584 static void
45585 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
45587 tree clauses
45588 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
45589 "#pragma omp taskwait", pragma_tok);
45591 if (clauses)
45593 tree stmt = make_node (OMP_TASK);
45594 TREE_TYPE (stmt) = void_node;
45595 OMP_TASK_CLAUSES (stmt) = clauses;
45596 OMP_TASK_BODY (stmt) = NULL_TREE;
45597 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45598 add_stmt (stmt);
45600 else
45601 finish_omp_taskwait ();
45604 /* OpenMP 3.1:
45605 # pragma omp taskyield new-line */
45607 static void
45608 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
45610 cp_parser_require_pragma_eol (parser, pragma_tok);
45611 finish_omp_taskyield ();
45614 /* OpenMP 4.0:
45615 # pragma omp taskgroup new-line
45616 structured-block
45618 OpenMP 5.0:
45619 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
45621 #define OMP_TASKGROUP_CLAUSE_MASK \
45622 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
45625 static tree
45626 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45628 tree clauses
45629 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
45630 "#pragma omp taskgroup", pragma_tok);
45631 return c_finish_omp_taskgroup (input_location,
45632 cp_parser_omp_structured_block (parser,
45633 if_p),
45634 clauses);
45638 /* OpenMP 2.5:
45639 # pragma omp threadprivate (variable-list) */
45641 static void
45642 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
45644 tree vars;
45646 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
45647 cp_parser_require_pragma_eol (parser, pragma_tok);
45649 finish_omp_threadprivate (vars);
45652 /* OpenMP 4.0:
45653 # pragma omp cancel cancel-clause[optseq] new-line */
45655 #define OMP_CANCEL_CLAUSE_MASK \
45656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
45657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
45658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
45659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
45660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
45662 static void
45663 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
45665 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
45666 "#pragma omp cancel", pragma_tok);
45667 finish_omp_cancel (clauses);
45670 /* OpenMP 4.0:
45671 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
45673 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
45674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
45675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
45676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
45677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
45679 static bool
45680 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
45681 enum pragma_context context)
45683 tree clauses;
45684 bool point_seen = false;
45686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45688 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45689 const char *p = IDENTIFIER_POINTER (id);
45691 if (strcmp (p, "point") == 0)
45693 cp_lexer_consume_token (parser->lexer);
45694 point_seen = true;
45697 if (!point_seen)
45699 cp_parser_error (parser, "expected %<point%>");
45700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45701 return false;
45704 if (context != pragma_compound)
45706 if (context == pragma_stmt)
45707 error_at (pragma_tok->location,
45708 "%<#pragma %s%> may only be used in compound statements",
45709 "omp cancellation point");
45710 else
45711 cp_parser_error (parser, "expected declaration specifiers");
45712 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45713 return true;
45716 clauses = cp_parser_omp_all_clauses (parser,
45717 OMP_CANCELLATION_POINT_CLAUSE_MASK,
45718 "#pragma omp cancellation point",
45719 pragma_tok);
45720 finish_omp_cancellation_point (clauses);
45721 return true;
45724 /* OpenMP 4.0:
45725 #pragma omp distribute distribute-clause[optseq] new-line
45726 for-loop */
45728 #define OMP_DISTRIBUTE_CLAUSE_MASK \
45729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
45733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45737 static tree
45738 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
45739 char *p_name, omp_clause_mask mask, tree *cclauses,
45740 bool *if_p)
45742 tree clauses, sb, ret;
45743 unsigned int save;
45744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45746 strcat (p_name, " distribute");
45747 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
45749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45752 const char *p = IDENTIFIER_POINTER (id);
45753 bool simd = false;
45754 bool parallel = false;
45756 if (strcmp (p, "simd") == 0)
45757 simd = true;
45758 else
45759 parallel = strcmp (p, "parallel") == 0;
45760 if (parallel || simd)
45762 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45763 if (cclauses == NULL)
45764 cclauses = cclauses_buf;
45765 cp_lexer_consume_token (parser->lexer);
45766 if (!flag_openmp) /* flag_openmp_simd */
45768 if (simd)
45769 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45770 cclauses, if_p);
45771 else
45772 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45773 cclauses, if_p);
45775 sb = begin_omp_structured_block ();
45776 save = cp_parser_begin_omp_structured_block (parser);
45777 if (simd)
45778 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45779 cclauses, if_p);
45780 else
45781 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45782 cclauses, if_p);
45783 cp_parser_end_omp_structured_block (parser, save);
45784 tree body = finish_omp_structured_block (sb);
45785 if (ret == NULL)
45786 return ret;
45787 ret = make_node (OMP_DISTRIBUTE);
45788 TREE_TYPE (ret) = void_type_node;
45789 OMP_FOR_BODY (ret) = body;
45790 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45791 SET_EXPR_LOCATION (ret, loc);
45792 add_stmt (ret);
45793 return ret;
45796 if (!flag_openmp) /* flag_openmp_simd */
45798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45799 return NULL_TREE;
45802 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45803 cclauses == NULL);
45804 if (cclauses)
45806 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
45807 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45810 keep_next_level (true);
45811 sb = begin_omp_structured_block ();
45812 save = cp_parser_begin_omp_structured_block (parser);
45814 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
45816 cp_parser_end_omp_structured_block (parser, save);
45817 add_stmt (finish_omp_structured_block (sb));
45819 return ret;
45822 /* OpenMP 4.0:
45823 # pragma omp teams teams-clause[optseq] new-line
45824 structured-block */
45826 #define OMP_TEAMS_CLAUSE_MASK \
45827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
45832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
45836 static tree
45837 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
45838 char *p_name, omp_clause_mask mask, tree *cclauses,
45839 bool *if_p)
45841 tree clauses, sb, ret;
45842 unsigned int save;
45843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45845 strcat (p_name, " teams");
45846 mask |= OMP_TEAMS_CLAUSE_MASK;
45848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45851 const char *p = IDENTIFIER_POINTER (id);
45852 if (strcmp (p, "distribute") == 0)
45854 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45855 if (cclauses == NULL)
45856 cclauses = cclauses_buf;
45858 cp_lexer_consume_token (parser->lexer);
45859 if (!flag_openmp) /* flag_openmp_simd */
45860 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45861 cclauses, if_p);
45862 keep_next_level (true);
45863 sb = begin_omp_structured_block ();
45864 save = cp_parser_begin_omp_structured_block (parser);
45865 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45866 cclauses, if_p);
45867 cp_parser_end_omp_structured_block (parser, save);
45868 tree body = finish_omp_structured_block (sb);
45869 if (ret == NULL)
45870 return ret;
45871 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45872 ret = make_node (OMP_TEAMS);
45873 TREE_TYPE (ret) = void_type_node;
45874 OMP_TEAMS_CLAUSES (ret) = clauses;
45875 OMP_TEAMS_BODY (ret) = body;
45876 OMP_TEAMS_COMBINED (ret) = 1;
45877 SET_EXPR_LOCATION (ret, loc);
45878 return add_stmt (ret);
45880 else if (strcmp (p, "loop") == 0)
45882 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45883 if (cclauses == NULL)
45884 cclauses = cclauses_buf;
45886 cp_lexer_consume_token (parser->lexer);
45887 if (!flag_openmp) /* flag_openmp_simd */
45888 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45889 cclauses, if_p);
45890 keep_next_level (true);
45891 sb = begin_omp_structured_block ();
45892 save = cp_parser_begin_omp_structured_block (parser);
45893 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45894 cclauses, if_p);
45895 cp_parser_end_omp_structured_block (parser, save);
45896 tree body = finish_omp_structured_block (sb);
45897 if (ret == NULL)
45898 return ret;
45899 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45900 ret = make_node (OMP_TEAMS);
45901 TREE_TYPE (ret) = void_type_node;
45902 OMP_TEAMS_CLAUSES (ret) = clauses;
45903 OMP_TEAMS_BODY (ret) = body;
45904 OMP_TEAMS_COMBINED (ret) = 1;
45905 SET_EXPR_LOCATION (ret, loc);
45906 return add_stmt (ret);
45909 if (!flag_openmp) /* flag_openmp_simd */
45911 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45912 return NULL_TREE;
45915 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45916 cclauses == NULL);
45917 if (cclauses)
45919 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
45920 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45923 tree stmt = make_node (OMP_TEAMS);
45924 TREE_TYPE (stmt) = void_type_node;
45925 OMP_TEAMS_CLAUSES (stmt) = clauses;
45926 keep_next_level (true);
45927 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45928 SET_EXPR_LOCATION (stmt, loc);
45930 return add_stmt (stmt);
45933 /* OpenMP 4.0:
45934 # pragma omp target data target-data-clause[optseq] new-line
45935 structured-block */
45937 #define OMP_TARGET_DATA_CLAUSE_MASK \
45938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
45942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
45944 static tree
45945 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45947 if (flag_openmp)
45948 omp_requires_mask
45949 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45951 tree clauses
45952 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
45953 "#pragma omp target data", pragma_tok);
45954 c_omp_adjust_map_clauses (clauses, false);
45955 int map_seen = 0;
45956 for (tree *pc = &clauses; *pc;)
45958 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45959 switch (OMP_CLAUSE_MAP_KIND (*pc))
45961 case GOMP_MAP_TO:
45962 case GOMP_MAP_ALWAYS_TO:
45963 case GOMP_MAP_PRESENT_TO:
45964 case GOMP_MAP_ALWAYS_PRESENT_TO:
45965 case GOMP_MAP_FROM:
45966 case GOMP_MAP_ALWAYS_FROM:
45967 case GOMP_MAP_PRESENT_FROM:
45968 case GOMP_MAP_ALWAYS_PRESENT_FROM:
45969 case GOMP_MAP_TOFROM:
45970 case GOMP_MAP_ALWAYS_TOFROM:
45971 case GOMP_MAP_PRESENT_TOFROM:
45972 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
45973 case GOMP_MAP_ALLOC:
45974 case GOMP_MAP_PRESENT_ALLOC:
45975 map_seen = 3;
45976 break;
45977 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45978 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45979 case GOMP_MAP_ALWAYS_POINTER:
45980 case GOMP_MAP_ATTACH_DETACH:
45981 break;
45982 default:
45983 map_seen |= 1;
45984 error_at (OMP_CLAUSE_LOCATION (*pc),
45985 "%<#pragma omp target data%> with map-type other "
45986 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
45987 "on %<map%> clause");
45988 *pc = OMP_CLAUSE_CHAIN (*pc);
45989 continue;
45991 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
45992 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
45993 map_seen = 3;
45994 pc = &OMP_CLAUSE_CHAIN (*pc);
45997 if (map_seen != 3)
45999 if (map_seen == 0)
46000 error_at (pragma_tok->location,
46001 "%<#pragma omp target data%> must contain at least "
46002 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
46003 "clause");
46004 return NULL_TREE;
46007 tree stmt = make_node (OMP_TARGET_DATA);
46008 TREE_TYPE (stmt) = void_type_node;
46009 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
46011 keep_next_level (true);
46012 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46014 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46015 return add_stmt (stmt);
46018 /* OpenMP 4.5:
46019 # pragma omp target enter data target-enter-data-clause[optseq] new-line
46020 structured-block */
46022 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
46023 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46029 static bool
46030 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
46031 enum pragma_context context)
46033 bool data_seen = false;
46034 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46036 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46037 const char *p = IDENTIFIER_POINTER (id);
46039 if (strcmp (p, "data") == 0)
46041 cp_lexer_consume_token (parser->lexer);
46042 data_seen = true;
46045 if (!data_seen)
46047 cp_parser_error (parser, "expected %<data%>");
46048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46049 return false;
46052 if (context == pragma_stmt)
46054 error_at (pragma_tok->location,
46055 "%<#pragma %s%> may only be used in compound statements",
46056 "omp target enter data");
46057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46058 return true;
46061 if (flag_openmp)
46062 omp_requires_mask
46063 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46065 tree clauses
46066 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
46067 "#pragma omp target enter data", pragma_tok);
46068 c_omp_adjust_map_clauses (clauses, false);
46069 int map_seen = 0;
46070 for (tree *pc = &clauses; *pc;)
46072 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46073 switch (OMP_CLAUSE_MAP_KIND (*pc))
46075 case GOMP_MAP_TO:
46076 case GOMP_MAP_ALWAYS_TO:
46077 case GOMP_MAP_PRESENT_TO:
46078 case GOMP_MAP_ALWAYS_PRESENT_TO:
46079 case GOMP_MAP_ALLOC:
46080 case GOMP_MAP_PRESENT_ALLOC:
46081 map_seen = 3;
46082 break;
46083 case GOMP_MAP_TOFROM:
46084 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46085 map_seen = 3;
46086 break;
46087 case GOMP_MAP_ALWAYS_TOFROM:
46088 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46089 map_seen = 3;
46090 break;
46091 case GOMP_MAP_PRESENT_TOFROM:
46092 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46093 map_seen = 3;
46094 break;
46095 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46096 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46097 map_seen = 3;
46098 break;
46099 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46100 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46101 case GOMP_MAP_ALWAYS_POINTER:
46102 case GOMP_MAP_ATTACH_DETACH:
46103 break;
46104 default:
46105 map_seen |= 1;
46106 error_at (OMP_CLAUSE_LOCATION (*pc),
46107 "%<#pragma omp target enter data%> with map-type other "
46108 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46109 *pc = OMP_CLAUSE_CHAIN (*pc);
46110 continue;
46112 pc = &OMP_CLAUSE_CHAIN (*pc);
46115 if (map_seen != 3)
46117 if (map_seen == 0)
46118 error_at (pragma_tok->location,
46119 "%<#pragma omp target enter data%> must contain at least "
46120 "one %<map%> clause");
46121 return true;
46124 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46125 TREE_TYPE (stmt) = void_type_node;
46126 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46127 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46128 add_stmt (stmt);
46129 return true;
46132 /* OpenMP 4.5:
46133 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46134 structured-block */
46136 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46143 static bool
46144 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46145 enum pragma_context context)
46147 bool data_seen = false;
46148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46151 const char *p = IDENTIFIER_POINTER (id);
46153 if (strcmp (p, "data") == 0)
46155 cp_lexer_consume_token (parser->lexer);
46156 data_seen = true;
46159 if (!data_seen)
46161 cp_parser_error (parser, "expected %<data%>");
46162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46163 return false;
46166 if (context == pragma_stmt)
46168 error_at (pragma_tok->location,
46169 "%<#pragma %s%> may only be used in compound statements",
46170 "omp target exit data");
46171 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46172 return true;
46175 if (flag_openmp)
46176 omp_requires_mask
46177 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46179 tree clauses
46180 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46181 "#pragma omp target exit data", pragma_tok);
46182 c_omp_adjust_map_clauses (clauses, false);
46183 int map_seen = 0;
46184 for (tree *pc = &clauses; *pc;)
46186 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46187 switch (OMP_CLAUSE_MAP_KIND (*pc))
46189 case GOMP_MAP_FROM:
46190 case GOMP_MAP_ALWAYS_FROM:
46191 case GOMP_MAP_PRESENT_FROM:
46192 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46193 case GOMP_MAP_RELEASE:
46194 case GOMP_MAP_DELETE:
46195 map_seen = 3;
46196 break;
46197 case GOMP_MAP_TOFROM:
46198 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
46199 map_seen = 3;
46200 break;
46201 case GOMP_MAP_ALWAYS_TOFROM:
46202 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
46203 map_seen = 3;
46204 break;
46205 case GOMP_MAP_PRESENT_TOFROM:
46206 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
46207 map_seen = 3;
46208 break;
46209 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46210 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
46211 map_seen = 3;
46212 break;
46213 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46214 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46215 case GOMP_MAP_ALWAYS_POINTER:
46216 case GOMP_MAP_ATTACH_DETACH:
46217 break;
46218 default:
46219 map_seen |= 1;
46220 error_at (OMP_CLAUSE_LOCATION (*pc),
46221 "%<#pragma omp target exit data%> with map-type other "
46222 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
46223 "on %<map%> clause");
46224 *pc = OMP_CLAUSE_CHAIN (*pc);
46225 continue;
46227 pc = &OMP_CLAUSE_CHAIN (*pc);
46230 if (map_seen != 3)
46232 if (map_seen == 0)
46233 error_at (pragma_tok->location,
46234 "%<#pragma omp target exit data%> must contain at least "
46235 "one %<map%> clause");
46236 return true;
46239 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
46240 TREE_TYPE (stmt) = void_type_node;
46241 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
46242 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46243 add_stmt (stmt);
46244 return true;
46247 /* OpenMP 4.0:
46248 # pragma omp target update target-update-clause[optseq] new-line */
46250 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
46251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
46252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46258 static bool
46259 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
46260 enum pragma_context context)
46262 if (context == pragma_stmt)
46264 error_at (pragma_tok->location,
46265 "%<#pragma %s%> may only be used in compound statements",
46266 "omp target update");
46267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46268 return true;
46271 tree clauses
46272 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
46273 "#pragma omp target update", pragma_tok);
46274 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
46275 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
46277 error_at (pragma_tok->location,
46278 "%<#pragma omp target update%> must contain at least one "
46279 "%<from%> or %<to%> clauses");
46280 return true;
46283 if (flag_openmp)
46284 omp_requires_mask
46285 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46287 tree stmt = make_node (OMP_TARGET_UPDATE);
46288 TREE_TYPE (stmt) = void_type_node;
46289 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
46290 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46291 add_stmt (stmt);
46292 return true;
46295 /* OpenMP 4.0:
46296 # pragma omp target target-clause[optseq] new-line
46297 structured-block */
46299 #define OMP_TARGET_CLAUSE_MASK \
46300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
46308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
46312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
46314 static bool
46315 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
46316 enum pragma_context context, bool *if_p)
46318 if (flag_openmp)
46319 omp_requires_mask
46320 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46325 const char *p = IDENTIFIER_POINTER (id);
46326 enum tree_code ccode = ERROR_MARK;
46328 if (strcmp (p, "teams") == 0)
46329 ccode = OMP_TEAMS;
46330 else if (strcmp (p, "parallel") == 0)
46331 ccode = OMP_PARALLEL;
46332 else if (strcmp (p, "simd") == 0)
46333 ccode = OMP_SIMD;
46334 if (ccode != ERROR_MARK)
46336 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
46337 char p_name[sizeof ("#pragma omp target teams distribute "
46338 "parallel for simd")];
46340 cp_lexer_consume_token (parser->lexer);
46341 strcpy (p_name, "#pragma omp target");
46342 if (!flag_openmp) /* flag_openmp_simd */
46344 tree stmt;
46345 switch (ccode)
46347 case OMP_TEAMS:
46348 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
46349 OMP_TARGET_CLAUSE_MASK,
46350 cclauses, if_p);
46351 break;
46352 case OMP_PARALLEL:
46353 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46354 OMP_TARGET_CLAUSE_MASK,
46355 cclauses, if_p);
46356 break;
46357 case OMP_SIMD:
46358 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
46359 OMP_TARGET_CLAUSE_MASK,
46360 cclauses, if_p);
46361 break;
46362 default:
46363 gcc_unreachable ();
46365 return stmt != NULL_TREE;
46367 keep_next_level (true);
46368 tree sb = begin_omp_structured_block (), ret;
46369 unsigned save = cp_parser_begin_omp_structured_block (parser);
46370 switch (ccode)
46372 case OMP_TEAMS:
46373 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
46374 OMP_TARGET_CLAUSE_MASK, cclauses,
46375 if_p);
46376 break;
46377 case OMP_PARALLEL:
46378 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46379 OMP_TARGET_CLAUSE_MASK, cclauses,
46380 if_p);
46381 break;
46382 case OMP_SIMD:
46383 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
46384 OMP_TARGET_CLAUSE_MASK, cclauses,
46385 if_p);
46386 break;
46387 default:
46388 gcc_unreachable ();
46390 cp_parser_end_omp_structured_block (parser, save);
46391 tree body = finish_omp_structured_block (sb);
46392 if (ret == NULL_TREE)
46393 return false;
46394 if (ccode == OMP_TEAMS && !processing_template_decl)
46395 /* For combined target teams, ensure the num_teams and
46396 thread_limit clause expressions are evaluated on the host,
46397 before entering the target construct. */
46398 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46399 c; c = OMP_CLAUSE_CHAIN (c))
46400 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
46401 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
46402 for (int i = 0;
46403 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
46404 if (OMP_CLAUSE_OPERAND (c, i)
46405 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
46407 tree expr = OMP_CLAUSE_OPERAND (c, i);
46408 expr = force_target_expr (TREE_TYPE (expr), expr,
46409 tf_none);
46410 if (expr == error_mark_node)
46411 continue;
46412 tree tmp = TARGET_EXPR_SLOT (expr);
46413 add_stmt (expr);
46414 OMP_CLAUSE_OPERAND (c, i) = expr;
46415 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
46416 OMP_CLAUSE_FIRSTPRIVATE);
46417 OMP_CLAUSE_DECL (tc) = tmp;
46418 OMP_CLAUSE_CHAIN (tc)
46419 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
46420 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
46422 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
46423 finish_omp_target (pragma_tok->location,
46424 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
46425 return true;
46427 else if (!flag_openmp) /* flag_openmp_simd */
46429 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46430 return false;
46432 else if (strcmp (p, "data") == 0)
46434 cp_lexer_consume_token (parser->lexer);
46435 cp_parser_omp_target_data (parser, pragma_tok, if_p);
46436 return true;
46438 else if (strcmp (p, "enter") == 0)
46440 cp_lexer_consume_token (parser->lexer);
46441 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
46443 else if (strcmp (p, "exit") == 0)
46445 cp_lexer_consume_token (parser->lexer);
46446 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
46448 else if (strcmp (p, "update") == 0)
46450 cp_lexer_consume_token (parser->lexer);
46451 return cp_parser_omp_target_update (parser, pragma_tok, context);
46454 if (!flag_openmp) /* flag_openmp_simd */
46456 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46457 return false;
46460 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
46461 "#pragma omp target", pragma_tok,
46462 false);
46463 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46464 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
46466 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
46467 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
46468 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
46469 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
46470 OMP_CLAUSE_CHAIN (c) = nc;
46472 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
46474 c_omp_adjust_map_clauses (clauses, true);
46475 keep_next_level (true);
46476 tree body = cp_parser_omp_structured_block (parser, if_p);
46478 finish_omp_target (pragma_tok->location, clauses, body, false);
46479 return true;
46482 /* OpenACC 2.0:
46483 # pragma acc cache (variable-list) new-line
46486 static tree
46487 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
46489 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
46490 clauses. */
46491 auto_suppress_location_wrappers sentinel;
46493 tree stmt, clauses;
46495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
46496 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46498 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
46500 stmt = make_node (OACC_CACHE);
46501 TREE_TYPE (stmt) = void_type_node;
46502 OACC_CACHE_CLAUSES (stmt) = clauses;
46503 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46504 add_stmt (stmt);
46506 return stmt;
46509 /* OpenACC 2.0:
46510 # pragma acc data oacc-data-clause[optseq] new-line
46511 structured-block */
46513 #define OACC_DATA_CLAUSE_MASK \
46514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
46526 static tree
46527 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46529 tree stmt, clauses, block;
46530 unsigned int save;
46532 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
46533 "#pragma acc data", pragma_tok);
46535 block = begin_omp_parallel ();
46536 save = cp_parser_begin_omp_structured_block (parser);
46537 cp_parser_statement (parser, NULL_TREE, false, if_p);
46538 cp_parser_end_omp_structured_block (parser, save);
46539 stmt = finish_oacc_data (clauses, block);
46540 return stmt;
46543 /* OpenACC 2.0:
46544 # pragma acc host_data <clauses> new-line
46545 structured-block */
46547 #define OACC_HOST_DATA_CLAUSE_MASK \
46548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
46549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
46552 static tree
46553 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46555 tree stmt, clauses, block;
46556 unsigned int save;
46558 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
46559 "#pragma acc host_data", pragma_tok,
46560 false);
46561 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
46563 error_at (pragma_tok->location,
46564 "%<host_data%> construct requires %<use_device%> clause");
46565 return error_mark_node;
46567 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46568 block = begin_omp_parallel ();
46569 save = cp_parser_begin_omp_structured_block (parser);
46570 cp_parser_statement (parser, NULL_TREE, false, if_p);
46571 cp_parser_end_omp_structured_block (parser, save);
46572 stmt = finish_oacc_host_data (clauses, block);
46573 return stmt;
46576 /* OpenACC 2.0:
46577 # pragma acc declare oacc-data-clause[optseq] new-line
46580 #define OACC_DECLARE_CLAUSE_MASK \
46581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
46587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
46588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
46590 static tree
46591 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
46593 tree clauses, stmt;
46594 bool error = false;
46595 bool found_in_scope = global_bindings_p ();
46597 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
46598 "#pragma acc declare", pragma_tok, true);
46601 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46603 error_at (pragma_tok->location,
46604 "no valid clauses specified in %<#pragma acc declare%>");
46605 return NULL_TREE;
46608 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
46610 location_t loc = OMP_CLAUSE_LOCATION (t);
46611 tree decl = OMP_CLAUSE_DECL (t);
46612 if (!DECL_P (decl))
46614 error_at (loc, "array section in %<#pragma acc declare%>");
46615 error = true;
46616 continue;
46618 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
46619 switch (OMP_CLAUSE_MAP_KIND (t))
46621 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46622 case GOMP_MAP_ALLOC:
46623 case GOMP_MAP_TO:
46624 case GOMP_MAP_FORCE_DEVICEPTR:
46625 case GOMP_MAP_DEVICE_RESIDENT:
46626 break;
46628 case GOMP_MAP_LINK:
46629 if (!global_bindings_p ()
46630 && (TREE_STATIC (decl)
46631 || !DECL_EXTERNAL (decl)))
46633 error_at (loc,
46634 "%qD must be a global variable in "
46635 "%<#pragma acc declare link%>",
46636 decl);
46637 error = true;
46638 continue;
46640 break;
46642 default:
46643 if (global_bindings_p ())
46645 error_at (loc, "invalid OpenACC clause at file scope");
46646 error = true;
46647 continue;
46649 if (DECL_EXTERNAL (decl))
46651 error_at (loc,
46652 "invalid use of %<extern%> variable %qD "
46653 "in %<#pragma acc declare%>", decl);
46654 error = true;
46655 continue;
46657 else if (TREE_PUBLIC (decl))
46659 error_at (loc,
46660 "invalid use of %<global%> variable %qD "
46661 "in %<#pragma acc declare%>", decl);
46662 error = true;
46663 continue;
46665 break;
46668 if (!found_in_scope)
46669 /* This seems to ignore the existence of cleanup scopes?
46670 What is the meaning for local extern decls? The local
46671 extern is in this scope, but it is referring to a decl that
46672 is namespace scope. */
46673 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
46674 if (d == decl)
46676 found_in_scope = true;
46677 break;
46679 if (!found_in_scope)
46681 error_at (loc,
46682 "%qD must be a variable declared in the same scope as "
46683 "%<#pragma acc declare%>", decl);
46684 error = true;
46685 continue;
46688 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
46689 || lookup_attribute ("omp declare target link",
46690 DECL_ATTRIBUTES (decl)))
46692 error_at (loc, "variable %qD used more than once with "
46693 "%<#pragma acc declare%>", decl);
46694 error = true;
46695 continue;
46698 if (!error)
46700 tree id;
46702 if (DECL_LOCAL_DECL_P (decl))
46703 /* We need to mark the aliased decl, as that is the entity
46704 that is being referred to. This won't work for
46705 dependent variables, but it didn't work for them before
46706 DECL_LOCAL_DECL_P was a thing either. But then
46707 dependent local extern variable decls are as rare as
46708 hen's teeth. */
46709 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
46710 if (alias != error_mark_node)
46711 decl = alias;
46713 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
46714 id = get_identifier ("omp declare target link");
46715 else
46716 id = get_identifier ("omp declare target");
46718 DECL_ATTRIBUTES (decl)
46719 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
46720 if (current_binding_level->kind == sk_namespace)
46722 symtab_node *node = symtab_node::get (decl);
46723 if (node != NULL)
46725 node->offloadable = 1;
46726 if (ENABLE_OFFLOADING)
46728 g->have_offload = true;
46729 if (is_a <varpool_node *> (node))
46730 vec_safe_push (offload_vars, decl);
46737 if (error || current_binding_level->kind == sk_namespace)
46738 return NULL_TREE;
46740 stmt = make_node (OACC_DECLARE);
46741 TREE_TYPE (stmt) = void_type_node;
46742 OACC_DECLARE_CLAUSES (stmt) = clauses;
46743 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46745 add_stmt (stmt);
46747 return NULL_TREE;
46750 /* OpenACC 2.0:
46751 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
46755 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
46757 LOC is the location of the #pragma token.
46760 #define OACC_ENTER_DATA_CLAUSE_MASK \
46761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46768 #define OACC_EXIT_DATA_CLAUSE_MASK \
46769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
46773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
46775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46777 static tree
46778 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
46779 bool enter)
46781 location_t loc = pragma_tok->location;
46782 tree stmt, clauses;
46783 const char *p = "";
46785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46786 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46788 if (strcmp (p, "data") != 0)
46790 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
46791 enter ? "enter" : "exit");
46792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46793 return NULL_TREE;
46796 cp_lexer_consume_token (parser->lexer);
46798 if (enter)
46799 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
46800 "#pragma acc enter data", pragma_tok);
46801 else
46802 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
46803 "#pragma acc exit data", pragma_tok);
46805 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46807 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
46808 enter ? "enter" : "exit");
46809 return NULL_TREE;
46812 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
46813 TREE_TYPE (stmt) = void_type_node;
46814 OMP_STANDALONE_CLAUSES (stmt) = clauses;
46815 SET_EXPR_LOCATION (stmt, loc);
46816 add_stmt (stmt);
46817 return stmt;
46820 /* OpenACC 2.0:
46821 # pragma acc loop oacc-loop-clause[optseq] new-line
46822 structured-block */
46824 #define OACC_LOOP_CLAUSE_MASK \
46825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
46826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
46832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
46833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
46836 static tree
46837 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
46838 omp_clause_mask mask, tree *cclauses, bool *if_p)
46840 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
46842 strcat (p_name, " loop");
46843 mask |= OACC_LOOP_CLAUSE_MASK;
46845 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
46846 cclauses == NULL);
46847 if (cclauses)
46849 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
46850 if (*cclauses)
46851 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
46852 if (clauses)
46853 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46856 tree block = begin_omp_structured_block ();
46857 int save = cp_parser_begin_omp_structured_block (parser);
46858 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
46859 cp_parser_end_omp_structured_block (parser, save);
46861 /* Later processing of combined acc loop constructs gets confused
46862 by an extra level of empty nested BIND_EXPRs, so flatten them. */
46863 block = finish_omp_structured_block (block);
46864 if (TREE_CODE (block) == BIND_EXPR
46865 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
46866 && !BIND_EXPR_VARS (block))
46867 block = BIND_EXPR_BODY (block);
46868 add_stmt (block);
46870 return stmt;
46873 /* OpenACC 2.0:
46874 # pragma acc kernels oacc-kernels-clause[optseq] new-line
46875 structured-block
46879 # pragma acc parallel oacc-parallel-clause[optseq] new-line
46880 structured-block
46882 OpenACC 2.6:
46884 # pragma acc serial oacc-serial-clause[optseq] new-line
46887 #define OACC_KERNELS_CLAUSE_MASK \
46888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46904 #define OACC_PARALLEL_CLAUSE_MASK \
46905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46924 #define OACC_SERIAL_CLAUSE_MASK \
46925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46941 static tree
46942 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
46943 char *p_name, bool *if_p)
46945 omp_clause_mask mask;
46946 enum tree_code code;
46947 switch (cp_parser_pragma_kind (pragma_tok))
46949 case PRAGMA_OACC_KERNELS:
46950 strcat (p_name, " kernels");
46951 mask = OACC_KERNELS_CLAUSE_MASK;
46952 code = OACC_KERNELS;
46953 break;
46954 case PRAGMA_OACC_PARALLEL:
46955 strcat (p_name, " parallel");
46956 mask = OACC_PARALLEL_CLAUSE_MASK;
46957 code = OACC_PARALLEL;
46958 break;
46959 case PRAGMA_OACC_SERIAL:
46960 strcat (p_name, " serial");
46961 mask = OACC_SERIAL_CLAUSE_MASK;
46962 code = OACC_SERIAL;
46963 break;
46964 default:
46965 gcc_unreachable ();
46968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46970 const char *p
46971 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46972 if (strcmp (p, "loop") == 0)
46974 cp_lexer_consume_token (parser->lexer);
46975 tree block = begin_omp_parallel ();
46976 tree clauses;
46977 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
46978 &clauses, if_p);
46979 protected_set_expr_location (stmt, pragma_tok->location);
46980 return finish_omp_construct (code, block, clauses);
46984 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
46986 tree block = begin_omp_parallel ();
46987 unsigned int save = cp_parser_begin_omp_structured_block (parser);
46988 cp_parser_statement (parser, NULL_TREE, false, if_p);
46989 cp_parser_end_omp_structured_block (parser, save);
46990 return finish_omp_construct (code, block, clauses);
46993 /* OpenACC 2.0:
46994 # pragma acc update oacc-update-clause[optseq] new-line
46997 #define OACC_UPDATE_CLAUSE_MASK \
46998 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
47000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
47001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
47003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
47005 static tree
47006 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
47008 tree stmt, clauses;
47010 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
47011 "#pragma acc update", pragma_tok);
47013 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47015 error_at (pragma_tok->location,
47016 "%<#pragma acc update%> must contain at least one "
47017 "%<device%> or %<host%> or %<self%> clause");
47018 return NULL_TREE;
47021 stmt = make_node (OACC_UPDATE);
47022 TREE_TYPE (stmt) = void_type_node;
47023 OACC_UPDATE_CLAUSES (stmt) = clauses;
47024 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47025 add_stmt (stmt);
47026 return stmt;
47029 /* OpenACC 2.0:
47030 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
47032 LOC is the location of the #pragma token.
47035 #define OACC_WAIT_CLAUSE_MASK \
47036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
47038 static tree
47039 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
47041 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
47042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47044 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47045 list = cp_parser_oacc_wait_list (parser, loc, list);
47047 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
47048 "#pragma acc wait", pragma_tok);
47050 stmt = c_finish_oacc_wait (loc, list, clauses);
47051 stmt = finish_expr_stmt (stmt);
47053 return stmt;
47056 /* OpenMP 4.0:
47057 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
47059 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
47060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
47061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
47062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
47063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
47064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
47065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
47067 static void
47068 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
47069 enum pragma_context context,
47070 bool variant_p)
47072 bool first_p = parser->omp_declare_simd == NULL;
47073 cp_omp_declare_simd_data data;
47074 if (first_p)
47076 data.error_seen = false;
47077 data.fndecl_seen = false;
47078 data.variant_p = variant_p;
47079 data.tokens = vNULL;
47080 data.attribs[0] = NULL;
47081 data.attribs[1] = NULL;
47082 data.loc = UNKNOWN_LOCATION;
47083 /* It is safe to take the address of a local variable; it will only be
47084 used while this scope is live. */
47085 parser->omp_declare_simd = &data;
47087 else if (parser->omp_declare_simd->variant_p != variant_p)
47089 error_at (pragma_tok->location,
47090 "%<#pragma omp declare %s%> followed by "
47091 "%<#pragma omp declare %s%>",
47092 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47093 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47094 parser->omp_declare_simd->error_seen = true;
47097 /* Store away all pragma tokens. */
47098 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47099 cp_lexer_consume_token (parser->lexer);
47100 cp_parser_require_pragma_eol (parser, pragma_tok);
47101 struct cp_token_cache *cp
47102 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47103 parser->omp_declare_simd->tokens.safe_push (cp);
47105 if (first_p)
47107 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47108 cp_parser_pragma (parser, context, NULL);
47109 switch (context)
47111 case pragma_external:
47112 cp_parser_declaration (parser, NULL_TREE);
47113 break;
47114 case pragma_member:
47115 cp_parser_member_declaration (parser);
47116 break;
47117 case pragma_objc_icode:
47118 cp_parser_block_declaration (parser, /*statement_p=*/false);
47119 break;
47120 default:
47121 cp_parser_declaration_statement (parser);
47122 break;
47124 if (parser->omp_declare_simd
47125 && !parser->omp_declare_simd->error_seen
47126 && !parser->omp_declare_simd->fndecl_seen)
47127 error_at (pragma_tok->location,
47128 "%<#pragma omp declare %s%> not immediately followed by "
47129 "function declaration or definition",
47130 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47131 data.tokens.release ();
47132 parser->omp_declare_simd = NULL;
47136 static const char *const omp_construct_selectors[] = {
47137 "simd", "target", "teams", "parallel", "for", NULL };
47138 static const char *const omp_device_selectors[] = {
47139 "kind", "isa", "arch", NULL };
47140 static const char *const omp_implementation_selectors[] = {
47141 "vendor", "extension", "atomic_default_mem_order", "unified_address",
47142 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
47143 static const char *const omp_user_selectors[] = {
47144 "condition", NULL };
47146 /* OpenMP 5.0:
47148 trait-selector:
47149 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47151 trait-score:
47152 score(score-expression) */
47154 static tree
47155 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
47157 tree ret = NULL_TREE;
47160 tree selector;
47161 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47162 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47163 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47164 else
47166 cp_parser_error (parser, "expected trait selector name");
47167 return error_mark_node;
47170 tree properties = NULL_TREE;
47171 const char *const *selectors = NULL;
47172 bool allow_score = true;
47173 bool allow_user = false;
47174 int property_limit = 0;
47175 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
47176 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
47177 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
47178 switch (IDENTIFIER_POINTER (set)[0])
47180 case 'c': /* construct */
47181 selectors = omp_construct_selectors;
47182 allow_score = false;
47183 property_limit = 1;
47184 property_kind = CTX_PROPERTY_SIMD;
47185 break;
47186 case 'd': /* device */
47187 selectors = omp_device_selectors;
47188 allow_score = false;
47189 allow_user = true;
47190 property_limit = 3;
47191 property_kind = CTX_PROPERTY_NAME_LIST;
47192 break;
47193 case 'i': /* implementation */
47194 selectors = omp_implementation_selectors;
47195 allow_user = true;
47196 property_limit = 3;
47197 property_kind = CTX_PROPERTY_NAME_LIST;
47198 break;
47199 case 'u': /* user */
47200 selectors = omp_user_selectors;
47201 property_limit = 1;
47202 property_kind = CTX_PROPERTY_EXPR;
47203 break;
47204 default:
47205 gcc_unreachable ();
47207 for (int i = 0; ; i++)
47209 if (selectors[i] == NULL)
47211 if (allow_user)
47213 property_kind = CTX_PROPERTY_USER;
47214 break;
47216 else
47218 error ("selector %qs not allowed for context selector "
47219 "set %qs", IDENTIFIER_POINTER (selector),
47220 IDENTIFIER_POINTER (set));
47221 cp_lexer_consume_token (parser->lexer);
47222 return error_mark_node;
47225 if (i == property_limit)
47226 property_kind = CTX_PROPERTY_NONE;
47227 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
47228 break;
47230 if (property_kind == CTX_PROPERTY_NAME_LIST
47231 && IDENTIFIER_POINTER (set)[0] == 'i'
47232 && strcmp (IDENTIFIER_POINTER (selector),
47233 "atomic_default_mem_order") == 0)
47234 property_kind = CTX_PROPERTY_ID;
47236 cp_lexer_consume_token (parser->lexer);
47238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47240 if (property_kind == CTX_PROPERTY_NONE)
47242 error ("selector %qs does not accept any properties",
47243 IDENTIFIER_POINTER (selector));
47244 return error_mark_node;
47247 matching_parens parens;
47248 parens.consume_open (parser);
47250 cp_token *token = cp_lexer_peek_token (parser->lexer);
47251 if (allow_score
47252 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47253 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
47254 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
47256 cp_lexer_save_tokens (parser->lexer);
47257 cp_lexer_consume_token (parser->lexer);
47258 cp_lexer_consume_token (parser->lexer);
47259 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
47260 true)
47261 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
47263 cp_lexer_rollback_tokens (parser->lexer);
47264 cp_lexer_consume_token (parser->lexer);
47266 matching_parens parens2;
47267 parens2.require_open (parser);
47268 tree score = cp_parser_constant_expression (parser);
47269 if (!parens2.require_close (parser))
47270 cp_parser_skip_to_closing_parenthesis (parser, true,
47271 false, true);
47272 cp_parser_require (parser, CPP_COLON, RT_COLON);
47273 if (score != error_mark_node)
47275 score = fold_non_dependent_expr (score);
47276 if (value_dependent_expression_p (score))
47277 properties = tree_cons (get_identifier (" score"),
47278 score, properties);
47279 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
47280 || TREE_CODE (score) != INTEGER_CST)
47281 error_at (token->location, "score argument must be "
47282 "constant integer expression");
47283 else if (tree_int_cst_sgn (score) < 0)
47284 error_at (token->location, "score argument must be "
47285 "non-negative");
47286 else
47287 properties = tree_cons (get_identifier (" score"),
47288 score, properties);
47291 else
47292 cp_lexer_rollback_tokens (parser->lexer);
47294 token = cp_lexer_peek_token (parser->lexer);
47297 switch (property_kind)
47299 tree t;
47300 case CTX_PROPERTY_USER:
47303 t = cp_parser_constant_expression (parser);
47304 if (t != error_mark_node)
47306 t = fold_non_dependent_expr (t);
47307 if (TREE_CODE (t) == STRING_CST)
47308 properties = tree_cons (NULL_TREE, t, properties);
47309 else if (!value_dependent_expression_p (t)
47310 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47311 || !tree_fits_shwi_p (t)))
47312 error_at (token->location, "property must be "
47313 "constant integer expression or string "
47314 "literal");
47315 else
47316 properties = tree_cons (NULL_TREE, t, properties);
47318 else
47319 return error_mark_node;
47321 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47322 cp_lexer_consume_token (parser->lexer);
47323 else
47324 break;
47326 while (1);
47327 break;
47328 case CTX_PROPERTY_ID:
47329 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47330 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47332 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
47333 cp_lexer_consume_token (parser->lexer);
47334 properties = tree_cons (prop, NULL_TREE, properties);
47336 else
47338 cp_parser_error (parser, "expected identifier");
47339 return error_mark_node;
47341 break;
47342 case CTX_PROPERTY_NAME_LIST:
47345 tree prop = NULL_TREE, value = NULL_TREE;
47346 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47347 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47349 prop = cp_lexer_peek_token (parser->lexer)->u.value;
47350 cp_lexer_consume_token (parser->lexer);
47352 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
47353 value = cp_parser_string_literal (parser,
47354 /*translate=*/false,
47355 /*wide_ok=*/false);
47356 else
47358 cp_parser_error (parser, "expected identifier or "
47359 "string literal");
47360 return error_mark_node;
47363 properties = tree_cons (prop, value, properties);
47365 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47366 cp_lexer_consume_token (parser->lexer);
47367 else
47368 break;
47370 while (1);
47371 break;
47372 case CTX_PROPERTY_EXPR:
47373 t = cp_parser_constant_expression (parser);
47374 if (t != error_mark_node)
47376 t = fold_non_dependent_expr (t);
47377 if (!value_dependent_expression_p (t)
47378 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47379 || !tree_fits_shwi_p (t)))
47380 error_at (token->location, "property must be "
47381 "constant integer expression");
47382 else
47383 properties = tree_cons (NULL_TREE, t, properties);
47385 else
47386 return error_mark_node;
47387 break;
47388 case CTX_PROPERTY_SIMD:
47389 if (!has_parms_p)
47391 error_at (token->location, "properties for %<simd%> "
47392 "selector may not be specified in "
47393 "%<metadirective%>");
47394 return error_mark_node;
47396 properties
47397 = cp_parser_omp_all_clauses (parser,
47398 OMP_DECLARE_SIMD_CLAUSE_MASK,
47399 "simd", NULL, true, 2);
47400 break;
47401 default:
47402 gcc_unreachable ();
47405 if (!parens.require_close (parser))
47406 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
47408 properties = nreverse (properties);
47410 else if (property_kind == CTX_PROPERTY_NAME_LIST
47411 || property_kind == CTX_PROPERTY_ID
47412 || property_kind == CTX_PROPERTY_EXPR)
47414 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
47415 return error_mark_node;
47418 ret = tree_cons (selector, properties, ret);
47420 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47421 cp_lexer_consume_token (parser->lexer);
47422 else
47423 break;
47425 while (1);
47427 return nreverse (ret);
47430 /* OpenMP 5.0:
47432 trait-set-selector[,trait-set-selector[,...]]
47434 trait-set-selector:
47435 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
47437 trait-set-selector-name:
47438 constructor
47439 device
47440 implementation
47441 user */
47443 static tree
47444 cp_parser_omp_context_selector_specification (cp_parser *parser,
47445 bool has_parms_p)
47447 tree ret = NULL_TREE;
47450 const char *setp = "";
47451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47452 setp
47453 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47454 switch (setp[0])
47456 case 'c':
47457 if (strcmp (setp, "construct") == 0)
47458 setp = NULL;
47459 break;
47460 case 'd':
47461 if (strcmp (setp, "device") == 0)
47462 setp = NULL;
47463 break;
47464 case 'i':
47465 if (strcmp (setp, "implementation") == 0)
47466 setp = NULL;
47467 break;
47468 case 'u':
47469 if (strcmp (setp, "user") == 0)
47470 setp = NULL;
47471 break;
47472 default:
47473 break;
47475 if (setp)
47477 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
47478 "%<implementation%> or %<user%>");
47479 return error_mark_node;
47482 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
47483 cp_lexer_consume_token (parser->lexer);
47485 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
47486 return error_mark_node;
47488 matching_braces braces;
47489 if (!braces.require_open (parser))
47490 return error_mark_node;
47492 tree selectors
47493 = cp_parser_omp_context_selector (parser, set, has_parms_p);
47494 if (selectors == error_mark_node)
47496 cp_parser_skip_to_closing_brace (parser);
47497 ret = error_mark_node;
47499 else if (ret != error_mark_node)
47500 ret = tree_cons (set, selectors, ret);
47502 braces.require_close (parser);
47504 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47505 cp_lexer_consume_token (parser->lexer);
47506 else
47507 break;
47509 while (1);
47511 if (ret == error_mark_node)
47512 return ret;
47513 return nreverse (ret);
47516 /* Assumption clauses:
47517 OpenMP 5.1
47518 absent (directive-name-list)
47519 contains (directive-name-list)
47520 holds (expression)
47521 no_openmp
47522 no_openmp_routines
47523 no_parallelism */
47525 static void
47526 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
47527 bool is_assume)
47529 bool no_openmp = false;
47530 bool no_openmp_routines = false;
47531 bool no_parallelism = false;
47532 bitmap_head absent_head, contains_head;
47534 bitmap_obstack_initialize (NULL);
47535 bitmap_initialize (&absent_head, &bitmap_default_obstack);
47536 bitmap_initialize (&contains_head, &bitmap_default_obstack);
47538 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47539 error_at (cp_lexer_peek_token (parser->lexer)->location,
47540 "expected at least one assumption clause");
47542 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47545 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47546 cp_lexer_consume_token (parser->lexer);
47548 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47549 break;
47551 const char *p
47552 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47553 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47555 if (!strcmp (p, "no_openmp"))
47557 cp_lexer_consume_token (parser->lexer);
47558 if (no_openmp)
47559 error_at (cloc, "too many %qs clauses", "no_openmp");
47560 no_openmp = true;
47562 else if (!strcmp (p, "no_openmp_routines"))
47564 cp_lexer_consume_token (parser->lexer);
47565 if (no_openmp_routines)
47566 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
47567 no_openmp_routines = true;
47569 else if (!strcmp (p, "no_parallelism"))
47571 cp_lexer_consume_token (parser->lexer);
47572 if (no_parallelism)
47573 error_at (cloc, "too many %qs clauses", "no_parallelism");
47574 no_parallelism = true;
47576 else if (!strcmp (p, "holds"))
47578 cp_lexer_consume_token (parser->lexer);
47579 matching_parens parens;
47580 if (parens.require_open (parser))
47582 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
47583 tree t = cp_parser_assignment_expression (parser);
47584 if (!type_dependent_expression_p (t))
47585 t = contextual_conv_bool (t, tf_warning_or_error);
47586 if (is_assume && !error_operand_p (t))
47587 finish_expr_stmt (build_assume_call (eloc, t));
47588 if (!parens.require_close (parser))
47589 cp_parser_skip_to_closing_parenthesis (parser,
47590 /*recovering=*/true,
47591 /*or_comma=*/false,
47592 /*consume_paren=*/true);
47595 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
47597 cp_lexer_consume_token (parser->lexer);
47598 matching_parens parens;
47599 if (parens.require_open (parser))
47603 const char *directive[3] = {};
47604 int i;
47605 location_t dloc
47606 = cp_lexer_peek_token (parser->lexer)->location;
47607 for (i = 0; i < 3; i++)
47609 tree id;
47610 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
47611 id = cp_lexer_peek_nth_token (parser->lexer,
47612 i + 1)->u.value;
47613 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
47614 CPP_KEYWORD))
47616 enum rid rid
47617 = cp_lexer_peek_nth_token (parser->lexer,
47618 i + 1)->keyword;
47619 id = ridpointers[rid];
47621 else
47622 break;
47623 directive[i] = IDENTIFIER_POINTER (id);
47625 if (i == 0)
47626 error_at (dloc, "expected directive name");
47627 else
47629 const struct c_omp_directive *dir
47630 = c_omp_categorize_directive (directive[0],
47631 directive[1],
47632 directive[2]);
47633 if (dir == NULL
47634 || dir->kind == C_OMP_DIR_DECLARATIVE
47635 || dir->kind == C_OMP_DIR_INFORMATIONAL
47636 || dir->id == PRAGMA_OMP_END
47637 || (!dir->second && directive[1])
47638 || (!dir->third && directive[2]))
47639 error_at (dloc, "unknown OpenMP directive name in "
47640 "%qs clause argument", p);
47641 else
47643 int id = dir - c_omp_directives;
47644 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
47645 : &absent_head, id))
47646 error_at (dloc, "%<%s%s%s%s%s%> directive "
47647 "mentioned in both %<absent%> and "
47648 "%<contains%> clauses",
47649 directive[0],
47650 directive[1] ? " " : "",
47651 directive[1] ? directive[1] : "",
47652 directive[2] ? " " : "",
47653 directive[2] ? directive[2] : "");
47654 else if (!bitmap_set_bit (p[0] == 'a'
47655 ? &absent_head
47656 : &contains_head, id))
47657 error_at (dloc, "%<%s%s%s%s%s%> directive "
47658 "mentioned multiple times in %qs "
47659 "clauses",
47660 directive[0],
47661 directive[1] ? " " : "",
47662 directive[1] ? directive[1] : "",
47663 directive[2] ? " " : "",
47664 directive[2] ? directive[2] : "", p);
47666 for (; i; --i)
47667 cp_lexer_consume_token (parser->lexer);
47669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47670 cp_lexer_consume_token (parser->lexer);
47671 else
47672 break;
47674 while (1);
47675 if (!parens.require_close (parser))
47676 cp_parser_skip_to_closing_parenthesis (parser,
47677 /*recovering=*/true,
47678 /*or_comma=*/false,
47679 /*consume_paren=*/true);
47682 else if (startswith (p, "ext_"))
47684 warning_at (cloc, 0, "unknown assumption clause %qs", p);
47685 cp_lexer_consume_token (parser->lexer);
47686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47687 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47688 n; --n)
47689 cp_lexer_consume_token (parser->lexer);
47691 else
47693 cp_lexer_consume_token (parser->lexer);
47694 error_at (cloc, "expected assumption clause");
47695 break;
47698 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47701 /* OpenMP 5.1
47702 # pragma omp assume clauses[optseq] new-line */
47704 static void
47705 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47707 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
47708 add_stmt (cp_parser_omp_structured_block (parser, if_p));
47711 /* OpenMP 5.1
47712 # pragma omp assumes clauses[optseq] new-line */
47714 static bool
47715 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
47717 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47718 return false;
47721 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
47722 that into "omp declare variant base" attribute. */
47724 static tree
47725 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
47726 tree attrs)
47728 matching_parens parens;
47729 if (!parens.require_open (parser))
47731 fail:
47732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47733 return attrs;
47736 bool template_p;
47737 cp_id_kind idk = CP_ID_KIND_NONE;
47738 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
47739 cp_expr varid
47740 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
47741 /*check_dependency_p=*/true,
47742 /*template_p=*/&template_p,
47743 /*declarator_p=*/false,
47744 /*optional_p=*/false);
47745 parens.require_close (parser);
47747 tree variant;
47748 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
47749 || TREE_CODE (varid) == TYPE_DECL
47750 || varid == error_mark_node)
47751 variant = varid;
47752 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
47753 variant = NULL_TREE;
47754 else
47756 tree ambiguous_decls;
47757 variant = cp_parser_lookup_name (parser, varid, none_type,
47758 template_p, /*is_namespace=*/false,
47759 /*check_dependency=*/true,
47760 &ambiguous_decls,
47761 varid.get_location ());
47762 if (ambiguous_decls)
47763 variant = NULL_TREE;
47765 if (variant == NULL_TREE)
47766 variant = error_mark_node;
47767 else if (TREE_CODE (variant) != SCOPE_REF)
47769 const char *error_msg;
47770 variant
47771 = finish_id_expression (varid, variant, parser->scope,
47772 &idk, false, true,
47773 &parser->non_integral_constant_expression_p,
47774 template_p, true, false, false, &error_msg,
47775 varid.get_location ());
47776 if (error_msg)
47777 cp_parser_error (parser, error_msg);
47779 location_t caret_loc = get_pure_location (varid.get_location ());
47780 location_t start_loc = get_start (varid_token->location);
47781 location_t finish_loc = get_finish (varid.get_location ());
47782 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
47784 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47786 cp_lexer_consume_token (parser->lexer);
47788 const char *clause = "";
47789 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
47790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47791 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47792 if (strcmp (clause, "match"))
47794 cp_parser_error (parser, "expected %<match%>");
47795 goto fail;
47798 cp_lexer_consume_token (parser->lexer);
47800 if (!parens.require_open (parser))
47801 goto fail;
47803 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
47804 if (ctx == error_mark_node)
47805 goto fail;
47806 ctx = omp_check_context_selector (match_loc, ctx);
47807 if (ctx != error_mark_node && variant != error_mark_node)
47809 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
47810 match_loc);
47811 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
47812 loc_node = tree_cons (match_loc_node,
47813 build_int_cst (integer_type_node, idk),
47814 build_tree_list (loc_node, integer_zero_node));
47815 attrs = tree_cons (get_identifier ("omp declare variant base"),
47816 tree_cons (variant, ctx, loc_node), attrs);
47817 if (processing_template_decl)
47818 ATTR_IS_DEPENDENT (attrs) = 1;
47821 parens.require_close (parser);
47822 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47823 return attrs;
47827 /* Finalize #pragma omp declare simd clauses after direct declarator has
47828 been parsed, and put that into "omp declare simd" attribute. */
47830 static tree
47831 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
47833 struct cp_token_cache *ce;
47834 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
47835 int i;
47837 if (!data->error_seen && data->fndecl_seen)
47839 error ("%<#pragma omp declare %s%> not immediately followed by "
47840 "a single function declaration or definition",
47841 data->variant_p ? "variant" : "simd");
47842 data->error_seen = true;
47844 if (data->error_seen)
47845 return attrs;
47847 FOR_EACH_VEC_ELT (data->tokens, i, ce)
47849 tree c, cl;
47851 cp_parser_push_lexer_for_tokens (parser, ce);
47852 parser->lexer->in_pragma = true;
47853 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47854 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47856 const char *kind = IDENTIFIER_POINTER (id);
47857 cp_lexer_consume_token (parser->lexer);
47858 if (strcmp (kind, "simd") == 0)
47860 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
47861 "#pragma omp declare simd",
47862 pragma_tok);
47863 if (cl)
47864 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47865 c = build_tree_list (get_identifier ("omp declare simd"), cl);
47866 TREE_CHAIN (c) = attrs;
47867 if (processing_template_decl)
47868 ATTR_IS_DEPENDENT (c) = 1;
47869 attrs = c;
47871 else
47873 gcc_assert (strcmp (kind, "variant") == 0);
47874 attrs
47875 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
47877 cp_parser_pop_lexer (parser);
47880 cp_lexer *lexer = NULL;
47881 for (int i = 0; i < 2; i++)
47883 if (data->attribs[i] == NULL)
47884 continue;
47885 for (tree *pa = data->attribs[i]; *pa; )
47886 if (get_attribute_namespace (*pa) == omp_identifier
47887 && is_attribute_p ("directive", get_attribute_name (*pa)))
47889 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
47891 tree d = TREE_VALUE (a);
47892 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
47893 cp_token *first = DEFPARSE_TOKENS (d)->first;
47894 cp_token *last = DEFPARSE_TOKENS (d)->last;
47895 const char *directive[3] = {};
47896 for (int j = 0; j < 3; j++)
47898 tree id = NULL_TREE;
47899 if (first + j == last)
47900 break;
47901 if (first[j].type == CPP_NAME)
47902 id = first[j].u.value;
47903 else if (first[j].type == CPP_KEYWORD)
47904 id = ridpointers[(int) first[j].keyword];
47905 else
47906 break;
47907 directive[j] = IDENTIFIER_POINTER (id);
47909 const c_omp_directive *dir = NULL;
47910 if (directive[0])
47911 dir = c_omp_categorize_directive (directive[0], directive[1],
47912 directive[2]);
47913 if (dir == NULL)
47915 error_at (first->location,
47916 "unknown OpenMP directive name in "
47917 "%qs attribute argument",
47918 TREE_PUBLIC (d)
47919 ? "omp::decl" : "omp::directive");
47920 continue;
47922 if (dir->id != PRAGMA_OMP_DECLARE
47923 || (strcmp (directive[1], "simd") != 0
47924 && strcmp (directive[1], "variant") != 0))
47926 error_at (first->location,
47927 "OpenMP directive other than %<declare simd%> "
47928 "or %<declare variant%> appertains to a "
47929 "declaration");
47930 continue;
47933 if (parser->omp_attrs_forbidden_p)
47935 error_at (first->location,
47936 "mixing OpenMP directives with attribute and "
47937 "pragma syntax on the same statement");
47938 parser->omp_attrs_forbidden_p = false;
47941 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
47942 continue;
47943 if (lexer == NULL)
47945 lexer = cp_lexer_alloc ();
47946 lexer->debugging_p = parser->lexer->debugging_p;
47948 vec_safe_reserve (lexer->buffer, (last - first) + 2);
47949 cp_token tok = {};
47950 tok.type = CPP_PRAGMA;
47951 tok.keyword = RID_MAX;
47952 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
47953 tok.location = first->location;
47954 lexer->buffer->quick_push (tok);
47955 while (++first < last)
47956 lexer->buffer->quick_push (*first);
47957 tok = {};
47958 tok.type = CPP_PRAGMA_EOL;
47959 tok.keyword = RID_MAX;
47960 tok.location = last->location;
47961 lexer->buffer->quick_push (tok);
47962 tok = {};
47963 tok.type = CPP_EOF;
47964 tok.keyword = RID_MAX;
47965 tok.location = last->location;
47966 lexer->buffer->quick_push (tok);
47967 lexer->next = parser->lexer;
47968 lexer->next_token = lexer->buffer->address ();
47969 lexer->last_token = lexer->next_token
47970 + lexer->buffer->length ()
47971 - 1;
47972 lexer->in_omp_attribute_pragma = true;
47973 parser->lexer = lexer;
47974 /* Move the current source position to that of the first token
47975 in the new lexer. */
47976 cp_lexer_set_source_position_from_token (lexer->next_token);
47978 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47980 const char *kind = IDENTIFIER_POINTER (id);
47981 cp_lexer_consume_token (parser->lexer);
47983 tree c, cl;
47984 if (strcmp (kind, "simd") == 0)
47986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47987 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47988 cp_lexer_consume_token (parser->lexer);
47990 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
47991 cl = cp_parser_omp_all_clauses (parser, mask,
47992 "#pragma omp declare simd",
47993 pragma_tok);
47994 if (cl)
47995 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47996 c = build_tree_list (get_identifier ("omp declare simd"),
47997 cl);
47998 TREE_CHAIN (c) = attrs;
47999 if (processing_template_decl)
48000 ATTR_IS_DEPENDENT (c) = 1;
48001 attrs = c;
48003 else
48005 gcc_assert (strcmp (kind, "variant") == 0);
48006 attrs
48007 = cp_finish_omp_declare_variant (parser, pragma_tok,
48008 attrs);
48010 gcc_assert (parser->lexer != lexer);
48011 vec_safe_truncate (lexer->buffer, 0);
48013 *pa = TREE_CHAIN (*pa);
48015 else
48016 pa = &TREE_CHAIN (*pa);
48018 if (lexer)
48019 cp_lexer_destroy (lexer);
48021 data->fndecl_seen = true;
48022 return attrs;
48025 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
48026 a threadprivate, groupprivate, allocate or declare target directive,
48027 return true and parse it for DECL. */
48029 bool
48030 cp_maybe_parse_omp_decl (tree decl, tree d)
48032 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48033 cp_token *first = DEFPARSE_TOKENS (d)->first;
48034 cp_token *last = DEFPARSE_TOKENS (d)->last;
48035 const char *directive[3] = {};
48036 for (int j = 0; j < 3; j++)
48038 tree id = NULL_TREE;
48039 if (first + j == last)
48040 break;
48041 if (first[j].type == CPP_NAME)
48042 id = first[j].u.value;
48043 else if (first[j].type == CPP_KEYWORD)
48044 id = ridpointers[(int) first[j].keyword];
48045 else
48046 break;
48047 directive[j] = IDENTIFIER_POINTER (id);
48049 const c_omp_directive *dir = NULL;
48050 if (directive[0])
48051 dir = c_omp_categorize_directive (directive[0], directive[1],
48052 directive[2]);
48053 if (dir == NULL)
48055 error_at (first->location,
48056 "unknown OpenMP directive name in "
48057 "%qs attribute argument", "omp::decl");
48058 return false;
48060 if (dir->id != PRAGMA_OMP_THREADPRIVATE
48061 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
48062 && dir->id != PRAGMA_OMP_ALLOCATE
48063 && (dir->id != PRAGMA_OMP_DECLARE
48064 || strcmp (directive[1], "target") != 0))
48065 return false;
48067 if (!flag_openmp && !dir->simd)
48068 return true;
48070 cp_parser *parser = the_parser;
48071 cp_lexer *lexer = cp_lexer_alloc ();
48072 lexer->debugging_p = parser->lexer->debugging_p;
48073 lexer->in_omp_decl_attribute = decl;
48074 vec_safe_reserve (lexer->buffer, last - first + 3, true);
48075 cp_token tok = {};
48076 tok.type = CPP_PRAGMA;
48077 tok.keyword = RID_MAX;
48078 tok.u.value = build_int_cst (NULL, dir->id);
48079 tok.location = first->location;
48080 lexer->buffer->quick_push (tok);
48081 while (++first < last)
48082 lexer->buffer->quick_push (*first);
48083 tok = {};
48084 tok.type = CPP_PRAGMA_EOL;
48085 tok.keyword = RID_MAX;
48086 tok.location = last->location;
48087 lexer->buffer->quick_push (tok);
48088 tok = {};
48089 tok.type = CPP_EOF;
48090 tok.keyword = RID_MAX;
48091 tok.location = last->location;
48092 lexer->buffer->quick_push (tok);
48093 lexer->next = parser->lexer;
48094 lexer->next_token = lexer->buffer->address ();
48095 lexer->last_token = lexer->next_token
48096 + lexer->buffer->length ()
48097 - 1;
48098 lexer->in_omp_attribute_pragma = true;
48099 parser->lexer = lexer;
48100 /* Move the current source position to that of the first token in the
48101 new lexer. */
48102 cp_lexer_set_source_position_from_token (lexer->next_token);
48103 cp_parser_pragma (parser, pragma_external, NULL);
48105 return true;
48108 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
48109 on #pragma omp declare target. Return false if errors were reported. */
48111 static bool
48112 handle_omp_declare_target_clause (tree c, tree t, int device_type)
48114 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
48115 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
48116 tree id;
48117 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
48119 id = get_identifier ("omp declare target link");
48120 std::swap (at1, at2);
48122 else
48123 id = get_identifier ("omp declare target");
48124 if (at2)
48126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
48127 error_at (OMP_CLAUSE_LOCATION (c),
48128 "%qD specified both in declare target %<link%> and %qs"
48129 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
48130 else
48131 error_at (OMP_CLAUSE_LOCATION (c),
48132 "%qD specified both in declare target %<link%> and "
48133 "%<to%> or %<enter%> clauses", t);
48134 return false;
48136 if (!at1)
48138 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48139 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
48140 return true;
48142 symtab_node *node = symtab_node::get (t);
48143 if (node != NULL)
48145 node->offloadable = 1;
48146 if (ENABLE_OFFLOADING)
48148 g->have_offload = true;
48149 if (is_a <varpool_node *> (node))
48150 vec_safe_push (offload_vars, t);
48154 if (TREE_CODE (t) != FUNCTION_DECL)
48155 return true;
48156 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
48158 tree at3 = lookup_attribute ("omp declare target host",
48159 DECL_ATTRIBUTES (t));
48160 if (at3 == NULL_TREE)
48162 id = get_identifier ("omp declare target host");
48163 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48166 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48168 tree at3 = lookup_attribute ("omp declare target nohost",
48169 DECL_ATTRIBUTES (t));
48170 if (at3 == NULL_TREE)
48172 id = get_identifier ("omp declare target nohost");
48173 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48176 return true;
48179 /* OpenMP 4.0:
48180 # pragma omp declare target new-line
48181 declarations and definitions
48182 # pragma omp end declare target new-line
48184 OpenMP 4.5:
48185 # pragma omp declare target ( extended-list ) new-line
48187 # pragma omp declare target declare-target-clauses[seq] new-line */
48189 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
48195 static void
48196 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48198 tree clauses = NULL_TREE;
48199 int device_type = 0;
48200 bool only_device_type = true;
48201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48202 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48203 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48204 clauses
48205 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48206 "#pragma omp declare target", pragma_tok);
48207 else if (parser->lexer->in_omp_decl_attribute
48208 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48211 clauses);
48212 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
48213 cp_parser_require_pragma_eol (parser, pragma_tok);
48215 else
48217 cp_omp_declare_target_attr a
48218 = { parser->lexer->in_omp_attribute_pragma, -1 };
48219 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48220 cp_parser_require_pragma_eol (parser, pragma_tok);
48221 return;
48223 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48225 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48226 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48228 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48229 continue;
48230 tree t = OMP_CLAUSE_DECL (c);
48231 only_device_type = false;
48232 if (!handle_omp_declare_target_clause (c, t, device_type))
48233 continue;
48234 if (VAR_OR_FUNCTION_DECL_P (t)
48235 && DECL_LOCAL_DECL_P (t)
48236 && DECL_LANG_SPECIFIC (t)
48237 && DECL_LOCAL_DECL_ALIAS (t)
48238 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
48239 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
48240 device_type);
48242 if (device_type && only_device_type)
48243 error_at (OMP_CLAUSE_LOCATION (clauses),
48244 "directive with only %<device_type%> clause");
48247 /* OpenMP 5.1
48248 # pragma omp begin assumes clauses[optseq] new-line
48250 # pragma omp begin declare target clauses[optseq] new-line */
48252 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
48253 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
48255 static void
48256 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
48258 const char *p = "";
48259 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48263 p = IDENTIFIER_POINTER (id);
48265 if (strcmp (p, "declare") == 0)
48267 cp_lexer_consume_token (parser->lexer);
48268 p = "";
48269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48271 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48272 p = IDENTIFIER_POINTER (id);
48274 if (strcmp (p, "target") == 0)
48276 cp_lexer_consume_token (parser->lexer);
48277 tree clauses
48278 = cp_parser_omp_all_clauses (parser,
48279 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
48280 "#pragma omp begin declare target",
48281 pragma_tok);
48282 int device_type = 0;
48283 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48284 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48285 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48286 cp_omp_declare_target_attr a
48287 = { in_omp_attribute_pragma, device_type };
48288 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48290 else
48292 cp_parser_error (parser, "expected %<target%>");
48293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48296 else if (strcmp (p, "assumes") == 0)
48298 cp_lexer_consume_token (parser->lexer);
48299 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48300 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
48301 vec_safe_push (scope_chain->omp_begin_assumes, a);
48303 else
48305 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
48306 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48310 /* OpenMP 4.0:
48311 # pragma omp end declare target new-line
48313 OpenMP 5.1:
48314 # pragma omp end assumes new-line */
48316 static void
48317 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
48319 const char *p = "";
48320 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48324 p = IDENTIFIER_POINTER (id);
48326 if (strcmp (p, "declare") == 0)
48328 cp_lexer_consume_token (parser->lexer);
48329 p = "";
48330 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48332 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48333 p = IDENTIFIER_POINTER (id);
48335 if (strcmp (p, "target") == 0)
48336 cp_lexer_consume_token (parser->lexer);
48337 else
48339 cp_parser_error (parser, "expected %<target%>");
48340 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48341 return;
48343 cp_parser_require_pragma_eol (parser, pragma_tok);
48344 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
48345 error_at (pragma_tok->location,
48346 "%<#pragma omp end declare target%> without corresponding "
48347 "%<#pragma omp declare target%> or "
48348 "%<#pragma omp begin declare target%>");
48349 else
48351 cp_omp_declare_target_attr
48352 a = scope_chain->omp_declare_target_attribute->pop ();
48353 if (a.attr_syntax != in_omp_attribute_pragma)
48355 if (a.attr_syntax)
48356 error_at (pragma_tok->location,
48357 "%qs in attribute syntax terminated "
48358 "with %qs in pragma syntax",
48359 a.device_type >= 0 ? "begin declare target"
48360 : "declare target",
48361 "end declare target");
48362 else
48363 error_at (pragma_tok->location,
48364 "%qs in pragma syntax terminated "
48365 "with %qs in attribute syntax",
48366 a.device_type >= 0 ? "begin declare target"
48367 : "declare target",
48368 "end declare target");
48372 else if (strcmp (p, "assumes") == 0)
48374 cp_lexer_consume_token (parser->lexer);
48375 cp_parser_require_pragma_eol (parser, pragma_tok);
48376 if (!vec_safe_length (scope_chain->omp_begin_assumes))
48377 error_at (pragma_tok->location,
48378 "%qs without corresponding %qs",
48379 "#pragma omp end assumes", "#pragma omp begin assumes");
48380 else
48382 cp_omp_begin_assumes_data
48383 a = scope_chain->omp_begin_assumes->pop ();
48384 if (a.attr_syntax != in_omp_attribute_pragma)
48386 if (a.attr_syntax)
48387 error_at (pragma_tok->location,
48388 "%qs in attribute syntax terminated "
48389 "with %qs in pragma syntax",
48390 "begin assumes", "end assumes");
48391 else
48392 error_at (pragma_tok->location,
48393 "%qs in pragma syntax terminated "
48394 "with %qs in attribute syntax",
48395 "begin assumes", "end assumes");
48399 else
48401 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
48402 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48403 return;
48407 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
48408 expression and optional initializer clause of
48409 #pragma omp declare reduction. We store the expression(s) as
48410 either 3, 6 or 7 special statements inside of the artificial function's
48411 body. The first two statements are DECL_EXPRs for the artificial
48412 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
48413 expression that uses those variables.
48414 If there was any INITIALIZER clause, this is followed by further statements,
48415 the fourth and fifth statements are DECL_EXPRs for the artificial
48416 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
48417 constructor variant (first token after open paren is not omp_priv),
48418 then the sixth statement is a statement with the function call expression
48419 that uses the OMP_PRIV and optionally OMP_ORIG variable.
48420 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
48421 to initialize the OMP_PRIV artificial variable and there is seventh
48422 statement, a DECL_EXPR of the OMP_PRIV statement again. */
48424 static bool
48425 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
48427 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
48428 gcc_assert (TYPE_REF_P (type));
48429 type = TREE_TYPE (type);
48430 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
48431 DECL_ARTIFICIAL (omp_out) = 1;
48432 pushdecl (omp_out);
48433 add_decl_expr (omp_out);
48434 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
48435 DECL_ARTIFICIAL (omp_in) = 1;
48436 pushdecl (omp_in);
48437 add_decl_expr (omp_in);
48438 tree combiner;
48439 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
48441 keep_next_level (true);
48442 tree block = begin_omp_structured_block ();
48443 combiner = cp_parser_expression (parser);
48444 finish_expr_stmt (combiner);
48445 block = finish_omp_structured_block (block);
48446 if (processing_template_decl)
48447 block = build_stmt (input_location, EXPR_STMT, block);
48448 add_stmt (block);
48450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
48451 return false;
48453 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48454 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48455 cp_lexer_consume_token (parser->lexer);
48457 const char *p = "";
48458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48460 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48461 p = IDENTIFIER_POINTER (id);
48464 if (strcmp (p, "initializer") == 0)
48466 cp_lexer_consume_token (parser->lexer);
48467 matching_parens parens;
48468 if (!parens.require_open (parser))
48469 return false;
48471 p = "";
48472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48475 p = IDENTIFIER_POINTER (id);
48478 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
48479 DECL_ARTIFICIAL (omp_priv) = 1;
48480 pushdecl (omp_priv);
48481 add_decl_expr (omp_priv);
48482 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
48483 DECL_ARTIFICIAL (omp_orig) = 1;
48484 pushdecl (omp_orig);
48485 add_decl_expr (omp_orig);
48487 keep_next_level (true);
48488 block = begin_omp_structured_block ();
48490 bool ctor = false;
48491 if (strcmp (p, "omp_priv") == 0)
48493 bool is_non_constant_init;
48494 ctor = true;
48495 cp_lexer_consume_token (parser->lexer);
48496 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
48497 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
48498 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
48499 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
48500 == CPP_CLOSE_PAREN
48501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
48502 == CPP_CLOSE_PAREN))
48504 finish_omp_structured_block (block);
48505 error ("invalid initializer clause");
48506 return false;
48508 initializer = cp_parser_initializer (parser,
48509 /*is_direct_init=*/nullptr,
48510 &is_non_constant_init);
48511 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
48512 NULL_TREE, LOOKUP_ONLYCONVERTING);
48514 else
48516 cp_parser_parse_tentatively (parser);
48517 /* Don't create location wrapper nodes here. */
48518 auto_suppress_location_wrappers sentinel;
48519 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
48520 /*check_dependency_p=*/true,
48521 /*template_p=*/NULL,
48522 /*declarator_p=*/false,
48523 /*optional_p=*/false);
48524 vec<tree, va_gc> *args;
48525 if (fn_name == error_mark_node
48526 || cp_parser_error_occurred (parser)
48527 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
48528 || ((args = cp_parser_parenthesized_expression_list
48529 (parser, non_attr, /*cast_p=*/false,
48530 /*allow_expansion_p=*/true,
48531 /*non_constant_p=*/NULL)),
48532 cp_parser_error_occurred (parser)))
48534 finish_omp_structured_block (block);
48535 cp_parser_abort_tentative_parse (parser);
48536 cp_parser_error (parser, "expected id-expression (arguments)");
48537 return false;
48539 unsigned int i;
48540 tree arg;
48541 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
48542 if (arg == omp_priv
48543 || (TREE_CODE (arg) == ADDR_EXPR
48544 && TREE_OPERAND (arg, 0) == omp_priv))
48545 break;
48546 cp_parser_abort_tentative_parse (parser);
48547 if (arg == NULL_TREE)
48548 error ("one of the initializer call arguments should be %<omp_priv%>"
48549 " or %<&omp_priv%>");
48550 initializer = cp_parser_postfix_expression (parser, false, false, false,
48551 false, NULL);
48552 finish_expr_stmt (initializer);
48555 block = finish_omp_structured_block (block);
48556 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
48557 if (processing_template_decl)
48558 block = build_stmt (input_location, EXPR_STMT, block);
48559 add_stmt (block);
48561 if (ctor)
48562 add_decl_expr (omp_orig);
48564 if (!parens.require_close (parser))
48565 return false;
48568 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48569 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
48570 UNKNOWN_LOCATION);
48572 return true;
48575 /* OpenMP 4.0
48576 #pragma omp declare reduction (reduction-id : typename-list : expression) \
48577 initializer-clause[opt] new-line
48579 initializer-clause:
48580 initializer (omp_priv initializer)
48581 initializer (function-name (argument-list)) */
48583 static void
48584 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
48585 enum pragma_context)
48587 auto_vec<tree> types;
48588 enum tree_code reduc_code = ERROR_MARK;
48589 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
48590 unsigned int i;
48591 cp_token *first_token;
48592 cp_token_cache *cp;
48593 int errs;
48594 void *p;
48596 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
48597 p = obstack_alloc (&declarator_obstack, 0);
48599 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
48600 goto fail;
48602 switch (cp_lexer_peek_token (parser->lexer)->type)
48604 case CPP_PLUS:
48605 reduc_code = PLUS_EXPR;
48606 break;
48607 case CPP_MULT:
48608 reduc_code = MULT_EXPR;
48609 break;
48610 case CPP_MINUS:
48611 reduc_code = MINUS_EXPR;
48612 break;
48613 case CPP_AND:
48614 reduc_code = BIT_AND_EXPR;
48615 break;
48616 case CPP_XOR:
48617 reduc_code = BIT_XOR_EXPR;
48618 break;
48619 case CPP_OR:
48620 reduc_code = BIT_IOR_EXPR;
48621 break;
48622 case CPP_AND_AND:
48623 reduc_code = TRUTH_ANDIF_EXPR;
48624 break;
48625 case CPP_OR_OR:
48626 reduc_code = TRUTH_ORIF_EXPR;
48627 break;
48628 case CPP_NAME:
48629 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
48630 break;
48631 default:
48632 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
48633 "%<|%>, %<&&%>, %<||%> or identifier");
48634 goto fail;
48637 if (reduc_code != ERROR_MARK)
48638 cp_lexer_consume_token (parser->lexer);
48640 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
48641 if (reduc_id == error_mark_node)
48642 goto fail;
48644 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
48645 goto fail;
48647 /* Types may not be defined in declare reduction type list. */
48648 const char *saved_message;
48649 saved_message = parser->type_definition_forbidden_message;
48650 parser->type_definition_forbidden_message
48651 = G_("types may not be defined in declare reduction type list");
48652 bool saved_colon_corrects_to_scope_p;
48653 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
48654 parser->colon_corrects_to_scope_p = false;
48655 bool saved_colon_doesnt_start_class_def_p;
48656 saved_colon_doesnt_start_class_def_p
48657 = parser->colon_doesnt_start_class_def_p;
48658 parser->colon_doesnt_start_class_def_p = true;
48660 while (true)
48662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48663 type = cp_parser_type_id (parser);
48664 if (type == error_mark_node)
48666 else if (ARITHMETIC_TYPE_P (type)
48667 && (orig_reduc_id == NULL_TREE
48668 || (TREE_CODE (type) != COMPLEX_TYPE
48669 && (id_equal (orig_reduc_id, "min")
48670 || id_equal (orig_reduc_id, "max")))))
48671 error_at (loc, "predeclared arithmetic type %qT in "
48672 "%<#pragma omp declare reduction%>", type);
48673 else if (FUNC_OR_METHOD_TYPE_P (type)
48674 || TREE_CODE (type) == ARRAY_TYPE)
48675 error_at (loc, "function or array type %qT in "
48676 "%<#pragma omp declare reduction%>", type);
48677 else if (TYPE_REF_P (type))
48678 error_at (loc, "reference type %qT in "
48679 "%<#pragma omp declare reduction%>", type);
48680 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
48681 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
48682 "type %qT in %<#pragma omp declare reduction%>", type);
48683 else
48684 types.safe_push (type);
48686 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48687 cp_lexer_consume_token (parser->lexer);
48688 else
48689 break;
48692 /* Restore the saved message. */
48693 parser->type_definition_forbidden_message = saved_message;
48694 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
48695 parser->colon_doesnt_start_class_def_p
48696 = saved_colon_doesnt_start_class_def_p;
48698 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
48699 || types.is_empty ())
48701 fail:
48702 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48703 goto done;
48706 first_token = cp_lexer_peek_token (parser->lexer);
48707 cp = NULL;
48708 errs = errorcount;
48709 FOR_EACH_VEC_ELT (types, i, type)
48711 tree fntype
48712 = build_function_type_list (void_type_node,
48713 cp_build_reference_type (type, false),
48714 NULL_TREE);
48715 tree this_reduc_id = reduc_id;
48716 if (!dependent_type_p (type))
48717 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
48718 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
48719 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
48720 DECL_ARTIFICIAL (fndecl) = 1;
48721 DECL_EXTERNAL (fndecl) = 1;
48722 DECL_DECLARED_INLINE_P (fndecl) = 1;
48723 DECL_IGNORED_P (fndecl) = 1;
48724 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
48725 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
48726 DECL_ATTRIBUTES (fndecl)
48727 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
48728 DECL_ATTRIBUTES (fndecl));
48729 bool block_scope = false;
48730 if (current_function_decl)
48732 block_scope = true;
48733 DECL_CONTEXT (fndecl) = current_function_decl;
48734 DECL_LOCAL_DECL_P (fndecl) = true;
48737 if (processing_template_decl)
48738 fndecl = push_template_decl (fndecl);
48740 if (block_scope)
48742 if (!processing_template_decl)
48743 pushdecl (fndecl);
48745 else if (current_class_type)
48747 if (cp == NULL)
48749 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48750 cp_lexer_consume_token (parser->lexer);
48751 cp = cp_token_cache_new (first_token,
48752 cp_lexer_peek_nth_token (parser->lexer,
48753 2));
48755 DECL_STATIC_FUNCTION_P (fndecl) = 1;
48756 finish_member_declaration (fndecl);
48757 DECL_PENDING_INLINE_INFO (fndecl) = cp;
48758 DECL_PENDING_INLINE_P (fndecl) = 1;
48759 vec_safe_push (unparsed_funs_with_definitions, fndecl);
48760 continue;
48762 else
48764 DECL_CONTEXT (fndecl) = current_namespace;
48765 tree d = pushdecl (fndecl);
48766 /* We should never meet a matched duplicate decl. */
48767 gcc_checking_assert (d == error_mark_node || d == fndecl);
48770 tree block = NULL_TREE;
48771 if (!block_scope)
48772 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
48773 else
48774 block = begin_omp_structured_block ();
48775 if (cp)
48777 cp_parser_push_lexer_for_tokens (parser, cp);
48778 parser->lexer->in_pragma = true;
48781 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
48783 if (cp)
48784 cp_parser_pop_lexer (parser);
48785 if (!block_scope)
48786 finish_function (/*inline_p=*/false);
48787 else
48789 DECL_CONTEXT (fndecl) = current_function_decl;
48790 if (DECL_TEMPLATE_INFO (fndecl))
48791 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
48793 if (!ok)
48794 goto fail;
48796 if (block_scope)
48798 block = finish_omp_structured_block (block);
48799 if (TREE_CODE (block) == BIND_EXPR)
48800 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
48801 else if (TREE_CODE (block) == STATEMENT_LIST)
48802 DECL_SAVED_TREE (fndecl) = block;
48803 if (processing_template_decl)
48804 add_decl_expr (fndecl);
48807 cp_check_omp_declare_reduction (fndecl);
48808 if (cp == NULL && types.length () > 1)
48809 cp = cp_token_cache_new (first_token,
48810 cp_lexer_peek_nth_token (parser->lexer, 2));
48811 if (errs != errorcount)
48812 break;
48815 cp_parser_require_pragma_eol (parser, pragma_tok);
48817 done:
48818 /* Free any declarators allocated. */
48819 obstack_free (&declarator_obstack, p);
48822 /* OpenMP 4.0
48823 #pragma omp declare simd declare-simd-clauses[optseq] new-line
48824 #pragma omp declare reduction (reduction-id : typename-list : expression) \
48825 initializer-clause[opt] new-line
48826 #pragma omp declare target new-line
48828 OpenMP 5.0
48829 #pragma omp declare variant (identifier) match (context-selector) */
48831 static bool
48832 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
48833 enum pragma_context context)
48835 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48837 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48838 const char *p = IDENTIFIER_POINTER (id);
48840 if (strcmp (p, "simd") == 0)
48842 cp_lexer_consume_token (parser->lexer);
48843 cp_parser_omp_declare_simd (parser, pragma_tok,
48844 context, false);
48845 return true;
48847 if (flag_openmp && strcmp (p, "variant") == 0)
48849 cp_lexer_consume_token (parser->lexer);
48850 cp_parser_omp_declare_simd (parser, pragma_tok,
48851 context, true);
48852 return true;
48854 cp_ensure_no_omp_declare_simd (parser);
48855 if (strcmp (p, "reduction") == 0)
48857 cp_lexer_consume_token (parser->lexer);
48858 cp_parser_omp_declare_reduction (parser, pragma_tok,
48859 context);
48860 return false;
48862 if (!flag_openmp) /* flag_openmp_simd */
48864 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48865 return false;
48867 if (strcmp (p, "target") == 0)
48869 cp_lexer_consume_token (parser->lexer);
48870 cp_parser_omp_declare_target (parser, pragma_tok);
48871 return false;
48874 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
48875 "%<target%> or %<variant%>");
48876 cp_parser_require_pragma_eol (parser, pragma_tok);
48877 return false;
48880 /* OpenMP 5.0
48881 #pragma omp requires clauses[optseq] new-line */
48883 static bool
48884 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
48886 enum omp_requires new_req = (enum omp_requires) 0;
48888 location_t loc = pragma_tok->location;
48889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48891 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48892 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48893 cp_lexer_consume_token (parser->lexer);
48895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48898 const char *p = IDENTIFIER_POINTER (id);
48899 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48900 enum omp_requires this_req = (enum omp_requires) 0;
48902 if (!strcmp (p, "unified_address"))
48903 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
48904 else if (!strcmp (p, "unified_shared_memory"))
48905 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
48906 else if (!strcmp (p, "dynamic_allocators"))
48907 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
48908 else if (!strcmp (p, "reverse_offload"))
48909 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
48910 else if (!strcmp (p, "atomic_default_mem_order"))
48912 cp_lexer_consume_token (parser->lexer);
48914 matching_parens parens;
48915 if (parens.require_open (parser))
48917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48919 id = cp_lexer_peek_token (parser->lexer)->u.value;
48920 p = IDENTIFIER_POINTER (id);
48922 if (!strcmp (p, "seq_cst"))
48923 this_req
48924 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
48925 else if (!strcmp (p, "relaxed"))
48926 this_req
48927 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
48928 else if (!strcmp (p, "acq_rel"))
48929 this_req
48930 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
48932 if (this_req == 0)
48934 error_at (cp_lexer_peek_token (parser->lexer)->location,
48935 "expected %<seq_cst%>, %<relaxed%> or "
48936 "%<acq_rel%>");
48937 switch (cp_lexer_peek_token (parser->lexer)->type)
48939 case CPP_EOF:
48940 case CPP_PRAGMA_EOL:
48941 case CPP_CLOSE_PAREN:
48942 break;
48943 default:
48944 if (cp_lexer_nth_token_is (parser->lexer, 2,
48945 CPP_CLOSE_PAREN))
48946 cp_lexer_consume_token (parser->lexer);
48947 break;
48950 else
48951 cp_lexer_consume_token (parser->lexer);
48953 if (!parens.require_close (parser))
48954 cp_parser_skip_to_closing_parenthesis (parser,
48955 /*recovering=*/true,
48956 /*or_comma=*/false,
48957 /*consume_paren=*/
48958 true);
48960 if (this_req == 0)
48962 cp_parser_require_pragma_eol (parser, pragma_tok);
48963 return false;
48966 p = NULL;
48968 else
48970 error_at (cloc, "expected %<unified_address%>, "
48971 "%<unified_shared_memory%>, "
48972 "%<dynamic_allocators%>, "
48973 "%<reverse_offload%> "
48974 "or %<atomic_default_mem_order%> clause");
48975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48976 return false;
48978 if (p)
48979 cp_lexer_consume_token (parser->lexer);
48980 if (this_req)
48982 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48984 if ((this_req & new_req) != 0)
48985 error_at (cloc, "too many %qs clauses", p);
48986 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
48987 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
48988 error_at (cloc, "%qs clause used lexically after first "
48989 "target construct or offloading API", p);
48991 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48993 error_at (cloc, "too many %qs clauses",
48994 "atomic_default_mem_order");
48995 this_req = (enum omp_requires) 0;
48997 else if ((omp_requires_mask
48998 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49000 error_at (cloc, "more than one %<atomic_default_mem_order%>"
49001 " clause in a single compilation unit");
49002 this_req
49003 = (enum omp_requires)
49004 (omp_requires_mask
49005 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
49007 else if ((omp_requires_mask
49008 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
49009 error_at (cloc, "%<atomic_default_mem_order%> clause used "
49010 "lexically after first %<atomic%> construct "
49011 "without memory order clause");
49012 new_req = (enum omp_requires) (new_req | this_req);
49013 omp_requires_mask
49014 = (enum omp_requires) (omp_requires_mask | this_req);
49015 continue;
49018 break;
49020 cp_parser_require_pragma_eol (parser, pragma_tok);
49022 if (new_req == 0)
49023 error_at (loc, "%<pragma omp requires%> requires at least one clause");
49024 return false;
49028 /* OpenMP 5.1:
49029 #pragma omp nothing new-line */
49031 static void
49032 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
49034 cp_parser_require_pragma_eol (parser, pragma_tok);
49038 /* OpenMP 5.1
49039 #pragma omp error clauses[optseq] new-line */
49041 static bool
49042 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
49043 enum pragma_context context)
49045 int at_compilation = -1;
49046 int severity_fatal = -1;
49047 tree message = NULL_TREE;
49048 bool bad = false;
49049 location_t loc = pragma_tok->location;
49051 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49054 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49055 cp_lexer_consume_token (parser->lexer);
49057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
49058 break;
49060 const char *p
49061 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49062 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49063 static const char *args[] = {
49064 "execution", "compilation", "warning", "fatal"
49066 int *v = NULL;
49067 int idx = 0, n = -1;
49068 tree m = NULL_TREE;
49070 if (!strcmp (p, "at"))
49071 v = &at_compilation;
49072 else if (!strcmp (p, "severity"))
49074 v = &severity_fatal;
49075 idx += 2;
49077 else if (strcmp (p, "message"))
49079 error_at (cloc,
49080 "expected %<at%>, %<severity%> or %<message%> clause");
49081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49082 return false;
49085 cp_lexer_consume_token (parser->lexer);
49087 matching_parens parens;
49088 if (parens.require_open (parser))
49090 if (v == NULL)
49092 m = cp_parser_assignment_expression (parser);
49093 if (type_dependent_expression_p (m))
49094 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
49095 else
49096 m = perform_implicit_conversion_flags (const_string_type_node, m,
49097 tf_warning_or_error,
49098 LOOKUP_NORMAL);
49100 else
49102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49104 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
49105 const char *q = IDENTIFIER_POINTER (val);
49107 if (!strcmp (q, args[idx]))
49108 n = 0;
49109 else if (!strcmp (q, args[idx + 1]))
49110 n = 1;
49112 if (n == -1)
49114 error_at (cp_lexer_peek_token (parser->lexer)->location,
49115 "expected %qs or %qs", args[idx], args[idx + 1]);
49116 bad = true;
49117 switch (cp_lexer_peek_token (parser->lexer)->type)
49119 case CPP_EOF:
49120 case CPP_PRAGMA_EOL:
49121 case CPP_CLOSE_PAREN:
49122 break;
49123 default:
49124 if (cp_lexer_nth_token_is (parser->lexer, 2,
49125 CPP_CLOSE_PAREN))
49126 cp_lexer_consume_token (parser->lexer);
49127 break;
49130 else
49131 cp_lexer_consume_token (parser->lexer);
49134 if (!parens.require_close (parser))
49135 cp_parser_skip_to_closing_parenthesis (parser,
49136 /*recovering=*/true,
49137 /*or_comma=*/false,
49138 /*consume_paren=*/
49139 true);
49141 if (v == NULL)
49143 if (message)
49145 error_at (cloc, "too many %qs clauses", p);
49146 bad = true;
49148 else
49149 message = m;
49151 else if (n != -1)
49153 if (*v != -1)
49155 error_at (cloc, "too many %qs clauses", p);
49156 bad = true;
49158 else
49159 *v = n;
49162 else
49163 bad = true;
49165 cp_parser_require_pragma_eol (parser, pragma_tok);
49166 if (bad)
49167 return true;
49169 if (at_compilation == -1)
49170 at_compilation = 1;
49171 if (severity_fatal == -1)
49172 severity_fatal = 1;
49173 if (!at_compilation)
49175 if (context != pragma_compound)
49177 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49178 "may only be used in compound statements");
49179 return true;
49181 tree fndecl
49182 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49183 : BUILT_IN_GOMP_WARNING);
49184 if (!message)
49185 message = build_zero_cst (const_string_type_node);
49186 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49187 build_all_ones_cst (size_type_node));
49188 add_stmt (stmt);
49189 return true;
49192 if (in_discarded_stmt)
49193 return false;
49195 const char *msg = NULL;
49196 if (message)
49198 msg = c_getstr (fold_for_warn (message));
49199 if (msg == NULL)
49200 msg = _("<message unknown at compile time>");
49202 if (msg)
49203 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49204 "%<pragma omp error%> encountered: %s", msg);
49205 else
49206 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49207 "%<pragma omp error%> encountered");
49208 return false;
49211 /* OpenMP 4.5:
49212 #pragma omp taskloop taskloop-clause[optseq] new-line
49213 for-loop
49215 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
49216 for-loop */
49218 #define OMP_TASKLOOP_CLAUSE_MASK \
49219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
49220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
49221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
49222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
49223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
49224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
49225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
49226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
49227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
49228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
49229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
49230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
49231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
49232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
49233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
49234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
49235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
49237 static tree
49238 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
49239 char *p_name, omp_clause_mask mask, tree *cclauses,
49240 bool *if_p)
49242 tree clauses, sb, ret;
49243 unsigned int save;
49244 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49246 strcat (p_name, " taskloop");
49247 mask |= OMP_TASKLOOP_CLAUSE_MASK;
49248 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
49249 clause. */
49250 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
49251 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
49253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49256 const char *p = IDENTIFIER_POINTER (id);
49258 if (strcmp (p, "simd") == 0)
49260 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
49261 if (cclauses == NULL)
49262 cclauses = cclauses_buf;
49264 cp_lexer_consume_token (parser->lexer);
49265 if (!flag_openmp) /* flag_openmp_simd */
49266 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49267 cclauses, if_p);
49268 sb = begin_omp_structured_block ();
49269 save = cp_parser_begin_omp_structured_block (parser);
49270 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49271 cclauses, if_p);
49272 cp_parser_end_omp_structured_block (parser, save);
49273 tree body = finish_omp_structured_block (sb);
49274 if (ret == NULL)
49275 return ret;
49276 ret = make_node (OMP_TASKLOOP);
49277 TREE_TYPE (ret) = void_type_node;
49278 OMP_FOR_BODY (ret) = body;
49279 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49280 SET_EXPR_LOCATION (ret, loc);
49281 add_stmt (ret);
49282 return ret;
49285 if (!flag_openmp) /* flag_openmp_simd */
49287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49288 return NULL_TREE;
49291 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
49292 cclauses == NULL);
49293 if (cclauses)
49295 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
49296 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49299 keep_next_level (true);
49300 sb = begin_omp_structured_block ();
49301 save = cp_parser_begin_omp_structured_block (parser);
49303 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
49304 if_p);
49306 cp_parser_end_omp_structured_block (parser, save);
49307 add_stmt (finish_omp_structured_block (sb));
49309 return ret;
49313 /* OpenACC 2.0:
49314 # pragma acc routine oacc-routine-clause[optseq] new-line
49315 function-definition
49317 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
49320 #define OACC_ROUTINE_CLAUSE_MASK \
49321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
49322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
49323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
49324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
49325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
49327 /* Parse the OpenACC routine pragma. This has an optional '( name )'
49328 component, which must resolve to a declared namespace-scope
49329 function. The clauses are either processed directly (for a named
49330 function), or defered until the immediatley following declaration
49331 is parsed. */
49333 static void
49334 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
49335 enum pragma_context context)
49337 gcc_checking_assert (context == pragma_external);
49338 /* The checking for "another pragma following this one" in the "no optional
49339 '( name )'" case makes sure that we dont re-enter. */
49340 gcc_checking_assert (parser->oacc_routine == NULL);
49342 cp_oacc_routine_data data;
49343 data.error_seen = false;
49344 data.fndecl_seen = false;
49345 data.tokens = vNULL;
49346 data.clauses = NULL_TREE;
49347 data.loc = pragma_tok->location;
49348 /* It is safe to take the address of a local variable; it will only be
49349 used while this scope is live. */
49350 parser->oacc_routine = &data;
49352 /* Look for optional '( name )'. */
49353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49355 matching_parens parens;
49356 parens.consume_open (parser); /* '(' */
49358 /* We parse the name as an id-expression. If it resolves to
49359 anything other than a non-overloaded function at namespace
49360 scope, it's an error. */
49361 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
49362 tree name = cp_parser_id_expression (parser,
49363 /*template_keyword_p=*/false,
49364 /*check_dependency_p=*/false,
49365 /*template_p=*/NULL,
49366 /*declarator_p=*/false,
49367 /*optional_p=*/false);
49368 tree decl = (identifier_p (name)
49369 ? cp_parser_lookup_name_simple (parser, name, name_loc)
49370 : name);
49371 if (name != error_mark_node && decl == error_mark_node)
49372 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
49374 if (decl == error_mark_node
49375 || !parens.require_close (parser))
49377 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49378 parser->oacc_routine = NULL;
49379 return;
49382 data.clauses
49383 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49384 "#pragma acc routine",
49385 cp_lexer_peek_token (parser->lexer));
49386 /* The clauses are in reverse order; fix that to make later diagnostic
49387 emission easier. */
49388 data.clauses = nreverse (data.clauses);
49390 if (decl && is_overloaded_fn (decl)
49391 && (TREE_CODE (decl) != FUNCTION_DECL
49392 || DECL_FUNCTION_TEMPLATE_P (decl)))
49394 error_at (name_loc,
49395 "%<#pragma acc routine%> names a set of overloads");
49396 parser->oacc_routine = NULL;
49397 return;
49400 /* Perhaps we should use the same rule as declarations in different
49401 namespaces? */
49402 if (!DECL_NAMESPACE_SCOPE_P (decl))
49404 error_at (name_loc,
49405 "%qD does not refer to a namespace scope function", decl);
49406 parser->oacc_routine = NULL;
49407 return;
49410 if (TREE_CODE (decl) != FUNCTION_DECL)
49412 error_at (name_loc, "%qD does not refer to a function", decl);
49413 parser->oacc_routine = NULL;
49414 return;
49417 cp_finalize_oacc_routine (parser, decl, false);
49418 parser->oacc_routine = NULL;
49420 else /* No optional '( name )'. */
49422 /* Store away all pragma tokens. */
49423 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49424 cp_lexer_consume_token (parser->lexer);
49425 cp_parser_require_pragma_eol (parser, pragma_tok);
49426 struct cp_token_cache *cp
49427 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
49428 parser->oacc_routine->tokens.safe_push (cp);
49430 /* Emit a helpful diagnostic if there's another pragma following this
49431 one. */
49432 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
49434 cp_ensure_no_oacc_routine (parser);
49435 data.tokens.release ();
49436 /* ..., and then just keep going. */
49437 return;
49440 /* We only have to consider the pragma_external case here. */
49441 cp_parser_declaration (parser, NULL_TREE);
49442 if (parser->oacc_routine
49443 && !parser->oacc_routine->fndecl_seen)
49444 cp_ensure_no_oacc_routine (parser);
49445 else
49446 parser->oacc_routine = NULL;
49447 data.tokens.release ();
49451 /* Finalize #pragma acc routine clauses after direct declarator has
49452 been parsed. */
49454 static tree
49455 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
49457 struct cp_token_cache *ce;
49458 cp_oacc_routine_data *data = parser->oacc_routine;
49460 if (!data->error_seen && data->fndecl_seen)
49462 error_at (data->loc,
49463 "%<#pragma acc routine%> not immediately followed by "
49464 "a single function declaration or definition");
49465 data->error_seen = true;
49467 if (data->error_seen)
49468 return attrs;
49470 gcc_checking_assert (data->tokens.length () == 1);
49471 ce = data->tokens[0];
49473 cp_parser_push_lexer_for_tokens (parser, ce);
49474 parser->lexer->in_pragma = true;
49475 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
49477 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
49478 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
49479 parser->oacc_routine->clauses
49480 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49481 "#pragma acc routine", pragma_tok);
49482 /* The clauses are in reverse order; fix that to make later diagnostic
49483 emission easier. */
49484 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
49485 cp_parser_pop_lexer (parser);
49486 /* Later, cp_finalize_oacc_routine will process the clauses. */
49487 parser->oacc_routine->fndecl_seen = true;
49489 return attrs;
49492 /* Apply any saved OpenACC routine clauses to a just-parsed
49493 declaration. */
49495 static void
49496 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
49498 if (UNLIKELY (parser->oacc_routine != NULL))
49500 /* Keep going if we're in error reporting mode. */
49501 if (parser->oacc_routine->error_seen
49502 || fndecl == error_mark_node)
49503 return;
49505 if (TREE_CODE (fndecl) != FUNCTION_DECL)
49507 if (parser->oacc_routine->fndecl_seen)
49509 error_at (parser->oacc_routine->loc,
49510 "%<#pragma acc routine%> not immediately followed by"
49511 " a single function declaration or definition");
49512 parser->oacc_routine = NULL;
49513 return;
49516 cp_ensure_no_oacc_routine (parser);
49517 return;
49520 int compatible
49521 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
49522 parser->oacc_routine->loc,
49523 "#pragma acc routine");
49524 if (compatible < 0)
49526 parser->oacc_routine = NULL;
49527 return;
49529 if (compatible > 0)
49532 else
49534 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
49536 error_at (parser->oacc_routine->loc,
49537 TREE_USED (fndecl)
49538 ? G_("%<#pragma acc routine%> must be applied before"
49539 " use")
49540 : G_("%<#pragma acc routine%> must be applied before"
49541 " definition"));
49542 parser->oacc_routine = NULL;
49543 return;
49546 /* Set the routine's level of parallelism. */
49547 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
49548 oacc_replace_fn_attrib (fndecl, dims);
49550 /* Add an "omp declare target" attribute. */
49551 DECL_ATTRIBUTES (fndecl)
49552 = tree_cons (get_identifier ("omp declare target"),
49553 parser->oacc_routine->clauses,
49554 DECL_ATTRIBUTES (fndecl));
49559 /* Main entry point to OpenMP statement pragmas. */
49561 static void
49562 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
49564 tree stmt;
49565 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
49566 omp_clause_mask mask (0);
49568 switch (cp_parser_pragma_kind (pragma_tok))
49570 case PRAGMA_OACC_ATOMIC:
49571 cp_parser_omp_atomic (parser, pragma_tok, true);
49572 return;
49573 case PRAGMA_OACC_CACHE:
49574 stmt = cp_parser_oacc_cache (parser, pragma_tok);
49575 break;
49576 case PRAGMA_OACC_DATA:
49577 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
49578 break;
49579 case PRAGMA_OACC_ENTER_DATA:
49580 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
49581 break;
49582 case PRAGMA_OACC_EXIT_DATA:
49583 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
49584 break;
49585 case PRAGMA_OACC_HOST_DATA:
49586 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
49587 break;
49588 case PRAGMA_OACC_KERNELS:
49589 case PRAGMA_OACC_PARALLEL:
49590 case PRAGMA_OACC_SERIAL:
49591 strcpy (p_name, "#pragma acc");
49592 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
49593 break;
49594 case PRAGMA_OACC_LOOP:
49595 strcpy (p_name, "#pragma acc");
49596 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
49597 if_p);
49598 break;
49599 case PRAGMA_OACC_UPDATE:
49600 stmt = cp_parser_oacc_update (parser, pragma_tok);
49601 break;
49602 case PRAGMA_OACC_WAIT:
49603 stmt = cp_parser_oacc_wait (parser, pragma_tok);
49604 break;
49605 case PRAGMA_OMP_ALLOCATE:
49606 cp_parser_omp_allocate (parser, pragma_tok);
49607 return;
49608 case PRAGMA_OMP_ATOMIC:
49609 cp_parser_omp_atomic (parser, pragma_tok, false);
49610 return;
49611 case PRAGMA_OMP_CRITICAL:
49612 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
49613 break;
49614 case PRAGMA_OMP_DISTRIBUTE:
49615 strcpy (p_name, "#pragma omp");
49616 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
49617 if_p);
49618 break;
49619 case PRAGMA_OMP_FOR:
49620 strcpy (p_name, "#pragma omp");
49621 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
49622 if_p);
49623 break;
49624 case PRAGMA_OMP_LOOP:
49625 strcpy (p_name, "#pragma omp");
49626 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
49627 if_p);
49628 break;
49629 case PRAGMA_OMP_MASKED:
49630 strcpy (p_name, "#pragma omp");
49631 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
49632 if_p);
49633 break;
49634 case PRAGMA_OMP_MASTER:
49635 strcpy (p_name, "#pragma omp");
49636 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
49637 if_p);
49638 break;
49639 case PRAGMA_OMP_PARALLEL:
49640 strcpy (p_name, "#pragma omp");
49641 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
49642 if_p);
49643 break;
49644 case PRAGMA_OMP_SCOPE:
49645 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
49646 break;
49647 case PRAGMA_OMP_SECTIONS:
49648 strcpy (p_name, "#pragma omp");
49649 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
49650 break;
49651 case PRAGMA_OMP_SIMD:
49652 strcpy (p_name, "#pragma omp");
49653 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
49654 if_p);
49655 break;
49656 case PRAGMA_OMP_SINGLE:
49657 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
49658 break;
49659 case PRAGMA_OMP_TASK:
49660 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
49661 break;
49662 case PRAGMA_OMP_TASKGROUP:
49663 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
49664 break;
49665 case PRAGMA_OMP_TASKLOOP:
49666 strcpy (p_name, "#pragma omp");
49667 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
49668 if_p);
49669 break;
49670 case PRAGMA_OMP_TEAMS:
49671 strcpy (p_name, "#pragma omp");
49672 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
49673 if_p);
49674 break;
49675 case PRAGMA_OMP_ASSUME:
49676 cp_parser_omp_assume (parser, pragma_tok, if_p);
49677 return;
49678 default:
49679 gcc_unreachable ();
49682 protected_set_expr_location (stmt, pragma_tok->location);
49685 /* Transactional Memory parsing routines. */
49687 /* Parse a transaction attribute.
49689 txn-attribute:
49690 attribute
49691 [ [ identifier ] ]
49693 We use this instead of cp_parser_attributes_opt for transactions to avoid
49694 the pedwarn in C++98 mode. */
49696 static tree
49697 cp_parser_txn_attribute_opt (cp_parser *parser)
49699 cp_token *token;
49700 tree attr_name, attr = NULL;
49702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
49703 return cp_parser_attributes_opt (parser);
49705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
49706 return NULL_TREE;
49707 cp_lexer_consume_token (parser->lexer);
49708 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
49709 goto error1;
49711 token = cp_lexer_peek_token (parser->lexer);
49712 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
49714 token = cp_lexer_consume_token (parser->lexer);
49716 attr_name = (token->type == CPP_KEYWORD
49717 /* For keywords, use the canonical spelling,
49718 not the parsed identifier. */
49719 ? ridpointers[(int) token->keyword]
49720 : token->u.value);
49721 attr = build_tree_list (attr_name, NULL_TREE);
49723 else
49724 cp_parser_error (parser, "expected identifier");
49726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
49727 error1:
49728 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
49729 return attr;
49732 /* Parse a __transaction_atomic or __transaction_relaxed statement.
49734 transaction-statement:
49735 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
49736 compound-statement
49737 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
49740 static tree
49741 cp_parser_transaction (cp_parser *parser, cp_token *token)
49743 unsigned char old_in = parser->in_transaction;
49744 unsigned char this_in = 1, new_in;
49745 enum rid keyword = token->keyword;
49746 tree stmt, attrs, noex;
49748 cp_lexer_consume_token (parser->lexer);
49750 if (keyword == RID_TRANSACTION_RELAXED
49751 || keyword == RID_SYNCHRONIZED)
49752 this_in |= TM_STMT_ATTR_RELAXED;
49753 else
49755 attrs = cp_parser_txn_attribute_opt (parser);
49756 if (attrs)
49757 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49760 /* Parse a noexcept specification. */
49761 if (keyword == RID_ATOMIC_NOEXCEPT)
49762 noex = boolean_true_node;
49763 else if (keyword == RID_ATOMIC_CANCEL)
49765 /* cancel-and-throw is unimplemented. */
49766 sorry ("%<atomic_cancel%>");
49767 noex = NULL_TREE;
49769 else
49770 noex = cp_parser_noexcept_specification_opt (parser,
49771 CP_PARSER_FLAGS_NONE,
49772 /*require_constexpr=*/true,
49773 /*consumed_expr=*/NULL,
49774 /*return_cond=*/true);
49776 /* Keep track if we're in the lexical scope of an outer transaction. */
49777 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
49779 stmt = begin_transaction_stmt (token->location, NULL, this_in);
49781 parser->in_transaction = new_in;
49782 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
49783 parser->in_transaction = old_in;
49785 finish_transaction_stmt (stmt, NULL, this_in, noex);
49787 return stmt;
49790 /* Parse a __transaction_atomic or __transaction_relaxed expression.
49792 transaction-expression:
49793 __transaction_atomic txn-noexcept-spec[opt] ( expression )
49794 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
49797 static tree
49798 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
49800 unsigned char old_in = parser->in_transaction;
49801 unsigned char this_in = 1;
49802 cp_token *token;
49803 tree expr, noex;
49804 bool noex_expr;
49805 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49807 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49808 || keyword == RID_TRANSACTION_RELAXED);
49810 if (!flag_tm)
49811 error_at (loc,
49812 keyword == RID_TRANSACTION_RELAXED
49813 ? G_("%<__transaction_relaxed%> without transactional memory "
49814 "support enabled")
49815 : G_("%<__transaction_atomic%> without transactional memory "
49816 "support enabled"));
49818 token = cp_parser_require_keyword (parser, keyword,
49819 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49820 : RT_TRANSACTION_RELAXED));
49821 gcc_assert (token != NULL);
49823 if (keyword == RID_TRANSACTION_RELAXED)
49824 this_in |= TM_STMT_ATTR_RELAXED;
49826 /* Set this early. This might mean that we allow transaction_cancel in
49827 an expression that we find out later actually has to be a constexpr.
49828 However, we expect that cxx_constant_value will be able to deal with
49829 this; also, if the noexcept has no constexpr, then what we parse next
49830 really is a transaction's body. */
49831 parser->in_transaction = this_in;
49833 /* Parse a noexcept specification. */
49834 noex = cp_parser_noexcept_specification_opt (parser,
49835 CP_PARSER_FLAGS_NONE,
49836 /*require_constexpr=*/false,
49837 &noex_expr,
49838 /*return_cond=*/true);
49840 if (!noex || !noex_expr
49841 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
49843 matching_parens parens;
49844 parens.require_open (parser);
49846 expr = cp_parser_expression (parser);
49847 expr = finish_parenthesized_expr (expr);
49849 parens.require_close (parser);
49851 else
49853 /* The only expression that is available got parsed for the noexcept
49854 already. noexcept is true then. */
49855 expr = noex;
49856 noex = boolean_true_node;
49859 expr = build_transaction_expr (token->location, expr, this_in, noex);
49860 parser->in_transaction = old_in;
49862 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
49863 return error_mark_node;
49865 return (flag_tm ? expr : error_mark_node);
49868 /* Parse a function-transaction-block.
49870 function-transaction-block:
49871 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
49872 function-body
49873 __transaction_atomic txn-attribute[opt] function-try-block
49874 __transaction_relaxed ctor-initializer[opt] function-body
49875 __transaction_relaxed function-try-block
49878 static void
49879 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
49881 unsigned char old_in = parser->in_transaction;
49882 unsigned char new_in = 1;
49883 tree compound_stmt, stmt, attrs;
49884 cp_token *token;
49886 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49887 || keyword == RID_TRANSACTION_RELAXED);
49888 token = cp_parser_require_keyword (parser, keyword,
49889 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49890 : RT_TRANSACTION_RELAXED));
49891 gcc_assert (token != NULL);
49893 if (keyword == RID_TRANSACTION_RELAXED)
49894 new_in |= TM_STMT_ATTR_RELAXED;
49895 else
49897 attrs = cp_parser_txn_attribute_opt (parser);
49898 if (attrs)
49899 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49902 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
49904 parser->in_transaction = new_in;
49906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
49907 cp_parser_function_try_block (parser);
49908 else
49909 cp_parser_ctor_initializer_opt_and_function_body
49910 (parser, /*in_function_try_block=*/false);
49912 parser->in_transaction = old_in;
49914 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
49917 /* Parse a __transaction_cancel statement.
49919 cancel-statement:
49920 __transaction_cancel txn-attribute[opt] ;
49921 __transaction_cancel txn-attribute[opt] throw-expression ;
49923 ??? Cancel and throw is not yet implemented. */
49925 static tree
49926 cp_parser_transaction_cancel (cp_parser *parser)
49928 cp_token *token;
49929 bool is_outer = false;
49930 tree stmt, attrs;
49932 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
49933 RT_TRANSACTION_CANCEL);
49934 gcc_assert (token != NULL);
49936 attrs = cp_parser_txn_attribute_opt (parser);
49937 if (attrs)
49938 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
49940 /* ??? Parse cancel-and-throw here. */
49942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
49944 if (!flag_tm)
49946 error_at (token->location, "%<__transaction_cancel%> without "
49947 "transactional memory support enabled");
49948 return error_mark_node;
49950 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
49952 error_at (token->location, "%<__transaction_cancel%> within a "
49953 "%<__transaction_relaxed%>");
49954 return error_mark_node;
49956 else if (is_outer)
49958 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
49959 && !is_tm_may_cancel_outer (current_function_decl))
49961 error_at (token->location, "outer %<__transaction_cancel%> not "
49962 "within outer %<__transaction_atomic%>");
49963 error_at (token->location,
49964 " or a %<transaction_may_cancel_outer%> function");
49965 return error_mark_node;
49968 else if (parser->in_transaction == 0)
49970 error_at (token->location, "%<__transaction_cancel%> not within "
49971 "%<__transaction_atomic%>");
49972 return error_mark_node;
49975 stmt = build_tm_abort_call (token->location, is_outer);
49976 add_stmt (stmt);
49978 return stmt;
49982 /* Special handling for the first token or line in the file. The first
49983 thing in the file might be #pragma GCC pch_preprocess, which loads a
49984 PCH file, which is a GC collection point. So we need to handle this
49985 first pragma without benefit of an existing lexer structure.
49987 Always returns one token to the caller in *FIRST_TOKEN. This is
49988 either the true first token of the file, or the first token after
49989 the initial pragma. */
49991 static void
49992 cp_parser_initial_pragma (cp_token *first_token)
49994 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
49995 return;
49997 cp_lexer_get_preprocessor_token (0, first_token);
49999 tree name = NULL;
50000 if (first_token->type == CPP_STRING)
50002 name = first_token->u.value;
50004 cp_lexer_get_preprocessor_token (0, first_token);
50007 /* Skip to the end of the pragma. */
50008 if (first_token->type != CPP_PRAGMA_EOL)
50010 error_at (first_token->location,
50011 "malformed %<#pragma GCC pch_preprocess%>");
50013 cp_lexer_get_preprocessor_token (0, first_token);
50014 while (first_token->type != CPP_PRAGMA_EOL);
50017 /* Now actually load the PCH file. */
50018 if (name)
50019 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
50021 /* Read one more token to return to our caller. We have to do this
50022 after reading the PCH file in, since its pointers have to be
50023 live. */
50024 cp_lexer_get_preprocessor_token (0, first_token);
50027 /* Parse a pragma GCC ivdep. */
50029 static bool
50030 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
50032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50033 return true;
50036 /* Parse a pragma GCC unroll. */
50038 static unsigned short
50039 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
50041 location_t location = cp_lexer_peek_token (parser->lexer)->location;
50042 tree expr = cp_parser_constant_expression (parser);
50043 unsigned short unroll;
50044 expr = maybe_constant_value (expr);
50045 HOST_WIDE_INT lunroll = 0;
50046 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
50047 || TREE_CODE (expr) != INTEGER_CST
50048 || (lunroll = tree_to_shwi (expr)) < 0
50049 || lunroll >= USHRT_MAX)
50051 error_at (location, "%<#pragma GCC unroll%> requires an"
50052 " assignment-expression that evaluates to a non-negative"
50053 " integral constant less than %u", USHRT_MAX);
50054 unroll = 0;
50056 else
50058 unroll = (unsigned short)lunroll;
50059 if (unroll == 0)
50060 unroll = 1;
50062 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50063 return unroll;
50066 /* Parse a pragma GCC novector. */
50068 static bool
50069 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
50071 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50072 return true;
50075 /* Normal parsing of a pragma token. Here we can (and must) use the
50076 regular lexer. */
50078 static bool
50079 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
50081 cp_token *pragma_tok;
50082 unsigned int id;
50083 tree stmt;
50084 bool ret = false;
50086 pragma_tok = cp_lexer_consume_token (parser->lexer);
50087 gcc_assert (pragma_tok->type == CPP_PRAGMA);
50088 parser->lexer->in_pragma = true;
50090 id = cp_parser_pragma_kind (pragma_tok);
50091 if (parser->omp_for_parse_state
50092 && parser->omp_for_parse_state->in_intervening_code
50093 && id >= PRAGMA_OMP__START_
50094 && id <= PRAGMA_OMP__LAST_)
50096 error_at (pragma_tok->location,
50097 "intervening code must not contain OpenMP directives");
50098 parser->omp_for_parse_state->fail = true;
50099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50100 return false;
50102 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
50103 cp_ensure_no_omp_declare_simd (parser);
50104 switch (id)
50106 case PRAGMA_GCC_PCH_PREPROCESS:
50107 error_at (pragma_tok->location,
50108 "%<#pragma GCC pch_preprocess%> must be first");
50109 break;
50111 case PRAGMA_OMP_BARRIER:
50112 switch (context)
50114 case pragma_compound:
50115 cp_parser_omp_barrier (parser, pragma_tok);
50116 return false;
50117 case pragma_stmt:
50118 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50119 "used in compound statements", "omp barrier");
50120 ret = true;
50121 break;
50122 default:
50123 goto bad_stmt;
50125 break;
50127 case PRAGMA_OMP_DEPOBJ:
50128 switch (context)
50130 case pragma_compound:
50131 cp_parser_omp_depobj (parser, pragma_tok);
50132 return false;
50133 case pragma_stmt:
50134 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50135 "used in compound statements", "omp depobj");
50136 ret = true;
50137 break;
50138 default:
50139 goto bad_stmt;
50141 break;
50143 case PRAGMA_OMP_FLUSH:
50144 switch (context)
50146 case pragma_compound:
50147 cp_parser_omp_flush (parser, pragma_tok);
50148 return false;
50149 case pragma_stmt:
50150 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50151 "used in compound statements", "omp flush");
50152 ret = true;
50153 break;
50154 default:
50155 goto bad_stmt;
50157 break;
50159 case PRAGMA_OMP_TASKWAIT:
50160 switch (context)
50162 case pragma_compound:
50163 cp_parser_omp_taskwait (parser, pragma_tok);
50164 return false;
50165 case pragma_stmt:
50166 error_at (pragma_tok->location,
50167 "%<#pragma %s%> may only be used in compound statements",
50168 "omp taskwait");
50169 ret = true;
50170 break;
50171 default:
50172 goto bad_stmt;
50174 break;
50176 case PRAGMA_OMP_TASKYIELD:
50177 switch (context)
50179 case pragma_compound:
50180 cp_parser_omp_taskyield (parser, pragma_tok);
50181 return false;
50182 case pragma_stmt:
50183 error_at (pragma_tok->location,
50184 "%<#pragma %s%> may only be used in compound statements",
50185 "omp taskyield");
50186 ret = true;
50187 break;
50188 default:
50189 goto bad_stmt;
50191 break;
50193 case PRAGMA_OMP_CANCEL:
50194 switch (context)
50196 case pragma_compound:
50197 cp_parser_omp_cancel (parser, pragma_tok);
50198 return false;
50199 case pragma_stmt:
50200 error_at (pragma_tok->location,
50201 "%<#pragma %s%> may only be used in compound statements",
50202 "omp cancel");
50203 ret = true;
50204 break;
50205 default:
50206 goto bad_stmt;
50208 break;
50210 case PRAGMA_OMP_CANCELLATION_POINT:
50211 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
50213 case PRAGMA_OMP_THREADPRIVATE:
50214 cp_parser_omp_threadprivate (parser, pragma_tok);
50215 return false;
50217 case PRAGMA_OMP_DECLARE:
50218 return cp_parser_omp_declare (parser, pragma_tok, context);
50220 case PRAGMA_OACC_DECLARE:
50221 cp_parser_oacc_declare (parser, pragma_tok);
50222 return false;
50224 case PRAGMA_OACC_ENTER_DATA:
50225 if (context == pragma_stmt)
50227 error_at (pragma_tok->location,
50228 "%<#pragma %s%> may only be used in compound statements",
50229 "acc enter data");
50230 ret = true;
50231 break;
50233 else if (context != pragma_compound)
50234 goto bad_stmt;
50235 cp_parser_omp_construct (parser, pragma_tok, if_p);
50236 return true;
50238 case PRAGMA_OACC_EXIT_DATA:
50239 if (context == pragma_stmt)
50241 error_at (pragma_tok->location,
50242 "%<#pragma %s%> may only be used in compound statements",
50243 "acc exit data");
50244 ret = true;
50245 break;
50247 else if (context != pragma_compound)
50248 goto bad_stmt;
50249 cp_parser_omp_construct (parser, pragma_tok, if_p);
50250 return true;
50252 case PRAGMA_OACC_ROUTINE:
50253 if (context != pragma_external)
50255 error_at (pragma_tok->location,
50256 "%<#pragma acc routine%> must be at file scope");
50257 ret = true;
50258 break;
50260 cp_parser_oacc_routine (parser, pragma_tok, context);
50261 return false;
50263 case PRAGMA_OACC_UPDATE:
50264 if (context == pragma_stmt)
50266 error_at (pragma_tok->location,
50267 "%<#pragma %s%> may only be used in compound statements",
50268 "acc update");
50269 ret = true;
50270 break;
50272 else if (context != pragma_compound)
50273 goto bad_stmt;
50274 cp_parser_omp_construct (parser, pragma_tok, if_p);
50275 return true;
50277 case PRAGMA_OACC_WAIT:
50278 if (context == pragma_stmt)
50280 error_at (pragma_tok->location,
50281 "%<#pragma %s%> may only be used in compound statements",
50282 "acc wait");
50283 ret = true;
50284 break;
50286 else if (context != pragma_compound)
50287 goto bad_stmt;
50288 cp_parser_omp_construct (parser, pragma_tok, if_p);
50289 return true;
50290 case PRAGMA_OMP_ALLOCATE:
50291 cp_parser_omp_allocate (parser, pragma_tok);
50292 return false;
50293 case PRAGMA_OACC_ATOMIC:
50294 case PRAGMA_OACC_CACHE:
50295 case PRAGMA_OACC_DATA:
50296 case PRAGMA_OACC_HOST_DATA:
50297 case PRAGMA_OACC_KERNELS:
50298 case PRAGMA_OACC_LOOP:
50299 case PRAGMA_OACC_PARALLEL:
50300 case PRAGMA_OACC_SERIAL:
50301 case PRAGMA_OMP_ASSUME:
50302 case PRAGMA_OMP_ATOMIC:
50303 case PRAGMA_OMP_CRITICAL:
50304 case PRAGMA_OMP_DISTRIBUTE:
50305 case PRAGMA_OMP_FOR:
50306 case PRAGMA_OMP_LOOP:
50307 case PRAGMA_OMP_MASKED:
50308 case PRAGMA_OMP_MASTER:
50309 case PRAGMA_OMP_PARALLEL:
50310 case PRAGMA_OMP_SCOPE:
50311 case PRAGMA_OMP_SECTIONS:
50312 case PRAGMA_OMP_SIMD:
50313 case PRAGMA_OMP_SINGLE:
50314 case PRAGMA_OMP_TASK:
50315 case PRAGMA_OMP_TASKGROUP:
50316 case PRAGMA_OMP_TASKLOOP:
50317 case PRAGMA_OMP_TEAMS:
50318 if (context != pragma_stmt && context != pragma_compound)
50319 goto bad_stmt;
50320 stmt = push_omp_privatization_clauses (false);
50321 cp_parser_omp_construct (parser, pragma_tok, if_p);
50322 pop_omp_privatization_clauses (stmt);
50323 return true;
50325 case PRAGMA_OMP_REQUIRES:
50326 if (context != pragma_external)
50328 error_at (pragma_tok->location,
50329 "%<#pragma omp requires%> may only be used at file or "
50330 "namespace scope");
50331 ret = true;
50332 break;
50334 return cp_parser_omp_requires (parser, pragma_tok);
50336 case PRAGMA_OMP_ASSUMES:
50337 if (context != pragma_external)
50339 error_at (pragma_tok->location,
50340 "%<#pragma omp assumes%> may only be used at file or "
50341 "namespace scope");
50342 ret = true;
50343 break;
50345 return cp_parser_omp_assumes (parser, pragma_tok);
50347 case PRAGMA_OMP_NOTHING:
50348 cp_parser_omp_nothing (parser, pragma_tok);
50349 return false;
50351 case PRAGMA_OMP_ERROR:
50352 return cp_parser_omp_error (parser, pragma_tok, context);
50354 case PRAGMA_OMP_ORDERED:
50355 if (context != pragma_stmt && context != pragma_compound)
50356 goto bad_stmt;
50357 stmt = push_omp_privatization_clauses (false);
50358 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
50359 pop_omp_privatization_clauses (stmt);
50360 return ret;
50362 case PRAGMA_OMP_TARGET:
50363 if (context != pragma_stmt && context != pragma_compound)
50364 goto bad_stmt;
50365 stmt = push_omp_privatization_clauses (false);
50366 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
50367 pop_omp_privatization_clauses (stmt);
50368 return ret;
50370 case PRAGMA_OMP_BEGIN:
50371 cp_parser_omp_begin (parser, pragma_tok);
50372 return false;
50374 case PRAGMA_OMP_END:
50375 cp_parser_omp_end (parser, pragma_tok);
50376 return false;
50378 case PRAGMA_OMP_SCAN:
50379 error_at (pragma_tok->location,
50380 "%<#pragma omp scan%> may only be used in "
50381 "a loop construct with %<inscan%> %<reduction%> clause");
50382 break;
50384 case PRAGMA_OMP_SECTION:
50385 error_at (pragma_tok->location,
50386 "%<#pragma omp section%> may only be used in "
50387 "%<#pragma omp sections%> construct");
50388 break;
50390 case PRAGMA_IVDEP:
50391 case PRAGMA_UNROLL:
50392 case PRAGMA_NOVECTOR:
50394 bool ivdep;
50395 unsigned short unroll = 0;
50396 bool novector = false;
50397 const char *pragma_str;
50399 switch (id)
50401 case PRAGMA_IVDEP:
50402 pragma_str = "ivdep";
50403 break;
50404 case PRAGMA_UNROLL:
50405 pragma_str = "unroll";
50406 break;
50407 case PRAGMA_NOVECTOR:
50408 pragma_str = "novector";
50409 break;
50410 default:
50411 gcc_unreachable ();
50414 if (context == pragma_external)
50416 error_at (pragma_tok->location,
50417 "%<#pragma GCC %s%> must be inside a function",
50418 pragma_str);
50419 break;
50422 cp_token *tok = pragma_tok;
50423 bool has_more = true;
50426 switch (cp_parser_pragma_kind (tok))
50428 case PRAGMA_IVDEP:
50430 if (tok != pragma_tok)
50431 tok = cp_lexer_consume_token (parser->lexer);
50432 ivdep = cp_parser_pragma_ivdep (parser, tok);
50433 break;
50435 case PRAGMA_UNROLL:
50437 if (tok != pragma_tok)
50438 tok = cp_lexer_consume_token (parser->lexer);
50439 unroll = cp_parser_pragma_unroll (parser, tok);
50440 break;
50442 case PRAGMA_NOVECTOR:
50444 if (tok != pragma_tok)
50445 tok = cp_lexer_consume_token (parser->lexer);
50446 novector = cp_parser_pragma_novector (parser, tok);
50447 break;
50449 default:
50450 has_more = false;
50451 break;
50453 tok = cp_lexer_peek_token (the_parser->lexer);
50454 has_more = has_more && tok->type == CPP_PRAGMA;
50456 while (has_more);
50458 if (tok->type != CPP_KEYWORD
50459 || (tok->keyword != RID_FOR
50460 && tok->keyword != RID_WHILE
50461 && tok->keyword != RID_DO))
50463 cp_parser_error (parser, "for, while or do statement expected");
50464 return false;
50466 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
50467 return true;
50470 default:
50471 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
50472 c_invoke_pragma_handler (id);
50473 break;
50475 bad_stmt:
50476 cp_parser_error (parser, "expected declaration specifiers");
50477 break;
50480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50481 return ret;
50484 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
50485 populated the lexer with any tokens (the tokens rather being read by
50486 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
50487 a pragma. */
50488 static void
50489 maybe_read_tokens_for_pragma_lex ()
50491 const auto lexer = the_parser->lexer;
50492 if (!lexer->buffer->is_empty ())
50493 return;
50495 /* Read the rest of the tokens comprising the pragma line. */
50496 cp_token *tok;
50499 tok = vec_safe_push (lexer->buffer, cp_token ());
50500 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
50501 gcc_assert (tok->type != CPP_EOF);
50502 } while (tok->type != CPP_PRAGMA_EOL);
50503 lexer->next_token = lexer->buffer->address ();
50504 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
50507 /* The interface the pragma parsers have to the lexer. */
50509 enum cpp_ttype
50510 pragma_lex (tree *value, location_t *loc)
50512 if (flag_preprocess_only)
50513 maybe_read_tokens_for_pragma_lex ();
50515 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
50516 enum cpp_ttype ret = tok->type;
50518 *value = tok->u.value;
50519 if (loc)
50520 *loc = tok->location;
50522 if (ret == CPP_PRAGMA_EOL)
50523 ret = CPP_EOF;
50524 else if (ret == CPP_STRING)
50525 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
50526 /*wide_ok=*/false);
50527 else
50529 if (ret == CPP_KEYWORD)
50530 ret = CPP_NAME;
50531 cp_lexer_consume_token (the_parser->lexer);
50534 return ret;
50537 void
50538 pragma_lex_discard_to_eol ()
50540 /* We have already read all the tokens, so we just need to discard
50541 them here. */
50542 const auto lexer = the_parser->lexer;
50543 lexer->next_token = lexer->last_token;
50544 lexer->buffer->truncate (0);
50548 /* External interface. */
50550 /* Parse one entire translation unit. */
50552 void
50553 c_parse_file (void)
50555 static bool already_called = false;
50557 if (already_called)
50558 fatal_error (input_location,
50559 "multi-source compilation not implemented for C++");
50560 already_called = true;
50562 /* cp_lexer_new_main is called before doing any GC allocation
50563 because tokenization might load a PCH file. */
50564 cp_lexer_new_main ();
50566 cp_parser_translation_unit (the_parser);
50567 class_decl_loc_t::diag_mismatched_tags ();
50569 the_parser = NULL;
50571 finish_translation_unit ();
50574 /* Create an identifier for a generic parameter type (a synthesized
50575 template parameter implied by `auto' or a concept identifier). */
50577 static GTY(()) int generic_parm_count;
50578 static tree
50579 make_generic_type_name ()
50581 char buf[32];
50582 sprintf (buf, "auto:%d", ++generic_parm_count);
50583 return get_identifier (buf);
50586 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
50587 (creating a new template parameter list if necessary). Returns the newly
50588 created template type parm. */
50590 static tree
50591 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
50593 /* A requires-clause is not a function and cannot have placeholders. */
50594 if (current_binding_level->requires_expression)
50596 error ("placeholder type not allowed in this context");
50597 return error_mark_node;
50600 gcc_assert (current_binding_level->kind == sk_function_parms);
50602 /* We are either continuing a function template that already contains implicit
50603 template parameters, creating a new fully-implicit function template, or
50604 extending an existing explicit function template with implicit template
50605 parameters. */
50607 cp_binding_level *const entry_scope = current_binding_level;
50609 bool become_template = false;
50610 cp_binding_level *parent_scope = 0;
50612 if (parser->implicit_template_scope)
50614 gcc_assert (parser->implicit_template_parms);
50616 current_binding_level = parser->implicit_template_scope;
50618 else
50620 /* Roll back to the existing template parameter scope (in the case of
50621 extending an explicit function template) or introduce a new template
50622 parameter scope ahead of the function parameter scope (or class scope
50623 in the case of out-of-line member definitions). The function scope is
50624 added back after template parameter synthesis below. */
50626 cp_binding_level *scope = entry_scope;
50628 while (scope->kind == sk_function_parms)
50630 parent_scope = scope;
50631 scope = scope->level_chain;
50633 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
50635 /* If not defining a class, then any class scope is a scope level in
50636 an out-of-line member definition. In this case simply wind back
50637 beyond the first such scope to inject the template parameter list.
50638 Otherwise wind back to the class being defined. The latter can
50639 occur in class member friend declarations such as:
50641 class A {
50642 void foo (auto);
50644 class B {
50645 friend void A::foo (auto);
50648 The template parameter list synthesized for the friend declaration
50649 must be injected in the scope of 'B'. This can also occur in
50650 erroneous cases such as:
50652 struct A {
50653 struct B {
50654 void foo (auto);
50656 void B::foo (auto) {}
50659 Here the attempted definition of 'B::foo' within 'A' is ill-formed
50660 but, nevertheless, the template parameter list synthesized for the
50661 declarator should be injected into the scope of 'A' as if the
50662 ill-formed template was specified explicitly. */
50664 while (scope->kind == sk_class && !scope->defining_class_p)
50666 parent_scope = scope;
50667 scope = scope->level_chain;
50671 current_binding_level = scope;
50673 if (scope->kind != sk_template_parms
50674 || !function_being_declared_is_template_p (parser))
50676 /* Introduce a new template parameter list for implicit template
50677 parameters. */
50679 become_template = true;
50681 parser->implicit_template_scope
50682 = begin_scope (sk_template_parms, NULL);
50684 ++processing_template_decl;
50686 parser->fully_implicit_function_template_p = true;
50687 ++parser->num_template_parameter_lists;
50689 else
50691 /* Synthesize implicit template parameters at the end of the explicit
50692 template parameter list. */
50694 gcc_assert (current_template_parms);
50696 parser->implicit_template_scope = scope;
50698 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
50699 parser->implicit_template_parms
50700 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
50704 /* Synthesize a new template parameter and track the current template
50705 parameter chain with implicit_template_parms. */
50707 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
50708 tree synth_id = make_generic_type_name ();
50709 bool non_type = false;
50711 /* Synthesize the type template parameter. */
50712 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
50713 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
50715 if (become_template)
50716 current_template_parms = tree_cons (size_int (current_template_depth + 1),
50717 NULL_TREE, current_template_parms);
50719 /* Attach the constraint to the parm before processing. */
50720 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
50721 TREE_TYPE (node) = constr;
50722 tree new_parm
50723 = process_template_parm (parser->implicit_template_parms,
50724 input_location,
50725 node,
50726 /*non_type=*/non_type,
50727 /*param_pack=*/false);
50728 // Process_template_parm returns the list of parms, and
50729 // parser->implicit_template_parms holds the final node of the parm
50730 // list. We really want to manipulate the newly appended element.
50731 gcc_checking_assert (!parser->implicit_template_parms
50732 || parser->implicit_template_parms == new_parm);
50733 if (parser->implicit_template_parms)
50734 new_parm = TREE_CHAIN (new_parm);
50735 gcc_checking_assert (!TREE_CHAIN (new_parm));
50737 // Record the last implicit parm node
50738 parser->implicit_template_parms = new_parm;
50740 /* Mark the synthetic declaration "virtual". This is used when
50741 comparing template-heads to determine if whether an abbreviated
50742 function template is equivalent to an explicit template.
50744 Note that DECL_ARTIFICIAL is used elsewhere for template
50745 parameters. */
50746 if (TREE_VALUE (new_parm) != error_mark_node)
50747 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
50749 tree new_decl = get_local_decls ();
50750 if (non_type)
50751 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
50752 new_decl = DECL_INITIAL (new_decl);
50754 /* If creating a fully implicit function template, start the new implicit
50755 template parameter list with this synthesized type, otherwise grow the
50756 current template parameter list. */
50758 if (become_template)
50760 parent_scope->level_chain = current_binding_level;
50762 tree new_parms = make_tree_vec (1);
50763 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
50764 TREE_VALUE (current_template_parms) = new_parms;
50766 else
50768 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
50769 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
50770 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
50771 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
50774 /* If the new parameter was constrained, we need to add that to the
50775 constraints in the template parameter list. */
50776 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
50778 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
50779 reqs = combine_constraint_expressions (reqs, req);
50780 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
50783 current_binding_level = entry_scope;
50785 return new_decl;
50788 /* Finish the declaration of a fully implicit function template. Such a
50789 template has no explicit template parameter list so has not been through the
50790 normal template head and tail processing. synthesize_implicit_template_parm
50791 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
50792 provided if the declaration is a class member such that its template
50793 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
50794 form is returned. Otherwise NULL_TREE is returned. */
50796 static tree
50797 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
50799 gcc_assert (parser->fully_implicit_function_template_p);
50801 if (member_decl_opt && member_decl_opt != error_mark_node
50802 && DECL_VIRTUAL_P (member_decl_opt))
50804 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
50805 "implicit templates may not be %<virtual%>");
50806 DECL_VIRTUAL_P (member_decl_opt) = false;
50809 if (member_decl_opt)
50810 member_decl_opt = finish_member_template_decl (member_decl_opt);
50811 end_template_decl ();
50813 parser->fully_implicit_function_template_p = false;
50814 parser->implicit_template_parms = 0;
50815 parser->implicit_template_scope = 0;
50816 --parser->num_template_parameter_lists;
50818 return member_decl_opt;
50821 /* Like finish_fully_implicit_template, but to be used in error
50822 recovery, rearranging scopes so that we restore the state we had
50823 before synthesize_implicit_template_parm inserted the implement
50824 template parms scope. */
50826 static void
50827 abort_fully_implicit_template (cp_parser *parser)
50829 cp_binding_level *return_to_scope = current_binding_level;
50831 if (parser->implicit_template_scope
50832 && return_to_scope != parser->implicit_template_scope)
50834 cp_binding_level *child = return_to_scope;
50835 for (cp_binding_level *scope = child->level_chain;
50836 scope != parser->implicit_template_scope;
50837 scope = child->level_chain)
50838 child = scope;
50839 child->level_chain = parser->implicit_template_scope->level_chain;
50840 parser->implicit_template_scope->level_chain = return_to_scope;
50841 current_binding_level = parser->implicit_template_scope;
50843 else
50844 return_to_scope = return_to_scope->level_chain;
50846 finish_fully_implicit_template (parser, NULL);
50848 gcc_assert (current_binding_level == return_to_scope);
50851 /* Helper function for diagnostics that have complained about things
50852 being used with 'extern "C"' linkage.
50854 Attempt to issue a note showing where the 'extern "C"' linkage began. */
50856 void
50857 maybe_show_extern_c_location (void)
50859 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
50860 inform (the_parser->innermost_linkage_specification_location,
50861 "%<extern \"C\"%> linkage started here");
50864 #include "gt-cp-parser.h"