LoongArch: Define loongarch_insn_cost and set the cost of movcf2gr and movgr2cf.
[official-gcc.git] / gcc / cp / parser.cc
blob26734beacce6662bc16a77ec4c29ea4b0fc64d4e
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2024 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;
249 static const cp_trait *cp_lexer_peek_trait
250 (cp_lexer *);
251 static const cp_trait *cp_lexer_peek_trait_expr
252 (cp_lexer *);
253 static const cp_trait *cp_lexer_peek_trait_type
254 (cp_lexer *);
256 static cp_token_cache *cp_token_cache_new
257 (cp_token *, cp_token *);
258 static tree cp_parser_late_noexcept_specifier
259 (cp_parser *, tree);
260 static void noexcept_override_late_checks
261 (tree);
263 static void cp_parser_initial_pragma
264 (cp_token *);
266 static bool cp_parser_omp_declare_reduction_exprs
267 (tree, cp_parser *);
268 static void cp_finalize_oacc_routine
269 (cp_parser *, tree, bool);
271 static void check_omp_intervening_code
272 (cp_parser *);
275 /* Manifest constants. */
276 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
277 #define CP_SAVED_TOKEN_STACK 5
279 /* Variables. */
281 /* The stream to which debugging output should be written. */
282 static FILE *cp_lexer_debug_stream;
284 /* Nonzero if we are parsing an unevaluated operand: an operand to
285 sizeof, typeof, or alignof. */
286 int cp_unevaluated_operand;
288 /* Dump up to NUM tokens in BUFFER to FILE starting with token
289 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
290 first token in BUFFER. If NUM is 0, dump all the tokens. If
291 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
292 highlighted by surrounding it in [[ ]]. */
294 static void
295 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
296 cp_token *start_token, unsigned num,
297 cp_token *curr_token)
299 unsigned i, nprinted;
300 cp_token *token;
301 bool do_print;
303 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
305 if (buffer == NULL)
306 return;
308 if (num == 0)
309 num = buffer->length ();
311 if (start_token == NULL)
312 start_token = buffer->address ();
314 if (start_token > buffer->address ())
316 cp_lexer_print_token (file, &(*buffer)[0]);
317 fprintf (file, " ... ");
320 do_print = false;
321 nprinted = 0;
322 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
324 if (token == start_token)
325 do_print = true;
327 if (!do_print)
328 continue;
330 nprinted++;
331 if (token == curr_token)
332 fprintf (file, "[[");
334 cp_lexer_print_token (file, token);
336 if (token == curr_token)
337 fprintf (file, "]]");
339 switch (token->type)
341 case CPP_SEMICOLON:
342 case CPP_OPEN_BRACE:
343 case CPP_CLOSE_BRACE:
344 case CPP_EOF:
345 fputc ('\n', file);
346 break;
348 default:
349 fputc (' ', file);
353 if (i == num && i < buffer->length ())
355 fprintf (file, " ... ");
356 cp_lexer_print_token (file, &buffer->last ());
359 fprintf (file, "\n");
363 /* Dump all tokens in BUFFER to stderr. */
365 void
366 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
368 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
371 DEBUG_FUNCTION void
372 debug (vec<cp_token, va_gc> &ref)
374 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
377 DEBUG_FUNCTION void
378 debug (vec<cp_token, va_gc> *ptr)
380 if (ptr)
381 debug (*ptr);
382 else
383 fprintf (stderr, "<nil>\n");
387 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
388 description for T. */
390 static void
391 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
393 if (t)
395 fprintf (file, "%s: ", desc);
396 print_node_brief (file, "", t, 0);
401 /* Dump parser context C to FILE. */
403 static void
404 cp_debug_print_context (FILE *file, cp_parser_context *c)
406 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
407 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
408 print_node_brief (file, "", c->object_type, 0);
409 fprintf (file, "}\n");
413 /* Print the stack of parsing contexts to FILE starting with FIRST. */
415 static void
416 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
418 unsigned i;
419 cp_parser_context *c;
421 fprintf (file, "Parsing context stack:\n");
422 for (i = 0, c = first; c; c = c->next, i++)
424 fprintf (file, "\t#%u: ", i);
425 cp_debug_print_context (file, c);
430 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
432 static void
433 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
435 if (flag)
436 fprintf (file, "%s: true\n", desc);
440 /* Print an unparsed function entry UF to FILE. */
442 static void
443 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
445 unsigned i;
446 cp_default_arg_entry *default_arg_fn;
447 tree fn;
449 fprintf (file, "\tFunctions with default args:\n");
450 for (i = 0;
451 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
452 i++)
454 fprintf (file, "\t\tClass type: ");
455 print_node_brief (file, "", default_arg_fn->class_type, 0);
456 fprintf (file, "\t\tDeclaration: ");
457 print_node_brief (file, "", default_arg_fn->decl, 0);
458 fprintf (file, "\n");
461 fprintf (file, "\n\tFunctions with definitions that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
468 fprintf (file, "\n");
470 fprintf (file, "\n\tNon-static data members with initializers that require "
471 "post-processing\n\t\t");
472 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
474 print_node_brief (file, "", fn, 0);
475 fprintf (file, " ");
477 fprintf (file, "\n");
481 /* Print the stack of unparsed member functions S to FILE. */
483 static void
484 cp_debug_print_unparsed_queues (FILE *file,
485 vec<cp_unparsed_functions_entry, va_gc> *s)
487 unsigned i;
488 cp_unparsed_functions_entry *uf;
490 fprintf (file, "Unparsed functions\n");
491 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
493 fprintf (file, "#%u:\n", i);
494 cp_debug_print_unparsed_function (file, uf);
499 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
500 the given PARSER. If FILE is NULL, the output is printed on stderr. */
502 static void
503 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
505 cp_token *next_token, *first_token, *start_token;
507 if (file == NULL)
508 file = stderr;
510 next_token = parser->lexer->next_token;
511 first_token = parser->lexer->buffer->address ();
512 start_token = (next_token > first_token + window_size / 2)
513 ? next_token - window_size / 2
514 : first_token;
515 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
516 next_token);
520 /* Dump debugging information for the given PARSER. If FILE is NULL,
521 the output is printed on stderr. */
523 void
524 cp_debug_parser (FILE *file, cp_parser *parser)
526 const size_t window_size = 20;
527 cp_token *token;
528 expanded_location eloc;
530 if (file == NULL)
531 file = stderr;
533 fprintf (file, "Parser state\n\n");
534 fprintf (file, "Number of tokens: %u\n",
535 vec_safe_length (parser->lexer->buffer));
536 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
537 cp_debug_print_tree_if_set (file, "Object scope",
538 parser->object_scope);
539 cp_debug_print_tree_if_set (file, "Qualifying scope",
540 parser->qualifying_scope);
541 cp_debug_print_context_stack (file, parser->context);
542 cp_debug_print_flag (file, "Allow GNU extensions",
543 parser->allow_gnu_extensions_p);
544 cp_debug_print_flag (file, "'>' token is greater-than",
545 parser->greater_than_is_operator_p);
546 cp_debug_print_flag (file, "Default args allowed in current "
547 "parameter list", parser->default_arg_ok_p);
548 cp_debug_print_flag (file, "Parsing integral constant-expression",
549 parser->integral_constant_expression_p);
550 cp_debug_print_flag (file, "Allow non-constant expression in current "
551 "constant-expression",
552 parser->allow_non_integral_constant_expression_p);
553 cp_debug_print_flag (file, "Seen non-constant expression",
554 parser->non_integral_constant_expression_p);
555 cp_debug_print_flag (file, "Local names forbidden in current context",
556 (parser->local_variables_forbidden_p
557 & LOCAL_VARS_FORBIDDEN));
558 cp_debug_print_flag (file, "'this' forbidden in current context",
559 (parser->local_variables_forbidden_p
560 & THIS_FORBIDDEN));
561 cp_debug_print_flag (file, "In unbraced linkage specification",
562 parser->in_unbraced_linkage_specification_p);
563 cp_debug_print_flag (file, "In unbraced export declaration",
564 parser->in_unbraced_export_declaration_p);
565 cp_debug_print_flag (file, "Parsing a declarator",
566 parser->in_declarator_p);
567 cp_debug_print_flag (file, "In template argument list",
568 parser->in_template_argument_list_p);
569 cp_debug_print_flag (file, "Parsing an iteration statement",
570 parser->in_statement & IN_ITERATION_STMT);
571 cp_debug_print_flag (file, "Parsing a switch statement",
572 parser->in_statement & IN_SWITCH_STMT);
573 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
574 parser->in_statement & IN_OMP_BLOCK);
575 cp_debug_print_flag (file, "Parsing an OpenMP loop",
576 parser->in_statement & IN_OMP_FOR);
577 cp_debug_print_flag (file, "Parsing an if statement",
578 parser->in_statement & IN_IF_STMT);
579 cp_debug_print_flag (file, "Parsing a type-id in an expression "
580 "context", parser->in_type_id_in_expr_p);
581 cp_debug_print_flag (file, "String expressions should be translated "
582 "to execution character set",
583 parser->translate_strings_p);
584 cp_debug_print_flag (file, "Parsing function body outside of a "
585 "local class", parser->in_function_body);
586 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
587 parser->colon_corrects_to_scope_p);
588 cp_debug_print_flag (file, "Colon doesn't start a class definition",
589 parser->colon_doesnt_start_class_def_p);
590 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
591 parser->objective_c_message_context_p);
592 if (parser->type_definition_forbidden_message)
593 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
594 parser->type_definition_forbidden_message,
595 parser->type_definition_forbidden_message_arg
596 ? parser->type_definition_forbidden_message_arg : "<none>");
597 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
598 fprintf (file, "Number of class definitions in progress: %u\n",
599 parser->num_classes_being_defined);
600 fprintf (file, "Number of template parameter lists for the current "
601 "declaration: %u\n", parser->num_template_parameter_lists);
602 cp_debug_parser_tokens (file, parser, window_size);
603 token = parser->lexer->next_token;
604 fprintf (file, "Next token to parse:\n");
605 fprintf (file, "\tToken: ");
606 cp_lexer_print_token (file, token);
607 eloc = expand_location (token->location);
608 fprintf (file, "\n\tFile: %s\n", eloc.file);
609 fprintf (file, "\tLine: %d\n", eloc.line);
610 fprintf (file, "\tColumn: %d\n", eloc.column);
613 DEBUG_FUNCTION void
614 debug (cp_parser &ref)
616 cp_debug_parser (stderr, &ref);
619 DEBUG_FUNCTION void
620 debug (cp_parser *ptr)
622 if (ptr)
623 debug (*ptr);
624 else
625 fprintf (stderr, "<nil>\n");
628 /* Allocate memory for a new lexer object and return it. */
630 static cp_lexer *
631 cp_lexer_alloc (void)
633 /* Allocate the memory. */
634 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
636 /* Initially we are not debugging. */
637 lexer->debugging_p = false;
639 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
641 /* Create the buffer. */
642 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
644 return lexer;
647 /* Return TRUE if token is the start of a module declaration that will be
648 terminated by a CPP_PRAGMA_EOL token. */
649 static inline bool
650 cp_token_is_module_directive (cp_token *token)
652 return token->keyword == RID__EXPORT
653 || token->keyword == RID__MODULE
654 || token->keyword == RID__IMPORT;
657 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
658 PRAGMA_NONE. */
660 static enum pragma_kind
661 cp_parser_pragma_kind (cp_token *token)
663 if (token->type != CPP_PRAGMA)
664 return PRAGMA_NONE;
665 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
666 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
669 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
670 during preprocessing for the case of preprocessing-related diagnostics. This
671 is called immediately after pushing the CPP_PRAGMA_EOL token onto
672 lexer->buffer. */
674 static void
675 cp_lexer_handle_early_pragma (cp_lexer *lexer)
677 const auto first_token = lexer->buffer->address ();
678 const auto last_token = first_token + lexer->buffer->length () - 1;
680 /* Back up to the start of the pragma so pragma_lex () can parse it when
681 c-pragma lib asks it to. */
682 auto begin = last_token;
683 gcc_assert (begin->type == CPP_PRAGMA_EOL);
684 while (begin->type != CPP_PRAGMA)
686 if (cp_token_is_module_directive (begin))
687 return;
688 gcc_assert (begin != first_token);
689 --begin;
691 gcc_assert (!lexer->next_token);
692 gcc_assert (!lexer->last_token);
693 lexer->next_token = begin;
694 lexer->last_token = last_token;
696 /* Dispatch it. */
697 const unsigned int id
698 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
699 if (id >= PRAGMA_FIRST_EXTERNAL)
700 c_invoke_early_pragma_handler (id);
702 /* Reset to normal state. */
703 lexer->next_token = lexer->last_token = nullptr;
706 /* The parser. */
707 static cp_parser *cp_parser_new (cp_lexer *);
708 static GTY (()) cp_parser *the_parser;
710 /* Create a new main C++ lexer, the lexer that gets tokens from the
711 preprocessor, and also create the main parser. */
713 static cp_lexer *
714 cp_lexer_new_main (void)
716 cp_token token;
718 /* It's possible that parsing the first pragma will load a PCH file,
719 which is a GC collection point. So we have to do that before
720 allocating any memory. */
721 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
722 cp_parser_initial_pragma (&token);
723 c_common_no_more_pch ();
725 cp_lexer *lexer = cp_lexer_alloc ();
726 /* Put the first token in the buffer. */
727 cp_token *tok = lexer->buffer->quick_push (token);
729 uintptr_t filter = 0;
730 if (modules_p ())
731 filter = module_token_cdtor (parse_in, filter);
733 /* Create the parser now, so we can use it to handle early pragmas. */
734 gcc_assert (!the_parser);
735 the_parser = cp_parser_new (lexer);
737 /* Get the remaining tokens from the preprocessor. */
738 while (tok->type != CPP_EOF)
740 if (filter)
741 /* Process the previous token. */
742 module_token_lang (tok->type, tok->keyword, tok->u.value,
743 tok->location, filter);
745 /* Check for early pragmas that need to be handled now. */
746 if (tok->type == CPP_PRAGMA_EOL)
747 cp_lexer_handle_early_pragma (lexer);
749 tok = vec_safe_push (lexer->buffer, cp_token ());
750 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
753 lexer->next_token = lexer->buffer->address ();
754 lexer->last_token = lexer->next_token
755 + lexer->buffer->length ()
756 - 1;
758 if (lexer->buffer->length () != 1)
760 /* Set the EOF token's location to be the just after the previous
761 token's range. That way 'at-eof' diagnostics point at something
762 meaninful. */
763 auto range = get_range_from_loc (line_table, tok[-1].location);
764 tok[0].location
765 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
768 if (filter)
769 module_token_cdtor (parse_in, filter);
771 /* Subsequent preprocessor diagnostics should use compiler
772 diagnostic functions to get the compiler source location. */
773 override_libcpp_locations = true;
775 maybe_check_all_macros (parse_in);
777 /* If we processed any #pragma GCC target directives, we handled them early so
778 any macros they defined would be effective during preprocessing. Now, we
779 need to reset to the default state to begin compilation, and we will
780 process them again at the correct time as needed. */
781 c_reset_target_pragmas ();
783 gcc_assert (!lexer->next_token->purged_p);
784 return lexer;
787 /* Create a lexer and parser to be used during preprocess-only mode.
788 This will be filled with tokens to parse when needed by pragma_lex (). */
789 void
790 c_init_preprocess ()
792 gcc_assert (!the_parser);
793 the_parser = cp_parser_new (cp_lexer_alloc ());
796 /* Create a new lexer whose token stream is primed with the tokens in
797 CACHE. When these tokens are exhausted, no new tokens will be read. */
799 static cp_lexer *
800 cp_lexer_new_from_tokens (cp_token_cache *cache)
802 cp_token *first = cache->first;
803 cp_token *last = cache->last;
804 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
806 /* We do not own the buffer. */
807 lexer->buffer = NULL;
809 /* Insert an EOF token. */
810 lexer->saved_type = last->type;
811 lexer->saved_keyword = last->keyword;
812 last->type = CPP_EOF;
813 last->keyword = RID_MAX;
815 lexer->next_token = first;
816 lexer->last_token = last;
818 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
820 /* Initially we are not debugging. */
821 lexer->debugging_p = false;
823 gcc_assert (!lexer->next_token->purged_p
824 && !lexer->last_token->purged_p);
825 return lexer;
828 /* Frees all resources associated with LEXER. */
830 static void
831 cp_lexer_destroy (cp_lexer *lexer)
833 if (lexer->buffer)
834 vec_free (lexer->buffer);
835 else
837 /* Restore the token we overwrite with EOF. */
838 lexer->last_token->type = lexer->saved_type;
839 lexer->last_token->keyword = lexer->saved_keyword;
841 lexer->saved_tokens.release ();
842 ggc_free (lexer);
845 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
846 be used. The point of this flag is to help the compiler to fold away calls
847 to cp_lexer_debugging_p within this source file at compile time, when the
848 lexer is not being debugged. */
850 #define LEXER_DEBUGGING_ENABLED_P false
852 /* Returns nonzero if debugging information should be output. */
854 static inline bool
855 cp_lexer_debugging_p (cp_lexer *lexer)
857 if (!LEXER_DEBUGGING_ENABLED_P)
858 return false;
860 return lexer->debugging_p;
864 static inline cp_token_position
865 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
867 return lexer->next_token - previous_p;
870 static inline cp_token *
871 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
873 return pos;
876 static inline void
877 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
879 lexer->next_token = cp_lexer_token_at (lexer, pos);
882 static inline cp_token_position
883 cp_lexer_previous_token_position (cp_lexer *lexer)
885 return cp_lexer_token_position (lexer, true);
888 static inline cp_token *
889 cp_lexer_previous_token (cp_lexer *lexer)
891 cp_token_position tp = cp_lexer_previous_token_position (lexer);
893 /* Skip past purged tokens. */
894 while (tp->purged_p)
896 gcc_assert (tp != vec_safe_address (lexer->buffer));
897 tp--;
900 return cp_lexer_token_at (lexer, tp);
903 /* Same as above, but return NULL when the lexer doesn't own the token
904 buffer or if the next_token is at the start of the token
905 vector or if all previous tokens are purged. */
907 static cp_token *
908 cp_lexer_safe_previous_token (cp_lexer *lexer)
910 if (lexer->buffer
911 && lexer->next_token != lexer->buffer->address ())
913 cp_token_position tp = cp_lexer_previous_token_position (lexer);
915 /* Skip past purged tokens. */
916 while (tp->purged_p)
918 if (tp == lexer->buffer->address ())
919 return NULL;
920 tp--;
922 return cp_lexer_token_at (lexer, tp);
925 return NULL;
928 /* Overload for make_location, taking the lexer to mean the location of the
929 previous token. */
931 static inline location_t
932 make_location (location_t caret, location_t start, cp_lexer *lexer)
934 cp_token *t = cp_lexer_previous_token (lexer);
935 return make_location (caret, start, t->location);
938 /* Overload for make_location taking tokens instead of locations. */
940 static inline location_t
941 make_location (cp_token *caret, cp_token *start, cp_token *end)
943 return make_location (caret->location, start->location, end->location);
946 /* nonzero if we are presently saving tokens. */
948 static inline int
949 cp_lexer_saving_tokens (const cp_lexer* lexer)
951 return lexer->saved_tokens.length () != 0;
954 /* Store the next token from the preprocessor in *TOKEN. Return true
955 if we reach EOF. If LEXER is NULL, assume we are handling an
956 initial #pragma pch_preprocess, and thus want the lexer to return
957 processed strings.
959 Diagnostics issued from this function must have their controlling option (if
960 any) in c.opt annotated as a libcpp option via the CppReason property. */
962 static void
963 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
965 static int is_extern_c = 0;
967 /* Get a new token from the preprocessor. */
968 token->type
969 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
970 flags);
971 token->keyword = RID_MAX;
972 token->purged_p = false;
973 token->error_reported = false;
974 token->tree_check_p = false;
975 /* Usually never see a zero, but just in case ... */
976 token->main_source_p = line_table->depth <= 1;
978 /* On some systems, some header files are surrounded by an
979 implicit extern "C" block. Set a flag in the token if it
980 comes from such a header. */
981 is_extern_c += pending_lang_change;
982 pending_lang_change = 0;
983 token->implicit_extern_c = is_extern_c > 0;
985 /* Check to see if this token is a keyword. */
986 if (token->type == CPP_NAME)
988 if (IDENTIFIER_KEYWORD_P (token->u.value))
990 /* Mark this token as a keyword. */
991 token->type = CPP_KEYWORD;
992 /* Record which keyword. */
993 token->keyword = C_RID_CODE (token->u.value);
995 else
997 if (warn_cxx11_compat
998 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
999 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
1000 /* These are outside the CXX11 range. */
1001 || C_RID_CODE (token->u.value) == RID_ALIGNOF
1002 || C_RID_CODE (token->u.value) == RID_ALIGNAS
1003 || C_RID_CODE (token->u.value)== RID_THREAD))
1005 /* Warn about the C++11 keyword (but still treat it as
1006 an identifier). */
1007 warning_at (token->location, OPT_Wc__11_compat,
1008 "identifier %qE is a keyword in C++11",
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);
1015 if (warn_cxx20_compat
1016 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1017 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1019 /* Warn about the C++20 keyword (but still treat it as
1020 an identifier). */
1021 warning_at (token->location, OPT_Wc__20_compat,
1022 "identifier %qE is a keyword in C++20",
1023 token->u.value);
1025 /* Clear out the C_RID_CODE so we don't warn about this
1026 particular identifier-turned-keyword again. */
1027 C_SET_RID_CODE (token->u.value, RID_MAX);
1030 token->keyword = RID_MAX;
1033 else if (token->type == CPP_AT_NAME)
1035 /* This only happens in Objective-C++; it must be a keyword. */
1036 token->type = CPP_KEYWORD;
1037 switch (C_RID_CODE (token->u.value))
1039 /* Replace 'class' with '@class', 'private' with '@private',
1040 etc. This prevents confusion with the C++ keyword
1041 'class', and makes the tokens consistent with other
1042 Objective-C 'AT' keywords. For example '@class' is
1043 reported as RID_AT_CLASS which is consistent with
1044 '@synchronized', which is reported as
1045 RID_AT_SYNCHRONIZED.
1047 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1048 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1049 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1050 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1051 case RID_THROW: token->keyword = RID_AT_THROW; break;
1052 case RID_TRY: token->keyword = RID_AT_TRY; break;
1053 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1054 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1055 default: token->keyword = C_RID_CODE (token->u.value);
1060 /* Update the globals input_location and the input file stack from TOKEN. */
1061 static inline void
1062 cp_lexer_set_source_position_from_token (cp_token *token)
1064 input_location = token->location;
1067 /* Update the globals input_location and the input file stack from LEXER. */
1068 static inline void
1069 cp_lexer_set_source_position (cp_lexer *lexer)
1071 cp_token *token = cp_lexer_peek_token (lexer);
1072 cp_lexer_set_source_position_from_token (token);
1075 /* Return a pointer to the next token in the token stream, but do not
1076 consume it. */
1078 static inline cp_token *
1079 cp_lexer_peek_token (cp_lexer *lexer)
1081 if (cp_lexer_debugging_p (lexer))
1083 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1084 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1085 putc ('\n', cp_lexer_debug_stream);
1087 return lexer->next_token;
1090 /* Return true if the next token has the indicated TYPE. */
1092 static inline bool
1093 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1095 return cp_lexer_peek_token (lexer)->type == type;
1098 /* Return true if the next token does not have the indicated TYPE. */
1100 static inline bool
1101 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1103 return !cp_lexer_next_token_is (lexer, type);
1106 /* Return true if the next token is the indicated KEYWORD. */
1108 static inline bool
1109 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1111 return cp_lexer_peek_token (lexer)->keyword == keyword;
1114 static inline bool
1115 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1117 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1120 static inline bool
1121 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1123 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1126 /* Return true if KEYWORD can start a decl-specifier. */
1128 bool
1129 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1131 switch (keyword)
1133 /* auto specifier: storage-class-specifier in C++,
1134 simple-type-specifier in C++0x. */
1135 case RID_AUTO:
1136 /* Storage classes. */
1137 case RID_REGISTER:
1138 case RID_STATIC:
1139 case RID_EXTERN:
1140 case RID_MUTABLE:
1141 case RID_THREAD:
1142 /* Elaborated type specifiers. */
1143 case RID_ENUM:
1144 case RID_CLASS:
1145 case RID_STRUCT:
1146 case RID_UNION:
1147 case RID_TYPENAME:
1148 /* Simple type specifiers. */
1149 case RID_CHAR:
1150 case RID_CHAR8:
1151 case RID_CHAR16:
1152 case RID_CHAR32:
1153 case RID_WCHAR:
1154 case RID_BOOL:
1155 case RID_SHORT:
1156 case RID_INT:
1157 case RID_LONG:
1158 case RID_SIGNED:
1159 case RID_UNSIGNED:
1160 case RID_FLOAT:
1161 case RID_DOUBLE:
1162 CASE_RID_FLOATN_NX:
1163 case RID_VOID:
1164 /* CV qualifiers. */
1165 case RID_CONST:
1166 case RID_VOLATILE:
1167 /* Function specifiers. */
1168 case RID_EXPLICIT:
1169 case RID_VIRTUAL:
1170 /* friend/typdef/inline specifiers. */
1171 case RID_FRIEND:
1172 case RID_TYPEDEF:
1173 case RID_INLINE:
1174 /* GNU extensions. */
1175 case RID_TYPEOF:
1176 /* C++11 extensions. */
1177 case RID_DECLTYPE:
1178 case RID_CONSTEXPR:
1179 /* C++20 extensions. */
1180 case RID_CONSTINIT:
1181 case RID_CONSTEVAL:
1182 return true;
1184 default:
1185 if (keyword >= RID_FIRST_INT_N
1186 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1187 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1188 return true;
1189 return false;
1193 /* Peeks the corresponding built-in trait if the first token is
1194 a built-in trait and the second token is either `(' or `<' depending
1195 on the trait. Otherwise, returns nullptr. */
1197 static const cp_trait *
1198 cp_lexer_peek_trait (cp_lexer *lexer)
1200 const cp_token *token1 = cp_lexer_peek_token (lexer);
1201 if (token1->type == CPP_NAME && IDENTIFIER_TRAIT_P (token1->u.value))
1203 const cp_trait &trait = cp_traits[IDENTIFIER_CP_INDEX (token1->u.value)];
1204 const bool is_pack_element = (trait.kind == CPTK_TYPE_PACK_ELEMENT);
1206 /* Check if the subsequent token is a `<' token to
1207 __type_pack_element or is a `(' token to everything else. */
1208 const cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
1209 if (is_pack_element && token2->type != CPP_LESS)
1210 return nullptr;
1211 if (!is_pack_element && token2->type != CPP_OPEN_PAREN)
1212 return nullptr;
1214 return &trait;
1216 return nullptr;
1219 /* Similarly, but only if the token is an expression-yielding
1220 built-in trait. */
1222 static const cp_trait *
1223 cp_lexer_peek_trait_expr (cp_lexer *lexer)
1225 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1226 if (trait && !trait->type)
1227 return trait;
1229 return nullptr;
1232 /* Similarly, but only if the token is a type-yielding
1233 built-in trait. */
1235 static const cp_trait *
1236 cp_lexer_peek_trait_type (cp_lexer *lexer)
1238 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1239 if (trait && trait->type)
1240 return trait;
1242 return nullptr;
1245 /* Return true if the next token is a keyword for a decl-specifier. */
1247 static bool
1248 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1250 cp_token *token;
1252 if (cp_lexer_peek_trait_type (lexer))
1253 return true;
1255 token = cp_lexer_peek_token (lexer);
1256 return cp_keyword_starts_decl_specifier_p (token->keyword);
1259 /* Returns TRUE iff the token T begins a decltype type. */
1261 static bool
1262 token_is_decltype (cp_token *t)
1264 return (t->keyword == RID_DECLTYPE
1265 || t->type == CPP_DECLTYPE);
1268 /* Returns TRUE iff the next token begins a decltype type. */
1270 static bool
1271 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1273 cp_token *t = cp_lexer_peek_token (lexer);
1274 return token_is_decltype (t);
1277 /* Called when processing a token with tree_check_value; perform or defer the
1278 associated checks and return the value. */
1280 static tree
1281 saved_checks_value (struct tree_check *check_value)
1283 /* Perform any access checks that were deferred. */
1284 vec<deferred_access_check, va_gc> *checks;
1285 deferred_access_check *chk;
1286 checks = check_value->checks;
1287 if (checks)
1289 int i;
1290 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1291 perform_or_defer_access_check (chk->binfo,
1292 chk->decl,
1293 chk->diag_decl, tf_warning_or_error);
1295 /* Return the stored value. */
1296 return check_value->value;
1299 /* Return a pointer to the Nth token in the token stream. If N is 1,
1300 then this is precisely equivalent to cp_lexer_peek_token (except
1301 that it is not inline). One would like to disallow that case, but
1302 there is one case (cp_parser_nth_token_starts_template_id) where
1303 the caller passes a variable for N and it might be 1. */
1305 static cp_token *
1306 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1308 cp_token *token;
1310 /* N is 1-based, not zero-based. */
1311 gcc_assert (n > 0);
1313 if (cp_lexer_debugging_p (lexer))
1314 fprintf (cp_lexer_debug_stream,
1315 "cp_lexer: peeking ahead " HOST_SIZE_T_PRINT_DEC " at token: ",
1316 (fmt_size_t) n);
1318 --n;
1319 token = lexer->next_token;
1320 while (n && token->type != CPP_EOF)
1322 ++token;
1323 if (!token->purged_p)
1324 --n;
1327 if (cp_lexer_debugging_p (lexer))
1329 cp_lexer_print_token (cp_lexer_debug_stream, token);
1330 putc ('\n', cp_lexer_debug_stream);
1333 return token;
1336 /* Return the next token, and advance the lexer's next_token pointer
1337 to point to the next non-purged token. */
1339 static cp_token *
1340 cp_lexer_consume_token (cp_lexer* lexer)
1342 cp_token *token = lexer->next_token;
1346 gcc_assert (token->type != CPP_EOF);
1347 lexer->next_token++;
1349 while (lexer->next_token->purged_p);
1351 cp_lexer_set_source_position_from_token (token);
1353 /* Provide debugging output. */
1354 if (cp_lexer_debugging_p (lexer))
1356 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1357 cp_lexer_print_token (cp_lexer_debug_stream, token);
1358 putc ('\n', cp_lexer_debug_stream);
1361 return token;
1364 /* Permanently remove the next token from the token stream, and
1365 advance the next_token pointer to refer to the next non-purged
1366 token. */
1368 static void
1369 cp_lexer_purge_token (cp_lexer *lexer)
1371 cp_token *tok = lexer->next_token;
1373 gcc_assert (tok->type != CPP_EOF);
1374 tok->purged_p = true;
1375 tok->location = UNKNOWN_LOCATION;
1376 tok->u.value = NULL_TREE;
1377 tok->keyword = RID_MAX;
1380 tok++;
1381 while (tok->purged_p);
1382 lexer->next_token = tok;
1385 /* Permanently remove all tokens after TOK, up to, but not
1386 including, the token that will be returned next by
1387 cp_lexer_peek_token. */
1389 static void
1390 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1392 cp_token *peek = lexer->next_token;
1394 gcc_assert (tok < peek);
1396 for (tok++; tok != peek; tok++)
1398 tok->purged_p = true;
1399 tok->location = UNKNOWN_LOCATION;
1400 tok->u.value = NULL_TREE;
1401 tok->keyword = RID_MAX;
1405 /* Begin saving tokens. All tokens consumed after this point will be
1406 preserved. */
1408 static void
1409 cp_lexer_save_tokens (cp_lexer* lexer)
1411 /* Provide debugging output. */
1412 if (cp_lexer_debugging_p (lexer))
1413 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1415 lexer->saved_tokens.safe_push (lexer->next_token);
1418 /* Commit to the portion of the token stream most recently saved. */
1420 static void
1421 cp_lexer_commit_tokens (cp_lexer* lexer)
1423 /* Provide debugging output. */
1424 if (cp_lexer_debugging_p (lexer))
1425 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1427 lexer->saved_tokens.pop ();
1430 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1431 to the token stream. Stop saving tokens. */
1433 static void
1434 cp_lexer_rollback_tokens (cp_lexer* lexer)
1436 /* Provide debugging output. */
1437 if (cp_lexer_debugging_p (lexer))
1438 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1440 lexer->next_token = lexer->saved_tokens.pop ();
1443 /* Determines what saved_token_sentinel does when going out of scope. */
1445 enum saved_token_sentinel_mode {
1446 STS_COMMIT,
1447 STS_ROLLBACK,
1448 STS_DONOTHING
1451 /* RAII wrapper around the above functions, with sanity checking (the token
1452 stream should be the same at the point of instantiation as it is at the
1453 point of destruction).
1455 Creating a variable saves tokens. MODE determines what happens when the
1456 object is destroyed. STS_COMMIT commits tokens (default),
1457 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1458 rollback() will immediately roll-back tokens and set MODE to
1459 STS_DONOTHING. */
1461 struct saved_token_sentinel
1463 cp_lexer *lexer;
1464 unsigned len;
1465 saved_token_sentinel_mode mode;
1466 saved_token_sentinel (cp_lexer *_lexer,
1467 saved_token_sentinel_mode _mode = STS_COMMIT)
1468 : lexer (_lexer), mode (_mode)
1470 len = lexer->saved_tokens.length ();
1471 cp_lexer_save_tokens (lexer);
1473 void rollback ()
1475 cp_lexer_rollback_tokens (lexer);
1476 cp_lexer_set_source_position_from_token
1477 (cp_lexer_previous_token (lexer));
1478 mode = STS_DONOTHING;
1480 ~saved_token_sentinel ()
1482 if (mode == STS_COMMIT)
1483 cp_lexer_commit_tokens (lexer);
1484 else if (mode == STS_ROLLBACK)
1485 rollback ();
1487 gcc_assert (lexer->saved_tokens.length () == len);
1491 /* Print a representation of the TOKEN on the STREAM. */
1493 static void
1494 cp_lexer_print_token (FILE * stream, cp_token *token)
1496 /* We don't use cpp_type2name here because the parser defines
1497 a few tokens of its own. */
1498 static const char *const token_names[] = {
1499 /* cpplib-defined token types */
1500 #define OP(e, s) #e,
1501 #define TK(e, s) #e,
1502 TTYPE_TABLE
1503 #undef OP
1504 #undef TK
1505 /* C++ parser token types - see "Manifest constants", above. */
1506 "KEYWORD",
1507 "TEMPLATE_ID",
1508 "NESTED_NAME_SPECIFIER",
1511 /* For some tokens, print the associated data. */
1512 switch (token->type)
1514 case CPP_KEYWORD:
1515 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1516 For example, `struct' is mapped to an INTEGER_CST. */
1517 if (!identifier_p (token->u.value))
1518 break;
1519 /* fall through */
1520 case CPP_NAME:
1521 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1522 break;
1524 case CPP_STRING:
1525 case CPP_STRING16:
1526 case CPP_STRING32:
1527 case CPP_WSTRING:
1528 case CPP_UTF8STRING:
1529 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1530 break;
1532 case CPP_NUMBER:
1533 print_generic_expr (stream, token->u.value);
1534 break;
1536 default:
1537 /* If we have a name for the token, print it out. Otherwise, we
1538 simply give the numeric code. */
1539 if (token->type < ARRAY_SIZE(token_names))
1540 fputs (token_names[token->type], stream);
1541 else
1542 fprintf (stream, "[%d]", token->type);
1543 break;
1547 DEBUG_FUNCTION void
1548 debug (cp_token &ref)
1550 cp_lexer_print_token (stderr, &ref);
1551 fprintf (stderr, "\n");
1554 DEBUG_FUNCTION void
1555 debug (cp_token *ptr)
1557 if (ptr)
1558 debug (*ptr);
1559 else
1560 fprintf (stderr, "<nil>\n");
1564 /* Start emitting debugging information. */
1566 static void
1567 cp_lexer_start_debugging (cp_lexer* lexer)
1569 if (!LEXER_DEBUGGING_ENABLED_P)
1570 fatal_error (input_location,
1571 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1573 lexer->debugging_p = true;
1574 cp_lexer_debug_stream = stderr;
1577 /* Stop emitting debugging information. */
1579 static void
1580 cp_lexer_stop_debugging (cp_lexer* lexer)
1582 if (!LEXER_DEBUGGING_ENABLED_P)
1583 fatal_error (input_location,
1584 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1586 lexer->debugging_p = false;
1587 cp_lexer_debug_stream = NULL;
1590 /* Create a new cp_token_cache, representing a range of tokens. */
1592 static cp_token_cache *
1593 cp_token_cache_new (cp_token *first, cp_token *last)
1595 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1596 cache->first = first;
1597 cache->last = last;
1598 return cache;
1601 /* Diagnose if #pragma omp declare simd isn't followed immediately
1602 by function declaration or definition. */
1604 static inline void
1605 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1607 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1609 error ("%<#pragma omp declare %s%> not immediately followed by "
1610 "function declaration or definition",
1611 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1612 parser->omp_declare_simd = NULL;
1616 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1617 and put that into "omp declare simd" attribute. */
1619 static inline void
1620 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1622 if (UNLIKELY (parser->omp_declare_simd != NULL))
1624 if (fndecl == error_mark_node)
1626 parser->omp_declare_simd = NULL;
1627 return;
1629 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1631 cp_ensure_no_omp_declare_simd (parser);
1632 return;
1637 /* Similarly, but for use in declaration parsing functions
1638 which call cp_parser_handle_directive_omp_attributes. */
1640 static inline void
1641 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1643 if (parser->omp_declare_simd != data)
1644 return;
1646 if (!parser->omp_declare_simd->error_seen
1647 && !parser->omp_declare_simd->fndecl_seen)
1648 error_at (parser->omp_declare_simd->loc,
1649 "%<declare %s%> directive not immediately followed by "
1650 "function declaration or definition",
1651 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1652 parser->omp_declare_simd = NULL;
1655 /* Diagnose if #pragma acc routine isn't followed immediately by function
1656 declaration or definition. */
1658 static inline void
1659 cp_ensure_no_oacc_routine (cp_parser *parser)
1661 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1663 error_at (parser->oacc_routine->loc,
1664 "%<#pragma acc routine%> not immediately followed by "
1665 "function declaration or definition");
1666 parser->oacc_routine = NULL;
1670 /* Decl-specifiers. */
1672 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1674 static void
1675 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1677 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1680 /* Declarators. */
1682 /* Nothing other than the parser should be creating declarators;
1683 declarators are a semi-syntactic representation of C++ entities.
1684 Other parts of the front end that need to create entities (like
1685 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1687 static cp_declarator *make_call_declarator
1688 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1689 tree, tree, tree, tree, tree, location_t);
1690 static cp_declarator *make_array_declarator
1691 (cp_declarator *, tree);
1692 static cp_declarator *make_pointer_declarator
1693 (cp_cv_quals, cp_declarator *, tree);
1694 static cp_declarator *make_reference_declarator
1695 (cp_cv_quals, cp_declarator *, bool, tree);
1696 static cp_declarator *make_ptrmem_declarator
1697 (cp_cv_quals, tree, cp_declarator *, tree);
1699 /* An erroneous declarator. */
1700 static cp_declarator *cp_error_declarator;
1702 /* The obstack on which declarators and related data structures are
1703 allocated. */
1704 static struct obstack declarator_obstack;
1706 /* Alloc BYTES from the declarator memory pool. */
1708 static inline void *
1709 alloc_declarator (size_t bytes)
1711 return obstack_alloc (&declarator_obstack, bytes);
1714 /* Allocate a declarator of the indicated KIND. Clear fields that are
1715 common to all declarators. */
1717 static cp_declarator *
1718 make_declarator (cp_declarator_kind kind)
1720 cp_declarator *declarator;
1722 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1723 declarator->kind = kind;
1724 declarator->parenthesized = UNKNOWN_LOCATION;
1725 declarator->attributes = NULL_TREE;
1726 declarator->std_attributes = NULL_TREE;
1727 declarator->declarator = NULL;
1728 declarator->parameter_pack_p = false;
1729 declarator->id_loc = UNKNOWN_LOCATION;
1730 declarator->init_loc = UNKNOWN_LOCATION;
1732 return declarator;
1735 /* Make a declarator for a generalized identifier. If
1736 QUALIFYING_SCOPE is non-NULL, the identifier is
1737 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1738 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1739 is, if any. */
1741 static cp_declarator *
1742 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1743 special_function_kind sfk, location_t id_location)
1745 cp_declarator *declarator;
1747 /* It is valid to write:
1749 class C { void f(); };
1750 typedef C D;
1751 void D::f();
1753 The standard is not clear about whether `typedef const C D' is
1754 legal; as of 2002-09-15 the committee is considering that
1755 question. EDG 3.0 allows that syntax. Therefore, we do as
1756 well. */
1757 if (qualifying_scope && TYPE_P (qualifying_scope))
1758 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1760 gcc_assert (identifier_p (unqualified_name)
1761 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1762 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1764 declarator = make_declarator (cdk_id);
1765 declarator->u.id.qualifying_scope = qualifying_scope;
1766 declarator->u.id.unqualified_name = unqualified_name;
1767 declarator->u.id.sfk = sfk;
1768 declarator->id_loc = id_location;
1770 return declarator;
1773 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1774 of modifiers such as const or volatile to apply to the pointer
1775 type, represented as identifiers. ATTRIBUTES represent the attributes that
1776 appertain to the pointer or reference. */
1778 cp_declarator *
1779 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1780 tree attributes)
1782 cp_declarator *declarator;
1784 declarator = make_declarator (cdk_pointer);
1785 declarator->declarator = target;
1786 declarator->u.pointer.qualifiers = cv_qualifiers;
1787 declarator->u.pointer.class_type = NULL_TREE;
1788 if (target)
1790 declarator->id_loc = target->id_loc;
1791 declarator->parameter_pack_p = target->parameter_pack_p;
1792 target->parameter_pack_p = false;
1794 else
1795 declarator->parameter_pack_p = false;
1797 declarator->std_attributes = attributes;
1799 return declarator;
1802 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1803 represent the attributes that appertain to the pointer or
1804 reference. */
1806 cp_declarator *
1807 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1808 bool rvalue_ref, tree attributes)
1810 cp_declarator *declarator;
1812 declarator = make_declarator (cdk_reference);
1813 declarator->declarator = target;
1814 declarator->u.reference.qualifiers = cv_qualifiers;
1815 declarator->u.reference.rvalue_ref = rvalue_ref;
1816 if (target)
1818 declarator->id_loc = target->id_loc;
1819 declarator->parameter_pack_p = target->parameter_pack_p;
1820 target->parameter_pack_p = false;
1822 else
1823 declarator->parameter_pack_p = false;
1825 declarator->std_attributes = attributes;
1827 return declarator;
1830 /* Like make_pointer_declarator -- but for a pointer to a non-static
1831 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1832 appertain to the pointer or reference. */
1834 cp_declarator *
1835 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1836 cp_declarator *pointee,
1837 tree attributes)
1839 cp_declarator *declarator;
1841 declarator = make_declarator (cdk_ptrmem);
1842 declarator->declarator = pointee;
1843 declarator->u.pointer.qualifiers = cv_qualifiers;
1844 declarator->u.pointer.class_type = class_type;
1846 if (pointee)
1848 declarator->parameter_pack_p = pointee->parameter_pack_p;
1849 pointee->parameter_pack_p = false;
1851 else
1852 declarator->parameter_pack_p = false;
1854 declarator->std_attributes = attributes;
1856 return declarator;
1859 /* Make a declarator for the function given by TARGET, with the
1860 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1861 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1862 indicates what exceptions can be thrown. STD_ATTRS contains
1863 attributes that appertain to the function type. */
1865 cp_declarator *
1866 make_call_declarator (cp_declarator *target,
1867 tree parms,
1868 cp_cv_quals cv_qualifiers,
1869 cp_virt_specifiers virt_specifiers,
1870 cp_ref_qualifier ref_qualifier,
1871 tree tx_qualifier,
1872 tree exception_specification,
1873 tree late_return_type,
1874 tree requires_clause,
1875 tree std_attrs,
1876 location_t parens_loc)
1878 cp_declarator *declarator;
1880 declarator = make_declarator (cdk_function);
1881 declarator->declarator = target;
1882 declarator->u.function.parameters = parms;
1883 declarator->u.function.qualifiers = cv_qualifiers;
1884 declarator->u.function.virt_specifiers = virt_specifiers;
1885 declarator->u.function.ref_qualifier = ref_qualifier;
1886 declarator->u.function.tx_qualifier = tx_qualifier;
1887 declarator->u.function.exception_specification = exception_specification;
1888 declarator->u.function.late_return_type = late_return_type;
1889 declarator->u.function.requires_clause = requires_clause;
1890 declarator->u.function.parens_loc = parens_loc;
1891 if (target)
1893 declarator->id_loc = target->id_loc;
1894 declarator->parameter_pack_p = target->parameter_pack_p;
1895 target->parameter_pack_p = false;
1897 else
1898 declarator->parameter_pack_p = false;
1900 declarator->std_attributes = std_attrs;
1902 return declarator;
1905 /* Make a declarator for an array of BOUNDS elements, each of which is
1906 defined by ELEMENT. */
1908 cp_declarator *
1909 make_array_declarator (cp_declarator *element, tree bounds)
1911 cp_declarator *declarator;
1913 declarator = make_declarator (cdk_array);
1914 declarator->declarator = element;
1915 declarator->u.array.bounds = bounds;
1916 if (element)
1918 declarator->id_loc = element->id_loc;
1919 declarator->parameter_pack_p = element->parameter_pack_p;
1920 element->parameter_pack_p = false;
1922 else
1923 declarator->parameter_pack_p = false;
1925 return declarator;
1928 /* Determine whether the declarator we've seen so far can be a
1929 parameter pack, when followed by an ellipsis. */
1930 static bool
1931 declarator_can_be_parameter_pack (cp_declarator *declarator)
1933 if (declarator && declarator->parameter_pack_p)
1934 /* We already saw an ellipsis. */
1935 return false;
1937 /* Search for a declarator name, or any other declarator that goes
1938 after the point where the ellipsis could appear in a parameter
1939 pack. If we find any of these, then this declarator cannot be
1940 made into a parameter pack. */
1941 bool found = false;
1942 while (declarator && !found)
1944 switch ((int)declarator->kind)
1946 case cdk_id:
1947 case cdk_array:
1948 case cdk_decomp:
1949 found = true;
1950 break;
1952 case cdk_error:
1953 return true;
1955 default:
1956 declarator = declarator->declarator;
1957 break;
1961 return !found;
1964 cp_parameter_declarator *no_parameters;
1966 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1967 DECLARATOR and DEFAULT_ARGUMENT. */
1969 cp_parameter_declarator *
1970 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1971 cp_declarator *declarator,
1972 tree default_argument,
1973 location_t loc,
1974 bool template_parameter_pack_p = false)
1976 cp_parameter_declarator *parameter;
1978 parameter = ((cp_parameter_declarator *)
1979 alloc_declarator (sizeof (cp_parameter_declarator)));
1980 parameter->next = NULL;
1981 if (decl_specifiers)
1982 parameter->decl_specifiers = *decl_specifiers;
1983 else
1984 clear_decl_specs (&parameter->decl_specifiers);
1985 parameter->declarator = declarator;
1986 parameter->default_argument = default_argument;
1987 parameter->template_parameter_pack_p = template_parameter_pack_p;
1988 parameter->loc = loc;
1990 return parameter;
1993 /* Returns true iff DECLARATOR is a declaration for a function. */
1995 static bool
1996 function_declarator_p (const cp_declarator *declarator)
1998 while (declarator)
2000 if (declarator->kind == cdk_function
2001 && declarator->declarator->kind == cdk_id)
2002 return true;
2003 if (declarator->kind == cdk_id
2004 || declarator->kind == cdk_decomp
2005 || declarator->kind == cdk_error)
2006 return false;
2007 declarator = declarator->declarator;
2009 return false;
2012 /* The parser. */
2014 /* Overview
2015 --------
2017 A cp_parser parses the token stream as specified by the C++
2018 grammar. Its job is purely parsing, not semantic analysis. For
2019 example, the parser breaks the token stream into declarators,
2020 expressions, statements, and other similar syntactic constructs.
2021 It does not check that the types of the expressions on either side
2022 of an assignment-statement are compatible, or that a function is
2023 not declared with a parameter of type `void'.
2025 The parser invokes routines elsewhere in the compiler to perform
2026 semantic analysis and to build up the abstract syntax tree for the
2027 code processed.
2029 The parser (and the template instantiation code, which is, in a
2030 way, a close relative of parsing) are the only parts of the
2031 compiler that should be calling push_scope and pop_scope, or
2032 related functions. The parser (and template instantiation code)
2033 keeps track of what scope is presently active; everything else
2034 should simply honor that. (The code that generates static
2035 initializers may also need to set the scope, in order to check
2036 access control correctly when emitting the initializers.)
2038 Methodology
2039 -----------
2041 The parser is of the standard recursive-descent variety. Upcoming
2042 tokens in the token stream are examined in order to determine which
2043 production to use when parsing a non-terminal. Some C++ constructs
2044 require arbitrary look ahead to disambiguate. For example, it is
2045 impossible, in the general case, to tell whether a statement is an
2046 expression or declaration without scanning the entire statement.
2047 Therefore, the parser is capable of "parsing tentatively." When the
2048 parser is not sure what construct comes next, it enters this mode.
2049 Then, while we attempt to parse the construct, the parser queues up
2050 error messages, rather than issuing them immediately, and saves the
2051 tokens it consumes. If the construct is parsed successfully, the
2052 parser "commits", i.e., it issues any queued error messages and
2053 the tokens that were being preserved are permanently discarded.
2054 If, however, the construct is not parsed successfully, the parser
2055 rolls back its state completely so that it can resume parsing using
2056 a different alternative.
2058 Future Improvements
2059 -------------------
2061 The performance of the parser could probably be improved substantially.
2062 We could often eliminate the need to parse tentatively by looking ahead
2063 a little bit. In some places, this approach might not entirely eliminate
2064 the need to parse tentatively, but it might still speed up the average
2065 case. */
2067 /* Flags that are passed to some parsing functions. These values can
2068 be bitwise-ored together. */
2070 enum
2072 /* No flags. */
2073 CP_PARSER_FLAGS_NONE = 0x0,
2074 /* The construct is optional. If it is not present, then no error
2075 should be issued. */
2076 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2077 /* When parsing a type-specifier, treat user-defined type-names
2078 as non-type identifiers. */
2079 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2080 /* When parsing a type-specifier, do not try to parse a class-specifier
2081 or enum-specifier. */
2082 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2083 /* When parsing a decl-specifier-seq, only allow type-specifier or
2084 constexpr. */
2085 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2086 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2087 for C++20 consteval or for C++23 static. */
2088 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2089 /* When parsing a decl-specifier-seq, allow missing typename. */
2090 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2091 /* When parsing of the noexcept-specifier should be delayed. */
2092 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2093 /* When parsing a consteval declarator. */
2094 CP_PARSER_FLAGS_CONSTEVAL = 0x80,
2095 /* When parsing a parameter declaration. */
2096 CP_PARSER_FLAGS_PARAMETER = 0x100
2099 /* This type is used for parameters and variables which hold
2100 combinations of the above flags. */
2101 typedef int cp_parser_flags;
2103 /* The different kinds of declarators we want to parse. */
2105 enum cp_parser_declarator_kind
2107 /* We want an abstract declarator. */
2108 CP_PARSER_DECLARATOR_ABSTRACT,
2109 /* We want a named declarator. */
2110 CP_PARSER_DECLARATOR_NAMED,
2111 /* We don't mind, but the name must be an unqualified-id. */
2112 CP_PARSER_DECLARATOR_EITHER
2115 /* The precedence values used to parse binary expressions. The minimum value
2116 of PREC must be 1, because zero is reserved to quickly discriminate
2117 binary operators from other tokens. */
2119 enum cp_parser_prec
2121 PREC_NOT_OPERATOR,
2122 PREC_LOGICAL_OR_EXPRESSION,
2123 PREC_LOGICAL_AND_EXPRESSION,
2124 PREC_INCLUSIVE_OR_EXPRESSION,
2125 PREC_EXCLUSIVE_OR_EXPRESSION,
2126 PREC_AND_EXPRESSION,
2127 PREC_EQUALITY_EXPRESSION,
2128 PREC_RELATIONAL_EXPRESSION,
2129 PREC_SPACESHIP_EXPRESSION,
2130 PREC_SHIFT_EXPRESSION,
2131 PREC_ADDITIVE_EXPRESSION,
2132 PREC_MULTIPLICATIVE_EXPRESSION,
2133 PREC_PM_EXPRESSION,
2134 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2137 /* A mapping from a token type to a corresponding tree node type, with a
2138 precedence value. */
2140 struct cp_parser_binary_operations_map_node
2142 /* The token type. */
2143 enum cpp_ttype token_type;
2144 /* The corresponding tree code. */
2145 enum tree_code tree_type;
2146 /* The precedence of this operator. */
2147 enum cp_parser_prec prec;
2150 struct cp_parser_expression_stack_entry
2152 /* Left hand side of the binary operation we are currently
2153 parsing. */
2154 cp_expr lhs;
2155 /* Original tree code for left hand side, if it was a binary
2156 expression itself (used for -Wparentheses). */
2157 enum tree_code lhs_type;
2158 /* Tree code for the binary operation we are parsing. */
2159 enum tree_code tree_type;
2160 /* Precedence of the binary operation we are parsing. */
2161 enum cp_parser_prec prec;
2162 /* Location of the binary operation we are parsing. */
2163 location_t loc;
2164 /* Flags from the operator token. */
2165 unsigned char flags;
2168 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2169 entries because precedence levels on the stack are monotonically
2170 increasing. */
2171 typedef struct cp_parser_expression_stack_entry
2172 cp_parser_expression_stack[NUM_PREC_VALUES];
2174 /* Used for parsing OMP for loops.
2176 Some notes on flags used for context:
2177 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2178 construct, except for the final-loop-body.
2179 The want_nested_loop flag is true if inside a {} sequence where
2180 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2181 but has not yet been seen. It's false when parsing intervening code
2182 statements or their substatements that cannot contain a loop-nest.
2183 The in_intervening_code flag is true when parsing any intervening code,
2184 including substatements, and whether or not want_nested_loop is true.
2186 And, about error handling:
2187 The saw_intervening_code flag is set if the loop is not perfectly
2188 nested, even in the usual case where this is not an error.
2189 perfect_nesting_fail is set if an error has been diagnosed because an
2190 imperfectly-nested loop was found where a perfectly-nested one is
2191 required (we diagnose this only once).
2192 fail is set if any kind of structural error in the loop nest
2193 has been found and diagnosed.
2195 struct omp_for_parse_data {
2196 enum tree_code code;
2197 tree declv, condv, incrv, initv;
2198 tree pre_body;
2199 tree orig_declv;
2200 auto_vec<tree, 4> orig_inits;
2201 int count; /* Expected nesting depth. */
2202 int depth; /* Current nesting depth. */
2203 location_t for_loc;
2204 releasing_vec init_blockv;
2205 releasing_vec body_blockv;
2206 releasing_vec init_placeholderv;
2207 releasing_vec body_placeholderv;
2208 bool ordered : 1;
2209 bool inscan : 1;
2210 bool want_nested_loop : 1;
2211 bool in_intervening_code : 1;
2212 bool saw_intervening_code : 1;
2213 bool perfect_nesting_fail : 1;
2214 bool fail : 1;
2215 tree clauses;
2216 tree *cclauses;
2217 tree ordered_cl;
2220 /* Prototypes. */
2222 /* Constructors and destructors. */
2224 static cp_parser_context *cp_parser_context_new
2225 (cp_parser_context *);
2227 /* Class variables. */
2229 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2231 /* The operator-precedence table used by cp_parser_binary_expression.
2232 Transformed into an associative array (binops_by_token) by
2233 cp_parser_new. */
2235 static const cp_parser_binary_operations_map_node binops[] = {
2236 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2237 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2239 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2240 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2241 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2243 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2244 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2246 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2247 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2249 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2251 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2252 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2253 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2254 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2256 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2257 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2259 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2261 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2263 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2265 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2267 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2270 /* The same as binops, but initialized by cp_parser_new so that
2271 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2272 for speed. */
2273 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2275 /* Constructors and destructors. */
2277 /* Construct a new context. The context below this one on the stack
2278 is given by NEXT. */
2280 static cp_parser_context *
2281 cp_parser_context_new (cp_parser_context* next)
2283 cp_parser_context *context;
2285 /* Allocate the storage. */
2286 if (cp_parser_context_free_list != NULL)
2288 /* Pull the first entry from the free list. */
2289 context = cp_parser_context_free_list;
2290 cp_parser_context_free_list = context->next;
2291 memset (context, 0, sizeof (*context));
2293 else
2294 context = ggc_cleared_alloc<cp_parser_context> ();
2296 /* No errors have occurred yet in this context. */
2297 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2298 /* If this is not the bottommost context, copy information that we
2299 need from the previous context. */
2300 if (next)
2302 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2303 expression, then we are parsing one in this context, too. */
2304 context->object_type = next->object_type;
2305 /* Thread the stack. */
2306 context->next = next;
2309 return context;
2312 /* Managing the unparsed function queues. */
2314 #define unparsed_funs_with_default_args \
2315 parser->unparsed_queues->last ().funs_with_default_args
2316 #define unparsed_funs_with_definitions \
2317 parser->unparsed_queues->last ().funs_with_definitions
2318 #define unparsed_nsdmis \
2319 parser->unparsed_queues->last ().nsdmis
2320 #define unparsed_noexcepts \
2321 parser->unparsed_queues->last ().noexcepts
2322 #define unparsed_contracts \
2323 parser->unparsed_queues->last ().contracts
2325 static void
2326 push_unparsed_function_queues (cp_parser *parser)
2328 cp_unparsed_functions_entry e
2329 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2330 vec_safe_push (parser->unparsed_queues, e);
2333 static void
2334 pop_unparsed_function_queues (cp_parser *parser)
2336 release_tree_vector (unparsed_funs_with_definitions);
2337 parser->unparsed_queues->pop ();
2340 /* Prototypes. */
2342 /* Routines to parse various constructs.
2344 Those that return `tree' will return the error_mark_node (rather
2345 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2346 Sometimes, they will return an ordinary node if error-recovery was
2347 attempted, even though a parse error occurred. So, to check
2348 whether or not a parse error occurred, you should always use
2349 cp_parser_error_occurred. If the construct is optional (indicated
2350 either by an `_opt' in the name of the function that does the
2351 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2352 the construct is not present. */
2354 /* Lexical conventions [gram.lex] */
2356 static tree finish_userdef_string_literal
2357 (tree);
2359 /* Basic concepts [gram.basic] */
2361 static void cp_parser_translation_unit (cp_parser *);
2363 /* Expressions [gram.expr] */
2365 static cp_expr cp_parser_primary_expression
2366 (cp_parser *, bool, bool, bool, cp_id_kind *);
2367 static cp_expr cp_parser_id_expression
2368 (cp_parser *, bool, bool, bool *, bool, bool);
2369 static cp_expr cp_parser_unqualified_id
2370 (cp_parser *, bool, bool, bool, bool);
2371 static tree cp_parser_nested_name_specifier_opt
2372 (cp_parser *, bool, bool, bool, bool, bool = false);
2373 static tree cp_parser_nested_name_specifier
2374 (cp_parser *, bool, bool, bool, bool);
2375 static tree cp_parser_qualifying_entity
2376 (cp_parser *, bool, bool, bool, bool, bool);
2377 static cp_expr cp_parser_postfix_expression
2378 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2379 static tree cp_parser_postfix_open_square_expression
2380 (cp_parser *, tree, bool, bool);
2381 static tree cp_parser_postfix_dot_deref_expression
2382 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2383 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2384 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2385 bool = false);
2386 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2387 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3,
2388 uneval_string_attr = 4 };
2389 static void cp_parser_pseudo_destructor_name
2390 (cp_parser *, tree, tree *, tree *);
2391 static cp_expr cp_parser_unary_expression
2392 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2393 static enum tree_code cp_parser_unary_operator
2394 (cp_token *);
2395 static tree cp_parser_has_attribute_expression
2396 (cp_parser *);
2397 static tree cp_parser_new_expression
2398 (cp_parser *);
2399 static vec<tree, va_gc> *cp_parser_new_placement
2400 (cp_parser *);
2401 static tree cp_parser_new_type_id
2402 (cp_parser *, tree *);
2403 static cp_declarator *cp_parser_new_declarator_opt
2404 (cp_parser *);
2405 static cp_declarator *cp_parser_direct_new_declarator
2406 (cp_parser *);
2407 static vec<tree, va_gc> *cp_parser_new_initializer
2408 (cp_parser *);
2409 static tree cp_parser_delete_expression
2410 (cp_parser *);
2411 static cp_expr cp_parser_cast_expression
2412 (cp_parser *, bool, bool, bool, cp_id_kind *);
2413 static cp_expr cp_parser_binary_expression
2414 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2415 static tree cp_parser_question_colon_clause
2416 (cp_parser *, cp_expr);
2417 static cp_expr cp_parser_conditional_expression (cp_parser *);
2418 static cp_expr cp_parser_assignment_expression
2419 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2420 static enum tree_code cp_parser_assignment_operator_opt
2421 (cp_parser *);
2422 static cp_expr cp_parser_expression
2423 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2424 static cp_expr cp_parser_constant_expression
2425 (cp_parser *, int = 0, bool * = NULL, bool = false);
2426 static cp_expr cp_parser_builtin_offsetof
2427 (cp_parser *);
2428 static cp_expr cp_parser_lambda_expression
2429 (cp_parser *);
2430 static void cp_parser_lambda_introducer
2431 (cp_parser *, tree);
2432 static bool cp_parser_lambda_declarator_opt
2433 (cp_parser *, tree);
2434 static void cp_parser_lambda_body
2435 (cp_parser *, tree);
2437 /* Statements [gram.stmt.stmt] */
2439 static void cp_parser_statement
2440 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2441 static void cp_parser_label_for_labeled_statement
2442 (cp_parser *, tree);
2443 static tree cp_parser_expression_statement
2444 (cp_parser *, tree);
2445 static tree cp_parser_compound_statement
2446 (cp_parser *, tree, int, bool);
2447 static void cp_parser_statement_seq_opt
2448 (cp_parser *, tree);
2449 static tree cp_parser_selection_statement
2450 (cp_parser *, bool *, vec<tree> *);
2451 static tree cp_parser_condition
2452 (cp_parser *);
2453 static tree cp_parser_iteration_statement
2454 (cp_parser *, bool *, bool, tree, bool);
2455 static bool cp_parser_init_statement
2456 (cp_parser *, tree *decl);
2457 static tree cp_parser_for
2458 (cp_parser *, bool, tree, bool);
2459 static tree cp_parser_c_for
2460 (cp_parser *, tree, tree, bool, tree, bool);
2461 static tree cp_parser_range_for
2462 (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
2463 static void do_range_for_auto_deduction
2464 (tree, tree, cp_decomp *);
2465 static tree cp_parser_perform_range_for_lookup
2466 (tree, tree *, tree *);
2467 static tree cp_parser_range_for_member_function
2468 (tree, tree);
2469 static tree cp_parser_jump_statement
2470 (cp_parser *);
2471 static void cp_parser_declaration_statement
2472 (cp_parser *);
2474 static tree cp_parser_implicitly_scoped_statement
2475 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2476 static void cp_parser_already_scoped_statement
2477 (cp_parser *, bool *, const token_indent_info &);
2479 /* State of module-declaration parsing. */
2480 enum module_parse
2482 MP_NOT_MODULE, /* Not a module. */
2484 _MP_UNUSED,
2486 MP_FIRST, /* First declaration of TU. */
2487 MP_GLOBAL, /* Global Module Fragment. */
2489 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2490 MP_PURVIEW, /* Purview of a named module. */
2492 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2493 MP_PRIVATE, /* Private Module Fragment. */
2496 static module_parse cp_parser_module_declaration
2497 (cp_parser *parser, module_parse, bool exporting);
2498 static void cp_parser_import_declaration
2499 (cp_parser *parser, module_parse, bool exporting);
2501 /* Declarations [gram.dcl.dcl] */
2503 static void cp_parser_declaration_seq_opt
2504 (cp_parser *);
2505 static void cp_parser_declaration
2506 (cp_parser *, tree);
2507 static void cp_parser_toplevel_declaration
2508 (cp_parser *);
2509 static void cp_parser_block_declaration
2510 (cp_parser *, bool);
2511 static void cp_parser_simple_declaration
2512 (cp_parser *, bool, tree *);
2513 static void cp_parser_decl_specifier_seq
2514 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2515 static tree cp_parser_storage_class_specifier_opt
2516 (cp_parser *);
2517 static tree cp_parser_function_specifier_opt
2518 (cp_parser *, cp_decl_specifier_seq *);
2519 static tree cp_parser_type_specifier
2520 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2521 int *, bool *);
2522 static tree cp_parser_simple_type_specifier
2523 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2524 static tree cp_parser_placeholder_type_specifier
2525 (cp_parser *, location_t, tree, bool);
2526 static tree cp_parser_type_name
2527 (cp_parser *, bool);
2528 static tree cp_parser_nonclass_name
2529 (cp_parser* parser);
2530 static tree cp_parser_elaborated_type_specifier
2531 (cp_parser *, bool, bool);
2532 static tree cp_parser_enum_specifier
2533 (cp_parser *);
2534 static void cp_parser_enumerator_list
2535 (cp_parser *, tree);
2536 static void cp_parser_enumerator_definition
2537 (cp_parser *, tree);
2538 static tree cp_parser_namespace_name
2539 (cp_parser *);
2540 static void cp_parser_namespace_definition
2541 (cp_parser *);
2542 static void cp_parser_namespace_body
2543 (cp_parser *);
2544 static tree cp_parser_qualified_namespace_specifier
2545 (cp_parser *);
2546 static void cp_parser_namespace_alias_definition
2547 (cp_parser *);
2548 static bool cp_parser_using_declaration
2549 (cp_parser *, bool);
2550 static void cp_parser_using_directive
2551 (cp_parser *);
2552 static void cp_parser_using_enum
2553 (cp_parser *);
2554 static tree cp_parser_alias_declaration
2555 (cp_parser *);
2556 static void cp_parser_asm_definition
2557 (cp_parser *);
2558 static void cp_parser_linkage_specification
2559 (cp_parser *, tree);
2560 static void cp_parser_static_assert
2561 (cp_parser *, bool);
2562 static tree cp_parser_decltype
2563 (cp_parser *);
2564 static tree cp_parser_decomposition_declaration
2565 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2567 /* Declarators [gram.dcl.decl] */
2569 static tree cp_parser_init_declarator
2570 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2571 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2572 location_t *, tree *);
2573 static cp_declarator *cp_parser_declarator
2574 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2575 bool, bool, bool);
2576 static cp_declarator *cp_parser_direct_declarator
2577 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2578 bool);
2579 static enum tree_code cp_parser_ptr_operator
2580 (cp_parser *, tree *, cp_cv_quals *, tree *);
2581 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2582 (cp_parser *);
2583 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2584 (cp_parser *);
2585 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2586 (cp_parser *);
2587 static tree cp_parser_tx_qualifier_opt
2588 (cp_parser *);
2589 static tree cp_parser_late_return_type_opt
2590 (cp_parser *, cp_declarator *, tree &);
2591 static tree cp_parser_declarator_id
2592 (cp_parser *, bool);
2593 static tree cp_parser_type_id
2594 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2595 static tree cp_parser_template_type_arg
2596 (cp_parser *);
2597 static tree cp_parser_trailing_type_id (cp_parser *);
2598 static tree cp_parser_type_id_1
2599 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2600 static void cp_parser_type_specifier_seq
2601 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2602 static tree cp_parser_parameter_declaration_clause
2603 (cp_parser *, cp_parser_flags);
2604 static tree cp_parser_parameter_declaration_list
2605 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2606 static cp_parameter_declarator *cp_parser_parameter_declaration
2607 (cp_parser *, cp_parser_flags, bool, bool *);
2608 static tree cp_parser_default_argument
2609 (cp_parser *, bool);
2610 static void cp_parser_function_body
2611 (cp_parser *, bool);
2612 static tree cp_parser_initializer
2613 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2614 static cp_expr cp_parser_initializer_clause
2615 (cp_parser *, bool * = nullptr);
2616 static cp_expr cp_parser_braced_list
2617 (cp_parser*, bool * = nullptr);
2618 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2619 (cp_parser *, bool *, bool *);
2621 static void cp_parser_ctor_initializer_opt_and_function_body
2622 (cp_parser *, bool);
2624 static tree cp_parser_late_parsing_omp_declare_simd
2625 (cp_parser *, tree);
2627 static tree cp_parser_late_parsing_oacc_routine
2628 (cp_parser *, tree);
2630 static tree synthesize_implicit_template_parm
2631 (cp_parser *, tree);
2632 static tree finish_fully_implicit_template
2633 (cp_parser *, tree);
2634 static void abort_fully_implicit_template
2635 (cp_parser *);
2637 /* Classes [gram.class] */
2639 static tree cp_parser_class_name
2640 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2641 static tree cp_parser_class_specifier
2642 (cp_parser *);
2643 static tree cp_parser_class_head
2644 (cp_parser *, bool *);
2645 static enum tag_types cp_parser_class_key
2646 (cp_parser *);
2647 static void cp_parser_type_parameter_key
2648 (cp_parser* parser);
2649 static void cp_parser_member_specification_opt
2650 (cp_parser *);
2651 static void cp_parser_member_declaration
2652 (cp_parser *);
2653 static tree cp_parser_pure_specifier
2654 (cp_parser *);
2655 static tree cp_parser_constant_initializer
2656 (cp_parser *);
2658 /* Derived classes [gram.class.derived] */
2660 static tree cp_parser_base_clause
2661 (cp_parser *);
2662 static tree cp_parser_base_specifier
2663 (cp_parser *);
2665 /* Special member functions [gram.special] */
2667 static tree cp_parser_conversion_function_id
2668 (cp_parser *);
2669 static tree cp_parser_conversion_type_id
2670 (cp_parser *);
2671 static cp_declarator *cp_parser_conversion_declarator_opt
2672 (cp_parser *);
2673 static void cp_parser_ctor_initializer_opt
2674 (cp_parser *);
2675 static void cp_parser_mem_initializer_list
2676 (cp_parser *);
2677 static tree cp_parser_mem_initializer
2678 (cp_parser *);
2679 static tree cp_parser_mem_initializer_id
2680 (cp_parser *);
2682 /* Overloading [gram.over] */
2684 static cp_expr cp_parser_operator_function_id
2685 (cp_parser *);
2686 static cp_expr cp_parser_operator
2687 (cp_parser *, location_t);
2689 /* Templates [gram.temp] */
2691 static void cp_parser_template_declaration
2692 (cp_parser *, bool);
2693 static tree cp_parser_template_parameter_list
2694 (cp_parser *);
2695 static tree cp_parser_template_parameter
2696 (cp_parser *, bool *, bool *);
2697 static tree cp_parser_type_parameter
2698 (cp_parser *, bool *);
2699 static tree cp_parser_template_id
2700 (cp_parser *, bool, bool, enum tag_types, bool);
2701 static tree cp_parser_template_id_expr
2702 (cp_parser *, bool, bool, bool);
2703 static tree cp_parser_template_name
2704 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2705 static tree cp_parser_template_argument_list
2706 (cp_parser *);
2707 static tree cp_parser_template_argument
2708 (cp_parser *);
2709 static void cp_parser_explicit_instantiation
2710 (cp_parser *);
2711 static void cp_parser_explicit_specialization
2712 (cp_parser *);
2714 /* Exception handling [gram.except] */
2716 static tree cp_parser_try_block
2717 (cp_parser *);
2718 static void cp_parser_function_try_block
2719 (cp_parser *);
2720 static void cp_parser_handler_seq
2721 (cp_parser *);
2722 static void cp_parser_handler
2723 (cp_parser *);
2724 static tree cp_parser_exception_declaration
2725 (cp_parser *);
2726 static tree cp_parser_throw_expression
2727 (cp_parser *);
2728 static tree cp_parser_exception_specification_opt
2729 (cp_parser *, cp_parser_flags);
2730 static tree cp_parser_type_id_list
2731 (cp_parser *);
2732 static tree cp_parser_noexcept_specification_opt
2733 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2735 /* GNU Extensions */
2737 static tree cp_parser_asm_specification_opt
2738 (cp_parser *);
2739 static tree cp_parser_asm_operand_list
2740 (cp_parser *);
2741 static tree cp_parser_asm_clobber_list
2742 (cp_parser *);
2743 static tree cp_parser_asm_label_list
2744 (cp_parser *);
2745 static bool cp_next_tokens_can_be_attribute_p
2746 (cp_parser *);
2747 static bool cp_next_tokens_can_be_gnu_attribute_p
2748 (cp_parser *);
2749 static bool cp_next_tokens_can_be_std_attribute_p
2750 (cp_parser *);
2751 static bool cp_nth_tokens_can_be_std_attribute_p
2752 (cp_parser *, size_t);
2753 static bool cp_nth_tokens_can_be_gnu_attribute_p
2754 (cp_parser *, size_t);
2755 static bool cp_nth_tokens_can_be_attribute_p
2756 (cp_parser *, size_t);
2757 static tree cp_parser_attributes_opt
2758 (cp_parser *);
2759 static tree cp_parser_gnu_attributes_opt
2760 (cp_parser *);
2761 static tree cp_parser_gnu_attribute_list
2762 (cp_parser *, bool = false);
2763 static tree cp_parser_std_attribute
2764 (cp_parser *, tree);
2765 static tree cp_parser_std_attribute_spec
2766 (cp_parser *);
2767 static tree cp_parser_std_attribute_spec_seq
2768 (cp_parser *);
2769 static size_t cp_parser_skip_std_attribute_spec_seq
2770 (cp_parser *, size_t);
2771 static size_t cp_parser_skip_attributes_opt
2772 (cp_parser *, size_t);
2773 static bool cp_parser_extension_opt
2774 (cp_parser *, int *);
2775 static void cp_parser_label_declaration
2776 (cp_parser *);
2778 /* Concept Extensions */
2780 static tree cp_parser_concept_definition
2781 (cp_parser *);
2782 static tree cp_parser_constraint_expression
2783 (cp_parser *);
2784 static tree cp_parser_requires_clause_opt
2785 (cp_parser *, bool);
2786 static tree cp_parser_requires_expression
2787 (cp_parser *);
2788 static tree cp_parser_requirement_parameter_list
2789 (cp_parser *);
2790 static tree cp_parser_requirement_body
2791 (cp_parser *);
2792 static tree cp_parser_requirement_seq
2793 (cp_parser *);
2794 static tree cp_parser_requirement
2795 (cp_parser *);
2796 static tree cp_parser_simple_requirement
2797 (cp_parser *);
2798 static tree cp_parser_compound_requirement
2799 (cp_parser *);
2800 static tree cp_parser_type_requirement
2801 (cp_parser *);
2802 static tree cp_parser_nested_requirement
2803 (cp_parser *);
2805 /* Transactional Memory Extensions */
2807 static tree cp_parser_transaction
2808 (cp_parser *, cp_token *);
2809 static tree cp_parser_transaction_expression
2810 (cp_parser *, enum rid);
2811 static void cp_parser_function_transaction
2812 (cp_parser *, enum rid);
2813 static tree cp_parser_transaction_cancel
2814 (cp_parser *);
2816 /* Coroutine extensions. */
2818 static tree cp_parser_yield_expression
2819 (cp_parser *);
2821 /* Contracts */
2823 static void cp_parser_late_contract_condition
2824 (cp_parser *, tree, tree);
2826 enum pragma_context {
2827 pragma_external,
2828 pragma_member,
2829 pragma_objc_icode,
2830 pragma_stmt,
2831 pragma_compound
2833 static bool cp_parser_pragma
2834 (cp_parser *, enum pragma_context, bool *);
2836 /* Objective-C++ Productions */
2838 static tree cp_parser_objc_message_receiver
2839 (cp_parser *);
2840 static tree cp_parser_objc_message_args
2841 (cp_parser *);
2842 static tree cp_parser_objc_message_expression
2843 (cp_parser *);
2844 static cp_expr cp_parser_objc_encode_expression
2845 (cp_parser *);
2846 static tree cp_parser_objc_defs_expression
2847 (cp_parser *);
2848 static tree cp_parser_objc_protocol_expression
2849 (cp_parser *);
2850 static tree cp_parser_objc_selector_expression
2851 (cp_parser *);
2852 static cp_expr cp_parser_objc_expression
2853 (cp_parser *);
2854 static bool cp_parser_objc_selector_p
2855 (enum cpp_ttype);
2856 static tree cp_parser_objc_selector
2857 (cp_parser *);
2858 static tree cp_parser_objc_protocol_refs_opt
2859 (cp_parser *);
2860 static void cp_parser_objc_declaration
2861 (cp_parser *, tree);
2862 static tree cp_parser_objc_statement
2863 (cp_parser *);
2864 static bool cp_parser_objc_valid_prefix_attributes
2865 (cp_parser *, tree *);
2866 static void cp_parser_objc_at_property_declaration
2867 (cp_parser *) ;
2868 static void cp_parser_objc_at_synthesize_declaration
2869 (cp_parser *) ;
2870 static void cp_parser_objc_at_dynamic_declaration
2871 (cp_parser *) ;
2872 static tree cp_parser_objc_struct_declaration
2873 (cp_parser *) ;
2875 /* Utility Routines */
2877 static cp_expr cp_parser_lookup_name
2878 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2879 static tree cp_parser_lookup_name_simple
2880 (cp_parser *, tree, location_t);
2881 static tree cp_parser_maybe_treat_template_as_class
2882 (tree, bool);
2883 static bool cp_parser_check_declarator_template_parameters
2884 (cp_parser *, cp_declarator *, location_t);
2885 static bool cp_parser_check_template_parameters
2886 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2887 static cp_expr cp_parser_simple_cast_expression
2888 (cp_parser *);
2889 static tree cp_parser_global_scope_opt
2890 (cp_parser *, bool);
2891 static bool cp_parser_constructor_declarator_p
2892 (cp_parser *, cp_parser_flags, bool);
2893 static tree cp_parser_function_definition_from_specifiers_and_declarator
2894 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2895 static tree cp_parser_function_definition_after_declarator
2896 (cp_parser *, bool);
2897 static bool cp_parser_template_declaration_after_export
2898 (cp_parser *, bool);
2899 static void cp_parser_perform_template_parameter_access_checks
2900 (vec<deferred_access_check, va_gc> *);
2901 static tree cp_parser_single_declaration
2902 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2903 static cp_expr cp_parser_functional_cast
2904 (cp_parser *, tree);
2905 static tree cp_parser_save_member_function_body
2906 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2907 static tree cp_parser_save_nsdmi
2908 (cp_parser *);
2909 static tree cp_parser_enclosed_template_argument_list
2910 (cp_parser *);
2911 static void cp_parser_save_default_args
2912 (cp_parser *, tree);
2913 static void cp_parser_late_parsing_for_member
2914 (cp_parser *, tree);
2915 static tree cp_parser_late_parse_one_default_arg
2916 (cp_parser *, tree, tree, tree);
2917 static void cp_parser_late_parsing_nsdmi
2918 (cp_parser *, tree);
2919 static void cp_parser_late_parsing_default_args
2920 (cp_parser *, tree);
2921 static tree cp_parser_sizeof_operand
2922 (cp_parser *, enum rid);
2923 static cp_expr cp_parser_trait
2924 (cp_parser *, const cp_trait *);
2925 static bool cp_parser_declares_only_class_p
2926 (cp_parser *);
2927 static void cp_parser_set_storage_class
2928 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2929 static void cp_parser_set_decl_spec_type
2930 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2931 static void set_and_check_decl_spec_loc
2932 (cp_decl_specifier_seq *decl_specs,
2933 cp_decl_spec ds, cp_token *);
2934 static bool cp_parser_friend_p
2935 (const cp_decl_specifier_seq *);
2936 static void cp_parser_required_error
2937 (cp_parser *, required_token, bool, location_t);
2938 static cp_token *cp_parser_require
2939 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2940 static cp_token *cp_parser_require_keyword
2941 (cp_parser *, enum rid, required_token);
2942 static bool cp_parser_token_starts_function_definition_p
2943 (cp_token *);
2944 static bool cp_parser_next_token_starts_class_definition_p
2945 (cp_parser *);
2946 static bool cp_parser_next_token_ends_template_argument_p
2947 (cp_parser *);
2948 static bool cp_parser_nth_token_starts_template_argument_list_p
2949 (cp_parser *, size_t);
2950 static enum tag_types cp_parser_token_is_class_key
2951 (cp_token *);
2952 static enum tag_types cp_parser_token_is_type_parameter_key
2953 (cp_token *);
2954 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2955 static void cp_parser_check_class_key
2956 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2957 static void cp_parser_check_access_in_redeclaration
2958 (tree type, location_t location);
2959 static bool cp_parser_optional_template_keyword
2960 (cp_parser *);
2961 static void cp_parser_pre_parsed_nested_name_specifier
2962 (cp_parser *);
2963 static bool cp_parser_cache_group
2964 (cp_parser *, enum cpp_ttype, unsigned);
2965 static tree cp_parser_cache_defarg
2966 (cp_parser *parser, bool nsdmi);
2967 static void cp_parser_parse_tentatively
2968 (cp_parser *);
2969 static void cp_parser_commit_to_tentative_parse
2970 (cp_parser *);
2971 static void cp_parser_commit_to_topmost_tentative_parse
2972 (cp_parser *);
2973 static void cp_parser_abort_tentative_parse
2974 (cp_parser *);
2975 static bool cp_parser_parse_definitely
2976 (cp_parser *);
2977 static inline bool cp_parser_parsing_tentatively
2978 (cp_parser *);
2979 static bool cp_parser_uncommitted_to_tentative_parse_p
2980 (cp_parser *);
2981 static void cp_parser_error
2982 (cp_parser *, const char *);
2983 static void cp_parser_name_lookup_error
2984 (cp_parser *, tree, tree, name_lookup_error, location_t);
2985 static bool cp_parser_simulate_error
2986 (cp_parser *);
2987 static bool cp_parser_check_type_definition
2988 (cp_parser *);
2989 static void cp_parser_check_for_definition_in_return_type
2990 (cp_declarator *, tree, location_t type_location);
2991 static void cp_parser_check_for_invalid_template_id
2992 (cp_parser *, tree, enum tag_types, location_t location);
2993 static bool cp_parser_non_integral_constant_expression
2994 (cp_parser *, non_integral_constant);
2995 static void cp_parser_diagnose_invalid_type_name
2996 (cp_parser *, tree, location_t);
2997 static bool cp_parser_parse_and_diagnose_invalid_type_name
2998 (cp_parser *);
2999 static int cp_parser_skip_to_closing_parenthesis
3000 (cp_parser *, bool, bool, bool);
3001 static void cp_parser_skip_to_end_of_statement
3002 (cp_parser *);
3003 static void cp_parser_consume_semicolon_at_end_of_statement
3004 (cp_parser *);
3005 static void cp_parser_skip_to_end_of_block_or_statement
3006 (cp_parser *);
3007 static bool cp_parser_skip_to_closing_brace
3008 (cp_parser *);
3009 static bool cp_parser_skip_entire_template_parameter_list
3010 (cp_parser *);
3011 static void cp_parser_require_end_of_template_parameter_list
3012 (cp_parser *);
3013 static bool cp_parser_skip_to_end_of_template_parameter_list
3014 (cp_parser *);
3015 static void cp_parser_skip_to_pragma_eol
3016 (cp_parser*, cp_token *);
3017 static bool cp_parser_error_occurred
3018 (cp_parser *);
3019 static bool cp_parser_allow_gnu_extensions_p
3020 (cp_parser *);
3021 static bool cp_parser_is_pure_string_literal
3022 (cp_token *);
3023 static bool cp_parser_is_string_literal
3024 (cp_token *);
3025 static bool cp_parser_is_keyword
3026 (cp_token *, enum rid);
3027 static tree cp_parser_make_typename_type
3028 (cp_parser *, tree, location_t location);
3029 static cp_declarator * cp_parser_make_indirect_declarator
3030 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
3031 static bool cp_parser_compound_literal_p
3032 (cp_parser *);
3033 static bool cp_parser_array_designator_p
3034 (cp_parser *);
3035 static bool cp_parser_init_statement_p
3036 (cp_parser *);
3037 static bool cp_parser_skip_up_to_closing_square_bracket
3038 (cp_parser *);
3039 static bool cp_parser_skip_to_closing_square_bracket
3040 (cp_parser *);
3041 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
3042 static bool cp_parser_next_tokens_can_be_canon_loop (cp_parser *,
3043 enum tree_code, bool);
3044 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
3046 // -------------------------------------------------------------------------- //
3047 // Unevaluated Operand Guard
3049 // Implementation of an RAII helper for unevaluated operand parsing.
3050 cp_unevaluated::cp_unevaluated ()
3052 ++cp_unevaluated_operand;
3053 ++c_inhibit_evaluation_warnings;
3056 cp_unevaluated::~cp_unevaluated ()
3058 --c_inhibit_evaluation_warnings;
3059 --cp_unevaluated_operand;
3062 // -------------------------------------------------------------------------- //
3063 // Tentative Parsing
3065 /* Returns nonzero if we are parsing tentatively. */
3067 static inline bool
3068 cp_parser_parsing_tentatively (cp_parser* parser)
3070 return parser->context->next != NULL;
3073 /* Returns nonzero if TOKEN is a string literal. */
3075 static bool
3076 cp_parser_is_pure_string_literal (cp_token* token)
3078 return (token->type == CPP_STRING ||
3079 token->type == CPP_STRING16 ||
3080 token->type == CPP_STRING32 ||
3081 token->type == CPP_WSTRING ||
3082 token->type == CPP_UTF8STRING);
3085 /* Returns nonzero if TOKEN is a string literal
3086 of a user-defined string literal. */
3088 static bool
3089 cp_parser_is_string_literal (cp_token* token)
3091 return (cp_parser_is_pure_string_literal (token) ||
3092 token->type == CPP_STRING_USERDEF ||
3093 token->type == CPP_STRING16_USERDEF ||
3094 token->type == CPP_STRING32_USERDEF ||
3095 token->type == CPP_WSTRING_USERDEF ||
3096 token->type == CPP_UTF8STRING_USERDEF);
3099 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3101 static bool
3102 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3104 return token->keyword == keyword;
3107 /* Helper function for cp_parser_error.
3108 Having peeked a token of kind TOK1_KIND that might signify
3109 a conflict marker, peek successor tokens to determine
3110 if we actually do have a conflict marker.
3111 Specifically, we consider a run of 7 '<', '=' or '>' characters
3112 at the start of a line as a conflict marker.
3113 These come through the lexer as three pairs and a single,
3114 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3115 If it returns true, *OUT_LOC is written to with the location/range
3116 of the marker. */
3118 static bool
3119 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3120 location_t *out_loc)
3122 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3123 if (token2->type != tok1_kind)
3124 return false;
3125 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3126 if (token3->type != tok1_kind)
3127 return false;
3128 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3129 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3130 return false;
3132 /* It must be at the start of the line. */
3133 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3134 if (LOCATION_COLUMN (start_loc) != 1)
3135 return false;
3137 /* We have a conflict marker. Construct a location of the form:
3138 <<<<<<<
3139 ^~~~~~~
3140 with start == caret, finishing at the end of the marker. */
3141 location_t finish_loc = get_finish (token4->location);
3142 *out_loc = make_location (start_loc, start_loc, finish_loc);
3144 return true;
3147 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3148 RT_CLOSE_PAREN. */
3150 static const char *
3151 get_matching_symbol (required_token token_desc)
3153 switch (token_desc)
3155 default:
3156 gcc_unreachable ();
3157 return "";
3158 case RT_CLOSE_BRACE:
3159 return "{";
3160 case RT_CLOSE_PAREN:
3161 return "(";
3165 /* Attempt to convert TOKEN_DESC from a required_token to an
3166 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3168 static enum cpp_ttype
3169 get_required_cpp_ttype (required_token token_desc)
3171 switch (token_desc)
3173 case RT_SEMICOLON:
3174 return CPP_SEMICOLON;
3175 case RT_OPEN_PAREN:
3176 return CPP_OPEN_PAREN;
3177 case RT_CLOSE_BRACE:
3178 return CPP_CLOSE_BRACE;
3179 case RT_OPEN_BRACE:
3180 return CPP_OPEN_BRACE;
3181 case RT_CLOSE_SQUARE:
3182 return CPP_CLOSE_SQUARE;
3183 case RT_OPEN_SQUARE:
3184 return CPP_OPEN_SQUARE;
3185 case RT_COMMA:
3186 return CPP_COMMA;
3187 case RT_COLON:
3188 return CPP_COLON;
3189 case RT_CLOSE_PAREN:
3190 return CPP_CLOSE_PAREN;
3192 default:
3193 /* Use CPP_EOF as a "no completions possible" code. */
3194 return CPP_EOF;
3199 /* Subroutine of cp_parser_error and cp_parser_required_error.
3201 Issue a diagnostic of the form
3202 FILE:LINE: MESSAGE before TOKEN
3203 where TOKEN is the next token in the input stream. MESSAGE
3204 (specified by the caller) is usually of the form "expected
3205 OTHER-TOKEN".
3207 This bypasses the check for tentative passing, and potentially
3208 adds material needed by cp_parser_required_error.
3210 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3211 suggesting insertion of the missing token.
3213 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3214 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3215 location. */
3217 static void
3218 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3219 required_token missing_token_desc,
3220 location_t matching_location)
3222 cp_token *token = cp_lexer_peek_token (parser->lexer);
3223 /* This diagnostic makes more sense if it is tagged to the line
3224 of the token we just peeked at. */
3225 cp_lexer_set_source_position_from_token (token);
3227 if (token->type == CPP_PRAGMA)
3229 error_at (token->location,
3230 "%<#pragma%> is not allowed here");
3231 cp_parser_skip_to_pragma_eol (parser, token);
3232 return;
3235 if (cp_token_is_module_directive (token))
3237 auto_diagnostic_group d;
3238 error_at (token->location, "unexpected module directive");
3239 if (token->keyword != RID__EXPORT)
3240 inform (token->location, "perhaps insert a line break after"
3241 " %qs, or other disambiguation, to prevent this being"
3242 " considered a module control-line",
3243 (token->keyword == RID__MODULE) ? "module" : "import");
3244 cp_parser_skip_to_pragma_eol (parser, token);
3245 return;
3248 /* If this is actually a conflict marker, report it as such. */
3249 if (token->type == CPP_LSHIFT
3250 || token->type == CPP_RSHIFT
3251 || token->type == CPP_EQ_EQ)
3253 location_t loc;
3254 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3256 error_at (loc, "version control conflict marker in file");
3257 expanded_location token_exploc = expand_location (token->location);
3258 /* Consume tokens until the end of the source line. */
3259 for (;;)
3261 cp_lexer_consume_token (parser->lexer);
3262 cp_token *next = cp_lexer_peek_token (parser->lexer);
3263 if (next->type == CPP_EOF)
3264 break;
3265 if (next->location == UNKNOWN_LOCATION
3266 || loc == UNKNOWN_LOCATION)
3267 break;
3269 expanded_location next_exploc = expand_location (next->location);
3270 if (next_exploc.file != token_exploc.file)
3271 break;
3272 if (next_exploc.line != token_exploc.line)
3273 break;
3275 return;
3279 auto_diagnostic_group d;
3280 gcc_rich_location richloc (input_location);
3282 bool added_matching_location = false;
3284 if (missing_token_desc != RT_NONE)
3285 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3287 /* Potentially supply a fix-it hint, suggesting to add the
3288 missing token immediately after the *previous* token.
3289 This may move the primary location within richloc. */
3290 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3291 location_t prev_token_loc = prev_token->location;
3292 maybe_suggest_missing_token_insertion (&richloc, ttype,
3293 prev_token_loc);
3295 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3296 Attempt to consolidate diagnostics by printing it as a
3297 secondary range within the main diagnostic. */
3298 if (matching_location != UNKNOWN_LOCATION)
3299 added_matching_location
3300 = richloc.add_location_if_nearby (*global_dc,
3301 matching_location);
3304 /* If we were parsing a string-literal and there is an unknown name
3305 token right after, then check to see if that could also have been
3306 a literal string by checking the name against a list of known
3307 standard string literal constants defined in header files. If
3308 there is one, then add that as an hint to the error message. */
3309 name_hint h;
3310 if (token->type == CPP_NAME)
3311 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3312 if (cp_parser_is_string_literal (prev_token))
3314 tree name = token->u.value;
3315 const char *token_name = IDENTIFIER_POINTER (name);
3316 const char *header_hint
3317 = get_cp_stdlib_header_for_string_macro_name (token_name);
3318 if (header_hint != NULL)
3319 h = name_hint (NULL, new suggest_missing_header (token->location,
3320 token_name,
3321 header_hint));
3324 /* Actually emit the error. */
3325 c_parse_error (gmsgid,
3326 /* Because c_parser_error does not understand
3327 CPP_KEYWORD, keywords are treated like
3328 identifiers. */
3329 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3330 token->u.value, token->flags, &richloc);
3332 if (missing_token_desc != RT_NONE)
3334 /* If we weren't able to consolidate matching_location, then
3335 print it as a secondary diagnostic. */
3336 if (matching_location != UNKNOWN_LOCATION
3337 && !added_matching_location)
3338 inform (matching_location, "to match this %qs",
3339 get_matching_symbol (missing_token_desc));
3343 /* If not parsing tentatively, issue a diagnostic of the form
3344 FILE:LINE: MESSAGE before TOKEN
3345 where TOKEN is the next token in the input stream. MESSAGE
3346 (specified by the caller) is usually of the form "expected
3347 OTHER-TOKEN". */
3349 static void
3350 cp_parser_error (cp_parser* parser, const char* gmsgid)
3352 if (!cp_parser_simulate_error (parser))
3353 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3356 /* Issue an error about name-lookup failing. NAME is the
3357 IDENTIFIER_NODE DECL is the result of
3358 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3359 the thing that we hoped to find. */
3361 static void
3362 cp_parser_name_lookup_error (cp_parser* parser,
3363 tree name,
3364 tree decl,
3365 name_lookup_error desired,
3366 location_t location)
3368 /* If name lookup completely failed, tell the user that NAME was not
3369 declared. */
3370 if (decl == error_mark_node)
3372 if (parser->scope && parser->scope != global_namespace)
3373 error_at (location, "%<%E::%E%> has not been declared",
3374 parser->scope, name);
3375 else if (parser->scope == global_namespace)
3376 error_at (location, "%<::%E%> has not been declared", name);
3377 else if (parser->object_scope
3378 && !CLASS_TYPE_P (parser->object_scope))
3379 error_at (location, "request for member %qE in non-class type %qT",
3380 name, parser->object_scope);
3381 else if (parser->object_scope)
3382 error_at (location, "%<%T::%E%> has not been declared",
3383 parser->object_scope, name);
3384 else
3385 error_at (location, "%qE has not been declared", name);
3387 else if (parser->scope && parser->scope != global_namespace)
3389 switch (desired)
3391 case NLE_TYPE:
3392 error_at (location, "%<%E::%E%> is not a type",
3393 parser->scope, name);
3394 break;
3395 case NLE_CXX98:
3396 error_at (location, "%<%E::%E%> is not a class or namespace",
3397 parser->scope, name);
3398 break;
3399 case NLE_NOT_CXX98:
3400 error_at (location,
3401 "%<%E::%E%> is not a class, namespace, or enumeration",
3402 parser->scope, name);
3403 break;
3404 default:
3405 gcc_unreachable ();
3409 else if (parser->scope == global_namespace)
3411 switch (desired)
3413 case NLE_TYPE:
3414 error_at (location, "%<::%E%> is not a type", name);
3415 break;
3416 case NLE_CXX98:
3417 error_at (location, "%<::%E%> is not a class or namespace", name);
3418 break;
3419 case NLE_NOT_CXX98:
3420 error_at (location,
3421 "%<::%E%> is not a class, namespace, or enumeration",
3422 name);
3423 break;
3424 default:
3425 gcc_unreachable ();
3428 else
3430 switch (desired)
3432 case NLE_TYPE:
3433 error_at (location, "%qE is not a type", name);
3434 break;
3435 case NLE_CXX98:
3436 error_at (location, "%qE is not a class or namespace", name);
3437 break;
3438 case NLE_NOT_CXX98:
3439 error_at (location,
3440 "%qE is not a class, namespace, or enumeration", name);
3441 break;
3442 default:
3443 gcc_unreachable ();
3448 /* If we are parsing tentatively, remember that an error has occurred
3449 during this tentative parse. Returns true if the error was
3450 simulated; false if a message should be issued by the caller. */
3452 static bool
3453 cp_parser_simulate_error (cp_parser* parser)
3455 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3457 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3458 return true;
3460 return false;
3463 /* This function is called when a type is defined. If type
3464 definitions are forbidden at this point, an error message is
3465 issued. */
3467 static bool
3468 cp_parser_check_type_definition (cp_parser* parser)
3470 /* If types are forbidden here, issue a message. */
3471 if (parser->type_definition_forbidden_message)
3473 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3474 or %qs in the message need to be interpreted. */
3475 error (parser->type_definition_forbidden_message,
3476 parser->type_definition_forbidden_message_arg);
3477 return false;
3479 return true;
3482 /* This function is called when the DECLARATOR is processed. The TYPE
3483 was a type defined in the decl-specifiers. If it is invalid to
3484 define a type in the decl-specifiers for DECLARATOR, an error is
3485 issued. TYPE_LOCATION is the location of TYPE and is used
3486 for error reporting. */
3488 static void
3489 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3490 tree type, location_t type_location)
3492 /* [dcl.fct] forbids type definitions in return types.
3493 Unfortunately, it's not easy to know whether or not we are
3494 processing a return type until after the fact. */
3495 while (declarator
3496 && (declarator->kind == cdk_pointer
3497 || declarator->kind == cdk_reference
3498 || declarator->kind == cdk_ptrmem))
3499 declarator = declarator->declarator;
3500 if (declarator
3501 && declarator->kind == cdk_function)
3503 error_at (type_location,
3504 "new types may not be defined in a return type");
3505 inform (type_location,
3506 "(perhaps a semicolon is missing after the definition of %qT)",
3507 type);
3511 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3512 "<" in any valid C++ program. If the next token is indeed "<",
3513 issue a message warning the user about what appears to be an
3514 invalid attempt to form a template-id. LOCATION is the location
3515 of the type-specifier (TYPE) */
3517 static void
3518 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3519 tree type,
3520 enum tag_types tag_type,
3521 location_t location)
3523 cp_token_position start = 0;
3525 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3527 if (TREE_CODE (type) == TYPE_DECL)
3528 type = TREE_TYPE (type);
3529 if (TYPE_P (type) && !template_placeholder_p (type))
3530 error_at (location, "%qT is not a template", type);
3531 else if (identifier_p (type))
3533 if (tag_type != none_type)
3534 error_at (location, "%qE is not a class template", type);
3535 else
3536 error_at (location, "%qE is not a template", type);
3538 else
3539 error_at (location, "invalid template-id");
3540 /* Remember the location of the invalid "<". */
3541 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3542 start = cp_lexer_token_position (parser->lexer, true);
3543 /* Consume the "<". */
3544 cp_lexer_consume_token (parser->lexer);
3545 /* Parse the template arguments. */
3546 cp_parser_enclosed_template_argument_list (parser);
3547 /* Permanently remove the invalid template arguments so that
3548 this error message is not issued again. */
3549 if (start)
3550 cp_lexer_purge_tokens_after (parser->lexer, start);
3554 /* If parsing an integral constant-expression, issue an error message
3555 about the fact that THING appeared and return true. Otherwise,
3556 return false. In either case, set
3557 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3559 static bool
3560 cp_parser_non_integral_constant_expression (cp_parser *parser,
3561 non_integral_constant thing)
3563 parser->non_integral_constant_expression_p = true;
3564 if (parser->integral_constant_expression_p)
3566 if (!parser->allow_non_integral_constant_expression_p)
3568 const char *msg = NULL;
3569 switch (thing)
3571 case NIC_FLOAT:
3572 pedwarn (input_location, OPT_Wpedantic,
3573 "ISO C++ forbids using a floating-point literal "
3574 "in a constant-expression");
3575 return true;
3576 case NIC_CAST:
3577 error ("a cast to a type other than an integral or "
3578 "enumeration type cannot appear in a "
3579 "constant-expression");
3580 return true;
3581 case NIC_TYPEID:
3582 error ("%<typeid%> operator "
3583 "cannot appear in a constant-expression");
3584 return true;
3585 case NIC_NCC:
3586 error ("non-constant compound literals "
3587 "cannot appear in a constant-expression");
3588 return true;
3589 case NIC_FUNC_CALL:
3590 error ("a function call "
3591 "cannot appear in a constant-expression");
3592 return true;
3593 case NIC_INC:
3594 error ("an increment "
3595 "cannot appear in a constant-expression");
3596 return true;
3597 case NIC_DEC:
3598 error ("an decrement "
3599 "cannot appear in a constant-expression");
3600 return true;
3601 case NIC_ARRAY_REF:
3602 error ("an array reference "
3603 "cannot appear in a constant-expression");
3604 return true;
3605 case NIC_ADDR_LABEL:
3606 error ("the address of a label "
3607 "cannot appear in a constant-expression");
3608 return true;
3609 case NIC_OVERLOADED:
3610 error ("calls to overloaded operators "
3611 "cannot appear in a constant-expression");
3612 return true;
3613 case NIC_ASSIGNMENT:
3614 error ("an assignment cannot appear in a constant-expression");
3615 return true;
3616 case NIC_COMMA:
3617 error ("a comma operator "
3618 "cannot appear in a constant-expression");
3619 return true;
3620 case NIC_CONSTRUCTOR:
3621 error ("a call to a constructor "
3622 "cannot appear in a constant-expression");
3623 return true;
3624 case NIC_TRANSACTION:
3625 error ("a transaction expression "
3626 "cannot appear in a constant-expression");
3627 return true;
3628 case NIC_THIS:
3629 msg = "this";
3630 break;
3631 case NIC_FUNC_NAME:
3632 msg = "__FUNCTION__";
3633 break;
3634 case NIC_PRETTY_FUNC:
3635 msg = "__PRETTY_FUNCTION__";
3636 break;
3637 case NIC_C99_FUNC:
3638 msg = "__func__";
3639 break;
3640 case NIC_VA_ARG:
3641 msg = "va_arg";
3642 break;
3643 case NIC_ARROW:
3644 msg = "->";
3645 break;
3646 case NIC_POINT:
3647 msg = ".";
3648 break;
3649 case NIC_STAR:
3650 msg = "*";
3651 break;
3652 case NIC_ADDR:
3653 msg = "&";
3654 break;
3655 case NIC_PREINCREMENT:
3656 msg = "++";
3657 break;
3658 case NIC_PREDECREMENT:
3659 msg = "--";
3660 break;
3661 case NIC_NEW:
3662 msg = "new";
3663 break;
3664 case NIC_DEL:
3665 msg = "delete";
3666 break;
3667 default:
3668 gcc_unreachable ();
3670 if (msg)
3671 error ("%qs cannot appear in a constant-expression", msg);
3672 return true;
3675 return false;
3678 /* Emit a diagnostic for an invalid type name. This function commits
3679 to the current active tentative parse, if any. (Otherwise, the
3680 problematic construct might be encountered again later, resulting
3681 in duplicate error messages.) LOCATION is the location of ID. */
3683 static void
3684 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3685 location_t location)
3687 tree decl, ambiguous_decls;
3688 cp_parser_commit_to_tentative_parse (parser);
3689 /* Try to lookup the identifier. */
3690 decl = cp_parser_lookup_name (parser, id, none_type,
3691 /*is_template=*/false,
3692 /*is_namespace=*/false,
3693 /*check_dependency=*/true,
3694 &ambiguous_decls, location);
3695 if (ambiguous_decls)
3696 /* If the lookup was ambiguous, an error will already have
3697 been issued. */
3698 return;
3699 /* If the lookup found a template-name, it means that the user forgot
3700 to specify an argument list. Emit a useful error message. */
3701 if (DECL_TYPE_TEMPLATE_P (decl))
3703 auto_diagnostic_group d;
3704 error_at (location,
3705 "invalid use of template-name %qE without an argument list",
3706 decl);
3707 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3708 inform (location, "class template argument deduction is only available "
3709 "with %<-std=c++17%> or %<-std=gnu++17%>");
3710 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3712 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3713 error_at (location, "invalid use of destructor %qD as a type", id);
3714 else if (TREE_CODE (decl) == TYPE_DECL)
3715 /* Something like 'unsigned A a;' */
3716 error_at (location, "invalid combination of multiple type-specifiers");
3717 else if (!parser->scope)
3719 /* Issue an error message. */
3720 auto_diagnostic_group d;
3721 name_hint hint;
3722 if (TREE_CODE (id) == IDENTIFIER_NODE)
3723 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3724 if (const char *suggestion = hint.suggestion ())
3726 gcc_rich_location richloc (location);
3727 richloc.add_fixit_replace (suggestion);
3728 error_at (&richloc,
3729 "%qE does not name a type; did you mean %qs?",
3730 id, suggestion);
3732 else
3733 error_at (location, "%qE does not name a type", id);
3734 /* If we're in a template class, it's possible that the user was
3735 referring to a type from a base class. For example:
3737 template <typename T> struct A { typedef T X; };
3738 template <typename T> struct B : public A<T> { X x; };
3740 The user should have said "typename A<T>::X". */
3741 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3742 inform (location, "C++11 %<constexpr%> only available with "
3743 "%<-std=c++11%> or %<-std=gnu++11%>");
3744 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3745 inform (location, "C++11 %<noexcept%> only available with "
3746 "%<-std=c++11%> or %<-std=gnu++11%>");
3747 else if (TREE_CODE (id) == IDENTIFIER_NODE
3748 && (id_equal (id, "module") || id_equal (id, "import")))
3750 if (modules_p ())
3751 inform (location, "%qE is not recognized as a module control-line",
3752 id);
3753 else if (cxx_dialect < cxx20)
3754 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3755 id);
3756 else
3757 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3758 ", which is not yet enabled with %<-std=c++20%>", id);
3760 else if (cxx_dialect < cxx11
3761 && TREE_CODE (id) == IDENTIFIER_NODE
3762 && id_equal (id, "thread_local"))
3763 inform (location, "C++11 %<thread_local%> only available with "
3764 "%<-std=c++11%> or %<-std=gnu++11%>");
3765 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3766 inform (location, "C++20 %<constinit%> only available with "
3767 "%<-std=c++20%> or %<-std=gnu++20%>");
3768 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3769 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3770 "%<-fconcepts%>");
3771 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3772 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3773 "%<-fconcepts%>");
3774 else if (processing_template_decl && current_class_type
3775 && TYPE_BINFO (current_class_type))
3777 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3778 b; b = TREE_CHAIN (b))
3780 tree base_type = BINFO_TYPE (b);
3781 if (CLASS_TYPE_P (base_type)
3782 && dependent_type_p (base_type))
3784 /* Go from a particular instantiation of the
3785 template (which will have an empty TYPE_FIELDs),
3786 to the main version. */
3787 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3788 for (tree field = TYPE_FIELDS (base_type);
3789 field; field = DECL_CHAIN (field))
3790 if (TREE_CODE (field) == TYPE_DECL
3791 && DECL_NAME (field) == id)
3793 inform (location,
3794 "(perhaps %<typename %T::%E%> was intended)",
3795 BINFO_TYPE (b), id);
3796 goto found;
3800 found:;
3803 /* Here we diagnose qualified-ids where the scope is actually correct,
3804 but the identifier does not resolve to a valid type name. */
3805 else if (parser->scope != error_mark_node)
3807 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3809 auto_diagnostic_group d;
3810 name_hint hint;
3811 if (decl == error_mark_node)
3812 hint = suggest_alternative_in_explicit_scope (location, id,
3813 parser->scope);
3814 const char *suggestion = hint.suggestion ();
3815 gcc_rich_location richloc (location_of (id));
3816 if (suggestion)
3817 richloc.add_fixit_replace (suggestion);
3818 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3820 if (suggestion)
3821 error_at (&richloc,
3822 "%qE in namespace %qE does not name a template"
3823 " type; did you mean %qs?",
3824 id, parser->scope, suggestion);
3825 else
3826 error_at (&richloc,
3827 "%qE in namespace %qE does not name a template type",
3828 id, parser->scope);
3830 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3832 if (suggestion)
3833 error_at (&richloc,
3834 "%qE in namespace %qE does not name a template"
3835 " type; did you mean %qs?",
3836 TREE_OPERAND (id, 0), parser->scope, suggestion);
3837 else
3838 error_at (&richloc,
3839 "%qE in namespace %qE does not name a template"
3840 " type",
3841 TREE_OPERAND (id, 0), parser->scope);
3843 else
3845 if (suggestion)
3846 error_at (&richloc,
3847 "%qE in namespace %qE does not name a type"
3848 "; did you mean %qs?",
3849 id, parser->scope, suggestion);
3850 else
3851 error_at (&richloc,
3852 "%qE in namespace %qE does not name a type",
3853 id, parser->scope);
3855 if (DECL_P (decl))
3856 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3858 else if (CLASS_TYPE_P (parser->scope)
3859 && constructor_name_p (id, parser->scope))
3861 /* A<T>::A<T>() */
3862 auto_diagnostic_group d;
3863 error_at (location, "%<%T::%E%> names the constructor, not"
3864 " the type", parser->scope, id);
3865 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3866 error_at (location, "and %qT has no template constructors",
3867 parser->scope);
3869 else if (TYPE_P (parser->scope)
3870 && dependent_scope_p (parser->scope))
3872 gcc_rich_location richloc (location);
3873 richloc.add_fixit_insert_before ("typename ");
3874 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3875 error_at (&richloc,
3876 "need %<typename%> before %<%T::%D::%E%> because "
3877 "%<%T::%D%> is a dependent scope",
3878 TYPE_CONTEXT (parser->scope),
3879 TYPENAME_TYPE_FULLNAME (parser->scope),
3881 TYPE_CONTEXT (parser->scope),
3882 TYPENAME_TYPE_FULLNAME (parser->scope));
3883 else
3884 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3885 "%qT is a dependent scope",
3886 parser->scope, id, parser->scope);
3888 else if (TYPE_P (parser->scope))
3890 auto_diagnostic_group d;
3891 if (!COMPLETE_TYPE_P (parser->scope))
3892 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3893 parser->scope);
3894 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3895 error_at (location_of (id),
3896 "%qE in %q#T does not name a template type",
3897 id, parser->scope);
3898 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3899 error_at (location_of (id),
3900 "%qE in %q#T does not name a template type",
3901 TREE_OPERAND (id, 0), parser->scope);
3902 else
3903 error_at (location_of (id),
3904 "%qE in %q#T does not name a type",
3905 id, parser->scope);
3906 if (DECL_P (decl))
3907 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3909 else
3910 gcc_unreachable ();
3914 /* Check for a common situation where a type-name should be present,
3915 but is not, and issue a sensible error message. Returns true if an
3916 invalid type-name was detected.
3918 The situation handled by this function are variable declarations of the
3919 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3920 Usually, `ID' should name a type, but if we got here it means that it
3921 does not. We try to emit the best possible error message depending on
3922 how exactly the id-expression looks like. */
3924 static bool
3925 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3927 tree id;
3928 cp_token *token = cp_lexer_peek_token (parser->lexer);
3930 /* Avoid duplicate error about ambiguous lookup. */
3931 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3933 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3934 if (next->type == CPP_NAME && next->error_reported)
3935 goto out;
3938 cp_parser_parse_tentatively (parser);
3939 id = cp_parser_id_expression (parser,
3940 /*template_keyword_p=*/false,
3941 /*check_dependency_p=*/true,
3942 /*template_p=*/NULL,
3943 /*declarator_p=*/true,
3944 /*optional_p=*/false);
3945 /* If the next token is a (, this is a function with no explicit return
3946 type, i.e. constructor, destructor or conversion op. */
3947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3948 || TREE_CODE (id) == TYPE_DECL)
3950 cp_parser_abort_tentative_parse (parser);
3951 return false;
3953 if (!cp_parser_parse_definitely (parser))
3954 return false;
3956 /* Emit a diagnostic for the invalid type. */
3957 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3958 out:
3959 /* If we aren't in the middle of a declarator (i.e. in a
3960 parameter-declaration-clause), skip to the end of the declaration;
3961 there's no point in trying to process it. */
3962 if (!parser->in_declarator_p)
3963 cp_parser_skip_to_end_of_block_or_statement (parser);
3964 return true;
3967 /* Consume tokens up to, and including, the next non-nested closing `)'.
3968 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3969 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3970 found an unnested token of that type. */
3972 static int
3973 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3974 bool recovering,
3975 cpp_ttype or_ttype,
3976 bool consume_paren)
3978 unsigned paren_depth = 0;
3979 unsigned brace_depth = 0;
3980 unsigned square_depth = 0;
3981 unsigned condop_depth = 0;
3983 if (recovering && or_ttype == CPP_EOF
3984 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3985 return 0;
3987 while (true)
3989 cp_token * token = cp_lexer_peek_token (parser->lexer);
3991 /* Have we found what we're looking for before the closing paren? */
3992 if (token->type == or_ttype && or_ttype != CPP_EOF
3993 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3994 return -1;
3996 switch (token->type)
3998 case CPP_PRAGMA_EOL:
3999 if (!parser->lexer->in_pragma)
4000 break;
4001 /* FALLTHRU */
4002 case CPP_EOF:
4003 /* If we've run out of tokens, then there is no closing `)'. */
4004 return 0;
4006 /* This is good for lambda expression capture-lists. */
4007 case CPP_OPEN_SQUARE:
4008 ++square_depth;
4009 break;
4010 case CPP_CLOSE_SQUARE:
4011 if (!square_depth--)
4012 return 0;
4013 break;
4015 case CPP_SEMICOLON:
4016 /* This matches the processing in skip_to_end_of_statement. */
4017 if (!brace_depth)
4018 return 0;
4019 break;
4021 case CPP_OPEN_BRACE:
4022 ++brace_depth;
4023 break;
4024 case CPP_CLOSE_BRACE:
4025 if (!brace_depth--)
4026 return 0;
4027 break;
4029 case CPP_OPEN_PAREN:
4030 if (!brace_depth)
4031 ++paren_depth;
4032 break;
4034 case CPP_CLOSE_PAREN:
4035 if (!brace_depth && !paren_depth--)
4037 if (consume_paren)
4038 cp_lexer_consume_token (parser->lexer);
4039 return 1;
4041 break;
4043 case CPP_QUERY:
4044 if (!brace_depth && !paren_depth && !square_depth)
4045 ++condop_depth;
4046 break;
4048 case CPP_COLON:
4049 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
4050 condop_depth--;
4051 break;
4053 case CPP_KEYWORD:
4054 if (!cp_token_is_module_directive (token))
4055 break;
4056 /* FALLTHROUGH */
4058 case CPP_PRAGMA:
4059 /* We fell into a pragma. Skip it, and continue. */
4060 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
4061 continue;
4063 default:
4064 break;
4067 /* Consume the token. */
4068 cp_lexer_consume_token (parser->lexer);
4072 /* Consume tokens up to, and including, the next non-nested closing `)'.
4073 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
4074 are doing error recovery. Returns -1 if OR_COMMA is true and we
4075 found an unnested token of that type. */
4077 static int
4078 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
4079 bool recovering,
4080 bool or_comma,
4081 bool consume_paren)
4083 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4084 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4085 ttype, consume_paren);
4088 /* Consume tokens until we reach the end of the current statement.
4089 Normally, that will be just before consuming a `;'. However, if a
4090 non-nested `}' comes first, then we stop before consuming that. */
4092 static void
4093 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4095 unsigned nesting_depth = 0;
4097 /* Unwind generic function template scope if necessary. */
4098 if (parser->fully_implicit_function_template_p)
4099 abort_fully_implicit_template (parser);
4101 while (true)
4103 cp_token *token = cp_lexer_peek_token (parser->lexer);
4105 switch (token->type)
4107 case CPP_PRAGMA_EOL:
4108 if (!parser->lexer->in_pragma)
4109 break;
4110 /* FALLTHRU */
4111 case CPP_EOF:
4112 /* If we've run out of tokens, stop. */
4113 return;
4115 case CPP_SEMICOLON:
4116 /* If the next token is a `;', we have reached the end of the
4117 statement. */
4118 if (!nesting_depth)
4119 return;
4120 break;
4122 case CPP_CLOSE_BRACE:
4123 /* If this is a non-nested '}', stop before consuming it.
4124 That way, when confronted with something like:
4126 { 3 + }
4128 we stop before consuming the closing '}', even though we
4129 have not yet reached a `;'. */
4130 if (nesting_depth == 0)
4131 return;
4133 /* If it is the closing '}' for a block that we have
4134 scanned, stop -- but only after consuming the token.
4135 That way given:
4137 void f g () { ... }
4138 typedef int I;
4140 we will stop after the body of the erroneously declared
4141 function, but before consuming the following `typedef'
4142 declaration. */
4143 if (--nesting_depth == 0)
4145 cp_lexer_consume_token (parser->lexer);
4146 return;
4148 break;
4150 case CPP_OPEN_BRACE:
4151 ++nesting_depth;
4152 break;
4154 case CPP_KEYWORD:
4155 if (!cp_token_is_module_directive (token))
4156 break;
4157 /* FALLTHROUGH */
4159 case CPP_PRAGMA:
4160 /* We fell into a pragma. Skip it, and continue or return. */
4161 cp_parser_skip_to_pragma_eol (parser, token);
4162 if (!nesting_depth)
4163 return;
4164 continue;
4166 default:
4167 break;
4170 /* Consume the token. */
4171 cp_lexer_consume_token (parser->lexer);
4175 /* This function is called at the end of a statement or declaration.
4176 If the next token is a semicolon, it is consumed; otherwise, error
4177 recovery is attempted. */
4179 static void
4180 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4182 /* Look for the trailing `;'. */
4183 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4185 /* If there is additional (erroneous) input, skip to the end of
4186 the statement. */
4187 cp_parser_skip_to_end_of_statement (parser);
4188 /* If the next token is now a `;', consume it. */
4189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4190 cp_lexer_consume_token (parser->lexer);
4194 /* Skip tokens until we have consumed an entire block, or until we
4195 have consumed a non-nested `;'. */
4197 static void
4198 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4200 int nesting_depth = 0;
4202 /* Unwind generic function template scope if necessary. */
4203 if (parser->fully_implicit_function_template_p)
4204 abort_fully_implicit_template (parser);
4206 while (nesting_depth >= 0)
4208 cp_token *token = cp_lexer_peek_token (parser->lexer);
4210 switch (token->type)
4212 case CPP_PRAGMA_EOL:
4213 if (!parser->lexer->in_pragma)
4214 break;
4215 /* FALLTHRU */
4216 case CPP_EOF:
4217 /* If we've run out of tokens, stop. */
4218 return;
4220 case CPP_SEMICOLON:
4221 /* Stop if this is an unnested ';'. */
4222 if (!nesting_depth)
4223 nesting_depth = -1;
4224 break;
4226 case CPP_CLOSE_BRACE:
4227 /* Stop if this is an unnested '}', or closes the outermost
4228 nesting level. */
4229 nesting_depth--;
4230 if (nesting_depth < 0)
4231 return;
4232 if (!nesting_depth)
4233 nesting_depth = -1;
4234 break;
4236 case CPP_OPEN_BRACE:
4237 /* Nest. */
4238 nesting_depth++;
4239 break;
4241 case CPP_KEYWORD:
4242 if (!cp_token_is_module_directive (token))
4243 break;
4244 /* FALLTHROUGH */
4246 case CPP_PRAGMA:
4247 /* Skip it, and continue or return. */
4248 cp_parser_skip_to_pragma_eol (parser, token);
4249 if (!nesting_depth)
4250 return;
4251 continue;
4253 default:
4254 break;
4257 /* Consume the token. */
4258 cp_lexer_consume_token (parser->lexer);
4262 /* Skip tokens until a non-nested closing curly brace is the next
4263 token, or there are no more tokens. Return true in the first case,
4264 false otherwise. */
4266 static bool
4267 cp_parser_skip_to_closing_brace (cp_parser *parser)
4269 unsigned nesting_depth = 0;
4271 while (true)
4273 cp_token *token = cp_lexer_peek_token (parser->lexer);
4275 switch (token->type)
4277 case CPP_PRAGMA_EOL:
4278 if (!parser->lexer->in_pragma)
4279 break;
4280 /* FALLTHRU */
4281 case CPP_EOF:
4282 /* If we've run out of tokens, stop. */
4283 return false;
4285 case CPP_CLOSE_BRACE:
4286 /* If the next token is a non-nested `}', then we have reached
4287 the end of the current block. */
4288 if (nesting_depth-- == 0)
4289 return true;
4290 break;
4292 case CPP_OPEN_BRACE:
4293 /* If it the next token is a `{', then we are entering a new
4294 block. Consume the entire block. */
4295 ++nesting_depth;
4296 break;
4298 default:
4299 break;
4302 /* Consume the token. */
4303 cp_lexer_consume_token (parser->lexer);
4307 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4308 parameter is the PRAGMA token, allowing us to purge the entire pragma
4309 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4310 forwards (not error recovery). */
4312 static void
4313 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4315 cp_token *token;
4319 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4320 before an EOF token, even when the EOF is on the pragma line.
4321 We should never get here without being inside a deferred
4322 pragma. */
4323 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4324 token = cp_lexer_consume_token (parser->lexer);
4326 while (token->type != CPP_PRAGMA_EOL);
4328 if (pragma_tok)
4330 parser->lexer->in_pragma = false;
4331 if (parser->lexer->in_omp_attribute_pragma
4332 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4334 parser->lexer = parser->lexer->next;
4335 /* Put the current source position back where it was before this
4336 lexer was pushed. */
4337 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4342 /* Require pragma end of line, resyncing with it as necessary. The
4343 arguments are as for cp_parser_skip_to_pragma_eol. */
4345 static void
4346 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4348 parser->lexer->in_pragma = false;
4349 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4350 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4351 else if (parser->lexer->in_omp_attribute_pragma
4352 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4354 parser->lexer = parser->lexer->next;
4355 /* Put the current source position back where it was before this
4356 lexer was pushed. */
4357 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4361 /* This is a simple wrapper around make_typename_type. When the id is
4362 an unresolved identifier node, we can provide a superior diagnostic
4363 using cp_parser_diagnose_invalid_type_name. */
4365 static tree
4366 cp_parser_make_typename_type (cp_parser *parser, tree id,
4367 location_t id_location)
4369 tree result;
4370 if (identifier_p (id))
4372 result = make_typename_type (parser->scope, id, typename_type,
4373 /*complain=*/tf_none);
4374 if (result == error_mark_node)
4375 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4376 return result;
4378 return make_typename_type (parser->scope, id, typename_type, tf_error);
4381 /* This is a wrapper around the
4382 make_{pointer,ptrmem,reference}_declarator functions that decides
4383 which one to call based on the CODE and CLASS_TYPE arguments. The
4384 CODE argument should be one of the values returned by
4385 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4386 appertain to the pointer or reference. */
4388 static cp_declarator *
4389 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4390 cp_cv_quals cv_qualifiers,
4391 cp_declarator *target,
4392 tree attributes)
4394 if (code == ERROR_MARK || target == cp_error_declarator)
4395 return cp_error_declarator;
4397 if (code == INDIRECT_REF)
4398 if (class_type == NULL_TREE)
4399 return make_pointer_declarator (cv_qualifiers, target, attributes);
4400 else
4401 return make_ptrmem_declarator (cv_qualifiers, class_type,
4402 target, attributes);
4403 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4404 return make_reference_declarator (cv_qualifiers, target,
4405 false, attributes);
4406 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4407 return make_reference_declarator (cv_qualifiers, target,
4408 true, attributes);
4409 gcc_unreachable ();
4412 /* Create a new C++ parser. */
4414 static cp_parser *
4415 cp_parser_new (cp_lexer *lexer)
4417 /* Initialize the binops_by_token so that we can get the tree
4418 directly from the token. */
4419 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4420 binops_by_token[binops[i].token_type] = binops[i];
4422 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4423 parser->lexer = lexer;
4424 parser->context = cp_parser_context_new (NULL);
4426 /* For now, we always accept GNU extensions. */
4427 parser->allow_gnu_extensions_p = 1;
4429 /* The `>' token is a greater-than operator, not the end of a
4430 template-id. */
4431 parser->greater_than_is_operator_p = true;
4433 parser->default_arg_ok_p = true;
4435 /* We are not parsing a constant-expression. */
4436 parser->integral_constant_expression_p = false;
4437 parser->allow_non_integral_constant_expression_p = false;
4438 parser->non_integral_constant_expression_p = false;
4440 /* Local variable names are not forbidden. */
4441 parser->local_variables_forbidden_p = 0;
4443 /* We are not processing an `extern "C"' declaration. */
4444 parser->in_unbraced_linkage_specification_p = false;
4446 /* We aren't parsing an export-declaration. */
4447 parser->in_unbraced_export_declaration_p = false;
4449 /* We are not processing a declarator. */
4450 parser->in_declarator_p = false;
4452 /* We are not processing a template-argument-list. */
4453 parser->in_template_argument_list_p = false;
4455 /* We are not in an iteration statement. */
4456 parser->in_statement = 0;
4458 /* We are not in a switch statement. */
4459 parser->in_switch_statement_p = false;
4461 /* We are not parsing a type-id inside an expression. */
4462 parser->in_type_id_in_expr_p = false;
4464 /* String literals should be translated to the execution character set. */
4465 parser->translate_strings_p = true;
4467 /* We are not parsing a function body. */
4468 parser->in_function_body = false;
4470 /* We can correct until told otherwise. */
4471 parser->colon_corrects_to_scope_p = true;
4473 /* The unparsed function queue is empty. */
4474 push_unparsed_function_queues (parser);
4476 /* There are no classes being defined. */
4477 parser->num_classes_being_defined = 0;
4479 /* No template parameters apply. */
4480 parser->num_template_parameter_lists = 0;
4482 /* Special parsing data structures. */
4483 parser->omp_declare_simd = NULL;
4484 parser->oacc_routine = NULL;
4486 /* Disallow OpenMP array sections in expressions. */
4487 parser->omp_array_section_p = false;
4489 /* Not declaring an implicit function template. */
4490 parser->auto_is_implicit_function_template_parm_p = false;
4491 parser->fully_implicit_function_template_p = false;
4492 parser->implicit_template_parms = 0;
4493 parser->implicit_template_scope = 0;
4495 /* Allow constrained-type-specifiers. */
4496 parser->prevent_constrained_type_specifiers = 0;
4498 /* We haven't yet seen an 'extern "C"'. */
4499 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4501 return parser;
4504 /* Create a cp_lexer structure which will emit the tokens in CACHE
4505 and push it onto the parser's lexer stack. This is used for delayed
4506 parsing of in-class method bodies and default arguments, and should
4507 not be confused with tentative parsing. */
4508 static void
4509 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4511 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4512 lexer->next = parser->lexer;
4513 parser->lexer = lexer;
4515 /* Move the current source position to that of the first token in the
4516 new lexer. */
4517 cp_lexer_set_source_position_from_token (lexer->next_token);
4520 /* Pop the top lexer off the parser stack. This is never used for the
4521 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4522 static void
4523 cp_parser_pop_lexer (cp_parser *parser)
4525 cp_lexer *lexer = parser->lexer;
4526 parser->lexer = lexer->next;
4527 cp_lexer_destroy (lexer);
4529 /* Put the current source position back where it was before this
4530 lexer was pushed. */
4531 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4534 /* Lexical conventions [gram.lex] */
4536 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4537 identifier. */
4539 static cp_expr
4540 cp_parser_identifier (cp_parser* parser)
4542 cp_token *token;
4544 /* Look for the identifier. */
4545 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4546 /* Return the value. */
4547 if (token)
4548 return cp_expr (token->u.value, token->location);
4549 else
4550 return error_mark_node;
4553 /* Worker for cp_parser_string_literal, cp_parser_userdef_string_literal
4554 and cp_parser_unevaluated_string_literal.
4555 Do not call this directly; use either of the above.
4557 Parse a sequence of adjacent string constants. Return a
4558 TREE_STRING representing the combined, nul-terminated string
4559 constant. If TRANSLATE is true, translate the string to the
4560 execution character set. If WIDE_OK is true, a wide string is
4561 valid here. If UDL_OK is true, a string literal with user-defined
4562 suffix can be used in this context. If UNEVAL is true, diagnose
4563 numeric and conditional escape sequences in it if pedantic.
4565 C++98 [lex.string] says that if a narrow string literal token is
4566 adjacent to a wide string literal token, the behavior is undefined.
4567 However, C99 6.4.5p4 says that this results in a wide string literal.
4568 We follow C99 here, for consistency with the C front end.
4570 This code is largely lifted from lex_string() in c-lex.cc.
4572 FUTURE: ObjC++ will need to handle @-strings here. */
4574 static cp_expr
4575 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4576 bool wide_ok, bool udl_ok,
4577 bool lookup_udlit, bool uneval)
4579 tree value;
4580 size_t count;
4581 struct obstack str_ob;
4582 struct obstack loc_ob;
4583 cpp_string str, istr, *strs;
4584 cp_token *tok;
4585 enum cpp_ttype type, curr_type;
4586 int have_suffix_p = 0;
4587 tree string_tree;
4588 tree suffix_id = NULL_TREE;
4589 bool curr_tok_is_userdef_p = false;
4591 tok = cp_lexer_peek_token (parser->lexer);
4592 if (!cp_parser_is_string_literal (tok))
4594 cp_parser_error (parser, "expected string-literal");
4595 return error_mark_node;
4598 location_t loc = tok->location;
4600 if (cpp_userdef_string_p (tok->type))
4602 if (!udl_ok)
4604 error_at (loc, "string literal with user-defined suffix "
4605 "is invalid in this context");
4606 return error_mark_node;
4608 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4609 curr_type = cpp_userdef_string_remove_type (tok->type);
4610 curr_tok_is_userdef_p = true;
4612 else
4614 string_tree = tok->u.value;
4615 curr_type = tok->type;
4617 type = curr_type;
4619 /* Try to avoid the overhead of creating and destroying an obstack
4620 for the common case of just one string. */
4621 if (!cp_parser_is_string_literal
4622 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4624 cp_lexer_consume_token (parser->lexer);
4626 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4627 str.len = TREE_STRING_LENGTH (string_tree);
4628 count = 1;
4630 if (curr_tok_is_userdef_p)
4632 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4633 have_suffix_p = 1;
4634 curr_type = cpp_userdef_string_remove_type (tok->type);
4636 else
4637 curr_type = tok->type;
4639 strs = &str;
4641 else
4643 location_t last_tok_loc = tok->location;
4644 gcc_obstack_init (&str_ob);
4645 gcc_obstack_init (&loc_ob);
4646 count = 0;
4650 cp_lexer_consume_token (parser->lexer);
4651 count++;
4652 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4653 str.len = TREE_STRING_LENGTH (string_tree);
4655 if (curr_tok_is_userdef_p)
4657 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4658 if (have_suffix_p == 0)
4660 suffix_id = curr_suffix_id;
4661 have_suffix_p = 1;
4663 else if (have_suffix_p == 1
4664 && curr_suffix_id != suffix_id)
4666 error ("inconsistent user-defined literal suffixes"
4667 " %qD and %qD in string literal",
4668 suffix_id, curr_suffix_id);
4669 have_suffix_p = -1;
4671 curr_type = cpp_userdef_string_remove_type (tok->type);
4673 else
4674 curr_type = tok->type;
4676 if (type != curr_type)
4678 if (type == CPP_STRING)
4679 type = curr_type;
4680 else if (curr_type != CPP_STRING)
4682 rich_location rich_loc (line_table, tok->location);
4683 rich_loc.add_range (last_tok_loc);
4684 error_at (&rich_loc,
4685 "concatenation of string literals with "
4686 "conflicting encoding prefixes");
4690 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4691 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4693 last_tok_loc = tok->location;
4695 tok = cp_lexer_peek_token (parser->lexer);
4696 if (cpp_userdef_string_p (tok->type))
4698 if (!udl_ok)
4700 error_at (loc, "string literal with user-defined suffix "
4701 "is invalid in this context");
4702 return error_mark_node;
4704 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4705 curr_type = cpp_userdef_string_remove_type (tok->type);
4706 curr_tok_is_userdef_p = true;
4708 else
4710 string_tree = tok->u.value;
4711 curr_type = tok->type;
4712 curr_tok_is_userdef_p = false;
4715 while (cp_parser_is_string_literal (tok));
4717 /* A string literal built by concatenation has its caret=start at
4718 the start of the initial string, and its finish at the finish of
4719 the final string literal. */
4720 loc = make_location (loc, loc, get_finish (last_tok_loc));
4722 strs = (cpp_string *) obstack_finish (&str_ob);
4725 if (type != CPP_STRING && !wide_ok)
4727 cp_parser_error (parser, "a wide string is invalid in this context");
4728 type = CPP_STRING;
4730 if (uneval)
4731 type = CPP_UNEVAL_STRING;
4733 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4734 (parse_in, strs, count, &istr, type))
4736 value = build_string (istr.len, (const char *)istr.text);
4737 free (CONST_CAST (unsigned char *, istr.text));
4738 if (count > 1)
4740 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4741 gcc_assert (g_string_concat_db);
4742 g_string_concat_db->record_string_concatenation (count, locs);
4745 switch (type)
4747 default:
4748 case CPP_STRING:
4749 TREE_TYPE (value) = char_array_type_node;
4750 break;
4751 case CPP_UTF8STRING:
4752 if (flag_char8_t)
4753 TREE_TYPE (value) = char8_array_type_node;
4754 else
4755 TREE_TYPE (value) = char_array_type_node;
4756 break;
4757 case CPP_STRING16:
4758 TREE_TYPE (value) = char16_array_type_node;
4759 break;
4760 case CPP_STRING32:
4761 TREE_TYPE (value) = char32_array_type_node;
4762 break;
4763 case CPP_WSTRING:
4764 TREE_TYPE (value) = wchar_array_type_node;
4765 break;
4768 value = fix_string_type (value);
4770 if (have_suffix_p)
4772 tree literal = build_userdef_literal (suffix_id, value,
4773 OT_NONE, NULL_TREE);
4774 if (lookup_udlit)
4775 value = finish_userdef_string_literal (literal);
4776 else
4777 value = literal;
4780 else
4781 /* cpp_interpret_string has issued an error. */
4782 value = error_mark_node;
4784 if (count > 1)
4786 obstack_free (&str_ob, 0);
4787 obstack_free (&loc_ob, 0);
4790 return cp_expr (value, loc);
4793 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4794 representing the combined, nul-terminated string constant. If
4795 TRANSLATE is true, translate the string to the execution character set.
4796 If WIDE_OK is true, a wide string is valid here.
4798 This function issues an error if a user defined string literal is
4799 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4801 static inline cp_expr
4802 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4804 return cp_parser_string_literal_common (parser, translate, wide_ok,
4805 /*udl_ok=*/false,
4806 /*lookup_udlit=*/false,
4807 /*uneval=*/false);
4810 /* Parse a string literal or user defined string literal.
4812 user-defined-string-literal :
4813 string-literal ud-suffix
4815 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4817 static inline cp_expr
4818 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4820 return cp_parser_string_literal_common (parser, /*translate=*/true,
4821 /*wide_ok=*/true, /*udl_ok=*/true,
4822 lookup_udlit, /*uneval=*/false);
4825 /* Parse an unevaluated string literal.
4827 unevaluated-string:
4828 string-literal */
4830 static inline cp_expr
4831 cp_parser_unevaluated_string_literal (cp_parser *parser)
4833 return cp_parser_string_literal_common (parser, /*translate=*/false,
4834 /*wide_ok=*/false, /*udl_ok=*/false,
4835 /*lookup_udlit=*/false,
4836 /*uneval=*/true);
4839 /* Look up a literal operator with the name and the exact arguments. */
4841 static tree
4842 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4844 tree decl = lookup_name (name);
4845 if (!decl || !is_overloaded_fn (decl))
4846 return error_mark_node;
4848 for (lkp_iterator iter (decl); iter; ++iter)
4850 tree fn = *iter;
4852 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4854 unsigned int ix;
4855 bool found = true;
4857 for (ix = 0;
4858 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4859 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4861 tree tparm = TREE_VALUE (parmtypes);
4862 tree targ = TREE_TYPE ((*args)[ix]);
4863 bool ptr = TYPE_PTR_P (tparm);
4864 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4865 if ((ptr || arr || !same_type_p (tparm, targ))
4866 && (!ptr || !arr
4867 || !same_type_p (TREE_TYPE (tparm),
4868 TREE_TYPE (targ))))
4869 found = false;
4872 if (found
4873 && ix == vec_safe_length (args)
4874 /* May be this should be sufficient_parms_p instead,
4875 depending on how exactly should user-defined literals
4876 work in presence of default arguments on the literal
4877 operator parameters. */
4878 && parmtypes == void_list_node)
4879 return decl;
4883 return error_mark_node;
4886 /* Parse a user-defined char constant. Returns a call to a user-defined
4887 literal operator taking the character as an argument. */
4889 static cp_expr
4890 cp_parser_userdef_char_literal (cp_parser *parser)
4892 cp_token *token = cp_lexer_consume_token (parser->lexer);
4893 tree literal = token->u.value;
4894 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4895 tree value = USERDEF_LITERAL_VALUE (literal);
4896 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4897 tree decl, result;
4899 /* Build up a call to the user-defined operator */
4900 /* Lookup the name we got back from the id-expression. */
4901 releasing_vec args;
4902 vec_safe_push (args, value);
4903 decl = lookup_literal_operator (name, args);
4904 if (!decl || decl == error_mark_node)
4906 error ("unable to find character literal operator %qD with %qT argument",
4907 name, TREE_TYPE (value));
4908 return error_mark_node;
4910 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4911 return result;
4914 /* A subroutine of cp_parser_userdef_numeric_literal to
4915 create a char... template parameter pack from a string node. */
4917 static tree
4918 make_char_string_pack (tree value)
4920 tree charvec;
4921 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4922 const unsigned char *str
4923 = (const unsigned char *) TREE_STRING_POINTER (value);
4924 int i, len = TREE_STRING_LENGTH (value) - 1;
4925 tree argvec = make_tree_vec (1);
4927 /* Fill in CHARVEC with all of the parameters. */
4928 charvec = make_tree_vec (len);
4929 for (i = 0; i < len; ++i)
4931 unsigned char s[3] = { '\'', str[i], '\'' };
4932 cpp_string in = { 3, s };
4933 cpp_string out = { 0, 0 };
4934 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4935 return NULL_TREE;
4936 gcc_assert (out.len == 2);
4937 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4938 out.text[0]);
4941 /* Build the argument packs. */
4942 ARGUMENT_PACK_ARGS (argpack) = charvec;
4944 TREE_VEC_ELT (argvec, 0) = argpack;
4946 return argvec;
4949 /* A subroutine of cp_parser_userdef_numeric_literal to
4950 create a char... template parameter pack from a string node. */
4952 static tree
4953 make_string_pack (tree value)
4955 tree charvec;
4956 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4957 const unsigned char *str
4958 = (const unsigned char *) TREE_STRING_POINTER (value);
4959 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4960 int len = TREE_STRING_LENGTH (value) / sz - 1;
4961 tree argvec = make_tree_vec (2);
4963 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4964 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4966 /* First template parm is character type. */
4967 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4969 /* Fill in CHARVEC with all of the parameters. */
4970 charvec = make_tree_vec (len);
4971 for (int i = 0; i < len; ++i)
4972 TREE_VEC_ELT (charvec, i)
4973 = double_int_to_tree (str_char_type_node,
4974 double_int::from_buffer (str + i * sz, sz));
4976 /* Build the argument packs. */
4977 ARGUMENT_PACK_ARGS (argpack) = charvec;
4979 TREE_VEC_ELT (argvec, 1) = argpack;
4981 return argvec;
4984 /* Parse a user-defined numeric constant. returns a call to a user-defined
4985 literal operator. */
4987 static cp_expr
4988 cp_parser_userdef_numeric_literal (cp_parser *parser)
4990 cp_token *token = cp_lexer_consume_token (parser->lexer);
4991 tree literal = token->u.value;
4992 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4993 tree value = USERDEF_LITERAL_VALUE (literal);
4994 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4995 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4996 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4997 tree decl, result;
4999 /* Look for a literal operator taking the exact type of numeric argument
5000 as the literal value. */
5001 releasing_vec args;
5002 vec_safe_push (args, value);
5003 decl = lookup_literal_operator (name, args);
5004 if (decl && decl != error_mark_node)
5006 result = finish_call_expr (decl, &args, false, true,
5007 tf_warning_or_error);
5009 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
5011 warning_at (token->location, OPT_Woverflow,
5012 "integer literal exceeds range of %qT type",
5013 long_long_unsigned_type_node);
5015 else
5017 if (overflow > 0)
5018 warning_at (token->location, OPT_Woverflow,
5019 "floating literal exceeds range of %qT type",
5020 long_double_type_node);
5021 else if (overflow < 0)
5022 warning_at (token->location, OPT_Woverflow,
5023 "floating literal truncated to zero");
5026 return result;
5029 /* If the numeric argument didn't work, look for a raw literal
5030 operator taking a const char* argument consisting of the number
5031 in string format. */
5032 args->truncate (0);
5033 vec_safe_push (args, num_string);
5034 decl = lookup_literal_operator (name, args);
5035 if (decl && decl != error_mark_node)
5037 result = finish_call_expr (decl, &args, false, true,
5038 tf_warning_or_error);
5039 return result;
5042 /* If the raw literal didn't work, look for a non-type template
5043 function with parameter pack char.... Call the function with
5044 template parameter characters representing the number. */
5045 args->truncate (0);
5046 decl = lookup_literal_operator (name, args);
5047 if (decl && decl != error_mark_node)
5049 tree tmpl_args = make_char_string_pack (num_string);
5050 if (tmpl_args == NULL_TREE)
5052 error ("failed to translate literal to execution character set %qT",
5053 num_string);
5054 return error_mark_node;
5056 decl = lookup_template_function (decl, tmpl_args);
5057 result = finish_call_expr (decl, &args, false, true,
5058 tf_warning_or_error);
5059 return result;
5062 /* In C++14 the standard library defines complex number suffixes that
5063 conflict with GNU extensions. Prefer them if <complex> is #included. */
5064 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
5065 bool i14 = (cxx_dialect > cxx11
5066 && (id_equal (suffix_id, "i")
5067 || id_equal (suffix_id, "if")
5068 || id_equal (suffix_id, "il")));
5069 diagnostic_t kind = DK_ERROR;
5070 int opt = 0;
5072 if (i14 && ext)
5074 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
5075 LOOK_want::NORMAL, false);
5076 if (cxlit == error_mark_node)
5078 /* No <complex>, so pedwarn and use GNU semantics. */
5079 kind = DK_PEDWARN;
5080 opt = OPT_Wpedantic;
5084 bool complained
5085 = emit_diagnostic (kind, input_location, opt,
5086 "unable to find numeric literal operator %qD", name);
5088 if (!complained)
5089 /* Don't inform either. */;
5090 else if (i14)
5092 inform (token->location, "add %<using namespace std::complex_literals%> "
5093 "(from %<<complex>%>) to enable the C++14 user-defined literal "
5094 "suffixes");
5095 if (ext)
5096 inform (token->location, "or use %<j%> instead of %<i%> for the "
5097 "GNU built-in suffix");
5099 else if (!ext)
5100 inform (token->location, "use %<-fext-numeric-literals%> "
5101 "to enable more built-in suffixes");
5103 if (kind == DK_ERROR)
5104 value = error_mark_node;
5105 else
5107 /* Use the built-in semantics. */
5108 tree type;
5109 if (id_equal (suffix_id, "i"))
5111 if (TREE_CODE (value) == INTEGER_CST)
5112 type = integer_type_node;
5113 else
5114 type = double_type_node;
5116 else if (id_equal (suffix_id, "if"))
5117 type = float_type_node;
5118 else /* if (id_equal (suffix_id, "il")) */
5119 type = long_double_type_node;
5121 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5122 build_zero_cst (type), fold_convert (type, value));
5125 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5126 /* Avoid repeated diagnostics. */
5127 token->u.value = value;
5128 return value;
5131 /* Parse a user-defined string constant. Returns a call to a user-defined
5132 literal operator taking a character pointer and the length of the string
5133 as arguments. */
5135 static tree
5136 finish_userdef_string_literal (tree literal)
5138 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5139 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5140 tree value = USERDEF_LITERAL_VALUE (literal);
5141 int len = TREE_STRING_LENGTH (value)
5142 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5143 tree decl;
5145 /* Build up a call to the user-defined operator. */
5146 /* Lookup the name we got back from the id-expression. */
5147 releasing_vec args;
5148 vec_safe_push (args, value);
5149 vec_safe_push (args, build_int_cst (size_type_node, len));
5150 decl = lookup_literal_operator (name, args);
5152 if (decl && decl != error_mark_node)
5153 return finish_call_expr (decl, &args, false, true,
5154 tf_warning_or_error);
5156 /* Look for a suitable template function, either (C++20) with a single
5157 parameter of class type, or (N3599) with typename parameter CharT and
5158 parameter pack CharT... */
5159 args->truncate (0);
5160 decl = lookup_literal_operator (name, args);
5161 if (decl && decl != error_mark_node)
5163 /* Use resolve_nondeduced_context to try to choose one form of template
5164 or the other. */
5165 tree tmpl_args = make_tree_vec (1);
5166 TREE_VEC_ELT (tmpl_args, 0) = value;
5167 decl = lookup_template_function (decl, tmpl_args);
5168 tree res = resolve_nondeduced_context (decl, tf_none);
5169 if (DECL_P (res))
5170 decl = res;
5171 else
5173 TREE_OPERAND (decl, 1) = make_string_pack (value);
5174 res = resolve_nondeduced_context (decl, tf_none);
5175 if (DECL_P (res))
5176 decl = res;
5178 if (!DECL_P (decl) && cxx_dialect > cxx17)
5179 TREE_OPERAND (decl, 1) = tmpl_args;
5180 return finish_call_expr (decl, &args, false, true,
5181 tf_warning_or_error);
5184 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5185 name, TREE_TYPE (value), size_type_node);
5186 return error_mark_node;
5190 /* Basic concepts [gram.basic] */
5192 /* Parse a translation-unit.
5194 translation-unit:
5195 declaration-seq [opt] */
5197 static void
5198 cp_parser_translation_unit (cp_parser* parser)
5200 gcc_checking_assert (!cp_error_declarator);
5202 /* Create the declarator obstack. */
5203 gcc_obstack_init (&declarator_obstack);
5204 /* Create the error declarator. */
5205 cp_error_declarator = make_declarator (cdk_error);
5206 /* Create the empty parameter list. */
5207 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5208 UNKNOWN_LOCATION);
5209 /* Remember where the base of the declarator obstack lies. */
5210 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5212 push_deferring_access_checks (flag_access_control
5213 ? dk_no_deferred : dk_no_check);
5215 module_parse mp_state = MP_NOT_MODULE;
5216 if (modules_p () && !header_module_p ())
5217 mp_state = MP_FIRST;
5219 bool implicit_extern_c = false;
5221 /* Parse until EOF. */
5222 for (;;)
5224 cp_token *token = cp_lexer_peek_token (parser->lexer);
5226 /* If we're entering or exiting a region that's implicitly
5227 extern "C", modify the lang context appropriately. This is
5228 so horrible. Please die. */
5229 if (implicit_extern_c
5230 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5232 implicit_extern_c = !implicit_extern_c;
5233 if (implicit_extern_c)
5234 push_lang_context (lang_name_c);
5235 else
5236 pop_lang_context ();
5239 if (token->type == CPP_EOF)
5240 break;
5242 if (modules_p ())
5244 /* Top-level module declarations are ok, and change the
5245 portion of file we're in. Top-level import declarations
5246 are significant for the import portions. */
5248 cp_token *next = token;
5249 bool exporting = token->keyword == RID__EXPORT;
5250 if (exporting)
5252 cp_lexer_consume_token (parser->lexer);
5253 next = cp_lexer_peek_token (parser->lexer);
5255 if (next->keyword == RID__MODULE)
5257 mp_state
5258 = cp_parser_module_declaration (parser, mp_state, exporting);
5259 continue;
5261 else if (next->keyword == RID__IMPORT)
5263 if (mp_state == MP_FIRST)
5264 mp_state = MP_NOT_MODULE;
5265 cp_parser_import_declaration (parser, mp_state, exporting);
5266 continue;
5268 else
5269 gcc_checking_assert (!exporting);
5271 if (mp_state == MP_GLOBAL && token->main_source_p)
5273 static bool warned = false;
5274 if (!warned)
5276 warned = true;
5277 pedwarn (token->location, OPT_Wglobal_module,
5278 "global module fragment contents must be"
5279 " from preprocessor inclusion");
5284 /* This relies on the ordering of module_parse values. */
5285 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5286 /* We're no longer in the import portion of a named module. */
5287 mp_state = module_parse (mp_state + 1);
5288 else if (mp_state == MP_FIRST)
5289 mp_state = MP_NOT_MODULE;
5291 if (token->type == CPP_CLOSE_BRACE)
5293 cp_parser_error (parser, "expected declaration");
5294 cp_lexer_consume_token (parser->lexer);
5295 /* If the next token is now a `;', consume it. */
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5297 cp_lexer_consume_token (parser->lexer);
5299 else
5300 cp_parser_toplevel_declaration (parser);
5303 /* Get rid of the token array; we don't need it any more. */
5304 cp_lexer_destroy (parser->lexer);
5305 parser->lexer = NULL;
5307 /* The EOF should have reset this. */
5308 gcc_checking_assert (!implicit_extern_c);
5310 /* Make sure the declarator obstack was fully cleaned up. */
5311 gcc_assert (obstack_next_free (&declarator_obstack)
5312 == declarator_obstack_base);
5315 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5316 decltype context. */
5318 static inline tsubst_flags_t
5319 complain_flags (bool decltype_p)
5321 tsubst_flags_t complain = tf_warning_or_error;
5322 if (decltype_p)
5323 complain |= tf_decltype;
5324 return complain;
5327 /* We're about to parse a collection of statements. If we're currently
5328 parsing tentatively, set up a firewall so that any nested
5329 cp_parser_commit_to_tentative_parse won't affect the current context. */
5331 static cp_token_position
5332 cp_parser_start_tentative_firewall (cp_parser *parser)
5334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5335 return 0;
5337 cp_parser_parse_tentatively (parser);
5338 cp_parser_commit_to_topmost_tentative_parse (parser);
5339 return cp_lexer_token_position (parser->lexer, false);
5342 /* We've finished parsing the collection of statements. Wrap up the
5343 firewall and replace the relevant tokens with the parsed form. */
5345 static void
5346 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5347 tree expr)
5349 if (!start)
5350 return;
5352 /* Finish the firewall level. */
5353 cp_parser_parse_definitely (parser);
5354 /* And remember the result of the parse for when we try again. */
5355 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5356 token->type = CPP_PREPARSED_EXPR;
5357 token->u.value = expr;
5358 token->keyword = RID_MAX;
5359 cp_lexer_purge_tokens_after (parser->lexer, start);
5362 /* Like the above functions, but let the user modify the tokens. Used by
5363 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5364 later parses, so it makes sense to localize the effects of
5365 cp_parser_commit_to_tentative_parse. */
5367 struct tentative_firewall
5369 cp_parser *parser;
5370 bool set;
5372 tentative_firewall (cp_parser *p): parser(p)
5374 /* If we're currently parsing tentatively, start a committed level as a
5375 firewall and then an inner tentative parse. */
5376 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5378 cp_parser_parse_tentatively (parser);
5379 cp_parser_commit_to_topmost_tentative_parse (parser);
5380 cp_parser_parse_tentatively (parser);
5384 ~tentative_firewall()
5386 if (set)
5388 /* Finish the inner tentative parse and the firewall, propagating any
5389 uncommitted error state to the outer tentative parse. */
5390 bool err = cp_parser_error_occurred (parser);
5391 cp_parser_parse_definitely (parser);
5392 cp_parser_parse_definitely (parser);
5393 if (err)
5394 cp_parser_simulate_error (parser);
5399 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5400 This class is for tracking such a matching pair of symbols.
5401 In particular, it tracks the location of the first token,
5402 so that if the second token is missing, we can highlight the
5403 location of the first token when notifying the user about the
5404 problem. */
5406 template <typename traits_t>
5407 class token_pair
5409 public:
5410 /* token_pair's ctor. */
5411 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5413 /* If the next token is the opening symbol for this pair, consume it and
5414 return true.
5415 Otherwise, issue an error and return false.
5416 In either case, record the location of the opening token. */
5418 bool require_open (cp_parser *parser)
5420 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5421 return cp_parser_require (parser, traits_t::open_token_type,
5422 traits_t::required_token_open);
5425 /* Consume the next token from PARSER, recording its location as
5426 that of the opening token within the pair. */
5428 cp_token * consume_open (cp_parser *parser)
5430 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5431 gcc_assert (tok->type == traits_t::open_token_type);
5432 m_open_loc = tok->location;
5433 return tok;
5436 /* If the next token is the closing symbol for this pair, consume it
5437 and return it.
5438 Otherwise, issue an error, highlighting the location of the
5439 corresponding opening token, and return NULL. */
5441 cp_token *require_close (cp_parser *parser) const
5443 return cp_parser_require (parser, traits_t::close_token_type,
5444 traits_t::required_token_close,
5445 m_open_loc);
5448 location_t open_location () const { return m_open_loc; }
5450 private:
5451 location_t m_open_loc;
5454 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5456 struct matching_paren_traits
5458 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5459 static const enum required_token required_token_open = RT_OPEN_PAREN;
5460 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5461 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5464 /* "matching_parens" is a token_pair<T> class for tracking matching
5465 pairs of parentheses. */
5467 typedef token_pair<matching_paren_traits> matching_parens;
5469 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5471 struct matching_brace_traits
5473 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5474 static const enum required_token required_token_open = RT_OPEN_BRACE;
5475 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5476 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5479 /* "matching_braces" is a token_pair<T> class for tracking matching
5480 pairs of braces. */
5482 typedef token_pair<matching_brace_traits> matching_braces;
5485 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5486 enclosing parentheses. */
5488 static cp_expr
5489 cp_parser_statement_expr (cp_parser *parser)
5491 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5492 auto oas = make_temp_override (parser->omp_array_section_p, false);
5494 /* Consume the '('. */
5495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5496 matching_parens parens;
5497 parens.consume_open (parser);
5498 /* Start the statement-expression. */
5499 tree expr = begin_stmt_expr ();
5500 /* Parse the compound-statement. */
5501 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5502 /* Finish up. */
5503 expr = finish_stmt_expr (expr, false);
5504 /* Consume the ')'. */
5505 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5506 if (!parens.require_close (parser))
5507 cp_parser_skip_to_end_of_statement (parser);
5509 cp_parser_end_tentative_firewall (parser, start, expr);
5510 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5511 return cp_expr (expr, combined_loc);
5514 /* Expressions [gram.expr] */
5516 /* Parse a fold-operator.
5518 fold-operator:
5519 - * / % ^ & | = < > << >>
5520 = -= *= /= %= ^= &= |= <<= >>=
5521 == != <= >= && || , .* ->*
5523 This returns the tree code corresponding to the matched operator
5524 as an int. When the current token matches a compound assignment
5525 operator, the resulting tree code is the negative value of the
5526 non-assignment operator. */
5528 static int
5529 cp_parser_fold_operator (cp_token *token)
5531 switch (token->type)
5533 case CPP_PLUS: return PLUS_EXPR;
5534 case CPP_MINUS: return MINUS_EXPR;
5535 case CPP_MULT: return MULT_EXPR;
5536 case CPP_DIV: return TRUNC_DIV_EXPR;
5537 case CPP_MOD: return TRUNC_MOD_EXPR;
5538 case CPP_XOR: return BIT_XOR_EXPR;
5539 case CPP_AND: return BIT_AND_EXPR;
5540 case CPP_OR: return BIT_IOR_EXPR;
5541 case CPP_LSHIFT: return LSHIFT_EXPR;
5542 case CPP_RSHIFT: return RSHIFT_EXPR;
5544 case CPP_EQ: return -NOP_EXPR;
5545 case CPP_PLUS_EQ: return -PLUS_EXPR;
5546 case CPP_MINUS_EQ: return -MINUS_EXPR;
5547 case CPP_MULT_EQ: return -MULT_EXPR;
5548 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5549 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5550 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5551 case CPP_AND_EQ: return -BIT_AND_EXPR;
5552 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5553 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5554 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5556 case CPP_EQ_EQ: return EQ_EXPR;
5557 case CPP_NOT_EQ: return NE_EXPR;
5558 case CPP_LESS: return LT_EXPR;
5559 case CPP_GREATER: return GT_EXPR;
5560 case CPP_LESS_EQ: return LE_EXPR;
5561 case CPP_GREATER_EQ: return GE_EXPR;
5563 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5564 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5566 case CPP_COMMA: return COMPOUND_EXPR;
5568 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5569 case CPP_DEREF_STAR: return MEMBER_REF;
5571 default: return ERROR_MARK;
5575 /* Returns true if CODE indicates a binary expression, which is not allowed in
5576 the LHS of a fold-expression. More codes will need to be added to use this
5577 function in other contexts. */
5579 static bool
5580 is_binary_op (tree_code code)
5582 switch (code)
5584 case PLUS_EXPR:
5585 case POINTER_PLUS_EXPR:
5586 case MINUS_EXPR:
5587 case MULT_EXPR:
5588 case TRUNC_DIV_EXPR:
5589 case TRUNC_MOD_EXPR:
5590 case BIT_XOR_EXPR:
5591 case BIT_AND_EXPR:
5592 case BIT_IOR_EXPR:
5593 case LSHIFT_EXPR:
5594 case RSHIFT_EXPR:
5596 case MODOP_EXPR:
5598 case EQ_EXPR:
5599 case NE_EXPR:
5600 case LE_EXPR:
5601 case GE_EXPR:
5602 case LT_EXPR:
5603 case GT_EXPR:
5605 case TRUTH_ANDIF_EXPR:
5606 case TRUTH_ORIF_EXPR:
5608 case COMPOUND_EXPR:
5610 case DOTSTAR_EXPR:
5611 case MEMBER_REF:
5612 return true;
5614 default:
5615 return false;
5619 /* If the next token is a suitable fold operator, consume it and return as
5620 the function above. */
5622 static int
5623 cp_parser_fold_operator (cp_parser *parser)
5625 cp_token* token = cp_lexer_peek_token (parser->lexer);
5626 int code = cp_parser_fold_operator (token);
5627 if (code != ERROR_MARK)
5628 cp_lexer_consume_token (parser->lexer);
5629 return code;
5632 /* Parse a fold-expression.
5634 fold-expression:
5635 ( ... folding-operator cast-expression)
5636 ( cast-expression folding-operator ... )
5637 ( cast-expression folding operator ... folding-operator cast-expression)
5639 Note that the '(' and ')' are matched in primary expression. */
5641 static cp_expr
5642 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5644 cp_id_kind pidk;
5645 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5646 const cp_token *token = nullptr;
5648 // Left fold.
5649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5651 if (expr1)
5652 return error_mark_node;
5653 cp_lexer_consume_token (parser->lexer);
5654 token = cp_lexer_peek_token (parser->lexer);
5655 int op = cp_parser_fold_operator (parser);
5656 if (op == ERROR_MARK)
5658 cp_parser_error (parser, "expected binary operator");
5659 return error_mark_node;
5662 tree expr = cp_parser_cast_expression (parser, false, false,
5663 false, &pidk);
5664 if (expr == error_mark_node)
5665 return error_mark_node;
5666 loc = make_location (token->location, loc, parser->lexer);
5667 return finish_left_unary_fold_expr (loc, expr, op);
5670 token = cp_lexer_peek_token (parser->lexer);
5671 int op = cp_parser_fold_operator (parser);
5672 if (op == ERROR_MARK)
5674 cp_parser_error (parser, "expected binary operator");
5675 return error_mark_node;
5678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5680 cp_parser_error (parser, "expected ...");
5681 return error_mark_node;
5683 cp_lexer_consume_token (parser->lexer);
5685 /* The operands of a fold-expression are cast-expressions, so binary or
5686 conditional expressions are not allowed. We check this here to avoid
5687 tentative parsing. */
5688 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5689 /* OK, the expression was parenthesized. */;
5690 else if (is_binary_op (TREE_CODE (expr1)))
5691 error_at (location_of (expr1),
5692 "binary expression in operand of fold-expression");
5693 else if (TREE_CODE (expr1) == COND_EXPR
5694 || (REFERENCE_REF_P (expr1)
5695 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5696 error_at (location_of (expr1),
5697 "conditional expression in operand of fold-expression");
5699 // Right fold.
5700 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5702 loc = make_location (token->location, loc, parser->lexer);
5703 return finish_right_unary_fold_expr (loc, expr1, op);
5706 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5708 cp_parser_error (parser, "mismatched operator in fold-expression");
5709 return error_mark_node;
5711 cp_lexer_consume_token (parser->lexer);
5713 // Binary left or right fold.
5714 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5715 if (expr2 == error_mark_node)
5716 return error_mark_node;
5717 loc = make_location (token->location, loc, parser->lexer);
5718 return finish_binary_fold_expr (loc, expr1, expr2, op);
5721 /* Parse a primary-expression.
5723 primary-expression:
5724 literal
5725 this
5726 ( expression )
5727 id-expression
5728 lambda-expression (C++11)
5730 GNU Extensions:
5732 primary-expression:
5733 ( compound-statement )
5734 __builtin_va_arg ( assignment-expression , type-id )
5735 __builtin_offsetof ( type-id , offsetof-expression )
5737 C++ Extensions:
5738 __has_nothrow_assign ( type-id )
5739 __has_nothrow_constructor ( type-id )
5740 __has_nothrow_copy ( type-id )
5741 __has_trivial_assign ( type-id )
5742 __has_trivial_constructor ( type-id )
5743 __has_trivial_copy ( type-id )
5744 __has_trivial_destructor ( type-id )
5745 __has_virtual_destructor ( type-id )
5746 __is_abstract ( type-id )
5747 __is_base_of ( type-id , type-id )
5748 __is_class ( type-id )
5749 __is_empty ( type-id )
5750 __is_enum ( type-id )
5751 __is_final ( type-id )
5752 __is_literal_type ( type-id )
5753 __is_pod ( type-id )
5754 __is_polymorphic ( type-id )
5755 __is_std_layout ( type-id )
5756 __is_trivial ( type-id )
5757 __is_union ( type-id )
5759 Objective-C++ Extension:
5761 primary-expression:
5762 objc-expression
5764 literal:
5765 __null
5767 ADDRESS_P is true iff this expression was immediately preceded by
5768 "&" and therefore might denote a pointer-to-member. CAST_P is true
5769 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5770 true iff this expression is a template argument.
5772 Returns a representation of the expression. Upon return, *IDK
5773 indicates what kind of id-expression (if any) was present. */
5775 static cp_expr
5776 cp_parser_primary_expression (cp_parser *parser,
5777 bool address_p,
5778 bool cast_p,
5779 bool template_arg_p,
5780 bool decltype_p,
5781 cp_id_kind *idk)
5783 cp_token *token = NULL;
5785 /* Assume the primary expression is not an id-expression. */
5786 *idk = CP_ID_KIND_NONE;
5788 /* Peek at the next token. */
5789 token = cp_lexer_peek_token (parser->lexer);
5790 switch ((int) token->type)
5792 /* literal:
5793 integer-literal
5794 character-literal
5795 floating-literal
5796 string-literal
5797 boolean-literal
5798 pointer-literal
5799 user-defined-literal */
5800 case CPP_CHAR:
5801 case CPP_CHAR16:
5802 case CPP_CHAR32:
5803 case CPP_WCHAR:
5804 case CPP_UTF8CHAR:
5805 case CPP_NUMBER:
5806 case CPP_PREPARSED_EXPR:
5807 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5808 return cp_parser_userdef_numeric_literal (parser);
5809 token = cp_lexer_consume_token (parser->lexer);
5810 if (TREE_CODE (token->u.value) == FIXED_CST)
5812 error_at (token->location,
5813 "fixed-point types not supported in C++");
5814 return error_mark_node;
5816 /* Floating-point literals are only allowed in an integral
5817 constant expression if they are cast to an integral or
5818 enumeration type. */
5819 if ((TREE_CODE (token->u.value) == REAL_CST
5820 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5821 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5822 && parser->integral_constant_expression_p
5823 && pedantic)
5825 /* CAST_P will be set even in invalid code like "int(2.7 +
5826 ...)". Therefore, we have to check that the next token
5827 is sure to end the cast. */
5828 if (cast_p)
5830 cp_token *next_token;
5832 next_token = cp_lexer_peek_token (parser->lexer);
5833 if (/* The comma at the end of an
5834 enumerator-definition. */
5835 next_token->type != CPP_COMMA
5836 /* The curly brace at the end of an enum-specifier. */
5837 && next_token->type != CPP_CLOSE_BRACE
5838 /* The end of a statement. */
5839 && next_token->type != CPP_SEMICOLON
5840 /* The end of the cast-expression. */
5841 && next_token->type != CPP_CLOSE_PAREN
5842 /* The end of an array bound. */
5843 && next_token->type != CPP_CLOSE_SQUARE
5844 /* The closing ">" in a template-argument-list. */
5845 && (next_token->type != CPP_GREATER
5846 || parser->greater_than_is_operator_p)
5847 /* C++0x only: A ">>" treated like two ">" tokens,
5848 in a template-argument-list. */
5849 && (next_token->type != CPP_RSHIFT
5850 || (cxx_dialect == cxx98)
5851 || parser->greater_than_is_operator_p))
5852 cast_p = false;
5855 /* If we are within a cast, then the constraint that the
5856 cast is to an integral or enumeration type will be
5857 checked at that point. If we are not within a cast, then
5858 this code is invalid. */
5859 if (!cast_p)
5860 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5862 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5863 .maybe_add_location_wrapper ());
5865 case CPP_CHAR_USERDEF:
5866 case CPP_CHAR16_USERDEF:
5867 case CPP_CHAR32_USERDEF:
5868 case CPP_WCHAR_USERDEF:
5869 case CPP_UTF8CHAR_USERDEF:
5870 return cp_parser_userdef_char_literal (parser);
5872 case CPP_STRING:
5873 case CPP_STRING16:
5874 case CPP_STRING32:
5875 case CPP_WSTRING:
5876 case CPP_UTF8STRING:
5877 case CPP_STRING_USERDEF:
5878 case CPP_STRING16_USERDEF:
5879 case CPP_STRING32_USERDEF:
5880 case CPP_WSTRING_USERDEF:
5881 case CPP_UTF8STRING_USERDEF:
5882 /* ??? Should wide strings be allowed when parser->translate_strings_p
5883 is false (i.e. in attributes)? If not, we can kill the third
5884 argument to cp_parser_string_literal. */
5885 if (parser->translate_strings_p)
5886 return (cp_parser_userdef_string_literal (parser,
5887 /*lookup_udlit=*/true)
5888 .maybe_add_location_wrapper ());
5889 else
5890 return (cp_parser_string_literal (parser,
5891 /*translate=*/false,
5892 /*wide_ok=*/true)
5893 .maybe_add_location_wrapper ());
5895 case CPP_OPEN_PAREN:
5896 /* If we see `( { ' then we are looking at the beginning of
5897 a GNU statement-expression. */
5898 if (cp_parser_allow_gnu_extensions_p (parser)
5899 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5901 /* Statement-expressions are not allowed by the standard. */
5902 pedwarn (token->location, OPT_Wpedantic,
5903 "ISO C++ forbids braced-groups within expressions");
5905 /* And they're not allowed outside of a function-body; you
5906 cannot, for example, write:
5908 int i = ({ int j = 3; j + 1; });
5910 at class or namespace scope. */
5911 if (!parser->in_function_body
5912 || parser->in_template_argument_list_p)
5914 error_at (token->location,
5915 "statement-expressions are not allowed outside "
5916 "functions nor in template-argument lists");
5917 cp_parser_skip_to_end_of_block_or_statement (parser);
5918 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5919 cp_lexer_consume_token (parser->lexer);
5920 return error_mark_node;
5922 else
5923 return cp_parser_statement_expr (parser);
5925 /* Otherwise it's a normal parenthesized expression. */
5927 cp_expr expr;
5928 bool saved_greater_than_is_operator_p;
5930 location_t open_paren_loc = token->location;
5932 /* Consume the `('. */
5933 matching_parens parens;
5934 parens.consume_open (parser);
5935 /* Within a parenthesized expression, a `>' token is always
5936 the greater-than operator. */
5937 saved_greater_than_is_operator_p
5938 = parser->greater_than_is_operator_p;
5939 parser->greater_than_is_operator_p = true;
5941 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5942 /* Left fold expression. */
5943 expr = NULL_TREE;
5944 else
5945 /* Parse the parenthesized expression. */
5946 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5948 token = cp_lexer_peek_token (parser->lexer);
5949 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5951 expr = cp_parser_fold_expression (parser, expr);
5952 if (expr != error_mark_node
5953 && cxx_dialect < cxx17)
5954 pedwarn (input_location, OPT_Wc__17_extensions,
5955 "fold-expressions only available with %<-std=c++17%> "
5956 "or %<-std=gnu++17%>");
5958 else
5959 /* Let the front end know that this expression was
5960 enclosed in parentheses. This matters in case, for
5961 example, the expression is of the form `A::B', since
5962 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5963 not. */
5964 expr = finish_parenthesized_expr (expr);
5966 /* DR 705: Wrapping an unqualified name in parentheses
5967 suppresses arg-dependent lookup. We want to pass back
5968 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5969 (c++/37862), but none of the others. */
5970 if (*idk != CP_ID_KIND_QUALIFIED)
5971 *idk = CP_ID_KIND_NONE;
5973 /* The `>' token might be the end of a template-id or
5974 template-parameter-list now. */
5975 parser->greater_than_is_operator_p
5976 = saved_greater_than_is_operator_p;
5978 /* Consume the `)'. */
5979 token = cp_lexer_peek_token (parser->lexer);
5980 location_t close_paren_loc = token->location;
5981 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5982 expr.set_range (open_paren_loc, close_paren_loc);
5983 if (no_wparens)
5984 suppress_warning (expr, OPT_Wparentheses);
5985 if (!parens.require_close (parser)
5986 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5987 cp_parser_skip_to_end_of_statement (parser);
5989 return expr;
5992 case CPP_OPEN_SQUARE:
5994 if (c_dialect_objc ())
5996 /* We might have an Objective-C++ message. */
5997 cp_parser_parse_tentatively (parser);
5998 tree msg = cp_parser_objc_message_expression (parser);
5999 /* If that works out, we're done ... */
6000 if (cp_parser_parse_definitely (parser))
6001 return msg;
6002 /* ... else, fall though to see if it's a lambda. */
6004 cp_expr lam = cp_parser_lambda_expression (parser);
6005 /* Don't warn about a failed tentative parse. */
6006 if (cp_parser_error_occurred (parser))
6007 return error_mark_node;
6008 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
6009 return lam;
6012 case CPP_OBJC_STRING:
6013 if (c_dialect_objc ())
6014 /* We have an Objective-C++ string literal. */
6015 return cp_parser_objc_expression (parser);
6016 cp_parser_error (parser, "expected primary-expression");
6017 return error_mark_node;
6019 case CPP_KEYWORD:
6020 switch (token->keyword)
6022 /* These two are the boolean literals. */
6023 case RID_TRUE:
6024 cp_lexer_consume_token (parser->lexer);
6025 return cp_expr (boolean_true_node, token->location);
6026 case RID_FALSE:
6027 cp_lexer_consume_token (parser->lexer);
6028 return cp_expr (boolean_false_node, token->location);
6030 /* The `__null' literal. */
6031 case RID_NULL:
6032 cp_lexer_consume_token (parser->lexer);
6033 return cp_expr (null_node, token->location);
6035 /* The `nullptr' literal. */
6036 case RID_NULLPTR:
6037 cp_lexer_consume_token (parser->lexer);
6038 return cp_expr (nullptr_node, token->location);
6040 /* Recognize the `this' keyword. */
6041 case RID_THIS:
6042 cp_lexer_consume_token (parser->lexer);
6043 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
6045 error_at (token->location,
6046 "%<this%> may not be used in this context");
6047 return error_mark_node;
6049 /* Pointers cannot appear in constant-expressions. */
6050 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
6051 return error_mark_node;
6052 return cp_expr (finish_this_expr (), token->location);
6054 /* The `operator' keyword can be the beginning of an
6055 id-expression. */
6056 case RID_OPERATOR:
6057 goto id_expression;
6059 case RID_FUNCTION_NAME:
6060 case RID_PRETTY_FUNCTION_NAME:
6061 case RID_C99_FUNCTION_NAME:
6063 non_integral_constant name;
6065 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
6066 __func__ are the names of variables -- but they are
6067 treated specially. Therefore, they are handled here,
6068 rather than relying on the generic id-expression logic
6069 below. Grammatically, these names are id-expressions.
6071 Consume the token. */
6072 token = cp_lexer_consume_token (parser->lexer);
6074 switch (token->keyword)
6076 case RID_FUNCTION_NAME:
6077 name = NIC_FUNC_NAME;
6078 break;
6079 case RID_PRETTY_FUNCTION_NAME:
6080 name = NIC_PRETTY_FUNC;
6081 break;
6082 case RID_C99_FUNCTION_NAME:
6083 name = NIC_C99_FUNC;
6084 break;
6085 default:
6086 gcc_unreachable ();
6089 if (cp_parser_non_integral_constant_expression (parser, name))
6090 return error_mark_node;
6092 /* Look up the name. */
6093 return finish_fname (token->u.value);
6096 case RID_VA_ARG:
6098 tree expression;
6099 tree type;
6100 location_t type_location;
6101 location_t start_loc
6102 = cp_lexer_peek_token (parser->lexer)->location;
6103 /* The `__builtin_va_arg' construct is used to handle
6104 `va_arg'. Consume the `__builtin_va_arg' token. */
6105 cp_lexer_consume_token (parser->lexer);
6106 /* Look for the opening `('. */
6107 matching_parens parens;
6108 parens.require_open (parser);
6109 /* Now, parse the assignment-expression. */
6110 expression = cp_parser_assignment_expression (parser);
6111 /* Look for the `,'. */
6112 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
6113 type_location = cp_lexer_peek_token (parser->lexer)->location;
6114 /* Parse the type-id. */
6116 type_id_in_expr_sentinel s (parser);
6117 type = cp_parser_type_id (parser);
6119 /* Look for the closing `)'. */
6120 location_t finish_loc
6121 = cp_lexer_peek_token (parser->lexer)->location;
6122 parens.require_close (parser);
6123 /* Using `va_arg' in a constant-expression is not
6124 allowed. */
6125 if (cp_parser_non_integral_constant_expression (parser,
6126 NIC_VA_ARG))
6127 return error_mark_node;
6128 /* Construct a location of the form:
6129 __builtin_va_arg (v, int)
6130 ~~~~~~~~~~~~~~~~~~~~~^~~~
6131 with the caret at the type, ranging from the start of the
6132 "__builtin_va_arg" token to the close paren. */
6133 location_t combined_loc
6134 = make_location (type_location, start_loc, finish_loc);
6135 return build_x_va_arg (combined_loc, expression, type);
6138 case RID_OFFSETOF:
6139 return cp_parser_builtin_offsetof (parser);
6141 // C++ concepts
6142 case RID_REQUIRES:
6143 return cp_parser_requires_expression (parser);
6145 /* Objective-C++ expressions. */
6146 case RID_AT_ENCODE:
6147 case RID_AT_PROTOCOL:
6148 case RID_AT_SELECTOR:
6149 return cp_parser_objc_expression (parser);
6151 case RID_OMP_ALL_MEMORY:
6152 gcc_assert (flag_openmp);
6153 cp_lexer_consume_token (parser->lexer);
6154 error_at (token->location,
6155 "%<omp_all_memory%> may only be used in OpenMP "
6156 "%<depend%> clause");
6157 return error_mark_node;
6159 case RID_TEMPLATE:
6160 if (parser->in_function_body
6161 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6162 == CPP_LESS))
6164 error_at (token->location,
6165 "a template declaration cannot appear at block scope");
6166 cp_parser_skip_to_end_of_block_or_statement (parser);
6167 return error_mark_node;
6169 /* FALLTHRU */
6170 default:
6171 cp_parser_error (parser, "expected primary-expression");
6172 return error_mark_node;
6175 /* An id-expression can start with either an identifier, a
6176 `::' as the beginning of a qualified-id, or the "operator"
6177 keyword. */
6178 case CPP_NAME:
6179 if (const cp_trait* trait = cp_lexer_peek_trait_expr (parser->lexer))
6180 return cp_parser_trait (parser, trait);
6181 /* FALLTHRU */
6182 case CPP_SCOPE:
6183 case CPP_TEMPLATE_ID:
6184 case CPP_NESTED_NAME_SPECIFIER:
6186 id_expression:
6187 cp_expr id_expression;
6188 cp_expr decl;
6189 const char *error_msg;
6190 bool template_p;
6191 bool done;
6192 cp_token *id_expr_token;
6194 /* Parse the id-expression. */
6195 id_expression
6196 = cp_parser_id_expression (parser,
6197 /*template_keyword_p=*/false,
6198 /*check_dependency_p=*/true,
6199 &template_p,
6200 /*declarator_p=*/false,
6201 /*optional_p=*/false);
6202 if (id_expression == error_mark_node)
6203 return error_mark_node;
6204 id_expr_token = token;
6205 token = cp_lexer_peek_token (parser->lexer);
6206 done = (token->type != CPP_OPEN_SQUARE
6207 && token->type != CPP_OPEN_PAREN
6208 && token->type != CPP_DOT
6209 && token->type != CPP_DEREF
6210 && token->type != CPP_PLUS_PLUS
6211 && token->type != CPP_MINUS_MINUS);
6212 /* If we have a template-id, then no further lookup is
6213 required. If the template-id was for a template-class, we
6214 will sometimes have a TYPE_DECL at this point. */
6215 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6216 || TREE_CODE (id_expression) == TYPE_DECL)
6217 decl = id_expression;
6218 /* Look up the name. */
6219 else
6221 tree ambiguous_decls;
6223 /* If we already know that this lookup is ambiguous, then
6224 we've already issued an error message; there's no reason
6225 to check again. */
6226 if (id_expr_token->type == CPP_NAME
6227 && id_expr_token->error_reported)
6229 cp_parser_simulate_error (parser);
6230 return error_mark_node;
6233 decl = cp_parser_lookup_name (parser, id_expression,
6234 none_type,
6235 template_p,
6236 /*is_namespace=*/false,
6237 /*check_dependency=*/true,
6238 &ambiguous_decls,
6239 id_expression.get_location ());
6240 /* If the lookup was ambiguous, an error will already have
6241 been issued. */
6242 if (ambiguous_decls)
6243 return error_mark_node;
6245 /* In Objective-C++, we may have an Objective-C 2.0
6246 dot-syntax for classes here. */
6247 if (c_dialect_objc ()
6248 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6249 && TREE_CODE (decl) == TYPE_DECL
6250 && objc_is_class_name (decl))
6252 tree component;
6253 cp_lexer_consume_token (parser->lexer);
6254 component = cp_parser_identifier (parser);
6255 if (component == error_mark_node)
6256 return error_mark_node;
6258 tree result = objc_build_class_component_ref (id_expression,
6259 component);
6260 /* Build a location of the form:
6261 expr.component
6262 ~~~~~^~~~~~~~~
6263 with caret at the start of the component name (at
6264 input_location), ranging from the start of the id_expression
6265 to the end of the component name. */
6266 location_t combined_loc
6267 = make_location (input_location, id_expression.get_start (),
6268 get_finish (input_location));
6269 protected_set_expr_location (result, combined_loc);
6270 return result;
6273 /* In Objective-C++, an instance variable (ivar) may be preferred
6274 to whatever cp_parser_lookup_name() found.
6275 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6276 rest of c-family, we have to do a little extra work to preserve
6277 any location information in cp_expr "decl". Given that
6278 objc_lookup_ivar is implemented in "c-family" and "objc", we
6279 have a trip through the pure "tree" type, rather than cp_expr.
6280 Naively copying it back to "decl" would implicitly give the
6281 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6282 store an EXPR_LOCATION. Hence we only update "decl" (and
6283 hence its location_t) if we get back a different tree node. */
6284 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6285 id_expression);
6286 if (decl_tree != decl.get_value ())
6287 decl = cp_expr (decl_tree);
6289 /* If name lookup gives us a SCOPE_REF, then the
6290 qualifying scope was dependent. */
6291 if (TREE_CODE (decl) == SCOPE_REF)
6293 /* At this point, we do not know if DECL is a valid
6294 integral constant expression. We assume that it is
6295 in fact such an expression, so that code like:
6297 template <int N> struct A {
6298 int a[B<N>::i];
6301 is accepted. At template-instantiation time, we
6302 will check that B<N>::i is actually a constant. */
6303 return decl;
6305 /* Check to see if DECL is a local variable in a context
6306 where that is forbidden. */
6307 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6308 && local_variable_p (decl)
6309 /* DR 2082 permits local variables in unevaluated contexts
6310 within a default argument. */
6311 && !cp_unevaluated_operand)
6313 const char *msg
6314 = (TREE_CODE (decl) == PARM_DECL
6315 ? G_("parameter %qD may not appear in this context")
6316 : G_("local variable %qD may not appear in this context"));
6317 error_at (id_expression.get_location (), msg,
6318 decl.get_value ());
6319 return error_mark_node;
6323 decl = (finish_id_expression
6324 (id_expression, decl, parser->scope,
6325 idk,
6326 parser->integral_constant_expression_p,
6327 parser->allow_non_integral_constant_expression_p,
6328 &parser->non_integral_constant_expression_p,
6329 template_p, done, address_p,
6330 template_arg_p,
6331 &error_msg,
6332 id_expression.get_location ()));
6333 if (error_msg)
6334 cp_parser_error (parser, error_msg);
6335 /* Build a location for an id-expression of the form:
6336 ::ns::id
6337 ~~~~~~^~
6341 i.e. from the start of the first token to the end of the final
6342 token, with the caret at the start of the unqualified-id. */
6343 location_t caret_loc = get_pure_location (id_expression.get_location ());
6344 location_t start_loc = get_start (id_expr_token->location);
6345 location_t finish_loc = get_finish (id_expression.get_location ());
6346 location_t combined_loc
6347 = make_location (caret_loc, start_loc, finish_loc);
6349 decl.set_location (combined_loc);
6350 return decl;
6353 /* Anything else is an error. */
6354 default:
6355 cp_parser_error (parser, "expected primary-expression");
6356 return error_mark_node;
6360 static inline cp_expr
6361 cp_parser_primary_expression (cp_parser *parser,
6362 bool address_p,
6363 bool cast_p,
6364 bool template_arg_p,
6365 cp_id_kind *idk)
6367 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6368 /*decltype*/false, idk);
6371 /* Complain about missing template keyword when naming a dependent
6372 member template. */
6374 static void
6375 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6377 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6378 return;
6380 gcc_rich_location richloc (loc);
6381 richloc.add_fixit_insert_before ("template");
6382 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6383 "expected %qs keyword before dependent "
6384 "template name", "template");
6385 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6388 /* Parse an id-expression.
6390 id-expression:
6391 unqualified-id
6392 qualified-id
6394 qualified-id:
6395 :: [opt] nested-name-specifier template [opt] unqualified-id
6396 :: identifier
6397 :: operator-function-id
6398 :: template-id
6400 Return a representation of the unqualified portion of the
6401 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6402 a `::' or nested-name-specifier.
6404 Often, if the id-expression was a qualified-id, the caller will
6405 want to make a SCOPE_REF to represent the qualified-id. This
6406 function does not do this in order to avoid wastefully creating
6407 SCOPE_REFs when they are not required.
6409 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6410 `template' keyword.
6412 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6413 uninstantiated templates.
6415 If *TEMPLATE_P is non-NULL, it is set to true iff the
6416 `template' keyword is used to explicitly indicate that the entity
6417 named is a template.
6419 If DECLARATOR_P is true, the id-expression is appearing as part of
6420 a declarator, rather than as part of an expression. */
6422 static cp_expr
6423 cp_parser_id_expression (cp_parser *parser,
6424 bool template_keyword_p,
6425 bool check_dependency_p,
6426 bool *template_p,
6427 bool declarator_p,
6428 bool optional_p)
6430 bool global_scope_p;
6431 bool nested_name_specifier_p;
6433 /* Assume the `template' keyword was not used. */
6434 if (template_p)
6435 *template_p = template_keyword_p;
6437 /* Look for the optional `::' operator. */
6438 global_scope_p
6439 = (!template_keyword_p
6440 && (cp_parser_global_scope_opt (parser,
6441 /*current_scope_valid_p=*/false)
6442 != NULL_TREE));
6444 /* Look for the optional nested-name-specifier. */
6445 nested_name_specifier_p
6446 = (cp_parser_nested_name_specifier_opt (parser,
6447 /*typename_keyword_p=*/false,
6448 check_dependency_p,
6449 /*type_p=*/false,
6450 declarator_p,
6451 template_keyword_p)
6452 != NULL_TREE);
6454 cp_expr id = NULL_TREE;
6455 tree scope = parser->scope;
6457 /* Peek at the next token. */
6458 cp_token *token = cp_lexer_peek_token (parser->lexer);
6460 /* If there is a nested-name-specifier, then we are looking at
6461 the first qualified-id production. */
6462 if (nested_name_specifier_p)
6464 tree saved_object_scope;
6465 tree saved_qualifying_scope;
6467 /* See if the next token is the `template' keyword. */
6468 if (!template_p)
6469 template_p = &template_keyword_p;
6470 *template_p = cp_parser_optional_template_keyword (parser);
6471 /* Name lookup we do during the processing of the
6472 unqualified-id might obliterate SCOPE. */
6473 saved_object_scope = parser->object_scope;
6474 saved_qualifying_scope = parser->qualifying_scope;
6475 /* Process the final unqualified-id. */
6476 id = cp_parser_unqualified_id (parser, *template_p,
6477 check_dependency_p,
6478 declarator_p,
6479 /*optional_p=*/false);
6480 /* Restore the SAVED_SCOPE for our caller. */
6481 parser->scope = scope;
6482 parser->object_scope = saved_object_scope;
6483 parser->qualifying_scope = saved_qualifying_scope;
6485 /* Otherwise, if we are in global scope, then we are looking at one
6486 of the other qualified-id productions. */
6487 else if (global_scope_p)
6489 /* If it's an identifier, and the next token is not a "<", then
6490 we can avoid the template-id case. This is an optimization
6491 for this common case. */
6492 if (token->type == CPP_NAME
6493 && !cp_parser_nth_token_starts_template_argument_list_p
6494 (parser, 2))
6495 return cp_parser_identifier (parser);
6497 cp_parser_parse_tentatively (parser);
6498 /* Try a template-id. */
6499 id = cp_parser_template_id_expr (parser,
6500 /*template_keyword_p=*/false,
6501 /*check_dependency_p=*/true,
6502 declarator_p);
6503 /* If that worked, we're done. */
6504 if (cp_parser_parse_definitely (parser))
6505 return id;
6507 /* Peek at the next token. (Changes in the token buffer may
6508 have invalidated the pointer obtained above.) */
6509 token = cp_lexer_peek_token (parser->lexer);
6511 switch (token->type)
6513 case CPP_NAME:
6514 id = cp_parser_identifier (parser);
6515 break;
6517 case CPP_KEYWORD:
6518 if (token->keyword == RID_OPERATOR)
6520 id = cp_parser_operator_function_id (parser);
6521 break;
6523 /* Fall through. */
6525 default:
6526 cp_parser_error (parser, "expected id-expression");
6527 return error_mark_node;
6530 else
6532 if (!scope)
6533 scope = parser->context->object_type;
6534 id = cp_parser_unqualified_id (parser, template_keyword_p,
6535 /*check_dependency_p=*/true,
6536 declarator_p,
6537 optional_p);
6540 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6541 && warn_missing_template_keyword
6542 && !template_keyword_p
6543 /* Don't warn if we're looking inside templates. */
6544 && check_dependency_p
6545 /* In a template argument list a > could be closing
6546 the enclosing targs. */
6547 && !parser->in_template_argument_list_p
6548 && scope && dependentish_scope_p (scope)
6549 /* Don't confuse an ill-formed constructor declarator for a missing
6550 template keyword in a return type. */
6551 && !(declarator_p && constructor_name_p (id, scope))
6552 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6553 && warning_enabled_at (token->location,
6554 OPT_Wmissing_template_keyword))
6556 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6557 if (cp_parser_skip_entire_template_parameter_list (parser)
6558 /* An operator after the > suggests that the > ends a
6559 template-id; a name or literal suggests that the > is an
6560 operator. */
6561 && (cp_lexer_peek_token (parser->lexer)->type
6562 <= CPP_LAST_PUNCTUATOR))
6563 missing_template_diag (token->location);
6566 return id;
6569 /* Parse an unqualified-id.
6571 unqualified-id:
6572 identifier
6573 operator-function-id
6574 conversion-function-id
6575 ~ class-name
6576 template-id
6578 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6579 keyword, in a construct like `A::template ...'.
6581 Returns a representation of unqualified-id. For the `identifier'
6582 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6583 production a BIT_NOT_EXPR is returned; the operand of the
6584 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6585 other productions, see the documentation accompanying the
6586 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6587 names are looked up in uninstantiated templates. If DECLARATOR_P
6588 is true, the unqualified-id is appearing as part of a declarator,
6589 rather than as part of an expression. */
6591 static cp_expr
6592 cp_parser_unqualified_id (cp_parser* parser,
6593 bool template_keyword_p,
6594 bool check_dependency_p,
6595 bool declarator_p,
6596 bool optional_p)
6598 cp_token *token;
6600 /* Peek at the next token. */
6601 token = cp_lexer_peek_token (parser->lexer);
6603 switch ((int) token->type)
6605 case CPP_NAME:
6607 tree id;
6609 /* We don't know yet whether or not this will be a
6610 template-id. */
6611 cp_parser_parse_tentatively (parser);
6612 /* Try a template-id. */
6613 id = cp_parser_template_id_expr (parser, template_keyword_p,
6614 check_dependency_p,
6615 declarator_p);
6616 /* If it worked, we're done. */
6617 if (cp_parser_parse_definitely (parser))
6618 return id;
6619 /* Otherwise, it's an ordinary identifier. */
6620 return cp_parser_identifier (parser);
6623 case CPP_TEMPLATE_ID:
6624 return cp_parser_template_id_expr (parser, template_keyword_p,
6625 check_dependency_p,
6626 declarator_p);
6628 case CPP_COMPL:
6630 tree type_decl;
6631 tree qualifying_scope;
6632 tree object_scope;
6633 tree scope;
6634 bool done;
6635 location_t tilde_loc = token->location;
6637 /* Consume the `~' token. */
6638 cp_lexer_consume_token (parser->lexer);
6639 /* Parse the class-name. The standard, as written, seems to
6640 say that:
6642 template <typename T> struct S { ~S (); };
6643 template <typename T> S<T>::~S() {}
6645 is invalid, since `~' must be followed by a class-name, but
6646 `S<T>' is dependent, and so not known to be a class.
6647 That's not right; we need to look in uninstantiated
6648 templates. A further complication arises from:
6650 template <typename T> void f(T t) {
6651 t.T::~T();
6654 Here, it is not possible to look up `T' in the scope of `T'
6655 itself. We must look in both the current scope, and the
6656 scope of the containing complete expression.
6658 Yet another issue is:
6660 struct S {
6661 int S;
6662 ~S();
6665 S::~S() {}
6667 The standard does not seem to say that the `S' in `~S'
6668 should refer to the type `S' and not the data member
6669 `S::S'. */
6671 /* DR 244 says that we look up the name after the "~" in the
6672 same scope as we looked up the qualifying name. That idea
6673 isn't fully worked out; it's more complicated than that. */
6674 scope = parser->scope;
6675 object_scope = parser->object_scope;
6676 qualifying_scope = parser->qualifying_scope;
6678 /* Check for invalid scopes. */
6679 if (scope == error_mark_node)
6681 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6682 cp_lexer_consume_token (parser->lexer);
6683 return error_mark_node;
6685 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6687 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6688 error_at (token->location,
6689 "scope %qT before %<~%> is not a class-name",
6690 scope);
6691 cp_parser_simulate_error (parser);
6692 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6693 cp_lexer_consume_token (parser->lexer);
6694 return error_mark_node;
6696 if (template_keyword_p)
6698 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6699 error_at (tilde_loc, "%<template%> keyword not permitted in "
6700 "destructor name");
6701 cp_parser_simulate_error (parser);
6702 return error_mark_node;
6705 gcc_assert (!scope || TYPE_P (scope));
6707 token = cp_lexer_peek_token (parser->lexer);
6709 /* Create a location with caret == start at the tilde,
6710 finishing at the end of the peeked token, e.g:
6711 ~token
6712 ^~~~~~. */
6713 location_t loc
6714 = make_location (tilde_loc, tilde_loc, token->location);
6716 /* If the name is of the form "X::~X" it's OK even if X is a
6717 typedef. */
6719 if (scope
6720 && token->type == CPP_NAME
6721 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6722 != CPP_LESS)
6723 && (token->u.value == TYPE_IDENTIFIER (scope)
6724 || (CLASS_TYPE_P (scope)
6725 && constructor_name_p (token->u.value, scope))))
6727 cp_lexer_consume_token (parser->lexer);
6728 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6731 /* ~auto means the destructor of whatever the object is. */
6732 if (cp_parser_is_keyword (token, RID_AUTO))
6734 if (cxx_dialect < cxx14)
6735 pedwarn (loc, OPT_Wc__14_extensions,
6736 "%<~auto%> only available with "
6737 "%<-std=c++14%> or %<-std=gnu++14%>");
6738 cp_lexer_consume_token (parser->lexer);
6739 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6742 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6743 declarator-id of a constructor or destructor. */
6744 if (token->type == CPP_TEMPLATE_ID && declarator_p)
6746 auto_diagnostic_group d;
6747 bool w = false;
6748 if (cxx_dialect >= cxx20 && !cp_parser_simulate_error (parser))
6749 w = pedwarn (tilde_loc, OPT_Wtemplate_id_cdtor,
6750 "template-id not allowed for destructor in C++20");
6751 else if (cxx_dialect < cxx20
6752 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6753 w = warning_at (tilde_loc, OPT_Wtemplate_id_cdtor,
6754 "template-id not allowed for destructor in C++20");
6755 if (w)
6756 inform (tilde_loc, "remove the %qs", "< >");
6759 /* If there was an explicit qualification (S::~T), first look
6760 in the scope given by the qualification (i.e., S).
6762 Note: in the calls to cp_parser_class_name below we pass
6763 typename_type so that lookup finds the injected-class-name
6764 rather than the constructor. */
6765 done = false;
6766 type_decl = NULL_TREE;
6767 if (scope)
6769 cp_parser_parse_tentatively (parser);
6770 type_decl = cp_parser_class_name (parser,
6771 /*typename_keyword_p=*/false,
6772 /*template_keyword_p=*/false,
6773 typename_type,
6774 /*check_dependency=*/false,
6775 /*class_head_p=*/false,
6776 declarator_p);
6777 if (cp_parser_parse_definitely (parser))
6778 done = true;
6780 /* In "N::S::~S", look in "N" as well. */
6781 if (!done && scope && qualifying_scope)
6783 cp_parser_parse_tentatively (parser);
6784 parser->scope = qualifying_scope;
6785 parser->object_scope = NULL_TREE;
6786 parser->qualifying_scope = NULL_TREE;
6787 type_decl
6788 = cp_parser_class_name (parser,
6789 /*typename_keyword_p=*/false,
6790 /*template_keyword_p=*/false,
6791 typename_type,
6792 /*check_dependency=*/false,
6793 /*class_head_p=*/false,
6794 declarator_p);
6795 if (cp_parser_parse_definitely (parser))
6796 done = true;
6798 /* In "p->S::~T", look in the scope given by "*p" as well. */
6799 else if (!done && object_scope)
6801 cp_parser_parse_tentatively (parser);
6802 parser->scope = object_scope;
6803 parser->object_scope = NULL_TREE;
6804 parser->qualifying_scope = NULL_TREE;
6805 type_decl
6806 = cp_parser_class_name (parser,
6807 /*typename_keyword_p=*/false,
6808 /*template_keyword_p=*/false,
6809 typename_type,
6810 /*check_dependency=*/false,
6811 /*class_head_p=*/false,
6812 declarator_p);
6813 if (cp_parser_parse_definitely (parser))
6814 done = true;
6816 /* Look in the surrounding context. */
6817 if (!done)
6819 parser->scope = NULL_TREE;
6820 parser->object_scope = NULL_TREE;
6821 parser->qualifying_scope = NULL_TREE;
6822 if (processing_template_decl)
6823 cp_parser_parse_tentatively (parser);
6824 type_decl
6825 = cp_parser_class_name (parser,
6826 /*typename_keyword_p=*/false,
6827 /*template_keyword_p=*/false,
6828 typename_type,
6829 /*check_dependency=*/false,
6830 /*class_head_p=*/false,
6831 declarator_p);
6832 if (processing_template_decl
6833 && ! cp_parser_parse_definitely (parser))
6835 /* We couldn't find a type with this name. If we're parsing
6836 tentatively, fail and try something else. */
6837 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6839 cp_parser_simulate_error (parser);
6840 return error_mark_node;
6842 /* Otherwise, accept it and check for a match at instantiation
6843 time. */
6844 type_decl = cp_parser_identifier (parser);
6845 if (type_decl != error_mark_node)
6846 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6847 return type_decl;
6850 /* If an error occurred, assume that the name of the
6851 destructor is the same as the name of the qualifying
6852 class. That allows us to keep parsing after running
6853 into ill-formed destructor names. */
6854 if (type_decl == error_mark_node && scope)
6855 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6856 else if (type_decl == error_mark_node)
6857 return error_mark_node;
6859 /* Check that destructor name and scope match. */
6860 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6862 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6863 error_at (loc,
6864 "declaration of %<~%T%> as member of %qT",
6865 type_decl, scope);
6866 cp_parser_simulate_error (parser);
6867 return error_mark_node;
6870 /* [class.dtor]
6872 A typedef-name that names a class shall not be used as the
6873 identifier in the declarator for a destructor declaration. */
6874 if (declarator_p
6875 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6876 && !DECL_SELF_REFERENCE_P (type_decl)
6877 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6878 error_at (loc,
6879 "typedef-name %qD used as destructor declarator",
6880 type_decl);
6882 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6885 case CPP_KEYWORD:
6886 if (token->keyword == RID_OPERATOR)
6888 cp_expr id;
6890 /* This could be a template-id, so we try that first. */
6891 cp_parser_parse_tentatively (parser);
6892 /* Try a template-id. */
6893 id = cp_parser_template_id_expr (parser, template_keyword_p,
6894 /*check_dependency_p=*/true,
6895 declarator_p);
6896 /* If that worked, we're done. */
6897 if (cp_parser_parse_definitely (parser))
6898 return id;
6899 /* We still don't know whether we're looking at an
6900 operator-function-id or a conversion-function-id. */
6901 cp_parser_parse_tentatively (parser);
6902 /* Try an operator-function-id. */
6903 id = cp_parser_operator_function_id (parser);
6904 /* If that didn't work, try a conversion-function-id. */
6905 if (!cp_parser_parse_definitely (parser))
6906 id = cp_parser_conversion_function_id (parser);
6908 return id;
6910 /* Fall through. */
6912 default:
6913 if (optional_p)
6914 return NULL_TREE;
6915 cp_parser_error (parser, "expected unqualified-id");
6916 return error_mark_node;
6920 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6921 be a template-id or the name shall refer to a class template or an
6922 alias template. */
6924 static void
6925 check_template_keyword_in_nested_name_spec (tree name)
6927 if (CLASS_TYPE_P (name)
6928 && ((CLASSTYPE_USE_TEMPLATE (name)
6929 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6930 || CLASSTYPE_IS_TEMPLATE (name)))
6931 return;
6933 if (TREE_CODE (name) == TYPENAME_TYPE
6934 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6935 return;
6936 /* Alias templates are also OK. */
6937 else if (alias_template_specialization_p (name, nt_opaque))
6938 return;
6940 permerror (input_location, TYPE_P (name)
6941 ? G_("%qT is not a template")
6942 : G_("%qD is not a template"),
6943 name);
6946 /* Parse an (optional) nested-name-specifier.
6948 nested-name-specifier: [C++98]
6949 class-or-namespace-name :: nested-name-specifier [opt]
6950 class-or-namespace-name :: template nested-name-specifier [opt]
6952 nested-name-specifier: [C++0x]
6953 type-name ::
6954 namespace-name ::
6955 nested-name-specifier identifier ::
6956 nested-name-specifier template [opt] simple-template-id ::
6958 PARSER->SCOPE should be set appropriately before this function is
6959 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6960 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6961 in name lookups.
6963 Sets PARSER->SCOPE to the class (TYPE) or namespace
6964 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6965 it unchanged if there is no nested-name-specifier. Returns the new
6966 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6968 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6970 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6971 part of a declaration and/or decl-specifier. */
6973 static tree
6974 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6975 bool typename_keyword_p,
6976 bool check_dependency_p,
6977 bool type_p,
6978 bool is_declaration,
6979 bool template_keyword_p /* = false */)
6981 bool success = false;
6982 cp_token_position start = 0;
6983 cp_token *token;
6985 /* Remember where the nested-name-specifier starts. */
6986 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6987 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6989 start = cp_lexer_token_position (parser->lexer, false);
6990 push_deferring_access_checks (dk_deferred);
6993 while (true)
6995 tree new_scope;
6996 tree old_scope;
6997 tree saved_qualifying_scope;
6999 /* Spot cases that cannot be the beginning of a
7000 nested-name-specifier. */
7001 token = cp_lexer_peek_token (parser->lexer);
7003 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
7004 the already parsed nested-name-specifier. */
7005 if (token->type == CPP_NESTED_NAME_SPECIFIER)
7007 /* Grab the nested-name-specifier and continue the loop. */
7008 cp_parser_pre_parsed_nested_name_specifier (parser);
7009 /* If we originally encountered this nested-name-specifier
7010 with CHECK_DEPENDENCY_P set to true, we will not have
7011 resolved TYPENAME_TYPEs, so we must do so here. */
7012 if (is_declaration
7013 && !check_dependency_p
7014 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7016 new_scope = resolve_typename_type (parser->scope,
7017 /*only_current_p=*/false);
7018 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
7019 parser->scope = new_scope;
7021 success = true;
7022 continue;
7025 /* Spot cases that cannot be the beginning of a
7026 nested-name-specifier. On the second and subsequent times
7027 through the loop, we look for the `template' keyword. */
7028 if (success && token->keyword == RID_TEMPLATE)
7030 /* A template-id can start a nested-name-specifier. */
7031 else if (token->type == CPP_TEMPLATE_ID)
7033 /* DR 743: decltype can be used in a nested-name-specifier. */
7034 else if (token_is_decltype (token))
7036 else
7038 /* If the next token is not an identifier, then it is
7039 definitely not a type-name or namespace-name. */
7040 if (token->type != CPP_NAME)
7041 break;
7042 /* If the following token is neither a `<' (to begin a
7043 template-id), nor a `::', then we are not looking at a
7044 nested-name-specifier. */
7045 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7047 if (token->type == CPP_COLON
7048 && parser->colon_corrects_to_scope_p
7049 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
7050 /* name:name is a valid sequence in an Objective C message. */
7051 && !parser->objective_c_message_context_p)
7053 gcc_rich_location richloc (token->location);
7054 richloc.add_fixit_replace ("::");
7055 error_at (&richloc,
7056 "found %<:%> in nested-name-specifier, "
7057 "expected %<::%>");
7058 token->type = CPP_SCOPE;
7061 if (token->type != CPP_SCOPE
7062 && !cp_parser_nth_token_starts_template_argument_list_p
7063 (parser, 2))
7064 break;
7067 /* The nested-name-specifier is optional, so we parse
7068 tentatively. */
7069 cp_parser_parse_tentatively (parser);
7071 /* Look for the optional `template' keyword, if this isn't the
7072 first time through the loop. */
7073 if (success)
7075 template_keyword_p = cp_parser_optional_template_keyword (parser);
7076 /* DR1710: "In a qualified-id used as the name in
7077 a typename-specifier, elaborated-type-specifier, using-declaration,
7078 or class-or-decltype, an optional keyword template appearing at
7079 the top level is ignored." */
7080 if (!template_keyword_p
7081 && typename_keyword_p
7082 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
7083 template_keyword_p = true;
7086 /* Save the old scope since the name lookup we are about to do
7087 might destroy it. */
7088 old_scope = parser->scope;
7089 saved_qualifying_scope = parser->qualifying_scope;
7090 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
7091 look up names in "X<T>::I" in order to determine that "Y" is
7092 a template. So, if we have a typename at this point, we make
7093 an effort to look through it. */
7094 if (is_declaration
7095 && !check_dependency_p
7096 && !typename_keyword_p
7097 && parser->scope
7098 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7099 parser->scope = resolve_typename_type (parser->scope,
7100 /*only_current_p=*/false);
7101 /* Parse the qualifying entity. */
7102 new_scope
7103 = cp_parser_qualifying_entity (parser,
7104 typename_keyword_p,
7105 template_keyword_p,
7106 check_dependency_p,
7107 type_p,
7108 is_declaration);
7109 /* Look for the `::' token. */
7110 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7112 /* If we found what we wanted, we keep going; otherwise, we're
7113 done. */
7114 if (!cp_parser_parse_definitely (parser))
7116 bool error_p = false;
7118 /* Restore the OLD_SCOPE since it was valid before the
7119 failed attempt at finding the last
7120 class-or-namespace-name. */
7121 parser->scope = old_scope;
7122 parser->qualifying_scope = saved_qualifying_scope;
7124 /* If the next token is a decltype, and the one after that is a
7125 `::', then the decltype has failed to resolve to a class or
7126 enumeration type. Give this error even when parsing
7127 tentatively since it can't possibly be valid--and we're going
7128 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7129 won't get another chance.*/
7130 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7131 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7132 == CPP_SCOPE))
7134 token = cp_lexer_consume_token (parser->lexer);
7135 tree dtype = token->u.tree_check_value->value;
7136 if (dtype != error_mark_node)
7137 error_at (token->location, "%<decltype%> evaluates to %qT, "
7138 "which is not a class or enumeration type",
7139 dtype);
7140 parser->scope = error_mark_node;
7141 error_p = true;
7142 /* As below. */
7143 success = true;
7144 cp_lexer_consume_token (parser->lexer);
7147 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7148 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7150 /* If we have a non-type template-id followed by ::, it can't
7151 possibly be valid. */
7152 token = cp_lexer_peek_token (parser->lexer);
7153 tree tid = token->u.tree_check_value->value;
7154 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7155 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7157 tree tmpl = NULL_TREE;
7158 if (is_overloaded_fn (tid))
7160 tree fns = get_fns (tid);
7161 if (OVL_SINGLE_P (fns))
7162 tmpl = OVL_FIRST (fns);
7163 if (function_concept_p (fns))
7164 error_at (token->location, "concept-id %qD "
7165 "in nested-name-specifier", tid);
7166 else
7167 error_at (token->location, "function template-id "
7168 "%qD in nested-name-specifier", tid);
7170 else
7172 tmpl = TREE_OPERAND (tid, 0);
7173 if (variable_concept_p (tmpl)
7174 || standard_concept_p (tmpl))
7175 error_at (token->location, "concept-id %qD "
7176 "in nested-name-specifier", tid);
7177 else
7179 /* Variable template. */
7180 gcc_assert (variable_template_p (tmpl));
7181 error_at (token->location, "variable template-id "
7182 "%qD in nested-name-specifier", tid);
7185 if (tmpl)
7186 inform (DECL_SOURCE_LOCATION (tmpl),
7187 "%qD declared here", tmpl);
7189 parser->scope = error_mark_node;
7190 error_p = true;
7191 /* As below. */
7192 success = true;
7193 cp_lexer_consume_token (parser->lexer);
7194 cp_lexer_consume_token (parser->lexer);
7198 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7199 break;
7200 /* If the next token is an identifier, and the one after
7201 that is a `::', then any valid interpretation would have
7202 found a class-or-namespace-name. */
7203 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7204 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7205 == CPP_SCOPE)
7206 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7207 != CPP_COMPL))
7209 token = cp_lexer_consume_token (parser->lexer);
7210 if (!error_p)
7212 if (!token->error_reported)
7214 tree decl;
7215 tree ambiguous_decls;
7217 decl = cp_parser_lookup_name (parser, token->u.value,
7218 none_type,
7219 /*is_template=*/false,
7220 /*is_namespace=*/false,
7221 /*check_dependency=*/true,
7222 &ambiguous_decls,
7223 token->location);
7224 if (TREE_CODE (decl) == TEMPLATE_DECL)
7225 error_at (token->location,
7226 "%qD used without template arguments",
7227 decl);
7228 else if (ambiguous_decls)
7230 // cp_parser_lookup_name has the same diagnostic,
7231 // thus make sure to emit it at most once.
7232 if (cp_parser_uncommitted_to_tentative_parse_p
7233 (parser))
7235 error_at (token->location,
7236 "reference to %qD is ambiguous",
7237 token->u.value);
7238 print_candidates (ambiguous_decls);
7240 decl = error_mark_node;
7242 else
7244 if (cxx_dialect != cxx98)
7245 cp_parser_name_lookup_error
7246 (parser, token->u.value, decl, NLE_NOT_CXX98,
7247 token->location);
7248 else
7249 cp_parser_name_lookup_error
7250 (parser, token->u.value, decl, NLE_CXX98,
7251 token->location);
7254 parser->scope = error_mark_node;
7255 error_p = true;
7256 /* Treat this as a successful nested-name-specifier
7257 due to:
7259 [basic.lookup.qual]
7261 If the name found is not a class-name (clause
7262 _class_) or namespace-name (_namespace.def_), the
7263 program is ill-formed. */
7264 success = true;
7266 cp_lexer_consume_token (parser->lexer);
7268 break;
7270 /* We've found one valid nested-name-specifier. */
7271 success = true;
7272 /* Name lookup always gives us a DECL. */
7273 if (TREE_CODE (new_scope) == TYPE_DECL)
7274 new_scope = TREE_TYPE (new_scope);
7275 /* Uses of "template" must be followed by actual templates. */
7276 if (template_keyword_p)
7277 check_template_keyword_in_nested_name_spec (new_scope);
7278 /* If it is a class scope, try to complete it; we are about to
7279 be looking up names inside the class. */
7280 if (TYPE_P (new_scope)
7281 /* Since checking types for dependency can be expensive,
7282 avoid doing it if the type is already complete. */
7283 && !COMPLETE_TYPE_P (new_scope)
7284 /* Do not try to complete dependent types. */
7285 && !dependent_type_p (new_scope))
7287 new_scope = complete_type (new_scope);
7288 /* If it is a typedef to current class, use the current
7289 class instead, as the typedef won't have any names inside
7290 it yet. */
7291 if (!COMPLETE_TYPE_P (new_scope)
7292 && currently_open_class (new_scope))
7293 new_scope = TYPE_MAIN_VARIANT (new_scope);
7295 /* Make sure we look in the right scope the next time through
7296 the loop. */
7297 parser->scope = new_scope;
7300 /* If parsing tentatively, replace the sequence of tokens that makes
7301 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7302 token. That way, should we re-parse the token stream, we will
7303 not have to repeat the effort required to do the parse, nor will
7304 we issue duplicate error messages. */
7305 if (success && start)
7307 cp_token *token;
7309 token = cp_lexer_token_at (parser->lexer, start);
7310 /* Reset the contents of the START token. */
7311 token->type = CPP_NESTED_NAME_SPECIFIER;
7312 /* Retrieve any deferred checks. Do not pop this access checks yet
7313 so the memory will not be reclaimed during token replacing below. */
7314 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7315 token->tree_check_p = true;
7316 token->u.tree_check_value->value = parser->scope;
7317 token->u.tree_check_value->checks = get_deferred_access_checks ();
7318 token->u.tree_check_value->qualifying_scope =
7319 parser->qualifying_scope;
7320 token->keyword = RID_MAX;
7322 /* Purge all subsequent tokens. */
7323 cp_lexer_purge_tokens_after (parser->lexer, start);
7326 if (start)
7327 pop_to_parent_deferring_access_checks ();
7329 return success ? parser->scope : NULL_TREE;
7332 /* Parse a nested-name-specifier. See
7333 cp_parser_nested_name_specifier_opt for details. This function
7334 behaves identically, except that it will an issue an error if no
7335 nested-name-specifier is present. */
7337 static tree
7338 cp_parser_nested_name_specifier (cp_parser *parser,
7339 bool typename_keyword_p,
7340 bool check_dependency_p,
7341 bool type_p,
7342 bool is_declaration)
7344 tree scope;
7346 /* Look for the nested-name-specifier. */
7347 scope = cp_parser_nested_name_specifier_opt (parser,
7348 typename_keyword_p,
7349 check_dependency_p,
7350 type_p,
7351 is_declaration);
7352 /* If it was not present, issue an error message. */
7353 if (!scope)
7355 cp_parser_error (parser, "expected nested-name-specifier");
7356 parser->scope = NULL_TREE;
7359 return scope;
7362 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7363 this is either a class-name or a namespace-name (which corresponds
7364 to the class-or-namespace-name production in the grammar). For
7365 C++0x, it can also be a type-name that refers to an enumeration
7366 type or a simple-template-id.
7368 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7369 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7370 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7371 TYPE_P is TRUE iff the next name should be taken as a class-name,
7372 even the same name is declared to be another entity in the same
7373 scope.
7375 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7376 specified by the class-or-namespace-name. If neither is found the
7377 ERROR_MARK_NODE is returned. */
7379 static tree
7380 cp_parser_qualifying_entity (cp_parser *parser,
7381 bool typename_keyword_p,
7382 bool template_keyword_p,
7383 bool check_dependency_p,
7384 bool type_p,
7385 bool is_declaration)
7387 tree saved_scope;
7388 tree saved_qualifying_scope;
7389 tree saved_object_scope;
7390 tree scope;
7391 bool only_class_p;
7392 bool successful_parse_p;
7394 /* DR 743: decltype can appear in a nested-name-specifier. */
7395 if (cp_lexer_next_token_is_decltype (parser->lexer))
7397 scope = cp_parser_decltype (parser);
7398 if (TREE_CODE (scope) != ENUMERAL_TYPE
7399 && !MAYBE_CLASS_TYPE_P (scope))
7401 cp_parser_simulate_error (parser);
7402 return error_mark_node;
7404 if (TYPE_NAME (scope))
7405 scope = TYPE_NAME (scope);
7406 return scope;
7409 /* Before we try to parse the class-name, we must save away the
7410 current PARSER->SCOPE since cp_parser_class_name will destroy
7411 it. */
7412 saved_scope = parser->scope;
7413 saved_qualifying_scope = parser->qualifying_scope;
7414 saved_object_scope = parser->object_scope;
7415 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7416 there is no need to look for a namespace-name. */
7417 only_class_p = template_keyword_p
7418 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7419 if (!only_class_p)
7420 cp_parser_parse_tentatively (parser);
7421 scope = cp_parser_class_name (parser,
7422 typename_keyword_p,
7423 template_keyword_p,
7424 type_p ? class_type : none_type,
7425 check_dependency_p,
7426 /*class_head_p=*/false,
7427 is_declaration,
7428 /*enum_ok=*/cxx_dialect > cxx98);
7429 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7430 /* If that didn't work, try for a namespace-name. */
7431 if (!only_class_p && !successful_parse_p)
7433 /* Restore the saved scope. */
7434 parser->scope = saved_scope;
7435 parser->qualifying_scope = saved_qualifying_scope;
7436 parser->object_scope = saved_object_scope;
7437 /* If we are not looking at an identifier followed by the scope
7438 resolution operator, then this is not part of a
7439 nested-name-specifier. (Note that this function is only used
7440 to parse the components of a nested-name-specifier.) */
7441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7442 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7443 return error_mark_node;
7444 scope = cp_parser_namespace_name (parser);
7447 return scope;
7450 /* Return true if we are looking at a compound-literal, false otherwise. */
7452 static bool
7453 cp_parser_compound_literal_p (cp_parser *parser)
7455 cp_lexer_save_tokens (parser->lexer);
7457 /* Skip tokens until the next token is a closing parenthesis.
7458 If we find the closing `)', and the next token is a `{', then
7459 we are looking at a compound-literal. */
7460 bool compound_literal_p
7461 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7462 /*consume_paren=*/true)
7463 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7465 /* Roll back the tokens we skipped. */
7466 cp_lexer_rollback_tokens (parser->lexer);
7468 return compound_literal_p;
7471 /* Return true if EXPR is the integer constant zero or a complex constant
7472 of zero, without any folding, but ignoring location wrappers. */
7474 bool
7475 literal_integer_zerop (const_tree expr)
7477 return (location_wrapper_p (expr)
7478 && integer_zerop (TREE_OPERAND (expr, 0)));
7481 /* Parse a postfix-expression.
7483 postfix-expression:
7484 primary-expression
7485 postfix-expression [ expression ]
7486 postfix-expression ( expression-list [opt] )
7487 simple-type-specifier ( expression-list [opt] )
7488 typename :: [opt] nested-name-specifier identifier
7489 ( expression-list [opt] )
7490 typename :: [opt] nested-name-specifier template [opt] template-id
7491 ( expression-list [opt] )
7492 postfix-expression . template [opt] id-expression
7493 postfix-expression -> template [opt] id-expression
7494 postfix-expression . pseudo-destructor-name
7495 postfix-expression -> pseudo-destructor-name
7496 postfix-expression ++
7497 postfix-expression --
7498 dynamic_cast < type-id > ( expression )
7499 static_cast < type-id > ( expression )
7500 reinterpret_cast < type-id > ( expression )
7501 const_cast < type-id > ( expression )
7502 typeid ( expression )
7503 typeid ( type-id )
7505 GNU Extension:
7507 postfix-expression:
7508 ( type-id ) { initializer-list , [opt] }
7510 This extension is a GNU version of the C99 compound-literal
7511 construct. (The C99 grammar uses `type-name' instead of `type-id',
7512 but they are essentially the same concept.)
7514 If ADDRESS_P is true, the postfix expression is the operand of the
7515 `&' operator. CAST_P is true if this expression is the target of a
7516 cast.
7518 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7519 class member access expressions [expr.ref].
7521 Returns a representation of the expression. */
7523 static cp_expr
7524 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7525 bool member_access_only_p, bool decltype_p,
7526 cp_id_kind * pidk_return)
7528 cp_token *token;
7529 location_t loc;
7530 enum rid keyword;
7531 cp_id_kind idk = CP_ID_KIND_NONE;
7532 cp_expr postfix_expression = NULL_TREE;
7533 bool is_member_access = false;
7535 /* Peek at the next token. */
7536 token = cp_lexer_peek_token (parser->lexer);
7537 loc = token->location;
7538 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7540 /* Some of the productions are determined by keywords. */
7541 keyword = token->keyword;
7542 switch (keyword)
7544 case RID_DYNCAST:
7545 case RID_STATCAST:
7546 case RID_REINTCAST:
7547 case RID_CONSTCAST:
7549 tree type;
7550 cp_expr expression;
7551 const char *saved_message;
7552 bool saved_in_type_id_in_expr_p;
7554 /* All of these can be handled in the same way from the point
7555 of view of parsing. Begin by consuming the token
7556 identifying the cast. */
7557 cp_lexer_consume_token (parser->lexer);
7559 /* New types cannot be defined in the cast. */
7560 saved_message = parser->type_definition_forbidden_message;
7561 parser->type_definition_forbidden_message
7562 = G_("types may not be defined in casts");
7564 /* Look for the opening `<'. */
7565 cp_parser_require (parser, CPP_LESS, RT_LESS);
7566 /* Parse the type to which we are casting. */
7567 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7568 parser->in_type_id_in_expr_p = true;
7569 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7570 NULL);
7571 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7572 /* Look for the closing `>'. */
7573 cp_parser_require_end_of_template_parameter_list (parser);
7574 /* Restore the old message. */
7575 parser->type_definition_forbidden_message = saved_message;
7577 bool saved_greater_than_is_operator_p
7578 = parser->greater_than_is_operator_p;
7579 parser->greater_than_is_operator_p = true;
7581 /* And the expression which is being cast. */
7582 matching_parens parens;
7583 parens.require_open (parser);
7584 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7585 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7586 RT_CLOSE_PAREN);
7587 location_t end_loc = close_paren ?
7588 close_paren->location : UNKNOWN_LOCATION;
7590 parser->greater_than_is_operator_p
7591 = saved_greater_than_is_operator_p;
7593 /* Only type conversions to integral or enumeration types
7594 can be used in constant-expressions. */
7595 if (!cast_valid_in_integral_constant_expression_p (type)
7596 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7598 postfix_expression = error_mark_node;
7599 break;
7602 /* Construct a location e.g. :
7603 reinterpret_cast <int *> (expr)
7604 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7605 ranging from the start of the "*_cast" token to the final closing
7606 paren, with the caret at the start. */
7607 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7609 switch (keyword)
7611 case RID_DYNCAST:
7612 postfix_expression
7613 = build_dynamic_cast (cp_cast_loc, type, expression,
7614 tf_warning_or_error);
7615 break;
7616 case RID_STATCAST:
7617 postfix_expression
7618 = build_static_cast (cp_cast_loc, type, expression,
7619 tf_warning_or_error);
7620 break;
7621 case RID_REINTCAST:
7622 postfix_expression
7623 = build_reinterpret_cast (cp_cast_loc, type, expression,
7624 tf_warning_or_error);
7625 break;
7626 case RID_CONSTCAST:
7627 postfix_expression
7628 = build_const_cast (cp_cast_loc, type, expression,
7629 tf_warning_or_error);
7630 break;
7631 default:
7632 gcc_unreachable ();
7635 break;
7637 case RID_TYPEID:
7639 tree type;
7640 const char *saved_message;
7641 bool saved_in_type_id_in_expr_p;
7643 /* Consume the `typeid' token. */
7644 cp_lexer_consume_token (parser->lexer);
7645 /* Look for the `(' token. */
7646 matching_parens parens;
7647 parens.require_open (parser);
7648 /* Types cannot be defined in a `typeid' expression. */
7649 saved_message = parser->type_definition_forbidden_message;
7650 parser->type_definition_forbidden_message
7651 = G_("types may not be defined in a %<typeid%> expression");
7652 /* We can't be sure yet whether we're looking at a type-id or an
7653 expression. */
7654 cp_parser_parse_tentatively (parser);
7655 /* Try a type-id first. */
7656 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7657 parser->in_type_id_in_expr_p = true;
7658 type = cp_parser_type_id (parser);
7659 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7660 /* Look for the `)' token. Otherwise, we can't be sure that
7661 we're not looking at an expression: consider `typeid (int
7662 (3))', for example. */
7663 cp_token *close_paren = parens.require_close (parser);
7664 /* If all went well, simply lookup the type-id. */
7665 if (cp_parser_parse_definitely (parser))
7666 postfix_expression = get_typeid (type, tf_warning_or_error);
7667 /* Otherwise, fall back to the expression variant. */
7668 else
7670 tree expression;
7672 /* Look for an expression. */
7673 expression = cp_parser_expression (parser, & idk);
7674 /* Compute its typeid. */
7675 postfix_expression = build_typeid (expression, tf_warning_or_error);
7676 /* Look for the `)' token. */
7677 close_paren = parens.require_close (parser);
7679 /* Restore the saved message. */
7680 parser->type_definition_forbidden_message = saved_message;
7681 /* `typeid' may not appear in an integral constant expression. */
7682 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7683 postfix_expression = error_mark_node;
7685 /* Construct a location e.g. :
7686 typeid (expr)
7687 ^~~~~~~~~~~~~
7688 ranging from the start of the "typeid" token to the final closing
7689 paren, with the caret at the start. */
7690 if (close_paren)
7692 location_t typeid_loc
7693 = make_location (start_loc, start_loc, close_paren->location);
7694 postfix_expression.set_location (typeid_loc);
7695 postfix_expression.maybe_add_location_wrapper ();
7698 break;
7700 case RID_TYPENAME:
7702 tree type;
7703 /* The syntax permitted here is the same permitted for an
7704 elaborated-type-specifier. */
7705 ++parser->prevent_constrained_type_specifiers;
7706 type = cp_parser_elaborated_type_specifier (parser,
7707 /*is_friend=*/false,
7708 /*is_declaration=*/false);
7709 --parser->prevent_constrained_type_specifiers;
7710 postfix_expression = cp_parser_functional_cast (parser, type);
7712 break;
7714 case RID_ADDRESSOF:
7715 case RID_BUILTIN_SHUFFLE:
7716 case RID_BUILTIN_SHUFFLEVECTOR:
7717 case RID_BUILTIN_LAUNDER:
7718 case RID_BUILTIN_ASSOC_BARRIER:
7720 vec<tree, va_gc> *vec;
7722 cp_lexer_consume_token (parser->lexer);
7723 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7724 /*cast_p=*/false, /*allow_expansion_p=*/true,
7725 /*non_constant_p=*/NULL);
7726 if (vec == NULL)
7728 postfix_expression = error_mark_node;
7729 break;
7732 for (tree p : *vec)
7733 mark_exp_read (p);
7735 switch (keyword)
7737 case RID_ADDRESSOF:
7738 if (vec->length () == 1)
7739 postfix_expression
7740 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7741 else
7743 error_at (loc, "wrong number of arguments to "
7744 "%<__builtin_addressof%>");
7745 postfix_expression = error_mark_node;
7747 break;
7749 case RID_BUILTIN_LAUNDER:
7750 if (vec->length () == 1)
7751 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7752 tf_warning_or_error);
7753 else
7755 error_at (loc, "wrong number of arguments to "
7756 "%<__builtin_launder%>");
7757 postfix_expression = error_mark_node;
7759 break;
7761 case RID_BUILTIN_ASSOC_BARRIER:
7762 if (vec->length () == 1)
7763 postfix_expression = build1_loc (loc, PAREN_EXPR,
7764 TREE_TYPE ((*vec)[0]),
7765 (*vec)[0]);
7766 else
7768 error_at (loc, "wrong number of arguments to "
7769 "%<__builtin_assoc_barrier%>");
7770 postfix_expression = error_mark_node;
7772 break;
7774 case RID_BUILTIN_SHUFFLE:
7775 if (vec->length () == 2)
7776 postfix_expression
7777 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7778 (*vec)[1], tf_warning_or_error);
7779 else if (vec->length () == 3)
7780 postfix_expression
7781 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7782 (*vec)[2], tf_warning_or_error);
7783 else
7785 error_at (loc, "wrong number of arguments to "
7786 "%<__builtin_shuffle%>");
7787 postfix_expression = error_mark_node;
7789 break;
7791 case RID_BUILTIN_SHUFFLEVECTOR:
7792 if (vec->length () < 3)
7794 error_at (loc, "wrong number of arguments to "
7795 "%<__builtin_shufflevector%>");
7796 postfix_expression = error_mark_node;
7798 else
7800 postfix_expression
7801 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7803 break;
7805 default:
7806 gcc_unreachable ();
7808 break;
7811 case RID_BUILTIN_CONVERTVECTOR:
7813 tree expression;
7814 tree type;
7815 /* Consume the `__builtin_convertvector' token. */
7816 cp_lexer_consume_token (parser->lexer);
7817 /* Look for the opening `('. */
7818 matching_parens parens;
7819 parens.require_open (parser);
7820 /* Now, parse the assignment-expression. */
7821 expression = cp_parser_assignment_expression (parser);
7822 /* Look for the `,'. */
7823 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7824 location_t type_location
7825 = cp_lexer_peek_token (parser->lexer)->location;
7826 /* Parse the type-id. */
7828 type_id_in_expr_sentinel s (parser);
7829 type = cp_parser_type_id (parser);
7831 /* Look for the closing `)'. */
7832 parens.require_close (parser);
7833 postfix_expression
7834 = cp_build_vec_convert (expression, type_location, type,
7835 tf_warning_or_error);
7836 break;
7839 case RID_BUILTIN_BIT_CAST:
7841 tree expression;
7842 tree type;
7843 /* Consume the `__builtin_bit_cast' token. */
7844 cp_lexer_consume_token (parser->lexer);
7845 /* Look for the opening `('. */
7846 matching_parens parens;
7847 parens.require_open (parser);
7848 location_t type_location
7849 = cp_lexer_peek_token (parser->lexer)->location;
7850 /* Parse the type-id. */
7852 type_id_in_expr_sentinel s (parser);
7853 type = cp_parser_type_id (parser);
7855 /* Look for the `,'. */
7856 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7857 /* Now, parse the assignment-expression. */
7858 expression = cp_parser_assignment_expression (parser);
7859 /* Look for the closing `)'. */
7860 parens.require_close (parser);
7861 postfix_expression
7862 = cp_build_bit_cast (type_location, type, expression,
7863 tf_warning_or_error);
7864 break;
7867 default:
7869 tree type;
7871 /* If the next thing is a simple-type-specifier, we may be
7872 looking at a functional cast. We could also be looking at
7873 an id-expression. So, we try the functional cast, and if
7874 that doesn't work we fall back to the primary-expression. */
7875 cp_parser_parse_tentatively (parser);
7876 /* Look for the simple-type-specifier. */
7877 ++parser->prevent_constrained_type_specifiers;
7878 type = cp_parser_simple_type_specifier (parser,
7879 /*decl_specs=*/NULL,
7880 CP_PARSER_FLAGS_NONE);
7881 --parser->prevent_constrained_type_specifiers;
7882 /* Parse the cast itself. */
7883 if (!cp_parser_error_occurred (parser))
7884 postfix_expression
7885 = cp_parser_functional_cast (parser, type);
7886 /* If that worked, we're done. */
7887 if (cp_parser_parse_definitely (parser))
7888 break;
7890 /* If the functional-cast didn't work out, try a
7891 compound-literal. */
7892 if (cp_parser_allow_gnu_extensions_p (parser)
7893 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7895 cp_expr initializer = NULL_TREE;
7897 cp_parser_parse_tentatively (parser);
7899 matching_parens parens;
7900 parens.consume_open (parser);
7902 /* Avoid calling cp_parser_type_id pointlessly, see comment
7903 in cp_parser_cast_expression about c++/29234. */
7904 if (!cp_parser_compound_literal_p (parser))
7905 cp_parser_simulate_error (parser);
7906 else
7908 /* Parse the type. */
7909 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7910 parser->in_type_id_in_expr_p = true;
7911 type = cp_parser_type_id (parser);
7912 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7913 parens.require_close (parser);
7916 /* If things aren't going well, there's no need to
7917 keep going. */
7918 if (!cp_parser_error_occurred (parser))
7919 /* Parse the brace-enclosed initializer list. */
7920 initializer = cp_parser_braced_list (parser);
7921 /* If that worked, we're definitely looking at a
7922 compound-literal expression. */
7923 if (cp_parser_parse_definitely (parser))
7925 /* Warn the user that a compound literal is not
7926 allowed in standard C++. */
7927 pedwarn (input_location, OPT_Wpedantic,
7928 "ISO C++ forbids compound-literals");
7929 /* For simplicity, we disallow compound literals in
7930 constant-expressions. We could
7931 allow compound literals of integer type, whose
7932 initializer was a constant, in constant
7933 expressions. Permitting that usage, as a further
7934 extension, would not change the meaning of any
7935 currently accepted programs. (Of course, as
7936 compound literals are not part of ISO C++, the
7937 standard has nothing to say.) */
7938 if (cp_parser_non_integral_constant_expression (parser,
7939 NIC_NCC))
7941 postfix_expression = error_mark_node;
7942 break;
7944 /* Form the representation of the compound-literal. */
7945 postfix_expression
7946 = finish_compound_literal (type, initializer,
7947 tf_warning_or_error, fcl_c99);
7948 postfix_expression.set_location (initializer.get_location ());
7949 break;
7953 /* It must be a primary-expression. */
7954 postfix_expression
7955 = cp_parser_primary_expression (parser, address_p, cast_p,
7956 /*template_arg_p=*/false,
7957 decltype_p,
7958 &idk);
7960 break;
7963 /* Note that we don't need to worry about calling build_cplus_new on a
7964 class-valued CALL_EXPR in decltype when it isn't the end of the
7965 postfix-expression; unary_complex_lvalue will take care of that for
7966 all these cases. */
7968 /* Keep looping until the postfix-expression is complete. */
7969 while (true)
7971 if (idk == CP_ID_KIND_UNQUALIFIED
7972 && identifier_p (postfix_expression)
7973 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7974 /* It is not a Koenig lookup function call. */
7975 postfix_expression
7976 = unqualified_name_lookup_error (postfix_expression);
7978 /* Peek at the next token. */
7979 token = cp_lexer_peek_token (parser->lexer);
7981 switch (token->type)
7983 case CPP_OPEN_SQUARE:
7984 if (cp_next_tokens_can_be_std_attribute_p (parser))
7986 cp_parser_error (parser,
7987 "two consecutive %<[%> shall "
7988 "only introduce an attribute");
7989 return error_mark_node;
7991 postfix_expression
7992 = cp_parser_postfix_open_square_expression (parser,
7993 postfix_expression,
7994 false,
7995 decltype_p);
7996 postfix_expression.set_range (start_loc,
7997 postfix_expression.get_location ());
7999 idk = CP_ID_KIND_NONE;
8000 is_member_access = false;
8001 break;
8003 case CPP_OPEN_PAREN:
8004 /* postfix-expression ( expression-list [opt] ) */
8006 bool koenig_p;
8007 bool is_builtin_constant_p;
8008 bool saved_integral_constant_expression_p = false;
8009 bool saved_non_integral_constant_expression_p = false;
8010 tsubst_flags_t complain = complain_flags (decltype_p);
8011 vec<tree, va_gc> *args;
8012 location_t close_paren_loc = UNKNOWN_LOCATION;
8013 location_t combined_loc = UNKNOWN_LOCATION;
8015 is_member_access = false;
8017 tree stripped_expression
8018 = tree_strip_any_location_wrapper (postfix_expression);
8019 is_builtin_constant_p
8020 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
8021 if (is_builtin_constant_p)
8023 /* The whole point of __builtin_constant_p is to allow
8024 non-constant expressions to appear as arguments. */
8025 saved_integral_constant_expression_p
8026 = parser->integral_constant_expression_p;
8027 saved_non_integral_constant_expression_p
8028 = parser->non_integral_constant_expression_p;
8029 parser->integral_constant_expression_p = false;
8031 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
8032 && fndecl_built_in_p (stripped_expression,
8033 BUILT_IN_CLASSIFY_TYPE))
8035 /* __builtin_classify_type (type) */
8036 auto cl1 = make_temp_override
8037 (parser->type_definition_forbidden_message,
8038 G_("types may not be defined in "
8039 "%<__builtin_classify_type%> calls"));
8040 auto cl2 = make_temp_override
8041 (parser->type_definition_forbidden_message_arg,
8042 NULL);
8043 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
8044 true);
8045 cp_unevaluated uev;
8046 cp_parser_parse_tentatively (parser);
8047 matching_parens parens;
8048 parens.consume_open (parser);
8049 tree type = cp_parser_type_id (parser);
8050 parens.require_close (parser);
8051 if (cp_parser_parse_definitely (parser))
8053 if (dependent_type_p (type))
8055 postfix_expression = build_vl_exp (CALL_EXPR, 4);
8056 CALL_EXPR_FN (postfix_expression)
8057 = stripped_expression;
8058 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
8059 CALL_EXPR_ARG (postfix_expression, 0)
8060 = build_min (SIZEOF_EXPR, size_type_node, type);
8061 TREE_TYPE (postfix_expression) = integer_type_node;
8063 else
8065 postfix_expression
8066 = build_int_cst (integer_type_node,
8067 type_to_class (type));
8069 break;
8072 args = (cp_parser_parenthesized_expression_list
8073 (parser, non_attr,
8074 /*cast_p=*/false, /*allow_expansion_p=*/true,
8075 /*non_constant_p=*/NULL,
8076 /*close_paren_loc=*/&close_paren_loc,
8077 /*wrap_locations_p=*/true));
8078 if (is_builtin_constant_p)
8080 parser->integral_constant_expression_p
8081 = saved_integral_constant_expression_p;
8082 parser->non_integral_constant_expression_p
8083 = saved_non_integral_constant_expression_p;
8086 if (args == NULL)
8088 postfix_expression = error_mark_node;
8089 break;
8092 /* Function calls are not permitted in
8093 constant-expressions. */
8094 if (! builtin_valid_in_constant_expr_p (postfix_expression)
8095 && cp_parser_non_integral_constant_expression (parser,
8096 NIC_FUNC_CALL))
8098 postfix_expression = error_mark_node;
8099 release_tree_vector (args);
8100 break;
8103 koenig_p = false;
8104 if (idk == CP_ID_KIND_UNQUALIFIED
8105 || idk == CP_ID_KIND_TEMPLATE_ID)
8107 if (identifier_p (postfix_expression)
8108 /* In C++20, we may need to perform ADL for a template
8109 name. */
8110 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
8111 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
8113 if (!args->is_empty ())
8115 koenig_p = true;
8116 if (!any_type_dependent_arguments_p (args))
8117 postfix_expression
8118 = perform_koenig_lookup (postfix_expression, args,
8119 complain);
8121 else
8122 postfix_expression
8123 = unqualified_fn_lookup_error (postfix_expression);
8125 /* We do not perform argument-dependent lookup if
8126 normal lookup finds a non-function, in accordance
8127 with the expected resolution of DR 218. */
8128 else if (!args->is_empty ()
8129 && is_overloaded_fn (postfix_expression))
8131 /* Do not do argument dependent lookup if regular
8132 lookup finds a member function or a block-scope
8133 function declaration. [basic.lookup.argdep]/3 */
8134 bool do_adl_p = true;
8135 tree fns = get_fns (postfix_expression);
8136 for (lkp_iterator iter (fns); iter; ++iter)
8138 tree fn = STRIP_TEMPLATE (*iter);
8139 if ((TREE_CODE (fn) == USING_DECL
8140 && DECL_DEPENDENT_P (fn))
8141 || DECL_FUNCTION_MEMBER_P (fn)
8142 || DECL_LOCAL_DECL_P (fn))
8144 do_adl_p = false;
8145 break;
8149 if (do_adl_p)
8151 koenig_p = true;
8152 if (!any_type_dependent_arguments_p (args))
8153 postfix_expression
8154 = perform_koenig_lookup (postfix_expression, args,
8155 complain);
8160 /* Temporarily set input_location to the combined location
8161 with call expression range, as e.g. build_out_target_exprs
8162 called from convert_default_arg relies on input_location,
8163 so updating it only when the call is fully built results
8164 in inconsistencies between location handling in templates
8165 and outside of templates. */
8166 if (close_paren_loc != UNKNOWN_LOCATION)
8167 combined_loc = make_location (token->location, start_loc,
8168 close_paren_loc);
8169 iloc_sentinel ils (combined_loc);
8171 if (TREE_CODE (postfix_expression) == OFFSET_REF
8172 || TREE_CODE (postfix_expression) == MEMBER_REF
8173 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8174 postfix_expression = (build_offset_ref_call_from_tree
8175 (postfix_expression, &args,
8176 complain));
8177 else
8178 /* All other function calls. */
8180 if (DECL_P (postfix_expression)
8181 && parser->omp_for_parse_state
8182 && parser->omp_for_parse_state->in_intervening_code
8183 && omp_runtime_api_call (postfix_expression))
8185 error_at (loc, "calls to the OpenMP runtime API are "
8186 "not permitted in intervening code");
8187 parser->omp_for_parse_state->fail = true;
8189 bool disallow_virtual = (idk == CP_ID_KIND_QUALIFIED);
8190 postfix_expression
8191 = finish_call_expr (postfix_expression, &args,
8192 disallow_virtual,
8193 koenig_p,
8194 complain);
8197 if (close_paren_loc != UNKNOWN_LOCATION)
8198 postfix_expression.set_location (combined_loc);
8200 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8201 idk = CP_ID_KIND_NONE;
8203 release_tree_vector (args);
8205 break;
8207 case CPP_DOT:
8208 case CPP_DEREF:
8209 /* postfix-expression . template [opt] id-expression
8210 postfix-expression . pseudo-destructor-name
8211 postfix-expression -> template [opt] id-expression
8212 postfix-expression -> pseudo-destructor-name */
8214 /* Consume the `.' or `->' operator. */
8215 cp_lexer_consume_token (parser->lexer);
8217 postfix_expression
8218 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8219 postfix_expression,
8220 false, &idk, loc);
8222 is_member_access = true;
8223 break;
8225 case CPP_PLUS_PLUS:
8226 /* postfix-expression ++ */
8227 /* Consume the `++' token. */
8228 cp_lexer_consume_token (parser->lexer);
8229 /* Generate a representation for the complete expression. */
8230 postfix_expression
8231 = finish_increment_expr (postfix_expression,
8232 POSTINCREMENT_EXPR);
8233 /* Increments may not appear in constant-expressions. */
8234 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8235 postfix_expression = error_mark_node;
8236 idk = CP_ID_KIND_NONE;
8237 is_member_access = false;
8238 break;
8240 case CPP_MINUS_MINUS:
8241 /* postfix-expression -- */
8242 /* Consume the `--' token. */
8243 cp_lexer_consume_token (parser->lexer);
8244 /* Generate a representation for the complete expression. */
8245 postfix_expression
8246 = finish_increment_expr (postfix_expression,
8247 POSTDECREMENT_EXPR);
8248 /* Decrements may not appear in constant-expressions. */
8249 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8250 postfix_expression = error_mark_node;
8251 idk = CP_ID_KIND_NONE;
8252 is_member_access = false;
8253 break;
8255 default:
8256 if (pidk_return != NULL)
8257 * pidk_return = idk;
8258 if (member_access_only_p)
8259 return is_member_access
8260 ? postfix_expression
8261 : cp_expr (error_mark_node);
8262 else
8263 return postfix_expression;
8268 /* Helper function for cp_parser_parenthesized_expression_list and
8269 cp_parser_postfix_open_square_expression. Parse a single element
8270 of parenthesized expression list. */
8272 static cp_expr
8273 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8274 bool allow_expansion_p,
8275 bool *non_constant_p)
8277 cp_expr expr (NULL_TREE);
8278 bool expr_non_constant_p;
8280 /* Parse the next assignment-expression. */
8281 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8283 /* A braced-init-list. */
8284 cp_lexer_set_source_position (parser->lexer);
8285 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8286 expr = cp_parser_braced_list (parser,
8287 (non_constant_p != nullptr
8288 ? &expr_non_constant_p
8289 : nullptr));
8290 if (non_constant_p && expr_non_constant_p)
8291 *non_constant_p = true;
8293 else if (non_constant_p)
8295 expr = cp_parser_constant_expression (parser,
8296 /*allow_non_constant_p=*/true,
8297 &expr_non_constant_p);
8298 if (expr_non_constant_p)
8299 *non_constant_p = true;
8301 else
8302 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8304 /* If we have an ellipsis, then this is an expression expansion. */
8305 if (allow_expansion_p
8306 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8308 /* Consume the `...'. */
8309 cp_lexer_consume_token (parser->lexer);
8311 /* Build the argument pack. */
8312 expr = make_pack_expansion (expr);
8314 return expr;
8317 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8318 by cp_parser_builtin_offsetof. We're looking for
8320 postfix-expression [ expression ]
8321 postfix-expression [ braced-init-list ] (C++11)
8322 postfix-expression [ expression-list[opt] ] (C++23)
8324 FOR_OFFSETOF is set if we're being called in that context, which
8325 changes how we deal with integer constant expressions. */
8327 static tree
8328 cp_parser_postfix_open_square_expression (cp_parser *parser,
8329 tree postfix_expression,
8330 bool for_offsetof,
8331 bool decltype_p)
8333 tree index = NULL_TREE;
8334 releasing_vec expression_list = NULL;
8335 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8336 bool saved_greater_than_is_operator_p;
8337 bool saved_colon_corrects_to_scope_p;
8339 /* Consume the `[' token. */
8340 cp_lexer_consume_token (parser->lexer);
8342 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8343 parser->greater_than_is_operator_p = true;
8345 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8346 if (parser->omp_array_section_p)
8347 parser->colon_corrects_to_scope_p = false;
8349 /* Parse the index expression. */
8350 /* ??? For offsetof, there is a question of what to allow here. If
8351 offsetof is not being used in an integral constant expression context,
8352 then we *could* get the right answer by computing the value at runtime.
8353 If we are in an integral constant expression context, then we might
8354 could accept any constant expression; hard to say without analysis.
8355 Rather than open the barn door too wide right away, allow only integer
8356 constant expressions here. */
8357 if (for_offsetof)
8358 index = cp_parser_constant_expression (parser);
8359 else if (!parser->omp_array_section_p
8360 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8362 if (cxx_dialect >= cxx23
8363 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8364 *&expression_list = make_tree_vector ();
8365 else if (cxx_dialect >= cxx23)
8367 while (true)
8369 cp_expr expr
8370 = cp_parser_parenthesized_expression_list_elt (parser,
8371 /*cast_p=*/
8372 false,
8373 /*allow_exp_p=*/
8374 true,
8375 /*non_cst_p=*/
8376 NULL);
8378 if (expr == error_mark_node)
8379 index = error_mark_node;
8380 else if (expression_list.get () == NULL
8381 && !PACK_EXPANSION_P (expr.get_value ()))
8382 index = expr.get_value ();
8383 else
8384 vec_safe_push (expression_list, expr.get_value ());
8386 /* If the next token isn't a `,', then we are done. */
8387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8388 break;
8390 if (expression_list.get () == NULL && index != error_mark_node)
8392 *&expression_list = make_tree_vector_single (index);
8393 index = NULL_TREE;
8396 /* Otherwise, consume the `,' and keep going. */
8397 cp_lexer_consume_token (parser->lexer);
8399 if (expression_list.get () && index == error_mark_node)
8400 expression_list.release ();
8402 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8404 cp_lexer_set_source_position (parser->lexer);
8405 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8406 index = cp_parser_braced_list (parser);
8408 else
8409 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8410 /*decltype_p=*/false,
8411 /*warn_comma_p=*/warn_comma_subscript);
8414 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8416 if (cxx_dialect >= cxx23
8417 && parser->omp_array_section_p
8418 && expression_list.get () != NULL
8419 && vec_safe_length (expression_list) > 1)
8421 error_at (loc, "cannot use multidimensional subscript in OpenMP array "
8422 "section");
8423 index = error_mark_node;
8425 if (parser->omp_array_section_p
8426 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8428 cp_lexer_consume_token (parser->lexer);
8429 tree length = NULL_TREE;
8430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8432 if (cxx_dialect >= cxx23)
8434 cp_expr expr
8435 = cp_parser_parenthesized_expression_list_elt (parser,
8436 /*cast_p=*/
8437 false,
8438 /*allow_exp_p=*/
8439 true,
8440 /*non_cst_p=*/
8441 NULL);
8443 if (expr == error_mark_node)
8444 length = error_mark_node;
8445 else
8446 length = expr.get_value ();
8448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8450 error_at (loc, "cannot use multidimensional subscript in "
8451 "OpenMP array section");
8452 length = error_mark_node;
8455 else
8456 length
8457 = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8458 /*decltype_p=*/false,
8459 /*warn_comma_p=*/warn_comma_subscript);
8462 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8464 if (index == error_mark_node || length == error_mark_node)
8466 cp_parser_skip_to_closing_square_bracket (parser);
8467 return error_mark_node;
8469 else
8470 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8472 return grok_omp_array_section (input_location, postfix_expression, index,
8473 length);
8476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8478 /* Look for the closing `]'. */
8479 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8481 /* Build the ARRAY_REF. */
8482 postfix_expression = grok_array_decl (loc, postfix_expression,
8483 index, &expression_list,
8484 tf_warning_or_error
8485 | (decltype_p ? tf_decltype : 0));
8487 /* When not doing offsetof, array references are not permitted in
8488 constant-expressions. */
8489 if (!for_offsetof
8490 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8491 postfix_expression = error_mark_node;
8493 return postfix_expression;
8496 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8497 dereference of incomplete type, returns true if error_mark_node should
8498 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8499 and *DEPENDENT_P. */
8501 bool
8502 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8503 bool *dependent_p)
8505 /* In a template, be permissive by treating an object expression
8506 of incomplete type as dependent (after a pedwarn). */
8507 diagnostic_t kind = (processing_template_decl
8508 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8510 switch (TREE_CODE (*postfix_expression))
8512 case CAST_EXPR:
8513 case REINTERPRET_CAST_EXPR:
8514 case CONST_CAST_EXPR:
8515 case STATIC_CAST_EXPR:
8516 case DYNAMIC_CAST_EXPR:
8517 case IMPLICIT_CONV_EXPR:
8518 case VIEW_CONVERT_EXPR:
8519 case NON_LVALUE_EXPR:
8520 kind = DK_ERROR;
8521 break;
8522 case OVERLOAD:
8523 /* Don't emit any diagnostic for OVERLOADs. */
8524 kind = DK_IGNORED;
8525 break;
8526 default:
8527 /* Avoid clobbering e.g. DECLs. */
8528 if (!EXPR_P (*postfix_expression))
8529 kind = DK_ERROR;
8530 break;
8533 if (kind == DK_IGNORED)
8534 return false;
8536 location_t exploc = location_of (*postfix_expression);
8537 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8538 if (!MAYBE_CLASS_TYPE_P (*scope))
8539 return true;
8540 if (kind == DK_ERROR)
8541 *scope = *postfix_expression = error_mark_node;
8542 else if (processing_template_decl)
8544 *dependent_p = true;
8545 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8547 return false;
8550 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8551 by cp_parser_builtin_offsetof. We're looking for
8553 postfix-expression . template [opt] id-expression
8554 postfix-expression . pseudo-destructor-name
8555 postfix-expression -> template [opt] id-expression
8556 postfix-expression -> pseudo-destructor-name
8558 FOR_OFFSETOF is set if we're being called in that context. That sorta
8559 limits what of the above we'll actually accept, but nevermind.
8560 TOKEN_TYPE is the "." or "->" token, which will already have been
8561 removed from the stream. */
8563 static tree
8564 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8565 enum cpp_ttype token_type,
8566 cp_expr postfix_expression,
8567 bool for_offsetof, cp_id_kind *idk,
8568 location_t location)
8570 tree name;
8571 bool dependent_p;
8572 bool pseudo_destructor_p;
8573 tree scope = NULL_TREE;
8574 location_t start_loc = postfix_expression.get_start ();
8576 /* If this is a `->' operator, dereference the pointer. */
8577 if (token_type == CPP_DEREF)
8578 postfix_expression = build_x_arrow (location, postfix_expression,
8579 tf_warning_or_error);
8580 /* Check to see whether or not the expression is type-dependent and
8581 not the current instantiation. */
8582 dependent_p = type_dependent_object_expression_p (postfix_expression);
8583 /* The identifier following the `->' or `.' is not qualified. */
8584 parser->scope = NULL_TREE;
8585 parser->qualifying_scope = NULL_TREE;
8586 parser->object_scope = NULL_TREE;
8587 *idk = CP_ID_KIND_NONE;
8589 /* Enter the scope corresponding to the type of the object
8590 given by the POSTFIX_EXPRESSION. */
8591 if (!dependent_p)
8593 scope = TREE_TYPE (postfix_expression);
8594 /* According to the standard, no expression should ever have
8595 reference type. Unfortunately, we do not currently match
8596 the standard in this respect in that our internal representation
8597 of an expression may have reference type even when the standard
8598 says it does not. Therefore, we have to manually obtain the
8599 underlying type here. */
8600 scope = non_reference (scope);
8601 /* The type of the POSTFIX_EXPRESSION must be complete. */
8602 /* Unlike the object expression in other contexts, *this is not
8603 required to be of complete type for purposes of class member
8604 access (5.2.5) outside the member function body. */
8605 if (postfix_expression != current_class_ref
8606 && scope != error_mark_node
8607 && !currently_open_class (scope))
8609 scope = complete_type (scope);
8610 if (!COMPLETE_TYPE_P (scope)
8611 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8612 &dependent_p))
8613 return error_mark_node;
8616 if (!dependent_p)
8618 /* Let the name lookup machinery know that we are processing a
8619 class member access expression. */
8620 parser->context->object_type = scope;
8621 /* If something went wrong, we want to be able to discern that case,
8622 as opposed to the case where there was no SCOPE due to the type
8623 of expression being dependent. */
8624 if (!scope)
8625 scope = error_mark_node;
8626 /* If the SCOPE was erroneous, make the various semantic analysis
8627 functions exit quickly -- and without issuing additional error
8628 messages. */
8629 if (scope == error_mark_node)
8630 postfix_expression = error_mark_node;
8634 if (dependent_p)
8636 tree type = TREE_TYPE (postfix_expression);
8637 /* If we don't have a (type-dependent) object of class type, use
8638 typeof to figure out the type of the object. */
8639 if (type == NULL_TREE || is_auto (type))
8640 type = finish_typeof (postfix_expression);
8641 parser->context->object_type = type;
8644 /* Assume this expression is not a pseudo-destructor access. */
8645 pseudo_destructor_p = false;
8647 /* If the SCOPE is a scalar type, then, if this is a valid program,
8648 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8649 is type dependent, it can be pseudo-destructor-name or something else.
8650 Try to parse it as pseudo-destructor-name first. */
8651 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8653 tree s;
8654 tree type;
8656 cp_parser_parse_tentatively (parser);
8657 /* Parse the pseudo-destructor-name. */
8658 s = NULL_TREE;
8659 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8660 &s, &type);
8661 if (dependent_p
8662 && (cp_parser_error_occurred (parser)
8663 || !SCALAR_TYPE_P (type)))
8664 cp_parser_abort_tentative_parse (parser);
8665 else if (cp_parser_parse_definitely (parser))
8667 pseudo_destructor_p = true;
8668 postfix_expression
8669 = finish_pseudo_destructor_expr (postfix_expression,
8670 s, type, location);
8674 if (!pseudo_destructor_p)
8676 /* If the SCOPE is not a scalar type, we are looking at an
8677 ordinary class member access expression, rather than a
8678 pseudo-destructor-name. */
8679 bool template_p;
8680 cp_token *token = cp_lexer_peek_token (parser->lexer);
8681 /* Parse the id-expression. */
8682 name = (cp_parser_id_expression
8683 (parser,
8684 cp_parser_optional_template_keyword (parser),
8685 /*check_dependency_p=*/true,
8686 &template_p,
8687 /*declarator_p=*/false,
8688 /*optional_p=*/false));
8689 /* In general, build a SCOPE_REF if the member name is qualified.
8690 However, if the name was not dependent and has already been
8691 resolved; there is no need to build the SCOPE_REF. For example;
8693 struct X { void f(); };
8694 template <typename T> void f(T* t) { t->X::f(); }
8696 Even though "t" is dependent, "X::f" is not and has been resolved
8697 to a BASELINK; there is no need to include scope information. */
8699 /* But we do need to remember that there was an explicit scope for
8700 virtual function calls. */
8701 if (parser->scope)
8702 *idk = CP_ID_KIND_QUALIFIED;
8704 /* If the name is a template-id that names a type, we will get a
8705 TYPE_DECL here. That is invalid code. */
8706 if (TREE_CODE (name) == TYPE_DECL)
8708 error_at (token->location, "invalid use of %qD", name);
8709 postfix_expression = error_mark_node;
8711 else
8713 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8715 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8717 error_at (token->location, "%<%D::%D%> is not a class member",
8718 parser->scope, name);
8719 postfix_expression = error_mark_node;
8721 else
8722 name = build_qualified_name (/*type=*/NULL_TREE,
8723 parser->scope,
8724 name,
8725 template_p);
8726 parser->scope = NULL_TREE;
8727 parser->qualifying_scope = NULL_TREE;
8728 parser->object_scope = NULL_TREE;
8730 if (parser->scope && name && BASELINK_P (name))
8731 adjust_result_of_qualified_name_lookup
8732 (name, parser->scope, scope);
8733 postfix_expression
8734 = finish_class_member_access_expr (postfix_expression, name,
8735 template_p,
8736 tf_warning_or_error);
8737 /* Build a location e.g.:
8738 ptr->access_expr
8739 ~~~^~~~~~~~~~~~~
8740 where the caret is at the deref token, ranging from
8741 the start of postfix_expression to the end of the access expr. */
8742 location_t combined_loc
8743 = make_location (input_location, start_loc, parser->lexer);
8744 protected_set_expr_location (postfix_expression, combined_loc);
8748 /* We no longer need to look up names in the scope of the object on
8749 the left-hand side of the `.' or `->' operator. */
8750 parser->context->object_type = NULL_TREE;
8752 /* Outside of offsetof, these operators may not appear in
8753 constant-expressions. */
8754 if (!for_offsetof
8755 && (cp_parser_non_integral_constant_expression
8756 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8757 postfix_expression = error_mark_node;
8759 return postfix_expression;
8762 /* Parse a parenthesized expression-list.
8764 expression-list:
8765 assignment-expression
8766 expression-list, assignment-expression
8768 attribute-list:
8769 expression-list
8770 identifier
8771 identifier, expression-list
8773 CAST_P is true if this expression is the target of a cast.
8775 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8776 argument pack.
8778 WRAP_LOCATIONS_P is true if expressions within this list for which
8779 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8780 their source locations.
8782 Returns a vector of trees. Each element is a representation of an
8783 assignment-expression. NULL is returned if the ( and or ) are
8784 missing. An empty, but allocated, vector is returned on no
8785 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8786 if we are parsing an attribute list for an attribute that wants a
8787 plain identifier argument, normal_attr for an attribute that wants
8788 an expression, or non_attr if we aren't parsing an attribute list. If
8789 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8790 not all of the expressions in the list were constant.
8791 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8792 will be written to with the location of the closing parenthesis. If
8793 an error occurs, it may or may not be written to. */
8795 static vec<tree, va_gc> *
8796 cp_parser_parenthesized_expression_list (cp_parser* parser,
8797 int is_attribute_list,
8798 bool cast_p,
8799 bool allow_expansion_p,
8800 bool *non_constant_p,
8801 location_t *close_paren_loc,
8802 bool wrap_locations_p)
8804 vec<tree, va_gc> *expression_list;
8805 bool saved_greater_than_is_operator_p;
8806 bool saved_omp_array_section_p;
8808 /* Assume all the expressions will be constant. */
8809 if (non_constant_p)
8810 *non_constant_p = false;
8812 matching_parens parens;
8813 if (!parens.require_open (parser))
8814 return NULL;
8816 expression_list = make_tree_vector ();
8818 /* Within a parenthesized expression, a `>' token is always
8819 the greater-than operator. */
8820 saved_greater_than_is_operator_p
8821 = parser->greater_than_is_operator_p;
8822 parser->greater_than_is_operator_p = true;
8824 saved_omp_array_section_p = parser->omp_array_section_p;
8825 parser->omp_array_section_p = false;
8827 cp_expr expr (NULL_TREE);
8829 /* Consume expressions until there are no more. */
8830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8831 while (true)
8833 /* At the beginning of attribute lists, check to see if the
8834 next token is an identifier. */
8835 if (is_attribute_list == id_attr
8836 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8837 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8838 else if (is_attribute_list == assume_attr)
8839 expr = cp_parser_conditional_expression (parser);
8840 else if (is_attribute_list == uneval_string_attr)
8841 expr = cp_parser_unevaluated_string_literal (parser);
8842 else
8843 expr
8844 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8845 allow_expansion_p,
8846 non_constant_p);
8848 if (wrap_locations_p)
8849 expr.maybe_add_location_wrapper ();
8851 /* Add it to the list. We add error_mark_node
8852 expressions to the list, so that we can still tell if
8853 the correct form for a parenthesized expression-list
8854 is found. That gives better errors. */
8855 vec_safe_push (expression_list, expr.get_value ());
8857 if (expr == error_mark_node)
8858 goto skip_comma;
8860 /* After the first item, attribute lists look the same as
8861 expression lists. */
8862 is_attribute_list = non_attr;
8864 get_comma:;
8865 /* If the next token isn't a `,', then we are done. */
8866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8867 break;
8869 /* Otherwise, consume the `,' and keep going. */
8870 cp_lexer_consume_token (parser->lexer);
8873 if (close_paren_loc)
8874 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8876 if (!parens.require_close (parser))
8878 int ending;
8880 skip_comma:;
8881 /* We try and resync to an unnested comma, as that will give the
8882 user better diagnostics. */
8883 ending = cp_parser_skip_to_closing_parenthesis (parser,
8884 /*recovering=*/true,
8885 /*or_comma=*/true,
8886 /*consume_paren=*/true);
8887 if (ending < 0)
8888 goto get_comma;
8889 if (!ending)
8891 parser->greater_than_is_operator_p
8892 = saved_greater_than_is_operator_p;
8893 parser->omp_array_section_p = saved_omp_array_section_p;
8894 return NULL;
8898 parser->greater_than_is_operator_p
8899 = saved_greater_than_is_operator_p;
8900 parser->omp_array_section_p = saved_omp_array_section_p;
8902 return expression_list;
8905 /* Parse a pseudo-destructor-name.
8907 pseudo-destructor-name:
8908 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8909 :: [opt] nested-name-specifier template template-id :: ~ type-name
8910 :: [opt] nested-name-specifier [opt] ~ type-name
8912 If either of the first two productions is used, sets *SCOPE to the
8913 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8914 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8915 or ERROR_MARK_NODE if the parse fails. */
8917 static void
8918 cp_parser_pseudo_destructor_name (cp_parser* parser,
8919 tree object,
8920 tree* scope,
8921 tree* type)
8923 bool nested_name_specifier_p;
8925 /* Handle ~auto. */
8926 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8927 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8928 && !type_dependent_expression_p (object))
8930 if (cxx_dialect < cxx14)
8931 pedwarn (input_location, OPT_Wc__14_extensions,
8932 "%<~auto%> only available with "
8933 "%<-std=c++14%> or %<-std=gnu++14%>");
8934 cp_lexer_consume_token (parser->lexer);
8935 cp_lexer_consume_token (parser->lexer);
8936 *scope = NULL_TREE;
8937 *type = TREE_TYPE (object);
8938 return;
8941 /* Assume that things will not work out. */
8942 *type = error_mark_node;
8944 /* Look for the optional `::' operator. */
8945 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8946 /* Look for the optional nested-name-specifier. */
8947 nested_name_specifier_p
8948 = (cp_parser_nested_name_specifier_opt (parser,
8949 /*typename_keyword_p=*/false,
8950 /*check_dependency_p=*/true,
8951 /*type_p=*/false,
8952 /*is_declaration=*/false)
8953 != NULL_TREE);
8954 /* Now, if we saw a nested-name-specifier, we might be doing the
8955 second production. */
8956 if (nested_name_specifier_p
8957 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8959 /* Consume the `template' keyword. */
8960 cp_lexer_consume_token (parser->lexer);
8961 /* Parse the template-id. */
8962 cp_parser_template_id (parser,
8963 /*template_keyword_p=*/true,
8964 /*check_dependency_p=*/false,
8965 class_type,
8966 /*is_declaration=*/true);
8967 /* Look for the `::' token. */
8968 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8970 /* If the next token is not a `~', then there might be some
8971 additional qualification. */
8972 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8974 /* At this point, we're looking for "type-name :: ~". The type-name
8975 must not be a class-name, since this is a pseudo-destructor. So,
8976 it must be either an enum-name, or a typedef-name -- both of which
8977 are just identifiers. So, we peek ahead to check that the "::"
8978 and "~" tokens are present; if they are not, then we can avoid
8979 calling type_name. */
8980 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8981 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8982 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8984 cp_parser_error (parser, "non-scalar type");
8985 return;
8988 /* Look for the type-name. */
8989 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8990 if (*scope == error_mark_node)
8991 return;
8993 /* Look for the `::' token. */
8994 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8996 else
8997 *scope = NULL_TREE;
8999 /* Look for the `~'. */
9000 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
9002 /* Once we see the ~, this has to be a pseudo-destructor. */
9003 if (!processing_template_decl && !cp_parser_error_occurred (parser))
9004 cp_parser_commit_to_topmost_tentative_parse (parser);
9006 /* Look for the type-name again. We are not responsible for
9007 checking that it matches the first type-name. */
9008 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
9011 /* Parse a unary-expression.
9013 unary-expression:
9014 postfix-expression
9015 ++ cast-expression
9016 -- cast-expression
9017 await-expression
9018 unary-operator cast-expression
9019 sizeof unary-expression
9020 sizeof ( type-id )
9021 alignof ( type-id ) [C++0x]
9022 new-expression
9023 delete-expression
9025 GNU Extensions:
9027 unary-expression:
9028 __extension__ cast-expression
9029 __alignof__ unary-expression
9030 __alignof__ ( type-id )
9031 alignof unary-expression [C++0x]
9032 __real__ cast-expression
9033 __imag__ cast-expression
9034 && identifier
9035 sizeof ( type-id ) { initializer-list , [opt] }
9036 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
9037 __alignof__ ( type-id ) { initializer-list , [opt] }
9039 ADDRESS_P is true iff the unary-expression is appearing as the
9040 operand of the `&' operator. CAST_P is true if this expression is
9041 the target of a cast.
9043 Returns a representation of the expression. */
9045 static cp_expr
9046 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
9047 bool address_p, bool cast_p, bool decltype_p)
9049 cp_token *token;
9050 enum tree_code unary_operator;
9052 /* Peek at the next token. */
9053 token = cp_lexer_peek_token (parser->lexer);
9054 /* Some keywords give away the kind of expression. */
9055 if (token->type == CPP_KEYWORD)
9057 enum rid keyword = token->keyword;
9059 switch (keyword)
9061 case RID_ALIGNOF:
9062 case RID_SIZEOF:
9064 tree operand, ret;
9065 enum tree_code op;
9066 location_t start_loc = token->location;
9068 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
9069 bool std_alignof = id_equal (token->u.value, "alignof");
9071 /* Consume the token. */
9072 cp_lexer_consume_token (parser->lexer);
9073 /* Parse the operand. */
9074 operand = cp_parser_sizeof_operand (parser, keyword);
9076 /* Construct a location e.g. :
9077 alignof (expr)
9078 ^~~~~~~~~~~~~~
9079 with start == caret at the start of the "alignof"/"sizeof"
9080 token, with the endpoint at the final closing paren. */
9081 location_t compound_loc
9082 = make_location (start_loc, start_loc, parser->lexer);
9084 if (TYPE_P (operand))
9085 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
9086 std_alignof, true);
9087 else
9089 /* ISO C++ defines alignof only with types, not with
9090 expressions. So pedwarn if alignof is used with a non-
9091 type expression. However, __alignof__ is ok. */
9092 if (std_alignof)
9093 pedwarn (token->location, OPT_Wpedantic,
9094 "ISO C++ does not allow %<alignof%> "
9095 "with a non-type");
9097 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
9098 std_alignof, true);
9100 /* For SIZEOF_EXPR, just issue diagnostics, but keep
9101 SIZEOF_EXPR with the original operand. */
9102 if (op == SIZEOF_EXPR && ret != error_mark_node)
9104 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
9106 if (!processing_template_decl && TYPE_P (operand))
9108 ret = build_min (SIZEOF_EXPR, size_type_node,
9109 build1 (NOP_EXPR, operand,
9110 error_mark_node));
9111 SIZEOF_EXPR_TYPE_P (ret) = 1;
9113 else
9114 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
9115 TREE_SIDE_EFFECTS (ret) = 0;
9116 TREE_READONLY (ret) = 1;
9117 SET_EXPR_LOCATION (ret, compound_loc);
9121 cp_expr ret_expr (ret, compound_loc);
9122 ret_expr = ret_expr.maybe_add_location_wrapper ();
9123 return ret_expr;
9126 case RID_BUILTIN_HAS_ATTRIBUTE:
9127 return cp_parser_has_attribute_expression (parser);
9129 case RID_NEW:
9130 return cp_parser_new_expression (parser);
9132 case RID_DELETE:
9133 return cp_parser_delete_expression (parser);
9135 case RID_EXTENSION:
9137 /* The saved value of the PEDANTIC flag. */
9138 int saved_pedantic;
9139 tree expr;
9141 /* Save away the PEDANTIC flag. */
9142 cp_parser_extension_opt (parser, &saved_pedantic);
9143 /* Parse the cast-expression. */
9144 expr = cp_parser_simple_cast_expression (parser);
9145 /* Restore the PEDANTIC flag. */
9146 pedantic = saved_pedantic;
9148 return expr;
9151 case RID_REALPART:
9152 case RID_IMAGPART:
9154 tree expression;
9156 /* Consume the `__real__' or `__imag__' token. */
9157 cp_lexer_consume_token (parser->lexer);
9158 /* Parse the cast-expression. */
9159 expression = cp_parser_simple_cast_expression (parser);
9160 /* Create the complete representation. */
9161 return build_x_unary_op (token->location,
9162 (keyword == RID_REALPART
9163 ? REALPART_EXPR : IMAGPART_EXPR),
9164 expression, NULL_TREE,
9165 tf_warning_or_error);
9167 break;
9169 case RID_TRANSACTION_ATOMIC:
9170 case RID_TRANSACTION_RELAXED:
9171 return cp_parser_transaction_expression (parser, keyword);
9173 case RID_NOEXCEPT:
9175 tree expr;
9176 const char *saved_message;
9177 bool saved_integral_constant_expression_p;
9178 bool saved_non_integral_constant_expression_p;
9179 bool saved_greater_than_is_operator_p;
9181 location_t start_loc = token->location;
9183 cp_lexer_consume_token (parser->lexer);
9184 matching_parens parens;
9185 parens.require_open (parser);
9187 saved_message = parser->type_definition_forbidden_message;
9188 parser->type_definition_forbidden_message
9189 = G_("types may not be defined in %<noexcept%> expressions");
9191 saved_integral_constant_expression_p
9192 = parser->integral_constant_expression_p;
9193 saved_non_integral_constant_expression_p
9194 = parser->non_integral_constant_expression_p;
9195 parser->integral_constant_expression_p = false;
9197 saved_greater_than_is_operator_p
9198 = parser->greater_than_is_operator_p;
9199 parser->greater_than_is_operator_p = true;
9201 ++cp_unevaluated_operand;
9202 ++c_inhibit_evaluation_warnings;
9203 ++cp_noexcept_operand;
9204 expr = cp_parser_expression (parser);
9205 --cp_noexcept_operand;
9206 --c_inhibit_evaluation_warnings;
9207 --cp_unevaluated_operand;
9209 parser->greater_than_is_operator_p
9210 = saved_greater_than_is_operator_p;
9212 parser->integral_constant_expression_p
9213 = saved_integral_constant_expression_p;
9214 parser->non_integral_constant_expression_p
9215 = saved_non_integral_constant_expression_p;
9217 parser->type_definition_forbidden_message = saved_message;
9219 parens.require_close (parser);
9221 /* Construct a location of the form:
9222 noexcept (expr)
9223 ^~~~~~~~~~~~~~~
9224 with start == caret, finishing at the close-paren. */
9225 location_t noexcept_loc
9226 = make_location (start_loc, start_loc, parser->lexer);
9228 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9229 noexcept_loc);
9232 case RID_CO_AWAIT:
9234 tree expr;
9235 location_t kw_loc = token->location;
9237 /* Consume the `co_await' token. */
9238 cp_lexer_consume_token (parser->lexer);
9239 /* Parse its cast-expression. */
9240 expr = cp_parser_simple_cast_expression (parser);
9241 if (expr == error_mark_node)
9242 return error_mark_node;
9244 /* Handle [expr.await]. */
9245 return cp_expr (finish_co_await_expr (kw_loc, expr));
9248 default:
9249 break;
9253 /* Look for the `:: new' and `:: delete', which also signal the
9254 beginning of a new-expression, or delete-expression,
9255 respectively. If the next token is `::', then it might be one of
9256 these. */
9257 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9259 enum rid keyword;
9261 /* See if the token after the `::' is one of the keywords in
9262 which we're interested. */
9263 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9264 /* If it's `new', we have a new-expression. */
9265 if (keyword == RID_NEW)
9266 return cp_parser_new_expression (parser);
9267 /* Similarly, for `delete'. */
9268 else if (keyword == RID_DELETE)
9269 return cp_parser_delete_expression (parser);
9272 /* Look for a unary operator. */
9273 unary_operator = cp_parser_unary_operator (token);
9274 /* The `++' and `--' operators can be handled similarly, even though
9275 they are not technically unary-operators in the grammar. */
9276 if (unary_operator == ERROR_MARK)
9278 if (token->type == CPP_PLUS_PLUS)
9279 unary_operator = PREINCREMENT_EXPR;
9280 else if (token->type == CPP_MINUS_MINUS)
9281 unary_operator = PREDECREMENT_EXPR;
9282 /* Handle the GNU address-of-label extension. */
9283 else if (cp_parser_allow_gnu_extensions_p (parser)
9284 && token->type == CPP_AND_AND)
9286 tree identifier;
9287 tree expression;
9288 location_t start_loc = token->location;
9290 /* Consume the '&&' token. */
9291 cp_lexer_consume_token (parser->lexer);
9292 /* Look for the identifier. */
9293 identifier = cp_parser_identifier (parser);
9294 /* Construct a location of the form:
9295 &&label
9296 ^~~~~~~
9297 with caret==start at the "&&", finish at the end of the label. */
9298 location_t combined_loc
9299 = make_location (start_loc, start_loc, parser->lexer);
9300 /* Create an expression representing the address. */
9301 expression = finish_label_address_expr (identifier, combined_loc);
9302 if (TREE_CODE (expression) == ADDR_EXPR)
9303 mark_label_addressed (identifier);
9304 if (cp_parser_non_integral_constant_expression (parser,
9305 NIC_ADDR_LABEL))
9306 expression = error_mark_node;
9307 return expression;
9310 if (unary_operator != ERROR_MARK)
9312 cp_expr cast_expression;
9313 cp_expr expression = error_mark_node;
9314 non_integral_constant non_constant_p = NIC_NONE;
9315 location_t loc = token->location;
9316 tsubst_flags_t complain = complain_flags (decltype_p);
9318 /* Consume the operator token. */
9319 token = cp_lexer_consume_token (parser->lexer);
9320 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9322 /* Parse the cast-expression. */
9323 cast_expression
9324 = cp_parser_cast_expression (parser,
9325 unary_operator == ADDR_EXPR,
9326 /*cast_p=*/false,
9327 /*decltype*/false,
9328 pidk);
9330 /* Make a location:
9331 OP_TOKEN CAST_EXPRESSION
9332 ^~~~~~~~~~~~~~~~~~~~~~~~~
9333 with start==caret at the operator token, and
9334 extending to the end of the cast_expression. */
9335 loc = make_location (loc, loc, cast_expression.get_finish ());
9337 /* Now, build an appropriate representation. */
9338 switch (unary_operator)
9340 case INDIRECT_REF:
9341 non_constant_p = NIC_STAR;
9342 expression = build_x_indirect_ref (loc, cast_expression,
9343 RO_UNARY_STAR, NULL_TREE,
9344 complain);
9345 /* TODO: build_x_indirect_ref does not always honor the
9346 location, so ensure it is set. */
9347 expression.set_location (loc);
9348 break;
9350 case ADDR_EXPR:
9351 non_constant_p = NIC_ADDR;
9352 /* Fall through. */
9353 case BIT_NOT_EXPR:
9354 expression = build_x_unary_op (loc, unary_operator,
9355 cast_expression,
9356 NULL_TREE, complain);
9357 /* TODO: build_x_unary_op does not always honor the location,
9358 so ensure it is set. */
9359 expression.set_location (loc);
9360 break;
9362 case PREINCREMENT_EXPR:
9363 case PREDECREMENT_EXPR:
9364 non_constant_p = unary_operator == PREINCREMENT_EXPR
9365 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9366 /* Fall through. */
9367 case NEGATE_EXPR:
9368 /* Immediately fold negation of a constant, unless the constant is 0
9369 (since -0 == 0) or it would overflow. */
9370 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9372 tree stripped_expr
9373 = tree_strip_any_location_wrapper (cast_expression);
9374 if (CONSTANT_CLASS_P (stripped_expr)
9375 && !integer_zerop (stripped_expr)
9376 && !TREE_OVERFLOW (stripped_expr))
9378 tree folded = fold_build1 (unary_operator,
9379 TREE_TYPE (stripped_expr),
9380 stripped_expr);
9381 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9383 expression = maybe_wrap_with_location (folded, loc);
9384 break;
9388 /* Fall through. */
9389 case UNARY_PLUS_EXPR:
9390 case TRUTH_NOT_EXPR:
9391 expression = finish_unary_op_expr (loc, unary_operator,
9392 cast_expression, complain);
9393 break;
9395 default:
9396 gcc_unreachable ();
9399 if (non_constant_p != NIC_NONE
9400 && cp_parser_non_integral_constant_expression (parser,
9401 non_constant_p))
9402 expression = error_mark_node;
9404 return expression;
9407 return cp_parser_postfix_expression (parser, address_p, cast_p,
9408 /*member_access_only_p=*/false,
9409 decltype_p,
9410 pidk);
9413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9414 unary-operator, the corresponding tree code is returned. */
9416 static enum tree_code
9417 cp_parser_unary_operator (cp_token* token)
9419 switch (token->type)
9421 case CPP_MULT:
9422 return INDIRECT_REF;
9424 case CPP_AND:
9425 return ADDR_EXPR;
9427 case CPP_PLUS:
9428 return UNARY_PLUS_EXPR;
9430 case CPP_MINUS:
9431 return NEGATE_EXPR;
9433 case CPP_NOT:
9434 return TRUTH_NOT_EXPR;
9436 case CPP_COMPL:
9437 return BIT_NOT_EXPR;
9439 default:
9440 return ERROR_MARK;
9444 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9445 Returns a representation of the expression. */
9447 static tree
9448 cp_parser_has_attribute_expression (cp_parser *parser)
9450 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9452 /* Consume the __builtin_has_attribute token. */
9453 cp_lexer_consume_token (parser->lexer);
9455 matching_parens parens;
9456 if (!parens.require_open (parser))
9457 return error_mark_node;
9459 /* Types cannot be defined in a `sizeof' expression. Save away the
9460 old message. */
9461 const char *saved_message = parser->type_definition_forbidden_message;
9462 const char *saved_message_arg
9463 = parser->type_definition_forbidden_message_arg;
9464 parser->type_definition_forbidden_message
9465 = G_("types may not be defined in %qs expressions");
9466 parser->type_definition_forbidden_message_arg
9467 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9469 /* The restrictions on constant-expressions do not apply inside
9470 sizeof expressions. */
9471 bool saved_integral_constant_expression_p
9472 = parser->integral_constant_expression_p;
9473 bool saved_non_integral_constant_expression_p
9474 = parser->non_integral_constant_expression_p;
9475 parser->integral_constant_expression_p = false;
9477 /* Do not actually evaluate the expression. */
9478 ++cp_unevaluated_operand;
9479 ++c_inhibit_evaluation_warnings;
9481 tree oper = NULL_TREE;
9483 /* We can't be sure yet whether we're looking at a type-id or an
9484 expression. */
9485 cp_parser_parse_tentatively (parser);
9487 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9488 parser->in_type_id_in_expr_p = true;
9489 /* Look for the type-id. */
9490 oper = cp_parser_type_id (parser);
9491 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9493 cp_parser_parse_definitely (parser);
9495 /* If the type-id production did not work out, then we must be
9496 looking at an expression. */
9497 if (!oper || oper == error_mark_node)
9498 oper = cp_parser_assignment_expression (parser);
9500 STRIP_ANY_LOCATION_WRAPPER (oper);
9502 /* Go back to evaluating expressions. */
9503 --cp_unevaluated_operand;
9504 --c_inhibit_evaluation_warnings;
9506 /* And restore the old one. */
9507 parser->type_definition_forbidden_message = saved_message;
9508 parser->type_definition_forbidden_message_arg = saved_message_arg;
9509 parser->integral_constant_expression_p
9510 = saved_integral_constant_expression_p;
9511 parser->non_integral_constant_expression_p
9512 = saved_non_integral_constant_expression_p;
9514 /* Consume the comma if it's there. */
9515 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9517 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9518 /*consume_paren=*/true);
9519 return error_mark_node;
9522 /* Parse the attribute specification. */
9523 bool ret = false;
9524 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9525 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9527 if (oper == error_mark_node)
9528 /* Nothing. */;
9529 else if (processing_template_decl && uses_template_parms (oper))
9530 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9531 "not supported yet");
9532 else
9534 /* Fold constant expressions used in attributes first. */
9535 cp_check_const_attributes (attr);
9537 /* Finally, see if OPER has been declared with ATTR. */
9538 ret = has_attribute (atloc, oper, attr, default_conversion);
9541 parens.require_close (parser);
9543 else
9545 error_at (atloc, "expected identifier");
9546 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9549 /* Construct a location e.g. :
9550 __builtin_has_attribute (oper, attr)
9551 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9552 with start == caret at the start of the built-in token,
9553 and with the endpoint at the final closing paren. */
9554 location_t compound_loc
9555 = make_location (start_loc, start_loc, parser->lexer);
9557 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9558 ret_expr.set_location (compound_loc);
9559 ret_expr = ret_expr.maybe_add_location_wrapper ();
9560 return ret_expr;
9563 /* Parse a new-expression.
9565 new-expression:
9566 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9567 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9569 Returns a representation of the expression. */
9571 static tree
9572 cp_parser_new_expression (cp_parser* parser)
9574 bool global_scope_p;
9575 vec<tree, va_gc> *placement;
9576 tree type;
9577 vec<tree, va_gc> *initializer;
9578 tree nelts = NULL_TREE;
9579 tree ret;
9581 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9583 /* Look for the optional `::' operator. */
9584 global_scope_p
9585 = (cp_parser_global_scope_opt (parser,
9586 /*current_scope_valid_p=*/false)
9587 != NULL_TREE);
9588 /* Look for the `new' operator. */
9589 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9590 /* There's no easy way to tell a new-placement from the
9591 `( type-id )' construct. */
9592 cp_parser_parse_tentatively (parser);
9593 /* Look for a new-placement. */
9594 placement = cp_parser_new_placement (parser);
9595 /* If that didn't work out, there's no new-placement. */
9596 if (!cp_parser_parse_definitely (parser))
9598 if (placement != NULL)
9599 release_tree_vector (placement);
9600 placement = NULL;
9603 /* If the next token is a `(', then we have a parenthesized
9604 type-id. */
9605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9607 cp_token *token;
9608 const char *saved_message = parser->type_definition_forbidden_message;
9610 /* Consume the `('. */
9611 matching_parens parens;
9612 parens.consume_open (parser);
9614 /* Parse the type-id. */
9615 parser->type_definition_forbidden_message
9616 = G_("types may not be defined in a new-expression");
9618 type_id_in_expr_sentinel s (parser);
9619 type = cp_parser_type_id (parser);
9621 parser->type_definition_forbidden_message = saved_message;
9623 /* Look for the closing `)'. */
9624 parens.require_close (parser);
9625 token = cp_lexer_peek_token (parser->lexer);
9626 /* There should not be a direct-new-declarator in this production,
9627 but GCC used to allowed this, so we check and emit a sensible error
9628 message for this case. */
9629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9631 error_at (token->location,
9632 "array bound forbidden after parenthesized type-id");
9633 inform (token->location,
9634 "try removing the parentheses around the type-id");
9635 cp_parser_direct_new_declarator (parser);
9638 /* Otherwise, there must be a new-type-id. */
9639 else
9640 type = cp_parser_new_type_id (parser, &nelts);
9642 /* If the next token is a `(' or '{', then we have a new-initializer. */
9643 cp_token *token = cp_lexer_peek_token (parser->lexer);
9644 if (token->type == CPP_OPEN_PAREN
9645 || token->type == CPP_OPEN_BRACE)
9646 initializer = cp_parser_new_initializer (parser);
9647 else
9648 initializer = NULL;
9650 /* A new-expression may not appear in an integral constant
9651 expression. */
9652 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9653 ret = error_mark_node;
9654 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9655 of a new-type-id or type-id of a new-expression, the new-expression shall
9656 contain a new-initializer of the form ( assignment-expression )".
9657 Additionally, consistently with the spirit of DR 1467, we want to accept
9658 'new auto { 2 }' too. */
9659 else if ((ret = type_uses_auto (type))
9660 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9661 && (vec_safe_length (initializer) != 1
9662 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9663 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9665 error_at (token->location,
9666 "initialization of new-expression for type %<auto%> "
9667 "requires exactly one element");
9668 ret = error_mark_node;
9670 else
9672 /* Construct a location e.g.:
9673 ptr = new int[100]
9674 ^~~~~~~~~~~~
9675 with caret == start at the start of the "new" token, and the end
9676 at the end of the final token we consumed. */
9677 location_t combined_loc = make_location (start_loc, start_loc,
9678 parser->lexer);
9679 /* Create a representation of the new-expression. */
9680 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9681 global_scope_p, tf_warning_or_error);
9684 if (placement != NULL)
9685 release_tree_vector (placement);
9686 if (initializer != NULL)
9687 release_tree_vector (initializer);
9689 return ret;
9692 /* Parse a new-placement.
9694 new-placement:
9695 ( expression-list )
9697 Returns the same representation as for an expression-list. */
9699 static vec<tree, va_gc> *
9700 cp_parser_new_placement (cp_parser* parser)
9702 vec<tree, va_gc> *expression_list;
9704 /* Parse the expression-list. */
9705 expression_list = (cp_parser_parenthesized_expression_list
9706 (parser, non_attr, /*cast_p=*/false,
9707 /*allow_expansion_p=*/true,
9708 /*non_constant_p=*/NULL));
9710 if (expression_list && expression_list->is_empty ())
9711 error ("expected expression-list or type-id");
9713 return expression_list;
9716 /* Parse a new-type-id.
9718 new-type-id:
9719 type-specifier-seq new-declarator [opt]
9721 Returns the TYPE allocated. If the new-type-id indicates an array
9722 type, *NELTS is set to the number of elements in the last array
9723 bound; the TYPE will not include the last array bound. */
9725 static tree
9726 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9728 cp_decl_specifier_seq type_specifier_seq;
9729 cp_declarator *new_declarator;
9730 cp_declarator *declarator;
9731 cp_declarator *outer_declarator;
9732 const char *saved_message;
9734 /* The type-specifier sequence must not contain type definitions.
9735 (It cannot contain declarations of new types either, but if they
9736 are not definitions we will catch that because they are not
9737 complete.) */
9738 saved_message = parser->type_definition_forbidden_message;
9739 parser->type_definition_forbidden_message
9740 = G_("types may not be defined in a new-type-id");
9741 /* Parse the type-specifier-seq. */
9742 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9743 /*is_declaration=*/false,
9744 /*is_trailing_return=*/false,
9745 &type_specifier_seq);
9746 /* Restore the old message. */
9747 parser->type_definition_forbidden_message = saved_message;
9749 if (type_specifier_seq.type == error_mark_node)
9750 return error_mark_node;
9752 /* Parse the new-declarator. */
9753 new_declarator = cp_parser_new_declarator_opt (parser);
9755 /* Determine the number of elements in the last array dimension, if
9756 any. */
9757 *nelts = NULL_TREE;
9758 /* Skip down to the last array dimension. */
9759 declarator = new_declarator;
9760 outer_declarator = NULL;
9761 while (declarator && (declarator->kind == cdk_pointer
9762 || declarator->kind == cdk_ptrmem))
9764 outer_declarator = declarator;
9765 declarator = declarator->declarator;
9767 while (declarator
9768 && declarator->kind == cdk_array
9769 && declarator->declarator
9770 && declarator->declarator->kind == cdk_array)
9772 outer_declarator = declarator;
9773 declarator = declarator->declarator;
9776 if (declarator && declarator->kind == cdk_array)
9778 *nelts = declarator->u.array.bounds;
9779 if (*nelts == error_mark_node)
9780 *nelts = integer_one_node;
9782 if (*nelts == NULL_TREE)
9783 /* Leave [] in the declarator. */;
9784 else if (outer_declarator)
9785 outer_declarator->declarator = declarator->declarator;
9786 else
9787 new_declarator = NULL;
9790 return groktypename (&type_specifier_seq, new_declarator, false);
9793 /* Parse an (optional) new-declarator.
9795 new-declarator:
9796 ptr-operator new-declarator [opt]
9797 direct-new-declarator
9799 Returns the declarator. */
9801 static cp_declarator *
9802 cp_parser_new_declarator_opt (cp_parser* parser)
9804 enum tree_code code;
9805 tree type, std_attributes = NULL_TREE;
9806 cp_cv_quals cv_quals;
9808 /* We don't know if there's a ptr-operator next, or not. */
9809 cp_parser_parse_tentatively (parser);
9810 /* Look for a ptr-operator. */
9811 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9812 /* If that worked, look for more new-declarators. */
9813 if (cp_parser_parse_definitely (parser))
9815 cp_declarator *declarator;
9817 /* Parse another optional declarator. */
9818 declarator = cp_parser_new_declarator_opt (parser);
9820 declarator = cp_parser_make_indirect_declarator
9821 (code, type, cv_quals, declarator, std_attributes);
9823 return declarator;
9826 /* If the next token is a `[', there is a direct-new-declarator. */
9827 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9828 return cp_parser_direct_new_declarator (parser);
9830 return NULL;
9833 /* Parse a direct-new-declarator.
9835 direct-new-declarator:
9836 [ expression ]
9837 direct-new-declarator [constant-expression]
9841 static cp_declarator *
9842 cp_parser_direct_new_declarator (cp_parser* parser)
9844 cp_declarator *declarator = NULL;
9845 bool first_p = true;
9847 while (true)
9849 tree expression;
9850 cp_token *token;
9852 /* Look for the opening `['. */
9853 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9855 token = cp_lexer_peek_token (parser->lexer);
9856 if (token->type == CPP_CLOSE_SQUARE && first_p)
9857 expression = NULL_TREE;
9858 else
9859 expression = cp_parser_expression (parser);
9860 /* The standard requires that the expression have integral
9861 type. DR 74 adds enumeration types. We believe that the
9862 real intent is that these expressions be handled like the
9863 expression in a `switch' condition, which also allows
9864 classes with a single conversion to integral or
9865 enumeration type. */
9866 if (expression && !processing_template_decl)
9868 expression
9869 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9870 expression,
9871 /*complain=*/true);
9872 if (!expression)
9874 error_at (token->location,
9875 "expression in new-declarator must have integral "
9876 "or enumeration type");
9877 expression = error_mark_node;
9881 /* Look for the closing `]'. */
9882 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9884 /* Add this bound to the declarator. */
9885 declarator = make_array_declarator (declarator, expression);
9887 /* If the next token is not a `[', then there are no more
9888 bounds. */
9889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9890 break;
9891 first_p = false;
9894 return declarator;
9897 /* Parse a new-initializer.
9899 new-initializer:
9900 ( expression-list [opt] )
9901 braced-init-list
9903 Returns a representation of the expression-list. */
9905 static vec<tree, va_gc> *
9906 cp_parser_new_initializer (cp_parser* parser)
9908 vec<tree, va_gc> *expression_list;
9910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9912 cp_lexer_set_source_position (parser->lexer);
9913 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9914 tree t = cp_parser_braced_list (parser);
9915 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9916 expression_list = make_tree_vector_single (t);
9918 else
9919 expression_list = (cp_parser_parenthesized_expression_list
9920 (parser, non_attr, /*cast_p=*/false,
9921 /*allow_expansion_p=*/true,
9922 /*non_constant_p=*/NULL));
9924 return expression_list;
9927 /* Parse a delete-expression.
9929 delete-expression:
9930 :: [opt] delete cast-expression
9931 :: [opt] delete [ ] cast-expression
9933 Returns a representation of the expression. */
9935 static tree
9936 cp_parser_delete_expression (cp_parser* parser)
9938 bool global_scope_p;
9939 bool array_p;
9940 tree expression;
9941 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9943 /* Look for the optional `::' operator. */
9944 global_scope_p
9945 = (cp_parser_global_scope_opt (parser,
9946 /*current_scope_valid_p=*/false)
9947 != NULL_TREE);
9948 /* Look for the `delete' keyword. */
9949 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9950 /* See if the array syntax is in use. */
9951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9953 /* Consume the `[' token. */
9954 cp_lexer_consume_token (parser->lexer);
9955 /* Look for the `]' token. */
9956 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9957 /* Remember that this is the `[]' construct. */
9958 array_p = true;
9960 else
9961 array_p = false;
9963 /* Parse the cast-expression. */
9964 expression = cp_parser_simple_cast_expression (parser);
9966 /* A delete-expression may not appear in an integral constant
9967 expression. */
9968 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9969 return error_mark_node;
9971 /* Construct a location e.g.:
9972 delete [ ] ptr
9973 ^~~~~~~~~~~~~~
9974 with caret == start at the start of the "delete" token, and
9975 the end at the end of the final token we consumed. */
9976 location_t combined_loc = make_location (start_loc, start_loc,
9977 parser->lexer);
9978 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9979 global_scope_p, tf_warning_or_error);
9981 return expression;
9984 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9985 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9986 0 otherwise. */
9988 static int
9989 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9991 cp_token *token = cp_lexer_peek_token (parser->lexer);
9992 switch (token->type)
9994 case CPP_COMMA:
9995 case CPP_SEMICOLON:
9996 case CPP_QUERY:
9997 case CPP_COLON:
9998 case CPP_CLOSE_SQUARE:
9999 case CPP_CLOSE_PAREN:
10000 case CPP_CLOSE_BRACE:
10001 case CPP_OPEN_BRACE:
10002 case CPP_DOT:
10003 case CPP_DOT_STAR:
10004 case CPP_DEREF:
10005 case CPP_DEREF_STAR:
10006 case CPP_DIV:
10007 case CPP_MOD:
10008 case CPP_LSHIFT:
10009 case CPP_RSHIFT:
10010 case CPP_LESS:
10011 case CPP_GREATER:
10012 case CPP_LESS_EQ:
10013 case CPP_GREATER_EQ:
10014 case CPP_EQ_EQ:
10015 case CPP_NOT_EQ:
10016 case CPP_EQ:
10017 case CPP_MULT_EQ:
10018 case CPP_DIV_EQ:
10019 case CPP_MOD_EQ:
10020 case CPP_PLUS_EQ:
10021 case CPP_MINUS_EQ:
10022 case CPP_RSHIFT_EQ:
10023 case CPP_LSHIFT_EQ:
10024 case CPP_AND_EQ:
10025 case CPP_XOR_EQ:
10026 case CPP_OR_EQ:
10027 case CPP_XOR:
10028 case CPP_OR:
10029 case CPP_OR_OR:
10030 case CPP_EOF:
10031 case CPP_ELLIPSIS:
10032 return 0;
10034 case CPP_OPEN_PAREN:
10035 /* In ((type ()) () the last () isn't a valid cast-expression,
10036 so the whole must be parsed as postfix-expression. */
10037 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
10038 != CPP_CLOSE_PAREN;
10040 case CPP_OPEN_SQUARE:
10041 /* '[' may start a primary-expression in obj-c++ and in C++11,
10042 as a lambda-expression, eg, '(void)[]{}'. */
10043 if (cxx_dialect >= cxx11)
10044 return -1;
10045 return c_dialect_objc ();
10047 case CPP_PLUS_PLUS:
10048 case CPP_MINUS_MINUS:
10049 /* '++' and '--' may or may not start a cast-expression:
10051 struct T { void operator++(int); };
10052 void f() { (T())++; }
10056 int a;
10057 (int)++a; */
10058 return -1;
10060 default:
10061 return 1;
10065 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
10066 in the order: const_cast, static_cast, reinterpret_cast.
10068 Don't suggest dynamic_cast.
10070 Return the first legal cast kind found, or NULL otherwise. */
10072 static const char *
10073 get_cast_suggestion (tree dst_type, tree orig_expr)
10075 tree trial;
10077 /* Reuse the parser logic by attempting to build the various kinds of
10078 cast, with "complain" disabled.
10079 Identify the first such cast that is valid. */
10081 /* Don't attempt to run such logic within template processing. */
10082 if (processing_template_decl)
10083 return NULL;
10085 /* First try const_cast. */
10086 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
10087 if (trial != error_mark_node)
10088 return "const_cast";
10090 /* If that fails, try static_cast. */
10091 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
10092 if (trial != error_mark_node)
10093 return "static_cast";
10095 /* Finally, try reinterpret_cast. */
10096 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
10097 tf_none);
10098 if (trial != error_mark_node)
10099 return "reinterpret_cast";
10101 /* No such cast possible. */
10102 return NULL;
10105 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
10106 suggesting how to convert a C-style cast of the form:
10108 (DST_TYPE)ORIG_EXPR
10110 to a C++-style cast.
10112 The primary range of RICHLOC is asssumed to be that of the original
10113 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
10114 of the parens in the C-style cast. */
10116 static void
10117 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
10118 location_t close_paren_loc, tree orig_expr,
10119 tree dst_type)
10121 /* This function is non-trivial, so bail out now if the warning isn't
10122 going to be emitted. */
10123 if (!warn_old_style_cast)
10124 return;
10126 /* Try to find a legal C++ cast, trying them in order:
10127 const_cast, static_cast, reinterpret_cast. */
10128 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
10129 if (!cast_suggestion)
10130 return;
10132 /* Replace the open paren with "CAST_SUGGESTION<". */
10133 pretty_printer pp;
10134 pp_string (&pp, cast_suggestion);
10135 pp_less (&pp);
10136 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
10138 /* Replace the close paren with "> (". */
10139 rich_loc->add_fixit_replace (close_paren_loc, "> (");
10141 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
10142 rich_loc->add_fixit_insert_after (")");
10146 /* Parse a cast-expression.
10148 cast-expression:
10149 unary-expression
10150 ( type-id ) cast-expression
10152 ADDRESS_P is true iff the unary-expression is appearing as the
10153 operand of the `&' operator. CAST_P is true if this expression is
10154 the target of a cast.
10156 Returns a representation of the expression. */
10158 static cp_expr
10159 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10160 bool decltype_p, cp_id_kind * pidk)
10162 /* If it's a `(', then we might be looking at a cast. */
10163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10165 tree type = NULL_TREE;
10166 cp_expr expr (NULL_TREE);
10167 int cast_expression = 0;
10168 const char *saved_message;
10170 /* There's no way to know yet whether or not this is a cast.
10171 For example, `(int (3))' is a unary-expression, while `(int)
10172 3' is a cast. So, we resort to parsing tentatively. */
10173 cp_parser_parse_tentatively (parser);
10174 /* Types may not be defined in a cast. */
10175 saved_message = parser->type_definition_forbidden_message;
10176 parser->type_definition_forbidden_message
10177 = G_("types may not be defined in casts");
10178 /* Consume the `('. */
10179 matching_parens parens;
10180 cp_token *open_paren = parens.consume_open (parser);
10181 location_t open_paren_loc = open_paren->location;
10182 location_t close_paren_loc = UNKNOWN_LOCATION;
10184 /* A very tricky bit is that `(struct S) { 3 }' is a
10185 compound-literal (which we permit in C++ as an extension).
10186 But, that construct is not a cast-expression -- it is a
10187 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10188 is legal; if the compound-literal were a cast-expression,
10189 you'd need an extra set of parentheses.) But, if we parse
10190 the type-id, and it happens to be a class-specifier, then we
10191 will commit to the parse at that point, because we cannot
10192 undo the action that is done when creating a new class. So,
10193 then we cannot back up and do a postfix-expression.
10195 Another tricky case is the following (c++/29234):
10197 struct S { void operator () (); };
10199 void foo ()
10201 ( S()() );
10204 As a type-id we parse the parenthesized S()() as a function
10205 returning a function, groktypename complains and we cannot
10206 back up in this case either.
10208 Therefore, we scan ahead to the closing `)', and check to see
10209 if the tokens after the `)' can start a cast-expression. Otherwise
10210 we are dealing with an unary-expression, a postfix-expression
10211 or something else.
10213 Yet another tricky case, in C++11, is the following (c++/54891):
10215 (void)[]{};
10217 The issue is that usually, besides the case of lambda-expressions,
10218 the parenthesized type-id cannot be followed by '[', and, eg, we
10219 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10220 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10221 we don't commit, we try a cast-expression, then an unary-expression.
10223 Save tokens so that we can put them back. */
10224 cp_lexer_save_tokens (parser->lexer);
10226 /* We may be looking at a cast-expression. */
10227 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10228 /*consume_paren=*/true))
10229 cast_expression
10230 = cp_parser_tokens_start_cast_expression (parser);
10232 /* Roll back the tokens we skipped. */
10233 cp_lexer_rollback_tokens (parser->lexer);
10234 /* If we aren't looking at a cast-expression, simulate an error so
10235 that the call to cp_parser_error_occurred below returns true. */
10236 if (!cast_expression)
10237 cp_parser_simulate_error (parser);
10238 else
10240 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10241 parser->in_type_id_in_expr_p = true;
10242 /* Look for the type-id. */
10243 type = cp_parser_type_id (parser);
10244 /* Look for the closing `)'. */
10245 cp_token *close_paren = parens.require_close (parser);
10246 if (close_paren)
10247 close_paren_loc = close_paren->location;
10248 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10251 /* Restore the saved message. */
10252 parser->type_definition_forbidden_message = saved_message;
10254 /* At this point this can only be either a cast or a
10255 parenthesized ctor such as `(T ())' that looks like a cast to
10256 function returning T. */
10257 if (!cp_parser_error_occurred (parser))
10259 /* Only commit if the cast-expression doesn't start with
10260 '++', '--', or '[' in C++11. */
10261 if (cast_expression > 0)
10262 cp_parser_commit_to_topmost_tentative_parse (parser);
10264 expr = cp_parser_cast_expression (parser,
10265 /*address_p=*/false,
10266 /*cast_p=*/true,
10267 /*decltype_p=*/false,
10268 pidk);
10270 if (cp_parser_parse_definitely (parser))
10272 /* Warn about old-style casts, if so requested. */
10273 if (warn_old_style_cast
10274 && !in_system_header_at (input_location)
10275 && !VOID_TYPE_P (type)
10276 && current_lang_name != lang_name_c)
10278 gcc_rich_location rich_loc (input_location);
10279 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10280 expr, type);
10281 warning_at (&rich_loc, OPT_Wold_style_cast,
10282 "use of old-style cast to %q#T", type);
10285 /* Only type conversions to integral or enumeration types
10286 can be used in constant-expressions. */
10287 if (!cast_valid_in_integral_constant_expression_p (type)
10288 && cp_parser_non_integral_constant_expression (parser,
10289 NIC_CAST))
10290 return error_mark_node;
10292 /* Perform the cast. */
10293 /* Make a location:
10294 (TYPE) EXPR
10295 ^~~~~~~~~~~
10296 with start==caret at the open paren, extending to the
10297 end of "expr". */
10298 location_t cast_loc = make_location (open_paren_loc,
10299 open_paren_loc,
10300 expr.get_finish ());
10301 expr = build_c_cast (cast_loc, type, expr);
10302 return expr;
10305 else
10306 cp_parser_abort_tentative_parse (parser);
10309 /* If we get here, then it's not a cast, so it must be a
10310 unary-expression. */
10311 return cp_parser_unary_expression (parser, pidk, address_p,
10312 cast_p, decltype_p);
10315 /* Parse a binary expression of the general form:
10317 pm-expression:
10318 cast-expression
10319 pm-expression .* cast-expression
10320 pm-expression ->* cast-expression
10322 multiplicative-expression:
10323 pm-expression
10324 multiplicative-expression * pm-expression
10325 multiplicative-expression / pm-expression
10326 multiplicative-expression % pm-expression
10328 additive-expression:
10329 multiplicative-expression
10330 additive-expression + multiplicative-expression
10331 additive-expression - multiplicative-expression
10333 shift-expression:
10334 additive-expression
10335 shift-expression << additive-expression
10336 shift-expression >> additive-expression
10338 relational-expression:
10339 shift-expression
10340 relational-expression < shift-expression
10341 relational-expression > shift-expression
10342 relational-expression <= shift-expression
10343 relational-expression >= shift-expression
10345 GNU Extension:
10347 relational-expression:
10348 relational-expression <? shift-expression
10349 relational-expression >? shift-expression
10351 equality-expression:
10352 relational-expression
10353 equality-expression == relational-expression
10354 equality-expression != relational-expression
10356 and-expression:
10357 equality-expression
10358 and-expression & equality-expression
10360 exclusive-or-expression:
10361 and-expression
10362 exclusive-or-expression ^ and-expression
10364 inclusive-or-expression:
10365 exclusive-or-expression
10366 inclusive-or-expression | exclusive-or-expression
10368 logical-and-expression:
10369 inclusive-or-expression
10370 logical-and-expression && inclusive-or-expression
10372 logical-or-expression:
10373 logical-and-expression
10374 logical-or-expression || logical-and-expression
10376 All these are implemented with a single function like:
10378 binary-expression:
10379 simple-cast-expression
10380 binary-expression <token> binary-expression
10382 CAST_P is true if this expression is the target of a cast.
10384 The binops_by_token map is used to get the tree codes for each <token> type.
10385 binary-expressions are associated according to a precedence table. */
10387 #define TOKEN_PRECEDENCE(token) \
10388 (((token->type == CPP_GREATER \
10389 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10390 && !parser->greater_than_is_operator_p) \
10391 ? PREC_NOT_OPERATOR \
10392 : binops_by_token[token->type].prec)
10394 static cp_expr
10395 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10396 bool no_toplevel_fold_p,
10397 bool decltype_p,
10398 enum cp_parser_prec prec,
10399 cp_id_kind * pidk)
10401 cp_parser_expression_stack stack;
10402 cp_parser_expression_stack_entry *sp = &stack[0];
10403 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10404 cp_parser_expression_stack_entry current;
10405 cp_expr rhs;
10406 cp_token *token;
10407 enum tree_code rhs_type;
10408 enum cp_parser_prec new_prec, lookahead_prec;
10409 tree overload;
10411 /* Parse the first expression. */
10412 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10413 ? TRUTH_NOT_EXPR : ERROR_MARK);
10414 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10415 cast_p, decltype_p, pidk);
10416 current.prec = prec;
10418 if (cp_parser_error_occurred (parser))
10419 return error_mark_node;
10421 for (;;)
10423 /* Get an operator token. */
10424 token = cp_lexer_peek_token (parser->lexer);
10426 if (warn_cxx11_compat
10427 && token->type == CPP_RSHIFT
10428 && !parser->greater_than_is_operator_p)
10430 if (warning_at (token->location, OPT_Wc__11_compat,
10431 "%<>>%> operator is treated"
10432 " as two right angle brackets in C++11"))
10433 inform (token->location,
10434 "suggest parentheses around %<>>%> expression");
10437 new_prec = TOKEN_PRECEDENCE (token);
10438 if (new_prec != PREC_NOT_OPERATOR
10439 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10440 /* This is a fold-expression; handle it later. */
10441 new_prec = PREC_NOT_OPERATOR;
10443 /* Popping an entry off the stack means we completed a subexpression:
10444 - either we found a token which is not an operator (`>' where it is not
10445 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10446 will happen repeatedly;
10447 - or, we found an operator which has lower priority. This is the case
10448 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10449 parsing `3 * 4'. */
10450 if (new_prec <= current.prec)
10452 if (sp == stack)
10453 break;
10454 else
10455 goto pop;
10458 get_rhs:
10459 current.tree_type = binops_by_token[token->type].tree_type;
10460 current.loc = token->location;
10461 current.flags = token->flags;
10463 /* We used the operator token. */
10464 cp_lexer_consume_token (parser->lexer);
10466 /* For "false && x" or "true || x", x will never be executed;
10467 disable warnings while evaluating it. */
10468 if ((current.tree_type == TRUTH_ANDIF_EXPR
10469 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10470 || (current.tree_type == TRUTH_ORIF_EXPR
10471 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10473 disable_warnings_sp = sp;
10474 ++c_inhibit_evaluation_warnings;
10477 /* Extract another operand. It may be the RHS of this expression
10478 or the LHS of a new, higher priority expression. */
10479 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10480 ? TRUTH_NOT_EXPR : ERROR_MARK);
10481 rhs = cp_parser_simple_cast_expression (parser);
10483 /* Get another operator token. Look up its precedence to avoid
10484 building a useless (immediately popped) stack entry for common
10485 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10486 token = cp_lexer_peek_token (parser->lexer);
10487 lookahead_prec = TOKEN_PRECEDENCE (token);
10488 if (lookahead_prec != PREC_NOT_OPERATOR
10489 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10490 lookahead_prec = PREC_NOT_OPERATOR;
10491 if (lookahead_prec > new_prec)
10493 /* ... and prepare to parse the RHS of the new, higher priority
10494 expression. Since precedence levels on the stack are
10495 monotonically increasing, we do not have to care about
10496 stack overflows. */
10497 *sp = current;
10498 ++sp;
10499 current.lhs = rhs;
10500 current.lhs_type = rhs_type;
10501 current.prec = new_prec;
10502 new_prec = lookahead_prec;
10503 goto get_rhs;
10505 pop:
10506 lookahead_prec = new_prec;
10507 /* If the stack is not empty, we have parsed into LHS the right side
10508 (`4' in the example above) of an expression we had suspended.
10509 We can use the information on the stack to recover the LHS (`3')
10510 from the stack together with the tree code (`MULT_EXPR'), and
10511 the precedence of the higher level subexpression
10512 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10513 which will be used to actually build the additive expression. */
10514 rhs = current.lhs;
10515 rhs_type = current.lhs_type;
10516 --sp;
10517 current = *sp;
10520 /* Undo the disabling of warnings done above. */
10521 if (sp == disable_warnings_sp)
10523 disable_warnings_sp = NULL;
10524 --c_inhibit_evaluation_warnings;
10527 if (warn_logical_not_paren
10528 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10529 && current.lhs_type == TRUTH_NOT_EXPR
10530 /* Avoid warning for !!x == y. */
10531 && (TREE_CODE (current.lhs) != NE_EXPR
10532 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10533 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10534 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10535 /* Avoid warning for !b == y where b is boolean. */
10536 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10537 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10538 != BOOLEAN_TYPE))))
10539 /* Avoid warning for !!b == y where b is boolean. */
10540 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10541 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10542 && DECL_P (tree_strip_any_location_wrapper
10543 (TREE_OPERAND (current.lhs, 0)))))
10544 || TREE_TYPE (current.lhs) == NULL_TREE
10545 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10546 warn_logical_not_parentheses (current.loc, current.tree_type,
10547 current.lhs, maybe_constant_value (rhs));
10549 if (warn_xor_used_as_pow
10550 && current.tree_type == BIT_XOR_EXPR
10551 /* Don't warn for named "xor" (as opposed to '^'). */
10552 && !(current.flags & NAMED_OP)
10553 && current.lhs.decimal_p ()
10554 && rhs.decimal_p ())
10555 check_for_xor_used_as_pow
10556 (current.lhs.get_location (),
10557 tree_strip_any_location_wrapper (current.lhs),
10558 current.loc,
10559 rhs.get_location (),
10560 tree_strip_any_location_wrapper (rhs));
10562 overload = NULL;
10564 location_t combined_loc = make_location (current.loc,
10565 current.lhs.get_start (),
10566 rhs.get_finish ());
10568 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10569 ERROR_MARK for everything that is not a binary expression.
10570 This makes warn_about_parentheses miss some warnings that
10571 involve unary operators. For unary expressions we should
10572 pass the correct tree_code unless the unary expression was
10573 surrounded by parentheses.
10575 if (no_toplevel_fold_p
10576 && lookahead_prec <= current.prec
10577 && sp == stack)
10579 if (current.lhs == error_mark_node || rhs == error_mark_node)
10580 current.lhs = error_mark_node;
10581 else
10583 current.lhs.maybe_add_location_wrapper ();
10584 rhs.maybe_add_location_wrapper ();
10585 current.lhs
10586 = build_min (current.tree_type,
10587 TREE_CODE_CLASS (current.tree_type)
10588 == tcc_comparison
10589 ? boolean_type_node : TREE_TYPE (current.lhs),
10590 current.lhs.get_value (), rhs.get_value ());
10591 SET_EXPR_LOCATION (current.lhs, combined_loc);
10594 else
10596 op_location_t op_loc (current.loc, combined_loc);
10597 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10598 current.lhs, current.lhs_type,
10599 rhs, rhs_type, NULL_TREE, &overload,
10600 complain_flags (decltype_p));
10601 /* TODO: build_x_binary_op doesn't always honor the location. */
10602 current.lhs.set_location (combined_loc);
10604 current.lhs_type = current.tree_type;
10606 /* If the binary operator required the use of an overloaded operator,
10607 then this expression cannot be an integral constant-expression.
10608 An overloaded operator can be used even if both operands are
10609 otherwise permissible in an integral constant-expression if at
10610 least one of the operands is of enumeration type. */
10612 if (overload
10613 && cp_parser_non_integral_constant_expression (parser,
10614 NIC_OVERLOADED))
10615 return error_mark_node;
10618 return current.lhs;
10621 static cp_expr
10622 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10623 bool no_toplevel_fold_p,
10624 enum cp_parser_prec prec,
10625 cp_id_kind * pidk)
10627 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10628 /*decltype*/false, prec, pidk);
10631 /* Parse the `? expression : assignment-expression' part of a
10632 conditional-expression. The LOGICAL_OR_EXPR is the
10633 logical-or-expression that started the conditional-expression.
10634 Returns a representation of the entire conditional-expression.
10636 This routine is used by cp_parser_assignment_expression
10637 and cp_parser_conditional_expression.
10639 ? expression : assignment-expression
10641 GNU Extensions:
10643 ? : assignment-expression */
10645 static tree
10646 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10648 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10649 cp_expr assignment_expr;
10650 struct cp_token *token;
10651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10653 /* Consume the `?' token. */
10654 cp_lexer_consume_token (parser->lexer);
10655 token = cp_lexer_peek_token (parser->lexer);
10656 if (cp_parser_allow_gnu_extensions_p (parser)
10657 && token->type == CPP_COLON)
10659 pedwarn (token->location, OPT_Wpedantic,
10660 "ISO C++ does not allow %<?:%> with omitted middle operand");
10661 /* Implicit true clause. */
10662 expr = NULL_TREE;
10663 c_inhibit_evaluation_warnings +=
10664 folded_logical_or_expr == truthvalue_true_node;
10665 warn_for_omitted_condop (token->location, logical_or_expr);
10667 else
10669 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10670 parser->colon_corrects_to_scope_p = false;
10671 /* Parse the expression. */
10672 c_inhibit_evaluation_warnings +=
10673 folded_logical_or_expr == truthvalue_false_node;
10674 expr = cp_parser_expression (parser);
10675 c_inhibit_evaluation_warnings +=
10676 ((folded_logical_or_expr == truthvalue_true_node)
10677 - (folded_logical_or_expr == truthvalue_false_node));
10678 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10681 /* The next token should be a `:'. */
10682 cp_parser_require (parser, CPP_COLON, RT_COLON);
10683 /* Parse the assignment-expression. */
10684 assignment_expr = cp_parser_assignment_expression (parser);
10685 c_inhibit_evaluation_warnings -=
10686 folded_logical_or_expr == truthvalue_true_node;
10688 /* Make a location:
10689 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10690 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10691 with the caret at the "?", ranging from the start of
10692 the logical_or_expr to the end of the assignment_expr. */
10693 loc = make_location (loc,
10694 logical_or_expr.get_start (),
10695 assignment_expr.get_finish ());
10697 /* Build the conditional-expression. */
10698 return build_x_conditional_expr (loc, logical_or_expr,
10699 expr,
10700 assignment_expr,
10701 tf_warning_or_error);
10704 /* Parse a conditional-expression.
10706 conditional-expression:
10707 logical-or-expression
10708 logical-or-expression ? expression : assignment-expression
10710 GNU Extensions:
10712 logical-or-expression ? : assignment-expression */
10714 static cp_expr
10715 cp_parser_conditional_expression (cp_parser *parser)
10717 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10718 PREC_NOT_OPERATOR, NULL);
10719 /* If the next token is a `?' then we're actually looking at
10720 a conditional-expression; otherwise we're done. */
10721 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10722 return cp_parser_question_colon_clause (parser, expr);
10723 return expr;
10726 /* Parse an assignment-expression.
10728 assignment-expression:
10729 conditional-expression
10730 logical-or-expression assignment-operator assignment_expression
10731 throw-expression
10732 yield-expression
10734 CAST_P is true if this expression is the target of a cast.
10735 DECLTYPE_P is true if this expression is the operand of decltype.
10737 Returns a representation for the expression. */
10739 static cp_expr
10740 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10741 bool cast_p, bool decltype_p)
10743 cp_expr expr;
10745 /* If the next token is the `throw' keyword, then we're looking at
10746 a throw-expression. */
10747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10748 expr = cp_parser_throw_expression (parser);
10749 /* If the next token is the `co_yield' keyword, then we're looking at
10750 a yield-expression. */
10751 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10752 expr = cp_parser_yield_expression (parser);
10753 /* Otherwise, it must be that we are looking at a
10754 logical-or-expression. */
10755 else
10757 /* Parse the binary expressions (logical-or-expression). */
10758 expr = cp_parser_binary_expression (parser, cast_p, false,
10759 decltype_p,
10760 PREC_NOT_OPERATOR, pidk);
10761 /* If the next token is a `?' then we're actually looking at a
10762 conditional-expression. */
10763 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10764 return cp_parser_question_colon_clause (parser, expr);
10765 else
10767 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10769 /* If it's an assignment-operator, we're using the second
10770 production. */
10771 enum tree_code assignment_operator
10772 = cp_parser_assignment_operator_opt (parser);
10773 if (assignment_operator != ERROR_MARK)
10775 /* Parse the right-hand side of the assignment. */
10776 cp_expr rhs = cp_parser_initializer_clause (parser);
10778 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10781 /* An assignment may not appear in a
10782 constant-expression. */
10783 if (cp_parser_non_integral_constant_expression (parser,
10784 NIC_ASSIGNMENT))
10785 return error_mark_node;
10786 /* Build the assignment expression. Its default
10787 location:
10788 LHS = RHS
10789 ~~~~^~~~~
10790 is the location of the '=' token as the
10791 caret, ranging from the start of the lhs to the
10792 end of the rhs. */
10793 loc = make_location (loc,
10794 expr.get_start (),
10795 rhs.get_finish ());
10796 expr = build_x_modify_expr (loc, expr,
10797 assignment_operator,
10798 rhs, NULL_TREE,
10799 complain_flags (decltype_p));
10800 /* TODO: build_x_modify_expr doesn't honor the location,
10801 so we must set it here. */
10802 expr.set_location (loc);
10807 return expr;
10810 /* Parse an (optional) assignment-operator.
10812 assignment-operator: one of
10813 = *= /= %= += -= >>= <<= &= ^= |=
10815 GNU Extension:
10817 assignment-operator: one of
10818 <?= >?=
10820 If the next token is an assignment operator, the corresponding tree
10821 code is returned, and the token is consumed. For example, for
10822 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10823 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10824 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10825 operator, ERROR_MARK is returned. */
10827 static enum tree_code
10828 cp_parser_assignment_operator_opt (cp_parser* parser)
10830 enum tree_code op;
10831 cp_token *token;
10833 /* Peek at the next token. */
10834 token = cp_lexer_peek_token (parser->lexer);
10836 switch (token->type)
10838 case CPP_EQ:
10839 op = NOP_EXPR;
10840 break;
10842 case CPP_MULT_EQ:
10843 op = MULT_EXPR;
10844 break;
10846 case CPP_DIV_EQ:
10847 op = TRUNC_DIV_EXPR;
10848 break;
10850 case CPP_MOD_EQ:
10851 op = TRUNC_MOD_EXPR;
10852 break;
10854 case CPP_PLUS_EQ:
10855 op = PLUS_EXPR;
10856 break;
10858 case CPP_MINUS_EQ:
10859 op = MINUS_EXPR;
10860 break;
10862 case CPP_RSHIFT_EQ:
10863 op = RSHIFT_EXPR;
10864 break;
10866 case CPP_LSHIFT_EQ:
10867 op = LSHIFT_EXPR;
10868 break;
10870 case CPP_AND_EQ:
10871 op = BIT_AND_EXPR;
10872 break;
10874 case CPP_XOR_EQ:
10875 op = BIT_XOR_EXPR;
10876 break;
10878 case CPP_OR_EQ:
10879 op = BIT_IOR_EXPR;
10880 break;
10882 default:
10883 /* Nothing else is an assignment operator. */
10884 op = ERROR_MARK;
10887 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10888 if (op != ERROR_MARK
10889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10890 op = ERROR_MARK;
10892 /* If it was an assignment operator, consume it. */
10893 if (op != ERROR_MARK)
10894 cp_lexer_consume_token (parser->lexer);
10896 return op;
10899 /* Parse an expression.
10901 expression:
10902 assignment-expression
10903 expression , assignment-expression
10905 CAST_P is true if this expression is the target of a cast.
10906 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10907 except possibly parenthesized or on the RHS of a comma (N3276).
10908 WARN_COMMA_P is true if a comma should be diagnosed.
10910 Returns a representation of the expression. */
10912 static cp_expr
10913 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10914 bool cast_p, bool decltype_p, bool warn_comma_p)
10916 cp_expr expression = NULL_TREE;
10917 location_t loc = UNKNOWN_LOCATION;
10919 while (true)
10921 cp_expr assignment_expression;
10923 /* Parse the next assignment-expression. */
10924 assignment_expression
10925 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10927 /* We don't create a temporary for a call that is the immediate operand
10928 of decltype or on the RHS of a comma. But when we see a comma, we
10929 need to create a temporary for a call on the LHS. */
10930 if (decltype_p && !processing_template_decl
10931 && TREE_CODE (assignment_expression) == CALL_EXPR
10932 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10933 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10934 assignment_expression
10935 = build_cplus_new (TREE_TYPE (assignment_expression),
10936 assignment_expression, tf_warning_or_error);
10938 /* If this is the first assignment-expression, we can just
10939 save it away. */
10940 if (!expression)
10941 expression = assignment_expression;
10942 else
10944 /* Create a location with caret at the comma, ranging
10945 from the start of the LHS to the end of the RHS. */
10946 loc = make_location (loc,
10947 expression.get_start (),
10948 assignment_expression.get_finish ());
10949 expression = build_x_compound_expr (loc, expression,
10950 assignment_expression, NULL_TREE,
10951 complain_flags (decltype_p));
10952 expression.set_location (loc);
10954 /* If the next token is not a comma, or we're in a fold-expression, then
10955 we are done with the expression. */
10956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10957 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10958 break;
10959 /* Consume the `,'. */
10960 loc = cp_lexer_peek_token (parser->lexer)->location;
10961 if (warn_comma_p)
10963 /* [depr.comma.subscript]: A comma expression appearing as
10964 the expr-or-braced-init-list of a subscripting expression
10965 is deprecated. A parenthesized comma expression is not
10966 deprecated. */
10967 warning_at (loc, OPT_Wcomma_subscript,
10968 "top-level comma expression in array subscript "
10969 "is deprecated");
10970 warn_comma_p = false;
10972 cp_lexer_consume_token (parser->lexer);
10973 /* A comma operator cannot appear in a constant-expression. */
10974 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10975 expression = error_mark_node;
10978 return expression;
10981 /* Parse a constant-expression.
10983 constant-expression:
10984 conditional-expression
10986 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10987 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10988 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10989 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10990 greater than 1, this isn't really a constant-expression, only a
10991 potentially constant-evaluated expression. If STRICT_P is true,
10992 only parse a conditional-expression, otherwise parse an
10993 assignment-expression. See below for rationale. */
10995 static cp_expr
10996 cp_parser_constant_expression (cp_parser* parser,
10997 int allow_non_constant_p /* = 0 */,
10998 bool *non_constant_p /* = NULL */,
10999 bool strict_p /* = false */)
11001 /* It might seem that we could simply parse the
11002 conditional-expression, and then check to see if it were
11003 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
11004 one that the compiler can figure out is constant, possibly after
11005 doing some simplifications or optimizations. The standard has a
11006 precise definition of constant-expression, and we must honor
11007 that, even though it is somewhat more restrictive.
11009 For example:
11011 int i[(2, 3)];
11013 is not a legal declaration, because `(2, 3)' is not a
11014 constant-expression. The `,' operator is forbidden in a
11015 constant-expression. However, GCC's constant-folding machinery
11016 will fold this operation to an INTEGER_CST for `3'. */
11018 /* Save the old settings. */
11019 bool saved_integral_constant_expression_p
11020 = parser->integral_constant_expression_p;
11021 bool saved_allow_non_integral_constant_expression_p
11022 = parser->allow_non_integral_constant_expression_p;
11023 bool saved_non_integral_constant_expression_p
11024 = parser->non_integral_constant_expression_p;
11025 /* We are now parsing a constant-expression. */
11026 parser->integral_constant_expression_p = true;
11027 parser->allow_non_integral_constant_expression_p
11028 = (allow_non_constant_p || cxx_dialect >= cxx11);
11029 parser->non_integral_constant_expression_p = false;
11031 /* A manifestly constant-evaluated expression is evaluated even in an
11032 unevaluated operand. */
11033 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
11035 /* Although the grammar says "conditional-expression", when not STRICT_P,
11036 we parse an "assignment-expression", which also permits
11037 "throw-expression" and the use of assignment operators. In the case
11038 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
11039 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
11040 actually essential that we look for an assignment-expression.
11041 For example, cp_parser_initializer_clauses uses this function to
11042 determine whether a particular assignment-expression is in fact
11043 constant. */
11044 cp_expr expression;
11045 if (strict_p)
11046 expression = cp_parser_conditional_expression (parser);
11047 else
11048 expression = cp_parser_assignment_expression (parser);
11049 /* Restore the old settings. */
11050 parser->integral_constant_expression_p
11051 = saved_integral_constant_expression_p;
11052 parser->allow_non_integral_constant_expression_p
11053 = saved_allow_non_integral_constant_expression_p;
11054 if (cxx_dialect >= cxx11
11055 && (!allow_non_constant_p || non_constant_p))
11057 /* Require an rvalue constant expression here; that's what our
11058 callers expect. Reference constant expressions are handled
11059 separately in e.g. cp_parser_template_argument. */
11060 tree decay = expression;
11061 if (TREE_TYPE (expression)
11062 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
11063 decay = build_address (expression);
11064 bool is_const = is_rvalue_constant_expression (decay);
11065 parser->non_integral_constant_expression_p = !is_const;
11066 if (!is_const && !allow_non_constant_p)
11067 require_rvalue_constant_expression (decay);
11069 if (allow_non_constant_p && non_constant_p)
11070 *non_constant_p = parser->non_integral_constant_expression_p;
11071 parser->non_integral_constant_expression_p
11072 = saved_non_integral_constant_expression_p;
11074 return expression;
11077 /* Parse __builtin_offsetof.
11079 offsetof-expression:
11080 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
11082 offsetof-member-designator:
11083 id-expression
11084 | offsetof-member-designator "." id-expression
11085 | offsetof-member-designator "[" expression "]"
11086 | offsetof-member-designator "->" id-expression */
11088 static cp_expr
11089 cp_parser_builtin_offsetof (cp_parser *parser)
11091 int save_ice_p, save_non_ice_p;
11092 tree type;
11093 cp_expr expr;
11094 cp_id_kind dummy;
11095 cp_token *token;
11096 location_t finish_loc;
11098 /* We're about to accept non-integral-constant things, but will
11099 definitely yield an integral constant expression. Save and
11100 restore these values around our local parsing. */
11101 save_ice_p = parser->integral_constant_expression_p;
11102 save_non_ice_p = parser->non_integral_constant_expression_p;
11104 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11106 /* Consume the "__builtin_offsetof" token. */
11107 cp_lexer_consume_token (parser->lexer);
11108 /* Consume the opening `('. */
11109 matching_parens parens;
11110 parens.require_open (parser);
11111 /* Parse the type-id. */
11112 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11114 const char *saved_message = parser->type_definition_forbidden_message;
11115 parser->type_definition_forbidden_message
11116 = G_("types may not be defined within %<__builtin_offsetof%>");
11117 type = cp_parser_type_id (parser);
11118 parser->type_definition_forbidden_message = saved_message;
11120 /* Look for the `,'. */
11121 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11122 token = cp_lexer_peek_token (parser->lexer);
11124 /* Build the (type *)null that begins the traditional offsetof macro. */
11125 tree object_ptr
11126 = build_static_cast (input_location, build_pointer_type (type),
11127 null_pointer_node, tf_warning_or_error);
11129 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
11130 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
11131 true, &dummy, token->location);
11132 while (true)
11134 token = cp_lexer_peek_token (parser->lexer);
11135 switch (token->type)
11137 case CPP_OPEN_SQUARE:
11138 /* offsetof-member-designator "[" expression "]" */
11139 expr = cp_parser_postfix_open_square_expression (parser, expr,
11140 true, false);
11141 break;
11143 case CPP_DEREF:
11144 /* offsetof-member-designator "->" identifier */
11145 expr = grok_array_decl (token->location, expr, integer_zero_node,
11146 NULL, tf_warning_or_error);
11147 /* FALLTHRU */
11149 case CPP_DOT:
11150 /* offsetof-member-designator "." identifier */
11151 cp_lexer_consume_token (parser->lexer);
11152 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
11153 expr, true, &dummy,
11154 token->location);
11155 break;
11157 case CPP_CLOSE_PAREN:
11158 /* Consume the ")" token. */
11159 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11160 cp_lexer_consume_token (parser->lexer);
11161 goto success;
11163 default:
11164 /* Error. We know the following require will fail, but
11165 that gives the proper error message. */
11166 parens.require_close (parser);
11167 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11168 expr = error_mark_node;
11169 goto failure;
11173 success:
11174 /* Make a location of the form:
11175 __builtin_offsetof (struct s, f)
11176 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11177 with caret at the type-id, ranging from the start of the
11178 "_builtin_offsetof" token to the close paren. */
11179 loc = make_location (loc, start_loc, finish_loc);
11180 /* The result will be an INTEGER_CST, so we need to explicitly
11181 preserve the location. */
11182 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11184 failure:
11185 parser->integral_constant_expression_p = save_ice_p;
11186 parser->non_integral_constant_expression_p = save_non_ice_p;
11188 expr = expr.maybe_add_location_wrapper ();
11189 return expr;
11192 /* Parse a builtin trait expression or type. */
11194 static cp_expr
11195 cp_parser_trait (cp_parser* parser, const cp_trait* trait)
11197 const cp_trait_kind kind = trait->kind;
11198 tree type1, type2 = NULL_TREE;
11199 const bool binary = (trait->arity == 2);
11200 const bool variadic = (trait->arity == -1);
11201 const bool type = trait->type;
11203 /* Get location of initial token. */
11204 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11206 /* Consume the token. */
11207 cp_lexer_consume_token (parser->lexer);
11209 matching_parens parens;
11210 if (kind == CPTK_TYPE_PACK_ELEMENT)
11211 cp_parser_require (parser, CPP_LESS, RT_LESS);
11212 else
11213 parens.require_open (parser);
11215 if (kind == CPTK_IS_DEDUCIBLE)
11217 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11218 type1 = cp_parser_id_expression (parser,
11219 /*template_keyword_p=*/false,
11220 /*check_dependency_p=*/true,
11221 nullptr,
11222 /*declarator_p=*/false,
11223 /*optional_p=*/false);
11224 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11226 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11227 /* __type_pack_element takes an expression as its first argument and uses
11228 template-id syntax instead of function call syntax (for consistency
11229 with Clang). We special case these properties of __type_pack_element
11230 here and elsewhere. */
11231 type1 = cp_parser_constant_expression (parser);
11232 else
11234 type_id_in_expr_sentinel s (parser);
11235 type1 = cp_parser_type_id (parser);
11238 if (type1 == error_mark_node)
11239 return error_mark_node;
11241 if (kind == CPTK_TYPE_PACK_ELEMENT)
11243 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11244 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11245 for (tree elt : tree_vec_range (trailing))
11247 if (!TYPE_P (elt))
11249 error_at (cp_expr_loc_or_input_loc (elt),
11250 "trailing argument to %<__type_pack_element%> "
11251 "is not a type");
11252 return error_mark_node;
11255 type2 = trailing;
11257 else if (binary)
11259 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11262 type_id_in_expr_sentinel s (parser);
11263 type2 = cp_parser_type_id (parser);
11266 if (type2 == error_mark_node)
11267 return error_mark_node;
11269 else if (variadic)
11271 auto_vec<tree, 4> trailing;
11272 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11274 cp_lexer_consume_token (parser->lexer);
11275 tree elt = cp_parser_type_id (parser);
11276 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11278 cp_lexer_consume_token (parser->lexer);
11279 elt = make_pack_expansion (elt);
11281 if (elt == error_mark_node)
11282 return error_mark_node;
11283 trailing.safe_push (elt);
11285 type2 = make_tree_vec (trailing.length ());
11286 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11287 TREE_VEC_ELT (type2, i) = trailing[i];
11290 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11291 if (kind == CPTK_TYPE_PACK_ELEMENT)
11292 /* cp_parser_enclosed_template_argument_list above already took care
11293 of parsing the closing '>'. */;
11294 else
11295 parens.require_close (parser);
11297 /* Construct a location of the form:
11298 __is_trivially_copyable(_Tp)
11299 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11300 with start == caret, finishing at the close-paren. */
11301 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11303 /* Complete the trait expression, which may mean either processing
11304 the trait expr now or saving it for template instantiation. */
11305 switch (kind)
11307 case CPTK_BASES:
11308 return cp_expr (finish_bases (type1, false), trait_loc);
11309 case CPTK_DIRECT_BASES:
11310 return cp_expr (finish_bases (type1, true), trait_loc);
11311 default:
11312 if (type)
11313 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11314 else
11315 return finish_trait_expr (trait_loc, kind, type1, type2);
11319 /* Parse a lambda expression.
11321 lambda-expression:
11322 lambda-introducer lambda-declarator [opt] compound-statement
11323 lambda-introducer < template-parameter-list > requires-clause [opt]
11324 lambda-declarator [opt] compound-statement
11326 Returns a representation of the expression. */
11328 static cp_expr
11329 cp_parser_lambda_expression (cp_parser* parser)
11331 tree lambda_expr = build_lambda_expr ();
11332 tree type;
11333 bool ok = true;
11334 cp_token *token = cp_lexer_peek_token (parser->lexer);
11335 cp_token_position start = 0;
11337 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11339 if (cxx_dialect >= cxx20)
11341 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11342 non-type parameter is nonsensical.
11344 Distinguish a lambda in the parameter type from a lambda in the
11345 default argument by looking at local_variables_forbidden_p, which is
11346 only set in default arguments. */
11347 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11349 error_at (token->location,
11350 "lambda-expression in template parameter type");
11351 token->error_reported = true;
11352 ok = false;
11355 else if (cp_unevaluated_operand)
11357 if (!token->error_reported)
11359 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11360 "lambda-expression in unevaluated context"
11361 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11362 token->error_reported = true;
11364 ok = false;
11366 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11368 if (!token->error_reported)
11370 error_at (token->location, "lambda-expression in template-argument"
11371 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11372 token->error_reported = true;
11374 ok = false;
11377 /* We may be in the middle of deferred access check. Disable
11378 it now. */
11379 push_deferring_access_checks (dk_no_deferred);
11381 cp_parser_lambda_introducer (parser, lambda_expr);
11382 if (cp_parser_error_occurred (parser))
11383 return error_mark_node;
11385 type = begin_lambda_type (lambda_expr);
11386 if (type == error_mark_node)
11387 return error_mark_node;
11389 record_lambda_scope (lambda_expr);
11390 record_lambda_scope_discriminator (lambda_expr);
11392 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11393 determine_visibility (TYPE_NAME (type));
11395 /* Now that we've started the type, add the capture fields for any
11396 explicit captures. */
11397 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11400 /* Inside the class, surrounding template-parameter-lists do not apply. */
11401 unsigned int saved_num_template_parameter_lists
11402 = parser->num_template_parameter_lists;
11403 unsigned char in_statement = parser->in_statement;
11404 bool in_switch_statement_p = parser->in_switch_statement_p;
11405 bool fully_implicit_function_template_p
11406 = parser->fully_implicit_function_template_p;
11407 tree implicit_template_parms = parser->implicit_template_parms;
11408 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11409 bool auto_is_implicit_function_template_parm_p
11410 = parser->auto_is_implicit_function_template_parm_p;
11411 bool saved_omp_array_section_p = parser->omp_array_section_p;
11413 parser->num_template_parameter_lists = 0;
11414 parser->in_statement = 0;
11415 parser->in_switch_statement_p = false;
11416 parser->fully_implicit_function_template_p = false;
11417 parser->implicit_template_parms = 0;
11418 parser->implicit_template_scope = 0;
11419 parser->auto_is_implicit_function_template_parm_p = false;
11420 parser->omp_array_section_p = false;
11422 /* The body of a lambda in a discarded statement is not discarded. */
11423 bool discarded = in_discarded_stmt;
11424 in_discarded_stmt = 0;
11426 /* Similarly the body of a lambda in immediate function context is not
11427 in immediate function context. */
11428 bool save_in_consteval_if_p = in_consteval_if_p;
11429 in_consteval_if_p = false;
11431 /* By virtue of defining a local class, a lambda expression has access to
11432 the private variables of enclosing classes. */
11434 if (cp_parser_start_tentative_firewall (parser))
11435 start = token;
11437 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11439 if (ok && cp_parser_error_occurred (parser))
11440 ok = false;
11442 if (ok)
11443 cp_parser_lambda_body (parser, lambda_expr);
11444 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11446 if (cp_parser_skip_to_closing_brace (parser))
11447 cp_lexer_consume_token (parser->lexer);
11450 /* The capture list was built up in reverse order; fix that now. */
11451 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11452 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11454 if (ok)
11455 maybe_add_lambda_conv_op (type);
11457 finish_struct (type, /*attributes=*/NULL_TREE);
11459 in_consteval_if_p = save_in_consteval_if_p;
11460 in_discarded_stmt = discarded;
11462 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11463 parser->in_statement = in_statement;
11464 parser->in_switch_statement_p = in_switch_statement_p;
11465 parser->fully_implicit_function_template_p
11466 = fully_implicit_function_template_p;
11467 parser->implicit_template_parms = implicit_template_parms;
11468 parser->implicit_template_scope = implicit_template_scope;
11469 parser->auto_is_implicit_function_template_parm_p
11470 = auto_is_implicit_function_template_parm_p;
11471 parser->omp_array_section_p = saved_omp_array_section_p;
11474 /* This field is only used during parsing of the lambda. */
11475 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11477 /* This lambda shouldn't have any proxies left at this point. */
11478 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11479 /* And now that we're done, push proxies for an enclosing lambda. */
11480 insert_pending_capture_proxies ();
11482 /* Update the lambda expression to a range. */
11483 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11484 token->location,
11485 parser->lexer);
11487 if (ok)
11488 lambda_expr = build_lambda_object (lambda_expr);
11489 else
11490 lambda_expr = error_mark_node;
11492 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11494 pop_deferring_access_checks ();
11496 return lambda_expr;
11499 /* Parse the beginning of a lambda expression.
11501 lambda-introducer:
11502 [ lambda-capture [opt] ]
11504 LAMBDA_EXPR is the current representation of the lambda expression. */
11506 static void
11507 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11509 /* Need commas after the first capture. */
11510 bool first = true;
11512 /* Eat the leading `['. */
11513 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11515 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11516 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11517 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11518 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11519 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11520 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11521 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11522 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11524 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11526 cp_lexer_consume_token (parser->lexer);
11527 first = false;
11529 if (!(at_function_scope_p () || parsing_nsdmi ()))
11530 error ("non-local lambda expression cannot have a capture-default");
11533 hash_set<tree, true> ids;
11534 tree first_capture_id = NULL_TREE;
11535 unsigned name_independent_cnt = 0;
11536 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11538 cp_token* capture_token;
11539 tree capture_id;
11540 tree capture_init_expr;
11541 cp_id_kind idk = CP_ID_KIND_NONE;
11542 bool explicit_init_p = false;
11544 enum capture_kind_type
11546 BY_COPY,
11547 BY_REFERENCE
11549 enum capture_kind_type capture_kind = BY_COPY;
11551 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11553 error ("expected end of capture-list");
11554 return;
11557 if (first)
11558 first = false;
11559 else
11560 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11562 /* Possibly capture `this'. */
11563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11566 if (cxx_dialect < cxx20 && pedantic
11567 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11568 pedwarn (loc, OPT_Wc__20_extensions,
11569 "explicit by-copy capture of %<this%> "
11570 "with by-copy capture default only available with "
11571 "%<-std=c++20%> or %<-std=gnu++20%>");
11572 cp_lexer_consume_token (parser->lexer);
11573 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11574 pedwarn (input_location, 0,
11575 "already captured %qD in lambda expression",
11576 this_identifier);
11577 else
11578 add_capture (lambda_expr, /*id=*/this_identifier,
11579 /*initializer=*/finish_this_expr (),
11580 /*by_reference_p=*/true, explicit_init_p, NULL);
11581 continue;
11584 /* Possibly capture `*this'. */
11585 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11586 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11588 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11589 if (cxx_dialect < cxx17)
11590 pedwarn (loc, OPT_Wc__17_extensions,
11591 "%<*this%> capture only available with "
11592 "%<-std=c++17%> or %<-std=gnu++17%>");
11593 cp_lexer_consume_token (parser->lexer);
11594 cp_lexer_consume_token (parser->lexer);
11595 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11596 pedwarn (input_location, 0,
11597 "already captured %qD in lambda expression",
11598 this_identifier);
11599 else
11600 add_capture (lambda_expr, /*id=*/this_identifier,
11601 /*initializer=*/finish_this_expr (),
11602 /*by_reference_p=*/false, explicit_init_p, NULL);
11603 continue;
11606 /* But reject `&this'. */
11607 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11608 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11610 error_at (cp_lexer_peek_token (parser->lexer)->location,
11611 "%<this%> cannot be captured by reference");
11612 cp_lexer_consume_token (parser->lexer);
11613 cp_lexer_consume_token (parser->lexer);
11614 continue;
11617 /* Remember whether we want to capture as a reference or not. */
11618 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11620 capture_kind = BY_REFERENCE;
11621 cp_lexer_consume_token (parser->lexer);
11624 bool init_pack_expansion = false;
11625 location_t ellipsis_loc = UNKNOWN_LOCATION;
11626 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11628 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11629 if (cxx_dialect < cxx20)
11630 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11631 "pack init-capture only available with "
11632 "%<-std=c++20%> or %<-std=gnu++20%>");
11633 cp_lexer_consume_token (parser->lexer);
11634 init_pack_expansion = true;
11637 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11638 if (init_pack_expansion && capture_kind != BY_REFERENCE
11639 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11641 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11642 0, "%<&%> should come before %<...%>");
11643 capture_kind = BY_REFERENCE;
11644 cp_lexer_consume_token (parser->lexer);
11647 /* Get the identifier. */
11648 capture_token = cp_lexer_peek_token (parser->lexer);
11649 capture_id = cp_parser_identifier (parser);
11651 if (capture_id == error_mark_node)
11652 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11653 delimiters, but I modified this to stop on unnested ']' as well. It
11654 was already changed to stop on unnested '}', so the
11655 "closing_parenthesis" name is no more misleading with my change. */
11657 cp_parser_skip_to_closing_parenthesis (parser,
11658 /*recovering=*/true,
11659 /*or_comma=*/true,
11660 /*consume_paren=*/true);
11661 break;
11664 /* Find the initializer for this capture. */
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11666 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11667 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11669 /* An explicit initializer exists. */
11670 if (cxx_dialect < cxx14)
11671 pedwarn (input_location, OPT_Wc__14_extensions,
11672 "lambda capture initializers "
11673 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11674 capture_init_expr = cp_parser_initializer (parser,
11675 /*direct_init=*/nullptr,
11676 /*non_constant=*/nullptr,
11677 /*subexpression_p=*/true);
11678 explicit_init_p = true;
11679 if (capture_init_expr == NULL_TREE)
11681 error ("empty initializer for lambda init-capture");
11682 capture_init_expr = error_mark_node;
11684 if (init_pack_expansion)
11685 capture_init_expr = make_pack_expansion (capture_init_expr);
11687 else
11689 const char* error_msg;
11691 /* Turn the identifier into an id-expression. */
11692 capture_init_expr
11693 = cp_parser_lookup_name_simple (parser, capture_id,
11694 capture_token->location);
11696 if (capture_init_expr == error_mark_node)
11698 unqualified_name_lookup_error (capture_id);
11699 continue;
11701 else if (!VAR_P (capture_init_expr)
11702 && TREE_CODE (capture_init_expr) != PARM_DECL)
11704 error_at (capture_token->location,
11705 "capture of non-variable %qE",
11706 capture_init_expr);
11707 if (DECL_P (capture_init_expr))
11708 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11709 "%q#D declared here", capture_init_expr);
11710 continue;
11712 if (VAR_P (capture_init_expr)
11713 && decl_storage_duration (capture_init_expr) != dk_auto)
11715 if (pedwarn (capture_token->location, 0, "capture of variable "
11716 "%qD with non-automatic storage duration",
11717 capture_init_expr))
11718 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11719 "%q#D declared here", capture_init_expr);
11720 continue;
11723 capture_init_expr
11724 = finish_id_expression
11725 (capture_id,
11726 capture_init_expr,
11727 parser->scope,
11728 &idk,
11729 /*integral_constant_expression_p=*/false,
11730 /*allow_non_integral_constant_expression_p=*/false,
11731 /*non_integral_constant_expression_p=*/NULL,
11732 /*template_p=*/false,
11733 /*done=*/true,
11734 /*address_p=*/false,
11735 /*template_arg_p=*/false,
11736 &error_msg,
11737 capture_token->location);
11739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11741 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11742 cp_lexer_consume_token (parser->lexer);
11743 capture_init_expr = make_pack_expansion (capture_init_expr);
11744 if (init_pack_expansion)
11746 /* If what follows is an initializer, the second '...' is
11747 invalid. But for cases like [...xs...], the first one
11748 is invalid. */
11749 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11750 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11751 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11752 ellipsis_loc = loc;
11753 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11754 continue;
11759 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11760 && !explicit_init_p)
11762 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11763 && capture_kind == BY_COPY)
11764 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11765 "of %qD redundant with by-copy capture default",
11766 capture_id);
11767 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11768 && capture_kind == BY_REFERENCE)
11769 pedwarn (capture_token->location, 0, "explicit by-reference "
11770 "capture of %qD redundant with by-reference capture "
11771 "default", capture_id);
11774 /* Check for duplicates.
11775 Optimize for the zero or one explicit captures cases and only create
11776 the hash_set after adding second capture. */
11777 bool found = false;
11778 if (!ids.is_empty ())
11779 found = ids.add (capture_id);
11780 else if (first_capture_id == NULL_TREE)
11781 first_capture_id = capture_id;
11782 else if (capture_id == first_capture_id)
11783 found = true;
11784 else
11786 ids.add (first_capture_id);
11787 ids.add (capture_id);
11789 if (found && explicit_init_p && id_equal (capture_id, "_"))
11790 found = false;
11791 if (found)
11792 pedwarn (input_location, 0,
11793 "already captured %qD in lambda expression", capture_id);
11794 else
11795 add_capture (lambda_expr, capture_id, capture_init_expr,
11796 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11797 explicit_init_p, &name_independent_cnt);
11799 /* If there is any qualification still in effect, clear it
11800 now; we will be starting fresh with the next capture. */
11801 parser->scope = NULL_TREE;
11802 parser->qualifying_scope = NULL_TREE;
11803 parser->object_scope = NULL_TREE;
11806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11809 /* Parse the (optional) middle of a lambda expression.
11811 lambda-declarator:
11812 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11813 lambda-specifiers (C++23)
11815 lambda-specifiers:
11816 decl-specifier-seq [opt] noexcept-specifier [opt]
11817 attribute-specifier-seq [opt] trailing-return-type [opt]
11819 LAMBDA_EXPR is the current representation of the lambda expression. */
11821 static bool
11822 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11824 /* 5.1.1.4 of the standard says:
11825 If a lambda-expression does not include a lambda-declarator, it is as if
11826 the lambda-declarator were ().
11827 This means an empty parameter list, no attributes, and no exception
11828 specification. */
11829 tree param_list = void_list_node;
11830 tree std_attrs = NULL_TREE;
11831 tree gnu_attrs = NULL_TREE;
11832 tree exception_spec = NULL_TREE;
11833 tree template_param_list = NULL_TREE;
11834 tree tx_qual = NULL_TREE;
11835 tree return_type = NULL_TREE;
11836 tree trailing_requires_clause = NULL_TREE;
11837 bool has_param_list = false;
11838 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11839 cp_decl_specifier_seq lambda_specs;
11840 clear_decl_specs (&lambda_specs);
11841 /* A lambda op() is const unless explicitly 'mutable'. */
11842 cp_cv_quals quals = TYPE_QUAL_CONST;
11844 /* The template-parameter-list is optional, but must begin with
11845 an opening angle if present. */
11846 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11848 if (cxx_dialect < cxx20
11849 && (pedantic || cxx_dialect < cxx14))
11850 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11851 "lambda templates are only available with "
11852 "%<-std=c++20%> or %<-std=gnu++20%>");
11854 cp_lexer_consume_token (parser->lexer);
11856 template_param_list = cp_parser_template_parameter_list (parser);
11857 cp_parser_require_end_of_template_parameter_list (parser);
11859 /* We may have a constrained generic lambda; parse the requires-clause
11860 immediately after the template-parameter-list and combine with any
11861 shorthand constraints present. */
11862 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11863 if (flag_concepts)
11865 tree reqs = get_shorthand_constraints (current_template_parms);
11866 if (dreqs)
11867 reqs = combine_constraint_expressions (reqs, dreqs);
11868 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11871 /* We just processed one more parameter list. */
11872 ++parser->num_template_parameter_lists;
11875 /* Committee discussion supports allowing attributes here. */
11876 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11878 /* The parameter-declaration-clause is optional (unless
11879 template-parameter-list was given), but must begin with an
11880 opening parenthesis if present. */
11881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11883 matching_parens parens;
11884 parens.consume_open (parser);
11886 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11888 /* Parse parameters. */
11889 param_list
11890 = cp_parser_parameter_declaration_clause
11891 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11893 /* Default arguments shall not be specified in the
11894 parameter-declaration-clause of a lambda-declarator. */
11895 if (pedantic && cxx_dialect < cxx14)
11896 for (tree t = param_list; t; t = TREE_CHAIN (t))
11897 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11898 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11899 OPT_Wc__14_extensions,
11900 "default argument specified for lambda parameter");
11902 parens.require_close (parser);
11903 has_param_list = true;
11905 else if (cxx_dialect < cxx23)
11906 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11908 /* [expr.prim.lambda.general]
11909 lambda-specifier:
11910 consteval, constexpr, mutable, static
11911 [4] A lambda-specifier-seq shall contain at most one of each
11912 lambda-specifier and shall not contain both constexpr and consteval.
11913 The lambda-specifier-seq shall not contain both mutable and static. */
11914 int declares_class_or_enum;
11915 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11916 cp_parser_decl_specifier_seq (parser,
11917 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11918 &lambda_specs, &declares_class_or_enum);
11920 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11922 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11923 "parameter declaration before lambda declaration "
11924 "specifiers only optional with %<-std=c++2b%> or "
11925 "%<-std=gnu++2b%>");
11926 omitted_parms_loc = UNKNOWN_LOCATION;
11928 /* Peek at the params, see if we have an xobj parameter. */
11929 if (param_list && TREE_PURPOSE (param_list) == this_identifier)
11931 quals = TYPE_UNQUALIFIED;
11932 /* We still need grokdeclarator to see that this is an xobj function
11933 and finish the rest of the work, don't mutate it. */
11934 tree const xobj_param = TREE_VALUE (param_list);
11935 tree const param_type = TREE_TYPE (xobj_param);
11936 /* [expr.prim.lambda.closure-5]
11937 Given a lambda with a lambda-capture, the type of the explicit object
11938 parameter, if any, of the lambda's function call operator (possibly
11939 instantiated from a function call operator template) shall be either:
11940 -- the closure type,
11941 -- a class type derived from the closure type, or
11942 -- a reference to a possibly cv-qualified such type. */
11943 bool const unrelated_with_captures
11944 = (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11945 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11946 /* Since a lambda's type is anonymous, we can assume an xobj
11947 parameter is unrelated to the closure if it is non-dependent.
11948 If it is dependent we handle it at instantiation time. */
11949 && !WILDCARD_TYPE_P (non_reference (param_type));
11950 if (unrelated_with_captures)
11952 error_at (DECL_SOURCE_LOCATION (xobj_param),
11953 "a lambda with captures may not have an explicit object "
11954 "parameter of an unrelated type");
11955 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = NULL_TREE;
11958 /* [expr.prim.lambda.general-4]
11959 If the lambda-declarator contains an explicit object parameter
11960 ([dcl.fct]), then no lambda-specifier in the lambda-specifier-seq
11961 shall be mutable or static. */
11962 if (lambda_specs.storage_class == sc_mutable)
11964 auto_diagnostic_group d;
11965 error_at (lambda_specs.locations[ds_storage_class],
11966 "%<mutable%> lambda specifier "
11967 "with explicit object parameter");
11968 /* Tell the user how to do what they probably meant, maybe fixits
11969 would be appropriate later? */
11970 if (unrelated_with_captures)
11971 /* The following hints don't make sense when we already have an
11972 unrelated type with captures, don't emit them. */;
11973 else if (!TYPE_REF_P (param_type))
11974 inform (DECL_SOURCE_LOCATION (xobj_param),
11975 "the passed in closure object will not be mutated because "
11976 "it is taken by value");
11977 else if (TYPE_READONLY (TREE_TYPE (param_type)))
11978 inform (DECL_SOURCE_LOCATION (xobj_param),
11979 "declare the explicit object parameter as non-const "
11980 "reference instead");
11981 else
11982 inform (DECL_SOURCE_LOCATION (xobj_param),
11983 "explicit object parameter is already a mutable "
11984 "reference");
11986 else if (lambda_specs.storage_class == sc_static)
11988 auto_diagnostic_group d;
11989 error_at (lambda_specs.locations[ds_storage_class],
11990 "%<static%> lambda specifier "
11991 "with explicit object parameter");
11992 inform (DECL_SOURCE_LOCATION (xobj_param),
11993 "explicit object parameter declared here");
11996 else if (lambda_specs.storage_class == sc_mutable)
11998 quals = TYPE_UNQUALIFIED;
12000 else if (lambda_specs.storage_class == sc_static)
12002 /* [expr.prim.lambda.general-4]
12003 If the lambda-specifier-seq contains static, there shall be no
12004 lambda-capture. */
12005 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
12006 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
12007 error_at (lambda_specs.locations[ds_storage_class],
12008 "%<static%> lambda specifier with lambda capture");
12009 else
12011 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
12012 quals = TYPE_UNQUALIFIED;
12016 tx_qual = cp_parser_tx_qualifier_opt (parser);
12017 if (omitted_parms_loc && tx_qual)
12019 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12020 "parameter declaration before lambda transaction "
12021 "qualifier only optional with %<-std=c++2b%> or "
12022 "%<-std=gnu++2b%>");
12023 omitted_parms_loc = UNKNOWN_LOCATION;
12026 /* Parse optional exception specification. */
12027 exception_spec
12028 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
12030 if (omitted_parms_loc && exception_spec)
12032 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12033 "parameter declaration before lambda exception "
12034 "specification only optional with %<-std=c++2b%> or "
12035 "%<-std=gnu++2b%>");
12036 omitted_parms_loc = UNKNOWN_LOCATION;
12039 /* GCC 8 accepted attributes here, and this is the place for standard C++11
12040 attributes that appertain to the function type. */
12041 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12042 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
12043 else
12044 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12046 /* Parse optional trailing return type. */
12047 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
12049 if (omitted_parms_loc)
12050 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12051 "parameter declaration before lambda trailing "
12052 "return type only optional with %<-std=c++2b%> or "
12053 "%<-std=gnu++2b%>");
12054 cp_lexer_consume_token (parser->lexer);
12055 return_type = cp_parser_trailing_type_id (parser);
12058 /* Also allow GNU attributes at the very end of the declaration, the usual
12059 place for GNU attributes. */
12060 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12061 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
12063 if (has_param_list)
12065 /* Parse optional trailing requires clause. */
12066 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
12068 /* The function parameters must be in scope all the way until after the
12069 trailing-return-type in case of decltype. */
12070 pop_bindings_and_leave_scope ();
12073 /* Create the function call operator.
12075 Messing with declarators like this is no uglier than building up the
12076 FUNCTION_DECL by hand, and this is less likely to get out of sync with
12077 other code. */
12079 cp_decl_specifier_seq return_type_specs;
12080 cp_declarator* declarator;
12081 tree fco;
12082 void *p;
12084 clear_decl_specs (&return_type_specs);
12085 return_type_specs.type = make_auto ();
12087 if (lambda_specs.locations[ds_constexpr])
12089 if (cxx_dialect >= cxx17)
12090 return_type_specs.locations[ds_constexpr]
12091 = lambda_specs.locations[ds_constexpr];
12092 else
12093 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
12094 "lambda only available with %<-std=c++17%> or "
12095 "%<-std=gnu++17%>");
12097 if (lambda_specs.locations[ds_consteval])
12098 return_type_specs.locations[ds_consteval]
12099 = lambda_specs.locations[ds_consteval];
12100 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
12102 return_type_specs.storage_class = sc_static;
12103 return_type_specs.locations[ds_storage_class]
12104 = lambda_specs.locations[ds_storage_class];
12107 p = obstack_alloc (&declarator_obstack, 0);
12109 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
12110 LAMBDA_EXPR_LOCATION (lambda_expr));
12112 declarator = make_call_declarator (declarator, param_list, quals,
12113 VIRT_SPEC_UNSPECIFIED,
12114 REF_QUAL_NONE,
12115 tx_qual,
12116 exception_spec,
12117 return_type,
12118 trailing_requires_clause,
12119 std_attrs,
12120 UNKNOWN_LOCATION);
12122 fco = grokmethod (&return_type_specs,
12123 declarator,
12124 chainon (gnu_attrs, lambda_specs.attributes));
12125 if (fco != error_mark_node)
12127 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
12128 DECL_ARTIFICIAL (fco) = 1;
12129 if (DECL_IOBJ_MEMBER_FUNCTION_P (fco))
12130 /* Give the object parameter a different name. */
12131 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
12132 DECL_SET_LAMBDA_FUNCTION (fco, true);
12134 if (template_param_list)
12136 fco = finish_member_template_decl (fco);
12137 finish_template_decl (template_param_list);
12138 --parser->num_template_parameter_lists;
12140 else if (parser->fully_implicit_function_template_p)
12141 fco = finish_fully_implicit_template (parser, fco);
12143 finish_member_declaration (fco);
12144 record_lambda_scope_sig_discriminator (lambda_expr, fco);
12146 obstack_free (&declarator_obstack, p);
12148 return (fco != error_mark_node);
12152 /* Parse the body of a lambda expression, which is simply
12154 compound-statement
12156 but which requires special handling.
12157 LAMBDA_EXPR is the current representation of the lambda expression. */
12159 static void
12160 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
12162 bool nested = (current_function_decl != NULL_TREE);
12163 unsigned char local_variables_forbidden_p
12164 = parser->local_variables_forbidden_p;
12165 bool in_function_body = parser->in_function_body;
12167 /* The body of a lambda-expression is not a subexpression of the enclosing
12168 expression. */
12169 cp_evaluated ev;
12171 if (nested)
12172 push_function_context ();
12173 else
12174 /* Still increment function_depth so that we don't GC in the
12175 middle of an expression. */
12176 ++function_depth;
12178 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
12179 auto ord = make_temp_override (parser->oacc_routine, NULL);
12180 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
12181 vec<tree> omp_privatization_save;
12182 save_omp_privatization_clauses (omp_privatization_save);
12183 /* Clear this in case we're in the middle of a default argument. */
12184 parser->local_variables_forbidden_p = 0;
12185 parser->in_function_body = true;
12188 local_specialization_stack s (lss_copy);
12189 tree fco = lambda_function (lambda_expr);
12190 tree body = start_lambda_function (fco, lambda_expr);
12192 /* Originally C++11 required us to peek for 'return expr'; and
12193 process it specially here to deduce the return type. N3638
12194 removed the need for that. */
12195 cp_parser_function_body (parser, false);
12197 finish_lambda_function (body);
12200 restore_omp_privatization_clauses (omp_privatization_save);
12201 parser->local_variables_forbidden_p = local_variables_forbidden_p;
12202 parser->in_function_body = in_function_body;
12203 if (nested)
12204 pop_function_context();
12205 else
12206 --function_depth;
12209 /* Statements [gram.stmt.stmt] */
12211 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
12213 static void
12214 add_debug_begin_stmt (location_t loc)
12216 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
12217 return;
12218 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
12219 /* A concept is never expanded normally. */
12220 return;
12222 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12223 SET_EXPR_LOCATION (stmt, loc);
12224 add_stmt (stmt);
12227 struct cp_omp_attribute_data
12229 cp_token_cache *tokens;
12230 const c_omp_directive *dir;
12231 c_omp_directive_kind kind;
12234 /* Handle omp::directive and omp::sequence attributes in ATTRS
12235 (if any) at the start of a statement or in attribute-declaration. */
12237 static tree
12238 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12240 if (!flag_openmp && !flag_openmp_simd)
12241 return attrs;
12243 auto_vec<cp_omp_attribute_data, 16> vec;
12244 int cnt = 0;
12245 int tokens = 0;
12246 bool bad = false;
12247 for (tree *pa = &attrs; *pa; )
12248 if (get_attribute_namespace (*pa) == omp_identifier
12249 && is_attribute_p ("directive", get_attribute_name (*pa)))
12251 cnt++;
12252 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12254 tree d = TREE_VALUE (a);
12255 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12256 cp_token *first = DEFPARSE_TOKENS (d)->first;
12257 cp_token *last = DEFPARSE_TOKENS (d)->last;
12258 if (parser->omp_attrs_forbidden_p)
12260 error_at (first->location,
12261 "mixing OpenMP directives with attribute and pragma "
12262 "syntax on the same statement");
12263 parser->omp_attrs_forbidden_p = false;
12264 bad = true;
12266 else if (TREE_PUBLIC (d))
12268 error_at (first->location,
12269 "OpenMP %<omp::decl%> attribute on a statement");
12270 bad = true;
12272 const char *directive[3] = {};
12273 for (int i = 0; i < 3; i++)
12275 tree id = NULL_TREE;
12276 if (first + i == last)
12277 break;
12278 if (first[i].type == CPP_NAME)
12279 id = first[i].u.value;
12280 else if (first[i].type == CPP_KEYWORD)
12281 id = ridpointers[(int) first[i].keyword];
12282 else
12283 break;
12284 directive[i] = IDENTIFIER_POINTER (id);
12286 const c_omp_directive *dir = NULL;
12287 if (directive[0])
12288 dir = c_omp_categorize_directive (directive[0], directive[1],
12289 directive[2]);
12290 if (dir == NULL)
12292 error_at (first->location,
12293 "unknown OpenMP directive name in %qs attribute "
12294 "argument",
12295 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12296 continue;
12298 c_omp_directive_kind kind = dir->kind;
12299 if (dir->id == PRAGMA_OMP_ORDERED)
12301 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12302 depend/doacross clause. */
12303 if (directive[1]
12304 && (strcmp (directive[1], "depend") == 0
12305 || strcmp (directive[1], "doacross") == 0))
12306 kind = C_OMP_DIR_STANDALONE;
12307 else if (first + 2 < last
12308 && first[1].type == CPP_COMMA
12309 && first[2].type == CPP_NAME
12310 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12311 "depend") == 0
12312 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12313 "doacross") == 0))
12314 kind = C_OMP_DIR_STANDALONE;
12316 else if (dir->id == PRAGMA_OMP_ERROR)
12318 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12319 int paren_depth = 0;
12320 for (int i = 1; first + i < last; i++)
12321 if (first[i].type == CPP_OPEN_PAREN)
12322 paren_depth++;
12323 else if (first[i].type == CPP_CLOSE_PAREN)
12324 paren_depth--;
12325 else if (paren_depth == 0
12326 && first + i + 2 < last
12327 && first[i].type == CPP_NAME
12328 && first[i + 1].type == CPP_OPEN_PAREN
12329 && first[i + 2].type == CPP_NAME
12330 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12331 "at")
12332 && !strcmp (IDENTIFIER_POINTER (first[i
12333 + 2].u.value),
12334 "execution"))
12336 kind = C_OMP_DIR_STANDALONE;
12337 break;
12340 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12341 vec.safe_push (v);
12342 if (flag_openmp || dir->simd)
12343 tokens += (last - first) + 1;
12345 cp_omp_attribute_data v = {};
12346 vec.safe_push (v);
12347 *pa = TREE_CHAIN (*pa);
12349 else
12350 pa = &TREE_CHAIN (*pa);
12352 if (bad)
12353 return attrs;
12355 unsigned int i;
12356 cp_omp_attribute_data *v;
12357 cp_omp_attribute_data *construct_seen = nullptr;
12358 cp_omp_attribute_data *standalone_seen = nullptr;
12359 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12360 FOR_EACH_VEC_ELT (vec, i, v)
12361 if (v->tokens)
12363 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12364 construct_seen = v;
12365 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12366 standalone_seen = v;
12368 else
12370 if (standalone_seen && !prev_standalone_seen)
12372 prev_standalone_seen = standalone_seen;
12373 standalone_seen = nullptr;
12377 if (cnt > 1 && construct_seen)
12379 error_at (construct_seen->tokens->first->location,
12380 "OpenMP construct among %<omp::directive%> attributes"
12381 " requires all %<omp::directive%> attributes on the"
12382 " same statement to be in the same %<omp::sequence%>");
12383 return attrs;
12385 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12387 error_at (standalone_seen->tokens->first->location,
12388 "multiple OpenMP standalone directives among"
12389 " %<omp::directive%> attributes must be all within the"
12390 " same %<omp::sequence%>");
12391 return attrs;
12394 if (prev_standalone_seen)
12395 standalone_seen = prev_standalone_seen;
12396 if (standalone_seen
12397 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12399 error_at (standalone_seen->tokens->first->location,
12400 "standalone OpenMP directives in %<omp::directive%> attribute"
12401 " can only appear on an empty statement");
12402 return attrs;
12404 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12406 cp_token *token = cp_lexer_peek_token (parser->lexer);
12407 enum pragma_kind kind = cp_parser_pragma_kind (token);
12408 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12410 error_at (token->location,
12411 "mixing OpenMP directives with attribute and pragma "
12412 "syntax on the same statement");
12413 return attrs;
12417 if (!tokens)
12418 return attrs;
12419 tokens++;
12420 cp_lexer *lexer = cp_lexer_alloc ();
12421 lexer->debugging_p = parser->lexer->debugging_p;
12422 vec_safe_reserve (lexer->buffer, tokens, true);
12423 FOR_EACH_VEC_ELT (vec, i, v)
12425 if (!v->tokens)
12426 continue;
12427 if (!flag_openmp && !v->dir->simd)
12428 continue;
12429 cp_token *first = v->tokens->first;
12430 cp_token *last = v->tokens->last;
12431 cp_token tok = {};
12432 tok.type = CPP_PRAGMA;
12433 tok.keyword = RID_MAX;
12434 tok.u.value = build_int_cst (NULL, v->dir->id);
12435 tok.location = first->location;
12436 lexer->buffer->quick_push (tok);
12437 while (++first < last)
12438 lexer->buffer->quick_push (*first);
12439 tok = {};
12440 tok.type = CPP_PRAGMA_EOL;
12441 tok.keyword = RID_MAX;
12442 tok.location = last->location;
12443 lexer->buffer->quick_push (tok);
12445 cp_token tok = {};
12446 tok.type = CPP_EOF;
12447 tok.keyword = RID_MAX;
12448 tok.location = lexer->buffer->last ().location;
12449 lexer->buffer->quick_push (tok);
12450 lexer->next = parser->lexer;
12451 lexer->next_token = lexer->buffer->address ();
12452 lexer->last_token = lexer->next_token
12453 + lexer->buffer->length ()
12454 - 1;
12455 lexer->in_omp_attribute_pragma = true;
12456 parser->lexer = lexer;
12457 /* Move the current source position to that of the first token in the
12458 new lexer. */
12459 cp_lexer_set_source_position_from_token (lexer->next_token);
12460 return attrs;
12463 /* True if and only if the name is one of the contract types. */
12465 static bool
12466 contract_attribute_p (const_tree id)
12468 return is_attribute_p ("assert", id)
12469 || is_attribute_p ("pre", id)
12470 || is_attribute_p ("post", id);
12473 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12474 (if any) at the start or after declaration-id of a declaration. */
12476 static void
12477 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12478 cp_omp_declare_simd_data *data,
12479 bool start)
12481 if (!flag_openmp && !flag_openmp_simd)
12482 return;
12484 int cnt = 0;
12485 bool bad = false;
12486 bool variant_p = false;
12487 location_t loc = UNKNOWN_LOCATION;
12488 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12489 if (get_attribute_namespace (pa) == omp_identifier
12490 && is_attribute_p ("directive", get_attribute_name (pa)))
12492 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12494 tree d = TREE_VALUE (a);
12495 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12496 cp_token *first = DEFPARSE_TOKENS (d)->first;
12497 cp_token *last = DEFPARSE_TOKENS (d)->last;
12498 const char *directive[3] = {};
12499 for (int i = 0; i < 3; i++)
12501 tree id = NULL_TREE;
12502 if (first + i == last)
12503 break;
12504 if (first[i].type == CPP_NAME)
12505 id = first[i].u.value;
12506 else if (first[i].type == CPP_KEYWORD)
12507 id = ridpointers[(int) first[i].keyword];
12508 else
12509 break;
12510 directive[i] = IDENTIFIER_POINTER (id);
12512 const c_omp_directive *dir = NULL;
12513 if (directive[0])
12514 dir = c_omp_categorize_directive (directive[0], directive[1],
12515 directive[2]);
12516 if (dir == NULL)
12517 continue;
12518 if (dir->id == PRAGMA_OMP_DECLARE
12519 && (strcmp (directive[1], "simd") == 0
12520 || strcmp (directive[1], "variant") == 0))
12522 if (cnt++ == 0)
12524 variant_p = strcmp (directive[1], "variant") == 0;
12525 loc = first->location;
12527 if (start && parser->omp_declare_simd && !bad)
12529 error_at (first->location,
12530 "mixing OpenMP directives with attribute and "
12531 "pragma syntax on the same declaration");
12532 bad = true;
12538 if (bad)
12540 for (tree *pa = pattrs; *pa; )
12541 if (get_attribute_namespace (*pa) == omp_identifier
12542 && is_attribute_p ("directive", get_attribute_name (*pa)))
12543 *pa = TREE_CHAIN (*pa);
12544 else
12545 pa = &TREE_CHAIN (*pa);
12546 return;
12548 if (cnt == 0)
12549 return;
12551 if (parser->omp_declare_simd == NULL)
12553 data->error_seen = false;
12554 data->fndecl_seen = false;
12555 data->variant_p = variant_p;
12556 data->loc = loc;
12557 data->tokens = vNULL;
12558 data->attribs[0] = NULL;
12559 data->attribs[1] = NULL;
12560 parser->omp_declare_simd = data;
12562 parser->omp_declare_simd->attribs[!start] = pattrs;
12565 /* Parse a statement.
12567 statement:
12568 labeled-statement
12569 expression-statement
12570 compound-statement
12571 selection-statement
12572 iteration-statement
12573 jump-statement
12574 declaration-statement
12575 try-block
12577 C++11:
12579 statement:
12580 labeled-statement
12581 attribute-specifier-seq (opt) expression-statement
12582 attribute-specifier-seq (opt) compound-statement
12583 attribute-specifier-seq (opt) selection-statement
12584 attribute-specifier-seq (opt) iteration-statement
12585 attribute-specifier-seq (opt) jump-statement
12586 declaration-statement
12587 attribute-specifier-seq (opt) try-block
12589 init-statement:
12590 expression-statement
12591 simple-declaration
12592 alias-declaration
12594 TM Extension:
12596 statement:
12597 atomic-statement
12599 IN_COMPOUND is true when the statement is nested inside a
12600 cp_parser_compound_statement.
12602 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12603 is a (possibly labeled) if statement which is not enclosed in braces
12604 and has an else clause. This is used to implement -Wparentheses.
12606 CHAIN is a vector of if-else-if conditions.
12608 Note that this version of parsing restricts assertions to be attached to
12609 empty statements. */
12611 static void
12612 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12613 const bool in_compound, bool *if_p, vec<tree> *chain,
12614 location_t *loc_after_labels)
12616 tree statement, std_attrs = NULL_TREE;
12617 cp_token *token;
12618 location_t statement_location, attrs_loc;
12619 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12620 bool has_std_attrs;
12621 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12622 This matters for certain pragmas. */
12623 bool in_compound_for_pragma = in_compound;
12625 restart:
12626 if (if_p != NULL)
12627 *if_p = false;
12628 /* There is no statement yet. */
12629 statement = NULL_TREE;
12631 saved_token_sentinel saved_tokens (parser->lexer);
12632 token = cp_lexer_peek_token (parser->lexer);
12633 attrs_loc = token->location;
12634 if (c_dialect_objc ())
12635 /* In obj-c++, seeing '[[' might be the either the beginning of
12636 c++11 attributes, or a nested objc-message-expression. So
12637 let's parse the c++11 attributes tentatively. */
12638 cp_parser_parse_tentatively (parser);
12639 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12640 if (std_attrs)
12641 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12642 if (c_dialect_objc ())
12644 if (!cp_parser_parse_definitely (parser))
12645 std_attrs = NULL_TREE;
12647 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12649 /* Peek at the next token. */
12650 token = cp_lexer_peek_token (parser->lexer);
12652 /* If we have contracts, check that they're valid in this context. */
12653 if (std_attrs != error_mark_node)
12655 if (tree pre = lookup_attribute ("pre", std_attrs))
12656 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12657 "preconditions cannot be statements");
12658 else if (tree post = lookup_attribute ("post", std_attrs))
12659 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12660 "postconditions cannot be statements");
12662 /* Check that assertions are null statements. */
12663 if (cp_contract_assertion_p (std_attrs))
12664 if (token->type != CPP_SEMICOLON)
12665 error_at (token->location, "assertions must be followed by %<;%>");
12668 bool omp_attrs_forbidden_p;
12669 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12671 if (std_attrs && (flag_openmp || flag_openmp_simd))
12673 bool handle_omp_attribs = false;
12674 if (token->type == CPP_KEYWORD)
12675 switch (token->keyword)
12677 case RID_IF:
12678 case RID_SWITCH:
12679 case RID_WHILE:
12680 case RID_DO:
12681 case RID_FOR:
12682 case RID_BREAK:
12683 case RID_CONTINUE:
12684 case RID_RETURN:
12685 case RID_CO_RETURN:
12686 case RID_GOTO:
12687 case RID_AT_TRY:
12688 case RID_AT_CATCH:
12689 case RID_AT_FINALLY:
12690 case RID_AT_SYNCHRONIZED:
12691 case RID_AT_THROW:
12692 case RID_TRY:
12693 case RID_TRANSACTION_ATOMIC:
12694 case RID_TRANSACTION_RELAXED:
12695 case RID_SYNCHRONIZED:
12696 case RID_ATOMIC_NOEXCEPT:
12697 case RID_ATOMIC_CANCEL:
12698 case RID_TRANSACTION_CANCEL:
12699 handle_omp_attribs = true;
12700 break;
12701 default:
12702 break;
12704 else if (token->type == CPP_SEMICOLON
12705 || token->type == CPP_OPEN_BRACE
12706 || token->type == CPP_PRAGMA)
12707 handle_omp_attribs = true;
12708 if (handle_omp_attribs)
12710 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12711 std_attrs);
12712 token = cp_lexer_peek_token (parser->lexer);
12715 parser->omp_attrs_forbidden_p = false;
12717 /* Remember the location of the first token in the statement. */
12718 cp_token *statement_token = token;
12719 statement_location = token->location;
12720 add_debug_begin_stmt (statement_location);
12721 /* If this is a keyword, then that will often determine what kind of
12722 statement we have. */
12723 if (token->type == CPP_KEYWORD)
12725 enum rid keyword = token->keyword;
12727 switch (keyword)
12729 case RID_CASE:
12730 case RID_DEFAULT:
12731 /* Looks like a labeled-statement with a case label.
12732 Parse the label, and then use tail recursion to parse
12733 the statement. */
12734 cp_parser_label_for_labeled_statement (parser, std_attrs);
12735 in_compound_for_pragma = false;
12736 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12737 goto restart;
12739 case RID_IF:
12740 case RID_SWITCH:
12741 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12742 statement = cp_parser_selection_statement (parser, if_p, chain);
12743 break;
12745 case RID_WHILE:
12746 case RID_DO:
12747 case RID_FOR:
12748 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12749 statement = cp_parser_iteration_statement (parser, if_p, false,
12750 NULL_TREE, false);
12751 break;
12753 case RID_BREAK:
12754 case RID_CONTINUE:
12755 case RID_RETURN:
12756 case RID_CO_RETURN:
12757 case RID_GOTO:
12758 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12759 statement = cp_parser_jump_statement (parser);
12760 break;
12762 /* Objective-C++ exception-handling constructs. */
12763 case RID_AT_TRY:
12764 case RID_AT_CATCH:
12765 case RID_AT_FINALLY:
12766 case RID_AT_SYNCHRONIZED:
12767 case RID_AT_THROW:
12768 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12769 statement = cp_parser_objc_statement (parser);
12770 break;
12772 case RID_TRY:
12773 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12774 statement = cp_parser_try_block (parser);
12775 break;
12777 case RID_NAMESPACE:
12778 /* This must be a namespace alias definition. */
12779 if (has_std_attrs)
12781 /* Attributes should be parsed as part of the
12782 declaration, so let's un-parse them. */
12783 saved_tokens.rollback();
12784 std_attrs = NULL_TREE;
12786 cp_parser_declaration_statement (parser);
12787 return;
12789 case RID_TRANSACTION_ATOMIC:
12790 case RID_TRANSACTION_RELAXED:
12791 case RID_SYNCHRONIZED:
12792 case RID_ATOMIC_NOEXCEPT:
12793 case RID_ATOMIC_CANCEL:
12794 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12795 statement = cp_parser_transaction (parser, token);
12796 break;
12797 case RID_TRANSACTION_CANCEL:
12798 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12799 statement = cp_parser_transaction_cancel (parser);
12800 break;
12802 default:
12803 /* It might be a keyword like `int' that can start a
12804 declaration-statement. */
12805 break;
12808 else if (token->type == CPP_NAME)
12810 /* If the next token is a `:', then we are looking at a
12811 labeled-statement. */
12812 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12813 if (token->type == CPP_COLON)
12815 /* Looks like a labeled-statement with an ordinary label.
12816 Parse the label, and then use tail recursion to parse
12817 the statement. */
12819 cp_parser_label_for_labeled_statement (parser, std_attrs);
12821 /* If there's no statement, it's not a labeled-statement, just
12822 a label. That's allowed in C++23, but only if we're at the
12823 end of a compound-statement. */
12824 if (in_compound
12825 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12828 if (cxx_dialect < cxx23)
12829 pedwarn (loc, OPT_Wc__23_extensions,
12830 "label at end of compound statement only available "
12831 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12832 return;
12834 in_compound_for_pragma = false;
12835 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12836 goto restart;
12839 /* Anything that starts with a `{' must be a compound-statement. */
12840 else if (token->type == CPP_OPEN_BRACE)
12842 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12843 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12845 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12846 a statement all its own. */
12847 else if (token->type == CPP_PRAGMA)
12849 do_pragma:;
12850 cp_lexer *lexer = parser->lexer;
12851 bool do_restart = false;
12852 /* Only certain OpenMP pragmas are attached to statements, and thus
12853 are considered statements themselves. All others are not. In
12854 the context of a compound, accept the pragma as a "statement" and
12855 return so that we can check for a close brace. Otherwise we
12856 require a real statement and must go back and read one. */
12857 if (in_compound_for_pragma)
12859 if (cp_parser_pragma (parser, pragma_compound, if_p)
12860 && parser->omp_for_parse_state)
12861 check_omp_intervening_code (parser);
12863 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12864 do_restart = true;
12865 else if (parser->omp_for_parse_state)
12866 check_omp_intervening_code (parser);
12867 if (parser->lexer != lexer
12868 && lexer->in_omp_attribute_pragma
12869 && (!in_omp_attribute_pragma || lexer->orphan_p))
12871 if (saved_tokens.lexer == lexer)
12873 if (saved_tokens.mode == STS_COMMIT)
12874 cp_lexer_commit_tokens (lexer);
12875 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12876 saved_tokens.lexer = parser->lexer;
12877 saved_tokens.mode = STS_DONOTHING;
12878 saved_tokens.len = parser->lexer->saved_tokens.length ();
12880 cp_lexer_destroy (lexer);
12881 lexer = parser->lexer;
12883 if (do_restart)
12884 goto restart;
12885 if (parser->lexer == lexer
12886 && lexer->in_omp_attribute_pragma
12887 && !in_omp_attribute_pragma)
12888 parser->lexer->orphan_p = true;
12889 return;
12891 else if (token->type == CPP_EOF)
12893 cp_parser_error (parser, "expected statement");
12894 return;
12897 /* Everything else must be a declaration-statement or an
12898 expression-statement. Try for the declaration-statement
12899 first, unless we are looking at a `;', in which case we know that
12900 we have an expression-statement. */
12901 if (!statement)
12903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12905 if (has_std_attrs)
12906 /* Attributes should be parsed as part of the declaration,
12907 so let's un-parse them. */
12908 saved_tokens.rollback();
12910 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12911 cp_parser_parse_tentatively (parser);
12912 /* Try to parse the declaration-statement. */
12913 cp_parser_declaration_statement (parser);
12914 parser->omp_attrs_forbidden_p = false;
12915 /* If that worked, we're done. */
12916 if (cp_parser_parse_definitely (parser))
12917 return;
12918 /* It didn't work, restore the post-attribute position. */
12919 if (has_std_attrs)
12921 cp_lexer_set_token_position (parser->lexer, statement_token);
12922 if (flag_openmp || flag_openmp_simd)
12924 size_t i = 1;
12925 bool handle_omp_attribs = true;
12926 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12927 == RID_EXTENSION)
12928 i++;
12929 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12931 case RID_ASM:
12932 case RID_NAMESPACE:
12933 case RID_USING:
12934 case RID_LABEL:
12935 case RID_STATIC_ASSERT:
12936 /* Don't handle OpenMP attribs on keywords that
12937 always start a declaration statement but don't
12938 accept attribute before it and therefore
12939 the tentative cp_parser_declaration_statement
12940 fails to parse because of that. */
12941 handle_omp_attribs = false;
12942 break;
12943 default:
12944 break;
12947 if (handle_omp_attribs)
12949 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12950 std_attrs
12951 = cp_parser_handle_statement_omp_attributes
12952 (parser, std_attrs);
12953 parser->omp_attrs_forbidden_p = false;
12954 token = cp_lexer_peek_token (parser->lexer);
12955 if (token->type == CPP_PRAGMA)
12956 goto do_pragma;
12961 /* All preceding labels have been parsed at this point. */
12962 if (loc_after_labels != NULL)
12963 *loc_after_labels = statement_location;
12965 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12967 /* Look for an expression-statement instead. */
12968 statement = cp_parser_expression_statement (parser, in_statement_expr);
12970 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12971 attrs_loc);
12973 /* Handle [[fallthrough]];. */
12974 if (attribute_fallthrough_p (std_attrs))
12976 /* The next token after the fallthrough attribute is ';'. */
12977 if (statement == NULL_TREE)
12979 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12980 statement = build_call_expr_internal_loc (statement_location,
12981 IFN_FALLTHROUGH,
12982 void_type_node, 0);
12983 finish_expr_stmt (statement);
12985 else
12986 warning_at (statement_location, OPT_Wattributes,
12987 "%<fallthrough%> attribute not followed by %<;%>");
12988 std_attrs = NULL_TREE;
12991 /* Handle [[assert: ...]]; */
12992 if (cp_contract_assertion_p (std_attrs))
12994 /* Add the assertion as a statement in the current block. */
12995 gcc_assert (!statement || statement == error_mark_node);
12996 emit_assertion (std_attrs);
12997 std_attrs = NULL_TREE;
13001 /* Set the line number for the statement. */
13002 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
13003 SET_EXPR_LOCATION (statement, statement_location);
13005 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
13006 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
13007 warning_at (attrs_loc, OPT_Wattributes,
13008 "attributes at the beginning of statement are ignored");
13011 /* Append ATTR to attribute list ATTRS. */
13013 tree
13014 attr_chainon (tree attrs, tree attr)
13016 if (attrs == error_mark_node)
13017 return error_mark_node;
13018 if (attr == error_mark_node)
13019 return error_mark_node;
13020 return chainon (attrs, attr);
13023 /* Parse the label for a labeled-statement, i.e.
13025 label:
13026 attribute-specifier-seq[opt] identifier :
13027 attribute-specifier-seq[opt] case constant-expression :
13028 attribute-specifier-seq[opt] default :
13030 labeled-statement:
13031 label statement
13033 GNU Extension:
13034 case constant-expression ... constant-expression : statement
13036 When a label is parsed without errors, the label is added to the
13037 parse tree by the finish_* functions, so this function doesn't
13038 have to return the label. */
13040 static void
13041 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
13043 cp_token *token;
13044 tree label = NULL_TREE;
13045 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13047 /* The next token should be an identifier. */
13048 token = cp_lexer_peek_token (parser->lexer);
13049 if (token->type != CPP_NAME
13050 && token->type != CPP_KEYWORD)
13052 cp_parser_error (parser, "expected labeled-statement");
13053 return;
13056 /* Remember whether this case or a user-defined label is allowed to fall
13057 through to. */
13058 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
13060 parser->colon_corrects_to_scope_p = false;
13061 switch (token->keyword)
13063 case RID_CASE:
13065 tree expr, expr_hi;
13066 cp_token *ellipsis;
13068 /* Consume the `case' token. */
13069 cp_lexer_consume_token (parser->lexer);
13070 /* Parse the constant-expression. */
13071 expr = cp_parser_constant_expression (parser);
13072 if (check_for_bare_parameter_packs (expr))
13073 expr = error_mark_node;
13075 ellipsis = cp_lexer_peek_token (parser->lexer);
13076 if (ellipsis->type == CPP_ELLIPSIS)
13078 /* Consume the `...' token. */
13079 cp_lexer_consume_token (parser->lexer);
13080 expr_hi = cp_parser_constant_expression (parser);
13081 if (check_for_bare_parameter_packs (expr_hi))
13082 expr_hi = error_mark_node;
13084 /* We don't need to emit warnings here, as the common code
13085 will do this for us. */
13087 else
13088 expr_hi = NULL_TREE;
13090 if (parser->in_switch_statement_p)
13092 tree l = finish_case_label (token->location, expr, expr_hi);
13093 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13095 label = CASE_LABEL (l);
13096 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13099 else
13100 error_at (token->location,
13101 "case label %qE not within a switch statement",
13102 expr);
13104 break;
13106 case RID_DEFAULT:
13107 /* Consume the `default' token. */
13108 cp_lexer_consume_token (parser->lexer);
13110 if (parser->in_switch_statement_p)
13112 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
13113 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13115 label = CASE_LABEL (l);
13116 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13119 else
13120 error_at (token->location, "case label not within a switch statement");
13121 break;
13123 default:
13124 /* Anything else must be an ordinary label. */
13125 label = finish_label_stmt (cp_parser_identifier (parser));
13126 if (label && TREE_CODE (label) == LABEL_DECL)
13128 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13129 if (!warning_enabled_at (input_location, OPT_Wunused_label))
13130 suppress_warning (label, OPT_Wunused_label);
13132 break;
13135 /* Require the `:' token. */
13136 cp_parser_require (parser, CPP_COLON, RT_COLON);
13138 /* An ordinary label may optionally be followed by attributes.
13139 However, this is only permitted if the attributes are then
13140 followed by a semicolon. This is because, for backward
13141 compatibility, when parsing
13142 lab: __attribute__ ((unused)) int i;
13143 we want the attribute to attach to "i", not "lab". */
13144 if (label != NULL_TREE
13145 && cp_next_tokens_can_be_gnu_attribute_p (parser))
13147 tree attrs;
13148 cp_parser_parse_tentatively (parser);
13149 attrs = cp_parser_gnu_attributes_opt (parser);
13150 if (attrs == NULL_TREE
13151 /* And fallthrough always binds to the expression-statement. */
13152 || attribute_fallthrough_p (attrs)
13153 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13154 cp_parser_abort_tentative_parse (parser);
13155 else if (!cp_parser_parse_definitely (parser))
13157 else
13158 attributes = attr_chainon (attributes, attrs);
13161 if (attributes != NULL_TREE)
13162 cplus_decl_attributes (&label, attributes, 0);
13164 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13167 /* Parse an expression-statement.
13169 expression-statement:
13170 expression [opt] ;
13172 Returns the new EXPR_STMT -- or NULL_TREE if the expression
13173 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
13174 indicates whether this expression-statement is part of an
13175 expression statement. */
13177 static tree
13178 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
13180 tree statement = NULL_TREE;
13181 cp_token *token = cp_lexer_peek_token (parser->lexer);
13182 location_t loc = token->location;
13184 /* There might be attribute fallthrough. */
13185 tree attr = cp_parser_gnu_attributes_opt (parser);
13187 /* If the next token is a ';', then there is no expression
13188 statement. */
13189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13191 statement = cp_parser_expression (parser);
13192 if (statement == error_mark_node
13193 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13195 /* If we ran into a problem, make sure we complained. */
13196 gcc_assert (seen_error ());
13198 cp_parser_skip_to_end_of_block_or_statement (parser);
13199 return error_mark_node;
13203 attr = process_stmt_assume_attribute (attr, statement, loc);
13205 /* Handle [[fallthrough]];. */
13206 if (attribute_fallthrough_p (attr))
13208 /* The next token after the fallthrough attribute is ';'. */
13209 if (statement == NULL_TREE)
13210 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
13211 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
13212 void_type_node, 0);
13213 else
13214 warning_at (loc, OPT_Wattributes,
13215 "%<fallthrough%> attribute not followed by %<;%>");
13216 attr = NULL_TREE;
13219 /* Allow "[[fallthrough]];", but warn otherwise. */
13220 if (attr != NULL_TREE && any_nonignored_attribute_p (attr))
13221 warning_at (loc, OPT_Wattributes,
13222 "attributes at the beginning of statement are ignored");
13224 /* Give a helpful message for "A<T>::type t;" and the like. */
13225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13226 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13228 if (TREE_CODE (statement) == SCOPE_REF)
13229 error_at (token->location, "need %<typename%> before %qE because "
13230 "%qT is a dependent scope",
13231 statement, TREE_OPERAND (statement, 0));
13232 else if (is_overloaded_fn (statement)
13233 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13235 /* A::A a; */
13236 tree fn = get_first_fn (statement);
13237 error_at (token->location,
13238 "%<%T::%D%> names the constructor, not the type",
13239 DECL_CONTEXT (fn), DECL_NAME (fn));
13243 /* Consume the final `;'. */
13244 cp_parser_consume_semicolon_at_end_of_statement (parser);
13246 if (in_statement_expr
13247 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13248 /* This is the final expression statement of a statement
13249 expression. */
13250 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13251 else if (statement)
13252 statement = finish_expr_stmt (statement);
13254 return statement;
13257 /* Parse a compound-statement.
13259 compound-statement:
13260 { statement-seq [opt] label-seq [opt] }
13262 label-seq:
13263 label
13264 label-seq label
13266 GNU extension:
13268 compound-statement:
13269 { label-declaration-seq [opt] statement-seq [opt] }
13271 label-declaration-seq:
13272 label-declaration
13273 label-declaration-seq label-declaration
13275 Returns a tree representing the statement. */
13277 static tree
13278 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13279 int bcs_flags, bool function_body)
13281 tree compound_stmt;
13282 matching_braces braces;
13284 /* Consume the `{'. */
13285 if (!braces.require_open (parser))
13286 return error_mark_node;
13287 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13288 && !function_body && cxx_dialect < cxx14)
13289 pedwarn (input_location, OPT_Wpedantic,
13290 "compound-statement in %<constexpr%> function");
13291 /* Begin the compound-statement. */
13292 compound_stmt = begin_compound_stmt (bcs_flags);
13293 /* If the next keyword is `__label__' we have a label declaration. */
13294 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13295 cp_parser_label_declaration (parser);
13296 /* Parse an (optional) statement-seq. */
13297 cp_parser_statement_seq_opt (parser, in_statement_expr);
13299 /* Consume the `}'. */
13300 braces.require_close (parser);
13302 /* Finish the compound-statement. */
13303 finish_compound_stmt (compound_stmt);
13305 return compound_stmt;
13308 /* Diagnose errors related to imperfectly nested loops in an OMP
13309 loop construct. This function is called when such code is seen.
13310 Only issue one such diagnostic no matter how much invalid
13311 intervening code there is in the loop.
13312 FIXME: maybe the location associated with the diagnostic should
13313 be the current parser token instead of the location of the outer loop
13314 nest. */
13316 static void
13317 check_omp_intervening_code (cp_parser *parser)
13319 struct omp_for_parse_data *omp_for_parse_state
13320 = parser->omp_for_parse_state;
13321 gcc_assert (omp_for_parse_state);
13323 if (!omp_for_parse_state->in_intervening_code)
13324 return;
13325 omp_for_parse_state->saw_intervening_code = true;
13327 /* Only diagnose errors related to perfect nesting once. */
13328 if (!omp_for_parse_state->perfect_nesting_fail)
13330 if (omp_for_parse_state->code == OACC_LOOP)
13332 error_at (omp_for_parse_state->for_loc,
13333 "inner loops must be perfectly nested in "
13334 "%<#pragma acc loop%>");
13335 omp_for_parse_state->perfect_nesting_fail = true;
13337 else if (omp_for_parse_state->ordered)
13339 error_at (omp_for_parse_state->for_loc,
13340 "inner loops must be perfectly nested with "
13341 "%<ordered%> clause");
13342 omp_for_parse_state->perfect_nesting_fail = true;
13344 else if (omp_for_parse_state->inscan)
13346 error_at (omp_for_parse_state->for_loc,
13347 "inner loops must be perfectly nested with "
13348 "%<reduction%> %<inscan%> clause");
13349 omp_for_parse_state->perfect_nesting_fail = true;
13351 else if (omp_for_parse_state->code == OMP_TILE)
13353 error_at (omp_for_parse_state->for_loc,
13354 "inner loops must be perfectly nested "
13355 "with %<tile%> directive");
13356 omp_for_parse_state->perfect_nesting_fail = true;
13358 if (omp_for_parse_state->perfect_nesting_fail)
13359 omp_for_parse_state->fail = true;
13363 /* Parse an (optional) statement-seq.
13365 statement-seq:
13366 statement
13367 statement-seq [opt] statement */
13369 static void
13370 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13372 struct omp_for_parse_data *omp_for_parse_state
13373 = parser->omp_for_parse_state;
13374 bool in_omp_loop_block
13375 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13377 /* Scan statements until there aren't any more. */
13378 while (true)
13380 cp_token *token = cp_lexer_peek_token (parser->lexer);
13382 /* If we are looking at a `}', then we have run out of
13383 statements; the same is true if we have reached the end
13384 of file, or have stumbled upon a stray '@end'. */
13385 if (token->type == CPP_CLOSE_BRACE
13386 || token->type == CPP_EOF
13387 || token->type == CPP_PRAGMA_EOL
13388 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13389 break;
13391 /* If we are in a compound statement and find 'else' then
13392 something went wrong. */
13393 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13395 if (parser->in_statement & IN_IF_STMT)
13396 break;
13397 else
13399 token = cp_lexer_consume_token (parser->lexer);
13400 error_at (token->location, "%<else%> without a previous %<if%>");
13404 /* Handle special cases for OMP FOR canonical loop syntax. */
13405 else if (in_omp_loop_block)
13407 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13408 tree_code code = omp_for_parse_state->code;
13409 if (want_nested_loop
13410 && cp_parser_next_tokens_can_be_canon_loop (parser, code, false))
13412 /* Found the nested loop. */
13413 omp_for_parse_state->depth++;
13414 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13415 omp_for_parse_state->depth--;
13417 else if (token->type == CPP_SEMICOLON)
13419 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13420 loop form, GCC used to accept an empty statements as not
13421 being intervening code. Continue to do that, as an
13422 extension. */
13423 /* FIXME: Maybe issue a warning or something here? */
13424 cp_lexer_consume_token (parser->lexer);
13426 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13427 /* The nested compound statement may contain the next loop, or
13428 it might just be intervening code. */
13430 cp_parser_statement (parser, in_statement_expr, true, NULL);
13431 if (omp_for_parse_state->want_nested_loop)
13432 check_omp_intervening_code (parser);
13434 else
13436 /* This must be intervening code. */
13437 omp_for_parse_state->want_nested_loop = false;
13438 /* Defer calling check_omp_intervening_code on pragmas until
13439 cp_parser_statement, because we can't know until we parse
13440 it whether or not the pragma is a statement. */
13441 if (token->type != CPP_PRAGMA)
13442 check_omp_intervening_code (parser);
13443 cp_parser_statement (parser, in_statement_expr, true, NULL);
13444 omp_for_parse_state->want_nested_loop = want_nested_loop;
13446 continue;
13449 /* Parse the statement. */
13450 cp_parser_statement (parser, in_statement_expr, true, NULL);
13454 /* Return true if this is the C++20 version of range-based-for with
13455 init-statement. */
13457 static bool
13458 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13460 bool r = false;
13462 /* Save tokens so that we can put them back. */
13463 cp_lexer_save_tokens (parser->lexer);
13465 /* There has to be an unnested ; followed by an unnested :. */
13466 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13467 /*recovering=*/false,
13468 CPP_SEMICOLON,
13469 /*consume_paren=*/false) != -1)
13470 goto out;
13472 /* We found the semicolon, eat it now. */
13473 cp_lexer_consume_token (parser->lexer);
13475 /* Now look for ':' that is not nested in () or {}. */
13476 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13477 /*recovering=*/false,
13478 CPP_COLON,
13479 /*consume_paren=*/false) == -1);
13481 out:
13482 /* Roll back the tokens we skipped. */
13483 cp_lexer_rollback_tokens (parser->lexer);
13485 return r;
13488 /* Return true if we're looking at (init; cond), false otherwise. */
13490 static bool
13491 cp_parser_init_statement_p (cp_parser *parser)
13493 /* Save tokens so that we can put them back. */
13494 cp_lexer_save_tokens (parser->lexer);
13496 /* Look for ';' that is not nested in () or {}. */
13497 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13498 /*recovering=*/false,
13499 CPP_SEMICOLON,
13500 /*consume_paren=*/false);
13502 /* Roll back the tokens we skipped. */
13503 cp_lexer_rollback_tokens (parser->lexer);
13505 return ret == -1;
13508 /* Parse a selection-statement.
13510 selection-statement:
13511 if ( init-statement [opt] condition ) statement
13512 if ( init-statement [opt] condition ) statement else statement
13513 switch ( init-statement [opt] condition ) statement
13515 Returns the new IF_STMT or SWITCH_STMT.
13517 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13518 is a (possibly labeled) if statement which is not enclosed in
13519 braces and has an else clause. This is used to implement
13520 -Wparentheses.
13522 CHAIN is a vector of if-else-if conditions. This is used to implement
13523 -Wduplicated-cond. */
13525 static tree
13526 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13527 vec<tree> *chain)
13529 cp_token *token;
13530 enum rid keyword;
13531 token_indent_info guard_tinfo;
13533 if (if_p != NULL)
13534 *if_p = false;
13536 /* Peek at the next token. */
13537 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13538 guard_tinfo = get_token_indent_info (token);
13540 /* See what kind of keyword it is. */
13541 keyword = token->keyword;
13542 switch (keyword)
13544 case RID_IF:
13545 case RID_SWITCH:
13547 tree statement;
13548 tree condition;
13550 bool cx = false;
13551 if (keyword == RID_IF
13552 && cp_lexer_next_token_is_keyword (parser->lexer,
13553 RID_CONSTEXPR))
13555 cx = true;
13556 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13557 if (cxx_dialect < cxx17)
13558 pedwarn (tok->location, OPT_Wc__17_extensions,
13559 "%<if constexpr%> only available with "
13560 "%<-std=c++17%> or %<-std=gnu++17%>");
13562 int ce = 0;
13563 if (keyword == RID_IF && !cx)
13565 if (cp_lexer_next_token_is_keyword (parser->lexer,
13566 RID_CONSTEVAL))
13567 ce = 1;
13568 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13569 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13570 RID_CONSTEVAL))
13572 ce = -1;
13573 cp_lexer_consume_token (parser->lexer);
13576 if (ce)
13578 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13579 if (cxx_dialect < cxx23)
13580 pedwarn (tok->location, OPT_Wc__23_extensions,
13581 "%<if consteval%> only available with "
13582 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13584 bool save_in_consteval_if_p = in_consteval_if_p;
13585 statement = begin_if_stmt ();
13586 IF_STMT_CONSTEVAL_P (statement) = true;
13587 condition = finish_if_stmt_cond (boolean_false_node, statement);
13589 gcc_rich_location richloc (tok->location);
13590 bool non_compound_stmt_p = false;
13591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13593 non_compound_stmt_p = true;
13594 richloc.add_fixit_insert_after (tok->location, "{");
13597 in_consteval_if_p |= ce > 0;
13598 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13600 if (non_compound_stmt_p)
13602 location_t before_loc
13603 = cp_lexer_peek_token (parser->lexer)->location;
13604 richloc.add_fixit_insert_before (before_loc, "}");
13605 error_at (&richloc,
13606 "%<if consteval%> requires compound statement");
13607 non_compound_stmt_p = false;
13610 finish_then_clause (statement);
13612 /* If the next token is `else', parse the else-clause. */
13613 if (cp_lexer_next_token_is_keyword (parser->lexer,
13614 RID_ELSE))
13616 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13617 gcc_rich_location else_richloc (else_tok->location);
13618 guard_tinfo = get_token_indent_info (else_tok);
13619 /* Consume the `else' keyword. */
13620 cp_lexer_consume_token (parser->lexer);
13622 begin_else_clause (statement);
13624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13626 non_compound_stmt_p = true;
13627 else_richloc.add_fixit_insert_after (else_tok->location,
13628 "{");
13631 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13632 cp_parser_implicitly_scoped_statement (parser, NULL,
13633 guard_tinfo);
13635 if (non_compound_stmt_p)
13637 location_t before_loc
13638 = cp_lexer_peek_token (parser->lexer)->location;
13639 else_richloc.add_fixit_insert_before (before_loc, "}");
13640 error_at (&else_richloc,
13641 "%<if consteval%> requires compound statement");
13644 finish_else_clause (statement);
13647 in_consteval_if_p = save_in_consteval_if_p;
13648 if (ce < 0)
13650 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13651 if (THEN_CLAUSE (statement) == NULL_TREE)
13652 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13655 finish_if_stmt (statement);
13656 return statement;
13659 /* Look for the `('. */
13660 matching_parens parens;
13661 if (!parens.require_open (parser))
13663 cp_parser_skip_to_end_of_statement (parser);
13664 return error_mark_node;
13667 /* Begin the selection-statement. */
13668 if (keyword == RID_IF)
13670 statement = begin_if_stmt ();
13671 IF_STMT_CONSTEXPR_P (statement) = cx;
13673 else
13674 statement = begin_switch_stmt ();
13676 /* Parse the optional init-statement. */
13677 if (cp_parser_init_statement_p (parser))
13679 tree decl;
13680 if (cxx_dialect < cxx17)
13681 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13682 OPT_Wc__17_extensions,
13683 "init-statement in selection statements only available "
13684 "with %<-std=c++17%> or %<-std=gnu++17%>");
13685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13686 /* A non-empty init-statement can have arbitrary side
13687 effects. */
13688 vec_free (chain);
13689 cp_parser_init_statement (parser, &decl);
13692 /* Parse the condition. */
13693 condition = cp_parser_condition (parser);
13694 /* Look for the `)'. */
13695 if (!parens.require_close (parser))
13696 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13697 /*consume_paren=*/true);
13699 if (keyword == RID_IF)
13701 bool nested_if;
13702 unsigned char in_statement;
13704 /* Add the condition. */
13705 condition = finish_if_stmt_cond (condition, statement);
13707 if (warn_duplicated_cond)
13708 warn_duplicated_cond_add_or_warn (token->location, condition,
13709 &chain);
13711 /* Parse the then-clause. */
13712 in_statement = parser->in_statement;
13713 parser->in_statement |= IN_IF_STMT;
13715 /* Outside a template, the non-selected branch of a constexpr
13716 if is a 'discarded statement', i.e. unevaluated. */
13717 bool was_discarded = in_discarded_stmt;
13718 bool discard_then = (cx && !processing_template_decl
13719 && integer_zerop (condition));
13720 if (discard_then)
13722 in_discarded_stmt = true;
13723 ++c_inhibit_evaluation_warnings;
13726 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13727 guard_tinfo);
13729 parser->in_statement = in_statement;
13731 finish_then_clause (statement);
13733 if (discard_then)
13735 THEN_CLAUSE (statement) = NULL_TREE;
13736 in_discarded_stmt = was_discarded;
13737 --c_inhibit_evaluation_warnings;
13740 /* If the next token is `else', parse the else-clause. */
13741 if (cp_lexer_next_token_is_keyword (parser->lexer,
13742 RID_ELSE))
13744 bool discard_else = (cx && !processing_template_decl
13745 && integer_nonzerop (condition));
13746 if (discard_else)
13748 in_discarded_stmt = true;
13749 ++c_inhibit_evaluation_warnings;
13752 guard_tinfo
13753 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13754 /* Consume the `else' keyword. */
13755 cp_lexer_consume_token (parser->lexer);
13756 if (warn_duplicated_cond)
13758 if (cp_lexer_next_token_is_keyword (parser->lexer,
13759 RID_IF)
13760 && chain == NULL)
13762 /* We've got "if (COND) else if (COND2)". Start
13763 the condition chain and add COND as the first
13764 element. */
13765 chain = new vec<tree> ();
13766 if (!CONSTANT_CLASS_P (condition)
13767 && !TREE_SIDE_EFFECTS (condition))
13769 /* Wrap it in a NOP_EXPR so that we can set the
13770 location of the condition. */
13771 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13772 condition);
13773 SET_EXPR_LOCATION (e, token->location);
13774 chain->safe_push (e);
13777 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13778 RID_IF))
13779 /* This is if-else without subsequent if. Zap the
13780 condition chain; we would have already warned at
13781 this point. */
13782 vec_free (chain);
13784 begin_else_clause (statement);
13785 /* Parse the else-clause. */
13786 cp_parser_implicitly_scoped_statement (parser, NULL,
13787 guard_tinfo, chain);
13789 finish_else_clause (statement);
13791 /* If we are currently parsing a then-clause, then
13792 IF_P will not be NULL. We set it to true to
13793 indicate that this if statement has an else clause.
13794 This may trigger the Wparentheses warning below
13795 when we get back up to the parent if statement. */
13796 if (if_p != NULL)
13797 *if_p = true;
13799 if (discard_else)
13801 ELSE_CLAUSE (statement) = NULL_TREE;
13802 in_discarded_stmt = was_discarded;
13803 --c_inhibit_evaluation_warnings;
13806 else
13808 /* This if statement does not have an else clause. If
13809 NESTED_IF is true, then the then-clause has an if
13810 statement which does have an else clause. We warn
13811 about the potential ambiguity. */
13812 if (nested_if)
13813 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13814 "suggest explicit braces to avoid ambiguous"
13815 " %<else%>");
13816 if (warn_duplicated_cond)
13817 /* We don't need the condition chain anymore. */
13818 vec_free (chain);
13821 /* Now we're all done with the if-statement. */
13822 finish_if_stmt (statement);
13824 else
13826 bool in_switch_statement_p;
13827 unsigned char in_statement;
13829 /* Add the condition. */
13830 finish_switch_cond (condition, statement);
13832 /* Parse the body of the switch-statement. */
13833 in_switch_statement_p = parser->in_switch_statement_p;
13834 in_statement = parser->in_statement;
13835 parser->in_switch_statement_p = true;
13836 parser->in_statement |= IN_SWITCH_STMT;
13837 cp_parser_implicitly_scoped_statement (parser, if_p,
13838 guard_tinfo);
13839 parser->in_switch_statement_p = in_switch_statement_p;
13840 parser->in_statement = in_statement;
13842 /* Now we're all done with the switch-statement. */
13843 finish_switch_stmt (statement);
13846 return statement;
13848 break;
13850 default:
13851 cp_parser_error (parser, "expected selection-statement");
13852 return error_mark_node;
13856 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13857 If we have seen at least one decl-specifier, and the next token is not
13858 a parenthesis (after "int (" we might be looking at a functional cast)
13859 neither we are dealing with a concept-check expression then we must be
13860 looking at a declaration. */
13862 static void
13863 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13864 cp_decl_specifier_seq *decl_specs)
13866 if (decl_specs->any_specifiers_p
13867 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13868 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13869 && !cp_parser_error_occurred (parser)
13870 && !(decl_specs->type
13871 && TREE_CODE (decl_specs->type) == TYPE_DECL
13872 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13873 cp_parser_commit_to_tentative_parse (parser);
13876 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13877 The declarator shall not specify a function or an array. Returns
13878 TRUE if the declarator is valid, FALSE otherwise. */
13880 static bool
13881 cp_parser_check_condition_declarator (cp_parser* parser,
13882 cp_declarator *declarator,
13883 location_t loc)
13885 if (declarator == cp_error_declarator
13886 || function_declarator_p (declarator)
13887 || declarator->kind == cdk_array)
13889 if (declarator == cp_error_declarator)
13890 /* Already complained. */;
13891 else if (declarator->kind == cdk_array)
13892 error_at (loc, "condition declares an array");
13893 else
13894 error_at (loc, "condition declares a function");
13895 if (parser->fully_implicit_function_template_p)
13896 abort_fully_implicit_template (parser);
13897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13898 /*or_comma=*/false,
13899 /*consume_paren=*/false);
13900 return false;
13902 else
13903 return true;
13906 /* Parse a condition.
13908 condition:
13909 expression
13910 type-specifier-seq declarator = initializer-clause
13911 type-specifier-seq declarator braced-init-list
13913 GNU Extension:
13915 condition:
13916 type-specifier-seq declarator asm-specification [opt]
13917 attributes [opt] = assignment-expression
13919 Returns the expression that should be tested. */
13921 static tree
13922 cp_parser_condition (cp_parser* parser)
13924 cp_decl_specifier_seq type_specifiers;
13925 const char *saved_message;
13926 int declares_class_or_enum;
13928 /* Try the declaration first. */
13929 cp_parser_parse_tentatively (parser);
13930 /* New types are not allowed in the type-specifier-seq for a
13931 condition. */
13932 saved_message = parser->type_definition_forbidden_message;
13933 parser->type_definition_forbidden_message
13934 = G_("types may not be defined in conditions");
13935 /* Parse the type-specifier-seq. */
13936 cp_parser_decl_specifier_seq (parser,
13937 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13938 &type_specifiers,
13939 &declares_class_or_enum);
13940 /* Restore the saved message. */
13941 parser->type_definition_forbidden_message = saved_message;
13943 /* Gather the attributes that were provided with the
13944 decl-specifiers. */
13945 tree prefix_attributes = type_specifiers.attributes;
13947 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13949 /* If all is well, we might be looking at a declaration. */
13950 if (!cp_parser_error_occurred (parser))
13952 tree decl;
13953 tree asm_specification;
13954 tree attributes;
13955 cp_declarator *declarator;
13956 tree initializer = NULL_TREE;
13957 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13959 /* Parse the declarator. */
13960 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13961 CP_PARSER_FLAGS_NONE,
13962 /*ctor_dtor_or_conv_p=*/NULL,
13963 /*parenthesized_p=*/NULL,
13964 /*member_p=*/false,
13965 /*friend_p=*/false,
13966 /*static_p=*/false);
13967 /* Parse the attributes. */
13968 attributes = cp_parser_attributes_opt (parser);
13969 /* Parse the asm-specification. */
13970 asm_specification = cp_parser_asm_specification_opt (parser);
13971 /* If the next token is not an `=' or '{', then we might still be
13972 looking at an expression. For example:
13974 if (A(a).x)
13976 looks like a decl-specifier-seq and a declarator -- but then
13977 there is no `=', so this is an expression. */
13978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13979 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13980 cp_parser_simulate_error (parser);
13982 /* If we did see an `=' or '{', then we are looking at a declaration
13983 for sure. */
13984 if (cp_parser_parse_definitely (parser))
13986 tree pushed_scope;
13987 bool non_constant_p = false;
13988 int flags = LOOKUP_ONLYCONVERTING;
13990 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13991 return error_mark_node;
13993 /* Create the declaration. */
13994 decl = start_decl (declarator, &type_specifiers,
13995 /*initialized_p=*/true,
13996 attributes, prefix_attributes,
13997 &pushed_scope);
13999 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
14000 /* Parse the initializer. */
14001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14003 initializer = cp_parser_braced_list (parser, &non_constant_p);
14004 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
14005 flags = 0;
14007 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14009 /* Consume the `='. */
14010 cp_lexer_consume_token (parser->lexer);
14011 initializer = cp_parser_initializer_clause (parser,
14012 &non_constant_p);
14014 else
14016 cp_parser_error (parser, "expected initializer");
14017 initializer = error_mark_node;
14019 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
14020 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14022 /* Process the initializer. */
14023 cp_finish_decl (decl,
14024 initializer, !non_constant_p,
14025 asm_specification,
14026 flags);
14028 if (pushed_scope)
14029 pop_scope (pushed_scope);
14031 return convert_from_reference (decl);
14034 /* If we didn't even get past the declarator successfully, we are
14035 definitely not looking at a declaration. */
14036 else
14037 cp_parser_abort_tentative_parse (parser);
14039 /* Otherwise, we are looking at an expression. */
14040 return cp_parser_expression (parser);
14043 /* Parses a for-statement or range-for-statement until the closing ')',
14044 not included. */
14046 static tree
14047 cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
14049 tree init, scope, decl;
14050 bool is_range_for;
14052 /* Begin the for-statement. */
14053 scope = begin_for_scope (&init);
14055 /* Maybe parse the optional init-statement in a range-based for loop. */
14056 if (cp_parser_range_based_for_with_init_p (parser)
14057 /* Checked for diagnostic purposes only. */
14058 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14060 tree dummy;
14061 cp_parser_init_statement (parser, &dummy);
14062 if (cxx_dialect < cxx20)
14064 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14065 OPT_Wc__20_extensions,
14066 "range-based %<for%> loops with initializer only "
14067 "available with %<-std=c++20%> or %<-std=gnu++20%>");
14068 decl = error_mark_node;
14072 /* Parse the initialization. */
14073 is_range_for = cp_parser_init_statement (parser, &decl);
14075 if (is_range_for)
14076 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
14077 novector, false);
14078 else
14079 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
14082 static tree
14083 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
14084 tree unroll, bool novector)
14086 /* Normal for loop */
14087 tree condition = NULL_TREE;
14088 tree expression = NULL_TREE;
14089 tree stmt;
14091 stmt = begin_for_stmt (scope, init);
14092 /* The init-statement has already been parsed in
14093 cp_parser_init_statement, so no work is needed here. */
14094 finish_init_stmt (stmt);
14096 /* If there's a condition, process it. */
14097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14098 condition = cp_parser_condition (parser);
14099 else if (ivdep)
14101 cp_parser_error (parser, "missing loop condition in loop with "
14102 "%<GCC ivdep%> pragma");
14103 condition = error_mark_node;
14105 else if (unroll)
14107 cp_parser_error (parser, "missing loop condition in loop with "
14108 "%<GCC unroll%> pragma");
14109 condition = error_mark_node;
14111 finish_for_cond (condition, stmt, ivdep, unroll, novector);
14112 /* Look for the `;'. */
14113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14115 /* If there's an expression, process it. */
14116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14117 expression = cp_parser_expression (parser);
14118 finish_for_expr (expression, stmt);
14120 return stmt;
14123 /* Tries to parse a range-based for-statement:
14125 range-based-for:
14126 decl-specifier-seq declarator : expression
14128 The decl-specifier-seq declarator and the `:' are already parsed by
14129 cp_parser_init_statement. If processing_template_decl it returns a
14130 newly created RANGE_FOR_STMT; if not, it is converted to a
14131 regular FOR_STMT. */
14133 static tree
14134 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
14135 bool ivdep, tree unroll, bool novector, bool is_omp)
14137 tree stmt, range_expr;
14138 auto_vec <cxx_binding *, 16> bindings;
14139 auto_vec <tree, 16> names;
14140 cp_decomp decomp_d, *decomp = NULL;
14142 /* Get the range declaration momentarily out of the way so that
14143 the range expression doesn't clash with it. */
14144 if (range_decl != error_mark_node)
14146 if (DECL_HAS_VALUE_EXPR_P (range_decl))
14148 tree v = DECL_VALUE_EXPR (range_decl);
14149 /* For decomposition declaration get all of the corresponding
14150 declarations out of the way. */
14151 if (TREE_CODE (v) == ARRAY_REF
14152 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
14154 tree d = range_decl;
14155 range_decl = TREE_OPERAND (v, 0);
14156 decomp = &decomp_d;
14157 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
14158 decomp->decl = d;
14159 for (unsigned int i = 0; i < decomp->count;
14160 i++, d = DECL_CHAIN (d))
14162 tree name = DECL_NAME (d);
14163 names.safe_push (name);
14164 bindings.safe_push (IDENTIFIER_BINDING (name));
14165 IDENTIFIER_BINDING (name)
14166 = IDENTIFIER_BINDING (name)->previous;
14170 if (names.is_empty ())
14172 tree name = DECL_NAME (range_decl);
14173 names.safe_push (name);
14174 bindings.safe_push (IDENTIFIER_BINDING (name));
14175 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
14179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14180 range_expr = cp_parser_braced_list (parser);
14181 else
14182 range_expr = cp_parser_expression (parser);
14184 /* Put the range declaration(s) back into scope. */
14185 for (unsigned int i = 0; i < names.length (); i++)
14187 cxx_binding *binding = bindings[i];
14188 binding->previous = IDENTIFIER_BINDING (names[i]);
14189 IDENTIFIER_BINDING (names[i]) = binding;
14192 /* finish_omp_for has its own code for the following, so just
14193 return the range_expr instead. */
14194 if (is_omp)
14195 return range_expr;
14197 /* If in template, STMT is converted to a normal for-statement
14198 at instantiation. If not, it is done just ahead. */
14199 if (processing_template_decl)
14201 if (check_for_bare_parameter_packs (range_expr))
14202 range_expr = error_mark_node;
14203 stmt = begin_range_for_stmt (scope, init);
14204 if (ivdep)
14205 RANGE_FOR_IVDEP (stmt) = 1;
14206 if (unroll)
14207 RANGE_FOR_UNROLL (stmt) = unroll;
14208 if (novector)
14209 RANGE_FOR_NOVECTOR (stmt) = 1;
14210 finish_range_for_decl (stmt, range_decl, range_expr);
14211 if (!type_dependent_expression_p (range_expr)
14212 /* do_auto_deduction doesn't mess with template init-lists. */
14213 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14214 do_range_for_auto_deduction (range_decl, range_expr, decomp);
14216 else
14218 stmt = begin_for_stmt (scope, init);
14219 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
14220 ivdep, unroll, novector);
14222 return stmt;
14225 /* Subroutine of cp_convert_range_for: given the initializer expression,
14226 builds up the range temporary. */
14228 static tree
14229 build_range_temp (tree range_expr)
14231 /* Find out the type deduced by the declaration
14232 `auto &&__range = range_expr'. */
14233 tree auto_node = make_auto ();
14234 tree range_type = cp_build_reference_type (auto_node, true);
14235 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14237 /* Create the __range variable. */
14238 tree range_temp = build_decl (input_location, VAR_DECL,
14239 for_range__identifier, range_type);
14240 TREE_USED (range_temp) = 1;
14241 DECL_ARTIFICIAL (range_temp) = 1;
14243 return range_temp;
14246 /* Used by cp_parser_range_for in template context: we aren't going to
14247 do a full conversion yet, but we still need to resolve auto in the
14248 type of the for-range-declaration if present. This is basically
14249 a shortcut version of cp_convert_range_for. */
14251 static void
14252 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14254 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14255 if (auto_node)
14257 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14258 range_temp = convert_from_reference (build_range_temp (range_expr));
14259 iter_type = (cp_parser_perform_range_for_lookup
14260 (range_temp, &begin_dummy, &end_dummy));
14261 if (iter_type)
14263 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14264 iter_type);
14265 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14266 RO_UNARY_STAR, NULL_TREE,
14267 tf_warning_or_error);
14268 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14269 iter_decl, auto_node,
14270 tf_warning_or_error,
14271 adc_variable_type);
14272 if (DECL_DECOMPOSITION_P (decl))
14273 cp_finish_decomp (decl, decomp);
14278 /* Warns when the loop variable should be changed to a reference type to
14279 avoid unnecessary copying. I.e., from
14281 for (const auto x : range)
14283 where range returns a reference, to
14285 for (const auto &x : range)
14287 if this version doesn't make a copy.
14289 This function also warns when the loop variable is initialized with
14290 a value of a different type resulting in a copy:
14292 int arr[10];
14293 for (const double &x : arr)
14295 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14296 This function is never called when processing_template_decl is on. */
14298 static void
14299 warn_for_range_copy (tree decl, tree expr)
14301 if (!warn_range_loop_construct
14302 || decl == error_mark_node)
14303 return;
14305 location_t loc = DECL_SOURCE_LOCATION (decl);
14306 tree type = TREE_TYPE (decl);
14308 if (from_macro_expansion_at (loc))
14309 return;
14311 if (TYPE_REF_P (type))
14313 if (glvalue_p (expr)
14314 && ref_conv_binds_to_temporary (type, expr).is_true ())
14316 auto_diagnostic_group d;
14317 if (warning_at (loc, OPT_Wrange_loop_construct,
14318 "loop variable %qD of type %qT binds to a temporary "
14319 "constructed from type %qT", decl, type,
14320 TREE_TYPE (expr)))
14322 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14323 TYPE_QUAL_CONST);
14324 ref = cp_build_reference_type (ref, /*rval*/false);
14325 inform (loc, "use non-reference type %qT to make the copy "
14326 "explicit or %qT to prevent copying",
14327 non_reference (type), ref);
14330 return;
14332 else if (!CP_TYPE_CONST_P (type))
14333 return;
14335 /* Since small trivially copyable types are cheap to copy, we suppress the
14336 warning for them. 64B is a common size of a cache line. */
14337 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14338 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14339 && trivially_copyable_p (type)))
14340 return;
14342 /* If we can initialize a reference directly, suggest that to avoid the
14343 copy. */
14344 tree rtype = cp_build_reference_type (type, /*rval*/false);
14345 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14347 auto_diagnostic_group d;
14348 if (warning_at (loc, OPT_Wrange_loop_construct,
14349 "loop variable %qD creates a copy from type %qT",
14350 decl, type))
14352 gcc_rich_location richloc (loc);
14353 richloc.add_fixit_insert_before ("&");
14354 inform (&richloc, "use reference type to prevent copying");
14359 /* Converts a range-based for-statement into a normal
14360 for-statement, as per the definition.
14362 for (RANGE_DECL : RANGE_EXPR)
14363 BLOCK
14365 should be equivalent to:
14368 auto &&__range = RANGE_EXPR;
14369 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14370 __begin != __end;
14371 ++__begin)
14373 RANGE_DECL = *__begin;
14374 BLOCK
14378 If RANGE_EXPR is an array:
14379 BEGIN_EXPR = __range
14380 END_EXPR = __range + ARRAY_SIZE(__range)
14381 Else if RANGE_EXPR has a member 'begin' or 'end':
14382 BEGIN_EXPR = __range.begin()
14383 END_EXPR = __range.end()
14384 Else:
14385 BEGIN_EXPR = begin(__range)
14386 END_EXPR = end(__range);
14388 If __range has a member 'begin' but not 'end', or vice versa, we must
14389 still use the second alternative (it will surely fail, however).
14390 When calling begin()/end() in the third alternative we must use
14391 argument dependent lookup, but always considering 'std' as an associated
14392 namespace. */
14394 tree
14395 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14396 cp_decomp *decomp, bool ivdep, tree unroll,
14397 bool novector)
14399 tree begin, end;
14400 tree iter_type, begin_expr, end_expr;
14401 tree condition, expression;
14403 range_expr = mark_lvalue_use (range_expr);
14405 if (range_decl == error_mark_node || range_expr == error_mark_node)
14406 /* If an error happened previously do nothing or else a lot of
14407 unhelpful errors would be issued. */
14408 begin_expr = end_expr = iter_type = error_mark_node;
14409 else
14411 tree range_temp;
14413 if (VAR_P (range_expr)
14414 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14415 /* Can't bind a reference to an array of runtime bound. */
14416 range_temp = range_expr;
14417 else
14419 range_temp = build_range_temp (range_expr);
14420 pushdecl (range_temp);
14421 cp_finish_decl (range_temp, range_expr,
14422 /*is_constant_init*/false, NULL_TREE,
14423 LOOKUP_ONLYCONVERTING);
14424 range_temp = convert_from_reference (range_temp);
14426 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14427 &begin_expr, &end_expr);
14430 /* The new for initialization statement. */
14431 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14432 iter_type);
14433 TREE_USED (begin) = 1;
14434 DECL_ARTIFICIAL (begin) = 1;
14435 pushdecl (begin);
14436 cp_finish_decl (begin, begin_expr,
14437 /*is_constant_init*/false, NULL_TREE,
14438 LOOKUP_ONLYCONVERTING);
14440 if (cxx_dialect >= cxx17)
14441 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14442 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14443 TREE_USED (end) = 1;
14444 DECL_ARTIFICIAL (end) = 1;
14445 pushdecl (end);
14446 cp_finish_decl (end, end_expr,
14447 /*is_constant_init*/false, NULL_TREE,
14448 LOOKUP_ONLYCONVERTING);
14450 finish_init_stmt (statement);
14452 /* The new for condition. */
14453 condition = build_x_binary_op (input_location, NE_EXPR,
14454 begin, ERROR_MARK,
14455 end, ERROR_MARK,
14456 NULL_TREE, NULL, tf_warning_or_error);
14457 finish_for_cond (condition, statement, ivdep, unroll, novector);
14459 /* The new increment expression. */
14460 expression = finish_unary_op_expr (input_location,
14461 PREINCREMENT_EXPR, begin,
14462 tf_warning_or_error);
14463 finish_for_expr (expression, statement);
14465 /* The declaration is initialized with *__begin inside the loop body. */
14466 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14467 NULL_TREE, tf_warning_or_error);
14468 cp_finish_decl (range_decl, deref_begin,
14469 /*is_constant_init*/false, NULL_TREE,
14470 LOOKUP_ONLYCONVERTING, decomp);
14471 if (DECL_DECOMPOSITION_P (range_decl))
14472 cp_finish_decomp (range_decl, decomp);
14474 warn_for_range_copy (range_decl, deref_begin);
14476 return statement;
14479 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14480 We need to solve both at the same time because the method used
14481 depends on the existence of members begin or end.
14482 Returns the type deduced for the iterator expression. */
14484 static tree
14485 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14487 if (error_operand_p (range))
14489 *begin = *end = error_mark_node;
14490 return error_mark_node;
14493 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14495 error ("range-based %<for%> expression of type %qT "
14496 "has incomplete type", TREE_TYPE (range));
14497 *begin = *end = error_mark_node;
14498 return error_mark_node;
14500 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14502 /* If RANGE is an array, we will use pointer arithmetic. */
14503 *begin = decay_conversion (range, tf_warning_or_error);
14504 *end = build_binary_op (input_location, PLUS_EXPR,
14505 range,
14506 array_type_nelts_top (TREE_TYPE (range)),
14507 false);
14508 return TREE_TYPE (*begin);
14510 else
14512 /* If it is not an array, we must do a bit of magic. */
14513 tree id_begin, id_end;
14514 tree member_begin, member_end;
14516 *begin = *end = error_mark_node;
14518 id_begin = get_identifier ("begin");
14519 id_end = get_identifier ("end");
14520 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14521 /*protect=*/2, /*want_type=*/false,
14522 tf_warning_or_error);
14523 member_end = lookup_member (TREE_TYPE (range), id_end,
14524 /*protect=*/2, /*want_type=*/false,
14525 tf_warning_or_error);
14527 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14529 /* Use the member functions. */
14530 *begin = cp_parser_range_for_member_function (range, id_begin);
14531 *end = cp_parser_range_for_member_function (range, id_end);
14533 else
14535 /* Use global functions with ADL. */
14536 releasing_vec vec;
14538 vec_safe_push (vec, range);
14540 member_begin = perform_koenig_lookup (id_begin, vec,
14541 tf_warning_or_error);
14542 *begin = finish_call_expr (member_begin, &vec, false, true,
14543 tf_warning_or_error);
14544 member_end = perform_koenig_lookup (id_end, vec,
14545 tf_warning_or_error);
14546 *end = finish_call_expr (member_end, &vec, false, true,
14547 tf_warning_or_error);
14550 /* Last common checks. */
14551 if (*begin == error_mark_node || *end == error_mark_node)
14553 /* If one of the expressions is an error do no more checks. */
14554 *begin = *end = error_mark_node;
14555 return error_mark_node;
14557 else if (type_dependent_expression_p (*begin)
14558 || type_dependent_expression_p (*end))
14559 /* Can happen, when, eg, in a template context, Koenig lookup
14560 can't resolve begin/end (c++/58503). */
14561 return NULL_TREE;
14562 else
14564 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14565 /* The unqualified type of the __begin and __end temporaries should
14566 be the same, as required by the multiple auto declaration. */
14567 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14569 if (cxx_dialect >= cxx17
14570 && (build_x_binary_op (input_location, NE_EXPR,
14571 *begin, ERROR_MARK,
14572 *end, ERROR_MARK,
14573 NULL_TREE, NULL, tf_none)
14574 != error_mark_node))
14575 /* P0184R0 allows __begin and __end to have different types,
14576 but make sure they are comparable so we can give a better
14577 diagnostic. */;
14578 else
14579 error ("inconsistent begin/end types in range-based %<for%> "
14580 "statement: %qT and %qT",
14581 TREE_TYPE (*begin), TREE_TYPE (*end));
14583 return iter_type;
14588 /* Helper function for cp_parser_perform_range_for_lookup.
14589 Builds a tree for RANGE.IDENTIFIER(). */
14591 static tree
14592 cp_parser_range_for_member_function (tree range, tree identifier)
14594 tree member, res;
14596 member = finish_class_member_access_expr (range, identifier,
14597 false, tf_warning_or_error);
14598 if (member == error_mark_node)
14599 return error_mark_node;
14601 releasing_vec vec;
14602 res = finish_call_expr (member, &vec,
14603 /*disallow_virtual=*/false,
14604 /*koenig_p=*/false,
14605 tf_warning_or_error);
14606 return res;
14609 /* Parse an iteration-statement.
14611 iteration-statement:
14612 while ( condition ) statement
14613 do statement while ( expression ) ;
14614 for ( init-statement condition [opt] ; expression [opt] )
14615 statement
14617 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14619 static tree
14620 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14621 tree unroll, bool novector)
14623 cp_token *token;
14624 enum rid keyword;
14625 tree statement;
14626 unsigned char in_statement;
14627 token_indent_info guard_tinfo;
14629 /* Peek at the next token. */
14630 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14631 if (!token)
14632 return error_mark_node;
14634 guard_tinfo = get_token_indent_info (token);
14636 /* Remember whether or not we are already within an iteration
14637 statement. */
14638 in_statement = parser->in_statement;
14640 /* Special case for OMP loop intervening code. Parsing of permitted
14641 collapsed loop nests is handled elsewhere. */
14642 if (parser->omp_for_parse_state)
14644 error_at (token->location,
14645 "loop not permitted in intervening code in OpenMP loop body");
14646 parser->omp_for_parse_state->fail = true;
14649 /* See what kind of keyword it is. */
14650 keyword = token->keyword;
14651 switch (keyword)
14653 case RID_WHILE:
14655 tree condition;
14657 /* Begin the while-statement. */
14658 statement = begin_while_stmt ();
14659 /* Look for the `('. */
14660 matching_parens parens;
14661 parens.require_open (parser);
14662 /* Parse the condition. */
14663 condition = cp_parser_condition (parser);
14664 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14665 /* Look for the `)'. */
14666 parens.require_close (parser);
14667 /* Parse the dependent statement. */
14668 parser->in_statement = IN_ITERATION_STMT;
14669 bool prev = note_iteration_stmt_body_start ();
14670 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14671 note_iteration_stmt_body_end (prev);
14672 parser->in_statement = in_statement;
14673 /* We're done with the while-statement. */
14674 finish_while_stmt (statement);
14676 break;
14678 case RID_DO:
14680 tree expression;
14682 /* Begin the do-statement. */
14683 statement = begin_do_stmt ();
14684 /* Parse the body of the do-statement. */
14685 parser->in_statement = IN_ITERATION_STMT;
14686 bool prev = note_iteration_stmt_body_start ();
14687 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14688 note_iteration_stmt_body_end (prev);
14689 parser->in_statement = in_statement;
14690 finish_do_body (statement);
14691 /* Look for the `while' keyword. */
14692 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14693 /* Look for the `('. */
14694 matching_parens parens;
14695 parens.require_open (parser);
14696 /* Parse the expression. */
14697 expression = cp_parser_expression (parser);
14698 /* We're done with the do-statement. */
14699 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14700 /* Look for the `)'. */
14701 parens.require_close (parser);
14702 /* Look for the `;'. */
14703 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14705 break;
14707 case RID_FOR:
14709 /* Look for the `('. */
14710 matching_parens parens;
14711 parens.require_open (parser);
14713 statement = cp_parser_for (parser, ivdep, unroll, novector);
14715 /* Look for the `)'. */
14716 parens.require_close (parser);
14718 /* Parse the body of the for-statement. */
14719 parser->in_statement = IN_ITERATION_STMT;
14720 bool prev = note_iteration_stmt_body_start ();
14721 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14722 note_iteration_stmt_body_end (prev);
14723 parser->in_statement = in_statement;
14725 /* We're done with the for-statement. */
14726 finish_for_stmt (statement);
14728 break;
14730 default:
14731 cp_parser_error (parser, "expected iteration-statement");
14732 statement = error_mark_node;
14733 break;
14736 return statement;
14739 /* Parse an init-statement or the declarator of a range-based-for.
14740 Returns true if a range-based-for declaration is seen.
14742 init-statement:
14743 expression-statement
14744 simple-declaration
14745 alias-declaration */
14747 static bool
14748 cp_parser_init_statement (cp_parser *parser, tree *decl)
14750 /* If the next token is a `;', then we have an empty
14751 expression-statement. Grammatically, this is also a
14752 simple-declaration, but an invalid one, because it does not
14753 declare anything. Therefore, if we did not handle this case
14754 specially, we would issue an error message about an invalid
14755 declaration. */
14756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14758 bool is_range_for = false;
14759 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14761 /* A colon is used in range-based for. */
14762 parser->colon_corrects_to_scope_p = false;
14764 /* We're going to speculatively look for a declaration, falling back
14765 to an expression, if necessary. */
14766 cp_parser_parse_tentatively (parser);
14767 bool expect_semicolon_p = true;
14768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14770 cp_parser_alias_declaration (parser);
14771 expect_semicolon_p = false;
14772 if (cxx_dialect < cxx23
14773 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14774 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14775 OPT_Wc__23_extensions,
14776 "alias-declaration in init-statement only "
14777 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14779 else
14780 /* Parse the declaration. */
14781 cp_parser_simple_declaration (parser,
14782 /*function_definition_allowed_p=*/false,
14783 decl);
14784 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14785 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14787 /* It is a range-for, consume the ':'. */
14788 cp_lexer_consume_token (parser->lexer);
14789 is_range_for = true;
14790 if (cxx_dialect < cxx11)
14791 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14792 OPT_Wc__11_extensions,
14793 "range-based %<for%> loops only available with "
14794 "%<-std=c++11%> or %<-std=gnu++11%>");
14796 else if (expect_semicolon_p)
14797 /* The ';' is not consumed yet because we told
14798 cp_parser_simple_declaration not to. */
14799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14801 if (cp_parser_parse_definitely (parser))
14802 return is_range_for;
14803 /* If the tentative parse failed, then we shall need to look for an
14804 expression-statement. */
14806 /* If we are here, it is an expression-statement. */
14807 cp_parser_expression_statement (parser, NULL_TREE);
14808 return false;
14811 /* Parse a jump-statement.
14813 jump-statement:
14814 break ;
14815 continue ;
14816 return expression [opt] ;
14817 return braced-init-list ;
14818 coroutine-return-statement;
14819 goto identifier ;
14821 GNU extension:
14823 jump-statement:
14824 goto * expression ;
14826 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14828 static tree
14829 cp_parser_jump_statement (cp_parser* parser)
14831 tree statement = error_mark_node;
14832 cp_token *token;
14833 enum rid keyword;
14834 unsigned char in_statement;
14836 /* Peek at the next token. */
14837 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14838 if (!token)
14839 return error_mark_node;
14841 /* See what kind of keyword it is. */
14842 keyword = token->keyword;
14843 switch (keyword)
14845 case RID_BREAK:
14846 in_statement = parser->in_statement & ~IN_IF_STMT;
14847 switch (in_statement)
14849 case 0:
14850 error_at (token->location, "break statement not within loop or switch");
14851 break;
14852 default:
14853 gcc_assert ((in_statement & IN_SWITCH_STMT)
14854 || in_statement == IN_ITERATION_STMT);
14855 statement = finish_break_stmt ();
14856 if (in_statement == IN_ITERATION_STMT)
14857 break_maybe_infinite_loop ();
14858 break;
14859 case IN_OMP_BLOCK:
14860 error_at (token->location, "invalid exit from OpenMP structured block");
14861 break;
14862 case IN_OMP_FOR:
14863 error_at (token->location, "break statement used with OpenMP for loop");
14864 break;
14866 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14867 break;
14869 case RID_CONTINUE:
14870 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14872 case 0:
14873 error_at (token->location, "continue statement not within a loop");
14874 break;
14875 /* Fall through. */
14876 case IN_ITERATION_STMT:
14877 case IN_OMP_FOR:
14878 statement = finish_continue_stmt ();
14879 break;
14880 case IN_OMP_BLOCK:
14881 error_at (token->location, "invalid exit from OpenMP structured block");
14882 break;
14883 default:
14884 gcc_unreachable ();
14886 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14887 break;
14889 case RID_CO_RETURN:
14890 case RID_RETURN:
14892 tree expr;
14894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14896 cp_lexer_set_source_position (parser->lexer);
14897 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14898 expr = cp_parser_braced_list (parser);
14900 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14901 expr = cp_parser_expression (parser);
14902 else
14903 /* If the next token is a `;', then there is no
14904 expression. */
14905 expr = NULL_TREE;
14906 /* Build the return-statement, check co-return first, since type
14907 deduction is not valid there. */
14908 if (keyword == RID_CO_RETURN)
14909 statement = finish_co_return_stmt (token->location, expr);
14910 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14911 /* Don't deduce from a discarded return statement. */;
14912 else
14913 statement = finish_return_stmt (expr);
14914 /* Look for the final `;'. */
14915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14917 break;
14919 case RID_GOTO:
14920 if (parser->in_function_body
14921 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14922 && cxx_dialect < cxx23)
14924 error ("%<goto%> in %<constexpr%> function only available with "
14925 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14926 cp_function_chain->invalid_constexpr = true;
14929 /* Create the goto-statement. */
14930 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14932 /* Issue a warning about this use of a GNU extension. */
14933 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14934 /* Consume the '*' token. */
14935 cp_lexer_consume_token (parser->lexer);
14936 /* Parse the dependent expression. */
14937 finish_goto_stmt (cp_parser_expression (parser));
14939 else
14940 finish_goto_stmt (cp_parser_identifier (parser));
14941 /* Look for the final `;'. */
14942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14943 break;
14945 default:
14946 cp_parser_error (parser, "expected jump-statement");
14947 break;
14950 return statement;
14953 /* Parse a declaration-statement.
14955 declaration-statement:
14956 block-declaration */
14958 static void
14959 cp_parser_declaration_statement (cp_parser* parser)
14961 void *p;
14963 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14964 p = obstack_alloc (&declarator_obstack, 0);
14966 /* Parse the block-declaration. */
14967 cp_parser_block_declaration (parser, /*statement_p=*/true);
14969 /* Free any declarators allocated. */
14970 obstack_free (&declarator_obstack, p);
14973 /* Some dependent statements (like `if (cond) statement'), are
14974 implicitly in their own scope. In other words, if the statement is
14975 a single statement (as opposed to a compound-statement), it is
14976 none-the-less treated as if it were enclosed in braces. Any
14977 declarations appearing in the dependent statement are out of scope
14978 after control passes that point. This function parses a statement,
14979 but ensures that is in its own scope, even if it is not a
14980 compound-statement.
14982 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14983 is a (possibly labeled) if statement which is not enclosed in
14984 braces and has an else clause. This is used to implement
14985 -Wparentheses.
14987 CHAIN is a vector of if-else-if conditions. This is used to implement
14988 -Wduplicated-cond.
14990 Returns the new statement. */
14992 static tree
14993 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14994 const token_indent_info &guard_tinfo,
14995 vec<tree> *chain)
14997 tree statement;
14998 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14999 location_t body_loc_after_labels = UNKNOWN_LOCATION;
15000 token_indent_info body_tinfo
15001 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15003 if (if_p != NULL)
15004 *if_p = false;
15006 /* Mark if () ; with a special NOP_EXPR. */
15007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15009 cp_lexer_consume_token (parser->lexer);
15010 statement = add_stmt (build_empty_stmt (body_loc));
15012 if (guard_tinfo.keyword == RID_IF
15013 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
15014 warning_at (body_loc, OPT_Wempty_body,
15015 "suggest braces around empty body in an %<if%> statement");
15016 else if (guard_tinfo.keyword == RID_ELSE)
15017 warning_at (body_loc, OPT_Wempty_body,
15018 "suggest braces around empty body in an %<else%> statement");
15020 /* if a compound is opened, we simply parse the statement directly. */
15021 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15022 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
15023 /* If the token is not a `{', then we must take special action. */
15024 else
15026 /* Create a compound-statement. */
15027 statement = begin_compound_stmt (0);
15028 /* Parse the dependent-statement. */
15029 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
15030 &body_loc_after_labels);
15031 /* Finish the dummy compound-statement. */
15032 finish_compound_stmt (statement);
15035 token_indent_info next_tinfo
15036 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15037 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15039 if (body_loc_after_labels != UNKNOWN_LOCATION
15040 && next_tinfo.type != CPP_SEMICOLON)
15041 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
15042 guard_tinfo.location, guard_tinfo.keyword);
15044 /* Return the statement. */
15045 return statement;
15048 /* For some dependent statements (like `while (cond) statement'), we
15049 have already created a scope. Therefore, even if the dependent
15050 statement is a compound-statement, we do not want to create another
15051 scope. */
15053 static void
15054 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
15055 const token_indent_info &guard_tinfo)
15057 /* If the token is a `{', then we must take special action. */
15058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15060 token_indent_info body_tinfo
15061 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15062 location_t loc_after_labels = UNKNOWN_LOCATION;
15064 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
15065 &loc_after_labels);
15066 token_indent_info next_tinfo
15067 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15068 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15070 if (loc_after_labels != UNKNOWN_LOCATION
15071 && next_tinfo.type != CPP_SEMICOLON)
15072 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
15073 guard_tinfo.location,
15074 guard_tinfo.keyword);
15076 else
15078 /* Avoid calling cp_parser_compound_statement, so that we
15079 don't create a new scope. Do everything else by hand. */
15080 matching_braces braces;
15081 braces.require_open (parser);
15082 /* If the next keyword is `__label__' we have a label declaration. */
15083 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
15084 cp_parser_label_declaration (parser);
15085 /* Parse an (optional) statement-seq. */
15086 cp_parser_statement_seq_opt (parser, NULL_TREE);
15087 braces.require_close (parser);
15091 /* Modules */
15093 /* Parse a module-name or module-partition.
15095 module-name:
15096 module-name-qualifier [opt] identifier
15098 module-partition:
15099 : module-name-qualifier [opt] identifier
15101 module-name-qualifier:
15102 identifier .
15103 module-name-qualifier identifier .
15105 Returns a pointer to the module object, or NULL on failure.
15106 For PARTITION_P, PARENT is the module this is a partition of. */
15108 static module_state *
15109 cp_parser_module_name (cp_parser *parser, bool partition_p = false,
15110 module_state *parent = NULL)
15112 if (partition_p
15113 && cp_lexer_consume_token (parser->lexer)->type != CPP_COLON)
15114 return NULL;
15116 for (;;)
15118 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
15120 if (partition_p)
15121 cp_parser_error (parser, "expected module-partition");
15122 else
15123 cp_parser_error (parser, "expected module-name");
15124 return NULL;
15127 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
15128 parent = get_module (name, parent, partition_p);
15129 if (cp_lexer_peek_token (parser->lexer)->type != CPP_DOT)
15130 break;
15132 cp_lexer_consume_token (parser->lexer);
15135 return parent;
15138 /* Parse a module-partition. Defers to cp_parser_module_name. */
15140 static module_state *
15141 cp_parser_module_partition (cp_parser *parser, module_state *parent = NULL)
15143 return cp_parser_module_name (parser, /*partition_p=*/true, parent);
15146 /* Named module-declaration
15147 __module ; PRAGMA_EOL
15148 __module : private ; PRAGMA_EOL (unimplemented)
15149 [__export] __module module-name module-partition [opt]
15150 attr-spec-seq-opt ; PRAGMA_EOL
15153 static module_parse
15154 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
15155 bool exporting)
15157 /* We're a pseudo pragma. */
15158 parser->lexer->in_pragma = true;
15159 cp_token *token = cp_lexer_consume_token (parser->lexer);
15161 tree scope = current_scope ();
15162 if (flag_header_unit)
15164 error_at (token->location,
15165 "module-declaration not permitted in header-unit");
15166 goto skip_eol;
15168 else if (scope != global_namespace)
15170 error_at (token->location, "module-declaration must be at global scope");
15171 inform (DECL_SOURCE_LOCATION (scope), "scope opened here");
15172 goto skip_eol;
15174 else if (mp_state == MP_FIRST && !exporting
15175 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15177 /* Start global module fragment. */
15178 cp_lexer_consume_token (parser->lexer);
15179 module_kind = MK_NAMED;
15180 mp_state = MP_GLOBAL;
15181 cp_parser_require_pragma_eol (parser, token);
15183 else if (!exporting
15184 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15185 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
15186 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
15188 cp_lexer_consume_token (parser->lexer);
15189 cp_lexer_consume_token (parser->lexer);
15190 cp_lexer_consume_token (parser->lexer);
15191 cp_parser_require_pragma_eol (parser, token);
15193 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
15194 && module_has_cmi_p ())
15196 mp_state = MP_PRIVATE_IMPORTS;
15197 sorry_at (token->location, "private module fragment");
15199 else
15200 error_at (token->location,
15201 "private module fragment only permitted in purview"
15202 " of module interface or partition");
15204 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
15206 /* Neither the first declaration, nor in a GMF. */
15207 error_at (token->location, "module-declaration only permitted as first"
15208 " declaration, or ending a global module fragment");
15209 skip_eol:
15210 cp_parser_skip_to_pragma_eol (parser, token);
15212 else
15214 module_state *mod = cp_parser_module_name (parser);
15215 if (mod && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
15216 mod = cp_parser_module_partition (parser, mod);
15217 tree attrs = cp_parser_attributes_opt (parser);
15219 if (mod)
15220 mp_state = MP_PURVIEW_IMPORTS;
15221 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15222 goto skip_eol;
15224 declare_module (mod, token->location, exporting, attrs, parse_in);
15225 cp_parser_require_pragma_eol (parser, token);
15228 return mp_state;
15231 /* Import-declaration
15232 __import module-name attr-spec-seq-opt ; PRAGMA_EOL
15233 __import module-partition attr-spec-seq-opt ; PRAGMA_EOL
15234 __import header-name attr-spec-seq-opt ; PRAGMA_EOL
15237 static void
15238 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
15239 bool exporting)
15241 /* We're a pseudo pragma. */
15242 parser->lexer->in_pragma = true;
15243 cp_token *token = cp_lexer_consume_token (parser->lexer);
15245 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
15247 error_at (token->location, "post-module-declaration"
15248 " imports must be contiguous");
15249 note_lexer:
15250 inform (token->location, "perhaps insert a line break after"
15251 " %<import%>, or other disambiguation, to prevent this"
15252 " being considered a module control-line");
15253 skip_eol:
15254 cp_parser_skip_to_pragma_eol (parser, token);
15256 else if (current_scope () != global_namespace)
15258 error_at (token->location, "import-declaration must be at global scope");
15259 goto note_lexer;
15261 else
15263 module_state *mod = NULL;
15264 cp_token *next = cp_lexer_peek_token (parser->lexer);
15265 if (next->type == CPP_HEADER_NAME)
15267 cp_lexer_consume_token (parser->lexer);
15268 mod = get_module (next->u.value);
15270 else if (next->type == CPP_COLON)
15272 /* An import specifying a module-partition shall only appear after the
15273 module-declaration in a module unit: [module.import]/4. */
15274 if (named_module_p ()
15275 && (mp_state == MP_PURVIEW_IMPORTS
15276 || mp_state == MP_PRIVATE_IMPORTS))
15277 mod = cp_parser_module_partition (parser);
15278 else
15279 error_at (next->location, "import specifying a module-partition"
15280 " must appear after a named module-declaration");
15282 else
15283 mod = cp_parser_module_name (parser);
15284 tree attrs = cp_parser_attributes_opt (parser);
15286 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15287 goto skip_eol;
15288 cp_parser_require_pragma_eol (parser, token);
15290 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15292 /* Module-purview imports must not be from source inclusion
15293 [cpp.import]/7 */
15294 if (attrs
15295 && private_lookup_attribute ("__translated",
15296 strlen ("__translated"), attrs))
15297 error_at (token->location, "post-module-declaration imports"
15298 " must not be include-translated");
15299 else if (!token->main_source_p)
15300 error_at (token->location, "post-module-declaration imports"
15301 " must not be from header inclusion");
15304 import_module (mod, token->location, exporting, attrs, parse_in);
15308 /* export-declaration.
15310 export name-declaration
15311 export { declaration-seq-opt } */
15313 static void
15314 cp_parser_module_export (cp_parser *parser)
15316 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15317 cp_token *token = cp_lexer_consume_token (parser->lexer);
15319 if (!module_interface_p ())
15320 error_at (token->location,
15321 "%qE may only occur after a module interface declaration",
15322 token->u.value);
15324 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15326 unsigned mk = module_kind;
15327 if (module_exporting_p ())
15328 error_at (token->location,
15329 "%qE may only occur once in an export declaration",
15330 token->u.value);
15331 module_kind |= MK_EXPORTING;
15333 if (braced)
15335 cp_ensure_no_omp_declare_simd (parser);
15336 cp_ensure_no_oacc_routine (parser);
15338 cp_lexer_consume_token (parser->lexer);
15339 cp_parser_declaration_seq_opt (parser);
15340 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15342 else
15344 /* Explicitly check if the next tokens might be a
15345 module-directive line, so we can give a clearer error message
15346 about why the directive will be rejected. */
15347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15348 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15349 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15350 error_at (token->location, "%<export%> not part of following"
15351 " module-directive");
15353 bool saved_in_unbraced_export_declaration_p
15354 = parser->in_unbraced_export_declaration_p;
15355 parser->in_unbraced_export_declaration_p = true;
15356 cp_parser_declaration (parser, NULL_TREE);
15357 parser->in_unbraced_export_declaration_p
15358 = saved_in_unbraced_export_declaration_p;
15361 module_kind = mk;
15364 /* Used for maybe_warn_extra_semi. */
15366 enum class extra_semi_kind { decl, member, in_class_fn_def };
15368 /* Warn about an extra semicolon. KIND says in which context the extra
15369 semicolon occurs. */
15371 static void
15372 maybe_warn_extra_semi (location_t loc, extra_semi_kind kind)
15374 /* -Wno-extra-semi suppresses all. */
15375 if (warn_extra_semi == 0)
15376 return;
15378 gcc_rich_location richloc (loc);
15379 richloc.add_fixit_remove ();
15381 switch (kind)
15383 case extra_semi_kind::decl:
15384 /* If -Wextra-semi wasn't specified, warn only when -pedantic is in
15385 effect in C++98. DR 569 says that spurious semicolons at namespace
15386 scope should be allowed. */
15387 if (pedantic && cxx_dialect < cxx11)
15388 pedwarn (&richloc, OPT_Wextra_semi,
15389 "extra %<;%> outside of a function only allowed in C++11");
15390 else if (warn_extra_semi > 0)
15391 warning_at (&richloc, OPT_Wextra_semi,
15392 "extra %<;%> outside of a function");
15393 break;
15395 case extra_semi_kind::member:
15396 /* If -Wextra-semi wasn't specified, warn only when -pedantic is in
15397 effect in C++98. DR 1693 added "empty-declaration" to the syntax for
15398 "member-declaration". */
15399 if (pedantic && cxx_dialect < cxx11)
15400 pedwarn (&richloc, OPT_Wextra_semi,
15401 "extra %<;%> inside a struct only allowed in C++11");
15402 else if (warn_extra_semi > 0)
15403 warning_at (&richloc, OPT_Wextra_semi, "extra %<;%> inside a struct");
15404 break;
15406 case extra_semi_kind::in_class_fn_def:
15407 /* A single semicolon is valid after a member function definition
15408 so this is just a warning. */
15409 if (warn_extra_semi > 0)
15410 warning_at (&richloc, OPT_Wextra_semi,
15411 "extra %<;%> after in-class function definition");
15412 break;
15414 default:
15415 gcc_unreachable ();
15419 /* Declarations [gram.dcl.dcl] */
15421 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15422 is the top-level declaration sequence. That affects whether we
15423 deal with module-preamble.
15425 declaration-seq:
15426 declaration
15427 declaration-seq declaration */
15429 static void
15430 cp_parser_declaration_seq_opt (cp_parser* parser)
15432 bool saved_in_unbraced_linkage_specification_p
15433 = parser->in_unbraced_linkage_specification_p;
15434 bool saved_in_unbraced_export_declaration_p
15435 = parser->in_unbraced_export_declaration_p;
15437 /* We're not in an unbraced linkage-specification
15438 or export-declaration anymore. */
15439 parser->in_unbraced_linkage_specification_p = false;
15440 parser->in_unbraced_export_declaration_p = false;
15442 while (true)
15444 cp_token *token = cp_lexer_peek_token (parser->lexer);
15446 if (token->type == CPP_CLOSE_BRACE
15447 || token->type == CPP_EOF)
15448 break;
15449 else
15450 cp_parser_toplevel_declaration (parser);
15453 parser->in_unbraced_linkage_specification_p
15454 = saved_in_unbraced_linkage_specification_p;
15455 parser->in_unbraced_export_declaration_p
15456 = saved_in_unbraced_export_declaration_p;
15459 /* Parse a declaration. The distinction between name-declaration
15460 and special-declaration is only since C++20.
15462 declaration:
15463 name-declaration
15464 special-declaration
15466 name-declaration:
15467 block-declaration
15468 nodeclspec-function-declaration
15469 function-definition
15470 template-declaration
15471 deduction-guide (C++17)
15472 linkage-specification
15473 namespace-definition
15474 empty-declaration
15475 attribute-declaration
15476 module-import-declaration (modules)
15478 special-declaration:
15479 explicit-instantiation
15480 explicit-specialization
15481 export-declaration (modules)
15483 GNU extension:
15485 declaration:
15486 __extension__ declaration */
15488 static void
15489 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15491 int saved_pedantic;
15493 /* Check for the `__extension__' keyword. */
15494 if (cp_parser_extension_opt (parser, &saved_pedantic))
15496 /* Parse the qualified declaration. */
15497 cp_parser_declaration (parser, prefix_attrs);
15498 /* Restore the PEDANTIC flag. */
15499 pedantic = saved_pedantic;
15501 return;
15504 /* P2615: Determine if we're parsing a name-declaration specifically,
15505 or if special-declarations are OK too. */
15506 bool require_name_decl_p
15507 = (parser->in_unbraced_export_declaration_p
15508 || (parser->in_unbraced_linkage_specification_p
15509 && cxx_dialect >= cxx20));
15511 /* Try to figure out what kind of declaration is present. */
15512 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15513 cp_token *token2 = (token1->type == CPP_EOF
15514 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15516 if (token1->type == CPP_SEMICOLON)
15518 location_t semicolon_loc
15519 = cp_lexer_consume_token (parser->lexer)->location;
15520 /* A declaration consisting of a single semicolon is invalid
15521 before C++11. Allow it unless we're being pedantic. */
15522 maybe_warn_extra_semi (semicolon_loc, extra_semi_kind::decl);
15523 return;
15525 else if (cp_lexer_nth_token_is (parser->lexer,
15526 cp_parser_skip_std_attribute_spec_seq (parser,
15528 CPP_SEMICOLON))
15530 location_t attrs_loc = token1->location;
15531 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15533 if (std_attrs && (flag_openmp || flag_openmp_simd))
15535 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15536 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15537 std_attrs);
15538 if (parser->lexer->in_omp_attribute_pragma)
15540 cp_lexer *lexer = parser->lexer;
15541 while (parser->lexer->in_omp_attribute_pragma)
15543 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15544 CPP_PRAGMA));
15545 cp_parser_pragma (parser, pragma_external, NULL);
15547 cp_lexer_destroy (lexer);
15551 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
15552 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15553 OPT_Wattributes, "attribute ignored");
15554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15555 cp_lexer_consume_token (parser->lexer);
15556 return;
15559 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15560 void *p = obstack_alloc (&declarator_obstack, 0);
15562 tree attributes = NULL_TREE;
15564 /* Conditionally, allow attributes to precede a linkage specification. */
15565 if (token1->keyword == RID_ATTRIBUTE)
15567 cp_lexer_save_tokens (parser->lexer);
15568 attributes = cp_parser_attributes_opt (parser);
15569 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15570 cp_token *t2 = (t1->type == CPP_EOF
15571 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15572 if (t1->keyword == RID_EXTERN
15573 && cp_parser_is_pure_string_literal (t2))
15575 cp_lexer_commit_tokens (parser->lexer);
15576 /* We might have already been here. */
15577 if (!c_dialect_objc ())
15579 location_t where = get_finish (t2->location);
15580 warning_at (token1->location, OPT_Wattributes, "attributes are"
15581 " not permitted in this position");
15582 where = linemap_position_for_loc_and_offset (line_table,
15583 where, 1);
15584 inform (where, "attributes may be inserted here");
15585 attributes = NULL_TREE;
15587 token1 = t1;
15588 token2 = t2;
15590 else
15592 cp_lexer_rollback_tokens (parser->lexer);
15593 attributes = NULL_TREE;
15596 /* If we already had some attributes, and we've added more, then prepend.
15597 Otherwise attributes just contains any that we just read. */
15598 if (prefix_attrs)
15600 if (attributes)
15601 TREE_CHAIN (prefix_attrs) = attributes;
15602 attributes = prefix_attrs;
15605 /* If the next token is `extern' and the following token is a string
15606 literal, then we have a linkage specification. */
15607 if (token1->keyword == RID_EXTERN
15608 && cp_parser_is_pure_string_literal (token2))
15609 cp_parser_linkage_specification (parser, attributes);
15610 /* If the next token is `template', then we have either a template
15611 declaration, an explicit instantiation, or an explicit
15612 specialization. */
15613 else if (token1->keyword == RID_TEMPLATE)
15615 /* `template <>' indicates a template specialization. */
15616 if (token2->type == CPP_LESS
15617 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15619 if (require_name_decl_p)
15621 auto_diagnostic_group d;
15622 cp_token *token3 = cp_lexer_peek_nth_token (parser->lexer, 3);
15623 location_t loc = make_location (token1, token1, token3);
15624 error_at (loc, "explicit specializations are not permitted here");
15625 if (parser->in_unbraced_export_declaration_p)
15626 inform (loc, "a specialization is always exported alongside "
15627 "its primary template");
15629 cp_parser_explicit_specialization (parser);
15631 /* `template <' indicates a template declaration. */
15632 else if (token2->type == CPP_LESS)
15633 cp_parser_template_declaration (parser, /*member_p=*/false);
15634 /* Anything else must be an explicit instantiation. */
15635 else
15637 if (require_name_decl_p)
15638 error_at (token1->location,
15639 "explicit instantiations are not permitted here");
15640 cp_parser_explicit_instantiation (parser);
15643 /* If the next token is `export', it's new-style modules or
15644 old-style template. */
15645 else if (token1->keyword == RID_EXPORT)
15647 if (!modules_p ())
15648 cp_parser_template_declaration (parser, /*member_p=*/false);
15649 else
15651 /* We check for nested exports in cp_parser_module_export. */
15652 if (require_name_decl_p
15653 && !parser->in_unbraced_export_declaration_p)
15654 error_at (token1->location,
15655 "export-declarations are not permitted here");
15656 cp_parser_module_export (parser);
15659 else if (cp_token_is_module_directive (token1))
15661 bool exporting = token1->keyword == RID__EXPORT;
15662 cp_token *next = exporting ? token2 : token1;
15663 if (exporting)
15664 cp_lexer_consume_token (parser->lexer);
15665 // In module purview this will be ill-formed.
15666 auto state = (!named_module_p () ? MP_NOT_MODULE
15667 : module_purview_p () ? MP_PURVIEW
15668 : MP_GLOBAL);
15669 if (next->keyword == RID__MODULE)
15670 cp_parser_module_declaration (parser, state, exporting);
15671 else
15672 cp_parser_import_declaration (parser, state, exporting);
15674 /* If the next token is `extern', 'static' or 'inline' and the one
15675 after that is `template', we have a GNU extended explicit
15676 instantiation directive. */
15677 else if (cp_parser_allow_gnu_extensions_p (parser)
15678 && token2->keyword == RID_TEMPLATE
15679 && (token1->keyword == RID_EXTERN
15680 || token1->keyword == RID_STATIC
15681 || token1->keyword == RID_INLINE))
15682 cp_parser_explicit_instantiation (parser);
15683 /* If the next token is `namespace', check for a named or unnamed
15684 namespace definition. */
15685 else if (token1->keyword == RID_NAMESPACE
15686 && (/* A named namespace definition. */
15687 (token2->type == CPP_NAME
15688 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15689 != CPP_EQ))
15690 || (token2->type == CPP_OPEN_SQUARE
15691 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15692 == CPP_OPEN_SQUARE)
15693 /* An unnamed namespace definition. */
15694 || token2->type == CPP_OPEN_BRACE
15695 || token2->keyword == RID_ATTRIBUTE))
15696 cp_parser_namespace_definition (parser);
15697 /* An inline (associated) namespace definition. */
15698 else if (token2->keyword == RID_NAMESPACE
15699 && token1->keyword == RID_INLINE)
15700 cp_parser_namespace_definition (parser);
15701 /* Objective-C++ declaration/definition. */
15702 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15703 cp_parser_objc_declaration (parser, attributes);
15704 else if (c_dialect_objc ()
15705 && token1->keyword == RID_ATTRIBUTE
15706 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15707 cp_parser_objc_declaration (parser, attributes);
15708 /* At this point we may have a template declared by a concept
15709 introduction. */
15710 else if (flag_concepts
15711 && cp_parser_template_declaration_after_export (parser,
15712 /*member_p=*/false))
15713 /* We did. */;
15714 else
15715 /* Try to parse a block-declaration, or a function-definition. */
15716 cp_parser_block_declaration (parser, /*statement_p=*/false);
15718 /* Free any declarators allocated. */
15719 obstack_free (&declarator_obstack, p);
15722 /* Parse a namespace-scope declaration. */
15724 static void
15725 cp_parser_toplevel_declaration (cp_parser* parser)
15727 cp_token *token = cp_lexer_peek_token (parser->lexer);
15729 if (token->type == CPP_PRAGMA)
15730 /* A top-level declaration can consist solely of a #pragma. A
15731 nested declaration cannot, so this is done here and not in
15732 cp_parser_declaration. (A #pragma at block scope is
15733 handled in cp_parser_statement.) */
15734 cp_parser_pragma (parser, pragma_external, NULL);
15735 else
15736 /* Parse the declaration itself. */
15737 cp_parser_declaration (parser, NULL_TREE);
15740 /* Parse a block-declaration.
15742 block-declaration:
15743 simple-declaration
15744 asm-definition
15745 namespace-alias-definition
15746 using-declaration
15747 using-directive
15749 GNU Extension:
15751 block-declaration:
15752 __extension__ block-declaration
15754 C++0x Extension:
15756 block-declaration:
15757 static_assert-declaration
15759 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15760 part of a declaration-statement. */
15762 static void
15763 cp_parser_block_declaration (cp_parser *parser,
15764 bool statement_p)
15766 int saved_pedantic;
15768 /* Check for the `__extension__' keyword. */
15769 if (cp_parser_extension_opt (parser, &saved_pedantic))
15771 /* Parse the qualified declaration. */
15772 cp_parser_block_declaration (parser, statement_p);
15773 /* Restore the PEDANTIC flag. */
15774 pedantic = saved_pedantic;
15776 return;
15779 /* Peek at the next token to figure out which kind of declaration is
15780 present. */
15781 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15783 /* If the next keyword is `asm', we have an asm-definition. */
15784 if (token1->keyword == RID_ASM)
15786 if (statement_p)
15787 cp_parser_commit_to_tentative_parse (parser);
15788 cp_parser_asm_definition (parser);
15790 /* If the next keyword is `namespace', we have a
15791 namespace-alias-definition. */
15792 else if (token1->keyword == RID_NAMESPACE)
15793 cp_parser_namespace_alias_definition (parser);
15794 /* If the next keyword is `using', we have a
15795 using-declaration, a using-directive, or an alias-declaration. */
15796 else if (token1->keyword == RID_USING)
15798 cp_token *token2;
15800 if (statement_p)
15801 cp_parser_commit_to_tentative_parse (parser);
15802 /* If the token after `using' is `namespace', then we have a
15803 using-directive. */
15804 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15805 if (token2->keyword == RID_NAMESPACE)
15806 cp_parser_using_directive (parser);
15807 else if (token2->keyword == RID_ENUM)
15808 cp_parser_using_enum (parser);
15809 /* If the second token after 'using' is '=', then we have an
15810 alias-declaration. */
15811 else if (cxx_dialect >= cxx11
15812 && token2->type == CPP_NAME
15813 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15814 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15815 cp_parser_alias_declaration (parser);
15816 /* Otherwise, it's a using-declaration. */
15817 else
15818 cp_parser_using_declaration (parser,
15819 /*access_declaration_p=*/false);
15821 /* If the next keyword is `__label__' we have a misplaced label
15822 declaration. */
15823 else if (token1->keyword == RID_LABEL)
15825 cp_lexer_consume_token (parser->lexer);
15826 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15827 cp_parser_skip_to_end_of_statement (parser);
15828 /* If the next token is now a `;', consume it. */
15829 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15830 cp_lexer_consume_token (parser->lexer);
15832 /* If the next token is `static_assert' we have a static assertion. */
15833 else if (token1->keyword == RID_STATIC_ASSERT)
15834 cp_parser_static_assert (parser, /*member_p=*/false);
15835 else
15837 size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1);
15838 cp_token *after_attr = NULL;
15839 if (attr_idx != 1)
15840 after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx);
15841 /* If the next tokens after attributes is `using namespace', then we have
15842 a using-directive. */
15843 if (after_attr
15844 && after_attr->keyword == RID_USING
15845 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15846 RID_NAMESPACE))
15848 if (statement_p)
15849 cp_parser_commit_to_tentative_parse (parser);
15850 cp_parser_using_directive (parser);
15852 /* If the next token after attributes is `asm', then we have
15853 an asm-definition. */
15854 else if (after_attr && after_attr->keyword == RID_ASM)
15856 if (statement_p)
15857 cp_parser_commit_to_tentative_parse (parser);
15858 cp_parser_asm_definition (parser);
15860 /* Anything else must be a simple-declaration. */
15861 else
15862 cp_parser_simple_declaration (parser, !statement_p,
15863 /*maybe_range_for_decl*/NULL);
15867 /* Parse a simple-declaration.
15869 simple-declaration:
15870 decl-specifier-seq [opt] init-declarator-list [opt] ;
15871 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15872 brace-or-equal-initializer ;
15874 init-declarator-list:
15875 init-declarator
15876 init-declarator-list , init-declarator
15878 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15879 function-definition as a simple-declaration.
15881 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15882 parsed declaration if it is an uninitialized single declarator not followed
15883 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15884 if present, will not be consumed. */
15886 static void
15887 cp_parser_simple_declaration (cp_parser* parser,
15888 bool function_definition_allowed_p,
15889 tree *maybe_range_for_decl)
15891 cp_decl_specifier_seq decl_specifiers;
15892 int declares_class_or_enum;
15893 bool saw_declarator;
15894 location_t comma_loc = UNKNOWN_LOCATION;
15895 location_t init_loc = UNKNOWN_LOCATION;
15897 if (maybe_range_for_decl)
15898 *maybe_range_for_decl = NULL_TREE;
15900 /* Defer access checks until we know what is being declared; the
15901 checks for names appearing in the decl-specifier-seq should be
15902 done as if we were in the scope of the thing being declared. */
15903 push_deferring_access_checks (dk_deferred);
15905 /* Parse the decl-specifier-seq. We have to keep track of whether
15906 or not the decl-specifier-seq declares a named class or
15907 enumeration type, since that is the only case in which the
15908 init-declarator-list is allowed to be empty.
15910 [dcl.dcl]
15912 In a simple-declaration, the optional init-declarator-list can be
15913 omitted only when declaring a class or enumeration, that is when
15914 the decl-specifier-seq contains either a class-specifier, an
15915 elaborated-type-specifier, or an enum-specifier. */
15916 cp_parser_decl_specifier_seq (parser,
15917 CP_PARSER_FLAGS_OPTIONAL,
15918 &decl_specifiers,
15919 &declares_class_or_enum);
15920 /* We no longer need to defer access checks. */
15921 stop_deferring_access_checks ();
15923 cp_omp_declare_simd_data odsd;
15924 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15925 cp_parser_handle_directive_omp_attributes (parser,
15926 &decl_specifiers.attributes,
15927 &odsd, true);
15929 /* In a block scope, a valid declaration must always have a
15930 decl-specifier-seq. By not trying to parse declarators, we can
15931 resolve the declaration/expression ambiguity more quickly. */
15932 if (!function_definition_allowed_p
15933 && !decl_specifiers.any_specifiers_p)
15935 cp_parser_error (parser, "expected declaration");
15936 goto done;
15939 /* If the next two tokens are both identifiers, the code is
15940 erroneous. The usual cause of this situation is code like:
15942 T t;
15944 where "T" should name a type -- but does not. */
15945 if (!decl_specifiers.any_type_specifiers_p
15946 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15948 /* If parsing tentatively, we should commit; we really are
15949 looking at a declaration. */
15950 cp_parser_commit_to_tentative_parse (parser);
15951 /* Give up. */
15952 goto done;
15955 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15957 /* Look for C++17 decomposition declaration. */
15958 for (size_t n = 1; ; n++)
15959 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15960 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15961 continue;
15962 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15963 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15964 && decl_specifiers.any_specifiers_p)
15966 tree decl
15967 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15968 maybe_range_for_decl,
15969 &init_loc);
15971 /* The next token should be either a `,' or a `;'. */
15972 cp_token *token = cp_lexer_peek_token (parser->lexer);
15973 /* If it's a `;', we are done. */
15974 if (token->type == CPP_SEMICOLON)
15975 goto finish;
15976 else if (maybe_range_for_decl)
15978 if (*maybe_range_for_decl == NULL_TREE)
15979 *maybe_range_for_decl = error_mark_node;
15980 goto finish;
15982 /* Anything else is an error. */
15983 else
15985 /* If we have already issued an error message we don't need
15986 to issue another one. */
15987 if ((decl != error_mark_node
15988 && DECL_INITIAL (decl) != error_mark_node)
15989 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15990 cp_parser_error (parser, "expected %<;%>");
15991 /* Skip tokens until we reach the end of the statement. */
15992 cp_parser_skip_to_end_of_statement (parser);
15993 /* If the next token is now a `;', consume it. */
15994 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15995 cp_lexer_consume_token (parser->lexer);
15996 goto done;
15999 else
16000 break;
16002 tree last_type;
16003 bool auto_specifier_p;
16004 /* NULL_TREE if both variable and function declaration are allowed,
16005 error_mark_node if function declaration are not allowed and
16006 a FUNCTION_DECL that should be diagnosed if it is followed by
16007 variable declarations. */
16008 tree auto_function_declaration;
16010 last_type = NULL_TREE;
16011 auto_specifier_p
16012 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
16013 auto_function_declaration = NULL_TREE;
16015 /* Keep going until we hit the `;' at the end of the simple
16016 declaration. */
16017 saw_declarator = false;
16018 while (cp_lexer_next_token_is_not (parser->lexer,
16019 CPP_SEMICOLON))
16021 cp_token *token;
16022 bool function_definition_p;
16023 tree decl;
16024 tree auto_result = NULL_TREE;
16026 if (saw_declarator)
16028 /* If we are processing next declarator, comma is expected */
16029 token = cp_lexer_peek_token (parser->lexer);
16030 gcc_assert (token->type == CPP_COMMA);
16031 cp_lexer_consume_token (parser->lexer);
16032 if (maybe_range_for_decl)
16034 *maybe_range_for_decl = error_mark_node;
16035 if (comma_loc == UNKNOWN_LOCATION)
16036 comma_loc = token->location;
16039 else
16040 saw_declarator = true;
16042 /* Parse the init-declarator. */
16043 decl = cp_parser_init_declarator (parser,
16044 CP_PARSER_FLAGS_NONE,
16045 &decl_specifiers,
16046 /*checks=*/NULL,
16047 function_definition_allowed_p,
16048 /*member_p=*/false,
16049 declares_class_or_enum,
16050 &function_definition_p,
16051 maybe_range_for_decl,
16052 &init_loc,
16053 &auto_result);
16054 const bool fndecl_p = TREE_CODE (decl) == FUNCTION_DECL;
16055 /* If an error occurred while parsing tentatively, exit quickly.
16056 (That usually happens when in the body of a function; each
16057 statement is treated as a declaration-statement until proven
16058 otherwise.) */
16059 if (cp_parser_error_occurred (parser))
16060 goto done;
16062 if (auto_specifier_p && cxx_dialect >= cxx14)
16064 /* If the init-declarator-list contains more than one
16065 init-declarator, they shall all form declarations of
16066 variables. */
16067 if (auto_function_declaration == NULL_TREE)
16068 auto_function_declaration = fndecl_p ? decl : error_mark_node;
16069 else if (fndecl_p || auto_function_declaration != error_mark_node)
16071 error_at (decl_specifiers.locations[ds_type_spec],
16072 "non-variable %qD in declaration with more than one "
16073 "declarator with placeholder type",
16074 fndecl_p ? decl : auto_function_declaration);
16075 auto_function_declaration = error_mark_node;
16079 if (auto_result
16080 && (!processing_template_decl || !type_uses_auto (auto_result)))
16082 if (last_type
16083 && last_type != error_mark_node
16084 && !same_type_p (auto_result, last_type))
16086 /* If the list of declarators contains more than one declarator,
16087 the type of each declared variable is determined as described
16088 above. If the type deduced for the template parameter U is not
16089 the same in each deduction, the program is ill-formed. */
16090 error_at (decl_specifiers.locations[ds_type_spec],
16091 "inconsistent deduction for %qT: %qT and then %qT",
16092 decl_specifiers.type, last_type, auto_result);
16093 last_type = error_mark_node;
16095 else
16096 last_type = auto_result;
16099 /* Handle function definitions specially. */
16100 if (function_definition_p)
16102 /* If the next token is a `,', then we are probably
16103 processing something like:
16105 void f() {}, *p;
16107 which is erroneous. */
16108 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16110 cp_token *token = cp_lexer_peek_token (parser->lexer);
16111 error_at (token->location,
16112 "mixing"
16113 " declarations and function-definitions is forbidden");
16115 /* Otherwise, we're done with the list of declarators. */
16116 else
16118 pop_deferring_access_checks ();
16119 cp_finalize_omp_declare_simd (parser, &odsd);
16120 return;
16123 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
16124 *maybe_range_for_decl = decl;
16125 /* The next token should be either a `,' or a `;'. */
16126 token = cp_lexer_peek_token (parser->lexer);
16127 /* If it's a `,', there are more declarators to come. */
16128 if (token->type == CPP_COMMA)
16129 /* will be consumed next time around */;
16130 /* If it's a `;', we are done. */
16131 else if (token->type == CPP_SEMICOLON)
16132 break;
16133 else if (maybe_range_for_decl)
16135 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
16136 permerror (decl_specifiers.locations[ds_type_spec],
16137 "types may not be defined in a for-range-declaration");
16138 break;
16140 /* Anything else is an error. */
16141 else
16143 /* If we have already issued an error message we don't need
16144 to issue another one. */
16145 if ((decl != error_mark_node
16146 /* grokfndecl sets DECL_INITIAL to error_mark_node for
16147 functions. */
16148 && (fndecl_p || DECL_INITIAL (decl) != error_mark_node))
16149 || cp_parser_uncommitted_to_tentative_parse_p (parser))
16150 cp_parser_error (parser, "expected %<,%> or %<;%>");
16151 /* Skip tokens until we reach the end of the statement. */
16152 cp_parser_skip_to_end_of_statement (parser);
16153 /* If the next token is now a `;', consume it. */
16154 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16155 cp_lexer_consume_token (parser->lexer);
16156 goto done;
16158 /* After the first time around, a function-definition is not
16159 allowed -- even if it was OK at first. For example:
16161 int i, f() {}
16163 is not valid. */
16164 function_definition_allowed_p = false;
16167 /* Issue an error message if no declarators are present, and the
16168 decl-specifier-seq does not itself declare a class or
16169 enumeration: [dcl.dcl]/3. */
16170 if (!saw_declarator)
16172 if (cp_parser_declares_only_class_p (parser))
16174 if (!declares_class_or_enum
16175 && decl_specifiers.type
16176 && OVERLOAD_TYPE_P (decl_specifiers.type))
16177 /* Ensure an error is issued anyway when finish_decltype_type,
16178 called via cp_parser_decl_specifier_seq, returns a class or
16179 an enumeration (c++/51786). */
16180 decl_specifiers.type = NULL_TREE;
16181 shadow_tag (&decl_specifiers);
16183 /* Perform any deferred access checks. */
16184 perform_deferred_access_checks (tf_warning_or_error);
16187 /* Consume the `;'. */
16188 finish:
16189 if (!maybe_range_for_decl)
16190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16191 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16193 if (init_loc != UNKNOWN_LOCATION)
16194 error_at (init_loc, "initializer in range-based %<for%> loop");
16195 if (comma_loc != UNKNOWN_LOCATION)
16196 error_at (comma_loc,
16197 "multiple declarations in range-based %<for%> loop");
16200 done:
16201 pop_deferring_access_checks ();
16202 cp_finalize_omp_declare_simd (parser, &odsd);
16205 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
16206 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
16207 initializer ; */
16209 static tree
16210 cp_parser_decomposition_declaration (cp_parser *parser,
16211 cp_decl_specifier_seq *decl_specifiers,
16212 tree *maybe_range_for_decl,
16213 location_t *init_loc)
16215 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
16216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16217 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
16219 /* Parse the identifier-list. */
16220 auto_vec<cp_expr, 10> v;
16221 bool attr_diagnosed = false;
16222 int first_attr = -1;
16223 unsigned int cnt = 0;
16224 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16225 while (true)
16227 cp_expr e = cp_parser_identifier (parser);
16228 if (e.get_value () == error_mark_node)
16229 break;
16230 tree attr = NULL_TREE;
16231 if (cp_next_tokens_can_be_std_attribute_p (parser))
16233 if (cxx_dialect >= cxx17 && cxx_dialect < cxx26 && !attr_diagnosed)
16235 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
16236 OPT_Wc__26_extensions,
16237 "structured bindings with attributed identifiers "
16238 "only available with %<-std=c++2c%> or "
16239 "%<-std=gnu++2c%>");
16240 attr_diagnosed = true;
16242 attr = cp_parser_std_attribute_spec_seq (parser);
16243 if (attr == error_mark_node)
16244 attr = NULL_TREE;
16245 if (attr && first_attr == -1)
16246 first_attr = v.length ();
16248 v.safe_push (e);
16249 ++cnt;
16250 if (first_attr != -1)
16251 v.safe_push (attr);
16252 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16253 break;
16254 cp_lexer_consume_token (parser->lexer);
16257 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
16258 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16260 end_loc = UNKNOWN_LOCATION;
16261 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
16262 false);
16263 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16264 cp_lexer_consume_token (parser->lexer);
16265 else
16267 cp_parser_skip_to_end_of_statement (parser);
16268 return error_mark_node;
16272 if (cxx_dialect < cxx17)
16273 pedwarn (loc, OPT_Wc__17_extensions,
16274 "structured bindings only available with "
16275 "%<-std=c++17%> or %<-std=gnu++17%>");
16277 tree pushed_scope;
16278 cp_declarator *declarator = make_declarator (cdk_decomp);
16279 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
16280 declarator->id_loc = loc;
16281 if (ref_qual != REF_QUAL_NONE)
16282 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
16283 ref_qual == REF_QUAL_RVALUE,
16284 NULL_TREE);
16285 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
16286 NULL_TREE, decl_specifiers->attributes,
16287 &pushed_scope);
16288 tree orig_decl = decl;
16290 unsigned int i;
16291 cp_expr e;
16292 cp_decl_specifier_seq decl_specs;
16293 clear_decl_specs (&decl_specs);
16294 decl_specs.type = make_auto ();
16295 if (decl_specifiers->storage_class == sc_static)
16296 decl_specs.storage_class = sc_static;
16297 tree prev = decl;
16298 FOR_EACH_VEC_ELT (v, i, e)
16300 if (i == 0)
16301 declarator = make_id_declarator (NULL_TREE, e.get_value (),
16302 sfk_none, e.get_location ());
16303 else
16305 declarator->u.id.unqualified_name = e.get_value ();
16306 declarator->id_loc = e.get_location ();
16308 tree elt_pushed_scope;
16309 tree attr = NULL_TREE;
16310 if (first_attr != -1 && i >= (unsigned) first_attr)
16311 attr = v[++i].get_value ();
16312 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
16313 NULL_TREE, attr, &elt_pushed_scope);
16314 if (decl2 == error_mark_node)
16315 decl = error_mark_node;
16316 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
16318 /* Ensure we've diagnosed redeclaration if we aren't creating
16319 a new VAR_DECL. */
16320 gcc_assert (errorcount);
16321 decl = error_mark_node;
16323 else
16324 prev = decl2;
16325 if (elt_pushed_scope)
16326 pop_scope (elt_pushed_scope);
16329 if (v.is_empty ())
16331 error_at (loc, "empty structured binding declaration");
16332 decl = error_mark_node;
16335 if (maybe_range_for_decl == NULL
16336 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16338 bool non_constant_p = false, is_direct_init = false;
16339 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
16340 tree initializer = cp_parser_initializer (parser, &is_direct_init,
16341 &non_constant_p);
16342 if (initializer == NULL_TREE
16343 || (TREE_CODE (initializer) == TREE_LIST
16344 && TREE_CHAIN (initializer))
16345 || (is_direct_init
16346 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
16347 && CONSTRUCTOR_NELTS (initializer) != 1))
16349 error_at (loc, "invalid initializer for structured binding "
16350 "declaration");
16351 initializer = error_mark_node;
16354 if (decl != error_mark_node)
16356 cp_decomp decomp = { prev, cnt };
16357 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
16358 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
16359 &decomp);
16360 cp_finish_decomp (decl, &decomp);
16363 else if (decl != error_mark_node)
16365 *maybe_range_for_decl = prev;
16366 cp_decomp decomp = { prev, cnt };
16367 /* Ensure DECL_VALUE_EXPR is created for all the decls but
16368 the underlying DECL. */
16369 cp_finish_decomp (decl, &decomp);
16372 if (pushed_scope)
16373 pop_scope (pushed_scope);
16375 if (decl == error_mark_node && DECL_P (orig_decl))
16377 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
16378 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
16381 return decl;
16384 /* Names of storage classes. */
16386 static const char *const
16387 cp_storage_class_name[] = {
16388 "", "auto", "register", "static", "extern", "mutable"
16391 /* Parse a decl-specifier-seq.
16393 decl-specifier-seq:
16394 decl-specifier-seq [opt] decl-specifier
16395 decl-specifier attribute-specifier-seq [opt] (C++11)
16397 decl-specifier:
16398 storage-class-specifier
16399 type-specifier
16400 function-specifier
16401 friend
16402 typedef
16404 GNU Extension:
16406 decl-specifier:
16407 attributes
16409 Concepts Extension:
16411 decl-specifier:
16412 concept
16414 Set *DECL_SPECS to a representation of the decl-specifier-seq.
16416 The parser flags FLAGS is used to control type-specifier parsing.
16418 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16419 flags:
16421 1: one of the decl-specifiers is an elaborated-type-specifier
16422 (i.e., a type declaration)
16423 2: one of the decl-specifiers is an enum-specifier or a
16424 class-specifier (i.e., a type definition)
16428 static void
16429 cp_parser_decl_specifier_seq (cp_parser* parser,
16430 cp_parser_flags flags,
16431 cp_decl_specifier_seq *decl_specs,
16432 int* declares_class_or_enum)
16434 bool constructor_possible_p = !parser->in_declarator_p;
16435 bool found_decl_spec = false;
16436 cp_token *start_token = NULL;
16437 cp_decl_spec ds;
16439 /* Clear DECL_SPECS. */
16440 clear_decl_specs (decl_specs);
16442 /* Assume no class or enumeration type is declared. */
16443 *declares_class_or_enum = 0;
16445 /* Keep a token that additionally will be used for diagnostics. */
16446 cp_token *first_specifier = NULL;
16447 /* Keep reading specifiers until there are no more to read. */
16448 while (true)
16450 bool constructor_p;
16451 cp_token *token;
16452 ds = ds_last;
16454 /* Peek at the next token. */
16455 token = cp_lexer_peek_token (parser->lexer);
16457 /* Save the first token of the decl spec list for error
16458 reporting. */
16459 if (!start_token)
16460 start_token = token;
16461 /* Handle attributes. */
16462 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16463 && cp_next_tokens_can_be_attribute_p (parser))
16465 /* Parse the attributes. */
16466 tree attrs = cp_parser_attributes_opt (parser);
16468 /* In a sequence of declaration specifiers, c++11 attributes
16469 appertain to the type that precede them. In that case
16470 [dcl.spec]/1 says:
16472 The attribute-specifier-seq affects the type only for
16473 the declaration it appears in, not other declarations
16474 involving the same type.
16476 But for now let's force the user to position the
16477 attribute either at the beginning of the declaration or
16478 after the declarator-id, which would clearly mean that it
16479 applies to the declarator. */
16480 if (cxx11_attribute_p (attrs))
16482 if (!found_decl_spec)
16483 /* The c++11 attribute is at the beginning of the
16484 declaration. It appertains to the entity being
16485 declared. */;
16486 else
16488 if (find_contract (attrs))
16490 diagnose_misapplied_contracts (attrs);
16491 attrs = NULL_TREE;
16493 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16495 /* This is an attribute following a
16496 class-specifier. */
16497 if (decl_specs->type_definition_p)
16498 warn_misplaced_attr_for_class_type (token->location,
16499 decl_specs->type);
16500 attrs = NULL_TREE;
16502 else
16504 decl_specs->std_attributes
16505 = attr_chainon (decl_specs->std_attributes, attrs);
16506 if (decl_specs->locations[ds_std_attribute] == 0)
16507 decl_specs->locations[ds_std_attribute] = token->location;
16509 continue;
16513 decl_specs->attributes
16514 = attr_chainon (decl_specs->attributes, attrs);
16515 if (decl_specs->locations[ds_attribute] == 0)
16516 decl_specs->locations[ds_attribute] = token->location;
16517 continue;
16519 /* We know by this point that the token is not part of an attribute. */
16520 if (!first_specifier)
16521 first_specifier = token;
16522 /* Special case for "this" specifier, indicating a parm is an xobj parm.
16523 The "this" specifier must be the first specifier in the declaration,
16524 after any attributes. */
16525 if (token->keyword == RID_THIS && (flags & CP_PARSER_FLAGS_PARAMETER))
16527 cp_lexer_consume_token (parser->lexer);
16528 if (token != first_specifier)
16530 /* Don't emit diagnostics if we have already seen "this",
16531 leave it for set_and_check_decl_spec_loc. */
16532 if (decl_specs->locations[ds_this] == 0)
16534 auto_diagnostic_group d;
16535 gcc_rich_location richloc (token->location);
16536 /* Works, need to add tests for it though. */
16537 richloc.add_fixit_remove ();
16538 richloc.add_fixit_insert_before (first_specifier->location,
16539 "this ");
16540 error_at (&richloc,
16541 "%<this%> must be the first specifier "
16542 "in a parameter declaration");
16545 set_and_check_decl_spec_loc (decl_specs, ds_this, token);
16546 continue;
16549 /* Assume we will find a decl-specifier keyword. */
16550 found_decl_spec = true;
16551 /* If the next token is an appropriate keyword, we can simply
16552 add it to the list. */
16553 switch (token->keyword)
16555 /* decl-specifier:
16556 friend
16557 constexpr
16558 constinit */
16559 case RID_FRIEND:
16560 if (!at_class_scope_p ())
16562 gcc_rich_location richloc (token->location);
16563 richloc.add_fixit_remove ();
16564 error_at (&richloc, "%<friend%> used outside of class");
16565 cp_lexer_purge_token (parser->lexer);
16567 else
16569 ds = ds_friend;
16570 /* Consume the token. */
16571 cp_lexer_consume_token (parser->lexer);
16573 break;
16575 case RID_CONSTEXPR:
16576 ds = ds_constexpr;
16577 cp_lexer_consume_token (parser->lexer);
16578 break;
16580 case RID_CONSTINIT:
16581 ds = ds_constinit;
16582 cp_lexer_consume_token (parser->lexer);
16583 break;
16585 case RID_CONSTEVAL:
16586 ds = ds_consteval;
16587 cp_lexer_consume_token (parser->lexer);
16588 break;
16590 case RID_CONCEPT:
16591 ds = ds_concept;
16592 cp_lexer_consume_token (parser->lexer);
16594 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16595 break;
16597 /* Warn for concept as a decl-specifier. We'll rewrite these as
16598 concept declarations later. */
16599 if (!flag_concepts_ts)
16601 cp_token *next = cp_lexer_peek_token (parser->lexer);
16602 if (next->keyword == RID_BOOL)
16603 permerror (next->location, "the %<bool%> keyword is not "
16604 "allowed in a C++20 concept definition");
16605 else
16606 error_at (token->location, "C++20 concept definition syntax "
16607 "is %<concept <name> = <expr>%>");
16610 /* In C++20 a concept definition is just 'concept name = expr;'
16611 Support that syntax as a TS extension by pretending we've seen
16612 the 'bool' specifier. */
16613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16614 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16615 && !decl_specs->any_type_specifiers_p)
16617 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16618 token, /*type_definition*/false);
16619 decl_specs->any_type_specifiers_p = true;
16621 break;
16623 /* function-specifier:
16624 inline
16625 virtual
16626 explicit */
16627 case RID_INLINE:
16628 case RID_VIRTUAL:
16629 case RID_EXPLICIT:
16630 cp_parser_function_specifier_opt (parser, decl_specs);
16631 break;
16633 /* decl-specifier:
16634 typedef */
16635 case RID_TYPEDEF:
16636 ds = ds_typedef;
16637 /* Consume the token. */
16638 cp_lexer_consume_token (parser->lexer);
16640 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16641 break;
16643 /* A constructor declarator cannot appear in a typedef. */
16644 constructor_possible_p = false;
16645 /* The "typedef" keyword can only occur in a declaration; we
16646 may as well commit at this point. */
16647 cp_parser_commit_to_tentative_parse (parser);
16649 if (decl_specs->storage_class != sc_none)
16651 if (decl_specs->conflicting_specifiers_p)
16652 break;
16653 gcc_rich_location richloc (token->location);
16654 location_t oloc = decl_specs->locations[ds_storage_class];
16655 richloc.add_location_if_nearby (*global_dc, oloc);
16656 error_at (&richloc,
16657 "%<typedef%> specifier conflicts with %qs",
16658 cp_storage_class_name[decl_specs->storage_class]);
16659 decl_specs->conflicting_specifiers_p = true;
16661 break;
16663 /* storage-class-specifier:
16664 auto
16665 register
16666 static
16667 extern
16668 mutable
16670 GNU Extension:
16671 thread */
16672 case RID_AUTO:
16673 if (cxx_dialect == cxx98)
16675 /* Consume the token. */
16676 cp_lexer_consume_token (parser->lexer);
16678 /* Complain about `auto' as a storage specifier, if
16679 we're complaining about C++0x compatibility. */
16680 gcc_rich_location richloc (token->location);
16681 richloc.add_fixit_remove ();
16682 warning_at (&richloc, OPT_Wc__11_compat,
16683 "%<auto%> changes meaning in C++11; "
16684 "please remove it");
16686 /* Set the storage class anyway. */
16687 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16688 token);
16690 else
16691 /* C++0x auto type-specifier. */
16692 found_decl_spec = false;
16693 break;
16695 case RID_REGISTER:
16696 case RID_STATIC:
16697 case RID_EXTERN:
16698 case RID_MUTABLE:
16699 /* Consume the token. */
16700 cp_lexer_consume_token (parser->lexer);
16701 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16702 token);
16703 break;
16704 case RID_THREAD:
16705 /* Consume the token. */
16706 ds = ds_thread;
16707 cp_lexer_consume_token (parser->lexer);
16708 break;
16710 default:
16711 /* We did not yet find a decl-specifier yet. */
16712 found_decl_spec = false;
16713 break;
16716 if (found_decl_spec
16717 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16718 && token->keyword != RID_CONSTEXPR)
16719 error ("%qD invalid in condition", ridpointers[token->keyword]);
16721 if (found_decl_spec
16722 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16723 && token->keyword != RID_MUTABLE
16724 && token->keyword != RID_CONSTEXPR
16725 && token->keyword != RID_CONSTEVAL)
16727 if (token->keyword != RID_STATIC)
16728 error_at (token->location, "%qD invalid in lambda",
16729 ridpointers[token->keyword]);
16730 else if (cxx_dialect < cxx23)
16731 pedwarn (token->location, OPT_Wc__23_extensions,
16732 "%qD only valid in lambda with %<-std=c++23%> or "
16733 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16736 if (ds != ds_last)
16737 set_and_check_decl_spec_loc (decl_specs, ds, token);
16739 /* Constructors are a special case. The `S' in `S()' is not a
16740 decl-specifier; it is the beginning of the declarator. */
16741 constructor_p
16742 = (!found_decl_spec
16743 && constructor_possible_p
16744 && (cp_parser_constructor_declarator_p
16745 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16746 ds_friend))));
16748 /* If we don't have a DECL_SPEC yet, then we must be looking at
16749 a type-specifier. */
16750 if (!found_decl_spec && !constructor_p)
16752 int decl_spec_declares_class_or_enum;
16753 bool is_cv_qualifier;
16754 tree type_spec;
16756 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16757 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16759 type_spec
16760 = cp_parser_type_specifier (parser, flags,
16761 decl_specs,
16762 /*is_declaration=*/true,
16763 &decl_spec_declares_class_or_enum,
16764 &is_cv_qualifier);
16765 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16767 /* If this type-specifier referenced a user-defined type
16768 (a typedef, class-name, etc.), then we can't allow any
16769 more such type-specifiers henceforth.
16771 [dcl.spec]
16773 The longest sequence of decl-specifiers that could
16774 possibly be a type name is taken as the
16775 decl-specifier-seq of a declaration. The sequence shall
16776 be self-consistent as described below.
16778 [dcl.type]
16780 As a general rule, at most one type-specifier is allowed
16781 in the complete decl-specifier-seq of a declaration. The
16782 only exceptions are the following:
16784 -- const or volatile can be combined with any other
16785 type-specifier.
16787 -- signed or unsigned can be combined with char, long,
16788 short, or int.
16790 -- ..
16792 Example:
16794 typedef char* Pc;
16795 void g (const int Pc);
16797 Here, Pc is *not* part of the decl-specifier seq; it's
16798 the declarator. Therefore, once we see a type-specifier
16799 (other than a cv-qualifier), we forbid any additional
16800 user-defined types. We *do* still allow things like `int
16801 int' to be considered a decl-specifier-seq, and issue the
16802 error message later. */
16803 if (type_spec && !is_cv_qualifier)
16804 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16805 /* A constructor declarator cannot follow a type-specifier. */
16806 if (type_spec)
16808 constructor_possible_p = false;
16809 found_decl_spec = true;
16810 if (!is_cv_qualifier)
16811 decl_specs->any_type_specifiers_p = true;
16813 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16814 error_at (token->location, "type-specifier invalid in lambda");
16818 /* If we still do not have a DECL_SPEC, then there are no more
16819 decl-specifiers. */
16820 if (!found_decl_spec)
16821 break;
16823 if (decl_specs->std_attributes)
16825 error_at (decl_specs->locations[ds_std_attribute],
16826 "standard attributes in middle of decl-specifiers");
16827 inform (decl_specs->locations[ds_std_attribute],
16828 "standard attributes must precede the decl-specifiers to "
16829 "apply to the declaration, or follow them to apply to "
16830 "the type");
16833 decl_specs->any_specifiers_p = true;
16834 /* After we see one decl-specifier, further decl-specifiers are
16835 always optional. */
16836 flags |= CP_PARSER_FLAGS_OPTIONAL;
16839 /* Don't allow a friend specifier with a class definition. */
16840 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16841 && (*declares_class_or_enum & 2))
16842 error_at (decl_specs->locations[ds_friend],
16843 "class definition may not be declared a friend");
16846 /* Parse an (optional) storage-class-specifier.
16848 storage-class-specifier:
16849 auto
16850 register
16851 static
16852 extern
16853 mutable
16855 GNU Extension:
16857 storage-class-specifier:
16858 thread
16860 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16862 static tree
16863 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16865 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16867 case RID_AUTO:
16868 if (cxx_dialect != cxx98)
16869 return NULL_TREE;
16870 /* Fall through for C++98. */
16871 gcc_fallthrough ();
16873 case RID_REGISTER:
16874 case RID_STATIC:
16875 case RID_EXTERN:
16876 case RID_MUTABLE:
16877 case RID_THREAD:
16878 /* Consume the token. */
16879 return cp_lexer_consume_token (parser->lexer)->u.value;
16881 default:
16882 return NULL_TREE;
16886 /* Parse an (optional) function-specifier.
16888 function-specifier:
16889 inline
16890 virtual
16891 explicit
16893 C++20 Extension:
16894 explicit(constant-expression)
16896 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16897 Updates DECL_SPECS, if it is non-NULL. */
16899 static tree
16900 cp_parser_function_specifier_opt (cp_parser* parser,
16901 cp_decl_specifier_seq *decl_specs)
16903 cp_token *token = cp_lexer_peek_token (parser->lexer);
16904 switch (token->keyword)
16906 case RID_INLINE:
16907 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16908 break;
16910 case RID_VIRTUAL:
16911 /* 14.5.2.3 [temp.mem]
16913 A member function template shall not be virtual. */
16914 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16915 && current_class_type)
16916 error_at (token->location, "templates may not be %<virtual%>");
16917 else
16918 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16919 break;
16921 case RID_EXPLICIT:
16923 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16924 /* If we see '(', it's C++20 explicit(bool). */
16925 tree expr;
16926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16928 matching_parens parens;
16929 parens.consume_open (parser);
16931 /* New types are not allowed in an explicit-specifier. */
16932 const char *saved_message
16933 = parser->type_definition_forbidden_message;
16934 parser->type_definition_forbidden_message
16935 = G_("types may not be defined in explicit-specifier");
16937 if (cxx_dialect < cxx20)
16938 pedwarn (token->location, OPT_Wc__20_extensions,
16939 "%<explicit(bool)%> only available with %<-std=c++20%> "
16940 "or %<-std=gnu++20%>");
16942 /* Parse the constant-expression. */
16943 expr = cp_parser_constant_expression (parser);
16945 /* Restore the saved message. */
16946 parser->type_definition_forbidden_message = saved_message;
16947 parens.require_close (parser);
16949 else
16950 /* The explicit-specifier explicit without a constant-expression is
16951 equivalent to the explicit-specifier explicit(true). */
16952 expr = boolean_true_node;
16954 /* [dcl.fct.spec]
16955 "the constant-expression, if supplied, shall be a contextually
16956 converted constant expression of type bool." */
16957 expr = build_explicit_specifier (expr, tf_warning_or_error);
16958 /* We could evaluate it -- mark the decl as appropriate. */
16959 if (expr == boolean_true_node)
16960 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16961 else if (expr == boolean_false_node)
16962 /* Don't mark the decl as explicit. */;
16963 else if (decl_specs)
16964 /* The expression was value-dependent. Remember it so that we can
16965 substitute it later. */
16966 decl_specs->explicit_specifier = expr;
16967 return id;
16970 default:
16971 return NULL_TREE;
16974 /* Consume the token. */
16975 return cp_lexer_consume_token (parser->lexer)->u.value;
16978 /* Parse a linkage-specification.
16980 linkage-specification:
16981 extern string-literal { declaration-seq [opt] }
16982 extern string-literal name-declaration */
16984 static void
16985 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16987 /* Look for the `extern' keyword. */
16988 cp_token *extern_token
16989 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16991 /* Look for the string-literal. */
16992 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16993 tree linkage;
16994 if (cxx_dialect >= cxx26)
16995 linkage = cp_parser_unevaluated_string_literal (parser);
16996 else
16997 linkage = cp_parser_string_literal (parser, /*translate=*/false,
16998 /*wide_ok=*/false);
17000 /* Transform the literal into an identifier. If the literal is a
17001 wide-character string, or contains embedded NULs, then we can't
17002 handle it as the user wants. */
17003 if (linkage == error_mark_node
17004 || strlen (TREE_STRING_POINTER (linkage))
17005 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
17007 cp_parser_error (parser, "invalid linkage-specification");
17008 /* Assume C++ linkage. */
17009 linkage = lang_name_cplusplus;
17011 else
17012 linkage = get_identifier (TREE_STRING_POINTER (linkage));
17014 /* We're now using the new linkage. */
17015 unsigned saved_module = module_kind;
17016 module_kind &= ~MK_ATTACH;
17017 push_lang_context (linkage);
17019 /* Preserve the location of the innermost linkage specification,
17020 tracking the locations of nested specifications via a local. */
17021 location_t saved_location
17022 = parser->innermost_linkage_specification_location;
17023 /* Construct a location ranging from the start of the "extern" to
17024 the end of the string-literal, with the caret at the start, e.g.:
17025 extern "C" {
17026 ^~~~~~~~~~
17028 parser->innermost_linkage_specification_location
17029 = make_location (extern_token->location,
17030 extern_token->location,
17031 get_finish (string_token->location));
17033 /* If the next token is a `{', then we're using the first
17034 production. */
17035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17037 cp_ensure_no_omp_declare_simd (parser);
17038 cp_ensure_no_oacc_routine (parser);
17040 /* Consume the `{' token. */
17041 matching_braces braces;
17042 braces.consume_open (parser);
17043 /* Parse the declarations. */
17044 cp_parser_declaration_seq_opt (parser);
17045 /* Look for the closing `}'. */
17046 braces.require_close (parser);
17048 /* Otherwise, there's just one declaration. */
17049 else
17051 bool saved_in_unbraced_linkage_specification_p;
17053 saved_in_unbraced_linkage_specification_p
17054 = parser->in_unbraced_linkage_specification_p;
17055 parser->in_unbraced_linkage_specification_p = true;
17056 cp_parser_declaration (parser, prefix_attr);
17057 parser->in_unbraced_linkage_specification_p
17058 = saved_in_unbraced_linkage_specification_p;
17061 /* We're done with the linkage-specification. */
17062 pop_lang_context ();
17063 module_kind = saved_module;
17065 /* Restore location of parent linkage specification, if any. */
17066 parser->innermost_linkage_specification_location = saved_location;
17069 /* Parse a static_assert-declaration.
17071 static_assert-declaration:
17072 static_assert ( constant-expression , string-literal ) ;
17073 static_assert ( constant-expression ) ; (C++17)
17074 static_assert ( constant-expression, conditional-expression ) ; (C++26)
17076 If MEMBER_P, this static_assert is a class member. */
17078 static void
17079 cp_parser_static_assert (cp_parser *parser, bool member_p)
17081 cp_expr condition;
17082 location_t token_loc;
17083 tree message;
17085 /* Peek at the `static_assert' token so we can keep track of exactly
17086 where the static assertion started. */
17087 token_loc = cp_lexer_peek_token (parser->lexer)->location;
17089 /* Look for the `static_assert' keyword. */
17090 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
17091 RT_STATIC_ASSERT))
17092 return;
17094 /* We know we are in a static assertion; commit to any tentative
17095 parse. */
17096 if (cp_parser_parsing_tentatively (parser))
17097 cp_parser_commit_to_tentative_parse (parser);
17099 /* Parse the `(' starting the static assertion condition. */
17100 matching_parens parens;
17101 parens.require_open (parser);
17103 /* Parse the constant-expression. Allow a non-constant expression
17104 here in order to give better diagnostics in finish_static_assert. */
17105 condition
17106 = cp_parser_constant_expression (parser,
17107 /*allow_non_constant_p=*/true,
17108 /*non_constant_p=*/nullptr);
17110 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17112 if (pedantic && cxx_dialect < cxx17)
17113 pedwarn (input_location, OPT_Wc__17_extensions,
17114 "%<static_assert%> without a message "
17115 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
17116 /* Eat the ')' */
17117 cp_lexer_consume_token (parser->lexer);
17118 message = build_string (1, "");
17119 TREE_TYPE (message) = char_array_type_node;
17120 fix_string_type (message);
17122 else
17124 /* Parse the separating `,'. */
17125 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
17127 /* Parse the message expression. */
17128 bool string_lit = true;
17129 for (unsigned int i = 1; ; ++i)
17131 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, i);
17132 if (cp_parser_is_pure_string_literal (tok))
17133 continue;
17134 else if (tok->type == CPP_CLOSE_PAREN)
17135 break;
17136 string_lit = false;
17137 break;
17139 if (!string_lit)
17141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
17142 if (cxx_dialect < cxx26)
17143 pedwarn (loc, OPT_Wc__26_extensions,
17144 "%<static_assert%> with non-string message only "
17145 "available with %<-std=c++2c%> or %<-std=gnu++2c%>");
17147 message = cp_parser_conditional_expression (parser);
17148 if (TREE_CODE (message) == STRING_CST)
17149 message = build1_loc (loc, PAREN_EXPR, TREE_TYPE (message),
17150 message);
17152 else if (cxx_dialect >= cxx26)
17153 message = cp_parser_unevaluated_string_literal (parser);
17154 else
17155 message = cp_parser_string_literal (parser, /*translate=*/false,
17156 /*wide_ok=*/true);
17158 /* A `)' completes the static assertion. */
17159 if (!parens.require_close (parser))
17160 cp_parser_skip_to_closing_parenthesis (parser,
17161 /*recovering=*/true,
17162 /*or_comma=*/false,
17163 /*consume_paren=*/true);
17166 /* A semicolon terminates the declaration. */
17167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17169 /* Get the location for the static assertion. Use that of the
17170 condition if available, otherwise, use that of the "static_assert"
17171 token. */
17172 location_t assert_loc = condition.get_location ();
17173 if (assert_loc == UNKNOWN_LOCATION)
17174 assert_loc = token_loc;
17176 /* Complete the static assertion, which may mean either processing
17177 the static assert now or saving it for template instantiation. */
17178 finish_static_assert (condition, message, assert_loc, member_p,
17179 /*show_expr_p=*/false);
17182 /* Parse the expression in decltype ( expression ). */
17184 static tree
17185 cp_parser_decltype_expr (cp_parser *parser,
17186 bool &id_expression_or_member_access_p)
17188 cp_token *id_expr_start_token;
17189 tree expr;
17191 /* First, try parsing an id-expression. */
17192 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
17193 cp_parser_parse_tentatively (parser);
17194 expr = cp_parser_id_expression (parser,
17195 /*template_keyword_p=*/false,
17196 /*check_dependency_p=*/true,
17197 /*template_p=*/NULL,
17198 /*declarator_p=*/false,
17199 /*optional_p=*/false);
17201 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
17203 bool non_integral_constant_expression_p = false;
17204 tree id_expression = expr;
17205 cp_id_kind idk;
17206 const char *error_msg;
17208 if (identifier_p (expr))
17209 /* Lookup the name we got back from the id-expression. */
17210 expr = cp_parser_lookup_name_simple (parser, expr,
17211 id_expr_start_token->location);
17213 if (expr
17214 && expr != error_mark_node
17215 && TREE_CODE (expr) != TYPE_DECL
17216 && (TREE_CODE (expr) != BIT_NOT_EXPR
17217 || !TYPE_P (TREE_OPERAND (expr, 0)))
17218 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17220 /* Complete lookup of the id-expression. */
17221 expr = (finish_id_expression
17222 (id_expression, expr, parser->scope, &idk,
17223 /*integral_constant_expression_p=*/false,
17224 /*allow_non_integral_constant_expression_p=*/true,
17225 &non_integral_constant_expression_p,
17226 /*template_p=*/false,
17227 /*done=*/true,
17228 /*address_p=*/false,
17229 /*template_arg_p=*/false,
17230 &error_msg,
17231 id_expr_start_token->location));
17233 if (error_msg)
17235 /* We found an id-expression, but it was something that we
17236 should not have found. This is an error, not something
17237 we can recover from, so report the error we found and
17238 we'll recover as gracefully as possible. */
17239 cp_parser_parse_definitely (parser);
17240 cp_parser_error (parser, error_msg);
17241 id_expression_or_member_access_p = true;
17242 return error_mark_node;
17246 if (expr
17247 && expr != error_mark_node
17248 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17249 /* We have an id-expression. */
17250 id_expression_or_member_access_p = true;
17253 if (!id_expression_or_member_access_p)
17255 /* Abort the id-expression parse. */
17256 cp_parser_abort_tentative_parse (parser);
17258 /* Parsing tentatively, again. */
17259 cp_parser_parse_tentatively (parser);
17261 /* Parse a class member access. */
17262 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
17263 /*cast_p=*/false, /*decltype*/true,
17264 /*member_access_only_p=*/true, NULL);
17266 if (expr
17267 && expr != error_mark_node
17268 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17269 /* We have an id-expression. */
17270 id_expression_or_member_access_p = true;
17273 if (id_expression_or_member_access_p)
17274 /* We have parsed the complete id-expression or member access. */
17275 cp_parser_parse_definitely (parser);
17276 else
17278 /* Abort our attempt to parse an id-expression or member access
17279 expression. */
17280 cp_parser_abort_tentative_parse (parser);
17282 /* Parse a full expression. */
17283 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
17284 /*decltype_p=*/true);
17287 return expr;
17290 /* Parse a `decltype' type. Returns the type.
17292 decltype-specifier:
17293 decltype ( expression )
17294 C++14:
17295 decltype ( auto ) */
17297 static tree
17298 cp_parser_decltype (cp_parser *parser)
17300 bool id_expression_or_member_access_p = false;
17301 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
17303 if (start_token->type == CPP_DECLTYPE)
17305 /* Already parsed. */
17306 cp_lexer_consume_token (parser->lexer);
17307 return saved_checks_value (start_token->u.tree_check_value);
17310 /* Look for the `decltype' token. */
17311 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
17312 return error_mark_node;
17314 /* Parse the opening `('. */
17315 matching_parens parens;
17316 if (!parens.require_open (parser))
17317 return error_mark_node;
17319 /* Since we're going to preserve any side-effects from this parse, set up a
17320 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17321 in the expression. */
17322 tentative_firewall firewall (parser);
17324 /* If in_declarator_p, a reparse as an expression might succeed (60361).
17325 Otherwise, commit now for better diagnostics. */
17326 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
17327 && !parser->in_declarator_p)
17328 cp_parser_commit_to_topmost_tentative_parse (parser);
17330 push_deferring_access_checks (dk_deferred);
17332 tree expr = NULL_TREE;
17334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
17335 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
17337 /* decltype (auto) */
17338 cp_lexer_consume_token (parser->lexer);
17339 if (cxx_dialect < cxx14)
17341 error_at (start_token->location,
17342 "%<decltype(auto)%> type specifier only available with "
17343 "%<-std=c++14%> or %<-std=gnu++14%>");
17344 expr = error_mark_node;
17347 else
17349 /* decltype (expression) */
17351 /* Types cannot be defined in a `decltype' expression. Save away the
17352 old message and set the new one. */
17353 const char *saved_message = parser->type_definition_forbidden_message;
17354 parser->type_definition_forbidden_message
17355 = G_("types may not be defined in %<decltype%> expressions");
17357 /* The restrictions on constant-expressions do not apply inside
17358 decltype expressions. */
17359 bool saved_integral_constant_expression_p
17360 = parser->integral_constant_expression_p;
17361 bool saved_non_integral_constant_expression_p
17362 = parser->non_integral_constant_expression_p;
17363 parser->integral_constant_expression_p = false;
17365 /* Within a parenthesized expression, a `>' token is always
17366 the greater-than operator. */
17367 bool saved_greater_than_is_operator_p
17368 = parser->greater_than_is_operator_p;
17369 parser->greater_than_is_operator_p = true;
17371 /* Don't synthesize an implicit template type parameter here. This
17372 could happen with C++23 code like
17374 void f(decltype(new auto{0}));
17376 where we want to deduce the auto right away so that the parameter
17377 is of type 'int *'. */
17378 auto cleanup = make_temp_override
17379 (parser->auto_is_implicit_function_template_parm_p, false);
17381 /* Do not actually evaluate the expression. */
17382 ++cp_unevaluated_operand;
17384 /* Do not warn about problems with the expression. */
17385 ++c_inhibit_evaluation_warnings;
17387 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
17388 STRIP_ANY_LOCATION_WRAPPER (expr);
17390 /* Go back to evaluating expressions. */
17391 --cp_unevaluated_operand;
17392 --c_inhibit_evaluation_warnings;
17394 /* The `>' token might be the end of a template-id or
17395 template-parameter-list now. */
17396 parser->greater_than_is_operator_p
17397 = saved_greater_than_is_operator_p;
17399 /* Restore the old message and the integral constant expression
17400 flags. */
17401 parser->type_definition_forbidden_message = saved_message;
17402 parser->integral_constant_expression_p
17403 = saved_integral_constant_expression_p;
17404 parser->non_integral_constant_expression_p
17405 = saved_non_integral_constant_expression_p;
17408 /* Parse to the closing `)'. */
17409 if (expr == error_mark_node || !parens.require_close (parser))
17411 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17412 /*consume_paren=*/true);
17413 expr = error_mark_node;
17416 /* If we got a parse error while tentative, bail out now. */
17417 if (cp_parser_error_occurred (parser))
17419 pop_deferring_access_checks ();
17420 return error_mark_node;
17423 if (!expr)
17424 /* Build auto. */
17425 expr = make_decltype_auto ();
17426 else
17427 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
17428 tf_warning_or_error);
17430 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
17431 it again. */
17432 start_token->type = CPP_DECLTYPE;
17433 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17434 start_token->tree_check_p = true;
17435 start_token->u.tree_check_value->value = expr;
17436 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
17437 start_token->keyword = RID_MAX;
17439 location_t loc = start_token->location;
17440 loc = make_location (loc, loc, parser->lexer);
17441 start_token->location = loc;
17443 cp_lexer_purge_tokens_after (parser->lexer, start_token);
17445 pop_to_parent_deferring_access_checks ();
17447 return expr;
17450 /* Special member functions [gram.special] */
17452 /* Parse a conversion-function-id.
17454 conversion-function-id:
17455 operator conversion-type-id
17457 Returns an IDENTIFIER_NODE representing the operator. */
17459 static tree
17460 cp_parser_conversion_function_id (cp_parser* parser)
17462 tree type;
17463 tree saved_scope;
17464 tree saved_qualifying_scope;
17465 tree saved_object_scope;
17466 tree pushed_scope = NULL_TREE;
17468 /* Look for the `operator' token. */
17469 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17470 return error_mark_node;
17471 /* When we parse the conversion-type-id, the current scope will be
17472 reset. However, we need that information in able to look up the
17473 conversion function later, so we save it here. */
17474 saved_scope = parser->scope;
17475 saved_qualifying_scope = parser->qualifying_scope;
17476 saved_object_scope = parser->object_scope;
17477 /* We must enter the scope of the class so that the names of
17478 entities declared within the class are available in the
17479 conversion-type-id. For example, consider:
17481 struct S {
17482 typedef int I;
17483 operator I();
17486 S::operator I() { ... }
17488 In order to see that `I' is a type-name in the definition, we
17489 must be in the scope of `S'. */
17490 if (saved_scope)
17491 pushed_scope = push_scope (saved_scope);
17492 /* Parse the conversion-type-id. */
17493 type = cp_parser_conversion_type_id (parser);
17494 /* Leave the scope of the class, if any. */
17495 if (pushed_scope)
17496 pop_scope (pushed_scope);
17497 /* Restore the saved scope. */
17498 parser->scope = saved_scope;
17499 parser->qualifying_scope = saved_qualifying_scope;
17500 parser->object_scope = saved_object_scope;
17501 /* If the TYPE is invalid, indicate failure. */
17502 if (type == error_mark_node)
17503 return error_mark_node;
17504 return make_conv_op_name (type);
17507 /* Parse a conversion-type-id:
17509 conversion-type-id:
17510 type-specifier-seq conversion-declarator [opt]
17512 Returns the TYPE specified. */
17514 static tree
17515 cp_parser_conversion_type_id (cp_parser* parser)
17517 tree attributes;
17518 cp_decl_specifier_seq type_specifiers;
17519 cp_declarator *declarator;
17520 tree type_specified;
17521 const char *saved_message;
17523 /* Parse the attributes. */
17524 attributes = cp_parser_attributes_opt (parser);
17526 saved_message = parser->type_definition_forbidden_message;
17527 parser->type_definition_forbidden_message
17528 = G_("types may not be defined in a conversion-type-id");
17530 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17531 optional in conversion-type-id. */
17532 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17533 /*is_declaration=*/false,
17534 /*is_trailing_return=*/false,
17535 &type_specifiers);
17537 parser->type_definition_forbidden_message = saved_message;
17539 /* If that didn't work, stop. */
17540 if (type_specifiers.type == error_mark_node)
17541 return error_mark_node;
17542 /* Parse the conversion-declarator. */
17543 declarator = cp_parser_conversion_declarator_opt (parser);
17545 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17546 /*initialized=*/0, &attributes);
17547 if (attributes)
17548 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17550 /* Don't give this error when parsing tentatively. This happens to
17551 work because we always parse this definitively once. */
17552 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17553 && type_uses_auto (type_specified))
17555 if (cxx_dialect < cxx14)
17557 error ("invalid use of %<auto%> in conversion operator");
17558 return error_mark_node;
17560 else if (template_parm_scope_p ())
17561 warning (0, "use of %<auto%> in member template "
17562 "conversion operator can never be deduced");
17565 return type_specified;
17568 /* Parse an (optional) conversion-declarator.
17570 conversion-declarator:
17571 ptr-operator conversion-declarator [opt]
17575 static cp_declarator *
17576 cp_parser_conversion_declarator_opt (cp_parser* parser)
17578 enum tree_code code;
17579 tree class_type, std_attributes = NULL_TREE;
17580 cp_cv_quals cv_quals;
17582 /* We don't know if there's a ptr-operator next, or not. */
17583 cp_parser_parse_tentatively (parser);
17584 /* Try the ptr-operator. */
17585 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17586 &std_attributes);
17587 /* If it worked, look for more conversion-declarators. */
17588 if (cp_parser_parse_definitely (parser))
17590 cp_declarator *declarator;
17592 /* Parse another optional declarator. */
17593 declarator = cp_parser_conversion_declarator_opt (parser);
17595 declarator = cp_parser_make_indirect_declarator
17596 (code, class_type, cv_quals, declarator, std_attributes);
17598 return declarator;
17601 return NULL;
17604 /* Parse an (optional) ctor-initializer.
17606 ctor-initializer:
17607 : mem-initializer-list */
17609 static void
17610 cp_parser_ctor_initializer_opt (cp_parser* parser)
17612 /* If the next token is not a `:', then there is no
17613 ctor-initializer. */
17614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17616 /* Do default initialization of any bases and members. */
17617 if (DECL_CONSTRUCTOR_P (current_function_decl))
17618 finish_mem_initializers (NULL_TREE);
17619 return;
17622 /* Consume the `:' token. */
17623 cp_lexer_consume_token (parser->lexer);
17624 /* And the mem-initializer-list. */
17625 cp_parser_mem_initializer_list (parser);
17628 /* Parse a mem-initializer-list.
17630 mem-initializer-list:
17631 mem-initializer ... [opt]
17632 mem-initializer ... [opt] , mem-initializer-list */
17634 static void
17635 cp_parser_mem_initializer_list (cp_parser* parser)
17637 tree mem_initializer_list = NULL_TREE;
17638 tree target_ctor = error_mark_node;
17639 cp_token *token = cp_lexer_peek_token (parser->lexer);
17641 /* Let the semantic analysis code know that we are starting the
17642 mem-initializer-list. */
17643 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17644 error_at (token->location,
17645 "only constructors take member initializers");
17647 /* Loop through the list. */
17648 while (true)
17650 tree mem_initializer;
17652 token = cp_lexer_peek_token (parser->lexer);
17653 /* Parse the mem-initializer. */
17654 mem_initializer = cp_parser_mem_initializer (parser);
17655 /* If the next token is a `...', we're expanding member initializers. */
17656 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17657 if (ellipsis
17658 || (mem_initializer != error_mark_node
17659 && check_for_bare_parameter_packs (TREE_PURPOSE
17660 (mem_initializer))))
17662 /* Consume the `...'. */
17663 if (ellipsis)
17664 cp_lexer_consume_token (parser->lexer);
17666 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17667 can be expanded but members cannot. */
17668 if (mem_initializer != error_mark_node
17669 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17671 error_at (token->location,
17672 "cannot expand initializer for member %qD",
17673 TREE_PURPOSE (mem_initializer));
17674 mem_initializer = error_mark_node;
17677 /* Construct the pack expansion type. */
17678 if (mem_initializer != error_mark_node)
17679 mem_initializer = make_pack_expansion (mem_initializer);
17681 if (target_ctor != error_mark_node
17682 && mem_initializer != error_mark_node)
17684 error ("mem-initializer for %qD follows constructor delegation",
17685 TREE_PURPOSE (mem_initializer));
17686 mem_initializer = error_mark_node;
17688 /* Look for a target constructor. */
17689 if (mem_initializer != error_mark_node
17690 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17691 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17693 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17694 if (mem_initializer_list)
17696 error ("constructor delegation follows mem-initializer for %qD",
17697 TREE_PURPOSE (mem_initializer_list));
17698 mem_initializer = error_mark_node;
17700 target_ctor = mem_initializer;
17702 /* Add it to the list, unless it was erroneous. */
17703 if (mem_initializer != error_mark_node)
17705 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17706 mem_initializer_list = mem_initializer;
17708 /* If the next token is not a `,', we're done. */
17709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17710 break;
17711 /* Consume the `,' token. */
17712 cp_lexer_consume_token (parser->lexer);
17715 /* Perform semantic analysis. */
17716 if (DECL_CONSTRUCTOR_P (current_function_decl))
17717 finish_mem_initializers (mem_initializer_list);
17720 /* Parse a mem-initializer.
17722 mem-initializer:
17723 mem-initializer-id ( expression-list [opt] )
17724 mem-initializer-id braced-init-list
17726 GNU extension:
17728 mem-initializer:
17729 ( expression-list [opt] )
17731 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17732 class) or FIELD_DECL (for a non-static data member) to initialize;
17733 the TREE_VALUE is the expression-list. An empty initialization
17734 list is represented by void_list_node. */
17736 static tree
17737 cp_parser_mem_initializer (cp_parser* parser)
17739 tree mem_initializer_id;
17740 tree expression_list;
17741 tree member;
17742 cp_token *token = cp_lexer_peek_token (parser->lexer);
17744 /* Find out what is being initialized. */
17745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17747 permerror (token->location,
17748 "anachronistic old-style base class initializer");
17749 mem_initializer_id = NULL_TREE;
17751 else
17753 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17754 if (mem_initializer_id == error_mark_node)
17755 return mem_initializer_id;
17757 member = expand_member_init (mem_initializer_id);
17758 if (member && !DECL_P (member))
17759 in_base_initializer = 1;
17761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17763 cp_lexer_set_source_position (parser->lexer);
17764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17765 expression_list = cp_parser_braced_list (parser);
17766 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17767 expression_list = build_tree_list (NULL_TREE, expression_list);
17769 else
17771 vec<tree, va_gc> *vec;
17772 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17773 /*cast_p=*/false,
17774 /*allow_expansion_p=*/true,
17775 /*non_constant_p=*/NULL,
17776 /*close_paren_loc=*/NULL,
17777 /*wrap_locations_p=*/true);
17778 if (vec == NULL)
17779 return error_mark_node;
17780 expression_list = build_tree_list_vec (vec);
17781 release_tree_vector (vec);
17784 if (expression_list == error_mark_node)
17785 return error_mark_node;
17786 if (!expression_list)
17787 expression_list = void_type_node;
17789 in_base_initializer = 0;
17791 if (!member)
17792 return error_mark_node;
17793 tree node = build_tree_list (member, expression_list);
17795 /* We can't attach the source location of this initializer directly to
17796 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17797 within the TREE_TYPE of the list node. */
17798 location_t loc
17799 = make_location (token->location, token->location, parser->lexer);
17800 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17801 SET_EXPR_LOCATION (dummy, loc);
17802 TREE_TYPE (node) = dummy;
17804 return node;
17807 /* Parse a mem-initializer-id.
17809 mem-initializer-id:
17810 :: [opt] nested-name-specifier [opt] class-name
17811 decltype-specifier (C++11)
17812 identifier
17814 Returns a TYPE indicating the class to be initialized for the first
17815 production (and the second in C++11). Returns an IDENTIFIER_NODE
17816 indicating the data member to be initialized for the last production. */
17818 static tree
17819 cp_parser_mem_initializer_id (cp_parser* parser)
17821 bool global_scope_p;
17822 bool nested_name_specifier_p;
17823 bool template_p = false;
17824 tree id;
17826 cp_token *token = cp_lexer_peek_token (parser->lexer);
17828 /* `typename' is not allowed in this context ([temp.res]). */
17829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17831 error_at (token->location,
17832 "keyword %<typename%> not allowed in this context (a qualified "
17833 "member initializer is implicitly a type)");
17834 cp_lexer_consume_token (parser->lexer);
17836 /* Look for the optional `::' operator. */
17837 global_scope_p
17838 = (cp_parser_global_scope_opt (parser,
17839 /*current_scope_valid_p=*/false)
17840 != NULL_TREE);
17841 /* Look for the optional nested-name-specifier. The simplest way to
17842 implement:
17844 [temp.res]
17846 The keyword `typename' is not permitted in a base-specifier or
17847 mem-initializer; in these contexts a qualified name that
17848 depends on a template-parameter is implicitly assumed to be a
17849 type name.
17851 is to assume that we have seen the `typename' keyword at this
17852 point. */
17853 nested_name_specifier_p
17854 = (cp_parser_nested_name_specifier_opt (parser,
17855 /*typename_keyword_p=*/true,
17856 /*check_dependency_p=*/true,
17857 /*type_p=*/true,
17858 /*is_declaration=*/true)
17859 != NULL_TREE);
17860 if (nested_name_specifier_p)
17861 template_p = cp_parser_optional_template_keyword (parser);
17862 /* If there is a `::' operator or a nested-name-specifier, then we
17863 are definitely looking for a class-name. */
17864 if (global_scope_p || nested_name_specifier_p)
17865 return cp_parser_class_name (parser,
17866 /*typename_keyword_p=*/true,
17867 /*template_keyword_p=*/template_p,
17868 typename_type,
17869 /*check_dependency_p=*/true,
17870 /*class_head_p=*/false,
17871 /*is_declaration=*/true);
17872 /* Otherwise, we could also be looking for an ordinary identifier. */
17873 cp_parser_parse_tentatively (parser);
17874 if (cp_lexer_next_token_is_decltype (parser->lexer))
17875 /* Try a decltype-specifier. */
17876 id = cp_parser_decltype (parser);
17877 else
17878 /* Otherwise, try a class-name. */
17879 id = cp_parser_class_name (parser,
17880 /*typename_keyword_p=*/true,
17881 /*template_keyword_p=*/false,
17882 none_type,
17883 /*check_dependency_p=*/true,
17884 /*class_head_p=*/false,
17885 /*is_declaration=*/true);
17886 /* If we found one, we're done. */
17887 if (cp_parser_parse_definitely (parser))
17888 return id;
17889 /* Otherwise, look for an ordinary identifier. */
17890 return cp_parser_identifier (parser);
17893 /* Overloading [gram.over] */
17895 /* Parse an operator-function-id.
17897 operator-function-id:
17898 operator operator
17900 Returns an IDENTIFIER_NODE for the operator which is a
17901 human-readable spelling of the identifier, e.g., `operator +'. */
17903 static cp_expr
17904 cp_parser_operator_function_id (cp_parser* parser)
17906 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17907 /* Look for the `operator' keyword. */
17908 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17909 return error_mark_node;
17910 /* And then the name of the operator itself. */
17911 return cp_parser_operator (parser, start_loc);
17914 /* Return an identifier node for a user-defined literal operator.
17915 The suffix identifier is chained to the operator name identifier. */
17917 tree
17918 cp_literal_operator_id (const char* name)
17920 tree identifier;
17921 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17922 + strlen (name) + 10);
17923 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17924 identifier = get_identifier (buffer);
17925 XDELETEVEC (buffer);
17927 return identifier;
17930 /* Parse an operator.
17932 operator:
17933 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17934 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17935 || ++ -- , ->* -> () []
17937 GNU Extensions:
17939 operator:
17940 <? >? <?= >?=
17942 Returns an IDENTIFIER_NODE for the operator which is a
17943 human-readable spelling of the identifier, e.g., `operator +'. */
17945 static cp_expr
17946 cp_parser_operator (cp_parser* parser, location_t start_loc)
17948 tree id = NULL_TREE;
17949 cp_token *token;
17950 bool utf8 = false;
17952 /* Peek at the next token. */
17953 token = cp_lexer_peek_token (parser->lexer);
17955 location_t end_loc = token->location;
17957 /* Figure out which operator we have. */
17958 enum tree_code op = ERROR_MARK;
17959 bool assop = false;
17960 bool consumed = false;
17961 switch (token->type)
17963 case CPP_KEYWORD:
17965 /* The keyword should be either `new', `delete' or `co_await'. */
17966 if (token->keyword == RID_NEW)
17967 op = NEW_EXPR;
17968 else if (token->keyword == RID_DELETE)
17969 op = DELETE_EXPR;
17970 else if (token->keyword == RID_CO_AWAIT)
17971 op = CO_AWAIT_EXPR;
17972 else
17973 break;
17975 /* Consume the `new', `delete' or co_await token. */
17976 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17978 /* Peek at the next token. */
17979 token = cp_lexer_peek_token (parser->lexer);
17980 /* If it's a `[' token then this is the array variant of the
17981 operator. */
17982 if (token->type == CPP_OPEN_SQUARE
17983 && op != CO_AWAIT_EXPR)
17985 /* Consume the `[' token. */
17986 cp_lexer_consume_token (parser->lexer);
17987 /* Look for the `]' token. */
17988 if (cp_token *close_token
17989 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17990 end_loc = close_token->location;
17991 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17993 consumed = true;
17994 break;
17997 case CPP_PLUS:
17998 op = PLUS_EXPR;
17999 break;
18001 case CPP_MINUS:
18002 op = MINUS_EXPR;
18003 break;
18005 case CPP_MULT:
18006 op = MULT_EXPR;
18007 break;
18009 case CPP_DIV:
18010 op = TRUNC_DIV_EXPR;
18011 break;
18013 case CPP_MOD:
18014 op = TRUNC_MOD_EXPR;
18015 break;
18017 case CPP_XOR:
18018 op = BIT_XOR_EXPR;
18019 break;
18021 case CPP_AND:
18022 op = BIT_AND_EXPR;
18023 break;
18025 case CPP_OR:
18026 op = BIT_IOR_EXPR;
18027 break;
18029 case CPP_COMPL:
18030 op = BIT_NOT_EXPR;
18031 break;
18033 case CPP_NOT:
18034 op = TRUTH_NOT_EXPR;
18035 break;
18037 case CPP_EQ:
18038 assop = true;
18039 op = NOP_EXPR;
18040 break;
18042 case CPP_LESS:
18043 op = LT_EXPR;
18044 break;
18046 case CPP_GREATER:
18047 op = GT_EXPR;
18048 break;
18050 case CPP_PLUS_EQ:
18051 assop = true;
18052 op = PLUS_EXPR;
18053 break;
18055 case CPP_MINUS_EQ:
18056 assop = true;
18057 op = MINUS_EXPR;
18058 break;
18060 case CPP_MULT_EQ:
18061 assop = true;
18062 op = MULT_EXPR;
18063 break;
18065 case CPP_DIV_EQ:
18066 assop = true;
18067 op = TRUNC_DIV_EXPR;
18068 break;
18070 case CPP_MOD_EQ:
18071 assop = true;
18072 op = TRUNC_MOD_EXPR;
18073 break;
18075 case CPP_XOR_EQ:
18076 assop = true;
18077 op = BIT_XOR_EXPR;
18078 break;
18080 case CPP_AND_EQ:
18081 assop = true;
18082 op = BIT_AND_EXPR;
18083 break;
18085 case CPP_OR_EQ:
18086 assop = true;
18087 op = BIT_IOR_EXPR;
18088 break;
18090 case CPP_LSHIFT:
18091 op = LSHIFT_EXPR;
18092 break;
18094 case CPP_RSHIFT:
18095 op = RSHIFT_EXPR;
18096 break;
18098 case CPP_LSHIFT_EQ:
18099 assop = true;
18100 op = LSHIFT_EXPR;
18101 break;
18103 case CPP_RSHIFT_EQ:
18104 assop = true;
18105 op = RSHIFT_EXPR;
18106 break;
18108 case CPP_EQ_EQ:
18109 op = EQ_EXPR;
18110 break;
18112 case CPP_NOT_EQ:
18113 op = NE_EXPR;
18114 break;
18116 case CPP_LESS_EQ:
18117 op = LE_EXPR;
18118 break;
18120 case CPP_GREATER_EQ:
18121 op = GE_EXPR;
18122 break;
18124 case CPP_SPACESHIP:
18125 op = SPACESHIP_EXPR;
18126 break;
18128 case CPP_AND_AND:
18129 op = TRUTH_ANDIF_EXPR;
18130 break;
18132 case CPP_OR_OR:
18133 op = TRUTH_ORIF_EXPR;
18134 break;
18136 case CPP_PLUS_PLUS:
18137 op = POSTINCREMENT_EXPR;
18138 break;
18140 case CPP_MINUS_MINUS:
18141 op = PREDECREMENT_EXPR;
18142 break;
18144 case CPP_COMMA:
18145 op = COMPOUND_EXPR;
18146 break;
18148 case CPP_DEREF_STAR:
18149 op = MEMBER_REF;
18150 break;
18152 case CPP_DEREF:
18153 op = COMPONENT_REF;
18154 break;
18156 case CPP_QUERY:
18157 op = COND_EXPR;
18158 /* Consume the `?'. */
18159 cp_lexer_consume_token (parser->lexer);
18160 /* Look for the matching `:'. */
18161 cp_parser_require (parser, CPP_COLON, RT_COLON);
18162 consumed = true;
18163 break;
18165 case CPP_OPEN_PAREN:
18167 /* Consume the `('. */
18168 matching_parens parens;
18169 parens.consume_open (parser);
18170 /* Look for the matching `)'. */
18171 token = parens.require_close (parser);
18172 if (token)
18173 end_loc = token->location;
18174 op = CALL_EXPR;
18175 consumed = true;
18176 break;
18179 case CPP_OPEN_SQUARE:
18180 /* Consume the `['. */
18181 cp_lexer_consume_token (parser->lexer);
18182 /* Look for the matching `]'. */
18183 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18184 if (token)
18185 end_loc = token->location;
18186 op = ARRAY_REF;
18187 consumed = true;
18188 break;
18190 case CPP_UTF8STRING:
18191 case CPP_UTF8STRING_USERDEF:
18192 utf8 = true;
18193 /* FALLTHRU */
18194 case CPP_STRING:
18195 case CPP_WSTRING:
18196 case CPP_STRING16:
18197 case CPP_STRING32:
18198 case CPP_STRING_USERDEF:
18199 case CPP_WSTRING_USERDEF:
18200 case CPP_STRING16_USERDEF:
18201 case CPP_STRING32_USERDEF:
18203 tree string_tree;
18204 int sz, len;
18206 if (cxx_dialect == cxx98)
18207 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
18209 /* Consume the string. */
18210 cp_expr str = cp_parser_userdef_string_literal (parser,
18211 /*lookup_udlit=*/false);
18212 if (str == error_mark_node)
18213 return error_mark_node;
18214 else if (TREE_CODE (str) == USERDEF_LITERAL)
18216 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
18217 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
18218 end_loc = str.get_location ();
18220 else
18222 string_tree = str;
18223 /* Look for the suffix identifier. */
18224 token = cp_lexer_peek_token (parser->lexer);
18225 if (token->type == CPP_NAME)
18227 id = cp_parser_identifier (parser);
18228 end_loc = token->location;
18230 else if (token->type == CPP_KEYWORD)
18232 error ("unexpected keyword;"
18233 " remove space between quotes and suffix identifier");
18234 return error_mark_node;
18236 else
18238 error ("expected suffix identifier");
18239 return error_mark_node;
18242 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
18243 (TREE_TYPE (TREE_TYPE (string_tree))));
18244 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
18245 if (len != 0)
18247 error ("expected empty string after %<operator%> keyword");
18248 return error_mark_node;
18250 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
18251 != char_type_node)
18253 error ("invalid encoding prefix in literal operator");
18254 return error_mark_node;
18256 if (id != error_mark_node)
18258 const char *name = IDENTIFIER_POINTER (id);
18259 id = cp_literal_operator_id (name);
18261 /* Generate a location of the form:
18262 "" _suffix_identifier
18263 ^~~~~~~~~~~~~~~~~~~~~
18264 with caret == start at the start token, finish at the end of the
18265 suffix identifier. */
18266 location_t combined_loc
18267 = make_location (start_loc, start_loc, parser->lexer);
18268 return cp_expr (id, combined_loc);
18271 default:
18272 /* Anything else is an error. */
18273 break;
18276 /* If we have selected an identifier, we need to consume the
18277 operator token. */
18278 if (op != ERROR_MARK)
18280 id = ovl_op_identifier (assop, op);
18281 if (!consumed)
18282 cp_lexer_consume_token (parser->lexer);
18284 /* Otherwise, no valid operator name was present. */
18285 else
18287 cp_parser_error (parser, "expected operator");
18288 id = error_mark_node;
18291 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
18292 return cp_expr (id, start_loc);
18295 /* Parse a template-declaration.
18297 template-declaration:
18298 export [opt] template < template-parameter-list > declaration
18300 If MEMBER_P is TRUE, this template-declaration occurs within a
18301 class-specifier.
18303 The grammar rule given by the standard isn't correct. What
18304 is really meant is:
18306 template-declaration:
18307 export [opt] template-parameter-list-seq
18308 decl-specifier-seq [opt] init-declarator [opt] ;
18309 export [opt] template-parameter-list-seq
18310 function-definition
18312 template-parameter-list-seq:
18313 template-parameter-list-seq [opt]
18314 template < template-parameter-list >
18316 Concept Extensions:
18318 template-parameter-list-seq:
18319 template < template-parameter-list > requires-clause [opt]
18321 requires-clause:
18322 requires logical-or-expression */
18324 static void
18325 cp_parser_template_declaration (cp_parser* parser, bool member_p)
18327 /* Check for `export'. */
18328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
18330 /* Consume the `export' token. */
18331 cp_lexer_consume_token (parser->lexer);
18332 /* Warn that this use of export is deprecated. */
18333 if (cxx_dialect < cxx11)
18334 warning (0, "keyword %<export%> not implemented, and will be ignored");
18335 else if (cxx_dialect < cxx20)
18336 warning (0, "keyword %<export%> is deprecated, and is ignored");
18337 else
18338 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
18341 cp_parser_template_declaration_after_export (parser, member_p);
18344 /* Parse a template-parameter-list.
18346 template-parameter-list:
18347 template-parameter
18348 template-parameter-list , template-parameter
18350 Returns a TREE_LIST. Each node represents a template parameter.
18351 The nodes are connected via their TREE_CHAINs. */
18353 static tree
18354 cp_parser_template_parameter_list (cp_parser* parser)
18356 tree parameter_list = NULL_TREE;
18358 /* Don't create wrapper nodes within a template-parameter-list,
18359 since we don't want to have different types based on the
18360 spelling location of constants and decls within them. */
18361 auto_suppress_location_wrappers sentinel;
18363 begin_template_parm_list ();
18365 /* The loop below parses the template parms. We first need to know
18366 the total number of template parms to be able to compute proper
18367 canonical types of each dependent type. So after the loop, when
18368 we know the total number of template parms,
18369 end_template_parm_list computes the proper canonical types and
18370 fixes up the dependent types accordingly. */
18371 while (true)
18373 tree parameter;
18374 bool is_non_type;
18375 bool is_parameter_pack;
18376 location_t parm_loc;
18378 /* Parse the template-parameter. */
18379 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
18380 parameter = cp_parser_template_parameter (parser,
18381 &is_non_type,
18382 &is_parameter_pack);
18383 /* Add it to the list. */
18384 if (parameter != error_mark_node)
18385 parameter_list = process_template_parm (parameter_list,
18386 parm_loc,
18387 parameter,
18388 is_non_type,
18389 is_parameter_pack);
18390 else
18392 tree err_parm = build_tree_list (parameter, parameter);
18393 parameter_list = chainon (parameter_list, err_parm);
18396 /* If the next token is not a `,', we're done. */
18397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18398 break;
18399 /* Otherwise, consume the `,' token. */
18400 cp_lexer_consume_token (parser->lexer);
18403 return end_template_parm_list (parameter_list);
18406 /* Parse a introduction-list.
18408 introduction-list:
18409 introduced-parameter
18410 introduction-list , introduced-parameter
18412 introduced-parameter:
18413 ...[opt] identifier
18415 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
18416 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
18417 WILDCARD_DECL will also have DECL_NAME set and token location in
18418 DECL_SOURCE_LOCATION. */
18420 static tree
18421 cp_parser_introduction_list (cp_parser *parser)
18423 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
18425 while (true)
18427 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
18428 if (is_pack)
18429 cp_lexer_consume_token (parser->lexer);
18431 tree identifier = cp_parser_identifier (parser);
18432 if (identifier == error_mark_node)
18433 break;
18435 /* Build placeholder. */
18436 tree parm = build_nt (WILDCARD_DECL);
18437 DECL_SOURCE_LOCATION (parm)
18438 = cp_lexer_peek_token (parser->lexer)->location;
18439 DECL_NAME (parm) = identifier;
18440 WILDCARD_PACK_P (parm) = is_pack;
18441 vec_safe_push (introduction_vec, parm);
18443 /* If the next token is not a `,', we're done. */
18444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18445 break;
18446 /* Otherwise, consume the `,' token. */
18447 cp_lexer_consume_token (parser->lexer);
18450 /* Convert the vec into a TREE_VEC. */
18451 tree introduction_list = make_tree_vec (introduction_vec->length ());
18452 unsigned int n;
18453 tree parm;
18454 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
18455 TREE_VEC_ELT (introduction_list, n) = parm;
18457 release_tree_vector (introduction_vec);
18458 return introduction_list;
18461 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
18462 is an abstract declarator. */
18464 static inline cp_declarator*
18465 get_id_declarator (cp_declarator *declarator)
18467 cp_declarator *d = declarator;
18468 while (d && d->kind != cdk_id)
18469 d = d->declarator;
18470 return d;
18473 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
18474 is an abstract declarator. */
18476 static inline tree
18477 get_unqualified_id (cp_declarator *declarator)
18479 declarator = get_id_declarator (declarator);
18480 if (declarator)
18481 return declarator->u.id.unqualified_name;
18482 else
18483 return NULL_TREE;
18486 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18488 static inline bool
18489 is_constrained_parameter (tree type)
18491 return (type
18492 && TREE_CODE (type) == TYPE_DECL
18493 && CONSTRAINED_PARM_CONCEPT (type)
18494 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18497 /* Returns true if PARM declares a constrained-parameter. */
18499 static inline bool
18500 is_constrained_parameter (cp_parameter_declarator *parm)
18502 return is_constrained_parameter (parm->decl_specifiers.type);
18505 /* Check that the type parameter is only a declarator-id, and that its
18506 type is not cv-qualified. */
18508 bool
18509 cp_parser_check_constrained_type_parm (cp_parser *parser,
18510 cp_parameter_declarator *parm)
18512 if (!parm->declarator)
18513 return true;
18515 if (parm->declarator->kind != cdk_id)
18517 cp_parser_error (parser, "invalid constrained type parameter");
18518 return false;
18521 /* Don't allow cv-qualified type parameters. */
18522 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18523 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18525 cp_parser_error (parser, "cv-qualified type parameter");
18526 return false;
18529 return true;
18532 /* Finish parsing/processing a template type parameter and checking
18533 various restrictions. */
18535 static inline tree
18536 cp_parser_constrained_type_template_parm (cp_parser *parser,
18537 tree id,
18538 cp_parameter_declarator* parmdecl)
18540 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18541 return finish_template_type_parm (class_type_node, id);
18542 else
18543 return error_mark_node;
18546 static tree
18547 finish_constrained_template_template_parm (tree proto, tree id)
18549 /* FIXME: This should probably be copied, and we may need to adjust
18550 the template parameter depths. */
18551 tree saved_parms = current_template_parms;
18552 begin_template_parm_list ();
18553 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18554 end_template_parm_list ();
18556 tree parm = finish_template_template_parm (class_type_node, id);
18557 current_template_parms = saved_parms;
18559 return parm;
18562 /* Finish parsing/processing a template template parameter by borrowing
18563 the template parameter list from the prototype parameter. */
18565 static tree
18566 cp_parser_constrained_template_template_parm (cp_parser *parser,
18567 tree proto,
18568 tree id,
18569 cp_parameter_declarator *parmdecl)
18571 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18572 return error_mark_node;
18573 return finish_constrained_template_template_parm (proto, id);
18576 /* Create a new non-type template parameter from the given PARM
18577 declarator. */
18579 static tree
18580 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18581 cp_parameter_declarator *parm)
18583 *is_non_type = true;
18584 cp_declarator *decl = parm->declarator;
18585 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18586 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18587 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18590 /* Build a constrained template parameter based on the PARMDECL
18591 declarator. The type of PARMDECL is the constrained type, which
18592 refers to the prototype template parameter that ultimately
18593 specifies the type of the declared parameter. */
18595 static tree
18596 finish_constrained_parameter (cp_parser *parser,
18597 cp_parameter_declarator *parmdecl,
18598 bool *is_non_type)
18600 tree decl = parmdecl->decl_specifiers.type;
18601 tree id = get_unqualified_id (parmdecl->declarator);
18602 tree def = parmdecl->default_argument;
18603 tree proto = DECL_INITIAL (decl);
18605 /* Build the parameter. Return an error if the declarator was invalid. */
18606 tree parm;
18607 if (TREE_CODE (proto) == TYPE_DECL)
18608 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18609 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18610 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18611 parmdecl);
18612 else
18613 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18614 if (parm == error_mark_node)
18615 return error_mark_node;
18617 /* Finish the parameter decl and create a node attaching the
18618 default argument and constraint. */
18619 parm = build_tree_list (def, parm);
18620 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18622 return parm;
18625 /* Returns true if the parsed type actually represents the declaration
18626 of a type template-parameter. */
18628 static bool
18629 declares_constrained_type_template_parameter (tree type)
18631 return (is_constrained_parameter (type)
18632 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18635 /* Returns true if the parsed type actually represents the declaration of
18636 a template template-parameter. */
18638 static bool
18639 declares_constrained_template_template_parameter (tree type)
18641 return (is_constrained_parameter (type)
18642 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18645 /* Parse a default argument for a type template-parameter.
18646 Note that diagnostics are handled in cp_parser_template_parameter. */
18648 static tree
18649 cp_parser_default_type_template_argument (cp_parser *parser)
18651 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18653 /* Consume the `=' token. */
18654 cp_lexer_consume_token (parser->lexer);
18656 cp_token *token = cp_lexer_peek_token (parser->lexer);
18658 /* Tell cp_parser_lambda_expression this is a default argument. */
18659 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18660 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18662 /* Parse the default-argument. */
18663 push_deferring_access_checks (dk_no_deferred);
18664 tree default_argument = cp_parser_type_id (parser,
18665 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18666 NULL);
18667 pop_deferring_access_checks ();
18669 if (flag_concepts && type_uses_auto (default_argument))
18671 error_at (token->location,
18672 "invalid use of %<auto%> in default template argument");
18673 return error_mark_node;
18676 return default_argument;
18679 /* Parse a default argument for a template template-parameter. */
18681 static tree
18682 cp_parser_default_template_template_argument (cp_parser *parser)
18684 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18686 bool is_template;
18688 /* Consume the `='. */
18689 cp_lexer_consume_token (parser->lexer);
18690 /* Parse the id-expression. */
18691 push_deferring_access_checks (dk_no_deferred);
18692 /* save token before parsing the id-expression, for error
18693 reporting */
18694 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18695 tree default_argument
18696 = cp_parser_id_expression (parser,
18697 /*template_keyword_p=*/false,
18698 /*check_dependency_p=*/true,
18699 /*template_p=*/&is_template,
18700 /*declarator_p=*/false,
18701 /*optional_p=*/false);
18702 if (TREE_CODE (default_argument) == TYPE_DECL)
18703 /* If the id-expression was a template-id that refers to
18704 a template-class, we already have the declaration here,
18705 so no further lookup is needed. */
18707 else
18708 /* Look up the name. */
18709 default_argument
18710 = cp_parser_lookup_name (parser, default_argument,
18711 none_type,
18712 /*is_template=*/is_template,
18713 /*is_namespace=*/false,
18714 /*check_dependency=*/true,
18715 /*ambiguous_decls=*/NULL,
18716 token->location);
18717 /* See if the default argument is valid. */
18718 default_argument = check_template_template_default_arg (default_argument);
18719 pop_deferring_access_checks ();
18720 return default_argument;
18723 /* Parse a template-parameter.
18725 template-parameter:
18726 type-parameter
18727 parameter-declaration
18729 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18730 the parameter. The TREE_PURPOSE is the default value, if any.
18731 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18732 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18733 set to true iff this parameter is a parameter pack. */
18735 static tree
18736 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18737 bool *is_parameter_pack)
18739 cp_token *token;
18740 cp_parameter_declarator *parameter_declarator;
18741 tree parm;
18743 /* Assume it is a type parameter or a template parameter. */
18744 *is_non_type = false;
18745 /* Assume it not a parameter pack. */
18746 *is_parameter_pack = false;
18747 /* Peek at the next token. */
18748 token = cp_lexer_peek_token (parser->lexer);
18749 /* If it is `template', we have a type-parameter. */
18750 if (token->keyword == RID_TEMPLATE)
18751 return cp_parser_type_parameter (parser, is_parameter_pack);
18752 /* If it is `class' or `typename' we do not know yet whether it is a
18753 type parameter or a non-type parameter. Consider:
18755 template <typename T, typename T::X X> ...
18759 template <class C, class D*> ...
18761 Here, the first parameter is a type parameter, and the second is
18762 a non-type parameter. We can tell by looking at the token after
18763 the identifier -- if it is a `,', `=', or `>' then we have a type
18764 parameter. */
18765 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18767 /* Peek at the token after `class' or `typename'. */
18768 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18769 /* If it's an ellipsis, we have a template type parameter
18770 pack. */
18771 if (token->type == CPP_ELLIPSIS)
18772 return cp_parser_type_parameter (parser, is_parameter_pack);
18773 /* If it's an identifier, skip it. */
18774 if (token->type == CPP_NAME)
18775 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18776 /* Now, see if the token looks like the end of a template
18777 parameter. */
18778 if (token->type == CPP_COMMA
18779 || token->type == CPP_EQ
18780 || token->type == CPP_GREATER)
18781 return cp_parser_type_parameter (parser, is_parameter_pack);
18784 /* Otherwise, it is a non-type parameter or a constrained parameter.
18786 [temp.param]
18788 When parsing a default template-argument for a non-type
18789 template-parameter, the first non-nested `>' is taken as the end
18790 of the template parameter-list rather than a greater-than
18791 operator. */
18792 parameter_declarator
18793 = cp_parser_parameter_declaration (parser,
18794 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18795 /*template_parm_p=*/true,
18796 /*parenthesized_p=*/NULL);
18798 if (!parameter_declarator)
18799 return error_mark_node;
18801 /* If the parameter declaration is marked as a parameter pack, set
18802 *IS_PARAMETER_PACK to notify the caller. */
18803 if (parameter_declarator->template_parameter_pack_p)
18804 *is_parameter_pack = true;
18806 if (parameter_declarator->default_argument)
18808 /* Can happen in some cases of erroneous input (c++/34892). */
18809 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18810 /* Consume the `...' for better error recovery. */
18811 cp_lexer_consume_token (parser->lexer);
18814 /* The parameter may have been constrained type parameter. */
18815 if (is_constrained_parameter (parameter_declarator))
18816 return finish_constrained_parameter (parser,
18817 parameter_declarator,
18818 is_non_type);
18820 // Now we're sure that the parameter is a non-type parameter.
18821 *is_non_type = true;
18823 parm = grokdeclarator (parameter_declarator->declarator,
18824 &parameter_declarator->decl_specifiers,
18825 TPARM, /*initialized=*/0,
18826 /*attrlist=*/NULL);
18827 if (parm == error_mark_node)
18828 return error_mark_node;
18830 return build_tree_list (parameter_declarator->default_argument, parm);
18833 /* Parse a type-parameter.
18835 type-parameter:
18836 class identifier [opt]
18837 class identifier [opt] = type-id
18838 typename identifier [opt]
18839 typename identifier [opt] = type-id
18840 template < template-parameter-list > class identifier [opt]
18841 template < template-parameter-list > class identifier [opt]
18842 = id-expression
18844 GNU Extension (variadic templates):
18846 type-parameter:
18847 class ... identifier [opt]
18848 typename ... identifier [opt]
18850 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18851 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18852 the declaration of the parameter.
18854 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18856 static tree
18857 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18859 cp_token *token;
18860 tree parameter;
18862 /* Look for a keyword to tell us what kind of parameter this is. */
18863 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18864 if (!token)
18865 return error_mark_node;
18867 switch (token->keyword)
18869 case RID_CLASS:
18870 case RID_TYPENAME:
18872 tree identifier;
18873 tree default_argument;
18875 /* If the next token is an ellipsis, we have a template
18876 argument pack. */
18877 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18879 /* Consume the `...' token. */
18880 cp_lexer_consume_token (parser->lexer);
18881 maybe_warn_variadic_templates ();
18883 *is_parameter_pack = true;
18886 /* If the next token is an identifier, then it names the
18887 parameter. */
18888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18889 identifier = cp_parser_identifier (parser);
18890 else
18891 identifier = NULL_TREE;
18893 /* Create the parameter. */
18894 parameter = finish_template_type_parm (class_type_node, identifier);
18896 /* If the next token is an `=', we have a default argument. */
18897 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18899 default_argument
18900 = cp_parser_default_type_template_argument (parser);
18902 /* Template parameter packs cannot have default
18903 arguments. */
18904 if (*is_parameter_pack)
18906 if (identifier)
18907 error_at (token->location,
18908 "template parameter pack %qD cannot have a "
18909 "default argument", identifier);
18910 else
18911 error_at (token->location,
18912 "template parameter packs cannot have "
18913 "default arguments");
18914 default_argument = NULL_TREE;
18916 else if (check_for_bare_parameter_packs (default_argument))
18917 default_argument = error_mark_node;
18919 else
18920 default_argument = NULL_TREE;
18922 /* Create the combined representation of the parameter and the
18923 default argument. */
18924 parameter = build_tree_list (default_argument, parameter);
18926 break;
18928 case RID_TEMPLATE:
18930 tree identifier;
18931 tree default_argument;
18933 /* Look for the `<'. */
18934 cp_parser_require (parser, CPP_LESS, RT_LESS);
18935 /* Parse the template-parameter-list. */
18936 cp_parser_template_parameter_list (parser);
18937 /* Look for the `>'. */
18938 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18940 /* If template requirements are present, parse them. */
18941 if (flag_concepts)
18943 tree reqs = get_shorthand_constraints (current_template_parms);
18944 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18945 reqs = combine_constraint_expressions (reqs, dreqs);
18946 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18949 /* Look for the `class' or 'typename' keywords. */
18950 cp_parser_type_parameter_key (parser);
18951 /* If the next token is an ellipsis, we have a template
18952 argument pack. */
18953 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18955 /* Consume the `...' token. */
18956 cp_lexer_consume_token (parser->lexer);
18957 maybe_warn_variadic_templates ();
18959 *is_parameter_pack = true;
18961 /* If the next token is an `=', then there is a
18962 default-argument. If the next token is a `>', we are at
18963 the end of the parameter-list. If the next token is a `,',
18964 then we are at the end of this parameter. */
18965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18966 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18967 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18969 identifier = cp_parser_identifier (parser);
18970 /* Treat invalid names as if the parameter were nameless. */
18971 if (identifier == error_mark_node)
18972 identifier = NULL_TREE;
18974 else
18975 identifier = NULL_TREE;
18977 /* Create the template parameter. */
18978 parameter = finish_template_template_parm (class_type_node,
18979 identifier);
18981 /* If the next token is an `=', then there is a
18982 default-argument. */
18983 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18985 default_argument
18986 = cp_parser_default_template_template_argument (parser);
18988 /* Template parameter packs cannot have default
18989 arguments. */
18990 if (*is_parameter_pack)
18992 if (identifier)
18993 error_at (token->location,
18994 "template parameter pack %qD cannot "
18995 "have a default argument",
18996 identifier);
18997 else
18998 error_at (token->location, "template parameter packs cannot "
18999 "have default arguments");
19000 default_argument = NULL_TREE;
19003 else
19004 default_argument = NULL_TREE;
19006 /* Create the combined representation of the parameter and the
19007 default argument. */
19008 parameter = build_tree_list (default_argument, parameter);
19010 break;
19012 default:
19013 gcc_unreachable ();
19014 break;
19017 return parameter;
19020 /* Parse a template-id.
19022 template-id:
19023 template-name < template-argument-list [opt] >
19025 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
19026 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
19027 returned. Otherwise, if the template-name names a function, or set
19028 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
19029 names a class, returns a TYPE_DECL for the specialization.
19031 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
19032 uninstantiated templates. */
19034 static tree
19035 cp_parser_template_id (cp_parser *parser,
19036 bool template_keyword_p,
19037 bool check_dependency_p,
19038 enum tag_types tag_type,
19039 bool is_declaration)
19041 tree templ;
19042 tree arguments;
19043 tree template_id;
19044 cp_token_position start_of_id = 0;
19045 cp_token *next_token = NULL, *next_token_2 = NULL;
19046 bool is_identifier;
19048 /* If the next token corresponds to a template-id, there is no need
19049 to reparse it. */
19050 cp_token *token = cp_lexer_peek_token (parser->lexer);
19052 if (token->type == CPP_TEMPLATE_ID)
19054 cp_lexer_consume_token (parser->lexer);
19055 return saved_checks_value (token->u.tree_check_value);
19058 /* Avoid performing name lookup if there is no possibility of
19059 finding a template-id. */
19060 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
19061 || (token->type == CPP_NAME
19062 && !cp_parser_nth_token_starts_template_argument_list_p
19063 (parser, 2)))
19065 cp_parser_error (parser, "expected template-id");
19066 return error_mark_node;
19069 /* Remember where the template-id starts. */
19070 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19071 start_of_id = cp_lexer_token_position (parser->lexer, false);
19073 push_deferring_access_checks (dk_deferred);
19075 /* Parse the template-name. */
19076 is_identifier = false;
19077 templ = cp_parser_template_name (parser, template_keyword_p,
19078 check_dependency_p,
19079 is_declaration,
19080 tag_type,
19081 &is_identifier);
19083 /* Push any access checks inside the firewall we're about to create. */
19084 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
19085 pop_deferring_access_checks ();
19086 if (templ == error_mark_node || is_identifier)
19087 return templ;
19089 /* Since we're going to preserve any side-effects from this parse, set up a
19090 firewall to protect our callers from cp_parser_commit_to_tentative_parse
19091 in the template arguments. */
19092 tentative_firewall firewall (parser);
19093 reopen_deferring_access_checks (checks);
19095 /* If we find the sequence `[:' after a template-name, it's probably
19096 a digraph-typo for `< ::'. Substitute the tokens and check if we can
19097 parse correctly the argument list. */
19098 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
19099 == CPP_OPEN_SQUARE)
19100 && next_token->flags & DIGRAPH
19101 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
19102 == CPP_COLON)
19103 && !(next_token_2->flags & PREV_WHITE))
19105 cp_parser_parse_tentatively (parser);
19106 /* Change `:' into `::'. */
19107 next_token_2->type = CPP_SCOPE;
19108 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
19109 CPP_LESS. */
19110 cp_lexer_consume_token (parser->lexer);
19112 /* Parse the arguments. */
19113 arguments = cp_parser_enclosed_template_argument_list (parser);
19114 if (!cp_parser_parse_definitely (parser))
19116 /* If we couldn't parse an argument list, then we revert our changes
19117 and return simply an error. Maybe this is not a template-id
19118 after all. */
19119 next_token_2->type = CPP_COLON;
19120 cp_parser_error (parser, "expected %<<%>");
19121 pop_deferring_access_checks ();
19122 return error_mark_node;
19124 /* Otherwise, emit an error about the invalid digraph, but continue
19125 parsing because we got our argument list. */
19126 if (permerror (next_token->location,
19127 "%<<::%> cannot begin a template-argument list"))
19129 static bool hint = false;
19130 inform (next_token->location,
19131 "%<<:%> is an alternate spelling for %<[%>."
19132 " Insert whitespace between %<<%> and %<::%>");
19133 if (!hint && !flag_permissive)
19135 inform (next_token->location, "(if you use %<-fpermissive%> "
19136 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
19137 "accept your code)");
19138 hint = true;
19142 else
19144 /* Look for the `<' that starts the template-argument-list. */
19145 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19147 pop_deferring_access_checks ();
19148 return error_mark_node;
19150 /* Parse the arguments. */
19151 arguments = cp_parser_enclosed_template_argument_list (parser);
19153 if ((cxx_dialect > cxx17)
19154 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
19155 && !template_keyword_p
19156 && (cp_parser_error_occurred (parser)
19157 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
19159 /* This didn't go well. */
19160 if (TREE_CODE (templ) == FUNCTION_DECL)
19162 /* C++20 says that "function-name < a;" is now ill-formed. */
19163 if (cp_parser_error_occurred (parser))
19165 error_at (token->location, "invalid template-argument-list");
19166 inform (token->location, "function name as the left hand "
19167 "operand of %<<%> is ill-formed in C++20; wrap the "
19168 "function name in %<()%>");
19170 else
19171 /* We expect "f<targs>" to be followed by "(args)". */
19172 error_at (cp_lexer_peek_token (parser->lexer)->location,
19173 "expected %<(%> after template-argument-list");
19174 if (start_of_id)
19175 /* Purge all subsequent tokens. */
19176 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19178 else
19179 cp_parser_simulate_error (parser);
19180 pop_deferring_access_checks ();
19181 return error_mark_node;
19185 /* Set the location to be of the form:
19186 template-name < template-argument-list [opt] >
19187 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19188 with caret == start at the start of the template-name,
19189 ranging until the closing '>'. */
19190 location_t combined_loc
19191 = make_location (token->location, token->location, parser->lexer);
19193 /* Check for concepts autos where they don't belong. We could
19194 identify types in some cases of identifier TEMPL, looking ahead
19195 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
19196 types. We reject them in functions, but if what we have is an
19197 identifier, even with none_type we can't conclude it's NOT a
19198 type, we have to wait for template substitution. */
19199 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
19200 template_id = error_mark_node;
19201 /* Build a representation of the specialization. */
19202 else if (identifier_p (templ))
19203 template_id = build_min_nt_loc (combined_loc,
19204 TEMPLATE_ID_EXPR,
19205 templ, arguments);
19206 else if (DECL_TYPE_TEMPLATE_P (templ)
19207 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
19209 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
19210 template (rather than some instantiation thereof) only if
19211 is not nested within some other construct. For example, in
19212 "template <typename T> void f(T) { A<T>::", A<T> is just an
19213 instantiation of A. */
19214 bool entering_scope
19215 = (template_parm_scope_p ()
19216 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
19217 template_id
19218 = finish_template_type (templ, arguments, entering_scope);
19220 else if (concept_definition_p (templ))
19222 /* The caller will decide whether this is a concept check or type
19223 constraint. */
19224 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
19225 boolean_type_node, templ, arguments);
19227 else if (variable_template_p (templ))
19229 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
19230 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19231 SET_EXPR_LOCATION (template_id, combined_loc);
19233 else if (TREE_CODE (templ) == TYPE_DECL
19234 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
19236 /* Some type template in dependent scope. */
19237 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
19238 name = build_min_nt_loc (combined_loc,
19239 TEMPLATE_ID_EXPR,
19240 name, arguments);
19241 template_id = templ;
19243 else
19245 /* If it's not a class-template or a template-template, it should be
19246 a function-template. */
19247 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
19249 template_id = lookup_template_function (templ, arguments);
19250 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19251 SET_EXPR_LOCATION (template_id, combined_loc);
19254 /* If parsing tentatively, replace the sequence of tokens that makes
19255 up the template-id with a CPP_TEMPLATE_ID token. That way,
19256 should we re-parse the token stream, we will not have to repeat
19257 the effort required to do the parse, nor will we issue duplicate
19258 error messages about problems during instantiation of the
19259 template. */
19260 if (start_of_id
19261 /* Don't do this if we had a parse error in a declarator; re-parsing
19262 might succeed if a name changes meaning (60361). */
19263 && !(cp_parser_error_occurred (parser)
19264 && cp_parser_parsing_tentatively (parser)
19265 && parser->in_declarator_p))
19267 /* Reset the contents of the START_OF_ID token. */
19268 token->type = CPP_TEMPLATE_ID;
19269 token->location = combined_loc;
19271 /* Retrieve any deferred checks. Do not pop this access checks yet
19272 so the memory will not be reclaimed during token replacing below. */
19273 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
19274 token->tree_check_p = true;
19275 token->u.tree_check_value->value = template_id;
19276 token->u.tree_check_value->checks = get_deferred_access_checks ();
19277 token->keyword = RID_MAX;
19279 /* Purge all subsequent tokens. */
19280 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19282 /* ??? Can we actually assume that, if template_id ==
19283 error_mark_node, we will have issued a diagnostic to the
19284 user, as opposed to simply marking the tentative parse as
19285 failed? */
19286 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
19287 error_at (token->location, "parse error in template argument list");
19290 pop_to_parent_deferring_access_checks ();
19291 return template_id;
19294 /* Like cp_parser_template_id, called in non-type context. */
19296 static tree
19297 cp_parser_template_id_expr (cp_parser *parser,
19298 bool template_keyword_p,
19299 bool check_dependency_p,
19300 bool is_declaration)
19302 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
19303 none_type, is_declaration);
19304 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
19305 && concept_check_p (x))
19306 /* We didn't check the arguments in cp_parser_template_id; do that now. */
19307 return build_concept_id (x);
19308 return x;
19311 /* Parse a template-name.
19313 template-name:
19314 identifier
19316 The standard should actually say:
19318 template-name:
19319 identifier
19320 operator-function-id
19322 A defect report has been filed about this issue.
19324 A conversion-function-id cannot be a template name because they cannot
19325 be part of a template-id. In fact, looking at this code:
19327 a.operator K<int>()
19329 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
19330 It is impossible to call a templated conversion-function-id with an
19331 explicit argument list, since the only allowed template parameter is
19332 the type to which it is converting.
19334 If TEMPLATE_KEYWORD_P is true, then we have just seen the
19335 `template' keyword, in a construction like:
19337 T::template f<3>()
19339 In that case `f' is taken to be a template-name, even though there
19340 is no way of knowing for sure.
19342 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
19343 name refers to a set of overloaded functions, at least one of which
19344 is a template, or an IDENTIFIER_NODE with the name of the template,
19345 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
19346 names are looked up inside uninstantiated templates. */
19348 static tree
19349 cp_parser_template_name (cp_parser* parser,
19350 bool template_keyword_p,
19351 bool check_dependency_p,
19352 bool is_declaration,
19353 enum tag_types tag_type,
19354 bool *is_identifier)
19356 tree identifier;
19357 tree decl;
19358 cp_token *token = cp_lexer_peek_token (parser->lexer);
19360 /* If the next token is `operator', then we have either an
19361 operator-function-id or a conversion-function-id. */
19362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
19364 /* We don't know whether we're looking at an
19365 operator-function-id or a conversion-function-id. */
19366 cp_parser_parse_tentatively (parser);
19367 /* Try an operator-function-id. */
19368 identifier = cp_parser_operator_function_id (parser);
19369 /* If that didn't work, try a conversion-function-id. */
19370 if (!cp_parser_parse_definitely (parser))
19372 cp_parser_error (parser, "expected template-name");
19373 return error_mark_node;
19376 /* Look for the identifier. */
19377 else
19378 identifier = cp_parser_identifier (parser);
19380 /* If we didn't find an identifier, we don't have a template-id. */
19381 if (identifier == error_mark_node)
19382 return error_mark_node;
19384 /* If the name immediately followed the `template' keyword, then it
19385 is a template-name. However, if the next token is not `<', then
19386 we do not treat it as a template-name, since it is not being used
19387 as part of a template-id. This enables us to handle constructs
19388 like:
19390 template <typename T> struct S { S(); };
19391 template <typename T> S<T>::S();
19393 correctly. We would treat `S' as a template -- if it were `S<T>'
19394 -- but we do not if there is no `<'. */
19396 if (processing_template_decl
19397 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19399 /* In a declaration, in a dependent context, we pretend that the
19400 "template" keyword was present in order to improve error
19401 recovery. For example, given:
19403 template <typename T> void f(T::X<int>);
19405 we want to treat "X<int>" as a template-id. */
19406 if (is_declaration
19407 && !template_keyword_p
19408 && parser->scope && TYPE_P (parser->scope)
19409 && check_dependency_p
19410 && dependent_scope_p (parser->scope)
19411 /* Do not do this for dtors (or ctors), since they never
19412 need the template keyword before their name. */
19413 && !constructor_name_p (identifier, parser->scope))
19415 cp_token_position start = 0;
19417 /* Explain what went wrong. */
19418 error_at (token->location, "non-template %qD used as template",
19419 identifier);
19420 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
19421 parser->scope, identifier);
19422 /* If parsing tentatively, find the location of the "<" token. */
19423 if (cp_parser_simulate_error (parser))
19424 start = cp_lexer_token_position (parser->lexer, true);
19425 /* Parse the template arguments so that we can issue error
19426 messages about them. */
19427 cp_lexer_consume_token (parser->lexer);
19428 cp_parser_enclosed_template_argument_list (parser);
19429 /* Skip tokens until we find a good place from which to
19430 continue parsing. */
19431 cp_parser_skip_to_closing_parenthesis (parser,
19432 /*recovering=*/true,
19433 /*or_comma=*/true,
19434 /*consume_paren=*/false);
19435 /* If parsing tentatively, permanently remove the
19436 template argument list. That will prevent duplicate
19437 error messages from being issued about the missing
19438 "template" keyword. */
19439 if (start)
19440 cp_lexer_purge_tokens_after (parser->lexer, start);
19441 if (is_identifier)
19442 *is_identifier = true;
19443 parser->context->object_type = NULL_TREE;
19444 return identifier;
19447 /* If the "template" keyword is present, then there is generally
19448 no point in doing name-lookup, so we just return IDENTIFIER.
19449 But, if the qualifying scope is non-dependent then we can
19450 (and must) do name-lookup normally. */
19451 if (template_keyword_p)
19453 tree scope = (parser->scope ? parser->scope
19454 : parser->context->object_type);
19455 if (scope && TYPE_P (scope)
19456 && (!CLASS_TYPE_P (scope)
19457 || (check_dependency_p && dependent_scope_p (scope))))
19459 /* We're optimizing away the call to cp_parser_lookup_name, but
19460 we still need to do this. */
19461 parser->object_scope = parser->context->object_type;
19462 parser->context->object_type = NULL_TREE;
19463 return identifier;
19468 /* cp_parser_lookup_name clears OBJECT_TYPE. */
19469 tree scope = (parser->scope ? parser->scope
19470 : parser->context->object_type);
19472 /* Look up the name. */
19473 decl = cp_parser_lookup_name (parser, identifier,
19474 tag_type,
19475 /*is_template=*/1 + template_keyword_p,
19476 /*is_namespace=*/false,
19477 check_dependency_p,
19478 /*ambiguous_decls=*/NULL,
19479 token->location);
19481 decl = strip_using_decl (decl);
19483 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19484 template-argument-list if it follows a name that is not a
19485 conversion-function-id and
19486 - that follows the keyword template or a ~ after a nested-name-specifier or
19487 in a class member access expression, or
19488 - for which name lookup finds the injected-class-name of a class template
19489 or finds any declaration of a template, or
19490 - that is an unqualified name for which name lookup either finds one or
19491 more functions or finds nothing, or
19492 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19493 (9.3.4), or in a type-only context other than a nested-name-specifier
19494 (13.8). */
19496 /* Handle injected-class-name. */
19497 decl = maybe_get_template_decl_from_type_decl (decl);
19499 /* If DECL is a template, then the name was a template-name. */
19500 if (TREE_CODE (decl) == TEMPLATE_DECL)
19502 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19503 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19505 tree d = DECL_TEMPLATE_RESULT (decl);
19506 tree attr;
19507 if (TREE_CODE (d) == TYPE_DECL)
19508 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19509 else
19510 attr = DECL_ATTRIBUTES (d);
19511 if (TREE_UNAVAILABLE (decl))
19513 attr = lookup_attribute ("unavailable", attr);
19514 error_unavailable_use (decl, attr);
19516 else if (TREE_DEPRECATED (decl)
19517 && deprecated_state != DEPRECATED_SUPPRESS)
19519 attr = lookup_attribute ("deprecated", attr);
19520 warn_deprecated_use (decl, attr);
19524 else
19526 /* Look through an overload set for any templates. */
19527 bool found = false;
19529 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19530 !found && iter; ++iter)
19531 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19532 found = true;
19534 /* "an unqualified name for which name lookup either finds one or more
19535 functions or finds nothing". */
19536 if (!found
19537 && (cxx_dialect > cxx17)
19538 && !scope
19539 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19540 && tag_type == none_type)
19542 /* The "more functions" case. Just use the OVERLOAD as normally.
19543 We don't use is_overloaded_fn here to avoid considering
19544 BASELINKs. */
19545 if (TREE_CODE (decl) == OVERLOAD
19546 /* Name lookup found one function. */
19547 || TREE_CODE (decl) == FUNCTION_DECL
19548 /* Name lookup found nothing. */
19549 || decl == error_mark_node)
19550 found = true;
19553 /* "that follows the keyword template"..."in a type-only context" */
19554 if (!found && scope
19555 && (template_keyword_p || tag_type != none_type)
19556 && dependentish_scope_p (scope)
19557 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19558 found = true;
19560 if (!found)
19562 /* The name does not name a template. */
19563 cp_parser_error (parser, "expected template-name");
19564 return error_mark_node;
19566 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19567 || TREE_CODE (decl) == USING_DECL
19568 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19569 || (TREE_CODE (decl) == TYPE_DECL
19570 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19571 /* Repeat the lookup at instantiation time. */
19572 decl = identifier;
19575 return decl;
19578 /* Parse a template-argument-list.
19580 template-argument-list:
19581 template-argument ... [opt]
19582 template-argument-list , template-argument ... [opt]
19584 Returns a TREE_VEC containing the arguments. */
19586 static tree
19587 cp_parser_template_argument_list (cp_parser* parser)
19589 bool saved_in_template_argument_list_p;
19590 bool saved_ice_p;
19591 bool saved_non_ice_p;
19593 /* Don't create location wrapper nodes within a template-argument-list. */
19594 auto_suppress_location_wrappers sentinel;
19596 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19597 parser->in_template_argument_list_p = true;
19598 /* Even if the template-id appears in an integral
19599 constant-expression, the contents of the argument list do
19600 not. */
19601 saved_ice_p = parser->integral_constant_expression_p;
19602 parser->integral_constant_expression_p = false;
19603 saved_non_ice_p = parser->non_integral_constant_expression_p;
19604 parser->non_integral_constant_expression_p = false;
19606 /* Parse the arguments. */
19607 auto_vec<tree, 10> args;
19610 if (!args.is_empty ())
19611 /* Consume the comma. */
19612 cp_lexer_consume_token (parser->lexer);
19614 /* Parse the template-argument. */
19615 tree argument = cp_parser_template_argument (parser);
19617 /* If the next token is an ellipsis, we're expanding a template
19618 argument pack. */
19619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19621 if (argument == error_mark_node)
19623 cp_token *token = cp_lexer_peek_token (parser->lexer);
19624 error_at (token->location,
19625 "expected parameter pack before %<...%>");
19627 /* Consume the `...' token. */
19628 cp_lexer_consume_token (parser->lexer);
19630 /* Make the argument into a TYPE_PACK_EXPANSION or
19631 EXPR_PACK_EXPANSION. */
19632 argument = make_pack_expansion (argument);
19635 args.safe_push (argument);
19637 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19639 int n_args = args.length ();
19640 tree vec = make_tree_vec (n_args);
19642 for (int i = 0; i < n_args; i++)
19643 TREE_VEC_ELT (vec, i) = args[i];
19645 parser->non_integral_constant_expression_p = saved_non_ice_p;
19646 parser->integral_constant_expression_p = saved_ice_p;
19647 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19648 if (CHECKING_P)
19649 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19650 return vec;
19653 /* Parse a template-argument.
19655 template-argument:
19656 assignment-expression
19657 type-id
19658 id-expression
19660 The representation is that of an assignment-expression, type-id, or
19661 id-expression -- except that the qualified id-expression is
19662 evaluated, so that the value returned is either a DECL or an
19663 OVERLOAD.
19665 Although the standard says "assignment-expression", it forbids
19666 throw-expressions or assignments in the template argument.
19667 Therefore, we use "conditional-expression" instead. */
19669 static tree
19670 cp_parser_template_argument (cp_parser* parser)
19672 tree argument;
19673 bool template_p;
19674 bool address_p;
19675 bool maybe_type_id = false;
19676 cp_token *token = NULL, *argument_start_token = NULL;
19677 location_t loc = 0;
19678 cp_id_kind idk;
19680 /* There's really no way to know what we're looking at, so we just
19681 try each alternative in order.
19683 [temp.arg]
19685 In a template-argument, an ambiguity between a type-id and an
19686 expression is resolved to a type-id, regardless of the form of
19687 the corresponding template-parameter.
19689 Therefore, we try a type-id first. */
19690 cp_parser_parse_tentatively (parser);
19691 argument = cp_parser_template_type_arg (parser);
19692 /* If there was no error parsing the type-id but the next token is a
19693 '>>', our behavior depends on which dialect of C++ we're
19694 parsing. In C++98, we probably found a typo for '> >'. But there
19695 are type-id which are also valid expressions. For instance:
19697 struct X { int operator >> (int); };
19698 template <int V> struct Foo {};
19699 Foo<X () >> 5> r;
19701 Here 'X()' is a valid type-id of a function type, but the user just
19702 wanted to write the expression "X() >> 5". Thus, we remember that we
19703 found a valid type-id, but we still try to parse the argument as an
19704 expression to see what happens.
19706 In C++0x, the '>>' will be considered two separate '>'
19707 tokens. */
19708 if (!cp_parser_error_occurred (parser)
19709 && ((cxx_dialect == cxx98
19710 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19711 /* Similarly for >= which
19712 cp_parser_next_token_ends_template_argument_p treats for
19713 diagnostics purposes as mistyped > =, but can be valid
19714 after a type-id. */
19715 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19717 maybe_type_id = true;
19718 cp_parser_abort_tentative_parse (parser);
19720 else
19722 /* If the next token isn't a `,' or a `>', then this argument wasn't
19723 really finished. This means that the argument is not a valid
19724 type-id. */
19725 if (!cp_parser_next_token_ends_template_argument_p (parser))
19726 cp_parser_error (parser, "expected template-argument");
19727 /* If that worked, we're done. */
19728 if (cp_parser_parse_definitely (parser))
19729 return argument;
19731 /* We're still not sure what the argument will be. */
19732 cp_parser_parse_tentatively (parser);
19733 /* Try a template. */
19734 argument_start_token = cp_lexer_peek_token (parser->lexer);
19735 argument = cp_parser_id_expression (parser,
19736 /*template_keyword_p=*/false,
19737 /*check_dependency_p=*/true,
19738 &template_p,
19739 /*declarator_p=*/false,
19740 /*optional_p=*/false);
19741 /* If the next token isn't a `,' or a `>', then this argument wasn't
19742 really finished. */
19743 if (!cp_parser_next_token_ends_template_argument_p (parser))
19744 cp_parser_error (parser, "expected template-argument");
19745 if (!cp_parser_error_occurred (parser))
19747 /* Figure out what is being referred to. If the id-expression
19748 was for a class template specialization, then we will have a
19749 TYPE_DECL at this point. There is no need to do name lookup
19750 at this point in that case. */
19751 if (TREE_CODE (argument) != TYPE_DECL)
19752 argument = cp_parser_lookup_name (parser, argument,
19753 none_type,
19754 /*is_template=*/template_p,
19755 /*is_namespace=*/false,
19756 /*check_dependency=*/true,
19757 /*ambiguous_decls=*/NULL,
19758 argument_start_token->location);
19759 if (TREE_CODE (argument) != TEMPLATE_DECL
19760 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19761 cp_parser_error (parser, "expected template-name");
19763 if (cp_parser_parse_definitely (parser))
19765 if (TREE_UNAVAILABLE (argument))
19766 error_unavailable_use (argument, NULL_TREE);
19767 else if (TREE_DEPRECATED (argument))
19768 warn_deprecated_use (argument, NULL_TREE);
19769 return argument;
19771 /* It must be a non-type argument. In C++17 any constant-expression is
19772 allowed. */
19773 if (cxx_dialect > cxx14)
19774 goto general_expr;
19776 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19778 -- an integral constant-expression of integral or enumeration
19779 type; or
19781 -- the name of a non-type template-parameter; or
19783 -- the name of an object or function with external linkage...
19785 -- the address of an object or function with external linkage...
19787 -- a pointer to member... */
19788 /* Look for a non-type template parameter. */
19789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19791 cp_parser_parse_tentatively (parser);
19792 argument = cp_parser_primary_expression (parser,
19793 /*address_p=*/false,
19794 /*cast_p=*/false,
19795 /*template_arg_p=*/true,
19796 &idk);
19797 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19798 || !cp_parser_next_token_ends_template_argument_p (parser))
19799 cp_parser_simulate_error (parser);
19800 if (cp_parser_parse_definitely (parser))
19801 return argument;
19804 /* If the next token is "&", the argument must be the address of an
19805 object or function with external linkage. */
19806 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19807 if (address_p)
19809 loc = cp_lexer_peek_token (parser->lexer)->location;
19810 cp_lexer_consume_token (parser->lexer);
19812 /* See if we might have an id-expression. */
19813 token = cp_lexer_peek_token (parser->lexer);
19814 if (token->type == CPP_NAME
19815 || token->keyword == RID_OPERATOR
19816 || token->type == CPP_SCOPE
19817 || token->type == CPP_TEMPLATE_ID
19818 || token->type == CPP_NESTED_NAME_SPECIFIER)
19820 cp_parser_parse_tentatively (parser);
19821 argument = cp_parser_primary_expression (parser,
19822 address_p,
19823 /*cast_p=*/false,
19824 /*template_arg_p=*/true,
19825 &idk);
19826 if (cp_parser_error_occurred (parser)
19827 || !cp_parser_next_token_ends_template_argument_p (parser))
19828 cp_parser_abort_tentative_parse (parser);
19829 else
19831 tree probe;
19833 if (INDIRECT_REF_P (argument))
19835 /* Strip the dereference temporarily. */
19836 gcc_assert (REFERENCE_REF_P (argument));
19837 argument = TREE_OPERAND (argument, 0);
19840 /* If we're in a template, we represent a qualified-id referring
19841 to a static data member as a SCOPE_REF even if the scope isn't
19842 dependent so that we can check access control later. */
19843 probe = argument;
19844 if (TREE_CODE (probe) == SCOPE_REF)
19845 probe = TREE_OPERAND (probe, 1);
19846 if (VAR_P (probe))
19848 /* A variable without external linkage might still be a
19849 valid constant-expression, so no error is issued here
19850 if the external-linkage check fails. */
19851 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19852 cp_parser_simulate_error (parser);
19854 else if (is_overloaded_fn (argument))
19855 /* All overloaded functions are allowed; if the external
19856 linkage test does not pass, an error will be issued
19857 later. */
19859 else if (address_p
19860 && (TREE_CODE (argument) == OFFSET_REF
19861 || TREE_CODE (argument) == SCOPE_REF))
19862 /* A pointer-to-member. */
19864 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19866 else
19867 cp_parser_simulate_error (parser);
19869 if (cp_parser_parse_definitely (parser))
19871 if (address_p)
19872 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19873 NULL_TREE, tf_warning_or_error);
19874 else
19875 argument = convert_from_reference (argument);
19876 return argument;
19880 /* If the argument started with "&", there are no other valid
19881 alternatives at this point. */
19882 if (address_p)
19884 cp_parser_error (parser, "invalid non-type template argument");
19885 return error_mark_node;
19888 general_expr:
19889 /* If the argument wasn't successfully parsed as a type-id followed
19890 by '>>', the argument can only be a constant expression now.
19891 Otherwise, we try parsing the constant-expression tentatively,
19892 because the argument could really be a type-id. */
19893 if (maybe_type_id)
19894 cp_parser_parse_tentatively (parser);
19896 if (cxx_dialect <= cxx14)
19897 argument = cp_parser_constant_expression (parser);
19898 else
19900 /* In C++20, we can encounter a braced-init-list. */
19901 if (cxx_dialect >= cxx20
19902 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19903 return cp_parser_braced_list (parser);
19905 /* With C++17 generalized non-type template arguments we need to handle
19906 lvalue constant expressions, too. */
19907 argument = cp_parser_assignment_expression (parser);
19908 require_potential_constant_expression (argument);
19911 if (!maybe_type_id)
19912 return argument;
19913 if (!cp_parser_next_token_ends_template_argument_p (parser))
19914 cp_parser_error (parser, "expected template-argument");
19915 if (cp_parser_parse_definitely (parser))
19916 return argument;
19917 /* We did our best to parse the argument as a non type-id, but that
19918 was the only alternative that matched (albeit with a '>' after
19919 it). We can assume it's just a typo from the user, and a
19920 diagnostic will then be issued. */
19921 return cp_parser_template_type_arg (parser);
19924 /* Parse an explicit-instantiation.
19926 explicit-instantiation:
19927 template declaration
19929 Although the standard says `declaration', what it really means is:
19931 explicit-instantiation:
19932 template decl-specifier-seq [opt] declarator [opt] ;
19934 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19935 supposed to be allowed. A defect report has been filed about this
19936 issue.
19938 GNU Extension:
19940 explicit-instantiation:
19941 storage-class-specifier template
19942 decl-specifier-seq [opt] declarator [opt] ;
19943 function-specifier template
19944 decl-specifier-seq [opt] declarator [opt] ; */
19946 static void
19947 cp_parser_explicit_instantiation (cp_parser* parser)
19949 int declares_class_or_enum;
19950 cp_decl_specifier_seq decl_specifiers;
19951 tree extension_specifier = NULL_TREE;
19953 auto_timevar tv (TV_TEMPLATE_INST);
19955 /* Look for an (optional) storage-class-specifier or
19956 function-specifier. */
19957 if (cp_parser_allow_gnu_extensions_p (parser))
19959 extension_specifier
19960 = cp_parser_storage_class_specifier_opt (parser);
19961 if (!extension_specifier)
19962 extension_specifier
19963 = cp_parser_function_specifier_opt (parser,
19964 /*decl_specs=*/NULL);
19967 /* Look for the `template' keyword. */
19968 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19969 /* Let the front end know that we are processing an explicit
19970 instantiation. */
19971 begin_explicit_instantiation ();
19972 /* [temp.explicit] says that we are supposed to ignore access
19973 control while processing explicit instantiation directives. */
19974 push_deferring_access_checks (dk_no_check);
19975 /* Parse a decl-specifier-seq. */
19976 cp_parser_decl_specifier_seq (parser,
19977 CP_PARSER_FLAGS_OPTIONAL,
19978 &decl_specifiers,
19979 &declares_class_or_enum);
19981 cp_omp_declare_simd_data odsd;
19982 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19983 cp_parser_handle_directive_omp_attributes (parser,
19984 &decl_specifiers.attributes,
19985 &odsd, true);
19987 /* If there was exactly one decl-specifier, and it declared a class,
19988 and there's no declarator, then we have an explicit type
19989 instantiation. */
19990 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19992 tree type = check_tag_decl (&decl_specifiers,
19993 /*explicit_type_instantiation_p=*/true);
19994 /* Turn access control back on for names used during
19995 template instantiation. */
19996 pop_deferring_access_checks ();
19997 if (type)
19998 do_type_instantiation (type, extension_specifier,
19999 /*complain=*/tf_error);
20001 else
20003 cp_declarator *declarator;
20004 tree decl;
20006 /* Parse the declarator. */
20007 declarator
20008 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20009 CP_PARSER_FLAGS_NONE,
20010 /*ctor_dtor_or_conv_p=*/NULL,
20011 /*parenthesized_p=*/NULL,
20012 /*member_p=*/false,
20013 /*friend_p=*/false,
20014 /*static_p=*/false);
20015 if (declares_class_or_enum & 2)
20016 cp_parser_check_for_definition_in_return_type (declarator,
20017 decl_specifiers.type,
20018 decl_specifiers.locations[ds_type_spec]);
20019 if (declarator != cp_error_declarator)
20021 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
20022 permerror (decl_specifiers.locations[ds_inline],
20023 "explicit instantiation shall not use"
20024 " %<inline%> specifier");
20025 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
20026 permerror (decl_specifiers.locations[ds_constexpr],
20027 "explicit instantiation shall not use"
20028 " %<constexpr%> specifier");
20029 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
20030 permerror (decl_specifiers.locations[ds_consteval],
20031 "explicit instantiation shall not use"
20032 " %<consteval%> specifier");
20034 decl = grokdeclarator (declarator, &decl_specifiers,
20035 NORMAL, 0, &decl_specifiers.attributes);
20036 /* Turn access control back on for names used during
20037 template instantiation. */
20038 pop_deferring_access_checks ();
20039 /* Do the explicit instantiation. */
20040 do_decl_instantiation (decl, extension_specifier);
20042 else
20044 pop_deferring_access_checks ();
20045 /* Skip the body of the explicit instantiation. */
20046 cp_parser_skip_to_end_of_statement (parser);
20049 /* We're done with the instantiation. */
20050 end_explicit_instantiation ();
20052 cp_parser_consume_semicolon_at_end_of_statement (parser);
20054 cp_finalize_omp_declare_simd (parser, &odsd);
20057 /* Parse an explicit-specialization.
20059 explicit-specialization:
20060 template < > declaration
20062 Although the standard says `declaration', what it really means is:
20064 explicit-specialization:
20065 template <> decl-specifier [opt] init-declarator [opt] ;
20066 template <> function-definition
20067 template <> explicit-specialization
20068 template <> template-declaration */
20070 static void
20071 cp_parser_explicit_specialization (cp_parser* parser)
20073 cp_token *token = cp_lexer_peek_token (parser->lexer);
20075 /* Look for the `template' keyword. */
20076 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
20077 /* Look for the `<'. */
20078 cp_parser_require (parser, CPP_LESS, RT_LESS);
20079 /* Look for the `>'. */
20080 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
20081 /* We have processed another parameter list. */
20082 ++parser->num_template_parameter_lists;
20084 /* [temp]
20086 A template ... explicit specialization ... shall not have C
20087 linkage. */
20088 bool need_lang_pop = current_lang_name == lang_name_c;
20089 if (need_lang_pop)
20091 error_at (token->location, "template specialization with C linkage");
20092 maybe_show_extern_c_location ();
20094 /* Give it C++ linkage to avoid confusing other parts of the
20095 front end. */
20096 push_lang_context (lang_name_cplusplus);
20099 /* Let the front end know that we are beginning a specialization. */
20100 if (begin_specialization ())
20102 /* If the next keyword is `template', we need to figure out
20103 whether or not we're looking a template-declaration. */
20104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20106 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20107 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
20108 cp_parser_template_declaration_after_export (parser,
20109 /*member_p=*/false);
20110 else
20111 cp_parser_explicit_specialization (parser);
20113 else
20114 /* Parse the dependent declaration. */
20115 cp_parser_single_declaration (parser,
20116 /*checks=*/NULL,
20117 /*member_p=*/false,
20118 /*explicit_specialization_p=*/true,
20119 /*friend_p=*/NULL);
20122 /* We're done with the specialization. */
20123 end_specialization ();
20125 /* For the erroneous case of a template with C linkage, we pushed an
20126 implicit C++ linkage scope; exit that scope now. */
20127 if (need_lang_pop)
20128 pop_lang_context ();
20130 /* We're done with this parameter list. */
20131 --parser->num_template_parameter_lists;
20134 /* Preserve the attributes across a garbage collect (by making it a GC
20135 root), which can occur when parsing a member function. */
20137 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
20139 /* Parse a type-specifier.
20141 type-specifier:
20142 simple-type-specifier
20143 class-specifier
20144 enum-specifier
20145 elaborated-type-specifier
20146 cv-qualifier
20148 GNU Extension:
20150 type-specifier:
20151 __complex__
20153 Returns a representation of the type-specifier. For a
20154 class-specifier, enum-specifier, or elaborated-type-specifier, a
20155 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
20157 The parser flags FLAGS is used to control type-specifier parsing.
20159 If IS_DECLARATION is TRUE, then this type-specifier is appearing
20160 in a decl-specifier-seq.
20162 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
20163 class-specifier, enum-specifier, or elaborated-type-specifier, then
20164 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
20165 if a type is declared; 2 if it is defined. Otherwise, it is set to
20166 zero.
20168 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
20169 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
20170 is set to FALSE. */
20172 static tree
20173 cp_parser_type_specifier (cp_parser* parser,
20174 cp_parser_flags flags,
20175 cp_decl_specifier_seq *decl_specs,
20176 bool is_declaration,
20177 int* declares_class_or_enum,
20178 bool* is_cv_qualifier)
20180 tree type_spec = NULL_TREE;
20181 cp_token *token;
20182 enum rid keyword;
20183 cp_decl_spec ds = ds_last;
20185 /* Assume this type-specifier does not declare a new type. */
20186 if (declares_class_or_enum)
20187 *declares_class_or_enum = 0;
20188 /* And that it does not specify a cv-qualifier. */
20189 if (is_cv_qualifier)
20190 *is_cv_qualifier = false;
20191 /* Peek at the next token. */
20192 token = cp_lexer_peek_token (parser->lexer);
20194 /* If we're looking at a keyword, we can use that to guide the
20195 production we choose. */
20196 keyword = token->keyword;
20197 switch (keyword)
20199 case RID_ENUM:
20200 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20201 goto elaborated_type_specifier;
20203 /* Look for the enum-specifier. */
20204 type_spec = cp_parser_enum_specifier (parser);
20205 /* If that worked, we're done. */
20206 if (type_spec)
20208 if (declares_class_or_enum)
20209 *declares_class_or_enum = 2;
20210 if (decl_specs)
20211 cp_parser_set_decl_spec_type (decl_specs,
20212 type_spec,
20213 token,
20214 /*type_definition_p=*/true);
20215 return type_spec;
20217 else
20218 goto elaborated_type_specifier;
20220 /* Any of these indicate either a class-specifier, or an
20221 elaborated-type-specifier. */
20222 case RID_CLASS:
20223 case RID_STRUCT:
20224 case RID_UNION:
20225 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20226 goto elaborated_type_specifier;
20228 /* Parse tentatively so that we can back up if we don't find a
20229 class-specifier. */
20230 cp_parser_parse_tentatively (parser);
20231 if (decl_specs->attributes)
20232 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
20233 /* Look for the class-specifier. */
20234 type_spec = cp_parser_class_specifier (parser);
20235 if (decl_specs->attributes)
20236 cp_parser_decl_specs_attrs->pop ();
20237 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
20238 /* If that worked, we're done. */
20239 if (cp_parser_parse_definitely (parser))
20241 if (declares_class_or_enum)
20242 *declares_class_or_enum = 2;
20243 if (decl_specs)
20244 cp_parser_set_decl_spec_type (decl_specs,
20245 type_spec,
20246 token,
20247 /*type_definition_p=*/true);
20248 return type_spec;
20251 /* Fall through. */
20252 elaborated_type_specifier:
20253 /* We're declaring (not defining) a class or enum. */
20254 if (declares_class_or_enum)
20255 *declares_class_or_enum = 1;
20257 /* Fall through. */
20258 case RID_TYPENAME:
20259 /* Look for an elaborated-type-specifier. */
20260 type_spec
20261 = (cp_parser_elaborated_type_specifier
20262 (parser,
20263 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
20264 is_declaration));
20265 if (decl_specs)
20266 cp_parser_set_decl_spec_type (decl_specs,
20267 type_spec,
20268 token,
20269 /*type_definition_p=*/false);
20270 return type_spec;
20272 case RID_CONST:
20273 ds = ds_const;
20274 if (is_cv_qualifier)
20275 *is_cv_qualifier = true;
20276 break;
20278 case RID_VOLATILE:
20279 ds = ds_volatile;
20280 if (is_cv_qualifier)
20281 *is_cv_qualifier = true;
20282 break;
20284 case RID_RESTRICT:
20285 ds = ds_restrict;
20286 if (is_cv_qualifier)
20287 *is_cv_qualifier = true;
20288 break;
20290 case RID_COMPLEX:
20291 /* The `__complex__' keyword is a GNU extension. */
20292 ds = ds_complex;
20293 break;
20295 default:
20296 break;
20299 /* Handle simple keywords. */
20300 if (ds != ds_last)
20302 if (decl_specs)
20304 set_and_check_decl_spec_loc (decl_specs, ds, token);
20305 decl_specs->any_specifiers_p = true;
20307 return cp_lexer_consume_token (parser->lexer)->u.value;
20310 /* If we do not already have a type-specifier, assume we are looking
20311 at a simple-type-specifier. */
20312 type_spec = cp_parser_simple_type_specifier (parser,
20313 decl_specs,
20314 flags);
20316 /* If we didn't find a type-specifier, and a type-specifier was not
20317 optional in this context, issue an error message. */
20318 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20320 cp_parser_error (parser, "expected type specifier");
20321 return error_mark_node;
20324 return type_spec;
20327 /* Parse a simple-type-specifier.
20329 simple-type-specifier:
20330 :: [opt] nested-name-specifier [opt] type-name
20331 :: [opt] nested-name-specifier template template-id
20332 char
20333 wchar_t
20334 bool
20335 short
20337 long
20338 signed
20339 unsigned
20340 float
20341 double
20342 void
20344 C++11 Extension:
20346 simple-type-specifier:
20347 auto
20348 decltype ( expression )
20349 char16_t
20350 char32_t
20351 __underlying_type ( type-id )
20353 C++17 extension:
20355 nested-name-specifier(opt) template-name
20357 GNU Extension:
20359 simple-type-specifier:
20360 __int128
20361 __typeof__ unary-expression
20362 __typeof__ ( type-id )
20363 __typeof__ ( type-id ) { initializer-list , [opt] }
20365 Concepts Extension:
20367 simple-type-specifier:
20368 constrained-type-specifier
20370 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
20371 appropriately updated. */
20373 static tree
20374 cp_parser_simple_type_specifier (cp_parser* parser,
20375 cp_decl_specifier_seq *decl_specs,
20376 cp_parser_flags flags)
20378 tree type = NULL_TREE;
20379 cp_token *token;
20380 int idx;
20382 /* Peek at the next token. */
20383 token = cp_lexer_peek_token (parser->lexer);
20385 /* If we're looking at a keyword, things are easy. */
20386 switch (token->keyword)
20388 case RID_CHAR:
20389 if (decl_specs)
20390 decl_specs->explicit_char_p = true;
20391 type = char_type_node;
20392 break;
20393 case RID_CHAR8:
20394 type = char8_type_node;
20395 break;
20396 case RID_CHAR16:
20397 type = char16_type_node;
20398 break;
20399 case RID_CHAR32:
20400 type = char32_type_node;
20401 break;
20402 case RID_WCHAR:
20403 type = wchar_type_node;
20404 break;
20405 case RID_BOOL:
20406 type = boolean_type_node;
20407 break;
20408 case RID_SHORT:
20409 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
20410 type = short_integer_type_node;
20411 break;
20412 case RID_INT:
20413 if (decl_specs)
20414 decl_specs->explicit_int_p = true;
20415 type = integer_type_node;
20416 break;
20417 case RID_INT_N_0:
20418 case RID_INT_N_1:
20419 case RID_INT_N_2:
20420 case RID_INT_N_3:
20421 idx = token->keyword - RID_INT_N_0;
20422 if (! int_n_enabled_p [idx])
20423 break;
20424 if (decl_specs)
20426 decl_specs->explicit_intN_p = true;
20427 decl_specs->int_n_idx = idx;
20428 /* Check if the alternate "__intN__" form has been used instead of
20429 "__intN". */
20430 if (startswith (IDENTIFIER_POINTER (token->u.value)
20431 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
20432 decl_specs->int_n_alt = true;
20434 type = int_n_trees [idx].signed_type;
20435 break;
20436 case RID_LONG:
20437 if (decl_specs)
20438 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
20439 type = long_integer_type_node;
20440 break;
20441 case RID_SIGNED:
20442 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
20443 type = integer_type_node;
20444 break;
20445 case RID_UNSIGNED:
20446 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
20447 type = unsigned_type_node;
20448 break;
20449 case RID_FLOAT:
20450 type = float_type_node;
20451 break;
20452 case RID_DOUBLE:
20453 type = double_type_node;
20454 break;
20455 CASE_RID_FLOATN_NX:
20456 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
20457 if (type == NULL_TREE)
20458 error ("%<_Float%d%s%> is not supported on this target",
20459 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
20460 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
20461 ? "x" : "");
20462 break;
20463 case RID_VOID:
20464 type = void_type_node;
20465 break;
20467 case RID_AUTO:
20468 maybe_warn_cpp0x (CPP0X_AUTO);
20469 if (parser->auto_is_implicit_function_template_parm_p)
20471 /* The 'auto' might be the placeholder return type for a function decl
20472 with trailing return type. */
20473 bool have_trailing_return_fn_decl = false;
20475 cp_parser_parse_tentatively (parser);
20476 cp_lexer_consume_token (parser->lexer);
20477 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
20478 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
20479 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20480 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
20482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20484 cp_lexer_consume_token (parser->lexer);
20485 cp_parser_skip_to_closing_parenthesis (parser,
20486 /*recovering*/false,
20487 /*or_comma*/false,
20488 /*consume_paren*/true);
20489 continue;
20492 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20494 have_trailing_return_fn_decl = true;
20495 break;
20498 cp_lexer_consume_token (parser->lexer);
20500 cp_parser_abort_tentative_parse (parser);
20502 if (have_trailing_return_fn_decl)
20504 type = make_auto ();
20505 break;
20508 if (cxx_dialect >= cxx14)
20510 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20511 type = TREE_TYPE (type);
20513 else
20514 type = error_mark_node;
20516 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20518 if (cxx_dialect < cxx14)
20519 error_at (token->location,
20520 "use of %<auto%> in lambda parameter declaration "
20521 "only available with "
20522 "%<-std=c++14%> or %<-std=gnu++14%>");
20524 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20525 pedwarn (token->location, 0,
20526 "use of %<auto%> in template argument "
20527 "only available with %<-fconcepts-ts%>");
20528 else if (!flag_concepts)
20529 pedwarn (token->location, 0,
20530 "use of %<auto%> in parameter declaration "
20531 "only available with %<-std=c++20%> or %<-fconcepts%>");
20532 else if (cxx_dialect < cxx14)
20533 error_at (token->location,
20534 "use of %<auto%> in parameter declaration "
20535 "only available with "
20536 "%<-std=c++14%> or %<-std=gnu++14%>");
20538 else
20539 type = make_auto ();
20540 break;
20542 case RID_DECLTYPE:
20543 /* Since DR 743, decltype can either be a simple-type-specifier by
20544 itself or begin a nested-name-specifier. Parsing it will replace
20545 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20546 handling below decide what to do. */
20547 cp_parser_decltype (parser);
20548 cp_lexer_set_token_position (parser->lexer, token);
20549 break;
20551 case RID_TYPEOF:
20552 /* Consume the `typeof' token. */
20553 cp_lexer_consume_token (parser->lexer);
20554 /* Parse the operand to `typeof'. */
20555 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20556 /* If it is not already a TYPE, take its type. */
20557 if (!TYPE_P (type))
20558 type = finish_typeof (type);
20560 if (decl_specs)
20561 cp_parser_set_decl_spec_type (decl_specs, type,
20562 token,
20563 /*type_definition_p=*/false);
20565 return type;
20567 default:
20568 /* If token is a type-yielding built-in traits, parse it. */
20569 const cp_trait* trait = cp_lexer_peek_trait_type (parser->lexer);
20570 if (trait)
20572 type = cp_parser_trait (parser, trait);
20573 if (decl_specs)
20574 cp_parser_set_decl_spec_type (decl_specs, type,
20575 token,
20576 /*type_definition_p=*/false);
20578 return type;
20581 break;
20584 /* If token is an already-parsed decltype not followed by ::,
20585 it's a simple-type-specifier. */
20586 if (token->type == CPP_DECLTYPE
20587 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20589 type = saved_checks_value (token->u.tree_check_value);
20590 if (decl_specs)
20592 cp_parser_set_decl_spec_type (decl_specs, type,
20593 token,
20594 /*type_definition_p=*/false);
20595 /* Remember that we are handling a decltype in order to
20596 implement the resolution of DR 1510 when the argument
20597 isn't instantiation dependent. */
20598 decl_specs->decltype_p = true;
20600 cp_lexer_consume_token (parser->lexer);
20601 return type;
20604 /* If the type-specifier was for a built-in type, we're done. */
20605 if (type)
20607 /* Record the type. */
20608 if (decl_specs
20609 && (token->keyword != RID_SIGNED
20610 && token->keyword != RID_UNSIGNED
20611 && token->keyword != RID_SHORT
20612 && token->keyword != RID_LONG))
20613 cp_parser_set_decl_spec_type (decl_specs,
20614 type,
20615 token,
20616 /*type_definition_p=*/false);
20617 if (decl_specs)
20618 decl_specs->any_specifiers_p = true;
20620 /* Consume the token. */
20621 cp_lexer_consume_token (parser->lexer);
20623 if (type == error_mark_node)
20624 return error_mark_node;
20626 /* There is no valid C++ program where a non-template type is
20627 followed by a "<". That usually indicates that the user thought
20628 that the type was a template. */
20629 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20630 token->location);
20632 return TYPE_NAME (type);
20635 /* The type-specifier must be a user-defined type. */
20636 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20638 bool qualified_p;
20639 bool global_p;
20640 const bool typename_p = (cxx_dialect >= cxx20
20641 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20643 /* Don't gobble tokens or issue error messages if this is an
20644 optional type-specifier. */
20645 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20646 cp_parser_parse_tentatively (parser);
20648 /* Remember current tentative parsing state -- if we know we need
20649 a type, we can give better diagnostics here. */
20650 bool tent = cp_parser_parsing_tentatively (parser);
20652 token = cp_lexer_peek_token (parser->lexer);
20654 /* Look for the optional `::' operator. */
20655 global_p
20656 = (cp_parser_global_scope_opt (parser,
20657 /*current_scope_valid_p=*/false)
20658 != NULL_TREE);
20659 /* Look for the nested-name specifier. */
20660 qualified_p
20661 = (cp_parser_nested_name_specifier_opt (parser,
20662 /*typename_keyword_p=*/false,
20663 /*check_dependency_p=*/true,
20664 /*type_p=*/false,
20665 /*is_declaration=*/false)
20666 != NULL_TREE);
20667 /* If we have seen a nested-name-specifier, and the next token
20668 is `template', then we are using the template-id production. */
20669 if (parser->scope
20670 && cp_parser_optional_template_keyword (parser))
20672 /* Look for the template-id. */
20673 type = cp_parser_template_id (parser,
20674 /*template_keyword_p=*/true,
20675 /*check_dependency_p=*/true,
20676 none_type,
20677 /*is_declaration=*/false);
20678 /* If the template-id did not name a type, we are out of
20679 luck. */
20680 if (TREE_CODE (type) != TYPE_DECL)
20682 /* ...unless we pretend we have seen 'typename'. */
20683 if (typename_p)
20684 type = cp_parser_make_typename_type (parser, type,
20685 token->location);
20686 else
20688 cp_parser_error (parser, "expected template-id for type");
20689 type = error_mark_node;
20693 /* DR 1812: A < following a qualified-id in a typename-specifier
20694 could safely be assumed to begin a template argument list, so
20695 the template keyword should be optional. */
20696 else if (parser->scope
20697 && qualified_p
20698 && typename_p
20699 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20701 cp_parser_parse_tentatively (parser);
20703 type = cp_parser_template_id (parser,
20704 /*template_keyword_p=*/true,
20705 /*check_dependency_p=*/true,
20706 none_type,
20707 /*is_declaration=*/false);
20708 /* This is handled below, so back off. */
20709 if (type && concept_check_p (type))
20710 cp_parser_simulate_error (parser);
20712 if (!cp_parser_parse_definitely (parser))
20713 type = NULL_TREE;
20714 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20715 type = make_typename_type (parser->scope, type, typename_type,
20716 /*complain=*/tf_error);
20717 else if (TREE_CODE (type) != TYPE_DECL)
20718 type = NULL_TREE;
20721 /* Otherwise, look for a type-name. */
20722 if (!type)
20724 if (cxx_dialect >= cxx17 || flag_concepts)
20725 cp_parser_parse_tentatively (parser);
20727 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20729 if ((cxx_dialect >= cxx17 || flag_concepts)
20730 && !cp_parser_parse_definitely (parser))
20731 type = NULL_TREE;
20734 if (!type && flag_concepts && decl_specs)
20736 /* Try for a type-constraint with template arguments. We check
20737 decl_specs here to avoid trying this for a functional cast. */
20739 cp_parser_parse_tentatively (parser);
20741 type = cp_parser_template_id (parser,
20742 /*template_keyword_p=*/false,
20743 /*check_dependency_p=*/true,
20744 none_type,
20745 /*is_declaration=*/false);
20746 if (type && concept_check_p (type))
20748 location_t loc = EXPR_LOCATION (type);
20749 type = cp_parser_placeholder_type_specifier (parser, loc,
20750 type, tent);
20751 if (tent && type == error_mark_node)
20752 /* Perhaps it's a concept-check expression. */
20753 cp_parser_simulate_error (parser);
20755 else
20756 cp_parser_simulate_error (parser);
20758 if (!cp_parser_parse_definitely (parser))
20759 type = NULL_TREE;
20762 if (!type && cxx_dialect >= cxx17)
20764 /* Try class template argument deduction or type-constraint without
20765 template arguments. */
20766 tree name = cp_parser_identifier (parser);
20767 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20768 && parser->scope != error_mark_node)
20770 location_t loc
20771 = cp_lexer_previous_token (parser->lexer)->location;
20772 tree tmpl = cp_parser_lookup_name (parser, name,
20773 none_type,
20774 /*is_template=*/false,
20775 /*is_namespace=*/false,
20776 /*check_dependency=*/true,
20777 /*ambiguous_decls=*/NULL,
20778 token->location);
20779 if (tmpl && tmpl != error_mark_node
20780 && ctad_template_p (tmpl))
20781 type = make_template_placeholder (tmpl);
20782 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20783 type = cp_parser_placeholder_type_specifier (parser, loc,
20784 tmpl, tent);
20785 else
20787 type = error_mark_node;
20788 if (!cp_parser_simulate_error (parser))
20789 cp_parser_name_lookup_error (parser, name, tmpl,
20790 NLE_TYPE, token->location);
20793 else
20794 type = error_mark_node;
20797 /* If it didn't work out, we don't have a TYPE. */
20798 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20799 && !cp_parser_parse_definitely (parser))
20800 type = NULL_TREE;
20802 /* Keep track of all name-lookups performed in class scopes. */
20803 if (type
20804 && !global_p
20805 && !qualified_p
20806 && TREE_CODE (type) == TYPE_DECL
20807 && identifier_p (DECL_NAME (type)))
20808 maybe_note_name_used_in_class (DECL_NAME (type), type);
20810 if (type && decl_specs)
20811 cp_parser_set_decl_spec_type (decl_specs, type,
20812 token,
20813 /*type_definition_p=*/false);
20816 /* If we didn't get a type-name, issue an error message. */
20817 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20819 cp_parser_error (parser, "expected type-name");
20820 return error_mark_node;
20823 if (type && type != error_mark_node)
20825 /* See if TYPE is an Objective-C type, and if so, parse and
20826 accept any protocol references following it. Do this before
20827 the cp_parser_check_for_invalid_template_id() call, because
20828 Objective-C types can be followed by '<...>' which would
20829 enclose protocol names rather than template arguments, and so
20830 everything is fine. */
20831 if (c_dialect_objc () && !parser->scope
20832 && (objc_is_id (type) || objc_is_class_name (type)))
20834 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20835 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20837 /* Clobber the "unqualified" type previously entered into
20838 DECL_SPECS with the new, improved protocol-qualified version. */
20839 if (decl_specs)
20840 decl_specs->type = qual_type;
20842 return qual_type;
20845 /* There is no valid C++ program where a non-template type is
20846 followed by a "<". That usually indicates that the user
20847 thought that the type was a template. */
20848 cp_parser_check_for_invalid_template_id (parser, type,
20849 none_type,
20850 token->location);
20853 return type;
20856 /* Parse the remainder of a placholder-type-specifier.
20858 placeholder-type-specifier:
20859 type-constraint_opt auto
20860 type-constraint_opt decltype(auto)
20862 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20863 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20864 parses the placeholder type, and performs some contextual syntactic analysis.
20866 LOC provides the location of the template name.
20868 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20869 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20870 might actually be a concept-check,
20872 Note that the Concepts TS allows the auto or decltype(auto) to be
20873 omitted in a constrained-type-specifier. */
20875 static tree
20876 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20877 tree tmpl, bool tentative)
20879 if (tmpl == error_mark_node)
20880 return error_mark_node;
20882 tree orig_tmpl = tmpl;
20884 /* Get the arguments as written for subsequent analysis. */
20885 tree args = NULL_TREE;
20886 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20888 args = TREE_OPERAND (tmpl, 1);
20889 tmpl = TREE_OPERAND (tmpl, 0);
20891 else
20892 /* A concept-name with no arguments can't be an expression. */
20893 tentative = false;
20895 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20897 /* Get the concept and prototype parameter for the constraint. */
20898 tree_pair info = finish_type_constraints (tmpl, args, complain);
20899 tree con = info.first;
20900 tree proto = info.second;
20901 if (con == error_mark_node)
20902 return error_mark_node;
20904 /* As per the standard, require auto or decltype(auto), except in some
20905 cases (template parameter lists, -fconcepts-ts enabled). */
20906 cp_token *placeholder = NULL, *close_paren = NULL;
20907 if (cxx_dialect >= cxx20)
20909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20910 placeholder = cp_lexer_consume_token (parser->lexer);
20911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20913 placeholder = cp_lexer_consume_token (parser->lexer);
20914 matching_parens parens;
20915 parens.require_open (parser);
20916 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20917 close_paren = parens.require_close (parser);
20921 /* A type constraint constrains a contextually determined type or type
20922 parameter pack. However, the Concepts TS does allow concepts
20923 to introduce non-type and template template parameters. */
20924 if (TREE_CODE (proto) != TYPE_DECL)
20926 if (!flag_concepts_ts
20927 || !processing_template_parmlist)
20929 if (!tentative)
20931 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20932 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20934 return error_mark_node;
20938 /* In a template parameter list, a type-parameter can be introduced
20939 by type-constraints alone. */
20940 if (processing_template_parmlist && !placeholder)
20942 /* In a default argument we may not be creating new parameters. */
20943 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20945 /* If this assert turns out to be false, do error() instead. */
20946 gcc_assert (tentative);
20947 return error_mark_node;
20949 return build_constrained_parameter (con, proto, args);
20952 /* Diagnose issues placeholder issues. */
20953 if (!flag_concepts_ts
20954 && !parser->in_result_type_constraint_p
20955 && !placeholder)
20957 if (tentative)
20958 /* Perhaps it's a concept-check expression (c++/91073). */
20959 return error_mark_node;
20961 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20962 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20963 error_at (input_location,
20964 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20965 /* Fall through. This is an error of omission. */
20967 else if (parser->in_result_type_constraint_p && placeholder)
20969 /* A trailing return type only allows type-constraints. */
20970 error_at (input_location,
20971 "unexpected placeholder in constrained result type");
20974 /* In a parameter-declaration-clause, a placeholder-type-specifier
20975 results in an invented template parameter. */
20976 if (parser->auto_is_implicit_function_template_parm_p)
20978 if (close_paren)
20980 location_t loc = make_location (placeholder->location,
20981 placeholder->location,
20982 close_paren->location);
20983 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20984 return error_mark_node;
20986 tree parm = build_constrained_parameter (con, proto, args);
20987 return synthesize_implicit_template_parm (parser, parm);
20990 /* Determine if the type should be deduced using template argument
20991 deduction or decltype deduction. Note that the latter is always
20992 used for type-constraints in trailing return types. */
20993 bool decltype_p = placeholder
20994 ? placeholder->keyword == RID_DECLTYPE
20995 : parser->in_result_type_constraint_p;
20997 /* Otherwise, this is the type of a variable or return type. */
20998 if (decltype_p)
20999 return make_constrained_decltype_auto (con, args);
21000 else
21001 return make_constrained_auto (con, args);
21004 /* Parse a type-name.
21006 type-name:
21007 class-name
21008 enum-name
21009 typedef-name
21010 simple-template-id [in c++0x]
21012 enum-name:
21013 identifier
21015 typedef-name:
21016 identifier
21018 Concepts:
21020 type-name:
21021 concept-name
21022 partial-concept-id
21024 concept-name:
21025 identifier
21027 Returns a TYPE_DECL for the type. */
21029 static tree
21030 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
21032 tree type_decl;
21034 /* We can't know yet whether it is a class-name or not. */
21035 cp_parser_parse_tentatively (parser);
21036 /* Try a class-name. */
21037 type_decl = cp_parser_class_name (parser,
21038 typename_keyword_p,
21039 /*template_keyword_p=*/false,
21040 none_type,
21041 /*check_dependency_p=*/true,
21042 /*class_head_p=*/false,
21043 /*is_declaration=*/false);
21044 /* If it's not a class-name, keep looking. */
21045 if (!cp_parser_parse_definitely (parser))
21047 if (cxx_dialect < cxx11)
21048 /* It must be a typedef-name or an enum-name. */
21049 return cp_parser_nonclass_name (parser);
21051 cp_parser_parse_tentatively (parser);
21052 /* It is either a simple-template-id representing an
21053 instantiation of an alias template... */
21054 type_decl = cp_parser_template_id (parser,
21055 /*template_keyword_p=*/false,
21056 /*check_dependency_p=*/true,
21057 none_type,
21058 /*is_declaration=*/false);
21059 /* Note that this must be an instantiation of an alias template
21060 because [temp.names]/6 says:
21062 A template-id that names an alias template specialization
21063 is a type-name.
21065 Whereas [temp.names]/7 says:
21067 A simple-template-id that names a class template
21068 specialization is a class-name.
21070 With concepts, this could also be a partial-concept-id that
21071 declares a non-type template parameter. */
21072 if (type_decl != NULL_TREE
21073 && TREE_CODE (type_decl) == TYPE_DECL
21074 && TYPE_DECL_ALIAS_P (type_decl))
21075 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
21076 else
21077 cp_parser_simulate_error (parser);
21079 if (!cp_parser_parse_definitely (parser))
21080 /* ... Or a typedef-name or an enum-name. */
21081 return cp_parser_nonclass_name (parser);
21084 return type_decl;
21087 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
21088 or a concept-name.
21090 enum-name:
21091 identifier
21093 typedef-name:
21094 identifier
21096 concept-name:
21097 identifier
21099 Returns a TYPE_DECL for the type. */
21101 static tree
21102 cp_parser_nonclass_name (cp_parser* parser)
21104 tree type_decl;
21105 tree identifier;
21107 cp_token *token = cp_lexer_peek_token (parser->lexer);
21108 identifier = cp_parser_identifier (parser);
21109 if (identifier == error_mark_node)
21110 return error_mark_node;
21112 /* Look up the type-name. */
21113 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
21115 type_decl = strip_using_decl (type_decl);
21117 if (TREE_CODE (type_decl) != TYPE_DECL
21118 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
21120 /* See if this is an Objective-C type. */
21121 tree protos = cp_parser_objc_protocol_refs_opt (parser);
21122 tree type = objc_get_protocol_qualified_type (identifier, protos);
21123 if (type)
21124 type_decl = TYPE_NAME (type);
21127 /* Issue an error if we did not find a type-name. */
21128 if (TREE_CODE (type_decl) != TYPE_DECL
21129 /* In Objective-C, we have the complication that class names are
21130 normally type names and start declarations (eg, the
21131 "NSObject" in "NSObject *object;"), but can be used in an
21132 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
21133 is an expression. So, a classname followed by a dot is not a
21134 valid type-name. */
21135 || (objc_is_class_name (TREE_TYPE (type_decl))
21136 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
21138 if (!cp_parser_simulate_error (parser))
21139 cp_parser_name_lookup_error (parser, identifier, type_decl,
21140 NLE_TYPE, token->location);
21141 return error_mark_node;
21143 /* Remember that the name was used in the definition of the
21144 current class so that we can check later to see if the
21145 meaning would have been different after the class was
21146 entirely defined. */
21147 else if (type_decl != error_mark_node
21148 && !parser->scope)
21149 maybe_note_name_used_in_class (identifier, type_decl);
21151 return type_decl;
21154 /* Parse an elaborated-type-specifier. Note that the grammar given
21155 here incorporates the resolution to DR68.
21157 elaborated-type-specifier:
21158 class-key :: [opt] nested-name-specifier [opt] identifier
21159 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
21160 enum-key :: [opt] nested-name-specifier [opt] identifier
21161 typename :: [opt] nested-name-specifier identifier
21162 typename :: [opt] nested-name-specifier template [opt]
21163 template-id
21165 GNU extension:
21167 elaborated-type-specifier:
21168 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21169 class-key attributes :: [opt] nested-name-specifier [opt]
21170 template [opt] template-id
21171 enum attributes :: [opt] nested-name-specifier [opt] identifier
21173 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
21174 declared `friend'. If IS_DECLARATION is TRUE, then this
21175 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
21176 something is being declared.
21178 Returns the TYPE specified. */
21180 static tree
21181 cp_parser_elaborated_type_specifier (cp_parser* parser,
21182 bool is_friend,
21183 bool is_declaration)
21185 enum tag_types tag_type;
21186 tree identifier;
21187 tree type = NULL_TREE;
21188 tree attributes = NULL_TREE;
21189 tree globalscope;
21190 cp_token *token = NULL;
21192 /* For class and enum types the location of the class-key or enum-key. */
21193 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
21194 /* For a scoped enum, the 'class' or 'struct' keyword id. */
21195 rid scoped_key = RID_MAX;
21197 /* See if we're looking at the `enum' keyword. */
21198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
21200 /* Consume the `enum' token. */
21201 cp_lexer_consume_token (parser->lexer);
21202 /* Remember that it's an enumeration type. */
21203 tag_type = enum_type;
21204 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
21205 enums) is used here. */
21206 cp_token *token = cp_lexer_peek_token (parser->lexer);
21207 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
21208 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
21210 location_t loc = token->location;
21211 gcc_rich_location richloc (loc);
21212 richloc.add_range (input_location);
21213 richloc.add_fixit_remove ();
21214 pedwarn (&richloc, 0, "elaborated-type-specifier for "
21215 "a scoped enum must not use the %qD keyword",
21216 token->u.value);
21217 /* Consume the `struct' or `class' and parse it anyway. */
21218 cp_lexer_consume_token (parser->lexer);
21219 /* Create a combined location for the whole scoped-enum-key. */
21220 key_loc = make_location (key_loc, key_loc, loc);
21222 else
21223 scoped_key = RID_MAX;
21225 /* Parse the attributes. */
21226 attributes = cp_parser_attributes_opt (parser);
21228 /* Or, it might be `typename'. */
21229 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21230 RID_TYPENAME))
21232 /* Consume the `typename' token. */
21233 cp_lexer_consume_token (parser->lexer);
21234 /* Remember that it's a `typename' type. */
21235 tag_type = typename_type;
21237 /* Otherwise it must be a class-key. */
21238 else
21240 key_loc = cp_lexer_peek_token (parser->lexer)->location;
21241 tag_type = cp_parser_class_key (parser);
21242 if (tag_type == none_type)
21243 return error_mark_node;
21244 /* Parse the attributes. */
21245 attributes = cp_parser_attributes_opt (parser);
21248 /* Look for the `::' operator. */
21249 globalscope = cp_parser_global_scope_opt (parser,
21250 /*current_scope_valid_p=*/false);
21251 /* Look for the nested-name-specifier. */
21252 tree nested_name_specifier;
21253 if (tag_type == typename_type && !globalscope)
21255 nested_name_specifier
21256 = cp_parser_nested_name_specifier (parser,
21257 /*typename_keyword_p=*/true,
21258 /*check_dependency_p=*/true,
21259 /*type_p=*/true,
21260 is_declaration);
21261 if (!nested_name_specifier)
21262 return error_mark_node;
21264 else
21265 /* Even though `typename' is not present, the proposed resolution
21266 to Core Issue 180 says that in `class A<T>::B', `B' should be
21267 considered a type-name, even if `A<T>' is dependent. */
21268 nested_name_specifier
21269 = cp_parser_nested_name_specifier_opt (parser,
21270 /*typename_keyword_p=*/true,
21271 /*check_dependency_p=*/true,
21272 /*type_p=*/true,
21273 is_declaration);
21274 /* For everything but enumeration types, consider a template-id.
21275 For an enumeration type, consider only a plain identifier. */
21276 if (tag_type != enum_type)
21278 bool template_p = false;
21279 tree decl;
21281 /* Allow the `template' keyword. */
21282 template_p = cp_parser_optional_template_keyword (parser);
21283 /* If we didn't see `template', we don't know if there's a
21284 template-id or not. */
21285 if (!template_p)
21286 cp_parser_parse_tentatively (parser);
21287 /* The `template' keyword must follow a nested-name-specifier. */
21288 else if (!nested_name_specifier && !globalscope)
21290 cp_parser_error (parser, "%<template%> must follow a nested-"
21291 "name-specifier");
21292 return error_mark_node;
21295 /* Parse the template-id. */
21296 token = cp_lexer_peek_token (parser->lexer);
21297 decl = cp_parser_template_id (parser, template_p,
21298 /*check_dependency_p=*/true,
21299 tag_type,
21300 is_declaration);
21301 /* If we didn't find a template-id, look for an ordinary
21302 identifier. */
21303 if (!template_p && !cp_parser_parse_definitely (parser))
21305 /* We can get here when cp_parser_template_id, called by
21306 cp_parser_class_name with tag_type == none_type, succeeds
21307 and caches a BASELINK. Then, when called again here,
21308 instead of failing and returning an error_mark_node
21309 returns it (see template/typename17.C in C++11).
21310 ??? Could we diagnose this earlier? */
21311 else if (tag_type == typename_type && BASELINK_P (decl))
21313 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
21314 type = error_mark_node;
21316 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
21317 in effect, then we must assume that, upon instantiation, the
21318 template will correspond to a class. */
21319 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21320 && tag_type == typename_type)
21321 type = make_typename_type (parser->scope, decl,
21322 typename_type,
21323 /*complain=*/tf_error);
21324 /* If the `typename' keyword is in effect and DECL is not a type
21325 decl, then type is non existent. */
21326 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
21328 else if (TREE_CODE (decl) == TYPE_DECL)
21330 type = check_elaborated_type_specifier (tag_type, decl,
21331 /*allow_template_p=*/true);
21333 /* If the next token is a semicolon, this must be a specialization,
21334 instantiation, or friend declaration. Check the scope while we
21335 still know whether or not we had a nested-name-specifier. */
21336 if (type != error_mark_node
21337 && !nested_name_specifier && !is_friend
21338 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21339 check_unqualified_spec_or_inst (type, token->location);
21341 else if (decl == error_mark_node)
21342 type = error_mark_node;
21345 if (!type)
21347 token = cp_lexer_peek_token (parser->lexer);
21348 identifier = cp_parser_identifier (parser);
21350 if (identifier == error_mark_node)
21352 parser->scope = NULL_TREE;
21353 return error_mark_node;
21356 /* For a `typename', we needn't call xref_tag. */
21357 if (tag_type == typename_type
21358 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21359 return cp_parser_make_typename_type (parser, identifier,
21360 token->location);
21362 /* Template parameter lists apply only if we are not within a
21363 function parameter list. */
21364 bool template_parm_lists_apply
21365 = parser->num_template_parameter_lists;
21366 if (template_parm_lists_apply)
21367 for (cp_binding_level *s = current_binding_level;
21368 s && s->kind != sk_template_parms;
21369 s = s->level_chain)
21370 if (s->kind == sk_function_parms)
21371 template_parm_lists_apply = false;
21373 /* Look up a qualified name in the usual way. */
21374 if (parser->scope)
21376 tree decl;
21377 tree ambiguous_decls;
21379 decl = cp_parser_lookup_name (parser, identifier,
21380 tag_type,
21381 /*is_template=*/false,
21382 /*is_namespace=*/false,
21383 /*check_dependency=*/true,
21384 &ambiguous_decls,
21385 token->location);
21387 /* If the lookup was ambiguous, an error will already have been
21388 issued. */
21389 if (ambiguous_decls)
21390 return error_mark_node;
21392 /* If we are parsing friend declaration, DECL may be a
21393 TEMPLATE_DECL tree node here. However, we need to check
21394 whether this TEMPLATE_DECL results in valid code. Consider
21395 the following example:
21397 namespace N {
21398 template <class T> class C {};
21400 class X {
21401 template <class T> friend class N::C; // #1, valid code
21403 template <class T> class Y {
21404 friend class N::C; // #2, invalid code
21407 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
21408 name lookup of `N::C'. We see that friend declaration must
21409 be template for the code to be valid. Note that
21410 processing_template_decl does not work here since it is
21411 always 1 for the above two cases. */
21413 decl = (cp_parser_maybe_treat_template_as_class
21414 (decl, /*tag_name_p=*/is_friend
21415 && template_parm_lists_apply));
21417 if (TREE_CODE (decl) != TYPE_DECL)
21419 cp_parser_diagnose_invalid_type_name (parser,
21420 identifier,
21421 token->location);
21422 return error_mark_node;
21425 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21427 bool allow_template = (template_parm_lists_apply
21428 || DECL_SELF_REFERENCE_P (decl));
21429 type = check_elaborated_type_specifier (tag_type, decl,
21430 allow_template);
21432 if (type == error_mark_node)
21433 return error_mark_node;
21436 /* Forward declarations of nested types, such as
21438 class C1::C2;
21439 class C1::C2::C3;
21441 are invalid unless all components preceding the final '::'
21442 are complete. If all enclosing types are complete, these
21443 declarations become merely pointless.
21445 Invalid forward declarations of nested types are errors
21446 caught elsewhere in parsing. Those that are pointless arrive
21447 here. */
21449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21450 && !is_friend && is_declaration
21451 && !processing_explicit_instantiation)
21452 warning (0, "declaration %qD does not declare anything", decl);
21454 type = TREE_TYPE (decl);
21456 else
21458 /* An elaborated-type-specifier sometimes introduces a new type and
21459 sometimes names an existing type. Normally, the rule is that it
21460 introduces a new type only if there is not an existing type of
21461 the same name already in scope. For example, given:
21463 struct S {};
21464 void f() { struct S s; }
21466 the `struct S' in the body of `f' is the same `struct S' as in
21467 the global scope; the existing definition is used. However, if
21468 there were no global declaration, this would introduce a new
21469 local class named `S'.
21471 An exception to this rule applies to the following code:
21473 namespace N { struct S; }
21475 Here, the elaborated-type-specifier names a new type
21476 unconditionally; even if there is already an `S' in the
21477 containing scope this declaration names a new type.
21478 This exception only applies if the elaborated-type-specifier
21479 forms the complete declaration:
21481 [class.name]
21483 A declaration consisting solely of `class-key identifier ;' is
21484 either a redeclaration of the name in the current scope or a
21485 forward declaration of the identifier as a class name. It
21486 introduces the name into the current scope.
21488 We are in this situation precisely when the next token is a `;'.
21490 An exception to the exception is that a `friend' declaration does
21491 *not* name a new type; i.e., given:
21493 struct S { friend struct T; };
21495 `T' is not a new type in the scope of `S'.
21497 Also, `new struct S' or `sizeof (struct S)' never results in the
21498 definition of a new type; a new type can only be declared in a
21499 declaration context. */
21501 TAG_how how;
21503 if (is_friend)
21504 /* Friends have special name lookup rules. */
21505 how = TAG_how::HIDDEN_FRIEND;
21506 else if (is_declaration
21507 && cp_lexer_next_token_is (parser->lexer,
21508 CPP_SEMICOLON))
21509 /* This is a `class-key identifier ;' */
21510 how = TAG_how::CURRENT_ONLY;
21511 else
21512 how = TAG_how::GLOBAL;
21514 bool template_p =
21515 (template_parm_lists_apply
21516 && (cp_parser_next_token_starts_class_definition_p (parser)
21517 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21518 /* An unqualified name was used to reference this type, so
21519 there were no qualifying templates. */
21520 if (template_parm_lists_apply
21521 && !cp_parser_check_template_parameters (parser,
21522 /*num_templates=*/0,
21523 /*template_id*/false,
21524 token->location,
21525 /*declarator=*/NULL))
21526 return error_mark_node;
21528 type = xref_tag (tag_type, identifier, how, template_p);
21532 if (type == error_mark_node)
21533 return error_mark_node;
21535 /* Allow attributes on forward declarations of classes. */
21536 if (attributes)
21538 if (TREE_CODE (type) == TYPENAME_TYPE)
21539 warning (OPT_Wattributes,
21540 "attributes ignored on uninstantiated type");
21541 else if (tag_type != enum_type
21542 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21543 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21544 && ! processing_explicit_instantiation)
21545 warning (OPT_Wattributes,
21546 "attributes ignored on template instantiation");
21547 else if (is_friend && cxx11_attribute_p (attributes))
21549 if (warning (OPT_Wattributes, "attribute ignored"))
21550 inform (input_location, "an attribute that appertains to a friend "
21551 "declaration that is not a definition is ignored");
21553 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21554 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21555 else
21556 warning (OPT_Wattributes,
21557 "attributes ignored on elaborated-type-specifier that is "
21558 "not a forward declaration");
21561 if (tag_type == enum_type)
21562 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21563 else
21565 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21566 for alias definition. */
21567 bool decl_class = (is_declaration
21568 && cp_parser_declares_only_class_p (parser));
21569 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21570 decl_class);
21572 /* Indicate whether this class was declared as a `class' or as a
21573 `struct'. */
21574 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21575 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21578 /* A "<" cannot follow an elaborated type specifier. If that
21579 happens, the user was probably trying to form a template-id. */
21580 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21581 token->location);
21583 return type;
21586 /* Parse an enum-specifier.
21588 enum-specifier:
21589 enum-head { enumerator-list [opt] }
21590 enum-head { enumerator-list , } [C++0x]
21592 enum-head:
21593 enum-key identifier [opt] enum-base [opt]
21594 enum-key nested-name-specifier identifier enum-base [opt]
21596 enum-key:
21597 enum
21598 enum class [C++0x]
21599 enum struct [C++0x]
21601 enum-base: [C++0x]
21602 : type-specifier-seq
21604 opaque-enum-specifier:
21605 enum-key identifier enum-base [opt] ;
21607 GNU Extensions:
21608 enum-key attributes[opt] identifier [opt] enum-base [opt]
21609 { enumerator-list [opt] }attributes[opt]
21610 enum-key attributes[opt] identifier [opt] enum-base [opt]
21611 { enumerator-list, }attributes[opt] [C++0x]
21613 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21614 if the token stream isn't an enum-specifier after all. */
21616 static tree
21617 cp_parser_enum_specifier (cp_parser* parser)
21619 tree identifier;
21620 tree type = NULL_TREE;
21621 tree prev_scope;
21622 tree nested_name_specifier = NULL_TREE;
21623 tree attributes;
21624 bool scoped_enum_p = false;
21625 bool has_underlying_type = false;
21626 bool nested_being_defined = false;
21627 bool new_value_list = false;
21628 bool is_new_type = false;
21629 bool is_unnamed = false;
21630 tree underlying_type = NULL_TREE;
21631 cp_token *type_start_token = NULL;
21632 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21634 /* Parse tentatively so that we can back up if we don't find a
21635 enum-specifier. */
21636 cp_parser_parse_tentatively (parser);
21638 /* Caller guarantees that the current token is 'enum', an identifier
21639 possibly follows, and the token after that is an opening brace.
21640 If we don't have an identifier, fabricate an anonymous name for
21641 the enumeration being defined. */
21642 cp_lexer_consume_token (parser->lexer);
21644 /* Parse the "class" or "struct", which indicates a scoped
21645 enumeration type in C++0x. */
21646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21647 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21649 if (cxx_dialect < cxx11)
21650 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21652 /* Consume the `struct' or `class' token. */
21653 cp_lexer_consume_token (parser->lexer);
21655 scoped_enum_p = true;
21658 attributes = cp_parser_attributes_opt (parser);
21660 /* Clear the qualification. */
21661 parser->scope = NULL_TREE;
21662 parser->qualifying_scope = NULL_TREE;
21663 parser->object_scope = NULL_TREE;
21665 /* Figure out in what scope the declaration is being placed. */
21666 prev_scope = current_scope ();
21668 type_start_token = cp_lexer_peek_token (parser->lexer);
21670 push_deferring_access_checks (dk_no_check);
21671 nested_name_specifier
21672 = cp_parser_nested_name_specifier_opt (parser,
21673 /*typename_keyword_p=*/true,
21674 /*check_dependency_p=*/false,
21675 /*type_p=*/false,
21676 /*is_declaration=*/false);
21678 if (nested_name_specifier)
21680 tree name;
21682 identifier = cp_parser_identifier (parser);
21683 name = cp_parser_lookup_name (parser, identifier,
21684 enum_type,
21685 /*is_template=*/false,
21686 /*is_namespace=*/false,
21687 /*check_dependency=*/true,
21688 /*ambiguous_decls=*/NULL,
21689 input_location);
21690 if (name && name != error_mark_node)
21692 type = TREE_TYPE (name);
21693 if (TREE_CODE (type) == TYPENAME_TYPE)
21695 /* Are template enums allowed in ISO? */
21696 if (template_parm_scope_p ())
21697 pedwarn (type_start_token->location, OPT_Wpedantic,
21698 "%qD is an enumeration template", name);
21699 /* ignore a typename reference, for it will be solved by name
21700 in start_enum. */
21701 type = NULL_TREE;
21704 else if (nested_name_specifier == error_mark_node)
21705 /* We already issued an error. */;
21706 else
21708 error_at (type_start_token->location,
21709 "%qD does not name an enumeration in %qT",
21710 identifier, nested_name_specifier);
21711 nested_name_specifier = error_mark_node;
21714 else
21716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21717 identifier = cp_parser_identifier (parser);
21718 else
21720 identifier = make_anon_name ();
21721 is_unnamed = true;
21722 if (scoped_enum_p)
21723 error_at (type_start_token->location,
21724 "unnamed scoped enum is not allowed");
21727 pop_deferring_access_checks ();
21729 /* Check for the `:' that denotes a specified underlying type in C++0x.
21730 Note that a ':' could also indicate a bitfield width, however. */
21731 location_t colon_loc = UNKNOWN_LOCATION;
21732 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21734 cp_decl_specifier_seq type_specifiers;
21736 /* Consume the `:'. */
21737 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21738 cp_lexer_consume_token (parser->lexer);
21740 auto tdf
21741 = make_temp_override (parser->type_definition_forbidden_message,
21742 G_("types may not be defined in enum-base"));
21744 /* Parse the type-specifier-seq. */
21745 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21746 /*is_declaration=*/false,
21747 /*is_trailing_return=*/false,
21748 &type_specifiers);
21750 /* At this point this is surely not elaborated type specifier. */
21751 if (!cp_parser_parse_definitely (parser))
21752 return NULL_TREE;
21754 if (cxx_dialect < cxx11)
21755 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21757 has_underlying_type = true;
21759 /* If that didn't work, stop. */
21760 if (type_specifiers.type != error_mark_node)
21762 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21763 /*initialized=*/0, NULL);
21764 if (underlying_type == error_mark_node
21765 || check_for_bare_parameter_packs (underlying_type))
21766 underlying_type = NULL_TREE;
21770 /* Look for the `{' but don't consume it yet. */
21771 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21773 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21775 if (has_underlying_type)
21776 cp_parser_commit_to_tentative_parse (parser);
21777 cp_parser_error (parser, "expected %<{%>");
21778 if (has_underlying_type)
21779 return error_mark_node;
21781 /* An opaque-enum-specifier must have a ';' here. */
21782 if ((scoped_enum_p || underlying_type)
21783 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21785 if (has_underlying_type)
21786 pedwarn (colon_loc,
21787 OPT_Welaborated_enum_base,
21788 "declaration of enumeration with "
21789 "fixed underlying type and no enumerator list is "
21790 "only permitted as a standalone declaration");
21791 else
21792 cp_parser_error (parser, "expected %<;%> or %<{%>");
21796 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21797 return NULL_TREE;
21799 if (nested_name_specifier)
21801 if (CLASS_TYPE_P (nested_name_specifier))
21803 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21804 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21805 push_scope (nested_name_specifier);
21807 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21808 push_nested_namespace (nested_name_specifier);
21811 /* Issue an error message if type-definitions are forbidden here. */
21812 if (!cp_parser_check_type_definition (parser))
21813 type = error_mark_node;
21814 else
21815 /* Create the new type. We do this before consuming the opening
21816 brace so the enum will be recorded as being on the line of its
21817 tag (or the 'enum' keyword, if there is no tag). */
21818 type = start_enum (identifier, type, underlying_type,
21819 attributes, scoped_enum_p, &is_new_type);
21821 /* If the next token is not '{' it is an opaque-enum-specifier or an
21822 elaborated-type-specifier. */
21823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21825 auto_timevar tv (TV_PARSE_ENUM);
21827 if (nested_name_specifier
21828 && nested_name_specifier != error_mark_node)
21830 /* The following catches invalid code such as:
21831 enum class S<int>::E { A, B, C }; */
21832 if (!processing_specialization
21833 && CLASS_TYPE_P (nested_name_specifier)
21834 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21835 error_at (type_start_token->location, "cannot add an enumerator "
21836 "list to a template instantiation");
21838 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21840 error_at (type_start_token->location,
21841 "%<%T::%E%> has not been declared",
21842 TYPE_CONTEXT (nested_name_specifier),
21843 nested_name_specifier);
21844 type = error_mark_node;
21846 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21847 && !CLASS_TYPE_P (nested_name_specifier))
21849 error_at (type_start_token->location, "nested name specifier "
21850 "%qT for enum declaration does not name a class "
21851 "or namespace", nested_name_specifier);
21852 type = error_mark_node;
21854 /* If that scope does not contain the scope in which the
21855 class was originally declared, the program is invalid. */
21856 else if (prev_scope && !is_ancestor (prev_scope,
21857 nested_name_specifier))
21859 if (at_namespace_scope_p ())
21860 error_at (type_start_token->location,
21861 "declaration of %qD in namespace %qD which does not "
21862 "enclose %qD",
21863 type, prev_scope, nested_name_specifier);
21864 else
21865 error_at (type_start_token->location,
21866 "declaration of %qD in %qD which does not "
21867 "enclose %qD",
21868 type, prev_scope, nested_name_specifier);
21869 type = error_mark_node;
21871 /* If that scope is the scope where the declaration is being placed
21872 the program is invalid. */
21873 else if (CLASS_TYPE_P (nested_name_specifier)
21874 && CLASS_TYPE_P (prev_scope)
21875 && same_type_p (nested_name_specifier, prev_scope))
21877 permerror (type_start_token->location,
21878 "extra qualification not allowed");
21879 nested_name_specifier = NULL_TREE;
21883 if (scoped_enum_p)
21884 begin_scope (sk_scoped_enum, type);
21886 /* Consume the opening brace. */
21887 matching_braces braces;
21888 braces.consume_open (parser);
21890 if (type == error_mark_node)
21891 ; /* Nothing to add */
21892 else if (OPAQUE_ENUM_P (type)
21893 || (cxx_dialect > cxx98 && processing_specialization))
21895 new_value_list = true;
21896 SET_OPAQUE_ENUM_P (type, false);
21897 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21899 else
21901 error_at (type_start_token->location,
21902 "multiple definition of %q#T", type);
21903 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21904 "previous definition here");
21905 type = error_mark_node;
21908 if (type == error_mark_node)
21909 cp_parser_skip_to_end_of_block_or_statement (parser);
21910 /* If the next token is not '}', then there are some enumerators. */
21911 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21913 if (is_unnamed && !scoped_enum_p
21914 /* Don't warn for enum {} a; here. */
21915 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21916 pedwarn (type_start_token->location, OPT_Wpedantic,
21917 "ISO C++ forbids empty unnamed enum");
21919 else
21921 /* We've seen a '{' so we know we're in an enum-specifier.
21922 Commit to any tentative parse to get syntax errors. */
21923 cp_parser_commit_to_tentative_parse (parser);
21924 cp_parser_enumerator_list (parser, type);
21927 /* Consume the final '}'. */
21928 braces.require_close (parser);
21930 if (scoped_enum_p)
21931 finish_scope ();
21933 else
21935 /* If a ';' follows, then it is an opaque-enum-specifier
21936 and additional restrictions apply. */
21937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21939 if (is_unnamed)
21940 error_at (type_start_token->location,
21941 "opaque-enum-specifier without name");
21942 else if (nested_name_specifier)
21943 error_at (type_start_token->location,
21944 "opaque-enum-specifier must use a simple identifier");
21948 /* Look for trailing attributes to apply to this enumeration, and
21949 apply them if appropriate. */
21950 if (cp_parser_allow_gnu_extensions_p (parser))
21952 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21953 cplus_decl_attributes (&type,
21954 trailing_attr,
21955 (int) ATTR_FLAG_TYPE_IN_PLACE);
21958 /* Finish up the enumeration. */
21959 if (type != error_mark_node)
21961 if (new_value_list)
21962 finish_enum_value_list (type);
21963 if (is_new_type)
21964 finish_enum (type);
21967 if (nested_name_specifier)
21969 if (CLASS_TYPE_P (nested_name_specifier))
21971 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21972 pop_scope (nested_name_specifier);
21974 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21975 pop_nested_namespace (nested_name_specifier);
21977 return type;
21980 /* Parse an enumerator-list. The enumerators all have the indicated
21981 TYPE.
21983 enumerator-list:
21984 enumerator-definition
21985 enumerator-list , enumerator-definition */
21987 static void
21988 cp_parser_enumerator_list (cp_parser* parser, tree type)
21990 while (true)
21992 /* Parse an enumerator-definition. */
21993 cp_parser_enumerator_definition (parser, type);
21995 /* If the next token is not a ',', we've reached the end of
21996 the list. */
21997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21998 break;
21999 /* Otherwise, consume the `,' and keep going. */
22000 cp_lexer_consume_token (parser->lexer);
22001 /* If the next token is a `}', there is a trailing comma. */
22002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22004 if (cxx_dialect < cxx11)
22005 pedwarn (input_location, OPT_Wpedantic,
22006 "comma at end of enumerator list");
22007 break;
22012 /* Parse an enumerator-definition. The enumerator has the indicated
22013 TYPE.
22015 enumerator-definition:
22016 enumerator
22017 enumerator = constant-expression
22019 enumerator:
22020 identifier
22022 GNU Extensions:
22024 enumerator-definition:
22025 enumerator attributes [opt]
22026 enumerator attributes [opt] = constant-expression */
22028 static void
22029 cp_parser_enumerator_definition (cp_parser* parser, tree type)
22031 tree identifier;
22032 tree value;
22033 location_t loc;
22035 /* Save the input location because we are interested in the location
22036 of the identifier and not the location of the explicit value. */
22037 loc = cp_lexer_peek_token (parser->lexer)->location;
22039 /* Look for the identifier. */
22040 identifier = cp_parser_identifier (parser);
22041 if (identifier == error_mark_node)
22042 return;
22044 /* Parse any specified attributes. */
22045 tree attrs = cp_parser_attributes_opt (parser);
22047 /* If the next token is an '=', then there is an explicit value. */
22048 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22050 /* Consume the `=' token. */
22051 cp_lexer_consume_token (parser->lexer);
22052 /* Parse the value. */
22053 value = cp_parser_constant_expression (parser);
22055 else
22056 value = NULL_TREE;
22058 /* If we are processing a template, make sure the initializer of the
22059 enumerator doesn't contain any bare template parameter pack. */
22060 if (current_lambda_expr ())
22062 /* In a lambda it should work, but doesn't currently. */
22063 if (uses_parameter_packs (value))
22065 sorry ("unexpanded parameter pack in enumerator in lambda");
22066 value = error_mark_node;
22069 else if (check_for_bare_parameter_packs (value))
22070 value = error_mark_node;
22072 /* Create the enumerator. */
22073 build_enumerator (identifier, value, type, attrs, loc);
22076 /* Parse a namespace-name.
22078 namespace-name:
22079 original-namespace-name
22080 namespace-alias
22082 Returns the NAMESPACE_DECL for the namespace. */
22084 static tree
22085 cp_parser_namespace_name (cp_parser* parser)
22087 tree identifier;
22088 tree namespace_decl;
22090 cp_token *token = cp_lexer_peek_token (parser->lexer);
22092 /* Get the name of the namespace. */
22093 identifier = cp_parser_identifier (parser);
22094 if (identifier == error_mark_node)
22095 return error_mark_node;
22097 /* Look up the identifier in the currently active scope. Look only
22098 for namespaces, due to:
22100 [basic.lookup.udir]
22102 When looking up a namespace-name in a using-directive or alias
22103 definition, only namespace names are considered.
22105 And:
22107 [basic.lookup.qual]
22109 During the lookup of a name preceding the :: scope resolution
22110 operator, object, function, and enumerator names are ignored.
22112 (Note that cp_parser_qualifying_entity only calls this
22113 function if the token after the name is the scope resolution
22114 operator.) */
22115 namespace_decl = cp_parser_lookup_name (parser, identifier,
22116 none_type,
22117 /*is_template=*/false,
22118 /*is_namespace=*/true,
22119 /*check_dependency=*/true,
22120 /*ambiguous_decls=*/NULL,
22121 token->location);
22122 /* If it's not a namespace, issue an error. */
22123 if (namespace_decl == error_mark_node
22124 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
22126 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22128 auto_diagnostic_group d;
22129 name_hint hint;
22130 if (namespace_decl == error_mark_node
22131 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
22132 hint = suggest_alternative_in_explicit_scope (token->location,
22133 identifier,
22134 parser->scope);
22135 if (const char *suggestion = hint.suggestion ())
22137 gcc_rich_location richloc (token->location);
22138 richloc.add_fixit_replace (suggestion);
22139 error_at (&richloc,
22140 "%qD is not a namespace-name; did you mean %qs?",
22141 identifier, suggestion);
22143 else
22144 error_at (token->location, "%qD is not a namespace-name",
22145 identifier);
22147 else
22148 cp_parser_error (parser, "expected namespace-name");
22149 namespace_decl = error_mark_node;
22152 return namespace_decl;
22155 /* Parse a namespace-definition.
22157 namespace-definition:
22158 named-namespace-definition
22159 unnamed-namespace-definition
22161 named-namespace-definition:
22162 original-namespace-definition
22163 extension-namespace-definition
22165 original-namespace-definition:
22166 namespace identifier { namespace-body }
22168 extension-namespace-definition:
22169 namespace original-namespace-name { namespace-body }
22171 unnamed-namespace-definition:
22172 namespace { namespace-body } */
22174 static void
22175 cp_parser_namespace_definition (cp_parser* parser)
22177 tree identifier;
22178 int nested_definition_count = 0;
22180 cp_ensure_no_omp_declare_simd (parser);
22181 cp_ensure_no_oacc_routine (parser);
22183 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
22184 const bool topmost_inline_p = is_inline;
22186 if (is_inline)
22188 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
22189 cp_lexer_consume_token (parser->lexer);
22192 /* Look for the `namespace' keyword. */
22193 cp_token* token
22194 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22196 /* Parse any specified attributes before the identifier. */
22197 tree attribs = cp_parser_attributes_opt (parser);
22199 for (;;)
22201 identifier = NULL_TREE;
22203 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
22204 RID_INLINE);
22205 if (nested_inline_p && nested_definition_count != 0)
22207 if (pedantic && cxx_dialect < cxx20)
22208 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
22209 OPT_Wc__20_extensions, "nested inline namespace "
22210 "definitions only available with %<-std=c++20%> or "
22211 "%<-std=gnu++20%>");
22212 cp_lexer_consume_token (parser->lexer);
22215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22217 identifier = cp_parser_identifier (parser);
22219 if (cp_next_tokens_can_be_std_attribute_p (parser))
22220 pedwarn (input_location, OPT_Wpedantic,
22221 "standard attributes on namespaces must precede "
22222 "the namespace name");
22224 /* Parse any attributes specified after the identifier. */
22225 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
22228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22230 /* Don't forget that the innermost namespace might have been
22231 marked as inline. Use |= because we cannot overwrite
22232 IS_INLINE in case the outermost namespace is inline, but
22233 there are no nested inlines. */
22234 is_inline |= nested_inline_p;
22235 break;
22238 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
22239 pedwarn (input_location, OPT_Wc__17_extensions,
22240 "nested namespace definitions only available with "
22241 "%<-std=c++17%> or %<-std=gnu++17%>");
22243 /* Nested namespace names can create new namespaces (unlike
22244 other qualified-ids). */
22245 if (int count = (identifier
22246 ? push_namespace (identifier, nested_inline_p)
22247 : 0))
22248 nested_definition_count += count;
22249 else
22250 cp_parser_error (parser, "nested namespace name required");
22251 cp_lexer_consume_token (parser->lexer);
22254 if (nested_definition_count && !identifier)
22255 cp_parser_error (parser, "namespace name required");
22257 if (nested_definition_count && attribs)
22258 error_at (token->location,
22259 "a nested namespace definition cannot have attributes");
22260 if (nested_definition_count && topmost_inline_p)
22261 error_at (token->location,
22262 "a nested namespace definition cannot be inline");
22264 /* Start the namespace. */
22265 nested_definition_count += push_namespace (identifier, is_inline);
22267 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
22269 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
22271 /* Look for the `{' to validate starting the namespace. */
22272 matching_braces braces;
22273 if (braces.require_open (parser))
22275 /* Parse the body of the namespace. */
22276 cp_parser_namespace_body (parser);
22278 /* Look for the final `}'. */
22279 braces.require_close (parser);
22282 if (has_visibility)
22283 pop_visibility (1);
22285 /* Pop the nested namespace definitions. */
22286 while (nested_definition_count--)
22287 pop_namespace ();
22290 /* Parse a namespace-body.
22292 namespace-body:
22293 declaration-seq [opt] */
22295 static void
22296 cp_parser_namespace_body (cp_parser* parser)
22298 cp_parser_declaration_seq_opt (parser);
22301 /* Parse a namespace-alias-definition.
22303 namespace-alias-definition:
22304 namespace identifier = qualified-namespace-specifier ; */
22306 static void
22307 cp_parser_namespace_alias_definition (cp_parser* parser)
22309 tree identifier;
22310 tree namespace_specifier;
22312 cp_token *token = cp_lexer_peek_token (parser->lexer);
22314 /* Look for the `namespace' keyword. */
22315 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22316 /* Look for the identifier. */
22317 identifier = cp_parser_identifier (parser);
22318 if (identifier == error_mark_node)
22319 return;
22320 /* Look for the `=' token. */
22321 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
22322 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22324 error_at (token->location, "%<namespace%> definition is not allowed here");
22325 /* Skip the definition. */
22326 cp_lexer_consume_token (parser->lexer);
22327 if (cp_parser_skip_to_closing_brace (parser))
22328 cp_lexer_consume_token (parser->lexer);
22329 return;
22331 cp_parser_require (parser, CPP_EQ, RT_EQ);
22332 /* Look for the qualified-namespace-specifier. */
22333 namespace_specifier
22334 = cp_parser_qualified_namespace_specifier (parser);
22335 cp_warn_deprecated_use_scopes (namespace_specifier);
22336 /* Look for the `;' token. */
22337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22339 /* Register the alias in the symbol table. */
22340 do_namespace_alias (identifier, namespace_specifier);
22343 /* Parse a qualified-namespace-specifier.
22345 qualified-namespace-specifier:
22346 :: [opt] nested-name-specifier [opt] namespace-name
22348 Returns a NAMESPACE_DECL corresponding to the specified
22349 namespace. */
22351 static tree
22352 cp_parser_qualified_namespace_specifier (cp_parser* parser)
22354 /* Look for the optional `::'. */
22355 cp_parser_global_scope_opt (parser,
22356 /*current_scope_valid_p=*/false);
22358 /* Look for the optional nested-name-specifier. */
22359 cp_parser_nested_name_specifier_opt (parser,
22360 /*typename_keyword_p=*/false,
22361 /*check_dependency_p=*/true,
22362 /*type_p=*/false,
22363 /*is_declaration=*/true);
22365 return cp_parser_namespace_name (parser);
22368 /* Subroutine of cp_parser_using_declaration. */
22370 static tree
22371 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
22373 tree decl = NULL_TREE;
22374 if (at_class_scope_p ())
22376 /* Create the USING_DECL. */
22377 decl = do_class_using_decl (qscope, identifier);
22379 if (check_for_bare_parameter_packs (decl))
22380 return error_mark_node;
22382 if (decl && typename_p)
22383 USING_DECL_TYPENAME_P (decl) = 1;
22385 /* Add it to the list of members in this class. */
22386 finish_member_declaration (decl);
22388 else
22389 finish_nonmember_using_decl (qscope, identifier);
22390 return decl;
22393 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
22394 access declaration.
22396 using-declaration:
22397 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
22398 using :: unqualified-id ;
22400 access-declaration:
22401 qualified-id ;
22405 static bool
22406 cp_parser_using_declaration (cp_parser* parser,
22407 bool access_declaration_p)
22409 cp_token *token;
22410 bool typename_p = false;
22411 bool global_scope_p;
22412 tree identifier;
22413 tree qscope;
22414 int oldcount = errorcount;
22415 cp_token *diag_token = NULL;
22417 if (access_declaration_p)
22419 diag_token = cp_lexer_peek_token (parser->lexer);
22420 cp_parser_parse_tentatively (parser);
22422 else
22424 /* Look for the `using' keyword. */
22425 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22427 again:
22428 /* Peek at the next token. */
22429 token = cp_lexer_peek_token (parser->lexer);
22430 /* See if it's `typename'. */
22431 if (token->keyword == RID_TYPENAME)
22433 /* Remember that we've seen it. */
22434 typename_p = true;
22435 /* Consume the `typename' token. */
22436 cp_lexer_consume_token (parser->lexer);
22440 /* Look for the optional global scope qualification. */
22441 global_scope_p
22442 = (cp_parser_global_scope_opt (parser,
22443 /*current_scope_valid_p=*/false)
22444 != NULL_TREE);
22446 /* If we saw `typename', or didn't see `::', then there must be a
22447 nested-name-specifier present. */
22448 if (typename_p || !global_scope_p)
22450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
22451 /*check_dependency_p=*/true,
22452 /*type_p=*/false,
22453 /*is_declaration=*/true);
22454 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
22456 cp_parser_skip_to_end_of_block_or_statement (parser);
22457 return false;
22460 /* Otherwise, we could be in either of the two productions. In that
22461 case, treat the nested-name-specifier as optional. */
22462 else
22463 qscope = cp_parser_nested_name_specifier_opt (parser,
22464 /*typename_keyword_p=*/false,
22465 /*check_dependency_p=*/true,
22466 /*type_p=*/false,
22467 /*is_declaration=*/true);
22468 if (!qscope)
22469 qscope = global_namespace;
22471 cp_warn_deprecated_use_scopes (qscope);
22473 if (access_declaration_p
22474 && !MAYBE_CLASS_TYPE_P (qscope)
22475 && TREE_CODE (qscope) != ENUMERAL_TYPE)
22476 /* If the qualifying scope of an access-declaration isn't a class
22477 or enumeration type then it can't be valid. */
22478 cp_parser_simulate_error (parser);
22480 if (access_declaration_p && cp_parser_error_occurred (parser))
22481 /* Something has already gone wrong; there's no need to parse
22482 further. Since an error has occurred, the return value of
22483 cp_parser_parse_definitely will be false, as required. */
22484 return cp_parser_parse_definitely (parser);
22486 token = cp_lexer_peek_token (parser->lexer);
22487 /* Parse the unqualified-id. */
22488 identifier = cp_parser_unqualified_id (parser,
22489 /*template_keyword_p=*/false,
22490 /*check_dependency_p=*/true,
22491 /*declarator_p=*/true,
22492 /*optional_p=*/false);
22494 if (access_declaration_p)
22496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22497 cp_parser_simulate_error (parser);
22498 if (!cp_parser_parse_definitely (parser))
22499 return false;
22501 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22503 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22504 if (cxx_dialect < cxx17)
22505 pedwarn (ell->location, OPT_Wc__17_extensions,
22506 "pack expansion in using-declaration only available "
22507 "with %<-std=c++17%> or %<-std=gnu++17%>");
22509 /* A parameter pack can appear in the qualifying scope, and/or in the
22510 terminal name (if naming a conversion function). Logically they're
22511 part of a single pack expansion of the overall USING_DECL, but we
22512 express them as separate pack expansions within the USING_DECL since
22513 we can't create a pack expansion over a USING_DECL. */
22514 bool saw_parm_pack = false;
22515 if (uses_parameter_packs (qscope))
22517 qscope = make_pack_expansion (qscope);
22518 saw_parm_pack = true;
22520 if (identifier_p (identifier)
22521 && IDENTIFIER_CONV_OP_P (identifier)
22522 && uses_parameter_packs (TREE_TYPE (identifier)))
22524 identifier = make_conv_op_name (make_pack_expansion
22525 (TREE_TYPE (identifier)));
22526 saw_parm_pack = true;
22528 if (!saw_parm_pack)
22530 /* Issue an error in terms using a SCOPE_REF that includes both
22531 components. */
22532 tree name
22533 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22534 make_pack_expansion (name);
22535 gcc_assert (seen_error ());
22536 qscope = identifier = error_mark_node;
22540 /* The function we call to handle a using-declaration is different
22541 depending on what scope we are in. */
22542 if (qscope == error_mark_node || identifier == error_mark_node)
22544 else if (!identifier_p (identifier)
22545 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22546 /* [namespace.udecl]
22548 A using declaration shall not name a template-id. */
22549 error_at (token->location,
22550 "a template-id may not appear in a using-declaration");
22551 else
22553 tree decl = finish_using_decl (qscope, identifier, typename_p);
22555 if (decl == error_mark_node)
22557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22558 return false;
22562 if (!access_declaration_p
22563 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22565 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22566 if (cxx_dialect < cxx17)
22567 pedwarn (comma->location, OPT_Wc__17_extensions,
22568 "comma-separated list in using-declaration only available "
22569 "with %<-std=c++17%> or %<-std=gnu++17%>");
22570 goto again;
22573 /* Look for the final `;'. */
22574 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22576 if (access_declaration_p && errorcount == oldcount)
22577 warning_at (diag_token->location, OPT_Wdeprecated,
22578 "access declarations are deprecated "
22579 "in favor of using-declarations; "
22580 "suggestion: add the %<using%> keyword");
22582 return true;
22585 /* C++20 using enum declaration.
22587 using-enum-declaration :
22588 using elaborated-enum-specifier ; */
22590 static void
22591 cp_parser_using_enum (cp_parser *parser)
22593 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22595 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22596 breaks one of the motivating examples in using-enum-5.C.
22597 cp_parser_simple_type_specifier seems to be closer to what we actually
22598 want, though that hasn't been properly specified yet. */
22600 /* Consume 'enum'. */
22601 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22602 cp_lexer_consume_token (parser->lexer);
22604 cp_token *start = cp_lexer_peek_token (parser->lexer);
22606 tree type = (cp_parser_simple_type_specifier
22607 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22609 cp_token *end = cp_lexer_previous_token (parser->lexer);
22611 if (type == error_mark_node
22612 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22614 cp_parser_skip_to_end_of_block_or_statement (parser);
22615 return;
22617 if (TREE_CODE (type) == TYPE_DECL)
22618 type = TREE_TYPE (type);
22620 /* The elaborated-enum-specifier shall not name a dependent type and the type
22621 shall have a reachable enum-specifier. */
22622 const char *msg = nullptr;
22623 if (cxx_dialect < cxx20)
22624 msg = G_("%<using enum%> "
22625 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22626 else if (dependent_type_p (type))
22627 msg = G_("%<using enum%> of dependent type %qT");
22628 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22629 msg = G_("%<using enum%> of non-enumeration type %q#T");
22630 else if (!COMPLETE_TYPE_P (type))
22631 msg = G_("%<using enum%> of incomplete type %qT");
22632 else if (OPAQUE_ENUM_P (type))
22633 msg = G_("%<using enum%> of %qT before its enum-specifier");
22634 if (msg)
22636 location_t loc = make_location (start, start, end);
22637 auto_diagnostic_group g;
22638 error_at (loc, msg, type);
22639 loc = location_of (type);
22640 if (cxx_dialect < cxx20 || loc == input_location)
22642 else if (OPAQUE_ENUM_P (type))
22643 inform (loc, "opaque-enum-declaration here");
22644 else
22645 inform (loc, "declared here");
22648 /* A using-enum-declaration introduces the enumerator names of the named
22649 enumeration as if by a using-declaration for each enumerator. */
22650 if (TREE_CODE (type) == ENUMERAL_TYPE)
22651 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22652 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22655 /* Parse an alias-declaration.
22657 alias-declaration:
22658 using identifier attribute-specifier-seq [opt] = type-id */
22660 static tree
22661 cp_parser_alias_declaration (cp_parser* parser)
22663 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22664 location_t id_location, type_location;
22665 cp_declarator *declarator;
22666 cp_decl_specifier_seq decl_specs;
22667 bool member_p;
22668 const char *saved_message = NULL;
22670 /* Look for the `using' keyword. */
22671 cp_token *using_token
22672 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22673 if (using_token == NULL)
22674 return error_mark_node;
22676 id_location = cp_lexer_peek_token (parser->lexer)->location;
22677 id = cp_parser_identifier (parser);
22678 if (id == error_mark_node)
22679 return error_mark_node;
22681 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22682 attributes = cp_parser_attributes_opt (parser);
22683 if (attributes == error_mark_node)
22684 return error_mark_node;
22686 cp_parser_require (parser, CPP_EQ, RT_EQ);
22688 if (cp_parser_error_occurred (parser))
22689 return error_mark_node;
22691 cp_parser_commit_to_tentative_parse (parser);
22693 /* Now we are going to parse the type-id of the declaration. */
22696 [dcl.type]/3 says:
22698 "A type-specifier-seq shall not define a class or enumeration
22699 unless it appears in the type-id of an alias-declaration (7.1.3) that
22700 is not the declaration of a template-declaration."
22702 In other words, if we currently are in an alias template, the
22703 type-id should not define a type.
22705 So let's set parser->type_definition_forbidden_message in that
22706 case; cp_parser_check_type_definition (called by
22707 cp_parser_class_specifier) will then emit an error if a type is
22708 defined in the type-id. */
22709 if (parser->num_template_parameter_lists)
22711 saved_message = parser->type_definition_forbidden_message;
22712 parser->type_definition_forbidden_message =
22713 G_("types may not be defined in alias template declarations");
22716 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22717 &type_location);
22719 /* Restore the error message if need be. */
22720 if (parser->num_template_parameter_lists)
22721 parser->type_definition_forbidden_message = saved_message;
22723 if (type == error_mark_node
22724 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22726 cp_parser_skip_to_end_of_block_or_statement (parser);
22727 return error_mark_node;
22730 /* A typedef-name can also be introduced by an alias-declaration. The
22731 identifier following the using keyword becomes a typedef-name. It has
22732 the same semantics as if it were introduced by the typedef
22733 specifier. In particular, it does not define a new type and it shall
22734 not appear in the type-id. */
22736 clear_decl_specs (&decl_specs);
22737 decl_specs.type = type;
22738 if (attributes != NULL_TREE)
22740 decl_specs.attributes = attributes;
22741 set_and_check_decl_spec_loc (&decl_specs,
22742 ds_attribute,
22743 attrs_token);
22745 set_and_check_decl_spec_loc (&decl_specs,
22746 ds_typedef,
22747 using_token);
22748 set_and_check_decl_spec_loc (&decl_specs,
22749 ds_alias,
22750 using_token);
22751 decl_specs.locations[ds_type_spec] = type_location;
22753 if (parser->num_template_parameter_lists
22754 && !cp_parser_check_template_parameters (parser,
22755 /*num_templates=*/0,
22756 /*template_id*/false,
22757 id_location,
22758 /*declarator=*/NULL))
22759 return error_mark_node;
22761 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22763 member_p = at_class_scope_p ();
22764 if (member_p)
22765 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22766 NULL_TREE, attributes);
22767 else
22768 decl = start_decl (declarator, &decl_specs, 0,
22769 attributes, NULL_TREE, &pushed_scope);
22770 if (decl == error_mark_node)
22771 return decl;
22773 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22775 if (pushed_scope)
22776 pop_scope (pushed_scope);
22778 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22779 added into the symbol table; otherwise, return the TYPE_DECL. */
22780 if (DECL_LANG_SPECIFIC (decl)
22781 && DECL_TEMPLATE_INFO (decl)
22782 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22784 decl = DECL_TI_TEMPLATE (decl);
22785 if (member_p)
22786 check_member_template (decl);
22789 return decl;
22792 /* Parse a using-directive.
22794 using-directive:
22795 attribute-specifier-seq [opt] using namespace :: [opt]
22796 nested-name-specifier [opt] namespace-name ; */
22798 static void
22799 cp_parser_using_directive (cp_parser* parser)
22801 tree namespace_decl;
22802 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22803 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22805 /* Error during attribute parsing that resulted in skipping
22806 to next semicolon. */
22807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22808 return;
22811 /* Look for the `using' keyword. */
22812 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22813 /* And the `namespace' keyword. */
22814 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22815 /* Look for the optional `::' operator. */
22816 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22817 /* And the optional nested-name-specifier. */
22818 cp_parser_nested_name_specifier_opt (parser,
22819 /*typename_keyword_p=*/false,
22820 /*check_dependency_p=*/true,
22821 /*type_p=*/false,
22822 /*is_declaration=*/true);
22823 /* Get the namespace being used. */
22824 namespace_decl = cp_parser_namespace_name (parser);
22825 cp_warn_deprecated_use_scopes (namespace_decl);
22826 /* And any specified GNU attributes. */
22827 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22828 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22830 /* Update the symbol table. */
22831 finish_using_directive (namespace_decl, attribs);
22833 /* Look for the final `;'. */
22834 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22837 /* Parse a string literal or constant expression yielding a string.
22838 The constant expression uses extra parens to avoid ambiguity with "x" (expr).
22840 asm-string-expr:
22841 string-literal
22842 ( constant-expr ) */
22844 static tree
22845 cp_parser_asm_string_expression (cp_parser *parser)
22847 cp_token *tok = cp_lexer_peek_token (parser->lexer);
22849 if (tok->type == CPP_OPEN_PAREN)
22851 matching_parens parens;
22852 parens.consume_open (parser);
22853 tree string = cp_parser_constant_expression (parser);
22854 if (string != error_mark_node)
22855 string = cxx_constant_value (string, tf_error);
22856 cexpr_str cstr (string);
22857 if (!cstr.type_check (tok->location))
22858 return error_mark_node;
22859 if (!cstr.extract (tok->location, string))
22860 string = error_mark_node;
22861 parens.require_close (parser);
22862 return string;
22864 else if (!cp_parser_is_string_literal (tok))
22866 error_at (tok->location,
22867 "expected string-literal or constexpr in parentheses");
22868 return error_mark_node;
22870 return cp_parser_string_literal (parser, false, false);
22873 /* Parse an asm-definition.
22875 asm-qualifier:
22876 volatile
22877 inline
22878 goto
22880 asm-qualifier-list:
22881 asm-qualifier
22882 asm-qualifier-list asm-qualifier
22884 asm-definition:
22885 asm ( constant-expr ) ;
22887 GNU Extension:
22889 asm-definition:
22890 asm asm-qualifier-list [opt] ( asm-string-expr ) ;
22891 asm asm-qualifier-list [opt] ( asm-string-expr : asm-operand-list [opt] ) ;
22892 asm asm-qualifier-list [opt] ( asm-string-expr : asm-operand-list [opt]
22893 : asm-operand-list [opt] ) ;
22894 asm asm-qualifier-list [opt] ( asm-string-expr : asm-operand-list [opt]
22895 : asm-operand-list [opt]
22896 : asm-clobber-list [opt] ) ;
22897 asm asm-qualifier-list [opt] ( asm-string-expr : : asm-operand-list [opt]
22898 : asm-clobber-list [opt]
22899 : asm-goto-list ) ;
22901 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22902 contains goto, and is the only allowed form in that case. No duplicates are
22903 allowed in an asm-qualifier-list. */
22905 static void
22906 cp_parser_asm_definition (cp_parser* parser)
22908 tree outputs = NULL_TREE;
22909 tree inputs = NULL_TREE;
22910 tree clobbers = NULL_TREE;
22911 tree labels = NULL_TREE;
22912 tree asm_stmt;
22913 bool extended_p = false;
22914 bool invalid_inputs_p = false;
22915 bool invalid_outputs_p = false;
22916 required_token missing = RT_NONE;
22917 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
22918 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22920 /* Look for the `asm' keyword. */
22921 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22923 /* In C++20, unevaluated inline assembly is permitted in constexpr
22924 functions. */
22925 if (parser->in_function_body
22926 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22927 && cxx_dialect < cxx20)
22928 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22929 "function only available with %<-std=c++20%> or "
22930 "%<-std=gnu++20%>");
22932 /* Handle the asm-qualifier-list. */
22933 location_t volatile_loc = UNKNOWN_LOCATION;
22934 location_t inline_loc = UNKNOWN_LOCATION;
22935 location_t goto_loc = UNKNOWN_LOCATION;
22936 location_t first_loc = UNKNOWN_LOCATION;
22938 if (cp_parser_allow_gnu_extensions_p (parser))
22939 for (;;)
22941 cp_token *token = cp_lexer_peek_token (parser->lexer);
22942 location_t loc = token->location;
22943 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22945 case RID_VOLATILE:
22946 if (volatile_loc)
22948 error_at (loc, "duplicate %<asm%> qualifier %qT",
22949 token->u.value);
22950 inform (volatile_loc, "first seen here");
22952 else
22954 if (!parser->in_function_body)
22955 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22956 "outside of function body", token->u.value);
22957 volatile_loc = loc;
22959 cp_lexer_consume_token (parser->lexer);
22960 continue;
22962 case RID_INLINE:
22963 if (inline_loc)
22965 error_at (loc, "duplicate %<asm%> qualifier %qT",
22966 token->u.value);
22967 inform (inline_loc, "first seen here");
22969 else
22970 inline_loc = loc;
22971 if (!first_loc)
22972 first_loc = loc;
22973 cp_lexer_consume_token (parser->lexer);
22974 continue;
22976 case RID_GOTO:
22977 if (goto_loc)
22979 error_at (loc, "duplicate %<asm%> qualifier %qT",
22980 token->u.value);
22981 inform (goto_loc, "first seen here");
22983 else
22984 goto_loc = loc;
22985 if (!first_loc)
22986 first_loc = loc;
22987 cp_lexer_consume_token (parser->lexer);
22988 continue;
22990 case RID_CONST:
22991 case RID_RESTRICT:
22992 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22993 cp_lexer_consume_token (parser->lexer);
22994 continue;
22996 default:
22997 break;
22999 break;
23002 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
23003 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
23004 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
23006 if (!parser->in_function_body && (inline_p || goto_p))
23008 error_at (first_loc, "%<asm%> qualifier outside of function body");
23009 inline_p = goto_p = false;
23012 /* Look for the opening `('. */
23013 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23014 return;
23015 /* Look for the string. */
23016 tree string = cp_parser_asm_string_expression (parser);
23017 if (string == error_mark_node)
23019 cp_parser_skip_to_closing_parenthesis (parser, true, false,
23020 /*consume_paren=*/true);
23021 return;
23024 /* If we're allowing GNU extensions, check for the extended assembly
23025 syntax. Unfortunately, the `:' tokens need not be separated by
23026 a space in C, and so, for compatibility, we tolerate that here
23027 too. Doing that means that we have to treat the `::' operator as
23028 two `:' tokens. */
23029 if (cp_parser_allow_gnu_extensions_p (parser)
23030 && parser->in_function_body
23031 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
23032 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
23034 bool inputs_p = false;
23035 bool clobbers_p = false;
23036 bool labels_p = false;
23038 /* The extended syntax was used. */
23039 extended_p = true;
23041 /* Look for outputs. */
23042 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23044 /* Consume the `:'. */
23045 cp_lexer_consume_token (parser->lexer);
23046 /* Parse the output-operands. */
23047 if (cp_lexer_next_token_is_not (parser->lexer,
23048 CPP_COLON)
23049 && cp_lexer_next_token_is_not (parser->lexer,
23050 CPP_SCOPE)
23051 && cp_lexer_next_token_is_not (parser->lexer,
23052 CPP_CLOSE_PAREN))
23054 outputs = cp_parser_asm_operand_list (parser);
23055 if (outputs == error_mark_node)
23056 invalid_outputs_p = true;
23059 /* If the next token is `::', there are no outputs, and the
23060 next token is the beginning of the inputs. */
23061 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23062 /* The inputs are coming next. */
23063 inputs_p = true;
23065 /* Look for inputs. */
23066 if (inputs_p
23067 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23069 /* Consume the `:' or `::'. */
23070 cp_lexer_consume_token (parser->lexer);
23071 /* Parse the output-operands. */
23072 if (cp_lexer_next_token_is_not (parser->lexer,
23073 CPP_COLON)
23074 && cp_lexer_next_token_is_not (parser->lexer,
23075 CPP_SCOPE)
23076 && cp_lexer_next_token_is_not (parser->lexer,
23077 CPP_CLOSE_PAREN))
23079 inputs = cp_parser_asm_operand_list (parser);
23080 if (inputs == error_mark_node)
23081 invalid_inputs_p = true;
23084 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23085 /* The clobbers are coming next. */
23086 clobbers_p = true;
23088 /* Look for clobbers. */
23089 if (clobbers_p
23090 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23092 clobbers_p = true;
23093 /* Consume the `:' or `::'. */
23094 cp_lexer_consume_token (parser->lexer);
23095 /* Parse the clobbers. */
23096 if (cp_lexer_next_token_is_not (parser->lexer,
23097 CPP_COLON)
23098 && cp_lexer_next_token_is_not (parser->lexer,
23099 CPP_CLOSE_PAREN))
23100 clobbers = cp_parser_asm_clobber_list (parser);
23102 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23103 /* The labels are coming next. */
23104 labels_p = true;
23106 /* Look for labels. */
23107 if (labels_p
23108 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
23110 labels_p = true;
23111 /* Consume the `:' or `::'. */
23112 cp_lexer_consume_token (parser->lexer);
23113 /* Parse the labels. */
23114 labels = cp_parser_asm_label_list (parser);
23117 if (goto_p && !labels_p)
23118 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
23120 else if (goto_p)
23121 missing = RT_COLON_SCOPE;
23123 /* Look for the closing `)'. */
23124 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
23125 missing ? missing : RT_CLOSE_PAREN))
23126 cp_parser_skip_to_closing_parenthesis (parser, true, false,
23127 /*consume_paren=*/true);
23128 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23130 if (!invalid_inputs_p && !invalid_outputs_p)
23132 /* Create the ASM_EXPR. */
23133 if (parser->in_function_body)
23135 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
23136 inputs, clobbers, labels, inline_p);
23137 /* If the extended syntax was not used, mark the ASM_EXPR. */
23138 if (!extended_p)
23140 tree temp = asm_stmt;
23141 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
23142 temp = TREE_OPERAND (temp, 0);
23144 ASM_INPUT_P (temp) = 1;
23147 else
23148 symtab->finalize_toplevel_asm (string);
23151 if (std_attrs && any_nonignored_attribute_p (std_attrs))
23152 warning_at (asm_loc, OPT_Wattributes,
23153 "attributes ignored on %<asm%> declaration");
23156 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
23157 type that comes from the decl-specifier-seq. */
23159 static tree
23160 strip_declarator_types (tree type, cp_declarator *declarator)
23162 for (cp_declarator *d = declarator; d;)
23163 switch (d->kind)
23165 case cdk_id:
23166 case cdk_decomp:
23167 case cdk_error:
23168 d = NULL;
23169 break;
23171 default:
23172 if (TYPE_PTRMEMFUNC_P (type))
23173 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
23174 type = TREE_TYPE (type);
23175 d = d->declarator;
23176 break;
23179 return type;
23182 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
23183 a construct looks like a variable definition but is actually a function
23184 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
23185 is the declarator for this function declaration. */
23187 static void
23188 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
23189 const cp_declarator *declarator)
23191 /* Only warn if we are declaring a function at block scope. */
23192 if (!at_function_scope_p ())
23193 return;
23195 /* And only if there is no storage class specified. */
23196 if (decl_specifiers->storage_class != sc_none
23197 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23198 return;
23200 if (declarator->kind != cdk_function
23201 || !declarator->declarator
23202 || declarator->declarator->kind != cdk_id
23203 || !identifier_p (get_unqualified_id
23204 (const_cast<cp_declarator *>(declarator))))
23205 return;
23207 /* Don't warn when the whole declarator (not just the declarator-id!)
23208 was parenthesized. That is, don't warn for int(n()) but do warn
23209 for int(f)(). */
23210 if (declarator->parenthesized != UNKNOWN_LOCATION)
23211 return;
23213 tree type;
23214 if (decl_specifiers->type)
23216 type = decl_specifiers->type;
23217 if (TREE_CODE (type) == TYPE_DECL)
23218 type = TREE_TYPE (type);
23220 /* If the return type is void there is no ambiguity. */
23221 if (same_type_p (type, void_type_node))
23222 return;
23224 else if (decl_specifiers->any_type_specifiers_p)
23225 /* Code like long f(); will have null ->type. If we have any
23226 type-specifiers, pretend we've seen int. */
23227 type = integer_type_node;
23228 else
23229 return;
23231 auto_diagnostic_group d;
23232 location_t loc = declarator->u.function.parens_loc;
23233 tree params = declarator->u.function.parameters;
23234 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
23236 /* The T t() case. */
23237 if (params == void_list_node)
23239 if (warning_at (loc, OPT_Wvexing_parse,
23240 "empty parentheses were disambiguated as a function "
23241 "declaration"))
23243 /* () means value-initialization (C++03 and up); {} (C++11 and up)
23244 means value-initialization or aggregate-initialization, nothing
23245 means default-initialization. We can only suggest removing the
23246 parentheses/adding {} if T has a default constructor. */
23247 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
23249 gcc_rich_location iloc (loc);
23250 iloc.add_fixit_remove ();
23251 inform (&iloc, "remove parentheses to default-initialize "
23252 "a variable");
23253 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23255 if (CP_AGGREGATE_TYPE_P (type))
23256 inform (loc, "or replace parentheses with braces to "
23257 "aggregate-initialize a variable");
23258 else
23259 inform (loc, "or replace parentheses with braces to "
23260 "value-initialize a variable");
23264 return;
23267 /* If we had (...) or the parameter-list wasn't parenthesized,
23268 we're done. */
23269 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
23270 return;
23272 /* The T t(X()) case. */
23273 if (list_length (params) == 2)
23275 if (warning_at (loc, OPT_Wvexing_parse,
23276 "parentheses were disambiguated as a function "
23277 "declaration"))
23279 gcc_rich_location iloc (loc);
23280 /* {}-initialization means that we can use an initializer-list
23281 constructor if no default constructor is available, so don't
23282 suggest using {} for classes that have an initializer_list
23283 constructor. */
23284 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23286 iloc.add_fixit_replace (get_start (loc), "{");
23287 iloc.add_fixit_replace (get_finish (loc), "}");
23288 inform (&iloc, "replace parentheses with braces to declare a "
23289 "variable");
23291 else
23293 iloc.add_fixit_insert_after (get_start (loc), "(");
23294 iloc.add_fixit_insert_before (get_finish (loc), ")");
23295 inform (&iloc, "add parentheses to declare a variable");
23299 /* The T t(X(), X()) case. */
23300 else if (warning_at (loc, OPT_Wvexing_parse,
23301 "parentheses were disambiguated as a function "
23302 "declaration"))
23304 gcc_rich_location iloc (loc);
23305 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23307 iloc.add_fixit_replace (get_start (loc), "{");
23308 iloc.add_fixit_replace (get_finish (loc), "}");
23309 inform (&iloc, "replace parentheses with braces to declare a "
23310 "variable");
23315 /* If DECLARATOR with DECL_SPECS is a function declarator that has
23316 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
23317 has the same meaning as in cp_parser_declarator. */
23319 static void
23320 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
23321 cp_decl_specifier_seq *decl_specs,
23322 cp_declarator *declarator,
23323 int *ctor_dtor_or_conv_p)
23325 if (cxx_dialect >= cxx17
23326 && *ctor_dtor_or_conv_p <= 0
23327 && !decl_specs->type
23328 && !decl_specs->any_type_specifiers_p
23329 && function_declarator_p (declarator))
23331 cp_declarator *id = get_id_declarator (declarator);
23332 tree name = id->u.id.unqualified_name;
23333 parser->scope = id->u.id.qualifying_scope;
23334 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
23335 if (tmpl
23336 && (DECL_CLASS_TEMPLATE_P (tmpl)
23337 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
23339 id->u.id.unqualified_name = dguide_name (tmpl);
23340 id->u.id.sfk = sfk_deduction_guide;
23341 *ctor_dtor_or_conv_p = 1;
23346 /* Declarators [gram.dcl.decl] */
23348 /* Parse an init-declarator.
23350 init-declarator:
23351 declarator initializer [opt]
23353 GNU Extension:
23355 init-declarator:
23356 declarator asm-specification [opt] attributes [opt] initializer [opt]
23358 function-definition:
23359 decl-specifier-seq [opt] declarator ctor-initializer [opt]
23360 function-body
23361 decl-specifier-seq [opt] declarator function-try-block
23363 GNU Extension:
23365 function-definition:
23366 __extension__ function-definition
23368 TM Extension:
23370 function-definition:
23371 decl-specifier-seq [opt] declarator function-transaction-block
23373 The parser flags FLAGS is used to control type-specifier parsing.
23375 The DECL_SPECIFIERS apply to this declarator. Returns a
23376 representation of the entity declared. If MEMBER_P is TRUE, then
23377 this declarator appears in a class scope. The new DECL created by
23378 this declarator is returned.
23380 The CHECKS are access checks that should be performed once we know
23381 what entity is being declared (and, therefore, what classes have
23382 befriended it).
23384 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
23385 for a function-definition here as well. If the declarator is a
23386 declarator for a function-definition, *FUNCTION_DEFINITION_P will
23387 be TRUE upon return. By that point, the function-definition will
23388 have been completely parsed.
23390 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
23391 is FALSE.
23393 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
23394 parsed declaration if it is an uninitialized single declarator not followed
23395 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
23396 if present, will not be consumed. If returned, this declarator will be
23397 created with SD_INITIALIZED but will not call cp_finish_decl.
23399 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
23400 and there is an initializer, the pointed location_t is set to the
23401 location of the '=' or `(', or '{' in C++11 token introducing the
23402 initializer. */
23404 static tree
23405 cp_parser_init_declarator (cp_parser* parser,
23406 cp_parser_flags flags,
23407 cp_decl_specifier_seq *decl_specifiers,
23408 vec<deferred_access_check, va_gc> *checks,
23409 bool function_definition_allowed_p,
23410 bool member_p,
23411 int declares_class_or_enum,
23412 bool* function_definition_p,
23413 tree* maybe_range_for_decl,
23414 location_t* init_loc,
23415 tree* auto_result)
23417 cp_token *token = NULL, *asm_spec_start_token = NULL,
23418 *attributes_start_token = NULL;
23419 cp_declarator *declarator;
23420 tree prefix_attributes;
23421 tree attributes = NULL;
23422 tree asm_specification;
23423 tree initializer;
23424 tree decl = NULL_TREE;
23425 tree scope;
23426 int is_initialized;
23427 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
23428 initialized with "= ..", CPP_OPEN_PAREN if initialized with
23429 "(...)". */
23430 enum cpp_ttype initialization_kind;
23431 bool is_direct_init = false;
23432 bool is_non_constant_init;
23433 int ctor_dtor_or_conv_p;
23434 bool friend_p = cp_parser_friend_p (decl_specifiers);
23435 bool static_p = decl_specifiers->storage_class == sc_static;
23436 tree pushed_scope = NULL_TREE;
23437 bool range_for_decl_p = false;
23438 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23439 location_t tmp_init_loc = UNKNOWN_LOCATION;
23441 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
23442 flags |= CP_PARSER_FLAGS_CONSTEVAL;
23444 /* Assume that this is not the declarator for a function
23445 definition. */
23446 if (function_definition_p)
23447 *function_definition_p = false;
23449 /* Default arguments are only permitted for function parameters. */
23450 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23451 parser->default_arg_ok_p = false;
23453 /* Defer access checks while parsing the declarator; we cannot know
23454 what names are accessible until we know what is being
23455 declared. */
23456 resume_deferring_access_checks ();
23458 token = cp_lexer_peek_token (parser->lexer);
23460 /* Parse the declarator. */
23461 declarator
23462 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23463 flags, &ctor_dtor_or_conv_p,
23464 /*parenthesized_p=*/NULL,
23465 member_p, friend_p, static_p);
23466 /* Gather up the deferred checks. */
23467 stop_deferring_access_checks ();
23469 parser->default_arg_ok_p = saved_default_arg_ok_p;
23471 /* If the DECLARATOR was erroneous, there's no need to go
23472 further. */
23473 if (declarator == cp_error_declarator)
23474 return error_mark_node;
23476 /* Check that the number of template-parameter-lists is OK. */
23477 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
23478 token->location))
23479 return error_mark_node;
23481 if (declares_class_or_enum & 2)
23482 cp_parser_check_for_definition_in_return_type (declarator,
23483 decl_specifiers->type,
23484 decl_specifiers->locations[ds_type_spec]);
23486 /* Figure out what scope the entity declared by the DECLARATOR is
23487 located in. `grokdeclarator' sometimes changes the scope, so
23488 we compute it now. */
23489 scope = get_scope_of_declarator (declarator);
23491 /* Perform any lookups in the declared type which were thought to be
23492 dependent, but are not in the scope of the declarator. */
23493 decl_specifiers->type
23494 = maybe_update_decl_type (decl_specifiers->type, scope);
23496 /* If we're allowing GNU extensions, look for an
23497 asm-specification. */
23498 if (cp_parser_allow_gnu_extensions_p (parser))
23500 /* Look for an asm-specification. */
23501 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
23502 asm_specification = cp_parser_asm_specification_opt (parser);
23504 else
23505 asm_specification = NULL_TREE;
23507 /* Gather the attributes that were provided with the
23508 decl-specifiers. */
23509 prefix_attributes = decl_specifiers->attributes;
23511 /* Look for attributes. */
23512 attributes_start_token = cp_lexer_peek_token (parser->lexer);
23513 attributes = cp_parser_attributes_opt (parser);
23515 /* Peek at the next token. */
23516 token = cp_lexer_peek_token (parser->lexer);
23518 bool bogus_implicit_tmpl = false;
23520 if (function_declarator_p (declarator))
23522 /* Handle C++17 deduction guides. Note that class-scope
23523 non-template deduction guides are instead handled in
23524 cp_parser_member_declaration. */
23525 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23526 decl_specifiers,
23527 declarator,
23528 &ctor_dtor_or_conv_p);
23530 if (!member_p && !cp_parser_error_occurred (parser))
23531 warn_about_ambiguous_parse (decl_specifiers, declarator);
23533 /* Check to see if the token indicates the start of a
23534 function-definition. */
23535 if (cp_parser_token_starts_function_definition_p (token))
23537 if (!function_definition_allowed_p)
23539 /* If a function-definition should not appear here, issue an
23540 error message. */
23541 cp_parser_error (parser,
23542 "a function-definition is not allowed here");
23543 return error_mark_node;
23546 location_t func_brace_location
23547 = cp_lexer_peek_token (parser->lexer)->location;
23549 /* Neither attributes nor an asm-specification are allowed
23550 on a function-definition. */
23551 if (asm_specification)
23552 error_at (asm_spec_start_token->location,
23553 "an %<asm%> specification is not allowed "
23554 "on a function-definition");
23555 if (attributes)
23556 error_at (attributes_start_token->location,
23557 "attributes are not allowed "
23558 "on a function-definition");
23559 /* This is a function-definition. */
23560 *function_definition_p = true;
23562 /* Parse the function definition. */
23563 if (member_p)
23564 decl = cp_parser_save_member_function_body (parser,
23565 decl_specifiers,
23566 declarator,
23567 prefix_attributes);
23568 else
23569 decl =
23570 (cp_parser_function_definition_from_specifiers_and_declarator
23571 (parser, decl_specifiers, prefix_attributes, declarator));
23573 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23575 /* This is where the prologue starts... */
23576 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23577 = func_brace_location;
23580 return decl;
23583 else if (parser->fully_implicit_function_template_p)
23585 /* A non-template declaration involving a function parameter list
23586 containing an implicit template parameter will be made into a
23587 template. If the resulting declaration is not going to be an
23588 actual function then finish the template scope here to prevent it.
23589 An error message will be issued once we have a decl to talk about.
23591 FIXME probably we should do type deduction rather than create an
23592 implicit template, but the standard currently doesn't allow it. */
23593 bogus_implicit_tmpl = true;
23594 finish_fully_implicit_template (parser, NULL_TREE);
23597 /* [dcl.dcl]
23599 Only in function declarations for constructors, destructors, type
23600 conversions, and deduction guides can the decl-specifier-seq be omitted.
23602 We explicitly postpone this check past the point where we handle
23603 function-definitions because we tolerate function-definitions
23604 that are missing their return types in some modes. */
23605 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23607 cp_parser_error (parser,
23608 "expected constructor, destructor, or type conversion");
23609 return error_mark_node;
23612 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23613 an initializer as well. */
23614 if (token->type == CPP_EQ
23615 || token->type == CPP_OPEN_PAREN
23616 || token->type == CPP_OPEN_BRACE)
23618 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23619 It isn't; it's an expression. (Here '(i)' would have already been
23620 parsed as a declarator.) */
23621 if (token->type == CPP_OPEN_PAREN
23622 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23624 cp_lexer_save_tokens (parser->lexer);
23625 cp_lexer_consume_token (parser->lexer);
23626 cp_parser_skip_to_closing_parenthesis (parser,
23627 /*recovering*/false,
23628 /*or_comma*/false,
23629 /*consume_paren*/true);
23630 /* If this is an initializer, only a ',' or ';' can follow: either
23631 we have another init-declarator, or we're at the end of an
23632 init-declarator-list which can only be followed by a ';'. */
23633 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23634 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23635 cp_lexer_rollback_tokens (parser->lexer);
23636 if (UNLIKELY (!ok))
23637 /* Not an init-declarator. */
23638 return error_mark_node;
23640 is_initialized = SD_INITIALIZED;
23641 initialization_kind = token->type;
23642 declarator->init_loc = token->location;
23643 if (maybe_range_for_decl)
23644 *maybe_range_for_decl = error_mark_node;
23645 tmp_init_loc = token->location;
23646 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23647 *init_loc = tmp_init_loc;
23649 if (token->type == CPP_EQ
23650 && function_declarator_p (declarator))
23652 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23653 if (t2->keyword == RID_DEFAULT)
23654 is_initialized = SD_DEFAULTED;
23655 else if (t2->keyword == RID_DELETE)
23656 is_initialized = SD_DELETED;
23659 else
23661 /* If the init-declarator isn't initialized and isn't followed by a
23662 `,' or `;', it's not a valid init-declarator. */
23663 if (token->type != CPP_COMMA
23664 && token->type != CPP_SEMICOLON)
23666 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23667 range_for_decl_p = true;
23668 else
23670 if (!maybe_range_for_decl)
23671 cp_parser_error (parser, "expected initializer");
23672 return error_mark_node;
23675 is_initialized = SD_UNINITIALIZED;
23676 initialization_kind = CPP_EOF;
23679 /* Because start_decl has side-effects, we should only call it if we
23680 know we're going ahead. By this point, we know that we cannot
23681 possibly be looking at any other construct. */
23682 cp_parser_commit_to_tentative_parse (parser);
23684 /* Enter the newly declared entry in the symbol table. If we're
23685 processing a declaration in a class-specifier, we wait until
23686 after processing the initializer. */
23687 if (!member_p)
23689 if (parser->in_unbraced_linkage_specification_p)
23690 decl_specifiers->storage_class = sc_extern;
23691 decl = start_decl (declarator, decl_specifiers,
23692 range_for_decl_p? SD_INITIALIZED : is_initialized,
23693 attributes, prefix_attributes, &pushed_scope);
23694 cp_finalize_omp_declare_simd (parser, decl);
23695 cp_finalize_oacc_routine (parser, decl, false);
23696 /* Adjust location of decl if declarator->id_loc is more appropriate:
23697 set, and decl wasn't merged with another decl, in which case its
23698 location would be different from input_location, and more accurate. */
23699 if (DECL_P (decl)
23700 && declarator->id_loc != UNKNOWN_LOCATION
23701 && DECL_SOURCE_LOCATION (decl) == input_location)
23702 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23704 else if (scope)
23705 /* Enter the SCOPE. That way unqualified names appearing in the
23706 initializer will be looked up in SCOPE. */
23707 pushed_scope = push_scope (scope);
23709 /* Perform deferred access control checks, now that we know in which
23710 SCOPE the declared entity resides. */
23711 if (!member_p && decl)
23713 tree saved_current_function_decl = NULL_TREE;
23715 /* If the entity being declared is a function, pretend that we
23716 are in its scope. If it is a `friend', it may have access to
23717 things that would not otherwise be accessible. */
23718 if (TREE_CODE (decl) == FUNCTION_DECL)
23720 saved_current_function_decl = current_function_decl;
23721 current_function_decl = decl;
23724 /* Perform access checks for template parameters. */
23725 cp_parser_perform_template_parameter_access_checks (checks);
23727 /* Perform the access control checks for the declarator and the
23728 decl-specifiers. */
23729 perform_deferred_access_checks (tf_warning_or_error);
23731 /* Restore the saved value. */
23732 if (TREE_CODE (decl) == FUNCTION_DECL)
23733 current_function_decl = saved_current_function_decl;
23736 /* Parse the initializer. */
23737 initializer = NULL_TREE;
23738 is_direct_init = false;
23739 is_non_constant_init = true;
23740 if (is_initialized)
23742 if (function_declarator_p (declarator))
23744 if (initialization_kind == CPP_EQ)
23745 initializer = cp_parser_pure_specifier (parser);
23746 else
23748 /* If the declaration was erroneous, we don't really
23749 know what the user intended, so just silently
23750 consume the initializer. */
23751 if (decl != error_mark_node)
23752 error_at (tmp_init_loc, "initializer provided for function");
23753 cp_parser_skip_to_closing_parenthesis (parser,
23754 /*recovering=*/true,
23755 /*or_comma=*/false,
23756 /*consume_paren=*/true);
23759 else
23761 /* We want to record the extra mangling scope for in-class
23762 initializers of class members and initializers of static
23763 data member templates and namespace-scope initializers.
23764 The former involves deferring parsing of the initializer
23765 until end of class as with default arguments. So right
23766 here we only handle the latter two. */
23767 bool has_lambda_scope = false;
23769 if (decl != error_mark_node
23770 && !member_p
23771 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23772 has_lambda_scope = true;
23774 if (has_lambda_scope)
23775 start_lambda_scope (decl);
23776 initializer = cp_parser_initializer (parser,
23777 &is_direct_init,
23778 &is_non_constant_init);
23779 if (has_lambda_scope)
23780 finish_lambda_scope ();
23781 if (initializer == error_mark_node)
23782 cp_parser_skip_to_end_of_statement (parser);
23786 /* The old parser allows attributes to appear after a parenthesized
23787 initializer. Mark Mitchell proposed removing this functionality
23788 on the GCC mailing lists on 2002-08-13. This parser accepts the
23789 attributes -- but ignores them. Made a permerror in GCC 8. */
23790 if (cp_parser_allow_gnu_extensions_p (parser)
23791 && initialization_kind == CPP_OPEN_PAREN
23792 && cp_parser_attributes_opt (parser)
23793 && permerror (input_location,
23794 "attributes after parenthesized initializer ignored"))
23796 static bool hint;
23797 if (flag_permissive && !hint)
23799 hint = true;
23800 inform (input_location,
23801 "this flexibility is deprecated and will be removed");
23805 /* And now complain about a non-function implicit template. */
23806 if (bogus_implicit_tmpl && decl != error_mark_node)
23807 error_at (DECL_SOURCE_LOCATION (decl),
23808 "non-function %qD declared as implicit template", decl);
23810 /* For an in-class declaration, use `grokfield' to create the
23811 declaration. */
23812 if (member_p)
23814 if (pushed_scope)
23816 pop_scope (pushed_scope);
23817 pushed_scope = NULL_TREE;
23819 decl = grokfield (declarator, decl_specifiers,
23820 initializer, !is_non_constant_init,
23821 /*asmspec=*/NULL_TREE,
23822 attr_chainon (attributes, prefix_attributes));
23823 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23824 cp_parser_save_default_args (parser, decl);
23825 cp_finalize_omp_declare_simd (parser, decl);
23826 cp_finalize_oacc_routine (parser, decl, false);
23829 /* Finish processing the declaration. But, skip member
23830 declarations. */
23831 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23833 cp_finish_decl (decl,
23834 initializer, !is_non_constant_init,
23835 asm_specification,
23836 /* If the initializer is in parentheses, then this is
23837 a direct-initialization, which means that an
23838 `explicit' constructor is OK. Otherwise, an
23839 `explicit' constructor cannot be used. */
23840 ((is_direct_init || !is_initialized)
23841 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23843 else if ((cxx_dialect != cxx98) && friend_p
23844 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23845 /* Core issue #226 (C++0x only): A default template-argument
23846 shall not be specified in a friend class template
23847 declaration. */
23848 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23849 /*is_partial=*/false, /*is_friend_decl=*/1);
23851 if (!friend_p && pushed_scope)
23852 pop_scope (pushed_scope);
23854 if (function_declarator_p (declarator)
23855 && parser->fully_implicit_function_template_p)
23857 if (member_p)
23858 decl = finish_fully_implicit_template (parser, decl);
23859 else
23860 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23863 if (auto_result && is_initialized && decl_specifiers->type
23864 && type_uses_auto (decl_specifiers->type))
23865 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23867 return decl;
23870 /* Parse a declarator.
23872 declarator:
23873 direct-declarator
23874 ptr-operator declarator
23876 abstract-declarator:
23877 ptr-operator abstract-declarator [opt]
23878 direct-abstract-declarator
23880 GNU Extensions:
23882 declarator:
23883 attributes [opt] direct-declarator
23884 attributes [opt] ptr-operator declarator
23886 abstract-declarator:
23887 attributes [opt] ptr-operator abstract-declarator [opt]
23888 attributes [opt] direct-abstract-declarator
23890 The parser flags FLAGS is used to control type-specifier parsing.
23892 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23893 detect constructors, destructors, deduction guides, or conversion operators.
23894 It is set to -1 if the declarator is a name, and +1 if it is a
23895 function. Otherwise it is set to zero. Usually you just want to
23896 test for >0, but internally the negative value is used.
23898 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23899 a decl-specifier-seq unless it declares a constructor, destructor,
23900 or conversion. It might seem that we could check this condition in
23901 semantic analysis, rather than parsing, but that makes it difficult
23902 to handle something like `f()'. We want to notice that there are
23903 no decl-specifiers, and therefore realize that this is an
23904 expression, not a declaration.)
23906 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23907 the declarator is a direct-declarator of the form "(...)".
23909 MEMBER_P is true iff this declarator is a member-declarator.
23911 FRIEND_P is true iff this declarator is a friend.
23913 STATIC_P is true iff the keyword static was seen. */
23915 static cp_declarator *
23916 cp_parser_declarator (cp_parser* parser,
23917 cp_parser_declarator_kind dcl_kind,
23918 cp_parser_flags flags,
23919 int* ctor_dtor_or_conv_p,
23920 bool* parenthesized_p,
23921 bool member_p, bool friend_p, bool static_p)
23923 cp_declarator *declarator;
23924 enum tree_code code;
23925 cp_cv_quals cv_quals;
23926 tree class_type;
23927 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23929 /* Assume this is not a constructor, destructor, or type-conversion
23930 operator. */
23931 if (ctor_dtor_or_conv_p)
23932 *ctor_dtor_or_conv_p = 0;
23934 if (cp_parser_allow_gnu_extensions_p (parser))
23935 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23937 /* Check for the ptr-operator production. */
23938 cp_parser_parse_tentatively (parser);
23939 /* Parse the ptr-operator. */
23940 code = cp_parser_ptr_operator (parser,
23941 &class_type,
23942 &cv_quals,
23943 &std_attributes);
23945 /* If that worked, then we have a ptr-operator. */
23946 if (cp_parser_parse_definitely (parser))
23948 /* If a ptr-operator was found, then this declarator was not
23949 parenthesized. */
23950 if (parenthesized_p)
23951 *parenthesized_p = false;
23952 /* The dependent declarator is optional if we are parsing an
23953 abstract-declarator. */
23954 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23955 cp_parser_parse_tentatively (parser);
23957 /* Parse the dependent declarator. */
23958 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23959 /*ctor_dtor_or_conv_p=*/NULL,
23960 /*parenthesized_p=*/NULL,
23961 member_p, friend_p, static_p);
23963 /* If we are parsing an abstract-declarator, we must handle the
23964 case where the dependent declarator is absent. */
23965 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23966 && !cp_parser_parse_definitely (parser))
23967 declarator = NULL;
23969 declarator = cp_parser_make_indirect_declarator
23970 (code, class_type, cv_quals, declarator, std_attributes);
23972 /* Everything else is a direct-declarator. */
23973 else
23975 if (parenthesized_p)
23976 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23977 CPP_OPEN_PAREN);
23978 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23979 flags, ctor_dtor_or_conv_p,
23980 member_p, friend_p, static_p);
23983 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23984 declarator->attributes = gnu_attributes;
23985 return declarator;
23988 /* Parse a direct-declarator or direct-abstract-declarator.
23990 direct-declarator:
23991 declarator-id
23992 direct-declarator ( parameter-declaration-clause )
23993 cv-qualifier-seq [opt]
23994 ref-qualifier [opt]
23995 exception-specification [opt]
23996 direct-declarator [ constant-expression [opt] ]
23997 ( declarator )
23999 direct-abstract-declarator:
24000 direct-abstract-declarator [opt]
24001 ( parameter-declaration-clause )
24002 cv-qualifier-seq [opt]
24003 ref-qualifier [opt]
24004 exception-specification [opt]
24005 direct-abstract-declarator [opt] [ constant-expression [opt] ]
24006 ( abstract-declarator )
24008 Returns a representation of the declarator. DCL_KIND is
24009 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
24010 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
24011 we are parsing a direct-declarator. It is
24012 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
24013 of ambiguity we prefer an abstract declarator, as per
24014 [dcl.ambig.res].
24015 The parser flags FLAGS is used to control type-specifier parsing.
24016 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
24017 as for cp_parser_declarator. */
24019 static cp_declarator *
24020 cp_parser_direct_declarator (cp_parser* parser,
24021 cp_parser_declarator_kind dcl_kind,
24022 cp_parser_flags flags,
24023 int* ctor_dtor_or_conv_p,
24024 bool member_p, bool friend_p, bool static_p)
24026 cp_token *token;
24027 cp_declarator *declarator = NULL;
24028 tree scope = NULL_TREE;
24029 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24030 bool saved_in_declarator_p = parser->in_declarator_p;
24031 bool first = true;
24032 tree pushed_scope = NULL_TREE;
24033 cp_token *open_paren = NULL, *close_paren = NULL;
24035 while (true)
24037 /* Peek at the next token. */
24038 token = cp_lexer_peek_token (parser->lexer);
24039 if (token->type == CPP_OPEN_PAREN
24040 || (first
24041 && dcl_kind != CP_PARSER_DECLARATOR_NAMED
24042 && token->type == CPP_ELLIPSIS
24043 && cxx_dialect > cxx98
24044 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)))
24046 /* This is either a parameter-declaration-clause, or a
24047 parenthesized declarator. When we know we are parsing a
24048 named declarator, it must be a parenthesized declarator
24049 if FIRST is true. For instance, `(int)' is a
24050 parameter-declaration-clause, with an omitted
24051 direct-abstract-declarator. But `((*))', is a
24052 parenthesized abstract declarator. Finally, when T is a
24053 template parameter `(T)' is a
24054 parameter-declaration-clause, and not a parenthesized
24055 named declarator.
24057 We first try and parse a parameter-declaration-clause,
24058 and then try a nested declarator (if FIRST is true).
24060 It is not an error for it not to be a
24061 parameter-declaration-clause, even when FIRST is
24062 false. Consider,
24064 int i (int);
24065 int i (3);
24067 The first is the declaration of a function while the
24068 second is the definition of a variable, including its
24069 initializer.
24071 Having seen only the parenthesis, we cannot know which of
24072 these two alternatives should be selected. Even more
24073 complex are examples like:
24075 int i (int (a));
24076 int i (int (3));
24078 The former is a function-declaration; the latter is a
24079 variable initialization.
24081 Thus again, we try a parameter-declaration-clause, and if
24082 that fails, we back out and return. */
24083 bool pack_expansion_p = token->type == CPP_ELLIPSIS;
24085 if (pack_expansion_p)
24086 /* Consume the `...' */
24087 cp_lexer_consume_token (parser->lexer);
24089 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
24091 tree params;
24092 bool is_declarator = false;
24094 open_paren = NULL;
24096 /* In a member-declarator, the only valid interpretation
24097 of a parenthesis is the start of a
24098 parameter-declaration-clause. (It is invalid to
24099 initialize a static data member with a parenthesized
24100 initializer; only the "=" form of initialization is
24101 permitted.) */
24102 if (!member_p)
24103 cp_parser_parse_tentatively (parser);
24105 /* Consume the `('. */
24106 const location_t parens_start = token->location;
24107 matching_parens parens;
24108 parens.consume_open (parser);
24109 if (first)
24111 /* If this is going to be an abstract declarator, we're
24112 in a declarator and we can't have default args. */
24113 parser->default_arg_ok_p = false;
24114 parser->in_declarator_p = true;
24117 begin_scope (sk_function_parms, NULL_TREE);
24119 /* Parse the parameter-declaration-clause. */
24120 params
24121 = cp_parser_parameter_declaration_clause (parser, flags);
24122 const location_t parens_end
24123 = cp_lexer_peek_token (parser->lexer)->location;
24125 /* Consume the `)'. */
24126 parens.require_close (parser);
24128 /* For code like
24129 int x(auto(42));
24130 A a(auto(i), 42);
24131 we have synthesized an implicit template parameter and marked
24132 what we thought was a function as an implicit function template.
24133 But now, having seen the whole parameter list, we know it's not
24134 a function declaration, so undo that. */
24135 if (cp_parser_error_occurred (parser)
24136 && parser->fully_implicit_function_template_p
24137 /* Don't do this for the inner (). */
24138 && parser->default_arg_ok_p)
24139 abort_fully_implicit_template (parser);
24141 /* If all went well, parse the cv-qualifier-seq,
24142 ref-qualifier and the exception-specification. */
24143 if (member_p || cp_parser_parse_definitely (parser))
24145 cp_cv_quals cv_quals;
24146 cp_virt_specifiers virt_specifiers;
24147 cp_ref_qualifier ref_qual;
24148 tree exception_specification;
24149 tree late_return;
24150 tree attrs;
24151 bool memfn = (member_p || (pushed_scope
24152 && CLASS_TYPE_P (pushed_scope)));
24153 unsigned char local_variables_forbidden_p
24154 = parser->local_variables_forbidden_p;
24155 /* 'this' is not allowed in static member functions. */
24156 if (static_p || friend_p)
24157 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24159 is_declarator = true;
24161 if (ctor_dtor_or_conv_p)
24162 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
24163 first = false;
24165 /* Parse the cv-qualifier-seq. */
24166 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24167 /* Parse the ref-qualifier. */
24168 ref_qual = cp_parser_ref_qualifier_opt (parser);
24169 /* Parse the tx-qualifier. */
24170 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
24172 tree save_ccp = current_class_ptr;
24173 tree save_ccr = current_class_ref;
24174 if (memfn && !friend_p && !static_p)
24175 /* DR 1207: 'this' is in scope after the cv-quals. */
24176 inject_this_parameter (current_class_type, cv_quals);
24178 /* If it turned out that this is e.g. a pointer to a
24179 function, we don't want to delay noexcept parsing. */
24180 if (declarator == NULL || declarator->kind != cdk_id)
24181 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
24183 /* Parse the exception-specification. */
24184 exception_specification
24185 = cp_parser_exception_specification_opt (parser,
24186 flags);
24188 attrs = cp_parser_std_attribute_spec_seq (parser);
24190 cp_omp_declare_simd_data odsd;
24191 if ((flag_openmp || flag_openmp_simd)
24192 && declarator
24193 && declarator->std_attributes
24194 && declarator->kind == cdk_id)
24196 tree *pa = &declarator->std_attributes;
24197 cp_parser_handle_directive_omp_attributes (parser, pa,
24198 &odsd, false);
24201 /* In here, we handle cases where attribute is used after
24202 the function declaration. For example:
24203 void func (int x) __attribute__((vector(..))); */
24204 tree gnu_attrs = NULL_TREE;
24205 tree requires_clause = NULL_TREE;
24206 late_return
24207 = cp_parser_late_return_type_opt (parser, declarator,
24208 requires_clause);
24210 cp_finalize_omp_declare_simd (parser, &odsd);
24212 /* Parse the virt-specifier-seq. */
24213 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24215 location_t parens_loc = make_location (parens_start,
24216 parens_start,
24217 parens_end);
24218 /* Create the function-declarator. */
24219 declarator = make_call_declarator (declarator,
24220 params,
24221 cv_quals,
24222 virt_specifiers,
24223 ref_qual,
24224 tx_qual,
24225 exception_specification,
24226 late_return,
24227 requires_clause,
24228 attrs,
24229 parens_loc);
24230 declarator->attributes = gnu_attrs;
24231 declarator->parameter_pack_p |= pack_expansion_p;
24232 /* Any subsequent parameter lists are to do with
24233 return type, so are not those of the declared
24234 function. */
24235 parser->default_arg_ok_p = false;
24237 current_class_ptr = save_ccp;
24238 current_class_ref = save_ccr;
24240 /* Restore the state of local_variables_forbidden_p. */
24241 parser->local_variables_forbidden_p
24242 = local_variables_forbidden_p;
24245 /* Remove the function parms from scope. */
24246 pop_bindings_and_leave_scope ();
24248 if (is_declarator)
24249 /* Repeat the main loop. */
24250 continue;
24253 /* If this is the first, we can try a parenthesized
24254 declarator. */
24255 if (first && !pack_expansion_p)
24257 bool saved_in_type_id_in_expr_p;
24259 parser->default_arg_ok_p = saved_default_arg_ok_p;
24260 parser->in_declarator_p = saved_in_declarator_p;
24262 open_paren = token;
24263 /* Consume the `('. */
24264 matching_parens parens;
24265 parens.consume_open (parser);
24266 /* Parse the nested declarator. */
24267 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24268 parser->in_type_id_in_expr_p = true;
24269 declarator
24270 = cp_parser_declarator (parser, dcl_kind, flags,
24271 ctor_dtor_or_conv_p,
24272 /*parenthesized_p=*/NULL,
24273 member_p, friend_p,
24274 /*static_p=*/false);
24275 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24276 first = false;
24277 /* Expect a `)'. */
24278 close_paren = cp_lexer_peek_token (parser->lexer);
24279 if (!parens.require_close (parser))
24280 declarator = cp_error_declarator;
24281 if (declarator == cp_error_declarator)
24282 break;
24284 goto handle_declarator;
24286 /* Otherwise, we must be done. */
24287 else
24288 break;
24290 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
24291 && token->type == CPP_OPEN_SQUARE
24292 && !cp_next_tokens_can_be_attribute_p (parser))
24294 /* Parse an array-declarator. */
24295 tree bounds, attrs;
24297 if (ctor_dtor_or_conv_p)
24298 *ctor_dtor_or_conv_p = 0;
24300 open_paren = NULL;
24301 first = false;
24302 parser->default_arg_ok_p = false;
24303 parser->in_declarator_p = true;
24304 /* Consume the `['. */
24305 cp_lexer_consume_token (parser->lexer);
24306 /* Peek at the next token. */
24307 token = cp_lexer_peek_token (parser->lexer);
24308 /* If the next token is `]', then there is no
24309 constant-expression. */
24310 if (token->type != CPP_CLOSE_SQUARE)
24312 bool non_constant_p;
24313 bounds
24314 = cp_parser_constant_expression (parser,
24315 /*allow_non_constant=*/true,
24316 &non_constant_p);
24317 if (!non_constant_p)
24318 /* OK */;
24319 else if (error_operand_p (bounds))
24320 /* Already gave an error. */;
24321 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24322 /* Let compute_array_index_type diagnose this. */;
24323 else if (!parser->in_function_body
24324 || parsing_function_declarator ())
24326 /* Normally, the array bound must be an integral constant
24327 expression. However, as an extension, we allow VLAs
24328 in function scopes as long as they aren't part of a
24329 parameter declaration. */
24330 cp_parser_error (parser,
24331 "array bound is not an integer constant");
24332 bounds = error_mark_node;
24334 else if (processing_template_decl
24335 && !type_dependent_expression_p (bounds))
24337 /* Remember this wasn't a constant-expression. */
24338 bounds = build_nop (TREE_TYPE (bounds), bounds);
24339 TREE_SIDE_EFFECTS (bounds) = 1;
24342 else
24343 bounds = NULL_TREE;
24344 /* Look for the closing `]'. */
24345 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24347 declarator = cp_error_declarator;
24348 break;
24351 attrs = cp_parser_std_attribute_spec_seq (parser);
24352 declarator = make_array_declarator (declarator, bounds);
24353 declarator->std_attributes = attrs;
24355 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
24358 tree qualifying_scope;
24359 tree unqualified_name;
24360 tree attrs;
24361 special_function_kind sfk;
24362 bool abstract_ok;
24363 bool pack_expansion_p = false;
24364 cp_token *declarator_id_start_token;
24366 /* Parse a declarator-id */
24367 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
24368 if (abstract_ok)
24370 cp_parser_parse_tentatively (parser);
24372 /* If we see an ellipsis, we should be looking at a
24373 parameter pack. */
24374 if (token->type == CPP_ELLIPSIS)
24376 /* Consume the `...' */
24377 cp_lexer_consume_token (parser->lexer);
24379 pack_expansion_p = true;
24383 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
24384 unqualified_name
24385 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
24386 qualifying_scope = parser->scope;
24387 if (abstract_ok)
24389 bool okay = false;
24391 if (!unqualified_name && pack_expansion_p)
24393 /* Check whether an error occurred. */
24394 okay = !cp_parser_error_occurred (parser);
24396 /* We already consumed the ellipsis to mark a
24397 parameter pack, but we have no way to report it,
24398 so abort the tentative parse. We will be exiting
24399 immediately anyway. */
24400 cp_parser_abort_tentative_parse (parser);
24402 else
24403 okay = cp_parser_parse_definitely (parser);
24405 if (!okay)
24406 unqualified_name = error_mark_node;
24407 else if (unqualified_name
24408 && (qualifying_scope
24409 || (!identifier_p (unqualified_name))))
24411 cp_parser_error (parser, "expected unqualified-id");
24412 unqualified_name = error_mark_node;
24416 if (!unqualified_name)
24417 return NULL;
24418 if (unqualified_name == error_mark_node)
24420 declarator = cp_error_declarator;
24421 pack_expansion_p = false;
24422 declarator->parameter_pack_p = false;
24423 break;
24426 attrs = cp_parser_std_attribute_spec_seq (parser);
24428 if (qualifying_scope && at_namespace_scope_p ()
24429 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
24431 /* In the declaration of a member of a template class
24432 outside of the class itself, the SCOPE will sometimes
24433 be a TYPENAME_TYPE. For example, given:
24435 template <typename T>
24436 int S<T>::R::i = 3;
24438 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
24439 this context, we must resolve S<T>::R to an ordinary
24440 type, rather than a typename type.
24442 The reason we normally avoid resolving TYPENAME_TYPEs
24443 is that a specialization of `S' might render
24444 `S<T>::R' not a type. However, if `S' is
24445 specialized, then this `i' will not be used, so there
24446 is no harm in resolving the types here. */
24447 tree type;
24449 /* Resolve the TYPENAME_TYPE. */
24450 type = resolve_typename_type (qualifying_scope,
24451 /*only_current_p=*/false);
24452 /* If that failed, the declarator is invalid. */
24453 if (TREE_CODE (type) == TYPENAME_TYPE)
24455 if (typedef_variant_p (type))
24456 error_at (declarator_id_start_token->location,
24457 "cannot define member of dependent typedef "
24458 "%qT", type);
24459 else
24460 error_at (declarator_id_start_token->location,
24461 "%<%T::%E%> is not a type",
24462 TYPE_CONTEXT (qualifying_scope),
24463 TYPE_IDENTIFIER (qualifying_scope));
24465 qualifying_scope = type;
24468 sfk = sfk_none;
24470 if (unqualified_name)
24472 tree class_type;
24474 if (qualifying_scope
24475 && CLASS_TYPE_P (qualifying_scope))
24476 class_type = qualifying_scope;
24477 else
24478 class_type = current_class_type;
24480 if (TREE_CODE (unqualified_name) == TYPE_DECL)
24482 tree name_type = TREE_TYPE (unqualified_name);
24484 if (!class_type || !same_type_p (name_type, class_type))
24486 /* We do not attempt to print the declarator
24487 here because we do not have enough
24488 information about its original syntactic
24489 form. */
24490 cp_parser_error (parser, "invalid declarator");
24491 declarator = cp_error_declarator;
24492 break;
24494 else if (qualifying_scope
24495 && CLASSTYPE_USE_TEMPLATE (name_type))
24497 error_at (declarator_id_start_token->location,
24498 "invalid use of constructor as a template");
24499 inform (declarator_id_start_token->location,
24500 "use %<%T::%D%> instead of %<%T::%D%> to "
24501 "name the constructor in a qualified name",
24502 class_type,
24503 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
24504 class_type, name_type);
24505 declarator = cp_error_declarator;
24506 break;
24508 unqualified_name = constructor_name (class_type);
24511 if (class_type)
24513 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
24514 sfk = sfk_destructor;
24515 else if (identifier_p (unqualified_name)
24516 && IDENTIFIER_CONV_OP_P (unqualified_name))
24517 sfk = sfk_conversion;
24518 else if (/* There's no way to declare a constructor
24519 for an unnamed type, even if the type
24520 got a name for linkage purposes. */
24521 !TYPE_WAS_UNNAMED (class_type)
24522 /* Handle correctly (c++/19200):
24524 struct S {
24525 struct T{};
24526 friend void S(T);
24529 and also:
24531 namespace N {
24532 void S();
24535 struct S {
24536 friend void N::S();
24537 }; */
24538 && (!friend_p || class_type == qualifying_scope)
24539 && constructor_name_p (unqualified_name,
24540 class_type))
24541 sfk = sfk_constructor;
24542 else if (is_overloaded_fn (unqualified_name)
24543 && DECL_CONSTRUCTOR_P (get_first_fn
24544 (unqualified_name)))
24545 sfk = sfk_constructor;
24547 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24548 *ctor_dtor_or_conv_p = -1;
24551 declarator = make_id_declarator (qualifying_scope,
24552 unqualified_name,
24553 sfk, token->location);
24554 declarator->std_attributes = attrs;
24555 declarator->parameter_pack_p = pack_expansion_p;
24557 if (pack_expansion_p)
24558 maybe_warn_variadic_templates ();
24560 /* We're looking for this case in [temp.res]:
24561 A qualified-id is assumed to name a type if [...]
24562 - it is a decl-specifier of the decl-specifier-seq of a
24563 parameter-declaration in a declarator of a function or
24564 function template declaration, ... */
24565 if (cxx_dialect >= cxx20
24566 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24567 && declarator->kind == cdk_id
24568 && !at_class_scope_p ()
24569 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24571 /* ...whose declarator-id is qualified. If it isn't, never
24572 assume the parameters to refer to types. */
24573 if (qualifying_scope == NULL_TREE)
24574 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24575 else
24577 /* Now we have something like
24578 template <typename T> int C::x(S::p);
24579 which can be a function template declaration or a
24580 variable template definition. If name lookup for
24581 the declarator-id C::x finds one or more function
24582 templates, assume S::p to name a type. Otherwise,
24583 don't. */
24584 tree decl
24585 = cp_parser_lookup_name (parser, unqualified_name,
24586 none_type,
24587 /*is_template=*/false,
24588 /*is_namespace=*/false,
24589 /*check_dependency=*/false,
24590 /*ambiguous_decls=*/NULL,
24591 token->location);
24593 if (!is_overloaded_fn (decl)
24594 /* Allow
24595 template<typename T>
24596 A<T>::A(T::type) { } */
24597 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24598 && constructor_name_p (unqualified_name,
24599 qualifying_scope)))
24600 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24605 handle_declarator:;
24606 scope = get_scope_of_declarator (declarator);
24607 if (scope)
24609 /* Any names that appear after the declarator-id for a
24610 member are looked up in the containing scope. */
24611 if (at_function_scope_p ())
24613 /* But declarations with qualified-ids can't appear in a
24614 function. */
24615 cp_parser_error (parser, "qualified-id in declaration");
24616 declarator = cp_error_declarator;
24617 break;
24619 pushed_scope = push_scope (scope);
24621 parser->in_declarator_p = true;
24622 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24623 || (declarator && declarator->kind == cdk_id))
24624 /* Default args are only allowed on function
24625 declarations. */
24626 parser->default_arg_ok_p = saved_default_arg_ok_p;
24627 else
24628 parser->default_arg_ok_p = false;
24630 first = false;
24632 /* We're done. */
24633 else
24634 break;
24637 /* For an abstract declarator, we might wind up with nothing at this
24638 point. That's an error; the declarator is not optional. */
24639 if (!declarator)
24640 cp_parser_error (parser, "expected declarator");
24641 else if (open_paren)
24643 /* Record overly parenthesized declarator so we can give a
24644 diagnostic about confusing decl/expr disambiguation. */
24645 if (declarator->kind == cdk_array)
24647 /* If the open and close parens are on different lines, this
24648 is probably a formatting thing, so ignore. */
24649 expanded_location open = expand_location (open_paren->location);
24650 expanded_location close = expand_location (close_paren->location);
24651 if (open.line != close.line || open.file != close.file)
24652 open_paren = NULL;
24654 if (open_paren)
24655 declarator->parenthesized = make_location (open_paren->location,
24656 open_paren->location,
24657 close_paren->location);
24660 /* If we entered a scope, we must exit it now. */
24661 if (pushed_scope)
24662 pop_scope (pushed_scope);
24664 parser->default_arg_ok_p = saved_default_arg_ok_p;
24665 parser->in_declarator_p = saved_in_declarator_p;
24667 return declarator;
24670 /* Parse a ptr-operator.
24672 ptr-operator:
24673 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24674 * cv-qualifier-seq [opt]
24676 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24677 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24679 GNU Extension:
24681 ptr-operator:
24682 & cv-qualifier-seq [opt]
24684 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24685 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24686 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24687 filled in with the TYPE containing the member. *CV_QUALS is
24688 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24689 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24690 Note that the tree codes returned by this function have nothing
24691 to do with the types of trees that will be eventually be created
24692 to represent the pointer or reference type being parsed. They are
24693 just constants with suggestive names. */
24694 static enum tree_code
24695 cp_parser_ptr_operator (cp_parser* parser,
24696 tree* type,
24697 cp_cv_quals *cv_quals,
24698 tree *attributes)
24700 enum tree_code code = ERROR_MARK;
24701 cp_token *token;
24702 tree attrs = NULL_TREE;
24704 /* Assume that it's not a pointer-to-member. */
24705 *type = NULL_TREE;
24706 /* And that there are no cv-qualifiers. */
24707 *cv_quals = TYPE_UNQUALIFIED;
24709 /* Peek at the next token. */
24710 token = cp_lexer_peek_token (parser->lexer);
24712 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24713 if (token->type == CPP_MULT)
24714 code = INDIRECT_REF;
24715 else if (token->type == CPP_AND)
24716 code = ADDR_EXPR;
24717 else if ((cxx_dialect != cxx98) &&
24718 token->type == CPP_AND_AND) /* C++0x only */
24719 code = NON_LVALUE_EXPR;
24721 if (code != ERROR_MARK)
24723 /* Consume the `*', `&' or `&&'. */
24724 cp_lexer_consume_token (parser->lexer);
24726 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24727 `&', if we are allowing GNU extensions. (The only qualifier
24728 that can legally appear after `&' is `restrict', but that is
24729 enforced during semantic analysis. */
24730 if (code == INDIRECT_REF
24731 || cp_parser_allow_gnu_extensions_p (parser))
24732 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24734 attrs = cp_parser_std_attribute_spec_seq (parser);
24735 if (attributes != NULL)
24736 *attributes = attrs;
24738 else
24740 /* Try the pointer-to-member case. */
24741 cp_parser_parse_tentatively (parser);
24742 /* Look for the optional `::' operator. */
24743 cp_parser_global_scope_opt (parser,
24744 /*current_scope_valid_p=*/false);
24745 /* Look for the nested-name specifier. */
24746 token = cp_lexer_peek_token (parser->lexer);
24747 cp_parser_nested_name_specifier (parser,
24748 /*typename_keyword_p=*/false,
24749 /*check_dependency_p=*/true,
24750 /*type_p=*/false,
24751 /*is_declaration=*/false);
24752 /* If we found it, and the next token is a `*', then we are
24753 indeed looking at a pointer-to-member operator. */
24754 if (!cp_parser_error_occurred (parser)
24755 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24757 /* Indicate that the `*' operator was used. */
24758 code = INDIRECT_REF;
24760 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24761 error_at (token->location, "%qD is a namespace", parser->scope);
24762 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24763 error_at (token->location, "cannot form pointer to member of "
24764 "non-class %q#T", parser->scope);
24765 else
24767 /* The type of which the member is a member is given by the
24768 current SCOPE. */
24769 *type = parser->scope;
24770 /* The next name will not be qualified. */
24771 parser->scope = NULL_TREE;
24772 parser->qualifying_scope = NULL_TREE;
24773 parser->object_scope = NULL_TREE;
24774 /* Look for optional c++11 attributes. */
24775 attrs = cp_parser_std_attribute_spec_seq (parser);
24776 if (attributes != NULL)
24777 *attributes = attrs;
24778 /* Look for the optional cv-qualifier-seq. */
24779 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24782 /* If that didn't work we don't have a ptr-operator. */
24783 if (!cp_parser_parse_definitely (parser))
24784 cp_parser_error (parser, "expected ptr-operator");
24787 return code;
24790 /* Parse an (optional) cv-qualifier-seq.
24792 cv-qualifier-seq:
24793 cv-qualifier cv-qualifier-seq [opt]
24795 cv-qualifier:
24796 const
24797 volatile
24799 GNU Extension:
24801 cv-qualifier:
24802 __restrict__
24804 Returns a bitmask representing the cv-qualifiers. */
24806 static cp_cv_quals
24807 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24809 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24811 while (true)
24813 cp_token *token;
24814 cp_cv_quals cv_qualifier;
24816 /* Peek at the next token. */
24817 token = cp_lexer_peek_token (parser->lexer);
24818 /* See if it's a cv-qualifier. */
24819 switch (token->keyword)
24821 case RID_CONST:
24822 cv_qualifier = TYPE_QUAL_CONST;
24823 break;
24825 case RID_VOLATILE:
24826 cv_qualifier = TYPE_QUAL_VOLATILE;
24827 break;
24829 case RID_RESTRICT:
24830 cv_qualifier = TYPE_QUAL_RESTRICT;
24831 break;
24833 default:
24834 cv_qualifier = TYPE_UNQUALIFIED;
24835 break;
24838 if (!cv_qualifier)
24839 break;
24841 if (cv_quals & cv_qualifier)
24843 gcc_rich_location richloc (token->location);
24844 richloc.add_fixit_remove ();
24845 error_at (&richloc, "duplicate cv-qualifier");
24846 cp_lexer_purge_token (parser->lexer);
24848 else
24850 cp_lexer_consume_token (parser->lexer);
24851 cv_quals |= cv_qualifier;
24855 return cv_quals;
24858 /* Parse an (optional) ref-qualifier
24860 ref-qualifier:
24864 Returns cp_ref_qualifier representing ref-qualifier. */
24866 static cp_ref_qualifier
24867 cp_parser_ref_qualifier_opt (cp_parser* parser)
24869 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24871 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24872 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24873 return ref_qual;
24875 while (true)
24877 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24878 cp_token *token = cp_lexer_peek_token (parser->lexer);
24880 switch (token->type)
24882 case CPP_AND:
24883 curr_ref_qual = REF_QUAL_LVALUE;
24884 break;
24886 case CPP_AND_AND:
24887 curr_ref_qual = REF_QUAL_RVALUE;
24888 break;
24890 default:
24891 curr_ref_qual = REF_QUAL_NONE;
24892 break;
24895 if (!curr_ref_qual)
24896 break;
24897 else if (ref_qual)
24899 error_at (token->location, "multiple ref-qualifiers");
24900 cp_lexer_purge_token (parser->lexer);
24902 else
24904 ref_qual = curr_ref_qual;
24905 cp_lexer_consume_token (parser->lexer);
24909 return ref_qual;
24912 /* Parse an optional tx-qualifier.
24914 tx-qualifier:
24915 transaction_safe
24916 transaction_safe_dynamic */
24918 static tree
24919 cp_parser_tx_qualifier_opt (cp_parser *parser)
24921 cp_token *token = cp_lexer_peek_token (parser->lexer);
24922 if (token->type == CPP_NAME)
24924 tree name = token->u.value;
24925 const char *p = IDENTIFIER_POINTER (name);
24926 const int len = strlen ("transaction_safe");
24927 if (startswith (p, "transaction_safe"))
24929 p += len;
24930 if (*p == '\0'
24931 || !strcmp (p, "_dynamic"))
24933 cp_lexer_consume_token (parser->lexer);
24934 if (!flag_tm)
24936 error ("%qE requires %<-fgnu-tm%>", name);
24937 return NULL_TREE;
24939 else
24940 return name;
24944 return NULL_TREE;
24947 /* Parse an (optional) virt-specifier-seq.
24949 virt-specifier-seq:
24950 virt-specifier virt-specifier-seq [opt]
24952 virt-specifier:
24953 override
24954 final
24956 Returns a bitmask representing the virt-specifiers. */
24958 static cp_virt_specifiers
24959 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24961 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24963 while (true)
24965 cp_token *token;
24966 cp_virt_specifiers virt_specifier;
24968 /* Peek at the next token. */
24969 token = cp_lexer_peek_token (parser->lexer);
24970 /* See if it's a virt-specifier-qualifier. */
24971 if (token->type != CPP_NAME)
24972 break;
24973 if (id_equal (token->u.value, "override"))
24975 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24976 virt_specifier = VIRT_SPEC_OVERRIDE;
24978 else if (id_equal (token->u.value, "final"))
24980 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24981 virt_specifier = VIRT_SPEC_FINAL;
24983 else if (id_equal (token->u.value, "__final"))
24985 virt_specifier = VIRT_SPEC_FINAL;
24987 else
24988 break;
24990 if (virt_specifiers & virt_specifier)
24992 gcc_rich_location richloc (token->location);
24993 richloc.add_fixit_remove ();
24994 error_at (&richloc, "duplicate virt-specifier");
24995 cp_lexer_purge_token (parser->lexer);
24997 else
24999 cp_lexer_consume_token (parser->lexer);
25000 virt_specifiers |= virt_specifier;
25003 return virt_specifiers;
25006 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
25007 is in scope even though it isn't real. */
25009 void
25010 inject_this_parameter (tree ctype, cp_cv_quals quals)
25012 tree this_parm;
25014 if (current_class_ptr)
25016 /* We don't clear this between NSDMIs. Is it already what we want? */
25017 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
25018 if (DECL_P (current_class_ptr)
25019 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
25020 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
25021 && cp_type_quals (type) == quals)
25022 return;
25025 this_parm = build_this_parm (NULL_TREE, ctype, quals);
25026 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
25027 current_class_ptr = NULL_TREE;
25028 current_class_ref
25029 = cp_build_fold_indirect_ref (this_parm);
25030 current_class_ptr = this_parm;
25033 /* Return true iff our current scope is a non-static data member
25034 initializer. */
25036 bool
25037 parsing_nsdmi (void)
25039 /* We recognize NSDMI context by the context-less 'this' pointer set up
25040 by the function above. */
25041 if (current_class_ptr
25042 && TREE_CODE (current_class_ptr) == PARM_DECL
25043 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
25044 return true;
25045 return false;
25048 /* True if we're parsing a function declarator. */
25050 bool
25051 parsing_function_declarator ()
25053 /* this_entity is NULL for a function parameter scope while parsing the
25054 declarator; it is set when parsing the body of the function. */
25055 return (current_binding_level->kind == sk_function_parms
25056 && !current_binding_level->this_entity);
25059 /* Parse a late-specified return type, if any. This is not a separate
25060 non-terminal, but part of a function declarator, which looks like
25062 -> trailing-type-specifier-seq abstract-declarator(opt)
25064 Returns the type indicated by the type-id.
25066 In addition to this, parse any queued up #pragma omp declare simd
25067 clauses, and #pragma acc routine clauses.
25069 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
25070 function. */
25072 static tree
25073 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
25074 tree& requires_clause)
25076 cp_token *token;
25077 tree type = NULL_TREE;
25078 bool declare_simd_p = (parser->omp_declare_simd
25079 && declarator
25080 && declarator->kind == cdk_id);
25082 bool oacc_routine_p = (parser->oacc_routine
25083 && declarator
25084 && declarator->kind == cdk_id);
25086 /* Peek at the next token. */
25087 token = cp_lexer_peek_token (parser->lexer);
25088 /* A late-specified return type is indicated by an initial '->'. */
25089 if (token->type != CPP_DEREF
25090 && token->keyword != RID_REQUIRES
25091 && !(token->type == CPP_NAME
25092 && token->u.value == ridpointers[RID_REQUIRES])
25093 && !(declare_simd_p || oacc_routine_p))
25094 return NULL_TREE;
25096 if (token->type == CPP_DEREF)
25098 /* Consume the ->. */
25099 cp_lexer_consume_token (parser->lexer);
25101 type = cp_parser_trailing_type_id (parser);
25104 /* Function declarations may be followed by a trailing
25105 requires-clause. */
25106 requires_clause = cp_parser_requires_clause_opt (parser, false);
25108 if (declare_simd_p)
25109 declarator->attributes
25110 = cp_parser_late_parsing_omp_declare_simd (parser,
25111 declarator->attributes);
25112 if (oacc_routine_p)
25113 declarator->attributes
25114 = cp_parser_late_parsing_oacc_routine (parser,
25115 declarator->attributes);
25117 return type;
25120 /* Parse a declarator-id.
25122 declarator-id:
25123 id-expression
25124 :: [opt] nested-name-specifier [opt] type-name
25126 In the `id-expression' case, the value returned is as for
25127 cp_parser_id_expression if the id-expression was an unqualified-id.
25128 If the id-expression was a qualified-id, then a SCOPE_REF is
25129 returned. The first operand is the scope (either a NAMESPACE_DECL
25130 or TREE_TYPE), but the second is still just a representation of an
25131 unqualified-id. */
25133 static tree
25134 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
25136 tree id;
25137 /* The expression must be an id-expression. Assume that qualified
25138 names are the names of types so that:
25140 template <class T>
25141 int S<T>::R::i = 3;
25143 will work; we must treat `S<T>::R' as the name of a type.
25144 Similarly, assume that qualified names are templates, where
25145 required, so that:
25147 template <class T>
25148 int S<T>::R<T>::i = 3;
25150 will work, too. */
25151 id = cp_parser_id_expression (parser,
25152 /*template_keyword_p=*/false,
25153 /*check_dependency_p=*/false,
25154 /*template_p=*/NULL,
25155 /*declarator_p=*/true,
25156 optional_p);
25157 if (id && BASELINK_P (id))
25158 id = BASELINK_FUNCTIONS (id);
25159 return id;
25162 /* Parse a type-id.
25164 type-id:
25165 type-specifier-seq abstract-declarator [opt]
25167 The parser flags FLAGS is used to control type-specifier parsing.
25169 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
25171 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25172 i.e. we've just seen "->".
25174 Returns the TYPE specified. */
25176 static tree
25177 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
25178 bool is_template_arg, bool is_trailing_return,
25179 location_t *type_location)
25181 cp_decl_specifier_seq type_specifier_seq;
25182 cp_declarator *abstract_declarator;
25184 /* Parse the type-specifier-seq. */
25185 cp_parser_type_specifier_seq (parser, flags,
25186 /*is_declaration=*/false,
25187 is_trailing_return,
25188 &type_specifier_seq);
25189 if (type_location)
25190 *type_location = type_specifier_seq.locations[ds_type_spec];
25192 if (is_template_arg && type_specifier_seq.type
25193 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
25194 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
25195 /* A bare template name as a template argument is a template template
25196 argument, not a placeholder, so fail parsing it as a type argument. */
25198 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
25199 cp_parser_simulate_error (parser);
25200 return error_mark_node;
25202 if (type_specifier_seq.type == error_mark_node)
25203 return error_mark_node;
25205 /* There might or might not be an abstract declarator. */
25206 cp_parser_parse_tentatively (parser);
25207 /* Look for the declarator. */
25208 abstract_declarator
25209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
25210 CP_PARSER_FLAGS_NONE, NULL,
25211 /*parenthesized_p=*/NULL,
25212 /*member_p=*/false,
25213 /*friend_p=*/false,
25214 /*static_p=*/false);
25215 /* Check to see if there really was a declarator. */
25216 if (!cp_parser_parse_definitely (parser))
25217 abstract_declarator = NULL;
25219 bool auto_typeid_ok = false;
25220 /* The concepts TS allows 'auto' as a type-id. */
25221 if (flag_concepts_ts)
25222 auto_typeid_ok = !parser->in_type_id_in_expr_p;
25223 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
25224 outside the template-argument-list context here only for the sake of
25225 diagnostic: grokdeclarator then can emit a better error message for
25226 e.g. using T = auto. */
25227 else if (flag_concepts)
25228 auto_typeid_ok = (!parser->in_type_id_in_expr_p
25229 && !parser->in_template_argument_list_p);
25231 if (type_specifier_seq.type
25232 && !auto_typeid_ok
25233 /* None of the valid uses of 'auto' in C++14 involve the type-id
25234 nonterminal, but it is valid in a trailing-return-type. */
25235 && !(cxx_dialect >= cxx14 && is_trailing_return))
25236 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
25238 /* A type-id with type 'auto' is only ok if the abstract declarator
25239 is a function declarator with a late-specified return type.
25241 A type-id with 'auto' is also valid in a trailing-return-type
25242 in a compound-requirement. */
25243 if (abstract_declarator
25244 && abstract_declarator->kind == cdk_function
25245 && abstract_declarator->u.function.late_return_type)
25246 /* OK */;
25247 else if (parser->in_result_type_constraint_p)
25248 /* OK */;
25249 else
25251 if (!cp_parser_simulate_error (parser))
25253 location_t loc = type_specifier_seq.locations[ds_type_spec];
25254 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25256 auto_diagnostic_group g;
25257 gcc_rich_location richloc (loc);
25258 richloc.add_fixit_insert_after ("<>");
25259 error_at (&richloc, "missing template arguments after %qE",
25260 tmpl);
25261 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
25262 tmpl);
25264 else if (parser->in_template_argument_list_p)
25265 error_at (loc, "%qT not permitted in template argument",
25266 auto_node);
25267 else
25268 error_at (loc, "invalid use of %qT", auto_node);
25270 return error_mark_node;
25274 return groktypename (&type_specifier_seq, abstract_declarator,
25275 is_template_arg);
25278 /* Wrapper for cp_parser_type_id_1. */
25280 static tree
25281 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
25282 location_t *type_location)
25284 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
25287 /* Wrapper for cp_parser_type_id_1. */
25289 static tree
25290 cp_parser_template_type_arg (cp_parser *parser)
25292 const char *saved_message = parser->type_definition_forbidden_message;
25293 parser->type_definition_forbidden_message
25294 = G_("types may not be defined in template arguments");
25295 tree r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE,
25296 /*is_template_arg=*/true,
25297 /*is_trailing_return=*/false, nullptr);
25298 parser->type_definition_forbidden_message = saved_message;
25299 /* cp_parser_type_id_1 checks for auto, but only for
25300 ->auto_is_implicit_function_template_parm_p. */
25301 if (cxx_dialect >= cxx14 && !flag_concepts_ts && type_uses_auto (r))
25303 error ("invalid use of %<auto%> in template argument");
25304 r = error_mark_node;
25306 return r;
25309 /* Wrapper for cp_parser_type_id_1. */
25311 static tree
25312 cp_parser_trailing_type_id (cp_parser *parser)
25314 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
25315 false, true, NULL);
25318 /* Parse a type-specifier-seq.
25320 type-specifier-seq:
25321 type-specifier type-specifier-seq [opt]
25323 GNU extension:
25325 type-specifier-seq:
25326 attributes type-specifier-seq [opt]
25328 The parser flags FLAGS is used to control type-specifier parsing.
25330 If IS_DECLARATION is true, we are at the start of a "condition" or
25331 exception-declaration, so we might be followed by a declarator-id.
25333 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25334 i.e. we've just seen "->".
25336 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
25338 static void
25339 cp_parser_type_specifier_seq (cp_parser* parser,
25340 cp_parser_flags flags,
25341 bool is_declaration,
25342 bool is_trailing_return,
25343 cp_decl_specifier_seq *type_specifier_seq)
25345 bool seen_type_specifier = false;
25346 cp_token *start_token = NULL;
25348 /* Clear the TYPE_SPECIFIER_SEQ. */
25349 clear_decl_specs (type_specifier_seq);
25351 flags |= CP_PARSER_FLAGS_OPTIONAL;
25352 /* In the context of a trailing return type, enum E { } is an
25353 elaborated-type-specifier followed by a function-body, not an
25354 enum-specifier. */
25355 if (is_trailing_return)
25356 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
25358 /* Parse the type-specifiers and attributes. */
25359 while (true)
25361 tree type_specifier;
25362 bool is_cv_qualifier;
25364 /* Check for attributes first. */
25365 if (cp_next_tokens_can_be_attribute_p (parser))
25367 /* GNU attributes at the end of a declaration apply to the
25368 declaration as a whole, not to the trailing return type. So look
25369 ahead to see if these attributes are at the end. */
25370 if (seen_type_specifier && is_trailing_return
25371 && cp_next_tokens_can_be_gnu_attribute_p (parser))
25373 size_t n = cp_parser_skip_attributes_opt (parser, 1);
25374 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
25375 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
25376 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
25377 break;
25379 type_specifier_seq->attributes
25380 = attr_chainon (type_specifier_seq->attributes,
25381 cp_parser_attributes_opt (parser));
25382 continue;
25385 /* record the token of the beginning of the type specifier seq,
25386 for error reporting purposes*/
25387 if (!start_token)
25388 start_token = cp_lexer_peek_token (parser->lexer);
25390 /* Look for the type-specifier. */
25391 type_specifier = cp_parser_type_specifier (parser,
25392 flags,
25393 type_specifier_seq,
25394 /*is_declaration=*/false,
25395 NULL,
25396 &is_cv_qualifier);
25397 if (!type_specifier)
25399 /* If the first type-specifier could not be found, this is not a
25400 type-specifier-seq at all. */
25401 if (!seen_type_specifier)
25403 /* Set in_declarator_p to avoid skipping to the semicolon. */
25404 int in_decl = parser->in_declarator_p;
25405 parser->in_declarator_p = true;
25407 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
25408 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
25409 cp_parser_error (parser, "expected type-specifier");
25411 parser->in_declarator_p = in_decl;
25413 type_specifier_seq->type = error_mark_node;
25414 return;
25416 /* If subsequent type-specifiers could not be found, the
25417 type-specifier-seq is complete. */
25418 break;
25421 seen_type_specifier = true;
25422 /* The standard says that a condition can be:
25424 type-specifier-seq declarator = assignment-expression
25426 However, given:
25428 struct S {};
25429 if (int S = ...)
25431 we should treat the "S" as a declarator, not as a
25432 type-specifier. The standard doesn't say that explicitly for
25433 type-specifier-seq, but it does say that for
25434 decl-specifier-seq in an ordinary declaration. Perhaps it
25435 would be clearer just to allow a decl-specifier-seq here, and
25436 then add a semantic restriction that if any decl-specifiers
25437 that are not type-specifiers appear, the program is invalid. */
25438 if (is_declaration && !is_cv_qualifier)
25439 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
25443 /* Return whether the function currently being declared has an associated
25444 template parameter list. */
25446 static bool
25447 function_being_declared_is_template_p (cp_parser* parser)
25449 if (!current_template_parms || processing_template_parmlist)
25450 return false;
25452 if (parser->implicit_template_scope)
25453 return true;
25455 if (at_class_scope_p ()
25456 && TYPE_BEING_DEFINED (current_class_type))
25457 return parser->num_template_parameter_lists != 0;
25459 return ((int) parser->num_template_parameter_lists > template_class_depth
25460 (current_class_type));
25463 /* Parse a parameter-declaration-clause.
25465 parameter-declaration-clause:
25466 parameter-declaration-list [opt] ... [opt]
25467 parameter-declaration-list , ...
25469 The parser flags FLAGS is used to control type-specifier parsing.
25471 Returns a representation for the parameter declarations. A return
25472 value of NULL indicates a parameter-declaration-clause consisting
25473 only of an ellipsis. */
25475 static tree
25476 cp_parser_parameter_declaration_clause (cp_parser* parser,
25477 cp_parser_flags flags)
25479 tree parameters;
25480 cp_token *token;
25481 bool ellipsis_p;
25483 auto cleanup = make_temp_override
25484 (parser->auto_is_implicit_function_template_parm_p);
25486 if (!processing_specialization
25487 && !processing_template_parmlist
25488 && !processing_explicit_instantiation
25489 /* default_arg_ok_p tracks whether this is a parameter-clause for an
25490 actual function or a random abstract declarator. */
25491 && parser->default_arg_ok_p)
25492 if (!current_function_decl
25493 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
25494 parser->auto_is_implicit_function_template_parm_p = true;
25496 /* Peek at the next token. */
25497 token = cp_lexer_peek_token (parser->lexer);
25498 /* Check for trivial parameter-declaration-clauses. */
25499 if (token->type == CPP_ELLIPSIS)
25501 /* Consume the `...' token. */
25502 cp_lexer_consume_token (parser->lexer);
25503 return NULL_TREE;
25505 else if (token->type == CPP_CLOSE_PAREN)
25506 /* There are no parameters. */
25507 return void_list_node;
25508 /* Check for `(void)', too, which is a special case. */
25509 else if (token->keyword == RID_VOID
25510 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25511 == CPP_CLOSE_PAREN))
25513 /* Consume the `void' token. */
25514 cp_lexer_consume_token (parser->lexer);
25515 /* There are no parameters. */
25516 return explicit_void_list_node;
25519 /* A vector of parameters that haven't been pushed yet. */
25520 auto_vec<tree> pending_decls;
25522 /* Parse the parameter-declaration-list. */
25523 parameters = cp_parser_parameter_declaration_list (parser, flags,
25524 &pending_decls);
25525 /* If a parse error occurred while parsing the
25526 parameter-declaration-list, then the entire
25527 parameter-declaration-clause is erroneous. */
25528 if (parameters == error_mark_node)
25529 return NULL_TREE;
25531 /* Peek at the next token. */
25532 token = cp_lexer_peek_token (parser->lexer);
25533 /* If it's a `,', the clause should terminate with an ellipsis. */
25534 if (token->type == CPP_COMMA)
25536 /* Consume the `,'. */
25537 cp_lexer_consume_token (parser->lexer);
25538 /* Expect an ellipsis. */
25539 ellipsis_p
25540 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
25542 /* It might also be `...' if the optional trailing `,' was
25543 omitted. */
25544 else if (token->type == CPP_ELLIPSIS)
25546 /* Consume the `...' token. */
25547 cp_lexer_consume_token (parser->lexer);
25548 /* And remember that we saw it. */
25549 ellipsis_p = true;
25551 else
25552 ellipsis_p = false;
25554 /* A valid parameter-declaration-clause can only be followed by a ')'.
25555 So it's time to push all the parameters we have seen now that we
25556 know we have a valid declaration. Note that here we may not have
25557 committed yet, nor should we. Pushing here will detect the error
25558 of redefining a parameter. */
25559 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25561 for (tree p : pending_decls)
25562 pushdecl (p);
25564 /* Delayed checking of auto parameters. */
25565 if (!parser->auto_is_implicit_function_template_parm_p
25566 && cxx_dialect >= cxx14)
25567 for (tree p = parameters; p; p = TREE_CHAIN (p))
25568 if (type_uses_auto (TREE_TYPE (TREE_VALUE (p))))
25570 error_at (location_of (TREE_VALUE (p)),
25571 "%<auto%> parameter not permitted in this context");
25572 TREE_TYPE (TREE_VALUE (p)) = error_mark_node;
25576 /* Finish the parameter list. */
25577 if (!ellipsis_p)
25578 parameters = chainon (parameters, void_list_node);
25580 return parameters;
25583 /* Parse a parameter-declaration-list.
25585 parameter-declaration-list:
25586 parameter-declaration
25587 parameter-declaration-list , parameter-declaration
25589 The parser flags FLAGS is used to control type-specifier parsing.
25590 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25592 Returns a representation of the parameter-declaration-list, as for
25593 cp_parser_parameter_declaration_clause. However, the
25594 `void_list_node' is never appended to the list. */
25596 static tree
25597 cp_parser_parameter_declaration_list (cp_parser* parser,
25598 cp_parser_flags flags,
25599 auto_vec<tree> *pending_decls)
25601 tree parameters = NULL_TREE;
25602 tree *tail = &parameters;
25603 bool saved_in_unbraced_linkage_specification_p;
25604 int index = 0;
25606 /* The special considerations that apply to a function within an
25607 unbraced linkage specifications do not apply to the parameters
25608 to the function. */
25609 saved_in_unbraced_linkage_specification_p
25610 = parser->in_unbraced_linkage_specification_p;
25611 parser->in_unbraced_linkage_specification_p = false;
25613 /* Look for more parameters. */
25614 while (true)
25616 cp_parameter_declarator *parameter;
25617 tree decl = error_mark_node;
25618 bool parenthesized_p = false;
25620 /* Parse the parameter. */
25621 parameter
25622 = cp_parser_parameter_declaration (parser, flags,
25623 /*template_parm_p=*/false,
25624 &parenthesized_p);
25626 /* We don't know yet if the enclosing context is unavailable or deprecated,
25627 so wait and deal with it in grokparms if appropriate. */
25628 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25630 if (parameter && !cp_parser_error_occurred (parser))
25632 decl = grokdeclarator (parameter->declarator,
25633 &parameter->decl_specifiers,
25634 PARM,
25635 parameter->default_argument != NULL_TREE,
25636 &parameter->decl_specifiers.attributes);
25637 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25638 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25641 deprecated_state = DEPRECATED_NORMAL;
25643 /* If a parse error occurred parsing the parameter declaration,
25644 then the entire parameter-declaration-list is erroneous. */
25645 if (decl == error_mark_node)
25647 parameters = error_mark_node;
25648 break;
25651 if (parameter->decl_specifiers.attributes)
25652 cplus_decl_attributes (&decl,
25653 parameter->decl_specifiers.attributes,
25655 if (DECL_NAME (decl))
25657 /* We cannot always pushdecl while parsing tentatively because
25658 it may have side effects and we can't be sure yet if we're
25659 parsing a declaration, e.g.:
25661 S foo(int(x), int(x), int{x});
25663 where it's not clear if we're dealing with a constructor call
25664 or a function declaration until we've seen the last argument
25665 which breaks it up.
25666 It's safe to pushdecl so long as it doesn't result in a clash
25667 with an already-pushed parameter. But we don't delay pushing
25668 different parameters to handle
25670 S foo(int(i), decltype(i) j = 42);
25672 which is valid. */
25673 if (pending_decls
25674 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25675 /* See if PARAMETERS already contains a parameter with the same
25676 DECL_NAME as DECL. */
25677 && [parameters, decl] {
25678 for (tree p = parameters; p; p = TREE_CHAIN (p))
25679 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25680 return true;
25681 return false;
25682 }())
25683 pending_decls->safe_push (decl);
25684 else
25685 decl = pushdecl (decl);
25688 if (decl != error_mark_node)
25690 retrofit_lang_decl (decl);
25691 DECL_PARM_INDEX (decl) = ++index;
25692 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25695 /* Add the new parameter to the list. */
25696 *tail = build_tree_list (parameter->default_argument, decl);
25697 tail = &TREE_CHAIN (*tail);
25699 /* If the parameters were parenthesized, it's the case of
25700 T foo(X(x)) which looks like a variable definition but
25701 is a function declaration. */
25702 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25703 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25705 /* Peek at the next token. */
25706 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25707 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25708 /* These are for Objective-C++ */
25709 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25710 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25711 /* The parameter-declaration-list is complete. */
25712 break;
25713 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25715 cp_token *token;
25717 /* Peek at the next token. */
25718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25719 /* If it's an ellipsis, then the list is complete. */
25720 if (token->type == CPP_ELLIPSIS)
25721 break;
25722 /* Otherwise, there must be more parameters. Consume the
25723 `,'. */
25724 cp_lexer_consume_token (parser->lexer);
25725 /* When parsing something like:
25727 int i(float f, double d)
25729 we can tell after seeing the declaration for "f" that we
25730 are not looking at an initialization of a variable "i",
25731 but rather at the declaration of a function "i".
25733 Due to the fact that the parsing of template arguments
25734 (as specified to a template-id) requires backtracking we
25735 cannot use this technique when inside a template argument
25736 list. */
25737 if (!parser->in_template_argument_list_p
25738 && !parser->in_type_id_in_expr_p
25739 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25740 /* However, a parameter-declaration of the form
25741 "float(f)" (which is a valid declaration of a
25742 parameter "f") can also be interpreted as an
25743 expression (the conversion of "f" to "float"). */
25744 && !parenthesized_p)
25745 cp_parser_commit_to_tentative_parse (parser);
25747 else
25749 cp_parser_error (parser, "expected %<,%> or %<...%>");
25750 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25751 cp_parser_skip_to_closing_parenthesis (parser,
25752 /*recovering=*/true,
25753 /*or_comma=*/false,
25754 /*consume_paren=*/false);
25755 break;
25759 parser->in_unbraced_linkage_specification_p
25760 = saved_in_unbraced_linkage_specification_p;
25762 /* Reset implicit_template_scope if we are about to leave the function
25763 parameter list that introduced it. Note that for out-of-line member
25764 definitions, there will be one or more class scopes before we get to
25765 the template parameter scope. */
25767 if (cp_binding_level *its = parser->implicit_template_scope)
25768 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25770 while (maybe_its->kind == sk_class)
25771 maybe_its = maybe_its->level_chain;
25772 if (maybe_its == its)
25774 parser->implicit_template_parms = 0;
25775 parser->implicit_template_scope = 0;
25779 return parameters;
25782 /* Parse a parameter declaration.
25784 parameter-declaration:
25785 decl-specifier-seq ... [opt] declarator
25786 decl-specifier-seq declarator = assignment-expression
25787 decl-specifier-seq ... [opt] abstract-declarator [opt]
25788 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25790 The parser flags FLAGS is used to control type-specifier parsing.
25792 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25793 declares a template parameter. (In that case, a non-nested `>'
25794 token encountered during the parsing of the assignment-expression
25795 is not interpreted as a greater-than operator.)
25797 Returns a representation of the parameter, or NULL if an error
25798 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25799 true iff the declarator is of the form "(p)". */
25801 static cp_parameter_declarator *
25802 cp_parser_parameter_declaration (cp_parser *parser,
25803 cp_parser_flags flags,
25804 bool template_parm_p,
25805 bool *parenthesized_p)
25807 int declares_class_or_enum;
25808 cp_decl_specifier_seq decl_specifiers;
25809 cp_declarator *declarator;
25810 tree default_argument;
25811 cp_token *token = NULL, *declarator_token_start = NULL;
25812 const char *saved_message;
25813 bool template_parameter_pack_p = false;
25815 /* In a template parameter, `>' is not an operator.
25817 [temp.param]
25819 When parsing a default template-argument for a non-type
25820 template-parameter, the first non-nested `>' is taken as the end
25821 of the template parameter-list rather than a greater-than
25822 operator. */
25824 /* Type definitions may not appear in parameter types. */
25825 saved_message = parser->type_definition_forbidden_message;
25826 parser->type_definition_forbidden_message
25827 = G_("types may not be defined in parameter types");
25829 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25830 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25831 (current_template_parms)) : 0);
25833 /* Parse the declaration-specifiers. */
25834 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25835 cp_parser_decl_specifier_seq (parser,
25836 flags | CP_PARSER_FLAGS_PARAMETER,
25837 &decl_specifiers,
25838 &declares_class_or_enum);
25840 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25841 type-constraint opt auto can be used as a decl-specifier of the
25842 decl-specifier-seq of a parameter-declaration of a function declaration
25843 or lambda-expression..." but we must not synthesize an implicit template
25844 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25845 we want to synthesize only the first auto. */
25846 auto cleanup = make_temp_override
25847 (parser->auto_is_implicit_function_template_parm_p, false);
25849 /* Complain about missing 'typename' or other invalid type names. */
25850 if (!decl_specifiers.any_type_specifiers_p
25851 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25852 decl_specifiers.type = error_mark_node;
25854 /* If an error occurred, there's no reason to attempt to parse the
25855 rest of the declaration. */
25856 if (cp_parser_error_occurred (parser))
25858 parser->type_definition_forbidden_message = saved_message;
25859 return NULL;
25862 /* Peek at the next token. */
25863 token = cp_lexer_peek_token (parser->lexer);
25865 /* If the next token is a `)', `,', `=', `>', or `...', then there
25866 is no declarator. However, when variadic templates are enabled,
25867 there may be a declarator following `...'. */
25868 if (token->type == CPP_CLOSE_PAREN
25869 || token->type == CPP_COMMA
25870 || token->type == CPP_EQ
25871 || token->type == CPP_GREATER)
25873 declarator = NULL;
25874 if (parenthesized_p)
25875 *parenthesized_p = false;
25877 /* Otherwise, there should be a declarator. */
25878 else
25880 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25881 parser->default_arg_ok_p = false;
25883 /* After seeing a decl-specifier-seq, if the next token is not a
25884 "(" or "{", there is no possibility that the code is a valid
25885 expression. Therefore, if parsing tentatively, we commit at
25886 this point. */
25887 if (!parser->in_template_argument_list_p
25888 /* In an expression context, having seen:
25890 (int((char ...
25892 we cannot be sure whether we are looking at a
25893 function-type (taking a "char" as a parameter) or a cast
25894 of some object of type "char" to "int". */
25895 && !parser->in_type_id_in_expr_p
25896 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25897 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25901 if (decl_specifiers.type
25902 && template_placeholder_p (decl_specifiers.type))
25903 /* This is a CTAD expression, not a parameter declaration. */
25904 cp_parser_simulate_error (parser);
25906 else
25907 cp_parser_commit_to_tentative_parse (parser);
25909 /* Parse the declarator. */
25910 declarator_token_start = token;
25911 declarator = cp_parser_declarator (parser,
25912 CP_PARSER_DECLARATOR_EITHER,
25913 CP_PARSER_FLAGS_NONE,
25914 /*ctor_dtor_or_conv_p=*/NULL,
25915 parenthesized_p,
25916 /*member_p=*/false,
25917 /*friend_p=*/false,
25918 /*static_p=*/false);
25919 parser->default_arg_ok_p = saved_default_arg_ok_p;
25920 /* After the declarator, allow more attributes. */
25921 decl_specifiers.attributes
25922 = attr_chainon (decl_specifiers.attributes,
25923 cp_parser_attributes_opt (parser));
25925 /* If the declarator is a template parameter pack, remember that and
25926 clear the flag in the declarator itself so we don't get errors
25927 from grokdeclarator. */
25928 if (template_parm_p && declarator && declarator->parameter_pack_p)
25930 declarator->parameter_pack_p = false;
25931 template_parameter_pack_p = true;
25935 /* If the next token is an ellipsis, and we have not seen a declarator
25936 name, and if either the type of the declarator contains parameter
25937 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25938 for, eg, abbreviated integral type names), then we actually have a
25939 parameter pack expansion expression. Otherwise, leave the ellipsis
25940 for a C-style variadic function. */
25941 token = cp_lexer_peek_token (parser->lexer);
25943 bool xobj_param_p
25944 = decl_spec_seq_has_spec_p (&decl_specifiers, ds_this);
25945 if (xobj_param_p && template_parm_p)
25947 error_at (decl_specifiers.locations[ds_this],
25948 "%<this%> specifier in template parameter declaration");
25949 xobj_param_p = false;
25950 decl_specifiers.locations[ds_this] = 0;
25953 /* If a function parameter pack was specified and an implicit template
25954 parameter was introduced during cp_parser_parameter_declaration,
25955 change any implicit parameters introduced into packs. */
25956 if (parser->implicit_template_parms
25957 && ((token->type == CPP_ELLIPSIS
25958 && declarator_can_be_parameter_pack (declarator))
25959 || (declarator && declarator->parameter_pack_p)))
25961 int latest_template_parm_idx = TREE_VEC_LENGTH
25962 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25964 if (latest_template_parm_idx != template_parm_idx)
25965 decl_specifiers.type
25966 = convert_generic_types_to_packs (decl_specifiers.type,
25967 template_parm_idx,
25968 latest_template_parm_idx);
25971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25973 tree type = decl_specifiers.type;
25975 if (type && DECL_P (type))
25976 type = TREE_TYPE (type);
25978 if (((type
25979 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25980 && (template_parm_p || uses_parameter_packs (type)))
25981 || (!type && template_parm_p))
25982 && declarator_can_be_parameter_pack (declarator))
25984 /* Consume the `...'. */
25985 cp_lexer_consume_token (parser->lexer);
25986 maybe_warn_variadic_templates ();
25988 /* Build a pack expansion type */
25989 if (template_parm_p)
25990 template_parameter_pack_p = true;
25991 else if (declarator)
25992 declarator->parameter_pack_p = true;
25993 else
25994 decl_specifiers.type = make_pack_expansion (type);
25998 if (xobj_param_p
25999 && ((declarator && declarator->parameter_pack_p)
26000 || (decl_specifiers.type
26001 && PACK_EXPANSION_P (decl_specifiers.type))))
26003 location_t xobj_param
26004 = make_location (decl_specifiers.locations[ds_this],
26005 decl_spec_token_start->location,
26006 input_location);
26007 error_at (xobj_param,
26008 "an explicit object parameter cannot "
26009 "be a function parameter pack");
26010 xobj_param_p = false;
26011 decl_specifiers.locations[ds_this] = 0;
26014 /* The restriction on defining new types applies only to the type
26015 of the parameter, not to the default argument. */
26016 parser->type_definition_forbidden_message = saved_message;
26017 cp_token *eq_token = NULL;
26018 /* If the next token is `=', then process a default argument. */
26019 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26021 tree type = decl_specifiers.type;
26022 token = cp_lexer_peek_token (parser->lexer);
26023 /* Used for diagnostics with an xobj parameter. */
26024 eq_token = token;
26025 if (declarator)
26026 declarator->init_loc = token->location;
26027 /* If we are defining a class, then the tokens that make up the
26028 default argument must be saved and processed later. */
26029 if (!template_parm_p && at_class_scope_p ()
26030 && TYPE_BEING_DEFINED (current_class_type)
26031 && !LAMBDA_TYPE_P (current_class_type))
26032 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
26034 /* A constrained-type-specifier may declare a type
26035 template-parameter. */
26036 else if (declares_constrained_type_template_parameter (type))
26037 default_argument
26038 = cp_parser_default_type_template_argument (parser);
26040 /* A constrained-type-specifier may declare a
26041 template-template-parameter. */
26042 else if (declares_constrained_template_template_parameter (type))
26043 default_argument
26044 = cp_parser_default_template_template_argument (parser);
26046 /* Outside of a class definition, we can just parse the
26047 assignment-expression. */
26048 else
26049 default_argument
26050 = cp_parser_default_argument (parser, template_parm_p);
26052 if (!parser->default_arg_ok_p)
26054 permerror (token->location,
26055 "default arguments are only "
26056 "permitted for function parameters");
26058 else if ((declarator && declarator->parameter_pack_p)
26059 || template_parameter_pack_p
26060 || (decl_specifiers.type
26061 && PACK_EXPANSION_P (decl_specifiers.type)))
26063 /* Find the name of the parameter pack. */
26064 cp_declarator *id_declarator = declarator;
26065 while (id_declarator && id_declarator->kind != cdk_id)
26066 id_declarator = id_declarator->declarator;
26068 if (id_declarator && id_declarator->kind == cdk_id)
26069 error_at (declarator_token_start->location,
26070 template_parm_p
26071 ? G_("template parameter pack %qD "
26072 "cannot have a default argument")
26073 : G_("parameter pack %qD cannot have "
26074 "a default argument"),
26075 id_declarator->u.id.unqualified_name);
26076 else
26077 error_at (declarator_token_start->location,
26078 template_parm_p
26079 ? G_("template parameter pack cannot have "
26080 "a default argument")
26081 : G_("parameter pack cannot have a "
26082 "default argument"));
26084 default_argument = NULL_TREE;
26087 else
26088 default_argument = NULL_TREE;
26090 if (default_argument)
26091 STRIP_ANY_LOCATION_WRAPPER (default_argument);
26093 if (xobj_param_p)
26095 if (default_argument)
26097 /* If there is a default_argument, eq_token should always be set. */
26098 gcc_assert (eq_token);
26099 location_t param_with_init_loc
26100 = make_location (eq_token->location,
26101 decl_spec_token_start->location,
26102 input_location);
26103 error_at (param_with_init_loc,
26104 "an explicit object parameter "
26105 "may not have a default argument");
26107 /* Xobj parameters can not have default arguments, thus
26108 we can reuse the default argument field to flag the param as such. */
26109 default_argument = this_identifier;
26112 /* Generate a location for the parameter, ranging from the start of the
26113 initial token to the end of the final token (using input_location for
26114 the latter, set up by cp_lexer_set_source_position_from_token when
26115 consuming tokens).
26117 If we have a identifier, then use it for the caret location, e.g.
26119 extern int callee (int one, int (*two)(int, int), float three);
26120 ~~~~~~^~~~~~~~~~~~~~
26122 otherwise, reuse the start location for the caret location e.g.:
26124 extern int callee (int one, int (*)(int, int), float three);
26125 ^~~~~~~~~~~~~~~~~
26128 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
26129 ? declarator->id_loc
26130 : decl_spec_token_start->location);
26131 location_t param_loc = make_location (caret_loc,
26132 decl_spec_token_start->location,
26133 input_location);
26135 return make_parameter_declarator (&decl_specifiers,
26136 declarator,
26137 default_argument,
26138 param_loc,
26139 template_parameter_pack_p);
26142 /* Parse a default argument and return it.
26144 TEMPLATE_PARM_P is true if this is a default argument for a
26145 non-type template parameter. */
26146 static tree
26147 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
26149 tree default_argument = NULL_TREE;
26150 bool saved_greater_than_is_operator_p;
26151 unsigned char saved_local_variables_forbidden_p;
26153 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
26154 set correctly. */
26155 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
26156 parser->greater_than_is_operator_p = !template_parm_p;
26157 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
26158 auto ord = make_temp_override (parser->oacc_routine, NULL);
26159 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
26161 /* Local variable names (and the `this' keyword) may not
26162 appear in a default argument. */
26163 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26164 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
26165 /* Parse the assignment-expression. */
26166 if (template_parm_p)
26167 push_deferring_access_checks (dk_no_deferred);
26168 tree saved_class_ptr = NULL_TREE;
26169 tree saved_class_ref = NULL_TREE;
26170 /* The "this" pointer is not valid in a default argument. */
26171 if (cfun)
26173 saved_class_ptr = current_class_ptr;
26174 cp_function_chain->x_current_class_ptr = NULL_TREE;
26175 saved_class_ref = current_class_ref;
26176 cp_function_chain->x_current_class_ref = NULL_TREE;
26178 default_argument = cp_parser_initializer (parser);
26179 /* Restore the "this" pointer. */
26180 if (cfun)
26182 cp_function_chain->x_current_class_ptr = saved_class_ptr;
26183 cp_function_chain->x_current_class_ref = saved_class_ref;
26185 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
26186 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26187 if (template_parm_p)
26188 pop_deferring_access_checks ();
26189 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
26190 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26192 return default_argument;
26195 /* Parse a function-body.
26197 function-body:
26198 compound_statement */
26200 static void
26201 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
26203 cp_parser_compound_statement (parser, NULL, (in_function_try_block
26204 ? BCS_TRY_BLOCK : BCS_NORMAL),
26205 true);
26208 /* Parse a ctor-initializer-opt followed by a function-body. Return
26209 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
26210 is true we are parsing a function-try-block. */
26212 static void
26213 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
26214 bool in_function_try_block)
26216 tree body, list;
26217 const bool check_body_p
26218 = (DECL_CONSTRUCTOR_P (current_function_decl)
26219 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
26220 tree last = NULL;
26222 if (in_function_try_block
26223 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26224 && cxx_dialect < cxx20)
26226 if (DECL_CONSTRUCTOR_P (current_function_decl))
26227 pedwarn (input_location, OPT_Wc__20_extensions,
26228 "function-try-block body of %<constexpr%> constructor only "
26229 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26230 else
26231 pedwarn (input_location, OPT_Wc__20_extensions,
26232 "function-try-block body of %<constexpr%> function only "
26233 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26236 /* Begin the function body. */
26237 body = begin_function_body ();
26238 /* Parse the optional ctor-initializer. */
26239 cp_parser_ctor_initializer_opt (parser);
26241 /* If we're parsing a constexpr constructor definition, we need
26242 to check that the constructor body is indeed empty. However,
26243 before we get to cp_parser_function_body lot of junk has been
26244 generated, so we can't just check that we have an empty block.
26245 Rather we take a snapshot of the outermost block, and check whether
26246 cp_parser_function_body changed its state. */
26247 if (check_body_p)
26249 list = cur_stmt_list;
26250 if (STATEMENT_LIST_TAIL (list))
26251 last = STATEMENT_LIST_TAIL (list)->stmt;
26253 /* Parse the function-body. */
26254 cp_parser_function_body (parser, in_function_try_block);
26255 if (check_body_p)
26256 check_constexpr_ctor_body (last, list, /*complain=*/true);
26257 /* Finish the function body. */
26258 finish_function_body (body);
26261 /* Parse an initializer.
26263 initializer:
26264 = initializer-clause
26265 ( expression-list )
26267 Returns an expression representing the initializer. If no
26268 initializer is present, NULL_TREE is returned.
26270 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
26271 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
26272 set to TRUE if there is no initializer present. If there is an
26273 initializer, and it is not a constant-expression, *NON_CONSTANT_P
26274 is set to true; otherwise it is set to false. */
26276 static tree
26277 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
26278 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
26280 cp_token *token;
26281 tree init;
26283 /* Peek at the next token. */
26284 token = cp_lexer_peek_token (parser->lexer);
26286 /* Let our caller know whether or not this initializer was
26287 parenthesized. */
26288 if (is_direct_init)
26289 *is_direct_init = (token->type != CPP_EQ);
26290 /* Assume that the initializer is constant. */
26291 if (non_constant_p)
26292 *non_constant_p = false;
26294 if (token->type == CPP_EQ)
26296 /* Consume the `='. */
26297 cp_lexer_consume_token (parser->lexer);
26298 /* Parse the initializer-clause. */
26299 init = cp_parser_initializer_clause (parser, non_constant_p);
26301 else if (token->type == CPP_OPEN_PAREN)
26303 vec<tree, va_gc> *vec;
26304 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26305 /*cast_p=*/false,
26306 /*allow_expansion_p=*/true,
26307 non_constant_p);
26308 if (vec == NULL)
26309 return error_mark_node;
26310 init = build_tree_list_vec (vec);
26311 release_tree_vector (vec);
26313 else if (token->type == CPP_OPEN_BRACE)
26315 cp_lexer_set_source_position (parser->lexer);
26316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26317 init = cp_parser_braced_list (parser, non_constant_p);
26318 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
26320 else
26322 /* Anything else is an error. */
26323 cp_parser_error (parser, "expected initializer");
26324 init = error_mark_node;
26327 if (!subexpression_p && check_for_bare_parameter_packs (init))
26328 init = error_mark_node;
26330 return init;
26333 /* Parse an initializer-clause.
26335 initializer-clause:
26336 assignment-expression
26337 braced-init-list
26339 Returns an expression representing the initializer.
26341 If the `assignment-expression' production is used the value
26342 returned is simply a representation for the expression.
26344 Otherwise, calls cp_parser_braced_list. */
26346 static cp_expr
26347 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
26349 cp_expr initializer;
26351 /* Assume the expression is constant. */
26352 if (non_constant_p)
26353 *non_constant_p = false;
26355 /* If it is not a `{', then we are looking at an
26356 assignment-expression. */
26357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26359 initializer
26360 = cp_parser_constant_expression (parser,
26361 /*allow_non_constant_p=*/2,
26362 non_constant_p);
26364 else
26365 initializer = cp_parser_braced_list (parser, non_constant_p);
26367 return initializer;
26370 /* Parse a brace-enclosed initializer list.
26372 braced-init-list:
26373 { initializer-list , [opt] }
26374 { designated-initializer-list , [opt] }
26377 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
26378 the elements of the initializer-list (or NULL, if the last
26379 production is used). The TREE_TYPE for the CONSTRUCTOR will be
26380 NULL_TREE. There is no way to detect whether or not the optional
26381 trailing `,' was provided. NON_CONSTANT_P is as for
26382 cp_parser_initializer. */
26384 static cp_expr
26385 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
26387 tree initializer;
26388 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26389 auto oas = make_temp_override (parser->omp_array_section_p, false);
26391 /* Consume the `{' token. */
26392 matching_braces braces;
26393 braces.require_open (parser);
26394 /* Create a CONSTRUCTOR to represent the braced-initializer. */
26395 initializer = make_node (CONSTRUCTOR);
26396 /* If it's not a `}', then there is a non-trivial initializer. */
26397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
26399 bool designated;
26400 /* Parse the initializer list. */
26401 CONSTRUCTOR_ELTS (initializer)
26402 = cp_parser_initializer_list (parser, non_constant_p, &designated);
26403 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
26404 /* A trailing `,' token is allowed. */
26405 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26406 cp_lexer_consume_token (parser->lexer);
26408 else if (non_constant_p)
26409 *non_constant_p = false;
26410 /* Now, there should be a trailing `}'. */
26411 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
26412 braces.require_close (parser);
26413 TREE_TYPE (initializer) = init_list_type_node;
26414 recompute_constructor_flags (initializer);
26416 cp_expr result (initializer);
26417 /* Build a location of the form:
26418 { ... }
26419 ^~~~~~~
26420 with caret==start at the open brace, finish at the close brace. */
26421 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26422 result.set_location (combined_loc);
26423 return result;
26426 /* Consume tokens up to, but not including, the next non-nested closing `]'.
26427 Returns true iff we found a closing `]'. */
26429 static bool
26430 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
26432 unsigned square_depth = 0;
26434 while (true)
26436 cp_token * token = cp_lexer_peek_token (parser->lexer);
26438 switch (token->type)
26440 case CPP_PRAGMA_EOL:
26441 if (!parser->lexer->in_pragma)
26442 break;
26443 /* FALLTHRU */
26445 case CPP_EOF:
26446 /* If we've run out of tokens, then there is no closing `]'. */
26447 return false;
26449 case CPP_OPEN_SQUARE:
26450 ++square_depth;
26451 break;
26453 case CPP_CLOSE_SQUARE:
26454 if (!square_depth--)
26455 return true;
26456 break;
26458 default:
26459 break;
26462 /* Consume the current token, skipping it. */
26463 cp_lexer_consume_token (parser->lexer);
26467 /* Consume tokens up to, and including, the next non-nested closing `]'.
26468 Returns true iff we found a closing `]'. */
26470 static bool
26471 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
26473 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
26474 if (found)
26475 cp_lexer_consume_token (parser->lexer);
26476 return found;
26479 /* Return true if we are looking at an array-designator, false otherwise. */
26481 static bool
26482 cp_parser_array_designator_p (cp_parser *parser)
26484 /* Consume the `['. */
26485 cp_lexer_consume_token (parser->lexer);
26487 cp_lexer_save_tokens (parser->lexer);
26489 /* Skip tokens until the next token is a closing square bracket.
26490 If we find the closing `]', and the next token is a `=', then
26491 we are looking at an array designator. */
26492 bool array_designator_p
26493 = (cp_parser_skip_to_closing_square_bracket (parser)
26494 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
26496 /* Roll back the tokens we skipped. */
26497 cp_lexer_rollback_tokens (parser->lexer);
26499 return array_designator_p;
26502 /* Parse an initializer-list.
26504 initializer-list:
26505 initializer-clause ... [opt]
26506 initializer-list , initializer-clause ... [opt]
26508 C++20 Extension:
26510 designated-initializer-list:
26511 designated-initializer-clause
26512 designated-initializer-list , designated-initializer-clause
26514 designated-initializer-clause:
26515 designator brace-or-equal-initializer
26517 designator:
26518 . identifier
26520 GNU Extension:
26522 initializer-list:
26523 designation initializer-clause ...[opt]
26524 initializer-list , designation initializer-clause ...[opt]
26526 designation:
26527 . identifier =
26528 identifier :
26529 [ constant-expression ] =
26531 Returns a vec of constructor_elt. The VALUE of each elt is an expression
26532 for the initializer. If the INDEX of the elt is non-NULL, it is the
26533 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
26534 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
26535 are any designators. */
26537 static vec<constructor_elt, va_gc> *
26538 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
26539 bool *designated)
26541 vec<constructor_elt, va_gc> *v = NULL;
26542 bool first_p = true;
26543 tree first_designator = NULL_TREE;
26545 /* Assume all of the expressions are constant. */
26546 if (non_constant_p)
26547 *non_constant_p = false;
26549 unsigned nelts = 0;
26550 int suppress = suppress_location_wrappers;
26552 /* Parse the rest of the list. */
26553 while (true)
26555 cp_token *token;
26556 tree designator;
26557 tree initializer;
26558 bool clause_non_constant_p;
26559 bool direct_p = false;
26560 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26562 /* Handle the C++20 syntax, '. id ='. */
26563 if ((cxx_dialect >= cxx20
26564 || cp_parser_allow_gnu_extensions_p (parser))
26565 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
26566 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
26567 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
26568 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
26569 == CPP_OPEN_BRACE)))
26571 if (pedantic && cxx_dialect < cxx20)
26572 pedwarn (loc, OPT_Wc__20_extensions,
26573 "C++ designated initializers only available with "
26574 "%<-std=c++20%> or %<-std=gnu++20%>");
26575 /* Consume the `.'. */
26576 cp_lexer_consume_token (parser->lexer);
26577 /* Consume the identifier. */
26578 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26579 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26580 /* Consume the `='. */
26581 cp_lexer_consume_token (parser->lexer);
26582 else
26583 direct_p = true;
26585 /* Also, if the next token is an identifier and the following one is a
26586 colon, we are looking at the GNU designated-initializer
26587 syntax. */
26588 else if (cp_parser_allow_gnu_extensions_p (parser)
26589 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
26590 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26591 == CPP_COLON))
26593 /* Warn the user that they are using an extension. */
26594 pedwarn (loc, OPT_Wpedantic,
26595 "ISO C++ does not allow GNU designated initializers");
26596 /* Consume the identifier. */
26597 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26598 /* Consume the `:'. */
26599 cp_lexer_consume_token (parser->lexer);
26601 /* Also handle C99 array designators, '[ const ] ='. */
26602 else if (cp_parser_allow_gnu_extensions_p (parser)
26603 && !c_dialect_objc ()
26604 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26606 /* In C++11, [ could start a lambda-introducer. */
26607 bool non_const = false;
26609 cp_parser_parse_tentatively (parser);
26611 if (!cp_parser_array_designator_p (parser))
26613 cp_parser_simulate_error (parser);
26614 designator = NULL_TREE;
26616 else
26618 designator = cp_parser_constant_expression (parser, true,
26619 &non_const);
26620 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26621 cp_parser_require (parser, CPP_EQ, RT_EQ);
26624 if (!cp_parser_parse_definitely (parser))
26625 designator = NULL_TREE;
26626 else if (non_const
26627 && (!require_potential_rvalue_constant_expression
26628 (designator)))
26629 designator = NULL_TREE;
26630 if (designator)
26631 /* Warn the user that they are using an extension. */
26632 pedwarn (loc, OPT_Wpedantic,
26633 "ISO C++ does not allow C99 designated initializers");
26635 else
26636 designator = NULL_TREE;
26638 if (first_p)
26640 first_designator = designator;
26641 first_p = false;
26643 else if (cxx_dialect >= cxx20
26644 && first_designator != error_mark_node
26645 && (!first_designator != !designator))
26647 error_at (loc, "either all initializer clauses should be designated "
26648 "or none of them should be");
26649 first_designator = error_mark_node;
26651 else if (cxx_dialect < cxx20 && !first_designator)
26652 first_designator = designator;
26654 /* Parse the initializer. */
26655 initializer = cp_parser_initializer_clause (parser,
26656 (non_constant_p != nullptr
26657 ? &clause_non_constant_p
26658 : nullptr));
26659 /* If any clause is non-constant, so is the entire initializer. */
26660 if (non_constant_p && clause_non_constant_p)
26661 *non_constant_p = true;
26663 if (TREE_CODE (initializer) == CONSTRUCTOR)
26664 /* This uses |= rather than = because C_I_D_I could have been set in
26665 cp_parser_functional_cast so we must be careful not to clear the
26666 flag. */
26667 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26669 /* If we have an ellipsis, this is an initializer pack
26670 expansion. */
26671 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26675 /* Consume the `...'. */
26676 cp_lexer_consume_token (parser->lexer);
26678 if (designator && cxx_dialect >= cxx20)
26679 error_at (loc,
26680 "%<...%> not allowed in designated initializer list");
26682 /* Turn the initializer into an initializer expansion. */
26683 initializer = make_pack_expansion (initializer);
26686 /* Add it to the vector. */
26687 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26689 /* If the next token is not a comma, we have reached the end of
26690 the list. */
26691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26692 break;
26694 /* Peek at the next token. */
26695 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26696 /* If the next token is a `}', then we're still done. An
26697 initializer-clause can have a trailing `,' after the
26698 initializer-list and before the closing `}'. */
26699 if (token->type == CPP_CLOSE_BRACE)
26700 break;
26702 /* Suppress location wrappers in a long initializer to save memory
26703 (14179). The cutoff is chosen arbitrarily. */
26704 const unsigned loc_max = 256;
26705 unsigned incr = 1;
26706 if (TREE_CODE (initializer) == CONSTRUCTOR)
26707 /* Look one level down because it's easy. Looking deeper would require
26708 passing down a nelts pointer, and I don't think multi-level massive
26709 initializers are common enough to justify this. */
26710 incr = CONSTRUCTOR_NELTS (initializer);
26711 nelts += incr;
26712 if (nelts >= loc_max && (nelts - incr) < loc_max)
26713 ++suppress_location_wrappers;
26715 /* Consume the `,' token. */
26716 cp_lexer_consume_token (parser->lexer);
26719 /* The same identifier shall not appear in multiple designators
26720 of a designated-initializer-list. */
26721 if (first_designator)
26723 unsigned int i;
26724 tree designator, val;
26725 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26726 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26728 if (IDENTIFIER_MARKED (designator))
26730 error_at (cp_expr_loc_or_input_loc (val),
26731 "%<.%s%> designator used multiple times in "
26732 "the same initializer list",
26733 IDENTIFIER_POINTER (designator));
26734 (*v)[i].index = error_mark_node;
26736 else
26737 IDENTIFIER_MARKED (designator) = 1;
26739 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26740 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26741 IDENTIFIER_MARKED (designator) = 0;
26744 suppress_location_wrappers = suppress;
26746 *designated = first_designator != NULL_TREE;
26747 return v;
26750 /* Classes [gram.class] */
26752 /* Parse a class-name.
26754 class-name:
26755 identifier
26756 template-id
26758 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26759 to indicate that names looked up in dependent types should be
26760 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26761 keyword has been used to indicate that the name that appears next
26762 is a template. TAG_TYPE indicates the explicit tag given before
26763 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26764 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26765 is the class being defined in a class-head. If ENUM_OK is TRUE,
26766 enum-names are also accepted.
26768 Returns the TYPE_DECL representing the class. */
26770 static tree
26771 cp_parser_class_name (cp_parser *parser,
26772 bool typename_keyword_p,
26773 bool template_keyword_p,
26774 enum tag_types tag_type,
26775 bool check_dependency_p,
26776 bool class_head_p,
26777 bool is_declaration,
26778 bool enum_ok)
26780 tree decl;
26781 tree identifier = NULL_TREE;
26783 /* All class-names start with an identifier. */
26784 cp_token *token = cp_lexer_peek_token (parser->lexer);
26785 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26787 cp_parser_error (parser, "expected class-name");
26788 return error_mark_node;
26791 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26792 to a template-id, so we save it here. Consider object scope too,
26793 so that make_typename_type below can use it (cp_parser_template_name
26794 considers object scope also). This may happen with code like
26796 p->template A<T>::a()
26798 where we first want to look up A<T>::a in the class of the object
26799 expression, as per [basic.lookup.classref]. */
26800 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26801 /* This only checks parser->scope to avoid duplicate errors; if
26802 ->object_type is erroneous, go on to give a parse error. */
26803 if (parser->scope == error_mark_node)
26804 return error_mark_node;
26806 /* Any name names a type if we're following the `typename' keyword
26807 in a qualified name where the enclosing scope is type-dependent. */
26808 const bool typename_p = (typename_keyword_p
26809 && parser->scope
26810 && TYPE_P (parser->scope)
26811 && dependent_scope_p (parser->scope));
26812 /* Handle the common case (an identifier, but not a template-id)
26813 efficiently. */
26814 if (token->type == CPP_NAME
26815 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26817 cp_token *identifier_token;
26818 bool ambiguous_p;
26820 /* Look for the identifier. */
26821 identifier_token = cp_lexer_peek_token (parser->lexer);
26822 ambiguous_p = identifier_token->error_reported;
26823 identifier = cp_parser_identifier (parser);
26824 /* If the next token isn't an identifier, we are certainly not
26825 looking at a class-name. */
26826 if (identifier == error_mark_node)
26827 decl = error_mark_node;
26828 /* If we know this is a type-name, there's no need to look it
26829 up. */
26830 else if (typename_p)
26831 decl = identifier;
26832 else
26834 tree ambiguous_decls;
26835 /* If we already know that this lookup is ambiguous, then
26836 we've already issued an error message; there's no reason
26837 to check again. */
26838 if (ambiguous_p)
26840 cp_parser_simulate_error (parser);
26841 return error_mark_node;
26843 /* If the next token is a `::', then the name must be a type
26844 name.
26846 [basic.lookup.qual]
26848 During the lookup for a name preceding the :: scope
26849 resolution operator, object, function, and enumerator
26850 names are ignored. */
26851 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26852 tag_type = scope_type;
26853 /* Look up the name. */
26854 decl = cp_parser_lookup_name (parser, identifier,
26855 tag_type,
26856 /*is_template=*/false,
26857 /*is_namespace=*/false,
26858 check_dependency_p,
26859 &ambiguous_decls,
26860 identifier_token->location);
26861 if (ambiguous_decls)
26863 if (cp_parser_parsing_tentatively (parser))
26864 cp_parser_simulate_error (parser);
26865 return error_mark_node;
26869 else
26871 /* Try a template-id. */
26872 decl = cp_parser_template_id (parser, template_keyword_p,
26873 check_dependency_p,
26874 tag_type,
26875 is_declaration);
26876 if (decl == error_mark_node)
26877 return error_mark_node;
26880 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26882 /* If this is a typename, create a TYPENAME_TYPE. */
26883 if (typename_p && decl != error_mark_node)
26885 decl = make_typename_type (scope, decl, typename_type,
26886 /*complain=*/tf_error);
26887 if (decl != error_mark_node)
26888 decl = TYPE_NAME (decl);
26891 decl = strip_using_decl (decl);
26893 /* Check to see that it is really the name of a class. */
26894 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26895 && identifier_p (TREE_OPERAND (decl, 0))
26896 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26897 /* Situations like this:
26899 template <typename T> struct A {
26900 typename T::template X<int>::I i;
26903 are problematic. Is `T::template X<int>' a class-name? The
26904 standard does not seem to be definitive, but there is no other
26905 valid interpretation of the following `::'. Therefore, those
26906 names are considered class-names. */
26908 decl = make_typename_type (scope, decl, tag_type, tf_error);
26909 if (decl != error_mark_node)
26910 decl = TYPE_NAME (decl);
26912 else if (TREE_CODE (decl) != TYPE_DECL
26913 || TREE_TYPE (decl) == error_mark_node
26914 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26915 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26916 /* In Objective-C 2.0, a classname followed by '.' starts a
26917 dot-syntax expression, and it's not a type-name. */
26918 || (c_dialect_objc ()
26919 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26920 && objc_is_class_name (decl)))
26921 decl = error_mark_node;
26923 if (decl == error_mark_node)
26924 cp_parser_error (parser, "expected class-name");
26925 else if (identifier && !parser->scope)
26926 maybe_note_name_used_in_class (identifier, decl);
26928 return decl;
26931 /* Make sure that any member-function parameters are in scope.
26932 For instance, a function's noexcept-specifier can use the function's
26933 parameters:
26935 struct S {
26936 void fn (int p) noexcept(noexcept(p));
26939 so we need to make sure name lookup can find them. This is used
26940 when we delay parsing of the noexcept-specifier. */
26942 static void
26943 inject_parm_decls (tree decl)
26945 begin_scope (sk_function_parms, decl);
26946 tree args = DECL_ARGUMENTS (decl);
26948 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26950 if (args && is_this_parameter (args))
26952 gcc_checking_assert (current_class_ptr == NULL_TREE);
26953 current_class_ref = cp_build_fold_indirect_ref (args);
26954 current_class_ptr = args;
26958 /* Undo the effects of inject_parm_decls. */
26960 static void
26961 pop_injected_parms (void)
26963 pop_bindings_and_leave_scope ();
26964 current_class_ptr = current_class_ref = NULL_TREE;
26967 /* Parse a class-specifier.
26969 class-specifier:
26970 class-head { member-specification [opt] }
26972 Returns the TREE_TYPE representing the class. */
26974 tree
26975 cp_parser_class_specifier (cp_parser* parser)
26977 auto_timevar tv (TV_PARSE_STRUCT);
26979 tree type;
26980 tree attributes = NULL_TREE;
26981 bool nested_name_specifier_p;
26982 unsigned saved_num_template_parameter_lists;
26983 bool saved_in_function_body;
26984 unsigned char in_statement;
26985 bool in_switch_statement_p;
26986 bool saved_in_unbraced_linkage_specification_p;
26987 bool saved_in_unbraced_export_declaration_p;
26988 tree old_scope = NULL_TREE;
26989 tree scope = NULL_TREE;
26990 cp_token *closing_brace;
26992 push_deferring_access_checks (dk_no_deferred);
26994 /* Parse the class-head. */
26995 type = cp_parser_class_head (parser,
26996 &nested_name_specifier_p);
26997 /* If the class-head was a semantic disaster, skip the entire body
26998 of the class. */
26999 if (!type)
27001 cp_parser_skip_to_end_of_block_or_statement (parser);
27002 pop_deferring_access_checks ();
27003 return error_mark_node;
27006 /* Look for the `{'. */
27007 matching_braces braces;
27008 if (!braces.require_open (parser))
27010 pop_deferring_access_checks ();
27011 return error_mark_node;
27014 cp_ensure_no_omp_declare_simd (parser);
27015 cp_ensure_no_oacc_routine (parser);
27017 /* Issue an error message if type-definitions are forbidden here. */
27018 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
27019 /* Remember that we are defining one more class. */
27020 ++parser->num_classes_being_defined;
27021 /* Inside the class, surrounding template-parameter-lists do not
27022 apply. */
27023 saved_num_template_parameter_lists
27024 = parser->num_template_parameter_lists;
27025 parser->num_template_parameter_lists = 0;
27026 /* We are not in a function body. */
27027 saved_in_function_body = parser->in_function_body;
27028 parser->in_function_body = false;
27029 /* Or in a loop. */
27030 in_statement = parser->in_statement;
27031 parser->in_statement = 0;
27032 /* Or in a switch. */
27033 in_switch_statement_p = parser->in_switch_statement_p;
27034 parser->in_switch_statement_p = false;
27035 /* We are not immediately inside an extern "lang" block. */
27036 saved_in_unbraced_linkage_specification_p
27037 = parser->in_unbraced_linkage_specification_p;
27038 parser->in_unbraced_linkage_specification_p = false;
27039 /* Or in an export-declaration. */
27040 saved_in_unbraced_export_declaration_p
27041 = parser->in_unbraced_export_declaration_p;
27042 parser->in_unbraced_export_declaration_p = false;
27043 /* 'this' from an enclosing non-static member function is unavailable. */
27044 tree saved_ccp = current_class_ptr;
27045 tree saved_ccr = current_class_ref;
27046 current_class_ptr = NULL_TREE;
27047 current_class_ref = NULL_TREE;
27049 /* Start the class. */
27050 if (nested_name_specifier_p)
27052 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
27053 /* SCOPE must be a scope nested inside current scope. */
27054 if (is_nested_namespace (current_namespace,
27055 decl_namespace_context (scope)))
27056 old_scope = push_inner_scope (scope);
27057 else
27058 nested_name_specifier_p = false;
27060 type = begin_class_definition (type);
27062 if (type == error_mark_node)
27063 /* If the type is erroneous, skip the entire body of the class. */
27064 cp_parser_skip_to_closing_brace (parser);
27065 else
27066 /* Parse the member-specification. */
27067 cp_parser_member_specification_opt (parser);
27069 /* Look for the trailing `}'. */
27070 closing_brace = braces.require_close (parser);
27071 /* Look for trailing attributes to apply to this class. */
27072 if (cp_parser_allow_gnu_extensions_p (parser))
27073 attributes = cp_parser_gnu_attributes_opt (parser);
27074 if (type != error_mark_node)
27075 type = finish_struct (type, attributes);
27076 if (nested_name_specifier_p)
27077 pop_inner_scope (old_scope, scope);
27079 /* We've finished a type definition. Check for the common syntax
27080 error of forgetting a semicolon after the definition. We need to
27081 be careful, as we can't just check for not-a-semicolon and be done
27082 with it; the user might have typed:
27084 class X { } c = ...;
27085 class X { } *p = ...;
27087 and so forth. Instead, enumerate all the possible tokens that
27088 might follow this production; if we don't see one of them, then
27089 complain and silently insert the semicolon. */
27091 cp_token *token = cp_lexer_peek_token (parser->lexer);
27092 bool want_semicolon = true;
27094 if (cp_next_tokens_can_be_std_attribute_p (parser))
27095 /* Don't try to parse c++11 attributes here. As per the
27096 grammar, that should be a task for
27097 cp_parser_decl_specifier_seq. */
27098 want_semicolon = false;
27100 switch (token->type)
27102 case CPP_NAME:
27103 case CPP_SEMICOLON:
27104 case CPP_MULT:
27105 case CPP_AND:
27106 case CPP_OPEN_PAREN:
27107 case CPP_CLOSE_PAREN:
27108 case CPP_COMMA:
27109 case CPP_SCOPE:
27110 want_semicolon = false;
27111 break;
27113 /* While it's legal for type qualifiers and storage class
27114 specifiers to follow type definitions in the grammar, only
27115 compiler testsuites contain code like that. Assume that if
27116 we see such code, then what we're really seeing is a case
27117 like:
27119 class X { }
27120 const <type> var = ...;
27124 class Y { }
27125 static <type> func (...) ...
27127 i.e. the qualifier or specifier applies to the next
27128 declaration. To do so, however, we need to look ahead one
27129 more token to see if *that* token is a type specifier.
27131 This code could be improved to handle:
27133 class Z { }
27134 static const <type> var = ...; */
27135 case CPP_KEYWORD:
27136 if (keyword_is_decl_specifier (token->keyword))
27138 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
27140 /* Handling user-defined types here would be nice, but very
27141 tricky. */
27142 want_semicolon
27143 = (lookahead->type == CPP_KEYWORD
27144 && keyword_begins_type_specifier (lookahead->keyword));
27146 break;
27147 default:
27148 break;
27151 /* If we don't have a type, then something is very wrong and we
27152 shouldn't try to do anything clever. Likewise for not seeing the
27153 closing brace. */
27154 if (closing_brace && TYPE_P (type) && want_semicolon)
27156 /* Locate the closing brace. */
27157 cp_token_position prev
27158 = cp_lexer_previous_token_position (parser->lexer);
27159 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
27160 location_t loc = prev_token->location;
27162 /* We want to suggest insertion of a ';' immediately *after* the
27163 closing brace, so, if we can, offset the location by 1 column. */
27164 location_t next_loc = loc;
27165 if (!linemap_location_from_macro_expansion_p (line_table, loc))
27166 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
27168 rich_location richloc (line_table, next_loc);
27170 /* If we successfully offset the location, suggest the fix-it. */
27171 if (next_loc != loc)
27172 richloc.add_fixit_insert_before (next_loc, ";");
27174 if (CLASSTYPE_DECLARED_CLASS (type))
27175 error_at (&richloc,
27176 "expected %<;%> after class definition");
27177 else if (TREE_CODE (type) == RECORD_TYPE)
27178 error_at (&richloc,
27179 "expected %<;%> after struct definition");
27180 else if (TREE_CODE (type) == UNION_TYPE)
27181 error_at (&richloc,
27182 "expected %<;%> after union definition");
27183 else
27184 gcc_unreachable ();
27186 /* Unget one token and smash it to look as though we encountered
27187 a semicolon in the input stream. */
27188 cp_lexer_set_token_position (parser->lexer, prev);
27189 token = cp_lexer_peek_token (parser->lexer);
27190 token->type = CPP_SEMICOLON;
27191 token->keyword = RID_MAX;
27195 /* If this class is not itself within the scope of another class,
27196 then we need to parse the bodies of all of the queued function
27197 definitions. Note that the queued functions defined in a class
27198 are not always processed immediately following the
27199 class-specifier for that class. Consider:
27201 struct A {
27202 struct B { void f() { sizeof (A); } };
27205 If `f' were processed before the processing of `A' were
27206 completed, there would be no way to compute the size of `A'.
27207 Note that the nesting we are interested in here is lexical --
27208 not the semantic nesting given by TYPE_CONTEXT. In particular,
27209 for:
27211 struct A { struct B; };
27212 struct A::B { void f() { } };
27214 there is no need to delay the parsing of `A::B::f'. */
27215 if (--parser->num_classes_being_defined == 0)
27217 tree decl;
27218 tree class_type = NULL_TREE;
27219 tree pushed_scope = NULL_TREE;
27220 unsigned ix;
27221 cp_default_arg_entry *e;
27223 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
27225 /* Skip default arguments, NSDMIs, etc, in order to improve
27226 error recovery (c++/71169, c++/71832). */
27227 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27228 vec_safe_truncate (unparsed_nsdmis, 0);
27229 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27232 /* In a first pass, parse default arguments to the functions.
27233 Then, in a second pass, parse the bodies of the functions.
27234 This two-phased approach handles cases like:
27236 struct S {
27237 void f() { g(); }
27238 void g(int i = 3);
27242 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
27244 decl = e->decl;
27245 /* If there are default arguments that have not yet been processed,
27246 take care of them now. */
27247 if (class_type != e->class_type)
27249 if (pushed_scope)
27250 pop_scope (pushed_scope);
27251 class_type = e->class_type;
27252 pushed_scope = push_scope (class_type);
27254 /* Make sure that any template parameters are in scope. */
27255 maybe_begin_member_template_processing (decl);
27256 /* Parse the default argument expressions. */
27257 cp_parser_late_parsing_default_args (parser, decl);
27258 /* Remove any template parameters from the symbol table. */
27259 maybe_end_member_template_processing ();
27261 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27263 /* If there are noexcept-specifiers that have not yet been processed,
27264 take care of them now. Do this before processing NSDMIs as they
27265 may depend on noexcept-specifiers already having been processed. */
27266 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
27268 tree ctx = DECL_CONTEXT (decl);
27269 if (class_type != ctx)
27271 if (pushed_scope)
27272 pop_scope (pushed_scope);
27273 class_type = ctx;
27274 pushed_scope = push_scope (class_type);
27277 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
27278 def_parse = TREE_PURPOSE (def_parse);
27280 /* Make sure that any template parameters are in scope. */
27281 maybe_begin_member_template_processing (decl);
27283 /* Make sure that any member-function parameters are in scope.
27284 This function doesn't expect ccp to be set. */
27285 current_class_ptr = current_class_ref = NULL_TREE;
27286 inject_parm_decls (decl);
27288 /* 'this' is not allowed in static member functions. */
27289 unsigned char local_variables_forbidden_p
27290 = parser->local_variables_forbidden_p;
27291 if (DECL_THIS_STATIC (decl))
27292 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27294 /* Now we can parse the noexcept-specifier. */
27295 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
27297 if (spec == error_mark_node)
27298 spec = NULL_TREE;
27300 /* Update the fn's type directly -- it might have escaped
27301 beyond this decl :( */
27302 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
27303 /* Update any instantiations we've already created. We must
27304 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
27305 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
27306 in the pattern. */
27307 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
27308 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
27309 = spec ? TREE_PURPOSE (spec) : error_mark_node;
27311 /* Restore the state of local_variables_forbidden_p. */
27312 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27314 /* The finish_struct call above performed various override checking,
27315 but it skipped unparsed noexcept-specifier operands. Now that we
27316 have resolved them, check again. */
27317 noexcept_override_late_checks (decl);
27319 /* Remove any member-function parameters from the symbol table. */
27320 pop_injected_parms ();
27322 /* Remove any template parameters from the symbol table. */
27323 maybe_end_member_template_processing ();
27325 vec_safe_truncate (unparsed_noexcepts, 0);
27327 /* Now parse any NSDMIs. */
27328 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
27330 tree ctx = type_context_for_name_lookup (decl);
27331 if (class_type != ctx)
27333 if (pushed_scope)
27334 pop_scope (pushed_scope);
27335 class_type = ctx;
27336 pushed_scope = push_scope (class_type);
27338 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
27339 cp_parser_late_parsing_nsdmi (parser, decl);
27341 vec_safe_truncate (unparsed_nsdmis, 0);
27343 /* Now contract attributes. */
27344 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
27346 tree ctx = DECL_CONTEXT (decl);
27347 if (class_type != ctx)
27349 if (pushed_scope)
27350 pop_scope (pushed_scope);
27351 class_type = ctx;
27352 pushed_scope = push_scope (class_type);
27355 temp_override<tree> cfd(current_function_decl, decl);
27357 /* Make sure that any template parameters are in scope. */
27358 maybe_begin_member_template_processing (decl);
27360 /* Make sure that any member-function parameters are in scope.
27361 This function doesn't expect ccp to be set. */
27362 current_class_ptr = current_class_ref = NULL_TREE;
27363 inject_parm_decls (decl);
27365 /* 'this' is not allowed in static member functions. */
27366 unsigned char local_variables_forbidden_p
27367 = parser->local_variables_forbidden_p;
27368 if (DECL_THIS_STATIC (decl))
27369 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27371 /* Now we can parse contract conditions. */
27372 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
27374 if (cxx_contract_attribute_p (a))
27375 cp_parser_late_contract_condition (parser, decl, a);
27378 /* Restore the state of local_variables_forbidden_p. */
27379 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27381 /* Remove any member-function parameters from the symbol table. */
27382 pop_injected_parms ();
27384 /* Remove any template parameters from the symbol table. */
27385 maybe_end_member_template_processing ();
27387 /* Perform any deferred contract matching. */
27388 match_deferred_contracts (decl);
27390 vec_safe_truncate (unparsed_contracts, 0);
27392 current_class_ptr = NULL_TREE;
27393 current_class_ref = NULL_TREE;
27394 if (pushed_scope)
27395 pop_scope (pushed_scope);
27397 /* Now parse the body of the functions. */
27398 if (flag_openmp)
27400 /* OpenMP UDRs need to be parsed before all other functions. */
27401 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27402 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
27403 cp_parser_late_parsing_for_member (parser, decl);
27404 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27405 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
27406 cp_parser_late_parsing_for_member (parser, decl);
27408 else
27409 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27410 cp_parser_late_parsing_for_member (parser, decl);
27411 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27414 /* Put back any saved access checks. */
27415 pop_deferring_access_checks ();
27417 /* Restore saved state. */
27418 parser->in_switch_statement_p = in_switch_statement_p;
27419 parser->in_statement = in_statement;
27420 parser->in_function_body = saved_in_function_body;
27421 parser->num_template_parameter_lists
27422 = saved_num_template_parameter_lists;
27423 parser->in_unbraced_linkage_specification_p
27424 = saved_in_unbraced_linkage_specification_p;
27425 parser->in_unbraced_export_declaration_p
27426 = saved_in_unbraced_export_declaration_p;
27427 current_class_ptr = saved_ccp;
27428 current_class_ref = saved_ccr;
27430 return type;
27433 /* Parse a class-head.
27435 class-head:
27436 class-key identifier [opt] base-clause [opt]
27437 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
27438 class-key nested-name-specifier [opt] template-id
27439 base-clause [opt]
27441 class-virt-specifier:
27442 final
27444 GNU Extensions:
27445 class-key attributes identifier [opt] base-clause [opt]
27446 class-key attributes nested-name-specifier identifier base-clause [opt]
27447 class-key attributes nested-name-specifier [opt] template-id
27448 base-clause [opt]
27450 Upon return BASES is initialized to the list of base classes (or
27451 NULL, if there are none) in the same form returned by
27452 cp_parser_base_clause.
27454 Returns the TYPE of the indicated class. Sets
27455 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
27456 involving a nested-name-specifier was used, and FALSE otherwise.
27458 Returns error_mark_node if this is not a class-head.
27460 Returns NULL_TREE if the class-head is syntactically valid, but
27461 semantically invalid in a way that means we should skip the entire
27462 body of the class. */
27464 static tree
27465 cp_parser_class_head (cp_parser* parser,
27466 bool* nested_name_specifier_p)
27468 tree nested_name_specifier;
27469 enum tag_types class_key;
27470 tree id = NULL_TREE;
27471 tree type = NULL_TREE;
27472 tree attributes;
27473 tree bases;
27474 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
27475 bool template_id_p = false;
27476 bool qualified_p = false;
27477 bool invalid_nested_name_p = false;
27478 bool invalid_explicit_specialization_p = false;
27479 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27480 tree pushed_scope = NULL_TREE;
27481 unsigned num_templates;
27482 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
27483 /* Assume no nested-name-specifier will be present. */
27484 *nested_name_specifier_p = false;
27485 /* Assume no template parameter lists will be used in defining the
27486 type. */
27487 num_templates = 0;
27488 parser->colon_corrects_to_scope_p = false;
27490 /* Look for the class-key. */
27491 class_key = cp_parser_class_key (parser);
27492 if (class_key == none_type)
27493 return error_mark_node;
27495 location_t class_head_start_location = input_location;
27497 /* Parse the attributes. */
27498 attributes = cp_parser_attributes_opt (parser);
27499 if (find_contract (attributes))
27500 diagnose_misapplied_contracts (attributes);
27502 /* If the next token is `::', that is invalid -- but sometimes
27503 people do try to write:
27505 struct ::S {};
27507 Handle this gracefully by accepting the extra qualifier, and then
27508 issuing an error about it later if this really is a
27509 class-head. If it turns out just to be an elaborated type
27510 specifier, remain silent. */
27511 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
27512 qualified_p = true;
27514 /* It is OK to define an inaccessible class; for example:
27516 class A { class B; };
27517 class A::B {};
27519 So we want to ignore access when parsing the class name.
27520 However, we might be tentatively parsing what is really an
27521 elaborated-type-specifier naming a template-id, e.g.
27523 struct C<&D::m> c;
27525 In this case the tentative parse as a class-head will fail, but not
27526 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
27527 Since dk_no_check is sticky, we must instead use dk_deferred so that
27528 any such CPP_TEMPLATE_ID token created during this tentative parse
27529 will correctly capture the access checks imposed by the template-id . */
27530 push_deferring_access_checks (dk_deferred);
27532 /* Determine the name of the class. Begin by looking for an
27533 optional nested-name-specifier. */
27534 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
27535 nested_name_specifier
27536 = cp_parser_nested_name_specifier_opt (parser,
27537 /*typename_keyword_p=*/false,
27538 /*check_dependency_p=*/false,
27539 /*type_p=*/true,
27540 /*is_declaration=*/false);
27541 /* If there was a nested-name-specifier, then there *must* be an
27542 identifier. */
27544 cp_token *bad_template_keyword = NULL;
27546 if (nested_name_specifier)
27548 type_start_token = cp_lexer_peek_token (parser->lexer);
27549 /* Although the grammar says `identifier', it really means
27550 `class-name' or `template-name'. You are only allowed to
27551 define a class that has already been declared with this
27552 syntax.
27554 The proposed resolution for Core Issue 180 says that wherever
27555 you see `class T::X' you should treat `X' as a type-name.
27557 We do not know if we will see a class-name, or a
27558 template-name. We look for a class-name first, in case the
27559 class-name is a template-id; if we looked for the
27560 template-name first we would stop after the template-name. */
27561 cp_parser_parse_tentatively (parser);
27562 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27563 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
27564 type = cp_parser_class_name (parser,
27565 /*typename_keyword_p=*/false,
27566 /*template_keyword_p=*/false,
27567 class_type,
27568 /*check_dependency_p=*/false,
27569 /*class_head_p=*/true,
27570 /*is_declaration=*/false);
27571 /* If that didn't work, ignore the nested-name-specifier. */
27572 if (!cp_parser_parse_definitely (parser))
27574 invalid_nested_name_p = true;
27575 type_start_token = cp_lexer_peek_token (parser->lexer);
27576 id = cp_parser_identifier (parser);
27577 if (id == error_mark_node)
27578 id = NULL_TREE;
27580 /* If we could not find a corresponding TYPE, treat this
27581 declaration like an unqualified declaration. */
27582 if (type == error_mark_node)
27583 nested_name_specifier = NULL_TREE;
27584 /* Otherwise, count the number of templates used in TYPE and its
27585 containing scopes. */
27586 else
27587 num_templates = num_template_headers_for_class (TREE_TYPE (type));
27589 /* Otherwise, the identifier is optional. */
27590 else
27592 /* We don't know whether what comes next is a template-id,
27593 an identifier, or nothing at all. */
27594 cp_parser_parse_tentatively (parser);
27595 /* Check for a template-id. */
27596 type_start_token = cp_lexer_peek_token (parser->lexer);
27597 id = cp_parser_template_id (parser,
27598 /*template_keyword_p=*/false,
27599 /*check_dependency_p=*/true,
27600 class_key,
27601 /*is_declaration=*/true);
27602 /* If that didn't work, it could still be an identifier. */
27603 if (!cp_parser_parse_definitely (parser))
27605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27607 type_start_token = cp_lexer_peek_token (parser->lexer);
27608 id = cp_parser_identifier (parser);
27610 else
27611 id = NULL_TREE;
27613 else
27615 template_id_p = true;
27616 ++num_templates;
27620 pop_deferring_access_checks ();
27622 if (id)
27624 cp_parser_check_for_invalid_template_id (parser, id,
27625 class_key,
27626 type_start_token->location);
27628 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27630 /* If it's not a `:' or a `{' then we can't really be looking at a
27631 class-head, since a class-head only appears as part of a
27632 class-specifier. We have to detect this situation before calling
27633 xref_tag, since that has irreversible side-effects. */
27634 if (!cp_parser_next_token_starts_class_definition_p (parser))
27636 cp_parser_error (parser, "expected %<{%> or %<:%>");
27637 type = error_mark_node;
27638 goto out;
27641 /* At this point, we're going ahead with the class-specifier, even
27642 if some other problem occurs. */
27643 cp_parser_commit_to_tentative_parse (parser);
27644 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27646 cp_parser_error (parser,
27647 "cannot specify %<override%> for a class");
27648 type = error_mark_node;
27649 goto out;
27651 /* Issue the error about the overly-qualified name now. */
27652 if (qualified_p)
27654 cp_parser_error (parser,
27655 "global qualification of class name is invalid");
27656 type = error_mark_node;
27657 goto out;
27659 else if (invalid_nested_name_p)
27661 cp_parser_error (parser,
27662 "qualified name does not name a class");
27663 type = error_mark_node;
27664 goto out;
27666 else if (nested_name_specifier)
27668 tree scope;
27670 if (bad_template_keyword)
27671 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27672 keyword template shall not appear at the top level. */
27673 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27674 "keyword %<template%> not allowed in class-head-name");
27676 /* Reject typedef-names in class heads. */
27677 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27679 error_at (type_start_token->location,
27680 "invalid class name in declaration of %qD",
27681 type);
27682 type = NULL_TREE;
27683 goto done;
27686 /* Figure out in what scope the declaration is being placed. */
27687 scope = current_scope ();
27688 /* If that scope does not contain the scope in which the
27689 class was originally declared, the program is invalid. */
27690 if (scope && !is_ancestor (scope, nested_name_specifier))
27692 if (at_namespace_scope_p ())
27693 error_at (type_start_token->location,
27694 "declaration of %qD in namespace %qD which does not "
27695 "enclose %qD",
27696 type, scope, nested_name_specifier);
27697 else
27698 error_at (type_start_token->location,
27699 "declaration of %qD in %qD which does not enclose %qD",
27700 type, scope, nested_name_specifier);
27701 type = NULL_TREE;
27702 goto done;
27704 /* [dcl.meaning]
27706 A declarator-id shall not be qualified except for the
27707 definition of a ... nested class outside of its class
27708 ... [or] the definition or explicit instantiation of a
27709 class member of a namespace outside of its namespace. */
27710 if (scope == nested_name_specifier)
27711 permerror (nested_name_specifier_token_start->location,
27712 "extra qualification not allowed");
27714 /* The name-declaration of an export-declaration shall not declare
27715 a partial specialization. */
27716 if (template_id_p
27717 && parser->in_unbraced_export_declaration_p
27718 && !processing_specialization
27719 && !processing_explicit_instantiation)
27721 auto_diagnostic_group d;
27722 location_t loc = type_start_token->location;
27723 error_at (loc, "declaration of partial specialization in unbraced "
27724 "export-declaration");
27725 inform (loc, "a specialization is always exported alongside its "
27726 "primary template");
27728 /* An explicit-specialization must be preceded by "template <>". If
27729 it is not, try to recover gracefully. */
27730 if (at_namespace_scope_p ()
27731 && parser->num_template_parameter_lists == 0
27732 && !processing_template_parmlist
27733 && template_id_p)
27735 /* Build a location of this form:
27736 struct typename <ARGS>
27737 ^~~~~~~~~~~~~~~~~~~~~~
27738 with caret==start at the start token, and
27739 finishing at the end of the type. */
27740 location_t reported_loc
27741 = make_location (class_head_start_location,
27742 class_head_start_location,
27743 get_finish (type_start_token->location));
27744 rich_location richloc (line_table, reported_loc);
27745 richloc.add_fixit_insert_before (class_head_start_location,
27746 "template <> ");
27747 error_at (&richloc,
27748 "an explicit specialization must be preceded by"
27749 " %<template <>%>");
27750 invalid_explicit_specialization_p = true;
27751 /* Take the same action that would have been taken by
27752 cp_parser_explicit_specialization. */
27753 ++parser->num_template_parameter_lists;
27754 begin_specialization ();
27756 /* There must be no "return" statements between this point and the
27757 end of this function; set "type "to the correct return value and
27758 use "goto done;" to return. */
27759 /* Make sure that the right number of template parameters were
27760 present. */
27761 if (!cp_parser_check_template_parameters (parser, num_templates,
27762 template_id_p,
27763 type_start_token->location,
27764 /*declarator=*/NULL))
27766 /* If something went wrong, there is no point in even trying to
27767 process the class-definition. */
27768 type = NULL_TREE;
27769 goto done;
27772 /* Look up the type. */
27773 if (template_id_p)
27775 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27776 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27777 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27779 error_at (type_start_token->location,
27780 "function template %qD redeclared as a class template", id);
27781 type = error_mark_node;
27783 else
27785 type = TREE_TYPE (id);
27786 type = maybe_process_partial_specialization (type);
27788 /* Check the scope while we still know whether or not we had a
27789 nested-name-specifier. */
27790 if (type != error_mark_node)
27791 check_unqualified_spec_or_inst (type, type_start_token->location);
27793 if (nested_name_specifier)
27794 pushed_scope = push_scope (nested_name_specifier);
27796 else if (nested_name_specifier)
27798 type = TREE_TYPE (type);
27800 /* Given:
27802 template <typename T> struct S { struct T };
27803 template <typename T> struct S<T>::T { };
27805 we will get a TYPENAME_TYPE when processing the definition of
27806 `S::T'. We need to resolve it to the actual type before we
27807 try to define it. */
27808 if (TREE_CODE (type) == TYPENAME_TYPE)
27810 type = resolve_typename_type (type, /*only_current_p=*/false);
27811 if (TREE_CODE (type) == TYPENAME_TYPE)
27813 cp_parser_error (parser, "could not resolve typename type");
27814 type = error_mark_node;
27818 type = maybe_process_partial_specialization (type);
27819 if (type == error_mark_node)
27821 type = NULL_TREE;
27822 goto done;
27825 /* Enter the scope indicated by the nested-name-specifier. */
27826 pushed_scope = push_scope (nested_name_specifier);
27827 /* Get the canonical version of this type. */
27828 type = TYPE_MAIN_DECL (type);
27829 /* Call push_template_decl if it seems like we should be defining a
27830 template either from the template headers or the type we're
27831 defining, so that we diagnose both extra and missing headers. */
27832 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27833 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27834 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27836 type = push_template_decl (type);
27837 if (type == error_mark_node)
27839 type = NULL_TREE;
27840 goto done;
27844 type = TREE_TYPE (type);
27845 *nested_name_specifier_p = true;
27847 else /* The name is not a nested name. */
27849 /* If the class was unnamed, create a dummy name. */
27850 if (!id)
27851 id = make_anon_name ();
27852 TAG_how how = (parser->in_type_id_in_expr_p
27853 ? TAG_how::INNERMOST_NON_CLASS
27854 : TAG_how::CURRENT_ONLY);
27855 type = xref_tag (class_key, id, how,
27856 parser->num_template_parameter_lists);
27859 /* Diagnose class/struct/union mismatches. */
27860 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27861 true, true);
27863 /* Indicate whether this class was declared as a `class' or as a
27864 `struct'. */
27865 if (TREE_CODE (type) == RECORD_TYPE)
27866 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27868 /* If this type was already complete, and we see another definition,
27869 that's an error. Likewise if the type is already being defined:
27870 this can happen, eg, when it's defined from within an expression
27871 (c++/84605). */
27872 if (type != error_mark_node
27873 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27875 error_at (type_start_token->location, "redefinition of %q#T",
27876 type);
27877 inform (location_of (type), "previous definition of %q#T",
27878 type);
27879 type = NULL_TREE;
27880 goto done;
27882 else if (type == error_mark_node)
27883 type = NULL_TREE;
27885 if (type)
27887 if (current_lambda_expr ()
27888 && uses_parameter_packs (attributes))
27890 /* In a lambda this should work, but doesn't currently. */
27891 sorry ("unexpanded parameter pack in local class in lambda");
27892 attributes = NULL_TREE;
27895 /* Apply attributes now, before any use of the class as a template
27896 argument in its base list. */
27897 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27898 fixup_attribute_variants (type);
27901 /* Associate constraints with the type. */
27902 if (flag_concepts)
27903 type = associate_classtype_constraints (type);
27905 /* We will have entered the scope containing the class; the names of
27906 base classes should be looked up in that context. For example:
27908 struct A { struct B {}; struct C; };
27909 struct A::C : B {};
27911 is valid. */
27913 /* Get the list of base-classes, if there is one. Defer access checking
27914 until the entire list has been seen, as per [class.access.general]. */
27915 push_deferring_access_checks (dk_deferred);
27916 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27918 if (type)
27920 pushclass (type);
27921 start_lambda_scope (TYPE_NAME (type));
27923 bases = cp_parser_base_clause (parser);
27924 if (type)
27926 finish_lambda_scope ();
27927 popclass ();
27930 else
27931 bases = NULL_TREE;
27933 /* If we're really defining a class, process the base classes.
27934 If they're invalid, fail. */
27935 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27936 xref_basetypes (type, bases);
27938 /* Now that all bases have been seen and attached to the class, check
27939 accessibility of the types named in the base-clause. This must be
27940 done relative to the class scope, so that we accept e.g.
27942 struct A { protected: struct B {}; };
27943 struct C : A::B, A {}; // OK: A::B is accessible via base A
27945 as per [class.access.general]. */
27946 if (type)
27947 pushclass (type);
27948 pop_to_parent_deferring_access_checks ();
27949 if (type)
27950 popclass ();
27952 done:
27953 /* Leave the scope given by the nested-name-specifier. We will
27954 enter the class scope itself while processing the members. */
27955 if (pushed_scope)
27956 pop_scope (pushed_scope);
27958 if (invalid_explicit_specialization_p)
27960 end_specialization ();
27961 --parser->num_template_parameter_lists;
27964 if (type)
27965 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27966 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27967 CLASSTYPE_FINAL (type) = 1;
27968 out:
27969 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27970 return type;
27973 /* Parse a class-key.
27975 class-key:
27976 class
27977 struct
27978 union
27980 Returns the kind of class-key specified, or none_type to indicate
27981 error. */
27983 static enum tag_types
27984 cp_parser_class_key (cp_parser* parser)
27986 cp_token *token;
27987 enum tag_types tag_type;
27989 /* Look for the class-key. */
27990 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27991 if (!token)
27992 return none_type;
27994 /* Check to see if the TOKEN is a class-key. */
27995 tag_type = cp_parser_token_is_class_key (token);
27996 if (!tag_type)
27997 cp_parser_error (parser, "expected class-key");
27998 return tag_type;
28001 /* Parse a type-parameter-key.
28003 type-parameter-key:
28004 class
28005 typename
28008 static void
28009 cp_parser_type_parameter_key (cp_parser* parser)
28011 /* Look for the type-parameter-key. */
28012 enum tag_types tag_type = none_type;
28013 cp_token *token = cp_lexer_peek_token (parser->lexer);
28014 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
28016 cp_lexer_consume_token (parser->lexer);
28017 if (pedantic && tag_type == typename_type
28018 && cxx_dialect < cxx17)
28019 /* typename is not allowed in a template template parameter
28020 by the standard until C++17. */
28021 pedwarn (token->location, OPT_Wc__17_extensions,
28022 "ISO C++ forbids typename key in template template parameter;"
28023 " use %<-std=c++17%> or %<-std=gnu++17%>");
28025 else
28026 cp_parser_error (parser, "expected %<class%> or %<typename%>");
28028 return;
28031 /* Parse an (optional) member-specification.
28033 member-specification:
28034 member-declaration member-specification [opt]
28035 access-specifier : member-specification [opt] */
28037 static void
28038 cp_parser_member_specification_opt (cp_parser* parser)
28040 while (true)
28042 cp_token *token;
28043 enum rid keyword;
28045 /* Peek at the next token. */
28046 token = cp_lexer_peek_token (parser->lexer);
28047 /* If it's a `}', or EOF then we've seen all the members. */
28048 if (token->type == CPP_CLOSE_BRACE
28049 || token->type == CPP_EOF
28050 || token->type == CPP_PRAGMA_EOL)
28051 break;
28053 /* See if this token is a keyword. */
28054 keyword = token->keyword;
28055 switch (keyword)
28057 case RID_PUBLIC:
28058 case RID_PROTECTED:
28059 case RID_PRIVATE:
28060 /* Consume the access-specifier. */
28061 cp_lexer_consume_token (parser->lexer);
28062 /* Remember which access-specifier is active. */
28063 current_access_specifier = token->u.value;
28064 /* Look for the `:'. */
28065 cp_parser_require (parser, CPP_COLON, RT_COLON);
28066 break;
28068 default:
28069 /* Accept #pragmas at class scope. */
28070 if (token->type == CPP_PRAGMA)
28072 cp_parser_pragma (parser, pragma_member, NULL);
28073 break;
28076 /* Otherwise, the next construction must be a
28077 member-declaration. */
28078 cp_parser_member_declaration (parser);
28083 /* Parse a member-declaration.
28085 member-declaration:
28086 decl-specifier-seq [opt] member-declarator-list [opt] ;
28087 function-definition ; [opt]
28088 :: [opt] nested-name-specifier template [opt] unqualified-id ;
28089 using-declaration
28090 template-declaration
28091 alias-declaration
28093 member-declarator-list:
28094 member-declarator
28095 member-declarator-list , member-declarator
28097 member-declarator:
28098 declarator pure-specifier [opt]
28099 declarator constant-initializer [opt]
28100 identifier [opt] : constant-expression
28102 GNU Extensions:
28104 member-declaration:
28105 __extension__ member-declaration
28107 member-declarator:
28108 declarator attributes [opt] pure-specifier [opt]
28109 declarator attributes [opt] constant-initializer [opt]
28110 identifier [opt] attributes [opt] : constant-expression
28112 C++0x Extensions:
28114 member-declaration:
28115 static_assert-declaration */
28117 static void
28118 cp_parser_member_declaration (cp_parser* parser)
28120 cp_decl_specifier_seq decl_specifiers;
28121 tree prefix_attributes;
28122 tree decl;
28123 int declares_class_or_enum;
28124 bool friend_p;
28125 cp_token *token = NULL;
28126 cp_token *decl_spec_token_start = NULL;
28127 cp_token *initializer_token_start = NULL;
28128 int saved_pedantic;
28129 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28131 /* Check for the `__extension__' keyword. */
28132 if (cp_parser_extension_opt (parser, &saved_pedantic))
28134 /* Recurse. */
28135 cp_parser_member_declaration (parser);
28136 /* Restore the old value of the PEDANTIC flag. */
28137 pedantic = saved_pedantic;
28139 return;
28142 /* Check for a template-declaration. */
28143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28145 /* An explicit specialization here is an error condition, and we
28146 expect the specialization handler to detect and report this. */
28147 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
28148 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
28149 cp_parser_explicit_specialization (parser);
28150 else
28151 cp_parser_template_declaration (parser, /*member_p=*/true);
28153 return;
28155 /* Check for a template introduction. */
28156 else if (cp_parser_template_declaration_after_export (parser, true))
28157 return;
28159 /* Check for a using-declaration. */
28160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
28162 if (cxx_dialect < cxx11)
28163 /* Parse the using-declaration. */
28164 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
28165 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
28166 cp_parser_using_enum (parser);
28167 else
28169 tree decl;
28170 bool alias_decl_expected;
28171 cp_parser_parse_tentatively (parser);
28172 decl = cp_parser_alias_declaration (parser);
28173 /* Note that if we actually see the '=' token after the
28174 identifier, cp_parser_alias_declaration commits the
28175 tentative parse. In that case, we really expect an
28176 alias-declaration. Otherwise, we expect a using
28177 declaration. */
28178 alias_decl_expected =
28179 !cp_parser_uncommitted_to_tentative_parse_p (parser);
28180 cp_parser_parse_definitely (parser);
28182 if (alias_decl_expected)
28183 finish_member_declaration (decl);
28184 else
28185 cp_parser_using_declaration (parser,
28186 /*access_declaration_p=*/false);
28188 return;
28191 /* Check for @defs. */
28192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
28194 tree ivar, member;
28195 tree ivar_chains = cp_parser_objc_defs_expression (parser);
28196 ivar = ivar_chains;
28197 while (ivar)
28199 member = ivar;
28200 ivar = TREE_CHAIN (member);
28201 TREE_CHAIN (member) = NULL_TREE;
28202 finish_member_declaration (member);
28204 return;
28207 /* If the next token is `static_assert' we have a static assertion. */
28208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
28210 cp_parser_static_assert (parser, /*member_p=*/true);
28211 return;
28214 parser->colon_corrects_to_scope_p = false;
28216 cp_omp_declare_simd_data odsd;
28217 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
28218 goto out;
28220 /* Parse the decl-specifier-seq. */
28221 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28222 cp_parser_decl_specifier_seq (parser,
28223 (CP_PARSER_FLAGS_OPTIONAL
28224 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28225 &decl_specifiers,
28226 &declares_class_or_enum);
28228 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
28229 cp_parser_handle_directive_omp_attributes (parser,
28230 &decl_specifiers.attributes,
28231 &odsd, true);
28233 /* Check for an invalid type-name. */
28234 if (!decl_specifiers.any_type_specifiers_p
28235 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28236 goto out;
28237 /* If there is no declarator, then the decl-specifier-seq should
28238 specify a type. For C++26 Variadic friends don't just check for
28239 a semicolon, but also for a comma and in both cases optionally
28240 preceded by ellipsis. */
28241 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28242 || (cp_parser_friend_p (&decl_specifiers)
28243 && cxx_dialect >= cxx11
28244 && (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28245 || (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
28246 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON)
28247 || cp_lexer_nth_token_is (parser->lexer, 2,
28248 CPP_COMMA))))))
28250 /* If there was no decl-specifier-seq, and the next token is a
28251 `;', then we have something like:
28253 struct S { ; };
28255 [class.mem] used to say
28257 Each member-declaration shall declare at least one member
28258 name of the class.
28260 but since DR 1693:
28262 A member-declaration does not declare new members of the class
28263 if it is
28264 -- [...]
28265 -- an empty-declaration.
28266 For any other member-declaration, each declared entity that is not
28267 an unnamed bit-field is a member of the class, and each such
28268 member-declaration shall either declare at least one member name of
28269 the class or declare at least one unnamed bit-field. */
28270 if (!decl_specifiers.any_specifiers_p)
28272 cp_token *token = cp_lexer_peek_token (parser->lexer);
28273 maybe_warn_extra_semi (token->location, extra_semi_kind::member);
28275 else
28277 /* See if this declaration is a friend. */
28278 friend_p = cp_parser_friend_p (&decl_specifiers);
28279 /* If there were decl-specifiers, check to see if there was
28280 a class-declaration. */
28281 tree type = check_tag_decl (&decl_specifiers,
28282 /*explicit_type_instantiation_p=*/false);
28283 /* Nested classes have already been added to the class, but
28284 a `friend' needs to be explicitly registered. */
28285 if (friend_p)
28287 /* If the `friend' keyword was present, the friend must
28288 be introduced with a class-key. */
28289 if (!declares_class_or_enum && cxx_dialect < cxx11)
28290 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
28291 "in C++03 a class-key must be used "
28292 "when declaring a friend");
28293 if (!cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28294 && cxx_dialect < cxx26)
28295 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
28296 OPT_Wc__26_extensions,
28297 "variadic friends or friend type declarations with "
28298 "multiple types only available with "
28299 "%<-std=c++2c%> or %<-std=gnu++2c%>");
28300 location_t friend_loc = decl_specifiers.locations[ds_friend];
28303 /* In this case:
28305 template <typename T> struct A {
28306 friend struct A<T>::B;
28309 A<T>::B will be represented by a TYPENAME_TYPE, and
28310 therefore not recognized by check_tag_decl. */
28311 if (!type)
28313 type = decl_specifiers.type;
28314 if (type && TREE_CODE (type) == TYPE_DECL)
28315 type = TREE_TYPE (type);
28317 /* Warn if an attribute cannot appear here, as per
28318 [dcl.attr.grammar]/5. But not when
28319 declares_class_or_enum: we ignore attributes in
28320 elaborated-type-specifiers. */
28321 if (!declares_class_or_enum
28322 && cxx11_attribute_p (decl_specifiers.attributes))
28324 decl_specifiers.attributes = NULL_TREE;
28325 if (warning_at (decl_spec_token_start->location,
28326 OPT_Wattributes, "attribute ignored"))
28327 inform (decl_spec_token_start->location, "an attribute "
28328 "that appertains to a friend declaration that "
28329 "is not a definition is ignored");
28331 bool ellipsis = cp_lexer_next_token_is (parser->lexer,
28332 CPP_ELLIPSIS);
28333 if (ellipsis)
28334 cp_lexer_consume_token (parser->lexer);
28335 if (!type || !TYPE_P (type))
28336 error_at (decl_spec_token_start->location,
28337 "friend declaration does not name a class or "
28338 "function");
28339 else
28341 if (ellipsis)
28342 type = make_pack_expansion (type);
28343 if (type != error_mark_node)
28344 make_friend_class (current_class_type, type,
28345 /*complain=*/true);
28347 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28348 break;
28349 cp_lexer_consume_token (parser->lexer);
28350 clear_decl_specs (&decl_specifiers);
28351 decl_specifiers.locations[ds_friend] = friend_loc;
28352 decl_specifiers.any_specifiers_p = true;
28353 declares_class_or_enum = false;
28354 cp_parser_type_specifier (parser,
28355 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28356 &decl_specifiers,
28357 /*is_declaration=*/true,
28358 &declares_class_or_enum, NULL);
28359 type = check_tag_decl (&decl_specifiers,
28360 /*explicit_type_instantiation_p=*/
28361 false);
28363 while (1);
28365 /* If there is no TYPE, an error message will already have
28366 been issued. */
28367 else if (!type || type == error_mark_node)
28369 /* An anonymous aggregate has to be handled specially; such
28370 a declaration really declares a data member (with a
28371 particular type), as opposed to a nested class. */
28372 else if (ANON_AGGR_TYPE_P (type))
28374 /* C++11 9.5/6. */
28375 if (decl_specifiers.storage_class != sc_none)
28376 error_at (decl_spec_token_start->location,
28377 "a storage class on an anonymous aggregate "
28378 "in class scope is not allowed");
28380 /* Remove constructors and such from TYPE, now that we
28381 know it is an anonymous aggregate. */
28382 fixup_anonymous_aggr (type);
28383 /* And make the corresponding data member. */
28384 decl = build_decl (decl_spec_token_start->location,
28385 FIELD_DECL, NULL_TREE, type);
28386 /* Add it to the class. */
28387 finish_member_declaration (decl);
28389 else
28390 cp_parser_check_access_in_redeclaration
28391 (TYPE_NAME (type),
28392 decl_spec_token_start->location);
28395 else
28397 bool assume_semicolon = false;
28399 /* Clear attributes from the decl_specifiers but keep them
28400 around as prefix attributes that apply them to the entity
28401 being declared. */
28402 prefix_attributes = decl_specifiers.attributes;
28403 decl_specifiers.attributes = NULL_TREE;
28404 if (parser->omp_declare_simd
28405 && (parser->omp_declare_simd->attribs[0]
28406 == &decl_specifiers.attributes))
28407 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
28409 /* See if these declarations will be friends. */
28410 friend_p = cp_parser_friend_p (&decl_specifiers);
28412 /* Keep going until we hit the `;' at the end of the
28413 declaration. */
28414 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28416 tree attributes = NULL_TREE;
28417 tree first_attribute;
28418 tree initializer;
28419 bool named_bitfld = false;
28421 /* Peek at the next token. */
28422 token = cp_lexer_peek_token (parser->lexer);
28424 /* The following code wants to know early if it is a bit-field
28425 or some other declaration. Attributes can appear before
28426 the `:' token. Skip over them without consuming any tokens
28427 to peek if they are followed by `:'. */
28428 if (cp_next_tokens_can_be_attribute_p (parser)
28429 || (token->type == CPP_NAME
28430 && cp_nth_tokens_can_be_attribute_p (parser, 2)
28431 && (named_bitfld = true)))
28433 size_t n
28434 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
28435 token = cp_lexer_peek_nth_token (parser->lexer, n);
28438 /* Check for a bitfield declaration. */
28439 if (token->type == CPP_COLON
28440 || (token->type == CPP_NAME
28441 && token == cp_lexer_peek_token (parser->lexer)
28442 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
28443 && (named_bitfld = true)))
28445 tree identifier;
28446 tree width;
28447 tree late_attributes = NULL_TREE;
28448 location_t id_location
28449 = cp_lexer_peek_token (parser->lexer)->location;
28451 if (named_bitfld)
28452 identifier = cp_parser_identifier (parser);
28453 else
28454 identifier = NULL_TREE;
28456 /* Look for attributes that apply to the bitfield. */
28457 attributes = cp_parser_attributes_opt (parser);
28459 /* Consume the `:' token. */
28460 cp_lexer_consume_token (parser->lexer);
28462 /* Get the width of the bitfield. */
28463 width = cp_parser_constant_expression (parser, false, NULL,
28464 cxx_dialect >= cxx11);
28466 /* In C++20 and as extension for C++11 and above we allow
28467 default member initializers for bit-fields. */
28468 initializer = NULL_TREE;
28469 if (cxx_dialect >= cxx11
28470 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
28471 || cp_lexer_next_token_is (parser->lexer,
28472 CPP_OPEN_BRACE)))
28474 location_t loc
28475 = cp_lexer_peek_token (parser->lexer)->location;
28476 if (cxx_dialect < cxx20
28477 && identifier != NULL_TREE)
28478 pedwarn (loc, OPT_Wc__20_extensions,
28479 "default member initializers for bit-fields "
28480 "only available with %<-std=c++20%> or "
28481 "%<-std=gnu++20%>");
28483 initializer = cp_parser_save_nsdmi (parser);
28484 if (identifier == NULL_TREE)
28486 error_at (loc, "default member initializer for "
28487 "unnamed bit-field");
28488 initializer = NULL_TREE;
28491 else
28493 /* Look for attributes that apply to the bitfield after
28494 the `:' token and width. This is where GCC used to
28495 parse attributes in the past, pedwarn if there is
28496 a std attribute. */
28497 if (cp_next_tokens_can_be_std_attribute_p (parser))
28498 pedwarn (input_location, OPT_Wpedantic,
28499 "ISO C++ allows bit-field attributes only "
28500 "before the %<:%> token");
28502 late_attributes = cp_parser_attributes_opt (parser);
28505 attributes = attr_chainon (attributes, late_attributes);
28507 /* Remember which attributes are prefix attributes and
28508 which are not. */
28509 first_attribute = attributes;
28510 /* Combine the attributes. */
28511 attributes = attr_chainon (prefix_attributes, attributes);
28513 /* Create the bitfield declaration. */
28514 decl = grokbitfield (identifier
28515 ? make_id_declarator (NULL_TREE,
28516 identifier,
28517 sfk_none,
28518 id_location)
28519 : NULL,
28520 &decl_specifiers,
28521 width, initializer,
28522 attributes);
28524 else
28526 cp_declarator *declarator;
28527 tree asm_specification;
28528 int ctor_dtor_or_conv_p;
28529 bool static_p = (decl_specifiers.storage_class == sc_static);
28530 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
28531 /* We can't delay parsing for friends,
28532 alias-declarations, and typedefs, even though the
28533 standard seems to require it. */
28534 if (!friend_p
28535 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28536 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
28538 /* Parse the declarator. */
28539 declarator
28540 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28541 flags,
28542 &ctor_dtor_or_conv_p,
28543 /*parenthesized_p=*/NULL,
28544 /*member_p=*/true,
28545 friend_p, static_p);
28547 /* If something went wrong parsing the declarator, make sure
28548 that we at least consume some tokens. */
28549 if (declarator == cp_error_declarator)
28551 /* Skip to the end of the statement. */
28552 cp_parser_skip_to_end_of_statement (parser);
28553 /* If the next token is not a semicolon, that is
28554 probably because we just skipped over the body of
28555 a function. So, we consume a semicolon if
28556 present, but do not issue an error message if it
28557 is not present. */
28558 if (cp_lexer_next_token_is (parser->lexer,
28559 CPP_SEMICOLON))
28560 cp_lexer_consume_token (parser->lexer);
28561 goto out;
28564 /* Handle class-scope non-template C++17 deduction guides. */
28565 cp_parser_maybe_adjust_declarator_for_dguide (parser,
28566 &decl_specifiers,
28567 declarator,
28568 &ctor_dtor_or_conv_p);
28570 if (declares_class_or_enum & 2)
28571 cp_parser_check_for_definition_in_return_type
28572 (declarator, decl_specifiers.type,
28573 decl_specifiers.locations[ds_type_spec]);
28575 /* Look for an asm-specification. */
28576 asm_specification = cp_parser_asm_specification_opt (parser);
28577 /* Look for attributes that apply to the declaration. */
28578 attributes = cp_parser_attributes_opt (parser);
28579 /* Remember which attributes are prefix attributes and
28580 which are not. */
28581 first_attribute = attributes;
28582 /* Combine the attributes. */
28583 attributes = attr_chainon (prefix_attributes, attributes);
28585 /* If it's an `=', then we have a constant-initializer or a
28586 pure-specifier. It is not correct to parse the
28587 initializer before registering the member declaration
28588 since the member declaration should be in scope while
28589 its initializer is processed. However, the rest of the
28590 front end does not yet provide an interface that allows
28591 us to handle this correctly. */
28592 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28594 /* In [class.mem]:
28596 A pure-specifier shall be used only in the declaration of
28597 a virtual function.
28599 A member-declarator can contain a constant-initializer
28600 only if it declares a static member of integral or
28601 enumeration type.
28603 Therefore, if the DECLARATOR is for a function, we look
28604 for a pure-specifier; otherwise, we look for a
28605 constant-initializer. When we call `grokfield', it will
28606 perform more stringent semantics checks. */
28607 initializer_token_start = cp_lexer_peek_token (parser->lexer);
28608 declarator->init_loc = initializer_token_start->location;
28609 if (function_declarator_p (declarator)
28610 || (decl_specifiers.type
28611 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
28612 && declarator->kind == cdk_id
28613 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
28614 == FUNCTION_TYPE)))
28615 initializer = cp_parser_pure_specifier (parser);
28616 else if (decl_specifiers.storage_class != sc_static)
28617 initializer = cp_parser_save_nsdmi (parser);
28618 else if (cxx_dialect >= cxx11)
28620 /* Don't require a constant rvalue in C++11, since we
28621 might want a reference constant. We'll enforce
28622 constancy later. */
28623 cp_lexer_consume_token (parser->lexer);
28624 /* Parse the initializer. */
28625 initializer = cp_parser_initializer_clause (parser);
28627 else
28628 /* Parse the initializer. */
28629 initializer = cp_parser_constant_initializer (parser);
28631 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
28632 && !function_declarator_p (declarator))
28634 declarator->init_loc
28635 = cp_lexer_peek_token (parser->lexer)->location;
28636 if (decl_specifiers.storage_class != sc_static)
28637 initializer = cp_parser_save_nsdmi (parser);
28638 else
28639 initializer = cp_parser_initializer (parser);
28641 /* Detect invalid bit-field cases such as
28643 int *p : 4;
28644 int &&r : 3;
28646 and similar. */
28647 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
28648 /* If there were no type specifiers, it was a
28649 constructor. */
28650 && decl_specifiers.any_type_specifiers_p)
28652 /* This is called for a decent diagnostic only. */
28653 tree d = grokdeclarator (declarator, &decl_specifiers,
28654 BITFIELD, /*initialized=*/false,
28655 &attributes);
28656 if (!error_operand_p (d))
28657 error_at (DECL_SOURCE_LOCATION (d),
28658 "bit-field %qD has non-integral type %qT",
28659 d, TREE_TYPE (d));
28660 cp_parser_skip_to_end_of_statement (parser);
28661 /* Avoid "extra ;" pedwarns. */
28662 if (cp_lexer_next_token_is (parser->lexer,
28663 CPP_SEMICOLON))
28664 cp_lexer_consume_token (parser->lexer);
28665 goto out;
28667 /* Otherwise, there is no initializer. */
28668 else
28669 initializer = NULL_TREE;
28671 /* See if we are probably looking at a function
28672 definition. We are certainly not looking at a
28673 member-declarator. Calling `grokfield' has
28674 side-effects, so we must not do it unless we are sure
28675 that we are looking at a member-declarator. */
28676 if (cp_parser_token_starts_function_definition_p
28677 (cp_lexer_peek_token (parser->lexer)))
28679 /* The grammar does not allow a pure-specifier to be
28680 used when a member function is defined. (It is
28681 possible that this fact is an oversight in the
28682 standard, since a pure function may be defined
28683 outside of the class-specifier. */
28684 if (initializer && initializer_token_start)
28685 error_at (initializer_token_start->location,
28686 "pure-specifier on function-definition");
28687 decl = cp_parser_save_member_function_body (parser,
28688 &decl_specifiers,
28689 declarator,
28690 attributes);
28692 if (parser->fully_implicit_function_template_p)
28693 decl = finish_fully_implicit_template (parser, decl);
28694 /* If the member was not a friend, declare it here. */
28695 if (!friend_p)
28696 finish_member_declaration (decl);
28697 /* Peek at the next token. */
28698 token = cp_lexer_peek_token (parser->lexer);
28699 /* If the next token is a semicolon, consume it. */
28700 if (token->type == CPP_SEMICOLON)
28702 location_t semicolon_loc
28703 = cp_lexer_consume_token (parser->lexer)->location;
28704 maybe_warn_extra_semi (semicolon_loc,
28705 extra_semi_kind::in_class_fn_def);
28707 goto out;
28709 else
28710 if (declarator->kind == cdk_function)
28711 declarator->id_loc = token->location;
28713 /* Create the declaration. */
28714 decl = grokfield (declarator, &decl_specifiers,
28715 initializer, /*init_const_expr_p=*/true,
28716 asm_specification, attributes);
28718 if (parser->fully_implicit_function_template_p)
28720 if (friend_p)
28721 finish_fully_implicit_template (parser, 0);
28722 else
28723 decl = finish_fully_implicit_template (parser, decl);
28727 cp_finalize_omp_declare_simd (parser, decl);
28728 cp_finalize_oacc_routine (parser, decl, false);
28730 /* Reset PREFIX_ATTRIBUTES. */
28731 if (attributes != error_mark_node)
28733 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28734 attributes = TREE_CHAIN (attributes);
28735 if (attributes)
28736 TREE_CHAIN (attributes) = NULL_TREE;
28739 /* If there is any qualification still in effect, clear it
28740 now; we will be starting fresh with the next declarator. */
28741 parser->scope = NULL_TREE;
28742 parser->qualifying_scope = NULL_TREE;
28743 parser->object_scope = NULL_TREE;
28744 /* If it's a `,', then there are more declarators. */
28745 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28747 cp_lexer_consume_token (parser->lexer);
28748 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28750 cp_token *token = cp_lexer_previous_token (parser->lexer);
28751 gcc_rich_location richloc (token->location);
28752 richloc.add_fixit_remove ();
28753 error_at (&richloc, "stray %<,%> at end of "
28754 "member declaration");
28757 /* If the next token isn't a `;', then we have a parse error. */
28758 else if (cp_lexer_next_token_is_not (parser->lexer,
28759 CPP_SEMICOLON))
28761 /* The next token might be a ways away from where the
28762 actual semicolon is missing. Find the previous token
28763 and use that for our error position. */
28764 cp_token *token = cp_lexer_previous_token (parser->lexer);
28765 gcc_rich_location richloc (token->location);
28766 richloc.add_fixit_insert_after (";");
28767 error_at (&richloc, "expected %<;%> at end of "
28768 "member declaration");
28770 /* Assume that the user meant to provide a semicolon. If
28771 we were to cp_parser_skip_to_end_of_statement, we might
28772 skip to a semicolon inside a member function definition
28773 and issue nonsensical error messages. */
28774 assume_semicolon = true;
28777 if (decl)
28779 /* Add DECL to the list of members. */
28780 if (!friend_p
28781 /* Explicitly include, eg, NSDMIs, for better error
28782 recovery (c++/58650). */
28783 || !DECL_DECLARES_FUNCTION_P (decl))
28784 finish_member_declaration (decl);
28786 if (DECL_DECLARES_FUNCTION_P (decl))
28787 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28788 else if (TREE_CODE (decl) == FIELD_DECL
28789 && DECL_INITIAL (decl))
28790 /* Add DECL to the queue of NSDMI to be parsed later. */
28791 vec_safe_push (unparsed_nsdmis, decl);
28794 if (assume_semicolon)
28795 goto out;
28799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28800 out:
28801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28802 cp_finalize_omp_declare_simd (parser, &odsd);
28805 /* Parse a pure-specifier.
28807 pure-specifier:
28810 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28811 Otherwise, ERROR_MARK_NODE is returned. */
28813 static tree
28814 cp_parser_pure_specifier (cp_parser* parser)
28816 cp_token *token;
28818 /* Look for the `=' token. */
28819 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28820 return error_mark_node;
28821 /* Look for the `0' token. */
28822 token = cp_lexer_peek_token (parser->lexer);
28824 if (token->type == CPP_EOF
28825 || token->type == CPP_PRAGMA_EOL)
28826 return error_mark_node;
28828 cp_lexer_consume_token (parser->lexer);
28830 /* Accept = default or = delete in c++0x mode. */
28831 if (token->keyword == RID_DEFAULT
28832 || token->keyword == RID_DELETE)
28834 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28837 if (cxx_dialect >= cxx11 && cxx_dialect < cxx26)
28838 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
28839 OPT_Wc__26_extensions,
28840 "%<delete%> reason only available with "
28841 "%<-std=c++2c%> or %<-std=gnu++2c%>");
28843 /* Consume the `('. */
28844 matching_parens parens;
28845 parens.consume_open (parser);
28846 tree reason = cp_parser_unevaluated_string_literal (parser);
28847 /* Consume the `)'. */
28848 parens.require_close (parser);
28849 if (TREE_CODE (reason) == STRING_CST)
28851 TREE_TYPE (reason) = token->u.value;
28852 return reason;
28856 return token->u.value;
28859 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28860 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28862 cp_parser_error (parser,
28863 "invalid pure specifier (only %<= 0%> is allowed)");
28864 cp_parser_skip_to_end_of_statement (parser);
28865 return error_mark_node;
28867 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28869 error_at (token->location, "templates may not be %<virtual%>");
28870 return error_mark_node;
28873 return integer_zero_node;
28876 /* Parse a constant-initializer.
28878 constant-initializer:
28879 = constant-expression
28881 Returns a representation of the constant-expression. */
28883 static tree
28884 cp_parser_constant_initializer (cp_parser* parser)
28886 /* Look for the `=' token. */
28887 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28888 return error_mark_node;
28890 /* It is invalid to write:
28892 struct S { static const int i = { 7 }; };
28895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28897 cp_parser_error (parser,
28898 "a brace-enclosed initializer is not allowed here");
28899 /* Consume the opening brace. */
28900 matching_braces braces;
28901 braces.consume_open (parser);
28902 /* Skip the initializer. */
28903 cp_parser_skip_to_closing_brace (parser);
28904 /* Look for the trailing `}'. */
28905 braces.require_close (parser);
28907 return error_mark_node;
28910 return cp_parser_constant_expression (parser);
28913 /* Derived classes [gram.class.derived] */
28915 /* Parse a base-clause.
28917 base-clause:
28918 : base-specifier-list
28920 base-specifier-list:
28921 base-specifier ... [opt]
28922 base-specifier-list , base-specifier ... [opt]
28924 Returns a TREE_LIST representing the base-classes, in the order in
28925 which they were declared. The representation of each node is as
28926 described by cp_parser_base_specifier.
28928 In the case that no bases are specified, this function will return
28929 NULL_TREE, not ERROR_MARK_NODE. */
28931 static tree
28932 cp_parser_base_clause (cp_parser* parser)
28934 tree bases = NULL_TREE;
28936 /* Look for the `:' that begins the list. */
28937 cp_parser_require (parser, CPP_COLON, RT_COLON);
28939 /* Scan the base-specifier-list. */
28940 while (true)
28942 cp_token *token;
28943 tree base;
28944 bool pack_expansion_p = false;
28946 /* Look for the base-specifier. */
28947 base = cp_parser_base_specifier (parser);
28948 /* Look for the (optional) ellipsis. */
28949 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28951 /* Consume the `...'. */
28952 cp_lexer_consume_token (parser->lexer);
28954 pack_expansion_p = true;
28957 /* Add BASE to the front of the list. */
28958 if (base && base != error_mark_node)
28960 if (pack_expansion_p)
28961 /* Make this a pack expansion type. */
28962 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28964 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28966 TREE_CHAIN (base) = bases;
28967 bases = base;
28970 /* Peek at the next token. */
28971 token = cp_lexer_peek_token (parser->lexer);
28972 /* If it's not a comma, then the list is complete. */
28973 if (token->type != CPP_COMMA)
28974 break;
28975 /* Consume the `,'. */
28976 cp_lexer_consume_token (parser->lexer);
28979 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28980 base class had a qualified name. However, the next name that
28981 appears is certainly not qualified. */
28982 parser->scope = NULL_TREE;
28983 parser->qualifying_scope = NULL_TREE;
28984 parser->object_scope = NULL_TREE;
28986 return nreverse (bases);
28989 /* Parse a base-specifier.
28991 base-specifier:
28992 :: [opt] nested-name-specifier [opt] class-name
28993 virtual access-specifier [opt] :: [opt] nested-name-specifier
28994 [opt] class-name
28995 access-specifier virtual [opt] :: [opt] nested-name-specifier
28996 [opt] class-name
28998 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28999 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
29000 indicate the specifiers provided. The TREE_VALUE will be a TYPE
29001 (or the ERROR_MARK_NODE) indicating the type that was specified. */
29003 static tree
29004 cp_parser_base_specifier (cp_parser* parser)
29006 cp_token *token;
29007 bool done = false;
29008 bool virtual_p = false;
29009 bool duplicate_virtual_error_issued_p = false;
29010 bool duplicate_access_error_issued_p = false;
29011 bool class_scope_p, template_p;
29012 tree access = access_default_node;
29013 tree type;
29015 /* Process the optional `virtual' and `access-specifier'. */
29016 while (!done)
29018 /* Peek at the next token. */
29019 token = cp_lexer_peek_token (parser->lexer);
29020 /* Process `virtual'. */
29021 switch (token->keyword)
29023 case RID_VIRTUAL:
29024 /* If `virtual' appears more than once, issue an error. */
29025 if (virtual_p && !duplicate_virtual_error_issued_p)
29027 cp_parser_error (parser,
29028 "%<virtual%> specified more than once in base-specifier");
29029 duplicate_virtual_error_issued_p = true;
29032 virtual_p = true;
29034 /* Consume the `virtual' token. */
29035 cp_lexer_consume_token (parser->lexer);
29037 break;
29039 case RID_PUBLIC:
29040 case RID_PROTECTED:
29041 case RID_PRIVATE:
29042 /* If more than one access specifier appears, issue an
29043 error. */
29044 if (access != access_default_node
29045 && !duplicate_access_error_issued_p)
29047 cp_parser_error (parser,
29048 "more than one access specifier in base-specifier");
29049 duplicate_access_error_issued_p = true;
29052 access = ridpointers[(int) token->keyword];
29054 /* Consume the access-specifier. */
29055 cp_lexer_consume_token (parser->lexer);
29057 break;
29059 default:
29060 done = true;
29061 break;
29064 /* It is not uncommon to see programs mechanically, erroneously, use
29065 the 'typename' keyword to denote (dependent) qualified types
29066 as base classes. */
29067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
29069 token = cp_lexer_peek_token (parser->lexer);
29070 if (!processing_template_decl)
29071 error_at (token->location,
29072 "keyword %<typename%> not allowed outside of templates");
29073 else
29074 error_at (token->location,
29075 "keyword %<typename%> not allowed in this context "
29076 "(the base class is implicitly a type)");
29077 cp_lexer_consume_token (parser->lexer);
29080 /* Look for the optional `::' operator. */
29081 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
29082 /* Look for the nested-name-specifier. The simplest way to
29083 implement:
29085 [temp.res]
29087 The keyword `typename' is not permitted in a base-specifier or
29088 mem-initializer; in these contexts a qualified name that
29089 depends on a template-parameter is implicitly assumed to be a
29090 type name.
29092 is to pretend that we have seen the `typename' keyword at this
29093 point. */
29094 cp_parser_nested_name_specifier_opt (parser,
29095 /*typename_keyword_p=*/true,
29096 /*check_dependency_p=*/true,
29097 /*type_p=*/true,
29098 /*is_declaration=*/true);
29099 /* If the base class is given by a qualified name, assume that names
29100 we see are type names or templates, as appropriate. */
29101 class_scope_p = (parser->scope && TYPE_P (parser->scope));
29102 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
29104 if (!parser->scope
29105 && cp_lexer_next_token_is_decltype (parser->lexer))
29106 /* DR 950 allows decltype as a base-specifier. */
29107 type = cp_parser_decltype (parser);
29108 else
29110 /* Otherwise, look for the class-name. */
29111 type = cp_parser_class_name (parser,
29112 class_scope_p,
29113 template_p,
29114 typename_type,
29115 /*check_dependency_p=*/true,
29116 /*class_head_p=*/false,
29117 /*is_declaration=*/true);
29118 type = TREE_TYPE (type);
29121 if (type == error_mark_node)
29122 return error_mark_node;
29124 return finish_base_specifier (type, access, virtual_p);
29127 /* Exception handling [gram.exception] */
29129 /* Save the tokens that make up the noexcept-specifier for a member-function.
29130 Returns a DEFERRED_PARSE. */
29132 static tree
29133 cp_parser_save_noexcept (cp_parser *parser)
29135 cp_token *first = parser->lexer->next_token;
29136 /* We want everything up to, including, the final ')'. */
29137 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
29138 cp_token *last = parser->lexer->next_token;
29140 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
29141 to carry the information we will need. */
29142 tree expr = make_node (DEFERRED_PARSE);
29143 /* Save away the noexcept-specifier; we will process it when the
29144 class is complete. */
29145 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
29146 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
29147 expr = build_tree_list (expr, NULL_TREE);
29148 return expr;
29151 /* Used for late processing of noexcept-specifiers of member-functions.
29152 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
29153 we saved for later; parse it now. DECL is the declaration of the
29154 member function. */
29156 static tree
29157 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
29159 /* Make sure we've gotten something that hasn't been parsed yet. */
29160 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
29162 push_unparsed_function_queues (parser);
29164 /* Push the saved tokens for the noexcept-specifier onto the parser's
29165 lexer stack. */
29166 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
29167 cp_parser_push_lexer_for_tokens (parser, tokens);
29169 /* Parse the cached noexcept-specifier. */
29170 tree parsed_arg
29171 = cp_parser_noexcept_specification_opt (parser,
29172 CP_PARSER_FLAGS_NONE,
29173 /*require_constexpr=*/true,
29174 /*consumed_expr=*/NULL,
29175 /*return_cond=*/false);
29177 /* Revert to the main lexer. */
29178 cp_parser_pop_lexer (parser);
29180 /* Restore the queue. */
29181 pop_unparsed_function_queues (parser);
29183 /* And we're done. */
29184 return parsed_arg;
29187 /* Perform late checking of overriding function with respect to their
29188 noexcept-specifiers. FNDECL is the member function that potentially
29189 overrides some virtual function with the same signature. */
29191 static void
29192 noexcept_override_late_checks (tree fndecl)
29194 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
29195 tree base_binfo;
29197 if (DECL_STATIC_FUNCTION_P (fndecl))
29198 return;
29200 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
29202 tree basetype = BINFO_TYPE (base_binfo);
29204 if (!TYPE_POLYMORPHIC_P (basetype))
29205 continue;
29207 tree fn = look_for_overrides_here (basetype, fndecl);
29208 if (fn)
29209 maybe_check_overriding_exception_spec (fndecl, fn);
29213 /* Parse an (optional) noexcept-specification.
29215 noexcept-specification:
29216 noexcept ( constant-expression ) [opt]
29218 If no noexcept-specification is present, returns NULL_TREE.
29219 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
29220 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
29221 there are no parentheses. CONSUMED_EXPR will be set accordingly.
29222 Otherwise, returns a noexcept specification unless RETURN_COND is true,
29223 in which case a boolean condition is returned instead. The parser flags
29224 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
29225 the (member) function is `const'. */
29227 static tree
29228 cp_parser_noexcept_specification_opt (cp_parser* parser,
29229 cp_parser_flags flags,
29230 bool require_constexpr,
29231 bool* consumed_expr,
29232 bool return_cond)
29234 cp_token *token;
29235 const char *saved_message;
29237 /* Peek at the next token. */
29238 token = cp_lexer_peek_token (parser->lexer);
29240 /* Is it a noexcept-specification? */
29241 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
29243 tree expr;
29245 /* [class.mem]/6 says that a noexcept-specifer (within the
29246 member-specification of the class) is a complete-class context of
29247 a class. So, if the noexcept-specifier has the optional expression,
29248 just save the tokens, and reparse this after we're done with the
29249 class. */
29251 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
29252 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
29253 /* No need to delay parsing for a number literal or true/false. */
29254 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
29255 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29256 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
29257 && at_class_scope_p ()
29258 && TYPE_BEING_DEFINED (current_class_type)
29259 && !LAMBDA_TYPE_P (current_class_type))
29260 return cp_parser_save_noexcept (parser);
29262 cp_lexer_consume_token (parser->lexer);
29264 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29266 matching_parens parens;
29267 parens.consume_open (parser);
29269 if (require_constexpr)
29271 /* Types may not be defined in an exception-specification. */
29272 saved_message = parser->type_definition_forbidden_message;
29273 parser->type_definition_forbidden_message
29274 = G_("types may not be defined in an exception-specification");
29276 bool non_constant_p;
29277 expr
29278 = cp_parser_constant_expression (parser,
29279 /*allow_non_constant=*/true,
29280 &non_constant_p);
29281 if (non_constant_p
29282 && !require_potential_rvalue_constant_expression (expr))
29284 expr = NULL_TREE;
29285 return_cond = true;
29288 /* Restore the saved message. */
29289 parser->type_definition_forbidden_message = saved_message;
29291 else
29293 expr = cp_parser_expression (parser);
29294 *consumed_expr = true;
29297 parens.require_close (parser);
29299 else
29301 expr = boolean_true_node;
29302 if (!require_constexpr)
29303 *consumed_expr = false;
29306 /* We cannot build a noexcept-spec right away because this will check
29307 that expr is a constexpr. */
29308 if (!return_cond)
29309 return build_noexcept_spec (expr, tf_warning_or_error);
29310 else
29311 return expr;
29313 else
29314 return NULL_TREE;
29317 /* Parse an (optional) exception-specification.
29319 exception-specification:
29320 throw ( type-id-list [opt] )
29322 Returns a TREE_LIST representing the exception-specification. The
29323 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
29324 control parsing. QUALS are qualifiers indicating whether the (member)
29325 function is `const'. */
29327 static tree
29328 cp_parser_exception_specification_opt (cp_parser* parser,
29329 cp_parser_flags flags)
29331 cp_token *token;
29332 tree type_id_list;
29333 const char *saved_message;
29335 /* Peek at the next token. */
29336 token = cp_lexer_peek_token (parser->lexer);
29338 /* Is it a noexcept-specification? */
29339 type_id_list
29340 = cp_parser_noexcept_specification_opt (parser, flags,
29341 /*require_constexpr=*/true,
29342 /*consumed_expr=*/NULL,
29343 /*return_cond=*/false);
29344 if (type_id_list != NULL_TREE)
29345 return type_id_list;
29347 /* If it's not `throw', then there's no exception-specification. */
29348 if (!cp_parser_is_keyword (token, RID_THROW))
29349 return NULL_TREE;
29351 location_t loc = token->location;
29353 /* Consume the `throw'. */
29354 cp_lexer_consume_token (parser->lexer);
29356 /* Look for the `('. */
29357 matching_parens parens;
29358 parens.require_open (parser);
29360 /* Peek at the next token. */
29361 token = cp_lexer_peek_token (parser->lexer);
29362 /* If it's not a `)', then there is a type-id-list. */
29363 if (token->type != CPP_CLOSE_PAREN)
29365 /* Types may not be defined in an exception-specification. */
29366 saved_message = parser->type_definition_forbidden_message;
29367 parser->type_definition_forbidden_message
29368 = G_("types may not be defined in an exception-specification");
29369 /* Parse the type-id-list. */
29370 type_id_list = cp_parser_type_id_list (parser);
29371 /* Restore the saved message. */
29372 parser->type_definition_forbidden_message = saved_message;
29374 if (cxx_dialect >= cxx17)
29376 error_at (loc, "ISO C++17 does not allow dynamic exception "
29377 "specifications");
29378 type_id_list = NULL_TREE;
29380 else if (cxx_dialect >= cxx11)
29381 warning_at (loc, OPT_Wdeprecated,
29382 "dynamic exception specifications are deprecated in "
29383 "C++11");
29385 /* In C++17, throw() is equivalent to noexcept (true). throw()
29386 is deprecated in C++11 and above as well, but is still widely used,
29387 so don't warn about it yet. */
29388 else if (cxx_dialect >= cxx17)
29389 type_id_list = noexcept_true_spec;
29390 else
29391 type_id_list = empty_except_spec;
29393 /* Look for the `)'. */
29394 parens.require_close (parser);
29396 return type_id_list;
29399 /* Parse an (optional) type-id-list.
29401 type-id-list:
29402 type-id ... [opt]
29403 type-id-list , type-id ... [opt]
29405 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
29406 in the order that the types were presented. */
29408 static tree
29409 cp_parser_type_id_list (cp_parser* parser)
29411 tree types = NULL_TREE;
29413 while (true)
29415 cp_token *token;
29416 tree type;
29418 token = cp_lexer_peek_token (parser->lexer);
29420 /* Get the next type-id. */
29421 type = cp_parser_type_id (parser);
29422 /* Check for invalid 'auto'. */
29423 if (flag_concepts && type_uses_auto (type))
29425 error_at (token->location,
29426 "invalid use of %<auto%> in exception-specification");
29427 type = error_mark_node;
29429 /* Parse the optional ellipsis. */
29430 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29432 /* Consume the `...'. */
29433 cp_lexer_consume_token (parser->lexer);
29435 /* Turn the type into a pack expansion expression. */
29436 type = make_pack_expansion (type);
29438 /* Add it to the list. */
29439 types = add_exception_specifier (types, type, /*complain=*/1);
29440 /* Peek at the next token. */
29441 token = cp_lexer_peek_token (parser->lexer);
29442 /* If it is not a `,', we are done. */
29443 if (token->type != CPP_COMMA)
29444 break;
29445 /* Consume the `,'. */
29446 cp_lexer_consume_token (parser->lexer);
29449 return nreverse (types);
29452 /* Parse a try-block.
29454 try-block:
29455 try compound-statement handler-seq */
29457 static tree
29458 cp_parser_try_block (cp_parser* parser)
29460 tree try_block;
29462 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
29463 if (parser->in_function_body
29464 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
29465 && cxx_dialect < cxx20)
29466 pedwarn (input_location, OPT_Wc__20_extensions,
29467 "%<try%> in %<constexpr%> function only "
29468 "available with %<-std=c++20%> or %<-std=gnu++20%>");
29470 try_block = begin_try_block ();
29471 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
29472 finish_try_block (try_block);
29473 cp_parser_handler_seq (parser);
29474 finish_handler_sequence (try_block);
29476 return try_block;
29479 /* Parse a function-try-block.
29481 function-try-block:
29482 try ctor-initializer [opt] function-body handler-seq */
29484 static void
29485 cp_parser_function_try_block (cp_parser* parser)
29487 tree compound_stmt;
29488 tree try_block;
29490 /* Look for the `try' keyword. */
29491 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
29492 return;
29493 /* Let the rest of the front end know where we are. */
29494 try_block = begin_function_try_block (&compound_stmt);
29495 /* Parse the function-body. */
29496 cp_parser_ctor_initializer_opt_and_function_body
29497 (parser, /*in_function_try_block=*/true);
29498 /* We're done with the `try' part. */
29499 finish_function_try_block (try_block);
29500 /* Parse the handlers. */
29501 cp_parser_handler_seq (parser);
29502 /* We're done with the handlers. */
29503 finish_function_handler_sequence (try_block, compound_stmt);
29506 /* Parse a handler-seq.
29508 handler-seq:
29509 handler handler-seq [opt] */
29511 static void
29512 cp_parser_handler_seq (cp_parser* parser)
29514 while (true)
29516 cp_token *token;
29518 /* Parse the handler. */
29519 cp_parser_handler (parser);
29520 /* Peek at the next token. */
29521 token = cp_lexer_peek_token (parser->lexer);
29522 /* If it's not `catch' then there are no more handlers. */
29523 if (!cp_parser_is_keyword (token, RID_CATCH))
29524 break;
29528 /* Parse a handler.
29530 handler:
29531 catch ( exception-declaration ) compound-statement */
29533 static void
29534 cp_parser_handler (cp_parser* parser)
29536 tree handler;
29537 tree declaration;
29539 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
29540 handler = begin_handler ();
29541 matching_parens parens;
29542 parens.require_open (parser);
29543 declaration = cp_parser_exception_declaration (parser);
29544 finish_handler_parms (declaration, handler);
29545 parens.require_close (parser);
29546 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29547 finish_handler (handler);
29550 /* Parse an exception-declaration.
29552 exception-declaration:
29553 type-specifier-seq declarator
29554 type-specifier-seq abstract-declarator
29555 type-specifier-seq
29558 Returns a VAR_DECL for the declaration, or NULL_TREE if the
29559 ellipsis variant is used. */
29561 static tree
29562 cp_parser_exception_declaration (cp_parser* parser)
29564 cp_decl_specifier_seq type_specifiers;
29565 cp_declarator *declarator;
29566 const char *saved_message;
29568 /* If it's an ellipsis, it's easy to handle. */
29569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29571 /* Consume the `...' token. */
29572 cp_lexer_consume_token (parser->lexer);
29573 return NULL_TREE;
29576 /* Types may not be defined in exception-declarations. */
29577 saved_message = parser->type_definition_forbidden_message;
29578 parser->type_definition_forbidden_message
29579 = G_("types may not be defined in exception-declarations");
29581 /* Parse the type-specifier-seq. */
29582 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
29583 /*is_declaration=*/true,
29584 /*is_trailing_return=*/false,
29585 &type_specifiers);
29586 /* If it's a `)', then there is no declarator. */
29587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29588 declarator = NULL;
29589 else
29590 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
29591 CP_PARSER_FLAGS_NONE,
29592 /*ctor_dtor_or_conv_p=*/NULL,
29593 /*parenthesized_p=*/NULL,
29594 /*member_p=*/false,
29595 /*friend_p=*/false,
29596 /*static_p=*/false);
29598 /* Restore the saved message. */
29599 parser->type_definition_forbidden_message = saved_message;
29601 if (!type_specifiers.any_specifiers_p)
29602 return error_mark_node;
29604 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
29607 /* Parse a throw-expression.
29609 throw-expression:
29610 throw assignment-expression [opt]
29612 Returns a THROW_EXPR representing the throw-expression. */
29614 static tree
29615 cp_parser_throw_expression (cp_parser* parser)
29617 tree expression;
29618 cp_token* token;
29619 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29621 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
29622 token = cp_lexer_peek_token (parser->lexer);
29623 /* Figure out whether or not there is an assignment-expression
29624 following the "throw" keyword. */
29625 if (token->type == CPP_COMMA
29626 || token->type == CPP_SEMICOLON
29627 || token->type == CPP_CLOSE_PAREN
29628 || token->type == CPP_CLOSE_SQUARE
29629 || token->type == CPP_CLOSE_BRACE
29630 || token->type == CPP_COLON)
29631 expression = NULL_TREE;
29632 else
29633 expression = cp_parser_assignment_expression (parser);
29635 /* Construct a location e.g.:
29636 throw x
29637 ^~~~~~~
29638 with caret == start at the start of the "throw" token, and
29639 the end at the end of the final token we consumed. */
29640 location_t combined_loc = make_location (start_loc, start_loc,
29641 parser->lexer);
29642 expression = build_throw (combined_loc, expression, tf_warning_or_error);
29644 return expression;
29647 /* Parse a yield-expression.
29649 yield-expression:
29650 co_yield assignment-expression
29651 co_yield braced-init-list
29653 Returns a CO_YIELD_EXPR representing the yield-expression. */
29655 static tree
29656 cp_parser_yield_expression (cp_parser* parser)
29658 tree expr;
29660 cp_token *token = cp_lexer_peek_token (parser->lexer);
29661 location_t kw_loc = token->location; /* Save for later. */
29663 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
29665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29667 cp_lexer_set_source_position (parser->lexer);
29668 /* ??? : probably a moot point? */
29669 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29670 expr = cp_parser_braced_list (parser);
29672 else
29673 expr = cp_parser_assignment_expression (parser);
29675 if (expr == error_mark_node)
29676 return expr;
29678 return finish_co_yield_expr (kw_loc, expr);
29681 /* GNU Extensions */
29683 /* Parse an (optional) asm-specification.
29685 asm-specification:
29686 asm ( asm-string-expr )
29688 If the asm-specification is present, returns a STRING_CST
29689 corresponding to the string-literal. Otherwise, returns
29690 NULL_TREE. */
29692 static tree
29693 cp_parser_asm_specification_opt (cp_parser* parser)
29695 /* Peek at the next token. */
29696 cp_token *token = cp_lexer_peek_token (parser->lexer);
29697 /* If the next token isn't the `asm' keyword, then there's no
29698 asm-specification. */
29699 if (!cp_parser_is_keyword (token, RID_ASM))
29700 return NULL_TREE;
29702 /* Consume the `asm' token. */
29703 cp_lexer_consume_token (parser->lexer);
29704 /* Look for the `('. */
29705 matching_parens parens;
29706 parens.require_open (parser);
29708 /* Look for the string-literal. */
29709 tree asm_specification = cp_parser_asm_string_expression (parser);
29711 /* Look for the `)'. */
29712 parens.require_close (parser);
29714 return asm_specification;
29717 /* Parse an asm-operand-list.
29719 asm-operand-list:
29720 asm-operand
29721 asm-operand-list , asm-operand
29723 asm-operand:
29724 asm-string-expr ( expression )
29725 [ asm-string-expr ] asm-string-expr ( expression )
29727 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29728 each node is the expression. The TREE_PURPOSE is itself a
29729 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29730 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29731 is a STRING_CST for the string literal before the parenthesis. Returns
29732 ERROR_MARK_NODE if any of the operands are invalid. */
29734 static tree
29735 cp_parser_asm_operand_list (cp_parser* parser)
29737 tree asm_operands = NULL_TREE;
29738 bool invalid_operands = false;
29740 while (true)
29742 tree name;
29744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29746 /* Consume the `[' token. */
29747 cp_lexer_consume_token (parser->lexer);
29748 /* Read the operand name. */
29749 name = cp_parser_identifier (parser);
29750 if (name != error_mark_node)
29751 name = build_string (IDENTIFIER_LENGTH (name),
29752 IDENTIFIER_POINTER (name));
29753 /* Look for the closing `]'. */
29754 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29756 else
29757 name = NULL_TREE;
29758 /* Look for the string. */
29759 tree string_literal = cp_parser_asm_string_expression (parser);
29761 /* Look for the `('. */
29762 matching_parens parens;
29763 parens.require_open (parser);
29764 /* Parse the expression. */
29765 tree expression = cp_parser_expression (parser);
29766 /* Look for the `)'. */
29767 parens.require_close (parser);
29769 if (name == error_mark_node
29770 || string_literal == error_mark_node
29771 || expression == error_mark_node)
29772 invalid_operands = true;
29774 /* Add this operand to the list. */
29775 asm_operands = tree_cons (build_tree_list (name, string_literal),
29776 expression,
29777 asm_operands);
29778 /* If the next token is not a `,', there are no more
29779 operands. */
29780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29781 break;
29782 /* Consume the `,'. */
29783 cp_lexer_consume_token (parser->lexer);
29786 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29789 /* Parse an asm-clobber-list.
29791 asm-clobber-list:
29792 const-expression
29793 asm-clobber-list , const-expression
29795 Returns a TREE_LIST, indicating the clobbers in the order that they
29796 appeared. The TREE_VALUE of each node is a STRING_CST. */
29798 static tree
29799 cp_parser_asm_clobber_list (cp_parser* parser)
29801 tree clobbers = NULL_TREE;
29803 while (true)
29805 /* Look for the string literal. */
29806 tree string_literal = cp_parser_asm_string_expression (parser);
29807 /* Add it to the list. */
29808 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29809 /* If the next token is not a `,', then the list is
29810 complete. */
29811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29812 break;
29813 /* Consume the `,' token. */
29814 cp_lexer_consume_token (parser->lexer);
29817 return clobbers;
29820 /* Parse an asm-label-list.
29822 asm-label-list:
29823 identifier
29824 asm-label-list , identifier
29826 Returns a TREE_LIST, indicating the labels in the order that they
29827 appeared. The TREE_VALUE of each node is a label. */
29829 static tree
29830 cp_parser_asm_label_list (cp_parser* parser)
29832 tree labels = NULL_TREE;
29834 while (true)
29836 tree identifier, label, name;
29838 /* Look for the identifier. */
29839 identifier = cp_parser_identifier (parser);
29840 if (!error_operand_p (identifier))
29842 label = lookup_label (identifier);
29843 if (TREE_CODE (label) == LABEL_DECL)
29845 TREE_USED (label) = 1;
29846 check_goto (label);
29847 name = build_string (IDENTIFIER_LENGTH (identifier),
29848 IDENTIFIER_POINTER (identifier));
29849 labels = tree_cons (name, label, labels);
29852 /* If the next token is not a `,', then the list is
29853 complete. */
29854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29855 break;
29856 /* Consume the `,' token. */
29857 cp_lexer_consume_token (parser->lexer);
29860 return nreverse (labels);
29863 /* Return TRUE iff the next tokens in the stream are possibly the
29864 beginning of a GNU extension attribute. */
29866 static bool
29867 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29869 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29872 /* Return TRUE iff the next tokens in the stream are possibly the
29873 beginning of a standard C++-11 attribute specifier. */
29875 static bool
29876 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29878 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29881 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29882 beginning of a standard C++-11 attribute specifier. */
29884 static bool
29885 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29887 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29889 return (cxx_dialect >= cxx11
29890 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29891 || (token->type == CPP_OPEN_SQUARE
29892 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29893 && token->type == CPP_OPEN_SQUARE)));
29896 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29897 beginning of a GNU extension attribute. */
29899 static bool
29900 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29902 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29904 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29907 /* Return true iff the next tokens can be the beginning of either a
29908 GNU attribute list, or a standard C++11 attribute sequence. */
29910 static bool
29911 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29913 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29914 || cp_next_tokens_can_be_std_attribute_p (parser));
29917 /* Return true iff the next Nth tokens can be the beginning of either
29918 a GNU attribute list, or a standard C++11 attribute sequence. */
29920 static bool
29921 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29923 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29924 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29927 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29928 of GNU attributes, or return NULL. */
29930 static tree
29931 cp_parser_attributes_opt (cp_parser *parser)
29933 tree attrs = NULL_TREE;
29934 while (true)
29936 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29937 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29938 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29939 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29940 else
29941 break;
29943 return attrs;
29946 /* Parse an (optional) series of attributes.
29948 attributes:
29949 attributes attribute
29951 attribute:
29952 __attribute__ (( attribute-list [opt] ))
29954 The return value is as for cp_parser_gnu_attribute_list. */
29956 static tree
29957 cp_parser_gnu_attributes_opt (cp_parser* parser)
29959 tree attributes = NULL_TREE;
29961 auto cleanup = make_temp_override
29962 (parser->auto_is_implicit_function_template_parm_p, false);
29964 while (true)
29966 cp_token *token;
29967 tree attribute_list;
29968 bool ok = true;
29970 /* Peek at the next token. */
29971 token = cp_lexer_peek_token (parser->lexer);
29972 /* If it's not `__attribute__', then we're done. */
29973 if (token->keyword != RID_ATTRIBUTE)
29974 break;
29976 /* Consume the `__attribute__' keyword. */
29977 cp_lexer_consume_token (parser->lexer);
29978 /* Look for the two `(' tokens. */
29979 matching_parens outer_parens;
29980 if (!outer_parens.require_open (parser))
29981 ok = false;
29982 matching_parens inner_parens;
29983 if (!inner_parens.require_open (parser))
29984 ok = false;
29986 /* Peek at the next token. */
29987 token = cp_lexer_peek_token (parser->lexer);
29988 if (token->type != CPP_CLOSE_PAREN)
29989 /* Parse the attribute-list. */
29990 attribute_list = cp_parser_gnu_attribute_list (parser);
29991 else
29992 /* If the next token is a `)', then there is no attribute
29993 list. */
29994 attribute_list = NULL;
29996 /* Look for the two `)' tokens. */
29997 if (!inner_parens.require_close (parser))
29998 ok = false;
29999 if (!outer_parens.require_close (parser))
30000 ok = false;
30001 if (!ok)
30002 cp_parser_skip_to_end_of_statement (parser);
30004 /* Add these new attributes to the list. */
30005 attributes = attr_chainon (attributes, attribute_list);
30008 return attributes;
30011 /* Parse a GNU attribute-list.
30013 attribute-list:
30014 attribute
30015 attribute-list , attribute
30017 attribute:
30018 identifier
30019 identifier ( identifier )
30020 identifier ( identifier , expression-list )
30021 identifier ( expression-list )
30023 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
30024 to an attribute. The TREE_PURPOSE of each node is the identifier
30025 indicating which attribute is in use. The TREE_VALUE represents
30026 the arguments, if any. */
30028 static tree
30029 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
30031 tree attribute_list = NULL_TREE;
30032 bool save_translate_strings_p = parser->translate_strings_p;
30034 /* Don't create wrapper nodes within attributes: the
30035 handlers don't know how to handle them. */
30036 auto_suppress_location_wrappers sentinel;
30038 parser->translate_strings_p = false;
30039 while (true)
30041 cp_token *token;
30042 tree identifier;
30043 tree attribute;
30045 /* Look for the identifier. We also allow keywords here; for
30046 example `__attribute__ ((const))' is legal. */
30047 token = cp_lexer_peek_token (parser->lexer);
30048 if (token->type == CPP_NAME
30049 || token->type == CPP_KEYWORD)
30051 tree arguments = NULL_TREE;
30053 /* Consume the token, but save it since we need it for the
30054 SIMD enabled function parsing. */
30055 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
30057 /* Save away the identifier that indicates which attribute
30058 this is. */
30059 identifier = (token->type == CPP_KEYWORD)
30060 /* For keywords, use the canonical spelling, not the
30061 parsed identifier. */
30062 ? ridpointers[(int) token->keyword]
30063 : id_token->u.value;
30065 identifier = canonicalize_attr_name (identifier);
30066 attribute = build_tree_list (identifier, NULL_TREE);
30068 /* Peek at the next token. */
30069 token = cp_lexer_peek_token (parser->lexer);
30070 /* If it's an `(', then parse the attribute arguments. */
30071 if (token->type == CPP_OPEN_PAREN)
30073 vec<tree, va_gc> *vec;
30074 int attr_flag = (attribute_takes_identifier_p (identifier)
30075 ? id_attr : normal_attr);
30076 if (is_attribute_p ("assume", identifier))
30077 attr_flag = assume_attr;
30078 vec = cp_parser_parenthesized_expression_list
30079 (parser, attr_flag, /*cast_p=*/false,
30080 /*allow_expansion_p=*/false,
30081 /*non_constant_p=*/NULL);
30082 if (vec == NULL)
30083 arguments = error_mark_node;
30084 else
30086 arguments = build_tree_list_vec (vec);
30087 release_tree_vector (vec);
30089 /* Save the arguments away. */
30090 TREE_VALUE (attribute) = arguments;
30093 if (arguments != error_mark_node)
30095 /* Add this attribute to the list. */
30096 TREE_CHAIN (attribute) = attribute_list;
30097 attribute_list = attribute;
30100 token = cp_lexer_peek_token (parser->lexer);
30102 /* Unless EXACTLY_ONE is set look for more attributes.
30103 If the next token isn't a `,', we're done. */
30104 if (exactly_one || token->type != CPP_COMMA)
30105 break;
30107 /* Consume the comma and keep going. */
30108 cp_lexer_consume_token (parser->lexer);
30110 parser->translate_strings_p = save_translate_strings_p;
30112 /* We built up the list in reverse order. */
30113 return nreverse (attribute_list);
30116 /* Parse arguments of omp::directive attribute.
30118 ( directive-name ,[opt] clause-list[opt] )
30120 For directive just remember the first/last tokens for subsequent
30121 parsing. */
30123 static void
30124 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
30126 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
30127 if (first->type == CPP_CLOSE_PAREN)
30129 cp_lexer_consume_token (parser->lexer);
30130 error_at (first->location, "expected OpenMP directive name");
30131 cp_lexer_consume_token (parser->lexer);
30132 TREE_VALUE (attribute) = NULL_TREE;
30133 return;
30135 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
30136 if (n == 1)
30138 cp_lexer_consume_token (parser->lexer);
30139 error_at (first->location, "expected attribute argument as balanced "
30140 "token sequence");
30141 TREE_VALUE (attribute) = NULL_TREE;
30142 return;
30144 for (n = n - 2; n; --n)
30145 cp_lexer_consume_token (parser->lexer);
30146 cp_token *last = cp_lexer_peek_token (parser->lexer);
30147 cp_lexer_consume_token (parser->lexer);
30148 tree arg = make_node (DEFERRED_PARSE);
30149 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
30150 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
30151 if (decl_p)
30152 TREE_PUBLIC (arg) = 1;
30153 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
30156 /* Parse arguments of omp::sequence attribute.
30158 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
30160 static void
30161 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
30163 matching_parens parens;
30164 parens.consume_open (parser);
30167 cp_token *token = cp_lexer_peek_token (parser->lexer);
30168 if (token->type == CPP_NAME
30169 && token->u.value == omp_identifier
30170 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
30172 cp_lexer_consume_token (parser->lexer);
30173 cp_lexer_consume_token (parser->lexer);
30174 token = cp_lexer_peek_token (parser->lexer);
30176 bool directive = false;
30177 const char *p;
30178 if (token->type != CPP_NAME)
30179 p = "";
30180 else
30181 p = IDENTIFIER_POINTER (token->u.value);
30182 if (strcmp (p, "directive") == 0)
30183 directive = true;
30184 else if (strcmp (p, "sequence") != 0)
30186 error_at (token->location, "expected %<directive%> or %<sequence%>");
30187 cp_parser_skip_to_closing_parenthesis (parser,
30188 /*recovering=*/true,
30189 /*or_comma=*/true,
30190 /*consume_paren=*/false);
30191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30192 break;
30193 cp_lexer_consume_token (parser->lexer);
30195 cp_lexer_consume_token (parser->lexer);
30196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
30197 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
30198 UNKNOWN_LOCATION);
30199 else if (directive)
30200 cp_parser_omp_directive_args (parser, attribute, false);
30201 else
30202 cp_parser_omp_sequence_args (parser, attribute);
30203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30204 break;
30205 cp_lexer_consume_token (parser->lexer);
30207 while (1);
30208 if (!parens.require_close (parser))
30209 cp_parser_skip_to_closing_parenthesis (parser, true, false,
30210 /*consume_paren=*/true);
30213 /* Parse a standard C++11 attribute.
30215 The returned representation is a TREE_LIST which TREE_PURPOSE is
30216 the scoped name of the attribute, and the TREE_VALUE is its
30217 arguments list.
30219 Note that the scoped name of the attribute is itself a TREE_LIST
30220 which TREE_PURPOSE is the namespace of the attribute, and
30221 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
30222 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
30223 and which TREE_PURPOSE is directly the attribute name.
30225 Clients of the attribute code should use get_attribute_namespace
30226 and get_attribute_name to get the actual namespace and name of
30227 attributes, regardless of their being GNU or C++11 attributes.
30229 attribute:
30230 attribute-token attribute-argument-clause [opt]
30232 attribute-token:
30233 identifier
30234 attribute-scoped-token
30236 attribute-scoped-token:
30237 attribute-namespace :: identifier
30239 attribute-namespace:
30240 identifier
30242 attribute-argument-clause:
30243 ( balanced-token-seq )
30245 balanced-token-seq:
30246 balanced-token [opt]
30247 balanced-token-seq balanced-token
30249 balanced-token:
30250 ( balanced-token-seq )
30251 [ balanced-token-seq ]
30252 { balanced-token-seq }. */
30254 static tree
30255 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
30257 tree attribute, attr_id = NULL_TREE, arguments;
30258 cp_token *token;
30260 auto cleanup = make_temp_override
30261 (parser->auto_is_implicit_function_template_parm_p, false);
30263 /* First, parse name of the attribute, a.k.a attribute-token. */
30265 token = cp_lexer_peek_token (parser->lexer);
30266 if (token->type == CPP_NAME)
30267 attr_id = token->u.value;
30268 else if (token->type == CPP_KEYWORD)
30269 attr_id = ridpointers[(int) token->keyword];
30270 else if (token->flags & NAMED_OP)
30271 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
30273 if (attr_id == NULL_TREE)
30274 return NULL_TREE;
30276 cp_lexer_consume_token (parser->lexer);
30278 token = cp_lexer_peek_token (parser->lexer);
30279 if (token->type == CPP_SCOPE)
30281 /* We are seeing a scoped attribute token. */
30283 cp_lexer_consume_token (parser->lexer);
30284 if (attr_ns)
30285 error_at (token->location, "attribute using prefix used together "
30286 "with scoped attribute token");
30287 attr_ns = attr_id;
30289 token = cp_lexer_peek_token (parser->lexer);
30290 if (token->type == CPP_NAME)
30291 attr_id = token->u.value;
30292 else if (token->type == CPP_KEYWORD)
30293 attr_id = ridpointers[(int) token->keyword];
30294 else if (token->flags & NAMED_OP)
30295 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
30296 else
30298 error_at (token->location,
30299 "expected an identifier for the attribute name");
30300 return error_mark_node;
30302 cp_lexer_consume_token (parser->lexer);
30304 attr_ns = canonicalize_attr_name (attr_ns);
30305 attr_id = canonicalize_attr_name (attr_id);
30306 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
30307 NULL_TREE);
30308 token = cp_lexer_peek_token (parser->lexer);
30310 else if (attr_ns)
30312 attr_ns = canonicalize_attr_name (attr_ns);
30313 attr_id = canonicalize_attr_name (attr_id);
30314 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
30315 NULL_TREE);
30317 else
30319 attr_id = canonicalize_attr_name (attr_id);
30320 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
30321 NULL_TREE);
30323 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
30324 but no longer: we have to be able to tell [[noreturn]] and
30325 __attribute__((noreturn)) apart. */
30326 /* C++14 deprecated attribute is equivalent to GNU's. */
30327 if (is_attribute_p ("deprecated", attr_id))
30328 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30329 /* C++17 fallthrough attribute is equivalent to GNU's. */
30330 else if (is_attribute_p ("fallthrough", attr_id))
30331 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30332 /* C++23 assume attribute is equivalent to GNU's. */
30333 else if (is_attribute_p ("assume", attr_id))
30334 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30335 /* Transactional Memory TS optimize_for_synchronized attribute is
30336 equivalent to GNU transaction_callable. */
30337 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
30338 TREE_PURPOSE (attribute)
30339 = get_identifier ("transaction_callable");
30340 /* Transactional Memory attributes are GNU attributes. */
30341 else if (tm_attr_to_mask (attr_id))
30342 TREE_PURPOSE (attribute) = attr_id;
30345 /* Now parse the optional argument clause of the attribute. */
30347 if (token->type != CPP_OPEN_PAREN)
30349 if ((flag_openmp || flag_openmp_simd)
30350 && attr_ns == omp_identifier
30351 && (is_attribute_p ("directive", attr_id)
30352 || is_attribute_p ("sequence", attr_id)
30353 || is_attribute_p ("decl", attr_id)))
30355 error_at (token->location, "%<omp::%E%> attribute requires argument",
30356 attr_id);
30357 return NULL_TREE;
30359 return attribute;
30363 vec<tree, va_gc> *vec;
30364 int attr_flag = normal_attr;
30366 /* Maybe we don't expect to see any arguments for this attribute. */
30367 const attribute_spec *as
30368 = lookup_attribute_spec (TREE_PURPOSE (attribute));
30369 if (as && as->max_length == 0)
30371 error_at (token->location, "%qE attribute does not take any arguments",
30372 attr_id);
30373 cp_parser_skip_to_closing_parenthesis (parser,
30374 /*recovering=*/true,
30375 /*or_comma=*/false,
30376 /*consume_paren=*/true);
30377 return error_mark_node;
30380 if (is_attribute_p ("assume", attr_id)
30381 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
30382 /* The assume attribute needs special handling of the argument. */
30383 attr_flag = assume_attr;
30384 else if (attr_ns == gnu_identifier
30385 && attribute_takes_identifier_p (attr_id))
30386 /* A GNU attribute that takes an identifier in parameter. */
30387 attr_flag = id_attr;
30388 else if (attr_ns == NULL_TREE
30389 && cxx_dialect >= cxx26
30390 && (is_attribute_p ("deprecated", attr_id)
30391 || is_attribute_p ("nodiscard", attr_id)))
30392 attr_flag = uneval_string_attr;
30394 /* If this is a fake attribute created to handle -Wno-attributes,
30395 we must skip parsing the arguments. */
30396 if (as == NULL || attribute_ignored_p (as))
30398 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
30400 if (is_attribute_p ("directive", attr_id))
30402 cp_parser_omp_directive_args (parser, attribute, false);
30403 return attribute;
30405 else if (is_attribute_p ("decl", attr_id))
30407 TREE_VALUE (TREE_PURPOSE (attribute))
30408 = get_identifier ("directive");
30409 cp_parser_omp_directive_args (parser, attribute, true);
30410 return attribute;
30412 else if (is_attribute_p ("sequence", attr_id))
30414 TREE_VALUE (TREE_PURPOSE (attribute))
30415 = get_identifier ("directive");
30416 cp_parser_omp_sequence_args (parser, attribute);
30417 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
30418 return attribute;
30422 /* For unknown attributes, just skip balanced tokens instead of
30423 trying to parse the arguments. Set TREE_VALUE (attribute) to
30424 error_mark_node to distinguish skipped arguments from attributes
30425 with no arguments. */
30426 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
30427 cp_lexer_consume_token (parser->lexer);
30428 TREE_VALUE (attribute) = error_mark_node;
30429 return attribute;
30432 vec = cp_parser_parenthesized_expression_list
30433 (parser, attr_flag, /*cast_p=*/false,
30434 /*allow_expansion_p=*/true,
30435 /*non_constant_p=*/NULL);
30436 if (vec == NULL)
30437 arguments = error_mark_node;
30438 else
30440 if (vec->is_empty ())
30441 /* e.g. [[attr()]]. */
30442 error_at (token->location, "parentheses must be omitted if "
30443 "%qE attribute argument list is empty",
30444 attr_id);
30445 arguments = build_tree_list_vec (vec);
30446 release_tree_vector (vec);
30449 if (arguments == error_mark_node)
30450 attribute = error_mark_node;
30451 else
30452 TREE_VALUE (attribute) = arguments;
30455 return attribute;
30458 /* Warn if the attribute ATTRIBUTE appears more than once in the
30459 attribute-list ATTRIBUTES. This used to be enforced for certain
30460 attributes, but the restriction was removed in P2156.
30461 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
30462 found in ATTRIBUTES. */
30464 static bool
30465 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
30467 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
30468 "likely", "unlikely", "fallthrough",
30469 "no_unique_address", "carries_dependency" };
30470 if (attributes)
30471 for (const auto &a : alist)
30472 if (is_attribute_p (a, get_attribute_name (attribute))
30473 && is_attribute_namespace_p ("", attribute)
30474 && lookup_attribute ("", a, attributes))
30476 if (!from_macro_expansion_at (loc))
30477 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
30478 "multiple times", a);
30479 return false;
30481 return true;
30484 /* Parse a list of standard C++-11 attributes.
30486 attribute-list:
30487 attribute [opt]
30488 attribute-list , attribute[opt]
30489 attribute ...
30490 attribute-list , attribute ...
30493 static tree
30494 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
30496 tree attributes = NULL_TREE, attribute = NULL_TREE;
30497 cp_token *token = NULL;
30499 while (true)
30501 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30502 attribute = cp_parser_std_attribute (parser, attr_ns);
30503 if (attribute == error_mark_node)
30504 break;
30505 if (attribute != NULL_TREE)
30507 if (cp_parser_check_std_attribute (loc, attributes, attribute))
30509 TREE_CHAIN (attribute) = attributes;
30510 attributes = attribute;
30513 token = cp_lexer_peek_token (parser->lexer);
30514 if (token->type == CPP_ELLIPSIS)
30516 cp_lexer_consume_token (parser->lexer);
30517 if (attribute == NULL_TREE)
30518 error_at (token->location,
30519 "expected attribute before %<...%>");
30520 else if (TREE_VALUE (attribute) == NULL_TREE)
30522 error_at (token->location, "attribute with no arguments "
30523 "contains no parameter packs");
30524 return error_mark_node;
30526 else if (TREE_VALUE (attribute) != error_mark_node)
30528 tree pack = make_pack_expansion (TREE_VALUE (attribute));
30529 if (pack == error_mark_node)
30530 return error_mark_node;
30531 TREE_VALUE (attribute) = pack;
30533 token = cp_lexer_peek_token (parser->lexer);
30535 if (token->type != CPP_COMMA)
30536 break;
30537 cp_lexer_consume_token (parser->lexer);
30539 attributes = nreverse (attributes);
30540 return attributes;
30543 /* Optionally parse a C++20 contract role. A NULL return means that no
30544 contract role was specified.
30546 contract-role:
30547 % default
30548 % identifier
30550 If the identifier does not name a known contract role, it will
30551 be assumed to be default. Returns the identifier for the role
30552 token. */
30554 static tree
30555 cp_parser_contract_role (cp_parser *parser)
30557 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
30558 cp_lexer_consume_token (parser->lexer);
30560 cp_token *token = cp_lexer_peek_token (parser->lexer);
30561 tree role_id = NULL_TREE;
30562 if (token->type == CPP_NAME)
30563 role_id = token->u.value;
30564 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30565 role_id = get_identifier ("default");
30566 else
30568 error_at (token->location, "expected contract-role");
30569 return error_mark_node;
30571 cp_lexer_consume_token (parser->lexer);
30573 /* FIXME: Warn about invalid/unknown roles? */
30574 return role_id;
30577 /* Parse an optional contract mode.
30579 contract-mode:
30580 contract-semantic
30581 [contract-level] [contract-role]
30583 contract-semantic:
30584 check_never_continue
30585 check_maybe_continue
30586 check_always_continue
30588 contract-level:
30589 default
30590 audit
30591 axiom
30593 contract-role:
30594 default
30595 identifier
30597 This grammar is taken from P1332R0. During parsing, this sets options
30598 on the MODE object to determine the configuration of the contract.
30600 Returns a tree containing the identifiers used in the configuration.
30601 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
30602 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
30603 contract-role, if any. NULL_TREE is returned if no information is
30604 given (i.e., all defaults selected). */
30606 static tree
30607 cp_parser_contract_mode_opt (cp_parser *parser,
30608 bool postcondition_p)
30610 /* The mode is empty; the level and role are default. */
30611 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30612 return NULL_TREE;
30614 /* There is only a role; the level is default. */
30615 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30617 tree role_id = cp_parser_contract_role (parser);
30618 return build_tree_list (role_id, get_identifier ("default"));
30621 /* Otherwise, match semantic or level. */
30622 cp_token *token = cp_lexer_peek_token (parser->lexer);
30623 contract_level level = CONTRACT_INVALID;
30624 contract_semantic semantic = CCS_INVALID;
30625 tree config_id;
30626 if (token->type == CPP_NAME)
30628 config_id = token->u.value;
30630 /* Either a named level, a concrete semantic, or an identifier
30631 for a postcondition. */
30632 const char *ident = IDENTIFIER_POINTER (token->u.value);
30633 level = map_contract_level (ident);
30634 semantic = map_contract_semantic (ident);
30636 /* The identifier is the return value for a postcondition. */
30637 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
30638 && postcondition_p)
30639 return NULL_TREE;
30641 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30643 config_id = get_identifier ("default");
30644 level = CONTRACT_DEFAULT;
30646 else
30648 /* We got some other token other than a ':'. */
30649 error_at (token->location, "expected contract semantic or level");
30650 return NULL_TREE;
30653 /* Consume the literal semantic or level token. */
30654 cp_lexer_consume_token (parser->lexer);
30656 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
30658 error_at (token->location,
30659 "expected contract level: "
30660 "%<default%>, %<audit%>, or %<axiom%>");
30661 return NULL_TREE;
30664 /* We matched an explicit semantic. */
30665 if (semantic != CCS_INVALID)
30667 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30669 error ("invalid use of contract role for explicit semantic");
30670 cp_lexer_consume_token (parser->lexer);
30671 cp_lexer_consume_token (parser->lexer);
30673 return config_id;
30676 /* We matched a level, there may be a role; otherwise this is default. */
30677 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30679 tree role_id = cp_parser_contract_role (parser);
30680 return build_tree_list (role_id, config_id);
30683 return build_tree_list (NULL_TREE, config_id);
30686 static tree
30687 find_error (tree *tp, int *, void *)
30689 if (*tp == error_mark_node)
30690 return *tp;
30691 return NULL_TREE;
30694 static bool
30695 contains_error_p (tree t)
30697 return walk_tree (&t, find_error, NULL, NULL);
30700 /* Parse a standard C++20 contract attribute specifier.
30702 contract-attribute-specifier:
30703 [ [ assert contract-level [opt] : conditional-expression ] ]
30704 [ [ pre contract-level [opt] : conditional-expression ] ]
30705 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
30707 For free functions, we cannot determine the type of the postcondition
30708 identifier because the we haven't called grokdeclarator yet. In those
30709 cases we parse the postcondition as if the identifier was declared as
30710 'auto <identifier>'. We then instantiate the postcondition once the
30711 return type is known.
30713 For member functions, contracts are in the complete-class context, so the
30714 parse is deferred. We also have the return type avaialable (unless it's
30715 deduced), so we don't need to parse the postcondition in terms of a
30716 placeholder. */
30718 static tree
30719 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30721 gcc_assert (contract_attribute_p (attribute));
30722 cp_token *token = cp_lexer_consume_token (parser->lexer);
30723 location_t loc = token->location;
30725 bool assertion_p = is_attribute_p ("assert", attribute);
30726 bool postcondition_p = is_attribute_p ("post", attribute);
30728 /* Parse the optional mode. */
30729 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30731 /* Check for postcondition identifiers. */
30732 cp_expr identifier;
30733 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30734 identifier = cp_parser_identifier (parser);
30735 if (identifier == error_mark_node)
30736 return error_mark_node;
30738 cp_parser_require (parser, CPP_COLON, RT_COLON);
30740 /* Defer the parsing of pre/post contracts inside class definitions. */
30741 tree contract;
30742 if (!assertion_p &&
30743 current_class_type &&
30744 TYPE_BEING_DEFINED (current_class_type))
30746 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30747 that doesn't close the attribute, return an error and let the attribute
30748 handling code emit an error for missing ']]'. */
30749 cp_token *first = cp_lexer_peek_token (parser->lexer);
30750 cp_parser_skip_to_closing_parenthesis_1 (parser,
30751 /*recovering=*/false,
30752 CPP_CLOSE_SQUARE,
30753 /*consume_paren=*/false);
30754 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30755 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30756 return error_mark_node;
30757 cp_token *last = cp_lexer_peek_token (parser->lexer);
30759 /* Build a deferred-parse node. */
30760 tree condition = make_node (DEFERRED_PARSE);
30761 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30762 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30764 /* And its corresponding contract. */
30765 contract = grok_contract (attribute, mode, identifier, condition, loc);
30767 else
30769 /* Enable location wrappers when parsing contracts. */
30770 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30772 /* Build a fake variable for the result identifier. */
30773 tree result = NULL_TREE;
30774 if (identifier)
30776 begin_scope (sk_block, NULL_TREE);
30777 result = make_postcondition_variable (identifier);
30778 ++processing_template_decl;
30781 /* Parse the condition, ensuring that parameters or the return variable
30782 aren't flagged for use outside the body of a function. */
30783 ++processing_contract_condition;
30784 cp_expr condition = cp_parser_conditional_expression (parser);
30785 --processing_contract_condition;
30787 /* Try to recover from errors by scanning up to the end of the
30788 attribute. Sometimes we get partially parsed expressions, so
30789 we need to search the condition for errors. */
30790 if (contains_error_p (condition))
30791 cp_parser_skip_up_to_closing_square_bracket (parser);
30793 /* Build the contract. */
30794 contract = grok_contract (attribute, mode, result, condition, loc);
30796 /* Leave our temporary scope for the postcondition result. */
30797 if (result)
30799 --processing_template_decl;
30800 pop_bindings_and_leave_scope ();
30804 if (!flag_contracts)
30806 error_at (loc, "contracts are only available with %<-fcontracts%>");
30807 return error_mark_node;
30810 return finish_contract_attribute (attribute, contract);
30813 /* Parse a contract condition for a deferred contract. */
30815 void cp_parser_late_contract_condition (cp_parser *parser,
30816 tree fn,
30817 tree attribute)
30819 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30821 /* Make sure we've gotten something that hasn't been parsed yet or that
30822 we're not parsing an invalid contract. */
30823 tree condition = CONTRACT_CONDITION (contract);
30824 if (TREE_CODE (condition) != DEFERRED_PARSE)
30825 return;
30827 tree identifier = NULL_TREE;
30828 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30829 identifier = POSTCONDITION_IDENTIFIER (contract);
30831 /* Build a fake variable for the result identifier. */
30832 tree result = NULL_TREE;
30833 if (identifier)
30835 /* TODO: Can we guarantee that the identifier has a location? */
30836 location_t loc = cp_expr_location (contract);
30837 tree type = TREE_TYPE (TREE_TYPE (fn));
30838 if (!check_postcondition_result (fn, type, loc))
30840 invalidate_contract (contract);
30841 return;
30844 begin_scope (sk_block, NULL_TREE);
30845 result = make_postcondition_variable (identifier, type);
30846 ++processing_template_decl;
30849 /* 'this' is not allowed in preconditions of constructors or in postconditions
30850 of destructors. Note that the previous value of this variable is
30851 established by the calling function, so we need to save it here. */
30852 tree saved_ccr = current_class_ref;
30853 tree saved_ccp = current_class_ptr;
30854 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30855 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30857 current_class_ref = current_class_ptr = NULL_TREE;
30858 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30861 push_unparsed_function_queues (parser);
30863 /* Push the saved tokens onto the parser's lexer stack. */
30864 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30865 cp_parser_push_lexer_for_tokens (parser, tokens);
30867 /* Parse the condition, ensuring that parameters or the return variable
30868 aren't flagged for use outside the body of a function. */
30869 ++processing_contract_condition;
30870 condition = cp_parser_conditional_expression (parser);
30871 --processing_contract_condition;
30873 /* Revert to the main lexer. */
30874 cp_parser_pop_lexer (parser);
30876 /* Restore the queue. */
30877 pop_unparsed_function_queues (parser);
30879 current_class_ref = saved_ccr;
30880 current_class_ptr = saved_ccp;
30882 /* Commit to changes. */
30883 update_late_contract (contract, result, condition);
30885 /* Leave our temporary scope for the postcondition result. */
30886 if (result)
30888 --processing_template_decl;
30889 pop_bindings_and_leave_scope ();
30893 /* Parse a standard C++-11 attribute specifier.
30895 attribute-specifier:
30896 [ [ attribute-using-prefix [opt] attribute-list ] ]
30897 contract-attribute-specifier
30898 alignment-specifier
30900 attribute-using-prefix:
30901 using attribute-namespace :
30903 alignment-specifier:
30904 alignas ( type-id ... [opt] )
30905 alignas ( alignment-expression ... [opt] ).
30907 Extensions for contracts:
30909 contract-attribute-specifier:
30910 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30911 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30912 [ [ post : contract-mode [opt] identifier [opt] :
30913 conditional-expression ] ]
30915 Return void_list_node if the current token doesn't start an
30916 attribute-specifier to differentiate from NULL_TREE returned e.g.
30917 for [ [ ] ]. */
30919 static tree
30920 cp_parser_std_attribute_spec (cp_parser *parser)
30922 tree attributes = NULL_TREE;
30923 cp_token *token = cp_lexer_peek_token (parser->lexer);
30925 if (token->type == CPP_OPEN_SQUARE
30926 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30928 tree attr_ns = NULL_TREE;
30929 tree attr_name = NULL_TREE;
30931 cp_lexer_consume_token (parser->lexer);
30932 cp_lexer_consume_token (parser->lexer);
30934 token = cp_lexer_peek_token (parser->lexer);
30935 if (token->type == CPP_NAME)
30937 attr_name = token->u.value;
30938 attr_name = canonicalize_attr_name (attr_name);
30941 /* Handle contract-attribute-specs specially. */
30942 if (attr_name && contract_attribute_p (attr_name))
30944 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30945 if (attrs != error_mark_node)
30946 attributes = attrs;
30947 goto finish_attrs;
30950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30952 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30953 if (token->type == CPP_NAME)
30954 attr_ns = token->u.value;
30955 else if (token->type == CPP_KEYWORD)
30956 attr_ns = ridpointers[(int) token->keyword];
30957 else if (token->flags & NAMED_OP)
30958 attr_ns = get_identifier (cpp_type2name (token->type,
30959 token->flags));
30960 if (attr_ns
30961 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30963 if (cxx_dialect < cxx17)
30964 pedwarn (input_location, OPT_Wc__17_extensions,
30965 "attribute using prefix only available "
30966 "with %<-std=c++17%> or %<-std=gnu++17%>");
30968 cp_lexer_consume_token (parser->lexer);
30969 cp_lexer_consume_token (parser->lexer);
30970 cp_lexer_consume_token (parser->lexer);
30972 else
30973 attr_ns = NULL_TREE;
30976 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30978 finish_attrs:
30979 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30980 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30981 cp_parser_skip_to_end_of_statement (parser);
30982 else
30983 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30984 when we are sure that we have actually parsed them. */
30985 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30987 else
30989 tree alignas_expr;
30991 /* Look for an alignment-specifier. */
30993 token = cp_lexer_peek_token (parser->lexer);
30995 if (token->type != CPP_KEYWORD
30996 || token->keyword != RID_ALIGNAS)
30997 return void_list_node;
30999 cp_lexer_consume_token (parser->lexer);
31000 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
31002 matching_parens parens;
31003 if (!parens.require_open (parser))
31004 return error_mark_node;
31006 cp_parser_parse_tentatively (parser);
31007 alignas_expr = cp_parser_type_id (parser);
31009 if (!cp_parser_parse_definitely (parser))
31011 alignas_expr = cp_parser_assignment_expression (parser);
31012 if (alignas_expr == error_mark_node)
31013 cp_parser_skip_to_end_of_statement (parser);
31014 if (alignas_expr == NULL_TREE
31015 || alignas_expr == error_mark_node)
31016 return alignas_expr;
31019 alignas_expr = cxx_alignas_expr (alignas_expr);
31020 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
31022 /* Handle alignas (pack...). */
31023 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31025 cp_lexer_consume_token (parser->lexer);
31026 alignas_expr = make_pack_expansion (alignas_expr);
31029 /* Something went wrong, so don't build the attribute. */
31030 if (alignas_expr == error_mark_node)
31031 return error_mark_node;
31033 /* Missing ')' means the code cannot possibly be valid; go ahead
31034 and commit to make sure we issue a hard error. */
31035 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
31036 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31037 cp_parser_commit_to_tentative_parse (parser);
31039 if (!parens.require_close (parser))
31040 return error_mark_node;
31042 /* Build the C++-11 representation of an 'aligned'
31043 attribute. */
31044 attributes
31045 = build_tree_list (build_tree_list (gnu_identifier,
31046 aligned_identifier), alignas_expr);
31049 return attributes;
31052 /* Parse a standard C++-11 attribute-specifier-seq.
31054 attribute-specifier-seq:
31055 attribute-specifier-seq [opt] attribute-specifier */
31057 static tree
31058 cp_parser_std_attribute_spec_seq (cp_parser *parser)
31060 tree attr_specs = NULL_TREE;
31061 tree attr_last = NULL_TREE;
31063 /* Don't create wrapper nodes within attributes: the
31064 handlers don't know how to handle them. */
31065 auto_suppress_location_wrappers sentinel;
31067 while (true)
31069 tree attr_spec = cp_parser_std_attribute_spec (parser);
31070 if (attr_spec == void_list_node)
31071 break;
31072 /* Accept [[]][[]]; for which cp_parser_std_attribute_spec
31073 returns NULL_TREE as there are no attributes. */
31074 if (attr_spec == NULL_TREE)
31075 continue;
31076 if (attr_spec == error_mark_node)
31077 return error_mark_node;
31079 if (attr_last)
31080 TREE_CHAIN (attr_last) = attr_spec;
31081 else
31082 attr_specs = attr_last = attr_spec;
31083 attr_last = tree_last (attr_last);
31086 return attr_specs;
31089 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
31090 return index of the first token after balanced-token, or N on failure. */
31092 static size_t
31093 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
31095 size_t orig_n = n;
31096 int nparens = 0, nbraces = 0, nsquares = 0;
31098 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
31100 case CPP_PRAGMA_EOL:
31101 if (!parser->lexer->in_pragma)
31102 break;
31103 /* FALLTHRU */
31104 case CPP_EOF:
31105 /* Ran out of tokens. */
31106 return orig_n;
31107 case CPP_OPEN_PAREN:
31108 ++nparens;
31109 break;
31110 case CPP_OPEN_BRACE:
31111 ++nbraces;
31112 break;
31113 case CPP_OPEN_SQUARE:
31114 ++nsquares;
31115 break;
31116 case CPP_CLOSE_PAREN:
31117 --nparens;
31118 break;
31119 case CPP_CLOSE_BRACE:
31120 --nbraces;
31121 break;
31122 case CPP_CLOSE_SQUARE:
31123 --nsquares;
31124 break;
31125 default:
31126 break;
31128 while (nparens || nbraces || nsquares);
31129 return n;
31132 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
31133 return index of the first token after the GNU attribute tokens, or N on
31134 failure. */
31136 static size_t
31137 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
31139 while (true)
31141 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
31142 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
31143 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
31144 break;
31146 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
31147 if (n2 == n + 2)
31148 break;
31149 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
31150 break;
31151 n = n2 + 1;
31153 return n;
31156 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
31157 next token), return index of the first token after the standard C++11
31158 attribute tokens, or N on failure. */
31160 static size_t
31161 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
31163 while (true)
31165 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
31166 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
31168 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
31169 if (n2 == n + 1)
31170 break;
31171 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
31172 break;
31173 n = n2 + 1;
31175 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
31176 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
31178 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
31179 if (n2 == n + 1)
31180 break;
31181 n = n2;
31183 else
31184 break;
31186 return n;
31189 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
31190 as the next token), return index of the first token after the attribute
31191 tokens, or N on failure. */
31193 static size_t
31194 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
31196 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
31197 return cp_parser_skip_gnu_attributes_opt (parser, n);
31198 return cp_parser_skip_std_attribute_spec_seq (parser, n);
31201 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
31202 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
31203 current value of the PEDANTIC flag, regardless of whether or not
31204 the `__extension__' keyword is present. The caller is responsible
31205 for restoring the value of the PEDANTIC flag. */
31207 static bool
31208 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
31210 /* Save the old value of the PEDANTIC flag. */
31211 *saved_pedantic = pedantic;
31213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
31215 /* Consume the `__extension__' token. */
31216 cp_lexer_consume_token (parser->lexer);
31217 /* We're not being pedantic while the `__extension__' keyword is
31218 in effect. */
31219 pedantic = 0;
31221 return true;
31224 return false;
31227 /* Parse a label declaration.
31229 label-declaration:
31230 __label__ label-declarator-seq ;
31232 label-declarator-seq:
31233 identifier , label-declarator-seq
31234 identifier */
31236 static void
31237 cp_parser_label_declaration (cp_parser* parser)
31239 /* Look for the `__label__' keyword. */
31240 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
31242 while (true)
31244 tree identifier;
31246 /* Look for an identifier. */
31247 identifier = cp_parser_identifier (parser);
31248 /* If we failed, stop. */
31249 if (identifier == error_mark_node)
31250 break;
31251 /* Declare it as a label. */
31252 finish_label_decl (identifier);
31253 /* If the next token is a `;', stop. */
31254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31255 break;
31256 /* Look for the `,' separating the label declarations. */
31257 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
31260 /* Look for the final `;'. */
31261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31264 // -------------------------------------------------------------------------- //
31265 // Concept definitions
31267 static tree
31268 cp_parser_concept_definition (cp_parser *parser)
31270 /* A concept definition is an unevaluated context. */
31271 cp_unevaluated u;
31273 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
31274 cp_lexer_consume_token (parser->lexer);
31276 cp_expr id = cp_parser_identifier (parser);
31277 if (id == error_mark_node)
31279 cp_parser_skip_to_end_of_statement (parser);
31280 cp_parser_consume_semicolon_at_end_of_statement (parser);
31281 return NULL_TREE;
31284 tree attrs = cp_parser_attributes_opt (parser);
31286 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31288 cp_parser_skip_to_end_of_statement (parser);
31289 cp_parser_consume_semicolon_at_end_of_statement (parser);
31290 return error_mark_node;
31293 processing_constraint_expression_sentinel parsing_constraint;
31294 tree init = cp_parser_constraint_expression (parser);
31295 if (init == error_mark_node)
31296 cp_parser_skip_to_end_of_statement (parser);
31298 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
31299 but continue as if it were. */
31300 cp_parser_consume_semicolon_at_end_of_statement (parser);
31302 return finish_concept_definition (id, init, attrs);
31305 // -------------------------------------------------------------------------- //
31306 // Requires Clause
31308 /* Diagnose an expression that should appear in ()'s within a requires-clause
31309 and suggest where to place those parentheses. */
31311 static void
31312 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
31314 error_at (loc, "expression must be enclosed in parentheses");
31317 static void
31318 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
31320 gcc_rich_location richloc (loc);
31321 richloc.add_fixit_insert_before ("(");
31322 richloc.add_fixit_insert_after (")");
31323 error_at (&richloc, "expression must be enclosed in parentheses");
31326 /* Characterizes the likely kind of expression intended by a mis-written
31327 primary constraint. */
31328 enum primary_constraint_error
31330 pce_ok,
31331 pce_maybe_operator,
31332 pce_maybe_postfix
31335 /* Returns true if the token(s) following a primary-expression in a
31336 constraint-logical-* expression would require parentheses. */
31338 static primary_constraint_error
31339 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
31341 cp_token *token = cp_lexer_peek_token (parser->lexer);
31342 switch (token->type)
31344 default:
31345 return pce_ok;
31347 case CPP_EQ:
31349 /* An equal sign may be part of the definition of a function,
31350 and not an assignment operator, when parsing the expression
31351 for a trailing requires-clause. For example:
31353 template<typename T>
31354 struct S {
31355 S() requires C<T> = default;
31358 Don't try to reparse this a binary operator. */
31359 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
31360 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
31361 return pce_ok;
31363 gcc_fallthrough ();
31366 /* Arithmetic operators. */
31367 case CPP_PLUS:
31368 case CPP_MINUS:
31369 case CPP_MULT:
31370 case CPP_DIV:
31371 case CPP_MOD:
31372 /* Bitwise operators. */
31373 case CPP_AND:
31374 case CPP_OR:
31375 case CPP_XOR:
31376 case CPP_RSHIFT:
31377 case CPP_LSHIFT:
31378 /* Relational operators. */
31379 case CPP_EQ_EQ:
31380 case CPP_NOT_EQ:
31381 case CPP_LESS:
31382 case CPP_GREATER:
31383 case CPP_LESS_EQ:
31384 case CPP_GREATER_EQ:
31385 case CPP_SPACESHIP:
31386 /* Pointer-to-member. */
31387 case CPP_DOT_STAR:
31388 case CPP_DEREF_STAR:
31389 /* Assignment operators. */
31390 case CPP_PLUS_EQ:
31391 case CPP_MINUS_EQ:
31392 case CPP_MULT_EQ:
31393 case CPP_DIV_EQ:
31394 case CPP_MOD_EQ:
31395 case CPP_AND_EQ:
31396 case CPP_OR_EQ:
31397 case CPP_XOR_EQ:
31398 case CPP_RSHIFT_EQ:
31399 case CPP_LSHIFT_EQ:
31400 /* Conditional operator */
31401 case CPP_QUERY:
31402 /* Unenclosed binary or conditional operator. */
31403 return pce_maybe_operator;
31405 case CPP_OPEN_PAREN:
31407 /* A primary constraint that precedes the parameter-list of a
31408 lambda expression is followed by an open paren.
31410 []<typename T> requires C (T a, T b) { ... }
31412 Don't try to re-parse this as a postfix expression. */
31413 if (lambda_p)
31414 return pce_ok;
31416 gcc_fallthrough ();
31418 case CPP_OPEN_SQUARE:
31420 /* A primary-constraint-expression followed by a '[[' is not a
31421 postfix expression. */
31422 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
31423 return pce_ok;
31425 gcc_fallthrough ();
31427 case CPP_PLUS_PLUS:
31428 case CPP_MINUS_MINUS:
31429 case CPP_DOT:
31430 /* Unenclosed postfix operator. */
31431 return pce_maybe_postfix;
31433 case CPP_DEREF:
31434 /* A primary constraint that precedes the lambda-declarator of a
31435 lambda expression is followed by trailing return type.
31437 []<typename T> requires C -> void {}
31439 Don't try to re-parse this as a postfix expression in
31440 C++23 and later. In C++20 ( needs to come in between but we
31441 allow it to be omitted with pedwarn. */
31442 if (lambda_p)
31443 return pce_ok;
31444 /* Unenclosed postfix operator. */
31445 return pce_maybe_postfix;
31449 /* Returns true if the next token begins a unary expression, preceded by
31450 an operator or keyword. */
31452 static bool
31453 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
31455 cp_token *token = cp_lexer_peek_token (parser->lexer);
31456 switch (token->type)
31458 case CPP_NOT:
31459 case CPP_PLUS:
31460 case CPP_MINUS:
31461 case CPP_MULT:
31462 case CPP_COMPL:
31463 case CPP_PLUS_PLUS:
31464 case CPP_MINUS_MINUS:
31465 return true;
31467 case CPP_KEYWORD:
31469 switch (token->keyword)
31471 case RID_STATCAST:
31472 case RID_DYNCAST:
31473 case RID_REINTCAST:
31474 case RID_CONSTCAST:
31475 case RID_TYPEID:
31476 case RID_SIZEOF:
31477 case RID_ALIGNOF:
31478 case RID_NOEXCEPT:
31479 case RID_NEW:
31480 case RID_DELETE:
31481 case RID_THROW:
31482 return true;
31484 default:
31485 break;
31489 default:
31490 break;
31493 return false;
31496 /* Parse a primary expression within a constraint. */
31498 static cp_expr
31499 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
31501 /* If this looks like a unary expression, parse it as such, but diagnose
31502 it as ill-formed; it requires parens. */
31503 if (cp_parser_unary_constraint_requires_parens (parser))
31505 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
31506 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
31507 return e;
31510 cp_lexer_save_tokens (parser->lexer);
31511 cp_id_kind idk;
31512 location_t loc = input_location;
31513 cp_expr expr = cp_parser_primary_expression (parser,
31514 /*address_p=*/false,
31515 /*cast_p=*/false,
31516 /*template_arg_p=*/false,
31517 &idk);
31518 expr.maybe_add_location_wrapper ();
31520 primary_constraint_error pce = pce_ok;
31521 if (expr != error_mark_node)
31523 /* The primary-expression could be part of an unenclosed non-logical
31524 compound expression. */
31525 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
31527 if (pce == pce_ok)
31529 if (idk == CP_ID_KIND_UNQUALIFIED && identifier_p (expr))
31530 expr = unqualified_name_lookup_error (expr);
31531 cp_lexer_commit_tokens (parser->lexer);
31532 return finish_constraint_primary_expr (expr);
31535 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
31536 error, but return the expression as if it were valid. */
31537 cp_lexer_rollback_tokens (parser->lexer);
31538 cp_parser_parse_tentatively (parser);
31539 if (pce == pce_maybe_operator)
31540 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31541 else
31542 expr = cp_parser_simple_cast_expression (parser);
31543 if (cp_parser_parse_definitely (parser))
31545 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
31546 return expr;
31549 /* Otherwise, something has gone very wrong, and we can't generate a more
31550 meaningful diagnostic or recover. */
31551 cp_parser_diagnose_ungrouped_constraint_plain (loc);
31552 return error_mark_node;
31555 /* Parse a constraint-logical-and-expression.
31557 constraint-logical-and-expression:
31558 primary-expression
31559 constraint-logical-and-expression '&&' primary-expression */
31561 static cp_expr
31562 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
31564 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31565 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
31567 cp_token *op = cp_lexer_consume_token (parser->lexer);
31568 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31569 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
31571 return lhs;
31574 /* Parse a constraint-logical-or-expression.
31576 constraint-logical-or-expression:
31577 constraint-logical-and-expression
31578 constraint-logical-or-expression '||' constraint-logical-and-expression */
31580 static cp_expr
31581 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
31583 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31584 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
31586 cp_token *op = cp_lexer_consume_token (parser->lexer);
31587 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31588 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
31590 return lhs;
31593 /* Parse the expression after a requires-clause. This has a different grammar
31594 than that in the concepts TS. */
31596 static tree
31597 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
31599 processing_constraint_expression_sentinel parsing_constraint;
31600 ++processing_template_decl;
31601 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
31602 --processing_template_decl;
31603 if (check_for_bare_parameter_packs (expr))
31604 expr = error_mark_node;
31605 return expr;
31608 /* Parse a expression after a requires clause.
31610 constraint-expression:
31611 logical-or-expression
31613 The required logical-or-expression must be a constant expression. Note
31614 that we don't check that the expression is constepxr here. We defer until
31615 we analyze constraints and then, we only check atomic constraints. */
31617 static tree
31618 cp_parser_constraint_expression (cp_parser *parser)
31620 processing_constraint_expression_sentinel parsing_constraint;
31621 ++processing_template_decl;
31622 cp_expr expr = cp_parser_binary_expression (parser, false, true,
31623 PREC_NOT_OPERATOR, NULL);
31624 --processing_template_decl;
31625 if (check_for_bare_parameter_packs (expr))
31626 expr = error_mark_node;
31627 expr.maybe_add_location_wrapper ();
31628 return expr;
31631 /* Optionally parse a requires clause:
31633 requires-clause:
31634 `requires` constraint-logical-or-expression.
31635 [ConceptsTS]
31636 `requires constraint-expression.
31638 LAMBDA_P is true when the requires-clause is parsed before the
31639 parameter-list of a lambda-declarator. */
31641 static tree
31642 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
31644 /* A requires clause is an unevaluated context. */
31645 cp_unevaluated u;
31647 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31648 if (tok->keyword != RID_REQUIRES)
31650 if (!flag_concepts && tok->type == CPP_NAME
31651 && tok->u.value == ridpointers[RID_REQUIRES])
31653 error_at (cp_lexer_peek_token (parser->lexer)->location,
31654 "%<requires%> only available with "
31655 "%<-std=c++20%> or %<-fconcepts%>");
31656 /* Parse and discard the requires-clause. */
31657 cp_lexer_consume_token (parser->lexer);
31658 cp_parser_constraint_expression (parser);
31660 return NULL_TREE;
31663 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
31664 if (tok2->type == CPP_OPEN_BRACE)
31666 /* An opening brace following the start of a requires-clause is
31667 ill-formed; the user likely forgot the second `requires' that
31668 would start a requires-expression. */
31669 gcc_rich_location richloc (tok2->location);
31670 richloc.add_fixit_insert_after (tok->location, " requires");
31671 error_at (&richloc, "missing additional %<requires%> to start "
31672 "a requires-expression");
31673 /* Don't consume the `requires', so that it's reused as the start of a
31674 requires-expression. */
31676 else
31677 cp_lexer_consume_token (parser->lexer);
31679 if (!flag_concepts_ts)
31680 return cp_parser_requires_clause_expression (parser, lambda_p);
31681 else
31682 return cp_parser_constraint_expression (parser);
31685 /*---------------------------------------------------------------------------
31686 Requires expressions
31687 ---------------------------------------------------------------------------*/
31689 /* Parse a requires expression
31691 requirement-expression:
31692 'requires' requirement-parameter-list [opt] requirement-body */
31694 static tree
31695 cp_parser_requires_expression (cp_parser *parser)
31697 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31698 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
31700 /* Avoid committing to outer tentative parse. */
31701 tentative_firewall firewall (parser);
31703 /* This is definitely a requires-expression. */
31704 cp_parser_commit_to_tentative_parse (parser);
31706 tree parms, reqs;
31708 /* Local parameters are delared as variables within the scope
31709 of the expression. They are not visible past the end of
31710 the expression. Expressions within the requires-expression
31711 are unevaluated. */
31712 struct scope_sentinel
31714 scope_sentinel ()
31716 ++cp_unevaluated_operand;
31717 begin_scope (sk_function_parms, NULL_TREE);
31718 current_binding_level->requires_expression = true;
31721 ~scope_sentinel ()
31723 pop_bindings_and_leave_scope ();
31724 --cp_unevaluated_operand;
31726 } s;
31728 /* Parse the optional parameter list. */
31729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31731 parms = cp_parser_requirement_parameter_list (parser);
31732 if (parms == error_mark_node)
31733 return error_mark_node;
31735 else
31736 parms = NULL_TREE;
31738 /* Parse the requirement body. */
31739 ++processing_template_decl;
31740 reqs = cp_parser_requirement_body (parser);
31741 --processing_template_decl;
31742 if (reqs == error_mark_node)
31743 return error_mark_node;
31746 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31747 the parm chain. */
31748 grokparms (parms, &parms);
31749 loc = make_location (loc, loc, parser->lexer);
31750 tree expr = finish_requires_expr (loc, parms, reqs);
31751 if (!processing_template_decl)
31753 /* Perform semantic processing now to diagnose any invalid types and
31754 expressions. */
31755 int saved_errorcount = errorcount;
31756 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31757 if (errorcount > saved_errorcount)
31758 return error_mark_node;
31760 return expr;
31763 /* Parse a parameterized requirement.
31765 requirement-parameter-list:
31766 '(' parameter-declaration-clause ')' */
31768 static tree
31769 cp_parser_requirement_parameter_list (cp_parser *parser)
31771 matching_parens parens;
31772 if (!parens.require_open (parser))
31773 return error_mark_node;
31775 tree parms = (cp_parser_parameter_declaration_clause
31776 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31778 if (!parens.require_close (parser))
31779 return error_mark_node;
31781 /* Modify the declared parameters by removing their context
31782 so they don't refer to the enclosing scope and explicitly
31783 indicating that they are constraint variables. */
31784 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31786 if (parm == void_list_node || parm == explicit_void_list_node)
31787 break;
31788 tree decl = TREE_VALUE (parm);
31789 if (decl != error_mark_node)
31791 DECL_CONTEXT (decl) = NULL_TREE;
31792 CONSTRAINT_VAR_P (decl) = true;
31796 return parms;
31799 /* Parse the body of a requirement.
31801 requirement-body:
31802 '{' requirement-list '}' */
31803 static tree
31804 cp_parser_requirement_body (cp_parser *parser)
31806 matching_braces braces;
31807 if (!braces.require_open (parser))
31808 return error_mark_node;
31810 tree reqs = cp_parser_requirement_seq (parser);
31812 if (!braces.require_close (parser))
31813 return error_mark_node;
31815 return reqs;
31818 /* Parse a sequence of requirements.
31820 requirement-seq:
31821 requirement
31822 requirement-seq requirement */
31824 static tree
31825 cp_parser_requirement_seq (cp_parser *parser)
31827 tree result = NULL_TREE;
31830 tree req = cp_parser_requirement (parser);
31831 if (req != error_mark_node)
31832 result = tree_cons (NULL_TREE, req, result);
31834 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31835 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31837 /* If there are no valid requirements, this is not a valid expression. */
31838 if (!result)
31839 return error_mark_node;
31841 /* Reverse the order of requirements so they are analyzed in order. */
31842 return nreverse (result);
31845 /* Parse a syntactic requirement or type requirement.
31847 requirement:
31848 simple-requirement
31849 compound-requirement
31850 type-requirement
31851 nested-requirement */
31853 static tree
31854 cp_parser_requirement (cp_parser *parser)
31856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31857 return cp_parser_compound_requirement (parser);
31858 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31860 /* It's probably a type-requirement. */
31861 cp_parser_parse_tentatively (parser);
31862 tree req = cp_parser_type_requirement (parser);
31863 if (cp_parser_parse_definitely (parser))
31864 return req;
31865 /* No, maybe it's something like typename T::type(); */
31866 cp_parser_parse_tentatively (parser);
31867 req = cp_parser_simple_requirement (parser);
31868 if (cp_parser_parse_definitely (parser))
31869 return req;
31870 /* Non-tentative for the error. */
31871 return cp_parser_type_requirement (parser);
31873 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31874 return cp_parser_nested_requirement (parser);
31875 else
31876 return cp_parser_simple_requirement (parser);
31879 /* Parse a simple requirement.
31881 simple-requirement:
31882 expression ';' */
31884 static tree
31885 cp_parser_simple_requirement (cp_parser *parser)
31887 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31888 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31889 if (expr == error_mark_node)
31890 cp_parser_skip_to_end_of_statement (parser);
31892 cp_parser_consume_semicolon_at_end_of_statement (parser);
31894 if (!expr || expr == error_mark_node)
31895 return error_mark_node;
31897 /* Sometimes we don't get locations, so use the cached token location
31898 as a reasonable approximation. */
31899 if (expr.get_location() == UNKNOWN_LOCATION)
31900 expr.set_location (start);
31902 for (tree t = expr; ; )
31904 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31905 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31907 t = TREE_OPERAND (t, 0);
31908 continue;
31910 if (concept_check_p (t))
31912 gcc_rich_location richloc (get_start (start));
31913 richloc.add_fixit_insert_before (start, "requires ");
31914 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31915 "if a concept-id is a valid expression; add "
31916 "%<requires%> to check satisfaction");
31918 break;
31921 return finish_simple_requirement (expr.get_location (), expr);
31924 /* Parse a type requirement
31926 type-requirement
31927 nested-name-specifier [opt] required-type-name ';'
31929 required-type-name:
31930 type-name
31931 'template' [opt] simple-template-id */
31933 static tree
31934 cp_parser_type_requirement (cp_parser *parser)
31936 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31937 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31939 // Save the scope before parsing name specifiers.
31940 tree saved_scope = parser->scope;
31941 tree saved_object_scope = parser->object_scope;
31942 tree saved_qualifying_scope = parser->qualifying_scope;
31943 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31944 cp_parser_nested_name_specifier_opt (parser,
31945 /*typename_keyword_p=*/true,
31946 /*check_dependency_p=*/true,
31947 /*type_p=*/true,
31948 /*is_declaration=*/false);
31950 tree type;
31951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31953 cp_lexer_consume_token (parser->lexer);
31954 type = cp_parser_template_id (parser,
31955 /*template_keyword_p=*/true,
31956 /*check_dependency_p=*/true,
31957 /*tag_type=*/none_type,
31958 /*is_declaration=*/false);
31959 type = make_typename_type (parser->scope, type, typename_type,
31960 /*complain=*/tf_error);
31962 else
31963 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31965 if (TREE_CODE (type) == TYPE_DECL)
31966 type = TREE_TYPE (type);
31968 parser->scope = saved_scope;
31969 parser->object_scope = saved_object_scope;
31970 parser->qualifying_scope = saved_qualifying_scope;
31972 if (type == error_mark_node)
31973 cp_parser_skip_to_end_of_statement (parser);
31975 cp_parser_consume_semicolon_at_end_of_statement (parser);
31977 if (type == error_mark_node)
31978 return error_mark_node;
31980 loc = make_location (loc, start_tok->location, parser->lexer);
31981 return finish_type_requirement (loc, type);
31984 /* Parse a compound requirement
31986 compound-requirement:
31987 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31989 static tree
31990 cp_parser_compound_requirement (cp_parser *parser)
31992 /* Parse an expression enclosed in '{ }'s. */
31993 matching_braces braces;
31994 if (!braces.require_open (parser))
31995 return error_mark_node;
31997 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31999 tree expr = cp_parser_expression (parser, NULL, false, false);
32000 if (expr == error_mark_node)
32001 cp_parser_skip_to_closing_brace (parser);
32003 if (!braces.require_close (parser))
32005 cp_parser_skip_to_end_of_statement (parser);
32006 cp_parser_consume_semicolon_at_end_of_statement (parser);
32007 return error_mark_node;
32010 /* If the expression was invalid, skip the remainder of the requirement. */
32011 if (!expr || expr == error_mark_node)
32013 cp_parser_skip_to_end_of_statement (parser);
32014 cp_parser_consume_semicolon_at_end_of_statement (parser);
32015 return error_mark_node;
32018 /* Parse the optional noexcept. */
32019 bool noexcept_p = false;
32020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
32022 cp_lexer_consume_token (parser->lexer);
32023 noexcept_p = true;
32026 /* Parse the optional trailing return type. */
32027 tree type = NULL_TREE;
32028 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
32030 cp_lexer_consume_token (parser->lexer);
32031 cp_token *tok = cp_lexer_peek_token (parser->lexer);
32033 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
32034 parser->in_result_type_constraint_p = true;
32035 /* C++20 allows either a type-id or a type-constraint. Parsing
32036 a type-id will subsume the parsing for a type-constraint but
32037 allow for more syntactic forms (e.g., const C<T>*). */
32038 type = cp_parser_trailing_type_id (parser);
32039 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
32040 if (type == error_mark_node)
32041 return error_mark_node;
32043 location_t type_loc = make_location (tok->location, tok->location,
32044 parser->lexer);
32046 /* Check that we haven't written something like 'const C<T>*'. */
32047 if (type_uses_auto (type))
32049 if (!is_auto (type))
32051 error_at (type_loc,
32052 "result type is not a plain type-constraint");
32053 cp_parser_consume_semicolon_at_end_of_statement (parser);
32054 return error_mark_node;
32057 else if (!flag_concepts_ts)
32058 /* P1452R2 removed the trailing-return-type option. */
32059 error_at (type_loc,
32060 "return-type-requirement is not a type-constraint");
32063 location_t loc = make_location (expr_token->location,
32064 braces.open_location (),
32065 parser->lexer);
32067 cp_parser_consume_semicolon_at_end_of_statement (parser);
32069 if (expr == error_mark_node || type == error_mark_node)
32070 return error_mark_node;
32072 return finish_compound_requirement (loc, expr, type, noexcept_p);
32075 /* Parse a nested requirement. This is the same as a requires clause.
32077 nested-requirement:
32078 requires-clause */
32080 static tree
32081 cp_parser_nested_requirement (cp_parser *parser)
32083 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
32084 cp_token *tok = cp_lexer_consume_token (parser->lexer);
32085 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32086 tree req = cp_parser_constraint_expression (parser);
32087 if (req == error_mark_node)
32088 cp_parser_skip_to_end_of_statement (parser);
32089 loc = make_location (loc, tok->location, parser->lexer);
32090 cp_parser_consume_semicolon_at_end_of_statement (parser);
32091 if (req == error_mark_node)
32092 return error_mark_node;
32093 return finish_nested_requirement (loc, req);
32096 /* Support Functions */
32098 /* Return the appropriate prefer_type argument for lookup_name based on
32099 tag_type. */
32101 static inline LOOK_want
32102 prefer_type_arg (tag_types tag_type)
32104 switch (tag_type)
32106 case none_type: return LOOK_want::NORMAL; // No preference.
32107 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
32108 default: return LOOK_want::TYPE; // Type only.
32112 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
32113 NAME should have one of the representations used for an
32114 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
32115 is returned. If PARSER->SCOPE is a dependent type, then a
32116 SCOPE_REF is returned.
32118 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
32119 returned; the name was already resolved when the TEMPLATE_ID_EXPR
32120 was formed. Abstractly, such entities should not be passed to this
32121 function, because they do not need to be looked up, but it is
32122 simpler to check for this special case here, rather than at the
32123 call-sites.
32125 In cases not explicitly covered above, this function returns a
32126 DECL, OVERLOAD, or baselink representing the result of the lookup.
32127 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
32128 is returned.
32130 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
32131 (e.g., "struct") that was used. In that case bindings that do not
32132 refer to types are ignored.
32134 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
32135 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
32137 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
32138 are ignored.
32140 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
32141 types.
32143 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
32144 TREE_LIST of candidates if name-lookup results in an ambiguity, and
32145 NULL_TREE otherwise. */
32147 static cp_expr
32148 cp_parser_lookup_name (cp_parser *parser, tree name,
32149 enum tag_types tag_type,
32150 int is_template,
32151 bool is_namespace,
32152 bool check_dependency,
32153 tree *ambiguous_decls,
32154 location_t name_location)
32156 tree decl;
32157 tree object_type = parser->context->object_type;
32159 /* Assume that the lookup will be unambiguous. */
32160 if (ambiguous_decls)
32161 *ambiguous_decls = NULL_TREE;
32163 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
32164 no longer valid. Note that if we are parsing tentatively, and
32165 the parse fails, OBJECT_TYPE will be automatically restored. */
32166 parser->context->object_type = NULL_TREE;
32168 if (name == error_mark_node)
32169 return error_mark_node;
32171 /* A template-id has already been resolved; there is no lookup to
32172 do. */
32173 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
32174 return name;
32175 if (BASELINK_P (name))
32177 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
32178 == TEMPLATE_ID_EXPR);
32179 return name;
32182 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
32183 it should already have been checked to make sure that the name
32184 used matches the type being destroyed. */
32185 if (TREE_CODE (name) == BIT_NOT_EXPR)
32187 tree type;
32189 /* Figure out to which type this destructor applies. */
32190 if (parser->scope)
32191 type = parser->scope;
32192 else if (object_type)
32193 type = object_type;
32194 else
32195 type = current_class_type;
32196 /* If that's not a class type, there is no destructor. */
32197 if (!type || !CLASS_TYPE_P (type))
32198 return error_mark_node;
32200 /* In a non-static member function, check implicit this->. */
32201 if (current_class_ref)
32202 return lookup_destructor (current_class_ref, parser->scope, name,
32203 tf_warning_or_error);
32205 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
32206 lazily_declare_fn (sfk_destructor, type);
32208 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
32209 return dtor;
32211 return error_mark_node;
32214 /* By this point, the NAME should be an ordinary identifier. If
32215 the id-expression was a qualified name, the qualifying scope is
32216 stored in PARSER->SCOPE at this point. */
32217 gcc_assert (identifier_p (name));
32219 /* Perform the lookup. */
32220 if (parser->scope)
32222 bool dependent_p;
32224 if (parser->scope == error_mark_node)
32225 return error_mark_node;
32227 /* If the SCOPE is dependent, the lookup must be deferred until
32228 the template is instantiated -- unless we are explicitly
32229 looking up names in uninstantiated templates. Even then, we
32230 cannot look up the name if the scope is not a class type; it
32231 might, for example, be a template type parameter. */
32232 dependent_p = (TYPE_P (parser->scope)
32233 && dependent_scope_p (parser->scope));
32234 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
32235 && dependent_p)
32236 /* Defer lookup. */
32237 decl = error_mark_node;
32238 else
32240 tree pushed_scope = NULL_TREE;
32242 /* If PARSER->SCOPE is a dependent type, then it must be a
32243 class type, and we must not be checking dependencies;
32244 otherwise, we would have processed this lookup above. So
32245 that PARSER->SCOPE is not considered a dependent base by
32246 lookup_member, we must enter the scope here. */
32247 if (dependent_p)
32248 pushed_scope = push_scope (parser->scope);
32250 /* If the PARSER->SCOPE is a template specialization, it
32251 may be instantiated during name lookup. In that case,
32252 errors may be issued. Even if we rollback the current
32253 tentative parse, those errors are valid. */
32254 decl = lookup_qualified_name (parser->scope, name,
32255 prefer_type_arg (tag_type),
32256 /*complain=*/true);
32258 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
32259 lookup result and the nested-name-specifier nominates a class C:
32260 * if the name specified after the nested-name-specifier, when
32261 looked up in C, is the injected-class-name of C (Clause 9), or
32262 * if the name specified after the nested-name-specifier is the
32263 same as the identifier or the simple-template-id's template-
32264 name in the last component of the nested-name-specifier,
32265 the name is instead considered to name the constructor of
32266 class C. [ Note: for example, the constructor is not an
32267 acceptable lookup result in an elaborated-type-specifier so
32268 the constructor would not be used in place of the
32269 injected-class-name. --end note ] Such a constructor name
32270 shall be used only in the declarator-id of a declaration that
32271 names a constructor or in a using-declaration. */
32272 if (tag_type == none_type
32273 && DECL_SELF_REFERENCE_P (decl)
32274 && same_type_p (DECL_CONTEXT (decl), parser->scope))
32275 decl = lookup_qualified_name (parser->scope, ctor_identifier,
32276 prefer_type_arg (tag_type),
32277 /*complain=*/true);
32279 if (pushed_scope)
32280 pop_scope (pushed_scope);
32283 /* If the scope is a dependent type and either we deferred lookup or
32284 we did lookup but didn't find the name, rememeber the name. */
32285 if (decl == error_mark_node && TYPE_P (parser->scope)
32286 && dependent_type_p (parser->scope))
32288 if (tag_type)
32290 tree type;
32292 /* The resolution to Core Issue 180 says that `struct
32293 A::B' should be considered a type-name, even if `A'
32294 is dependent. */
32295 type = make_typename_type (parser->scope, name, tag_type,
32296 /*complain=*/tf_error);
32297 if (type != error_mark_node)
32298 decl = TYPE_NAME (type);
32300 else if (is_template
32301 && (cp_parser_next_token_ends_template_argument_p (parser)
32302 || cp_lexer_next_token_is (parser->lexer,
32303 CPP_CLOSE_PAREN)))
32304 decl = make_unbound_class_template (parser->scope,
32305 name, NULL_TREE,
32306 /*complain=*/tf_error);
32307 else
32308 decl = build_qualified_name (/*type=*/NULL_TREE,
32309 parser->scope, name,
32310 is_template);
32312 parser->qualifying_scope = parser->scope;
32313 parser->object_scope = NULL_TREE;
32315 else if (object_type)
32317 bool dep = dependent_scope_p (object_type);
32319 /* Look up the name in the scope of the OBJECT_TYPE, unless the
32320 OBJECT_TYPE is not a class. */
32321 if (!dep && CLASS_TYPE_P (object_type))
32322 /* If the OBJECT_TYPE is a template specialization, it may
32323 be instantiated during name lookup. In that case, errors
32324 may be issued. Even if we rollback the current tentative
32325 parse, those errors are valid. */
32326 decl = lookup_member (object_type,
32327 name,
32328 /*protect=*/0,
32329 /*prefer_type=*/tag_type != none_type,
32330 tf_warning_or_error);
32331 else
32332 decl = NULL_TREE;
32334 /* If we didn't find a member and have dependent bases, the member lookup
32335 is now dependent. */
32336 if (!dep && !decl && any_dependent_bases_p (object_type))
32337 dep = true;
32339 if (dep && is_template == 2)
32340 /* The template keyword specifies a dependent template. */;
32341 else if (!decl)
32342 /* Look it up in the enclosing context. DR 141: When looking for a
32343 template-name after -> or ., only consider class templates. */
32344 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32345 /* DR 141: When looking in the
32346 current enclosing context for a
32347 template-name after -> or ., only
32348 consider class templates. */
32349 : is_template ? LOOK_want::TYPE
32350 : prefer_type_arg (tag_type));
32352 /* If we did unqualified lookup of a dependent member-qualified name and
32353 found something, do we want to use it? P1787 clarified that we need
32354 to look in the object scope first even if it's dependent, but for now
32355 let's still use it in some cases.
32356 FIXME remember unqualified lookup result to use if member lookup fails
32357 at instantiation time. */
32358 if (decl && dep && is_template)
32360 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
32361 /* Only use the unqualified class template lookup if we're actually
32362 looking at a template arg list. */
32363 if (!cp_parser_skip_entire_template_parameter_list (parser))
32364 decl = NULL_TREE;
32367 /* If we know we're looking for a type (e.g. A in p->A::x),
32368 mock up a typename. */
32369 if (!decl && dep && tag_type != none_type)
32371 tree type = build_typename_type (object_type, name, name,
32372 typename_type);
32373 decl = TYPE_NAME (type);
32376 parser->object_scope = object_type;
32377 parser->qualifying_scope = NULL_TREE;
32379 else
32381 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32382 : prefer_type_arg (tag_type));
32383 parser->qualifying_scope = NULL_TREE;
32384 parser->object_scope = NULL_TREE;
32387 /* If the lookup failed, let our caller know. */
32388 if (!decl || decl == error_mark_node)
32389 return error_mark_node;
32391 /* If we have resolved the name of a member declaration, check to
32392 see if the declaration is accessible. When the name resolves to
32393 set of overloaded functions, accessibility is checked when
32394 overload resolution is done. If we have a TREE_LIST, then the lookup
32395 is either ambiguous or it found multiple injected-class-names, the
32396 accessibility of which is trivially satisfied.
32398 During an explicit instantiation, access is not checked at all,
32399 as per [temp.explicit]. */
32400 if (DECL_P (decl))
32401 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
32402 tf_warning_or_error);
32404 /* Pull out the template from an injected-class-name (or multiple). */
32405 if (is_template)
32406 decl = maybe_get_template_decl_from_type_decl (decl);
32408 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
32409 if (TREE_CODE (decl) == TREE_LIST)
32411 if (ambiguous_decls)
32412 *ambiguous_decls = decl;
32413 /* The error message we have to print is too complicated for
32414 cp_parser_error, so we incorporate its actions directly. */
32415 if (!cp_parser_simulate_error (parser))
32417 error_at (name_location, "reference to %qD is ambiguous",
32418 name);
32419 print_candidates (decl);
32421 return error_mark_node;
32424 gcc_assert (DECL_P (decl)
32425 || TREE_CODE (decl) == OVERLOAD
32426 || TREE_CODE (decl) == SCOPE_REF
32427 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
32428 || BASELINK_P (decl));
32430 maybe_record_typedef_use (decl);
32432 return cp_expr (decl, name_location);
32435 /* Like cp_parser_lookup_name, but for use in the typical case where
32436 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
32437 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
32439 static tree
32440 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
32442 return cp_parser_lookup_name (parser, name,
32443 none_type,
32444 /*is_template=*/false,
32445 /*is_namespace=*/false,
32446 /*check_dependency=*/true,
32447 /*ambiguous_decls=*/NULL,
32448 location);
32451 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
32452 the current context, return the TYPE_DECL. If TAG_NAME_P is
32453 true, the DECL indicates the class being defined in a class-head,
32454 or declared in an elaborated-type-specifier.
32456 Otherwise, return DECL. */
32458 static tree
32459 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
32461 /* If the TEMPLATE_DECL is being declared as part of a class-head,
32462 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
32464 struct A {
32465 template <typename T> struct B;
32468 template <typename T> struct A::B {};
32470 Similarly, in an elaborated-type-specifier:
32472 namespace N { struct X{}; }
32474 struct A {
32475 template <typename T> friend struct N::X;
32478 However, if the DECL refers to a class type, and we are in
32479 the scope of the class, then the name lookup automatically
32480 finds the TYPE_DECL created by build_self_reference rather
32481 than a TEMPLATE_DECL. For example, in:
32483 template <class T> struct S {
32484 S s;
32487 there is no need to handle such case. */
32489 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
32490 return DECL_TEMPLATE_RESULT (decl);
32492 return decl;
32495 /* If too many, or too few, template-parameter lists apply to the
32496 declarator, issue an error message. Returns TRUE if all went well,
32497 and FALSE otherwise. */
32499 static bool
32500 cp_parser_check_declarator_template_parameters (cp_parser* parser,
32501 cp_declarator *declarator,
32502 location_t declarator_location)
32504 switch (declarator->kind)
32506 case cdk_id:
32508 unsigned num_templates = 0;
32509 tree scope = declarator->u.id.qualifying_scope;
32510 bool template_id_p = false;
32512 if (scope)
32513 num_templates = num_template_headers_for_class (scope);
32514 else if (TREE_CODE (declarator->u.id.unqualified_name)
32515 == TEMPLATE_ID_EXPR)
32517 /* If the DECLARATOR has the form `X<y>' then it uses one
32518 additional level of template parameters. */
32519 ++num_templates;
32520 template_id_p = true;
32523 return cp_parser_check_template_parameters
32524 (parser, num_templates, template_id_p, declarator_location,
32525 declarator);
32528 case cdk_function:
32529 case cdk_array:
32530 case cdk_pointer:
32531 case cdk_reference:
32532 case cdk_ptrmem:
32533 return (cp_parser_check_declarator_template_parameters
32534 (parser, declarator->declarator, declarator_location));
32536 case cdk_decomp:
32537 case cdk_error:
32538 return true;
32540 default:
32541 gcc_unreachable ();
32543 return false;
32546 /* NUM_TEMPLATES were used in the current declaration. If that is
32547 invalid, return FALSE and issue an error messages. Otherwise,
32548 return TRUE. If DECLARATOR is non-NULL, then we are checking a
32549 declarator and we can print more accurate diagnostics. */
32551 static bool
32552 cp_parser_check_template_parameters (cp_parser* parser,
32553 unsigned num_templates,
32554 bool template_id_p,
32555 location_t location,
32556 cp_declarator *declarator)
32558 /* If there are the same number of template classes and parameter
32559 lists, that's OK. */
32560 if (parser->num_template_parameter_lists == num_templates)
32561 return true;
32562 /* If there are more, but only one more, and the name ends in an identifier,
32563 then we are declaring a primary template. That's OK too. */
32564 if (!template_id_p
32565 && parser->num_template_parameter_lists == num_templates + 1)
32566 return true;
32568 if (cp_parser_simulate_error (parser))
32569 return false;
32571 /* If there are more template classes than parameter lists, we have
32572 something like:
32574 template <class T> void S<T>::R<T>::f (); */
32575 if (parser->num_template_parameter_lists < num_templates)
32577 if (declarator && !current_function_decl)
32578 error_at (location, "specializing member %<%T::%E%> "
32579 "requires %<template<>%> syntax",
32580 declarator->u.id.qualifying_scope,
32581 declarator->u.id.unqualified_name);
32582 else if (declarator)
32583 error_at (location, "invalid declaration of %<%T::%E%>",
32584 declarator->u.id.qualifying_scope,
32585 declarator->u.id.unqualified_name);
32586 else
32587 error_at (location, "too few template-parameter-lists");
32588 return false;
32590 /* Otherwise, there are too many template parameter lists. We have
32591 something like:
32593 template <class T> template <class U> void S::f(); */
32594 error_at (location, "too many template-parameter-lists");
32595 return false;
32598 /* Parse an optional `::' token indicating that the following name is
32599 from the global namespace. If so, PARSER->SCOPE is set to the
32600 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
32601 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
32602 Returns the new value of PARSER->SCOPE, if the `::' token is
32603 present, and NULL_TREE otherwise. */
32605 static tree
32606 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
32608 cp_token *token;
32610 /* Peek at the next token. */
32611 token = cp_lexer_peek_token (parser->lexer);
32612 /* If we're looking at a `::' token then we're starting from the
32613 global namespace, not our current location. */
32614 if (token->type == CPP_SCOPE)
32616 /* Consume the `::' token. */
32617 cp_lexer_consume_token (parser->lexer);
32618 /* Set the SCOPE so that we know where to start the lookup. */
32619 parser->scope = global_namespace;
32620 parser->qualifying_scope = global_namespace;
32621 parser->object_scope = NULL_TREE;
32623 return parser->scope;
32625 else if (!current_scope_valid_p)
32627 parser->scope = NULL_TREE;
32628 parser->qualifying_scope = NULL_TREE;
32629 parser->object_scope = NULL_TREE;
32632 return NULL_TREE;
32635 /* Returns TRUE if the upcoming token sequence is the start of a
32636 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
32637 declarator is preceded by the `friend' specifier. The parser flags FLAGS
32638 is used to control type-specifier parsing. */
32640 static bool
32641 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
32642 bool friend_p)
32644 bool constructor_p;
32645 bool outside_class_specifier_p;
32646 tree nested_name_specifier;
32647 cp_token *next_token;
32649 /* The common case is that this is not a constructor declarator, so
32650 try to avoid doing lots of work if at all possible. It's not
32651 valid declare a constructor at function scope. */
32652 if (parser->in_function_body)
32653 return false;
32654 /* And only certain tokens can begin a constructor declarator. */
32655 next_token = cp_lexer_peek_token (parser->lexer);
32656 if (next_token->type != CPP_NAME
32657 && next_token->type != CPP_SCOPE
32658 && next_token->type != CPP_NESTED_NAME_SPECIFIER
32659 && next_token->type != CPP_TEMPLATE_ID)
32660 return false;
32662 /* Parse tentatively; we are going to roll back all of the tokens
32663 consumed here. */
32664 cp_parser_parse_tentatively (parser);
32665 /* Assume that we are looking at a constructor declarator. */
32666 constructor_p = true;
32668 /* Look for the optional `::' operator. */
32669 cp_parser_global_scope_opt (parser,
32670 /*current_scope_valid_p=*/false);
32671 /* Look for the nested-name-specifier. */
32672 nested_name_specifier
32673 = (cp_parser_nested_name_specifier_opt (parser,
32674 /*typename_keyword_p=*/false,
32675 /*check_dependency_p=*/false,
32676 /*type_p=*/false,
32677 /*is_declaration=*/false));
32679 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
32680 if (nested_name_specifier
32681 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
32683 tree s = resolve_typename_type (nested_name_specifier,
32684 /*only_current_p=*/false);
32685 if (TREE_CODE (s) != TYPENAME_TYPE)
32686 nested_name_specifier = s;
32689 outside_class_specifier_p = (!at_class_scope_p ()
32690 || !TYPE_BEING_DEFINED (current_class_type)
32691 || friend_p);
32693 /* Outside of a class-specifier, there must be a
32694 nested-name-specifier. Except in C++17 mode, where we
32695 might be declaring a guiding declaration. */
32696 if (!nested_name_specifier && outside_class_specifier_p
32697 && cxx_dialect < cxx17)
32698 constructor_p = false;
32699 else if (nested_name_specifier == error_mark_node)
32700 constructor_p = false;
32702 /* If we have a class scope, this is easy; DR 147 says that S::S always
32703 names the constructor, and no other qualified name could. */
32704 if (constructor_p && nested_name_specifier
32705 && CLASS_TYPE_P (nested_name_specifier))
32707 tree id = cp_parser_unqualified_id (parser,
32708 /*template_keyword_p=*/false,
32709 /*check_dependency_p=*/false,
32710 /*declarator_p=*/true,
32711 /*optional_p=*/false);
32712 if (is_overloaded_fn (id))
32713 id = DECL_NAME (get_first_fn (id));
32714 if (!constructor_name_p (id, nested_name_specifier))
32715 constructor_p = false;
32717 /* If we still think that this might be a constructor-declarator,
32718 look for a class-name. */
32719 else if (constructor_p)
32721 /* If we have:
32723 template <typename T> struct S {
32724 S();
32727 we must recognize that the nested `S' names a class. */
32728 if (cxx_dialect >= cxx17)
32729 cp_parser_parse_tentatively (parser);
32731 tree type_decl;
32732 type_decl = cp_parser_class_name (parser,
32733 /*typename_keyword_p=*/false,
32734 /*template_keyword_p=*/false,
32735 none_type,
32736 /*check_dependency_p=*/false,
32737 /*class_head_p=*/false,
32738 /*is_declaration=*/false);
32740 if (cxx_dialect >= cxx17
32741 && !cp_parser_parse_definitely (parser))
32743 type_decl = NULL_TREE;
32744 tree tmpl = cp_parser_template_name (parser,
32745 /*template_keyword*/false,
32746 /*check_dependency_p*/false,
32747 /*is_declaration*/false,
32748 none_type,
32749 /*is_identifier*/NULL);
32750 if (DECL_CLASS_TEMPLATE_P (tmpl)
32751 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32752 /* It's a deduction guide, return true. */;
32753 else
32754 cp_parser_simulate_error (parser);
32757 /* If there was no class-name, then this is not a constructor.
32758 Otherwise, if we are in a class-specifier and we aren't
32759 handling a friend declaration, check that its type matches
32760 current_class_type (c++/38313). Note: error_mark_node
32761 is left alone for error recovery purposes. */
32762 constructor_p = (!cp_parser_error_occurred (parser)
32763 && (outside_class_specifier_p
32764 || type_decl == NULL_TREE
32765 || type_decl == error_mark_node
32766 || same_type_p (current_class_type,
32767 TREE_TYPE (type_decl))));
32769 /* If we're still considering a constructor, we have to see a `(',
32770 to begin the parameter-declaration-clause, followed by either a
32771 `)', an `...', or a decl-specifier. We need to check for a
32772 type-specifier to avoid being fooled into thinking that:
32774 S (f) (int);
32776 is a constructor. (It is actually a function named `f' that
32777 takes one parameter (of type `int') and returns a value of type
32778 `S'. */
32779 if (constructor_p
32780 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32781 constructor_p = false;
32783 if (constructor_p
32784 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32785 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32786 /* A parameter declaration begins with a decl-specifier,
32787 which is either the "attribute" keyword, a storage class
32788 specifier, or (usually) a type-specifier. */
32789 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32790 /* GNU attributes can actually appear both at the start of
32791 a parameter and parenthesized declarator.
32792 S (__attribute__((unused)) int);
32793 is a constructor, but
32794 S (__attribute__((unused)) foo) (int);
32795 is a function declaration. [[attribute]] can appear in the
32796 first form too, but not in the second form. */
32797 && !cp_next_tokens_can_be_std_attribute_p (parser))
32799 tree type;
32800 tree pushed_scope = NULL_TREE;
32801 unsigned saved_num_template_parameter_lists;
32803 if (cp_parser_allow_gnu_extensions_p (parser)
32804 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32806 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32807 while (--n)
32808 cp_lexer_consume_token (parser->lexer);
32811 /* Names appearing in the type-specifier should be looked up
32812 in the scope of the class. */
32813 if (current_class_type)
32814 type = NULL_TREE;
32815 else if (type_decl)
32817 type = TREE_TYPE (type_decl);
32818 if (TREE_CODE (type) == TYPENAME_TYPE)
32820 type = resolve_typename_type (type,
32821 /*only_current_p=*/false);
32822 if (TREE_CODE (type) == TYPENAME_TYPE)
32824 cp_parser_abort_tentative_parse (parser);
32825 return false;
32828 pushed_scope = push_scope (type);
32831 /* Inside the constructor parameter list, surrounding
32832 template-parameter-lists do not apply. */
32833 saved_num_template_parameter_lists
32834 = parser->num_template_parameter_lists;
32835 parser->num_template_parameter_lists = 0;
32837 /* Look for the type-specifier. It's not optional, but its typename
32838 might be. Unless this is a friend declaration; we don't want to
32839 treat
32841 friend S (T::fn)(int);
32843 as a constructor, but with P0634, we might assume a type when
32844 looking for the type-specifier. It is actually a function named
32845 `T::fn' that takes one parameter (of type `int') and returns a
32846 value of type `S'. Constructors can be friends, but they must
32847 use a qualified name.
32849 Parse with an empty set of declaration specifiers since we're
32850 trying to match a decl-specifier-seq of the first parameter.
32851 This must be non-null so that cp_parser_simple_type_specifier
32852 will recognize a constrained placeholder type such as:
32853 'C<int> auto' where C is a type concept. */
32854 cp_decl_specifier_seq ctor_specs;
32855 clear_decl_specs (&ctor_specs);
32856 cp_parser_type_specifier (parser,
32857 (friend_p ? CP_PARSER_FLAGS_NONE
32858 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32859 /*decl_specs=*/&ctor_specs,
32860 /*is_declarator=*/true,
32861 /*declares_class_or_enum=*/NULL,
32862 /*is_cv_qualifier=*/NULL);
32864 parser->num_template_parameter_lists
32865 = saved_num_template_parameter_lists;
32867 /* Leave the scope of the class. */
32868 if (pushed_scope)
32869 pop_scope (pushed_scope);
32871 constructor_p = !cp_parser_error_occurred (parser);
32875 /* We did not really want to consume any tokens. */
32876 cp_parser_abort_tentative_parse (parser);
32878 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
32879 declarator-id of a constructor or destructor. */
32880 if (constructor_p
32881 && cp_lexer_peek_token (parser->lexer)->type == CPP_TEMPLATE_ID)
32883 auto_diagnostic_group d;
32884 if (emit_diagnostic (cxx_dialect >= cxx20 ? DK_PEDWARN : DK_WARNING,
32885 input_location, OPT_Wtemplate_id_cdtor,
32886 "template-id not allowed for constructor in C++20"))
32887 inform (input_location, "remove the %qs", "< >");
32890 return constructor_p;
32893 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32894 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32895 they must be performed once we are in the scope of the function.
32897 Returns the function defined. */
32899 static tree
32900 cp_parser_function_definition_from_specifiers_and_declarator
32901 (cp_parser* parser,
32902 cp_decl_specifier_seq *decl_specifiers,
32903 tree attributes,
32904 const cp_declarator *declarator)
32906 tree fn;
32907 bool success_p;
32909 /* Begin the function-definition. */
32910 success_p = start_function (decl_specifiers, declarator, attributes);
32912 /* The things we're about to see are not directly qualified by any
32913 template headers we've seen thus far. */
32914 reset_specialization ();
32916 /* If there were names looked up in the decl-specifier-seq that we
32917 did not check, check them now. We must wait until we are in the
32918 scope of the function to perform the checks, since the function
32919 might be a friend. */
32920 perform_deferred_access_checks (tf_warning_or_error);
32922 if (success_p)
32924 cp_finalize_omp_declare_simd (parser, current_function_decl);
32925 parser->omp_declare_simd = NULL;
32926 cp_finalize_oacc_routine (parser, current_function_decl, true);
32927 parser->oacc_routine = NULL;
32930 if (!success_p)
32932 /* Skip the entire function. */
32933 cp_parser_skip_to_end_of_block_or_statement (parser);
32934 fn = error_mark_node;
32936 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32938 /* Seen already, skip it. An error message has already been output. */
32939 cp_parser_skip_to_end_of_block_or_statement (parser);
32940 fn = current_function_decl;
32941 current_function_decl = NULL_TREE;
32942 /* If this is a function from a class, pop the nested class. */
32943 if (current_class_name)
32944 pop_nested_class ();
32946 else
32948 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32949 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32950 fn = cp_parser_function_definition_after_declarator (parser,
32951 /*inline_p=*/false);
32954 return fn;
32957 /* Parse the part of a function-definition that follows the
32958 declarator. INLINE_P is TRUE iff this function is an inline
32959 function defined within a class-specifier.
32961 Returns the function defined. */
32963 static tree
32964 cp_parser_function_definition_after_declarator (cp_parser* parser,
32965 bool inline_p)
32967 tree fn;
32968 bool saved_in_unbraced_linkage_specification_p;
32969 bool saved_in_function_body;
32970 unsigned saved_num_template_parameter_lists;
32971 cp_token *token;
32972 bool fully_implicit_function_template_p
32973 = parser->fully_implicit_function_template_p;
32974 parser->fully_implicit_function_template_p = false;
32975 tree implicit_template_parms
32976 = parser->implicit_template_parms;
32977 parser->implicit_template_parms = 0;
32978 cp_binding_level* implicit_template_scope
32979 = parser->implicit_template_scope;
32980 parser->implicit_template_scope = 0;
32982 saved_in_function_body = parser->in_function_body;
32983 parser->in_function_body = true;
32984 /* If the next token is `return', then the code may be trying to
32985 make use of the "named return value" extension that G++ used to
32986 support. */
32987 token = cp_lexer_peek_token (parser->lexer);
32988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32990 /* Consume the `return' keyword. */
32991 cp_lexer_consume_token (parser->lexer);
32992 /* Look for the identifier that indicates what value is to be
32993 returned. */
32994 cp_parser_identifier (parser);
32995 /* Issue an error message. */
32996 error_at (token->location,
32997 "named return values are no longer supported");
32998 /* Skip tokens until we reach the start of the function body. */
32999 while (true)
33001 cp_token *token = cp_lexer_peek_token (parser->lexer);
33002 if (token->type == CPP_OPEN_BRACE
33003 || token->type == CPP_EOF
33004 || token->type == CPP_PRAGMA_EOL)
33005 break;
33006 cp_lexer_consume_token (parser->lexer);
33009 /* The `extern' in `extern "C" void f () { ... }' does not apply to
33010 anything declared inside `f'. */
33011 saved_in_unbraced_linkage_specification_p
33012 = parser->in_unbraced_linkage_specification_p;
33013 parser->in_unbraced_linkage_specification_p = false;
33014 /* Inside the function, surrounding template-parameter-lists do not
33015 apply. */
33016 saved_num_template_parameter_lists
33017 = parser->num_template_parameter_lists;
33018 parser->num_template_parameter_lists = 0;
33020 /* If the next token is `try', `__transaction_atomic', or
33021 `__transaction_relaxed`, then we are looking at either function-try-block
33022 or function-transaction-block. Note that all of these include the
33023 function-body. */
33024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
33025 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
33026 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33027 RID_TRANSACTION_RELAXED))
33028 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
33029 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33030 cp_parser_function_try_block (parser);
33031 else
33032 cp_parser_ctor_initializer_opt_and_function_body
33033 (parser, /*in_function_try_block=*/false);
33035 /* Finish the function. */
33036 fn = finish_function (inline_p);
33038 if (modules_p ()
33039 && !inline_p
33040 && TYPE_P (DECL_CONTEXT (fn))
33041 && (DECL_DECLARED_INLINE_P (fn)
33042 || processing_template_decl))
33043 set_defining_module (fn);
33045 /* Generate code for it, if necessary. */
33046 expand_or_defer_fn (fn);
33048 /* Restore the saved values. */
33049 parser->in_unbraced_linkage_specification_p
33050 = saved_in_unbraced_linkage_specification_p;
33051 parser->num_template_parameter_lists
33052 = saved_num_template_parameter_lists;
33053 parser->in_function_body = saved_in_function_body;
33055 parser->fully_implicit_function_template_p
33056 = fully_implicit_function_template_p;
33057 parser->implicit_template_parms
33058 = implicit_template_parms;
33059 parser->implicit_template_scope
33060 = implicit_template_scope;
33062 if (parser->fully_implicit_function_template_p)
33063 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
33065 return fn;
33068 /* Parse a template-declaration body (following argument list). */
33070 static void
33071 cp_parser_template_declaration_after_parameters (cp_parser* parser,
33072 tree parameter_list,
33073 bool member_p)
33075 tree decl = NULL_TREE;
33076 bool friend_p = false;
33078 /* We just processed one more parameter list. */
33079 ++parser->num_template_parameter_lists;
33081 /* Get the deferred access checks from the parameter list. These
33082 will be checked once we know what is being declared, as for a
33083 member template the checks must be performed in the scope of the
33084 class containing the member. */
33085 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
33087 /* Tentatively parse for a new template parameter list, which can either be
33088 the template keyword or a template introduction. */
33089 if (cp_parser_template_declaration_after_export (parser, member_p))
33090 /* OK */;
33091 else if (cxx_dialect >= cxx11
33092 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
33093 decl = cp_parser_alias_declaration (parser);
33094 else if (flag_concepts
33095 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
33096 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33097 /* -fconcept-ts 'concept bool' syntax is handled below, in
33098 cp_parser_single_declaration. */
33099 decl = cp_parser_concept_definition (parser);
33100 else
33102 cp_token *token = cp_lexer_peek_token (parser->lexer);
33103 decl = cp_parser_single_declaration (parser,
33104 checks,
33105 member_p,
33106 /*explicit_specialization_p=*/false,
33107 &friend_p);
33109 /* If this is a member template declaration, let the front
33110 end know. */
33111 if (member_p && !friend_p && decl)
33113 if (TREE_CODE (decl) == TYPE_DECL)
33114 cp_parser_check_access_in_redeclaration (decl, token->location);
33116 decl = finish_member_template_decl (decl);
33118 else if (friend_p && decl
33119 && DECL_DECLARES_TYPE_P (decl))
33120 make_friend_class (current_class_type, TREE_TYPE (decl),
33121 /*complain=*/true);
33123 /* We are done with the current parameter list. */
33124 --parser->num_template_parameter_lists;
33126 pop_deferring_access_checks ();
33128 /* Finish up. */
33129 finish_template_decl (parameter_list);
33131 /* Check the template arguments for a literal operator template. */
33132 if (decl
33133 && DECL_DECLARES_FUNCTION_P (decl)
33134 && UDLIT_OPER_P (DECL_NAME (decl)))
33136 bool ok = true;
33137 if (parameter_list == NULL_TREE)
33138 ok = false;
33139 else
33141 int num_parms = TREE_VEC_LENGTH (parameter_list);
33142 if (num_parms == 1)
33144 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
33145 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
33146 if (TREE_CODE (parm) != PARM_DECL)
33147 ok = false;
33148 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
33149 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
33150 /* OK, C++20 string literal operator template. We don't need
33151 to warn in lower dialects here because we will have already
33152 warned about the template parameter. */;
33153 else if (TREE_TYPE (parm) != char_type_node
33154 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
33155 ok = false;
33157 else if (num_parms == 2 && cxx_dialect >= cxx14)
33159 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
33160 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
33161 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
33162 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
33163 if (TREE_CODE (parm) != PARM_DECL
33164 || TREE_TYPE (parm) != TREE_TYPE (type)
33165 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
33166 ok = false;
33167 else
33168 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
33169 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
33170 "ISO C++ did not adopt string literal operator templa"
33171 "tes taking an argument pack of characters");
33173 else
33174 ok = false;
33176 if (!ok)
33178 if (cxx_dialect > cxx17)
33179 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
33180 "template %qD has invalid parameter list; expected "
33181 "non-type template parameter pack %<<char...>%> or "
33182 "single non-type parameter of class type",
33183 decl);
33184 else
33185 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
33186 "template %qD has invalid parameter list; expected "
33187 "non-type template parameter pack %<<char...>%>",
33188 decl);
33192 /* Register member declarations. */
33193 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
33194 finish_member_declaration (decl);
33195 /* If DECL is a function template, we must return to parse it later.
33196 (Even though there is no definition, there might be default
33197 arguments that need handling.) */
33198 if (member_p && decl
33199 && DECL_DECLARES_FUNCTION_P (decl))
33200 vec_safe_push (unparsed_funs_with_definitions, decl);
33203 /* Parse a template introduction header for a template-declaration. Returns
33204 false if tentative parse fails. */
33206 static bool
33207 cp_parser_template_introduction (cp_parser* parser, bool member_p)
33209 cp_parser_parse_tentatively (parser);
33211 tree saved_scope = parser->scope;
33212 tree saved_object_scope = parser->object_scope;
33213 tree saved_qualifying_scope = parser->qualifying_scope;
33214 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33216 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
33218 /* In classes don't parse valid unnamed bitfields as invalid
33219 template introductions. */
33220 if (member_p)
33221 parser->colon_corrects_to_scope_p = false;
33223 /* Look for the optional `::' operator. */
33224 cp_parser_global_scope_opt (parser,
33225 /*current_scope_valid_p=*/false);
33226 /* Look for the nested-name-specifier. */
33227 cp_parser_nested_name_specifier_opt (parser,
33228 /*typename_keyword_p=*/false,
33229 /*check_dependency_p=*/true,
33230 /*type_p=*/false,
33231 /*is_declaration=*/false);
33233 cp_token *token = cp_lexer_peek_token (parser->lexer);
33234 tree concept_name = cp_parser_identifier (parser);
33236 /* Look up the concept for which we will be matching
33237 template parameters. */
33238 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
33239 token->location);
33240 parser->scope = saved_scope;
33241 parser->object_scope = saved_object_scope;
33242 parser->qualifying_scope = saved_qualifying_scope;
33243 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33245 if (concept_name == error_mark_node
33246 || (seen_error () && !concept_definition_p (tmpl_decl)))
33247 cp_parser_simulate_error (parser);
33249 /* Look for opening brace for introduction. */
33250 matching_braces braces;
33251 braces.require_open (parser);
33252 location_t open_loc = input_location;
33254 if (!cp_parser_parse_definitely (parser))
33255 return false;
33257 push_deferring_access_checks (dk_deferred);
33259 /* Build vector of placeholder parameters and grab
33260 matching identifiers. */
33261 tree introduction_list = cp_parser_introduction_list (parser);
33263 /* Look for closing brace for introduction. */
33264 if (!braces.require_close (parser))
33265 return true;
33267 /* The introduction-list shall not be empty. */
33268 int nargs = TREE_VEC_LENGTH (introduction_list);
33269 if (nargs == 0)
33271 /* In cp_parser_introduction_list we have already issued an error. */
33272 return true;
33275 if (tmpl_decl == error_mark_node)
33277 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
33278 token->location);
33279 return true;
33282 /* Build and associate the constraint. */
33283 location_t introduction_loc = make_location (open_loc,
33284 start_token->location,
33285 parser->lexer);
33286 tree parms = finish_template_introduction (tmpl_decl,
33287 introduction_list,
33288 introduction_loc);
33289 if (parms && parms != error_mark_node)
33291 if (!flag_concepts_ts)
33292 pedwarn (introduction_loc, 0, "template-introductions"
33293 " are not part of C++20 concepts; use %qs to enable",
33294 "-fconcepts-ts");
33296 cp_parser_template_declaration_after_parameters (parser, parms,
33297 member_p);
33298 return true;
33301 if (parms == NULL_TREE)
33302 error_at (token->location, "no matching concept for template-introduction");
33304 return true;
33307 /* Parse a normal template-declaration following the template keyword. */
33309 static void
33310 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
33312 tree parameter_list;
33313 bool need_lang_pop;
33314 location_t location = input_location;
33316 /* Look for the `<' token. */
33317 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
33318 return;
33319 if (at_class_scope_p () && current_function_decl)
33321 /* 14.5.2.2 [temp.mem]
33323 A local class shall not have member templates. */
33324 error_at (location,
33325 "invalid declaration of member template in local class");
33326 cp_parser_skip_to_end_of_block_or_statement (parser);
33327 return;
33329 /* [temp]
33331 A template ... shall not have C linkage. */
33332 if (current_lang_name == lang_name_c)
33334 error_at (location, "template with C linkage");
33335 maybe_show_extern_c_location ();
33336 /* Give it C++ linkage to avoid confusing other parts of the
33337 front end. */
33338 push_lang_context (lang_name_cplusplus);
33339 need_lang_pop = true;
33341 else
33342 need_lang_pop = false;
33344 /* We cannot perform access checks on the template parameter
33345 declarations until we know what is being declared, just as we
33346 cannot check the decl-specifier list. */
33347 push_deferring_access_checks (dk_deferred);
33349 /* If the next token is `>', then we have an invalid
33350 specialization. Rather than complain about an invalid template
33351 parameter, issue an error message here. */
33352 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
33354 cp_parser_error (parser, "invalid explicit specialization");
33355 begin_specialization ();
33356 parameter_list = NULL_TREE;
33358 else
33360 /* Parse the template parameters. */
33361 parameter_list = cp_parser_template_parameter_list (parser);
33364 /* Look for the `>'. */
33365 cp_parser_require_end_of_template_parameter_list (parser);
33367 /* Manage template requirements */
33368 if (flag_concepts)
33370 tree reqs = get_shorthand_constraints (current_template_parms);
33371 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
33372 reqs = combine_constraint_expressions (reqs, treqs);
33373 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
33376 cp_parser_template_declaration_after_parameters (parser, parameter_list,
33377 member_p);
33379 /* For the erroneous case of a template with C linkage, we pushed an
33380 implicit C++ linkage scope; exit that scope now. */
33381 if (need_lang_pop)
33382 pop_lang_context ();
33385 /* Parse a template-declaration, assuming that the `export' (and
33386 `extern') keywords, if present, has already been scanned. MEMBER_P
33387 is as for cp_parser_template_declaration. */
33389 static bool
33390 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
33392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33394 cp_lexer_consume_token (parser->lexer);
33395 cp_parser_explicit_template_declaration (parser, member_p);
33396 return true;
33398 else if (flag_concepts)
33399 return cp_parser_template_introduction (parser, member_p);
33401 return false;
33404 /* Perform the deferred access checks from a template-parameter-list.
33405 CHECKS is a TREE_LIST of access checks, as returned by
33406 get_deferred_access_checks. */
33408 static void
33409 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
33411 ++processing_template_parmlist;
33412 perform_access_checks (checks, tf_warning_or_error);
33413 --processing_template_parmlist;
33416 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
33417 `function-definition' sequence that follows a template header.
33418 If MEMBER_P is true, this declaration appears in a class scope.
33420 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
33421 *FRIEND_P is set to TRUE iff the declaration is a friend. */
33423 static tree
33424 cp_parser_single_declaration (cp_parser* parser,
33425 vec<deferred_access_check, va_gc> *checks,
33426 bool member_p,
33427 bool explicit_specialization_p,
33428 bool* friend_p)
33430 int declares_class_or_enum;
33431 tree decl = NULL_TREE;
33432 cp_decl_specifier_seq decl_specifiers;
33433 bool function_definition_p = false;
33434 cp_token *decl_spec_token_start;
33436 /* This function is only used when processing a template
33437 declaration. */
33438 gcc_assert (innermost_scope_kind () == sk_template_parms
33439 || innermost_scope_kind () == sk_template_spec);
33441 /* Defer access checks until we know what is being declared. */
33442 push_deferring_access_checks (dk_deferred);
33444 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
33445 alternative. */
33446 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
33447 cp_parser_decl_specifier_seq (parser,
33448 (CP_PARSER_FLAGS_OPTIONAL
33449 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
33450 &decl_specifiers,
33451 &declares_class_or_enum);
33453 cp_omp_declare_simd_data odsd;
33454 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
33455 cp_parser_handle_directive_omp_attributes (parser,
33456 &decl_specifiers.attributes,
33457 &odsd, true);
33459 if (friend_p)
33460 *friend_p = cp_parser_friend_p (&decl_specifiers);
33462 /* There are no template typedefs. */
33463 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
33465 error_at (decl_spec_token_start->location,
33466 "template declaration of %<typedef%>");
33467 decl = error_mark_node;
33470 /* Gather up the access checks that occurred the
33471 decl-specifier-seq. */
33472 stop_deferring_access_checks ();
33474 /* Check for the declaration of a template class. */
33475 if (declares_class_or_enum)
33477 if (cp_parser_declares_only_class_p (parser)
33478 || (declares_class_or_enum & 2))
33480 decl = shadow_tag (&decl_specifiers);
33482 /* In this case:
33484 struct C {
33485 friend template <typename T> struct A<T>::B;
33488 A<T>::B will be represented by a TYPENAME_TYPE, and
33489 therefore not recognized by shadow_tag. */
33490 if (friend_p && *friend_p
33491 && !decl
33492 && decl_specifiers.type
33493 && TYPE_P (decl_specifiers.type))
33494 decl = decl_specifiers.type;
33496 if (decl && decl != error_mark_node)
33497 decl = TYPE_NAME (decl);
33498 else
33499 decl = error_mark_node;
33501 /* If this is a declaration, but not a definition, associate
33502 any constraints with the type declaration. Constraints
33503 are associated with definitions in cp_parser_class_specifier. */
33504 if (declares_class_or_enum == 1)
33505 associate_classtype_constraints (TREE_TYPE (decl));
33507 /* Perform access checks for template parameters. */
33508 cp_parser_perform_template_parameter_access_checks (checks);
33510 /* Give a helpful diagnostic for
33511 template <class T> struct A { } a;
33512 if we aren't already recovering from an error. */
33513 if (!cp_parser_declares_only_class_p (parser)
33514 && !seen_error ())
33516 error_at (cp_lexer_peek_token (parser->lexer)->location,
33517 "a class template declaration must not declare "
33518 "anything else");
33519 cp_parser_skip_to_end_of_block_or_statement (parser);
33520 goto out;
33525 /* Complain about missing 'typename' or other invalid type names. */
33526 if (!decl_specifiers.any_type_specifiers_p
33527 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
33529 /* cp_parser_parse_and_diagnose_invalid_type_name calls
33530 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
33531 the rest of this declaration. */
33532 decl = error_mark_node;
33533 goto out;
33536 /* If it's not a template class, try for a template function. If
33537 the next token is a `;', then this declaration does not declare
33538 anything. But, if there were errors in the decl-specifiers, then
33539 the error might well have come from an attempted class-specifier.
33540 In that case, there's no need to warn about a missing declarator. */
33541 if (!decl
33542 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
33543 || decl_specifiers.type != error_mark_node))
33545 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
33546 /* We don't delay parsing for friends, though CWG 2510 may change
33547 that. */
33548 if (member_p && !(friend_p && *friend_p))
33549 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
33550 decl = cp_parser_init_declarator (parser,
33551 flags,
33552 &decl_specifiers,
33553 checks,
33554 /*function_definition_allowed_p=*/true,
33555 member_p,
33556 declares_class_or_enum,
33557 &function_definition_p,
33558 NULL, NULL, NULL);
33560 /* 7.1.1-1 [dcl.stc]
33562 A storage-class-specifier shall not be specified in an explicit
33563 specialization... */
33564 if (decl
33565 && explicit_specialization_p
33566 && decl_specifiers.storage_class != sc_none)
33568 error_at (decl_spec_token_start->location,
33569 "explicit template specialization cannot have a storage class");
33570 decl = error_mark_node;
33573 if (decl && VAR_P (decl))
33574 check_template_variable (decl);
33577 /* Look for a trailing `;' after the declaration. */
33578 if (!function_definition_p
33579 && (decl == error_mark_node
33580 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
33581 cp_parser_skip_to_end_of_block_or_statement (parser);
33583 out:
33584 pop_deferring_access_checks ();
33586 /* Clear any current qualification; whatever comes next is the start
33587 of something new. */
33588 parser->scope = NULL_TREE;
33589 parser->qualifying_scope = NULL_TREE;
33590 parser->object_scope = NULL_TREE;
33592 cp_finalize_omp_declare_simd (parser, &odsd);
33594 return decl;
33597 /* Parse a cast-expression that is not the operand of a unary "&". */
33599 static cp_expr
33600 cp_parser_simple_cast_expression (cp_parser *parser)
33602 return cp_parser_cast_expression (parser, /*address_p=*/false,
33603 /*cast_p=*/false, /*decltype*/false, NULL);
33606 /* Parse a functional cast to TYPE. Returns an expression
33607 representing the cast. */
33609 static cp_expr
33610 cp_parser_functional_cast (cp_parser* parser, tree type)
33612 vec<tree, va_gc> *vec;
33613 tree expression_list;
33614 cp_expr cast;
33616 location_t start_loc = input_location;
33618 if (!type)
33619 type = error_mark_node;
33621 if (TREE_CODE (type) == TYPE_DECL
33622 && is_auto (TREE_TYPE (type)))
33623 type = TREE_TYPE (type);
33625 if (is_auto (type)
33626 && !AUTO_IS_DECLTYPE (type)
33627 && !PLACEHOLDER_TYPE_CONSTRAINTS (type)
33628 && !CLASS_PLACEHOLDER_TEMPLATE (type))
33629 /* auto(x) and auto{x} need to use a level-less auto. */
33630 type = make_cast_auto ();
33632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33634 cp_lexer_set_source_position (parser->lexer);
33635 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33636 expression_list = cp_parser_braced_list (parser);
33637 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
33638 if (TREE_CODE (type) == TYPE_DECL)
33639 type = TREE_TYPE (type);
33641 cast = finish_compound_literal (type, expression_list,
33642 tf_warning_or_error, fcl_functional);
33643 /* Create a location of the form:
33644 type_name{i, f}
33645 ^~~~~~~~~~~~~~~
33646 with caret == start at the start of the type name,
33647 finishing at the closing brace. */
33648 location_t combined_loc = make_location (start_loc, start_loc,
33649 parser->lexer);
33650 cast.set_location (combined_loc);
33651 return cast;
33655 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
33656 /*cast_p=*/true,
33657 /*allow_expansion_p=*/true,
33658 /*non_constant_p=*/NULL);
33659 if (vec == NULL)
33660 expression_list = error_mark_node;
33661 else
33663 expression_list = build_tree_list_vec (vec);
33664 release_tree_vector (vec);
33667 /* Create a location of the form:
33668 float(i)
33669 ^~~~~~~~
33670 with caret == start at the start of the type name,
33671 finishing at the closing paren. */
33672 location_t combined_loc = make_location (start_loc, start_loc,
33673 parser->lexer);
33674 cast = build_functional_cast (combined_loc, type, expression_list,
33675 tf_warning_or_error);
33677 /* [expr.const]/1: In an integral constant expression "only type
33678 conversions to integral or enumeration type can be used". */
33679 if (TREE_CODE (type) == TYPE_DECL)
33680 type = TREE_TYPE (type);
33681 if (cast != error_mark_node
33682 && !cast_valid_in_integral_constant_expression_p (type)
33683 && cp_parser_non_integral_constant_expression (parser,
33684 NIC_CONSTRUCTOR))
33685 return error_mark_node;
33687 return cast;
33690 /* Save the tokens that make up the body of a member function defined
33691 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
33692 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
33693 specifiers applied to the declaration. Returns the FUNCTION_DECL
33694 for the member function. */
33696 static tree
33697 cp_parser_save_member_function_body (cp_parser* parser,
33698 cp_decl_specifier_seq *decl_specifiers,
33699 cp_declarator *declarator,
33700 tree attributes)
33702 cp_token *first;
33703 cp_token *last;
33704 tree fn;
33705 bool function_try_block = false;
33707 /* Create the FUNCTION_DECL. */
33708 fn = grokmethod (decl_specifiers, declarator, attributes);
33709 cp_finalize_omp_declare_simd (parser, fn);
33710 cp_finalize_oacc_routine (parser, fn, true);
33711 /* If something went badly wrong, bail out now. */
33712 if (fn == error_mark_node)
33714 /* If there's a function-body, skip it. */
33715 if (cp_parser_token_starts_function_definition_p
33716 (cp_lexer_peek_token (parser->lexer)))
33717 cp_parser_skip_to_end_of_block_or_statement (parser);
33718 return error_mark_node;
33721 /* Remember it, if there are default args to post process. */
33722 cp_parser_save_default_args (parser, fn);
33724 /* Save away the tokens that make up the body of the
33725 function. */
33726 first = parser->lexer->next_token;
33728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
33729 cp_lexer_consume_token (parser->lexer);
33730 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33731 RID_TRANSACTION_ATOMIC))
33733 cp_lexer_consume_token (parser->lexer);
33734 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
33735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
33736 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
33737 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
33738 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
33739 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
33740 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
33742 cp_lexer_consume_token (parser->lexer);
33743 cp_lexer_consume_token (parser->lexer);
33744 cp_lexer_consume_token (parser->lexer);
33745 cp_lexer_consume_token (parser->lexer);
33746 cp_lexer_consume_token (parser->lexer);
33748 else
33749 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33750 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33752 cp_lexer_consume_token (parser->lexer);
33753 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33754 break;
33758 /* Handle function try blocks. */
33759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33761 cp_lexer_consume_token (parser->lexer);
33762 function_try_block = true;
33764 /* We can have braced-init-list mem-initializers before the fn body. */
33765 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33767 cp_lexer_consume_token (parser->lexer);
33768 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33770 /* cache_group will stop after an un-nested { } pair, too. */
33771 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33772 break;
33774 /* variadic mem-inits have ... after the ')'. */
33775 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33776 cp_lexer_consume_token (parser->lexer);
33779 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33780 /* Handle function try blocks. */
33781 if (function_try_block)
33782 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33783 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33784 last = parser->lexer->next_token;
33786 /* Save away the inline definition; we will process it when the
33787 class is complete. */
33788 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33789 DECL_PENDING_INLINE_P (fn) = 1;
33791 /* We need to know that this was defined in the class, so that
33792 friend templates are handled correctly. */
33793 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33795 /* Add FN to the queue of functions to be parsed later. */
33796 vec_safe_push (unparsed_funs_with_definitions, fn);
33798 return fn;
33801 /* Save the tokens that make up the in-class initializer for a non-static
33802 data member. Returns a DEFERRED_PARSE. */
33804 static tree
33805 cp_parser_save_nsdmi (cp_parser* parser)
33807 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33810 /* Parse a template-argument-list, as well as the trailing ">" (but
33811 not the opening "<"). See cp_parser_template_argument_list for the
33812 return value. */
33814 static tree
33815 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33817 tree arguments;
33818 tree saved_scope;
33819 tree saved_qualifying_scope;
33820 tree saved_object_scope;
33821 bool saved_greater_than_is_operator_p;
33823 /* [temp.names]
33825 When parsing a template-id, the first non-nested `>' is taken as
33826 the end of the template-argument-list rather than a greater-than
33827 operator. */
33828 saved_greater_than_is_operator_p
33829 = parser->greater_than_is_operator_p;
33830 parser->greater_than_is_operator_p = false;
33831 /* Parsing the argument list may modify SCOPE, so we save it
33832 here. */
33833 saved_scope = parser->scope;
33834 saved_qualifying_scope = parser->qualifying_scope;
33835 saved_object_scope = parser->object_scope;
33836 /* We need to evaluate the template arguments, even though this
33837 template-id may be nested within a "sizeof". */
33838 cp_evaluated ev;
33839 /* Parse the template-argument-list itself. */
33840 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33841 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33842 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33843 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33845 arguments = make_tree_vec (0);
33846 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33848 else
33849 arguments = cp_parser_template_argument_list (parser);
33850 /* Look for the `>' that ends the template-argument-list. If we find
33851 a '>>' instead, it's probably just a typo. */
33852 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33854 if (cxx_dialect != cxx98)
33856 /* In C++0x, a `>>' in a template argument list or cast
33857 expression is considered to be two separate `>'
33858 tokens. So, change the current token to a `>', but don't
33859 consume it: it will be consumed later when the outer
33860 template argument list (or cast expression) is parsed.
33861 Note that this replacement of `>' for `>>' is necessary
33862 even if we are parsing tentatively: in the tentative
33863 case, after calling
33864 cp_parser_enclosed_template_argument_list we will always
33865 throw away all of the template arguments and the first
33866 closing `>', either because the template argument list
33867 was erroneous or because we are replacing those tokens
33868 with a CPP_TEMPLATE_ID token. The second `>' (which will
33869 not have been thrown away) is needed either to close an
33870 outer template argument list or to complete a new-style
33871 cast. */
33872 cp_token *token = cp_lexer_peek_token (parser->lexer);
33873 token->type = CPP_GREATER;
33875 else if (!saved_greater_than_is_operator_p)
33877 /* If we're in a nested template argument list, the '>>' has
33878 to be a typo for '> >'. We emit the error message, but we
33879 continue parsing and we push a '>' as next token, so that
33880 the argument list will be parsed correctly. Note that the
33881 global source location is still on the token before the
33882 '>>', so we need to say explicitly where we want it. */
33883 cp_token *token = cp_lexer_peek_token (parser->lexer);
33884 gcc_rich_location richloc (token->location);
33885 richloc.add_fixit_replace ("> >");
33886 error_at (&richloc, "%<>>%> should be %<> >%> "
33887 "within a nested template argument list");
33889 token->type = CPP_GREATER;
33891 else
33893 /* If this is not a nested template argument list, the '>>'
33894 is a typo for '>'. Emit an error message and continue.
33895 Same deal about the token location, but here we can get it
33896 right by consuming the '>>' before issuing the diagnostic. */
33897 cp_token *token = cp_lexer_consume_token (parser->lexer);
33898 error_at (token->location,
33899 "spurious %<>>%>, use %<>%> to terminate "
33900 "a template argument list");
33903 /* Similarly for >>= and >=. */
33904 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33905 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33907 cp_token *token = cp_lexer_consume_token (parser->lexer);
33908 gcc_rich_location richloc (token->location);
33909 enum cpp_ttype new_type;
33910 const char *replacement;
33911 if (token->type == CPP_GREATER_EQ)
33913 replacement = "> =";
33914 new_type = CPP_EQ;
33916 else if (!saved_greater_than_is_operator_p)
33918 if (cxx_dialect != cxx98)
33919 replacement = ">> =";
33920 else
33921 replacement = "> > =";
33922 new_type = CPP_GREATER;
33924 else
33926 replacement = "> >=";
33927 new_type = CPP_GREATER_EQ;
33929 richloc.add_fixit_replace (replacement);
33930 error_at (&richloc, "%qs should be %qs to terminate a template "
33931 "argument list",
33932 cpp_type2name (token->type, token->flags), replacement);
33933 token->type = new_type;
33935 else
33936 cp_parser_require_end_of_template_parameter_list (parser);
33937 /* The `>' token might be a greater-than operator again now. */
33938 parser->greater_than_is_operator_p
33939 = saved_greater_than_is_operator_p;
33940 /* Restore the SAVED_SCOPE. */
33941 parser->scope = saved_scope;
33942 parser->qualifying_scope = saved_qualifying_scope;
33943 parser->object_scope = saved_object_scope;
33945 return arguments;
33948 /* MEMBER_FUNCTION is a member function, or a friend. If default
33949 arguments, or the body of the function have not yet been parsed,
33950 parse them now. */
33952 static void
33953 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33955 auto_timevar tv (TV_PARSE_INMETH);
33957 /* If this member is a template, get the underlying
33958 FUNCTION_DECL. */
33959 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33960 member_function = DECL_TEMPLATE_RESULT (member_function);
33962 /* There should not be any class definitions in progress at this
33963 point; the bodies of members are only parsed outside of all class
33964 definitions. */
33965 gcc_assert (parser->num_classes_being_defined == 0);
33966 /* While we're parsing the member functions we might encounter more
33967 classes. We want to handle them right away, but we don't want
33968 them getting mixed up with functions that are currently in the
33969 queue. */
33970 push_unparsed_function_queues (parser);
33972 /* Make sure that any template parameters are in scope. */
33973 maybe_begin_member_template_processing (member_function);
33975 /* If the body of the function has not yet been parsed, parse it
33976 now. Except if the tokens have been purged (PR c++/39751). */
33977 if (DECL_PENDING_INLINE_P (member_function)
33978 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33980 tree function_scope;
33981 cp_token_cache *tokens;
33983 /* The function is no longer pending; we are processing it. */
33984 tokens = DECL_PENDING_INLINE_INFO (member_function);
33985 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33986 DECL_PENDING_INLINE_P (member_function) = 0;
33988 /* If this is a local class, enter the scope of the containing
33989 function. */
33990 function_scope = current_function_decl;
33991 if (function_scope)
33992 push_function_context ();
33994 /* Push the body of the function onto the lexer stack. */
33995 cp_parser_push_lexer_for_tokens (parser, tokens);
33997 /* Let the front end know that we going to be defining this
33998 function. */
33999 start_preparsed_function (member_function, NULL_TREE,
34000 SF_PRE_PARSED | SF_INCLASS_INLINE);
34002 /* #pragma omp declare reduction needs special parsing. */
34003 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
34005 parser->lexer->in_pragma = true;
34006 cp_parser_omp_declare_reduction_exprs (member_function, parser);
34007 finish_function (/*inline_p=*/true);
34008 cp_check_omp_declare_reduction (member_function);
34010 else
34011 /* Now, parse the body of the function. */
34012 cp_parser_function_definition_after_declarator (parser,
34013 /*inline_p=*/true);
34015 /* Leave the scope of the containing function. */
34016 if (function_scope)
34017 pop_function_context ();
34018 cp_parser_pop_lexer (parser);
34021 /* Remove any template parameters from the symbol table. */
34022 maybe_end_member_template_processing ();
34024 /* Restore the queue. */
34025 pop_unparsed_function_queues (parser);
34028 /* If DECL contains any default args, remember it on the unparsed
34029 functions queue. */
34031 static void
34032 cp_parser_save_default_args (cp_parser* parser, tree decl)
34034 tree probe;
34036 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
34037 probe;
34038 probe = TREE_CHAIN (probe))
34039 if (TREE_PURPOSE (probe))
34041 cp_default_arg_entry entry = {current_class_type, decl};
34042 vec_safe_push (unparsed_funs_with_default_args, entry);
34043 break;
34046 /* Remember if there is a noexcept-specifier to post process. */
34047 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
34048 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
34049 vec_safe_push (unparsed_noexcepts, decl);
34051 /* Contracts are deferred. */
34052 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
34053 if (cxx_contract_attribute_p (attr))
34055 vec_safe_push (unparsed_contracts, decl);
34056 break;
34060 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
34061 which is either a FIELD_DECL or PARM_DECL. Parse it and return
34062 the result. For a PARM_DECL, PARMTYPE is the corresponding type
34063 from the parameter-type-list. */
34065 static tree
34066 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
34067 tree default_arg, tree parmtype)
34069 cp_token_cache *tokens;
34070 tree parsed_arg;
34072 if (default_arg == error_mark_node)
34073 return error_mark_node;
34075 /* Push the saved tokens for the default argument onto the parser's
34076 lexer stack. */
34077 tokens = DEFPARSE_TOKENS (default_arg);
34078 cp_parser_push_lexer_for_tokens (parser, tokens);
34080 start_lambda_scope (decl);
34082 /* Parse the default argument. */
34083 parsed_arg = cp_parser_initializer (parser);
34084 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
34085 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
34087 finish_lambda_scope ();
34089 if (parsed_arg == error_mark_node)
34090 cp_parser_skip_to_end_of_statement (parser);
34092 if (!processing_template_decl)
34094 /* In a non-template class, check conversions now. In a template,
34095 we'll wait and instantiate these as needed. */
34096 if (TREE_CODE (decl) == PARM_DECL)
34097 parsed_arg = check_default_argument (parmtype, parsed_arg,
34098 tf_warning_or_error);
34099 else if (maybe_reject_flexarray_init (decl, parsed_arg))
34100 parsed_arg = error_mark_node;
34101 else
34102 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
34105 /* If the token stream has not been completely used up, then
34106 there was extra junk after the end of the default
34107 argument. */
34108 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34110 if (TREE_CODE (decl) == PARM_DECL)
34111 cp_parser_error (parser, "expected %<,%>");
34112 else
34113 cp_parser_error (parser, "expected %<;%>");
34116 /* Revert to the main lexer. */
34117 cp_parser_pop_lexer (parser);
34119 return parsed_arg;
34122 /* FIELD is a non-static data member with an initializer which we saved for
34123 later; parse it now. */
34125 static void
34126 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
34128 tree def;
34130 maybe_begin_member_template_processing (field);
34132 push_unparsed_function_queues (parser);
34133 def = cp_parser_late_parse_one_default_arg (parser, field,
34134 DECL_INITIAL (field),
34135 NULL_TREE);
34136 pop_unparsed_function_queues (parser);
34138 maybe_end_member_template_processing ();
34140 DECL_INITIAL (field) = def;
34143 /* FN is a FUNCTION_DECL which may contains a parameter with an
34144 unparsed DEFERRED_PARSE. Parse the default args now. This function
34145 assumes that the current scope is the scope in which the default
34146 argument should be processed. */
34148 static void
34149 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
34151 unsigned char saved_local_variables_forbidden_p;
34153 /* While we're parsing the default args, we might (due to the
34154 statement expression extension) encounter more classes. We want
34155 to handle them right away, but we don't want them getting mixed
34156 up with default args that are currently in the queue. */
34157 push_unparsed_function_queues (parser);
34159 /* Local variable names (and the `this' keyword) may not appear
34160 in a default argument. */
34161 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
34162 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
34164 push_defarg_context (fn);
34166 begin_scope (sk_function_parms, fn);
34168 /* Gather the PARM_DECLs into a vec so we can keep track of them when
34169 pushdecl clears DECL_CHAIN. */
34170 releasing_vec parms;
34171 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
34172 parmdecl = DECL_CHAIN (parmdecl))
34173 vec_safe_push (parms, parmdecl);
34175 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
34176 for (int i = 0;
34177 parm && parm != void_list_node;
34178 parm = TREE_CHAIN (parm),
34179 ++i)
34181 tree default_arg = TREE_PURPOSE (parm);
34182 tree parsed_arg;
34184 tree parmdecl = parms[i];
34185 pushdecl (parmdecl);
34187 if (!default_arg)
34188 continue;
34190 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
34191 /* This can happen for a friend declaration for a function
34192 already declared with default arguments. */
34193 continue;
34195 parsed_arg
34196 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
34197 default_arg,
34198 TREE_VALUE (parm));
34199 TREE_PURPOSE (parm) = parsed_arg;
34201 /* Update any instantiations we've already created. */
34202 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
34203 TREE_PURPOSE (copy) = parsed_arg;
34206 pop_bindings_and_leave_scope ();
34208 /* Restore DECL_CHAINs after clobbering by pushdecl. */
34209 parm = NULL_TREE;
34210 for (int i = parms->length () - 1; i >= 0; --i)
34212 DECL_CHAIN (parms[i]) = parm;
34213 parm = parms[i];
34216 pop_defarg_context ();
34218 /* Make sure no default arg is missing. */
34219 check_default_args (fn);
34221 /* Restore the state of local_variables_forbidden_p. */
34222 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
34224 /* Restore the queue. */
34225 pop_unparsed_function_queues (parser);
34228 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
34230 sizeof ... ( identifier )
34232 where the 'sizeof' token has already been consumed. */
34234 static tree
34235 cp_parser_sizeof_pack (cp_parser *parser)
34237 /* Consume the `...'. */
34238 cp_lexer_consume_token (parser->lexer);
34239 maybe_warn_variadic_templates ();
34241 matching_parens parens;
34242 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
34243 if (paren)
34244 parens.consume_open (parser);
34245 else
34246 permerror (cp_lexer_peek_token (parser->lexer)->location,
34247 "%<sizeof...%> argument must be surrounded by parentheses");
34249 cp_token *token = cp_lexer_peek_token (parser->lexer);
34250 tree name = cp_parser_identifier (parser);
34251 if (name == error_mark_node)
34252 return error_mark_node;
34253 /* The name is not qualified. */
34254 parser->scope = NULL_TREE;
34255 parser->qualifying_scope = NULL_TREE;
34256 parser->object_scope = NULL_TREE;
34257 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
34258 if (expr == error_mark_node)
34259 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
34260 token->location);
34261 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
34262 expr = TREE_TYPE (expr);
34263 else if (TREE_CODE (expr) == CONST_DECL)
34264 expr = DECL_INITIAL (expr);
34265 expr = make_pack_expansion (expr);
34266 if (expr != error_mark_node)
34267 PACK_EXPANSION_SIZEOF_P (expr) = true;
34269 if (paren)
34270 parens.require_close (parser);
34272 return expr;
34275 /* Parse the operand of `sizeof' (or a similar operator). Returns
34276 either a TYPE or an expression, depending on the form of the
34277 input. The KEYWORD indicates which kind of expression we have
34278 encountered. */
34280 static tree
34281 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
34283 tree expr = NULL_TREE;
34284 const char *saved_message;
34285 const char *saved_message_arg;
34286 bool saved_integral_constant_expression_p;
34287 bool saved_non_integral_constant_expression_p;
34289 /* If it's a `...', then we are computing the length of a parameter
34290 pack. */
34291 if (keyword == RID_SIZEOF
34292 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
34293 return cp_parser_sizeof_pack (parser);
34295 /* Types cannot be defined in a `sizeof' expression. Save away the
34296 old message. */
34297 saved_message = parser->type_definition_forbidden_message;
34298 saved_message_arg = parser->type_definition_forbidden_message_arg;
34299 parser->type_definition_forbidden_message
34300 = G_("types may not be defined in %qs expressions");
34301 parser->type_definition_forbidden_message_arg
34302 = IDENTIFIER_POINTER (ridpointers[keyword]);
34304 /* The restrictions on constant-expressions do not apply inside
34305 sizeof expressions. */
34306 saved_integral_constant_expression_p
34307 = parser->integral_constant_expression_p;
34308 saved_non_integral_constant_expression_p
34309 = parser->non_integral_constant_expression_p;
34310 parser->integral_constant_expression_p = false;
34312 auto cleanup = make_temp_override
34313 (parser->auto_is_implicit_function_template_parm_p, false);
34315 /* Do not actually evaluate the expression. */
34316 ++cp_unevaluated_operand;
34317 ++c_inhibit_evaluation_warnings;
34318 /* If it's a `(', then we might be looking at the type-id
34319 construction. */
34320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34322 tree type = NULL_TREE;
34324 tentative_firewall firewall (parser);
34326 /* We can't be sure yet whether we're looking at a type-id or an
34327 expression. */
34328 cp_parser_parse_tentatively (parser);
34330 matching_parens parens;
34331 parens.consume_open (parser);
34333 /* Note: as a GNU Extension, compound literals are considered
34334 postfix-expressions as they are in C99, so they are valid
34335 arguments to sizeof. See comment in cp_parser_cast_expression
34336 for details. */
34337 if (cp_parser_compound_literal_p (parser))
34338 cp_parser_simulate_error (parser);
34339 else
34341 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
34342 parser->in_type_id_in_expr_p = true;
34343 /* Look for the type-id. */
34344 type = cp_parser_type_id (parser);
34345 /* Look for the closing `)'. */
34346 parens.require_close (parser);
34347 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
34350 /* If all went well, then we're done. */
34351 if (cp_parser_parse_definitely (parser))
34352 expr = type;
34353 else
34355 /* Commit to the tentative_firewall so we get syntax errors. */
34356 cp_parser_commit_to_tentative_parse (parser);
34358 expr = cp_parser_unary_expression (parser);
34361 else
34362 expr = cp_parser_unary_expression (parser);
34364 /* Go back to evaluating expressions. */
34365 --cp_unevaluated_operand;
34366 --c_inhibit_evaluation_warnings;
34368 /* And restore the old one. */
34369 parser->type_definition_forbidden_message = saved_message;
34370 parser->type_definition_forbidden_message_arg = saved_message_arg;
34371 parser->integral_constant_expression_p
34372 = saved_integral_constant_expression_p;
34373 parser->non_integral_constant_expression_p
34374 = saved_non_integral_constant_expression_p;
34376 return expr;
34379 /* If the current declaration has no declarator, return true. */
34381 static bool
34382 cp_parser_declares_only_class_p (cp_parser *parser)
34384 /* If the next token is a `;' or a `,' then there is no
34385 declarator. */
34386 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34387 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
34390 /* Update the DECL_SPECS to reflect the storage class indicated by
34391 KEYWORD. */
34393 static void
34394 cp_parser_set_storage_class (cp_parser *parser,
34395 cp_decl_specifier_seq *decl_specs,
34396 enum rid keyword,
34397 cp_token *token)
34399 cp_storage_class storage_class;
34401 switch (keyword)
34403 case RID_AUTO:
34404 storage_class = sc_auto;
34405 break;
34406 case RID_REGISTER:
34407 storage_class = sc_register;
34408 break;
34409 case RID_STATIC:
34410 storage_class = sc_static;
34411 break;
34412 case RID_EXTERN:
34413 storage_class = sc_extern;
34414 break;
34415 case RID_MUTABLE:
34416 storage_class = sc_mutable;
34417 break;
34418 default:
34419 gcc_unreachable ();
34422 if (parser->in_unbraced_linkage_specification_p)
34424 error_at (token->location, "invalid use of %qD in linkage specification",
34425 ridpointers[keyword]);
34426 return;
34428 else if (decl_specs->storage_class != sc_none)
34430 if (decl_specs->conflicting_specifiers_p)
34431 return;
34432 gcc_rich_location richloc (token->location);
34433 richloc.add_location_if_nearby (*global_dc,
34434 decl_specs->locations[ds_storage_class]);
34435 if (decl_specs->storage_class == storage_class)
34436 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
34437 else
34438 error_at (&richloc,
34439 "%qD specifier conflicts with %qs",
34440 ridpointers[keyword],
34441 cp_storage_class_name[decl_specs->storage_class]);
34442 decl_specs->conflicting_specifiers_p = true;
34443 return;
34446 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
34447 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
34448 && decl_specs->gnu_thread_keyword_p)
34450 pedwarn (decl_specs->locations[ds_thread], 0,
34451 "%<__thread%> before %qD", ridpointers[keyword]);
34454 decl_specs->storage_class = storage_class;
34455 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
34457 /* A storage class specifier cannot be applied alongside a typedef
34458 specifier. If there is a typedef specifier present then set
34459 conflicting_specifiers_p which will trigger an error later
34460 on in grokdeclarator. */
34461 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34462 && !decl_specs->conflicting_specifiers_p)
34464 gcc_rich_location richloc (token->location);
34465 richloc.add_location_if_nearby (*global_dc,
34466 decl_specs->locations[ds_typedef]);
34467 error_at (&richloc,
34468 "%qD specifier conflicts with %<typedef%>",
34469 ridpointers[keyword]);
34470 decl_specs->conflicting_specifiers_p = true;
34474 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
34475 is true, the type is a class or enum definition. */
34477 static void
34478 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
34479 tree type_spec,
34480 cp_token *token,
34481 bool type_definition_p)
34483 decl_specs->any_specifiers_p = true;
34485 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
34486 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
34487 this is what happened. In system headers, we ignore these
34488 declarations so that G++ can work with system headers that are not
34489 C++-safe. */
34490 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34491 && !type_definition_p
34492 && TYPE_P (type_spec)
34493 && (type_spec == boolean_type_node
34494 || type_spec == char8_type_node
34495 || type_spec == char16_type_node
34496 || type_spec == char32_type_node
34497 || extended_float_type_p (type_spec)
34498 || type_spec == wchar_type_node)
34499 && (decl_specs->type
34500 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
34501 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
34502 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
34503 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
34505 decl_specs->redefined_builtin_type = type_spec;
34506 set_and_check_decl_spec_loc (decl_specs,
34507 ds_redefined_builtin_type_spec,
34508 token);
34509 if (!decl_specs->type)
34511 decl_specs->type = type_spec;
34512 decl_specs->type_definition_p = false;
34513 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
34516 else if (decl_specs->type)
34517 decl_specs->multiple_types_p = true;
34518 else
34520 decl_specs->type = type_spec;
34521 decl_specs->type_definition_p = type_definition_p;
34522 decl_specs->redefined_builtin_type = NULL_TREE;
34523 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
34527 /* True iff TOKEN is the GNU keyword __thread. */
34529 static bool
34530 token_is__thread (cp_token *token)
34532 gcc_assert (token->keyword == RID_THREAD);
34533 return id_equal (token->u.value, "__thread");
34536 /* Set the location for a declarator specifier and check if it is
34537 duplicated.
34539 DECL_SPECS is the sequence of declarator specifiers onto which to
34540 set the location.
34542 DS is the single declarator specifier to set which location is to
34543 be set onto the existing sequence of declarators.
34545 LOCATION is the location for the declarator specifier to
34546 consider. */
34548 static void
34549 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
34550 cp_decl_spec ds, cp_token *token)
34552 gcc_assert (ds < ds_last);
34554 if (decl_specs == NULL)
34555 return;
34557 location_t location = token->location;
34559 if (decl_specs->locations[ds] == 0)
34561 decl_specs->locations[ds] = location;
34562 if (ds == ds_thread)
34563 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
34565 else
34567 if (ds == ds_long)
34569 if (decl_specs->locations[ds_long_long] != 0)
34570 error_at (location,
34571 "%<long long long%> is too long for GCC");
34572 else
34574 decl_specs->locations[ds_long_long] = location;
34575 pedwarn_cxx98 (location,
34576 OPT_Wlong_long,
34577 "ISO C++ 1998 does not support %<long long%>");
34580 else if (ds == ds_thread)
34582 bool gnu = token_is__thread (token);
34583 gcc_rich_location richloc (location);
34584 if (gnu != decl_specs->gnu_thread_keyword_p)
34586 richloc.add_range (decl_specs->locations[ds_thread]);
34587 error_at (&richloc,
34588 "both %<__thread%> and %<thread_local%> specified");
34590 else
34592 richloc.add_fixit_remove ();
34593 error_at (&richloc, "duplicate %qD", token->u.value);
34596 else
34598 /* These correspond to cp-tree.h:cp_decl_spec,
34599 changes here should also be reflected there. */
34600 static const char *const decl_spec_names[] = {
34601 "signed",
34602 "unsigned",
34603 "short",
34604 "long",
34605 "const",
34606 "volatile",
34607 "restrict",
34608 "inline",
34609 "virtual",
34610 "explicit",
34611 "friend",
34612 "typedef",
34613 "using",
34614 "constexpr",
34615 "__complex",
34616 "constinit",
34617 "consteval",
34618 "this"
34620 gcc_rich_location richloc (location);
34621 richloc.add_fixit_remove ();
34622 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
34627 /* Return true iff the declarator specifier DS is present in the
34628 sequence of declarator specifiers DECL_SPECS. */
34630 bool
34631 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
34632 cp_decl_spec ds)
34634 gcc_assert (ds < ds_last);
34636 if (decl_specs == NULL)
34637 return false;
34639 return decl_specs->locations[ds] != 0;
34642 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
34643 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
34645 static bool
34646 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
34648 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
34651 /* Issue an error message indicating that TOKEN_DESC was expected.
34652 If KEYWORD is true, it indicated this function is called by
34653 cp_parser_require_keword and the required token can only be
34654 a indicated keyword.
34656 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34657 within any error as the location of an "opening" token matching
34658 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34659 RT_CLOSE_PAREN). */
34661 static void
34662 cp_parser_required_error (cp_parser *parser,
34663 required_token token_desc,
34664 bool keyword,
34665 location_t matching_location)
34667 if (cp_parser_simulate_error (parser))
34668 return;
34670 const char *gmsgid = NULL;
34671 switch (token_desc)
34673 case RT_NEW:
34674 gmsgid = G_("expected %<new%>");
34675 break;
34676 case RT_DELETE:
34677 gmsgid = G_("expected %<delete%>");
34678 break;
34679 case RT_RETURN:
34680 gmsgid = G_("expected %<return%>");
34681 break;
34682 case RT_WHILE:
34683 gmsgid = G_("expected %<while%>");
34684 break;
34685 case RT_EXTERN:
34686 gmsgid = G_("expected %<extern%>");
34687 break;
34688 case RT_STATIC_ASSERT:
34689 gmsgid = G_("expected %<static_assert%>");
34690 break;
34691 case RT_DECLTYPE:
34692 gmsgid = G_("expected %<decltype%>");
34693 break;
34694 case RT_OPERATOR:
34695 gmsgid = G_("expected %<operator%>");
34696 break;
34697 case RT_CLASS:
34698 gmsgid = G_("expected %<class%>");
34699 break;
34700 case RT_TEMPLATE:
34701 gmsgid = G_("expected %<template%>");
34702 break;
34703 case RT_NAMESPACE:
34704 gmsgid = G_("expected %<namespace%>");
34705 break;
34706 case RT_USING:
34707 gmsgid = G_("expected %<using%>");
34708 break;
34709 case RT_ASM:
34710 gmsgid = G_("expected %<asm%>");
34711 break;
34712 case RT_TRY:
34713 gmsgid = G_("expected %<try%>");
34714 break;
34715 case RT_CATCH:
34716 gmsgid = G_("expected %<catch%>");
34717 break;
34718 case RT_THROW:
34719 gmsgid = G_("expected %<throw%>");
34720 break;
34721 case RT_AUTO:
34722 gmsgid = G_("expected %<auto%>");
34723 break;
34724 case RT_LABEL:
34725 gmsgid = G_("expected %<__label__%>");
34726 break;
34727 case RT_AT_TRY:
34728 gmsgid = G_("expected %<@try%>");
34729 break;
34730 case RT_AT_SYNCHRONIZED:
34731 gmsgid = G_("expected %<@synchronized%>");
34732 break;
34733 case RT_AT_THROW:
34734 gmsgid = G_("expected %<@throw%>");
34735 break;
34736 case RT_TRANSACTION_ATOMIC:
34737 gmsgid = G_("expected %<__transaction_atomic%>");
34738 break;
34739 case RT_TRANSACTION_RELAXED:
34740 gmsgid = G_("expected %<__transaction_relaxed%>");
34741 break;
34742 case RT_CO_YIELD:
34743 gmsgid = G_("expected %<co_yield%>");
34744 break;
34745 default:
34746 break;
34749 if (!gmsgid && !keyword)
34751 switch (token_desc)
34753 case RT_SEMICOLON:
34754 gmsgid = G_("expected %<;%>");
34755 break;
34756 case RT_OPEN_PAREN:
34757 gmsgid = G_("expected %<(%>");
34758 break;
34759 case RT_CLOSE_BRACE:
34760 gmsgid = G_("expected %<}%>");
34761 break;
34762 case RT_OPEN_BRACE:
34763 gmsgid = G_("expected %<{%>");
34764 break;
34765 case RT_CLOSE_SQUARE:
34766 gmsgid = G_("expected %<]%>");
34767 break;
34768 case RT_OPEN_SQUARE:
34769 gmsgid = G_("expected %<[%>");
34770 break;
34771 case RT_COMMA:
34772 gmsgid = G_("expected %<,%>");
34773 break;
34774 case RT_SCOPE:
34775 gmsgid = G_("expected %<::%>");
34776 break;
34777 case RT_LESS:
34778 gmsgid = G_("expected %<<%>");
34779 break;
34780 case RT_GREATER:
34781 gmsgid = G_("expected %<>%>");
34782 break;
34783 case RT_EQ:
34784 gmsgid = G_("expected %<=%>");
34785 break;
34786 case RT_ELLIPSIS:
34787 gmsgid = G_("expected %<...%>");
34788 break;
34789 case RT_MULT:
34790 gmsgid = G_("expected %<*%>");
34791 break;
34792 case RT_COMPL:
34793 gmsgid = G_("expected %<~%>");
34794 break;
34795 case RT_COLON:
34796 gmsgid = G_("expected %<:%>");
34797 break;
34798 case RT_COLON_SCOPE:
34799 gmsgid = G_("expected %<:%> or %<::%>");
34800 break;
34801 case RT_CLOSE_PAREN:
34802 gmsgid = G_("expected %<)%>");
34803 break;
34804 case RT_COMMA_CLOSE_PAREN:
34805 gmsgid = G_("expected %<,%> or %<)%>");
34806 break;
34807 case RT_PRAGMA_EOL:
34808 gmsgid = G_("expected end of line");
34809 break;
34810 case RT_NAME:
34811 gmsgid = G_("expected identifier");
34812 break;
34813 case RT_SELECT:
34814 gmsgid = G_("expected selection-statement");
34815 break;
34816 case RT_ITERATION:
34817 gmsgid = G_("expected iteration-statement");
34818 break;
34819 case RT_JUMP:
34820 gmsgid = G_("expected jump-statement");
34821 break;
34822 case RT_CLASS_KEY:
34823 gmsgid = G_("expected class-key");
34824 break;
34825 case RT_CLASS_TYPENAME_TEMPLATE:
34826 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34827 break;
34828 default:
34829 gcc_unreachable ();
34833 if (gmsgid)
34834 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34838 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34839 issue an error message indicating that TOKEN_DESC was expected.
34841 Returns the token consumed, if the token had the appropriate type.
34842 Otherwise, returns NULL.
34844 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34845 within any error as the location of an "opening" token matching
34846 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34847 RT_CLOSE_PAREN). */
34849 static cp_token *
34850 cp_parser_require (cp_parser* parser,
34851 enum cpp_ttype type,
34852 required_token token_desc,
34853 location_t matching_location)
34855 if (cp_lexer_next_token_is (parser->lexer, type))
34856 return cp_lexer_consume_token (parser->lexer);
34857 else
34859 /* Output the MESSAGE -- unless we're parsing tentatively. */
34860 if (!cp_parser_simulate_error (parser))
34861 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34862 matching_location);
34863 return NULL;
34867 /* Skip an entire parameter list from start to finish. The next token must
34868 be the initial "<" of the parameter list. Returns true on success and
34869 false otherwise. */
34871 static bool
34872 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34874 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34875 requires it. */
34876 cp_lexer_consume_token (parser->lexer);
34877 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34880 /* Ensure we are at the end of a template parameter list. If we are, return.
34881 If we are not, something has gone wrong, in which case issue an error and
34882 skip to end of the parameter list. */
34884 static void
34885 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34887 /* Are we ready, yet? If not, issue error message. */
34888 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34889 return;
34891 cp_parser_skip_to_end_of_template_parameter_list (parser);
34894 /* You should only call this function from inside a template parameter list
34895 (i.e. the current token should at least be the initial "<" of the
34896 parameter list). If you are skipping the entire list, it may be better to
34897 use cp_parser_skip_entire_template_parameter_list.
34899 Tokens are skipped until the final ">" is found, or if we see
34900 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34902 Returns true if we successfully reached the end, and false if
34903 something unexpected happened (e.g. end of file). */
34905 static bool
34906 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34908 /* Current level of '< ... >'. */
34909 unsigned level = 0;
34910 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34911 unsigned nesting_depth = 0;
34913 /* Skip tokens until the desired token is found. */
34914 while (true)
34916 /* Peek at the next token. */
34917 switch (cp_lexer_peek_token (parser->lexer)->type)
34919 case CPP_LESS:
34920 if (!nesting_depth)
34921 ++level;
34922 break;
34924 case CPP_RSHIFT:
34925 if (cxx_dialect == cxx98)
34926 /* C++0x views the `>>' operator as two `>' tokens, but
34927 C++98 does not. */
34928 break;
34929 else if (!nesting_depth && level-- == 0)
34931 /* We've hit a `>>' where the first `>' closes the
34932 template argument list, and the second `>' is
34933 spurious. Just consume the `>>' and stop; we've
34934 already produced at least one error. */
34935 cp_lexer_consume_token (parser->lexer);
34936 return false;
34938 /* Fall through for C++0x, so we handle the second `>' in
34939 the `>>'. */
34940 gcc_fallthrough ();
34942 case CPP_GREATER:
34943 if (!nesting_depth && level-- == 0)
34945 /* We've reached the token we want, consume it and stop. */
34946 cp_lexer_consume_token (parser->lexer);
34947 return true;
34949 break;
34951 case CPP_OPEN_PAREN:
34952 case CPP_OPEN_SQUARE:
34953 ++nesting_depth;
34954 break;
34956 case CPP_CLOSE_PAREN:
34957 case CPP_CLOSE_SQUARE:
34958 if (nesting_depth-- == 0)
34959 return false;
34960 break;
34962 case CPP_EOF:
34963 case CPP_PRAGMA_EOL:
34964 case CPP_SEMICOLON:
34965 case CPP_OPEN_BRACE:
34966 case CPP_CLOSE_BRACE:
34967 /* The '>' was probably forgotten, don't look further. */
34968 return false;
34970 default:
34971 break;
34974 /* Consume this token. */
34975 cp_lexer_consume_token (parser->lexer);
34979 /* If the next token is the indicated keyword, consume it. Otherwise,
34980 issue an error message indicating that TOKEN_DESC was expected.
34982 Returns the token consumed, if the token had the appropriate type.
34983 Otherwise, returns NULL. */
34985 static cp_token *
34986 cp_parser_require_keyword (cp_parser* parser,
34987 enum rid keyword,
34988 required_token token_desc)
34990 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34992 if (token && token->keyword != keyword)
34994 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34995 UNKNOWN_LOCATION);
34996 return NULL;
34999 return token;
35002 /* Returns TRUE iff TOKEN is a token that can begin the body of a
35003 function-definition. */
35005 static bool
35006 cp_parser_token_starts_function_definition_p (cp_token* token)
35008 return (/* An ordinary function-body begins with an `{'. */
35009 token->type == CPP_OPEN_BRACE
35010 /* A ctor-initializer begins with a `:'. */
35011 || token->type == CPP_COLON
35012 /* A function-try-block begins with `try'. */
35013 || token->keyword == RID_TRY
35014 /* A function-transaction-block begins with `__transaction_atomic'
35015 or `__transaction_relaxed'. */
35016 || token->keyword == RID_TRANSACTION_ATOMIC
35017 || token->keyword == RID_TRANSACTION_RELAXED
35018 /* The named return value extension begins with `return'. */
35019 || token->keyword == RID_RETURN);
35022 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
35023 definition. */
35025 static bool
35026 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
35028 cp_token *token;
35030 token = cp_lexer_peek_token (parser->lexer);
35031 return (token->type == CPP_OPEN_BRACE
35032 || (token->type == CPP_COLON
35033 && !parser->colon_doesnt_start_class_def_p));
35036 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
35037 C++0x) ending a template-argument. */
35039 static bool
35040 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
35042 cp_token *token;
35044 token = cp_lexer_peek_token (parser->lexer);
35045 return (token->type == CPP_COMMA
35046 || token->type == CPP_GREATER
35047 || token->type == CPP_ELLIPSIS
35048 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
35049 /* For better diagnostics, treat >>= like that too, that
35050 shouldn't appear non-nested in template arguments. */
35051 || token->type == CPP_RSHIFT_EQ);
35054 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
35055 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
35057 static bool
35058 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
35059 size_t n)
35061 cp_token *token;
35063 token = cp_lexer_peek_nth_token (parser->lexer, n);
35064 if (token->type == CPP_LESS)
35065 return true;
35066 /* Check for the sequence `<::' in the original code. It would be lexed as
35067 `[:', where `[' is a digraph, and there is no whitespace before
35068 `:'. */
35069 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
35071 cp_token *token2;
35072 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
35073 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
35074 return true;
35076 return false;
35079 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
35080 or none_type otherwise. */
35082 static enum tag_types
35083 cp_parser_token_is_class_key (cp_token* token)
35085 switch (token->keyword)
35087 case RID_CLASS:
35088 return class_type;
35089 case RID_STRUCT:
35090 return record_type;
35091 case RID_UNION:
35092 return union_type;
35094 default:
35095 return none_type;
35099 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
35100 or none_type otherwise or if the token is null. */
35102 static enum tag_types
35103 cp_parser_token_is_type_parameter_key (cp_token* token)
35105 if (!token)
35106 return none_type;
35108 switch (token->keyword)
35110 case RID_CLASS:
35111 return class_type;
35112 case RID_TYPENAME:
35113 return typename_type;
35115 default:
35116 return none_type;
35120 /* Diagnose redundant enum-keys. */
35122 static void
35123 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
35124 tree type, rid scoped_key)
35126 if (!warn_redundant_tags)
35127 return;
35129 tree type_decl = TYPE_MAIN_DECL (type);
35130 tree name = DECL_NAME (type_decl);
35131 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
35132 push_deferring_access_checks (dk_no_check);
35133 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
35134 pop_deferring_access_checks ();
35136 /* The enum-key is redundant for uses of the TYPE that are not
35137 declarations and for which name lookup returns just the type
35138 itself. */
35139 if (decl != type_decl)
35140 return;
35142 if (scoped_key != RID_CLASS
35143 && scoped_key != RID_STRUCT
35144 && current_lang_name != lang_name_cplusplus
35145 && current_namespace == global_namespace)
35147 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
35148 enum tag in shared C/C++ code in files (such as headers) included
35149 in the main source file. */
35150 const line_map_ordinary *map = NULL;
35151 linemap_resolve_location (line_table, key_loc,
35152 LRK_MACRO_DEFINITION_LOCATION,
35153 &map);
35154 if (!MAIN_FILE_P (map))
35155 return;
35158 gcc_rich_location richloc (key_loc);
35159 richloc.add_fixit_remove (key_loc);
35160 warning_at (&richloc, OPT_Wredundant_tags,
35161 "redundant enum-key %<enum%s%> in reference to %q#T",
35162 (scoped_key == RID_CLASS ? " class"
35163 : scoped_key == RID_STRUCT ? " struct" : ""), type);
35166 /* Describes the set of declarations of a struct, class, or class template
35167 or its specializations. Used for -Wmismatched-tags. */
35169 class class_decl_loc_t
35171 public:
35173 class_decl_loc_t ()
35174 : locvec (), idxdef (), def_class_key ()
35176 locvec.create (4);
35179 /* Constructs an object for a single declaration of a class with
35180 CLASS_KEY at the current location in the current function (or
35181 at another scope). KEY_REDUNDANT is true if the class-key may
35182 be omitted in the current context without an ambiguity with
35183 another symbol with the same name.
35184 DEF_P is true for a class declaration that is a definition.
35185 CURLOC is the associated location. */
35186 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
35187 location_t curloc = input_location)
35188 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
35190 locvec.create (4);
35191 class_key_loc_t ckl (current_function_decl, curloc, class_key,
35192 key_redundant);
35193 locvec.quick_push (ckl);
35196 /* Copy, assign, and destroy the object. Necessary because LOCVEC
35197 isn't safely copyable and assignable and doesn't release storage
35198 on its own. */
35199 class_decl_loc_t (const class_decl_loc_t &rhs)
35200 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
35201 def_class_key (rhs.def_class_key)
35204 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
35206 if (this == &rhs)
35207 return *this;
35208 locvec.release ();
35209 locvec = rhs.locvec.copy ();
35210 idxdef = rhs.idxdef;
35211 def_class_key = rhs.def_class_key;
35212 return *this;
35215 ~class_decl_loc_t ()
35217 locvec.release ();
35220 /* Issues -Wmismatched-tags for a single class. */
35221 void diag_mismatched_tags (tree);
35223 /* Issues -Wmismatched-tags for all classes. */
35224 static void diag_mismatched_tags ();
35226 /* Adds TYPE_DECL to the collection of class decls and diagnoses
35227 redundant tags (if -Wredundant-tags is enabled). */
35228 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
35230 /* Either adds this decl to the collection of class decls
35231 or diagnoses it, whichever is appropriate. */
35232 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
35234 private:
35236 tree function (unsigned i) const
35238 return locvec[i].func;
35241 location_t location (unsigned i) const
35243 return locvec[i].loc;
35246 bool key_redundant (unsigned i) const
35248 return locvec[i].key_redundant;
35251 tag_types class_key (unsigned i) const
35253 return locvec[i].class_key;
35256 /* True if a definition for the class has been seen. */
35257 bool def_p () const
35259 return idxdef < locvec.length ();
35262 /* The location of a single mention of a class type with the given
35263 class-key. */
35264 struct class_key_loc_t
35266 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
35267 : func (func), loc (loc), class_key (key), key_redundant (redundant)
35270 /* The function the type is mentioned in. */
35271 tree func;
35272 /* The exact location. */
35273 location_t loc;
35274 /* The class-key used in the mention of the type. */
35275 tag_types class_key;
35276 /* True when the class-key could be omitted at this location
35277 without an ambiguity with another symbol of the same name. */
35278 bool key_redundant;
35280 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
35281 vec <class_key_loc_t> locvec;
35282 /* LOCVEC index of the definition or UINT_MAX if none exists. */
35283 unsigned idxdef;
35284 /* The class-key the class was last declared with or none_type when
35285 it has been declared with a mismatched key. */
35286 tag_types def_class_key;
35288 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
35289 description above. */
35290 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
35291 static class_to_loc_map_t class2loc;
35294 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
35296 /* Issue an error message if the CLASS_KEY does not match the TYPE.
35297 DEF_P is expected to be set for a definition of class TYPE. DECL_P
35298 is set for a declaration of class TYPE and clear for a reference to
35299 it that is not a declaration of it. */
35301 static void
35302 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
35303 tag_types class_key, tree type, bool def_p,
35304 bool decl_p)
35306 if (type == error_mark_node)
35307 return;
35309 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
35310 if (seen_as_union != (class_key == union_type))
35312 if (permerror (input_location, "%qs tag used in naming %q#T",
35313 class_key == union_type ? "union"
35314 : class_key == record_type ? "struct" : "class",
35315 type))
35316 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
35317 "%q#T was previously declared here", type);
35318 return;
35321 if (!warn_mismatched_tags && !warn_redundant_tags)
35322 return;
35324 /* Only consider the true class-keys below and ignore typename_type,
35325 etc. that are not C++ class-keys. */
35326 if (class_key != class_type
35327 && class_key != record_type
35328 && class_key != union_type)
35329 return;
35331 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
35334 /* Returns the template or specialization of one to which the RECORD_TYPE
35335 TYPE corresponds. */
35337 static tree
35338 specialization_of (tree type)
35340 tree ret = type;
35342 /* Determine the template or its partial specialization to which TYPE
35343 corresponds. */
35344 if (tree ti = most_specialized_partial_spec (type, tf_none))
35345 if (ti != error_mark_node)
35346 ret = TREE_TYPE (TI_TEMPLATE (ti));
35348 if (ret == type)
35349 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
35351 return TYPE_MAIN_DECL (ret);
35355 /* Adds the class TYPE to the collection of class decls and diagnoses
35356 redundant tags (if -Wredundant-tags is enabled).
35357 DEF_P is expected to be set for a definition of class TYPE. DECL_P
35358 is set for a (likely, based on syntactic context) declaration of class
35359 TYPE and clear for a reference to it that is not a declaration of it. */
35361 void
35362 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
35363 tag_types class_key, tree type, bool def_p, bool decl_p)
35365 tree type_decl = TYPE_MAIN_DECL (type);
35366 tree name = DECL_NAME (type_decl);
35367 /* Look up the NAME to see if it unambiguously refers to the TYPE
35368 and set KEY_REDUNDANT if so. */
35369 push_deferring_access_checks (dk_no_check);
35370 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
35371 pop_deferring_access_checks ();
35373 /* The class-key is redundant for uses of the CLASS_TYPE that are
35374 neither definitions of it nor declarations, and for which name
35375 lookup returns just the type itself. */
35376 bool key_redundant = (!def_p && !decl_p
35377 && (decl == type_decl
35378 || TREE_CODE (decl) == TEMPLATE_DECL
35379 || (CLASS_TYPE_P (type)
35380 && TYPE_BEING_DEFINED (type))));
35382 if (key_redundant
35383 && class_key != class_type
35384 && current_lang_name != lang_name_cplusplus
35385 && current_namespace == global_namespace)
35387 /* Avoid issuing the diagnostic for apparently redundant struct
35388 and union class-keys in shared C/C++ code in files (such as
35389 headers) included in the main source file. */
35390 const line_map_ordinary *map = NULL;
35391 linemap_resolve_location (line_table, key_loc,
35392 LRK_MACRO_DEFINITION_LOCATION,
35393 &map);
35394 if (!MAIN_FILE_P (map))
35395 key_redundant = false;
35398 /* Set if a declaration of TYPE has previously been seen or if it must
35399 exist in a precompiled header. */
35400 bool exist;
35401 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
35402 if (!exist)
35404 tree type = TREE_TYPE (type_decl);
35405 if (def_p || !COMPLETE_TYPE_P (type))
35407 /* TYPE_DECL is the first declaration or definition of the type
35408 (outside precompiled headers -- see below). Just create
35409 a new entry for it and return unless it's a declaration
35410 involving a template that may need to be diagnosed by
35411 -Wredundant-tags. */
35412 *rdl = class_decl_loc_t (class_key, false, def_p);
35413 if (TREE_CODE (decl) != TEMPLATE_DECL)
35414 return;
35416 else
35418 /* TYPE was previously defined in some unknown precompiled header.
35419 Simply add a record of its definition at an unknown location and
35420 proceed below to add a reference to it at the current location.
35421 (Declarations in precompiled headers that are not definitions
35422 are ignored.) */
35423 tag_types def_key
35424 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
35425 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
35426 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
35427 exist = true;
35431 /* A prior declaration of TYPE_DECL has been seen. */
35433 if (key_redundant)
35435 gcc_rich_location richloc (key_loc);
35436 richloc.add_fixit_remove (key_loc);
35437 warning_at (&richloc, OPT_Wredundant_tags,
35438 "redundant class-key %qs in reference to %q#T",
35439 class_key == union_type ? "union"
35440 : class_key == record_type ? "struct" : "class",
35441 type);
35444 if (!exist)
35445 /* Do nothing if this is the first declaration of the type. */
35446 return;
35448 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
35449 /* Do nothing if the class-key in this declaration matches
35450 the definition. */
35451 return;
35453 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
35454 def_p);
35457 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
35458 of class decls or diagnoses it, whichever is appropriate. */
35460 void
35461 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
35462 tag_types class_key,
35463 bool redundant,
35464 bool def_p)
35466 /* Reset the CLASS_KEY associated with this type on mismatch.
35467 This is an optimization that lets the diagnostic code skip
35468 over classes that use the same class-key in all declarations. */
35469 if (def_class_key != class_key)
35470 def_class_key = none_type;
35472 /* Set IDXDEF to the index of the vector corresponding to
35473 the definition. */
35474 if (def_p)
35475 idxdef = locvec.length ();
35477 /* Append a record of this declaration to the vector. */
35478 class_key_loc_t ckl (current_function_decl, input_location, class_key,
35479 redundant);
35480 locvec.safe_push (ckl);
35482 if (idxdef == UINT_MAX)
35483 return;
35485 /* As a space optimization diagnose declarations of a class
35486 whose definition has been seen and purge the LOCVEC of
35487 all entries except the definition. */
35488 diag_mismatched_tags (type_decl);
35489 if (idxdef)
35491 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
35492 locvec.release ();
35493 locvec.reserve (2);
35494 locvec.safe_push (ent);
35495 idxdef = 0;
35497 else
35498 /* Pop the entry pushed above for this declaration. */
35499 locvec.pop ();
35502 /* Issues -Wmismatched-tags for a single class. */
35504 void
35505 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
35507 if (!warn_mismatched_tags)
35508 return;
35510 /* Number of uses of the class. */
35511 const unsigned ndecls = locvec.length ();
35513 /* The class (or template) declaration guiding the decisions about
35514 the diagnostic. For ordinary classes it's the same as THIS. For
35515 uses of instantiations of templates other than their declarations
35516 it points to the record for the declaration of the corresponding
35517 primary template or partial specialization. */
35518 class_decl_loc_t *cdlguide = this;
35520 tree type = TREE_TYPE (type_decl);
35521 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
35523 /* For implicit instantiations of a primary template look up
35524 the primary or partial specialization and use it as
35525 the expected class-key rather than using the class-key of
35526 the first reference to the instantiation. The primary must
35527 be (and inevitably is) at index zero. */
35528 tree spec = specialization_of (type);
35529 cdlguide = class2loc.get (spec);
35530 /* It's possible that we didn't find SPEC. Consider:
35532 template<typename T> struct A {
35533 template<typename U> struct W { };
35535 struct A<int>::W<int> w; // #1
35537 where while parsing A and #1 we've stashed
35538 A<T>
35539 A<T>::W<U>
35540 A<int>::W<int>
35541 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
35542 will yield A<int>::W<U> which may be in CLASS2LOC if we had
35543 an A<int> class specialization, but otherwise won't be in it.
35544 So try to look up A<T>::W<U>. */
35545 if (!cdlguide)
35547 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
35548 cdlguide = class2loc.get (spec);
35550 /* Now we really should have found something. */
35551 gcc_assert (cdlguide != NULL);
35553 /* Skip declarations that consistently use the same class-key. */
35554 else if (def_class_key != none_type)
35555 return;
35557 /* Set if a definition for the class has been seen. */
35558 const bool def_p = cdlguide->def_p ();
35560 /* The index of the declaration whose class-key this declaration
35561 is expected to match. It's either the class-key of the class
35562 definition if one exists or the first declaration otherwise. */
35563 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
35565 /* The class-key the class is expected to be declared with: it's
35566 either the key used in its definition or the first declaration
35567 if no definition has been provided.
35568 For implicit instantiations of a primary template it's
35569 the class-key used to declare the primary with. The primary
35570 must be at index zero. */
35571 const tag_types xpect_key = cdlguide->class_key (idxguide);
35573 unsigned idx = 0;
35574 /* Advance IDX to the first declaration that either is not
35575 a definition or that doesn't match the first declaration
35576 if no definition is provided. */
35577 while (class_key (idx) == xpect_key)
35578 if (++idx == ndecls)
35579 return;
35581 /* Save the current function before changing it below. */
35582 tree save_func = current_function_decl;
35583 /* Set the function declaration to print in diagnostic context. */
35584 current_function_decl = function (idx);
35586 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
35587 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
35589 location_t loc = location (idx);
35590 bool key_redundant_p = key_redundant (idx);
35591 auto_diagnostic_group d;
35592 /* Issue a warning for the first mismatched declaration.
35593 Avoid using "%#qT" since the class-key for the same type will
35594 be the same regardless of which one was used in the declaraion. */
35595 if (warning_at (loc, OPT_Wmismatched_tags,
35596 "%qT declared with a mismatched class-key %qs",
35597 type_decl, xmatchkstr))
35599 /* Suggest how to avoid the warning for each instance since
35600 the guidance may be different depending on context. */
35601 inform (loc,
35602 (key_redundant_p
35603 ? G_("remove the class-key or replace it with %qs")
35604 : G_("replace the class-key with %qs")),
35605 xpectkstr);
35607 /* Also point to the first declaration or definition that guided
35608 the decision to issue the warning above. */
35609 inform (cdlguide->location (idxguide),
35610 (def_p
35611 ? G_("%qT defined as %qs here")
35612 : G_("%qT first declared as %qs here")),
35613 type_decl, xpectkstr);
35616 /* Issue warnings for the remaining inconsistent declarations. */
35617 for (unsigned i = idx + 1; i != ndecls; ++i)
35619 tag_types clskey = class_key (i);
35620 /* Skip over the declarations that match either the definition
35621 if one was provided or the first declaration. */
35622 if (clskey == xpect_key)
35623 continue;
35625 loc = location (i);
35626 key_redundant_p = key_redundant (i);
35627 /* Set the function declaration to print in diagnostic context. */
35628 current_function_decl = function (i);
35629 if (warning_at (loc, OPT_Wmismatched_tags,
35630 "%qT declared with a mismatched class-key %qs",
35631 type_decl, xmatchkstr))
35632 /* Suggest how to avoid the warning for each instance since
35633 the guidance may be different depending on context. */
35634 inform (loc,
35635 (key_redundant_p
35636 ? G_("remove the class-key or replace it with %qs")
35637 : G_("replace the class-key with %qs")),
35638 xpectkstr);
35641 /* Restore the current function in case it was replaced above. */
35642 current_function_decl = save_func;
35645 /* Issues -Wmismatched-tags for all classes. Called at the end
35646 of processing a translation unit, after declarations of all class
35647 types and their uses have been recorded. */
35649 void
35650 class_decl_loc_t::diag_mismatched_tags ()
35652 /* CLASS2LOC should be empty if both -Wmismatched-tags and
35653 -Wredundant-tags are disabled. */
35654 gcc_assert (warn_mismatched_tags
35655 || warn_redundant_tags
35656 || class2loc.is_empty ());
35658 /* Save the current function before changing on return. It should
35659 be null at this point. */
35660 temp_override<tree> cleanup (current_function_decl);
35662 if (warn_mismatched_tags)
35664 /* Iterate over the collected class/struct/template declarations. */
35665 typedef class_to_loc_map_t::iterator iter_t;
35666 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
35668 tree type_decl = (*it).first;
35669 class_decl_loc_t &recloc = (*it).second;
35670 recloc.diag_mismatched_tags (type_decl);
35674 class2loc.empty ();
35677 /* Issue an error message if DECL is redeclared with different
35678 access than its original declaration [class.access.spec/3].
35679 This applies to nested classes, nested class templates and
35680 enumerations [class.mem/1]. */
35682 static void
35683 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
35685 if (!decl
35686 || (!(CLASS_TYPE_P (TREE_TYPE (decl))
35687 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
35688 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
35689 return;
35691 if ((TREE_PRIVATE (decl)
35692 != (current_access_specifier == access_private_node))
35693 || (TREE_PROTECTED (decl)
35694 != (current_access_specifier == access_protected_node)))
35695 error_at (location, "%qD redeclared with different access", decl);
35698 /* Look for the `template' keyword, as a syntactic disambiguator.
35699 Return TRUE iff it is present, in which case it will be
35700 consumed. */
35702 static bool
35703 cp_parser_optional_template_keyword (cp_parser *parser)
35705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
35707 /* In C++98 the `template' keyword can only be used within templates;
35708 outside templates the parser can always figure out what is a
35709 template and what is not. In C++11, per the resolution of DR 468,
35710 `template' is allowed in cases where it is not strictly necessary. */
35711 if (!processing_template_decl
35712 && pedantic && cxx_dialect == cxx98)
35714 cp_token *token = cp_lexer_peek_token (parser->lexer);
35715 pedwarn (token->location, OPT_Wpedantic,
35716 "in C++98 %<template%> (as a disambiguator) is only "
35717 "allowed within templates");
35718 /* If this part of the token stream is rescanned, the same
35719 error message would be generated. So, we purge the token
35720 from the stream. */
35721 cp_lexer_purge_token (parser->lexer);
35722 return false;
35724 else
35726 /* Consume the `template' keyword. */
35727 cp_lexer_consume_token (parser->lexer);
35728 return true;
35731 return false;
35734 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
35735 set PARSER->SCOPE, and perform other related actions. */
35737 static void
35738 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
35740 struct tree_check *check_value;
35742 /* Get the stored value. */
35743 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
35744 /* Set the scope from the stored value. */
35745 parser->scope = saved_checks_value (check_value);
35746 parser->qualifying_scope = check_value->qualifying_scope;
35747 parser->object_scope = parser->context->object_type;
35748 parser->context->object_type = NULL_TREE;
35751 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35752 encounter the end of a block before what we were looking for. */
35754 static bool
35755 cp_parser_cache_group (cp_parser *parser,
35756 enum cpp_ttype end,
35757 unsigned depth)
35759 while (true)
35761 cp_token *token = cp_lexer_peek_token (parser->lexer);
35763 /* Abort a parenthesized expression if we encounter a semicolon. */
35764 if ((end == CPP_CLOSE_PAREN || depth == 0)
35765 && token->type == CPP_SEMICOLON)
35766 return true;
35767 /* If we've reached the end of the file, stop. */
35768 if (token->type == CPP_EOF
35769 || (end != CPP_PRAGMA_EOL
35770 && token->type == CPP_PRAGMA_EOL))
35771 return true;
35772 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35773 /* We've hit the end of an enclosing block, so there's been some
35774 kind of syntax error. */
35775 return true;
35777 /* Consume the token. */
35778 cp_lexer_consume_token (parser->lexer);
35779 /* See if it starts a new group. */
35780 if (token->type == CPP_OPEN_BRACE)
35782 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35783 /* In theory this should probably check end == '}', but
35784 cp_parser_save_member_function_body needs it to exit
35785 after either '}' or ')' when called with ')'. */
35786 if (depth == 0)
35787 return false;
35789 else if (token->type == CPP_OPEN_PAREN)
35791 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35792 if (depth == 0 && end == CPP_CLOSE_PAREN)
35793 return false;
35795 else if (token->type == CPP_PRAGMA)
35796 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35797 else if (token->type == end)
35798 return false;
35802 /* Like above, for caching a default argument or NSDMI. Both of these are
35803 terminated by a non-nested comma, but it can be unclear whether or not a
35804 comma is nested in a template argument list unless we do more parsing.
35805 In order to handle this ambiguity, when we encounter a ',' after a '<'
35806 we try to parse what follows as a parameter-declaration-list (in the
35807 case of a default argument) or a member-declarator (in the case of an
35808 NSDMI). If that succeeds, then we stop caching. */
35810 static tree
35811 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35813 unsigned depth = 0;
35814 int maybe_template_id = 0;
35815 cp_token *first_token;
35816 cp_token *token;
35817 tree default_argument;
35819 /* Add tokens until we have processed the entire default
35820 argument. We add the range [first_token, token). */
35821 first_token = cp_lexer_peek_token (parser->lexer);
35822 if (first_token->type == CPP_OPEN_BRACE)
35824 /* For list-initialization, this is straightforward. */
35825 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35826 token = cp_lexer_peek_token (parser->lexer);
35828 else while (true)
35830 bool done = false;
35832 /* Peek at the next token. */
35833 token = cp_lexer_peek_token (parser->lexer);
35834 /* What we do depends on what token we have. */
35835 switch (token->type)
35837 /* In valid code, a default argument must be
35838 immediately followed by a `,' `)', or `...'. */
35839 case CPP_COMMA:
35840 if (depth == 0 && maybe_template_id)
35842 /* If we've seen a '<', we might be in a
35843 template-argument-list. Until Core issue 325 is
35844 resolved, we don't know how this situation ought
35845 to be handled, so try to DTRT. We check whether
35846 what comes after the comma is a valid parameter
35847 declaration list. If it is, then the comma ends
35848 the default argument; otherwise the default
35849 argument continues. */
35850 bool error = false;
35851 cp_token *peek;
35853 /* Set ITALP so cp_parser_parameter_declaration_list
35854 doesn't decide to commit to this parse. */
35855 bool saved_italp = parser->in_template_argument_list_p;
35856 parser->in_template_argument_list_p = true;
35858 cp_parser_parse_tentatively (parser);
35860 if (nsdmi)
35862 /* Parse declarators until we reach a non-comma or
35863 somthing that cannot be an initializer.
35864 Just checking whether we're looking at a single
35865 declarator is insufficient. Consider:
35866 int var = tuple<T,U>::x;
35867 The template parameter 'U' looks exactly like a
35868 declarator. */
35871 int ctor_dtor_or_conv_p;
35872 cp_lexer_consume_token (parser->lexer);
35873 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35874 CP_PARSER_FLAGS_NONE,
35875 &ctor_dtor_or_conv_p,
35876 /*parenthesized_p=*/NULL,
35877 /*member_p=*/true,
35878 /*friend_p=*/false,
35879 /*static_p=*/false);
35880 peek = cp_lexer_peek_token (parser->lexer);
35881 if (cp_parser_error_occurred (parser))
35882 break;
35884 while (peek->type == CPP_COMMA);
35885 /* If we met an '=' or ';' then the original comma
35886 was the end of the NSDMI. Otherwise assume
35887 we're still in the NSDMI. */
35888 error = (peek->type != CPP_EQ
35889 && peek->type != CPP_SEMICOLON);
35891 else
35893 cp_lexer_consume_token (parser->lexer);
35894 begin_scope (sk_function_parms, NULL_TREE);
35895 tree t = cp_parser_parameter_declaration_list
35896 (parser, CP_PARSER_FLAGS_NONE,
35897 /*pending_decls*/nullptr);
35898 if (t == error_mark_node)
35899 error = true;
35900 pop_bindings_and_leave_scope ();
35902 if (!cp_parser_error_occurred (parser) && !error)
35903 done = true;
35904 cp_parser_abort_tentative_parse (parser);
35906 parser->in_template_argument_list_p = saved_italp;
35907 break;
35909 /* FALLTHRU */
35910 case CPP_CLOSE_PAREN:
35911 case CPP_ELLIPSIS:
35912 /* If we run into a non-nested `;', `}', or `]',
35913 then the code is invalid -- but the default
35914 argument is certainly over. */
35915 case CPP_SEMICOLON:
35916 case CPP_CLOSE_BRACE:
35917 case CPP_CLOSE_SQUARE:
35918 if (depth == 0
35919 /* Handle correctly int n = sizeof ... ( p ); */
35920 && token->type != CPP_ELLIPSIS)
35921 done = true;
35922 /* Update DEPTH, if necessary. */
35923 else if (token->type == CPP_CLOSE_PAREN
35924 || token->type == CPP_CLOSE_BRACE
35925 || token->type == CPP_CLOSE_SQUARE)
35926 --depth;
35927 break;
35929 case CPP_OPEN_PAREN:
35930 case CPP_OPEN_SQUARE:
35931 case CPP_OPEN_BRACE:
35932 ++depth;
35933 break;
35935 case CPP_LESS:
35936 if (depth == 0)
35937 /* This might be the comparison operator, or it might
35938 start a template argument list. */
35939 ++maybe_template_id;
35940 break;
35942 case CPP_RSHIFT:
35943 if (cxx_dialect == cxx98)
35944 break;
35945 /* Fall through for C++0x, which treats the `>>'
35946 operator like two `>' tokens in certain
35947 cases. */
35948 gcc_fallthrough ();
35950 case CPP_GREATER:
35951 if (depth == 0)
35953 /* This might be an operator, or it might close a
35954 template argument list. But if a previous '<'
35955 started a template argument list, this will have
35956 closed it, so we can't be in one anymore. */
35957 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35958 if (maybe_template_id < 0)
35959 maybe_template_id = 0;
35961 break;
35963 /* If we run out of tokens, issue an error message. */
35964 case CPP_EOF:
35965 case CPP_PRAGMA_EOL:
35966 error_at (token->location, "file ends in default argument");
35967 return error_mark_node;
35969 case CPP_NAME:
35970 case CPP_SCOPE:
35971 /* In these cases, we should look for template-ids.
35972 For example, if the default argument is
35973 `X<int, double>()', we need to do name lookup to
35974 figure out whether or not `X' is a template; if
35975 so, the `,' does not end the default argument.
35977 That is not yet done. */
35978 break;
35980 default:
35981 break;
35984 /* If we've reached the end, stop. */
35985 if (done)
35986 break;
35988 /* Add the token to the token block. */
35989 token = cp_lexer_consume_token (parser->lexer);
35992 /* Create a DEFERRED_PARSE to represent the unparsed default
35993 argument. */
35994 default_argument = make_node (DEFERRED_PARSE);
35995 DEFPARSE_TOKENS (default_argument)
35996 = cp_token_cache_new (first_token, token);
35997 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35999 return default_argument;
36002 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
36004 location_t
36005 defparse_location (tree default_argument)
36007 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
36008 location_t start = tokens->first->location;
36009 location_t end = tokens->last->location;
36010 return make_location (start, start, end);
36013 /* Begin parsing tentatively. We always save tokens while parsing
36014 tentatively so that if the tentative parsing fails we can restore the
36015 tokens. */
36017 static void
36018 cp_parser_parse_tentatively (cp_parser* parser)
36020 /* Enter a new parsing context. */
36021 parser->context = cp_parser_context_new (parser->context);
36022 /* Begin saving tokens. */
36023 cp_lexer_save_tokens (parser->lexer);
36024 /* In order to avoid repetitive access control error messages,
36025 access checks are queued up until we are no longer parsing
36026 tentatively. */
36027 push_deferring_access_checks (dk_deferred);
36030 /* Commit to the currently active tentative parse. */
36032 static void
36033 cp_parser_commit_to_tentative_parse (cp_parser* parser)
36035 cp_parser_context *context;
36036 cp_lexer *lexer;
36038 /* Mark all of the levels as committed. */
36039 lexer = parser->lexer;
36040 for (context = parser->context; context->next; context = context->next)
36042 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
36043 break;
36044 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
36045 while (!cp_lexer_saving_tokens (lexer))
36046 lexer = lexer->next;
36047 cp_lexer_commit_tokens (lexer);
36051 /* Commit to the topmost currently active tentative parse.
36053 Note that this function shouldn't be called when there are
36054 irreversible side-effects while in a tentative state. For
36055 example, we shouldn't create a permanent entry in the symbol
36056 table, or issue an error message that might not apply if the
36057 tentative parse is aborted. */
36059 static void
36060 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
36062 cp_parser_context *context = parser->context;
36063 cp_lexer *lexer = parser->lexer;
36065 if (context)
36067 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
36068 return;
36069 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
36071 while (!cp_lexer_saving_tokens (lexer))
36072 lexer = lexer->next;
36073 cp_lexer_commit_tokens (lexer);
36077 /* Abort the currently active tentative parse. All consumed tokens
36078 will be rolled back, and no diagnostics will be issued. */
36080 static void
36081 cp_parser_abort_tentative_parse (cp_parser* parser)
36083 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
36084 || errorcount > 0);
36085 cp_parser_simulate_error (parser);
36086 /* Now, pretend that we want to see if the construct was
36087 successfully parsed. */
36088 cp_parser_parse_definitely (parser);
36091 /* Stop parsing tentatively. If a parse error has occurred, restore the
36092 token stream. Otherwise, commit to the tokens we have consumed.
36093 Returns true if no error occurred; false otherwise. */
36095 static bool
36096 cp_parser_parse_definitely (cp_parser* parser)
36098 bool error_occurred;
36099 cp_parser_context *context;
36101 /* Remember whether or not an error occurred, since we are about to
36102 destroy that information. */
36103 error_occurred = cp_parser_error_occurred (parser);
36104 /* Remove the topmost context from the stack. */
36105 context = parser->context;
36106 parser->context = context->next;
36107 /* If no parse errors occurred, commit to the tentative parse. */
36108 if (!error_occurred)
36110 /* Commit to the tokens read tentatively, unless that was
36111 already done. */
36112 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
36113 cp_lexer_commit_tokens (parser->lexer);
36115 pop_to_parent_deferring_access_checks ();
36117 /* Otherwise, if errors occurred, roll back our state so that things
36118 are just as they were before we began the tentative parse. */
36119 else
36121 cp_lexer_rollback_tokens (parser->lexer);
36122 pop_deferring_access_checks ();
36124 /* Add the context to the front of the free list. */
36125 context->next = cp_parser_context_free_list;
36126 cp_parser_context_free_list = context;
36128 return !error_occurred;
36131 /* Returns true if we are parsing tentatively and are not committed to
36132 this tentative parse. */
36134 static bool
36135 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
36137 return (cp_parser_parsing_tentatively (parser)
36138 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
36141 /* Returns nonzero iff an error has occurred during the most recent
36142 tentative parse. */
36144 static bool
36145 cp_parser_error_occurred (cp_parser* parser)
36147 return (cp_parser_parsing_tentatively (parser)
36148 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
36151 /* Returns nonzero if GNU extensions are allowed. */
36153 static bool
36154 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
36156 return parser->allow_gnu_extensions_p;
36159 /* Objective-C++ Productions */
36162 /* Parse an Objective-C expression, which feeds into a primary-expression
36163 above.
36165 objc-expression:
36166 objc-message-expression
36167 objc-string-literal
36168 objc-encode-expression
36169 objc-protocol-expression
36170 objc-selector-expression
36172 Returns a tree representation of the expression. */
36174 static cp_expr
36175 cp_parser_objc_expression (cp_parser* parser)
36177 /* Try to figure out what kind of declaration is present. */
36178 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36180 switch (kwd->type)
36182 case CPP_OPEN_SQUARE:
36183 return cp_parser_objc_message_expression (parser);
36185 case CPP_OBJC_STRING:
36186 kwd = cp_lexer_consume_token (parser->lexer);
36187 return objc_build_string_object (kwd->u.value);
36189 case CPP_KEYWORD:
36190 switch (kwd->keyword)
36192 case RID_AT_ENCODE:
36193 return cp_parser_objc_encode_expression (parser);
36195 case RID_AT_PROTOCOL:
36196 return cp_parser_objc_protocol_expression (parser);
36198 case RID_AT_SELECTOR:
36199 return cp_parser_objc_selector_expression (parser);
36201 default:
36202 break;
36204 /* FALLTHRU */
36205 default:
36206 error_at (kwd->location,
36207 "misplaced %<@%D%> Objective-C++ construct",
36208 kwd->u.value);
36209 cp_parser_skip_to_end_of_block_or_statement (parser);
36212 return error_mark_node;
36215 /* Parse an Objective-C message expression.
36217 objc-message-expression:
36218 [ objc-message-receiver objc-message-args ]
36220 Returns a representation of an Objective-C message. */
36222 static tree
36223 cp_parser_objc_message_expression (cp_parser* parser)
36225 tree receiver, messageargs;
36227 parser->objective_c_message_context_p = true;
36228 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36229 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
36230 receiver = cp_parser_objc_message_receiver (parser);
36231 messageargs = cp_parser_objc_message_args (parser);
36232 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
36233 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36235 tree result = objc_build_message_expr (receiver, messageargs);
36237 /* Construct a location e.g.
36238 [self func1:5]
36239 ^~~~~~~~~~~~~~
36240 ranging from the '[' to the ']', with the caret at the start. */
36241 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
36242 protected_set_expr_location (result, combined_loc);
36244 parser->objective_c_message_context_p = false;
36245 return result;
36248 /* Parse an objc-message-receiver.
36250 objc-message-receiver:
36251 expression
36252 simple-type-specifier
36254 Returns a representation of the type or expression. */
36256 static tree
36257 cp_parser_objc_message_receiver (cp_parser* parser)
36259 tree rcv;
36261 /* An Objective-C message receiver may be either (1) a type
36262 or (2) an expression. */
36263 cp_parser_parse_tentatively (parser);
36264 rcv = cp_parser_expression (parser);
36266 /* If that worked out, fine. */
36267 if (cp_parser_parse_definitely (parser))
36268 return rcv;
36270 cp_parser_parse_tentatively (parser);
36271 rcv = cp_parser_simple_type_specifier (parser,
36272 /*decl_specs=*/NULL,
36273 CP_PARSER_FLAGS_NONE);
36275 if (cp_parser_parse_definitely (parser))
36276 return objc_get_class_reference (rcv);
36278 cp_parser_error (parser, "objective-c++ message receiver expected");
36279 return error_mark_node;
36282 /* Parse the arguments and selectors comprising an Objective-C message.
36284 objc-message-args:
36285 objc-selector
36286 objc-selector-args
36287 objc-selector-args , objc-comma-args
36289 objc-selector-args:
36290 objc-selector [opt] : assignment-expression
36291 objc-selector-args objc-selector [opt] : assignment-expression
36293 objc-comma-args:
36294 assignment-expression
36295 objc-comma-args , assignment-expression
36297 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
36298 selector arguments and TREE_VALUE containing a list of comma
36299 arguments. */
36301 static tree
36302 cp_parser_objc_message_args (cp_parser* parser)
36304 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
36305 bool maybe_unary_selector_p = true;
36306 cp_token *token = cp_lexer_peek_token (parser->lexer);
36308 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36310 tree selector = NULL_TREE, arg;
36312 if (token->type != CPP_COLON)
36313 selector = cp_parser_objc_selector (parser);
36315 /* Detect if we have a unary selector. */
36316 if (maybe_unary_selector_p
36317 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36318 return build_tree_list (selector, NULL_TREE);
36320 maybe_unary_selector_p = false;
36321 cp_parser_require (parser, CPP_COLON, RT_COLON);
36322 arg = cp_parser_assignment_expression (parser);
36324 sel_args
36325 = chainon (sel_args,
36326 build_tree_list (selector, arg));
36328 token = cp_lexer_peek_token (parser->lexer);
36331 /* Handle non-selector arguments, if any. */
36332 while (token->type == CPP_COMMA)
36334 tree arg;
36336 cp_lexer_consume_token (parser->lexer);
36337 arg = cp_parser_assignment_expression (parser);
36339 addl_args
36340 = chainon (addl_args,
36341 build_tree_list (NULL_TREE, arg));
36343 token = cp_lexer_peek_token (parser->lexer);
36346 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
36348 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
36349 return build_tree_list (error_mark_node, error_mark_node);
36352 return build_tree_list (sel_args, addl_args);
36355 /* Parse an Objective-C encode expression.
36357 objc-encode-expression:
36358 @encode objc-typename
36360 Returns an encoded representation of the type argument. */
36362 static cp_expr
36363 cp_parser_objc_encode_expression (cp_parser* parser)
36365 tree type;
36366 cp_token *token;
36367 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36369 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
36370 matching_parens parens;
36371 parens.require_open (parser);
36372 token = cp_lexer_peek_token (parser->lexer);
36373 type = complete_type (cp_parser_type_id (parser));
36374 parens.require_close (parser);
36376 if (!type)
36378 error_at (token->location,
36379 "%<@encode%> must specify a type as an argument");
36380 return error_mark_node;
36383 /* This happens if we find @encode(T) (where T is a template
36384 typename or something dependent on a template typename) when
36385 parsing a template. In that case, we can't compile it
36386 immediately, but we rather create an AT_ENCODE_EXPR which will
36387 need to be instantiated when the template is used.
36389 if (dependent_type_p (type))
36391 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
36392 TREE_READONLY (value) = 1;
36393 return value;
36397 /* Build a location of the form:
36398 @encode(int)
36399 ^~~~~~~~~~~~
36400 with caret==start at the @ token, finishing at the close paren. */
36401 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36403 return cp_expr (objc_build_encode_expr (type), combined_loc);
36406 /* Parse an Objective-C @defs expression. */
36408 static tree
36409 cp_parser_objc_defs_expression (cp_parser *parser)
36411 tree name;
36413 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
36414 matching_parens parens;
36415 parens.require_open (parser);
36416 name = cp_parser_identifier (parser);
36417 parens.require_close (parser);
36419 return objc_get_class_ivars (name);
36422 /* Parse an Objective-C protocol expression.
36424 objc-protocol-expression:
36425 @protocol ( identifier )
36427 Returns a representation of the protocol expression. */
36429 static tree
36430 cp_parser_objc_protocol_expression (cp_parser* parser)
36432 tree proto;
36433 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36435 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36436 matching_parens parens;
36437 parens.require_open (parser);
36438 proto = cp_parser_identifier (parser);
36439 parens.require_close (parser);
36441 /* Build a location of the form:
36442 @protocol(prot)
36443 ^~~~~~~~~~~~~~~
36444 with caret==start at the @ token, finishing at the close paren. */
36445 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36446 tree result = objc_build_protocol_expr (proto);
36447 protected_set_expr_location (result, combined_loc);
36448 return result;
36451 /* Parse an Objective-C selector expression.
36453 objc-selector-expression:
36454 @selector ( objc-method-signature )
36456 objc-method-signature:
36457 objc-selector
36458 objc-selector-seq
36460 objc-selector-seq:
36461 objc-selector :
36462 objc-selector-seq objc-selector :
36464 Returns a representation of the method selector. */
36466 static tree
36467 cp_parser_objc_selector_expression (cp_parser* parser)
36469 tree sel_seq = NULL_TREE;
36470 bool maybe_unary_selector_p = true;
36471 cp_token *token;
36472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36474 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
36475 matching_parens parens;
36476 parens.require_open (parser);
36477 token = cp_lexer_peek_token (parser->lexer);
36479 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
36480 || token->type == CPP_SCOPE)
36482 tree selector = NULL_TREE;
36484 if (token->type != CPP_COLON
36485 || token->type == CPP_SCOPE)
36486 selector = cp_parser_objc_selector (parser);
36488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
36489 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
36491 /* Detect if we have a unary selector. */
36492 if (maybe_unary_selector_p)
36494 sel_seq = selector;
36495 goto finish_selector;
36497 else
36499 cp_parser_error (parser, "expected %<:%>");
36502 maybe_unary_selector_p = false;
36503 token = cp_lexer_consume_token (parser->lexer);
36505 if (token->type == CPP_SCOPE)
36507 sel_seq
36508 = chainon (sel_seq,
36509 build_tree_list (selector, NULL_TREE));
36510 sel_seq
36511 = chainon (sel_seq,
36512 build_tree_list (NULL_TREE, NULL_TREE));
36514 else
36515 sel_seq
36516 = chainon (sel_seq,
36517 build_tree_list (selector, NULL_TREE));
36519 token = cp_lexer_peek_token (parser->lexer);
36522 finish_selector:
36523 parens.require_close (parser);
36526 /* Build a location of the form:
36527 @selector(func)
36528 ^~~~~~~~~~~~~~~
36529 with caret==start at the @ token, finishing at the close paren. */
36530 location_t combined_loc = make_location (loc, loc, parser->lexer);
36531 tree result = objc_build_selector_expr (combined_loc, sel_seq);
36532 /* TODO: objc_build_selector_expr doesn't always honor the location. */
36533 protected_set_expr_location (result, combined_loc);
36534 return result;
36537 /* Parse a list of identifiers.
36539 objc-identifier-list:
36540 identifier
36541 objc-identifier-list , identifier
36543 Returns a TREE_LIST of identifier nodes. */
36545 static tree
36546 cp_parser_objc_identifier_list (cp_parser* parser)
36548 tree identifier;
36549 tree list;
36550 cp_token *sep;
36552 identifier = cp_parser_identifier (parser);
36553 if (identifier == error_mark_node)
36554 return error_mark_node;
36556 list = build_tree_list (NULL_TREE, identifier);
36557 sep = cp_lexer_peek_token (parser->lexer);
36559 while (sep->type == CPP_COMMA)
36561 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36562 identifier = cp_parser_identifier (parser);
36563 if (identifier == error_mark_node)
36564 return list;
36566 list = chainon (list, build_tree_list (NULL_TREE,
36567 identifier));
36568 sep = cp_lexer_peek_token (parser->lexer);
36571 return list;
36574 /* Parse an Objective-C alias declaration.
36576 objc-alias-declaration:
36577 @compatibility_alias identifier identifier ;
36579 This function registers the alias mapping with the Objective-C front end.
36580 It returns nothing. */
36582 static void
36583 cp_parser_objc_alias_declaration (cp_parser* parser)
36585 tree alias, orig;
36587 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
36588 alias = cp_parser_identifier (parser);
36589 orig = cp_parser_identifier (parser);
36590 objc_declare_alias (alias, orig);
36591 cp_parser_consume_semicolon_at_end_of_statement (parser);
36594 /* Parse an Objective-C class forward-declaration.
36596 objc-class-declaration:
36597 @class objc-identifier-list ;
36599 The function registers the forward declarations with the Objective-C
36600 front end. It returns nothing. */
36602 static void
36603 cp_parser_objc_class_declaration (cp_parser* parser)
36605 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
36606 while (true)
36608 tree id;
36610 id = cp_parser_identifier (parser);
36611 if (id == error_mark_node)
36612 break;
36614 objc_declare_class (id);
36616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36617 cp_lexer_consume_token (parser->lexer);
36618 else
36619 break;
36621 cp_parser_consume_semicolon_at_end_of_statement (parser);
36624 /* Parse a list of Objective-C protocol references.
36626 objc-protocol-refs-opt:
36627 objc-protocol-refs [opt]
36629 objc-protocol-refs:
36630 < objc-identifier-list >
36632 Returns a TREE_LIST of identifiers, if any. */
36634 static tree
36635 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
36637 tree protorefs = NULL_TREE;
36639 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
36641 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
36642 protorefs = cp_parser_objc_identifier_list (parser);
36643 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
36646 return protorefs;
36649 /* Parse a Objective-C visibility specification. */
36651 static void
36652 cp_parser_objc_visibility_spec (cp_parser* parser)
36654 cp_token *vis = cp_lexer_peek_token (parser->lexer);
36656 switch (vis->keyword)
36658 case RID_AT_PRIVATE:
36659 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
36660 break;
36661 case RID_AT_PROTECTED:
36662 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
36663 break;
36664 case RID_AT_PUBLIC:
36665 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
36666 break;
36667 case RID_AT_PACKAGE:
36668 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
36669 break;
36670 default:
36671 return;
36674 /* Eat '@private'/'@protected'/'@public'. */
36675 cp_lexer_consume_token (parser->lexer);
36678 /* Parse an Objective-C method type. Return 'true' if it is a class
36679 (+) method, and 'false' if it is an instance (-) method. */
36681 static inline bool
36682 cp_parser_objc_method_type (cp_parser* parser)
36684 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
36685 return true;
36686 else
36687 return false;
36690 /* Parse an Objective-C protocol qualifier. */
36692 static tree
36693 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
36695 tree quals = NULL_TREE, node;
36696 cp_token *token = cp_lexer_peek_token (parser->lexer);
36698 node = token->u.value;
36700 while (node && identifier_p (node)
36701 && (node == ridpointers [(int) RID_IN]
36702 || node == ridpointers [(int) RID_OUT]
36703 || node == ridpointers [(int) RID_INOUT]
36704 || node == ridpointers [(int) RID_BYCOPY]
36705 || node == ridpointers [(int) RID_BYREF]
36706 || node == ridpointers [(int) RID_ONEWAY]))
36708 quals = tree_cons (NULL_TREE, node, quals);
36709 cp_lexer_consume_token (parser->lexer);
36710 token = cp_lexer_peek_token (parser->lexer);
36711 node = token->u.value;
36714 return quals;
36717 /* Parse an Objective-C typename. */
36719 static tree
36720 cp_parser_objc_typename (cp_parser* parser)
36722 tree type_name = NULL_TREE;
36724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36726 tree proto_quals, cp_type = NULL_TREE;
36728 matching_parens parens;
36729 parens.consume_open (parser); /* Eat '('. */
36730 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
36732 /* An ObjC type name may consist of just protocol qualifiers, in which
36733 case the type shall default to 'id'. */
36734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36736 cp_type = cp_parser_type_id (parser);
36738 /* If the type could not be parsed, an error has already
36739 been produced. For error recovery, behave as if it had
36740 not been specified, which will use the default type
36741 'id'. */
36742 if (cp_type == error_mark_node)
36744 cp_type = NULL_TREE;
36745 /* We need to skip to the closing parenthesis as
36746 cp_parser_type_id() does not seem to do it for
36747 us. */
36748 cp_parser_skip_to_closing_parenthesis (parser,
36749 /*recovering=*/true,
36750 /*or_comma=*/false,
36751 /*consume_paren=*/false);
36755 parens.require_close (parser);
36756 type_name = build_tree_list (proto_quals, cp_type);
36759 return type_name;
36762 /* Check to see if TYPE refers to an Objective-C selector name. */
36764 static bool
36765 cp_parser_objc_selector_p (enum cpp_ttype type)
36767 return (type == CPP_NAME || type == CPP_KEYWORD
36768 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36769 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36770 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36771 || type == CPP_XOR || type == CPP_XOR_EQ);
36774 /* Parse an Objective-C selector. */
36776 static tree
36777 cp_parser_objc_selector (cp_parser* parser)
36779 cp_token *token = cp_lexer_consume_token (parser->lexer);
36781 if (!cp_parser_objc_selector_p (token->type))
36783 error_at (token->location, "invalid Objective-C++ selector name");
36784 return error_mark_node;
36787 /* C++ operator names are allowed to appear in ObjC selectors. */
36788 switch (token->type)
36790 case CPP_AND_AND: return get_identifier ("and");
36791 case CPP_AND_EQ: return get_identifier ("and_eq");
36792 case CPP_AND: return get_identifier ("bitand");
36793 case CPP_OR: return get_identifier ("bitor");
36794 case CPP_COMPL: return get_identifier ("compl");
36795 case CPP_NOT: return get_identifier ("not");
36796 case CPP_NOT_EQ: return get_identifier ("not_eq");
36797 case CPP_OR_OR: return get_identifier ("or");
36798 case CPP_OR_EQ: return get_identifier ("or_eq");
36799 case CPP_XOR: return get_identifier ("xor");
36800 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36801 default: return token->u.value;
36805 /* Parse an Objective-C params list. */
36807 static tree
36808 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36810 tree params = NULL_TREE;
36811 bool maybe_unary_selector_p = true;
36812 cp_token *token = cp_lexer_peek_token (parser->lexer);
36814 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36816 tree selector = NULL_TREE, type_name, identifier;
36817 tree parm_attr = NULL_TREE;
36819 if (token->keyword == RID_ATTRIBUTE)
36820 break;
36822 if (token->type != CPP_COLON)
36823 selector = cp_parser_objc_selector (parser);
36825 /* Detect if we have a unary selector. */
36826 if (maybe_unary_selector_p
36827 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36829 params = selector; /* Might be followed by attributes. */
36830 break;
36833 maybe_unary_selector_p = false;
36834 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36836 /* Something went quite wrong. There should be a colon
36837 here, but there is not. Stop parsing parameters. */
36838 break;
36840 type_name = cp_parser_objc_typename (parser);
36841 /* New ObjC allows attributes on parameters too. */
36842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36843 parm_attr = cp_parser_attributes_opt (parser);
36844 identifier = cp_parser_identifier (parser);
36846 params
36847 = chainon (params,
36848 objc_build_keyword_decl (selector,
36849 type_name,
36850 identifier,
36851 parm_attr));
36853 token = cp_lexer_peek_token (parser->lexer);
36856 if (params == NULL_TREE)
36858 cp_parser_error (parser, "objective-c++ method declaration is expected");
36859 return error_mark_node;
36862 /* We allow tail attributes for the method. */
36863 if (token->keyword == RID_ATTRIBUTE)
36865 *attributes = cp_parser_attributes_opt (parser);
36866 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36867 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36868 return params;
36869 cp_parser_error (parser,
36870 "method attributes must be specified at the end");
36871 return error_mark_node;
36874 if (params == NULL_TREE)
36876 cp_parser_error (parser, "objective-c++ method declaration is expected");
36877 return error_mark_node;
36879 return params;
36882 /* Parse the non-keyword Objective-C params. */
36884 static tree
36885 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36886 tree* attributes)
36888 tree params = make_node (TREE_LIST);
36889 cp_token *token = cp_lexer_peek_token (parser->lexer);
36890 *ellipsisp = false; /* Initially, assume no ellipsis. */
36892 while (token->type == CPP_COMMA)
36894 cp_parameter_declarator *parmdecl;
36895 tree parm;
36897 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36898 token = cp_lexer_peek_token (parser->lexer);
36900 if (token->type == CPP_ELLIPSIS)
36902 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36903 *ellipsisp = true;
36904 token = cp_lexer_peek_token (parser->lexer);
36905 break;
36908 /* TODO: parse attributes for tail parameters. */
36909 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36910 false, NULL);
36911 parm = grokdeclarator (parmdecl->declarator,
36912 &parmdecl->decl_specifiers,
36913 PARM, /*initialized=*/0,
36914 /*attrlist=*/NULL);
36916 chainon (params, build_tree_list (NULL_TREE, parm));
36917 token = cp_lexer_peek_token (parser->lexer);
36920 /* We allow tail attributes for the method. */
36921 if (token->keyword == RID_ATTRIBUTE)
36923 if (*attributes == NULL_TREE)
36925 *attributes = cp_parser_attributes_opt (parser);
36926 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36927 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36928 return params;
36930 else
36931 /* We have an error, but parse the attributes, so that we can
36932 carry on. */
36933 *attributes = cp_parser_attributes_opt (parser);
36935 cp_parser_error (parser,
36936 "method attributes must be specified at the end");
36937 return error_mark_node;
36940 return params;
36943 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36945 static void
36946 cp_parser_objc_interstitial_code (cp_parser* parser)
36948 cp_token *token = cp_lexer_peek_token (parser->lexer);
36950 /* If the next token is `extern' and the following token is a string
36951 literal, then we have a linkage specification. */
36952 if (token->keyword == RID_EXTERN
36953 && cp_parser_is_pure_string_literal
36954 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36955 cp_parser_linkage_specification (parser, NULL_TREE);
36956 /* Handle #pragma, if any. */
36957 else if (token->type == CPP_PRAGMA)
36958 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36959 /* Allow stray semicolons. */
36960 else if (token->type == CPP_SEMICOLON)
36961 cp_lexer_consume_token (parser->lexer);
36962 /* Mark methods as optional or required, when building protocols. */
36963 else if (token->keyword == RID_AT_OPTIONAL)
36965 cp_lexer_consume_token (parser->lexer);
36966 objc_set_method_opt (true);
36968 else if (token->keyword == RID_AT_REQUIRED)
36970 cp_lexer_consume_token (parser->lexer);
36971 objc_set_method_opt (false);
36973 else if (token->keyword == RID_NAMESPACE)
36974 cp_parser_namespace_definition (parser);
36975 /* Other stray characters must generate errors. */
36976 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36978 cp_lexer_consume_token (parser->lexer);
36979 error ("stray %qs between Objective-C++ methods",
36980 token->type == CPP_OPEN_BRACE ? "{" : "}");
36982 /* Finally, try to parse a block-declaration, or a function-definition. */
36983 else
36984 cp_parser_block_declaration (parser, /*statement_p=*/false);
36987 /* Parse a method signature. */
36989 static tree
36990 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36992 tree rettype, kwdparms, optparms;
36993 bool ellipsis = false;
36994 bool is_class_method;
36996 is_class_method = cp_parser_objc_method_type (parser);
36997 rettype = cp_parser_objc_typename (parser);
36998 *attributes = NULL_TREE;
36999 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
37000 if (kwdparms == error_mark_node)
37001 return error_mark_node;
37002 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
37003 if (optparms == error_mark_node)
37004 return error_mark_node;
37006 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
37009 static bool
37010 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
37012 tree tattr;
37013 cp_lexer_save_tokens (parser->lexer);
37014 tattr = cp_parser_attributes_opt (parser);
37015 gcc_assert (tattr) ;
37017 /* If the attributes are followed by a method introducer, this is not allowed.
37018 Dump the attributes and flag the situation. */
37019 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
37020 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
37021 return true;
37023 /* Otherwise, the attributes introduce some interstitial code, possibly so
37024 rewind to allow that check. */
37025 cp_lexer_rollback_tokens (parser->lexer);
37026 return false;
37029 /* Parse an Objective-C method prototype list. */
37031 static void
37032 cp_parser_objc_method_prototype_list (cp_parser* parser)
37034 cp_token *token = cp_lexer_peek_token (parser->lexer);
37036 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
37038 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
37040 tree attributes, sig;
37041 bool is_class_method;
37042 if (token->type == CPP_PLUS)
37043 is_class_method = true;
37044 else
37045 is_class_method = false;
37046 sig = cp_parser_objc_method_signature (parser, &attributes);
37047 if (sig == error_mark_node)
37049 cp_parser_skip_to_end_of_block_or_statement (parser);
37050 token = cp_lexer_peek_token (parser->lexer);
37051 continue;
37053 objc_add_method_declaration (is_class_method, sig, attributes);
37054 cp_parser_consume_semicolon_at_end_of_statement (parser);
37056 else if (token->keyword == RID_AT_PROPERTY)
37057 cp_parser_objc_at_property_declaration (parser);
37058 else if (token->keyword == RID_ATTRIBUTE
37059 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
37060 warning_at (cp_lexer_peek_token (parser->lexer)->location,
37061 OPT_Wattributes,
37062 "prefix attributes are ignored for methods");
37063 else
37064 /* Allow for interspersed non-ObjC++ code. */
37065 cp_parser_objc_interstitial_code (parser);
37067 token = cp_lexer_peek_token (parser->lexer);
37070 if (token->type != CPP_EOF)
37071 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37072 else
37073 cp_parser_error (parser, "expected %<@end%>");
37075 objc_finish_interface ();
37078 /* Parse an Objective-C method definition list. */
37080 static void
37081 cp_parser_objc_method_definition_list (cp_parser* parser)
37083 for (;;)
37085 cp_token *token = cp_lexer_peek_token (parser->lexer);
37087 if (token->keyword == RID_AT_END)
37089 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37090 break;
37092 else if (token->type == CPP_EOF)
37094 cp_parser_error (parser, "expected %<@end%>");
37095 break;
37097 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
37099 bool is_class_method = token->type == CPP_PLUS;
37101 push_deferring_access_checks (dk_deferred);
37102 tree attribute;
37103 tree sig = cp_parser_objc_method_signature (parser, &attribute);
37104 if (sig == error_mark_node)
37105 cp_parser_skip_to_end_of_block_or_statement (parser);
37106 else
37108 objc_start_method_definition (is_class_method, sig,
37109 attribute, NULL_TREE);
37111 /* For historical reasons, we accept an optional semicolon. */
37112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37113 cp_lexer_consume_token (parser->lexer);
37115 perform_deferred_access_checks (tf_warning_or_error);
37116 stop_deferring_access_checks ();
37117 tree meth
37118 = cp_parser_function_definition_after_declarator (parser, false);
37119 pop_deferring_access_checks ();
37120 objc_finish_method_definition (meth);
37123 /* The following case will be removed once @synthesize is
37124 completely implemented. */
37125 else if (token->keyword == RID_AT_PROPERTY)
37126 cp_parser_objc_at_property_declaration (parser);
37127 else if (token->keyword == RID_AT_SYNTHESIZE)
37128 cp_parser_objc_at_synthesize_declaration (parser);
37129 else if (token->keyword == RID_AT_DYNAMIC)
37130 cp_parser_objc_at_dynamic_declaration (parser);
37131 else if (token->keyword == RID_ATTRIBUTE
37132 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
37133 warning_at (token->location, OPT_Wattributes,
37134 "prefix attributes are ignored for methods");
37135 else
37136 /* Allow for interspersed non-ObjC++ code. */
37137 cp_parser_objc_interstitial_code (parser);
37140 objc_finish_implementation ();
37143 /* Parse Objective-C ivars. */
37145 static void
37146 cp_parser_objc_class_ivars (cp_parser* parser)
37148 cp_token *token = cp_lexer_peek_token (parser->lexer);
37150 if (token->type != CPP_OPEN_BRACE)
37151 return; /* No ivars specified. */
37153 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
37154 token = cp_lexer_peek_token (parser->lexer);
37156 while (token->type != CPP_CLOSE_BRACE
37157 && token->keyword != RID_AT_END && token->type != CPP_EOF)
37159 cp_decl_specifier_seq declspecs;
37160 int decl_class_or_enum_p;
37161 tree prefix_attributes;
37163 cp_parser_objc_visibility_spec (parser);
37165 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37166 break;
37168 cp_parser_decl_specifier_seq (parser,
37169 CP_PARSER_FLAGS_OPTIONAL,
37170 &declspecs,
37171 &decl_class_or_enum_p);
37173 /* auto, register, static, extern, mutable. */
37174 if (declspecs.storage_class != sc_none)
37176 cp_parser_error (parser, "invalid type for instance variable");
37177 declspecs.storage_class = sc_none;
37180 /* thread_local. */
37181 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37183 cp_parser_error (parser, "invalid type for instance variable");
37184 declspecs.locations[ds_thread] = 0;
37187 /* typedef. */
37188 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37190 cp_parser_error (parser, "invalid type for instance variable");
37191 declspecs.locations[ds_typedef] = 0;
37194 prefix_attributes = declspecs.attributes;
37195 declspecs.attributes = NULL_TREE;
37197 /* Keep going until we hit the `;' at the end of the
37198 declaration. */
37199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37201 tree width = NULL_TREE, attributes, first_attribute, decl;
37202 cp_declarator *declarator = NULL;
37203 int ctor_dtor_or_conv_p;
37205 /* Check for a (possibly unnamed) bitfield declaration. */
37206 token = cp_lexer_peek_token (parser->lexer);
37207 if (token->type == CPP_COLON)
37208 goto eat_colon;
37210 if (token->type == CPP_NAME
37211 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
37212 == CPP_COLON))
37214 /* Get the name of the bitfield. */
37215 declarator = make_id_declarator (NULL_TREE,
37216 cp_parser_identifier (parser),
37217 sfk_none, token->location);
37219 eat_colon:
37220 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
37221 /* Get the width of the bitfield. */
37222 width
37223 = cp_parser_constant_expression (parser);
37225 else
37227 /* Parse the declarator. */
37228 declarator
37229 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37230 CP_PARSER_FLAGS_NONE,
37231 &ctor_dtor_or_conv_p,
37232 /*parenthesized_p=*/NULL,
37233 /*member_p=*/false,
37234 /*friend_p=*/false,
37235 /*static_p=*/false);
37238 /* Look for attributes that apply to the ivar. */
37239 attributes = cp_parser_attributes_opt (parser);
37240 /* Remember which attributes are prefix attributes and
37241 which are not. */
37242 first_attribute = attributes;
37243 /* Combine the attributes. */
37244 attributes = attr_chainon (prefix_attributes, attributes);
37246 if (width)
37247 /* Create the bitfield declaration. */
37248 decl = grokbitfield (declarator, &declspecs,
37249 width, NULL_TREE, attributes);
37250 else
37251 decl = grokfield (declarator, &declspecs,
37252 NULL_TREE, /*init_const_expr_p=*/false,
37253 NULL_TREE, attributes);
37255 /* Add the instance variable. */
37256 if (decl != error_mark_node && decl != NULL_TREE)
37257 objc_add_instance_variable (decl);
37259 /* Reset PREFIX_ATTRIBUTES. */
37260 if (attributes != error_mark_node)
37262 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37263 attributes = TREE_CHAIN (attributes);
37264 if (attributes)
37265 TREE_CHAIN (attributes) = NULL_TREE;
37268 token = cp_lexer_peek_token (parser->lexer);
37270 if (token->type == CPP_COMMA)
37272 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37273 continue;
37275 break;
37278 cp_parser_consume_semicolon_at_end_of_statement (parser);
37279 token = cp_lexer_peek_token (parser->lexer);
37282 if (token->keyword == RID_AT_END)
37283 cp_parser_error (parser, "expected %<}%>");
37285 /* Do not consume the RID_AT_END, so it will be read again as terminating
37286 the @interface of @implementation. */
37287 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
37288 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
37290 /* For historical reasons, we accept an optional semicolon. */
37291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37292 cp_lexer_consume_token (parser->lexer);
37295 /* Parse an Objective-C protocol declaration. */
37297 static void
37298 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
37300 tree proto, protorefs;
37301 cp_token *tok;
37303 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
37304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37306 tok = cp_lexer_peek_token (parser->lexer);
37307 error_at (tok->location, "identifier expected after %<@protocol%>");
37308 cp_parser_consume_semicolon_at_end_of_statement (parser);
37309 return;
37312 /* See if we have a forward declaration or a definition. */
37313 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
37315 /* Try a forward declaration first. */
37316 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
37318 while (true)
37320 tree id;
37322 id = cp_parser_identifier (parser);
37323 if (id == error_mark_node)
37324 break;
37326 objc_declare_protocol (id, attributes);
37328 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37329 cp_lexer_consume_token (parser->lexer);
37330 else
37331 break;
37333 cp_parser_consume_semicolon_at_end_of_statement (parser);
37336 /* Ok, we got a full-fledged definition (or at least should). */
37337 else
37339 proto = cp_parser_identifier (parser);
37340 protorefs = cp_parser_objc_protocol_refs_opt (parser);
37341 objc_start_protocol (proto, protorefs, attributes);
37342 cp_parser_objc_method_prototype_list (parser);
37346 /* Parse an Objective-C superclass or category. */
37348 static void
37349 cp_parser_objc_superclass_or_category (cp_parser *parser,
37350 bool iface_p,
37351 tree *super,
37352 tree *categ, bool *is_class_extension)
37354 cp_token *next = cp_lexer_peek_token (parser->lexer);
37356 *super = *categ = NULL_TREE;
37357 *is_class_extension = false;
37358 if (next->type == CPP_COLON)
37360 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
37361 *super = cp_parser_identifier (parser);
37363 else if (next->type == CPP_OPEN_PAREN)
37365 matching_parens parens;
37366 parens.consume_open (parser); /* Eat '('. */
37368 /* If there is no category name, and this is an @interface, we
37369 have a class extension. */
37370 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37372 *categ = NULL_TREE;
37373 *is_class_extension = true;
37375 else
37376 *categ = cp_parser_identifier (parser);
37378 parens.require_close (parser);
37382 /* Parse an Objective-C class interface. */
37384 static void
37385 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
37387 tree name, super, categ, protos;
37388 bool is_class_extension;
37390 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
37391 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
37392 name = cp_parser_identifier (parser);
37393 if (name == error_mark_node)
37395 /* It's hard to recover because even if valid @interface stuff
37396 is to follow, we can't compile it (or validate it) if we
37397 don't even know which class it refers to. Let's assume this
37398 was a stray '@interface' token in the stream and skip it.
37400 return;
37402 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
37403 &is_class_extension);
37404 protos = cp_parser_objc_protocol_refs_opt (parser);
37406 /* We have either a class or a category on our hands. */
37407 if (categ || is_class_extension)
37408 objc_start_category_interface (name, categ, protos, attributes);
37409 else
37411 objc_start_class_interface (name, nam_loc, super, protos, attributes);
37412 /* Handle instance variable declarations, if any. */
37413 cp_parser_objc_class_ivars (parser);
37414 objc_continue_interface ();
37417 cp_parser_objc_method_prototype_list (parser);
37420 /* Parse an Objective-C class implementation. */
37422 static void
37423 cp_parser_objc_class_implementation (cp_parser* parser)
37425 tree name, super, categ;
37426 bool is_class_extension;
37428 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
37429 name = cp_parser_identifier (parser);
37430 if (name == error_mark_node)
37432 /* It's hard to recover because even if valid @implementation
37433 stuff is to follow, we can't compile it (or validate it) if
37434 we don't even know which class it refers to. Let's assume
37435 this was a stray '@implementation' token in the stream and
37436 skip it.
37438 return;
37440 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
37441 &is_class_extension);
37443 /* We have either a class or a category on our hands. */
37444 if (categ)
37445 objc_start_category_implementation (name, categ);
37446 else
37448 objc_start_class_implementation (name, super);
37449 /* Handle instance variable declarations, if any. */
37450 cp_parser_objc_class_ivars (parser);
37451 objc_continue_implementation ();
37454 cp_parser_objc_method_definition_list (parser);
37457 /* Consume the @end token and finish off the implementation. */
37459 static void
37460 cp_parser_objc_end_implementation (cp_parser* parser)
37462 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37463 objc_finish_implementation ();
37466 /* Parse an Objective-C declaration. */
37468 static void
37469 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
37471 /* Try to figure out what kind of declaration is present. */
37472 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37474 if (attributes)
37475 switch (kwd->keyword)
37477 case RID_AT_ALIAS:
37478 case RID_AT_CLASS:
37479 case RID_AT_END:
37480 error_at (kwd->location, "attributes may not be specified before"
37481 " the %<@%D%> Objective-C++ keyword",
37482 kwd->u.value);
37483 attributes = NULL;
37484 break;
37485 case RID_AT_IMPLEMENTATION:
37486 warning_at (kwd->location, OPT_Wattributes,
37487 "prefix attributes are ignored before %<@%D%>",
37488 kwd->u.value);
37489 attributes = NULL;
37490 default:
37491 break;
37494 switch (kwd->keyword)
37496 case RID_AT_ALIAS:
37497 cp_parser_objc_alias_declaration (parser);
37498 break;
37499 case RID_AT_CLASS:
37500 cp_parser_objc_class_declaration (parser);
37501 break;
37502 case RID_AT_PROTOCOL:
37503 cp_parser_objc_protocol_declaration (parser, attributes);
37504 break;
37505 case RID_AT_INTERFACE:
37506 cp_parser_objc_class_interface (parser, attributes);
37507 break;
37508 case RID_AT_IMPLEMENTATION:
37509 cp_parser_objc_class_implementation (parser);
37510 break;
37511 case RID_AT_END:
37512 cp_parser_objc_end_implementation (parser);
37513 break;
37514 default:
37515 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37516 kwd->u.value);
37517 cp_parser_skip_to_end_of_block_or_statement (parser);
37521 /* Parse an Objective-C try-catch-finally statement.
37523 objc-try-catch-finally-stmt:
37524 @try compound-statement objc-catch-clause-seq [opt]
37525 objc-finally-clause [opt]
37527 objc-catch-clause-seq:
37528 objc-catch-clause objc-catch-clause-seq [opt]
37530 objc-catch-clause:
37531 @catch ( objc-exception-declaration ) compound-statement
37533 objc-finally-clause:
37534 @finally compound-statement
37536 objc-exception-declaration:
37537 parameter-declaration
37538 '...'
37540 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
37542 Returns NULL_TREE.
37544 PS: This function is identical to c_parser_objc_try_catch_finally_statement
37545 for C. Keep them in sync. */
37547 static tree
37548 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
37550 location_t location;
37551 tree stmt;
37553 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
37554 location = cp_lexer_peek_token (parser->lexer)->location;
37555 objc_maybe_warn_exceptions (location);
37556 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
37557 node, lest it get absorbed into the surrounding block. */
37558 stmt = push_stmt_list ();
37559 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37560 objc_begin_try_stmt (location, pop_stmt_list (stmt));
37562 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
37564 cp_parameter_declarator *parm;
37565 tree parameter_declaration = error_mark_node;
37566 bool seen_open_paren = false;
37567 matching_parens parens;
37569 cp_lexer_consume_token (parser->lexer);
37570 if (parens.require_open (parser))
37571 seen_open_paren = true;
37572 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
37574 /* We have "@catch (...)" (where the '...' are literally
37575 what is in the code). Skip the '...'.
37576 parameter_declaration is set to NULL_TREE, and
37577 objc_being_catch_clauses() knows that that means
37578 '...'. */
37579 cp_lexer_consume_token (parser->lexer);
37580 parameter_declaration = NULL_TREE;
37582 else
37584 /* We have "@catch (NSException *exception)" or something
37585 like that. Parse the parameter declaration. */
37586 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
37587 false, NULL);
37588 if (parm == NULL)
37589 parameter_declaration = error_mark_node;
37590 else
37591 parameter_declaration = grokdeclarator (parm->declarator,
37592 &parm->decl_specifiers,
37593 PARM, /*initialized=*/0,
37594 /*attrlist=*/NULL);
37596 if (seen_open_paren)
37597 parens.require_close (parser);
37598 else
37600 /* If there was no open parenthesis, we are recovering from
37601 an error, and we are trying to figure out what mistake
37602 the user has made. */
37604 /* If there is an immediate closing parenthesis, the user
37605 probably forgot the opening one (ie, they typed "@catch
37606 NSException *e)". Parse the closing parenthesis and keep
37607 going. */
37608 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37609 cp_lexer_consume_token (parser->lexer);
37611 /* If these is no immediate closing parenthesis, the user
37612 probably doesn't know that parenthesis are required at
37613 all (ie, they typed "@catch NSException *e"). So, just
37614 forget about the closing parenthesis and keep going. */
37616 objc_begin_catch_clause (parameter_declaration);
37617 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37618 objc_finish_catch_clause ();
37620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
37622 cp_lexer_consume_token (parser->lexer);
37623 location = cp_lexer_peek_token (parser->lexer)->location;
37624 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
37625 node, lest it get absorbed into the surrounding block. */
37626 stmt = push_stmt_list ();
37627 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37628 objc_build_finally_clause (location, pop_stmt_list (stmt));
37631 return objc_finish_try_stmt ();
37634 /* Parse an Objective-C synchronized statement.
37636 objc-synchronized-stmt:
37637 @synchronized ( expression ) compound-statement
37639 Returns NULL_TREE. */
37641 static tree
37642 cp_parser_objc_synchronized_statement (cp_parser *parser)
37644 location_t location;
37645 tree lock, stmt;
37647 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
37649 location = cp_lexer_peek_token (parser->lexer)->location;
37650 objc_maybe_warn_exceptions (location);
37651 matching_parens parens;
37652 parens.require_open (parser);
37653 lock = cp_parser_expression (parser);
37654 parens.require_close (parser);
37656 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
37657 node, lest it get absorbed into the surrounding block. */
37658 stmt = push_stmt_list ();
37659 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37661 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
37664 /* Parse an Objective-C throw statement.
37666 objc-throw-stmt:
37667 @throw assignment-expression [opt] ;
37669 Returns a constructed '@throw' statement. */
37671 static tree
37672 cp_parser_objc_throw_statement (cp_parser *parser)
37674 tree expr = NULL_TREE;
37675 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37677 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
37679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37680 expr = cp_parser_expression (parser);
37682 cp_parser_consume_semicolon_at_end_of_statement (parser);
37684 return objc_build_throw_stmt (loc, expr);
37687 /* Parse an Objective-C statement. */
37689 static tree
37690 cp_parser_objc_statement (cp_parser * parser)
37692 /* Try to figure out what kind of declaration is present. */
37693 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37695 switch (kwd->keyword)
37697 case RID_AT_TRY:
37698 return cp_parser_objc_try_catch_finally_statement (parser);
37699 case RID_AT_SYNCHRONIZED:
37700 return cp_parser_objc_synchronized_statement (parser);
37701 case RID_AT_THROW:
37702 return cp_parser_objc_throw_statement (parser);
37703 default:
37704 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37705 kwd->u.value);
37706 cp_parser_skip_to_end_of_block_or_statement (parser);
37709 return error_mark_node;
37712 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
37713 look ahead to see if an objc keyword follows the attributes. This
37714 is to detect the use of prefix attributes on ObjC @interface and
37715 @protocol. */
37717 static bool
37718 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
37720 cp_lexer_save_tokens (parser->lexer);
37721 tree addon = cp_parser_attributes_opt (parser);
37722 if (addon
37723 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
37725 cp_lexer_commit_tokens (parser->lexer);
37726 if (*attrib)
37727 TREE_CHAIN (*attrib) = addon;
37728 else
37729 *attrib = addon;
37730 return true;
37732 cp_lexer_rollback_tokens (parser->lexer);
37733 return false;
37736 /* This routine is a minimal replacement for
37737 c_parser_struct_declaration () used when parsing the list of
37738 types/names or ObjC++ properties. For example, when parsing the
37739 code
37741 @property (readonly) int a, b, c;
37743 this function is responsible for parsing "int a, int b, int c" and
37744 returning the declarations as CHAIN of DECLs.
37746 TODO: Share this code with cp_parser_objc_class_ivars. It's very
37747 similar parsing. */
37748 static tree
37749 cp_parser_objc_struct_declaration (cp_parser *parser)
37751 tree decls = NULL_TREE;
37752 cp_decl_specifier_seq declspecs;
37753 int decl_class_or_enum_p;
37754 tree prefix_attributes;
37756 cp_parser_decl_specifier_seq (parser,
37757 CP_PARSER_FLAGS_NONE,
37758 &declspecs,
37759 &decl_class_or_enum_p);
37761 if (declspecs.type == error_mark_node)
37762 return error_mark_node;
37764 /* auto, register, static, extern, mutable. */
37765 if (declspecs.storage_class != sc_none)
37767 cp_parser_error (parser, "invalid type for property");
37768 declspecs.storage_class = sc_none;
37771 /* thread_local. */
37772 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37774 cp_parser_error (parser, "invalid type for property");
37775 declspecs.locations[ds_thread] = 0;
37778 /* typedef. */
37779 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37781 cp_parser_error (parser, "invalid type for property");
37782 declspecs.locations[ds_typedef] = 0;
37785 prefix_attributes = declspecs.attributes;
37786 declspecs.attributes = NULL_TREE;
37788 /* Keep going until we hit the `;' at the end of the declaration. */
37789 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37791 tree attributes, first_attribute, decl;
37792 cp_declarator *declarator;
37793 cp_token *token;
37795 /* Parse the declarator. */
37796 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37797 CP_PARSER_FLAGS_NONE,
37798 NULL, NULL, false, false, false);
37800 /* Look for attributes that apply to the ivar. */
37801 attributes = cp_parser_attributes_opt (parser);
37802 /* Remember which attributes are prefix attributes and
37803 which are not. */
37804 first_attribute = attributes;
37805 /* Combine the attributes. */
37806 attributes = attr_chainon (prefix_attributes, attributes);
37808 decl = grokfield (declarator, &declspecs,
37809 NULL_TREE, /*init_const_expr_p=*/false,
37810 NULL_TREE, attributes);
37812 if (decl == error_mark_node || decl == NULL_TREE)
37813 return error_mark_node;
37815 /* Reset PREFIX_ATTRIBUTES. */
37816 if (attributes != error_mark_node)
37818 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37819 attributes = TREE_CHAIN (attributes);
37820 if (attributes)
37821 TREE_CHAIN (attributes) = NULL_TREE;
37824 DECL_CHAIN (decl) = decls;
37825 decls = decl;
37827 token = cp_lexer_peek_token (parser->lexer);
37828 if (token->type == CPP_COMMA)
37830 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37831 continue;
37833 else
37834 break;
37836 return decls;
37839 /* Parse an Objective-C @property declaration. The syntax is:
37841 objc-property-declaration:
37842 '@property' objc-property-attributes[opt] struct-declaration ;
37844 objc-property-attributes:
37845 '(' objc-property-attribute-list ')'
37847 objc-property-attribute-list:
37848 objc-property-attribute
37849 objc-property-attribute-list, objc-property-attribute
37851 objc-property-attribute
37852 'getter' = identifier
37853 'setter' = identifier
37854 'readonly'
37855 'readwrite'
37856 'assign'
37857 'retain'
37858 'copy'
37859 'nonatomic'
37861 For example:
37862 @property NSString *name;
37863 @property (readonly) id object;
37864 @property (retain, nonatomic, getter=getTheName) id name;
37865 @property int a, b, c;
37867 PS: This function is identical to
37868 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37869 static void
37870 cp_parser_objc_at_property_declaration (cp_parser *parser)
37872 /* Parse the optional attribute list.
37874 A list of parsed, but not verified, attributes. */
37875 auto_delete_vec<property_attribute_info> prop_attr_list;
37876 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37878 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37880 /* Parse the optional attribute list... */
37881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37883 /* Eat the '('. */
37884 matching_parens parens;
37885 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37886 parens.consume_open (parser);
37887 bool syntax_error = false;
37889 /* Allow empty @property attribute lists, but with a warning. */
37890 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37891 location_t attr_comb;
37892 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37894 attr_comb = make_location (attr_end, attr_start, attr_end);
37895 warning_at (attr_comb, OPT_Wattributes,
37896 "empty property attribute list");
37898 else
37899 while (true)
37901 cp_token *token = cp_lexer_peek_token (parser->lexer);
37902 attr_start = token->location;
37903 attr_end = get_finish (token->location);
37904 attr_comb = make_location (attr_start, attr_start, attr_end);
37906 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37908 warning_at (attr_comb, OPT_Wattributes,
37909 "missing property attribute");
37910 if (token->type == CPP_CLOSE_PAREN)
37911 break;
37912 cp_lexer_consume_token (parser->lexer);
37913 continue;
37916 tree attr_name = NULL_TREE;
37917 if (identifier_p (token->u.value))
37918 attr_name = token->u.value;
37920 enum rid keyword;
37921 if (token->type == CPP_NAME)
37922 keyword = C_RID_CODE (token->u.value);
37923 else if (token->type == CPP_KEYWORD
37924 && token->keyword == RID_CLASS)
37925 /* Account for accepting the 'class' keyword in this context. */
37926 keyword = RID_CLASS;
37927 else
37928 keyword = RID_MAX; /* By definition, an unknown property. */
37929 cp_lexer_consume_token (parser->lexer);
37931 enum objc_property_attribute_kind prop_kind
37932 = objc_prop_attr_kind_for_rid (keyword);
37933 property_attribute_info *prop
37934 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37935 prop_attr_list.safe_push (prop);
37937 tree meth_name;
37938 switch (prop->prop_kind)
37940 default: break;
37941 case OBJC_PROPERTY_ATTR_UNKNOWN:
37942 if (attr_name)
37943 error_at (attr_start, "unknown property attribute %qE",
37944 attr_name);
37945 else
37946 error_at (attr_start, "unknown property attribute");
37947 prop->parse_error = syntax_error = true;
37948 break;
37950 case OBJC_PROPERTY_ATTR_GETTER:
37951 case OBJC_PROPERTY_ATTR_SETTER:
37952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37954 attr_comb = make_location (attr_end, attr_start, attr_end);
37955 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37956 attr_name);
37957 prop->parse_error = syntax_error = true;
37958 break;
37961 token = cp_lexer_peek_token (parser->lexer);
37962 attr_end = token->location;
37963 cp_lexer_consume_token (parser->lexer); /* eat the = */
37965 if (!cp_parser_objc_selector_p
37966 (cp_lexer_peek_token (parser->lexer)->type))
37968 attr_comb = make_location (attr_end, attr_start, attr_end);
37969 error_at (attr_comb, "expected %qE selector name",
37970 attr_name);
37971 prop->parse_error = syntax_error = true;
37972 break;
37975 /* Get the end of the method name, and consume the name. */
37976 token = cp_lexer_peek_token (parser->lexer);
37977 attr_end = get_finish (token->location);
37978 /* Because method names may contain C++ keywords, we have a
37979 routine to fetch them (this also consumes the token). */
37980 meth_name = cp_parser_objc_selector (parser);
37982 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37986 attr_comb = make_location (attr_end, attr_start,
37987 attr_end);
37988 error_at (attr_comb, "setter method names must"
37989 " terminate with %<:%>");
37990 prop->parse_error = syntax_error = true;
37992 else
37994 attr_end = get_finish (cp_lexer_peek_token
37995 (parser->lexer)->location);
37996 cp_lexer_consume_token (parser->lexer);
37998 attr_comb = make_location (attr_start, attr_start,
37999 attr_end);
38001 else
38002 attr_comb = make_location (attr_start, attr_start,
38003 attr_end);
38004 prop->ident = meth_name;
38005 /* Updated location including all that was successfully
38006 parsed. */
38007 prop->prop_loc = attr_comb;
38008 break;
38011 /* If we see a comma here, then keep going - even if we already
38012 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
38013 this makes a more useful output and avoid spurious warnings
38014 about missing attributes that are, in fact, specified after the
38015 one with the syntax error. */
38016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38017 cp_lexer_consume_token (parser->lexer);
38018 else
38019 break;
38022 if (syntax_error || !parens.require_close (parser))
38023 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38024 /*or_comma=*/false,
38025 /*consume_paren=*/true);
38028 /* 'properties' is the list of properties that we read. Usually a
38029 single one, but maybe more (eg, in "@property int a, b, c;" there
38030 are three).
38031 TODO: Update this parsing so that it accepts (erroneous) bitfields so
38032 that we can issue a meaningful and consistent (between C/C++) error
38033 message from objc_add_property_declaration (). */
38034 tree properties = cp_parser_objc_struct_declaration (parser);
38036 if (properties == error_mark_node)
38037 cp_parser_skip_to_end_of_statement (parser);
38038 else if (properties == NULL_TREE)
38039 cp_parser_error (parser, "expected identifier");
38040 else
38042 /* Comma-separated properties are chained together in reverse order;
38043 add them one by one. */
38044 properties = nreverse (properties);
38045 for (; properties; properties = TREE_CHAIN (properties))
38046 objc_add_property_declaration (loc, copy_node (properties),
38047 prop_attr_list);
38050 cp_parser_consume_semicolon_at_end_of_statement (parser);
38053 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
38055 objc-synthesize-declaration:
38056 @synthesize objc-synthesize-identifier-list ;
38058 objc-synthesize-identifier-list:
38059 objc-synthesize-identifier
38060 objc-synthesize-identifier-list, objc-synthesize-identifier
38062 objc-synthesize-identifier
38063 identifier
38064 identifier = identifier
38066 For example:
38067 @synthesize MyProperty;
38068 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
38070 PS: This function is identical to c_parser_objc_at_synthesize_declaration
38071 for C. Keep them in sync.
38073 static void
38074 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
38076 tree list = NULL_TREE;
38077 location_t loc;
38078 loc = cp_lexer_peek_token (parser->lexer)->location;
38080 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
38081 while (true)
38083 tree property, ivar;
38084 property = cp_parser_identifier (parser);
38085 if (property == error_mark_node)
38087 cp_parser_consume_semicolon_at_end_of_statement (parser);
38088 return;
38090 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
38092 cp_lexer_consume_token (parser->lexer);
38093 ivar = cp_parser_identifier (parser);
38094 if (ivar == error_mark_node)
38096 cp_parser_consume_semicolon_at_end_of_statement (parser);
38097 return;
38100 else
38101 ivar = NULL_TREE;
38102 list = chainon (list, build_tree_list (ivar, property));
38103 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38104 cp_lexer_consume_token (parser->lexer);
38105 else
38106 break;
38108 cp_parser_consume_semicolon_at_end_of_statement (parser);
38109 objc_add_synthesize_declaration (loc, list);
38112 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
38114 objc-dynamic-declaration:
38115 @dynamic identifier-list ;
38117 For example:
38118 @dynamic MyProperty;
38119 @dynamic MyProperty, AnotherProperty;
38121 PS: This function is identical to c_parser_objc_at_dynamic_declaration
38122 for C. Keep them in sync.
38124 static void
38125 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
38127 tree list = NULL_TREE;
38128 location_t loc;
38129 loc = cp_lexer_peek_token (parser->lexer)->location;
38131 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
38132 while (true)
38134 tree property;
38135 property = cp_parser_identifier (parser);
38136 if (property == error_mark_node)
38138 cp_parser_consume_semicolon_at_end_of_statement (parser);
38139 return;
38141 list = chainon (list, build_tree_list (NULL, property));
38142 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38143 cp_lexer_consume_token (parser->lexer);
38144 else
38145 break;
38147 cp_parser_consume_semicolon_at_end_of_statement (parser);
38148 objc_add_dynamic_declaration (loc, list);
38152 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
38154 /* Returns name of the next clause.
38155 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
38156 the token is not consumed. Otherwise appropriate pragma_omp_clause is
38157 returned and the token is consumed. */
38159 static pragma_omp_clause
38160 cp_parser_omp_clause_name (cp_parser *parser)
38162 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
38164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38165 result = PRAGMA_OACC_CLAUSE_AUTO;
38166 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
38167 result = PRAGMA_OMP_CLAUSE_IF;
38168 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38169 result = PRAGMA_OMP_CLAUSE_DEFAULT;
38170 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
38171 result = PRAGMA_OACC_CLAUSE_DELETE;
38172 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38173 result = PRAGMA_OMP_CLAUSE_PRIVATE;
38174 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38175 result = PRAGMA_OMP_CLAUSE_FOR;
38176 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38179 const char *p = IDENTIFIER_POINTER (id);
38181 switch (p[0])
38183 case 'a':
38184 if (!strcmp ("affinity", p))
38185 result = PRAGMA_OMP_CLAUSE_AFFINITY;
38186 else if (!strcmp ("aligned", p))
38187 result = PRAGMA_OMP_CLAUSE_ALIGNED;
38188 else if (!strcmp ("allocate", p))
38189 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
38190 else if (!strcmp ("async", p))
38191 result = PRAGMA_OACC_CLAUSE_ASYNC;
38192 else if (!strcmp ("attach", p))
38193 result = PRAGMA_OACC_CLAUSE_ATTACH;
38194 break;
38195 case 'b':
38196 if (!strcmp ("bind", p))
38197 result = PRAGMA_OMP_CLAUSE_BIND;
38198 break;
38199 case 'c':
38200 if (!strcmp ("collapse", p))
38201 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
38202 else if (!strcmp ("copy", p))
38203 result = PRAGMA_OACC_CLAUSE_COPY;
38204 else if (!strcmp ("copyin", p))
38205 result = PRAGMA_OMP_CLAUSE_COPYIN;
38206 else if (!strcmp ("copyout", p))
38207 result = PRAGMA_OACC_CLAUSE_COPYOUT;
38208 else if (!strcmp ("copyprivate", p))
38209 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
38210 else if (!strcmp ("create", p))
38211 result = PRAGMA_OACC_CLAUSE_CREATE;
38212 break;
38213 case 'd':
38214 if (!strcmp ("defaultmap", p))
38215 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
38216 else if (!strcmp ("depend", p))
38217 result = PRAGMA_OMP_CLAUSE_DEPEND;
38218 else if (!strcmp ("detach", p))
38219 result = PRAGMA_OACC_CLAUSE_DETACH;
38220 else if (!strcmp ("device", p))
38221 result = PRAGMA_OMP_CLAUSE_DEVICE;
38222 else if (!strcmp ("deviceptr", p))
38223 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
38224 else if (!strcmp ("device_resident", p))
38225 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
38226 else if (!strcmp ("device_type", p))
38227 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
38228 else if (!strcmp ("dist_schedule", p))
38229 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
38230 else if (!strcmp ("doacross", p))
38231 result = PRAGMA_OMP_CLAUSE_DOACROSS;
38232 break;
38233 case 'e':
38234 if (!strcmp ("enter", p))
38235 result = PRAGMA_OMP_CLAUSE_ENTER;
38236 break;
38237 case 'f':
38238 if (!strcmp ("filter", p))
38239 result = PRAGMA_OMP_CLAUSE_FILTER;
38240 else if (!strcmp ("final", p))
38241 result = PRAGMA_OMP_CLAUSE_FINAL;
38242 else if (!strcmp ("finalize", p))
38243 result = PRAGMA_OACC_CLAUSE_FINALIZE;
38244 else if (!strcmp ("firstprivate", p))
38245 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
38246 else if (!strcmp ("from", p))
38247 result = PRAGMA_OMP_CLAUSE_FROM;
38248 else if (!strcmp ("full", p))
38249 result = PRAGMA_OMP_CLAUSE_FULL;
38250 break;
38251 case 'g':
38252 if (!strcmp ("gang", p))
38253 result = PRAGMA_OACC_CLAUSE_GANG;
38254 else if (!strcmp ("grainsize", p))
38255 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
38256 break;
38257 case 'h':
38258 if (!strcmp ("has_device_addr", p))
38259 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
38260 else if (!strcmp ("hint", p))
38261 result = PRAGMA_OMP_CLAUSE_HINT;
38262 else if (!strcmp ("host", p))
38263 result = PRAGMA_OACC_CLAUSE_HOST;
38264 break;
38265 case 'i':
38266 if (!strcmp ("if_present", p))
38267 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
38268 else if (!strcmp ("in_reduction", p))
38269 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
38270 else if (!strcmp ("inbranch", p))
38271 result = PRAGMA_OMP_CLAUSE_INBRANCH;
38272 else if (!strcmp ("independent", p))
38273 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
38274 else if (!strcmp ("indirect", p))
38275 result = PRAGMA_OMP_CLAUSE_INDIRECT;
38276 else if (!strcmp ("is_device_ptr", p))
38277 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
38278 break;
38279 case 'l':
38280 if (!strcmp ("lastprivate", p))
38281 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
38282 else if (!strcmp ("linear", p))
38283 result = PRAGMA_OMP_CLAUSE_LINEAR;
38284 else if (!strcmp ("link", p))
38285 result = PRAGMA_OMP_CLAUSE_LINK;
38286 break;
38287 case 'm':
38288 if (!strcmp ("map", p))
38289 result = PRAGMA_OMP_CLAUSE_MAP;
38290 else if (!strcmp ("mergeable", p))
38291 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
38292 break;
38293 case 'n':
38294 if (!strcmp ("no_create", p))
38295 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
38296 else if (!strcmp ("nogroup", p))
38297 result = PRAGMA_OMP_CLAUSE_NOGROUP;
38298 else if (!strcmp ("nohost", p))
38299 result = PRAGMA_OACC_CLAUSE_NOHOST;
38300 else if (!strcmp ("nontemporal", p))
38301 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
38302 else if (!strcmp ("notinbranch", p))
38303 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
38304 else if (!strcmp ("nowait", p))
38305 result = PRAGMA_OMP_CLAUSE_NOWAIT;
38306 else if (!strcmp ("num_gangs", p))
38307 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
38308 else if (!strcmp ("num_tasks", p))
38309 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
38310 else if (!strcmp ("num_teams", p))
38311 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
38312 else if (!strcmp ("num_threads", p))
38313 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
38314 else if (!strcmp ("num_workers", p))
38315 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
38316 break;
38317 case 'o':
38318 if (!strcmp ("ordered", p))
38319 result = PRAGMA_OMP_CLAUSE_ORDERED;
38320 else if (!strcmp ("order", p))
38321 result = PRAGMA_OMP_CLAUSE_ORDER;
38322 break;
38323 case 'p':
38324 if (!strcmp ("parallel", p))
38325 result = PRAGMA_OMP_CLAUSE_PARALLEL;
38326 if (!strcmp ("partial", p))
38327 result = PRAGMA_OMP_CLAUSE_PARTIAL;
38328 else if (!strcmp ("present", p))
38329 result = PRAGMA_OACC_CLAUSE_PRESENT;
38330 else if (!strcmp ("present_or_copy", p)
38331 || !strcmp ("pcopy", p))
38332 result = PRAGMA_OACC_CLAUSE_COPY;
38333 else if (!strcmp ("present_or_copyin", p)
38334 || !strcmp ("pcopyin", p))
38335 result = PRAGMA_OACC_CLAUSE_COPYIN;
38336 else if (!strcmp ("present_or_copyout", p)
38337 || !strcmp ("pcopyout", p))
38338 result = PRAGMA_OACC_CLAUSE_COPYOUT;
38339 else if (!strcmp ("present_or_create", p)
38340 || !strcmp ("pcreate", p))
38341 result = PRAGMA_OACC_CLAUSE_CREATE;
38342 else if (!strcmp ("priority", p))
38343 result = PRAGMA_OMP_CLAUSE_PRIORITY;
38344 else if (!strcmp ("proc_bind", p))
38345 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
38346 break;
38347 case 'r':
38348 if (!strcmp ("reduction", p))
38349 result = PRAGMA_OMP_CLAUSE_REDUCTION;
38350 break;
38351 case 's':
38352 if (!strcmp ("safelen", p))
38353 result = PRAGMA_OMP_CLAUSE_SAFELEN;
38354 else if (!strcmp ("schedule", p))
38355 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
38356 else if (!strcmp ("sections", p))
38357 result = PRAGMA_OMP_CLAUSE_SECTIONS;
38358 else if (!strcmp ("self", p))
38359 result = PRAGMA_OACC_CLAUSE_SELF;
38360 else if (!strcmp ("seq", p))
38361 result = PRAGMA_OACC_CLAUSE_SEQ;
38362 else if (!strcmp ("shared", p))
38363 result = PRAGMA_OMP_CLAUSE_SHARED;
38364 else if (!strcmp ("simd", p))
38365 result = PRAGMA_OMP_CLAUSE_SIMD;
38366 else if (!strcmp ("simdlen", p))
38367 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
38368 break;
38369 case 't':
38370 if (!strcmp ("task_reduction", p))
38371 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
38372 else if (!strcmp ("taskgroup", p))
38373 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
38374 else if (!strcmp ("thread_limit", p))
38375 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
38376 else if (!strcmp ("threads", p))
38377 result = PRAGMA_OMP_CLAUSE_THREADS;
38378 else if (!strcmp ("tile", p))
38379 result = PRAGMA_OACC_CLAUSE_TILE;
38380 else if (!strcmp ("to", p))
38381 result = PRAGMA_OMP_CLAUSE_TO;
38382 break;
38383 case 'u':
38384 if (!strcmp ("uniform", p))
38385 result = PRAGMA_OMP_CLAUSE_UNIFORM;
38386 else if (!strcmp ("untied", p))
38387 result = PRAGMA_OMP_CLAUSE_UNTIED;
38388 else if (!strcmp ("use_device", p))
38389 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
38390 else if (!strcmp ("use_device_addr", p))
38391 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
38392 else if (!strcmp ("use_device_ptr", p))
38393 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
38394 break;
38395 case 'v':
38396 if (!strcmp ("vector", p))
38397 result = PRAGMA_OACC_CLAUSE_VECTOR;
38398 else if (!strcmp ("vector_length", p))
38399 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
38400 break;
38401 case 'w':
38402 if (!strcmp ("wait", p))
38403 result = PRAGMA_OACC_CLAUSE_WAIT;
38404 else if (!strcmp ("worker", p))
38405 result = PRAGMA_OACC_CLAUSE_WORKER;
38406 break;
38410 if (result != PRAGMA_OMP_CLAUSE_NONE)
38411 cp_lexer_consume_token (parser->lexer);
38413 return result;
38416 /* Validate that a clause of the given type does not already exist. */
38418 static void
38419 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
38420 const char *name, location_t location)
38422 if (omp_find_clause (clauses, code))
38423 error_at (location, "too many %qs clauses", name);
38426 /* OpenMP 2.5:
38427 variable-list:
38428 identifier
38429 variable-list , identifier
38431 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
38432 colon). An opening parenthesis will have been consumed by the caller.
38434 If KIND is nonzero, create the appropriate node and install the decl
38435 in OMP_CLAUSE_DECL and add the node to the head of the list.
38437 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
38438 return the list created.
38440 COLON can be NULL if only closing parenthesis should end the list,
38441 or pointer to bool which will receive false if the list is terminated
38442 by closing parenthesis or true if the list is terminated by colon.
38444 The optional ALLOW_DEREF argument is true if list items can use the deref
38445 (->) operator. */
38447 struct omp_dim
38449 tree low_bound, length;
38450 location_t loc;
38451 bool no_colon;
38452 omp_dim (tree lb, tree len, location_t lo, bool nc)
38453 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
38456 static tree
38457 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
38458 tree list, bool *colon,
38459 bool map_lvalue = false)
38461 auto_vec<omp_dim> dims;
38462 bool array_section_p;
38463 cp_token *token;
38464 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38465 if (colon)
38467 parser->colon_corrects_to_scope_p = false;
38468 *colon = false;
38470 while (1)
38472 tree name, decl;
38474 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38475 cp_parser_parse_tentatively (parser);
38476 /* This condition doesn't include OMP_CLAUSE_DEPEND or
38477 OMP_CLAUSE_AFFINITY since lvalue ("locator list") parsing for those is
38478 handled further down the function. */
38479 else if (map_lvalue
38480 && (kind == OMP_CLAUSE_MAP
38481 || kind == OMP_CLAUSE_TO
38482 || kind == OMP_CLAUSE_FROM))
38484 auto s = make_temp_override (parser->omp_array_section_p, true);
38485 token = cp_lexer_peek_token (parser->lexer);
38486 location_t loc = token->location;
38487 decl = cp_parser_assignment_expression (parser);
38489 /* This code rewrites a parsed expression containing various tree
38490 codes used to represent array accesses into a more uniform nest of
38491 OMP_ARRAY_SECTION nodes before it is processed by
38492 semantics.cc:handle_omp_array_sections_1. It might be more
38493 efficient to move this logic to that function instead, analysing
38494 the parsed expression directly rather than this preprocessed
38495 form. */
38496 dims.truncate (0);
38497 if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38499 while (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38501 tree low_bound = TREE_OPERAND (decl, 1);
38502 tree length = TREE_OPERAND (decl, 2);
38503 dims.safe_push (omp_dim (low_bound, length, loc, false));
38504 decl = TREE_OPERAND (decl, 0);
38507 while (TREE_CODE (decl) == ARRAY_REF
38508 || TREE_CODE (decl) == INDIRECT_REF
38509 || TREE_CODE (decl) == COMPOUND_EXPR)
38511 if (REFERENCE_REF_P (decl))
38512 break;
38514 if (TREE_CODE (decl) == COMPOUND_EXPR)
38516 decl = TREE_OPERAND (decl, 1);
38517 STRIP_NOPS (decl);
38519 else if (TREE_CODE (decl) == INDIRECT_REF)
38521 dims.safe_push (omp_dim (integer_zero_node,
38522 integer_one_node, loc, true));
38523 decl = TREE_OPERAND (decl, 0);
38525 else /* ARRAY_REF. */
38527 tree index = TREE_OPERAND (decl, 1);
38528 dims.safe_push (omp_dim (index, integer_one_node, loc,
38529 true));
38530 decl = TREE_OPERAND (decl, 0);
38534 /* Bare references have their own special handling, so remove
38535 the explicit dereference added by convert_from_reference. */
38536 if (REFERENCE_REF_P (decl))
38537 decl = TREE_OPERAND (decl, 0);
38539 for (int i = dims.length () - 1; i >= 0; i--)
38540 decl = grok_omp_array_section (loc, decl, dims[i].low_bound,
38541 dims[i].length);
38543 else if (TREE_CODE (decl) == INDIRECT_REF)
38545 bool ref_p = REFERENCE_REF_P (decl);
38547 /* If we have "*foo" and
38548 - it's an indirection of a reference, "unconvert" it, i.e.
38549 strip the indirection (to just "foo").
38550 - it's an indirection of a pointer, turn it into
38551 "foo[0:1]". */
38552 decl = TREE_OPERAND (decl, 0);
38553 STRIP_NOPS (decl);
38555 if (!ref_p)
38556 decl = grok_omp_array_section (loc, decl, integer_zero_node,
38557 integer_one_node);
38559 else if (TREE_CODE (decl) == ARRAY_REF)
38561 tree idx = TREE_OPERAND (decl, 1);
38563 decl = TREE_OPERAND (decl, 0);
38564 STRIP_NOPS (decl);
38566 decl = grok_omp_array_section (loc, decl, idx, integer_one_node);
38568 else if (TREE_CODE (decl) == NON_LVALUE_EXPR
38569 || CONVERT_EXPR_P (decl))
38570 decl = TREE_OPERAND (decl, 0);
38572 goto build_clause;
38574 token = cp_lexer_peek_token (parser->lexer);
38575 if (kind != 0
38576 && cp_parser_is_keyword (token, RID_THIS))
38578 decl = finish_this_expr ();
38579 if (TREE_CODE (decl) == NON_LVALUE_EXPR
38580 || CONVERT_EXPR_P (decl))
38581 decl = TREE_OPERAND (decl, 0);
38582 cp_lexer_consume_token (parser->lexer);
38584 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
38585 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
38586 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
38588 cp_id_kind idk;
38589 decl = cp_parser_primary_expression (parser, false, false, false,
38590 &idk);
38592 else if (kind == OMP_CLAUSE_DEPEND
38593 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
38594 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38595 || cp_lexer_nth_token_is (parser->lexer, 2,
38596 CPP_CLOSE_PAREN)))
38598 decl = ridpointers[RID_OMP_ALL_MEMORY];
38599 cp_lexer_consume_token (parser->lexer);
38601 else
38603 name = cp_parser_id_expression (parser, /*template_p=*/false,
38604 /*check_dependency_p=*/true,
38605 /*template_p=*/NULL,
38606 /*declarator_p=*/false,
38607 /*optional_p=*/false);
38608 if (name == error_mark_node)
38610 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38611 && cp_parser_simulate_error (parser))
38612 goto depend_lvalue;
38613 goto skip_comma;
38616 if (identifier_p (name))
38617 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38618 else
38619 decl = name;
38620 if (decl == error_mark_node)
38622 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38623 && cp_parser_simulate_error (parser))
38624 goto depend_lvalue;
38625 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38626 token->location);
38629 if (outer_automatic_var_p (decl))
38630 decl = process_outer_var_ref (decl, tf_warning_or_error);
38631 if (decl == error_mark_node)
38633 else if (kind != 0)
38635 switch (kind)
38637 case OMP_CLAUSE__CACHE_:
38638 /* The OpenACC cache directive explicitly only allows "array
38639 elements or subarrays". */
38640 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
38642 error_at (token->location, "expected %<[%>");
38643 decl = error_mark_node;
38644 break;
38646 /* FALLTHROUGH. */
38647 case OMP_CLAUSE_MAP:
38648 case OMP_CLAUSE_FROM:
38649 case OMP_CLAUSE_TO:
38650 start_component_ref:
38651 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38652 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
38654 cpp_ttype ttype
38655 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38656 ? CPP_DOT : CPP_DEREF;
38657 location_t loc
38658 = cp_lexer_peek_token (parser->lexer)->location;
38659 cp_id_kind idk = CP_ID_KIND_NONE;
38660 cp_lexer_consume_token (parser->lexer);
38661 decl = convert_from_reference (decl);
38662 decl = (cp_parser_postfix_dot_deref_expression
38663 (parser, ttype, cp_expr (decl, token->location),
38664 false, &idk, loc));
38666 /* FALLTHROUGH. */
38667 case OMP_CLAUSE_AFFINITY:
38668 case OMP_CLAUSE_DEPEND:
38669 case OMP_CLAUSE_REDUCTION:
38670 case OMP_CLAUSE_IN_REDUCTION:
38671 case OMP_CLAUSE_TASK_REDUCTION:
38672 case OMP_CLAUSE_HAS_DEVICE_ADDR:
38673 array_section_p = false;
38674 dims.truncate (0);
38675 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
38677 location_t loc = UNKNOWN_LOCATION;
38678 tree low_bound = NULL_TREE, length = NULL_TREE;
38679 bool no_colon = false;
38681 parser->colon_corrects_to_scope_p = false;
38682 cp_lexer_consume_token (parser->lexer);
38683 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38685 loc = cp_lexer_peek_token (parser->lexer)->location;
38686 low_bound = cp_parser_expression (parser);
38687 /* Later handling is not prepared to see through these. */
38688 gcc_checking_assert (!location_wrapper_p (low_bound));
38690 if (!colon)
38691 parser->colon_corrects_to_scope_p
38692 = saved_colon_corrects_to_scope_p;
38693 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
38695 length = integer_one_node;
38696 no_colon = true;
38698 else
38700 /* Look for `:'. */
38701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38703 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38704 && cp_parser_simulate_error (parser))
38705 goto depend_lvalue;
38706 goto skip_comma;
38708 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38709 cp_parser_commit_to_tentative_parse (parser);
38710 else
38711 array_section_p = true;
38712 if (!cp_lexer_next_token_is (parser->lexer,
38713 CPP_CLOSE_SQUARE))
38715 length = cp_parser_expression (parser);
38716 /* Later handling is not prepared to see through these. */
38717 gcc_checking_assert (!location_wrapper_p (length));
38720 /* Look for the closing `]'. */
38721 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
38722 RT_CLOSE_SQUARE))
38724 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38725 && cp_parser_simulate_error (parser))
38726 goto depend_lvalue;
38727 goto skip_comma;
38730 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
38733 if ((kind == OMP_CLAUSE_MAP
38734 || kind == OMP_CLAUSE_FROM
38735 || kind == OMP_CLAUSE_TO)
38736 && !array_section_p
38737 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38738 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
38740 for (unsigned i = 0; i < dims.length (); i++)
38742 gcc_assert (dims[i].length == integer_one_node);
38743 decl = build_array_ref (dims[i].loc,
38744 decl, dims[i].low_bound);
38746 goto start_component_ref;
38748 else
38749 for (unsigned i = 0; i < dims.length (); i++)
38750 decl = build_omp_array_section (input_location, decl,
38751 dims[i].low_bound,
38752 dims[i].length);
38753 break;
38754 default:
38755 break;
38758 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38761 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
38762 && cp_parser_simulate_error (parser))
38764 depend_lvalue:
38765 cp_parser_abort_tentative_parse (parser);
38766 decl = cp_parser_assignment_expression (parser, NULL,
38767 false, false);
38769 else
38770 cp_parser_parse_definitely (parser);
38773 build_clause:
38774 tree u = build_omp_clause (token->location, kind);
38775 OMP_CLAUSE_DECL (u) = decl;
38776 OMP_CLAUSE_CHAIN (u) = list;
38777 list = u;
38779 else
38780 list = tree_cons (decl, NULL_TREE, list);
38782 get_comma:
38783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38784 break;
38785 cp_lexer_consume_token (parser->lexer);
38788 if (colon)
38789 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38791 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38793 *colon = true;
38794 cp_parser_require (parser, CPP_COLON, RT_COLON);
38795 return list;
38798 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38800 int ending;
38802 /* Try to resync to an unnested comma. Copied from
38803 cp_parser_parenthesized_expression_list. */
38804 skip_comma:
38805 if (colon)
38806 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38807 ending = cp_parser_skip_to_closing_parenthesis (parser,
38808 /*recovering=*/true,
38809 /*or_comma=*/true,
38810 /*consume_paren=*/true);
38811 if (ending < 0)
38812 goto get_comma;
38815 return list;
38818 /* Similarly, but expect leading and trailing parenthesis. This is a very
38819 common case for omp clauses. */
38821 static tree
38822 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
38823 bool map_lvalue = false)
38825 if (parser->lexer->in_omp_decl_attribute)
38827 if (kind)
38829 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38830 tree u = build_omp_clause (loc, kind);
38831 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
38832 OMP_CLAUSE_CHAIN (u) = list;
38833 return u;
38835 else
38836 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
38837 list);
38840 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38841 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
38842 map_lvalue);
38843 return list;
38846 /* OpenACC 2.0:
38847 copy ( variable-list )
38848 copyin ( variable-list )
38849 copyout ( variable-list )
38850 create ( variable-list )
38851 delete ( variable-list )
38852 present ( variable-list )
38854 OpenACC 2.6:
38855 no_create ( variable-list )
38856 attach ( variable-list )
38857 detach ( variable-list )
38859 OpenACC 2.7:
38860 copyin (readonly : variable-list )
38863 static tree
38864 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38865 tree list)
38867 enum gomp_map_kind kind;
38868 switch (c_kind)
38870 case PRAGMA_OACC_CLAUSE_ATTACH:
38871 kind = GOMP_MAP_ATTACH;
38872 break;
38873 case PRAGMA_OACC_CLAUSE_COPY:
38874 kind = GOMP_MAP_TOFROM;
38875 break;
38876 case PRAGMA_OACC_CLAUSE_COPYIN:
38877 kind = GOMP_MAP_TO;
38878 break;
38879 case PRAGMA_OACC_CLAUSE_COPYOUT:
38880 kind = GOMP_MAP_FROM;
38881 break;
38882 case PRAGMA_OACC_CLAUSE_CREATE:
38883 kind = GOMP_MAP_ALLOC;
38884 break;
38885 case PRAGMA_OACC_CLAUSE_DELETE:
38886 kind = GOMP_MAP_RELEASE;
38887 break;
38888 case PRAGMA_OACC_CLAUSE_DETACH:
38889 kind = GOMP_MAP_DETACH;
38890 break;
38891 case PRAGMA_OACC_CLAUSE_DEVICE:
38892 kind = GOMP_MAP_FORCE_TO;
38893 break;
38894 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38895 kind = GOMP_MAP_DEVICE_RESIDENT;
38896 break;
38897 case PRAGMA_OACC_CLAUSE_LINK:
38898 kind = GOMP_MAP_LINK;
38899 break;
38900 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38901 kind = GOMP_MAP_IF_PRESENT;
38902 break;
38903 case PRAGMA_OACC_CLAUSE_PRESENT:
38904 kind = GOMP_MAP_FORCE_PRESENT;
38905 break;
38906 case PRAGMA_OACC_CLAUSE_SELF:
38907 /* "The 'host' clause is a synonym for the 'self' clause." */
38908 case PRAGMA_OACC_CLAUSE_HOST:
38909 kind = GOMP_MAP_FORCE_FROM;
38910 break;
38911 default:
38912 gcc_unreachable ();
38915 tree nl = list;
38916 bool readonly = false;
38917 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38919 /* Turn on readonly modifier parsing for copyin clause. */
38920 if (c_kind == PRAGMA_OACC_CLAUSE_COPYIN)
38922 cp_token *token = cp_lexer_peek_token (parser->lexer);
38923 if (token->type == CPP_NAME
38924 && !strcmp (IDENTIFIER_POINTER (token->u.value), "readonly")
38925 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
38927 cp_lexer_consume_token (parser->lexer);
38928 cp_lexer_consume_token (parser->lexer);
38929 readonly = true;
38932 nl = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list, NULL,
38933 false);
38936 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38938 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38939 if (readonly)
38940 OMP_CLAUSE_MAP_READONLY (c) = 1;
38943 return nl;
38946 /* OpenACC 2.0:
38947 deviceptr ( variable-list ) */
38949 static tree
38950 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38952 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38953 tree vars, t;
38955 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38956 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38957 variable-list must only allow for pointer variables. */
38958 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38959 for (t = vars; t; t = TREE_CHAIN (t))
38961 tree v = TREE_PURPOSE (t);
38962 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38963 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38964 OMP_CLAUSE_DECL (u) = v;
38965 OMP_CLAUSE_CHAIN (u) = list;
38966 list = u;
38969 return list;
38972 /* OpenACC 2.5:
38973 auto
38974 finalize
38975 independent
38976 nohost
38977 seq */
38979 static tree
38980 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38981 tree list)
38983 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38985 tree c = build_omp_clause (loc, code);
38986 OMP_CLAUSE_CHAIN (c) = list;
38988 return c;
38991 /* OpenACC:
38992 num_gangs ( expression )
38993 num_workers ( expression )
38994 vector_length ( expression ) */
38996 static tree
38997 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38998 const char *str, tree list)
39000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39002 matching_parens parens;
39003 if (!parens.require_open (parser))
39004 return list;
39006 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
39008 if (t == error_mark_node
39009 || !parens.require_close (parser))
39011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39012 /*or_comma=*/false,
39013 /*consume_paren=*/true);
39014 return list;
39017 check_no_duplicate_clause (list, code, str, loc);
39019 tree c = build_omp_clause (loc, code);
39020 OMP_CLAUSE_OPERAND (c, 0) = t;
39021 OMP_CLAUSE_CHAIN (c) = list;
39022 return c;
39025 /* OpenACC:
39027 gang [( gang-arg-list )]
39028 worker [( [num:] int-expr )]
39029 vector [( [length:] int-expr )]
39031 where gang-arg is one of:
39033 [num:] int-expr
39034 static: size-expr
39036 and size-expr may be:
39039 int-expr
39042 static tree
39043 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
39044 omp_clause_code kind,
39045 const char *str, tree list)
39047 const char *id = "num";
39048 cp_lexer *lexer = parser->lexer;
39049 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
39051 if (kind == OMP_CLAUSE_VECTOR)
39052 id = "length";
39054 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
39056 matching_parens parens;
39057 parens.consume_open (parser);
39061 cp_token *next = cp_lexer_peek_token (lexer);
39062 int idx = 0;
39064 /* Gang static argument. */
39065 if (kind == OMP_CLAUSE_GANG
39066 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
39068 cp_lexer_consume_token (lexer);
39070 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39071 goto cleanup_error;
39073 idx = 1;
39074 if (ops[idx] != NULL)
39076 cp_parser_error (parser, "too many %<static%> arguments");
39077 goto cleanup_error;
39080 /* Check for the '*' argument. */
39081 if (cp_lexer_next_token_is (lexer, CPP_MULT)
39082 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
39083 || cp_lexer_nth_token_is (parser->lexer, 2,
39084 CPP_CLOSE_PAREN)))
39086 cp_lexer_consume_token (lexer);
39087 ops[idx] = integer_minus_one_node;
39089 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
39091 cp_lexer_consume_token (lexer);
39092 continue;
39094 else break;
39097 /* Worker num: argument and vector length: arguments. */
39098 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
39099 && id_equal (next->u.value, id)
39100 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
39102 cp_lexer_consume_token (lexer); /* id */
39103 cp_lexer_consume_token (lexer); /* ':' */
39106 /* Now collect the actual argument. */
39107 if (ops[idx] != NULL_TREE)
39109 cp_parser_error (parser, "unexpected argument");
39110 goto cleanup_error;
39113 tree expr = cp_parser_assignment_expression (parser, NULL, false,
39114 false);
39115 if (expr == error_mark_node)
39116 goto cleanup_error;
39118 mark_exp_read (expr);
39119 ops[idx] = expr;
39121 if (kind == OMP_CLAUSE_GANG
39122 && cp_lexer_next_token_is (lexer, CPP_COMMA))
39124 cp_lexer_consume_token (lexer);
39125 continue;
39127 break;
39129 while (1);
39131 if (!parens.require_close (parser))
39132 goto cleanup_error;
39135 check_no_duplicate_clause (list, kind, str, loc);
39137 c = build_omp_clause (loc, kind);
39139 if (ops[1])
39140 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
39142 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
39143 OMP_CLAUSE_CHAIN (c) = list;
39145 return c;
39147 cleanup_error:
39148 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39149 return list;
39152 /* OpenACC 2.0:
39153 tile ( size-expr-list ) */
39155 static tree
39156 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
39158 tree c, expr = error_mark_node;
39159 tree tile = NULL_TREE;
39161 /* Collapse and tile are mutually exclusive. (The spec doesn't say
39162 so, but the spec authors never considered such a case and have
39163 differing opinions on what it might mean, including 'not
39164 allowed'.) */
39165 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
39166 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
39167 clause_loc);
39169 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39170 return list;
39174 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
39175 return list;
39177 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
39178 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
39179 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
39181 cp_lexer_consume_token (parser->lexer);
39182 expr = integer_zero_node;
39184 else
39185 expr = cp_parser_constant_expression (parser);
39187 tile = tree_cons (NULL_TREE, expr, tile);
39189 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
39191 /* Consume the trailing ')'. */
39192 cp_lexer_consume_token (parser->lexer);
39194 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
39195 tile = nreverse (tile);
39196 OMP_CLAUSE_TILE_LIST (c) = tile;
39197 OMP_CLAUSE_CHAIN (c) = list;
39198 return c;
39201 /* OpenACC 2.0
39202 Parse wait clause or directive parameters. */
39204 static tree
39205 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
39207 vec<tree, va_gc> *args;
39208 tree t, args_tree;
39210 args = cp_parser_parenthesized_expression_list (parser, non_attr,
39211 /*cast_p=*/false,
39212 /*allow_expansion_p=*/true,
39213 /*non_constant_p=*/NULL);
39215 if (args == NULL || args->length () == 0)
39217 if (args != NULL)
39219 cp_parser_error (parser, "expected integer expression list");
39220 release_tree_vector (args);
39222 return list;
39225 args_tree = build_tree_list_vec (args);
39227 release_tree_vector (args);
39229 for (t = args_tree; t; t = TREE_CHAIN (t))
39231 tree targ = TREE_VALUE (t);
39233 if (targ != error_mark_node)
39235 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
39236 error ("%<wait%> expression must be integral");
39237 else
39239 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
39241 targ = mark_rvalue_use (targ);
39242 OMP_CLAUSE_DECL (c) = targ;
39243 OMP_CLAUSE_CHAIN (c) = list;
39244 list = c;
39249 return list;
39252 /* OpenACC:
39253 wait [( int-expr-list )] */
39255 static tree
39256 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
39258 location_t location = cp_lexer_peek_token (parser->lexer)->location;
39260 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39261 list = cp_parser_oacc_wait_list (parser, location, list);
39262 else
39264 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
39266 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39267 OMP_CLAUSE_CHAIN (c) = list;
39268 list = c;
39271 return list;
39274 /* OpenMP 3.0:
39275 collapse ( constant-expression ) */
39277 static tree
39278 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
39280 tree c, num;
39281 location_t loc;
39282 HOST_WIDE_INT n;
39284 loc = cp_lexer_peek_token (parser->lexer)->location;
39285 matching_parens parens;
39286 if (!parens.require_open (parser))
39287 return list;
39289 num = cp_parser_constant_expression (parser);
39291 if (!parens.require_close (parser))
39292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39293 /*or_comma=*/false,
39294 /*consume_paren=*/true);
39296 if (num == error_mark_node)
39297 return list;
39298 num = fold_non_dependent_expr (num);
39299 if (!tree_fits_shwi_p (num)
39300 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39301 || (n = tree_to_shwi (num)) <= 0
39302 || (int) n != n)
39304 error_at (loc, "collapse argument needs positive constant integer expression");
39305 return list;
39308 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
39309 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
39310 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
39311 OMP_CLAUSE_CHAIN (c) = list;
39312 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
39314 return c;
39317 /* OpenMP 2.5:
39318 default ( none | shared )
39320 OpenMP 5.1:
39321 default ( private | firstprivate )
39323 OpenACC:
39324 default ( none | present ) */
39326 static tree
39327 cp_parser_omp_clause_default (cp_parser *parser, tree list,
39328 location_t location, bool is_oacc)
39330 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
39331 tree c;
39333 matching_parens parens;
39334 if (!parens.require_open (parser))
39335 return list;
39336 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
39338 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
39339 cp_lexer_consume_token (parser->lexer);
39341 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39344 const char *p = IDENTIFIER_POINTER (id);
39346 switch (p[0])
39348 case 'n':
39349 if (strcmp ("none", p) != 0)
39350 goto invalid_kind;
39351 kind = OMP_CLAUSE_DEFAULT_NONE;
39352 break;
39354 case 'p':
39355 if (strcmp ("present", p) != 0 || !is_oacc)
39356 goto invalid_kind;
39357 kind = OMP_CLAUSE_DEFAULT_PRESENT;
39358 break;
39360 case 'f':
39361 if (strcmp ("firstprivate", p) != 0 || is_oacc)
39362 goto invalid_kind;
39363 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
39364 break;
39366 case 's':
39367 if (strcmp ("shared", p) != 0 || is_oacc)
39368 goto invalid_kind;
39369 kind = OMP_CLAUSE_DEFAULT_SHARED;
39370 break;
39372 default:
39373 goto invalid_kind;
39376 cp_lexer_consume_token (parser->lexer);
39378 else
39380 invalid_kind:
39381 if (is_oacc)
39382 cp_parser_error (parser, "expected %<none%> or %<present%>");
39383 else
39384 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
39385 "%<private%> or %<firstprivate%>");
39388 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
39389 || !parens.require_close (parser))
39390 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39391 /*or_comma=*/false,
39392 /*consume_paren=*/true);
39394 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
39395 return list;
39397 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
39398 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
39399 OMP_CLAUSE_CHAIN (c) = list;
39400 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
39402 return c;
39405 /* OpenMP 3.1:
39406 final ( expression ) */
39408 static tree
39409 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
39411 tree t, c;
39413 matching_parens parens;
39414 if (!parens.require_open (parser))
39415 return list;
39417 t = cp_parser_assignment_expression (parser);
39419 if (t == error_mark_node
39420 || !parens.require_close (parser))
39421 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39422 /*or_comma=*/false,
39423 /*consume_paren=*/true);
39425 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
39427 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
39428 OMP_CLAUSE_FINAL_EXPR (c) = t;
39429 OMP_CLAUSE_CHAIN (c) = list;
39431 return c;
39434 /* OpenMP 5.1:
39435 indirect [( expression )]
39438 static tree
39439 cp_parser_omp_clause_indirect (cp_parser *parser, tree list,
39440 location_t location)
39442 tree t;
39444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39446 matching_parens parens;
39447 if (!parens.require_open (parser))
39448 return list;
39450 bool non_constant_p;
39451 t = cp_parser_constant_expression (parser, true, &non_constant_p);
39453 if (t != error_mark_node && non_constant_p)
39454 error_at (location, "expected constant logical expression");
39456 if (t == error_mark_node
39457 || !parens.require_close (parser))
39458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39459 /*or_comma=*/false,
39460 /*consume_paren=*/true);
39462 else
39463 t = integer_one_node;
39465 check_no_duplicate_clause (list, OMP_CLAUSE_INDIRECT, "indirect", location);
39467 tree c = build_omp_clause (location, OMP_CLAUSE_INDIRECT);
39468 OMP_CLAUSE_INDIRECT_EXPR (c) = t;
39469 OMP_CLAUSE_CHAIN (c) = list;
39471 return c;
39474 /* OpenMP 2.5:
39475 if ( expression )
39477 OpenMP 4.5:
39478 if ( directive-name-modifier : expression )
39480 directive-name-modifier:
39481 parallel | task | taskloop | target data | target | target update
39482 | target enter data | target exit data
39484 OpenMP 5.0:
39485 directive-name-modifier:
39486 ... | simd | cancel */
39488 static tree
39489 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
39490 bool is_omp)
39492 tree t, c;
39493 enum tree_code if_modifier = ERROR_MARK;
39495 matching_parens parens;
39496 if (!parens.require_open (parser))
39497 return list;
39499 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39502 const char *p = IDENTIFIER_POINTER (id);
39503 int n = 2;
39505 if (strcmp ("cancel", p) == 0)
39506 if_modifier = VOID_CST;
39507 else if (strcmp ("parallel", p) == 0)
39508 if_modifier = OMP_PARALLEL;
39509 else if (strcmp ("simd", p) == 0)
39510 if_modifier = OMP_SIMD;
39511 else if (strcmp ("task", p) == 0)
39512 if_modifier = OMP_TASK;
39513 else if (strcmp ("taskloop", p) == 0)
39514 if_modifier = OMP_TASKLOOP;
39515 else if (strcmp ("target", p) == 0)
39517 if_modifier = OMP_TARGET;
39518 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39520 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
39521 p = IDENTIFIER_POINTER (id);
39522 if (strcmp ("data", p) == 0)
39523 if_modifier = OMP_TARGET_DATA;
39524 else if (strcmp ("update", p) == 0)
39525 if_modifier = OMP_TARGET_UPDATE;
39526 else if (strcmp ("enter", p) == 0)
39527 if_modifier = OMP_TARGET_ENTER_DATA;
39528 else if (strcmp ("exit", p) == 0)
39529 if_modifier = OMP_TARGET_EXIT_DATA;
39530 if (if_modifier != OMP_TARGET)
39531 n = 3;
39532 else
39534 location_t loc
39535 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
39536 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
39537 "or %<exit%>");
39538 if_modifier = ERROR_MARK;
39540 if (if_modifier == OMP_TARGET_ENTER_DATA
39541 || if_modifier == OMP_TARGET_EXIT_DATA)
39543 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
39545 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39546 p = IDENTIFIER_POINTER (id);
39547 if (strcmp ("data", p) == 0)
39548 n = 4;
39550 if (n != 4)
39552 location_t loc
39553 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
39554 error_at (loc, "expected %<data%>");
39555 if_modifier = ERROR_MARK;
39560 if (if_modifier != ERROR_MARK)
39562 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
39564 while (n-- > 0)
39565 cp_lexer_consume_token (parser->lexer);
39567 else
39569 if (n > 2)
39571 location_t loc
39572 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
39573 error_at (loc, "expected %<:%>");
39575 if_modifier = ERROR_MARK;
39580 t = cp_parser_assignment_expression (parser);
39582 if (t == error_mark_node
39583 || !parens.require_close (parser))
39584 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39585 /*or_comma=*/false,
39586 /*consume_paren=*/true);
39588 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39589 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
39591 if (if_modifier != ERROR_MARK
39592 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39594 const char *p = NULL;
39595 switch (if_modifier)
39597 case VOID_CST: p = "cancel"; break;
39598 case OMP_PARALLEL: p = "parallel"; break;
39599 case OMP_SIMD: p = "simd"; break;
39600 case OMP_TASK: p = "task"; break;
39601 case OMP_TASKLOOP: p = "taskloop"; break;
39602 case OMP_TARGET_DATA: p = "target data"; break;
39603 case OMP_TARGET: p = "target"; break;
39604 case OMP_TARGET_UPDATE: p = "target update"; break;
39605 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
39606 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
39607 default: gcc_unreachable ();
39609 error_at (location, "too many %<if%> clauses with %qs modifier",
39611 return list;
39613 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39615 if (!is_omp)
39616 error_at (location, "too many %<if%> clauses");
39617 else
39618 error_at (location, "too many %<if%> clauses without modifier");
39619 return list;
39621 else if (if_modifier == ERROR_MARK
39622 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
39624 error_at (location, "if any %<if%> clause has modifier, then all "
39625 "%<if%> clauses have to use modifier");
39626 return list;
39630 c = build_omp_clause (location, OMP_CLAUSE_IF);
39631 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
39632 OMP_CLAUSE_IF_EXPR (c) = t;
39633 OMP_CLAUSE_CHAIN (c) = list;
39635 return c;
39638 /* OpenMP 3.1:
39639 mergeable */
39641 static tree
39642 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
39643 tree list, location_t location)
39645 tree c;
39647 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
39648 location);
39650 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
39651 OMP_CLAUSE_CHAIN (c) = list;
39652 return c;
39655 /* OpenMP 2.5:
39656 nowait */
39658 static tree
39659 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
39660 tree list, location_t location)
39662 tree c;
39664 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
39666 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
39667 OMP_CLAUSE_CHAIN (c) = list;
39668 return c;
39671 /* OpenMP 2.5:
39672 num_threads ( expression ) */
39674 static tree
39675 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
39676 location_t location)
39678 tree t, c;
39680 matching_parens parens;
39681 if (!parens.require_open (parser))
39682 return list;
39684 t = cp_parser_assignment_expression (parser);
39686 if (t == error_mark_node
39687 || !parens.require_close (parser))
39688 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39689 /*or_comma=*/false,
39690 /*consume_paren=*/true);
39692 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
39693 "num_threads", location);
39695 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
39696 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
39697 OMP_CLAUSE_CHAIN (c) = list;
39699 return c;
39702 /* OpenMP 4.5:
39703 num_tasks ( expression )
39705 OpenMP 5.1:
39706 num_tasks ( strict : expression ) */
39708 static tree
39709 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
39710 location_t location)
39712 tree t, c;
39714 matching_parens parens;
39715 if (!parens.require_open (parser))
39716 return list;
39718 bool strict = false;
39719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39720 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39723 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39725 strict = true;
39726 cp_lexer_consume_token (parser->lexer);
39727 cp_lexer_consume_token (parser->lexer);
39731 t = cp_parser_assignment_expression (parser);
39733 if (t == error_mark_node
39734 || !parens.require_close (parser))
39735 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39736 /*or_comma=*/false,
39737 /*consume_paren=*/true);
39739 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
39740 "num_tasks", location);
39742 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
39743 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
39744 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
39745 OMP_CLAUSE_CHAIN (c) = list;
39747 return c;
39750 /* OpenMP 4.5:
39751 grainsize ( expression )
39753 OpenMP 5.1:
39754 grainsize ( strict : expression ) */
39756 static tree
39757 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
39758 location_t location)
39760 tree t, c;
39762 matching_parens parens;
39763 if (!parens.require_open (parser))
39764 return list;
39766 bool strict = false;
39767 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39768 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39771 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39773 strict = true;
39774 cp_lexer_consume_token (parser->lexer);
39775 cp_lexer_consume_token (parser->lexer);
39779 t = cp_parser_assignment_expression (parser);
39781 if (t == error_mark_node
39782 || !parens.require_close (parser))
39783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39784 /*or_comma=*/false,
39785 /*consume_paren=*/true);
39787 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
39788 "grainsize", location);
39790 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
39791 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
39792 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
39793 OMP_CLAUSE_CHAIN (c) = list;
39795 return c;
39798 /* OpenMP 4.5:
39799 priority ( expression ) */
39801 static tree
39802 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
39803 location_t location)
39805 tree t, c;
39807 matching_parens parens;
39808 if (!parens.require_open (parser))
39809 return list;
39811 t = cp_parser_assignment_expression (parser);
39813 if (t == error_mark_node
39814 || !parens.require_close (parser))
39815 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39816 /*or_comma=*/false,
39817 /*consume_paren=*/true);
39819 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
39820 "priority", location);
39822 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
39823 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
39824 OMP_CLAUSE_CHAIN (c) = list;
39826 return c;
39829 /* OpenMP 4.5:
39830 hint ( expression ) */
39832 static tree
39833 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
39835 tree t, c;
39837 matching_parens parens;
39838 if (!parens.require_open (parser))
39839 return list;
39841 t = cp_parser_assignment_expression (parser);
39843 if (t != error_mark_node)
39845 t = fold_non_dependent_expr (t);
39846 if (!value_dependent_expression_p (t)
39847 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
39848 || !tree_fits_shwi_p (t)
39849 || tree_int_cst_sgn (t) == -1))
39850 error_at (location, "expected constant integer expression with "
39851 "valid sync-hint value");
39853 if (t == error_mark_node
39854 || !parens.require_close (parser))
39855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39856 /*or_comma=*/false,
39857 /*consume_paren=*/true);
39858 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
39860 c = build_omp_clause (location, OMP_CLAUSE_HINT);
39861 OMP_CLAUSE_HINT_EXPR (c) = t;
39862 OMP_CLAUSE_CHAIN (c) = list;
39864 return c;
39867 /* OpenMP 5.1:
39868 filter ( integer-expression ) */
39870 static tree
39871 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
39873 tree t, c;
39875 matching_parens parens;
39876 if (!parens.require_open (parser))
39877 return list;
39879 t = cp_parser_assignment_expression (parser);
39881 if (t == error_mark_node
39882 || !parens.require_close (parser))
39883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39884 /*or_comma=*/false,
39885 /*consume_paren=*/true);
39886 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
39888 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
39889 OMP_CLAUSE_FILTER_EXPR (c) = t;
39890 OMP_CLAUSE_CHAIN (c) = list;
39892 return c;
39895 /* OpenMP 4.5:
39896 defaultmap ( tofrom : scalar )
39898 OpenMP 5.0:
39899 defaultmap ( implicit-behavior [ : variable-category ] ) */
39901 static tree
39902 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
39903 location_t location)
39905 tree c, id;
39906 const char *p;
39907 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39908 enum omp_clause_defaultmap_kind category
39909 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
39911 matching_parens parens;
39912 if (!parens.require_open (parser))
39913 return list;
39915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
39916 p = "default";
39917 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39919 invalid_behavior:
39920 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
39921 "%<tofrom%>, %<firstprivate%>, %<none%> "
39922 "or %<default%>");
39923 goto out_err;
39925 else
39927 id = cp_lexer_peek_token (parser->lexer)->u.value;
39928 p = IDENTIFIER_POINTER (id);
39931 switch (p[0])
39933 case 'a':
39934 if (strcmp ("alloc", p) == 0)
39935 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39936 else
39937 goto invalid_behavior;
39938 break;
39940 case 'd':
39941 if (strcmp ("default", p) == 0)
39942 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39943 else
39944 goto invalid_behavior;
39945 break;
39947 case 'f':
39948 if (strcmp ("firstprivate", p) == 0)
39949 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39950 else if (strcmp ("from", p) == 0)
39951 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39952 else
39953 goto invalid_behavior;
39954 break;
39956 case 'n':
39957 if (strcmp ("none", p) == 0)
39958 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39959 else
39960 goto invalid_behavior;
39961 break;
39963 case 'p':
39964 if (strcmp ("present", p) == 0)
39965 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39966 else
39967 goto invalid_behavior;
39968 break;
39970 case 't':
39971 if (strcmp ("tofrom", p) == 0)
39972 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39973 else if (strcmp ("to", p) == 0)
39974 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39975 else
39976 goto invalid_behavior;
39977 break;
39979 default:
39980 goto invalid_behavior;
39982 cp_lexer_consume_token (parser->lexer);
39984 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39986 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39987 goto out_err;
39989 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39991 invalid_category:
39992 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39993 "%<all%>");
39994 goto out_err;
39996 id = cp_lexer_peek_token (parser->lexer)->u.value;
39997 p = IDENTIFIER_POINTER (id);
39999 switch (p[0])
40001 case 'a':
40002 if (strcmp ("aggregate", p) == 0)
40003 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
40004 else if (strcmp ("all", p) == 0)
40005 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
40006 else
40007 goto invalid_category;
40008 break;
40010 case 'p':
40011 if (strcmp ("pointer", p) == 0)
40012 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
40013 else
40014 goto invalid_category;
40015 break;
40017 case 's':
40018 if (strcmp ("scalar", p) == 0)
40019 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
40020 else
40021 goto invalid_category;
40022 break;
40024 default:
40025 goto invalid_category;
40028 cp_lexer_consume_token (parser->lexer);
40030 if (!parens.require_close (parser))
40031 goto out_err;
40033 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
40034 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
40035 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
40036 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
40037 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
40038 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
40039 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
40040 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
40041 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
40043 enum omp_clause_defaultmap_kind cat = category;
40044 location_t loc = OMP_CLAUSE_LOCATION (c);
40045 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
40046 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
40047 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
40048 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
40049 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
40050 p = NULL;
40051 switch (cat)
40053 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
40054 p = NULL;
40055 break;
40056 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
40057 p = "all";
40058 break;
40059 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
40060 p = "aggregate";
40061 break;
40062 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
40063 p = "pointer";
40064 break;
40065 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
40066 p = "scalar";
40067 break;
40068 default:
40069 gcc_unreachable ();
40071 if (p)
40072 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
40074 else
40075 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
40076 "category");
40077 break;
40080 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
40081 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
40082 OMP_CLAUSE_CHAIN (c) = list;
40083 return c;
40085 out_err:
40086 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40087 /*or_comma=*/false,
40088 /*consume_paren=*/true);
40089 return list;
40092 /* OpenMP 5.0:
40093 order ( concurrent )
40095 OpenMP 5.1:
40096 order ( order-modifier : concurrent )
40098 order-modifier:
40099 reproducible
40100 unconstrained */
40102 static tree
40103 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
40105 tree c, id;
40106 const char *p;
40107 bool unconstrained = false;
40108 bool reproducible = false;
40110 matching_parens parens;
40111 if (!parens.require_open (parser))
40112 return list;
40114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40115 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40117 id = cp_lexer_peek_token (parser->lexer)->u.value;
40118 p = IDENTIFIER_POINTER (id);
40119 if (strcmp (p, "unconstrained") == 0)
40120 unconstrained = true;
40121 else if (strcmp (p, "reproducible") == 0)
40122 reproducible = true;
40123 else
40125 cp_parser_error (parser, "expected %<reproducible%> or "
40126 "%<unconstrained%>");
40127 goto out_err;
40129 cp_lexer_consume_token (parser->lexer);
40130 cp_lexer_consume_token (parser->lexer);
40132 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40134 cp_parser_error (parser, "expected %<concurrent%>");
40135 goto out_err;
40137 else
40139 id = cp_lexer_peek_token (parser->lexer)->u.value;
40140 p = IDENTIFIER_POINTER (id);
40142 if (strcmp (p, "concurrent") != 0)
40144 cp_parser_error (parser, "expected %<concurrent%>");
40145 goto out_err;
40147 cp_lexer_consume_token (parser->lexer);
40148 if (!parens.require_close (parser))
40149 goto out_err;
40151 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
40152 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
40153 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
40154 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
40155 OMP_CLAUSE_CHAIN (c) = list;
40156 return c;
40158 out_err:
40159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40160 /*or_comma=*/false,
40161 /*consume_paren=*/true);
40162 return list;
40165 /* OpenMP 5.0:
40166 bind ( teams | parallel | thread ) */
40168 static tree
40169 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
40170 location_t location)
40172 tree c;
40173 const char *p;
40174 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
40176 matching_parens parens;
40177 if (!parens.require_open (parser))
40178 return list;
40180 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40182 invalid:
40183 cp_parser_error (parser,
40184 "expected %<teams%>, %<parallel%> or %<thread%>");
40185 goto out_err;
40187 else
40189 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40190 p = IDENTIFIER_POINTER (id);
40192 if (strcmp (p, "teams") == 0)
40193 kind = OMP_CLAUSE_BIND_TEAMS;
40194 else if (strcmp (p, "parallel") == 0)
40195 kind = OMP_CLAUSE_BIND_PARALLEL;
40196 else if (strcmp (p, "thread") != 0)
40197 goto invalid;
40198 cp_lexer_consume_token (parser->lexer);
40199 if (!parens.require_close (parser))
40200 goto out_err;
40202 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
40203 c = build_omp_clause (location, OMP_CLAUSE_BIND);
40204 OMP_CLAUSE_BIND_KIND (c) = kind;
40205 OMP_CLAUSE_CHAIN (c) = list;
40206 return c;
40208 out_err:
40209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40210 /*or_comma=*/false,
40211 /*consume_paren=*/true);
40212 return list;
40215 /* OpenMP 2.5:
40216 ordered
40218 OpenMP 4.5:
40219 ordered ( constant-expression ) */
40221 static tree
40222 cp_parser_omp_clause_ordered (cp_parser *parser,
40223 tree list, location_t location)
40225 tree c, num = NULL_TREE;
40226 HOST_WIDE_INT n;
40228 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
40229 "ordered", location);
40231 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40233 matching_parens parens;
40234 parens.consume_open (parser);
40236 num = cp_parser_constant_expression (parser);
40238 if (!parens.require_close (parser))
40239 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40240 /*or_comma=*/false,
40241 /*consume_paren=*/true);
40243 if (num == error_mark_node)
40244 return list;
40245 num = fold_non_dependent_expr (num);
40246 if (!tree_fits_shwi_p (num)
40247 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
40248 || (n = tree_to_shwi (num)) <= 0
40249 || (int) n != n)
40251 error_at (location,
40252 "ordered argument needs positive constant integer "
40253 "expression");
40254 return list;
40258 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
40259 OMP_CLAUSE_ORDERED_EXPR (c) = num;
40260 OMP_CLAUSE_CHAIN (c) = list;
40261 return c;
40264 /* OpenMP 2.5:
40265 reduction ( reduction-operator : variable-list )
40267 reduction-operator:
40268 One of: + * - & ^ | && ||
40270 OpenMP 3.1:
40272 reduction-operator:
40273 One of: + * - & ^ | && || min max
40275 OpenMP 4.0:
40277 reduction-operator:
40278 One of: + * - & ^ | && ||
40279 id-expression
40281 OpenMP 5.0:
40282 reduction ( reduction-modifier, reduction-operator : variable-list )
40283 in_reduction ( reduction-operator : variable-list )
40284 task_reduction ( reduction-operator : variable-list ) */
40286 static tree
40287 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
40288 bool is_omp, tree list)
40290 enum tree_code code = ERROR_MARK;
40291 tree nlist, c, id = NULL_TREE;
40292 bool task = false;
40293 bool inscan = false;
40295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40296 return list;
40298 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
40300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
40301 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
40303 cp_lexer_consume_token (parser->lexer);
40304 cp_lexer_consume_token (parser->lexer);
40306 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40307 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
40309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40310 const char *p = IDENTIFIER_POINTER (id);
40311 if (strcmp (p, "task") == 0)
40312 task = true;
40313 else if (strcmp (p, "inscan") == 0)
40314 inscan = true;
40315 if (task || inscan)
40317 cp_lexer_consume_token (parser->lexer);
40318 cp_lexer_consume_token (parser->lexer);
40323 switch (cp_lexer_peek_token (parser->lexer)->type)
40325 case CPP_PLUS: code = PLUS_EXPR; break;
40326 case CPP_MULT: code = MULT_EXPR; break;
40327 case CPP_MINUS: code = MINUS_EXPR; break;
40328 case CPP_AND: code = BIT_AND_EXPR; break;
40329 case CPP_XOR: code = BIT_XOR_EXPR; break;
40330 case CPP_OR: code = BIT_IOR_EXPR; break;
40331 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
40332 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
40333 default: break;
40336 if (code != ERROR_MARK)
40337 cp_lexer_consume_token (parser->lexer);
40338 else
40340 bool saved_colon_corrects_to_scope_p;
40341 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40342 parser->colon_corrects_to_scope_p = false;
40343 id = cp_parser_id_expression (parser, /*template_p=*/false,
40344 /*check_dependency_p=*/true,
40345 /*template_p=*/NULL,
40346 /*declarator_p=*/false,
40347 /*optional_p=*/false);
40348 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40349 if (identifier_p (id))
40351 const char *p = IDENTIFIER_POINTER (id);
40353 if (strcmp (p, "min") == 0)
40354 code = MIN_EXPR;
40355 else if (strcmp (p, "max") == 0)
40356 code = MAX_EXPR;
40357 else if (id == ovl_op_identifier (false, PLUS_EXPR))
40358 code = PLUS_EXPR;
40359 else if (id == ovl_op_identifier (false, MULT_EXPR))
40360 code = MULT_EXPR;
40361 else if (id == ovl_op_identifier (false, MINUS_EXPR))
40362 code = MINUS_EXPR;
40363 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
40364 code = BIT_AND_EXPR;
40365 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
40366 code = BIT_IOR_EXPR;
40367 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
40368 code = BIT_XOR_EXPR;
40369 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
40370 code = TRUTH_ANDIF_EXPR;
40371 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
40372 code = TRUTH_ORIF_EXPR;
40373 id = omp_reduction_id (code, id, NULL_TREE);
40374 tree scope = parser->scope;
40375 if (scope)
40376 id = build_qualified_name (NULL_TREE, scope, id, false);
40377 parser->scope = NULL_TREE;
40378 parser->qualifying_scope = NULL_TREE;
40379 parser->object_scope = NULL_TREE;
40381 else
40383 error ("invalid reduction-identifier");
40384 resync_fail:
40385 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40386 /*or_comma=*/false,
40387 /*consume_paren=*/true);
40388 return list;
40392 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40393 goto resync_fail;
40395 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
40396 NULL);
40397 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40399 OMP_CLAUSE_REDUCTION_CODE (c) = code;
40400 if (task)
40401 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
40402 else if (inscan)
40403 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
40404 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
40407 return nlist;
40410 /* OpenMP 2.5:
40411 schedule ( schedule-kind )
40412 schedule ( schedule-kind , expression )
40414 schedule-kind:
40415 static | dynamic | guided | runtime | auto
40417 OpenMP 4.5:
40418 schedule ( schedule-modifier : schedule-kind )
40419 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
40421 schedule-modifier:
40422 simd
40423 monotonic
40424 nonmonotonic */
40426 static tree
40427 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
40429 tree c, t;
40430 int modifiers = 0, nmodifiers = 0;
40432 matching_parens parens;
40433 if (!parens.require_open (parser))
40434 return list;
40436 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
40438 location_t comma = UNKNOWN_LOCATION;
40439 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40442 const char *p = IDENTIFIER_POINTER (id);
40443 if (strcmp ("simd", p) == 0)
40444 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
40445 else if (strcmp ("monotonic", p) == 0)
40446 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
40447 else if (strcmp ("nonmonotonic", p) == 0)
40448 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
40449 else
40450 break;
40451 comma = UNKNOWN_LOCATION;
40452 cp_lexer_consume_token (parser->lexer);
40453 if (nmodifiers++ == 0
40454 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40456 comma = cp_lexer_peek_token (parser->lexer)->location;
40457 cp_lexer_consume_token (parser->lexer);
40459 else
40461 cp_parser_require (parser, CPP_COLON, RT_COLON);
40462 break;
40465 if (comma != UNKNOWN_LOCATION)
40466 error_at (comma, "expected %<:%>");
40468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40471 const char *p = IDENTIFIER_POINTER (id);
40473 switch (p[0])
40475 case 'd':
40476 if (strcmp ("dynamic", p) != 0)
40477 goto invalid_kind;
40478 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
40479 break;
40481 case 'g':
40482 if (strcmp ("guided", p) != 0)
40483 goto invalid_kind;
40484 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
40485 break;
40487 case 'r':
40488 if (strcmp ("runtime", p) != 0)
40489 goto invalid_kind;
40490 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
40491 break;
40493 default:
40494 goto invalid_kind;
40497 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40498 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
40499 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
40500 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
40501 else
40502 goto invalid_kind;
40503 cp_lexer_consume_token (parser->lexer);
40505 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
40506 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40507 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
40508 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40510 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
40511 "specified");
40512 modifiers = 0;
40515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40517 cp_token *token;
40518 cp_lexer_consume_token (parser->lexer);
40520 token = cp_lexer_peek_token (parser->lexer);
40521 t = cp_parser_assignment_expression (parser);
40523 if (t == error_mark_node)
40524 goto resync_fail;
40525 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
40526 error_at (token->location, "schedule %<runtime%> does not take "
40527 "a %<chunk_size%> parameter");
40528 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
40529 error_at (token->location, "schedule %<auto%> does not take "
40530 "a %<chunk_size%> parameter");
40531 else
40532 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
40534 if (!parens.require_close (parser))
40535 goto resync_fail;
40537 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40538 goto resync_fail;
40540 OMP_CLAUSE_SCHEDULE_KIND (c)
40541 = (enum omp_clause_schedule_kind)
40542 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
40544 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
40545 OMP_CLAUSE_CHAIN (c) = list;
40546 return c;
40548 invalid_kind:
40549 cp_parser_error (parser, "invalid schedule kind");
40550 resync_fail:
40551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40552 /*or_comma=*/false,
40553 /*consume_paren=*/true);
40554 return list;
40557 /* OpenMP 3.0:
40558 untied */
40560 static tree
40561 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
40562 tree list, location_t location)
40564 tree c;
40566 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
40568 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
40569 OMP_CLAUSE_CHAIN (c) = list;
40570 return c;
40573 /* OpenMP 4.0:
40574 inbranch
40575 notinbranch */
40577 static tree
40578 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
40579 tree list, location_t location)
40581 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40582 tree c = build_omp_clause (location, code);
40583 OMP_CLAUSE_CHAIN (c) = list;
40584 return c;
40587 /* OpenMP 4.0:
40588 parallel
40590 sections
40591 taskgroup */
40593 static tree
40594 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
40595 enum omp_clause_code code,
40596 tree list, location_t location)
40598 tree c = build_omp_clause (location, code);
40599 OMP_CLAUSE_CHAIN (c) = list;
40600 return c;
40603 /* OpenMP 4.5:
40604 nogroup */
40606 static tree
40607 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
40608 tree list, location_t location)
40610 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
40611 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
40612 OMP_CLAUSE_CHAIN (c) = list;
40613 return c;
40616 /* OpenMP 4.5:
40617 simd
40618 threads */
40620 static tree
40621 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
40622 enum omp_clause_code code,
40623 tree list, location_t location)
40625 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40626 tree c = build_omp_clause (location, code);
40627 OMP_CLAUSE_CHAIN (c) = list;
40628 return c;
40631 /* OpenMP 4.0:
40632 num_teams ( expression )
40634 OpenMP 5.1:
40635 num_teams ( expression : expression ) */
40637 static tree
40638 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
40639 location_t location)
40641 tree upper, lower = NULL_TREE, c;
40643 matching_parens parens;
40644 if (!parens.require_open (parser))
40645 return list;
40647 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40648 parser->colon_corrects_to_scope_p = false;
40649 upper = cp_parser_assignment_expression (parser);
40650 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40652 if (upper != error_mark_node
40653 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40655 lower = upper;
40656 cp_lexer_consume_token (parser->lexer);
40657 upper = cp_parser_assignment_expression (parser);
40660 if (upper == error_mark_node
40661 || !parens.require_close (parser))
40662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40663 /*or_comma=*/false,
40664 /*consume_paren=*/true);
40666 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
40667 "num_teams", location);
40669 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
40670 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
40671 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
40672 OMP_CLAUSE_CHAIN (c) = list;
40674 return c;
40677 /* OpenMP 4.0:
40678 thread_limit ( expression ) */
40680 static tree
40681 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
40682 location_t location)
40684 tree t, c;
40686 matching_parens parens;
40687 if (!parens.require_open (parser))
40688 return list;
40690 t = cp_parser_assignment_expression (parser);
40692 if (t == error_mark_node
40693 || !parens.require_close (parser))
40694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40695 /*or_comma=*/false,
40696 /*consume_paren=*/true);
40698 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
40699 "thread_limit", location);
40701 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
40702 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
40703 OMP_CLAUSE_CHAIN (c) = list;
40705 return c;
40708 /* OpenMP 5.1
40709 full */
40711 static tree
40712 cp_parser_omp_clause_full (tree list, location_t loc)
40714 check_no_duplicate_clause (list, OMP_CLAUSE_FULL, "full", loc);
40716 tree c = build_omp_clause (loc, OMP_CLAUSE_FULL);
40717 OMP_CLAUSE_CHAIN (c) = list;
40718 return c;
40721 /* OpenMP 5.1
40722 partial ( constant-expression ) */
40724 static tree
40725 cp_parser_omp_clause_partial (cp_parser *parser, tree list, location_t loc)
40727 tree num = NULL_TREE;
40728 check_no_duplicate_clause (list, OMP_CLAUSE_PARTIAL, "partial", loc);
40730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40732 matching_parens parens;
40733 parens.consume_open (parser);
40734 num = cp_parser_constant_expression (parser);
40735 if (num == error_mark_node
40736 || !parens.require_close (parser))
40737 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40738 /*or_comma=*/false,
40739 /*consume_paren=*/true);
40740 if (num == error_mark_node)
40741 return list;
40744 tree c = build_omp_clause (loc, OMP_CLAUSE_PARTIAL);
40745 OMP_CLAUSE_PARTIAL_EXPR (c) = num;
40746 OMP_CLAUSE_CHAIN (c) = list;
40747 return c;
40750 /* OpenMP 4.0:
40751 aligned ( variable-list )
40752 aligned ( variable-list : constant-expression ) */
40754 static tree
40755 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
40757 tree nlist, c, alignment = NULL_TREE;
40758 bool colon;
40760 matching_parens parens;
40761 if (!parens.require_open (parser))
40762 return list;
40764 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
40765 &colon);
40767 if (colon)
40769 alignment = cp_parser_constant_expression (parser);
40771 if (!parens.require_close (parser))
40772 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40773 /*or_comma=*/false,
40774 /*consume_paren=*/true);
40776 if (alignment == error_mark_node)
40777 alignment = NULL_TREE;
40780 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40781 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
40783 return nlist;
40786 /* OpenMP 5.0:
40787 allocate ( variable-list )
40788 allocate ( expression : variable-list )
40790 OpenMP 5.1:
40791 allocate ( allocator-modifier : variable-list )
40792 allocate ( allocator-modifier , allocator-modifier : variable-list )
40794 allocator-modifier:
40795 allocator ( expression )
40796 align ( expression ) */
40798 static tree
40799 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
40801 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
40802 bool colon, has_modifiers = false;
40804 matching_parens parens;
40805 if (!parens.require_open (parser))
40806 return list;
40808 cp_parser_parse_tentatively (parser);
40809 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40810 parser->colon_corrects_to_scope_p = false;
40811 for (int mod = 0; mod < 2; mod++)
40812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40813 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40816 const char *p = IDENTIFIER_POINTER (id);
40817 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
40818 break;
40819 cp_lexer_consume_token (parser->lexer);
40820 matching_parens parens2;
40821 if (!parens2.require_open (parser))
40822 break;
40823 if (strcmp (p, "allocator") == 0)
40825 if (allocator != NULL_TREE)
40826 break;
40827 allocator = cp_parser_assignment_expression (parser);
40829 else
40831 if (align != NULL_TREE)
40832 break;
40833 align = cp_parser_assignment_expression (parser);
40835 if (!parens2.require_close (parser))
40836 break;
40837 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40839 has_modifiers = true;
40840 break;
40842 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
40843 break;
40844 cp_lexer_consume_token (parser->lexer);
40846 else
40847 break;
40848 if (!has_modifiers)
40850 cp_parser_abort_tentative_parse (parser);
40851 align = NULL_TREE;
40852 allocator = NULL_TREE;
40853 cp_parser_parse_tentatively (parser);
40854 allocator = cp_parser_assignment_expression (parser);
40856 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40857 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40859 cp_parser_parse_definitely (parser);
40860 cp_lexer_consume_token (parser->lexer);
40861 if (allocator == error_mark_node)
40862 allocator = NULL_TREE;
40863 if (align == error_mark_node)
40864 align = NULL_TREE;
40866 else
40868 cp_parser_abort_tentative_parse (parser);
40869 allocator = NULL_TREE;
40870 align = NULL_TREE;
40873 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
40874 &colon);
40876 if (allocator || align)
40877 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40879 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40880 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
40883 return nlist;
40886 /* OpenMP 2.5:
40887 lastprivate ( variable-list )
40889 OpenMP 5.0:
40890 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
40892 static tree
40893 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
40895 bool conditional = false;
40897 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40898 return list;
40900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40901 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40903 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40904 const char *p = IDENTIFIER_POINTER (id);
40906 if (strcmp ("conditional", p) == 0)
40908 conditional = true;
40909 cp_lexer_consume_token (parser->lexer);
40910 cp_lexer_consume_token (parser->lexer);
40914 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
40915 list, NULL);
40917 if (conditional)
40918 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40919 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
40920 return nlist;
40923 /* OpenMP 4.0:
40924 linear ( variable-list )
40925 linear ( variable-list : expression )
40927 OpenMP 4.5:
40928 linear ( modifier ( variable-list ) )
40929 linear ( modifier ( variable-list ) : expression )
40931 modifier:
40934 uval
40936 OpenMP 5.2:
40937 linear ( variable-list : modifiers-list )
40939 modifiers:
40942 uval
40943 step ( expression ) */
40945 static tree
40946 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
40947 bool declare_simd)
40949 tree nlist, c, step = integer_one_node;
40950 bool colon;
40951 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
40952 bool old_linear_modifier = false;
40954 matching_parens parens;
40955 if (!parens.require_open (parser))
40956 return list;
40958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40961 const char *p = IDENTIFIER_POINTER (id);
40963 if (strcmp ("ref", p) == 0)
40964 kind = OMP_CLAUSE_LINEAR_REF;
40965 else if (strcmp ("val", p) == 0)
40966 kind = OMP_CLAUSE_LINEAR_VAL;
40967 else if (strcmp ("uval", p) == 0)
40968 kind = OMP_CLAUSE_LINEAR_UVAL;
40969 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40971 cp_lexer_consume_token (parser->lexer);
40972 old_linear_modifier = true;
40974 else
40975 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40978 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40979 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40980 &colon);
40981 else
40983 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40984 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40985 if (colon)
40986 cp_parser_require (parser, CPP_COLON, RT_COLON);
40987 else if (!parens.require_close (parser))
40988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40989 /*or_comma=*/false,
40990 /*consume_paren=*/true);
40993 if (colon)
40995 bool has_modifiers = false;
40996 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40997 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40999 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41000 const char *p = IDENTIFIER_POINTER (id);
41001 size_t pos = 0;
41002 if (strcmp ("ref", p) == 0
41003 || strcmp ("val", p) == 0
41004 || strcmp ("uval", p) == 0)
41005 pos = 2;
41006 else if (strcmp ("step", p) == 0
41007 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
41009 pos = cp_parser_skip_balanced_tokens (parser, 2);
41010 if (pos == 2)
41011 pos = 0;
41013 if (pos != 0
41014 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
41015 || cp_lexer_nth_token_is (parser->lexer, pos,
41016 CPP_CLOSE_PAREN)))
41017 has_modifiers = true;
41020 step = NULL_TREE;
41021 if (has_modifiers)
41023 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41025 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41026 const char *p = IDENTIFIER_POINTER (id);
41027 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
41028 if (strcmp ("ref", p) == 0)
41029 nkind = OMP_CLAUSE_LINEAR_REF;
41030 else if (strcmp ("val", p) == 0)
41031 nkind = OMP_CLAUSE_LINEAR_VAL;
41032 else if (strcmp ("uval", p) == 0)
41033 nkind = OMP_CLAUSE_LINEAR_UVAL;
41034 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
41036 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
41037 error_at (cp_lexer_peek_token (parser->lexer)->location,
41038 "multiple linear modifiers");
41039 kind = nkind;
41040 cp_lexer_consume_token (parser->lexer);
41042 else if (strcmp ("step", p) == 0)
41044 location_t step_loc
41045 = cp_lexer_peek_token (parser->lexer)->location;
41046 cp_lexer_consume_token (parser->lexer);
41047 matching_parens parens2;
41048 if (parens2.require_open (parser))
41050 if (step)
41051 error_at (step_loc, "multiple %<step%> modifiers");
41052 if (declare_simd
41053 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41054 && cp_lexer_nth_token_is (parser->lexer, 2,
41055 CPP_CLOSE_PAREN))
41057 cp_token *token
41058 = cp_lexer_peek_token (parser->lexer);
41059 location_t tok_loc = token->location;
41060 cp_parser_parse_tentatively (parser);
41061 step = cp_parser_id_expression (parser, false, true,
41062 NULL, false, false);
41063 if (step != error_mark_node)
41064 step = cp_parser_lookup_name_simple (parser, step,
41065 tok_loc);
41066 if (step == error_mark_node)
41068 step = NULL_TREE;
41069 cp_parser_abort_tentative_parse (parser);
41071 else if (!cp_parser_parse_definitely (parser))
41072 step = NULL_TREE;
41074 if (!step)
41075 step = cp_parser_assignment_expression (parser);
41076 if (!parens2.require_close (parser))
41077 cp_parser_skip_to_closing_parenthesis (parser, true,
41078 false, true);
41080 else
41081 break;
41083 else
41084 break;
41085 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41087 cp_lexer_consume_token (parser->lexer);
41088 continue;
41090 break;
41092 if (!step)
41093 step = integer_one_node;
41095 else if (declare_simd
41096 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41097 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
41099 cp_token *token = cp_lexer_peek_token (parser->lexer);
41100 cp_parser_parse_tentatively (parser);
41101 step = cp_parser_id_expression (parser, /*template_p=*/false,
41102 /*check_dependency_p=*/true,
41103 /*template_p=*/NULL,
41104 /*declarator_p=*/false,
41105 /*optional_p=*/false);
41106 if (step != error_mark_node)
41107 step = cp_parser_lookup_name_simple (parser, step, token->location);
41108 if (step == error_mark_node)
41110 step = NULL_TREE;
41111 cp_parser_abort_tentative_parse (parser);
41113 else if (!cp_parser_parse_definitely (parser))
41114 step = NULL_TREE;
41116 if (!step)
41117 step = cp_parser_assignment_expression (parser);
41119 if (!parens.require_close (parser))
41120 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41121 /*or_comma=*/false,
41122 /*consume_paren=*/true);
41124 if (step == error_mark_node)
41125 return list;
41128 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41130 OMP_CLAUSE_LINEAR_STEP (c) = step;
41131 OMP_CLAUSE_LINEAR_KIND (c) = kind;
41132 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
41135 return nlist;
41138 /* OpenMP 4.0:
41139 safelen ( constant-expression ) */
41141 static tree
41142 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
41143 location_t location)
41145 tree t, c;
41147 matching_parens parens;
41148 if (!parens.require_open (parser))
41149 return list;
41151 t = cp_parser_constant_expression (parser);
41153 if (t == error_mark_node
41154 || !parens.require_close (parser))
41155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41156 /*or_comma=*/false,
41157 /*consume_paren=*/true);
41159 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
41161 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
41162 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
41163 OMP_CLAUSE_CHAIN (c) = list;
41165 return c;
41168 /* OpenMP 4.0:
41169 simdlen ( constant-expression ) */
41171 static tree
41172 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
41173 location_t location)
41175 tree t, c;
41177 matching_parens parens;
41178 if (!parens.require_open (parser))
41179 return list;
41181 t = cp_parser_constant_expression (parser);
41183 if (t == error_mark_node
41184 || !parens.require_close (parser))
41185 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41186 /*or_comma=*/false,
41187 /*consume_paren=*/true);
41189 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
41191 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
41192 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
41193 OMP_CLAUSE_CHAIN (c) = list;
41195 return c;
41198 /* OpenMP 4.5:
41199 vec:
41200 identifier [+/- integer]
41201 vec , identifier [+/- integer]
41204 static tree
41205 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
41206 tree list, bool depend_p)
41208 tree vec = NULL;
41210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41212 cp_parser_error (parser, "expected identifier");
41213 return list;
41216 if (!depend_p)
41218 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41219 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
41220 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
41221 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
41222 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
41224 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
41225 if (integer_onep (val))
41227 cp_lexer_consume_token (parser->lexer);
41228 cp_lexer_consume_token (parser->lexer);
41229 cp_lexer_consume_token (parser->lexer);
41230 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
41231 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
41232 OMP_CLAUSE_CHAIN (u) = list;
41233 return u;
41238 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41240 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
41241 tree t, identifier = cp_parser_identifier (parser);
41242 tree addend = NULL;
41244 if (identifier == error_mark_node)
41245 t = error_mark_node;
41246 else
41248 t = cp_parser_lookup_name_simple
41249 (parser, identifier,
41250 cp_lexer_peek_token (parser->lexer)->location);
41251 if (t == error_mark_node)
41252 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
41253 id_loc);
41256 bool neg = false;
41257 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
41258 neg = true;
41259 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
41261 addend = integer_zero_node;
41262 goto add_to_vector;
41264 cp_lexer_consume_token (parser->lexer);
41266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
41268 cp_parser_error (parser, "expected integer");
41269 return list;
41272 addend = cp_lexer_peek_token (parser->lexer)->u.value;
41273 if (TREE_CODE (addend) != INTEGER_CST)
41275 cp_parser_error (parser, "expected integer");
41276 return list;
41278 cp_lexer_consume_token (parser->lexer);
41280 add_to_vector:
41281 if (t != error_mark_node)
41283 vec = tree_cons (addend, t, vec);
41284 if (neg)
41285 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
41288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
41289 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41290 break;
41292 cp_lexer_consume_token (parser->lexer);
41295 if (vec)
41297 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
41298 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
41299 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
41300 OMP_CLAUSE_DECL (u) = nreverse (vec);
41301 OMP_CLAUSE_CHAIN (u) = list;
41302 return u;
41304 return list;
41307 /* OpenMP 5.0:
41308 detach ( event-handle ) */
41310 static tree
41311 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
41313 matching_parens parens;
41315 if (!parens.require_open (parser))
41316 return list;
41318 cp_token *token;
41319 tree name, decl;
41321 token = cp_lexer_peek_token (parser->lexer);
41322 name = cp_parser_id_expression (parser, /*template_p=*/false,
41323 /*check_dependency_p=*/true,
41324 /*template_p=*/NULL,
41325 /*declarator_p=*/false,
41326 /*optional_p=*/false);
41327 if (name == error_mark_node)
41328 decl = error_mark_node;
41329 else
41331 if (identifier_p (name))
41332 decl = cp_parser_lookup_name_simple (parser, name, token->location);
41333 else
41334 decl = name;
41335 if (decl == error_mark_node)
41336 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
41337 token->location);
41340 if (decl == error_mark_node
41341 || !parens.require_close (parser))
41342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41343 /*or_comma=*/false,
41344 /*consume_paren=*/true);
41346 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
41347 OMP_CLAUSE_DECL (u) = decl;
41348 OMP_CLAUSE_CHAIN (u) = list;
41350 return u;
41353 /* OpenMP 5.0:
41354 iterators ( iterators-definition )
41356 iterators-definition:
41357 iterator-specifier
41358 iterator-specifier , iterators-definition
41360 iterator-specifier:
41361 identifier = range-specification
41362 iterator-type identifier = range-specification
41364 range-specification:
41365 begin : end
41366 begin : end : step */
41368 static tree
41369 cp_parser_omp_iterators (cp_parser *parser)
41371 tree ret = NULL_TREE, *last = &ret;
41372 cp_lexer_consume_token (parser->lexer);
41374 matching_parens parens;
41375 if (!parens.require_open (parser))
41376 return error_mark_node;
41378 bool saved_colon_corrects_to_scope_p
41379 = parser->colon_corrects_to_scope_p;
41380 bool saved_colon_doesnt_start_class_def_p
41381 = parser->colon_doesnt_start_class_def_p;
41385 tree iter_type;
41386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41387 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
41388 iter_type = integer_type_node;
41389 else
41391 const char *saved_message
41392 = parser->type_definition_forbidden_message;
41393 parser->type_definition_forbidden_message
41394 = G_("types may not be defined in iterator type");
41396 iter_type = cp_parser_type_id (parser);
41398 parser->type_definition_forbidden_message = saved_message;
41401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41404 cp_parser_error (parser, "expected identifier");
41405 break;
41408 tree id = cp_parser_identifier (parser);
41409 if (id == error_mark_node)
41410 break;
41412 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41413 break;
41415 parser->colon_corrects_to_scope_p = false;
41416 parser->colon_doesnt_start_class_def_p = true;
41417 tree begin = cp_parser_assignment_expression (parser);
41419 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41420 break;
41422 tree end = cp_parser_assignment_expression (parser);
41424 tree step = integer_one_node;
41425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41427 cp_lexer_consume_token (parser->lexer);
41428 step = cp_parser_assignment_expression (parser);
41431 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
41432 DECL_ARTIFICIAL (iter_var) = 1;
41433 DECL_CONTEXT (iter_var) = current_function_decl;
41434 pushdecl (iter_var);
41436 *last = make_tree_vec (6);
41437 TREE_VEC_ELT (*last, 0) = iter_var;
41438 TREE_VEC_ELT (*last, 1) = begin;
41439 TREE_VEC_ELT (*last, 2) = end;
41440 TREE_VEC_ELT (*last, 3) = step;
41441 last = &TREE_CHAIN (*last);
41443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41445 cp_lexer_consume_token (parser->lexer);
41446 continue;
41448 break;
41450 while (1);
41452 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41453 parser->colon_doesnt_start_class_def_p
41454 = saved_colon_doesnt_start_class_def_p;
41456 if (!parens.require_close (parser))
41457 cp_parser_skip_to_closing_parenthesis (parser,
41458 /*recovering=*/true,
41459 /*or_comma=*/false,
41460 /*consume_paren=*/true);
41462 return ret ? ret : error_mark_node;
41465 /* OpenMP 5.0:
41466 affinity ( [aff-modifier :] variable-list )
41467 aff-modifier:
41468 iterator ( iterators-definition ) */
41470 static tree
41471 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
41473 tree nlist, c, iterators = NULL_TREE;
41475 matching_parens parens;
41476 if (!parens.require_open (parser))
41477 return list;
41479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41482 const char *p = IDENTIFIER_POINTER (id);
41483 bool parse_iter = ((strcmp ("iterator", p) == 0)
41484 && (cp_lexer_nth_token_is (parser->lexer, 2,
41485 CPP_OPEN_PAREN)));
41486 if (parse_iter)
41488 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
41489 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
41491 if (parse_iter)
41493 begin_scope (sk_omp, NULL);
41494 iterators = cp_parser_omp_iterators (parser);
41495 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41497 if (iterators)
41498 poplevel (0, 1, 0);
41499 cp_parser_skip_to_closing_parenthesis (parser,
41500 /*recovering=*/true,
41501 /*or_comma=*/false,
41502 /*consume_paren=*/true);
41503 return list;
41507 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
41508 list, NULL);
41509 if (iterators)
41511 tree block = poplevel (1, 1, 0);
41512 if (iterators != error_mark_node)
41514 TREE_VEC_ELT (iterators, 5) = block;
41515 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41516 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
41517 OMP_CLAUSE_DECL (c));
41520 return nlist;
41523 /* OpenMP 4.0:
41524 depend ( depend-kind : variable-list )
41526 depend-kind:
41527 in | out | inout
41529 OpenMP 4.5:
41530 depend ( source )
41532 depend ( sink : vec )
41534 OpenMP 5.0:
41535 depend ( depend-modifier , depend-kind: variable-list )
41537 depend-kind:
41538 in | out | inout | mutexinoutset | depobj
41540 depend-modifier:
41541 iterator ( iterators-definition ) */
41543 static tree
41544 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
41546 tree nlist, c, iterators = NULL_TREE;
41547 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
41548 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
41550 matching_parens parens;
41551 if (!parens.require_open (parser))
41552 return list;
41556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41557 goto invalid_kind;
41559 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41560 const char *p = IDENTIFIER_POINTER (id);
41562 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
41564 begin_scope (sk_omp, NULL);
41565 iterators = cp_parser_omp_iterators (parser);
41566 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
41567 continue;
41569 if (strcmp ("in", p) == 0)
41570 kind = OMP_CLAUSE_DEPEND_IN;
41571 else if (strcmp ("inout", p) == 0)
41572 kind = OMP_CLAUSE_DEPEND_INOUT;
41573 else if (strcmp ("inoutset", p) == 0)
41574 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41575 else if (strcmp ("mutexinoutset", p) == 0)
41576 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41577 else if (strcmp ("out", p) == 0)
41578 kind = OMP_CLAUSE_DEPEND_OUT;
41579 else if (strcmp ("depobj", p) == 0)
41580 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
41581 else if (strcmp ("sink", p) == 0)
41582 dkind = OMP_CLAUSE_DOACROSS_SINK;
41583 else if (strcmp ("source", p) == 0)
41584 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
41585 else
41586 goto invalid_kind;
41587 break;
41589 while (1);
41591 cp_lexer_consume_token (parser->lexer);
41593 if (iterators
41594 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
41595 || dkind == OMP_CLAUSE_DOACROSS_SINK))
41597 poplevel (0, 1, 0);
41598 error_at (loc, "%<iterator%> modifier incompatible with %qs",
41599 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
41600 iterators = NULL_TREE;
41603 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
41605 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41606 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
41607 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
41608 OMP_CLAUSE_DECL (c) = NULL_TREE;
41609 OMP_CLAUSE_CHAIN (c) = list;
41610 if (!parens.require_close (parser))
41611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41612 /*or_comma=*/false,
41613 /*consume_paren=*/true);
41614 return c;
41617 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41618 goto resync_fail;
41620 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
41622 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
41623 if (!parens.require_close (parser))
41624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41625 /*or_comma=*/false,
41626 /*consume_paren=*/true);
41628 else
41630 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
41631 list, NULL);
41633 if (iterators)
41635 tree block = poplevel (1, 1, 0);
41636 if (iterators == error_mark_node)
41637 iterators = NULL_TREE;
41638 else
41639 TREE_VEC_ELT (iterators, 5) = block;
41642 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41644 OMP_CLAUSE_DEPEND_KIND (c) = kind;
41645 if (iterators)
41646 OMP_CLAUSE_DECL (c)
41647 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
41650 return nlist;
41652 invalid_kind:
41653 cp_parser_error (parser, "invalid depend kind");
41654 resync_fail:
41655 if (iterators)
41656 poplevel (0, 1, 0);
41657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41658 /*or_comma=*/false,
41659 /*consume_paren=*/true);
41660 return list;
41663 /* OpenMP 5.2:
41664 doacross ( source : )
41665 doacross ( source : omp_cur_iteration )
41667 doacross ( sink : vec )
41668 doacross ( sink : omp_cur_iteration - logical_iteration ) */
41670 static tree
41671 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
41673 tree nlist;
41674 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
41676 matching_parens parens;
41677 if (!parens.require_open (parser))
41678 return list;
41680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41682 invalid_kind:
41683 cp_parser_error (parser, "invalid doacross kind");
41684 resync_fail:
41685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41686 /*or_comma=*/false,
41687 /*consume_paren=*/true);
41688 return list;
41691 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41692 const char *p = IDENTIFIER_POINTER (id);
41694 if (strcmp ("sink", p) == 0)
41695 kind = OMP_CLAUSE_DOACROSS_SINK;
41696 else if (strcmp ("source", p) == 0)
41697 kind = OMP_CLAUSE_DOACROSS_SOURCE;
41698 else
41699 goto invalid_kind;
41701 cp_lexer_consume_token (parser->lexer);
41703 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41704 goto resync_fail;
41706 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
41708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41710 id = cp_lexer_peek_token (parser->lexer)->u.value;
41711 p = IDENTIFIER_POINTER (id);
41712 if (strcmp (p, "omp_cur_iteration") == 0)
41713 cp_lexer_consume_token (parser->lexer);
41715 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41716 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
41717 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
41718 OMP_CLAUSE_CHAIN (nlist) = list;
41720 else
41721 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
41723 if (!parens.require_close (parser))
41724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41725 /*or_comma=*/false,
41726 /*consume_paren=*/true);
41727 return nlist;
41730 /* OpenMP 4.0:
41731 from ( variable-list )
41732 to ( variable-list )
41734 OpenMP 5.1:
41735 from ( [present :] variable-list )
41736 to ( [present :] variable-list ) */
41738 static tree
41739 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
41740 tree list)
41742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41743 return list;
41745 bool present = false;
41746 cp_token *token = cp_lexer_peek_token (parser->lexer);
41748 if (token->type == CPP_NAME
41749 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
41750 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41752 present = true;
41753 cp_lexer_consume_token (parser->lexer);
41754 cp_lexer_consume_token (parser->lexer);
41757 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
41758 if (present)
41759 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
41760 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
41762 return nl;
41765 /* OpenMP 4.0:
41766 map ( map-kind : variable-list )
41767 map ( variable-list )
41769 map-kind:
41770 alloc | to | from | tofrom
41772 OpenMP 4.5:
41773 map-kind:
41774 alloc | to | from | tofrom | release | delete
41776 map ( always [,] map-kind: variable-list )
41778 OpenMP 5.0:
41779 map ( [map-type-modifier[,] ...] map-kind: variable-list )
41781 map-type-modifier:
41782 always | close */
41784 static tree
41785 cp_parser_omp_clause_map (cp_parser *parser, tree list)
41787 tree nlist, c;
41788 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
41790 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41791 return list;
41793 int pos = 1;
41794 int map_kind_pos = 0;
41795 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
41796 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
41798 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
41800 map_kind_pos = pos;
41801 break;
41804 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
41805 pos++;
41806 pos++;
41809 bool always_modifier = false;
41810 bool close_modifier = false;
41811 bool present_modifier = false;
41812 for (int pos = 1; pos < map_kind_pos; ++pos)
41814 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41815 if (tok->type == CPP_COMMA)
41817 cp_lexer_consume_token (parser->lexer);
41818 continue;
41821 const char *p = IDENTIFIER_POINTER (tok->u.value);
41822 if (strcmp ("always", p) == 0)
41824 if (always_modifier)
41826 cp_parser_error (parser, "too many %<always%> modifiers");
41827 cp_parser_skip_to_closing_parenthesis (parser,
41828 /*recovering=*/true,
41829 /*or_comma=*/false,
41830 /*consume_paren=*/true);
41831 return list;
41833 always_modifier = true;
41835 else if (strcmp ("close", p) == 0)
41837 if (close_modifier)
41839 cp_parser_error (parser, "too many %<close%> modifiers");
41840 cp_parser_skip_to_closing_parenthesis (parser,
41841 /*recovering=*/true,
41842 /*or_comma=*/false,
41843 /*consume_paren=*/true);
41844 return list;
41846 close_modifier = true;
41848 else if (strcmp ("present", p) == 0)
41850 if (present_modifier)
41852 cp_parser_error (parser, "too many %<present%> modifiers");
41853 cp_parser_skip_to_closing_parenthesis (parser,
41854 /*recovering=*/true,
41855 /*or_comma=*/false,
41856 /*consume_paren=*/true);
41857 return list;
41859 present_modifier = true;
41861 else
41863 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
41864 " than %<always%>, %<close%> or %<present%>");
41865 cp_parser_skip_to_closing_parenthesis (parser,
41866 /*recovering=*/true,
41867 /*or_comma=*/false,
41868 /*consume_paren=*/true);
41869 return list;
41872 cp_lexer_consume_token (parser->lexer);
41875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41876 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41879 const char *p = IDENTIFIER_POINTER (id);
41880 int always_present_modifier = always_modifier && present_modifier;
41882 if (strcmp ("alloc", p) == 0)
41883 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
41884 else if (strcmp ("to", p) == 0)
41885 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
41886 : present_modifier ? GOMP_MAP_PRESENT_TO
41887 : always_modifier ? GOMP_MAP_ALWAYS_TO
41888 : GOMP_MAP_TO);
41889 else if (strcmp ("from", p) == 0)
41890 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
41891 : present_modifier ? GOMP_MAP_PRESENT_FROM
41892 : always_modifier ? GOMP_MAP_ALWAYS_FROM
41893 : GOMP_MAP_FROM);
41894 else if (strcmp ("tofrom", p) == 0)
41895 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
41896 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
41897 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
41898 : GOMP_MAP_TOFROM);
41899 else if (strcmp ("release", p) == 0)
41900 kind = GOMP_MAP_RELEASE;
41901 else
41903 cp_parser_error (parser, "invalid map kind");
41904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41905 /*or_comma=*/false,
41906 /*consume_paren=*/true);
41907 return list;
41909 cp_lexer_consume_token (parser->lexer);
41910 cp_lexer_consume_token (parser->lexer);
41912 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
41913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41915 kind = GOMP_MAP_DELETE;
41916 cp_lexer_consume_token (parser->lexer);
41917 cp_lexer_consume_token (parser->lexer);
41920 /* We introduce a scope here so that errors parsing e.g. "always", "close"
41921 tokens do not propagate to later directives that might use them
41922 legally. */
41923 begin_scope (sk_omp, NULL);
41924 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
41925 NULL, true);
41926 finish_scope ();
41928 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41929 OMP_CLAUSE_SET_MAP_KIND (c, kind);
41931 return nlist;
41934 /* OpenMP 4.0:
41935 device ( expression )
41937 OpenMP 5.0:
41938 device ( [device-modifier :] integer-expression )
41940 device-modifier:
41941 ancestor | device_num */
41943 static tree
41944 cp_parser_omp_clause_device (cp_parser *parser, tree list,
41945 location_t location)
41947 tree t, c;
41948 bool ancestor = false;
41950 matching_parens parens;
41951 if (!parens.require_open (parser))
41952 return list;
41954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41955 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41957 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41958 const char *p = IDENTIFIER_POINTER (tok->u.value);
41959 if (strcmp ("ancestor", p) == 0)
41961 ancestor = true;
41963 /* A requires directive with the reverse_offload clause must be
41964 specified. */
41965 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
41967 error_at (tok->location, "%<ancestor%> device modifier not "
41968 "preceded by %<requires%> directive "
41969 "with %<reverse_offload%> clause");
41970 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41971 return list;
41974 else if (strcmp ("device_num", p) == 0)
41976 else
41978 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41979 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41980 return list;
41982 cp_lexer_consume_token (parser->lexer);
41983 cp_lexer_consume_token (parser->lexer);
41986 t = cp_parser_assignment_expression (parser);
41988 if (t == error_mark_node
41989 || !parens.require_close (parser))
41990 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41991 /*or_comma=*/false,
41992 /*consume_paren=*/true);
41994 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41995 "device", location);
41997 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41998 OMP_CLAUSE_DEVICE_ID (c) = t;
41999 OMP_CLAUSE_CHAIN (c) = list;
42000 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
42002 return c;
42005 /* OpenMP 4.0:
42006 dist_schedule ( static )
42007 dist_schedule ( static , expression ) */
42009 static tree
42010 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
42011 location_t location)
42013 tree c, t;
42015 matching_parens parens;
42016 if (!parens.require_open (parser))
42017 return list;
42019 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
42021 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
42022 goto invalid_kind;
42023 cp_lexer_consume_token (parser->lexer);
42025 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42027 cp_lexer_consume_token (parser->lexer);
42029 t = cp_parser_assignment_expression (parser);
42031 if (t == error_mark_node)
42032 goto resync_fail;
42033 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
42035 if (!parens.require_close (parser))
42036 goto resync_fail;
42038 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
42039 goto resync_fail;
42041 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
42042 "dist_schedule", location); */
42043 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
42044 warning_at (location, OPT_Wopenmp, "too many %qs clauses", "dist_schedule");
42045 OMP_CLAUSE_CHAIN (c) = list;
42046 return c;
42048 invalid_kind:
42049 cp_parser_error (parser, "invalid dist_schedule kind");
42050 resync_fail:
42051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42052 /*or_comma=*/false,
42053 /*consume_paren=*/true);
42054 return list;
42057 /* OpenMP 4.0:
42058 proc_bind ( proc-bind-kind )
42060 proc-bind-kind:
42061 primary | master | close | spread
42062 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
42064 static tree
42065 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
42066 location_t location)
42068 tree c;
42069 enum omp_clause_proc_bind_kind kind;
42071 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42072 return list;
42074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42076 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42077 const char *p = IDENTIFIER_POINTER (id);
42079 if (strcmp ("primary", p) == 0)
42080 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
42081 else if (strcmp ("master", p) == 0)
42082 kind = OMP_CLAUSE_PROC_BIND_MASTER;
42083 else if (strcmp ("close", p) == 0)
42084 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
42085 else if (strcmp ("spread", p) == 0)
42086 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
42087 else
42088 goto invalid_kind;
42090 else
42091 goto invalid_kind;
42093 cp_lexer_consume_token (parser->lexer);
42094 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
42095 goto resync_fail;
42097 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
42098 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
42099 location);
42100 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
42101 OMP_CLAUSE_CHAIN (c) = list;
42102 return c;
42104 invalid_kind:
42105 cp_parser_error (parser, "invalid depend kind");
42106 resync_fail:
42107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42108 /*or_comma=*/false,
42109 /*consume_paren=*/true);
42110 return list;
42113 /* OpenMP 5.0:
42114 device_type ( host | nohost | any ) */
42116 static tree
42117 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
42118 location_t location)
42120 tree c;
42121 enum omp_clause_device_type_kind kind;
42123 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42124 return list;
42126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42129 const char *p = IDENTIFIER_POINTER (id);
42131 if (strcmp ("host", p) == 0)
42132 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
42133 else if (strcmp ("nohost", p) == 0)
42134 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
42135 else if (strcmp ("any", p) == 0)
42136 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
42137 else
42138 goto invalid_kind;
42140 else
42141 goto invalid_kind;
42143 cp_lexer_consume_token (parser->lexer);
42144 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
42145 goto resync_fail;
42147 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
42148 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
42149 location);
42150 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
42151 OMP_CLAUSE_CHAIN (c) = list;
42152 return c;
42154 invalid_kind:
42155 cp_parser_error (parser, "invalid depend kind");
42156 resync_fail:
42157 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42158 /*or_comma=*/false,
42159 /*consume_paren=*/true);
42160 return list;
42163 /* OpenACC:
42164 async [( int-expr )] */
42166 static tree
42167 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
42169 tree c, t;
42170 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42172 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
42174 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42176 matching_parens parens;
42177 parens.consume_open (parser);
42179 t = cp_parser_assignment_expression (parser);
42180 if (t == error_mark_node
42181 || !parens.require_close (parser))
42182 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42183 /*or_comma=*/false,
42184 /*consume_paren=*/true);
42187 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
42189 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
42190 OMP_CLAUSE_ASYNC_EXPR (c) = t;
42191 OMP_CLAUSE_CHAIN (c) = list;
42192 list = c;
42194 return list;
42197 /* OpenACC 2.7:
42198 self [( expression )] */
42200 static tree
42201 cp_parser_oacc_compute_clause_self (cp_parser *parser, tree list)
42203 tree t;
42204 location_t location = cp_lexer_peek_token (parser->lexer)->location;
42205 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42207 matching_parens parens;
42208 parens.consume_open (parser);
42209 t = cp_parser_assignment_expression (parser);
42210 if (t == error_mark_node
42211 || !parens.require_close (parser))
42213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42214 /*or_comma=*/false,
42215 /*consume_paren=*/true);
42216 return list;
42219 else
42220 t = truthvalue_true_node;
42222 for (tree c = list; c; c = OMP_CLAUSE_CHAIN (c))
42223 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SELF)
42225 error_at (location, "too many %<self%> clauses");
42226 return list;
42229 tree c = build_omp_clause (location, OMP_CLAUSE_SELF);
42230 OMP_CLAUSE_SELF_EXPR (c) = t;
42231 OMP_CLAUSE_CHAIN (c) = list;
42232 return c;
42235 /* Parse all OpenACC clauses. The set clauses allowed by the directive
42236 is a bitmask in MASK. Return the list of clauses found. */
42238 static tree
42239 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
42240 const char *where, cp_token *pragma_tok,
42241 bool finish_p = true, bool target_p = false)
42243 tree clauses = NULL;
42244 bool first = true;
42246 /* Don't create location wrapper nodes within OpenACC clauses. */
42247 auto_suppress_location_wrappers sentinel;
42249 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42251 location_t here;
42252 pragma_omp_clause c_kind;
42253 omp_clause_code code;
42254 const char *c_name;
42255 tree prev = clauses;
42257 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42258 cp_lexer_consume_token (parser->lexer);
42260 here = cp_lexer_peek_token (parser->lexer)->location;
42261 c_kind = cp_parser_omp_clause_name (parser);
42263 switch (c_kind)
42265 case PRAGMA_OACC_CLAUSE_ASYNC:
42266 clauses = cp_parser_oacc_clause_async (parser, clauses);
42267 c_name = "async";
42268 break;
42269 case PRAGMA_OACC_CLAUSE_AUTO:
42270 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
42271 clauses);
42272 c_name = "auto";
42273 break;
42274 case PRAGMA_OACC_CLAUSE_ATTACH:
42275 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42276 c_name = "attach";
42277 break;
42278 case PRAGMA_OACC_CLAUSE_COLLAPSE:
42279 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
42280 c_name = "collapse";
42281 break;
42282 case PRAGMA_OACC_CLAUSE_COPY:
42283 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42284 c_name = "copy";
42285 break;
42286 case PRAGMA_OACC_CLAUSE_COPYIN:
42287 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42288 c_name = "copyin";
42289 break;
42290 case PRAGMA_OACC_CLAUSE_COPYOUT:
42291 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42292 c_name = "copyout";
42293 break;
42294 case PRAGMA_OACC_CLAUSE_CREATE:
42295 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42296 c_name = "create";
42297 break;
42298 case PRAGMA_OACC_CLAUSE_DELETE:
42299 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42300 c_name = "delete";
42301 break;
42302 case PRAGMA_OMP_CLAUSE_DEFAULT:
42303 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
42304 c_name = "default";
42305 break;
42306 case PRAGMA_OACC_CLAUSE_DETACH:
42307 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42308 c_name = "detach";
42309 break;
42310 case PRAGMA_OACC_CLAUSE_DEVICE:
42311 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42312 c_name = "device";
42313 break;
42314 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
42315 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
42316 c_name = "deviceptr";
42317 break;
42318 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
42319 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42320 c_name = "device_resident";
42321 break;
42322 case PRAGMA_OACC_CLAUSE_FINALIZE:
42323 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
42324 clauses);
42325 c_name = "finalize";
42326 break;
42327 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
42328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42329 clauses);
42330 c_name = "firstprivate";
42331 break;
42332 case PRAGMA_OACC_CLAUSE_GANG:
42333 c_name = "gang";
42334 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
42335 c_name, clauses);
42336 break;
42337 case PRAGMA_OACC_CLAUSE_HOST:
42338 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42339 c_name = "host";
42340 break;
42341 case PRAGMA_OACC_CLAUSE_IF:
42342 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
42343 c_name = "if";
42344 break;
42345 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
42346 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
42347 clauses);
42348 c_name = "if_present";
42349 break;
42350 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
42351 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
42352 clauses);
42353 c_name = "independent";
42354 break;
42355 case PRAGMA_OACC_CLAUSE_LINK:
42356 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42357 c_name = "link";
42358 break;
42359 case PRAGMA_OACC_CLAUSE_NO_CREATE:
42360 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42361 c_name = "no_create";
42362 break;
42363 case PRAGMA_OACC_CLAUSE_NOHOST:
42364 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
42365 clauses);
42366 c_name = "nohost";
42367 break;
42368 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
42369 code = OMP_CLAUSE_NUM_GANGS;
42370 c_name = "num_gangs";
42371 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42372 clauses);
42373 break;
42374 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
42375 c_name = "num_workers";
42376 code = OMP_CLAUSE_NUM_WORKERS;
42377 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42378 clauses);
42379 break;
42380 case PRAGMA_OACC_CLAUSE_PRESENT:
42381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42382 c_name = "present";
42383 break;
42384 case PRAGMA_OACC_CLAUSE_PRIVATE:
42385 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42386 clauses);
42387 c_name = "private";
42388 break;
42389 case PRAGMA_OACC_CLAUSE_REDUCTION:
42390 clauses
42391 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42392 false, clauses);
42393 c_name = "reduction";
42394 break;
42395 case PRAGMA_OACC_CLAUSE_SELF:
42396 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)) == 0)
42397 /* OpenACC compute construct */
42398 clauses = cp_parser_oacc_compute_clause_self (parser, clauses);
42399 else
42400 /* OpenACC 'update' directive */
42401 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42402 c_name = "self";
42403 break;
42404 case PRAGMA_OACC_CLAUSE_SEQ:
42405 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
42406 clauses);
42407 c_name = "seq";
42408 break;
42409 case PRAGMA_OACC_CLAUSE_TILE:
42410 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
42411 c_name = "tile";
42412 break;
42413 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
42414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42415 clauses);
42416 c_name = "use_device";
42417 break;
42418 case PRAGMA_OACC_CLAUSE_VECTOR:
42419 c_name = "vector";
42420 clauses = cp_parser_oacc_shape_clause (parser, here,
42421 OMP_CLAUSE_VECTOR,
42422 c_name, clauses);
42423 break;
42424 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
42425 c_name = "vector_length";
42426 code = OMP_CLAUSE_VECTOR_LENGTH;
42427 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42428 clauses);
42429 break;
42430 case PRAGMA_OACC_CLAUSE_WAIT:
42431 clauses = cp_parser_oacc_clause_wait (parser, clauses);
42432 c_name = "wait";
42433 break;
42434 case PRAGMA_OACC_CLAUSE_WORKER:
42435 c_name = "worker";
42436 clauses = cp_parser_oacc_shape_clause (parser, here,
42437 OMP_CLAUSE_WORKER,
42438 c_name, clauses);
42439 break;
42440 default:
42441 cp_parser_error (parser, "expected an OpenACC clause");
42442 goto saw_error;
42445 first = false;
42447 if (((mask >> c_kind) & 1) == 0)
42449 /* Remove the invalid clause(s) from the list to avoid
42450 confusing the rest of the compiler. */
42451 clauses = prev;
42452 error_at (here, "%qs is not valid for %qs", c_name, where);
42456 saw_error:
42457 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42459 if (finish_p)
42460 return finish_omp_clauses (clauses, target_p ? C_ORT_ACC_TARGET
42461 : C_ORT_ACC);
42463 return clauses;
42466 /* Parse all OpenMP clauses. The set clauses allowed by the directive
42467 is a bitmask in MASK. Return the list of clauses found.
42468 FINISH_P set if finish_omp_clauses should be called.
42469 NESTED non-zero if clauses should be terminated by closing paren instead
42470 of end of pragma. If it is 2, additionally commas are required in between
42471 the clauses. */
42473 static tree
42474 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
42475 const char *where, cp_token *pragma_tok,
42476 bool finish_p = true, int nested = 0)
42478 tree clauses = NULL;
42479 bool first = true;
42480 cp_token *token = NULL;
42482 /* Don't create location wrapper nodes within OpenMP clauses. */
42483 auto_suppress_location_wrappers sentinel;
42485 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42487 pragma_omp_clause c_kind;
42488 const char *c_name;
42489 tree prev = clauses;
42491 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
42492 break;
42494 if (!first || nested != 2)
42496 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42497 cp_lexer_consume_token (parser->lexer);
42498 else if (nested == 2)
42499 error_at (cp_lexer_peek_token (parser->lexer)->location,
42500 "clauses in %<simd%> trait should be separated "
42501 "by %<,%>");
42504 token = cp_lexer_peek_token (parser->lexer);
42505 c_kind = cp_parser_omp_clause_name (parser);
42507 switch (c_kind)
42509 case PRAGMA_OMP_CLAUSE_BIND:
42510 clauses = cp_parser_omp_clause_bind (parser, clauses,
42511 token->location);
42512 c_name = "bind";
42513 break;
42514 case PRAGMA_OMP_CLAUSE_COLLAPSE:
42515 clauses = cp_parser_omp_clause_collapse (parser, clauses,
42516 token->location);
42517 c_name = "collapse";
42518 break;
42519 case PRAGMA_OMP_CLAUSE_COPYIN:
42520 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
42521 c_name = "copyin";
42522 break;
42523 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
42524 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
42525 clauses);
42526 c_name = "copyprivate";
42527 break;
42528 case PRAGMA_OMP_CLAUSE_DEFAULT:
42529 clauses = cp_parser_omp_clause_default (parser, clauses,
42530 token->location, false);
42531 c_name = "default";
42532 break;
42533 case PRAGMA_OMP_CLAUSE_FILTER:
42534 clauses = cp_parser_omp_clause_filter (parser, clauses,
42535 token->location);
42536 c_name = "filter";
42537 break;
42538 case PRAGMA_OMP_CLAUSE_FINAL:
42539 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
42540 c_name = "final";
42541 break;
42542 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
42543 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42544 clauses);
42545 c_name = "firstprivate";
42546 break;
42547 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
42548 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
42549 token->location);
42550 c_name = "grainsize";
42551 break;
42552 case PRAGMA_OMP_CLAUSE_HINT:
42553 clauses = cp_parser_omp_clause_hint (parser, clauses,
42554 token->location);
42555 c_name = "hint";
42556 break;
42557 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
42558 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
42559 token->location);
42560 c_name = "defaultmap";
42561 break;
42562 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
42563 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42564 clauses);
42565 c_name = "use_device_ptr";
42566 break;
42567 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
42568 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
42569 clauses);
42570 c_name = "use_device_addr";
42571 break;
42572 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
42573 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
42574 clauses);
42575 c_name = "is_device_ptr";
42576 break;
42577 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
42578 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
42579 clauses);
42580 c_name = "has_device_addr";
42581 break;
42582 case PRAGMA_OMP_CLAUSE_IF:
42583 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
42584 true);
42585 c_name = "if";
42586 break;
42587 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
42588 clauses
42589 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
42590 true, clauses);
42591 c_name = "in_reduction";
42592 break;
42593 case PRAGMA_OMP_CLAUSE_INDIRECT:
42594 clauses = cp_parser_omp_clause_indirect (parser, clauses,
42595 token->location);
42596 c_name = "indirect";
42597 break;
42598 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
42599 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
42600 c_name = "lastprivate";
42601 break;
42602 case PRAGMA_OMP_CLAUSE_MERGEABLE:
42603 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
42604 token->location);
42605 c_name = "mergeable";
42606 break;
42607 case PRAGMA_OMP_CLAUSE_NOWAIT:
42608 clauses = cp_parser_omp_clause_nowait (parser, clauses,
42609 token->location);
42610 c_name = "nowait";
42611 break;
42612 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
42613 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
42614 token->location);
42615 c_name = "num_tasks";
42616 break;
42617 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
42618 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
42619 token->location);
42620 c_name = "num_threads";
42621 break;
42622 case PRAGMA_OMP_CLAUSE_ORDER:
42623 clauses = cp_parser_omp_clause_order (parser, clauses,
42624 token->location);
42625 c_name = "order";
42626 break;
42627 case PRAGMA_OMP_CLAUSE_ORDERED:
42628 clauses = cp_parser_omp_clause_ordered (parser, clauses,
42629 token->location);
42630 c_name = "ordered";
42631 break;
42632 case PRAGMA_OMP_CLAUSE_PRIORITY:
42633 clauses = cp_parser_omp_clause_priority (parser, clauses,
42634 token->location);
42635 c_name = "priority";
42636 break;
42637 case PRAGMA_OMP_CLAUSE_PRIVATE:
42638 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42639 clauses);
42640 c_name = "private";
42641 break;
42642 case PRAGMA_OMP_CLAUSE_REDUCTION:
42643 clauses
42644 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42645 true, clauses);
42646 c_name = "reduction";
42647 break;
42648 case PRAGMA_OMP_CLAUSE_SCHEDULE:
42649 clauses = cp_parser_omp_clause_schedule (parser, clauses,
42650 token->location);
42651 c_name = "schedule";
42652 break;
42653 case PRAGMA_OMP_CLAUSE_SHARED:
42654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
42655 clauses);
42656 c_name = "shared";
42657 break;
42658 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
42659 clauses
42660 = cp_parser_omp_clause_reduction (parser,
42661 OMP_CLAUSE_TASK_REDUCTION,
42662 true, clauses);
42663 c_name = "task_reduction";
42664 break;
42665 case PRAGMA_OMP_CLAUSE_UNTIED:
42666 clauses = cp_parser_omp_clause_untied (parser, clauses,
42667 token->location);
42668 c_name = "untied";
42669 break;
42670 case PRAGMA_OMP_CLAUSE_INBRANCH:
42671 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
42672 clauses, token->location);
42673 c_name = "inbranch";
42674 break;
42675 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
42676 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
42677 clauses);
42678 c_name = "nontemporal";
42679 break;
42680 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
42681 clauses = cp_parser_omp_clause_branch (parser,
42682 OMP_CLAUSE_NOTINBRANCH,
42683 clauses, token->location);
42684 c_name = "notinbranch";
42685 break;
42686 case PRAGMA_OMP_CLAUSE_PARALLEL:
42687 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
42688 clauses, token->location);
42689 c_name = "parallel";
42690 if (!first)
42692 clause_not_first:
42693 error_at (token->location, "%qs must be the first clause of %qs",
42694 c_name, where);
42695 clauses = prev;
42697 break;
42698 case PRAGMA_OMP_CLAUSE_FOR:
42699 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
42700 clauses, token->location);
42701 c_name = "for";
42702 if (!first)
42703 goto clause_not_first;
42704 break;
42705 case PRAGMA_OMP_CLAUSE_SECTIONS:
42706 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
42707 clauses, token->location);
42708 c_name = "sections";
42709 if (!first)
42710 goto clause_not_first;
42711 break;
42712 case PRAGMA_OMP_CLAUSE_TASKGROUP:
42713 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
42714 clauses, token->location);
42715 c_name = "taskgroup";
42716 if (!first)
42717 goto clause_not_first;
42718 break;
42719 case PRAGMA_OMP_CLAUSE_LINK:
42720 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
42721 c_name = "link";
42722 break;
42723 case PRAGMA_OMP_CLAUSE_TO:
42724 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
42726 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42727 clauses);
42728 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
42729 OMP_CLAUSE_ENTER_TO (c) = 1;
42730 clauses = nl;
42732 else
42733 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
42734 clauses);
42735 c_name = "to";
42736 break;
42737 case PRAGMA_OMP_CLAUSE_FROM:
42738 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
42739 clauses);
42740 c_name = "from";
42741 break;
42742 case PRAGMA_OMP_CLAUSE_UNIFORM:
42743 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
42744 clauses);
42745 c_name = "uniform";
42746 break;
42747 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
42748 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
42749 token->location);
42750 c_name = "num_teams";
42751 break;
42752 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
42753 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
42754 token->location);
42755 c_name = "thread_limit";
42756 break;
42757 case PRAGMA_OMP_CLAUSE_ALIGNED:
42758 clauses = cp_parser_omp_clause_aligned (parser, clauses);
42759 c_name = "aligned";
42760 break;
42761 case PRAGMA_OMP_CLAUSE_ALLOCATE:
42762 clauses = cp_parser_omp_clause_allocate (parser, clauses);
42763 c_name = "allocate";
42764 break;
42765 case PRAGMA_OMP_CLAUSE_LINEAR:
42767 bool declare_simd = false;
42768 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
42769 declare_simd = true;
42770 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
42772 c_name = "linear";
42773 break;
42774 case PRAGMA_OMP_CLAUSE_AFFINITY:
42775 clauses = cp_parser_omp_clause_affinity (parser, clauses);
42776 c_name = "affinity";
42777 break;
42778 case PRAGMA_OMP_CLAUSE_DEPEND:
42779 clauses = cp_parser_omp_clause_depend (parser, clauses,
42780 token->location);
42781 c_name = "depend";
42782 break;
42783 case PRAGMA_OMP_CLAUSE_DOACROSS:
42784 clauses = cp_parser_omp_clause_doacross (parser, clauses,
42785 token->location);
42786 c_name = "doacross";
42787 break;
42788 case PRAGMA_OMP_CLAUSE_DETACH:
42789 clauses = cp_parser_omp_clause_detach (parser, clauses);
42790 c_name = "detach";
42791 break;
42792 case PRAGMA_OMP_CLAUSE_MAP:
42793 clauses = cp_parser_omp_clause_map (parser, clauses);
42794 c_name = "map";
42795 break;
42796 case PRAGMA_OMP_CLAUSE_DEVICE:
42797 clauses = cp_parser_omp_clause_device (parser, clauses,
42798 token->location);
42799 c_name = "device";
42800 break;
42801 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
42802 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
42803 token->location);
42804 c_name = "dist_schedule";
42805 break;
42806 case PRAGMA_OMP_CLAUSE_PROC_BIND:
42807 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
42808 token->location);
42809 c_name = "proc_bind";
42810 break;
42811 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
42812 clauses = cp_parser_omp_clause_device_type (parser, clauses,
42813 token->location);
42814 c_name = "device_type";
42815 break;
42816 case PRAGMA_OMP_CLAUSE_SAFELEN:
42817 clauses = cp_parser_omp_clause_safelen (parser, clauses,
42818 token->location);
42819 c_name = "safelen";
42820 break;
42821 case PRAGMA_OMP_CLAUSE_SIMDLEN:
42822 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
42823 token->location);
42824 c_name = "simdlen";
42825 break;
42826 case PRAGMA_OMP_CLAUSE_NOGROUP:
42827 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
42828 token->location);
42829 c_name = "nogroup";
42830 break;
42831 case PRAGMA_OMP_CLAUSE_THREADS:
42832 clauses
42833 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
42834 clauses, token->location);
42835 c_name = "threads";
42836 break;
42837 case PRAGMA_OMP_CLAUSE_SIMD:
42838 clauses
42839 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
42840 clauses, token->location);
42841 c_name = "simd";
42842 break;
42843 case PRAGMA_OMP_CLAUSE_ENTER:
42844 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42845 clauses);
42846 c_name = "enter";
42847 break;
42848 case PRAGMA_OMP_CLAUSE_PARTIAL:
42849 clauses = cp_parser_omp_clause_partial (parser, clauses,
42850 token->location);
42851 c_name = "partial";
42852 break;
42853 case PRAGMA_OMP_CLAUSE_FULL:
42854 clauses = cp_parser_omp_clause_full (clauses, token->location);
42855 c_name = "full";
42856 break;
42857 default:
42858 cp_parser_error (parser, "expected an OpenMP clause");
42859 goto saw_error;
42862 first = false;
42864 if (((mask >> c_kind) & 1) == 0)
42866 /* Remove the invalid clause(s) from the list to avoid
42867 confusing the rest of the compiler. */
42868 clauses = prev;
42869 error_at (token->location, "%qs is not valid for %qs", c_name, where);
42872 saw_error:
42873 if (!nested)
42874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42875 if (finish_p)
42877 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
42878 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
42879 else
42880 return finish_omp_clauses (clauses, C_ORT_OMP);
42882 return clauses;
42885 /* OpenMP 2.5:
42886 structured-block:
42887 statement
42889 In practice, we're also interested in adding the statement to an
42890 outer node. So it is convenient if we work around the fact that
42891 cp_parser_statement calls add_stmt. */
42893 static unsigned
42894 cp_parser_begin_omp_structured_block (cp_parser *parser)
42896 unsigned save = parser->in_statement;
42898 /* Only move the values to IN_OMP_BLOCK if they weren't false.
42899 This preserves the "not within loop or switch" style error messages
42900 for nonsense cases like
42901 void foo() {
42902 #pragma omp single
42903 break;
42906 if (parser->in_statement)
42907 parser->in_statement = IN_OMP_BLOCK;
42909 return save;
42912 static void
42913 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
42915 parser->in_statement = save;
42918 static tree
42919 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
42921 tree stmt = begin_omp_structured_block ();
42922 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42924 parser->omp_attrs_forbidden_p = true;
42925 cp_parser_statement (parser, NULL_TREE, false, if_p);
42927 cp_parser_end_omp_structured_block (parser, save);
42928 return finish_omp_structured_block (stmt);
42931 /* OpenMP 5.x:
42932 # pragma omp allocate (list) clauses
42934 OpenMP 5.0 clause:
42935 allocator (omp_allocator_handle_t expression)
42937 OpenMP 5.1 additional clause:
42938 align (constant-expression)] */
42940 static void
42941 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
42943 tree allocator = NULL_TREE;
42944 tree alignment = NULL_TREE;
42945 location_t loc = pragma_tok->location;
42946 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
42950 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42951 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42952 cp_lexer_consume_token (parser->lexer);
42954 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42955 break;
42956 matching_parens parens;
42957 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42958 const char *p = IDENTIFIER_POINTER (id);
42959 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42960 cp_lexer_consume_token (parser->lexer);
42961 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
42963 error_at (cloc, "expected %<allocator%> or %<align%>");
42964 break;
42966 if (!parens.require_open (parser))
42967 break;
42968 tree expr = cp_parser_assignment_expression (parser);
42969 if (p[2] == 'i' && alignment)
42971 error_at (cloc, "too many %qs clauses", "align");
42972 break;
42974 else if (p[2] == 'i')
42976 if (expr != error_mark_node)
42977 alignment = expr;
42978 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
42979 if (alignment
42980 && !type_dependent_expression_p (alignment)
42981 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
42983 error_at (cloc, "%<align%> clause argument needs to be "
42984 "positive constant power of two integer "
42985 "expression");
42986 alignment = NULL_TREE;
42988 else if (alignment)
42990 alignment = mark_rvalue_use (alignment);
42991 if (!processing_template_decl)
42993 alignment = maybe_constant_value (alignment);
42994 if (TREE_CODE (alignment) != INTEGER_CST
42995 || !tree_fits_uhwi_p (alignment)
42996 || !integer_pow2p (alignment))
42998 error_at (cloc, "%<align%> clause argument needs to be "
42999 "positive constant power of two integer "
43000 "expression");
43001 alignment = NULL_TREE;
43006 else if (allocator)
43008 error_at (cloc, "too many %qs clauses", "allocator");
43009 break;
43011 else
43013 if (expr != error_mark_node)
43014 allocator = expr;
43016 parens.require_close (parser);
43017 } while (true);
43018 cp_parser_require_pragma_eol (parser, pragma_tok);
43020 if (allocator || alignment)
43021 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
43023 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
43024 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
43027 /* FIXME: When implementing properly, delete the align/allocate expr error
43028 check above and add one in semantics.cc (to properly handle templates).
43029 Base this on the allocator/align modifiers check for the 'allocate' clause
43030 in semantics.cc's finish_omp_clauses. */
43031 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
43034 /* OpenMP 2.5:
43035 # pragma omp atomic new-line
43036 expression-stmt
43038 expression-stmt:
43039 x binop= expr | x++ | ++x | x-- | --x
43040 binop:
43041 +, *, -, /, &, ^, |, <<, >>
43043 where x is an lvalue expression with scalar type.
43045 OpenMP 3.1:
43046 # pragma omp atomic new-line
43047 update-stmt
43049 # pragma omp atomic read new-line
43050 read-stmt
43052 # pragma omp atomic write new-line
43053 write-stmt
43055 # pragma omp atomic update new-line
43056 update-stmt
43058 # pragma omp atomic capture new-line
43059 capture-stmt
43061 # pragma omp atomic capture new-line
43062 capture-block
43064 read-stmt:
43065 v = x
43066 write-stmt:
43067 x = expr
43068 update-stmt:
43069 expression-stmt | x = x binop expr
43070 capture-stmt:
43071 v = expression-stmt
43072 capture-block:
43073 { v = x; update-stmt; } | { update-stmt; v = x; }
43075 OpenMP 4.0:
43076 update-stmt:
43077 expression-stmt | x = x binop expr | x = expr binop x
43078 capture-stmt:
43079 v = update-stmt
43080 capture-block:
43081 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
43083 OpenMP 5.1:
43084 # pragma omp atomic compare new-line
43085 conditional-update-atomic
43087 # pragma omp atomic compare capture new-line
43088 conditional-update-capture-atomic
43090 conditional-update-atomic:
43091 cond-expr-stmt | cond-update-stmt
43092 cond-expr-stmt:
43093 x = expr ordop x ? expr : x;
43094 x = x ordop expr ? expr : x;
43095 x = x == e ? d : x;
43096 cond-update-stmt:
43097 if (expr ordop x) { x = expr; }
43098 if (x ordop expr) { x = expr; }
43099 if (x == e) { x = d; }
43100 ordop:
43101 <, >
43102 conditional-update-capture-atomic:
43103 v = cond-expr-stmt
43104 { v = x; cond-expr-stmt }
43105 { cond-expr-stmt v = x; }
43106 { v = x; cond-update-stmt }
43107 { cond-update-stmt v = x; }
43108 if (x == e) { x = d; } else { v = x; }
43109 { r = x == e; if (r) { x = d; } }
43110 { r = x == e; if (r) { x = d; } else { v = x; } }
43112 where x, r and v are lvalue expressions with scalar type,
43113 expr, e and d are expressions with scalar type and e might be
43114 the same as v. */
43116 static void
43117 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
43119 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
43120 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
43121 location_t loc = pragma_tok->location;
43122 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
43123 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
43124 bool structured_block = false;
43125 tree clauses = NULL_TREE;
43126 bool capture = false;
43127 bool compare = false;
43128 bool weak = false;
43129 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
43130 bool no_semicolon = false;
43131 bool extra_scope = false;
43133 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43136 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43137 cp_lexer_consume_token (parser->lexer);
43139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43141 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43142 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
43143 const char *p = IDENTIFIER_POINTER (id);
43144 enum tree_code new_code = ERROR_MARK;
43145 enum omp_memory_order new_memory_order
43146 = OMP_MEMORY_ORDER_UNSPECIFIED;
43147 bool new_capture = false;
43148 bool new_compare = false;
43149 bool new_weak = false;
43150 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
43152 if (!strcmp (p, "read"))
43153 new_code = OMP_ATOMIC_READ;
43154 else if (!strcmp (p, "write"))
43155 new_code = NOP_EXPR;
43156 else if (!strcmp (p, "update"))
43157 new_code = OMP_ATOMIC;
43158 else if (openacc && !strcmp (p, "capture"))
43159 new_code = OMP_ATOMIC_CAPTURE_NEW;
43160 else if (openacc)
43162 p = NULL;
43163 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
43164 "or %<capture%> clause");
43166 else if (!strcmp (p, "capture"))
43167 new_capture = true;
43168 else if (!strcmp (p, "compare"))
43169 new_compare = true;
43170 else if (!strcmp (p, "weak"))
43171 new_weak = true;
43172 else if (!strcmp (p, "fail"))
43174 matching_parens parens;
43176 cp_lexer_consume_token (parser->lexer);
43177 if (!parens.require_open (parser))
43178 continue;
43180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43182 id = cp_lexer_peek_token (parser->lexer)->u.value;
43183 const char *q = IDENTIFIER_POINTER (id);
43185 if (!strcmp (q, "seq_cst"))
43186 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
43187 else if (!strcmp (q, "acquire"))
43188 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
43189 else if (!strcmp (q, "relaxed"))
43190 new_fail = OMP_MEMORY_ORDER_RELAXED;
43193 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
43195 cp_lexer_consume_token (parser->lexer);
43196 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
43197 error_at (cloc, "too many %qs clauses", "fail");
43198 else
43199 fail = new_fail;
43201 else
43202 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
43203 "or %<relaxed%>");
43204 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
43205 || !parens.require_close (parser))
43206 cp_parser_skip_to_closing_parenthesis (parser,
43207 /*recovering=*/true,
43208 /*or_comma=*/false,
43209 /*consume_paren=*/true);
43210 continue;
43212 else if (!strcmp (p, "seq_cst"))
43213 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43214 else if (!strcmp (p, "acq_rel"))
43215 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
43216 else if (!strcmp (p, "release"))
43217 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
43218 else if (!strcmp (p, "acquire"))
43219 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43220 else if (!strcmp (p, "relaxed"))
43221 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
43222 else if (!strcmp (p, "hint"))
43224 cp_lexer_consume_token (parser->lexer);
43225 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
43226 continue;
43228 else
43230 p = NULL;
43231 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
43232 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
43233 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
43234 "%<relaxed%> or %<hint%> clause");
43236 if (p)
43238 if (new_code != ERROR_MARK)
43240 /* OpenACC permits 'update capture'. */
43241 if (openacc
43242 && code == OMP_ATOMIC
43243 && new_code == OMP_ATOMIC_CAPTURE_NEW)
43244 code = new_code;
43245 else if (code != ERROR_MARK)
43246 error_at (cloc, "too many atomic clauses");
43247 else
43248 code = new_code;
43250 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
43252 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
43253 error_at (cloc, "too many memory order clauses");
43254 else
43255 memory_order = new_memory_order;
43257 else if (new_capture)
43259 if (capture)
43260 error_at (cloc, "too many %qs clauses", "capture");
43261 else
43262 capture = true;
43264 else if (new_compare)
43266 if (compare)
43267 error_at (cloc, "too many %qs clauses", "compare");
43268 else
43269 compare = true;
43271 else if (new_weak)
43273 if (weak)
43274 error_at (cloc, "too many %qs clauses", "weak");
43275 else
43276 weak = true;
43278 cp_lexer_consume_token (parser->lexer);
43279 continue;
43282 break;
43284 cp_parser_require_pragma_eol (parser, pragma_tok);
43286 if (code == ERROR_MARK)
43287 code = OMP_ATOMIC;
43288 if (capture)
43290 if (code != OMP_ATOMIC)
43291 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
43292 "clauses", "capture");
43293 else
43294 code = OMP_ATOMIC_CAPTURE_NEW;
43296 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
43298 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
43299 "clauses", "compare");
43300 compare = false;
43302 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
43304 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
43305 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
43307 if (weak && !compare)
43309 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
43310 weak = false;
43312 if (openacc)
43313 memory_order = OMP_MEMORY_ORDER_RELAXED;
43314 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
43316 omp_requires_mask
43317 = (enum omp_requires) (omp_requires_mask
43318 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
43319 switch ((enum omp_memory_order)
43320 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
43322 case OMP_MEMORY_ORDER_UNSPECIFIED:
43323 case OMP_MEMORY_ORDER_RELAXED:
43324 memory_order = OMP_MEMORY_ORDER_RELAXED;
43325 break;
43326 case OMP_MEMORY_ORDER_SEQ_CST:
43327 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43328 break;
43329 case OMP_MEMORY_ORDER_ACQUIRE:
43330 if (code == NOP_EXPR) /* atomic write */
43332 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
43333 "%<acquire%> clause implicitly provided by a "
43334 "%<requires%> directive");
43335 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43337 else
43338 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43339 break;
43340 case OMP_MEMORY_ORDER_RELEASE:
43341 if (code == OMP_ATOMIC_READ)
43343 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
43344 "%<release%> clause implicitly provided by a "
43345 "%<requires%> directive");
43346 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43348 else
43349 memory_order = OMP_MEMORY_ORDER_RELEASE;
43350 break;
43351 case OMP_MEMORY_ORDER_ACQ_REL:
43352 switch (code)
43354 case OMP_ATOMIC_READ:
43355 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43356 break;
43357 case NOP_EXPR: /* atomic write */
43358 memory_order = OMP_MEMORY_ORDER_RELEASE;
43359 break;
43360 default:
43361 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
43362 break;
43364 break;
43365 default:
43366 gcc_unreachable ();
43369 else
43370 switch (code)
43372 case OMP_ATOMIC_READ:
43373 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
43375 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
43376 "%<release%> clause");
43377 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43379 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
43380 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43381 break;
43382 case NOP_EXPR: /* atomic write */
43383 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
43385 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
43386 "%<acquire%> clause");
43387 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43389 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
43390 memory_order = OMP_MEMORY_ORDER_RELEASE;
43391 break;
43392 default:
43393 break;
43395 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
43396 memory_order
43397 = (enum omp_memory_order) (memory_order
43398 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
43400 switch (code)
43402 case OMP_ATOMIC_READ:
43403 case NOP_EXPR: /* atomic write */
43404 v = cp_parser_unary_expression (parser);
43405 if (v == error_mark_node)
43406 goto saw_error;
43407 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43408 goto saw_error;
43409 if (code == NOP_EXPR)
43410 lhs = cp_parser_expression (parser);
43411 else
43412 lhs = cp_parser_unary_expression (parser);
43413 if (lhs == error_mark_node)
43414 goto saw_error;
43415 if (code == NOP_EXPR)
43417 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
43418 opcode. */
43419 code = OMP_ATOMIC;
43420 rhs = lhs;
43421 lhs = v;
43422 v = NULL_TREE;
43424 goto done;
43425 case OMP_ATOMIC_CAPTURE_NEW:
43426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43428 cp_lexer_consume_token (parser->lexer);
43429 structured_block = true;
43431 else if (compare
43432 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43433 break;
43434 else
43436 v = cp_parser_unary_expression (parser);
43437 if (v == error_mark_node)
43438 goto saw_error;
43439 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43440 goto saw_error;
43441 if (compare
43442 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43444 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43445 error_at (eloc, "expected expression");
43446 goto saw_error;
43449 default:
43450 break;
43453 restart:
43454 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43456 cp_lexer_consume_token (parser->lexer);
43458 matching_parens parens;
43459 if (!parens.require_open (parser))
43460 goto saw_error;
43461 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43462 tree cmp_expr;
43463 if (r)
43464 cmp_expr = cp_parser_unary_expression (parser);
43465 else
43466 cmp_expr = cp_parser_binary_expression (parser, false, true,
43467 PREC_NOT_OPERATOR, NULL);
43468 if (!parens.require_close (parser))
43469 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
43470 if (cmp_expr == error_mark_node)
43471 goto saw_error;
43472 if (r)
43474 if (!cp_tree_equal (cmp_expr, r))
43475 goto bad_if;
43476 cmp_expr = rhs;
43477 rhs = NULL_TREE;
43478 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
43480 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43482 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43484 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43485 "expected %<==%> comparison in %<if%> condition");
43486 goto saw_error;
43488 else if (TREE_CODE (cmp_expr) != GT_EXPR
43489 && TREE_CODE (cmp_expr) != LT_EXPR)
43491 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43492 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
43493 "condition");
43494 goto saw_error;
43496 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43497 goto saw_error;
43499 extra_scope = true;
43500 eloc = cp_lexer_peek_token (parser->lexer)->location;
43501 lhs = cp_parser_unary_expression (parser);
43502 orig_lhs = lhs;
43503 if (lhs == error_mark_node)
43504 goto saw_error;
43505 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43507 cp_parser_error (parser, "expected %<=%>");
43508 goto saw_error;
43510 cp_lexer_consume_token (parser->lexer);
43511 eloc = cp_lexer_peek_token (parser->lexer)->location;
43512 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43513 rhs1 = cp_parser_expression (parser);
43514 else
43515 rhs1 = cp_parser_simple_cast_expression (parser);
43517 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43518 goto saw_error;
43520 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43521 goto saw_error;
43523 extra_scope = false;
43524 no_semicolon = true;
43526 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
43528 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43530 opcode = COND_EXPR;
43531 rhs = TREE_OPERAND (cmp_expr, 1);
43533 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
43535 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43536 ? MIN_EXPR : MAX_EXPR);
43537 rhs = rhs1;
43538 rhs1 = TREE_OPERAND (cmp_expr, 0);
43540 else
43541 goto bad_if;
43543 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
43544 goto bad_if;
43545 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
43546 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
43548 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43549 ? MAX_EXPR : MIN_EXPR);
43550 rhs = rhs1;
43551 rhs1 = TREE_OPERAND (cmp_expr, 1);
43553 else
43555 bad_if:
43556 cp_parser_error (parser,
43557 "invalid form of %<#pragma omp atomic compare%>");
43558 goto saw_error;
43561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
43563 if (code != OMP_ATOMIC_CAPTURE_NEW
43564 || (structured_block && r == NULL_TREE)
43565 || TREE_CODE (cmp_expr) != EQ_EXPR)
43567 eloc = cp_lexer_peek_token (parser->lexer)->location;
43568 error_at (eloc, "unexpected %<else%>");
43569 goto saw_error;
43572 cp_lexer_consume_token (parser->lexer);
43574 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43575 goto saw_error;
43577 extra_scope = true;
43578 v = cp_parser_unary_expression (parser);
43579 if (v == error_mark_node)
43580 goto saw_error;
43581 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43582 goto saw_error;
43584 tree expr = cp_parser_simple_cast_expression (parser);
43586 if (!cp_tree_equal (expr, lhs))
43587 goto bad_if;
43589 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43590 goto saw_error;
43592 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43593 goto saw_error;
43595 extra_scope = false;
43596 code = OMP_ATOMIC_CAPTURE_OLD;
43597 if (r == NULL_TREE)
43598 /* Signal to c_finish_omp_atomic that in
43599 if (x == e) { x = d; } else { v = x; }
43600 case the store to v should be conditional. */
43601 r = void_list_node;
43603 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43605 cp_parser_error (parser, "expected %<else%>");
43606 goto saw_error;
43608 else if (code == OMP_ATOMIC_CAPTURE_NEW
43609 && r != NULL_TREE
43610 && v == NULL_TREE)
43611 code = OMP_ATOMIC;
43612 goto stmt_done;
43614 lhs = cp_parser_unary_expression (parser);
43615 orig_lhs = lhs;
43616 switch (TREE_CODE (lhs))
43618 case ERROR_MARK:
43619 goto saw_error;
43621 case POSTINCREMENT_EXPR:
43622 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43623 code = OMP_ATOMIC_CAPTURE_OLD;
43624 /* FALLTHROUGH */
43625 case PREINCREMENT_EXPR:
43626 lhs = TREE_OPERAND (lhs, 0);
43627 opcode = PLUS_EXPR;
43628 rhs = integer_one_node;
43629 if (compare)
43630 goto invalid_compare;
43631 break;
43633 case POSTDECREMENT_EXPR:
43634 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43635 code = OMP_ATOMIC_CAPTURE_OLD;
43636 /* FALLTHROUGH */
43637 case PREDECREMENT_EXPR:
43638 lhs = TREE_OPERAND (lhs, 0);
43639 opcode = MINUS_EXPR;
43640 rhs = integer_one_node;
43641 if (compare)
43642 goto invalid_compare;
43643 break;
43645 case COMPOUND_EXPR:
43646 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
43647 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
43648 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
43649 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
43650 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
43651 (TREE_OPERAND (lhs, 1), 0), 0)))
43652 == BOOLEAN_TYPE)
43653 /* Undo effects of boolean_increment for post {in,de}crement. */
43654 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
43655 /* FALLTHRU */
43656 case MODIFY_EXPR:
43657 if (TREE_CODE (lhs) == MODIFY_EXPR
43658 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
43660 /* Undo effects of boolean_increment. */
43661 if (integer_onep (TREE_OPERAND (lhs, 1)))
43663 /* This is pre or post increment. */
43664 rhs = TREE_OPERAND (lhs, 1);
43665 lhs = TREE_OPERAND (lhs, 0);
43666 opcode = NOP_EXPR;
43667 if (code == OMP_ATOMIC_CAPTURE_NEW
43668 && !structured_block
43669 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
43670 code = OMP_ATOMIC_CAPTURE_OLD;
43671 if (compare)
43672 goto invalid_compare;
43673 break;
43676 /* FALLTHRU */
43677 default:
43678 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43680 cp_parser_error (parser, "expected %<=%>");
43681 goto saw_error;
43683 switch (cp_lexer_peek_token (parser->lexer)->type)
43685 case CPP_MULT_EQ:
43686 opcode = MULT_EXPR;
43687 break;
43688 case CPP_DIV_EQ:
43689 opcode = TRUNC_DIV_EXPR;
43690 break;
43691 case CPP_PLUS_EQ:
43692 opcode = PLUS_EXPR;
43693 break;
43694 case CPP_MINUS_EQ:
43695 opcode = MINUS_EXPR;
43696 break;
43697 case CPP_LSHIFT_EQ:
43698 opcode = LSHIFT_EXPR;
43699 break;
43700 case CPP_RSHIFT_EQ:
43701 opcode = RSHIFT_EXPR;
43702 break;
43703 case CPP_AND_EQ:
43704 opcode = BIT_AND_EXPR;
43705 break;
43706 case CPP_OR_EQ:
43707 opcode = BIT_IOR_EXPR;
43708 break;
43709 case CPP_XOR_EQ:
43710 opcode = BIT_XOR_EXPR;
43711 break;
43712 case CPP_EQ:
43713 enum cp_parser_prec oprec;
43714 cp_token *token;
43715 cp_lexer_consume_token (parser->lexer);
43716 cp_parser_parse_tentatively (parser);
43717 rhs1 = cp_parser_simple_cast_expression (parser);
43718 if (rhs1 == error_mark_node)
43720 cp_parser_abort_tentative_parse (parser);
43721 cp_parser_simple_cast_expression (parser);
43722 goto saw_error;
43724 token = cp_lexer_peek_token (parser->lexer);
43725 if (token->type != CPP_SEMICOLON
43726 && (!compare || token->type != CPP_QUERY)
43727 && !cp_tree_equal (lhs, rhs1))
43729 cp_parser_abort_tentative_parse (parser);
43730 cp_parser_parse_tentatively (parser);
43731 rhs = cp_parser_binary_expression (parser, false, true,
43732 PREC_NOT_OPERATOR, NULL);
43733 if (rhs == error_mark_node)
43735 cp_parser_abort_tentative_parse (parser);
43736 cp_parser_binary_expression (parser, false, true,
43737 PREC_NOT_OPERATOR, NULL);
43738 goto saw_error;
43740 switch (TREE_CODE (rhs))
43742 case MULT_EXPR:
43743 case TRUNC_DIV_EXPR:
43744 case RDIV_EXPR:
43745 case PLUS_EXPR:
43746 case MINUS_EXPR:
43747 case LSHIFT_EXPR:
43748 case RSHIFT_EXPR:
43749 case BIT_AND_EXPR:
43750 case BIT_IOR_EXPR:
43751 case BIT_XOR_EXPR:
43752 if (compare)
43753 break;
43754 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
43756 if (cp_parser_parse_definitely (parser))
43758 opcode = TREE_CODE (rhs);
43759 rhs1 = TREE_OPERAND (rhs, 0);
43760 rhs = TREE_OPERAND (rhs, 1);
43761 goto stmt_done;
43763 else
43764 goto saw_error;
43766 break;
43767 case EQ_EXPR:
43768 if (!compare
43769 || code != OMP_ATOMIC_CAPTURE_NEW
43770 || !structured_block
43771 || v
43772 || r)
43773 break;
43774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
43775 && cp_lexer_nth_token_is_keyword (parser->lexer,
43776 2, RID_IF))
43778 if (cp_parser_parse_definitely (parser))
43780 r = lhs;
43781 lhs = NULL_TREE;
43782 rhs1 = NULL_TREE;
43783 cp_lexer_consume_token (parser->lexer);
43784 goto restart;
43787 break;
43788 case GT_EXPR:
43789 case LT_EXPR:
43790 if (compare
43791 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
43792 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
43793 && cp_parser_parse_definitely (parser))
43795 opcode = TREE_CODE (rhs);
43796 rhs1 = TREE_OPERAND (rhs, 0);
43797 rhs = TREE_OPERAND (rhs, 1);
43798 cond_expr:
43799 cp_lexer_consume_token (parser->lexer);
43800 bool saved_colon_corrects_to_scope_p
43801 = parser->colon_corrects_to_scope_p;
43802 parser->colon_corrects_to_scope_p = false;
43803 tree e1 = cp_parser_expression (parser);
43804 parser->colon_corrects_to_scope_p
43805 = saved_colon_corrects_to_scope_p;
43806 cp_parser_require (parser, CPP_COLON, RT_COLON);
43807 tree e2 = cp_parser_simple_cast_expression (parser);
43808 if (cp_tree_equal (lhs, e2))
43810 if (cp_tree_equal (lhs, rhs1))
43812 if (opcode == EQ_EXPR)
43814 opcode = COND_EXPR;
43815 rhs1 = e1;
43816 goto stmt_done;
43818 if (cp_tree_equal (rhs, e1))
43820 opcode
43821 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
43822 rhs = e1;
43823 goto stmt_done;
43826 else
43828 gcc_assert (opcode != EQ_EXPR);
43829 if (cp_tree_equal (rhs1, e1))
43831 opcode
43832 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
43833 rhs1 = rhs;
43834 rhs = e1;
43835 goto stmt_done;
43839 cp_parser_error (parser,
43840 "invalid form of "
43841 "%<#pragma omp atomic compare%>");
43842 goto saw_error;
43844 break;
43845 default:
43846 break;
43848 cp_parser_abort_tentative_parse (parser);
43849 if (structured_block
43850 && code == OMP_ATOMIC_CAPTURE_OLD
43851 && !compare)
43853 rhs = cp_parser_expression (parser);
43854 if (rhs == error_mark_node)
43855 goto saw_error;
43856 opcode = NOP_EXPR;
43857 rhs1 = NULL_TREE;
43858 goto stmt_done;
43860 cp_parser_error (parser,
43861 "invalid form of %<#pragma omp atomic%>");
43862 goto saw_error;
43864 if (!cp_parser_parse_definitely (parser))
43865 goto saw_error;
43866 switch (token->type)
43868 case CPP_SEMICOLON:
43869 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43871 code = OMP_ATOMIC_CAPTURE_OLD;
43872 v = lhs;
43873 lhs = NULL_TREE;
43874 lhs1 = rhs1;
43875 rhs1 = NULL_TREE;
43876 cp_lexer_consume_token (parser->lexer);
43877 goto restart;
43879 else if (structured_block && !compare)
43881 opcode = NOP_EXPR;
43882 rhs = rhs1;
43883 rhs1 = NULL_TREE;
43884 goto stmt_done;
43886 cp_parser_error (parser,
43887 "invalid form of %<#pragma omp atomic%>");
43888 goto saw_error;
43889 case CPP_MULT:
43890 opcode = MULT_EXPR;
43891 break;
43892 case CPP_DIV:
43893 opcode = TRUNC_DIV_EXPR;
43894 break;
43895 case CPP_PLUS:
43896 opcode = PLUS_EXPR;
43897 break;
43898 case CPP_MINUS:
43899 opcode = MINUS_EXPR;
43900 break;
43901 case CPP_LSHIFT:
43902 opcode = LSHIFT_EXPR;
43903 break;
43904 case CPP_RSHIFT:
43905 opcode = RSHIFT_EXPR;
43906 break;
43907 case CPP_AND:
43908 opcode = BIT_AND_EXPR;
43909 break;
43910 case CPP_OR:
43911 opcode = BIT_IOR_EXPR;
43912 break;
43913 case CPP_XOR:
43914 opcode = BIT_XOR_EXPR;
43915 break;
43916 case CPP_EQ_EQ:
43917 opcode = EQ_EXPR;
43918 break;
43919 case CPP_GREATER:
43920 opcode = GT_EXPR;
43921 break;
43922 case CPP_LESS:
43923 opcode = LT_EXPR;
43924 break;
43925 default:
43926 cp_parser_error (parser,
43927 "invalid operator for %<#pragma omp atomic%>");
43928 goto saw_error;
43930 if (compare
43931 && TREE_CODE_CLASS (opcode) != tcc_comparison)
43933 cp_parser_error (parser,
43934 "invalid form of "
43935 "%<#pragma omp atomic compare%>");
43936 goto saw_error;
43938 oprec = TOKEN_PRECEDENCE (token);
43939 gcc_assert (oprec != PREC_NOT_OPERATOR);
43940 if (commutative_tree_code (opcode))
43941 oprec = (enum cp_parser_prec) (oprec - 1);
43942 cp_lexer_consume_token (parser->lexer);
43943 rhs = cp_parser_binary_expression (parser, false, false,
43944 oprec, NULL);
43945 if (rhs == error_mark_node)
43946 goto saw_error;
43947 if (compare)
43949 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
43951 cp_parser_error (parser,
43952 "invalid form of "
43953 "%<#pragma omp atomic compare%>");
43954 goto saw_error;
43956 goto cond_expr;
43958 goto stmt_done;
43959 default:
43960 cp_parser_error (parser,
43961 "invalid operator for %<#pragma omp atomic%>");
43962 goto saw_error;
43964 cp_lexer_consume_token (parser->lexer);
43966 rhs = cp_parser_expression (parser);
43967 if (rhs == error_mark_node)
43968 goto saw_error;
43969 break;
43971 stmt_done:
43972 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
43974 if (!no_semicolon
43975 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43976 goto saw_error;
43977 no_semicolon = false;
43978 v = cp_parser_unary_expression (parser);
43979 if (v == error_mark_node)
43980 goto saw_error;
43981 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43982 goto saw_error;
43983 lhs1 = cp_parser_unary_expression (parser);
43984 if (lhs1 == error_mark_node)
43985 goto saw_error;
43987 if (structured_block)
43989 if (!no_semicolon)
43990 cp_parser_consume_semicolon_at_end_of_statement (parser);
43991 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
43993 done:
43994 if (weak && opcode != COND_EXPR)
43996 error_at (loc, "%<weak%> clause requires atomic equality comparison");
43997 weak = false;
43999 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
44000 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
44001 rhs1, r, clauses, memory_order, weak);
44002 if (!structured_block && !no_semicolon)
44003 cp_parser_consume_semicolon_at_end_of_statement (parser);
44004 return;
44006 invalid_compare:
44007 error ("invalid form of %<pragma omp atomic compare%>");
44008 /* FALLTHRU */
44009 saw_error:
44010 cp_parser_skip_to_end_of_block_or_statement (parser);
44011 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44012 cp_lexer_consume_token (parser->lexer);
44013 if (structured_block)
44015 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44016 cp_lexer_consume_token (parser->lexer);
44017 else if (code == OMP_ATOMIC_CAPTURE_NEW)
44019 cp_parser_skip_to_end_of_block_or_statement (parser);
44020 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44021 cp_lexer_consume_token (parser->lexer);
44027 /* OpenMP 2.5:
44028 # pragma omp barrier new-line */
44030 static void
44031 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
44033 cp_parser_require_pragma_eol (parser, pragma_tok);
44034 finish_omp_barrier ();
44037 /* OpenMP 2.5:
44038 # pragma omp critical [(name)] new-line
44039 structured-block
44041 OpenMP 4.5:
44042 # pragma omp critical [(name) [hint(expression)]] new-line
44043 structured-block */
44045 #define OMP_CRITICAL_CLAUSE_MASK \
44046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
44048 static tree
44049 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44051 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
44053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
44055 matching_parens parens;
44056 parens.consume_open (parser);
44058 name = cp_parser_identifier (parser);
44060 if (name == error_mark_node
44061 || !parens.require_close (parser))
44062 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44063 /*or_comma=*/false,
44064 /*consume_paren=*/true);
44065 if (name == error_mark_node)
44066 name = NULL;
44068 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
44069 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
44070 cp_lexer_consume_token (parser->lexer);
44073 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
44074 "#pragma omp critical", pragma_tok);
44076 stmt = cp_parser_omp_structured_block (parser, if_p);
44077 return c_finish_omp_critical (input_location, stmt, name, clauses);
44080 /* OpenMP 5.0:
44081 # pragma omp depobj ( depobj ) depobj-clause new-line
44083 depobj-clause:
44084 depend (dependence-type : locator)
44085 destroy
44086 update (dependence-type)
44088 OpenMP 5.2 additionally:
44089 destroy ( depobj )
44091 dependence-type:
44094 inout
44095 mutexinout */
44097 static void
44098 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
44100 location_t loc = pragma_tok->location;
44101 matching_parens parens;
44102 if (!parens.require_open (parser))
44104 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44105 return;
44108 tree depobj = cp_parser_assignment_expression (parser);
44110 if (!parens.require_close (parser))
44111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44112 /*or_comma=*/false,
44113 /*consume_paren=*/true);
44115 tree clause = NULL_TREE;
44116 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
44117 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44118 cp_lexer_consume_token (parser->lexer);
44119 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
44120 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44123 const char *p = IDENTIFIER_POINTER (id);
44125 cp_lexer_consume_token (parser->lexer);
44126 if (!strcmp ("depend", p))
44128 /* Don't create location wrapper nodes within the depend clause. */
44129 auto_suppress_location_wrappers sentinel;
44130 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
44131 if (clause)
44132 clause = finish_omp_clauses (clause, C_ORT_OMP);
44133 if (!clause)
44134 clause = error_mark_node;
44136 else if (!strcmp ("destroy", p))
44138 kind = OMP_CLAUSE_DEPEND_LAST;
44139 matching_parens c_parens;
44140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
44141 && c_parens.require_open (parser))
44143 tree destobj = cp_parser_assignment_expression (parser);
44144 if (depobj != error_mark_node
44145 && destobj != error_mark_node
44146 && !operand_equal_p (destobj, depobj, OEP_MATCH_SIDE_EFFECTS
44147 | OEP_LEXICOGRAPHIC))
44148 warning_at (EXPR_LOC_OR_LOC (destobj, c_loc), OPT_Wopenmp,
44149 "the %<destroy%> expression %qE should be the same "
44150 "as the %<depobj%> argument %qE", destobj, depobj);
44151 if (!c_parens.require_close (parser))
44152 cp_parser_skip_to_closing_parenthesis (parser,
44153 /*recovering=*/true,
44154 /*or_comma=*/false,
44155 /*consume_paren=*/true);
44158 else if (!strcmp ("update", p))
44160 matching_parens c_parens;
44161 if (c_parens.require_open (parser))
44163 location_t c2_loc
44164 = cp_lexer_peek_token (parser->lexer)->location;
44165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44167 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
44168 const char *p2 = IDENTIFIER_POINTER (id2);
44170 cp_lexer_consume_token (parser->lexer);
44171 if (!strcmp ("in", p2))
44172 kind = OMP_CLAUSE_DEPEND_IN;
44173 else if (!strcmp ("out", p2))
44174 kind = OMP_CLAUSE_DEPEND_OUT;
44175 else if (!strcmp ("inout", p2))
44176 kind = OMP_CLAUSE_DEPEND_INOUT;
44177 else if (!strcmp ("mutexinoutset", p2))
44178 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
44179 else if (!strcmp ("inoutset", p2))
44180 kind = OMP_CLAUSE_DEPEND_INOUTSET;
44182 if (kind == OMP_CLAUSE_DEPEND_INVALID)
44184 clause = error_mark_node;
44185 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
44186 "%<mutexinoutset%> or %<inoutset%>");
44188 if (!c_parens.require_close (parser))
44189 cp_parser_skip_to_closing_parenthesis (parser,
44190 /*recovering=*/true,
44191 /*or_comma=*/false,
44192 /*consume_paren=*/true);
44194 else
44195 clause = error_mark_node;
44198 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
44200 clause = error_mark_node;
44201 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
44203 cp_parser_require_pragma_eol (parser, pragma_tok);
44205 finish_omp_depobj (loc, depobj, kind, clause);
44209 /* OpenMP 2.5:
44210 # pragma omp flush flush-vars[opt] new-line
44212 flush-vars:
44213 ( variable-list )
44215 OpenMP 5.0:
44216 # pragma omp flush memory-order-clause new-line */
44218 static void
44219 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
44221 enum memmodel mo = MEMMODEL_LAST;
44222 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
44223 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
44224 cp_lexer_consume_token (parser->lexer);
44225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44228 const char *p = IDENTIFIER_POINTER (id);
44229 if (!strcmp (p, "seq_cst"))
44230 mo = MEMMODEL_SEQ_CST;
44231 else if (!strcmp (p, "acq_rel"))
44232 mo = MEMMODEL_ACQ_REL;
44233 else if (!strcmp (p, "release"))
44234 mo = MEMMODEL_RELEASE;
44235 else if (!strcmp (p, "acquire"))
44236 mo = MEMMODEL_ACQUIRE;
44237 else
44238 error_at (cp_lexer_peek_token (parser->lexer)->location,
44239 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
44240 "%<acquire%>");
44241 cp_lexer_consume_token (parser->lexer);
44243 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
44245 if (mo != MEMMODEL_LAST)
44246 error_at (cp_lexer_peek_token (parser->lexer)->location,
44247 "%<flush%> list specified together with memory order "
44248 "clause");
44249 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
44251 cp_parser_require_pragma_eol (parser, pragma_tok);
44253 finish_omp_flush (mo);
44256 /* Helper function, to parse omp for increment expression. */
44258 static tree
44259 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
44261 tree cond = cp_parser_binary_expression (parser, false, true,
44262 PREC_NOT_OPERATOR, NULL);
44263 if (cond == error_mark_node
44264 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44266 cp_parser_skip_to_end_of_statement (parser);
44267 return error_mark_node;
44270 switch (TREE_CODE (cond))
44272 case GT_EXPR:
44273 case GE_EXPR:
44274 case LT_EXPR:
44275 case LE_EXPR:
44276 break;
44277 case NE_EXPR:
44278 if (code != OACC_LOOP)
44279 break;
44280 gcc_fallthrough ();
44281 default:
44282 return error_mark_node;
44285 /* If decl is an iterator, preserve LHS and RHS of the relational
44286 expr until finish_omp_for. */
44287 if (decl
44288 && (type_dependent_expression_p (decl)
44289 || CLASS_TYPE_P (TREE_TYPE (decl))))
44290 return cond;
44292 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
44293 TREE_CODE (cond),
44294 TREE_OPERAND (cond, 0), ERROR_MARK,
44295 TREE_OPERAND (cond, 1), ERROR_MARK,
44296 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
44299 /* Helper function, to parse omp for increment expression. */
44301 static tree
44302 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
44304 cp_token *token = cp_lexer_peek_token (parser->lexer);
44305 enum tree_code op;
44306 tree lhs, rhs;
44307 cp_id_kind idk;
44308 bool decl_first;
44310 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
44312 op = (token->type == CPP_PLUS_PLUS
44313 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
44314 cp_lexer_consume_token (parser->lexer);
44315 lhs = cp_parser_simple_cast_expression (parser);
44316 if (lhs != decl
44317 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
44318 return error_mark_node;
44319 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
44322 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
44323 if (lhs != decl
44324 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
44325 return error_mark_node;
44327 token = cp_lexer_peek_token (parser->lexer);
44328 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
44330 op = (token->type == CPP_PLUS_PLUS
44331 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
44332 cp_lexer_consume_token (parser->lexer);
44333 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
44336 op = cp_parser_assignment_operator_opt (parser);
44337 if (op == ERROR_MARK)
44338 return error_mark_node;
44340 if (op != NOP_EXPR)
44342 rhs = cp_parser_assignment_expression (parser);
44343 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
44344 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
44347 lhs = cp_parser_binary_expression (parser, false, false,
44348 PREC_ADDITIVE_EXPRESSION, NULL);
44349 token = cp_lexer_peek_token (parser->lexer);
44350 decl_first = (lhs == decl
44351 || (processing_template_decl && cp_tree_equal (lhs, decl)));
44352 if (decl_first)
44353 lhs = NULL_TREE;
44354 if (token->type != CPP_PLUS
44355 && token->type != CPP_MINUS)
44356 return error_mark_node;
44360 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
44361 cp_lexer_consume_token (parser->lexer);
44362 rhs = cp_parser_binary_expression (parser, false, false,
44363 PREC_ADDITIVE_EXPRESSION, NULL);
44364 token = cp_lexer_peek_token (parser->lexer);
44365 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
44367 if (lhs == NULL_TREE)
44369 if (op == PLUS_EXPR)
44370 lhs = rhs;
44371 else
44372 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
44373 NULL_TREE, tf_warning_or_error);
44375 else
44376 lhs = build_x_binary_op (input_location, op,
44377 lhs, ERROR_MARK,
44378 rhs, ERROR_MARK,
44379 NULL_TREE, NULL, tf_warning_or_error);
44382 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
44384 if (!decl_first)
44386 if ((rhs != decl
44387 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
44388 || op == MINUS_EXPR)
44389 return error_mark_node;
44390 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
44392 else
44393 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
44395 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
44398 /* Parse the initialization statement of an OpenMP for loop. Range-for
44399 is handled separately in cp_convert_omp_range_for.
44401 On entry SL is the current statement list. Parsing of some forms
44402 of initialization pops this list and stores its contents in either INIT
44403 or THIS_PRE_BODY, and sets SL to null. Initialization for class
44404 iterators is added directly to SL and it is not popped until later.
44406 On return, DECL is set if the initialization is by binding the
44407 iteration variable. If the initialization is by assignment, REAL_DECL
44408 is set to point to a variable in an outer scope. ORIG_INIT is set
44409 if the iteration variable is of class type; this is a copy saved for
44410 error checking in finish_omp_for.
44412 Return true if the resulting construct should have an
44413 OMP_CLAUSE_PRIVATE added to it. */
44415 static tree
44416 cp_parser_omp_for_loop_init (cp_parser *parser,
44417 tree &this_pre_body,
44418 tree &sl,
44419 tree &init,
44420 tree &orig_init,
44421 tree &decl,
44422 tree &real_decl)
44424 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
44425 return NULL_TREE;
44427 tree add_private_clause = NULL_TREE;
44429 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
44431 init-expr:
44432 var = lb
44433 integer-type var = lb
44434 random-access-iterator-type var = lb
44435 pointer-type var = lb
44437 cp_decl_specifier_seq type_specifiers;
44439 /* First, try to parse as an initialized declaration. See
44440 cp_parser_condition, from whence the bulk of this is copied. */
44442 cp_parser_parse_tentatively (parser);
44443 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
44444 /*is_declaration=*/true,
44445 /*is_trailing_return=*/false,
44446 &type_specifiers);
44447 if (cp_parser_parse_definitely (parser))
44449 /* If parsing a type specifier seq succeeded, then this
44450 MUST be a initialized declaration. */
44451 tree asm_specification, attributes;
44452 cp_declarator *declarator;
44454 declarator = cp_parser_declarator (parser,
44455 CP_PARSER_DECLARATOR_NAMED,
44456 CP_PARSER_FLAGS_NONE,
44457 /*ctor_dtor_or_conv_p=*/NULL,
44458 /*parenthesized_p=*/NULL,
44459 /*member_p=*/false,
44460 /*friend_p=*/false,
44461 /*static_p=*/false);
44462 attributes = cp_parser_attributes_opt (parser);
44463 asm_specification = cp_parser_asm_specification_opt (parser);
44465 if (declarator == cp_error_declarator)
44466 cp_parser_skip_to_end_of_statement (parser);
44468 else
44470 tree pushed_scope, auto_node;
44472 decl = start_decl (declarator, &type_specifiers,
44473 SD_INITIALIZED, attributes,
44474 /*prefix_attributes=*/NULL_TREE,
44475 &pushed_scope);
44477 auto_node = type_uses_auto (TREE_TYPE (decl));
44478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
44480 if (cp_lexer_next_token_is (parser->lexer,
44481 CPP_OPEN_PAREN))
44482 error ("parenthesized initialization is not allowed in "
44483 "OpenMP %<for%> loop");
44484 else
44485 /* Trigger an error. */
44486 cp_parser_require (parser, CPP_EQ, RT_EQ);
44488 init = error_mark_node;
44489 cp_parser_skip_to_end_of_statement (parser);
44491 else if (CLASS_TYPE_P (TREE_TYPE (decl))
44492 || type_dependent_expression_p (decl)
44493 || auto_node)
44495 bool is_non_constant_init;
44497 init = cp_parser_initializer (parser,
44498 /*is_direct_init=*/nullptr,
44499 &is_non_constant_init);
44501 if (auto_node)
44503 TREE_TYPE (decl)
44504 = do_auto_deduction (TREE_TYPE (decl), init,
44505 auto_node);
44507 if (!CLASS_TYPE_P (TREE_TYPE (decl))
44508 && !type_dependent_expression_p (decl))
44509 goto non_class;
44512 cp_finish_decl (decl, init, !is_non_constant_init,
44513 asm_specification,
44514 LOOKUP_ONLYCONVERTING);
44515 orig_init = init;
44517 /* In the case of a class iterator, do not pop sl here.
44518 Both class initialization and finalization must happen in
44519 the enclosing init block scope. For now set the init
44520 expression to null; it'll be filled in properly in
44521 finish_omp_for before stuffing it in the OMP_FOR. */
44522 if (CLASS_TYPE_P (TREE_TYPE (decl)))
44523 init = NULL_TREE;
44524 else /* It is a parameterized type. */
44526 init = pop_stmt_list (sl);
44527 sl = NULL_TREE;
44528 if (init && TREE_CODE (init) == STATEMENT_LIST)
44530 tree_stmt_iterator i = tsi_start (init);
44531 /* Move lambda DECL_EXPRs to the enclosing block. */
44532 while (!tsi_end_p (i))
44534 tree t = tsi_stmt (i);
44535 if (TREE_CODE (t) == DECL_EXPR
44536 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
44538 tsi_delink (&i);
44539 add_stmt (t);
44540 continue;
44542 break;
44544 if (tsi_one_before_end_p (i))
44546 tree t = tsi_stmt (i);
44547 tsi_delink (&i);
44548 free_stmt_list (init);
44549 init = t;
44554 else
44555 /* This is an initialized declaration of non-class,
44556 non-parameterized type iteration variable. */
44558 /* Consume '='. */
44559 cp_lexer_consume_token (parser->lexer);
44560 init = cp_parser_assignment_expression (parser);
44562 non_class:
44563 if (TYPE_REF_P (TREE_TYPE (decl)))
44564 init = error_mark_node;
44565 else
44566 cp_finish_decl (decl, NULL_TREE,
44567 /*init_const_expr_p=*/false,
44568 asm_specification,
44569 LOOKUP_ONLYCONVERTING);
44570 this_pre_body = pop_stmt_list (sl);
44571 sl = NULL_TREE;
44574 if (pushed_scope)
44575 pop_scope (pushed_scope);
44578 else
44580 cp_id_kind idk;
44581 /* If parsing a type specifier sequence failed, then
44582 this MUST be a simple expression. */
44583 cp_parser_parse_tentatively (parser);
44584 decl = cp_parser_primary_expression (parser, false, false,
44585 false, &idk);
44586 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
44587 if (!cp_parser_error_occurred (parser)
44588 && decl
44589 && (TREE_CODE (decl) == COMPONENT_REF
44590 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
44592 cp_parser_abort_tentative_parse (parser);
44593 cp_parser_parse_tentatively (parser);
44594 cp_token *token = cp_lexer_peek_token (parser->lexer);
44595 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
44596 /*check_dependency_p=*/true,
44597 /*template_p=*/NULL,
44598 /*declarator_p=*/false,
44599 /*optional_p=*/false);
44600 if (name != error_mark_node
44601 && last_tok == cp_lexer_peek_token (parser->lexer))
44603 decl = cp_parser_lookup_name_simple (parser, name,
44604 token->location);
44605 if (TREE_CODE (decl) == FIELD_DECL)
44606 add_private_clause = omp_privatize_field (decl, false);
44608 cp_parser_abort_tentative_parse (parser);
44609 cp_parser_parse_tentatively (parser);
44610 decl = cp_parser_primary_expression (parser, false, false,
44611 false, &idk);
44613 if (!cp_parser_error_occurred (parser)
44614 && decl
44615 && DECL_P (decl)
44616 && CLASS_TYPE_P (TREE_TYPE (decl)))
44618 tree rhs;
44620 cp_parser_parse_definitely (parser);
44621 cp_parser_require (parser, CPP_EQ, RT_EQ);
44622 rhs = cp_parser_assignment_expression (parser);
44623 orig_init = rhs;
44624 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
44625 decl, NOP_EXPR,
44626 rhs, NULL_TREE,
44627 tf_warning_or_error));
44628 if (!add_private_clause)
44629 add_private_clause = decl;
44631 else
44633 decl = NULL;
44634 cp_parser_abort_tentative_parse (parser);
44635 init = cp_parser_expression (parser);
44636 if (init)
44638 if (TREE_CODE (init) == MODIFY_EXPR
44639 || TREE_CODE (init) == MODOP_EXPR)
44640 real_decl = TREE_OPERAND (init, 0);
44643 this_pre_body = pop_stmt_list (sl);
44644 sl = NULL_TREE;
44646 return add_private_clause;
44649 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
44650 including introducing new temporaries for the range start and end,
44651 doing auto deduction, and processing decomposition variables.
44653 This function is also called from pt.cc during template instantiation.
44654 In that case SL is NULL_TREE, otherwise it is the current statement
44655 list. */
44656 void
44657 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
44658 tree &decl, tree &orig_decl, tree &init,
44659 tree &orig_init, tree &cond, tree &incr)
44661 tree begin, end, range_temp_decl = NULL_TREE;
44662 tree iter_type, begin_expr, end_expr;
44663 bool clear_has_value_expr = false;
44665 if (processing_template_decl)
44667 if (check_for_bare_parameter_packs (init))
44668 init = error_mark_node;
44669 if (!type_dependent_expression_p (init)
44670 /* do_auto_deduction doesn't mess with template init-lists. */
44671 && !BRACE_ENCLOSED_INITIALIZER_P (init))
44673 tree d = decl;
44674 cp_decomp decomp_d, *decomp = NULL;
44675 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
44677 tree v = DECL_VALUE_EXPR (decl);
44678 if (TREE_CODE (v) == ARRAY_REF
44679 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44681 d = TREE_OPERAND (v, 0);
44682 decomp = &decomp_d;
44683 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44684 decomp->decl = decl;
44687 do_range_for_auto_deduction (d, init, decomp);
44689 cond = global_namespace;
44690 incr = NULL_TREE;
44691 orig_init = init;
44692 if (sl)
44694 this_pre_body = pop_stmt_list (sl);
44695 sl = NULL_TREE;
44697 return;
44700 init = mark_lvalue_use (init);
44702 if (decl == error_mark_node || init == error_mark_node)
44703 /* If an error happened previously do nothing or else a lot of
44704 unhelpful errors would be issued. */
44705 begin_expr = end_expr = iter_type = error_mark_node;
44706 else
44708 tree range_temp;
44710 if (VAR_P (init)
44711 && array_of_runtime_bound_p (TREE_TYPE (init)))
44712 /* Can't bind a reference to an array of runtime bound. */
44713 range_temp = init;
44714 else
44716 range_temp = build_range_temp (init);
44717 DECL_NAME (range_temp) = NULL_TREE;
44718 pushdecl (range_temp);
44719 cp_finish_decl (range_temp, init,
44720 /*is_constant_init*/false, NULL_TREE,
44721 LOOKUP_ONLYCONVERTING);
44722 range_temp_decl = range_temp;
44723 range_temp = convert_from_reference (range_temp);
44725 iter_type = cp_parser_perform_range_for_lookup (range_temp,
44726 &begin_expr, &end_expr);
44729 tree end_iter_type = iter_type;
44730 if (cxx_dialect >= cxx17)
44731 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
44732 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
44733 TREE_USED (end) = 1;
44734 DECL_ARTIFICIAL (end) = 1;
44735 pushdecl (end);
44736 cp_finish_decl (end, end_expr,
44737 /*is_constant_init*/false, NULL_TREE,
44738 LOOKUP_ONLYCONVERTING);
44740 /* The new for initialization statement. */
44741 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
44742 TREE_USED (begin) = 1;
44743 DECL_ARTIFICIAL (begin) = 1;
44744 pushdecl (begin);
44745 orig_init = init;
44746 if (CLASS_TYPE_P (iter_type))
44747 init = NULL_TREE;
44748 else
44750 init = begin_expr;
44751 begin_expr = NULL_TREE;
44753 cp_finish_decl (begin, begin_expr,
44754 /*is_constant_init*/false, NULL_TREE,
44755 LOOKUP_ONLYCONVERTING);
44757 /* The new for condition. */
44758 if (CLASS_TYPE_P (iter_type))
44759 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
44760 else
44761 cond = build_x_binary_op (input_location, NE_EXPR,
44762 begin, ERROR_MARK,
44763 end, ERROR_MARK,
44764 NULL_TREE, NULL, tf_warning_or_error);
44766 /* The new increment expression. */
44767 if (CLASS_TYPE_P (iter_type))
44768 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
44769 else
44770 incr = finish_unary_op_expr (input_location,
44771 PREINCREMENT_EXPR, begin,
44772 tf_warning_or_error);
44774 orig_decl = decl;
44775 decl = begin;
44776 /* Defer popping sl here. */
44778 cp_decomp decomp_d, *decomp = NULL;
44779 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
44781 tree v = DECL_VALUE_EXPR (orig_decl);
44782 if (TREE_CODE (v) == ARRAY_REF
44783 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44785 tree d = orig_decl;
44786 orig_decl = TREE_OPERAND (v, 0);
44787 decomp = &decomp_d;
44788 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44789 decomp->decl = d;
44793 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
44794 if (auto_node)
44796 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44797 NULL_TREE, tf_none);
44798 if (!error_operand_p (t))
44800 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
44801 t, auto_node);
44802 if (decomp)
44804 ++processing_template_decl;
44805 cp_finish_decomp (orig_decl, decomp);
44806 --processing_template_decl;
44807 if (!processing_template_decl)
44808 clear_has_value_expr = true;
44813 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
44814 that holds decls for variables implementing the iterator, represented
44815 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
44816 of the vector are decls of scratch variables for the range start and
44817 end that will eventually be bound in the implicit scope surrounding
44818 the whole loop nest. The remaining elements are decls of derived
44819 decomposition variables that are bound inside the loop body. This
44820 structure is further mangled by finish_omp_for into the form required
44821 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
44822 unsigned decomp_cnt = decomp ? decomp->count : 0;
44823 tree v = make_tree_vec (decomp_cnt + 3);
44824 TREE_VEC_ELT (v, 0) = range_temp_decl;
44825 TREE_VEC_ELT (v, 1) = end;
44826 TREE_VEC_ELT (v, 2) = orig_decl;
44827 if (clear_has_value_expr)
44828 TREE_PUBLIC (v) = 1;
44829 for (unsigned i = 0; i < decomp_cnt; i++)
44831 if (clear_has_value_expr)
44833 /* If cp_finish_decomp was called with processing_template_decl
44834 temporarily set to 1, then decomp names will have deduced
44835 name but the DECL_VALUE_EXPR will be dependent. Hide those
44836 from folding of other loop initializers e.g. for warning
44837 purposes until cp_finish_omp_range_for. */
44838 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
44839 || (TREE_TYPE (decomp->decl)
44840 == error_mark_node));
44841 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
44843 TREE_VEC_ELT (v, i + 3) = decomp->decl;
44844 decomp->decl = DECL_CHAIN (decomp->decl);
44846 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
44849 /* Helper for cp_parser_omp_for_loop, finalize part of range for
44850 inside of the collapsed body. */
44852 void
44853 cp_finish_omp_range_for (tree orig, tree begin)
44855 gcc_assert (TREE_CODE (orig) == TREE_LIST
44856 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
44857 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
44858 cp_decomp decomp_d, *decomp = NULL;
44860 if (DECL_DECOMPOSITION_P (decl))
44862 decomp = &decomp_d;
44863 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
44864 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
44865 if (TREE_PUBLIC (TREE_CHAIN (orig)))
44867 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
44868 by cp_convert_omp_range_for above. */
44869 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
44870 tree d = decomp_d.decl;
44871 for (unsigned i = 0; i < decomp_d.count; i++)
44873 if (TREE_TYPE (d) != error_mark_node)
44874 DECL_HAS_VALUE_EXPR_P (d) = 1;
44875 d = DECL_CHAIN (d);
44880 /* The declaration is initialized with *__begin inside the loop body. */
44881 cp_finish_decl (decl,
44882 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44883 NULL_TREE, tf_warning_or_error),
44884 /*is_constant_init*/false, NULL_TREE,
44885 LOOKUP_ONLYCONVERTING, decomp);
44886 if (DECL_DECOMPOSITION_P (decl))
44887 cp_finish_decomp (decl, decomp);
44890 /* Return true if next tokens contain a standard attribute that contains
44891 omp::directive (DIRECTIVE). */
44893 static bool
44894 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
44895 bool tentative)
44897 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
44898 if (n < 10)
44899 return false;
44900 for (i = 5; i < n - 4; i++)
44901 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
44902 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
44903 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
44905 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
44906 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
44907 if (strcmp (IDENTIFIER_POINTER (first), "directive")
44908 && strcmp (IDENTIFIER_POINTER (first), "__directive__"))
44909 continue;
44910 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
44911 break;
44913 if (i == n - 4)
44914 return false;
44915 cp_parser_parse_tentatively (parser);
44916 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
44917 location_t last_loc
44918 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
44919 location_t middle_loc = UNKNOWN_LOCATION;
44920 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
44921 int cnt = 0;
44922 bool seen = false;
44923 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
44924 if (get_attribute_namespace (attr) == omp_identifier
44925 && is_attribute_p ("directive", get_attribute_name (attr)))
44927 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
44929 tree d = TREE_VALUE (a);
44930 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
44931 cp_token *first = DEFPARSE_TOKENS (d)->first;
44932 cnt++;
44933 if (first->type == CPP_NAME
44934 && strcmp (IDENTIFIER_POINTER (first->u.value),
44935 directive) == 0)
44937 seen = true;
44938 if (middle_loc == UNKNOWN_LOCATION)
44939 middle_loc = first->location;
44943 if (!seen || tentative)
44945 cp_parser_abort_tentative_parse (parser);
44946 return seen;
44948 if (cnt != 1 || TREE_CHAIN (std_attrs))
44950 error_at (make_location (first_loc, last_loc, middle_loc),
44951 "%<[[omp::directive(%s)]]%> must be the only specified "
44952 "attribute on a statement", directive);
44953 cp_parser_abort_tentative_parse (parser);
44954 return false;
44956 if (!cp_parser_parse_definitely (parser))
44957 return false;
44958 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
44959 return true;
44962 /* Parse an OpenMP structured block sequence. KIND is the corresponding
44963 separating directive. */
44965 static tree
44966 cp_parser_omp_structured_block_sequence (cp_parser *parser,
44967 enum pragma_kind kind)
44969 tree stmt = begin_omp_structured_block ();
44970 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44972 cp_parser_statement (parser, NULL_TREE, false, NULL);
44973 while (true)
44975 cp_token *token = cp_lexer_peek_token (parser->lexer);
44977 if (token->type == CPP_CLOSE_BRACE
44978 || token->type == CPP_EOF
44979 || token->type == CPP_PRAGMA_EOL
44980 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
44981 || (kind != PRAGMA_NONE
44982 && cp_parser_pragma_kind (token) == kind))
44983 break;
44985 if (kind != PRAGMA_NONE
44986 && cp_parser_omp_section_scan (parser,
44987 kind == PRAGMA_OMP_SCAN
44988 ? "scan" : "section", false))
44989 break;
44991 cp_parser_statement (parser, NULL_TREE, false, NULL);
44994 cp_parser_end_omp_structured_block (parser, save);
44995 return finish_omp_structured_block (stmt);
44999 /* OpenMP 5.0:
45001 scan-loop-body:
45002 { structured-block scan-directive structured-block } */
45004 static void
45005 cp_parser_omp_scan_loop_body (cp_parser *parser)
45007 tree substmt, clauses = NULL_TREE;
45008 bool found_scan = false;
45010 matching_braces braces;
45011 if (!braces.require_open (parser))
45012 return;
45014 cp_token *tok = cp_lexer_peek_token (parser->lexer);
45015 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
45016 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
45017 else
45019 warning_at (tok->location, OPT_Wopenmp,
45020 "%<#pragma omp scan%> with zero preceding executable "
45021 "statements");
45022 substmt = build_empty_stmt (tok->location);
45024 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
45025 add_stmt (substmt);
45027 tok = cp_lexer_peek_token (parser->lexer);
45028 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
45030 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
45031 found_scan = true;
45033 cp_lexer_consume_token (parser->lexer);
45035 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45036 cp_lexer_consume_token (parser->lexer);
45038 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45040 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45041 const char *p = IDENTIFIER_POINTER (id);
45042 if (strcmp (p, "inclusive") == 0)
45043 clause = OMP_CLAUSE_INCLUSIVE;
45044 else if (strcmp (p, "exclusive") == 0)
45045 clause = OMP_CLAUSE_EXCLUSIVE;
45047 if (clause != OMP_CLAUSE_ERROR)
45049 cp_lexer_consume_token (parser->lexer);
45050 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
45052 else
45053 cp_parser_error (parser, "expected %<inclusive%> or "
45054 "%<exclusive%> clause");
45056 cp_parser_require_pragma_eol (parser, tok);
45058 else
45059 error ("expected %<#pragma omp scan%>");
45061 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
45062 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
45063 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
45064 else
45066 if (found_scan)
45067 warning_at (tok->location, OPT_Wopenmp,
45068 "%<#pragma omp scan%> with zero succeeding executable "
45069 "statements");
45070 substmt = build_empty_stmt (tok->location);
45072 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
45073 clauses);
45074 add_stmt (substmt);
45076 braces.require_close (parser);
45080 /* Check that the next token starts a loop nest. Return true if yes,
45081 otherwise diagnose an error if ERROR_P is true and return false. */
45082 static bool
45083 cp_parser_next_tokens_can_be_canon_loop (cp_parser *parser, enum tree_code code,
45084 bool error_p)
45086 if (code == OACC_LOOP)
45088 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45089 return true;
45090 if (error_p)
45091 cp_parser_error (parser, "for statement expected");
45093 else
45095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45096 return true;
45097 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA)
45098 && ((cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45099 == PRAGMA_OMP_UNROLL)
45100 || (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45101 == PRAGMA_OMP_TILE)))
45102 return true;
45103 /* Skip standard attributes on next for in case they are
45104 [[omp::directive (unroll partial (4))]] or
45105 [[omp::directive (tile sizes (1, 2, 3))]] etc. */
45106 size_t n = cp_parser_skip_std_attribute_spec_seq (parser, 1);
45107 if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_FOR))
45108 return true;
45109 if (error_p)
45111 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45112 error_at (cp_lexer_peek_token (parser->lexer)->location,
45113 "loop nest expected");
45114 else
45115 cp_parser_error (parser, "loop nest expected");
45118 return false;
45121 static tree cp_parser_omp_unroll (cp_parser *, cp_token *, bool *);
45122 static tree cp_parser_omp_tile (cp_parser *, cp_token *, bool *);
45124 /* This function parses a single level of a loop nest, invoking itself
45125 recursively if necessary.
45127 loop-nest :: for (...) loop-body
45128 loop-body :: loop-nest
45129 | { [intervening-code] loop-body [intervening-code] }
45130 | final-loop-body
45131 intervening-code :: structured-block-sequence
45132 final-loop-body :: structured-block
45134 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
45135 all the iterator information from the inner loops into vectors in the
45136 parser->omp_for_parse_state structure.
45138 In the "range for" case, it is transformed into a regular "for" iterator
45139 by introducing some temporary variables for the begin/end,
45140 as well as bindings of the actual iteration variables which are
45141 injected into the body of the loop.
45143 Initialization code for iterator variables may end up either in the
45144 init vector (simple assignments), in omp_for_parse_state->pre_body
45145 (decl_exprs for iterators bound in the for statement), or in the
45146 scope surrounding this level of loop initialization.
45148 The scopes of class iterator variables and their finalizers need to
45149 be adjusted after parsing so that all of the initialization happens
45150 in a scope surrounding all of the intervening and body code. For
45151 this reason we separately store the initialization and body blocks
45152 for each level of loops in the omp_for_parse_state structure and
45153 reassemble/reorder them in cp_parser_omp_for. See additional
45154 comments there about the use of placeholders, etc. */
45156 static tree
45157 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
45159 tree decl = NULL_TREE, cond = NULL_TREE, incr = NULL_TREE, init = NULL_TREE;
45160 tree orig_init = NULL_TREE, real_decl = NULL_TREE, orig_decl = NULL_TREE;
45161 tree init_block, body_block;
45162 tree init_placeholder, body_placeholder;
45163 tree init_scope;
45164 tree this_pre_body = NULL_TREE;
45165 bool moreloops;
45166 unsigned char save_in_statement;
45167 tree add_private_clause = NULL_TREE;
45168 location_t loc;
45169 bool is_range_for = false;
45170 tree sl = NULL_TREE;
45171 struct omp_for_parse_data *omp_for_parse_state
45172 = parser->omp_for_parse_state;
45173 gcc_assert (omp_for_parse_state);
45174 int depth = omp_for_parse_state->depth;
45176 /* Handle loop transformations first. Note that when we get here
45177 omp_for_parse_state->depth has already been incremented to indicate
45178 the depth of the *next* loop, not the level of the loop body the
45179 transformation directive appears in. */
45181 /* Arrange for C++ standard attribute syntax to be parsed as regular
45182 pragmas. Give an error if there are other random attributes present. */
45183 cp_token *token = cp_lexer_peek_token (parser->lexer);
45184 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
45185 std_attrs = cp_parser_handle_statement_omp_attributes (parser, std_attrs);
45186 if (std_attrs)
45187 error_at (token->location,
45188 "attributes other than OpenMP directives "
45189 "are not allowed on %<for%> in loop nest");
45191 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45193 tree transform = NULL_TREE, sizes, body = NULL_TREE;
45194 int count = 0;
45195 cp_token *pragma_tok;
45196 tree stmt;
45197 loc = cp_lexer_peek_token (parser->lexer)->location;
45198 switch (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer)))
45200 case PRAGMA_OMP_UNROLL:
45201 pragma_tok = cp_lexer_consume_token (parser->lexer);
45202 parser->lexer->in_pragma = true;
45203 body = push_stmt_list ();
45204 stmt = push_omp_privatization_clauses (false);
45205 transform = cp_parser_omp_unroll (parser, pragma_tok, if_p);
45206 pop_omp_privatization_clauses (stmt);
45207 body = pop_stmt_list (body);
45208 if (transform == NULL_TREE || transform == error_mark_node)
45210 transform = error_mark_node;
45211 break;
45213 gcc_assert (TREE_CODE (transform) == OMP_UNROLL);
45214 if (omp_find_clause (OMP_FOR_CLAUSES (transform),
45215 OMP_CLAUSE_PARTIAL))
45217 if (omp_for_parse_state->count - depth > 1)
45219 error_at (loc, "%<unroll%> construct with %<partial%> "
45220 "clause generates just one loop with "
45221 "canonical form but %d loops are needed",
45222 omp_for_parse_state->count - depth);
45223 transform = error_mark_node;
45225 else
45226 count = 1;
45228 else
45230 error_at (loc, "generated loop of %<unroll%> construct "
45231 "without %<partial%> clause does not have "
45232 "canonical form");
45233 transform = error_mark_node;
45235 break;
45236 case PRAGMA_OMP_TILE:
45237 pragma_tok = cp_lexer_consume_token (parser->lexer);
45238 parser->lexer->in_pragma = true;
45239 body = push_stmt_list ();
45240 stmt = push_omp_privatization_clauses (false);
45241 transform = cp_parser_omp_tile (parser, pragma_tok, if_p);
45242 pop_omp_privatization_clauses (stmt);
45243 body = pop_stmt_list (body);
45244 if (transform == NULL_TREE || transform == error_mark_node)
45246 transform = error_mark_node;
45247 break;
45249 gcc_assert (TREE_CODE (transform) == OMP_TILE);
45250 sizes = omp_find_clause (OMP_FOR_CLAUSES (transform),
45251 OMP_CLAUSE_SIZES);
45252 gcc_assert (sizes);
45253 count = list_length (OMP_CLAUSE_SIZES_LIST (sizes));
45254 if (depth + count < omp_for_parse_state->count)
45256 error_at (loc, "%<tile%> construct generates %d loops "
45257 "with canonical form but %d loops are needed",
45258 count, omp_for_parse_state->count - depth);
45259 transform = error_mark_node;
45261 break;
45262 default:
45263 cp_parser_pragma (parser, pragma_stmt, NULL);
45264 break;
45266 if (transform == NULL_TREE)
45267 error_at (loc, "expected %<for%> loop or OpenMP loop "
45268 "transformation construct");
45269 if (transform == NULL_TREE || transform == error_mark_node)
45271 omp_for_parse_state->fail = true;
45272 return NULL_TREE;
45274 for (count = omp_for_parse_state->count; depth < count; ++depth)
45276 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = global_namespace;
45277 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = NULL_TREE;
45278 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = NULL_TREE;
45279 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = NULL_TREE;
45280 if (omp_for_parse_state->orig_declv)
45281 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = NULL_TREE;
45282 vec_safe_push (omp_for_parse_state->init_blockv, NULL_TREE);
45283 vec_safe_push (omp_for_parse_state->body_blockv, NULL_TREE);
45284 vec_safe_push (omp_for_parse_state->init_placeholderv, NULL_TREE);
45285 vec_safe_push (omp_for_parse_state->body_placeholderv, NULL_TREE);
45287 omp_for_parse_state->want_nested_loop = false;
45288 return body;
45291 /* Diagnose errors if we don't have a "for" loop following the
45292 optional loop transforms. Otherwise, consume the token. */
45293 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45295 omp_for_parse_state->fail = true;
45296 cp_token *token = cp_lexer_peek_token (parser->lexer);
45297 /* Don't call cp_parser_error here since it overrides the
45298 provided message with a more confusing one if there was
45299 a bad pragma or attribute directive. */
45300 error_at (token->location, "loop nest expected");
45301 /* See if we can recover by skipping over bad pragma(s). */
45302 while (token->type == CPP_PRAGMA)
45304 cp_parser_skip_to_pragma_eol (parser, token);
45305 if (cp_parser_next_tokens_can_be_canon_loop (parser, omp_for_parse_state->code,
45306 false))
45307 return cp_parser_omp_loop_nest (parser, if_p);
45308 token = cp_lexer_peek_token (parser->lexer);
45310 return NULL_TREE;
45312 loc = cp_lexer_consume_token (parser->lexer)->location;
45314 /* Forbid break/continue in the loop initializer, condition, and
45315 increment expressions. */
45316 save_in_statement = parser->in_statement;
45317 parser->in_statement = IN_OMP_BLOCK;
45319 /* We are not in intervening code now. */
45320 omp_for_parse_state->in_intervening_code = false;
45322 /* Don't create location wrapper nodes within an OpenMP "for"
45323 statement. */
45324 auto_suppress_location_wrappers sentinel;
45326 matching_parens parens;
45327 if (!parens.require_open (parser))
45328 return NULL;
45330 init_placeholder = build_stmt (input_location, EXPR_STMT,
45331 integer_zero_node);
45332 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
45334 /* The init_block acts as a container for this level of loop goo. */
45335 init_block = push_stmt_list ();
45336 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
45338 /* Wrap a scope around this entire level of loop to hold bindings
45339 of loop iteration variables. We can't insert them directly
45340 in the containing scope because that would cause their visibility to
45341 be incorrect with respect to intervening code after this loop.
45342 We will combine the nested init_scopes in postprocessing after the
45343 entire loop is parsed. */
45344 init_scope = begin_compound_stmt (0);
45346 /* Now we need another level of statement list container to capture the
45347 initialization (and possible finalization) bits. In some cases this
45348 container may be popped off during initializer parsing to store code in
45349 INIT or THIS_PRE_BODY, depending on the form of initialization. If
45350 we have a class iterator we will pop it at the end of parsing this
45351 level, so the cleanups are handled correctly. */
45352 sl = push_stmt_list ();
45354 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
45356 /* Save tokens so that we can put them back. */
45357 cp_lexer_save_tokens (parser->lexer);
45359 /* Look for ':' that is not nested in () or {}. */
45360 is_range_for
45361 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
45362 /*recovering=*/false,
45363 CPP_COLON,
45364 /*consume_paren=*/
45365 false) == -1);
45367 /* Roll back the tokens we skipped. */
45368 cp_lexer_rollback_tokens (parser->lexer);
45370 if (is_range_for)
45372 bool saved_colon_corrects_to_scope_p
45373 = parser->colon_corrects_to_scope_p;
45375 /* A colon is used in range-based for. */
45376 parser->colon_corrects_to_scope_p = false;
45378 /* Parse the declaration. */
45379 cp_parser_simple_declaration (parser,
45380 /*function_definition_allowed_p=*/
45381 false, &decl);
45382 parser->colon_corrects_to_scope_p
45383 = saved_colon_corrects_to_scope_p;
45385 cp_parser_require (parser, CPP_COLON, RT_COLON);
45387 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
45388 false, NULL_TREE, false, true);
45390 cp_convert_omp_range_for (this_pre_body, sl, decl,
45391 orig_decl, init, orig_init,
45392 cond, incr);
45394 if (omp_for_parse_state->ordered_cl)
45395 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
45396 "%<ordered%> clause with parameter on "
45397 "range-based %<for%> loop");
45399 goto parse_close_paren;
45403 add_private_clause
45404 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
45405 init, orig_init, decl, real_decl);
45407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
45409 /* If the iteration variable was introduced via a declaration in the
45410 for statement, DECL points at it. Otherwise DECL is null and
45411 REAL_DECL is a variable previously declared in an outer scope.
45412 Make REAL_DECL point at the iteration variable no matter where it
45413 was introduced. */
45414 if (decl)
45415 real_decl = decl;
45417 /* Some clauses treat iterator variables specially. */
45418 if (omp_for_parse_state->cclauses != NULL
45419 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
45420 && real_decl != NULL_TREE
45421 && omp_for_parse_state->code != OMP_LOOP)
45423 tree *c;
45424 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
45425 *c ; )
45426 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
45427 && OMP_CLAUSE_DECL (*c) == real_decl)
45429 error_at (loc, "iteration variable %qD"
45430 " should not be firstprivate", real_decl);
45431 *c = OMP_CLAUSE_CHAIN (*c);
45433 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
45434 && OMP_CLAUSE_DECL (*c) == real_decl)
45436 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
45437 tree l = *c;
45438 *c = OMP_CLAUSE_CHAIN (*c);
45439 if (omp_for_parse_state->code == OMP_SIMD)
45441 OMP_CLAUSE_CHAIN (l)
45442 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45443 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
45445 else
45447 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
45448 omp_for_parse_state->clauses = l;
45450 add_private_clause = NULL_TREE;
45452 else
45454 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
45455 && OMP_CLAUSE_DECL (*c) == real_decl)
45456 add_private_clause = NULL_TREE;
45457 c = &OMP_CLAUSE_CHAIN (*c);
45461 if (add_private_clause)
45463 tree c;
45464 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
45466 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
45467 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
45468 && OMP_CLAUSE_DECL (c) == decl)
45469 break;
45470 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
45471 && OMP_CLAUSE_DECL (c) == decl)
45472 error_at (loc, "iteration variable %qD "
45473 "should not be firstprivate",
45474 decl);
45475 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
45476 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
45477 && OMP_CLAUSE_DECL (c) == decl)
45478 error_at (loc, "iteration variable %qD should not be reduction",
45479 decl);
45481 if (c == NULL)
45483 if ((omp_for_parse_state->code == OMP_SIMD
45484 && omp_for_parse_state->count != 1)
45485 || omp_for_parse_state->code == OMP_LOOP)
45486 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
45487 else if (omp_for_parse_state->code != OMP_SIMD)
45488 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
45489 else
45490 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
45491 OMP_CLAUSE_DECL (c) = add_private_clause;
45492 c = finish_omp_clauses (c, C_ORT_OMP);
45493 if (c)
45495 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
45496 omp_for_parse_state->clauses = c;
45497 /* For linear, signal that we need to fill up
45498 the so far unknown linear step. */
45499 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
45500 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
45505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
45506 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
45507 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
45509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
45511 /* If decl is an iterator, preserve the operator on decl
45512 until finish_omp_for. */
45513 if (real_decl
45514 && ((processing_template_decl
45515 && (TREE_TYPE (real_decl) == NULL_TREE
45516 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
45517 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
45518 incr = cp_parser_omp_for_incr (parser, real_decl);
45519 else
45520 incr = cp_parser_expression (parser);
45521 protected_set_expr_location_if_unset (incr, input_location);
45524 parse_close_paren:
45525 if (!parens.require_close (parser))
45526 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
45527 /*or_comma=*/false,
45528 /*consume_paren=*/true);
45530 /* We've parsed all the for (...) stuff now. Store the bits. */
45531 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
45532 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
45533 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
45534 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
45535 if (orig_init)
45537 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
45538 omp_for_parse_state->orig_inits[depth] = orig_init;
45540 if (orig_decl)
45542 if (!omp_for_parse_state->orig_declv)
45543 omp_for_parse_state->orig_declv
45544 = copy_node (omp_for_parse_state->declv);
45545 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
45547 else if (omp_for_parse_state->orig_declv)
45548 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
45549 if (this_pre_body)
45550 append_to_statement_list_force (this_pre_body,
45551 &(omp_for_parse_state->pre_body));
45553 /* Start a nested block for the loop body. */
45554 body_placeholder = build_stmt (input_location, EXPR_STMT,
45555 integer_zero_node);
45556 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
45557 body_block = push_stmt_list ();
45558 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
45560 moreloops = depth < omp_for_parse_state->count - 1;
45561 omp_for_parse_state->want_nested_loop = moreloops;
45562 if (moreloops
45563 && cp_parser_next_tokens_can_be_canon_loop (parser,
45564 omp_for_parse_state->code,
45565 false))
45567 omp_for_parse_state->depth++;
45568 tree nest = cp_parser_omp_loop_nest (parser, if_p);
45569 if (nest)
45570 add_stmt (nest);
45571 omp_for_parse_state->depth--;
45573 else if (moreloops
45574 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
45576 /* This is the open brace in the loop-body grammar production. Rather
45577 than trying to special-case braces, just parse it as a compound
45578 statement and handle the nested loop-body case there. Note that
45579 when we see a further open brace inside the compound statement
45580 loop-body, we don't know whether it is the start of intervening
45581 code that is a compound statement, or a level of braces
45582 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
45583 bit to ensure we have only one nested loop at each level. */
45585 omp_for_parse_state->in_intervening_code = true;
45586 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
45587 omp_for_parse_state->in_intervening_code = false;
45589 if (omp_for_parse_state->want_nested_loop)
45591 /* We have already parsed the whole loop body and not found a
45592 nested loop. */
45593 error_at (omp_for_parse_state->for_loc,
45594 "not enough nested loops");
45595 omp_for_parse_state->fail = true;
45597 if_p = NULL;
45599 else
45601 /* This is the final-loop-body case in the grammar: we have something
45602 that is not a FOR and not an open brace. */
45603 if (moreloops)
45605 /* If we were expecting a nested loop, give an error and mark
45606 that parsing has failed, and try to recover by parsing the
45607 body as regular code without further collapsing. */
45608 error_at (omp_for_parse_state->for_loc,
45609 "not enough nested loops");
45610 omp_for_parse_state->fail = true;
45612 parser->in_statement = IN_OMP_FOR;
45614 /* Generate the parts of range for that belong in the loop body,
45615 to be executed on every iteration. This includes setting the
45616 user-declared decomposition variables from the compiler-generated
45617 temporaries that are the real iteration variables for OMP_FOR.
45618 FIXME: Not sure if this is correct with respect to visibility
45619 of the variables from intervening code. However, putting this
45620 code in each level of loop instead of all around the innermost
45621 body also makes the decomposition variables visible to the
45622 inner for init/bound/step exressions, which is not supposed to
45623 happen and causes test failures. */
45624 if (omp_for_parse_state->orig_declv)
45625 for (int i = 0; i < omp_for_parse_state->count; i++)
45627 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
45628 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
45629 if (o != d)
45630 cp_finish_omp_range_for (o, d);
45633 /* Now parse the final-loop-body for the innermost loop. */
45634 parser->omp_for_parse_state = NULL;
45635 if (omp_for_parse_state->inscan)
45636 cp_parser_omp_scan_loop_body (parser);
45637 else
45638 cp_parser_statement (parser, NULL_TREE, false, if_p);
45639 parser->omp_for_parse_state = omp_for_parse_state;
45641 parser->in_statement = save_in_statement;
45642 omp_for_parse_state->want_nested_loop = false;
45643 omp_for_parse_state->in_intervening_code = true;
45645 /* Pop and remember the body block. Add the body placeholder
45646 to the surrounding statement list instead. This is just a unique
45647 token that will be replaced when we reassemble the generated
45648 code for the entire omp for statement. */
45649 body_block = pop_stmt_list (body_block);
45650 omp_for_parse_state->body_blockv[depth] = body_block;
45651 add_stmt (body_placeholder);
45653 /* Pop and remember the init block. */
45654 if (sl)
45655 add_stmt (pop_stmt_list (sl));
45656 finish_compound_stmt (init_scope);
45657 init_block = pop_stmt_list (init_block);
45658 omp_for_parse_state->init_blockv[depth] = init_block;
45660 /* Return the init placeholder rather than the remembered init block.
45661 Again, this is just a unique cookie that will be used to reassemble
45662 code pieces when the entire omp for statement has been parsed. */
45663 return init_placeholder;
45666 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
45667 and ITER is the element that is or contains a nested loop. This
45668 function moves the statements before and after ITER into
45669 OMP_STRUCTURED_BLOCKs and modifies *TP. */
45670 static void
45671 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
45673 tree sl = push_stmt_list ();
45674 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
45675 if (i == iter)
45677 sl = pop_stmt_list (sl);
45678 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45679 tsi_link_before (&i,
45680 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45681 TSI_SAME_STMT);
45682 i++;
45683 sl = push_stmt_list ();
45685 else
45687 tree s = tsi_stmt (i);
45688 tsi_delink (&i); /* Advances i to next statement. */
45689 add_stmt (s);
45691 sl = pop_stmt_list (sl);
45692 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45693 tsi_link_after (&iter,
45694 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45695 TSI_SAME_STMT);
45698 /* Helper to find and mark structured blocks in intervening code for a
45699 single loop level with markers for later error checking. *TP is the
45700 piece of code to be marked and INNER is the inner loop placeholder.
45701 Returns true if INNER was found (recursively) in *TP. */
45702 static bool
45703 find_structured_blocks (tree *tp, tree inner)
45705 if (*tp == inner)
45706 return true;
45707 else if (TREE_CODE (*tp) == BIND_EXPR)
45708 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
45709 else if (TREE_CODE (*tp) == STATEMENT_LIST)
45711 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
45713 tree *p = tsi_stmt_ptr (i);
45714 /* The normal case is that there is no intervening code and we
45715 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
45716 if (find_structured_blocks (p, inner))
45718 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
45719 insert_structured_blocks (tp, i);
45720 return true;
45723 return false;
45725 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
45726 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
45727 else if (TREE_CODE (*tp) == CLEANUP_STMT)
45728 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
45729 else
45730 return false;
45733 /* Helpers used for relinking tree structures: In tree rooted at
45734 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
45735 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
45737 struct sit_data {
45738 tree orig;
45739 tree repl;
45740 bool flatten;
45743 static tree
45744 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
45745 void *dp)
45747 struct sit_data *sit = (struct sit_data *)dp;
45748 if (*tp == sit->orig)
45750 *tp = sit->repl;
45751 return *tp;
45753 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
45754 trying to flatten. */
45755 else if (TREE_CODE (*tp) == BIND_EXPR
45756 && BIND_EXPR_BODY (*tp) == sit->orig
45757 && !BIND_EXPR_VARS (*tp)
45758 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
45760 *tp = sit->repl;
45761 return *tp;
45763 else if (sit->flatten
45764 && TREE_CODE (*tp) == BIND_EXPR
45765 && TREE_CODE (sit->repl) == BIND_EXPR)
45767 if (BIND_EXPR_BODY (*tp) == sit->orig)
45769 /* Merge binding lists for two directly nested BIND_EXPRs,
45770 keeping the outer one. */
45771 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45772 BIND_EXPR_VARS (sit->repl));
45773 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
45774 return *tp;
45776 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
45777 /* There might be a statement list containing cleanup_points
45778 etc between the two levels of BIND_EXPR. We can still merge
45779 them, again keeping the outer BIND_EXPR. */
45780 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
45781 !tsi_end_p (i); ++i)
45783 tree *p = tsi_stmt_ptr (i);
45784 if (*p == sit->orig)
45786 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45787 BIND_EXPR_VARS (sit->repl));
45788 *p = BIND_EXPR_BODY (sit->repl);
45789 return *tp;
45793 return NULL;
45796 static void
45797 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
45799 struct sit_data data;
45801 gcc_assert (*context && orig && repl);
45802 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
45803 repl = BIND_EXPR_BODY (repl);
45804 data.orig = orig;
45805 data.repl = repl;
45806 data.flatten = flatten;
45808 tree result = cp_walk_tree (context, substitute_in_tree_walker,
45809 (void *)&data, NULL);
45810 gcc_assert (result != NULL_TREE);
45813 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
45814 *DP is the parent block. */
45816 static tree
45817 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
45819 tree superblock = *(tree *)dp;
45821 /* BIND_EXPR_BLOCK may be null if the expression is not a
45822 full-expression; if there's no block, no patching is necessary
45823 for this node. */
45824 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
45826 tree block = BIND_EXPR_BLOCK (*tp);
45827 if (superblock)
45829 BLOCK_SUPERCONTEXT (block) = superblock;
45830 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
45831 BLOCK_SUBBLOCKS (superblock) = block;
45833 BLOCK_SUBBLOCKS (block) = NULL_TREE;
45834 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
45835 (void *)&block, NULL);
45836 *walk_subtrees = 0;
45839 return NULL;
45842 /* Parse the restricted form of the for statement allowed by OpenMP. */
45844 static tree
45845 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
45846 tree *cclauses, bool *if_p)
45848 tree ret;
45849 tree cl, ordered_cl = NULL_TREE;
45850 int collapse = 1, ordered = 0;
45851 unsigned int count;
45852 bool oacc_tiling = false;
45853 bool inscan = false;
45854 struct omp_for_parse_data data;
45855 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
45856 tree result;
45857 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
45859 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
45860 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
45861 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
45862 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
45864 oacc_tiling = true;
45865 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
45867 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_SIZES)
45868 collapse = list_length (OMP_CLAUSE_SIZES_LIST (cl));
45869 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
45870 && OMP_CLAUSE_ORDERED_EXPR (cl))
45872 ordered_cl = cl;
45873 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
45875 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
45876 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
45877 && (code == OMP_SIMD || code == OMP_FOR))
45878 inscan = true;
45880 if (ordered && ordered < collapse)
45882 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
45883 "%<ordered%> clause parameter is less than %<collapse%>");
45884 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
45885 = build_int_cst (NULL_TREE, collapse);
45886 ordered = collapse;
45889 gcc_assert (oacc_tiling || (collapse >= 1 && ordered >= 0));
45890 count = ordered ? ordered : collapse;
45892 if (!cp_parser_next_tokens_can_be_canon_loop (parser, code, true))
45893 return NULL;
45895 /* Initialize parse state for recursive descent. */
45896 data.declv = make_tree_vec (count);
45897 data.initv = make_tree_vec (count);
45898 data.condv = make_tree_vec (count);
45899 data.incrv = make_tree_vec (count);
45900 data.pre_body = NULL_TREE;
45901 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
45902 data.count = count;
45903 data.depth = 0;
45904 data.want_nested_loop = true;
45905 data.ordered = ordered > 0;
45906 data.in_intervening_code = false;
45907 data.perfect_nesting_fail = false;
45908 data.fail = false;
45909 data.inscan = inscan;
45910 data.saw_intervening_code = false;
45911 data.code = code;
45912 data.orig_declv = NULL_TREE;
45913 data.clauses = clauses;
45914 data.cclauses = cclauses;
45915 data.ordered_cl = ordered_cl;
45916 parser->omp_for_parse_state = &data;
45918 tree body = cp_parser_omp_loop_nest (parser, if_p);
45920 /* Bomb out early if there was an error (not enough loops, etc). */
45921 if (data.fail || data.declv == NULL_TREE)
45923 parser->omp_for_parse_state = save_data;
45924 return NULL_TREE;
45927 /* Relink the init and body blocks that were built during parsing. At
45928 this point we have a structure nested like
45929 init 0
45930 body 0
45931 init 1
45932 body 1
45933 init 2
45934 body 2
45935 and we want to turn it into
45936 init 0
45937 init 1
45938 init 2
45939 omp_for
45940 body 0
45941 body 1
45942 body 2
45943 We also need to flatten the init blocks, as some code for later
45944 processing of combined directives gets confused otherwise. */
45946 gcc_assert (vec_safe_length (data.init_blockv) == count);
45947 gcc_assert (vec_safe_length (data.body_blockv) == count);
45948 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
45949 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
45951 /* First insert markers for structured blocks for intervening code in
45952 the loop bodies. */
45953 for (unsigned int i = 0; i < count - 1; i++)
45954 if (data.body_blockv[i])
45955 for (unsigned int j = i + 1; j < count; j++)
45956 if (data.init_placeholderv[j])
45958 bool good = find_structured_blocks (&data.body_blockv[i],
45959 data.init_placeholderv[j]);
45960 gcc_assert (good);
45961 break;
45964 /* Do the substitution from the inside out. */
45965 for (unsigned int i = count - 1; i > 0; i--)
45966 if (data.init_placeholderv[i])
45967 for (unsigned int j = i; j > 0; j--)
45968 if (data.body_blockv[j - 1])
45970 substitute_in_tree (&data.body_blockv[j - 1],
45971 data.init_placeholderv[i],
45972 data.body_blockv[i], false);
45973 substitute_in_tree (&data.init_blockv[j - 1],
45974 data.body_placeholderv[j - 1],
45975 data.init_blockv[i], true);
45976 break;
45979 for (unsigned int i = 0; i < count; ++i)
45980 if (data.body_blockv[i])
45982 body = data.body_blockv[i];
45983 break;
45986 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
45987 (and possibly other stuff) to the current statement list but
45988 returns a pointer to the OMP_FOR itself, or null in case of error. */
45989 result = push_stmt_list ();
45990 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
45991 data.initv, data.condv, data.incrv, body,
45992 data.pre_body, &data.orig_inits, data.clauses);
45993 result = pop_stmt_list (result);
45995 /* Check for errors involving lb/ub/incr expressions referencing
45996 variables declared in intervening code. */
45997 if (data.saw_intervening_code
45998 && ret
45999 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
46000 ret = NULL_TREE;
46002 if (ret)
46003 for (unsigned int i = 0; i < count; ++i)
46004 if (data.init_blockv[i])
46006 int j;
46007 for (j = count - 1; j >= 0; --j)
46008 if (data.body_placeholderv[j])
46009 break;
46010 gcc_assert (j >= 0);
46012 /* Splice the omp_for into the nest of init blocks. */
46013 substitute_in_tree (&data.init_blockv[i],
46014 data.body_placeholderv[j],
46015 result, true);
46017 /* Some later processing for combined directives assumes
46018 that the BIND_EXPR containing range for variables appears
46019 at top level in the OMP_FOR body. Fix that up if it's
46020 not the case, e.g. because there is intervening code. */
46021 if (code != OACC_LOOP)
46022 finish_omp_for_block (data.init_blockv[i], ret);
46024 /* Clean up the block subblock/superblock links. Per comment in
46025 begin_compound_stmt, "we don't build BLOCK nodes when processing
46026 templates", so skip this step in that case. */
46027 if (!processing_template_decl)
46029 tree superblock = NULL_TREE;
46030 cp_walk_tree (&data.init_blockv[i], fixup_blocks_walker,
46031 (void *)&superblock, NULL);
46034 /* Finally record the result. */
46035 add_stmt (data.init_blockv[0]);
46036 result = NULL_TREE;
46037 break;
46039 if (ret && result)
46040 add_stmt (result);
46042 parser->omp_for_parse_state = save_data;
46043 return ret;
46046 /* Helper function for OpenMP parsing, split clauses and call
46047 finish_omp_clauses on each of the set of clauses afterwards. */
46049 static void
46050 cp_omp_split_clauses (location_t loc, enum tree_code code,
46051 omp_clause_mask mask, tree clauses, tree *cclauses)
46053 int i;
46054 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
46055 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
46056 if (cclauses[i])
46057 cclauses[i] = finish_omp_clauses (cclauses[i],
46058 i == C_OMP_CLAUSE_SPLIT_TARGET
46059 ? C_ORT_OMP_TARGET : C_ORT_OMP);
46062 /* OpenMP 5.0:
46063 #pragma omp loop loop-clause[optseq] new-line
46064 for-loop */
46066 #define OMP_LOOP_CLAUSE_MASK \
46067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
46072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46074 static tree
46075 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
46076 char *p_name, omp_clause_mask mask, tree *cclauses,
46077 bool *if_p)
46079 tree clauses, sb, ret;
46080 unsigned int save;
46081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46083 strcat (p_name, " loop");
46084 mask |= OMP_LOOP_CLAUSE_MASK;
46086 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46087 cclauses == NULL);
46088 if (cclauses)
46090 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
46091 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
46094 keep_next_level (true);
46095 sb = begin_omp_structured_block ();
46096 save = cp_parser_begin_omp_structured_block (parser);
46098 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
46100 cp_parser_end_omp_structured_block (parser, save);
46101 add_stmt (finish_omp_structured_block (sb));
46103 return ret;
46106 /* OpenMP 4.0:
46107 #pragma omp simd simd-clause[optseq] new-line
46108 for-loop */
46110 #define OMP_SIMD_CLAUSE_MASK \
46111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
46112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
46113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
46115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
46121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46123 static tree
46124 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
46125 char *p_name, omp_clause_mask mask, tree *cclauses,
46126 bool *if_p)
46128 tree clauses, sb, ret;
46129 unsigned int save;
46130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46132 strcat (p_name, " simd");
46133 mask |= OMP_SIMD_CLAUSE_MASK;
46135 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46136 cclauses == NULL);
46137 if (cclauses)
46139 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
46140 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
46143 keep_next_level (true);
46144 sb = begin_omp_structured_block ();
46145 save = cp_parser_begin_omp_structured_block (parser);
46147 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
46149 cp_parser_end_omp_structured_block (parser, save);
46150 add_stmt (finish_omp_structured_block (sb));
46152 return ret;
46155 /* OpenMP 2.5:
46156 #pragma omp for for-clause[optseq] new-line
46157 for-loop
46159 OpenMP 4.0:
46160 #pragma omp for simd for-simd-clause[optseq] new-line
46161 for-loop */
46163 #define OMP_FOR_CLAUSE_MASK \
46164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
46170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
46171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46176 static tree
46177 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
46178 char *p_name, omp_clause_mask mask, tree *cclauses,
46179 bool *if_p)
46181 tree clauses, sb, ret;
46182 unsigned int save;
46183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46185 strcat (p_name, " for");
46186 mask |= OMP_FOR_CLAUSE_MASK;
46187 /* parallel for{, simd} disallows nowait clause, but for
46188 target {teams distribute ,}parallel for{, simd} it should be accepted. */
46189 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
46190 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
46191 /* Composite distribute parallel for{, simd} disallows ordered clause. */
46192 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
46193 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
46195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46198 const char *p = IDENTIFIER_POINTER (id);
46200 if (strcmp (p, "simd") == 0)
46202 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46203 if (cclauses == NULL)
46204 cclauses = cclauses_buf;
46206 cp_lexer_consume_token (parser->lexer);
46207 if (!flag_openmp) /* flag_openmp_simd */
46208 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46209 cclauses, if_p);
46210 sb = begin_omp_structured_block ();
46211 save = cp_parser_begin_omp_structured_block (parser);
46212 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46213 cclauses, if_p);
46214 cp_parser_end_omp_structured_block (parser, save);
46215 tree body = finish_omp_structured_block (sb);
46216 if (ret == NULL)
46217 return ret;
46218 ret = make_node (OMP_FOR);
46219 TREE_TYPE (ret) = void_type_node;
46220 OMP_FOR_BODY (ret) = body;
46221 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
46222 SET_EXPR_LOCATION (ret, loc);
46223 add_stmt (ret);
46224 return ret;
46227 if (!flag_openmp) /* flag_openmp_simd */
46229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46230 return NULL_TREE;
46233 /* Composite distribute parallel for disallows linear clause. */
46234 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
46235 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
46237 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46238 cclauses == NULL);
46239 if (cclauses)
46241 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
46242 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
46245 keep_next_level (true);
46246 sb = begin_omp_structured_block ();
46247 save = cp_parser_begin_omp_structured_block (parser);
46249 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
46251 cp_parser_end_omp_structured_block (parser, save);
46252 add_stmt (finish_omp_structured_block (sb));
46254 return ret;
46257 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
46258 omp_clause_mask, tree *, bool *);
46260 /* OpenMP 2.5:
46261 # pragma omp master new-line
46262 structured-block */
46264 static tree
46265 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
46266 char *p_name, omp_clause_mask mask, tree *cclauses,
46267 bool *if_p)
46269 tree clauses, sb, ret;
46270 unsigned int save;
46271 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46273 strcat (p_name, " master");
46275 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46277 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46278 const char *p = IDENTIFIER_POINTER (id);
46280 if (strcmp (p, "taskloop") == 0)
46282 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46283 if (cclauses == NULL)
46284 cclauses = cclauses_buf;
46286 cp_lexer_consume_token (parser->lexer);
46287 if (!flag_openmp) /* flag_openmp_simd */
46288 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
46289 cclauses, if_p);
46290 sb = begin_omp_structured_block ();
46291 save = cp_parser_begin_omp_structured_block (parser);
46292 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
46293 cclauses, if_p);
46294 cp_parser_end_omp_structured_block (parser, save);
46295 tree body = finish_omp_structured_block (sb);
46296 if (ret == NULL)
46297 return ret;
46298 ret = c_finish_omp_master (loc, body);
46299 OMP_MASTER_COMBINED (ret) = 1;
46300 return ret;
46303 if (!flag_openmp) /* flag_openmp_simd */
46305 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46306 return NULL_TREE;
46309 if (cclauses)
46311 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46312 false);
46313 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
46315 else
46316 cp_parser_require_pragma_eol (parser, pragma_tok);
46318 return c_finish_omp_master (loc,
46319 cp_parser_omp_structured_block (parser, if_p));
46322 /* OpenMP 5.1:
46323 # pragma omp masked masked-clauses new-line
46324 structured-block */
46326 #define OMP_MASKED_CLAUSE_MASK \
46327 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
46329 static tree
46330 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
46331 char *p_name, omp_clause_mask mask, tree *cclauses,
46332 bool *if_p)
46334 tree clauses, sb, ret;
46335 unsigned int save;
46336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46338 strcat (p_name, " masked");
46339 mask |= OMP_MASKED_CLAUSE_MASK;
46341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46344 const char *p = IDENTIFIER_POINTER (id);
46346 if (strcmp (p, "taskloop") == 0)
46348 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46349 if (cclauses == NULL)
46350 cclauses = cclauses_buf;
46352 cp_lexer_consume_token (parser->lexer);
46353 if (!flag_openmp) /* flag_openmp_simd */
46354 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
46355 cclauses, if_p);
46356 sb = begin_omp_structured_block ();
46357 save = cp_parser_begin_omp_structured_block (parser);
46358 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
46359 cclauses, if_p);
46360 cp_parser_end_omp_structured_block (parser, save);
46361 tree body = finish_omp_structured_block (sb);
46362 if (ret == NULL)
46363 return ret;
46364 ret = c_finish_omp_masked (loc, body,
46365 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
46366 OMP_MASKED_COMBINED (ret) = 1;
46367 return ret;
46370 if (!flag_openmp) /* flag_openmp_simd */
46372 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46373 return NULL_TREE;
46376 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46377 cclauses == NULL);
46378 if (cclauses)
46380 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
46381 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
46384 return c_finish_omp_masked (loc,
46385 cp_parser_omp_structured_block (parser, if_p),
46386 clauses);
46389 /* OpenMP 2.5:
46390 # pragma omp ordered new-line
46391 structured-block
46393 OpenMP 4.5:
46394 # pragma omp ordered ordered-clauses new-line
46395 structured-block */
46397 #define OMP_ORDERED_CLAUSE_MASK \
46398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
46399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
46401 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
46402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
46405 static bool
46406 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
46407 enum pragma_context context, bool *if_p)
46409 location_t loc = pragma_tok->location;
46410 int n = 1;
46412 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46413 n = 2;
46415 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
46417 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
46418 const char *p = IDENTIFIER_POINTER (id);
46420 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
46422 if (!flag_openmp) /* flag_openmp_simd */
46424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46425 return false;
46427 if (context == pragma_stmt)
46429 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
46430 "%qs clause may only be used in compound "
46431 "statements", p);
46432 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46433 return true;
46435 tree clauses
46436 = cp_parser_omp_all_clauses (parser,
46437 OMP_ORDERED_DEPEND_CLAUSE_MASK,
46438 "#pragma omp ordered", pragma_tok);
46439 c_finish_omp_ordered (loc, clauses, NULL_TREE);
46440 return false;
46444 tree clauses
46445 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
46446 "#pragma omp ordered", pragma_tok);
46448 if (!flag_openmp /* flag_openmp_simd */
46449 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
46450 return false;
46452 c_finish_omp_ordered (loc, clauses,
46453 cp_parser_omp_structured_block (parser, if_p));
46454 return true;
46457 /* OpenMP 2.5:
46459 section-scope:
46460 { section-sequence }
46462 section-sequence:
46463 section-directive[opt] structured-block
46464 section-sequence section-directive structured-block */
46466 static tree
46467 cp_parser_omp_sections_scope (cp_parser *parser)
46469 tree stmt, substmt;
46470 bool error_suppress = false;
46471 cp_token *tok;
46473 matching_braces braces;
46474 if (!braces.require_open (parser))
46475 return NULL_TREE;
46477 stmt = push_stmt_list ();
46479 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
46480 != PRAGMA_OMP_SECTION
46481 && !cp_parser_omp_section_scan (parser, "section", true))
46483 substmt = cp_parser_omp_structured_block_sequence (parser,
46484 PRAGMA_OMP_SECTION);
46485 substmt = build1 (OMP_SECTION, void_type_node, substmt);
46486 add_stmt (substmt);
46489 while (1)
46491 tok = cp_lexer_peek_token (parser->lexer);
46492 if (tok->type == CPP_CLOSE_BRACE)
46493 break;
46494 if (tok->type == CPP_EOF)
46495 break;
46497 if (cp_parser_omp_section_scan (parser, "section", false))
46498 tok = cp_lexer_peek_token (parser->lexer);
46499 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
46501 cp_lexer_consume_token (parser->lexer);
46502 cp_parser_require_pragma_eol (parser, tok);
46503 error_suppress = false;
46505 else if (!error_suppress)
46507 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
46508 error_suppress = true;
46511 substmt = cp_parser_omp_structured_block_sequence (parser,
46512 PRAGMA_OMP_SECTION);
46513 substmt = build1 (OMP_SECTION, void_type_node, substmt);
46514 add_stmt (substmt);
46516 braces.require_close (parser);
46518 substmt = pop_stmt_list (stmt);
46520 stmt = make_node (OMP_SECTIONS);
46521 TREE_TYPE (stmt) = void_type_node;
46522 OMP_SECTIONS_BODY (stmt) = substmt;
46524 add_stmt (stmt);
46525 return stmt;
46528 /* OpenMP 2.5:
46529 # pragma omp sections sections-clause[optseq] newline
46530 sections-scope */
46532 #define OMP_SECTIONS_CLAUSE_MASK \
46533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46540 static tree
46541 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
46542 char *p_name, omp_clause_mask mask, tree *cclauses)
46544 tree clauses, ret;
46545 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46547 strcat (p_name, " sections");
46548 mask |= OMP_SECTIONS_CLAUSE_MASK;
46549 if (cclauses)
46550 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
46552 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46553 cclauses == NULL);
46554 if (cclauses)
46556 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
46557 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
46560 ret = cp_parser_omp_sections_scope (parser);
46561 if (ret)
46562 OMP_SECTIONS_CLAUSES (ret) = clauses;
46564 return ret;
46567 /* OpenMP 2.5:
46568 # pragma omp parallel parallel-clause[optseq] new-line
46569 structured-block
46570 # pragma omp parallel for parallel-for-clause[optseq] new-line
46571 structured-block
46572 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
46573 structured-block
46575 OpenMP 4.0:
46576 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
46577 structured-block */
46579 #define OMP_PARALLEL_CLAUSE_MASK \
46580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
46586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
46588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
46591 static tree
46592 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
46593 char *p_name, omp_clause_mask mask, tree *cclauses,
46594 bool *if_p)
46596 tree stmt, clauses, block;
46597 unsigned int save;
46598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46600 strcat (p_name, " parallel");
46601 mask |= OMP_PARALLEL_CLAUSE_MASK;
46602 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
46603 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
46604 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
46605 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
46607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
46609 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46610 if (cclauses == NULL)
46611 cclauses = cclauses_buf;
46613 cp_lexer_consume_token (parser->lexer);
46614 if (!flag_openmp) /* flag_openmp_simd */
46615 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46616 if_p);
46617 block = begin_omp_parallel ();
46618 save = cp_parser_begin_omp_structured_block (parser);
46619 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46620 if_p);
46621 cp_parser_end_omp_structured_block (parser, save);
46622 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46623 block);
46624 if (ret == NULL_TREE)
46625 return ret;
46626 OMP_PARALLEL_COMBINED (stmt) = 1;
46627 return stmt;
46629 /* When combined with distribute, parallel has to be followed by for.
46630 #pragma omp target parallel is allowed though. */
46631 else if (cclauses
46632 && (mask & (OMP_CLAUSE_MASK_1
46633 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
46635 error_at (loc, "expected %<for%> after %qs", p_name);
46636 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46637 return NULL_TREE;
46639 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46641 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46642 const char *p = IDENTIFIER_POINTER (id);
46643 if (cclauses == NULL && strcmp (p, "masked") == 0)
46645 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46646 cclauses = cclauses_buf;
46648 cp_lexer_consume_token (parser->lexer);
46649 if (!flag_openmp) /* flag_openmp_simd */
46650 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46651 cclauses, if_p);
46652 block = begin_omp_parallel ();
46653 save = cp_parser_begin_omp_structured_block (parser);
46654 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46655 cclauses, if_p);
46656 cp_parser_end_omp_structured_block (parser, save);
46657 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46658 block);
46659 if (ret == NULL_TREE)
46660 return ret;
46661 /* masked does have just filter clause, but during gimplification
46662 isn't represented by a gimplification omp context, so for
46663 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
46664 so that
46665 #pragma omp parallel masked
46666 #pragma omp taskloop simd lastprivate (x)
46667 isn't confused with
46668 #pragma omp parallel masked taskloop simd lastprivate (x) */
46669 if (OMP_MASKED_COMBINED (ret))
46670 OMP_PARALLEL_COMBINED (stmt) = 1;
46671 return stmt;
46673 else if (cclauses == NULL && strcmp (p, "master") == 0)
46675 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46676 cclauses = cclauses_buf;
46678 cp_lexer_consume_token (parser->lexer);
46679 if (!flag_openmp) /* flag_openmp_simd */
46680 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46681 cclauses, if_p);
46682 block = begin_omp_parallel ();
46683 save = cp_parser_begin_omp_structured_block (parser);
46684 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46685 cclauses, if_p);
46686 cp_parser_end_omp_structured_block (parser, save);
46687 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46688 block);
46689 if (ret == NULL_TREE)
46690 return ret;
46691 /* master doesn't have any clauses and during gimplification
46692 isn't represented by a gimplification omp context, so for
46693 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
46694 so that
46695 #pragma omp parallel master
46696 #pragma omp taskloop simd lastprivate (x)
46697 isn't confused with
46698 #pragma omp parallel master taskloop simd lastprivate (x) */
46699 if (OMP_MASTER_COMBINED (ret))
46700 OMP_PARALLEL_COMBINED (stmt) = 1;
46701 return stmt;
46703 else if (strcmp (p, "loop") == 0)
46705 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46706 if (cclauses == NULL)
46707 cclauses = cclauses_buf;
46709 cp_lexer_consume_token (parser->lexer);
46710 if (!flag_openmp) /* flag_openmp_simd */
46711 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46712 cclauses, if_p);
46713 block = begin_omp_parallel ();
46714 save = cp_parser_begin_omp_structured_block (parser);
46715 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46716 cclauses, if_p);
46717 cp_parser_end_omp_structured_block (parser, save);
46718 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46719 block);
46720 if (ret == NULL_TREE)
46721 return ret;
46722 OMP_PARALLEL_COMBINED (stmt) = 1;
46723 return stmt;
46725 else if (!flag_openmp) /* flag_openmp_simd */
46727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46728 return NULL_TREE;
46730 else if (cclauses == NULL && strcmp (p, "sections") == 0)
46732 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46733 cclauses = cclauses_buf;
46735 cp_lexer_consume_token (parser->lexer);
46736 block = begin_omp_parallel ();
46737 save = cp_parser_begin_omp_structured_block (parser);
46738 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
46739 cp_parser_end_omp_structured_block (parser, save);
46740 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46741 block);
46742 OMP_PARALLEL_COMBINED (stmt) = 1;
46743 return stmt;
46746 else if (!flag_openmp) /* flag_openmp_simd */
46748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46749 return NULL_TREE;
46752 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46753 cclauses == NULL);
46754 if (cclauses)
46756 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
46757 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
46760 block = begin_omp_parallel ();
46761 save = cp_parser_begin_omp_structured_block (parser);
46762 parser->omp_attrs_forbidden_p = true;
46763 cp_parser_statement (parser, NULL_TREE, false, if_p);
46764 cp_parser_end_omp_structured_block (parser, save);
46765 stmt = finish_omp_parallel (clauses, block);
46766 return stmt;
46769 /* OpenMP 2.5:
46770 # pragma omp single single-clause[optseq] new-line
46771 structured-block */
46773 #define OMP_SINGLE_CLAUSE_MASK \
46774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
46777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46780 static tree
46781 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46783 tree stmt = make_node (OMP_SINGLE);
46784 TREE_TYPE (stmt) = void_type_node;
46785 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46787 OMP_SINGLE_CLAUSES (stmt)
46788 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
46789 "#pragma omp single", pragma_tok);
46790 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46792 return add_stmt (stmt);
46795 /* OpenMP 5.1:
46796 # pragma omp scope scope-clause[optseq] new-line
46797 structured-block */
46799 #define OMP_SCOPE_CLAUSE_MASK \
46800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46806 static tree
46807 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46809 tree stmt = make_node (OMP_SCOPE);
46810 TREE_TYPE (stmt) = void_type_node;
46811 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46813 OMP_SCOPE_CLAUSES (stmt)
46814 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
46815 "#pragma omp scope", pragma_tok);
46816 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46818 return add_stmt (stmt);
46821 /* OpenMP 3.0:
46822 # pragma omp task task-clause[optseq] new-line
46823 structured-block */
46825 #define OMP_TASK_CLAUSE_MASK \
46826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
46839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
46841 static tree
46842 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46844 tree clauses, block;
46845 unsigned int save;
46847 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
46848 "#pragma omp task", pragma_tok);
46849 block = begin_omp_task ();
46850 save = cp_parser_begin_omp_structured_block (parser);
46851 parser->omp_attrs_forbidden_p = true;
46852 cp_parser_statement (parser, NULL_TREE, false, if_p);
46853 cp_parser_end_omp_structured_block (parser, save);
46854 return finish_omp_task (clauses, block);
46857 /* OpenMP 3.0:
46858 # pragma omp taskwait new-line
46860 OpenMP 5.0:
46861 # pragma omp taskwait taskwait-clause[opt] new-line */
46863 #define OMP_TASKWAIT_CLAUSE_MASK \
46864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46867 static void
46868 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
46870 tree clauses
46871 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
46872 "#pragma omp taskwait", pragma_tok);
46874 if (clauses)
46876 tree stmt = make_node (OMP_TASK);
46877 TREE_TYPE (stmt) = void_node;
46878 OMP_TASK_CLAUSES (stmt) = clauses;
46879 OMP_TASK_BODY (stmt) = NULL_TREE;
46880 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46881 add_stmt (stmt);
46883 else
46884 finish_omp_taskwait ();
46887 /* OpenMP 3.1:
46888 # pragma omp taskyield new-line */
46890 static void
46891 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
46893 cp_parser_require_pragma_eol (parser, pragma_tok);
46894 finish_omp_taskyield ();
46897 /* OpenMP 4.0:
46898 # pragma omp taskgroup new-line
46899 structured-block
46901 OpenMP 5.0:
46902 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
46904 #define OMP_TASKGROUP_CLAUSE_MASK \
46905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
46908 static tree
46909 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46911 tree clauses
46912 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
46913 "#pragma omp taskgroup", pragma_tok);
46914 return c_finish_omp_taskgroup (input_location,
46915 cp_parser_omp_structured_block (parser,
46916 if_p),
46917 clauses);
46921 /* OpenMP 2.5:
46922 # pragma omp threadprivate (variable-list) */
46924 static void
46925 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
46927 tree vars;
46929 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
46930 cp_parser_require_pragma_eol (parser, pragma_tok);
46932 finish_omp_threadprivate (vars);
46935 /* OpenMP 4.0:
46936 # pragma omp cancel cancel-clause[optseq] new-line */
46938 #define OMP_CANCEL_CLAUSE_MASK \
46939 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
46943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
46945 static void
46946 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
46948 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
46949 "#pragma omp cancel", pragma_tok);
46950 finish_omp_cancel (clauses);
46953 /* OpenMP 4.0:
46954 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
46956 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
46957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
46962 static bool
46963 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
46964 enum pragma_context context)
46966 tree clauses;
46967 bool point_seen = false;
46969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46972 const char *p = IDENTIFIER_POINTER (id);
46974 if (strcmp (p, "point") == 0)
46976 cp_lexer_consume_token (parser->lexer);
46977 point_seen = true;
46980 if (!point_seen)
46982 cp_parser_error (parser, "expected %<point%>");
46983 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46984 return false;
46987 if (context != pragma_compound)
46989 if (context == pragma_stmt)
46990 error_at (pragma_tok->location,
46991 "%<#pragma %s%> may only be used in compound statements",
46992 "omp cancellation point");
46993 else
46994 cp_parser_error (parser, "expected declaration specifiers");
46995 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46996 return true;
46999 clauses = cp_parser_omp_all_clauses (parser,
47000 OMP_CANCELLATION_POINT_CLAUSE_MASK,
47001 "#pragma omp cancellation point",
47002 pragma_tok);
47003 finish_omp_cancellation_point (clauses);
47004 return true;
47007 /* OpenMP 4.0:
47008 #pragma omp distribute distribute-clause[optseq] new-line
47009 for-loop */
47011 #define OMP_DISTRIBUTE_CLAUSE_MASK \
47012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
47015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
47016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
47018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
47020 static tree
47021 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
47022 char *p_name, omp_clause_mask mask, tree *cclauses,
47023 bool *if_p)
47025 tree clauses, sb, ret;
47026 unsigned int save;
47027 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47029 strcat (p_name, " distribute");
47030 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
47032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47034 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47035 const char *p = IDENTIFIER_POINTER (id);
47036 bool simd = false;
47037 bool parallel = false;
47039 if (strcmp (p, "simd") == 0)
47040 simd = true;
47041 else
47042 parallel = strcmp (p, "parallel") == 0;
47043 if (parallel || simd)
47045 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
47046 if (cclauses == NULL)
47047 cclauses = cclauses_buf;
47048 cp_lexer_consume_token (parser->lexer);
47049 if (!flag_openmp) /* flag_openmp_simd */
47051 if (simd)
47052 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47053 cclauses, if_p);
47054 else
47055 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
47056 cclauses, if_p);
47058 sb = begin_omp_structured_block ();
47059 save = cp_parser_begin_omp_structured_block (parser);
47060 if (simd)
47061 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47062 cclauses, if_p);
47063 else
47064 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
47065 cclauses, if_p);
47066 cp_parser_end_omp_structured_block (parser, save);
47067 tree body = finish_omp_structured_block (sb);
47068 if (ret == NULL)
47069 return ret;
47070 ret = make_node (OMP_DISTRIBUTE);
47071 TREE_TYPE (ret) = void_type_node;
47072 OMP_FOR_BODY (ret) = body;
47073 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
47074 SET_EXPR_LOCATION (ret, loc);
47075 add_stmt (ret);
47076 return ret;
47079 if (!flag_openmp) /* flag_openmp_simd */
47081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47082 return NULL_TREE;
47085 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
47086 cclauses == NULL);
47087 if (cclauses)
47089 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
47090 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
47093 keep_next_level (true);
47094 sb = begin_omp_structured_block ();
47095 save = cp_parser_begin_omp_structured_block (parser);
47097 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
47099 cp_parser_end_omp_structured_block (parser, save);
47100 add_stmt (finish_omp_structured_block (sb));
47102 return ret;
47105 /* OpenMP 4.0:
47106 # pragma omp teams teams-clause[optseq] new-line
47107 structured-block */
47109 #define OMP_TEAMS_CLAUSE_MASK \
47110 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
47113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
47114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
47115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
47116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
47119 static tree
47120 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
47121 char *p_name, omp_clause_mask mask, tree *cclauses,
47122 bool *if_p)
47124 tree clauses, sb, ret;
47125 unsigned int save;
47126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47128 strcat (p_name, " teams");
47129 mask |= OMP_TEAMS_CLAUSE_MASK;
47131 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47134 const char *p = IDENTIFIER_POINTER (id);
47135 if (strcmp (p, "distribute") == 0)
47137 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
47138 if (cclauses == NULL)
47139 cclauses = cclauses_buf;
47141 cp_lexer_consume_token (parser->lexer);
47142 if (!flag_openmp) /* flag_openmp_simd */
47143 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
47144 cclauses, if_p);
47145 keep_next_level (true);
47146 sb = begin_omp_structured_block ();
47147 save = cp_parser_begin_omp_structured_block (parser);
47148 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
47149 cclauses, if_p);
47150 cp_parser_end_omp_structured_block (parser, save);
47151 tree body = finish_omp_structured_block (sb);
47152 if (ret == NULL)
47153 return ret;
47154 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47155 ret = make_node (OMP_TEAMS);
47156 TREE_TYPE (ret) = void_type_node;
47157 OMP_TEAMS_CLAUSES (ret) = clauses;
47158 OMP_TEAMS_BODY (ret) = body;
47159 OMP_TEAMS_COMBINED (ret) = 1;
47160 SET_EXPR_LOCATION (ret, loc);
47161 return add_stmt (ret);
47163 else if (strcmp (p, "loop") == 0)
47165 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
47166 if (cclauses == NULL)
47167 cclauses = cclauses_buf;
47169 cp_lexer_consume_token (parser->lexer);
47170 if (!flag_openmp) /* flag_openmp_simd */
47171 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
47172 cclauses, if_p);
47173 keep_next_level (true);
47174 sb = begin_omp_structured_block ();
47175 save = cp_parser_begin_omp_structured_block (parser);
47176 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
47177 cclauses, if_p);
47178 cp_parser_end_omp_structured_block (parser, save);
47179 tree body = finish_omp_structured_block (sb);
47180 if (ret == NULL)
47181 return ret;
47182 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47183 ret = make_node (OMP_TEAMS);
47184 TREE_TYPE (ret) = void_type_node;
47185 OMP_TEAMS_CLAUSES (ret) = clauses;
47186 OMP_TEAMS_BODY (ret) = body;
47187 OMP_TEAMS_COMBINED (ret) = 1;
47188 SET_EXPR_LOCATION (ret, loc);
47189 return add_stmt (ret);
47192 if (!flag_openmp) /* flag_openmp_simd */
47194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47195 return NULL_TREE;
47198 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
47199 cclauses == NULL);
47200 if (cclauses)
47202 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
47203 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47206 tree stmt = make_node (OMP_TEAMS);
47207 TREE_TYPE (stmt) = void_type_node;
47208 OMP_TEAMS_CLAUSES (stmt) = clauses;
47209 keep_next_level (true);
47210 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
47211 SET_EXPR_LOCATION (stmt, loc);
47213 return add_stmt (stmt);
47216 /* OpenMP 4.0:
47217 # pragma omp target data target-data-clause[optseq] new-line
47218 structured-block */
47220 #define OMP_TARGET_DATA_CLAUSE_MASK \
47221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
47223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
47225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
47227 static tree
47228 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47230 if (flag_openmp)
47231 omp_requires_mask
47232 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47234 tree clauses
47235 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
47236 "#pragma omp target data", pragma_tok);
47237 c_omp_adjust_map_clauses (clauses, false);
47238 int map_seen = 0;
47239 for (tree *pc = &clauses; *pc;)
47241 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
47242 switch (OMP_CLAUSE_MAP_KIND (*pc))
47244 case GOMP_MAP_TO:
47245 case GOMP_MAP_ALWAYS_TO:
47246 case GOMP_MAP_PRESENT_TO:
47247 case GOMP_MAP_ALWAYS_PRESENT_TO:
47248 case GOMP_MAP_FROM:
47249 case GOMP_MAP_ALWAYS_FROM:
47250 case GOMP_MAP_PRESENT_FROM:
47251 case GOMP_MAP_ALWAYS_PRESENT_FROM:
47252 case GOMP_MAP_TOFROM:
47253 case GOMP_MAP_ALWAYS_TOFROM:
47254 case GOMP_MAP_PRESENT_TOFROM:
47255 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
47256 case GOMP_MAP_ALLOC:
47257 case GOMP_MAP_PRESENT_ALLOC:
47258 map_seen = 3;
47259 break;
47260 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47261 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
47262 case GOMP_MAP_ALWAYS_POINTER:
47263 case GOMP_MAP_ATTACH_DETACH:
47264 case GOMP_MAP_ATTACH:
47265 break;
47266 default:
47267 map_seen |= 1;
47268 error_at (OMP_CLAUSE_LOCATION (*pc),
47269 "%<#pragma omp target data%> with map-type other "
47270 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
47271 "on %<map%> clause");
47272 *pc = OMP_CLAUSE_CHAIN (*pc);
47273 continue;
47275 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
47276 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
47277 map_seen = 3;
47278 pc = &OMP_CLAUSE_CHAIN (*pc);
47281 if (map_seen != 3)
47283 if (map_seen == 0)
47284 error_at (pragma_tok->location,
47285 "%<#pragma omp target data%> must contain at least "
47286 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
47287 "clause");
47288 return NULL_TREE;
47291 tree stmt = make_node (OMP_TARGET_DATA);
47292 TREE_TYPE (stmt) = void_type_node;
47293 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
47295 keep_next_level (true);
47296 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
47298 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47299 return add_stmt (stmt);
47302 /* OpenMP 4.5:
47303 # pragma omp target enter data target-enter-data-clause[optseq] new-line
47304 structured-block */
47306 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
47307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
47309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
47313 static bool
47314 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
47315 enum pragma_context context)
47317 bool data_seen = false;
47318 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47320 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47321 const char *p = IDENTIFIER_POINTER (id);
47323 if (strcmp (p, "data") == 0)
47325 cp_lexer_consume_token (parser->lexer);
47326 data_seen = true;
47329 if (!data_seen)
47331 cp_parser_error (parser, "expected %<data%>");
47332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47333 return false;
47336 if (context == pragma_stmt)
47338 error_at (pragma_tok->location,
47339 "%<#pragma %s%> may only be used in compound statements",
47340 "omp target enter data");
47341 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47342 return true;
47345 if (flag_openmp)
47346 omp_requires_mask
47347 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47349 tree clauses
47350 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
47351 "#pragma omp target enter data", pragma_tok);
47352 c_omp_adjust_map_clauses (clauses, false);
47353 int map_seen = 0;
47354 for (tree *pc = &clauses; *pc;)
47356 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
47357 switch (OMP_CLAUSE_MAP_KIND (*pc))
47359 case GOMP_MAP_TO:
47360 case GOMP_MAP_ALWAYS_TO:
47361 case GOMP_MAP_PRESENT_TO:
47362 case GOMP_MAP_ALWAYS_PRESENT_TO:
47363 case GOMP_MAP_ALLOC:
47364 case GOMP_MAP_PRESENT_ALLOC:
47365 map_seen = 3;
47366 break;
47367 case GOMP_MAP_TOFROM:
47368 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
47369 map_seen = 3;
47370 break;
47371 case GOMP_MAP_ALWAYS_TOFROM:
47372 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
47373 map_seen = 3;
47374 break;
47375 case GOMP_MAP_PRESENT_TOFROM:
47376 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
47377 map_seen = 3;
47378 break;
47379 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
47380 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
47381 map_seen = 3;
47382 break;
47383 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47384 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
47385 case GOMP_MAP_ALWAYS_POINTER:
47386 case GOMP_MAP_ATTACH_DETACH:
47387 case GOMP_MAP_ATTACH:
47388 break;
47389 default:
47390 map_seen |= 1;
47391 error_at (OMP_CLAUSE_LOCATION (*pc),
47392 "%<#pragma omp target enter data%> with map-type other "
47393 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
47394 *pc = OMP_CLAUSE_CHAIN (*pc);
47395 continue;
47397 pc = &OMP_CLAUSE_CHAIN (*pc);
47400 if (map_seen != 3)
47402 if (map_seen == 0)
47403 error_at (pragma_tok->location,
47404 "%<#pragma omp target enter data%> must contain at least "
47405 "one %<map%> clause");
47406 return true;
47409 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
47410 TREE_TYPE (stmt) = void_type_node;
47411 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
47412 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47413 add_stmt (stmt);
47414 return true;
47417 /* OpenMP 4.5:
47418 # pragma omp target exit data target-enter-data-clause[optseq] new-line
47419 structured-block */
47421 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
47422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
47424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
47428 static bool
47429 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
47430 enum pragma_context context)
47432 bool data_seen = false;
47433 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47435 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47436 const char *p = IDENTIFIER_POINTER (id);
47438 if (strcmp (p, "data") == 0)
47440 cp_lexer_consume_token (parser->lexer);
47441 data_seen = true;
47444 if (!data_seen)
47446 cp_parser_error (parser, "expected %<data%>");
47447 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47448 return false;
47451 if (context == pragma_stmt)
47453 error_at (pragma_tok->location,
47454 "%<#pragma %s%> may only be used in compound statements",
47455 "omp target exit data");
47456 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47457 return true;
47460 if (flag_openmp)
47461 omp_requires_mask
47462 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47464 tree clauses
47465 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
47466 "#pragma omp target exit data", pragma_tok,
47467 false);
47468 clauses = finish_omp_clauses (clauses, C_ORT_OMP_EXIT_DATA);
47469 c_omp_adjust_map_clauses (clauses, false);
47470 int map_seen = 0;
47471 for (tree *pc = &clauses; *pc;)
47473 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
47474 switch (OMP_CLAUSE_MAP_KIND (*pc))
47476 case GOMP_MAP_FROM:
47477 case GOMP_MAP_ALWAYS_FROM:
47478 case GOMP_MAP_PRESENT_FROM:
47479 case GOMP_MAP_ALWAYS_PRESENT_FROM:
47480 case GOMP_MAP_RELEASE:
47481 case GOMP_MAP_DELETE:
47482 map_seen = 3;
47483 break;
47484 case GOMP_MAP_TOFROM:
47485 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
47486 map_seen = 3;
47487 break;
47488 case GOMP_MAP_ALWAYS_TOFROM:
47489 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
47490 map_seen = 3;
47491 break;
47492 case GOMP_MAP_PRESENT_TOFROM:
47493 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
47494 map_seen = 3;
47495 break;
47496 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
47497 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
47498 map_seen = 3;
47499 break;
47500 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47501 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
47502 case GOMP_MAP_ALWAYS_POINTER:
47503 case GOMP_MAP_ATTACH_DETACH:
47504 case GOMP_MAP_DETACH:
47505 break;
47506 default:
47507 map_seen |= 1;
47508 error_at (OMP_CLAUSE_LOCATION (*pc),
47509 "%<#pragma omp target exit data%> with map-type other "
47510 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
47511 "on %<map%> clause");
47512 *pc = OMP_CLAUSE_CHAIN (*pc);
47513 continue;
47515 pc = &OMP_CLAUSE_CHAIN (*pc);
47518 if (map_seen != 3)
47520 if (map_seen == 0)
47521 error_at (pragma_tok->location,
47522 "%<#pragma omp target exit data%> must contain at least "
47523 "one %<map%> clause");
47524 return true;
47527 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
47528 TREE_TYPE (stmt) = void_type_node;
47529 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
47530 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47531 add_stmt (stmt);
47532 return true;
47535 /* OpenMP 4.0:
47536 # pragma omp target update target-update-clause[optseq] new-line */
47538 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
47539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
47540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
47541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
47546 static bool
47547 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
47548 enum pragma_context context)
47550 if (context == pragma_stmt)
47552 error_at (pragma_tok->location,
47553 "%<#pragma %s%> may only be used in compound statements",
47554 "omp target update");
47555 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47556 return true;
47559 tree clauses
47560 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
47561 "#pragma omp target update", pragma_tok);
47562 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
47563 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
47565 error_at (pragma_tok->location,
47566 "%<#pragma omp target update%> must contain at least one "
47567 "%<from%> or %<to%> clauses");
47568 return true;
47571 if (flag_openmp)
47572 omp_requires_mask
47573 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47575 tree stmt = make_node (OMP_TARGET_UPDATE);
47576 TREE_TYPE (stmt) = void_type_node;
47577 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
47578 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47579 add_stmt (stmt);
47580 return true;
47583 /* OpenMP 4.0:
47584 # pragma omp target target-clause[optseq] new-line
47585 structured-block */
47587 #define OMP_TARGET_CLAUSE_MASK \
47588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
47590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
47593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
47596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
47598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
47599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
47600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
47602 static bool
47603 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
47604 enum pragma_context context, bool *if_p)
47606 if (flag_openmp)
47607 omp_requires_mask
47608 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47612 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47613 const char *p = IDENTIFIER_POINTER (id);
47614 enum tree_code ccode = ERROR_MARK;
47616 if (strcmp (p, "teams") == 0)
47617 ccode = OMP_TEAMS;
47618 else if (strcmp (p, "parallel") == 0)
47619 ccode = OMP_PARALLEL;
47620 else if (strcmp (p, "simd") == 0)
47621 ccode = OMP_SIMD;
47622 if (ccode != ERROR_MARK)
47624 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
47625 char p_name[sizeof ("#pragma omp target teams distribute "
47626 "parallel for simd")];
47628 cp_lexer_consume_token (parser->lexer);
47629 strcpy (p_name, "#pragma omp target");
47630 if (!flag_openmp) /* flag_openmp_simd */
47632 tree stmt;
47633 switch (ccode)
47635 case OMP_TEAMS:
47636 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
47637 OMP_TARGET_CLAUSE_MASK,
47638 cclauses, if_p);
47639 break;
47640 case OMP_PARALLEL:
47641 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47642 OMP_TARGET_CLAUSE_MASK,
47643 cclauses, if_p);
47644 break;
47645 case OMP_SIMD:
47646 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
47647 OMP_TARGET_CLAUSE_MASK,
47648 cclauses, if_p);
47649 break;
47650 default:
47651 gcc_unreachable ();
47653 return stmt != NULL_TREE;
47655 keep_next_level (true);
47656 tree sb = begin_omp_structured_block (), ret;
47657 unsigned save = cp_parser_begin_omp_structured_block (parser);
47658 switch (ccode)
47660 case OMP_TEAMS:
47661 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
47662 OMP_TARGET_CLAUSE_MASK, cclauses,
47663 if_p);
47664 break;
47665 case OMP_PARALLEL:
47666 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47667 OMP_TARGET_CLAUSE_MASK, cclauses,
47668 if_p);
47669 break;
47670 case OMP_SIMD:
47671 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
47672 OMP_TARGET_CLAUSE_MASK, cclauses,
47673 if_p);
47674 break;
47675 default:
47676 gcc_unreachable ();
47678 cp_parser_end_omp_structured_block (parser, save);
47679 tree body = finish_omp_structured_block (sb);
47680 if (ret == NULL_TREE)
47681 return false;
47682 if (ccode == OMP_TEAMS && !processing_template_decl)
47683 /* For combined target teams, ensure the num_teams and
47684 thread_limit clause expressions are evaluated on the host,
47685 before entering the target construct. */
47686 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47687 c; c = OMP_CLAUSE_CHAIN (c))
47688 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
47689 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
47690 for (int i = 0;
47691 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
47692 if (OMP_CLAUSE_OPERAND (c, i)
47693 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
47695 tree expr = OMP_CLAUSE_OPERAND (c, i);
47696 expr = force_target_expr (TREE_TYPE (expr), expr,
47697 tf_none);
47698 if (expr == error_mark_node)
47699 continue;
47700 tree tmp = TARGET_EXPR_SLOT (expr);
47701 add_stmt (expr);
47702 OMP_CLAUSE_OPERAND (c, i) = expr;
47703 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
47704 OMP_CLAUSE_FIRSTPRIVATE);
47705 OMP_CLAUSE_DECL (tc) = tmp;
47706 OMP_CLAUSE_CHAIN (tc)
47707 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
47708 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
47710 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
47711 finish_omp_target (pragma_tok->location,
47712 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
47713 return true;
47715 else if (!flag_openmp) /* flag_openmp_simd */
47717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47718 return false;
47720 else if (strcmp (p, "data") == 0)
47722 cp_lexer_consume_token (parser->lexer);
47723 cp_parser_omp_target_data (parser, pragma_tok, if_p);
47724 return true;
47726 else if (strcmp (p, "enter") == 0)
47728 cp_lexer_consume_token (parser->lexer);
47729 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
47731 else if (strcmp (p, "exit") == 0)
47733 cp_lexer_consume_token (parser->lexer);
47734 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
47736 else if (strcmp (p, "update") == 0)
47738 cp_lexer_consume_token (parser->lexer);
47739 return cp_parser_omp_target_update (parser, pragma_tok, context);
47742 if (!flag_openmp) /* flag_openmp_simd */
47744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47745 return false;
47748 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
47749 "#pragma omp target", pragma_tok,
47750 false);
47751 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47752 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
47754 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
47755 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
47756 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
47757 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
47758 OMP_CLAUSE_CHAIN (c) = nc;
47760 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
47762 c_omp_adjust_map_clauses (clauses, true);
47763 keep_next_level (true);
47764 tree body = cp_parser_omp_structured_block (parser, if_p);
47766 finish_omp_target (pragma_tok->location, clauses, body, false);
47767 return true;
47771 /* OpenMP 5.1: Parse sizes list for "omp tile sizes"
47772 sizes ( size-expr-list ) */
47773 static tree
47774 cp_parser_omp_tile_sizes (cp_parser *parser, location_t loc)
47776 tree sizes = NULL_TREE;
47778 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47779 cp_lexer_consume_token (parser->lexer);
47781 cp_token *tok = cp_lexer_peek_token (parser->lexer);
47782 if (tok->type != CPP_NAME
47783 || strcmp ("sizes", IDENTIFIER_POINTER (tok->u.value)))
47785 cp_parser_error (parser, "expected %<sizes%>");
47786 return error_mark_node;
47788 cp_lexer_consume_token (parser->lexer);
47790 matching_parens parens;
47791 if (!parens.require_open (parser))
47792 return error_mark_node;
47796 if (sizes && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
47797 return error_mark_node;
47799 tree expr = cp_parser_constant_expression (parser);
47800 if (expr == error_mark_node)
47802 cp_parser_skip_to_closing_parenthesis (parser,
47803 /*recovering=*/true,
47804 /*or_comma=*/false,
47805 /*consume_paren=*/true);
47806 return error_mark_node;
47809 sizes = tree_cons (NULL_TREE, expr, sizes);
47811 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
47812 parens.require_close (parser);
47814 gcc_assert (sizes);
47815 tree c = build_omp_clause (loc, OMP_CLAUSE_SIZES);
47816 OMP_CLAUSE_SIZES_LIST (c) = nreverse (sizes);
47817 return c;
47820 /* OpenMP 5.1:
47821 #pragma omp tile sizes ( size-expr-list ) */
47823 static tree
47824 cp_parser_omp_tile (cp_parser *parser, cp_token *tok, bool *if_p)
47826 tree clauses = cp_parser_omp_tile_sizes (parser, tok->location);
47827 cp_parser_require_pragma_eol (parser, tok);
47829 if (!clauses || clauses == error_mark_node)
47830 return error_mark_node;
47832 tree block = begin_omp_structured_block ();
47833 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
47834 tree ret = cp_parser_omp_for_loop (parser, OMP_TILE, clauses, NULL, if_p);
47835 block = finish_omp_structured_block (block);
47836 add_stmt (block);
47838 return ret;
47841 #define OMP_UNROLL_CLAUSE_MASK \
47842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARTIAL) \
47843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FULL))
47845 /* OpenMP 5.1:
47846 #pragma omp unroll unroll-clause[optseq] */
47848 static tree
47849 cp_parser_omp_unroll (cp_parser *parser, cp_token *tok, bool *if_p)
47851 tree clauses = cp_parser_omp_all_clauses (parser, OMP_UNROLL_CLAUSE_MASK,
47852 "#pragma omp unroll", tok, true);
47854 tree block = begin_omp_structured_block ();
47855 tree ret = cp_parser_omp_for_loop (parser, OMP_UNROLL, clauses, NULL, if_p);
47856 block = finish_omp_structured_block (block);
47857 add_stmt (block);
47859 return ret;
47862 /* OpenACC 2.0:
47863 # pragma acc cache (variable-list) new-line
47865 OpenACC 2.7:
47866 # pragma acc cache (readonly: variable-list) new-line
47869 static tree
47870 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
47872 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
47873 clauses. */
47874 auto_suppress_location_wrappers sentinel;
47876 tree stmt, clauses = NULL_TREE;
47877 bool readonly = false;
47879 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
47881 cp_token *token = cp_lexer_peek_token (parser->lexer);
47882 if (token->type == CPP_NAME
47883 && !strcmp (IDENTIFIER_POINTER (token->u.value), "readonly")
47884 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
47886 cp_lexer_consume_token (parser->lexer);
47887 cp_lexer_consume_token (parser->lexer);
47888 readonly = true;
47890 clauses = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE__CACHE_,
47891 NULL, NULL);
47894 if (readonly)
47895 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47896 OMP_CLAUSE__CACHE__READONLY (c) = 1;
47898 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47900 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
47902 stmt = make_node (OACC_CACHE);
47903 TREE_TYPE (stmt) = void_type_node;
47904 OACC_CACHE_CLAUSES (stmt) = clauses;
47905 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47906 add_stmt (stmt);
47908 return stmt;
47911 /* OpenACC 2.0:
47912 # pragma acc data oacc-data-clause[optseq] new-line
47913 structured-block */
47915 #define OACC_DATA_CLAUSE_MASK \
47916 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47928 static tree
47929 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47931 tree stmt, clauses, block;
47932 unsigned int save;
47934 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
47935 "#pragma acc data", pragma_tok);
47937 block = begin_omp_parallel ();
47938 save = cp_parser_begin_omp_structured_block (parser);
47939 cp_parser_statement (parser, NULL_TREE, false, if_p);
47940 cp_parser_end_omp_structured_block (parser, save);
47941 stmt = finish_oacc_data (clauses, block);
47942 return stmt;
47945 /* OpenACC 2.0:
47946 # pragma acc host_data <clauses> new-line
47947 structured-block */
47949 #define OACC_HOST_DATA_CLAUSE_MASK \
47950 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
47951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
47954 static tree
47955 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47957 tree stmt, clauses, block;
47958 unsigned int save;
47960 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
47961 "#pragma acc host_data", pragma_tok,
47962 false);
47963 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
47965 error_at (pragma_tok->location,
47966 "%<host_data%> construct requires %<use_device%> clause");
47967 return error_mark_node;
47969 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47970 block = begin_omp_parallel ();
47971 save = cp_parser_begin_omp_structured_block (parser);
47972 cp_parser_statement (parser, NULL_TREE, false, if_p);
47973 cp_parser_end_omp_structured_block (parser, save);
47974 stmt = finish_oacc_host_data (clauses, block);
47975 return stmt;
47978 /* OpenACC 2.0:
47979 # pragma acc declare oacc-data-clause[optseq] new-line
47982 #define OACC_DECLARE_CLAUSE_MASK \
47983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
47989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
47990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47992 static tree
47993 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
47995 tree clauses, stmt;
47996 bool error = false;
47997 bool found_in_scope = global_bindings_p ();
47999 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
48000 "#pragma acc declare", pragma_tok);
48003 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
48005 error_at (pragma_tok->location,
48006 "no valid clauses specified in %<#pragma acc declare%>");
48007 return NULL_TREE;
48010 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
48012 location_t loc = OMP_CLAUSE_LOCATION (t);
48013 tree decl = OMP_CLAUSE_DECL (t);
48014 if (!DECL_P (decl))
48016 error_at (loc, "array section in %<#pragma acc declare%>");
48017 error = true;
48018 continue;
48020 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
48021 switch (OMP_CLAUSE_MAP_KIND (t))
48023 case GOMP_MAP_FIRSTPRIVATE_POINTER:
48024 case GOMP_MAP_ALLOC:
48025 case GOMP_MAP_TO:
48026 case GOMP_MAP_FORCE_DEVICEPTR:
48027 case GOMP_MAP_DEVICE_RESIDENT:
48028 break;
48030 case GOMP_MAP_LINK:
48031 if (!global_bindings_p ()
48032 && (TREE_STATIC (decl)
48033 || !DECL_EXTERNAL (decl)))
48035 error_at (loc,
48036 "%qD must be a global variable in "
48037 "%<#pragma acc declare link%>",
48038 decl);
48039 error = true;
48040 continue;
48042 break;
48044 default:
48045 if (global_bindings_p ())
48047 error_at (loc, "invalid OpenACC clause at file scope");
48048 error = true;
48049 continue;
48051 if (DECL_EXTERNAL (decl))
48053 error_at (loc,
48054 "invalid use of %<extern%> variable %qD "
48055 "in %<#pragma acc declare%>", decl);
48056 error = true;
48057 continue;
48059 else if (TREE_PUBLIC (decl))
48061 error_at (loc,
48062 "invalid use of %<global%> variable %qD "
48063 "in %<#pragma acc declare%>", decl);
48064 error = true;
48065 continue;
48067 break;
48070 if (!found_in_scope)
48071 /* This seems to ignore the existence of cleanup scopes?
48072 What is the meaning for local extern decls? The local
48073 extern is in this scope, but it is referring to a decl that
48074 is namespace scope. */
48075 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
48076 if (d == decl)
48078 found_in_scope = true;
48079 break;
48081 if (!found_in_scope)
48083 error_at (loc,
48084 "%qD must be a variable declared in the same scope as "
48085 "%<#pragma acc declare%>", decl);
48086 error = true;
48087 continue;
48090 if (!error)
48092 if (DECL_LOCAL_DECL_P (decl))
48093 /* We need to mark the aliased decl, as that is the entity
48094 that is being referred to. This won't work for
48095 dependent variables, but it didn't work for them before
48096 DECL_LOCAL_DECL_P was a thing either. But then
48097 dependent local extern variable decls are as rare as
48098 hen's teeth. */
48099 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
48100 if (alias != error_mark_node)
48101 decl = alias;
48103 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
48104 || lookup_attribute ("omp declare target link",
48105 DECL_ATTRIBUTES (decl)))
48107 error_at (loc, "variable %qD used more than once with "
48108 "%<#pragma acc declare%>", decl);
48109 error = true;
48110 continue;
48113 tree id;
48114 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
48115 id = get_identifier ("omp declare target link");
48116 else
48117 id = get_identifier ("omp declare target");
48119 DECL_ATTRIBUTES (decl)
48120 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
48121 if (current_binding_level->kind == sk_namespace)
48123 symtab_node *node = symtab_node::get (decl);
48124 if (node != NULL)
48126 node->offloadable = 1;
48127 if (ENABLE_OFFLOADING)
48129 g->have_offload = true;
48130 if (is_a <varpool_node *> (node))
48131 vec_safe_push (offload_vars, decl);
48138 if (error || current_binding_level->kind == sk_namespace)
48139 return NULL_TREE;
48141 stmt = make_node (OACC_DECLARE);
48142 TREE_TYPE (stmt) = void_type_node;
48143 OACC_DECLARE_CLAUSES (stmt) = clauses;
48144 SET_EXPR_LOCATION (stmt, pragma_tok->location);
48146 add_stmt (stmt);
48148 return NULL_TREE;
48151 /* OpenACC 2.0:
48152 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
48156 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
48158 LOC is the location of the #pragma token.
48161 #define OACC_ENTER_DATA_CLAUSE_MASK \
48162 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
48164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
48166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
48167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
48169 #define OACC_EXIT_DATA_CLAUSE_MASK \
48170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
48173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
48174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
48175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
48176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
48178 static tree
48179 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
48180 bool enter)
48182 location_t loc = pragma_tok->location;
48183 tree stmt, clauses;
48184 const char *p = "";
48186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48187 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48189 if (strcmp (p, "data") != 0)
48191 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
48192 enter ? "enter" : "exit");
48193 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48194 return NULL_TREE;
48197 cp_lexer_consume_token (parser->lexer);
48199 if (enter)
48200 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
48201 "#pragma acc enter data", pragma_tok);
48202 else
48203 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
48204 "#pragma acc exit data", pragma_tok);
48206 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
48208 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
48209 enter ? "enter" : "exit");
48210 return NULL_TREE;
48213 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
48214 TREE_TYPE (stmt) = void_type_node;
48215 OMP_STANDALONE_CLAUSES (stmt) = clauses;
48216 SET_EXPR_LOCATION (stmt, loc);
48217 add_stmt (stmt);
48218 return stmt;
48221 /* OpenACC 2.0:
48222 # pragma acc loop oacc-loop-clause[optseq] new-line
48223 structured-block */
48225 #define OACC_LOOP_CLAUSE_MASK \
48226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
48227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
48228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
48229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
48230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
48231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
48232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
48233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
48234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
48235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
48237 static tree
48238 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
48239 omp_clause_mask mask, tree *cclauses, bool *if_p)
48241 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
48243 strcat (p_name, " loop");
48244 mask |= OACC_LOOP_CLAUSE_MASK;
48246 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
48247 /*finish_p=*/cclauses == NULL,
48248 /*target=*/is_parallel);
48249 if (cclauses)
48251 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
48252 if (*cclauses)
48253 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC_TARGET);
48254 if (clauses)
48255 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
48258 tree block = begin_omp_structured_block ();
48259 int save = cp_parser_begin_omp_structured_block (parser);
48260 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
48261 cp_parser_end_omp_structured_block (parser, save);
48263 /* Later processing of combined acc loop constructs gets confused
48264 by an extra level of empty nested BIND_EXPRs, so flatten them. */
48265 block = finish_omp_structured_block (block);
48266 if (TREE_CODE (block) == BIND_EXPR
48267 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
48268 && !BIND_EXPR_VARS (block))
48269 block = BIND_EXPR_BODY (block);
48270 add_stmt (block);
48272 return stmt;
48275 /* OpenACC 2.0:
48276 # pragma acc kernels oacc-kernels-clause[optseq] new-line
48277 structured-block
48281 # pragma acc parallel oacc-parallel-clause[optseq] new-line
48282 structured-block
48284 OpenACC 2.6:
48286 # pragma acc serial oacc-serial-clause[optseq] new-line
48289 #define OACC_KERNELS_CLAUSE_MASK \
48290 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
48292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
48293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
48294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
48295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
48296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
48297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
48298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
48300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
48301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
48302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
48303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
48304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
48305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
48307 #define OACC_PARALLEL_CLAUSE_MASK \
48308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
48310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
48311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
48312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
48313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
48314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
48315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
48316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
48317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
48319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
48320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
48321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
48322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
48323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
48324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
48325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
48326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
48328 #define OACC_SERIAL_CLAUSE_MASK \
48329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
48331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
48332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
48333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
48334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
48335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
48336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
48337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
48339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
48340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
48341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
48342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
48343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
48344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
48346 static tree
48347 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
48348 char *p_name, bool *if_p)
48350 omp_clause_mask mask;
48351 enum tree_code code;
48352 switch (cp_parser_pragma_kind (pragma_tok))
48354 case PRAGMA_OACC_KERNELS:
48355 strcat (p_name, " kernels");
48356 mask = OACC_KERNELS_CLAUSE_MASK;
48357 code = OACC_KERNELS;
48358 break;
48359 case PRAGMA_OACC_PARALLEL:
48360 strcat (p_name, " parallel");
48361 mask = OACC_PARALLEL_CLAUSE_MASK;
48362 code = OACC_PARALLEL;
48363 break;
48364 case PRAGMA_OACC_SERIAL:
48365 strcat (p_name, " serial");
48366 mask = OACC_SERIAL_CLAUSE_MASK;
48367 code = OACC_SERIAL;
48368 break;
48369 default:
48370 gcc_unreachable ();
48373 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48375 const char *p
48376 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48377 if (strcmp (p, "loop") == 0)
48379 cp_lexer_consume_token (parser->lexer);
48380 tree block = begin_omp_parallel ();
48381 tree clauses;
48382 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
48383 &clauses, if_p);
48384 protected_set_expr_location (stmt, pragma_tok->location);
48385 return finish_omp_construct (code, block, clauses);
48389 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
48390 /*finish_p=*/true,
48391 /*target=*/true);
48393 tree block = begin_omp_parallel ();
48394 unsigned int save = cp_parser_begin_omp_structured_block (parser);
48395 cp_parser_statement (parser, NULL_TREE, false, if_p);
48396 cp_parser_end_omp_structured_block (parser, save);
48397 return finish_omp_construct (code, block, clauses);
48400 /* OpenACC 2.0:
48401 # pragma acc update oacc-update-clause[optseq] new-line
48404 #define OACC_UPDATE_CLAUSE_MASK \
48405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
48406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
48407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
48408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
48409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
48410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
48411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
48413 static tree
48414 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
48416 tree stmt, clauses;
48418 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
48419 "#pragma acc update", pragma_tok);
48421 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
48423 error_at (pragma_tok->location,
48424 "%<#pragma acc update%> must contain at least one "
48425 "%<device%> or %<host%> or %<self%> clause");
48426 return NULL_TREE;
48429 stmt = make_node (OACC_UPDATE);
48430 TREE_TYPE (stmt) = void_type_node;
48431 OACC_UPDATE_CLAUSES (stmt) = clauses;
48432 SET_EXPR_LOCATION (stmt, pragma_tok->location);
48433 add_stmt (stmt);
48434 return stmt;
48437 /* OpenACC 2.0:
48438 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
48440 LOC is the location of the #pragma token.
48443 #define OACC_WAIT_CLAUSE_MASK \
48444 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
48446 static tree
48447 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
48449 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
48450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48452 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
48453 list = cp_parser_oacc_wait_list (parser, loc, list);
48455 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
48456 "#pragma acc wait", pragma_tok);
48458 stmt = c_finish_oacc_wait (loc, list, clauses);
48459 stmt = finish_expr_stmt (stmt);
48461 return stmt;
48464 /* OpenMP 4.0:
48465 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
48467 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
48468 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
48469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
48470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
48471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
48472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
48473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
48475 static void
48476 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
48477 enum pragma_context context,
48478 bool variant_p)
48480 bool first_p = parser->omp_declare_simd == NULL;
48481 cp_omp_declare_simd_data data;
48482 if (first_p)
48484 data.error_seen = false;
48485 data.fndecl_seen = false;
48486 data.variant_p = variant_p;
48487 data.tokens = vNULL;
48488 data.attribs[0] = NULL;
48489 data.attribs[1] = NULL;
48490 data.loc = UNKNOWN_LOCATION;
48491 /* It is safe to take the address of a local variable; it will only be
48492 used while this scope is live. */
48493 parser->omp_declare_simd = &data;
48495 else if (parser->omp_declare_simd->variant_p != variant_p)
48497 error_at (pragma_tok->location,
48498 "%<#pragma omp declare %s%> followed by "
48499 "%<#pragma omp declare %s%>",
48500 parser->omp_declare_simd->variant_p ? "variant" : "simd",
48501 parser->omp_declare_simd->variant_p ? "simd" : "variant");
48502 parser->omp_declare_simd->error_seen = true;
48505 /* Store away all pragma tokens. */
48506 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48507 cp_lexer_consume_token (parser->lexer);
48508 cp_parser_require_pragma_eol (parser, pragma_tok);
48509 struct cp_token_cache *cp
48510 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
48511 parser->omp_declare_simd->tokens.safe_push (cp);
48513 if (first_p)
48515 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
48516 cp_parser_pragma (parser, context, NULL);
48517 switch (context)
48519 case pragma_external:
48520 cp_parser_declaration (parser, NULL_TREE);
48521 break;
48522 case pragma_member:
48523 cp_parser_member_declaration (parser);
48524 break;
48525 case pragma_objc_icode:
48526 cp_parser_block_declaration (parser, /*statement_p=*/false);
48527 break;
48528 default:
48529 cp_parser_declaration_statement (parser);
48530 break;
48532 if (parser->omp_declare_simd
48533 && !parser->omp_declare_simd->error_seen
48534 && !parser->omp_declare_simd->fndecl_seen)
48535 error_at (pragma_tok->location,
48536 "%<#pragma omp declare %s%> not immediately followed by "
48537 "function declaration or definition",
48538 parser->omp_declare_simd->variant_p ? "variant" : "simd");
48539 data.tokens.release ();
48540 parser->omp_declare_simd = NULL;
48544 /* OpenMP 5.0:
48546 trait-selector:
48547 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
48549 trait-score:
48550 score(score-expression)
48552 Note that this function returns a list of trait selectors for the
48553 trait-selector-set SET. */
48555 static tree
48556 cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
48557 bool has_parms_p)
48559 tree ret = NULL_TREE;
48562 tree selector;
48563 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
48564 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48565 selector = cp_lexer_peek_token (parser->lexer)->u.value;
48566 else
48568 cp_parser_error (parser, "expected trait selector name");
48569 return error_mark_node;
48572 enum omp_ts_code sel
48573 = omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
48575 if (sel == OMP_TRAIT_INVALID)
48577 /* Per the spec, "Implementations can ignore specified selectors
48578 that are not those described in this section"; however, we
48579 must record such selectors because they cause match failures. */
48580 warning_at (cp_lexer_peek_token (parser->lexer)->location,
48581 OPT_Wopenmp,
48582 "unknown selector %qs for context selector set %qs",
48583 IDENTIFIER_POINTER (selector), omp_tss_map[set]);
48584 cp_lexer_consume_token (parser->lexer);
48585 ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
48586 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48587 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48588 n; --n)
48589 cp_lexer_consume_token (parser->lexer);
48590 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48592 cp_lexer_consume_token (parser->lexer);
48593 continue;
48595 else
48596 break;
48599 cp_lexer_consume_token (parser->lexer);
48601 tree properties = NULL_TREE;
48602 tree scoreval = NULL_TREE;
48603 enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
48604 bool allow_score = omp_ts_map[sel].allow_score;
48605 tree t;
48607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48609 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
48611 error ("selector %qs does not accept any properties",
48612 IDENTIFIER_POINTER (selector));
48613 return error_mark_node;
48616 matching_parens parens;
48617 parens.consume_open (parser);
48619 cp_token *token = cp_lexer_peek_token (parser->lexer);
48620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48621 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
48622 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
48624 cp_lexer_save_tokens (parser->lexer);
48625 cp_lexer_consume_token (parser->lexer);
48626 cp_lexer_consume_token (parser->lexer);
48627 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
48628 true)
48629 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
48631 cp_lexer_rollback_tokens (parser->lexer);
48632 cp_lexer_consume_token (parser->lexer);
48634 matching_parens parens2;
48635 parens2.require_open (parser);
48636 tree score = cp_parser_constant_expression (parser);
48637 if (!parens2.require_close (parser))
48638 cp_parser_skip_to_closing_parenthesis (parser, true,
48639 false, true);
48640 cp_parser_require (parser, CPP_COLON, RT_COLON);
48641 if (!allow_score)
48642 error_at (token->location,
48643 "%<score%> cannot be specified in traits "
48644 "in the %qs trait-selector-set",
48645 omp_tss_map[set]);
48646 else if (score != error_mark_node)
48648 score = fold_non_dependent_expr (score);
48649 if (value_dependent_expression_p (score))
48650 scoreval = score;
48651 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
48652 || TREE_CODE (score) != INTEGER_CST)
48653 error_at (token->location, "%<score%> argument must "
48654 "be constant integer expression");
48655 else if (tree_int_cst_sgn (score) < 0)
48656 error_at (token->location, "%<score%> argument must "
48657 "be non-negative");
48658 else
48659 scoreval = score;
48662 else
48663 cp_lexer_rollback_tokens (parser->lexer);
48665 token = cp_lexer_peek_token (parser->lexer);
48668 switch (property_kind)
48670 case OMP_TRAIT_PROPERTY_ID:
48671 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
48672 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48674 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
48675 cp_lexer_consume_token (parser->lexer);
48676 properties = make_trait_property (prop, NULL_TREE,
48677 properties);
48679 else
48681 cp_parser_error (parser, "expected identifier");
48682 return error_mark_node;
48684 break;
48685 case OMP_TRAIT_PROPERTY_NAME_LIST:
48688 tree prop = OMP_TP_NAMELIST_NODE;
48689 tree value = NULL_TREE;
48690 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
48691 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48693 value = cp_lexer_peek_token (parser->lexer)->u.value;
48694 cp_lexer_consume_token (parser->lexer);
48696 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
48697 value = cp_parser_string_literal (parser,
48698 /*translate=*/false,
48699 /*wide_ok=*/false);
48700 else
48702 cp_parser_error (parser, "expected identifier or "
48703 "string literal");
48704 return error_mark_node;
48707 properties = make_trait_property (prop, value, properties);
48709 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48710 cp_lexer_consume_token (parser->lexer);
48711 else
48712 break;
48714 while (1);
48715 break;
48716 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
48717 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
48718 /* FIXME: this is bogus, the expression need
48719 not be constant. */
48720 t = cp_parser_constant_expression (parser);
48721 if (t != error_mark_node)
48723 t = fold_non_dependent_expr (t);
48724 if (!value_dependent_expression_p (t)
48725 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
48726 || !tree_fits_shwi_p (t)))
48727 error_at (token->location, "property must be "
48728 "constant integer expression");
48729 else
48730 properties = make_trait_property (NULL_TREE, t,
48731 properties);
48733 else
48734 return error_mark_node;
48735 break;
48736 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
48737 if (sel == OMP_TRAIT_CONSTRUCT_SIMD)
48739 if (!has_parms_p)
48741 error_at (token->location, "properties for %<simd%> "
48742 "selector may not be specified in "
48743 "%<metadirective%>");
48744 return error_mark_node;
48746 properties
48747 = cp_parser_omp_all_clauses (parser,
48748 OMP_DECLARE_SIMD_CLAUSE_MASK,
48749 "simd", NULL, true, 2);
48751 else if (sel == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
48753 /* FIXME: The "requires" selector was added in OpenMP 5.1.
48754 Currently only the now-deprecated syntax
48755 from OpenMP 5.0 is supported. */
48756 sorry_at (token->location,
48757 "%<requires%> selector is not supported yet");
48758 return error_mark_node;
48760 else
48761 gcc_unreachable ();
48762 break;
48763 default:
48764 gcc_unreachable ();
48767 if (!parens.require_close (parser))
48768 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
48770 properties = nreverse (properties);
48772 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
48773 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
48774 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
48776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
48777 return error_mark_node;
48780 ret = make_trait_selector (sel, scoreval, properties, ret);
48782 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48783 cp_lexer_consume_token (parser->lexer);
48784 else
48785 break;
48787 while (1);
48789 return nreverse (ret);
48792 /* OpenMP 5.0:
48794 trait-set-selector[,trait-set-selector[,...]]
48796 trait-set-selector:
48797 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
48799 trait-set-selector-name:
48800 constructor
48801 device
48802 implementation
48803 user */
48805 static tree
48806 cp_parser_omp_context_selector_specification (cp_parser *parser,
48807 bool has_parms_p)
48809 tree ret = NULL_TREE;
48812 const char *setp = "";
48813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48814 setp
48815 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48816 enum omp_tss_code set = omp_lookup_tss_code (setp);
48818 if (set == OMP_TRAIT_SET_INVALID)
48820 cp_parser_error (parser, "expected context selector set name");
48821 return error_mark_node;
48824 cp_lexer_consume_token (parser->lexer);
48826 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
48827 return error_mark_node;
48829 matching_braces braces;
48830 if (!braces.require_open (parser))
48831 return error_mark_node;
48833 tree selectors
48834 = cp_parser_omp_context_selector (parser, set, has_parms_p);
48835 if (selectors == error_mark_node)
48837 cp_parser_skip_to_closing_brace (parser);
48838 ret = error_mark_node;
48840 else if (ret != error_mark_node)
48841 ret = make_trait_set_selector (set, selectors, ret);
48843 braces.require_close (parser);
48845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48846 cp_lexer_consume_token (parser->lexer);
48847 else
48848 break;
48850 while (1);
48852 if (ret == error_mark_node)
48853 return ret;
48854 return nreverse (ret);
48857 /* Assumption clauses:
48858 OpenMP 5.1
48859 absent (directive-name-list)
48860 contains (directive-name-list)
48861 holds (expression)
48862 no_openmp
48863 no_openmp_routines
48864 no_parallelism */
48866 static void
48867 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
48868 bool is_assume)
48870 bool no_openmp = false;
48871 bool no_openmp_routines = false;
48872 bool no_parallelism = false;
48873 bitmap_head absent_head, contains_head;
48875 bitmap_obstack_initialize (NULL);
48876 bitmap_initialize (&absent_head, &bitmap_default_obstack);
48877 bitmap_initialize (&contains_head, &bitmap_default_obstack);
48879 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48880 error_at (cp_lexer_peek_token (parser->lexer)->location,
48881 "expected at least one assumption clause");
48883 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48886 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48887 cp_lexer_consume_token (parser->lexer);
48889 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48890 break;
48892 const char *p
48893 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48894 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48896 if (!strcmp (p, "no_openmp"))
48898 cp_lexer_consume_token (parser->lexer);
48899 if (no_openmp)
48900 error_at (cloc, "too many %qs clauses", "no_openmp");
48901 no_openmp = true;
48903 else if (!strcmp (p, "no_openmp_routines"))
48905 cp_lexer_consume_token (parser->lexer);
48906 if (no_openmp_routines)
48907 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
48908 no_openmp_routines = true;
48910 else if (!strcmp (p, "no_parallelism"))
48912 cp_lexer_consume_token (parser->lexer);
48913 if (no_parallelism)
48914 error_at (cloc, "too many %qs clauses", "no_parallelism");
48915 no_parallelism = true;
48917 else if (!strcmp (p, "holds"))
48919 cp_lexer_consume_token (parser->lexer);
48920 matching_parens parens;
48921 if (parens.require_open (parser))
48923 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
48924 tree t = cp_parser_assignment_expression (parser);
48925 if (!type_dependent_expression_p (t))
48926 t = contextual_conv_bool (t, tf_warning_or_error);
48927 if (is_assume && !error_operand_p (t))
48928 finish_expr_stmt (build_assume_call (eloc, t));
48929 if (!parens.require_close (parser))
48930 cp_parser_skip_to_closing_parenthesis (parser,
48931 /*recovering=*/true,
48932 /*or_comma=*/false,
48933 /*consume_paren=*/true);
48936 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
48938 cp_lexer_consume_token (parser->lexer);
48939 matching_parens parens;
48940 if (parens.require_open (parser))
48944 const char *directive[3] = {};
48945 int i;
48946 location_t dloc
48947 = cp_lexer_peek_token (parser->lexer)->location;
48948 for (i = 0; i < 3; i++)
48950 tree id;
48951 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
48952 id = cp_lexer_peek_nth_token (parser->lexer,
48953 i + 1)->u.value;
48954 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
48955 CPP_KEYWORD))
48957 enum rid rid
48958 = cp_lexer_peek_nth_token (parser->lexer,
48959 i + 1)->keyword;
48960 id = ridpointers[rid];
48962 else
48963 break;
48964 directive[i] = IDENTIFIER_POINTER (id);
48966 if (i == 0)
48967 error_at (dloc, "expected directive name");
48968 else
48970 const struct c_omp_directive *dir
48971 = c_omp_categorize_directive (directive[0],
48972 directive[1],
48973 directive[2]);
48974 if (dir == NULL
48975 || dir->kind == C_OMP_DIR_DECLARATIVE
48976 || dir->kind == C_OMP_DIR_INFORMATIONAL
48977 || dir->id == PRAGMA_OMP_END
48978 || (!dir->second && directive[1])
48979 || (!dir->third && directive[2]))
48980 error_at (dloc, "unknown OpenMP directive name in "
48981 "%qs clause argument", p);
48982 else
48984 int id = dir - c_omp_directives;
48985 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
48986 : &absent_head, id))
48987 error_at (dloc, "%<%s%s%s%s%s%> directive "
48988 "mentioned in both %<absent%> and "
48989 "%<contains%> clauses",
48990 directive[0],
48991 directive[1] ? " " : "",
48992 directive[1] ? directive[1] : "",
48993 directive[2] ? " " : "",
48994 directive[2] ? directive[2] : "");
48995 else if (!bitmap_set_bit (p[0] == 'a'
48996 ? &absent_head
48997 : &contains_head, id))
48998 error_at (dloc, "%<%s%s%s%s%s%> directive "
48999 "mentioned multiple times in %qs "
49000 "clauses",
49001 directive[0],
49002 directive[1] ? " " : "",
49003 directive[1] ? directive[1] : "",
49004 directive[2] ? " " : "",
49005 directive[2] ? directive[2] : "", p);
49007 for (; i; --i)
49008 cp_lexer_consume_token (parser->lexer);
49010 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
49011 cp_lexer_consume_token (parser->lexer);
49012 else
49013 break;
49015 while (1);
49016 if (!parens.require_close (parser))
49017 cp_parser_skip_to_closing_parenthesis (parser,
49018 /*recovering=*/true,
49019 /*or_comma=*/false,
49020 /*consume_paren=*/true);
49023 else if (startswith (p, "ext_"))
49025 warning_at (cloc, OPT_Wopenmp, "unknown assumption clause %qs", p);
49026 cp_lexer_consume_token (parser->lexer);
49027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49028 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
49029 n; --n)
49030 cp_lexer_consume_token (parser->lexer);
49032 else
49034 cp_lexer_consume_token (parser->lexer);
49035 error_at (cloc, "expected assumption clause");
49036 break;
49039 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49042 /* OpenMP 5.1
49043 # pragma omp assume clauses[optseq] new-line */
49045 static void
49046 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
49048 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
49049 add_stmt (cp_parser_omp_structured_block (parser, if_p));
49052 /* OpenMP 5.1
49053 # pragma omp assumes clauses[optseq] new-line */
49055 static bool
49056 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
49058 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
49059 return false;
49062 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
49063 that into "omp declare variant base" attribute. */
49065 static tree
49066 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
49067 tree attrs)
49069 matching_parens parens;
49070 if (!parens.require_open (parser))
49072 fail:
49073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49074 return attrs;
49077 bool template_p;
49078 cp_id_kind idk = CP_ID_KIND_NONE;
49079 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
49080 cp_expr varid
49081 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
49082 /*check_dependency_p=*/true,
49083 /*template_p=*/&template_p,
49084 /*declarator_p=*/false,
49085 /*optional_p=*/false);
49086 parens.require_close (parser);
49088 tree variant;
49089 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
49090 || TREE_CODE (varid) == TYPE_DECL
49091 || varid == error_mark_node)
49092 variant = varid;
49093 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
49094 variant = NULL_TREE;
49095 else
49097 tree ambiguous_decls;
49098 variant = cp_parser_lookup_name (parser, varid, none_type,
49099 template_p, /*is_namespace=*/false,
49100 /*check_dependency=*/true,
49101 &ambiguous_decls,
49102 varid.get_location ());
49103 if (ambiguous_decls)
49104 variant = NULL_TREE;
49106 if (variant == NULL_TREE)
49107 variant = error_mark_node;
49108 else if (TREE_CODE (variant) != SCOPE_REF)
49110 const char *error_msg;
49111 variant
49112 = finish_id_expression (varid, variant, parser->scope,
49113 &idk, false, true,
49114 &parser->non_integral_constant_expression_p,
49115 template_p, true, false, false, &error_msg,
49116 varid.get_location ());
49117 if (error_msg)
49118 cp_parser_error (parser, error_msg);
49120 location_t caret_loc = get_pure_location (varid.get_location ());
49121 location_t start_loc = get_start (varid_token->location);
49122 location_t finish_loc = get_finish (varid.get_location ());
49123 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
49125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49126 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49127 cp_lexer_consume_token (parser->lexer);
49129 const char *clause = "";
49130 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
49131 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49132 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49133 if (strcmp (clause, "match"))
49135 cp_parser_error (parser, "expected %<match%>");
49136 goto fail;
49139 cp_lexer_consume_token (parser->lexer);
49141 if (!parens.require_open (parser))
49142 goto fail;
49144 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
49145 if (ctx == error_mark_node)
49146 goto fail;
49147 ctx = omp_check_context_selector (match_loc, ctx);
49148 if (ctx != error_mark_node && variant != error_mark_node)
49150 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
49151 match_loc);
49152 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
49153 loc_node = tree_cons (match_loc_node,
49154 build_int_cst (integer_type_node, idk),
49155 build_tree_list (loc_node, integer_zero_node));
49156 attrs = tree_cons (get_identifier ("omp declare variant base"),
49157 tree_cons (variant, ctx, loc_node), attrs);
49158 if (processing_template_decl)
49159 ATTR_IS_DEPENDENT (attrs) = 1;
49162 parens.require_close (parser);
49163 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49164 return attrs;
49168 /* Finalize #pragma omp declare simd clauses after direct declarator has
49169 been parsed, and put that into "omp declare simd" attribute. */
49171 static tree
49172 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
49174 struct cp_token_cache *ce;
49175 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
49176 int i;
49178 if (!data->error_seen && data->fndecl_seen)
49180 error ("%<#pragma omp declare %s%> not immediately followed by "
49181 "a single function declaration or definition",
49182 data->variant_p ? "variant" : "simd");
49183 data->error_seen = true;
49185 if (data->error_seen)
49186 return attrs;
49188 FOR_EACH_VEC_ELT (data->tokens, i, ce)
49190 tree c, cl;
49192 cp_parser_push_lexer_for_tokens (parser, ce);
49193 parser->lexer->in_pragma = true;
49194 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
49195 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
49196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49197 const char *kind = IDENTIFIER_POINTER (id);
49198 cp_lexer_consume_token (parser->lexer);
49199 if (strcmp (kind, "simd") == 0)
49201 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
49202 "#pragma omp declare simd",
49203 pragma_tok);
49204 if (cl)
49205 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
49206 c = build_tree_list (get_identifier ("omp declare simd"), cl);
49207 TREE_CHAIN (c) = attrs;
49208 if (processing_template_decl)
49209 ATTR_IS_DEPENDENT (c) = 1;
49210 attrs = c;
49212 else
49214 gcc_assert (strcmp (kind, "variant") == 0);
49215 attrs
49216 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
49218 cp_parser_pop_lexer (parser);
49221 cp_lexer *lexer = NULL;
49222 for (int i = 0; i < 2; i++)
49224 if (data->attribs[i] == NULL)
49225 continue;
49226 for (tree *pa = data->attribs[i]; *pa; )
49227 if (get_attribute_namespace (*pa) == omp_identifier
49228 && is_attribute_p ("directive", get_attribute_name (*pa)))
49230 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
49232 tree d = TREE_VALUE (a);
49233 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
49234 cp_token *first = DEFPARSE_TOKENS (d)->first;
49235 cp_token *last = DEFPARSE_TOKENS (d)->last;
49236 const char *directive[3] = {};
49237 for (int j = 0; j < 3; j++)
49239 tree id = NULL_TREE;
49240 if (first + j == last)
49241 break;
49242 if (first[j].type == CPP_NAME)
49243 id = first[j].u.value;
49244 else if (first[j].type == CPP_KEYWORD)
49245 id = ridpointers[(int) first[j].keyword];
49246 else
49247 break;
49248 directive[j] = IDENTIFIER_POINTER (id);
49250 const c_omp_directive *dir = NULL;
49251 if (directive[0])
49252 dir = c_omp_categorize_directive (directive[0], directive[1],
49253 directive[2]);
49254 if (dir == NULL)
49256 error_at (first->location,
49257 "unknown OpenMP directive name in "
49258 "%qs attribute argument",
49259 TREE_PUBLIC (d)
49260 ? "omp::decl" : "omp::directive");
49261 continue;
49263 if (dir->id != PRAGMA_OMP_DECLARE
49264 || (strcmp (directive[1], "simd") != 0
49265 && strcmp (directive[1], "variant") != 0))
49267 error_at (first->location,
49268 "OpenMP directive other than %<declare simd%> "
49269 "or %<declare variant%> appertains to a "
49270 "declaration");
49271 continue;
49274 if (parser->omp_attrs_forbidden_p)
49276 error_at (first->location,
49277 "mixing OpenMP directives with attribute and "
49278 "pragma syntax on the same statement");
49279 parser->omp_attrs_forbidden_p = false;
49282 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
49283 continue;
49284 if (lexer == NULL)
49286 lexer = cp_lexer_alloc ();
49287 lexer->debugging_p = parser->lexer->debugging_p;
49289 vec_safe_reserve (lexer->buffer, (last - first) + 2);
49290 cp_token tok = {};
49291 tok.type = CPP_PRAGMA;
49292 tok.keyword = RID_MAX;
49293 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
49294 tok.location = first->location;
49295 lexer->buffer->quick_push (tok);
49296 while (++first < last)
49297 lexer->buffer->quick_push (*first);
49298 tok = {};
49299 tok.type = CPP_PRAGMA_EOL;
49300 tok.keyword = RID_MAX;
49301 tok.location = last->location;
49302 lexer->buffer->quick_push (tok);
49303 tok = {};
49304 tok.type = CPP_EOF;
49305 tok.keyword = RID_MAX;
49306 tok.location = last->location;
49307 lexer->buffer->quick_push (tok);
49308 lexer->next = parser->lexer;
49309 lexer->next_token = lexer->buffer->address ();
49310 lexer->last_token = lexer->next_token
49311 + lexer->buffer->length ()
49312 - 1;
49313 lexer->in_omp_attribute_pragma = true;
49314 parser->lexer = lexer;
49315 /* Move the current source position to that of the first token
49316 in the new lexer. */
49317 cp_lexer_set_source_position_from_token (lexer->next_token);
49319 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
49320 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49321 const char *kind = IDENTIFIER_POINTER (id);
49322 cp_lexer_consume_token (parser->lexer);
49324 tree c, cl;
49325 if (strcmp (kind, "simd") == 0)
49327 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49328 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49329 cp_lexer_consume_token (parser->lexer);
49331 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
49332 cl = cp_parser_omp_all_clauses (parser, mask,
49333 "#pragma omp declare simd",
49334 pragma_tok);
49335 if (cl)
49336 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
49337 c = build_tree_list (get_identifier ("omp declare simd"),
49338 cl);
49339 TREE_CHAIN (c) = attrs;
49340 if (processing_template_decl)
49341 ATTR_IS_DEPENDENT (c) = 1;
49342 attrs = c;
49344 else
49346 gcc_assert (strcmp (kind, "variant") == 0);
49347 attrs
49348 = cp_finish_omp_declare_variant (parser, pragma_tok,
49349 attrs);
49351 gcc_assert (parser->lexer != lexer);
49352 vec_safe_truncate (lexer->buffer, 0);
49354 *pa = TREE_CHAIN (*pa);
49356 else
49357 pa = &TREE_CHAIN (*pa);
49359 if (lexer)
49360 cp_lexer_destroy (lexer);
49362 data->fndecl_seen = true;
49363 return attrs;
49366 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
49367 a threadprivate, groupprivate, allocate or declare target directive,
49368 return true and parse it for DECL. */
49370 bool
49371 cp_maybe_parse_omp_decl (tree decl, tree d)
49373 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
49374 cp_token *first = DEFPARSE_TOKENS (d)->first;
49375 cp_token *last = DEFPARSE_TOKENS (d)->last;
49376 const char *directive[3] = {};
49377 for (int j = 0; j < 3; j++)
49379 tree id = NULL_TREE;
49380 if (first + j == last)
49381 break;
49382 if (first[j].type == CPP_NAME)
49383 id = first[j].u.value;
49384 else if (first[j].type == CPP_KEYWORD)
49385 id = ridpointers[(int) first[j].keyword];
49386 else
49387 break;
49388 directive[j] = IDENTIFIER_POINTER (id);
49390 const c_omp_directive *dir = NULL;
49391 if (directive[0])
49392 dir = c_omp_categorize_directive (directive[0], directive[1],
49393 directive[2]);
49394 if (dir == NULL)
49396 error_at (first->location,
49397 "unknown OpenMP directive name in "
49398 "%qs attribute argument", "omp::decl");
49399 return false;
49401 if (dir->id != PRAGMA_OMP_THREADPRIVATE
49402 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
49403 && dir->id != PRAGMA_OMP_ALLOCATE
49404 && (dir->id != PRAGMA_OMP_DECLARE
49405 || strcmp (directive[1], "target") != 0))
49406 return false;
49408 if (!flag_openmp && !dir->simd)
49409 return true;
49411 cp_parser *parser = the_parser;
49412 cp_lexer *lexer = cp_lexer_alloc ();
49413 lexer->debugging_p = parser->lexer->debugging_p;
49414 lexer->in_omp_decl_attribute = decl;
49415 vec_safe_reserve (lexer->buffer, last - first + 3, true);
49416 cp_token tok = {};
49417 tok.type = CPP_PRAGMA;
49418 tok.keyword = RID_MAX;
49419 tok.u.value = build_int_cst (NULL, dir->id);
49420 tok.location = first->location;
49421 lexer->buffer->quick_push (tok);
49422 while (++first < last)
49423 lexer->buffer->quick_push (*first);
49424 tok = {};
49425 tok.type = CPP_PRAGMA_EOL;
49426 tok.keyword = RID_MAX;
49427 tok.location = last->location;
49428 lexer->buffer->quick_push (tok);
49429 tok = {};
49430 tok.type = CPP_EOF;
49431 tok.keyword = RID_MAX;
49432 tok.location = last->location;
49433 lexer->buffer->quick_push (tok);
49434 lexer->next = parser->lexer;
49435 lexer->next_token = lexer->buffer->address ();
49436 lexer->last_token = lexer->next_token
49437 + lexer->buffer->length ()
49438 - 1;
49439 lexer->in_omp_attribute_pragma = true;
49440 parser->lexer = lexer;
49441 /* Move the current source position to that of the first token in the
49442 new lexer. */
49443 cp_lexer_set_source_position_from_token (lexer->next_token);
49444 cp_parser_pragma (parser, pragma_external, NULL);
49446 return true;
49449 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
49450 on #pragma omp declare target. Return false if errors were reported. */
49452 static bool
49453 handle_omp_declare_target_clause (tree c, tree t, int device_type,
49454 bool indirect)
49456 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
49457 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
49458 tree id;
49459 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
49461 id = get_identifier ("omp declare target link");
49462 std::swap (at1, at2);
49464 else
49465 id = get_identifier ("omp declare target");
49466 if (at2)
49468 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
49469 error_at (OMP_CLAUSE_LOCATION (c),
49470 "%qD specified both in declare target %<link%> and %qs"
49471 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
49472 else
49473 error_at (OMP_CLAUSE_LOCATION (c),
49474 "%qD specified both in declare target %<link%> and "
49475 "%<to%> or %<enter%> clauses", t);
49476 return false;
49478 if (!at1)
49480 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
49481 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
49482 return true;
49484 symtab_node *node = symtab_node::get (t);
49485 if (node != NULL)
49487 node->offloadable = 1;
49488 if (ENABLE_OFFLOADING)
49490 g->have_offload = true;
49491 if (is_a <varpool_node *> (node))
49492 vec_safe_push (offload_vars, t);
49496 if (TREE_CODE (t) != FUNCTION_DECL)
49497 return true;
49498 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
49500 tree at3 = lookup_attribute ("omp declare target host",
49501 DECL_ATTRIBUTES (t));
49502 if (at3 == NULL_TREE)
49504 id = get_identifier ("omp declare target host");
49505 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
49508 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
49510 tree at3 = lookup_attribute ("omp declare target nohost",
49511 DECL_ATTRIBUTES (t));
49512 if (at3 == NULL_TREE)
49514 id = get_identifier ("omp declare target nohost");
49515 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
49518 if (indirect)
49520 tree at4 = lookup_attribute ("omp declare target indirect",
49521 DECL_ATTRIBUTES (t));
49522 if (at4 == NULL_TREE)
49524 id = get_identifier ("omp declare target indirect");
49525 DECL_ATTRIBUTES (t)
49526 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
49529 return true;
49532 /* OpenMP 4.0:
49533 # pragma omp declare target new-line
49534 declarations and definitions
49535 # pragma omp end declare target new-line
49537 OpenMP 4.5:
49538 # pragma omp declare target ( extended-list ) new-line
49540 # pragma omp declare target declare-target-clauses[seq] new-line */
49542 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
49543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
49544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
49545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
49546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
49547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
49549 static void
49550 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
49552 tree clauses = NULL_TREE;
49553 int device_type = 0;
49554 bool indirect = false;
49555 bool only_device_type_or_indirect = true;
49556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
49557 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49558 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
49559 clauses
49560 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
49561 "#pragma omp declare target", pragma_tok);
49562 else if (parser->lexer->in_omp_decl_attribute
49563 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49565 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
49566 clauses);
49567 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
49568 cp_parser_require_pragma_eol (parser, pragma_tok);
49570 else
49572 cp_omp_declare_target_attr a
49573 = { parser->lexer->in_omp_attribute_pragma, -1, false };
49574 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
49575 cp_parser_require_pragma_eol (parser, pragma_tok);
49576 return;
49578 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49580 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
49581 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
49582 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49583 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
49585 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49587 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE
49588 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49589 continue;
49590 tree t = OMP_CLAUSE_DECL (c);
49591 only_device_type_or_indirect = false;
49592 if (!handle_omp_declare_target_clause (c, t, device_type, indirect))
49593 continue;
49594 if (VAR_OR_FUNCTION_DECL_P (t)
49595 && DECL_LOCAL_DECL_P (t)
49596 && DECL_LANG_SPECIFIC (t)
49597 && DECL_LOCAL_DECL_ALIAS (t)
49598 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
49599 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
49600 device_type, indirect);
49602 if ((device_type || indirect) && only_device_type_or_indirect)
49603 error_at (OMP_CLAUSE_LOCATION (clauses),
49604 "directive with only %<device_type%> or %<indirect%> clauses");
49605 if (indirect && device_type && device_type != OMP_CLAUSE_DEVICE_TYPE_ANY)
49606 error_at (OMP_CLAUSE_LOCATION (clauses),
49607 "%<device_type%> clause must specify 'any' when used with "
49608 "an %<indirect%> clause");
49611 /* OpenMP 5.1
49612 # pragma omp begin assumes clauses[optseq] new-line
49614 # pragma omp begin declare target clauses[optseq] new-line */
49616 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
49617 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
49618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
49620 static void
49621 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
49623 const char *p = "";
49624 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
49625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49628 p = IDENTIFIER_POINTER (id);
49630 if (strcmp (p, "declare") == 0)
49632 cp_lexer_consume_token (parser->lexer);
49633 p = "";
49634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49637 p = IDENTIFIER_POINTER (id);
49639 if (strcmp (p, "target") == 0)
49641 cp_lexer_consume_token (parser->lexer);
49642 tree clauses
49643 = cp_parser_omp_all_clauses (parser,
49644 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
49645 "#pragma omp begin declare target",
49646 pragma_tok);
49647 int device_type = 0;
49648 bool indirect = 0;
49649 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
49652 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
49653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49654 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
49656 cp_omp_declare_target_attr a
49657 = { in_omp_attribute_pragma, device_type, indirect };
49658 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
49660 else
49662 cp_parser_error (parser, "expected %<target%>");
49663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49666 else if (strcmp (p, "assumes") == 0)
49668 cp_lexer_consume_token (parser->lexer);
49669 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
49670 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
49671 vec_safe_push (scope_chain->omp_begin_assumes, a);
49673 else
49675 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
49676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49680 /* OpenMP 4.0:
49681 # pragma omp end declare target new-line
49683 OpenMP 5.1:
49684 # pragma omp end assumes new-line */
49686 static void
49687 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
49689 const char *p = "";
49690 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
49691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49694 p = IDENTIFIER_POINTER (id);
49696 if (strcmp (p, "declare") == 0)
49698 cp_lexer_consume_token (parser->lexer);
49699 p = "";
49700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49702 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49703 p = IDENTIFIER_POINTER (id);
49705 if (strcmp (p, "target") == 0)
49706 cp_lexer_consume_token (parser->lexer);
49707 else
49709 cp_parser_error (parser, "expected %<target%>");
49710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49711 return;
49713 cp_parser_require_pragma_eol (parser, pragma_tok);
49714 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
49715 error_at (pragma_tok->location,
49716 "%<#pragma omp end declare target%> without corresponding "
49717 "%<#pragma omp declare target%> or "
49718 "%<#pragma omp begin declare target%>");
49719 else
49721 cp_omp_declare_target_attr
49722 a = scope_chain->omp_declare_target_attribute->pop ();
49723 if (a.attr_syntax != in_omp_attribute_pragma)
49725 if (a.attr_syntax)
49726 error_at (pragma_tok->location,
49727 "%qs in attribute syntax terminated "
49728 "with %qs in pragma syntax",
49729 a.device_type >= 0 ? "begin declare target"
49730 : "declare target",
49731 "end declare target");
49732 else
49733 error_at (pragma_tok->location,
49734 "%qs in pragma syntax terminated "
49735 "with %qs in attribute syntax",
49736 a.device_type >= 0 ? "begin declare target"
49737 : "declare target",
49738 "end declare target");
49742 else if (strcmp (p, "assumes") == 0)
49744 cp_lexer_consume_token (parser->lexer);
49745 cp_parser_require_pragma_eol (parser, pragma_tok);
49746 if (!vec_safe_length (scope_chain->omp_begin_assumes))
49747 error_at (pragma_tok->location,
49748 "%qs without corresponding %qs",
49749 "#pragma omp end assumes", "#pragma omp begin assumes");
49750 else
49752 cp_omp_begin_assumes_data
49753 a = scope_chain->omp_begin_assumes->pop ();
49754 if (a.attr_syntax != in_omp_attribute_pragma)
49756 if (a.attr_syntax)
49757 error_at (pragma_tok->location,
49758 "%qs in attribute syntax terminated "
49759 "with %qs in pragma syntax",
49760 "begin assumes", "end assumes");
49761 else
49762 error_at (pragma_tok->location,
49763 "%qs in pragma syntax terminated "
49764 "with %qs in attribute syntax",
49765 "begin assumes", "end assumes");
49769 else
49771 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
49772 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49773 return;
49777 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
49778 expression and optional initializer clause of
49779 #pragma omp declare reduction. We store the expression(s) as
49780 either 3, 6 or 7 special statements inside of the artificial function's
49781 body. The first two statements are DECL_EXPRs for the artificial
49782 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
49783 expression that uses those variables.
49784 If there was any INITIALIZER clause, this is followed by further statements,
49785 the fourth and fifth statements are DECL_EXPRs for the artificial
49786 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
49787 constructor variant (first token after open paren is not omp_priv),
49788 then the sixth statement is a statement with the function call expression
49789 that uses the OMP_PRIV and optionally OMP_ORIG variable.
49790 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
49791 to initialize the OMP_PRIV artificial variable and there is seventh
49792 statement, a DECL_EXPR of the OMP_PRIV statement again. */
49794 static bool
49795 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
49797 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
49798 gcc_assert (TYPE_REF_P (type));
49799 type = TREE_TYPE (type);
49800 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
49801 DECL_ARTIFICIAL (omp_out) = 1;
49802 pushdecl (omp_out);
49803 add_decl_expr (omp_out);
49804 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
49805 DECL_ARTIFICIAL (omp_in) = 1;
49806 pushdecl (omp_in);
49807 add_decl_expr (omp_in);
49808 tree combiner;
49809 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
49811 keep_next_level (true);
49812 tree block = begin_omp_structured_block ();
49813 combiner = cp_parser_expression (parser);
49814 finish_expr_stmt (combiner);
49815 block = finish_omp_structured_block (block);
49816 if (processing_template_decl)
49817 block = build_stmt (input_location, EXPR_STMT, block);
49818 add_stmt (block);
49820 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
49821 return false;
49823 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49824 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49825 cp_lexer_consume_token (parser->lexer);
49827 const char *p = "";
49828 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49830 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49831 p = IDENTIFIER_POINTER (id);
49834 if (strcmp (p, "initializer") == 0)
49836 cp_lexer_consume_token (parser->lexer);
49837 matching_parens parens;
49838 if (!parens.require_open (parser))
49839 return false;
49841 p = "";
49842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49845 p = IDENTIFIER_POINTER (id);
49848 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
49849 DECL_ARTIFICIAL (omp_priv) = 1;
49850 pushdecl (omp_priv);
49851 add_decl_expr (omp_priv);
49852 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
49853 DECL_ARTIFICIAL (omp_orig) = 1;
49854 pushdecl (omp_orig);
49855 add_decl_expr (omp_orig);
49857 keep_next_level (true);
49858 block = begin_omp_structured_block ();
49860 bool ctor = false;
49861 if (strcmp (p, "omp_priv") == 0)
49863 bool is_non_constant_init;
49864 ctor = true;
49865 cp_lexer_consume_token (parser->lexer);
49866 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
49867 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
49868 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49869 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
49870 == CPP_CLOSE_PAREN
49871 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
49872 == CPP_CLOSE_PAREN))
49874 finish_omp_structured_block (block);
49875 error ("invalid initializer clause");
49876 return false;
49878 initializer = cp_parser_initializer (parser,
49879 /*is_direct_init=*/nullptr,
49880 &is_non_constant_init);
49881 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
49882 NULL_TREE, LOOKUP_ONLYCONVERTING);
49884 else
49886 cp_parser_parse_tentatively (parser);
49887 /* Don't create location wrapper nodes here. */
49888 auto_suppress_location_wrappers sentinel;
49889 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
49890 /*check_dependency_p=*/true,
49891 /*template_p=*/NULL,
49892 /*declarator_p=*/false,
49893 /*optional_p=*/false);
49894 vec<tree, va_gc> *args;
49895 if (fn_name == error_mark_node
49896 || cp_parser_error_occurred (parser)
49897 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49898 || ((args = cp_parser_parenthesized_expression_list
49899 (parser, non_attr, /*cast_p=*/false,
49900 /*allow_expansion_p=*/true,
49901 /*non_constant_p=*/NULL)),
49902 cp_parser_error_occurred (parser)))
49904 finish_omp_structured_block (block);
49905 cp_parser_abort_tentative_parse (parser);
49906 cp_parser_error (parser, "expected id-expression (arguments)");
49907 return false;
49909 unsigned int i;
49910 tree arg;
49911 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
49912 if (arg == omp_priv
49913 || (TREE_CODE (arg) == ADDR_EXPR
49914 && TREE_OPERAND (arg, 0) == omp_priv))
49915 break;
49916 cp_parser_abort_tentative_parse (parser);
49917 if (arg == NULL_TREE)
49918 error ("one of the initializer call arguments should be %<omp_priv%>"
49919 " or %<&omp_priv%>");
49920 initializer = cp_parser_postfix_expression (parser, false, false, false,
49921 false, NULL);
49922 finish_expr_stmt (initializer);
49925 block = finish_omp_structured_block (block);
49926 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
49927 if (processing_template_decl)
49928 block = build_stmt (input_location, EXPR_STMT, block);
49929 add_stmt (block);
49931 if (ctor)
49932 add_decl_expr (omp_orig);
49934 if (!parens.require_close (parser))
49935 return false;
49938 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
49939 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
49940 UNKNOWN_LOCATION);
49942 return true;
49945 /* OpenMP 4.0
49946 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49947 initializer-clause[opt] new-line
49949 initializer-clause:
49950 initializer (omp_priv initializer)
49951 initializer (function-name (argument-list)) */
49953 static void
49954 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
49955 enum pragma_context)
49957 auto_vec<tree> types;
49958 enum tree_code reduc_code = ERROR_MARK;
49959 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
49960 unsigned int i;
49961 cp_token *first_token;
49962 cp_token_cache *cp;
49963 int errs;
49964 void *p;
49966 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
49967 p = obstack_alloc (&declarator_obstack, 0);
49969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
49970 goto fail;
49972 switch (cp_lexer_peek_token (parser->lexer)->type)
49974 case CPP_PLUS:
49975 reduc_code = PLUS_EXPR;
49976 break;
49977 case CPP_MULT:
49978 reduc_code = MULT_EXPR;
49979 break;
49980 case CPP_MINUS:
49981 reduc_code = MINUS_EXPR;
49982 break;
49983 case CPP_AND:
49984 reduc_code = BIT_AND_EXPR;
49985 break;
49986 case CPP_XOR:
49987 reduc_code = BIT_XOR_EXPR;
49988 break;
49989 case CPP_OR:
49990 reduc_code = BIT_IOR_EXPR;
49991 break;
49992 case CPP_AND_AND:
49993 reduc_code = TRUTH_ANDIF_EXPR;
49994 break;
49995 case CPP_OR_OR:
49996 reduc_code = TRUTH_ORIF_EXPR;
49997 break;
49998 case CPP_NAME:
49999 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
50000 break;
50001 default:
50002 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
50003 "%<|%>, %<&&%>, %<||%> or identifier");
50004 goto fail;
50007 if (reduc_code != ERROR_MARK)
50008 cp_lexer_consume_token (parser->lexer);
50010 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
50011 if (reduc_id == error_mark_node)
50012 goto fail;
50014 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
50015 goto fail;
50017 /* Types may not be defined in declare reduction type list. */
50018 const char *saved_message;
50019 saved_message = parser->type_definition_forbidden_message;
50020 parser->type_definition_forbidden_message
50021 = G_("types may not be defined in declare reduction type list");
50022 bool saved_colon_corrects_to_scope_p;
50023 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
50024 parser->colon_corrects_to_scope_p = false;
50025 bool saved_colon_doesnt_start_class_def_p;
50026 saved_colon_doesnt_start_class_def_p
50027 = parser->colon_doesnt_start_class_def_p;
50028 parser->colon_doesnt_start_class_def_p = true;
50030 while (true)
50032 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50033 type = cp_parser_type_id (parser);
50034 if (type == error_mark_node)
50036 else if (ARITHMETIC_TYPE_P (type)
50037 && (orig_reduc_id == NULL_TREE
50038 || (TREE_CODE (type) != COMPLEX_TYPE
50039 && (id_equal (orig_reduc_id, "min")
50040 || id_equal (orig_reduc_id, "max")))))
50041 error_at (loc, "predeclared arithmetic type %qT in "
50042 "%<#pragma omp declare reduction%>", type);
50043 else if (FUNC_OR_METHOD_TYPE_P (type)
50044 || TREE_CODE (type) == ARRAY_TYPE)
50045 error_at (loc, "function or array type %qT in "
50046 "%<#pragma omp declare reduction%>", type);
50047 else if (TYPE_REF_P (type))
50048 error_at (loc, "reference type %qT in "
50049 "%<#pragma omp declare reduction%>", type);
50050 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
50051 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
50052 "type %qT in %<#pragma omp declare reduction%>", type);
50053 else
50054 types.safe_push (type);
50056 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
50057 cp_lexer_consume_token (parser->lexer);
50058 else
50059 break;
50062 /* Restore the saved message. */
50063 parser->type_definition_forbidden_message = saved_message;
50064 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
50065 parser->colon_doesnt_start_class_def_p
50066 = saved_colon_doesnt_start_class_def_p;
50068 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
50069 || types.is_empty ())
50071 fail:
50072 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50073 goto done;
50076 first_token = cp_lexer_peek_token (parser->lexer);
50077 cp = NULL;
50078 errs = errorcount;
50079 FOR_EACH_VEC_ELT (types, i, type)
50081 tree fntype
50082 = build_function_type_list (void_type_node,
50083 cp_build_reference_type (type, false),
50084 NULL_TREE);
50085 tree this_reduc_id = reduc_id;
50086 if (!dependent_type_p (type))
50087 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
50088 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
50089 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
50090 DECL_ARTIFICIAL (fndecl) = 1;
50091 DECL_EXTERNAL (fndecl) = 1;
50092 DECL_DECLARED_INLINE_P (fndecl) = 1;
50093 DECL_IGNORED_P (fndecl) = 1;
50094 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
50095 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
50096 DECL_ATTRIBUTES (fndecl)
50097 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
50098 DECL_ATTRIBUTES (fndecl));
50099 bool block_scope = false;
50100 if (current_function_decl)
50102 block_scope = true;
50103 DECL_CONTEXT (fndecl) = current_function_decl;
50104 DECL_LOCAL_DECL_P (fndecl) = true;
50107 if (processing_template_decl)
50108 fndecl = push_template_decl (fndecl);
50110 if (block_scope)
50112 if (!processing_template_decl)
50113 pushdecl (fndecl);
50115 else if (current_class_type)
50117 if (cp == NULL)
50119 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50120 cp_lexer_consume_token (parser->lexer);
50121 cp = cp_token_cache_new (first_token,
50122 cp_lexer_peek_nth_token (parser->lexer,
50123 2));
50125 DECL_STATIC_FUNCTION_P (fndecl) = 1;
50126 finish_member_declaration (fndecl);
50127 DECL_PENDING_INLINE_INFO (fndecl) = cp;
50128 DECL_PENDING_INLINE_P (fndecl) = 1;
50129 vec_safe_push (unparsed_funs_with_definitions, fndecl);
50130 continue;
50132 else
50134 DECL_CONTEXT (fndecl) = current_namespace;
50135 tree d = pushdecl (fndecl);
50136 /* We should never meet a matched duplicate decl. */
50137 gcc_checking_assert (d == error_mark_node || d == fndecl);
50140 tree block = NULL_TREE;
50141 if (!block_scope)
50142 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
50143 else
50144 block = begin_omp_structured_block ();
50145 if (cp)
50147 cp_parser_push_lexer_for_tokens (parser, cp);
50148 parser->lexer->in_pragma = true;
50151 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
50153 if (cp)
50154 cp_parser_pop_lexer (parser);
50155 if (!block_scope)
50156 finish_function (/*inline_p=*/false);
50157 else
50159 DECL_CONTEXT (fndecl) = current_function_decl;
50160 if (DECL_TEMPLATE_INFO (fndecl))
50161 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
50163 if (!ok)
50164 goto fail;
50166 if (block_scope)
50168 block = finish_omp_structured_block (block);
50169 if (TREE_CODE (block) == BIND_EXPR)
50170 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
50171 else if (TREE_CODE (block) == STATEMENT_LIST)
50172 DECL_SAVED_TREE (fndecl) = block;
50173 if (processing_template_decl)
50174 add_decl_expr (fndecl);
50177 cp_check_omp_declare_reduction (fndecl);
50178 if (cp == NULL && types.length () > 1)
50179 cp = cp_token_cache_new (first_token,
50180 cp_lexer_peek_nth_token (parser->lexer, 2));
50181 if (errs != errorcount)
50182 break;
50185 cp_parser_require_pragma_eol (parser, pragma_tok);
50187 done:
50188 /* Free any declarators allocated. */
50189 obstack_free (&declarator_obstack, p);
50192 /* OpenMP 4.0
50193 #pragma omp declare simd declare-simd-clauses[optseq] new-line
50194 #pragma omp declare reduction (reduction-id : typename-list : expression) \
50195 initializer-clause[opt] new-line
50196 #pragma omp declare target new-line
50198 OpenMP 5.0
50199 #pragma omp declare variant (identifier) match (context-selector) */
50201 static bool
50202 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
50203 enum pragma_context context)
50205 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50207 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
50208 const char *p = IDENTIFIER_POINTER (id);
50210 if (strcmp (p, "simd") == 0)
50212 cp_lexer_consume_token (parser->lexer);
50213 cp_parser_omp_declare_simd (parser, pragma_tok,
50214 context, false);
50215 return true;
50217 if (flag_openmp && strcmp (p, "variant") == 0)
50219 cp_lexer_consume_token (parser->lexer);
50220 cp_parser_omp_declare_simd (parser, pragma_tok,
50221 context, true);
50222 return true;
50224 cp_ensure_no_omp_declare_simd (parser);
50225 if (strcmp (p, "reduction") == 0)
50227 cp_lexer_consume_token (parser->lexer);
50228 cp_parser_omp_declare_reduction (parser, pragma_tok,
50229 context);
50230 return false;
50232 if (!flag_openmp) /* flag_openmp_simd */
50234 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50235 return false;
50237 if (strcmp (p, "target") == 0)
50239 cp_lexer_consume_token (parser->lexer);
50240 cp_parser_omp_declare_target (parser, pragma_tok);
50241 return false;
50244 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
50245 "%<target%> or %<variant%>");
50246 cp_parser_require_pragma_eol (parser, pragma_tok);
50247 return false;
50250 /* OpenMP 5.0
50251 #pragma omp requires clauses[optseq] new-line */
50253 static bool
50254 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
50256 enum omp_requires new_req = (enum omp_requires) 0;
50258 location_t loc = pragma_tok->location;
50259 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
50262 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
50263 cp_lexer_consume_token (parser->lexer);
50265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
50268 const char *p = IDENTIFIER_POINTER (id);
50269 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
50270 enum omp_requires this_req = (enum omp_requires) 0;
50272 if (!strcmp (p, "unified_address"))
50273 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
50274 else if (!strcmp (p, "unified_shared_memory"))
50275 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
50276 else if (!strcmp (p, "dynamic_allocators"))
50277 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
50278 else if (!strcmp (p, "reverse_offload"))
50279 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
50280 else if (!strcmp (p, "atomic_default_mem_order"))
50282 cp_lexer_consume_token (parser->lexer);
50284 matching_parens parens;
50285 if (parens.require_open (parser))
50287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50289 id = cp_lexer_peek_token (parser->lexer)->u.value;
50290 p = IDENTIFIER_POINTER (id);
50292 if (!strcmp (p, "seq_cst"))
50293 this_req
50294 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
50295 else if (!strcmp (p, "relaxed"))
50296 this_req
50297 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
50298 else if (!strcmp (p, "release"))
50299 this_req
50300 = (enum omp_requires) OMP_MEMORY_ORDER_RELEASE;
50301 else if (!strcmp (p, "acq_rel"))
50302 this_req
50303 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
50304 else if (!strcmp (p, "acquire"))
50305 this_req
50306 = (enum omp_requires) OMP_MEMORY_ORDER_ACQUIRE;
50308 if (this_req == 0)
50310 error_at (cp_lexer_peek_token (parser->lexer)->location,
50311 "expected %<acq_rel%>, %<acquire%>, "
50312 "%<relaxed%>, %<release%> or %<seq_cst%>");
50313 switch (cp_lexer_peek_token (parser->lexer)->type)
50315 case CPP_EOF:
50316 case CPP_PRAGMA_EOL:
50317 case CPP_CLOSE_PAREN:
50318 break;
50319 default:
50320 if (cp_lexer_nth_token_is (parser->lexer, 2,
50321 CPP_CLOSE_PAREN))
50322 cp_lexer_consume_token (parser->lexer);
50323 break;
50326 else
50327 cp_lexer_consume_token (parser->lexer);
50329 if (!parens.require_close (parser))
50330 cp_parser_skip_to_closing_parenthesis (parser,
50331 /*recovering=*/true,
50332 /*or_comma=*/false,
50333 /*consume_paren=*/
50334 true);
50336 if (this_req == 0)
50338 cp_parser_require_pragma_eol (parser, pragma_tok);
50339 return false;
50342 p = NULL;
50344 else
50346 error_at (cloc, "expected %<unified_address%>, "
50347 "%<unified_shared_memory%>, "
50348 "%<dynamic_allocators%>, "
50349 "%<reverse_offload%> "
50350 "or %<atomic_default_mem_order%> clause");
50351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50352 return false;
50354 if (p)
50355 cp_lexer_consume_token (parser->lexer);
50356 if (this_req)
50358 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
50360 if ((this_req & new_req) != 0)
50361 error_at (cloc, "too many %qs clauses", p);
50362 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
50363 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
50364 error_at (cloc, "%qs clause used lexically after first "
50365 "target construct or offloading API", p);
50367 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
50369 error_at (cloc, "too many %qs clauses",
50370 "atomic_default_mem_order");
50371 this_req = (enum omp_requires) 0;
50373 else if ((omp_requires_mask
50374 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
50376 error_at (cloc, "more than one %<atomic_default_mem_order%>"
50377 " clause in a single compilation unit");
50378 this_req
50379 = (enum omp_requires)
50380 (omp_requires_mask
50381 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
50383 else if ((omp_requires_mask
50384 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
50385 error_at (cloc, "%<atomic_default_mem_order%> clause used "
50386 "lexically after first %<atomic%> construct "
50387 "without memory order clause");
50388 new_req = (enum omp_requires) (new_req | this_req);
50389 omp_requires_mask
50390 = (enum omp_requires) (omp_requires_mask | this_req);
50391 continue;
50394 break;
50396 cp_parser_require_pragma_eol (parser, pragma_tok);
50398 if (new_req == 0)
50399 error_at (loc, "%<pragma omp requires%> requires at least one clause");
50400 return false;
50404 /* OpenMP 5.1:
50405 #pragma omp nothing new-line */
50407 static void
50408 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
50410 cp_parser_require_pragma_eol (parser, pragma_tok);
50414 /* OpenMP 5.1
50415 #pragma omp error clauses[optseq] new-line */
50417 static bool
50418 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
50419 enum pragma_context context)
50421 int at_compilation = -1;
50422 int severity_fatal = -1;
50423 tree message = NULL_TREE;
50424 bool bad = false;
50425 location_t loc = pragma_tok->location;
50427 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50429 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
50430 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
50431 cp_lexer_consume_token (parser->lexer);
50433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
50434 break;
50436 const char *p
50437 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
50438 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
50439 static const char *args[] = {
50440 "execution", "compilation", "warning", "fatal"
50442 int *v = NULL;
50443 int idx = 0, n = -1;
50444 tree m = NULL_TREE;
50446 if (!strcmp (p, "at"))
50447 v = &at_compilation;
50448 else if (!strcmp (p, "severity"))
50450 v = &severity_fatal;
50451 idx += 2;
50453 else if (strcmp (p, "message"))
50455 error_at (cloc,
50456 "expected %<at%>, %<severity%> or %<message%> clause");
50457 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50458 return false;
50461 cp_lexer_consume_token (parser->lexer);
50463 matching_parens parens;
50464 if (parens.require_open (parser))
50466 if (v == NULL)
50468 m = cp_parser_assignment_expression (parser);
50469 if (type_dependent_expression_p (m))
50470 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
50471 else
50472 m = perform_implicit_conversion_flags (const_string_type_node, m,
50473 tf_warning_or_error,
50474 LOOKUP_NORMAL);
50476 else
50478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50480 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
50481 const char *q = IDENTIFIER_POINTER (val);
50483 if (!strcmp (q, args[idx]))
50484 n = 0;
50485 else if (!strcmp (q, args[idx + 1]))
50486 n = 1;
50488 if (n == -1)
50490 error_at (cp_lexer_peek_token (parser->lexer)->location,
50491 "expected %qs or %qs", args[idx], args[idx + 1]);
50492 bad = true;
50493 switch (cp_lexer_peek_token (parser->lexer)->type)
50495 case CPP_EOF:
50496 case CPP_PRAGMA_EOL:
50497 case CPP_CLOSE_PAREN:
50498 break;
50499 default:
50500 if (cp_lexer_nth_token_is (parser->lexer, 2,
50501 CPP_CLOSE_PAREN))
50502 cp_lexer_consume_token (parser->lexer);
50503 break;
50506 else
50507 cp_lexer_consume_token (parser->lexer);
50510 if (!parens.require_close (parser))
50511 cp_parser_skip_to_closing_parenthesis (parser,
50512 /*recovering=*/true,
50513 /*or_comma=*/false,
50514 /*consume_paren=*/
50515 true);
50517 if (v == NULL)
50519 if (message)
50521 error_at (cloc, "too many %qs clauses", p);
50522 bad = true;
50524 else
50525 message = m;
50527 else if (n != -1)
50529 if (*v != -1)
50531 error_at (cloc, "too many %qs clauses", p);
50532 bad = true;
50534 else
50535 *v = n;
50538 else
50539 bad = true;
50541 cp_parser_require_pragma_eol (parser, pragma_tok);
50542 if (bad)
50543 return true;
50545 if (at_compilation == -1)
50546 at_compilation = 1;
50547 if (severity_fatal == -1)
50548 severity_fatal = 1;
50549 if (!at_compilation)
50551 if (context != pragma_compound)
50553 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
50554 "may only be used in compound statements");
50555 return true;
50557 tree fndecl
50558 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
50559 : BUILT_IN_GOMP_WARNING);
50560 if (!message)
50561 message = build_zero_cst (const_string_type_node);
50562 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
50563 build_all_ones_cst (size_type_node));
50564 add_stmt (stmt);
50565 return true;
50568 if (in_discarded_stmt)
50569 return false;
50571 const char *msg = NULL;
50572 if (message)
50574 msg = c_getstr (fold_for_warn (message));
50575 if (msg == NULL)
50576 msg = _("<message unknown at compile time>");
50578 if (msg)
50579 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
50580 "%<pragma omp error%> encountered: %s", msg);
50581 else
50582 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
50583 "%<pragma omp error%> encountered");
50584 return false;
50587 /* OpenMP 4.5:
50588 #pragma omp taskloop taskloop-clause[optseq] new-line
50589 for-loop
50591 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
50592 for-loop */
50594 #define OMP_TASKLOOP_CLAUSE_MASK \
50595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
50596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
50597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
50598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
50599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
50600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
50601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
50602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
50603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
50604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
50605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
50606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
50607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
50608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
50609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
50610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
50611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
50613 static tree
50614 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
50615 char *p_name, omp_clause_mask mask, tree *cclauses,
50616 bool *if_p)
50618 tree clauses, sb, ret;
50619 unsigned int save;
50620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50622 strcat (p_name, " taskloop");
50623 mask |= OMP_TASKLOOP_CLAUSE_MASK;
50624 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
50625 clause. */
50626 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
50627 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
50629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
50632 const char *p = IDENTIFIER_POINTER (id);
50634 if (strcmp (p, "simd") == 0)
50636 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
50637 if (cclauses == NULL)
50638 cclauses = cclauses_buf;
50640 cp_lexer_consume_token (parser->lexer);
50641 if (!flag_openmp) /* flag_openmp_simd */
50642 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
50643 cclauses, if_p);
50644 sb = begin_omp_structured_block ();
50645 save = cp_parser_begin_omp_structured_block (parser);
50646 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
50647 cclauses, if_p);
50648 cp_parser_end_omp_structured_block (parser, save);
50649 tree body = finish_omp_structured_block (sb);
50650 if (ret == NULL)
50651 return ret;
50652 ret = make_node (OMP_TASKLOOP);
50653 TREE_TYPE (ret) = void_type_node;
50654 OMP_FOR_BODY (ret) = body;
50655 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
50656 SET_EXPR_LOCATION (ret, loc);
50657 add_stmt (ret);
50658 return ret;
50661 if (!flag_openmp) /* flag_openmp_simd */
50663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50664 return NULL_TREE;
50667 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
50668 cclauses == NULL);
50669 if (cclauses)
50671 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
50672 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
50675 keep_next_level (true);
50676 sb = begin_omp_structured_block ();
50677 save = cp_parser_begin_omp_structured_block (parser);
50679 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
50680 if_p);
50682 cp_parser_end_omp_structured_block (parser, save);
50683 add_stmt (finish_omp_structured_block (sb));
50685 return ret;
50689 /* OpenACC 2.0:
50690 # pragma acc routine oacc-routine-clause[optseq] new-line
50691 function-definition
50693 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
50696 #define OACC_ROUTINE_CLAUSE_MASK \
50697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
50698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
50699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
50700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
50701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
50703 /* Parse the OpenACC routine pragma. This has an optional '( name )'
50704 component, which must resolve to a declared namespace-scope
50705 function. The clauses are either processed directly (for a named
50706 function), or defered until the immediatley following declaration
50707 is parsed. */
50709 static void
50710 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
50711 enum pragma_context context)
50713 gcc_checking_assert (context == pragma_external);
50714 /* The checking for "another pragma following this one" in the "no optional
50715 '( name )'" case makes sure that we dont re-enter. */
50716 gcc_checking_assert (parser->oacc_routine == NULL);
50718 cp_oacc_routine_data data;
50719 data.error_seen = false;
50720 data.fndecl_seen = false;
50721 data.tokens = vNULL;
50722 data.clauses = NULL_TREE;
50723 data.loc = pragma_tok->location;
50724 /* It is safe to take the address of a local variable; it will only be
50725 used while this scope is live. */
50726 parser->oacc_routine = &data;
50728 /* Look for optional '( name )'. */
50729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
50731 matching_parens parens;
50732 parens.consume_open (parser); /* '(' */
50734 /* We parse the name as an id-expression. If it resolves to
50735 anything other than a non-overloaded function at namespace
50736 scope, it's an error. */
50737 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
50738 tree name = cp_parser_id_expression (parser,
50739 /*template_keyword_p=*/false,
50740 /*check_dependency_p=*/false,
50741 /*template_p=*/NULL,
50742 /*declarator_p=*/false,
50743 /*optional_p=*/false);
50744 tree decl = (identifier_p (name)
50745 ? cp_parser_lookup_name_simple (parser, name, name_loc)
50746 : name);
50747 if (name != error_mark_node && decl == error_mark_node)
50748 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
50750 if (decl == error_mark_node
50751 || !parens.require_close (parser))
50753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50754 parser->oacc_routine = NULL;
50755 return;
50758 data.clauses
50759 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50760 "#pragma acc routine",
50761 cp_lexer_peek_token (parser->lexer));
50762 /* The clauses are in reverse order; fix that to make later diagnostic
50763 emission easier. */
50764 data.clauses = nreverse (data.clauses);
50766 if (decl && is_overloaded_fn (decl)
50767 && (TREE_CODE (decl) != FUNCTION_DECL
50768 || DECL_FUNCTION_TEMPLATE_P (decl)))
50770 error_at (name_loc,
50771 "%<#pragma acc routine%> names a set of overloads");
50772 parser->oacc_routine = NULL;
50773 return;
50776 /* Perhaps we should use the same rule as declarations in different
50777 namespaces? */
50778 if (!DECL_NAMESPACE_SCOPE_P (decl))
50780 error_at (name_loc,
50781 "%qD does not refer to a namespace scope function", decl);
50782 parser->oacc_routine = NULL;
50783 return;
50786 if (TREE_CODE (decl) != FUNCTION_DECL)
50788 error_at (name_loc, "%qD does not refer to a function", decl);
50789 parser->oacc_routine = NULL;
50790 return;
50793 cp_finalize_oacc_routine (parser, decl, false);
50794 parser->oacc_routine = NULL;
50796 else /* No optional '( name )'. */
50798 /* Store away all pragma tokens. */
50799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50800 cp_lexer_consume_token (parser->lexer);
50801 cp_parser_require_pragma_eol (parser, pragma_tok);
50802 struct cp_token_cache *cp
50803 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
50804 parser->oacc_routine->tokens.safe_push (cp);
50806 /* Emit a helpful diagnostic if there's another pragma following this
50807 one. */
50808 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
50810 cp_ensure_no_oacc_routine (parser);
50811 data.tokens.release ();
50812 /* ..., and then just keep going. */
50813 return;
50816 /* We only have to consider the pragma_external case here. */
50817 cp_parser_declaration (parser, NULL_TREE);
50818 if (parser->oacc_routine
50819 && !parser->oacc_routine->fndecl_seen)
50820 cp_ensure_no_oacc_routine (parser);
50821 else
50822 parser->oacc_routine = NULL;
50823 data.tokens.release ();
50827 /* Finalize #pragma acc routine clauses after direct declarator has
50828 been parsed. */
50830 static tree
50831 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
50833 struct cp_token_cache *ce;
50834 cp_oacc_routine_data *data = parser->oacc_routine;
50836 if (!data->error_seen && data->fndecl_seen)
50838 error_at (data->loc,
50839 "%<#pragma acc routine%> not immediately followed by "
50840 "a single function declaration or definition");
50841 data->error_seen = true;
50843 if (data->error_seen)
50844 return attrs;
50846 gcc_checking_assert (data->tokens.length () == 1);
50847 ce = data->tokens[0];
50849 cp_parser_push_lexer_for_tokens (parser, ce);
50850 parser->lexer->in_pragma = true;
50851 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
50853 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
50854 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
50855 parser->oacc_routine->clauses
50856 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50857 "#pragma acc routine", pragma_tok);
50858 /* The clauses are in reverse order; fix that to make later diagnostic
50859 emission easier. */
50860 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
50861 cp_parser_pop_lexer (parser);
50862 /* Later, cp_finalize_oacc_routine will process the clauses. */
50863 parser->oacc_routine->fndecl_seen = true;
50865 return attrs;
50868 /* Apply any saved OpenACC routine clauses to a just-parsed
50869 declaration. */
50871 static void
50872 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
50874 if (UNLIKELY (parser->oacc_routine != NULL))
50876 /* Keep going if we're in error reporting mode. */
50877 if (parser->oacc_routine->error_seen
50878 || fndecl == error_mark_node)
50879 return;
50881 if (TREE_CODE (fndecl) != FUNCTION_DECL)
50883 if (parser->oacc_routine->fndecl_seen)
50885 error_at (parser->oacc_routine->loc,
50886 "%<#pragma acc routine%> not immediately followed by"
50887 " a single function declaration or definition");
50888 parser->oacc_routine = NULL;
50889 return;
50892 cp_ensure_no_oacc_routine (parser);
50893 return;
50896 int compatible
50897 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
50898 parser->oacc_routine->loc,
50899 "#pragma acc routine");
50900 if (compatible < 0)
50902 parser->oacc_routine = NULL;
50903 return;
50905 if (compatible > 0)
50908 else
50910 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
50912 error_at (parser->oacc_routine->loc,
50913 TREE_USED (fndecl)
50914 ? G_("%<#pragma acc routine%> must be applied before"
50915 " use")
50916 : G_("%<#pragma acc routine%> must be applied before"
50917 " definition"));
50918 parser->oacc_routine = NULL;
50919 return;
50922 /* Set the routine's level of parallelism. */
50923 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
50924 oacc_replace_fn_attrib (fndecl, dims);
50926 /* Add an "omp declare target" attribute. */
50927 DECL_ATTRIBUTES (fndecl)
50928 = tree_cons (get_identifier ("omp declare target"),
50929 parser->oacc_routine->clauses,
50930 DECL_ATTRIBUTES (fndecl));
50935 /* Main entry point to OpenMP statement pragmas. */
50937 static void
50938 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
50940 tree stmt;
50941 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
50942 omp_clause_mask mask (0);
50944 switch (cp_parser_pragma_kind (pragma_tok))
50946 case PRAGMA_OACC_ATOMIC:
50947 cp_parser_omp_atomic (parser, pragma_tok, true);
50948 return;
50949 case PRAGMA_OACC_CACHE:
50950 stmt = cp_parser_oacc_cache (parser, pragma_tok);
50951 break;
50952 case PRAGMA_OACC_DATA:
50953 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
50954 break;
50955 case PRAGMA_OACC_ENTER_DATA:
50956 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
50957 break;
50958 case PRAGMA_OACC_EXIT_DATA:
50959 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
50960 break;
50961 case PRAGMA_OACC_HOST_DATA:
50962 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
50963 break;
50964 case PRAGMA_OACC_KERNELS:
50965 case PRAGMA_OACC_PARALLEL:
50966 case PRAGMA_OACC_SERIAL:
50967 strcpy (p_name, "#pragma acc");
50968 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
50969 break;
50970 case PRAGMA_OACC_LOOP:
50971 strcpy (p_name, "#pragma acc");
50972 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
50973 if_p);
50974 break;
50975 case PRAGMA_OACC_UPDATE:
50976 stmt = cp_parser_oacc_update (parser, pragma_tok);
50977 break;
50978 case PRAGMA_OACC_WAIT:
50979 stmt = cp_parser_oacc_wait (parser, pragma_tok);
50980 break;
50981 case PRAGMA_OMP_ALLOCATE:
50982 cp_parser_omp_allocate (parser, pragma_tok);
50983 return;
50984 case PRAGMA_OMP_ATOMIC:
50985 cp_parser_omp_atomic (parser, pragma_tok, false);
50986 return;
50987 case PRAGMA_OMP_CRITICAL:
50988 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
50989 break;
50990 case PRAGMA_OMP_DISTRIBUTE:
50991 strcpy (p_name, "#pragma omp");
50992 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
50993 if_p);
50994 break;
50995 case PRAGMA_OMP_FOR:
50996 strcpy (p_name, "#pragma omp");
50997 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
50998 if_p);
50999 break;
51000 case PRAGMA_OMP_LOOP:
51001 strcpy (p_name, "#pragma omp");
51002 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
51003 if_p);
51004 break;
51005 case PRAGMA_OMP_MASKED:
51006 strcpy (p_name, "#pragma omp");
51007 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
51008 if_p);
51009 break;
51010 case PRAGMA_OMP_MASTER:
51011 strcpy (p_name, "#pragma omp");
51012 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
51013 if_p);
51014 break;
51015 case PRAGMA_OMP_PARALLEL:
51016 strcpy (p_name, "#pragma omp");
51017 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
51018 if_p);
51019 break;
51020 case PRAGMA_OMP_SCOPE:
51021 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
51022 break;
51023 case PRAGMA_OMP_SECTIONS:
51024 strcpy (p_name, "#pragma omp");
51025 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
51026 break;
51027 case PRAGMA_OMP_SIMD:
51028 strcpy (p_name, "#pragma omp");
51029 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
51030 if_p);
51031 break;
51032 case PRAGMA_OMP_SINGLE:
51033 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
51034 break;
51035 case PRAGMA_OMP_TASK:
51036 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
51037 break;
51038 case PRAGMA_OMP_TASKGROUP:
51039 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
51040 break;
51041 case PRAGMA_OMP_TASKLOOP:
51042 strcpy (p_name, "#pragma omp");
51043 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
51044 if_p);
51045 break;
51046 case PRAGMA_OMP_TEAMS:
51047 strcpy (p_name, "#pragma omp");
51048 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
51049 if_p);
51050 break;
51051 case PRAGMA_OMP_ASSUME:
51052 cp_parser_omp_assume (parser, pragma_tok, if_p);
51053 return;
51054 case PRAGMA_OMP_TILE:
51055 stmt = cp_parser_omp_tile (parser, pragma_tok, if_p);
51056 break;
51057 case PRAGMA_OMP_UNROLL:
51058 stmt = cp_parser_omp_unroll (parser, pragma_tok, if_p);
51059 break;
51060 default:
51061 gcc_unreachable ();
51064 protected_set_expr_location (stmt, pragma_tok->location);
51067 /* Transactional Memory parsing routines. */
51069 /* Parse a transaction attribute.
51071 txn-attribute:
51072 attribute
51073 [ [ identifier ] ]
51075 We use this instead of cp_parser_attributes_opt for transactions to avoid
51076 the pedwarn in C++98 mode. */
51078 static tree
51079 cp_parser_txn_attribute_opt (cp_parser *parser)
51081 cp_token *token;
51082 tree attr_name, attr = NULL;
51084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
51085 return cp_parser_attributes_opt (parser);
51087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
51088 return NULL_TREE;
51089 cp_lexer_consume_token (parser->lexer);
51090 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
51091 goto error1;
51093 token = cp_lexer_peek_token (parser->lexer);
51094 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
51096 token = cp_lexer_consume_token (parser->lexer);
51098 attr_name = (token->type == CPP_KEYWORD
51099 /* For keywords, use the canonical spelling,
51100 not the parsed identifier. */
51101 ? ridpointers[(int) token->keyword]
51102 : token->u.value);
51103 attr = build_tree_list (attr_name, NULL_TREE);
51105 else
51106 cp_parser_error (parser, "expected identifier");
51108 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
51109 error1:
51110 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
51111 return attr;
51114 /* Parse a __transaction_atomic or __transaction_relaxed statement.
51116 transaction-statement:
51117 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
51118 compound-statement
51119 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
51122 static tree
51123 cp_parser_transaction (cp_parser *parser, cp_token *token)
51125 unsigned char old_in = parser->in_transaction;
51126 unsigned char this_in = 1, new_in;
51127 enum rid keyword = token->keyword;
51128 tree stmt, attrs, noex;
51130 cp_lexer_consume_token (parser->lexer);
51132 if (keyword == RID_TRANSACTION_RELAXED
51133 || keyword == RID_SYNCHRONIZED)
51134 this_in |= TM_STMT_ATTR_RELAXED;
51135 else
51137 attrs = cp_parser_txn_attribute_opt (parser);
51138 if (attrs)
51139 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
51142 /* Parse a noexcept specification. */
51143 if (keyword == RID_ATOMIC_NOEXCEPT)
51144 noex = boolean_true_node;
51145 else if (keyword == RID_ATOMIC_CANCEL)
51147 /* cancel-and-throw is unimplemented. */
51148 sorry ("%<atomic_cancel%>");
51149 noex = NULL_TREE;
51151 else
51152 noex = cp_parser_noexcept_specification_opt (parser,
51153 CP_PARSER_FLAGS_NONE,
51154 /*require_constexpr=*/true,
51155 /*consumed_expr=*/NULL,
51156 /*return_cond=*/true);
51158 /* Keep track if we're in the lexical scope of an outer transaction. */
51159 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
51161 stmt = begin_transaction_stmt (token->location, NULL, this_in);
51163 parser->in_transaction = new_in;
51164 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
51165 parser->in_transaction = old_in;
51167 finish_transaction_stmt (stmt, NULL, this_in, noex);
51169 return stmt;
51172 /* Parse a __transaction_atomic or __transaction_relaxed expression.
51174 transaction-expression:
51175 __transaction_atomic txn-noexcept-spec[opt] ( expression )
51176 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
51179 static tree
51180 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
51182 unsigned char old_in = parser->in_transaction;
51183 unsigned char this_in = 1;
51184 cp_token *token;
51185 tree expr, noex;
51186 bool noex_expr;
51187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
51189 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
51190 || keyword == RID_TRANSACTION_RELAXED);
51192 if (!flag_tm)
51193 error_at (loc,
51194 keyword == RID_TRANSACTION_RELAXED
51195 ? G_("%<__transaction_relaxed%> without transactional memory "
51196 "support enabled")
51197 : G_("%<__transaction_atomic%> without transactional memory "
51198 "support enabled"));
51200 token = cp_parser_require_keyword (parser, keyword,
51201 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
51202 : RT_TRANSACTION_RELAXED));
51203 gcc_assert (token != NULL);
51205 if (keyword == RID_TRANSACTION_RELAXED)
51206 this_in |= TM_STMT_ATTR_RELAXED;
51208 /* Set this early. This might mean that we allow transaction_cancel in
51209 an expression that we find out later actually has to be a constexpr.
51210 However, we expect that cxx_constant_value will be able to deal with
51211 this; also, if the noexcept has no constexpr, then what we parse next
51212 really is a transaction's body. */
51213 parser->in_transaction = this_in;
51215 /* Parse a noexcept specification. */
51216 noex = cp_parser_noexcept_specification_opt (parser,
51217 CP_PARSER_FLAGS_NONE,
51218 /*require_constexpr=*/false,
51219 &noex_expr,
51220 /*return_cond=*/true);
51222 if (!noex || !noex_expr
51223 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
51225 matching_parens parens;
51226 parens.require_open (parser);
51228 expr = cp_parser_expression (parser);
51229 expr = finish_parenthesized_expr (expr);
51231 parens.require_close (parser);
51233 else
51235 /* The only expression that is available got parsed for the noexcept
51236 already. noexcept is true then. */
51237 expr = noex;
51238 noex = boolean_true_node;
51241 expr = build_transaction_expr (token->location, expr, this_in, noex);
51242 parser->in_transaction = old_in;
51244 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
51245 return error_mark_node;
51247 return (flag_tm ? expr : error_mark_node);
51250 /* Parse a function-transaction-block.
51252 function-transaction-block:
51253 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
51254 function-body
51255 __transaction_atomic txn-attribute[opt] function-try-block
51256 __transaction_relaxed ctor-initializer[opt] function-body
51257 __transaction_relaxed function-try-block
51260 static void
51261 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
51263 unsigned char old_in = parser->in_transaction;
51264 unsigned char new_in = 1;
51265 tree compound_stmt, stmt, attrs;
51266 cp_token *token;
51268 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
51269 || keyword == RID_TRANSACTION_RELAXED);
51270 token = cp_parser_require_keyword (parser, keyword,
51271 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
51272 : RT_TRANSACTION_RELAXED));
51273 gcc_assert (token != NULL);
51275 if (keyword == RID_TRANSACTION_RELAXED)
51276 new_in |= TM_STMT_ATTR_RELAXED;
51277 else
51279 attrs = cp_parser_txn_attribute_opt (parser);
51280 if (attrs)
51281 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
51284 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
51286 parser->in_transaction = new_in;
51288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
51289 cp_parser_function_try_block (parser);
51290 else
51291 cp_parser_ctor_initializer_opt_and_function_body
51292 (parser, /*in_function_try_block=*/false);
51294 parser->in_transaction = old_in;
51296 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
51299 /* Parse a __transaction_cancel statement.
51301 cancel-statement:
51302 __transaction_cancel txn-attribute[opt] ;
51303 __transaction_cancel txn-attribute[opt] throw-expression ;
51305 ??? Cancel and throw is not yet implemented. */
51307 static tree
51308 cp_parser_transaction_cancel (cp_parser *parser)
51310 cp_token *token;
51311 bool is_outer = false;
51312 tree stmt, attrs;
51314 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
51315 RT_TRANSACTION_CANCEL);
51316 gcc_assert (token != NULL);
51318 attrs = cp_parser_txn_attribute_opt (parser);
51319 if (attrs)
51320 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
51322 /* ??? Parse cancel-and-throw here. */
51324 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
51326 if (!flag_tm)
51328 error_at (token->location, "%<__transaction_cancel%> without "
51329 "transactional memory support enabled");
51330 return error_mark_node;
51332 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
51334 error_at (token->location, "%<__transaction_cancel%> within a "
51335 "%<__transaction_relaxed%>");
51336 return error_mark_node;
51338 else if (is_outer)
51340 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
51341 && !is_tm_may_cancel_outer (current_function_decl))
51343 error_at (token->location, "outer %<__transaction_cancel%> not "
51344 "within outer %<__transaction_atomic%>");
51345 error_at (token->location,
51346 " or a %<transaction_may_cancel_outer%> function");
51347 return error_mark_node;
51350 else if (parser->in_transaction == 0)
51352 error_at (token->location, "%<__transaction_cancel%> not within "
51353 "%<__transaction_atomic%>");
51354 return error_mark_node;
51357 stmt = build_tm_abort_call (token->location, is_outer);
51358 add_stmt (stmt);
51360 return stmt;
51364 /* Special handling for the first token or line in the file. The first
51365 thing in the file might be #pragma GCC pch_preprocess, which loads a
51366 PCH file, which is a GC collection point. So we need to handle this
51367 first pragma without benefit of an existing lexer structure.
51369 Always returns one token to the caller in *FIRST_TOKEN. This is
51370 either the true first token of the file, or the first token after
51371 the initial pragma. */
51373 static void
51374 cp_parser_initial_pragma (cp_token *first_token)
51376 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
51377 return;
51379 cp_lexer_get_preprocessor_token (0, first_token);
51381 tree name = NULL;
51382 if (first_token->type == CPP_STRING)
51384 name = first_token->u.value;
51386 cp_lexer_get_preprocessor_token (0, first_token);
51389 /* Skip to the end of the pragma. */
51390 if (first_token->type != CPP_PRAGMA_EOL)
51392 error_at (first_token->location,
51393 "malformed %<#pragma GCC pch_preprocess%>");
51395 cp_lexer_get_preprocessor_token (0, first_token);
51396 while (first_token->type != CPP_PRAGMA_EOL);
51399 /* Now actually load the PCH file. */
51400 if (name)
51401 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
51403 /* Read one more token to return to our caller. We have to do this
51404 after reading the PCH file in, since its pointers have to be
51405 live. */
51406 cp_lexer_get_preprocessor_token (0, first_token);
51409 /* Parse a pragma GCC ivdep. */
51411 static bool
51412 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
51414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51415 return true;
51418 /* Parse a pragma GCC unroll. */
51420 static tree
51421 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
51423 location_t location = cp_lexer_peek_token (parser->lexer)->location;
51424 tree unroll = cp_parser_constant_expression (parser);
51425 unroll = cp_check_pragma_unroll (location, fold_non_dependent_expr (unroll));
51426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51427 return unroll;
51430 /* Parse a pragma GCC novector. */
51432 static bool
51433 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
51435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51436 return true;
51439 /* Normal parsing of a pragma token. Here we can (and must) use the
51440 regular lexer. */
51442 static bool
51443 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
51445 cp_token *pragma_tok;
51446 unsigned int id;
51447 tree stmt;
51448 bool ret = false;
51450 pragma_tok = cp_lexer_consume_token (parser->lexer);
51451 gcc_assert (pragma_tok->type == CPP_PRAGMA);
51452 parser->lexer->in_pragma = true;
51454 id = cp_parser_pragma_kind (pragma_tok);
51455 if (parser->omp_for_parse_state
51456 && parser->omp_for_parse_state->in_intervening_code
51457 && id >= PRAGMA_OMP__START_
51458 && id <= PRAGMA_OMP__LAST_)
51460 error_at (pragma_tok->location,
51461 "intervening code must not contain OpenMP directives");
51462 parser->omp_for_parse_state->fail = true;
51463 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51464 return false;
51466 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
51467 cp_ensure_no_omp_declare_simd (parser);
51468 switch (id)
51470 case PRAGMA_GCC_PCH_PREPROCESS:
51471 error_at (pragma_tok->location,
51472 "%<#pragma GCC pch_preprocess%> must be first");
51473 break;
51475 case PRAGMA_OMP_BARRIER:
51476 switch (context)
51478 case pragma_compound:
51479 cp_parser_omp_barrier (parser, pragma_tok);
51480 return false;
51481 case pragma_stmt:
51482 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
51483 "used in compound statements", "omp barrier");
51484 ret = true;
51485 break;
51486 default:
51487 goto bad_stmt;
51489 break;
51491 case PRAGMA_OMP_DEPOBJ:
51492 switch (context)
51494 case pragma_compound:
51495 cp_parser_omp_depobj (parser, pragma_tok);
51496 return false;
51497 case pragma_stmt:
51498 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
51499 "used in compound statements", "omp depobj");
51500 ret = true;
51501 break;
51502 default:
51503 goto bad_stmt;
51505 break;
51507 case PRAGMA_OMP_FLUSH:
51508 switch (context)
51510 case pragma_compound:
51511 cp_parser_omp_flush (parser, pragma_tok);
51512 return false;
51513 case pragma_stmt:
51514 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
51515 "used in compound statements", "omp flush");
51516 ret = true;
51517 break;
51518 default:
51519 goto bad_stmt;
51521 break;
51523 case PRAGMA_OMP_TASKWAIT:
51524 switch (context)
51526 case pragma_compound:
51527 cp_parser_omp_taskwait (parser, pragma_tok);
51528 return false;
51529 case pragma_stmt:
51530 error_at (pragma_tok->location,
51531 "%<#pragma %s%> may only be used in compound statements",
51532 "omp taskwait");
51533 ret = true;
51534 break;
51535 default:
51536 goto bad_stmt;
51538 break;
51540 case PRAGMA_OMP_TASKYIELD:
51541 switch (context)
51543 case pragma_compound:
51544 cp_parser_omp_taskyield (parser, pragma_tok);
51545 return false;
51546 case pragma_stmt:
51547 error_at (pragma_tok->location,
51548 "%<#pragma %s%> may only be used in compound statements",
51549 "omp taskyield");
51550 ret = true;
51551 break;
51552 default:
51553 goto bad_stmt;
51555 break;
51557 case PRAGMA_OMP_CANCEL:
51558 switch (context)
51560 case pragma_compound:
51561 cp_parser_omp_cancel (parser, pragma_tok);
51562 return false;
51563 case pragma_stmt:
51564 error_at (pragma_tok->location,
51565 "%<#pragma %s%> may only be used in compound statements",
51566 "omp cancel");
51567 ret = true;
51568 break;
51569 default:
51570 goto bad_stmt;
51572 break;
51574 case PRAGMA_OMP_CANCELLATION_POINT:
51575 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
51577 case PRAGMA_OMP_THREADPRIVATE:
51578 cp_parser_omp_threadprivate (parser, pragma_tok);
51579 return false;
51581 case PRAGMA_OMP_DECLARE:
51582 return cp_parser_omp_declare (parser, pragma_tok, context);
51584 case PRAGMA_OACC_DECLARE:
51585 cp_parser_oacc_declare (parser, pragma_tok);
51586 return false;
51588 case PRAGMA_OACC_ENTER_DATA:
51589 if (context == pragma_stmt)
51591 error_at (pragma_tok->location,
51592 "%<#pragma %s%> may only be used in compound statements",
51593 "acc enter data");
51594 ret = true;
51595 break;
51597 else if (context != pragma_compound)
51598 goto bad_stmt;
51599 cp_parser_omp_construct (parser, pragma_tok, if_p);
51600 return true;
51602 case PRAGMA_OACC_EXIT_DATA:
51603 if (context == pragma_stmt)
51605 error_at (pragma_tok->location,
51606 "%<#pragma %s%> may only be used in compound statements",
51607 "acc exit data");
51608 ret = true;
51609 break;
51611 else if (context != pragma_compound)
51612 goto bad_stmt;
51613 cp_parser_omp_construct (parser, pragma_tok, if_p);
51614 return true;
51616 case PRAGMA_OACC_ROUTINE:
51617 if (context != pragma_external)
51619 error_at (pragma_tok->location,
51620 "%<#pragma acc routine%> must be at file scope");
51621 ret = true;
51622 break;
51624 cp_parser_oacc_routine (parser, pragma_tok, context);
51625 return false;
51627 case PRAGMA_OACC_UPDATE:
51628 if (context == pragma_stmt)
51630 error_at (pragma_tok->location,
51631 "%<#pragma %s%> may only be used in compound statements",
51632 "acc update");
51633 ret = true;
51634 break;
51636 else if (context != pragma_compound)
51637 goto bad_stmt;
51638 cp_parser_omp_construct (parser, pragma_tok, if_p);
51639 return true;
51641 case PRAGMA_OACC_WAIT:
51642 if (context == pragma_stmt)
51644 error_at (pragma_tok->location,
51645 "%<#pragma %s%> may only be used in compound statements",
51646 "acc wait");
51647 ret = true;
51648 break;
51650 else if (context != pragma_compound)
51651 goto bad_stmt;
51652 cp_parser_omp_construct (parser, pragma_tok, if_p);
51653 return true;
51654 case PRAGMA_OMP_ALLOCATE:
51655 cp_parser_omp_allocate (parser, pragma_tok);
51656 return false;
51657 case PRAGMA_OACC_ATOMIC:
51658 case PRAGMA_OACC_CACHE:
51659 case PRAGMA_OACC_DATA:
51660 case PRAGMA_OACC_HOST_DATA:
51661 case PRAGMA_OACC_KERNELS:
51662 case PRAGMA_OACC_LOOP:
51663 case PRAGMA_OACC_PARALLEL:
51664 case PRAGMA_OACC_SERIAL:
51665 case PRAGMA_OMP_ASSUME:
51666 case PRAGMA_OMP_ATOMIC:
51667 case PRAGMA_OMP_CRITICAL:
51668 case PRAGMA_OMP_DISTRIBUTE:
51669 case PRAGMA_OMP_FOR:
51670 case PRAGMA_OMP_LOOP:
51671 case PRAGMA_OMP_MASKED:
51672 case PRAGMA_OMP_MASTER:
51673 case PRAGMA_OMP_PARALLEL:
51674 case PRAGMA_OMP_SCOPE:
51675 case PRAGMA_OMP_SECTIONS:
51676 case PRAGMA_OMP_SIMD:
51677 case PRAGMA_OMP_SINGLE:
51678 case PRAGMA_OMP_TASK:
51679 case PRAGMA_OMP_TASKGROUP:
51680 case PRAGMA_OMP_TASKLOOP:
51681 case PRAGMA_OMP_TEAMS:
51682 case PRAGMA_OMP_TILE:
51683 case PRAGMA_OMP_UNROLL:
51684 if (context != pragma_stmt && context != pragma_compound)
51685 goto bad_stmt;
51686 stmt = push_omp_privatization_clauses (false);
51687 cp_parser_omp_construct (parser, pragma_tok, if_p);
51688 pop_omp_privatization_clauses (stmt);
51689 return true;
51691 case PRAGMA_OMP_REQUIRES:
51692 if (context != pragma_external)
51694 error_at (pragma_tok->location,
51695 "%<#pragma omp requires%> may only be used at file or "
51696 "namespace scope");
51697 ret = true;
51698 break;
51700 return cp_parser_omp_requires (parser, pragma_tok);
51702 case PRAGMA_OMP_ASSUMES:
51703 if (context != pragma_external)
51705 error_at (pragma_tok->location,
51706 "%<#pragma omp assumes%> may only be used at file or "
51707 "namespace scope");
51708 ret = true;
51709 break;
51711 return cp_parser_omp_assumes (parser, pragma_tok);
51713 case PRAGMA_OMP_NOTHING:
51714 cp_parser_omp_nothing (parser, pragma_tok);
51715 return false;
51717 case PRAGMA_OMP_ERROR:
51718 return cp_parser_omp_error (parser, pragma_tok, context);
51720 case PRAGMA_OMP_ORDERED:
51721 if (context != pragma_stmt && context != pragma_compound)
51722 goto bad_stmt;
51723 stmt = push_omp_privatization_clauses (false);
51724 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
51725 pop_omp_privatization_clauses (stmt);
51726 return ret;
51728 case PRAGMA_OMP_TARGET:
51729 if (context != pragma_stmt && context != pragma_compound)
51730 goto bad_stmt;
51731 stmt = push_omp_privatization_clauses (false);
51732 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
51733 pop_omp_privatization_clauses (stmt);
51734 return ret;
51736 case PRAGMA_OMP_BEGIN:
51737 cp_parser_omp_begin (parser, pragma_tok);
51738 return false;
51740 case PRAGMA_OMP_END:
51741 cp_parser_omp_end (parser, pragma_tok);
51742 return false;
51744 case PRAGMA_OMP_SCAN:
51745 error_at (pragma_tok->location,
51746 "%<#pragma omp scan%> may only be used in "
51747 "a loop construct with %<inscan%> %<reduction%> clause");
51748 break;
51750 case PRAGMA_OMP_SECTION:
51751 error_at (pragma_tok->location,
51752 "%<#pragma omp section%> may only be used in "
51753 "%<#pragma omp sections%> construct");
51754 break;
51756 case PRAGMA_IVDEP:
51757 case PRAGMA_UNROLL:
51758 case PRAGMA_NOVECTOR:
51760 bool ivdep = false;
51761 tree unroll = NULL_TREE;
51762 bool novector = false;
51763 const char *pragma_str;
51765 switch (id)
51767 case PRAGMA_IVDEP:
51768 pragma_str = "ivdep";
51769 break;
51770 case PRAGMA_UNROLL:
51771 pragma_str = "unroll";
51772 break;
51773 case PRAGMA_NOVECTOR:
51774 pragma_str = "novector";
51775 break;
51776 default:
51777 gcc_unreachable ();
51780 if (context == pragma_external)
51782 error_at (pragma_tok->location,
51783 "%<#pragma GCC %s%> must be inside a function",
51784 pragma_str);
51785 break;
51788 cp_token *tok = pragma_tok;
51789 bool has_more = true;
51792 switch (cp_parser_pragma_kind (tok))
51794 case PRAGMA_IVDEP:
51796 if (tok != pragma_tok)
51797 tok = cp_lexer_consume_token (parser->lexer);
51798 ivdep = cp_parser_pragma_ivdep (parser, tok);
51799 break;
51801 case PRAGMA_UNROLL:
51803 if (tok != pragma_tok)
51804 tok = cp_lexer_consume_token (parser->lexer);
51805 unroll = cp_parser_pragma_unroll (parser, tok);
51806 break;
51808 case PRAGMA_NOVECTOR:
51810 if (tok != pragma_tok)
51811 tok = cp_lexer_consume_token (parser->lexer);
51812 novector = cp_parser_pragma_novector (parser, tok);
51813 break;
51815 default:
51816 has_more = false;
51817 break;
51819 tok = cp_lexer_peek_token (the_parser->lexer);
51820 has_more = has_more && tok->type == CPP_PRAGMA;
51822 while (has_more);
51824 if (tok->type != CPP_KEYWORD
51825 || (tok->keyword != RID_FOR
51826 && tok->keyword != RID_WHILE
51827 && tok->keyword != RID_DO))
51829 cp_parser_error (parser, "for, while or do statement expected");
51830 return false;
51832 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
51833 return true;
51836 default:
51837 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
51838 c_invoke_pragma_handler (id);
51839 break;
51841 bad_stmt:
51842 cp_parser_error (parser, "expected declaration specifiers");
51843 break;
51846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51847 return ret;
51850 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
51851 populated the lexer with any tokens (the tokens rather being read by
51852 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
51853 a pragma. */
51854 static void
51855 maybe_read_tokens_for_pragma_lex ()
51857 const auto lexer = the_parser->lexer;
51858 if (!lexer->buffer->is_empty ())
51859 return;
51861 /* Read the rest of the tokens comprising the pragma line. */
51862 cp_token *tok;
51865 tok = vec_safe_push (lexer->buffer, cp_token ());
51866 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
51867 gcc_assert (tok->type != CPP_EOF);
51868 } while (tok->type != CPP_PRAGMA_EOL);
51869 lexer->next_token = lexer->buffer->address ();
51870 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
51873 /* The interface the pragma parsers have to the lexer. */
51875 enum cpp_ttype
51876 pragma_lex (tree *value, location_t *loc)
51878 if (flag_preprocess_only)
51879 maybe_read_tokens_for_pragma_lex ();
51881 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
51882 enum cpp_ttype ret = tok->type;
51884 *value = tok->u.value;
51885 if (loc)
51886 *loc = tok->location;
51888 if (ret == CPP_PRAGMA_EOL)
51889 ret = CPP_EOF;
51890 else if (ret == CPP_STRING)
51891 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
51892 /*wide_ok=*/false);
51893 else
51895 if (ret == CPP_KEYWORD)
51896 ret = CPP_NAME;
51897 cp_lexer_consume_token (the_parser->lexer);
51900 return ret;
51903 void
51904 pragma_lex_discard_to_eol ()
51906 /* We have already read all the tokens, so we just need to discard
51907 them here. */
51908 const auto lexer = the_parser->lexer;
51909 lexer->next_token = lexer->last_token;
51910 lexer->buffer->truncate (0);
51914 /* External interface. */
51916 /* Parse one entire translation unit. */
51918 void
51919 c_parse_file (void)
51921 static bool already_called = false;
51923 if (already_called)
51924 fatal_error (input_location,
51925 "multi-source compilation not implemented for C++");
51926 already_called = true;
51928 /* cp_lexer_new_main is called before doing any GC allocation
51929 because tokenization might load a PCH file. */
51930 cp_lexer_new_main ();
51932 cp_parser_translation_unit (the_parser);
51933 class_decl_loc_t::diag_mismatched_tags ();
51935 the_parser = NULL;
51937 finish_translation_unit ();
51940 /* Create an identifier for a generic parameter type (a synthesized
51941 template parameter implied by `auto' or a concept identifier). */
51943 static GTY(()) int generic_parm_count;
51944 static tree
51945 make_generic_type_name ()
51947 char buf[32];
51948 sprintf (buf, "auto:%d", ++generic_parm_count);
51949 return get_identifier (buf);
51952 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
51953 (creating a new template parameter list if necessary). Returns the newly
51954 created template type parm. */
51956 static tree
51957 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
51959 /* A requires-clause is not a function and cannot have placeholders. */
51960 if (current_binding_level->requires_expression)
51962 error ("placeholder type not allowed in this context");
51963 return error_mark_node;
51966 gcc_assert (current_binding_level->kind == sk_function_parms);
51968 /* We are either continuing a function template that already contains implicit
51969 template parameters, creating a new fully-implicit function template, or
51970 extending an existing explicit function template with implicit template
51971 parameters. */
51973 cp_binding_level *const entry_scope = current_binding_level;
51975 bool become_template = false;
51976 cp_binding_level *parent_scope = 0;
51978 if (parser->implicit_template_scope)
51980 gcc_assert (parser->implicit_template_parms);
51982 current_binding_level = parser->implicit_template_scope;
51984 else
51986 /* Roll back to the existing template parameter scope (in the case of
51987 extending an explicit function template) or introduce a new template
51988 parameter scope ahead of the function parameter scope (or class scope
51989 in the case of out-of-line member definitions). The function scope is
51990 added back after template parameter synthesis below. */
51992 cp_binding_level *scope = entry_scope;
51994 while (scope->kind == sk_function_parms)
51996 parent_scope = scope;
51997 scope = scope->level_chain;
51999 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
52001 /* If not defining a class, then any class scope is a scope level in
52002 an out-of-line member definition. In this case simply wind back
52003 beyond the first such scope to inject the template parameter list.
52004 Otherwise wind back to the class being defined. The latter can
52005 occur in class member friend declarations such as:
52007 class A {
52008 void foo (auto);
52010 class B {
52011 friend void A::foo (auto);
52014 The template parameter list synthesized for the friend declaration
52015 must be injected in the scope of 'B'. This can also occur in
52016 erroneous cases such as:
52018 struct A {
52019 struct B {
52020 void foo (auto);
52022 void B::foo (auto) {}
52025 Here the attempted definition of 'B::foo' within 'A' is ill-formed
52026 but, nevertheless, the template parameter list synthesized for the
52027 declarator should be injected into the scope of 'A' as if the
52028 ill-formed template was specified explicitly. */
52030 while (scope->kind == sk_class && !scope->defining_class_p)
52032 parent_scope = scope;
52033 scope = scope->level_chain;
52037 current_binding_level = scope;
52039 if (scope->kind != sk_template_parms
52040 || !function_being_declared_is_template_p (parser))
52042 /* Introduce a new template parameter list for implicit template
52043 parameters. */
52045 become_template = true;
52047 parser->implicit_template_scope
52048 = begin_scope (sk_template_parms, NULL);
52050 ++processing_template_decl;
52052 parser->fully_implicit_function_template_p = true;
52053 ++parser->num_template_parameter_lists;
52055 else
52057 /* Synthesize implicit template parameters at the end of the explicit
52058 template parameter list. */
52060 gcc_assert (current_template_parms);
52062 parser->implicit_template_scope = scope;
52064 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
52065 parser->implicit_template_parms
52066 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
52070 /* Synthesize a new template parameter and track the current template
52071 parameter chain with implicit_template_parms. */
52073 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
52074 tree synth_id = make_generic_type_name ();
52075 bool non_type = false;
52077 /* Synthesize the type template parameter. */
52078 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
52079 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
52081 if (become_template)
52082 current_template_parms = tree_cons (size_int (current_template_depth + 1),
52083 NULL_TREE, current_template_parms);
52085 /* Attach the constraint to the parm before processing. */
52086 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
52087 TREE_TYPE (node) = constr;
52088 tree new_parm
52089 = process_template_parm (parser->implicit_template_parms,
52090 input_location,
52091 node,
52092 /*non_type=*/non_type,
52093 /*param_pack=*/false);
52094 // Process_template_parm returns the list of parms, and
52095 // parser->implicit_template_parms holds the final node of the parm
52096 // list. We really want to manipulate the newly appended element.
52097 gcc_checking_assert (!parser->implicit_template_parms
52098 || parser->implicit_template_parms == new_parm);
52099 if (parser->implicit_template_parms)
52100 new_parm = TREE_CHAIN (new_parm);
52101 gcc_checking_assert (!TREE_CHAIN (new_parm));
52103 // Record the last implicit parm node
52104 parser->implicit_template_parms = new_parm;
52106 /* Mark the synthetic declaration "virtual". This is used when
52107 comparing template-heads to determine if whether an abbreviated
52108 function template is equivalent to an explicit template.
52110 Note that DECL_ARTIFICIAL is used elsewhere for template
52111 parameters. */
52112 if (TREE_VALUE (new_parm) != error_mark_node)
52113 DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (new_parm)) = true;
52115 tree new_decl = get_local_decls ();
52116 if (non_type)
52117 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
52118 new_decl = DECL_INITIAL (new_decl);
52120 /* If creating a fully implicit function template, start the new implicit
52121 template parameter list with this synthesized type, otherwise grow the
52122 current template parameter list. */
52124 if (become_template)
52126 parent_scope->level_chain = current_binding_level;
52128 tree new_parms = make_tree_vec (1);
52129 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
52130 TREE_VALUE (current_template_parms) = new_parms;
52132 else
52134 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
52135 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
52136 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
52137 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
52140 /* If the new parameter was constrained, we need to add that to the
52141 constraints in the template parameter list. */
52142 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
52144 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
52145 reqs = combine_constraint_expressions (reqs, req);
52146 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
52149 current_binding_level = entry_scope;
52151 return new_decl;
52154 /* Finish the declaration of a fully implicit function template. Such a
52155 template has no explicit template parameter list so has not been through the
52156 normal template head and tail processing. synthesize_implicit_template_parm
52157 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
52158 provided if the declaration is a class member such that its template
52159 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
52160 form is returned. Otherwise NULL_TREE is returned. */
52162 static tree
52163 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
52165 gcc_assert (parser->fully_implicit_function_template_p);
52167 if (member_decl_opt && member_decl_opt != error_mark_node
52168 && DECL_VIRTUAL_P (member_decl_opt))
52170 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
52171 "implicit templates may not be %<virtual%>");
52172 DECL_VIRTUAL_P (member_decl_opt) = false;
52175 if (member_decl_opt)
52176 member_decl_opt = finish_member_template_decl (member_decl_opt);
52177 end_template_decl ();
52179 parser->fully_implicit_function_template_p = false;
52180 parser->implicit_template_parms = 0;
52181 parser->implicit_template_scope = 0;
52182 --parser->num_template_parameter_lists;
52184 return member_decl_opt;
52187 /* Like finish_fully_implicit_template, but to be used in error
52188 recovery, rearranging scopes so that we restore the state we had
52189 before synthesize_implicit_template_parm inserted the implement
52190 template parms scope. */
52192 static void
52193 abort_fully_implicit_template (cp_parser *parser)
52195 cp_binding_level *return_to_scope = current_binding_level;
52197 if (parser->implicit_template_scope
52198 && return_to_scope != parser->implicit_template_scope)
52200 cp_binding_level *child = return_to_scope;
52201 for (cp_binding_level *scope = child->level_chain;
52202 scope != parser->implicit_template_scope;
52203 scope = child->level_chain)
52204 child = scope;
52205 child->level_chain = parser->implicit_template_scope->level_chain;
52206 parser->implicit_template_scope->level_chain = return_to_scope;
52207 current_binding_level = parser->implicit_template_scope;
52209 else
52210 return_to_scope = return_to_scope->level_chain;
52212 finish_fully_implicit_template (parser, NULL);
52214 gcc_assert (current_binding_level == return_to_scope);
52217 /* Helper function for diagnostics that have complained about things
52218 being used with 'extern "C"' linkage.
52220 Attempt to issue a note showing where the 'extern "C"' linkage began. */
52222 void
52223 maybe_show_extern_c_location (void)
52225 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
52226 inform (the_parser->innermost_linkage_specification_location,
52227 "%<extern \"C\"%> linkage started here");
52230 #include "gt-cp-parser.h"