Daily bump.
[official-gcc.git] / gcc / cp / parser.cc
blobd71522dcd74ddbc1b9192b16225a936a357ae25a
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, "Parsing a declarator",
564 parser->in_declarator_p);
565 cp_debug_print_flag (file, "In template argument list",
566 parser->in_template_argument_list_p);
567 cp_debug_print_flag (file, "Parsing an iteration statement",
568 parser->in_statement & IN_ITERATION_STMT);
569 cp_debug_print_flag (file, "Parsing a switch statement",
570 parser->in_statement & IN_SWITCH_STMT);
571 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
572 parser->in_statement & IN_OMP_BLOCK);
573 cp_debug_print_flag (file, "Parsing an OpenMP loop",
574 parser->in_statement & IN_OMP_FOR);
575 cp_debug_print_flag (file, "Parsing an if statement",
576 parser->in_statement & IN_IF_STMT);
577 cp_debug_print_flag (file, "Parsing a type-id in an expression "
578 "context", parser->in_type_id_in_expr_p);
579 cp_debug_print_flag (file, "String expressions should be translated "
580 "to execution character set",
581 parser->translate_strings_p);
582 cp_debug_print_flag (file, "Parsing function body outside of a "
583 "local class", parser->in_function_body);
584 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
585 parser->colon_corrects_to_scope_p);
586 cp_debug_print_flag (file, "Colon doesn't start a class definition",
587 parser->colon_doesnt_start_class_def_p);
588 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
589 parser->objective_c_message_context_p);
590 if (parser->type_definition_forbidden_message)
591 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
592 parser->type_definition_forbidden_message,
593 parser->type_definition_forbidden_message_arg
594 ? parser->type_definition_forbidden_message_arg : "<none>");
595 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
596 fprintf (file, "Number of class definitions in progress: %u\n",
597 parser->num_classes_being_defined);
598 fprintf (file, "Number of template parameter lists for the current "
599 "declaration: %u\n", parser->num_template_parameter_lists);
600 cp_debug_parser_tokens (file, parser, window_size);
601 token = parser->lexer->next_token;
602 fprintf (file, "Next token to parse:\n");
603 fprintf (file, "\tToken: ");
604 cp_lexer_print_token (file, token);
605 eloc = expand_location (token->location);
606 fprintf (file, "\n\tFile: %s\n", eloc.file);
607 fprintf (file, "\tLine: %d\n", eloc.line);
608 fprintf (file, "\tColumn: %d\n", eloc.column);
611 DEBUG_FUNCTION void
612 debug (cp_parser &ref)
614 cp_debug_parser (stderr, &ref);
617 DEBUG_FUNCTION void
618 debug (cp_parser *ptr)
620 if (ptr)
621 debug (*ptr);
622 else
623 fprintf (stderr, "<nil>\n");
626 /* Allocate memory for a new lexer object and return it. */
628 static cp_lexer *
629 cp_lexer_alloc (void)
631 /* Allocate the memory. */
632 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
634 /* Initially we are not debugging. */
635 lexer->debugging_p = false;
637 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
639 /* Create the buffer. */
640 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
642 return lexer;
645 /* Return TRUE if token is the start of a module declaration that will be
646 terminated by a CPP_PRAGMA_EOL token. */
647 static inline bool
648 cp_token_is_module_directive (cp_token *token)
650 return token->keyword == RID__EXPORT
651 || token->keyword == RID__MODULE
652 || token->keyword == RID__IMPORT;
655 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
656 PRAGMA_NONE. */
658 static enum pragma_kind
659 cp_parser_pragma_kind (cp_token *token)
661 if (token->type != CPP_PRAGMA)
662 return PRAGMA_NONE;
663 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
664 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
667 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
668 during preprocessing for the case of preprocessing-related diagnostics. This
669 is called immediately after pushing the CPP_PRAGMA_EOL token onto
670 lexer->buffer. */
672 static void
673 cp_lexer_handle_early_pragma (cp_lexer *lexer)
675 const auto first_token = lexer->buffer->address ();
676 const auto last_token = first_token + lexer->buffer->length () - 1;
678 /* Back up to the start of the pragma so pragma_lex () can parse it when
679 c-pragma lib asks it to. */
680 auto begin = last_token;
681 gcc_assert (begin->type == CPP_PRAGMA_EOL);
682 while (begin->type != CPP_PRAGMA)
684 if (cp_token_is_module_directive (begin))
685 return;
686 gcc_assert (begin != first_token);
687 --begin;
689 gcc_assert (!lexer->next_token);
690 gcc_assert (!lexer->last_token);
691 lexer->next_token = begin;
692 lexer->last_token = last_token;
694 /* Dispatch it. */
695 const unsigned int id
696 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
697 if (id >= PRAGMA_FIRST_EXTERNAL)
698 c_invoke_early_pragma_handler (id);
700 /* Reset to normal state. */
701 lexer->next_token = lexer->last_token = nullptr;
704 /* The parser. */
705 static cp_parser *cp_parser_new (cp_lexer *);
706 static GTY (()) cp_parser *the_parser;
708 /* Create a new main C++ lexer, the lexer that gets tokens from the
709 preprocessor, and also create the main parser. */
711 static cp_lexer *
712 cp_lexer_new_main (void)
714 cp_token token;
716 /* It's possible that parsing the first pragma will load a PCH file,
717 which is a GC collection point. So we have to do that before
718 allocating any memory. */
719 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
720 cp_parser_initial_pragma (&token);
721 c_common_no_more_pch ();
723 cp_lexer *lexer = cp_lexer_alloc ();
724 /* Put the first token in the buffer. */
725 cp_token *tok = lexer->buffer->quick_push (token);
727 uintptr_t filter = 0;
728 if (modules_p ())
729 filter = module_token_cdtor (parse_in, filter);
731 /* Create the parser now, so we can use it to handle early pragmas. */
732 gcc_assert (!the_parser);
733 the_parser = cp_parser_new (lexer);
735 /* Get the remaining tokens from the preprocessor. */
736 while (tok->type != CPP_EOF)
738 if (filter)
739 /* Process the previous token. */
740 module_token_lang (tok->type, tok->keyword, tok->u.value,
741 tok->location, filter);
743 /* Check for early pragmas that need to be handled now. */
744 if (tok->type == CPP_PRAGMA_EOL)
745 cp_lexer_handle_early_pragma (lexer);
747 tok = vec_safe_push (lexer->buffer, cp_token ());
748 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
751 lexer->next_token = lexer->buffer->address ();
752 lexer->last_token = lexer->next_token
753 + lexer->buffer->length ()
754 - 1;
756 if (lexer->buffer->length () != 1)
758 /* Set the EOF token's location to be the just after the previous
759 token's range. That way 'at-eof' diagnostics point at something
760 meaninful. */
761 auto range = get_range_from_loc (line_table, tok[-1].location);
762 tok[0].location
763 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
766 if (filter)
767 module_token_cdtor (parse_in, filter);
769 /* Subsequent preprocessor diagnostics should use compiler
770 diagnostic functions to get the compiler source location. */
771 override_libcpp_locations = true;
773 maybe_check_all_macros (parse_in);
775 /* If we processed any #pragma GCC target directives, we handled them early so
776 any macros they defined would be effective during preprocessing. Now, we
777 need to reset to the default state to begin compilation, and we will
778 process them again at the correct time as needed. */
779 c_reset_target_pragmas ();
781 gcc_assert (!lexer->next_token->purged_p);
782 return lexer;
785 /* Create a lexer and parser to be used during preprocess-only mode.
786 This will be filled with tokens to parse when needed by pragma_lex (). */
787 void
788 c_init_preprocess ()
790 gcc_assert (!the_parser);
791 the_parser = cp_parser_new (cp_lexer_alloc ());
794 /* Create a new lexer whose token stream is primed with the tokens in
795 CACHE. When these tokens are exhausted, no new tokens will be read. */
797 static cp_lexer *
798 cp_lexer_new_from_tokens (cp_token_cache *cache)
800 cp_token *first = cache->first;
801 cp_token *last = cache->last;
802 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
804 /* We do not own the buffer. */
805 lexer->buffer = NULL;
807 /* Insert an EOF token. */
808 lexer->saved_type = last->type;
809 lexer->saved_keyword = last->keyword;
810 last->type = CPP_EOF;
811 last->keyword = RID_MAX;
813 lexer->next_token = first;
814 lexer->last_token = last;
816 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
818 /* Initially we are not debugging. */
819 lexer->debugging_p = false;
821 gcc_assert (!lexer->next_token->purged_p
822 && !lexer->last_token->purged_p);
823 return lexer;
826 /* Frees all resources associated with LEXER. */
828 static void
829 cp_lexer_destroy (cp_lexer *lexer)
831 if (lexer->buffer)
832 vec_free (lexer->buffer);
833 else
835 /* Restore the token we overwrite with EOF. */
836 lexer->last_token->type = lexer->saved_type;
837 lexer->last_token->keyword = lexer->saved_keyword;
839 lexer->saved_tokens.release ();
840 ggc_free (lexer);
843 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
844 be used. The point of this flag is to help the compiler to fold away calls
845 to cp_lexer_debugging_p within this source file at compile time, when the
846 lexer is not being debugged. */
848 #define LEXER_DEBUGGING_ENABLED_P false
850 /* Returns nonzero if debugging information should be output. */
852 static inline bool
853 cp_lexer_debugging_p (cp_lexer *lexer)
855 if (!LEXER_DEBUGGING_ENABLED_P)
856 return false;
858 return lexer->debugging_p;
862 static inline cp_token_position
863 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
865 return lexer->next_token - previous_p;
868 static inline cp_token *
869 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
871 return pos;
874 static inline void
875 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
877 lexer->next_token = cp_lexer_token_at (lexer, pos);
880 static inline cp_token_position
881 cp_lexer_previous_token_position (cp_lexer *lexer)
883 return cp_lexer_token_position (lexer, true);
886 static inline cp_token *
887 cp_lexer_previous_token (cp_lexer *lexer)
889 cp_token_position tp = cp_lexer_previous_token_position (lexer);
891 /* Skip past purged tokens. */
892 while (tp->purged_p)
894 gcc_assert (tp != vec_safe_address (lexer->buffer));
895 tp--;
898 return cp_lexer_token_at (lexer, tp);
901 /* Same as above, but return NULL when the lexer doesn't own the token
902 buffer or if the next_token is at the start of the token
903 vector or if all previous tokens are purged. */
905 static cp_token *
906 cp_lexer_safe_previous_token (cp_lexer *lexer)
908 if (lexer->buffer
909 && lexer->next_token != lexer->buffer->address ())
911 cp_token_position tp = cp_lexer_previous_token_position (lexer);
913 /* Skip past purged tokens. */
914 while (tp->purged_p)
916 if (tp == lexer->buffer->address ())
917 return NULL;
918 tp--;
920 return cp_lexer_token_at (lexer, tp);
923 return NULL;
926 /* Overload for make_location, taking the lexer to mean the location of the
927 previous token. */
929 static inline location_t
930 make_location (location_t caret, location_t start, cp_lexer *lexer)
932 cp_token *t = cp_lexer_previous_token (lexer);
933 return make_location (caret, start, t->location);
936 /* Overload for make_location taking tokens instead of locations. */
938 static inline location_t
939 make_location (cp_token *caret, cp_token *start, cp_token *end)
941 return make_location (caret->location, start->location, end->location);
944 /* nonzero if we are presently saving tokens. */
946 static inline int
947 cp_lexer_saving_tokens (const cp_lexer* lexer)
949 return lexer->saved_tokens.length () != 0;
952 /* Store the next token from the preprocessor in *TOKEN. Return true
953 if we reach EOF. If LEXER is NULL, assume we are handling an
954 initial #pragma pch_preprocess, and thus want the lexer to return
955 processed strings.
957 Diagnostics issued from this function must have their controlling option (if
958 any) in c.opt annotated as a libcpp option via the CppReason property. */
960 static void
961 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
963 static int is_extern_c = 0;
965 /* Get a new token from the preprocessor. */
966 token->type
967 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
968 flags);
969 token->keyword = RID_MAX;
970 token->purged_p = false;
971 token->error_reported = false;
972 token->tree_check_p = false;
973 /* Usually never see a zero, but just in case ... */
974 token->main_source_p = line_table->depth <= 1;
976 /* On some systems, some header files are surrounded by an
977 implicit extern "C" block. Set a flag in the token if it
978 comes from such a header. */
979 is_extern_c += pending_lang_change;
980 pending_lang_change = 0;
981 token->implicit_extern_c = is_extern_c > 0;
983 /* Check to see if this token is a keyword. */
984 if (token->type == CPP_NAME)
986 if (IDENTIFIER_KEYWORD_P (token->u.value))
988 /* Mark this token as a keyword. */
989 token->type = CPP_KEYWORD;
990 /* Record which keyword. */
991 token->keyword = C_RID_CODE (token->u.value);
993 else
995 if (warn_cxx11_compat
996 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
997 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
998 /* These are outside the CXX11 range. */
999 || C_RID_CODE (token->u.value) == RID_ALIGNOF
1000 || C_RID_CODE (token->u.value) == RID_ALIGNAS
1001 || C_RID_CODE (token->u.value)== RID_THREAD))
1003 /* Warn about the C++11 keyword (but still treat it as
1004 an identifier). */
1005 warning_at (token->location, OPT_Wc__11_compat,
1006 "identifier %qE is a keyword in C++11",
1007 token->u.value);
1009 /* Clear out the C_RID_CODE so we don't warn about this
1010 particular identifier-turned-keyword again. */
1011 C_SET_RID_CODE (token->u.value, RID_MAX);
1013 if (warn_cxx20_compat
1014 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1015 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1017 /* Warn about the C++20 keyword (but still treat it as
1018 an identifier). */
1019 warning_at (token->location, OPT_Wc__20_compat,
1020 "identifier %qE is a keyword in C++20",
1021 token->u.value);
1023 /* Clear out the C_RID_CODE so we don't warn about this
1024 particular identifier-turned-keyword again. */
1025 C_SET_RID_CODE (token->u.value, RID_MAX);
1028 token->keyword = RID_MAX;
1031 else if (token->type == CPP_AT_NAME)
1033 /* This only happens in Objective-C++; it must be a keyword. */
1034 token->type = CPP_KEYWORD;
1035 switch (C_RID_CODE (token->u.value))
1037 /* Replace 'class' with '@class', 'private' with '@private',
1038 etc. This prevents confusion with the C++ keyword
1039 'class', and makes the tokens consistent with other
1040 Objective-C 'AT' keywords. For example '@class' is
1041 reported as RID_AT_CLASS which is consistent with
1042 '@synchronized', which is reported as
1043 RID_AT_SYNCHRONIZED.
1045 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1046 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1047 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1048 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1049 case RID_THROW: token->keyword = RID_AT_THROW; break;
1050 case RID_TRY: token->keyword = RID_AT_TRY; break;
1051 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1052 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1053 default: token->keyword = C_RID_CODE (token->u.value);
1058 /* Update the globals input_location and the input file stack from TOKEN. */
1059 static inline void
1060 cp_lexer_set_source_position_from_token (cp_token *token)
1062 input_location = token->location;
1065 /* Update the globals input_location and the input file stack from LEXER. */
1066 static inline void
1067 cp_lexer_set_source_position (cp_lexer *lexer)
1069 cp_token *token = cp_lexer_peek_token (lexer);
1070 cp_lexer_set_source_position_from_token (token);
1073 /* Return a pointer to the next token in the token stream, but do not
1074 consume it. */
1076 static inline cp_token *
1077 cp_lexer_peek_token (cp_lexer *lexer)
1079 if (cp_lexer_debugging_p (lexer))
1081 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1082 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1083 putc ('\n', cp_lexer_debug_stream);
1085 return lexer->next_token;
1088 /* Return true if the next token has the indicated TYPE. */
1090 static inline bool
1091 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1093 return cp_lexer_peek_token (lexer)->type == type;
1096 /* Return true if the next token does not have the indicated TYPE. */
1098 static inline bool
1099 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1101 return !cp_lexer_next_token_is (lexer, type);
1104 /* Return true if the next token is the indicated KEYWORD. */
1106 static inline bool
1107 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1109 return cp_lexer_peek_token (lexer)->keyword == keyword;
1112 static inline bool
1113 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1115 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1118 static inline bool
1119 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1121 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1124 /* Return true if KEYWORD can start a decl-specifier. */
1126 bool
1127 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1129 switch (keyword)
1131 /* auto specifier: storage-class-specifier in C++,
1132 simple-type-specifier in C++0x. */
1133 case RID_AUTO:
1134 /* Storage classes. */
1135 case RID_REGISTER:
1136 case RID_STATIC:
1137 case RID_EXTERN:
1138 case RID_MUTABLE:
1139 case RID_THREAD:
1140 /* Elaborated type specifiers. */
1141 case RID_ENUM:
1142 case RID_CLASS:
1143 case RID_STRUCT:
1144 case RID_UNION:
1145 case RID_TYPENAME:
1146 /* Simple type specifiers. */
1147 case RID_CHAR:
1148 case RID_CHAR8:
1149 case RID_CHAR16:
1150 case RID_CHAR32:
1151 case RID_WCHAR:
1152 case RID_BOOL:
1153 case RID_SHORT:
1154 case RID_INT:
1155 case RID_LONG:
1156 case RID_SIGNED:
1157 case RID_UNSIGNED:
1158 case RID_FLOAT:
1159 case RID_DOUBLE:
1160 CASE_RID_FLOATN_NX:
1161 case RID_VOID:
1162 /* CV qualifiers. */
1163 case RID_CONST:
1164 case RID_VOLATILE:
1165 /* Function specifiers. */
1166 case RID_EXPLICIT:
1167 case RID_VIRTUAL:
1168 /* friend/typdef/inline specifiers. */
1169 case RID_FRIEND:
1170 case RID_TYPEDEF:
1171 case RID_INLINE:
1172 /* GNU extensions. */
1173 case RID_TYPEOF:
1174 /* C++11 extensions. */
1175 case RID_DECLTYPE:
1176 case RID_CONSTEXPR:
1177 /* C++20 extensions. */
1178 case RID_CONSTINIT:
1179 case RID_CONSTEVAL:
1180 return true;
1182 default:
1183 if (keyword >= RID_FIRST_INT_N
1184 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1185 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1186 return true;
1187 return false;
1191 /* Peeks the corresponding built-in trait if the first token is
1192 a built-in trait and the second token is either `(' or `<' depending
1193 on the trait. Otherwise, returns nullptr. */
1195 static const cp_trait *
1196 cp_lexer_peek_trait (cp_lexer *lexer)
1198 const cp_token *token1 = cp_lexer_peek_token (lexer);
1199 if (token1->type == CPP_NAME && IDENTIFIER_TRAIT_P (token1->u.value))
1201 const cp_trait &trait = cp_traits[IDENTIFIER_CP_INDEX (token1->u.value)];
1202 const bool is_pack_element = (trait.kind == CPTK_TYPE_PACK_ELEMENT);
1204 /* Check if the subsequent token is a `<' token to
1205 __type_pack_element or is a `(' token to everything else. */
1206 const cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
1207 if (is_pack_element && token2->type != CPP_LESS)
1208 return nullptr;
1209 if (!is_pack_element && token2->type != CPP_OPEN_PAREN)
1210 return nullptr;
1212 return &trait;
1214 return nullptr;
1217 /* Similarly, but only if the token is an expression-yielding
1218 built-in trait. */
1220 static const cp_trait *
1221 cp_lexer_peek_trait_expr (cp_lexer *lexer)
1223 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1224 if (trait && !trait->type)
1225 return trait;
1227 return nullptr;
1230 /* Similarly, but only if the token is a type-yielding
1231 built-in trait. */
1233 static const cp_trait *
1234 cp_lexer_peek_trait_type (cp_lexer *lexer)
1236 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1237 if (trait && trait->type)
1238 return trait;
1240 return nullptr;
1243 /* Return true if the next token is a keyword for a decl-specifier. */
1245 static bool
1246 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1248 cp_token *token;
1250 if (cp_lexer_peek_trait_type (lexer))
1251 return true;
1253 token = cp_lexer_peek_token (lexer);
1254 return cp_keyword_starts_decl_specifier_p (token->keyword);
1257 /* Returns TRUE iff the token T begins a decltype type. */
1259 static bool
1260 token_is_decltype (cp_token *t)
1262 return (t->keyword == RID_DECLTYPE
1263 || t->type == CPP_DECLTYPE);
1266 /* Returns TRUE iff the next token begins a decltype type. */
1268 static bool
1269 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1271 cp_token *t = cp_lexer_peek_token (lexer);
1272 return token_is_decltype (t);
1275 /* Called when processing a token with tree_check_value; perform or defer the
1276 associated checks and return the value. */
1278 static tree
1279 saved_checks_value (struct tree_check *check_value)
1281 /* Perform any access checks that were deferred. */
1282 vec<deferred_access_check, va_gc> *checks;
1283 deferred_access_check *chk;
1284 checks = check_value->checks;
1285 if (checks)
1287 int i;
1288 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1289 perform_or_defer_access_check (chk->binfo,
1290 chk->decl,
1291 chk->diag_decl, tf_warning_or_error);
1293 /* Return the stored value. */
1294 return check_value->value;
1297 /* Return a pointer to the Nth token in the token stream. If N is 1,
1298 then this is precisely equivalent to cp_lexer_peek_token (except
1299 that it is not inline). One would like to disallow that case, but
1300 there is one case (cp_parser_nth_token_starts_template_id) where
1301 the caller passes a variable for N and it might be 1. */
1303 static cp_token *
1304 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1306 cp_token *token;
1308 /* N is 1-based, not zero-based. */
1309 gcc_assert (n > 0);
1311 if (cp_lexer_debugging_p (lexer))
1312 fprintf (cp_lexer_debug_stream,
1313 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1315 --n;
1316 token = lexer->next_token;
1317 while (n && token->type != CPP_EOF)
1319 ++token;
1320 if (!token->purged_p)
1321 --n;
1324 if (cp_lexer_debugging_p (lexer))
1326 cp_lexer_print_token (cp_lexer_debug_stream, token);
1327 putc ('\n', cp_lexer_debug_stream);
1330 return token;
1333 /* Return the next token, and advance the lexer's next_token pointer
1334 to point to the next non-purged token. */
1336 static cp_token *
1337 cp_lexer_consume_token (cp_lexer* lexer)
1339 cp_token *token = lexer->next_token;
1343 gcc_assert (token->type != CPP_EOF);
1344 lexer->next_token++;
1346 while (lexer->next_token->purged_p);
1348 cp_lexer_set_source_position_from_token (token);
1350 /* Provide debugging output. */
1351 if (cp_lexer_debugging_p (lexer))
1353 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1354 cp_lexer_print_token (cp_lexer_debug_stream, token);
1355 putc ('\n', cp_lexer_debug_stream);
1358 return token;
1361 /* Permanently remove the next token from the token stream, and
1362 advance the next_token pointer to refer to the next non-purged
1363 token. */
1365 static void
1366 cp_lexer_purge_token (cp_lexer *lexer)
1368 cp_token *tok = lexer->next_token;
1370 gcc_assert (tok->type != CPP_EOF);
1371 tok->purged_p = true;
1372 tok->location = UNKNOWN_LOCATION;
1373 tok->u.value = NULL_TREE;
1374 tok->keyword = RID_MAX;
1377 tok++;
1378 while (tok->purged_p);
1379 lexer->next_token = tok;
1382 /* Permanently remove all tokens after TOK, up to, but not
1383 including, the token that will be returned next by
1384 cp_lexer_peek_token. */
1386 static void
1387 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1389 cp_token *peek = lexer->next_token;
1391 gcc_assert (tok < peek);
1393 for (tok++; tok != peek; tok++)
1395 tok->purged_p = true;
1396 tok->location = UNKNOWN_LOCATION;
1397 tok->u.value = NULL_TREE;
1398 tok->keyword = RID_MAX;
1402 /* Begin saving tokens. All tokens consumed after this point will be
1403 preserved. */
1405 static void
1406 cp_lexer_save_tokens (cp_lexer* lexer)
1408 /* Provide debugging output. */
1409 if (cp_lexer_debugging_p (lexer))
1410 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1412 lexer->saved_tokens.safe_push (lexer->next_token);
1415 /* Commit to the portion of the token stream most recently saved. */
1417 static void
1418 cp_lexer_commit_tokens (cp_lexer* lexer)
1420 /* Provide debugging output. */
1421 if (cp_lexer_debugging_p (lexer))
1422 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1424 lexer->saved_tokens.pop ();
1427 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1428 to the token stream. Stop saving tokens. */
1430 static void
1431 cp_lexer_rollback_tokens (cp_lexer* lexer)
1433 /* Provide debugging output. */
1434 if (cp_lexer_debugging_p (lexer))
1435 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1437 lexer->next_token = lexer->saved_tokens.pop ();
1440 /* Determines what saved_token_sentinel does when going out of scope. */
1442 enum saved_token_sentinel_mode {
1443 STS_COMMIT,
1444 STS_ROLLBACK,
1445 STS_DONOTHING
1448 /* RAII wrapper around the above functions, with sanity checking (the token
1449 stream should be the same at the point of instantiation as it is at the
1450 point of destruction).
1452 Creating a variable saves tokens. MODE determines what happens when the
1453 object is destroyed. STS_COMMIT commits tokens (default),
1454 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1455 rollback() will immediately roll-back tokens and set MODE to
1456 STS_DONOTHING. */
1458 struct saved_token_sentinel
1460 cp_lexer *lexer;
1461 unsigned len;
1462 saved_token_sentinel_mode mode;
1463 saved_token_sentinel (cp_lexer *_lexer,
1464 saved_token_sentinel_mode _mode = STS_COMMIT)
1465 : lexer (_lexer), mode (_mode)
1467 len = lexer->saved_tokens.length ();
1468 cp_lexer_save_tokens (lexer);
1470 void rollback ()
1472 cp_lexer_rollback_tokens (lexer);
1473 cp_lexer_set_source_position_from_token
1474 (cp_lexer_previous_token (lexer));
1475 mode = STS_DONOTHING;
1477 ~saved_token_sentinel ()
1479 if (mode == STS_COMMIT)
1480 cp_lexer_commit_tokens (lexer);
1481 else if (mode == STS_ROLLBACK)
1482 rollback ();
1484 gcc_assert (lexer->saved_tokens.length () == len);
1488 /* Print a representation of the TOKEN on the STREAM. */
1490 static void
1491 cp_lexer_print_token (FILE * stream, cp_token *token)
1493 /* We don't use cpp_type2name here because the parser defines
1494 a few tokens of its own. */
1495 static const char *const token_names[] = {
1496 /* cpplib-defined token types */
1497 #define OP(e, s) #e,
1498 #define TK(e, s) #e,
1499 TTYPE_TABLE
1500 #undef OP
1501 #undef TK
1502 /* C++ parser token types - see "Manifest constants", above. */
1503 "KEYWORD",
1504 "TEMPLATE_ID",
1505 "NESTED_NAME_SPECIFIER",
1508 /* For some tokens, print the associated data. */
1509 switch (token->type)
1511 case CPP_KEYWORD:
1512 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1513 For example, `struct' is mapped to an INTEGER_CST. */
1514 if (!identifier_p (token->u.value))
1515 break;
1516 /* fall through */
1517 case CPP_NAME:
1518 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1519 break;
1521 case CPP_STRING:
1522 case CPP_STRING16:
1523 case CPP_STRING32:
1524 case CPP_WSTRING:
1525 case CPP_UTF8STRING:
1526 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1527 break;
1529 case CPP_NUMBER:
1530 print_generic_expr (stream, token->u.value);
1531 break;
1533 default:
1534 /* If we have a name for the token, print it out. Otherwise, we
1535 simply give the numeric code. */
1536 if (token->type < ARRAY_SIZE(token_names))
1537 fputs (token_names[token->type], stream);
1538 else
1539 fprintf (stream, "[%d]", token->type);
1540 break;
1544 DEBUG_FUNCTION void
1545 debug (cp_token &ref)
1547 cp_lexer_print_token (stderr, &ref);
1548 fprintf (stderr, "\n");
1551 DEBUG_FUNCTION void
1552 debug (cp_token *ptr)
1554 if (ptr)
1555 debug (*ptr);
1556 else
1557 fprintf (stderr, "<nil>\n");
1561 /* Start emitting debugging information. */
1563 static void
1564 cp_lexer_start_debugging (cp_lexer* lexer)
1566 if (!LEXER_DEBUGGING_ENABLED_P)
1567 fatal_error (input_location,
1568 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1570 lexer->debugging_p = true;
1571 cp_lexer_debug_stream = stderr;
1574 /* Stop emitting debugging information. */
1576 static void
1577 cp_lexer_stop_debugging (cp_lexer* lexer)
1579 if (!LEXER_DEBUGGING_ENABLED_P)
1580 fatal_error (input_location,
1581 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1583 lexer->debugging_p = false;
1584 cp_lexer_debug_stream = NULL;
1587 /* Create a new cp_token_cache, representing a range of tokens. */
1589 static cp_token_cache *
1590 cp_token_cache_new (cp_token *first, cp_token *last)
1592 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1593 cache->first = first;
1594 cache->last = last;
1595 return cache;
1598 /* Diagnose if #pragma omp declare simd isn't followed immediately
1599 by function declaration or definition. */
1601 static inline void
1602 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1604 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1606 error ("%<#pragma omp declare %s%> not immediately followed by "
1607 "function declaration or definition",
1608 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1609 parser->omp_declare_simd = NULL;
1613 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1614 and put that into "omp declare simd" attribute. */
1616 static inline void
1617 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1619 if (UNLIKELY (parser->omp_declare_simd != NULL))
1621 if (fndecl == error_mark_node)
1623 parser->omp_declare_simd = NULL;
1624 return;
1626 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1628 cp_ensure_no_omp_declare_simd (parser);
1629 return;
1634 /* Similarly, but for use in declaration parsing functions
1635 which call cp_parser_handle_directive_omp_attributes. */
1637 static inline void
1638 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1640 if (parser->omp_declare_simd != data)
1641 return;
1643 if (!parser->omp_declare_simd->error_seen
1644 && !parser->omp_declare_simd->fndecl_seen)
1645 error_at (parser->omp_declare_simd->loc,
1646 "%<declare %s%> directive not immediately followed by "
1647 "function declaration or definition",
1648 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1649 parser->omp_declare_simd = NULL;
1652 /* Diagnose if #pragma acc routine isn't followed immediately by function
1653 declaration or definition. */
1655 static inline void
1656 cp_ensure_no_oacc_routine (cp_parser *parser)
1658 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1660 error_at (parser->oacc_routine->loc,
1661 "%<#pragma acc routine%> not immediately followed by "
1662 "function declaration or definition");
1663 parser->oacc_routine = NULL;
1667 /* Decl-specifiers. */
1669 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1671 static void
1672 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1674 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1677 /* Declarators. */
1679 /* Nothing other than the parser should be creating declarators;
1680 declarators are a semi-syntactic representation of C++ entities.
1681 Other parts of the front end that need to create entities (like
1682 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1684 static cp_declarator *make_call_declarator
1685 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1686 tree, tree, tree, tree, tree, location_t);
1687 static cp_declarator *make_array_declarator
1688 (cp_declarator *, tree);
1689 static cp_declarator *make_pointer_declarator
1690 (cp_cv_quals, cp_declarator *, tree);
1691 static cp_declarator *make_reference_declarator
1692 (cp_cv_quals, cp_declarator *, bool, tree);
1693 static cp_declarator *make_ptrmem_declarator
1694 (cp_cv_quals, tree, cp_declarator *, tree);
1696 /* An erroneous declarator. */
1697 static cp_declarator *cp_error_declarator;
1699 /* The obstack on which declarators and related data structures are
1700 allocated. */
1701 static struct obstack declarator_obstack;
1703 /* Alloc BYTES from the declarator memory pool. */
1705 static inline void *
1706 alloc_declarator (size_t bytes)
1708 return obstack_alloc (&declarator_obstack, bytes);
1711 /* Allocate a declarator of the indicated KIND. Clear fields that are
1712 common to all declarators. */
1714 static cp_declarator *
1715 make_declarator (cp_declarator_kind kind)
1717 cp_declarator *declarator;
1719 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1720 declarator->kind = kind;
1721 declarator->parenthesized = UNKNOWN_LOCATION;
1722 declarator->attributes = NULL_TREE;
1723 declarator->std_attributes = NULL_TREE;
1724 declarator->declarator = NULL;
1725 declarator->parameter_pack_p = false;
1726 declarator->id_loc = UNKNOWN_LOCATION;
1727 declarator->init_loc = UNKNOWN_LOCATION;
1729 return declarator;
1732 /* Make a declarator for a generalized identifier. If
1733 QUALIFYING_SCOPE is non-NULL, the identifier is
1734 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1735 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1736 is, if any. */
1738 static cp_declarator *
1739 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1740 special_function_kind sfk, location_t id_location)
1742 cp_declarator *declarator;
1744 /* It is valid to write:
1746 class C { void f(); };
1747 typedef C D;
1748 void D::f();
1750 The standard is not clear about whether `typedef const C D' is
1751 legal; as of 2002-09-15 the committee is considering that
1752 question. EDG 3.0 allows that syntax. Therefore, we do as
1753 well. */
1754 if (qualifying_scope && TYPE_P (qualifying_scope))
1755 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1757 gcc_assert (identifier_p (unqualified_name)
1758 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1759 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1761 declarator = make_declarator (cdk_id);
1762 declarator->u.id.qualifying_scope = qualifying_scope;
1763 declarator->u.id.unqualified_name = unqualified_name;
1764 declarator->u.id.sfk = sfk;
1765 declarator->id_loc = id_location;
1767 return declarator;
1770 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1771 of modifiers such as const or volatile to apply to the pointer
1772 type, represented as identifiers. ATTRIBUTES represent the attributes that
1773 appertain to the pointer or reference. */
1775 cp_declarator *
1776 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1777 tree attributes)
1779 cp_declarator *declarator;
1781 declarator = make_declarator (cdk_pointer);
1782 declarator->declarator = target;
1783 declarator->u.pointer.qualifiers = cv_qualifiers;
1784 declarator->u.pointer.class_type = NULL_TREE;
1785 if (target)
1787 declarator->id_loc = target->id_loc;
1788 declarator->parameter_pack_p = target->parameter_pack_p;
1789 target->parameter_pack_p = false;
1791 else
1792 declarator->parameter_pack_p = false;
1794 declarator->std_attributes = attributes;
1796 return declarator;
1799 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1800 represent the attributes that appertain to the pointer or
1801 reference. */
1803 cp_declarator *
1804 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1805 bool rvalue_ref, tree attributes)
1807 cp_declarator *declarator;
1809 declarator = make_declarator (cdk_reference);
1810 declarator->declarator = target;
1811 declarator->u.reference.qualifiers = cv_qualifiers;
1812 declarator->u.reference.rvalue_ref = rvalue_ref;
1813 if (target)
1815 declarator->id_loc = target->id_loc;
1816 declarator->parameter_pack_p = target->parameter_pack_p;
1817 target->parameter_pack_p = false;
1819 else
1820 declarator->parameter_pack_p = false;
1822 declarator->std_attributes = attributes;
1824 return declarator;
1827 /* Like make_pointer_declarator -- but for a pointer to a non-static
1828 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1829 appertain to the pointer or reference. */
1831 cp_declarator *
1832 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1833 cp_declarator *pointee,
1834 tree attributes)
1836 cp_declarator *declarator;
1838 declarator = make_declarator (cdk_ptrmem);
1839 declarator->declarator = pointee;
1840 declarator->u.pointer.qualifiers = cv_qualifiers;
1841 declarator->u.pointer.class_type = class_type;
1843 if (pointee)
1845 declarator->parameter_pack_p = pointee->parameter_pack_p;
1846 pointee->parameter_pack_p = false;
1848 else
1849 declarator->parameter_pack_p = false;
1851 declarator->std_attributes = attributes;
1853 return declarator;
1856 /* Make a declarator for the function given by TARGET, with the
1857 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1858 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1859 indicates what exceptions can be thrown. STD_ATTRS contains
1860 attributes that appertain to the function type. */
1862 cp_declarator *
1863 make_call_declarator (cp_declarator *target,
1864 tree parms,
1865 cp_cv_quals cv_qualifiers,
1866 cp_virt_specifiers virt_specifiers,
1867 cp_ref_qualifier ref_qualifier,
1868 tree tx_qualifier,
1869 tree exception_specification,
1870 tree late_return_type,
1871 tree requires_clause,
1872 tree std_attrs,
1873 location_t parens_loc)
1875 cp_declarator *declarator;
1877 declarator = make_declarator (cdk_function);
1878 declarator->declarator = target;
1879 declarator->u.function.parameters = parms;
1880 declarator->u.function.qualifiers = cv_qualifiers;
1881 declarator->u.function.virt_specifiers = virt_specifiers;
1882 declarator->u.function.ref_qualifier = ref_qualifier;
1883 declarator->u.function.tx_qualifier = tx_qualifier;
1884 declarator->u.function.exception_specification = exception_specification;
1885 declarator->u.function.late_return_type = late_return_type;
1886 declarator->u.function.requires_clause = requires_clause;
1887 declarator->u.function.parens_loc = parens_loc;
1888 if (target)
1890 declarator->id_loc = target->id_loc;
1891 declarator->parameter_pack_p = target->parameter_pack_p;
1892 target->parameter_pack_p = false;
1894 else
1895 declarator->parameter_pack_p = false;
1897 declarator->std_attributes = std_attrs;
1899 return declarator;
1902 /* Make a declarator for an array of BOUNDS elements, each of which is
1903 defined by ELEMENT. */
1905 cp_declarator *
1906 make_array_declarator (cp_declarator *element, tree bounds)
1908 cp_declarator *declarator;
1910 declarator = make_declarator (cdk_array);
1911 declarator->declarator = element;
1912 declarator->u.array.bounds = bounds;
1913 if (element)
1915 declarator->id_loc = element->id_loc;
1916 declarator->parameter_pack_p = element->parameter_pack_p;
1917 element->parameter_pack_p = false;
1919 else
1920 declarator->parameter_pack_p = false;
1922 return declarator;
1925 /* Determine whether the declarator we've seen so far can be a
1926 parameter pack, when followed by an ellipsis. */
1927 static bool
1928 declarator_can_be_parameter_pack (cp_declarator *declarator)
1930 if (declarator && declarator->parameter_pack_p)
1931 /* We already saw an ellipsis. */
1932 return false;
1934 /* Search for a declarator name, or any other declarator that goes
1935 after the point where the ellipsis could appear in a parameter
1936 pack. If we find any of these, then this declarator cannot be
1937 made into a parameter pack. */
1938 bool found = false;
1939 while (declarator && !found)
1941 switch ((int)declarator->kind)
1943 case cdk_id:
1944 case cdk_array:
1945 case cdk_decomp:
1946 found = true;
1947 break;
1949 case cdk_error:
1950 return true;
1952 default:
1953 declarator = declarator->declarator;
1954 break;
1958 return !found;
1961 cp_parameter_declarator *no_parameters;
1963 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1964 DECLARATOR and DEFAULT_ARGUMENT. */
1966 cp_parameter_declarator *
1967 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1968 cp_declarator *declarator,
1969 tree default_argument,
1970 location_t loc,
1971 bool template_parameter_pack_p = false)
1973 cp_parameter_declarator *parameter;
1975 parameter = ((cp_parameter_declarator *)
1976 alloc_declarator (sizeof (cp_parameter_declarator)));
1977 parameter->next = NULL;
1978 if (decl_specifiers)
1979 parameter->decl_specifiers = *decl_specifiers;
1980 else
1981 clear_decl_specs (&parameter->decl_specifiers);
1982 parameter->declarator = declarator;
1983 parameter->default_argument = default_argument;
1984 parameter->template_parameter_pack_p = template_parameter_pack_p;
1985 parameter->loc = loc;
1987 return parameter;
1990 /* Returns true iff DECLARATOR is a declaration for a function. */
1992 static bool
1993 function_declarator_p (const cp_declarator *declarator)
1995 while (declarator)
1997 if (declarator->kind == cdk_function
1998 && declarator->declarator->kind == cdk_id)
1999 return true;
2000 if (declarator->kind == cdk_id
2001 || declarator->kind == cdk_decomp
2002 || declarator->kind == cdk_error)
2003 return false;
2004 declarator = declarator->declarator;
2006 return false;
2009 /* The parser. */
2011 /* Overview
2012 --------
2014 A cp_parser parses the token stream as specified by the C++
2015 grammar. Its job is purely parsing, not semantic analysis. For
2016 example, the parser breaks the token stream into declarators,
2017 expressions, statements, and other similar syntactic constructs.
2018 It does not check that the types of the expressions on either side
2019 of an assignment-statement are compatible, or that a function is
2020 not declared with a parameter of type `void'.
2022 The parser invokes routines elsewhere in the compiler to perform
2023 semantic analysis and to build up the abstract syntax tree for the
2024 code processed.
2026 The parser (and the template instantiation code, which is, in a
2027 way, a close relative of parsing) are the only parts of the
2028 compiler that should be calling push_scope and pop_scope, or
2029 related functions. The parser (and template instantiation code)
2030 keeps track of what scope is presently active; everything else
2031 should simply honor that. (The code that generates static
2032 initializers may also need to set the scope, in order to check
2033 access control correctly when emitting the initializers.)
2035 Methodology
2036 -----------
2038 The parser is of the standard recursive-descent variety. Upcoming
2039 tokens in the token stream are examined in order to determine which
2040 production to use when parsing a non-terminal. Some C++ constructs
2041 require arbitrary look ahead to disambiguate. For example, it is
2042 impossible, in the general case, to tell whether a statement is an
2043 expression or declaration without scanning the entire statement.
2044 Therefore, the parser is capable of "parsing tentatively." When the
2045 parser is not sure what construct comes next, it enters this mode.
2046 Then, while we attempt to parse the construct, the parser queues up
2047 error messages, rather than issuing them immediately, and saves the
2048 tokens it consumes. If the construct is parsed successfully, the
2049 parser "commits", i.e., it issues any queued error messages and
2050 the tokens that were being preserved are permanently discarded.
2051 If, however, the construct is not parsed successfully, the parser
2052 rolls back its state completely so that it can resume parsing using
2053 a different alternative.
2055 Future Improvements
2056 -------------------
2058 The performance of the parser could probably be improved substantially.
2059 We could often eliminate the need to parse tentatively by looking ahead
2060 a little bit. In some places, this approach might not entirely eliminate
2061 the need to parse tentatively, but it might still speed up the average
2062 case. */
2064 /* Flags that are passed to some parsing functions. These values can
2065 be bitwise-ored together. */
2067 enum
2069 /* No flags. */
2070 CP_PARSER_FLAGS_NONE = 0x0,
2071 /* The construct is optional. If it is not present, then no error
2072 should be issued. */
2073 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2074 /* When parsing a type-specifier, treat user-defined type-names
2075 as non-type identifiers. */
2076 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2077 /* When parsing a type-specifier, do not try to parse a class-specifier
2078 or enum-specifier. */
2079 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2080 /* When parsing a decl-specifier-seq, only allow type-specifier or
2081 constexpr. */
2082 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2083 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2084 for C++20 consteval or for C++23 static. */
2085 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2086 /* When parsing a decl-specifier-seq, allow missing typename. */
2087 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2088 /* When parsing of the noexcept-specifier should be delayed. */
2089 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2090 /* When parsing a consteval declarator. */
2091 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2094 /* This type is used for parameters and variables which hold
2095 combinations of the above flags. */
2096 typedef int cp_parser_flags;
2098 /* The different kinds of declarators we want to parse. */
2100 enum cp_parser_declarator_kind
2102 /* We want an abstract declarator. */
2103 CP_PARSER_DECLARATOR_ABSTRACT,
2104 /* We want a named declarator. */
2105 CP_PARSER_DECLARATOR_NAMED,
2106 /* We don't mind, but the name must be an unqualified-id. */
2107 CP_PARSER_DECLARATOR_EITHER
2110 /* The precedence values used to parse binary expressions. The minimum value
2111 of PREC must be 1, because zero is reserved to quickly discriminate
2112 binary operators from other tokens. */
2114 enum cp_parser_prec
2116 PREC_NOT_OPERATOR,
2117 PREC_LOGICAL_OR_EXPRESSION,
2118 PREC_LOGICAL_AND_EXPRESSION,
2119 PREC_INCLUSIVE_OR_EXPRESSION,
2120 PREC_EXCLUSIVE_OR_EXPRESSION,
2121 PREC_AND_EXPRESSION,
2122 PREC_EQUALITY_EXPRESSION,
2123 PREC_RELATIONAL_EXPRESSION,
2124 PREC_SPACESHIP_EXPRESSION,
2125 PREC_SHIFT_EXPRESSION,
2126 PREC_ADDITIVE_EXPRESSION,
2127 PREC_MULTIPLICATIVE_EXPRESSION,
2128 PREC_PM_EXPRESSION,
2129 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2132 /* A mapping from a token type to a corresponding tree node type, with a
2133 precedence value. */
2135 struct cp_parser_binary_operations_map_node
2137 /* The token type. */
2138 enum cpp_ttype token_type;
2139 /* The corresponding tree code. */
2140 enum tree_code tree_type;
2141 /* The precedence of this operator. */
2142 enum cp_parser_prec prec;
2145 struct cp_parser_expression_stack_entry
2147 /* Left hand side of the binary operation we are currently
2148 parsing. */
2149 cp_expr lhs;
2150 /* Original tree code for left hand side, if it was a binary
2151 expression itself (used for -Wparentheses). */
2152 enum tree_code lhs_type;
2153 /* Tree code for the binary operation we are parsing. */
2154 enum tree_code tree_type;
2155 /* Precedence of the binary operation we are parsing. */
2156 enum cp_parser_prec prec;
2157 /* Location of the binary operation we are parsing. */
2158 location_t loc;
2159 /* Flags from the operator token. */
2160 unsigned char flags;
2163 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2164 entries because precedence levels on the stack are monotonically
2165 increasing. */
2166 typedef struct cp_parser_expression_stack_entry
2167 cp_parser_expression_stack[NUM_PREC_VALUES];
2169 /* Used for parsing OMP for loops.
2171 Some notes on flags used for context:
2172 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2173 construct, except for the final-loop-body.
2174 The want_nested_loop flag is true if inside a {} sequence where
2175 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2176 but has not yet been seen. It's false when parsing intervening code
2177 statements or their substatements that cannot contain a loop-nest.
2178 The in_intervening_code flag is true when parsing any intervening code,
2179 including substatements, and whether or not want_nested_loop is true.
2181 And, about error handling:
2182 The saw_intervening_code flag is set if the loop is not perfectly
2183 nested, even in the usual case where this is not an error.
2184 perfect_nesting_fail is set if an error has been diagnosed because an
2185 imperfectly-nested loop was found where a perfectly-nested one is
2186 required (we diagnose this only once).
2187 fail is set if any kind of structural error in the loop nest
2188 has been found and diagnosed.
2190 struct omp_for_parse_data {
2191 enum tree_code code;
2192 tree declv, condv, incrv, initv;
2193 tree pre_body;
2194 tree orig_declv;
2195 auto_vec<tree, 4> orig_inits;
2196 int count; /* Expected nesting depth. */
2197 int depth; /* Current nesting depth. */
2198 location_t for_loc;
2199 releasing_vec init_blockv;
2200 releasing_vec body_blockv;
2201 releasing_vec init_placeholderv;
2202 releasing_vec body_placeholderv;
2203 bool ordered : 1;
2204 bool inscan : 1;
2205 bool want_nested_loop : 1;
2206 bool in_intervening_code : 1;
2207 bool saw_intervening_code : 1;
2208 bool perfect_nesting_fail : 1;
2209 bool fail : 1;
2210 tree clauses;
2211 tree *cclauses;
2212 tree ordered_cl;
2215 /* Prototypes. */
2217 /* Constructors and destructors. */
2219 static cp_parser_context *cp_parser_context_new
2220 (cp_parser_context *);
2222 /* Class variables. */
2224 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2226 /* The operator-precedence table used by cp_parser_binary_expression.
2227 Transformed into an associative array (binops_by_token) by
2228 cp_parser_new. */
2230 static const cp_parser_binary_operations_map_node binops[] = {
2231 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2232 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2234 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2235 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2236 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2238 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2239 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2241 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2242 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2244 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2246 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2247 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2248 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2249 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2251 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2252 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2254 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2256 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2258 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2260 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2262 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2265 /* The same as binops, but initialized by cp_parser_new so that
2266 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2267 for speed. */
2268 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2270 /* Constructors and destructors. */
2272 /* Construct a new context. The context below this one on the stack
2273 is given by NEXT. */
2275 static cp_parser_context *
2276 cp_parser_context_new (cp_parser_context* next)
2278 cp_parser_context *context;
2280 /* Allocate the storage. */
2281 if (cp_parser_context_free_list != NULL)
2283 /* Pull the first entry from the free list. */
2284 context = cp_parser_context_free_list;
2285 cp_parser_context_free_list = context->next;
2286 memset (context, 0, sizeof (*context));
2288 else
2289 context = ggc_cleared_alloc<cp_parser_context> ();
2291 /* No errors have occurred yet in this context. */
2292 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2293 /* If this is not the bottommost context, copy information that we
2294 need from the previous context. */
2295 if (next)
2297 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2298 expression, then we are parsing one in this context, too. */
2299 context->object_type = next->object_type;
2300 /* Thread the stack. */
2301 context->next = next;
2304 return context;
2307 /* Managing the unparsed function queues. */
2309 #define unparsed_funs_with_default_args \
2310 parser->unparsed_queues->last ().funs_with_default_args
2311 #define unparsed_funs_with_definitions \
2312 parser->unparsed_queues->last ().funs_with_definitions
2313 #define unparsed_nsdmis \
2314 parser->unparsed_queues->last ().nsdmis
2315 #define unparsed_noexcepts \
2316 parser->unparsed_queues->last ().noexcepts
2317 #define unparsed_contracts \
2318 parser->unparsed_queues->last ().contracts
2320 static void
2321 push_unparsed_function_queues (cp_parser *parser)
2323 cp_unparsed_functions_entry e
2324 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2325 vec_safe_push (parser->unparsed_queues, e);
2328 static void
2329 pop_unparsed_function_queues (cp_parser *parser)
2331 release_tree_vector (unparsed_funs_with_definitions);
2332 parser->unparsed_queues->pop ();
2335 /* Prototypes. */
2337 /* Routines to parse various constructs.
2339 Those that return `tree' will return the error_mark_node (rather
2340 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2341 Sometimes, they will return an ordinary node if error-recovery was
2342 attempted, even though a parse error occurred. So, to check
2343 whether or not a parse error occurred, you should always use
2344 cp_parser_error_occurred. If the construct is optional (indicated
2345 either by an `_opt' in the name of the function that does the
2346 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2347 the construct is not present. */
2349 /* Lexical conventions [gram.lex] */
2351 static tree finish_userdef_string_literal
2352 (tree);
2354 /* Basic concepts [gram.basic] */
2356 static void cp_parser_translation_unit (cp_parser *);
2358 /* Expressions [gram.expr] */
2360 static cp_expr cp_parser_primary_expression
2361 (cp_parser *, bool, bool, bool, cp_id_kind *);
2362 static cp_expr cp_parser_id_expression
2363 (cp_parser *, bool, bool, bool *, bool, bool);
2364 static cp_expr cp_parser_unqualified_id
2365 (cp_parser *, bool, bool, bool, bool);
2366 static tree cp_parser_nested_name_specifier_opt
2367 (cp_parser *, bool, bool, bool, bool, bool = false);
2368 static tree cp_parser_nested_name_specifier
2369 (cp_parser *, bool, bool, bool, bool);
2370 static tree cp_parser_qualifying_entity
2371 (cp_parser *, bool, bool, bool, bool, bool);
2372 static cp_expr cp_parser_postfix_expression
2373 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2374 static tree cp_parser_postfix_open_square_expression
2375 (cp_parser *, tree, bool, bool);
2376 static tree cp_parser_postfix_dot_deref_expression
2377 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2378 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2379 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2380 bool = false);
2381 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2382 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3,
2383 uneval_string_attr = 4 };
2384 static void cp_parser_pseudo_destructor_name
2385 (cp_parser *, tree, tree *, tree *);
2386 static cp_expr cp_parser_unary_expression
2387 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2388 static enum tree_code cp_parser_unary_operator
2389 (cp_token *);
2390 static tree cp_parser_has_attribute_expression
2391 (cp_parser *);
2392 static tree cp_parser_new_expression
2393 (cp_parser *);
2394 static vec<tree, va_gc> *cp_parser_new_placement
2395 (cp_parser *);
2396 static tree cp_parser_new_type_id
2397 (cp_parser *, tree *);
2398 static cp_declarator *cp_parser_new_declarator_opt
2399 (cp_parser *);
2400 static cp_declarator *cp_parser_direct_new_declarator
2401 (cp_parser *);
2402 static vec<tree, va_gc> *cp_parser_new_initializer
2403 (cp_parser *);
2404 static tree cp_parser_delete_expression
2405 (cp_parser *);
2406 static cp_expr cp_parser_cast_expression
2407 (cp_parser *, bool, bool, bool, cp_id_kind *);
2408 static cp_expr cp_parser_binary_expression
2409 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2410 static tree cp_parser_question_colon_clause
2411 (cp_parser *, cp_expr);
2412 static cp_expr cp_parser_conditional_expression (cp_parser *);
2413 static cp_expr cp_parser_assignment_expression
2414 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2415 static enum tree_code cp_parser_assignment_operator_opt
2416 (cp_parser *);
2417 static cp_expr cp_parser_expression
2418 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2419 static cp_expr cp_parser_constant_expression
2420 (cp_parser *, int = 0, bool * = NULL, bool = false);
2421 static cp_expr cp_parser_builtin_offsetof
2422 (cp_parser *);
2423 static cp_expr cp_parser_lambda_expression
2424 (cp_parser *);
2425 static void cp_parser_lambda_introducer
2426 (cp_parser *, tree);
2427 static bool cp_parser_lambda_declarator_opt
2428 (cp_parser *, tree);
2429 static void cp_parser_lambda_body
2430 (cp_parser *, tree);
2432 /* Statements [gram.stmt.stmt] */
2434 static void cp_parser_statement
2435 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2436 static void cp_parser_label_for_labeled_statement
2437 (cp_parser *, tree);
2438 static tree cp_parser_expression_statement
2439 (cp_parser *, tree);
2440 static tree cp_parser_compound_statement
2441 (cp_parser *, tree, int, bool);
2442 static void cp_parser_statement_seq_opt
2443 (cp_parser *, tree);
2444 static tree cp_parser_selection_statement
2445 (cp_parser *, bool *, vec<tree> *);
2446 static tree cp_parser_condition
2447 (cp_parser *);
2448 static tree cp_parser_iteration_statement
2449 (cp_parser *, bool *, bool, tree, bool);
2450 static bool cp_parser_init_statement
2451 (cp_parser *, tree *decl);
2452 static tree cp_parser_for
2453 (cp_parser *, bool, tree, bool);
2454 static tree cp_parser_c_for
2455 (cp_parser *, tree, tree, bool, tree, bool);
2456 static tree cp_parser_range_for
2457 (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
2458 static void do_range_for_auto_deduction
2459 (tree, tree, cp_decomp *);
2460 static tree cp_parser_perform_range_for_lookup
2461 (tree, tree *, tree *);
2462 static tree cp_parser_range_for_member_function
2463 (tree, tree);
2464 static tree cp_parser_jump_statement
2465 (cp_parser *);
2466 static void cp_parser_declaration_statement
2467 (cp_parser *);
2469 static tree cp_parser_implicitly_scoped_statement
2470 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2471 static void cp_parser_already_scoped_statement
2472 (cp_parser *, bool *, const token_indent_info &);
2474 /* State of module-declaration parsing. */
2475 enum module_parse
2477 MP_NOT_MODULE, /* Not a module. */
2479 _MP_UNUSED,
2481 MP_FIRST, /* First declaration of TU. */
2482 MP_GLOBAL, /* Global Module Fragment. */
2484 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2485 MP_PURVIEW, /* Purview of a named module. */
2487 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2488 MP_PRIVATE, /* Private Module Fragment. */
2491 static module_parse cp_parser_module_declaration
2492 (cp_parser *parser, module_parse, bool exporting);
2493 static void cp_parser_import_declaration
2494 (cp_parser *parser, module_parse, bool exporting);
2496 /* Declarations [gram.dcl.dcl] */
2498 static void cp_parser_declaration_seq_opt
2499 (cp_parser *);
2500 static void cp_parser_declaration
2501 (cp_parser *, tree);
2502 static void cp_parser_toplevel_declaration
2503 (cp_parser *);
2504 static void cp_parser_block_declaration
2505 (cp_parser *, bool);
2506 static void cp_parser_simple_declaration
2507 (cp_parser *, bool, tree *);
2508 static void cp_parser_decl_specifier_seq
2509 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2510 static tree cp_parser_storage_class_specifier_opt
2511 (cp_parser *);
2512 static tree cp_parser_function_specifier_opt
2513 (cp_parser *, cp_decl_specifier_seq *);
2514 static tree cp_parser_type_specifier
2515 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2516 int *, bool *);
2517 static tree cp_parser_simple_type_specifier
2518 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2519 static tree cp_parser_placeholder_type_specifier
2520 (cp_parser *, location_t, tree, bool);
2521 static tree cp_parser_type_name
2522 (cp_parser *, bool);
2523 static tree cp_parser_nonclass_name
2524 (cp_parser* parser);
2525 static tree cp_parser_elaborated_type_specifier
2526 (cp_parser *, bool, bool);
2527 static tree cp_parser_enum_specifier
2528 (cp_parser *);
2529 static void cp_parser_enumerator_list
2530 (cp_parser *, tree);
2531 static void cp_parser_enumerator_definition
2532 (cp_parser *, tree);
2533 static tree cp_parser_namespace_name
2534 (cp_parser *);
2535 static void cp_parser_namespace_definition
2536 (cp_parser *);
2537 static void cp_parser_namespace_body
2538 (cp_parser *);
2539 static tree cp_parser_qualified_namespace_specifier
2540 (cp_parser *);
2541 static void cp_parser_namespace_alias_definition
2542 (cp_parser *);
2543 static bool cp_parser_using_declaration
2544 (cp_parser *, bool);
2545 static void cp_parser_using_directive
2546 (cp_parser *);
2547 static void cp_parser_using_enum
2548 (cp_parser *);
2549 static tree cp_parser_alias_declaration
2550 (cp_parser *);
2551 static void cp_parser_asm_definition
2552 (cp_parser *);
2553 static void cp_parser_linkage_specification
2554 (cp_parser *, tree);
2555 static void cp_parser_static_assert
2556 (cp_parser *, bool);
2557 static tree cp_parser_decltype
2558 (cp_parser *);
2559 static tree cp_parser_decomposition_declaration
2560 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2562 /* Declarators [gram.dcl.decl] */
2564 static tree cp_parser_init_declarator
2565 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2566 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2567 location_t *, tree *);
2568 static cp_declarator *cp_parser_declarator
2569 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2570 bool, bool, bool);
2571 static cp_declarator *cp_parser_direct_declarator
2572 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2573 bool);
2574 static enum tree_code cp_parser_ptr_operator
2575 (cp_parser *, tree *, cp_cv_quals *, tree *);
2576 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2577 (cp_parser *);
2578 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2579 (cp_parser *);
2580 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2581 (cp_parser *);
2582 static tree cp_parser_tx_qualifier_opt
2583 (cp_parser *);
2584 static tree cp_parser_late_return_type_opt
2585 (cp_parser *, cp_declarator *, tree &);
2586 static tree cp_parser_declarator_id
2587 (cp_parser *, bool);
2588 static tree cp_parser_type_id
2589 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2590 static tree cp_parser_template_type_arg
2591 (cp_parser *);
2592 static tree cp_parser_trailing_type_id (cp_parser *);
2593 static tree cp_parser_type_id_1
2594 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2595 static void cp_parser_type_specifier_seq
2596 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2597 static tree cp_parser_parameter_declaration_clause
2598 (cp_parser *, cp_parser_flags);
2599 static tree cp_parser_parameter_declaration_list
2600 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2601 static cp_parameter_declarator *cp_parser_parameter_declaration
2602 (cp_parser *, cp_parser_flags, bool, bool *);
2603 static tree cp_parser_default_argument
2604 (cp_parser *, bool);
2605 static void cp_parser_function_body
2606 (cp_parser *, bool);
2607 static tree cp_parser_initializer
2608 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2609 static cp_expr cp_parser_initializer_clause
2610 (cp_parser *, bool * = nullptr);
2611 static cp_expr cp_parser_braced_list
2612 (cp_parser*, bool * = nullptr);
2613 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2614 (cp_parser *, bool *, bool *);
2616 static void cp_parser_ctor_initializer_opt_and_function_body
2617 (cp_parser *, bool);
2619 static tree cp_parser_late_parsing_omp_declare_simd
2620 (cp_parser *, tree);
2622 static tree cp_parser_late_parsing_oacc_routine
2623 (cp_parser *, tree);
2625 static tree synthesize_implicit_template_parm
2626 (cp_parser *, tree);
2627 static tree finish_fully_implicit_template
2628 (cp_parser *, tree);
2629 static void abort_fully_implicit_template
2630 (cp_parser *);
2632 /* Classes [gram.class] */
2634 static tree cp_parser_class_name
2635 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2636 static tree cp_parser_class_specifier
2637 (cp_parser *);
2638 static tree cp_parser_class_head
2639 (cp_parser *, bool *);
2640 static enum tag_types cp_parser_class_key
2641 (cp_parser *);
2642 static void cp_parser_type_parameter_key
2643 (cp_parser* parser);
2644 static void cp_parser_member_specification_opt
2645 (cp_parser *);
2646 static void cp_parser_member_declaration
2647 (cp_parser *);
2648 static tree cp_parser_pure_specifier
2649 (cp_parser *);
2650 static tree cp_parser_constant_initializer
2651 (cp_parser *);
2653 /* Derived classes [gram.class.derived] */
2655 static tree cp_parser_base_clause
2656 (cp_parser *);
2657 static tree cp_parser_base_specifier
2658 (cp_parser *);
2660 /* Special member functions [gram.special] */
2662 static tree cp_parser_conversion_function_id
2663 (cp_parser *);
2664 static tree cp_parser_conversion_type_id
2665 (cp_parser *);
2666 static cp_declarator *cp_parser_conversion_declarator_opt
2667 (cp_parser *);
2668 static void cp_parser_ctor_initializer_opt
2669 (cp_parser *);
2670 static void cp_parser_mem_initializer_list
2671 (cp_parser *);
2672 static tree cp_parser_mem_initializer
2673 (cp_parser *);
2674 static tree cp_parser_mem_initializer_id
2675 (cp_parser *);
2677 /* Overloading [gram.over] */
2679 static cp_expr cp_parser_operator_function_id
2680 (cp_parser *);
2681 static cp_expr cp_parser_operator
2682 (cp_parser *, location_t);
2684 /* Templates [gram.temp] */
2686 static void cp_parser_template_declaration
2687 (cp_parser *, bool);
2688 static tree cp_parser_template_parameter_list
2689 (cp_parser *);
2690 static tree cp_parser_template_parameter
2691 (cp_parser *, bool *, bool *);
2692 static tree cp_parser_type_parameter
2693 (cp_parser *, bool *);
2694 static tree cp_parser_template_id
2695 (cp_parser *, bool, bool, enum tag_types, bool);
2696 static tree cp_parser_template_id_expr
2697 (cp_parser *, bool, bool, bool);
2698 static tree cp_parser_template_name
2699 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2700 static tree cp_parser_template_argument_list
2701 (cp_parser *);
2702 static tree cp_parser_template_argument
2703 (cp_parser *);
2704 static void cp_parser_explicit_instantiation
2705 (cp_parser *);
2706 static void cp_parser_explicit_specialization
2707 (cp_parser *);
2709 /* Exception handling [gram.except] */
2711 static tree cp_parser_try_block
2712 (cp_parser *);
2713 static void cp_parser_function_try_block
2714 (cp_parser *);
2715 static void cp_parser_handler_seq
2716 (cp_parser *);
2717 static void cp_parser_handler
2718 (cp_parser *);
2719 static tree cp_parser_exception_declaration
2720 (cp_parser *);
2721 static tree cp_parser_throw_expression
2722 (cp_parser *);
2723 static tree cp_parser_exception_specification_opt
2724 (cp_parser *, cp_parser_flags);
2725 static tree cp_parser_type_id_list
2726 (cp_parser *);
2727 static tree cp_parser_noexcept_specification_opt
2728 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2730 /* GNU Extensions */
2732 static tree cp_parser_asm_specification_opt
2733 (cp_parser *);
2734 static tree cp_parser_asm_operand_list
2735 (cp_parser *);
2736 static tree cp_parser_asm_clobber_list
2737 (cp_parser *);
2738 static tree cp_parser_asm_label_list
2739 (cp_parser *);
2740 static bool cp_next_tokens_can_be_attribute_p
2741 (cp_parser *);
2742 static bool cp_next_tokens_can_be_gnu_attribute_p
2743 (cp_parser *);
2744 static bool cp_next_tokens_can_be_std_attribute_p
2745 (cp_parser *);
2746 static bool cp_nth_tokens_can_be_std_attribute_p
2747 (cp_parser *, size_t);
2748 static bool cp_nth_tokens_can_be_gnu_attribute_p
2749 (cp_parser *, size_t);
2750 static bool cp_nth_tokens_can_be_attribute_p
2751 (cp_parser *, size_t);
2752 static tree cp_parser_attributes_opt
2753 (cp_parser *);
2754 static tree cp_parser_gnu_attributes_opt
2755 (cp_parser *);
2756 static tree cp_parser_gnu_attribute_list
2757 (cp_parser *, bool = false);
2758 static tree cp_parser_std_attribute
2759 (cp_parser *, tree);
2760 static tree cp_parser_std_attribute_spec
2761 (cp_parser *);
2762 static tree cp_parser_std_attribute_spec_seq
2763 (cp_parser *);
2764 static size_t cp_parser_skip_std_attribute_spec_seq
2765 (cp_parser *, size_t);
2766 static size_t cp_parser_skip_attributes_opt
2767 (cp_parser *, size_t);
2768 static bool cp_parser_extension_opt
2769 (cp_parser *, int *);
2770 static void cp_parser_label_declaration
2771 (cp_parser *);
2773 /* Concept Extensions */
2775 static tree cp_parser_concept_definition
2776 (cp_parser *);
2777 static tree cp_parser_constraint_expression
2778 (cp_parser *);
2779 static tree cp_parser_requires_clause_opt
2780 (cp_parser *, bool);
2781 static tree cp_parser_requires_expression
2782 (cp_parser *);
2783 static tree cp_parser_requirement_parameter_list
2784 (cp_parser *);
2785 static tree cp_parser_requirement_body
2786 (cp_parser *);
2787 static tree cp_parser_requirement_seq
2788 (cp_parser *);
2789 static tree cp_parser_requirement
2790 (cp_parser *);
2791 static tree cp_parser_simple_requirement
2792 (cp_parser *);
2793 static tree cp_parser_compound_requirement
2794 (cp_parser *);
2795 static tree cp_parser_type_requirement
2796 (cp_parser *);
2797 static tree cp_parser_nested_requirement
2798 (cp_parser *);
2800 /* Transactional Memory Extensions */
2802 static tree cp_parser_transaction
2803 (cp_parser *, cp_token *);
2804 static tree cp_parser_transaction_expression
2805 (cp_parser *, enum rid);
2806 static void cp_parser_function_transaction
2807 (cp_parser *, enum rid);
2808 static tree cp_parser_transaction_cancel
2809 (cp_parser *);
2811 /* Coroutine extensions. */
2813 static tree cp_parser_yield_expression
2814 (cp_parser *);
2816 /* Contracts */
2818 static void cp_parser_late_contract_condition
2819 (cp_parser *, tree, tree);
2821 enum pragma_context {
2822 pragma_external,
2823 pragma_member,
2824 pragma_objc_icode,
2825 pragma_stmt,
2826 pragma_compound
2828 static bool cp_parser_pragma
2829 (cp_parser *, enum pragma_context, bool *);
2831 /* Objective-C++ Productions */
2833 static tree cp_parser_objc_message_receiver
2834 (cp_parser *);
2835 static tree cp_parser_objc_message_args
2836 (cp_parser *);
2837 static tree cp_parser_objc_message_expression
2838 (cp_parser *);
2839 static cp_expr cp_parser_objc_encode_expression
2840 (cp_parser *);
2841 static tree cp_parser_objc_defs_expression
2842 (cp_parser *);
2843 static tree cp_parser_objc_protocol_expression
2844 (cp_parser *);
2845 static tree cp_parser_objc_selector_expression
2846 (cp_parser *);
2847 static cp_expr cp_parser_objc_expression
2848 (cp_parser *);
2849 static bool cp_parser_objc_selector_p
2850 (enum cpp_ttype);
2851 static tree cp_parser_objc_selector
2852 (cp_parser *);
2853 static tree cp_parser_objc_protocol_refs_opt
2854 (cp_parser *);
2855 static void cp_parser_objc_declaration
2856 (cp_parser *, tree);
2857 static tree cp_parser_objc_statement
2858 (cp_parser *);
2859 static bool cp_parser_objc_valid_prefix_attributes
2860 (cp_parser *, tree *);
2861 static void cp_parser_objc_at_property_declaration
2862 (cp_parser *) ;
2863 static void cp_parser_objc_at_synthesize_declaration
2864 (cp_parser *) ;
2865 static void cp_parser_objc_at_dynamic_declaration
2866 (cp_parser *) ;
2867 static tree cp_parser_objc_struct_declaration
2868 (cp_parser *) ;
2870 /* Utility Routines */
2872 static cp_expr cp_parser_lookup_name
2873 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2874 static tree cp_parser_lookup_name_simple
2875 (cp_parser *, tree, location_t);
2876 static tree cp_parser_maybe_treat_template_as_class
2877 (tree, bool);
2878 static bool cp_parser_check_declarator_template_parameters
2879 (cp_parser *, cp_declarator *, location_t);
2880 static bool cp_parser_check_template_parameters
2881 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2882 static cp_expr cp_parser_simple_cast_expression
2883 (cp_parser *);
2884 static tree cp_parser_global_scope_opt
2885 (cp_parser *, bool);
2886 static bool cp_parser_constructor_declarator_p
2887 (cp_parser *, cp_parser_flags, bool);
2888 static tree cp_parser_function_definition_from_specifiers_and_declarator
2889 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2890 static tree cp_parser_function_definition_after_declarator
2891 (cp_parser *, bool);
2892 static bool cp_parser_template_declaration_after_export
2893 (cp_parser *, bool);
2894 static void cp_parser_perform_template_parameter_access_checks
2895 (vec<deferred_access_check, va_gc> *);
2896 static tree cp_parser_single_declaration
2897 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2898 static cp_expr cp_parser_functional_cast
2899 (cp_parser *, tree);
2900 static tree cp_parser_save_member_function_body
2901 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2902 static tree cp_parser_save_nsdmi
2903 (cp_parser *);
2904 static tree cp_parser_enclosed_template_argument_list
2905 (cp_parser *);
2906 static void cp_parser_save_default_args
2907 (cp_parser *, tree);
2908 static void cp_parser_late_parsing_for_member
2909 (cp_parser *, tree);
2910 static tree cp_parser_late_parse_one_default_arg
2911 (cp_parser *, tree, tree, tree);
2912 static void cp_parser_late_parsing_nsdmi
2913 (cp_parser *, tree);
2914 static void cp_parser_late_parsing_default_args
2915 (cp_parser *, tree);
2916 static tree cp_parser_sizeof_operand
2917 (cp_parser *, enum rid);
2918 static cp_expr cp_parser_trait
2919 (cp_parser *, const cp_trait *);
2920 static bool cp_parser_declares_only_class_p
2921 (cp_parser *);
2922 static void cp_parser_set_storage_class
2923 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2924 static void cp_parser_set_decl_spec_type
2925 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2926 static void set_and_check_decl_spec_loc
2927 (cp_decl_specifier_seq *decl_specs,
2928 cp_decl_spec ds, cp_token *);
2929 static bool cp_parser_friend_p
2930 (const cp_decl_specifier_seq *);
2931 static void cp_parser_required_error
2932 (cp_parser *, required_token, bool, location_t);
2933 static cp_token *cp_parser_require
2934 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2935 static cp_token *cp_parser_require_keyword
2936 (cp_parser *, enum rid, required_token);
2937 static bool cp_parser_token_starts_function_definition_p
2938 (cp_token *);
2939 static bool cp_parser_next_token_starts_class_definition_p
2940 (cp_parser *);
2941 static bool cp_parser_next_token_ends_template_argument_p
2942 (cp_parser *);
2943 static bool cp_parser_nth_token_starts_template_argument_list_p
2944 (cp_parser *, size_t);
2945 static enum tag_types cp_parser_token_is_class_key
2946 (cp_token *);
2947 static enum tag_types cp_parser_token_is_type_parameter_key
2948 (cp_token *);
2949 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2950 static void cp_parser_check_class_key
2951 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2952 static void cp_parser_check_access_in_redeclaration
2953 (tree type, location_t location);
2954 static bool cp_parser_optional_template_keyword
2955 (cp_parser *);
2956 static void cp_parser_pre_parsed_nested_name_specifier
2957 (cp_parser *);
2958 static bool cp_parser_cache_group
2959 (cp_parser *, enum cpp_ttype, unsigned);
2960 static tree cp_parser_cache_defarg
2961 (cp_parser *parser, bool nsdmi);
2962 static void cp_parser_parse_tentatively
2963 (cp_parser *);
2964 static void cp_parser_commit_to_tentative_parse
2965 (cp_parser *);
2966 static void cp_parser_commit_to_topmost_tentative_parse
2967 (cp_parser *);
2968 static void cp_parser_abort_tentative_parse
2969 (cp_parser *);
2970 static bool cp_parser_parse_definitely
2971 (cp_parser *);
2972 static inline bool cp_parser_parsing_tentatively
2973 (cp_parser *);
2974 static bool cp_parser_uncommitted_to_tentative_parse_p
2975 (cp_parser *);
2976 static void cp_parser_error
2977 (cp_parser *, const char *);
2978 static void cp_parser_name_lookup_error
2979 (cp_parser *, tree, tree, name_lookup_error, location_t);
2980 static bool cp_parser_simulate_error
2981 (cp_parser *);
2982 static bool cp_parser_check_type_definition
2983 (cp_parser *);
2984 static void cp_parser_check_for_definition_in_return_type
2985 (cp_declarator *, tree, location_t type_location);
2986 static void cp_parser_check_for_invalid_template_id
2987 (cp_parser *, tree, enum tag_types, location_t location);
2988 static bool cp_parser_non_integral_constant_expression
2989 (cp_parser *, non_integral_constant);
2990 static void cp_parser_diagnose_invalid_type_name
2991 (cp_parser *, tree, location_t);
2992 static bool cp_parser_parse_and_diagnose_invalid_type_name
2993 (cp_parser *);
2994 static int cp_parser_skip_to_closing_parenthesis
2995 (cp_parser *, bool, bool, bool);
2996 static void cp_parser_skip_to_end_of_statement
2997 (cp_parser *);
2998 static void cp_parser_consume_semicolon_at_end_of_statement
2999 (cp_parser *);
3000 static void cp_parser_skip_to_end_of_block_or_statement
3001 (cp_parser *);
3002 static bool cp_parser_skip_to_closing_brace
3003 (cp_parser *);
3004 static bool cp_parser_skip_entire_template_parameter_list
3005 (cp_parser *);
3006 static void cp_parser_require_end_of_template_parameter_list
3007 (cp_parser *);
3008 static bool cp_parser_skip_to_end_of_template_parameter_list
3009 (cp_parser *);
3010 static void cp_parser_skip_to_pragma_eol
3011 (cp_parser*, cp_token *);
3012 static bool cp_parser_error_occurred
3013 (cp_parser *);
3014 static bool cp_parser_allow_gnu_extensions_p
3015 (cp_parser *);
3016 static bool cp_parser_is_pure_string_literal
3017 (cp_token *);
3018 static bool cp_parser_is_string_literal
3019 (cp_token *);
3020 static bool cp_parser_is_keyword
3021 (cp_token *, enum rid);
3022 static tree cp_parser_make_typename_type
3023 (cp_parser *, tree, location_t location);
3024 static cp_declarator * cp_parser_make_indirect_declarator
3025 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
3026 static bool cp_parser_compound_literal_p
3027 (cp_parser *);
3028 static bool cp_parser_array_designator_p
3029 (cp_parser *);
3030 static bool cp_parser_init_statement_p
3031 (cp_parser *);
3032 static bool cp_parser_skip_up_to_closing_square_bracket
3033 (cp_parser *);
3034 static bool cp_parser_skip_to_closing_square_bracket
3035 (cp_parser *);
3036 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
3037 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
3039 // -------------------------------------------------------------------------- //
3040 // Unevaluated Operand Guard
3042 // Implementation of an RAII helper for unevaluated operand parsing.
3043 cp_unevaluated::cp_unevaluated ()
3045 ++cp_unevaluated_operand;
3046 ++c_inhibit_evaluation_warnings;
3049 cp_unevaluated::~cp_unevaluated ()
3051 --c_inhibit_evaluation_warnings;
3052 --cp_unevaluated_operand;
3055 // -------------------------------------------------------------------------- //
3056 // Tentative Parsing
3058 /* Returns nonzero if we are parsing tentatively. */
3060 static inline bool
3061 cp_parser_parsing_tentatively (cp_parser* parser)
3063 return parser->context->next != NULL;
3066 /* Returns nonzero if TOKEN is a string literal. */
3068 static bool
3069 cp_parser_is_pure_string_literal (cp_token* token)
3071 return (token->type == CPP_STRING ||
3072 token->type == CPP_STRING16 ||
3073 token->type == CPP_STRING32 ||
3074 token->type == CPP_WSTRING ||
3075 token->type == CPP_UTF8STRING);
3078 /* Returns nonzero if TOKEN is a string literal
3079 of a user-defined string literal. */
3081 static bool
3082 cp_parser_is_string_literal (cp_token* token)
3084 return (cp_parser_is_pure_string_literal (token) ||
3085 token->type == CPP_STRING_USERDEF ||
3086 token->type == CPP_STRING16_USERDEF ||
3087 token->type == CPP_STRING32_USERDEF ||
3088 token->type == CPP_WSTRING_USERDEF ||
3089 token->type == CPP_UTF8STRING_USERDEF);
3092 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3094 static bool
3095 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3097 return token->keyword == keyword;
3100 /* Helper function for cp_parser_error.
3101 Having peeked a token of kind TOK1_KIND that might signify
3102 a conflict marker, peek successor tokens to determine
3103 if we actually do have a conflict marker.
3104 Specifically, we consider a run of 7 '<', '=' or '>' characters
3105 at the start of a line as a conflict marker.
3106 These come through the lexer as three pairs and a single,
3107 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3108 If it returns true, *OUT_LOC is written to with the location/range
3109 of the marker. */
3111 static bool
3112 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3113 location_t *out_loc)
3115 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3116 if (token2->type != tok1_kind)
3117 return false;
3118 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3119 if (token3->type != tok1_kind)
3120 return false;
3121 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3122 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3123 return false;
3125 /* It must be at the start of the line. */
3126 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3127 if (LOCATION_COLUMN (start_loc) != 1)
3128 return false;
3130 /* We have a conflict marker. Construct a location of the form:
3131 <<<<<<<
3132 ^~~~~~~
3133 with start == caret, finishing at the end of the marker. */
3134 location_t finish_loc = get_finish (token4->location);
3135 *out_loc = make_location (start_loc, start_loc, finish_loc);
3137 return true;
3140 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3141 RT_CLOSE_PAREN. */
3143 static const char *
3144 get_matching_symbol (required_token token_desc)
3146 switch (token_desc)
3148 default:
3149 gcc_unreachable ();
3150 return "";
3151 case RT_CLOSE_BRACE:
3152 return "{";
3153 case RT_CLOSE_PAREN:
3154 return "(";
3158 /* Attempt to convert TOKEN_DESC from a required_token to an
3159 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3161 static enum cpp_ttype
3162 get_required_cpp_ttype (required_token token_desc)
3164 switch (token_desc)
3166 case RT_SEMICOLON:
3167 return CPP_SEMICOLON;
3168 case RT_OPEN_PAREN:
3169 return CPP_OPEN_PAREN;
3170 case RT_CLOSE_BRACE:
3171 return CPP_CLOSE_BRACE;
3172 case RT_OPEN_BRACE:
3173 return CPP_OPEN_BRACE;
3174 case RT_CLOSE_SQUARE:
3175 return CPP_CLOSE_SQUARE;
3176 case RT_OPEN_SQUARE:
3177 return CPP_OPEN_SQUARE;
3178 case RT_COMMA:
3179 return CPP_COMMA;
3180 case RT_COLON:
3181 return CPP_COLON;
3182 case RT_CLOSE_PAREN:
3183 return CPP_CLOSE_PAREN;
3185 default:
3186 /* Use CPP_EOF as a "no completions possible" code. */
3187 return CPP_EOF;
3192 /* Subroutine of cp_parser_error and cp_parser_required_error.
3194 Issue a diagnostic of the form
3195 FILE:LINE: MESSAGE before TOKEN
3196 where TOKEN is the next token in the input stream. MESSAGE
3197 (specified by the caller) is usually of the form "expected
3198 OTHER-TOKEN".
3200 This bypasses the check for tentative passing, and potentially
3201 adds material needed by cp_parser_required_error.
3203 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3204 suggesting insertion of the missing token.
3206 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3207 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3208 location. */
3210 static void
3211 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3212 required_token missing_token_desc,
3213 location_t matching_location)
3215 cp_token *token = cp_lexer_peek_token (parser->lexer);
3216 /* This diagnostic makes more sense if it is tagged to the line
3217 of the token we just peeked at. */
3218 cp_lexer_set_source_position_from_token (token);
3220 if (token->type == CPP_PRAGMA)
3222 error_at (token->location,
3223 "%<#pragma%> is not allowed here");
3224 cp_parser_skip_to_pragma_eol (parser, token);
3225 return;
3228 /* If this is actually a conflict marker, report it as such. */
3229 if (token->type == CPP_LSHIFT
3230 || token->type == CPP_RSHIFT
3231 || token->type == CPP_EQ_EQ)
3233 location_t loc;
3234 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3236 error_at (loc, "version control conflict marker in file");
3237 expanded_location token_exploc = expand_location (token->location);
3238 /* Consume tokens until the end of the source line. */
3239 for (;;)
3241 cp_lexer_consume_token (parser->lexer);
3242 cp_token *next = cp_lexer_peek_token (parser->lexer);
3243 if (next->type == CPP_EOF)
3244 break;
3245 if (next->location == UNKNOWN_LOCATION
3246 || loc == UNKNOWN_LOCATION)
3247 break;
3249 expanded_location next_exploc = expand_location (next->location);
3250 if (next_exploc.file != token_exploc.file)
3251 break;
3252 if (next_exploc.line != token_exploc.line)
3253 break;
3255 return;
3259 auto_diagnostic_group d;
3260 gcc_rich_location richloc (input_location);
3262 bool added_matching_location = false;
3264 if (missing_token_desc != RT_NONE)
3265 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3267 /* Potentially supply a fix-it hint, suggesting to add the
3268 missing token immediately after the *previous* token.
3269 This may move the primary location within richloc. */
3270 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3271 location_t prev_token_loc = prev_token->location;
3272 maybe_suggest_missing_token_insertion (&richloc, ttype,
3273 prev_token_loc);
3275 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3276 Attempt to consolidate diagnostics by printing it as a
3277 secondary range within the main diagnostic. */
3278 if (matching_location != UNKNOWN_LOCATION)
3279 added_matching_location
3280 = richloc.add_location_if_nearby (matching_location);
3283 /* If we were parsing a string-literal and there is an unknown name
3284 token right after, then check to see if that could also have been
3285 a literal string by checking the name against a list of known
3286 standard string literal constants defined in header files. If
3287 there is one, then add that as an hint to the error message. */
3288 name_hint h;
3289 if (token->type == CPP_NAME)
3290 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3291 if (cp_parser_is_string_literal (prev_token))
3293 tree name = token->u.value;
3294 const char *token_name = IDENTIFIER_POINTER (name);
3295 const char *header_hint
3296 = get_cp_stdlib_header_for_string_macro_name (token_name);
3297 if (header_hint != NULL)
3298 h = name_hint (NULL, new suggest_missing_header (token->location,
3299 token_name,
3300 header_hint));
3303 /* Actually emit the error. */
3304 c_parse_error (gmsgid,
3305 /* Because c_parser_error does not understand
3306 CPP_KEYWORD, keywords are treated like
3307 identifiers. */
3308 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3309 token->u.value, token->flags, &richloc);
3311 if (missing_token_desc != RT_NONE)
3313 /* If we weren't able to consolidate matching_location, then
3314 print it as a secondary diagnostic. */
3315 if (matching_location != UNKNOWN_LOCATION
3316 && !added_matching_location)
3317 inform (matching_location, "to match this %qs",
3318 get_matching_symbol (missing_token_desc));
3322 /* If not parsing tentatively, issue a diagnostic of the form
3323 FILE:LINE: MESSAGE before TOKEN
3324 where TOKEN is the next token in the input stream. MESSAGE
3325 (specified by the caller) is usually of the form "expected
3326 OTHER-TOKEN". */
3328 static void
3329 cp_parser_error (cp_parser* parser, const char* gmsgid)
3331 if (!cp_parser_simulate_error (parser))
3332 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3335 /* Issue an error about name-lookup failing. NAME is the
3336 IDENTIFIER_NODE DECL is the result of
3337 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3338 the thing that we hoped to find. */
3340 static void
3341 cp_parser_name_lookup_error (cp_parser* parser,
3342 tree name,
3343 tree decl,
3344 name_lookup_error desired,
3345 location_t location)
3347 /* If name lookup completely failed, tell the user that NAME was not
3348 declared. */
3349 if (decl == error_mark_node)
3351 if (parser->scope && parser->scope != global_namespace)
3352 error_at (location, "%<%E::%E%> has not been declared",
3353 parser->scope, name);
3354 else if (parser->scope == global_namespace)
3355 error_at (location, "%<::%E%> has not been declared", name);
3356 else if (parser->object_scope
3357 && !CLASS_TYPE_P (parser->object_scope))
3358 error_at (location, "request for member %qE in non-class type %qT",
3359 name, parser->object_scope);
3360 else if (parser->object_scope)
3361 error_at (location, "%<%T::%E%> has not been declared",
3362 parser->object_scope, name);
3363 else
3364 error_at (location, "%qE has not been declared", name);
3366 else if (parser->scope && parser->scope != global_namespace)
3368 switch (desired)
3370 case NLE_TYPE:
3371 error_at (location, "%<%E::%E%> is not a type",
3372 parser->scope, name);
3373 break;
3374 case NLE_CXX98:
3375 error_at (location, "%<%E::%E%> is not a class or namespace",
3376 parser->scope, name);
3377 break;
3378 case NLE_NOT_CXX98:
3379 error_at (location,
3380 "%<%E::%E%> is not a class, namespace, or enumeration",
3381 parser->scope, name);
3382 break;
3383 default:
3384 gcc_unreachable ();
3388 else if (parser->scope == global_namespace)
3390 switch (desired)
3392 case NLE_TYPE:
3393 error_at (location, "%<::%E%> is not a type", name);
3394 break;
3395 case NLE_CXX98:
3396 error_at (location, "%<::%E%> is not a class or namespace", name);
3397 break;
3398 case NLE_NOT_CXX98:
3399 error_at (location,
3400 "%<::%E%> is not a class, namespace, or enumeration",
3401 name);
3402 break;
3403 default:
3404 gcc_unreachable ();
3407 else
3409 switch (desired)
3411 case NLE_TYPE:
3412 error_at (location, "%qE is not a type", name);
3413 break;
3414 case NLE_CXX98:
3415 error_at (location, "%qE is not a class or namespace", name);
3416 break;
3417 case NLE_NOT_CXX98:
3418 error_at (location,
3419 "%qE is not a class, namespace, or enumeration", name);
3420 break;
3421 default:
3422 gcc_unreachable ();
3427 /* If we are parsing tentatively, remember that an error has occurred
3428 during this tentative parse. Returns true if the error was
3429 simulated; false if a message should be issued by the caller. */
3431 static bool
3432 cp_parser_simulate_error (cp_parser* parser)
3434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3436 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3437 return true;
3439 return false;
3442 /* This function is called when a type is defined. If type
3443 definitions are forbidden at this point, an error message is
3444 issued. */
3446 static bool
3447 cp_parser_check_type_definition (cp_parser* parser)
3449 /* If types are forbidden here, issue a message. */
3450 if (parser->type_definition_forbidden_message)
3452 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3453 or %qs in the message need to be interpreted. */
3454 error (parser->type_definition_forbidden_message,
3455 parser->type_definition_forbidden_message_arg);
3456 return false;
3458 return true;
3461 /* This function is called when the DECLARATOR is processed. The TYPE
3462 was a type defined in the decl-specifiers. If it is invalid to
3463 define a type in the decl-specifiers for DECLARATOR, an error is
3464 issued. TYPE_LOCATION is the location of TYPE and is used
3465 for error reporting. */
3467 static void
3468 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3469 tree type, location_t type_location)
3471 /* [dcl.fct] forbids type definitions in return types.
3472 Unfortunately, it's not easy to know whether or not we are
3473 processing a return type until after the fact. */
3474 while (declarator
3475 && (declarator->kind == cdk_pointer
3476 || declarator->kind == cdk_reference
3477 || declarator->kind == cdk_ptrmem))
3478 declarator = declarator->declarator;
3479 if (declarator
3480 && declarator->kind == cdk_function)
3482 error_at (type_location,
3483 "new types may not be defined in a return type");
3484 inform (type_location,
3485 "(perhaps a semicolon is missing after the definition of %qT)",
3486 type);
3490 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3491 "<" in any valid C++ program. If the next token is indeed "<",
3492 issue a message warning the user about what appears to be an
3493 invalid attempt to form a template-id. LOCATION is the location
3494 of the type-specifier (TYPE) */
3496 static void
3497 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3498 tree type,
3499 enum tag_types tag_type,
3500 location_t location)
3502 cp_token_position start = 0;
3504 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3506 if (TREE_CODE (type) == TYPE_DECL)
3507 type = TREE_TYPE (type);
3508 if (TYPE_P (type) && !template_placeholder_p (type))
3509 error_at (location, "%qT is not a template", type);
3510 else if (identifier_p (type))
3512 if (tag_type != none_type)
3513 error_at (location, "%qE is not a class template", type);
3514 else
3515 error_at (location, "%qE is not a template", type);
3517 else
3518 error_at (location, "invalid template-id");
3519 /* Remember the location of the invalid "<". */
3520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3521 start = cp_lexer_token_position (parser->lexer, true);
3522 /* Consume the "<". */
3523 cp_lexer_consume_token (parser->lexer);
3524 /* Parse the template arguments. */
3525 cp_parser_enclosed_template_argument_list (parser);
3526 /* Permanently remove the invalid template arguments so that
3527 this error message is not issued again. */
3528 if (start)
3529 cp_lexer_purge_tokens_after (parser->lexer, start);
3533 /* If parsing an integral constant-expression, issue an error message
3534 about the fact that THING appeared and return true. Otherwise,
3535 return false. In either case, set
3536 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3538 static bool
3539 cp_parser_non_integral_constant_expression (cp_parser *parser,
3540 non_integral_constant thing)
3542 parser->non_integral_constant_expression_p = true;
3543 if (parser->integral_constant_expression_p)
3545 if (!parser->allow_non_integral_constant_expression_p)
3547 const char *msg = NULL;
3548 switch (thing)
3550 case NIC_FLOAT:
3551 pedwarn (input_location, OPT_Wpedantic,
3552 "ISO C++ forbids using a floating-point literal "
3553 "in a constant-expression");
3554 return true;
3555 case NIC_CAST:
3556 error ("a cast to a type other than an integral or "
3557 "enumeration type cannot appear in a "
3558 "constant-expression");
3559 return true;
3560 case NIC_TYPEID:
3561 error ("%<typeid%> operator "
3562 "cannot appear in a constant-expression");
3563 return true;
3564 case NIC_NCC:
3565 error ("non-constant compound literals "
3566 "cannot appear in a constant-expression");
3567 return true;
3568 case NIC_FUNC_CALL:
3569 error ("a function call "
3570 "cannot appear in a constant-expression");
3571 return true;
3572 case NIC_INC:
3573 error ("an increment "
3574 "cannot appear in a constant-expression");
3575 return true;
3576 case NIC_DEC:
3577 error ("an decrement "
3578 "cannot appear in a constant-expression");
3579 return true;
3580 case NIC_ARRAY_REF:
3581 error ("an array reference "
3582 "cannot appear in a constant-expression");
3583 return true;
3584 case NIC_ADDR_LABEL:
3585 error ("the address of a label "
3586 "cannot appear in a constant-expression");
3587 return true;
3588 case NIC_OVERLOADED:
3589 error ("calls to overloaded operators "
3590 "cannot appear in a constant-expression");
3591 return true;
3592 case NIC_ASSIGNMENT:
3593 error ("an assignment cannot appear in a constant-expression");
3594 return true;
3595 case NIC_COMMA:
3596 error ("a comma operator "
3597 "cannot appear in a constant-expression");
3598 return true;
3599 case NIC_CONSTRUCTOR:
3600 error ("a call to a constructor "
3601 "cannot appear in a constant-expression");
3602 return true;
3603 case NIC_TRANSACTION:
3604 error ("a transaction expression "
3605 "cannot appear in a constant-expression");
3606 return true;
3607 case NIC_THIS:
3608 msg = "this";
3609 break;
3610 case NIC_FUNC_NAME:
3611 msg = "__FUNCTION__";
3612 break;
3613 case NIC_PRETTY_FUNC:
3614 msg = "__PRETTY_FUNCTION__";
3615 break;
3616 case NIC_C99_FUNC:
3617 msg = "__func__";
3618 break;
3619 case NIC_VA_ARG:
3620 msg = "va_arg";
3621 break;
3622 case NIC_ARROW:
3623 msg = "->";
3624 break;
3625 case NIC_POINT:
3626 msg = ".";
3627 break;
3628 case NIC_STAR:
3629 msg = "*";
3630 break;
3631 case NIC_ADDR:
3632 msg = "&";
3633 break;
3634 case NIC_PREINCREMENT:
3635 msg = "++";
3636 break;
3637 case NIC_PREDECREMENT:
3638 msg = "--";
3639 break;
3640 case NIC_NEW:
3641 msg = "new";
3642 break;
3643 case NIC_DEL:
3644 msg = "delete";
3645 break;
3646 default:
3647 gcc_unreachable ();
3649 if (msg)
3650 error ("%qs cannot appear in a constant-expression", msg);
3651 return true;
3654 return false;
3657 /* Emit a diagnostic for an invalid type name. This function commits
3658 to the current active tentative parse, if any. (Otherwise, the
3659 problematic construct might be encountered again later, resulting
3660 in duplicate error messages.) LOCATION is the location of ID. */
3662 static void
3663 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3664 location_t location)
3666 tree decl, ambiguous_decls;
3667 cp_parser_commit_to_tentative_parse (parser);
3668 /* Try to lookup the identifier. */
3669 decl = cp_parser_lookup_name (parser, id, none_type,
3670 /*is_template=*/false,
3671 /*is_namespace=*/false,
3672 /*check_dependency=*/true,
3673 &ambiguous_decls, location);
3674 if (ambiguous_decls)
3675 /* If the lookup was ambiguous, an error will already have
3676 been issued. */
3677 return;
3678 /* If the lookup found a template-name, it means that the user forgot
3679 to specify an argument list. Emit a useful error message. */
3680 if (DECL_TYPE_TEMPLATE_P (decl))
3682 auto_diagnostic_group d;
3683 error_at (location,
3684 "invalid use of template-name %qE without an argument list",
3685 decl);
3686 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3687 inform (location, "class template argument deduction is only available "
3688 "with %<-std=c++17%> or %<-std=gnu++17%>");
3689 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3691 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3692 error_at (location, "invalid use of destructor %qD as a type", id);
3693 else if (TREE_CODE (decl) == TYPE_DECL)
3694 /* Something like 'unsigned A a;' */
3695 error_at (location, "invalid combination of multiple type-specifiers");
3696 else if (!parser->scope)
3698 /* Issue an error message. */
3699 auto_diagnostic_group d;
3700 name_hint hint;
3701 if (TREE_CODE (id) == IDENTIFIER_NODE)
3702 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3703 if (const char *suggestion = hint.suggestion ())
3705 gcc_rich_location richloc (location);
3706 richloc.add_fixit_replace (suggestion);
3707 error_at (&richloc,
3708 "%qE does not name a type; did you mean %qs?",
3709 id, suggestion);
3711 else
3712 error_at (location, "%qE does not name a type", id);
3713 /* If we're in a template class, it's possible that the user was
3714 referring to a type from a base class. For example:
3716 template <typename T> struct A { typedef T X; };
3717 template <typename T> struct B : public A<T> { X x; };
3719 The user should have said "typename A<T>::X". */
3720 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3721 inform (location, "C++11 %<constexpr%> only available with "
3722 "%<-std=c++11%> or %<-std=gnu++11%>");
3723 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3724 inform (location, "C++11 %<noexcept%> only available with "
3725 "%<-std=c++11%> or %<-std=gnu++11%>");
3726 else if (TREE_CODE (id) == IDENTIFIER_NODE
3727 && (id_equal (id, "module") || id_equal (id, "import")))
3729 if (modules_p ())
3730 inform (location, "%qE is not recognized as a module control-line",
3731 id);
3732 else if (cxx_dialect < cxx20)
3733 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3734 id);
3735 else
3736 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3737 ", which is not yet enabled with %<-std=c++20%>", id);
3739 else if (cxx_dialect < cxx11
3740 && TREE_CODE (id) == IDENTIFIER_NODE
3741 && id_equal (id, "thread_local"))
3742 inform (location, "C++11 %<thread_local%> only available with "
3743 "%<-std=c++11%> or %<-std=gnu++11%>");
3744 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3745 inform (location, "C++20 %<constinit%> only available with "
3746 "%<-std=c++20%> or %<-std=gnu++20%>");
3747 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3748 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3749 "%<-fconcepts%>");
3750 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3751 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3752 "%<-fconcepts%>");
3753 else if (processing_template_decl && current_class_type
3754 && TYPE_BINFO (current_class_type))
3756 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3757 b; b = TREE_CHAIN (b))
3759 tree base_type = BINFO_TYPE (b);
3760 if (CLASS_TYPE_P (base_type)
3761 && dependent_type_p (base_type))
3763 /* Go from a particular instantiation of the
3764 template (which will have an empty TYPE_FIELDs),
3765 to the main version. */
3766 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3767 for (tree field = TYPE_FIELDS (base_type);
3768 field; field = DECL_CHAIN (field))
3769 if (TREE_CODE (field) == TYPE_DECL
3770 && DECL_NAME (field) == id)
3772 inform (location,
3773 "(perhaps %<typename %T::%E%> was intended)",
3774 BINFO_TYPE (b), id);
3775 goto found;
3779 found:;
3782 /* Here we diagnose qualified-ids where the scope is actually correct,
3783 but the identifier does not resolve to a valid type name. */
3784 else if (parser->scope != error_mark_node)
3786 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3788 auto_diagnostic_group d;
3789 name_hint hint;
3790 if (decl == error_mark_node)
3791 hint = suggest_alternative_in_explicit_scope (location, id,
3792 parser->scope);
3793 const char *suggestion = hint.suggestion ();
3794 gcc_rich_location richloc (location_of (id));
3795 if (suggestion)
3796 richloc.add_fixit_replace (suggestion);
3797 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3799 if (suggestion)
3800 error_at (&richloc,
3801 "%qE in namespace %qE does not name a template"
3802 " type; did you mean %qs?",
3803 id, parser->scope, suggestion);
3804 else
3805 error_at (&richloc,
3806 "%qE in namespace %qE does not name a template type",
3807 id, parser->scope);
3809 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3811 if (suggestion)
3812 error_at (&richloc,
3813 "%qE in namespace %qE does not name a template"
3814 " type; did you mean %qs?",
3815 TREE_OPERAND (id, 0), parser->scope, suggestion);
3816 else
3817 error_at (&richloc,
3818 "%qE in namespace %qE does not name a template"
3819 " type",
3820 TREE_OPERAND (id, 0), parser->scope);
3822 else
3824 if (suggestion)
3825 error_at (&richloc,
3826 "%qE in namespace %qE does not name a type"
3827 "; did you mean %qs?",
3828 id, parser->scope, suggestion);
3829 else
3830 error_at (&richloc,
3831 "%qE in namespace %qE does not name a type",
3832 id, parser->scope);
3834 if (DECL_P (decl))
3835 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3837 else if (CLASS_TYPE_P (parser->scope)
3838 && constructor_name_p (id, parser->scope))
3840 /* A<T>::A<T>() */
3841 auto_diagnostic_group d;
3842 error_at (location, "%<%T::%E%> names the constructor, not"
3843 " the type", parser->scope, id);
3844 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3845 error_at (location, "and %qT has no template constructors",
3846 parser->scope);
3848 else if (TYPE_P (parser->scope)
3849 && dependent_scope_p (parser->scope))
3851 gcc_rich_location richloc (location);
3852 richloc.add_fixit_insert_before ("typename ");
3853 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3854 error_at (&richloc,
3855 "need %<typename%> before %<%T::%D::%E%> because "
3856 "%<%T::%D%> is a dependent scope",
3857 TYPE_CONTEXT (parser->scope),
3858 TYPENAME_TYPE_FULLNAME (parser->scope),
3860 TYPE_CONTEXT (parser->scope),
3861 TYPENAME_TYPE_FULLNAME (parser->scope));
3862 else
3863 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3864 "%qT is a dependent scope",
3865 parser->scope, id, parser->scope);
3867 else if (TYPE_P (parser->scope))
3869 auto_diagnostic_group d;
3870 if (!COMPLETE_TYPE_P (parser->scope))
3871 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3872 parser->scope);
3873 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3874 error_at (location_of (id),
3875 "%qE in %q#T does not name a template type",
3876 id, parser->scope);
3877 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3878 error_at (location_of (id),
3879 "%qE in %q#T does not name a template type",
3880 TREE_OPERAND (id, 0), parser->scope);
3881 else
3882 error_at (location_of (id),
3883 "%qE in %q#T does not name a type",
3884 id, parser->scope);
3885 if (DECL_P (decl))
3886 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3888 else
3889 gcc_unreachable ();
3893 /* Check for a common situation where a type-name should be present,
3894 but is not, and issue a sensible error message. Returns true if an
3895 invalid type-name was detected.
3897 The situation handled by this function are variable declarations of the
3898 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3899 Usually, `ID' should name a type, but if we got here it means that it
3900 does not. We try to emit the best possible error message depending on
3901 how exactly the id-expression looks like. */
3903 static bool
3904 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3906 tree id;
3907 cp_token *token = cp_lexer_peek_token (parser->lexer);
3909 /* Avoid duplicate error about ambiguous lookup. */
3910 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3912 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3913 if (next->type == CPP_NAME && next->error_reported)
3914 goto out;
3917 cp_parser_parse_tentatively (parser);
3918 id = cp_parser_id_expression (parser,
3919 /*template_keyword_p=*/false,
3920 /*check_dependency_p=*/true,
3921 /*template_p=*/NULL,
3922 /*declarator_p=*/true,
3923 /*optional_p=*/false);
3924 /* If the next token is a (, this is a function with no explicit return
3925 type, i.e. constructor, destructor or conversion op. */
3926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3927 || TREE_CODE (id) == TYPE_DECL)
3929 cp_parser_abort_tentative_parse (parser);
3930 return false;
3932 if (!cp_parser_parse_definitely (parser))
3933 return false;
3935 /* Emit a diagnostic for the invalid type. */
3936 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3937 out:
3938 /* If we aren't in the middle of a declarator (i.e. in a
3939 parameter-declaration-clause), skip to the end of the declaration;
3940 there's no point in trying to process it. */
3941 if (!parser->in_declarator_p)
3942 cp_parser_skip_to_end_of_block_or_statement (parser);
3943 return true;
3946 /* Consume tokens up to, and including, the next non-nested closing `)'.
3947 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3948 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3949 found an unnested token of that type. */
3951 static int
3952 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3953 bool recovering,
3954 cpp_ttype or_ttype,
3955 bool consume_paren)
3957 unsigned paren_depth = 0;
3958 unsigned brace_depth = 0;
3959 unsigned square_depth = 0;
3960 unsigned condop_depth = 0;
3962 if (recovering && or_ttype == CPP_EOF
3963 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3964 return 0;
3966 while (true)
3968 cp_token * token = cp_lexer_peek_token (parser->lexer);
3970 /* Have we found what we're looking for before the closing paren? */
3971 if (token->type == or_ttype && or_ttype != CPP_EOF
3972 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3973 return -1;
3975 switch (token->type)
3977 case CPP_PRAGMA_EOL:
3978 if (!parser->lexer->in_pragma)
3979 break;
3980 /* FALLTHRU */
3981 case CPP_EOF:
3982 /* If we've run out of tokens, then there is no closing `)'. */
3983 return 0;
3985 /* This is good for lambda expression capture-lists. */
3986 case CPP_OPEN_SQUARE:
3987 ++square_depth;
3988 break;
3989 case CPP_CLOSE_SQUARE:
3990 if (!square_depth--)
3991 return 0;
3992 break;
3994 case CPP_SEMICOLON:
3995 /* This matches the processing in skip_to_end_of_statement. */
3996 if (!brace_depth)
3997 return 0;
3998 break;
4000 case CPP_OPEN_BRACE:
4001 ++brace_depth;
4002 break;
4003 case CPP_CLOSE_BRACE:
4004 if (!brace_depth--)
4005 return 0;
4006 break;
4008 case CPP_OPEN_PAREN:
4009 if (!brace_depth)
4010 ++paren_depth;
4011 break;
4013 case CPP_CLOSE_PAREN:
4014 if (!brace_depth && !paren_depth--)
4016 if (consume_paren)
4017 cp_lexer_consume_token (parser->lexer);
4018 return 1;
4020 break;
4022 case CPP_QUERY:
4023 if (!brace_depth && !paren_depth && !square_depth)
4024 ++condop_depth;
4025 break;
4027 case CPP_COLON:
4028 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
4029 condop_depth--;
4030 break;
4032 case CPP_KEYWORD:
4033 if (!cp_token_is_module_directive (token))
4034 break;
4035 /* FALLTHROUGH */
4037 case CPP_PRAGMA:
4038 /* We fell into a pragma. Skip it, and continue. */
4039 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
4040 continue;
4042 default:
4043 break;
4046 /* Consume the token. */
4047 cp_lexer_consume_token (parser->lexer);
4051 /* Consume tokens up to, and including, the next non-nested closing `)'.
4052 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
4053 are doing error recovery. Returns -1 if OR_COMMA is true and we
4054 found an unnested token of that type. */
4056 static int
4057 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
4058 bool recovering,
4059 bool or_comma,
4060 bool consume_paren)
4062 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4063 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4064 ttype, consume_paren);
4067 /* Consume tokens until we reach the end of the current statement.
4068 Normally, that will be just before consuming a `;'. However, if a
4069 non-nested `}' comes first, then we stop before consuming that. */
4071 static void
4072 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4074 unsigned nesting_depth = 0;
4076 /* Unwind generic function template scope if necessary. */
4077 if (parser->fully_implicit_function_template_p)
4078 abort_fully_implicit_template (parser);
4080 while (true)
4082 cp_token *token = cp_lexer_peek_token (parser->lexer);
4084 switch (token->type)
4086 case CPP_PRAGMA_EOL:
4087 if (!parser->lexer->in_pragma)
4088 break;
4089 /* FALLTHRU */
4090 case CPP_EOF:
4091 /* If we've run out of tokens, stop. */
4092 return;
4094 case CPP_SEMICOLON:
4095 /* If the next token is a `;', we have reached the end of the
4096 statement. */
4097 if (!nesting_depth)
4098 return;
4099 break;
4101 case CPP_CLOSE_BRACE:
4102 /* If this is a non-nested '}', stop before consuming it.
4103 That way, when confronted with something like:
4105 { 3 + }
4107 we stop before consuming the closing '}', even though we
4108 have not yet reached a `;'. */
4109 if (nesting_depth == 0)
4110 return;
4112 /* If it is the closing '}' for a block that we have
4113 scanned, stop -- but only after consuming the token.
4114 That way given:
4116 void f g () { ... }
4117 typedef int I;
4119 we will stop after the body of the erroneously declared
4120 function, but before consuming the following `typedef'
4121 declaration. */
4122 if (--nesting_depth == 0)
4124 cp_lexer_consume_token (parser->lexer);
4125 return;
4127 break;
4129 case CPP_OPEN_BRACE:
4130 ++nesting_depth;
4131 break;
4133 case CPP_KEYWORD:
4134 if (!cp_token_is_module_directive (token))
4135 break;
4136 /* FALLTHROUGH */
4138 case CPP_PRAGMA:
4139 /* We fell into a pragma. Skip it, and continue or return. */
4140 cp_parser_skip_to_pragma_eol (parser, token);
4141 if (!nesting_depth)
4142 return;
4143 continue;
4145 default:
4146 break;
4149 /* Consume the token. */
4150 cp_lexer_consume_token (parser->lexer);
4154 /* This function is called at the end of a statement or declaration.
4155 If the next token is a semicolon, it is consumed; otherwise, error
4156 recovery is attempted. */
4158 static void
4159 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4161 /* Look for the trailing `;'. */
4162 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4164 /* If there is additional (erroneous) input, skip to the end of
4165 the statement. */
4166 cp_parser_skip_to_end_of_statement (parser);
4167 /* If the next token is now a `;', consume it. */
4168 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4169 cp_lexer_consume_token (parser->lexer);
4173 /* Skip tokens until we have consumed an entire block, or until we
4174 have consumed a non-nested `;'. */
4176 static void
4177 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4179 int nesting_depth = 0;
4181 /* Unwind generic function template scope if necessary. */
4182 if (parser->fully_implicit_function_template_p)
4183 abort_fully_implicit_template (parser);
4185 while (nesting_depth >= 0)
4187 cp_token *token = cp_lexer_peek_token (parser->lexer);
4189 switch (token->type)
4191 case CPP_PRAGMA_EOL:
4192 if (!parser->lexer->in_pragma)
4193 break;
4194 /* FALLTHRU */
4195 case CPP_EOF:
4196 /* If we've run out of tokens, stop. */
4197 return;
4199 case CPP_SEMICOLON:
4200 /* Stop if this is an unnested ';'. */
4201 if (!nesting_depth)
4202 nesting_depth = -1;
4203 break;
4205 case CPP_CLOSE_BRACE:
4206 /* Stop if this is an unnested '}', or closes the outermost
4207 nesting level. */
4208 nesting_depth--;
4209 if (nesting_depth < 0)
4210 return;
4211 if (!nesting_depth)
4212 nesting_depth = -1;
4213 break;
4215 case CPP_OPEN_BRACE:
4216 /* Nest. */
4217 nesting_depth++;
4218 break;
4220 case CPP_KEYWORD:
4221 if (!cp_token_is_module_directive (token))
4222 break;
4223 /* FALLTHROUGH */
4225 case CPP_PRAGMA:
4226 /* Skip it, and continue or return. */
4227 cp_parser_skip_to_pragma_eol (parser, token);
4228 if (!nesting_depth)
4229 return;
4230 continue;
4232 default:
4233 break;
4236 /* Consume the token. */
4237 cp_lexer_consume_token (parser->lexer);
4241 /* Skip tokens until a non-nested closing curly brace is the next
4242 token, or there are no more tokens. Return true in the first case,
4243 false otherwise. */
4245 static bool
4246 cp_parser_skip_to_closing_brace (cp_parser *parser)
4248 unsigned nesting_depth = 0;
4250 while (true)
4252 cp_token *token = cp_lexer_peek_token (parser->lexer);
4254 switch (token->type)
4256 case CPP_PRAGMA_EOL:
4257 if (!parser->lexer->in_pragma)
4258 break;
4259 /* FALLTHRU */
4260 case CPP_EOF:
4261 /* If we've run out of tokens, stop. */
4262 return false;
4264 case CPP_CLOSE_BRACE:
4265 /* If the next token is a non-nested `}', then we have reached
4266 the end of the current block. */
4267 if (nesting_depth-- == 0)
4268 return true;
4269 break;
4271 case CPP_OPEN_BRACE:
4272 /* If it the next token is a `{', then we are entering a new
4273 block. Consume the entire block. */
4274 ++nesting_depth;
4275 break;
4277 default:
4278 break;
4281 /* Consume the token. */
4282 cp_lexer_consume_token (parser->lexer);
4286 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4287 parameter is the PRAGMA token, allowing us to purge the entire pragma
4288 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4289 forwards (not error recovery). */
4291 static void
4292 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4294 cp_token *token;
4298 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4299 before an EOF token, even when the EOF is on the pragma line.
4300 We should never get here without being inside a deferred
4301 pragma. */
4302 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4303 token = cp_lexer_consume_token (parser->lexer);
4305 while (token->type != CPP_PRAGMA_EOL);
4307 if (pragma_tok)
4309 parser->lexer->in_pragma = false;
4310 if (parser->lexer->in_omp_attribute_pragma
4311 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4313 parser->lexer = parser->lexer->next;
4314 /* Put the current source position back where it was before this
4315 lexer was pushed. */
4316 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4321 /* Require pragma end of line, resyncing with it as necessary. The
4322 arguments are as for cp_parser_skip_to_pragma_eol. */
4324 static void
4325 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4327 parser->lexer->in_pragma = false;
4328 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4329 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4330 else if (parser->lexer->in_omp_attribute_pragma
4331 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4333 parser->lexer = parser->lexer->next;
4334 /* Put the current source position back where it was before this
4335 lexer was pushed. */
4336 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4340 /* This is a simple wrapper around make_typename_type. When the id is
4341 an unresolved identifier node, we can provide a superior diagnostic
4342 using cp_parser_diagnose_invalid_type_name. */
4344 static tree
4345 cp_parser_make_typename_type (cp_parser *parser, tree id,
4346 location_t id_location)
4348 tree result;
4349 if (identifier_p (id))
4351 result = make_typename_type (parser->scope, id, typename_type,
4352 /*complain=*/tf_none);
4353 if (result == error_mark_node)
4354 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4355 return result;
4357 return make_typename_type (parser->scope, id, typename_type, tf_error);
4360 /* This is a wrapper around the
4361 make_{pointer,ptrmem,reference}_declarator functions that decides
4362 which one to call based on the CODE and CLASS_TYPE arguments. The
4363 CODE argument should be one of the values returned by
4364 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4365 appertain to the pointer or reference. */
4367 static cp_declarator *
4368 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4369 cp_cv_quals cv_qualifiers,
4370 cp_declarator *target,
4371 tree attributes)
4373 if (code == ERROR_MARK || target == cp_error_declarator)
4374 return cp_error_declarator;
4376 if (code == INDIRECT_REF)
4377 if (class_type == NULL_TREE)
4378 return make_pointer_declarator (cv_qualifiers, target, attributes);
4379 else
4380 return make_ptrmem_declarator (cv_qualifiers, class_type,
4381 target, attributes);
4382 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4383 return make_reference_declarator (cv_qualifiers, target,
4384 false, attributes);
4385 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4386 return make_reference_declarator (cv_qualifiers, target,
4387 true, attributes);
4388 gcc_unreachable ();
4391 /* Create a new C++ parser. */
4393 static cp_parser *
4394 cp_parser_new (cp_lexer *lexer)
4396 /* Initialize the binops_by_token so that we can get the tree
4397 directly from the token. */
4398 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4399 binops_by_token[binops[i].token_type] = binops[i];
4401 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4402 parser->lexer = lexer;
4403 parser->context = cp_parser_context_new (NULL);
4405 /* For now, we always accept GNU extensions. */
4406 parser->allow_gnu_extensions_p = 1;
4408 /* The `>' token is a greater-than operator, not the end of a
4409 template-id. */
4410 parser->greater_than_is_operator_p = true;
4412 parser->default_arg_ok_p = true;
4414 /* We are not parsing a constant-expression. */
4415 parser->integral_constant_expression_p = false;
4416 parser->allow_non_integral_constant_expression_p = false;
4417 parser->non_integral_constant_expression_p = false;
4419 /* Local variable names are not forbidden. */
4420 parser->local_variables_forbidden_p = 0;
4422 /* We are not processing an `extern "C"' declaration. */
4423 parser->in_unbraced_linkage_specification_p = false;
4425 /* We are not processing a declarator. */
4426 parser->in_declarator_p = false;
4428 /* We are not processing a template-argument-list. */
4429 parser->in_template_argument_list_p = false;
4431 /* We are not in an iteration statement. */
4432 parser->in_statement = 0;
4434 /* We are not in a switch statement. */
4435 parser->in_switch_statement_p = false;
4437 /* We are not parsing a type-id inside an expression. */
4438 parser->in_type_id_in_expr_p = false;
4440 /* String literals should be translated to the execution character set. */
4441 parser->translate_strings_p = true;
4443 /* We are not parsing a function body. */
4444 parser->in_function_body = false;
4446 /* We can correct until told otherwise. */
4447 parser->colon_corrects_to_scope_p = true;
4449 /* The unparsed function queue is empty. */
4450 push_unparsed_function_queues (parser);
4452 /* There are no classes being defined. */
4453 parser->num_classes_being_defined = 0;
4455 /* No template parameters apply. */
4456 parser->num_template_parameter_lists = 0;
4458 /* Special parsing data structures. */
4459 parser->omp_declare_simd = NULL;
4460 parser->oacc_routine = NULL;
4462 /* Disallow OpenMP array sections in expressions. */
4463 parser->omp_array_section_p = false;
4465 /* Not declaring an implicit function template. */
4466 parser->auto_is_implicit_function_template_parm_p = false;
4467 parser->fully_implicit_function_template_p = false;
4468 parser->implicit_template_parms = 0;
4469 parser->implicit_template_scope = 0;
4471 /* Allow constrained-type-specifiers. */
4472 parser->prevent_constrained_type_specifiers = 0;
4474 /* We haven't yet seen an 'extern "C"'. */
4475 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4477 return parser;
4480 /* Create a cp_lexer structure which will emit the tokens in CACHE
4481 and push it onto the parser's lexer stack. This is used for delayed
4482 parsing of in-class method bodies and default arguments, and should
4483 not be confused with tentative parsing. */
4484 static void
4485 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4487 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4488 lexer->next = parser->lexer;
4489 parser->lexer = lexer;
4491 /* Move the current source position to that of the first token in the
4492 new lexer. */
4493 cp_lexer_set_source_position_from_token (lexer->next_token);
4496 /* Pop the top lexer off the parser stack. This is never used for the
4497 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4498 static void
4499 cp_parser_pop_lexer (cp_parser *parser)
4501 cp_lexer *lexer = parser->lexer;
4502 parser->lexer = lexer->next;
4503 cp_lexer_destroy (lexer);
4505 /* Put the current source position back where it was before this
4506 lexer was pushed. */
4507 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4510 /* Lexical conventions [gram.lex] */
4512 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4513 identifier. */
4515 static cp_expr
4516 cp_parser_identifier (cp_parser* parser)
4518 cp_token *token;
4520 /* Look for the identifier. */
4521 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4522 /* Return the value. */
4523 if (token)
4524 return cp_expr (token->u.value, token->location);
4525 else
4526 return error_mark_node;
4529 /* Worker for cp_parser_string_literal, cp_parser_userdef_string_literal
4530 and cp_parser_unevaluated_string_literal.
4531 Do not call this directly; use either of the above.
4533 Parse a sequence of adjacent string constants. Return a
4534 TREE_STRING representing the combined, nul-terminated string
4535 constant. If TRANSLATE is true, translate the string to the
4536 execution character set. If WIDE_OK is true, a wide string is
4537 valid here. If UDL_OK is true, a string literal with user-defined
4538 suffix can be used in this context. If UNEVAL is true, diagnose
4539 numeric and conditional escape sequences in it if pedantic.
4541 C++98 [lex.string] says that if a narrow string literal token is
4542 adjacent to a wide string literal token, the behavior is undefined.
4543 However, C99 6.4.5p4 says that this results in a wide string literal.
4544 We follow C99 here, for consistency with the C front end.
4546 This code is largely lifted from lex_string() in c-lex.cc.
4548 FUTURE: ObjC++ will need to handle @-strings here. */
4550 static cp_expr
4551 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4552 bool wide_ok, bool udl_ok,
4553 bool lookup_udlit, bool uneval)
4555 tree value;
4556 size_t count;
4557 struct obstack str_ob;
4558 struct obstack loc_ob;
4559 cpp_string str, istr, *strs;
4560 cp_token *tok;
4561 enum cpp_ttype type, curr_type;
4562 int have_suffix_p = 0;
4563 tree string_tree;
4564 tree suffix_id = NULL_TREE;
4565 bool curr_tok_is_userdef_p = false;
4567 tok = cp_lexer_peek_token (parser->lexer);
4568 if (!cp_parser_is_string_literal (tok))
4570 cp_parser_error (parser, "expected string-literal");
4571 return error_mark_node;
4574 location_t loc = tok->location;
4576 if (cpp_userdef_string_p (tok->type))
4578 if (!udl_ok)
4580 error_at (loc, "string literal with user-defined suffix "
4581 "is invalid in this context");
4582 return error_mark_node;
4584 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4585 curr_type = cpp_userdef_string_remove_type (tok->type);
4586 curr_tok_is_userdef_p = true;
4588 else
4590 string_tree = tok->u.value;
4591 curr_type = tok->type;
4593 type = curr_type;
4595 /* Try to avoid the overhead of creating and destroying an obstack
4596 for the common case of just one string. */
4597 if (!cp_parser_is_string_literal
4598 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4600 cp_lexer_consume_token (parser->lexer);
4602 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4603 str.len = TREE_STRING_LENGTH (string_tree);
4604 count = 1;
4606 if (curr_tok_is_userdef_p)
4608 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4609 have_suffix_p = 1;
4610 curr_type = cpp_userdef_string_remove_type (tok->type);
4612 else
4613 curr_type = tok->type;
4615 strs = &str;
4617 else
4619 location_t last_tok_loc = tok->location;
4620 gcc_obstack_init (&str_ob);
4621 gcc_obstack_init (&loc_ob);
4622 count = 0;
4626 cp_lexer_consume_token (parser->lexer);
4627 count++;
4628 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4629 str.len = TREE_STRING_LENGTH (string_tree);
4631 if (curr_tok_is_userdef_p)
4633 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4634 if (have_suffix_p == 0)
4636 suffix_id = curr_suffix_id;
4637 have_suffix_p = 1;
4639 else if (have_suffix_p == 1
4640 && curr_suffix_id != suffix_id)
4642 error ("inconsistent user-defined literal suffixes"
4643 " %qD and %qD in string literal",
4644 suffix_id, curr_suffix_id);
4645 have_suffix_p = -1;
4647 curr_type = cpp_userdef_string_remove_type (tok->type);
4649 else
4650 curr_type = tok->type;
4652 if (type != curr_type)
4654 if (type == CPP_STRING)
4655 type = curr_type;
4656 else if (curr_type != CPP_STRING)
4658 rich_location rich_loc (line_table, tok->location);
4659 rich_loc.add_range (last_tok_loc);
4660 error_at (&rich_loc,
4661 "concatenation of string literals with "
4662 "conflicting encoding prefixes");
4666 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4667 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4669 last_tok_loc = tok->location;
4671 tok = cp_lexer_peek_token (parser->lexer);
4672 if (cpp_userdef_string_p (tok->type))
4674 if (!udl_ok)
4676 error_at (loc, "string literal with user-defined suffix "
4677 "is invalid in this context");
4678 return error_mark_node;
4680 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4681 curr_type = cpp_userdef_string_remove_type (tok->type);
4682 curr_tok_is_userdef_p = true;
4684 else
4686 string_tree = tok->u.value;
4687 curr_type = tok->type;
4688 curr_tok_is_userdef_p = false;
4691 while (cp_parser_is_string_literal (tok));
4693 /* A string literal built by concatenation has its caret=start at
4694 the start of the initial string, and its finish at the finish of
4695 the final string literal. */
4696 loc = make_location (loc, loc, get_finish (last_tok_loc));
4698 strs = (cpp_string *) obstack_finish (&str_ob);
4701 if (type != CPP_STRING && !wide_ok)
4703 cp_parser_error (parser, "a wide string is invalid in this context");
4704 type = CPP_STRING;
4706 if (uneval)
4707 type = CPP_UNEVAL_STRING;
4709 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4710 (parse_in, strs, count, &istr, type))
4712 value = build_string (istr.len, (const char *)istr.text);
4713 free (CONST_CAST (unsigned char *, istr.text));
4714 if (count > 1)
4716 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4717 gcc_assert (g_string_concat_db);
4718 g_string_concat_db->record_string_concatenation (count, locs);
4721 switch (type)
4723 default:
4724 case CPP_STRING:
4725 TREE_TYPE (value) = char_array_type_node;
4726 break;
4727 case CPP_UTF8STRING:
4728 if (flag_char8_t)
4729 TREE_TYPE (value) = char8_array_type_node;
4730 else
4731 TREE_TYPE (value) = char_array_type_node;
4732 break;
4733 case CPP_STRING16:
4734 TREE_TYPE (value) = char16_array_type_node;
4735 break;
4736 case CPP_STRING32:
4737 TREE_TYPE (value) = char32_array_type_node;
4738 break;
4739 case CPP_WSTRING:
4740 TREE_TYPE (value) = wchar_array_type_node;
4741 break;
4744 value = fix_string_type (value);
4746 if (have_suffix_p)
4748 tree literal = build_userdef_literal (suffix_id, value,
4749 OT_NONE, NULL_TREE);
4750 if (lookup_udlit)
4751 value = finish_userdef_string_literal (literal);
4752 else
4753 value = literal;
4756 else
4757 /* cpp_interpret_string has issued an error. */
4758 value = error_mark_node;
4760 if (count > 1)
4762 obstack_free (&str_ob, 0);
4763 obstack_free (&loc_ob, 0);
4766 return cp_expr (value, loc);
4769 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4770 representing the combined, nul-terminated string constant. If
4771 TRANSLATE is true, translate the string to the execution character set.
4772 If WIDE_OK is true, a wide string is valid here.
4774 This function issues an error if a user defined string literal is
4775 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4777 static inline cp_expr
4778 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4780 return cp_parser_string_literal_common (parser, translate, wide_ok,
4781 /*udl_ok=*/false,
4782 /*lookup_udlit=*/false,
4783 /*uneval=*/false);
4786 /* Parse a string literal or user defined string literal.
4788 user-defined-string-literal :
4789 string-literal ud-suffix
4791 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4793 static inline cp_expr
4794 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4796 return cp_parser_string_literal_common (parser, /*translate=*/true,
4797 /*wide_ok=*/true, /*udl_ok=*/true,
4798 lookup_udlit, /*uneval=*/false);
4801 /* Parse an unevaluated string literal.
4803 unevaluated-string:
4804 string-literal */
4806 static inline cp_expr
4807 cp_parser_unevaluated_string_literal (cp_parser *parser)
4809 return cp_parser_string_literal_common (parser, /*translate=*/false,
4810 /*wide_ok=*/false, /*udl_ok=*/false,
4811 /*lookup_udlit=*/false,
4812 /*uneval=*/true);
4815 /* Look up a literal operator with the name and the exact arguments. */
4817 static tree
4818 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4820 tree decl = lookup_name (name);
4821 if (!decl || !is_overloaded_fn (decl))
4822 return error_mark_node;
4824 for (lkp_iterator iter (decl); iter; ++iter)
4826 tree fn = *iter;
4828 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4830 unsigned int ix;
4831 bool found = true;
4833 for (ix = 0;
4834 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4835 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4837 tree tparm = TREE_VALUE (parmtypes);
4838 tree targ = TREE_TYPE ((*args)[ix]);
4839 bool ptr = TYPE_PTR_P (tparm);
4840 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4841 if ((ptr || arr || !same_type_p (tparm, targ))
4842 && (!ptr || !arr
4843 || !same_type_p (TREE_TYPE (tparm),
4844 TREE_TYPE (targ))))
4845 found = false;
4848 if (found
4849 && ix == vec_safe_length (args)
4850 /* May be this should be sufficient_parms_p instead,
4851 depending on how exactly should user-defined literals
4852 work in presence of default arguments on the literal
4853 operator parameters. */
4854 && parmtypes == void_list_node)
4855 return decl;
4859 return error_mark_node;
4862 /* Parse a user-defined char constant. Returns a call to a user-defined
4863 literal operator taking the character as an argument. */
4865 static cp_expr
4866 cp_parser_userdef_char_literal (cp_parser *parser)
4868 cp_token *token = cp_lexer_consume_token (parser->lexer);
4869 tree literal = token->u.value;
4870 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4871 tree value = USERDEF_LITERAL_VALUE (literal);
4872 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4873 tree decl, result;
4875 /* Build up a call to the user-defined operator */
4876 /* Lookup the name we got back from the id-expression. */
4877 releasing_vec args;
4878 vec_safe_push (args, value);
4879 decl = lookup_literal_operator (name, args);
4880 if (!decl || decl == error_mark_node)
4882 error ("unable to find character literal operator %qD with %qT argument",
4883 name, TREE_TYPE (value));
4884 return error_mark_node;
4886 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4887 return result;
4890 /* A subroutine of cp_parser_userdef_numeric_literal to
4891 create a char... template parameter pack from a string node. */
4893 static tree
4894 make_char_string_pack (tree value)
4896 tree charvec;
4897 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4898 const unsigned char *str
4899 = (const unsigned char *) TREE_STRING_POINTER (value);
4900 int i, len = TREE_STRING_LENGTH (value) - 1;
4901 tree argvec = make_tree_vec (1);
4903 /* Fill in CHARVEC with all of the parameters. */
4904 charvec = make_tree_vec (len);
4905 for (i = 0; i < len; ++i)
4907 unsigned char s[3] = { '\'', str[i], '\'' };
4908 cpp_string in = { 3, s };
4909 cpp_string out = { 0, 0 };
4910 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4911 return NULL_TREE;
4912 gcc_assert (out.len == 2);
4913 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4914 out.text[0]);
4917 /* Build the argument packs. */
4918 ARGUMENT_PACK_ARGS (argpack) = charvec;
4920 TREE_VEC_ELT (argvec, 0) = argpack;
4922 return argvec;
4925 /* A subroutine of cp_parser_userdef_numeric_literal to
4926 create a char... template parameter pack from a string node. */
4928 static tree
4929 make_string_pack (tree value)
4931 tree charvec;
4932 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4933 const unsigned char *str
4934 = (const unsigned char *) TREE_STRING_POINTER (value);
4935 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4936 int len = TREE_STRING_LENGTH (value) / sz - 1;
4937 tree argvec = make_tree_vec (2);
4939 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4940 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4942 /* First template parm is character type. */
4943 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4945 /* Fill in CHARVEC with all of the parameters. */
4946 charvec = make_tree_vec (len);
4947 for (int i = 0; i < len; ++i)
4948 TREE_VEC_ELT (charvec, i)
4949 = double_int_to_tree (str_char_type_node,
4950 double_int::from_buffer (str + i * sz, sz));
4952 /* Build the argument packs. */
4953 ARGUMENT_PACK_ARGS (argpack) = charvec;
4955 TREE_VEC_ELT (argvec, 1) = argpack;
4957 return argvec;
4960 /* Parse a user-defined numeric constant. returns a call to a user-defined
4961 literal operator. */
4963 static cp_expr
4964 cp_parser_userdef_numeric_literal (cp_parser *parser)
4966 cp_token *token = cp_lexer_consume_token (parser->lexer);
4967 tree literal = token->u.value;
4968 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4969 tree value = USERDEF_LITERAL_VALUE (literal);
4970 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4971 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4972 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4973 tree decl, result;
4975 /* Look for a literal operator taking the exact type of numeric argument
4976 as the literal value. */
4977 releasing_vec args;
4978 vec_safe_push (args, value);
4979 decl = lookup_literal_operator (name, args);
4980 if (decl && decl != error_mark_node)
4982 result = finish_call_expr (decl, &args, false, true,
4983 tf_warning_or_error);
4985 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4987 warning_at (token->location, OPT_Woverflow,
4988 "integer literal exceeds range of %qT type",
4989 long_long_unsigned_type_node);
4991 else
4993 if (overflow > 0)
4994 warning_at (token->location, OPT_Woverflow,
4995 "floating literal exceeds range of %qT type",
4996 long_double_type_node);
4997 else if (overflow < 0)
4998 warning_at (token->location, OPT_Woverflow,
4999 "floating literal truncated to zero");
5002 return result;
5005 /* If the numeric argument didn't work, look for a raw literal
5006 operator taking a const char* argument consisting of the number
5007 in string format. */
5008 args->truncate (0);
5009 vec_safe_push (args, num_string);
5010 decl = lookup_literal_operator (name, args);
5011 if (decl && decl != error_mark_node)
5013 result = finish_call_expr (decl, &args, false, true,
5014 tf_warning_or_error);
5015 return result;
5018 /* If the raw literal didn't work, look for a non-type template
5019 function with parameter pack char.... Call the function with
5020 template parameter characters representing the number. */
5021 args->truncate (0);
5022 decl = lookup_literal_operator (name, args);
5023 if (decl && decl != error_mark_node)
5025 tree tmpl_args = make_char_string_pack (num_string);
5026 if (tmpl_args == NULL_TREE)
5028 error ("failed to translate literal to execution character set %qT",
5029 num_string);
5030 return error_mark_node;
5032 decl = lookup_template_function (decl, tmpl_args);
5033 result = finish_call_expr (decl, &args, false, true,
5034 tf_warning_or_error);
5035 return result;
5038 /* In C++14 the standard library defines complex number suffixes that
5039 conflict with GNU extensions. Prefer them if <complex> is #included. */
5040 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
5041 bool i14 = (cxx_dialect > cxx11
5042 && (id_equal (suffix_id, "i")
5043 || id_equal (suffix_id, "if")
5044 || id_equal (suffix_id, "il")));
5045 diagnostic_t kind = DK_ERROR;
5046 int opt = 0;
5048 if (i14 && ext)
5050 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
5051 LOOK_want::NORMAL, false);
5052 if (cxlit == error_mark_node)
5054 /* No <complex>, so pedwarn and use GNU semantics. */
5055 kind = DK_PEDWARN;
5056 opt = OPT_Wpedantic;
5060 bool complained
5061 = emit_diagnostic (kind, input_location, opt,
5062 "unable to find numeric literal operator %qD", name);
5064 if (!complained)
5065 /* Don't inform either. */;
5066 else if (i14)
5068 inform (token->location, "add %<using namespace std::complex_literals%> "
5069 "(from %<<complex>%>) to enable the C++14 user-defined literal "
5070 "suffixes");
5071 if (ext)
5072 inform (token->location, "or use %<j%> instead of %<i%> for the "
5073 "GNU built-in suffix");
5075 else if (!ext)
5076 inform (token->location, "use %<-fext-numeric-literals%> "
5077 "to enable more built-in suffixes");
5079 if (kind == DK_ERROR)
5080 value = error_mark_node;
5081 else
5083 /* Use the built-in semantics. */
5084 tree type;
5085 if (id_equal (suffix_id, "i"))
5087 if (TREE_CODE (value) == INTEGER_CST)
5088 type = integer_type_node;
5089 else
5090 type = double_type_node;
5092 else if (id_equal (suffix_id, "if"))
5093 type = float_type_node;
5094 else /* if (id_equal (suffix_id, "il")) */
5095 type = long_double_type_node;
5097 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5098 build_zero_cst (type), fold_convert (type, value));
5101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5102 /* Avoid repeated diagnostics. */
5103 token->u.value = value;
5104 return value;
5107 /* Parse a user-defined string constant. Returns a call to a user-defined
5108 literal operator taking a character pointer and the length of the string
5109 as arguments. */
5111 static tree
5112 finish_userdef_string_literal (tree literal)
5114 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5115 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5116 tree value = USERDEF_LITERAL_VALUE (literal);
5117 int len = TREE_STRING_LENGTH (value)
5118 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5119 tree decl;
5121 /* Build up a call to the user-defined operator. */
5122 /* Lookup the name we got back from the id-expression. */
5123 releasing_vec args;
5124 vec_safe_push (args, value);
5125 vec_safe_push (args, build_int_cst (size_type_node, len));
5126 decl = lookup_literal_operator (name, args);
5128 if (decl && decl != error_mark_node)
5129 return finish_call_expr (decl, &args, false, true,
5130 tf_warning_or_error);
5132 /* Look for a suitable template function, either (C++20) with a single
5133 parameter of class type, or (N3599) with typename parameter CharT and
5134 parameter pack CharT... */
5135 args->truncate (0);
5136 decl = lookup_literal_operator (name, args);
5137 if (decl && decl != error_mark_node)
5139 /* Use resolve_nondeduced_context to try to choose one form of template
5140 or the other. */
5141 tree tmpl_args = make_tree_vec (1);
5142 TREE_VEC_ELT (tmpl_args, 0) = value;
5143 decl = lookup_template_function (decl, tmpl_args);
5144 tree res = resolve_nondeduced_context (decl, tf_none);
5145 if (DECL_P (res))
5146 decl = res;
5147 else
5149 TREE_OPERAND (decl, 1) = make_string_pack (value);
5150 res = resolve_nondeduced_context (decl, tf_none);
5151 if (DECL_P (res))
5152 decl = res;
5154 if (!DECL_P (decl) && cxx_dialect > cxx17)
5155 TREE_OPERAND (decl, 1) = tmpl_args;
5156 return finish_call_expr (decl, &args, false, true,
5157 tf_warning_or_error);
5160 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5161 name, TREE_TYPE (value), size_type_node);
5162 return error_mark_node;
5166 /* Basic concepts [gram.basic] */
5168 /* Parse a translation-unit.
5170 translation-unit:
5171 declaration-seq [opt] */
5173 static void
5174 cp_parser_translation_unit (cp_parser* parser)
5176 gcc_checking_assert (!cp_error_declarator);
5178 /* Create the declarator obstack. */
5179 gcc_obstack_init (&declarator_obstack);
5180 /* Create the error declarator. */
5181 cp_error_declarator = make_declarator (cdk_error);
5182 /* Create the empty parameter list. */
5183 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5184 UNKNOWN_LOCATION);
5185 /* Remember where the base of the declarator obstack lies. */
5186 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5188 push_deferring_access_checks (flag_access_control
5189 ? dk_no_deferred : dk_no_check);
5191 module_parse mp_state = MP_NOT_MODULE;
5192 if (modules_p () && !header_module_p ())
5193 mp_state = MP_FIRST;
5195 bool implicit_extern_c = false;
5197 /* Parse until EOF. */
5198 for (;;)
5200 cp_token *token = cp_lexer_peek_token (parser->lexer);
5202 /* If we're entering or exiting a region that's implicitly
5203 extern "C", modify the lang context appropriately. This is
5204 so horrible. Please die. */
5205 if (implicit_extern_c
5206 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5208 implicit_extern_c = !implicit_extern_c;
5209 if (implicit_extern_c)
5210 push_lang_context (lang_name_c);
5211 else
5212 pop_lang_context ();
5215 if (token->type == CPP_EOF)
5216 break;
5218 if (modules_p ())
5220 /* Top-level module declarations are ok, and change the
5221 portion of file we're in. Top-level import declarations
5222 are significant for the import portions. */
5224 cp_token *next = token;
5225 bool exporting = token->keyword == RID__EXPORT;
5226 if (exporting)
5228 cp_lexer_consume_token (parser->lexer);
5229 next = cp_lexer_peek_token (parser->lexer);
5231 if (next->keyword == RID__MODULE)
5233 mp_state
5234 = cp_parser_module_declaration (parser, mp_state, exporting);
5235 continue;
5237 else if (next->keyword == RID__IMPORT)
5239 if (mp_state == MP_FIRST)
5240 mp_state = MP_NOT_MODULE;
5241 cp_parser_import_declaration (parser, mp_state, exporting);
5242 continue;
5244 else
5245 gcc_checking_assert (!exporting);
5247 if (mp_state == MP_GLOBAL && token->main_source_p)
5249 static bool warned = false;
5250 if (!warned)
5252 warned = true;
5253 error_at (token->location,
5254 "global module fragment contents must be"
5255 " from preprocessor inclusion");
5260 /* This relies on the ordering of module_parse values. */
5261 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5262 /* We're no longer in the import portion of a named module. */
5263 mp_state = module_parse (mp_state + 1);
5264 else if (mp_state == MP_FIRST)
5265 mp_state = MP_NOT_MODULE;
5267 if (token->type == CPP_CLOSE_BRACE)
5269 cp_parser_error (parser, "expected declaration");
5270 cp_lexer_consume_token (parser->lexer);
5271 /* If the next token is now a `;', consume it. */
5272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5273 cp_lexer_consume_token (parser->lexer);
5275 else
5276 cp_parser_toplevel_declaration (parser);
5279 /* Get rid of the token array; we don't need it any more. */
5280 cp_lexer_destroy (parser->lexer);
5281 parser->lexer = NULL;
5283 /* The EOF should have reset this. */
5284 gcc_checking_assert (!implicit_extern_c);
5286 /* Make sure the declarator obstack was fully cleaned up. */
5287 gcc_assert (obstack_next_free (&declarator_obstack)
5288 == declarator_obstack_base);
5291 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5292 decltype context. */
5294 static inline tsubst_flags_t
5295 complain_flags (bool decltype_p)
5297 tsubst_flags_t complain = tf_warning_or_error;
5298 if (decltype_p)
5299 complain |= tf_decltype;
5300 return complain;
5303 /* We're about to parse a collection of statements. If we're currently
5304 parsing tentatively, set up a firewall so that any nested
5305 cp_parser_commit_to_tentative_parse won't affect the current context. */
5307 static cp_token_position
5308 cp_parser_start_tentative_firewall (cp_parser *parser)
5310 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5311 return 0;
5313 cp_parser_parse_tentatively (parser);
5314 cp_parser_commit_to_topmost_tentative_parse (parser);
5315 return cp_lexer_token_position (parser->lexer, false);
5318 /* We've finished parsing the collection of statements. Wrap up the
5319 firewall and replace the relevant tokens with the parsed form. */
5321 static void
5322 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5323 tree expr)
5325 if (!start)
5326 return;
5328 /* Finish the firewall level. */
5329 cp_parser_parse_definitely (parser);
5330 /* And remember the result of the parse for when we try again. */
5331 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5332 token->type = CPP_PREPARSED_EXPR;
5333 token->u.value = expr;
5334 token->keyword = RID_MAX;
5335 cp_lexer_purge_tokens_after (parser->lexer, start);
5338 /* Like the above functions, but let the user modify the tokens. Used by
5339 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5340 later parses, so it makes sense to localize the effects of
5341 cp_parser_commit_to_tentative_parse. */
5343 struct tentative_firewall
5345 cp_parser *parser;
5346 bool set;
5348 tentative_firewall (cp_parser *p): parser(p)
5350 /* If we're currently parsing tentatively, start a committed level as a
5351 firewall and then an inner tentative parse. */
5352 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5354 cp_parser_parse_tentatively (parser);
5355 cp_parser_commit_to_topmost_tentative_parse (parser);
5356 cp_parser_parse_tentatively (parser);
5360 ~tentative_firewall()
5362 if (set)
5364 /* Finish the inner tentative parse and the firewall, propagating any
5365 uncommitted error state to the outer tentative parse. */
5366 bool err = cp_parser_error_occurred (parser);
5367 cp_parser_parse_definitely (parser);
5368 cp_parser_parse_definitely (parser);
5369 if (err)
5370 cp_parser_simulate_error (parser);
5375 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5376 This class is for tracking such a matching pair of symbols.
5377 In particular, it tracks the location of the first token,
5378 so that if the second token is missing, we can highlight the
5379 location of the first token when notifying the user about the
5380 problem. */
5382 template <typename traits_t>
5383 class token_pair
5385 public:
5386 /* token_pair's ctor. */
5387 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5389 /* If the next token is the opening symbol for this pair, consume it and
5390 return true.
5391 Otherwise, issue an error and return false.
5392 In either case, record the location of the opening token. */
5394 bool require_open (cp_parser *parser)
5396 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5397 return cp_parser_require (parser, traits_t::open_token_type,
5398 traits_t::required_token_open);
5401 /* Consume the next token from PARSER, recording its location as
5402 that of the opening token within the pair. */
5404 cp_token * consume_open (cp_parser *parser)
5406 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5407 gcc_assert (tok->type == traits_t::open_token_type);
5408 m_open_loc = tok->location;
5409 return tok;
5412 /* If the next token is the closing symbol for this pair, consume it
5413 and return it.
5414 Otherwise, issue an error, highlighting the location of the
5415 corresponding opening token, and return NULL. */
5417 cp_token *require_close (cp_parser *parser) const
5419 return cp_parser_require (parser, traits_t::close_token_type,
5420 traits_t::required_token_close,
5421 m_open_loc);
5424 location_t open_location () const { return m_open_loc; }
5426 private:
5427 location_t m_open_loc;
5430 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5432 struct matching_paren_traits
5434 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5435 static const enum required_token required_token_open = RT_OPEN_PAREN;
5436 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5437 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5440 /* "matching_parens" is a token_pair<T> class for tracking matching
5441 pairs of parentheses. */
5443 typedef token_pair<matching_paren_traits> matching_parens;
5445 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5447 struct matching_brace_traits
5449 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5450 static const enum required_token required_token_open = RT_OPEN_BRACE;
5451 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5452 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5455 /* "matching_braces" is a token_pair<T> class for tracking matching
5456 pairs of braces. */
5458 typedef token_pair<matching_brace_traits> matching_braces;
5461 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5462 enclosing parentheses. */
5464 static cp_expr
5465 cp_parser_statement_expr (cp_parser *parser)
5467 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5468 auto oas = make_temp_override (parser->omp_array_section_p, false);
5470 /* Consume the '('. */
5471 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5472 matching_parens parens;
5473 parens.consume_open (parser);
5474 /* Start the statement-expression. */
5475 tree expr = begin_stmt_expr ();
5476 /* Parse the compound-statement. */
5477 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5478 /* Finish up. */
5479 expr = finish_stmt_expr (expr, false);
5480 /* Consume the ')'. */
5481 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5482 if (!parens.require_close (parser))
5483 cp_parser_skip_to_end_of_statement (parser);
5485 cp_parser_end_tentative_firewall (parser, start, expr);
5486 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5487 return cp_expr (expr, combined_loc);
5490 /* Expressions [gram.expr] */
5492 /* Parse a fold-operator.
5494 fold-operator:
5495 - * / % ^ & | = < > << >>
5496 = -= *= /= %= ^= &= |= <<= >>=
5497 == != <= >= && || , .* ->*
5499 This returns the tree code corresponding to the matched operator
5500 as an int. When the current token matches a compound assignment
5501 operator, the resulting tree code is the negative value of the
5502 non-assignment operator. */
5504 static int
5505 cp_parser_fold_operator (cp_token *token)
5507 switch (token->type)
5509 case CPP_PLUS: return PLUS_EXPR;
5510 case CPP_MINUS: return MINUS_EXPR;
5511 case CPP_MULT: return MULT_EXPR;
5512 case CPP_DIV: return TRUNC_DIV_EXPR;
5513 case CPP_MOD: return TRUNC_MOD_EXPR;
5514 case CPP_XOR: return BIT_XOR_EXPR;
5515 case CPP_AND: return BIT_AND_EXPR;
5516 case CPP_OR: return BIT_IOR_EXPR;
5517 case CPP_LSHIFT: return LSHIFT_EXPR;
5518 case CPP_RSHIFT: return RSHIFT_EXPR;
5520 case CPP_EQ: return -NOP_EXPR;
5521 case CPP_PLUS_EQ: return -PLUS_EXPR;
5522 case CPP_MINUS_EQ: return -MINUS_EXPR;
5523 case CPP_MULT_EQ: return -MULT_EXPR;
5524 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5525 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5526 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5527 case CPP_AND_EQ: return -BIT_AND_EXPR;
5528 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5529 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5530 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5532 case CPP_EQ_EQ: return EQ_EXPR;
5533 case CPP_NOT_EQ: return NE_EXPR;
5534 case CPP_LESS: return LT_EXPR;
5535 case CPP_GREATER: return GT_EXPR;
5536 case CPP_LESS_EQ: return LE_EXPR;
5537 case CPP_GREATER_EQ: return GE_EXPR;
5539 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5540 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5542 case CPP_COMMA: return COMPOUND_EXPR;
5544 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5545 case CPP_DEREF_STAR: return MEMBER_REF;
5547 default: return ERROR_MARK;
5551 /* Returns true if CODE indicates a binary expression, which is not allowed in
5552 the LHS of a fold-expression. More codes will need to be added to use this
5553 function in other contexts. */
5555 static bool
5556 is_binary_op (tree_code code)
5558 switch (code)
5560 case PLUS_EXPR:
5561 case POINTER_PLUS_EXPR:
5562 case MINUS_EXPR:
5563 case MULT_EXPR:
5564 case TRUNC_DIV_EXPR:
5565 case TRUNC_MOD_EXPR:
5566 case BIT_XOR_EXPR:
5567 case BIT_AND_EXPR:
5568 case BIT_IOR_EXPR:
5569 case LSHIFT_EXPR:
5570 case RSHIFT_EXPR:
5572 case MODOP_EXPR:
5574 case EQ_EXPR:
5575 case NE_EXPR:
5576 case LE_EXPR:
5577 case GE_EXPR:
5578 case LT_EXPR:
5579 case GT_EXPR:
5581 case TRUTH_ANDIF_EXPR:
5582 case TRUTH_ORIF_EXPR:
5584 case COMPOUND_EXPR:
5586 case DOTSTAR_EXPR:
5587 case MEMBER_REF:
5588 return true;
5590 default:
5591 return false;
5595 /* If the next token is a suitable fold operator, consume it and return as
5596 the function above. */
5598 static int
5599 cp_parser_fold_operator (cp_parser *parser)
5601 cp_token* token = cp_lexer_peek_token (parser->lexer);
5602 int code = cp_parser_fold_operator (token);
5603 if (code != ERROR_MARK)
5604 cp_lexer_consume_token (parser->lexer);
5605 return code;
5608 /* Parse a fold-expression.
5610 fold-expression:
5611 ( ... folding-operator cast-expression)
5612 ( cast-expression folding-operator ... )
5613 ( cast-expression folding operator ... folding-operator cast-expression)
5615 Note that the '(' and ')' are matched in primary expression. */
5617 static cp_expr
5618 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5620 cp_id_kind pidk;
5621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5622 const cp_token *token = nullptr;
5624 // Left fold.
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5627 if (expr1)
5628 return error_mark_node;
5629 cp_lexer_consume_token (parser->lexer);
5630 token = cp_lexer_peek_token (parser->lexer);
5631 int op = cp_parser_fold_operator (parser);
5632 if (op == ERROR_MARK)
5634 cp_parser_error (parser, "expected binary operator");
5635 return error_mark_node;
5638 tree expr = cp_parser_cast_expression (parser, false, false,
5639 false, &pidk);
5640 if (expr == error_mark_node)
5641 return error_mark_node;
5642 loc = make_location (token->location, loc, parser->lexer);
5643 return finish_left_unary_fold_expr (loc, expr, op);
5646 token = cp_lexer_peek_token (parser->lexer);
5647 int op = cp_parser_fold_operator (parser);
5648 if (op == ERROR_MARK)
5650 cp_parser_error (parser, "expected binary operator");
5651 return error_mark_node;
5654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5656 cp_parser_error (parser, "expected ...");
5657 return error_mark_node;
5659 cp_lexer_consume_token (parser->lexer);
5661 /* The operands of a fold-expression are cast-expressions, so binary or
5662 conditional expressions are not allowed. We check this here to avoid
5663 tentative parsing. */
5664 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5665 /* OK, the expression was parenthesized. */;
5666 else if (is_binary_op (TREE_CODE (expr1)))
5667 error_at (location_of (expr1),
5668 "binary expression in operand of fold-expression");
5669 else if (TREE_CODE (expr1) == COND_EXPR
5670 || (REFERENCE_REF_P (expr1)
5671 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5672 error_at (location_of (expr1),
5673 "conditional expression in operand of fold-expression");
5675 // Right fold.
5676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5678 loc = make_location (token->location, loc, parser->lexer);
5679 return finish_right_unary_fold_expr (loc, expr1, op);
5682 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5684 cp_parser_error (parser, "mismatched operator in fold-expression");
5685 return error_mark_node;
5687 cp_lexer_consume_token (parser->lexer);
5689 // Binary left or right fold.
5690 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5691 if (expr2 == error_mark_node)
5692 return error_mark_node;
5693 loc = make_location (token->location, loc, parser->lexer);
5694 return finish_binary_fold_expr (loc, expr1, expr2, op);
5697 /* Parse a primary-expression.
5699 primary-expression:
5700 literal
5701 this
5702 ( expression )
5703 id-expression
5704 lambda-expression (C++11)
5706 GNU Extensions:
5708 primary-expression:
5709 ( compound-statement )
5710 __builtin_va_arg ( assignment-expression , type-id )
5711 __builtin_offsetof ( type-id , offsetof-expression )
5713 C++ Extensions:
5714 __has_nothrow_assign ( type-id )
5715 __has_nothrow_constructor ( type-id )
5716 __has_nothrow_copy ( type-id )
5717 __has_trivial_assign ( type-id )
5718 __has_trivial_constructor ( type-id )
5719 __has_trivial_copy ( type-id )
5720 __has_trivial_destructor ( type-id )
5721 __has_virtual_destructor ( type-id )
5722 __is_abstract ( type-id )
5723 __is_base_of ( type-id , type-id )
5724 __is_class ( type-id )
5725 __is_empty ( type-id )
5726 __is_enum ( type-id )
5727 __is_final ( type-id )
5728 __is_literal_type ( type-id )
5729 __is_pod ( type-id )
5730 __is_polymorphic ( type-id )
5731 __is_std_layout ( type-id )
5732 __is_trivial ( type-id )
5733 __is_union ( type-id )
5735 Objective-C++ Extension:
5737 primary-expression:
5738 objc-expression
5740 literal:
5741 __null
5743 ADDRESS_P is true iff this expression was immediately preceded by
5744 "&" and therefore might denote a pointer-to-member. CAST_P is true
5745 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5746 true iff this expression is a template argument.
5748 Returns a representation of the expression. Upon return, *IDK
5749 indicates what kind of id-expression (if any) was present. */
5751 static cp_expr
5752 cp_parser_primary_expression (cp_parser *parser,
5753 bool address_p,
5754 bool cast_p,
5755 bool template_arg_p,
5756 bool decltype_p,
5757 cp_id_kind *idk)
5759 cp_token *token = NULL;
5761 /* Assume the primary expression is not an id-expression. */
5762 *idk = CP_ID_KIND_NONE;
5764 /* Peek at the next token. */
5765 token = cp_lexer_peek_token (parser->lexer);
5766 switch ((int) token->type)
5768 /* literal:
5769 integer-literal
5770 character-literal
5771 floating-literal
5772 string-literal
5773 boolean-literal
5774 pointer-literal
5775 user-defined-literal */
5776 case CPP_CHAR:
5777 case CPP_CHAR16:
5778 case CPP_CHAR32:
5779 case CPP_WCHAR:
5780 case CPP_UTF8CHAR:
5781 case CPP_NUMBER:
5782 case CPP_PREPARSED_EXPR:
5783 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5784 return cp_parser_userdef_numeric_literal (parser);
5785 token = cp_lexer_consume_token (parser->lexer);
5786 if (TREE_CODE (token->u.value) == FIXED_CST)
5788 error_at (token->location,
5789 "fixed-point types not supported in C++");
5790 return error_mark_node;
5792 /* Floating-point literals are only allowed in an integral
5793 constant expression if they are cast to an integral or
5794 enumeration type. */
5795 if ((TREE_CODE (token->u.value) == REAL_CST
5796 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5797 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5798 && parser->integral_constant_expression_p
5799 && pedantic)
5801 /* CAST_P will be set even in invalid code like "int(2.7 +
5802 ...)". Therefore, we have to check that the next token
5803 is sure to end the cast. */
5804 if (cast_p)
5806 cp_token *next_token;
5808 next_token = cp_lexer_peek_token (parser->lexer);
5809 if (/* The comma at the end of an
5810 enumerator-definition. */
5811 next_token->type != CPP_COMMA
5812 /* The curly brace at the end of an enum-specifier. */
5813 && next_token->type != CPP_CLOSE_BRACE
5814 /* The end of a statement. */
5815 && next_token->type != CPP_SEMICOLON
5816 /* The end of the cast-expression. */
5817 && next_token->type != CPP_CLOSE_PAREN
5818 /* The end of an array bound. */
5819 && next_token->type != CPP_CLOSE_SQUARE
5820 /* The closing ">" in a template-argument-list. */
5821 && (next_token->type != CPP_GREATER
5822 || parser->greater_than_is_operator_p)
5823 /* C++0x only: A ">>" treated like two ">" tokens,
5824 in a template-argument-list. */
5825 && (next_token->type != CPP_RSHIFT
5826 || (cxx_dialect == cxx98)
5827 || parser->greater_than_is_operator_p))
5828 cast_p = false;
5831 /* If we are within a cast, then the constraint that the
5832 cast is to an integral or enumeration type will be
5833 checked at that point. If we are not within a cast, then
5834 this code is invalid. */
5835 if (!cast_p)
5836 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5838 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5839 .maybe_add_location_wrapper ());
5841 case CPP_CHAR_USERDEF:
5842 case CPP_CHAR16_USERDEF:
5843 case CPP_CHAR32_USERDEF:
5844 case CPP_WCHAR_USERDEF:
5845 case CPP_UTF8CHAR_USERDEF:
5846 return cp_parser_userdef_char_literal (parser);
5848 case CPP_STRING:
5849 case CPP_STRING16:
5850 case CPP_STRING32:
5851 case CPP_WSTRING:
5852 case CPP_UTF8STRING:
5853 case CPP_STRING_USERDEF:
5854 case CPP_STRING16_USERDEF:
5855 case CPP_STRING32_USERDEF:
5856 case CPP_WSTRING_USERDEF:
5857 case CPP_UTF8STRING_USERDEF:
5858 /* ??? Should wide strings be allowed when parser->translate_strings_p
5859 is false (i.e. in attributes)? If not, we can kill the third
5860 argument to cp_parser_string_literal. */
5861 if (parser->translate_strings_p)
5862 return (cp_parser_userdef_string_literal (parser,
5863 /*lookup_udlit=*/true)
5864 .maybe_add_location_wrapper ());
5865 else
5866 return (cp_parser_string_literal (parser,
5867 /*translate=*/false,
5868 /*wide_ok=*/true)
5869 .maybe_add_location_wrapper ());
5871 case CPP_OPEN_PAREN:
5872 /* If we see `( { ' then we are looking at the beginning of
5873 a GNU statement-expression. */
5874 if (cp_parser_allow_gnu_extensions_p (parser)
5875 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5877 /* Statement-expressions are not allowed by the standard. */
5878 pedwarn (token->location, OPT_Wpedantic,
5879 "ISO C++ forbids braced-groups within expressions");
5881 /* And they're not allowed outside of a function-body; you
5882 cannot, for example, write:
5884 int i = ({ int j = 3; j + 1; });
5886 at class or namespace scope. */
5887 if (!parser->in_function_body
5888 || parser->in_template_argument_list_p)
5890 error_at (token->location,
5891 "statement-expressions are not allowed outside "
5892 "functions nor in template-argument lists");
5893 cp_parser_skip_to_end_of_block_or_statement (parser);
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5895 cp_lexer_consume_token (parser->lexer);
5896 return error_mark_node;
5898 else
5899 return cp_parser_statement_expr (parser);
5901 /* Otherwise it's a normal parenthesized expression. */
5903 cp_expr expr;
5904 bool saved_greater_than_is_operator_p;
5906 location_t open_paren_loc = token->location;
5908 /* Consume the `('. */
5909 matching_parens parens;
5910 parens.consume_open (parser);
5911 /* Within a parenthesized expression, a `>' token is always
5912 the greater-than operator. */
5913 saved_greater_than_is_operator_p
5914 = parser->greater_than_is_operator_p;
5915 parser->greater_than_is_operator_p = true;
5917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5918 /* Left fold expression. */
5919 expr = NULL_TREE;
5920 else
5921 /* Parse the parenthesized expression. */
5922 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5927 expr = cp_parser_fold_expression (parser, expr);
5928 if (expr != error_mark_node
5929 && cxx_dialect < cxx17)
5930 pedwarn (input_location, OPT_Wc__17_extensions,
5931 "fold-expressions only available with %<-std=c++17%> "
5932 "or %<-std=gnu++17%>");
5934 else
5935 /* Let the front end know that this expression was
5936 enclosed in parentheses. This matters in case, for
5937 example, the expression is of the form `A::B', since
5938 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5939 not. */
5940 expr = finish_parenthesized_expr (expr);
5942 /* DR 705: Wrapping an unqualified name in parentheses
5943 suppresses arg-dependent lookup. We want to pass back
5944 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5945 (c++/37862), but none of the others. */
5946 if (*idk != CP_ID_KIND_QUALIFIED)
5947 *idk = CP_ID_KIND_NONE;
5949 /* The `>' token might be the end of a template-id or
5950 template-parameter-list now. */
5951 parser->greater_than_is_operator_p
5952 = saved_greater_than_is_operator_p;
5954 /* Consume the `)'. */
5955 token = cp_lexer_peek_token (parser->lexer);
5956 location_t close_paren_loc = token->location;
5957 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5958 expr.set_range (open_paren_loc, close_paren_loc);
5959 if (no_wparens)
5960 suppress_warning (expr, OPT_Wparentheses);
5961 if (!parens.require_close (parser)
5962 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5963 cp_parser_skip_to_end_of_statement (parser);
5965 return expr;
5968 case CPP_OPEN_SQUARE:
5970 if (c_dialect_objc ())
5972 /* We might have an Objective-C++ message. */
5973 cp_parser_parse_tentatively (parser);
5974 tree msg = cp_parser_objc_message_expression (parser);
5975 /* If that works out, we're done ... */
5976 if (cp_parser_parse_definitely (parser))
5977 return msg;
5978 /* ... else, fall though to see if it's a lambda. */
5980 cp_expr lam = cp_parser_lambda_expression (parser);
5981 /* Don't warn about a failed tentative parse. */
5982 if (cp_parser_error_occurred (parser))
5983 return error_mark_node;
5984 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5985 return lam;
5988 case CPP_OBJC_STRING:
5989 if (c_dialect_objc ())
5990 /* We have an Objective-C++ string literal. */
5991 return cp_parser_objc_expression (parser);
5992 cp_parser_error (parser, "expected primary-expression");
5993 return error_mark_node;
5995 case CPP_KEYWORD:
5996 switch (token->keyword)
5998 /* These two are the boolean literals. */
5999 case RID_TRUE:
6000 cp_lexer_consume_token (parser->lexer);
6001 return cp_expr (boolean_true_node, token->location);
6002 case RID_FALSE:
6003 cp_lexer_consume_token (parser->lexer);
6004 return cp_expr (boolean_false_node, token->location);
6006 /* The `__null' literal. */
6007 case RID_NULL:
6008 cp_lexer_consume_token (parser->lexer);
6009 return cp_expr (null_node, token->location);
6011 /* The `nullptr' literal. */
6012 case RID_NULLPTR:
6013 cp_lexer_consume_token (parser->lexer);
6014 return cp_expr (nullptr_node, token->location);
6016 /* Recognize the `this' keyword. */
6017 case RID_THIS:
6018 cp_lexer_consume_token (parser->lexer);
6019 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
6021 error_at (token->location,
6022 "%<this%> may not be used in this context");
6023 return error_mark_node;
6025 /* Pointers cannot appear in constant-expressions. */
6026 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
6027 return error_mark_node;
6028 return cp_expr (finish_this_expr (), token->location);
6030 /* The `operator' keyword can be the beginning of an
6031 id-expression. */
6032 case RID_OPERATOR:
6033 goto id_expression;
6035 case RID_FUNCTION_NAME:
6036 case RID_PRETTY_FUNCTION_NAME:
6037 case RID_C99_FUNCTION_NAME:
6039 non_integral_constant name;
6041 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
6042 __func__ are the names of variables -- but they are
6043 treated specially. Therefore, they are handled here,
6044 rather than relying on the generic id-expression logic
6045 below. Grammatically, these names are id-expressions.
6047 Consume the token. */
6048 token = cp_lexer_consume_token (parser->lexer);
6050 switch (token->keyword)
6052 case RID_FUNCTION_NAME:
6053 name = NIC_FUNC_NAME;
6054 break;
6055 case RID_PRETTY_FUNCTION_NAME:
6056 name = NIC_PRETTY_FUNC;
6057 break;
6058 case RID_C99_FUNCTION_NAME:
6059 name = NIC_C99_FUNC;
6060 break;
6061 default:
6062 gcc_unreachable ();
6065 if (cp_parser_non_integral_constant_expression (parser, name))
6066 return error_mark_node;
6068 /* Look up the name. */
6069 return finish_fname (token->u.value);
6072 case RID_VA_ARG:
6074 tree expression;
6075 tree type;
6076 location_t type_location;
6077 location_t start_loc
6078 = cp_lexer_peek_token (parser->lexer)->location;
6079 /* The `__builtin_va_arg' construct is used to handle
6080 `va_arg'. Consume the `__builtin_va_arg' token. */
6081 cp_lexer_consume_token (parser->lexer);
6082 /* Look for the opening `('. */
6083 matching_parens parens;
6084 parens.require_open (parser);
6085 /* Now, parse the assignment-expression. */
6086 expression = cp_parser_assignment_expression (parser);
6087 /* Look for the `,'. */
6088 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
6089 type_location = cp_lexer_peek_token (parser->lexer)->location;
6090 /* Parse the type-id. */
6092 type_id_in_expr_sentinel s (parser);
6093 type = cp_parser_type_id (parser);
6095 /* Look for the closing `)'. */
6096 location_t finish_loc
6097 = cp_lexer_peek_token (parser->lexer)->location;
6098 parens.require_close (parser);
6099 /* Using `va_arg' in a constant-expression is not
6100 allowed. */
6101 if (cp_parser_non_integral_constant_expression (parser,
6102 NIC_VA_ARG))
6103 return error_mark_node;
6104 /* Construct a location of the form:
6105 __builtin_va_arg (v, int)
6106 ~~~~~~~~~~~~~~~~~~~~~^~~~
6107 with the caret at the type, ranging from the start of the
6108 "__builtin_va_arg" token to the close paren. */
6109 location_t combined_loc
6110 = make_location (type_location, start_loc, finish_loc);
6111 return build_x_va_arg (combined_loc, expression, type);
6114 case RID_OFFSETOF:
6115 return cp_parser_builtin_offsetof (parser);
6117 // C++ concepts
6118 case RID_REQUIRES:
6119 return cp_parser_requires_expression (parser);
6121 /* Objective-C++ expressions. */
6122 case RID_AT_ENCODE:
6123 case RID_AT_PROTOCOL:
6124 case RID_AT_SELECTOR:
6125 return cp_parser_objc_expression (parser);
6127 case RID_OMP_ALL_MEMORY:
6128 gcc_assert (flag_openmp);
6129 cp_lexer_consume_token (parser->lexer);
6130 error_at (token->location,
6131 "%<omp_all_memory%> may only be used in OpenMP "
6132 "%<depend%> clause");
6133 return error_mark_node;
6135 case RID_TEMPLATE:
6136 if (parser->in_function_body
6137 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6138 == CPP_LESS))
6140 error_at (token->location,
6141 "a template declaration cannot appear at block scope");
6142 cp_parser_skip_to_end_of_block_or_statement (parser);
6143 return error_mark_node;
6145 /* FALLTHRU */
6146 default:
6147 cp_parser_error (parser, "expected primary-expression");
6148 return error_mark_node;
6151 /* An id-expression can start with either an identifier, a
6152 `::' as the beginning of a qualified-id, or the "operator"
6153 keyword. */
6154 case CPP_NAME:
6155 if (const cp_trait* trait = cp_lexer_peek_trait_expr (parser->lexer))
6156 return cp_parser_trait (parser, trait);
6157 /* FALLTHRU */
6158 case CPP_SCOPE:
6159 case CPP_TEMPLATE_ID:
6160 case CPP_NESTED_NAME_SPECIFIER:
6162 id_expression:
6163 cp_expr id_expression;
6164 cp_expr decl;
6165 const char *error_msg;
6166 bool template_p;
6167 bool done;
6168 cp_token *id_expr_token;
6170 /* Parse the id-expression. */
6171 id_expression
6172 = cp_parser_id_expression (parser,
6173 /*template_keyword_p=*/false,
6174 /*check_dependency_p=*/true,
6175 &template_p,
6176 /*declarator_p=*/false,
6177 /*optional_p=*/false);
6178 if (id_expression == error_mark_node)
6179 return error_mark_node;
6180 id_expr_token = token;
6181 token = cp_lexer_peek_token (parser->lexer);
6182 done = (token->type != CPP_OPEN_SQUARE
6183 && token->type != CPP_OPEN_PAREN
6184 && token->type != CPP_DOT
6185 && token->type != CPP_DEREF
6186 && token->type != CPP_PLUS_PLUS
6187 && token->type != CPP_MINUS_MINUS);
6188 /* If we have a template-id, then no further lookup is
6189 required. If the template-id was for a template-class, we
6190 will sometimes have a TYPE_DECL at this point. */
6191 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6192 || TREE_CODE (id_expression) == TYPE_DECL)
6193 decl = id_expression;
6194 /* Look up the name. */
6195 else
6197 tree ambiguous_decls;
6199 /* If we already know that this lookup is ambiguous, then
6200 we've already issued an error message; there's no reason
6201 to check again. */
6202 if (id_expr_token->type == CPP_NAME
6203 && id_expr_token->error_reported)
6205 cp_parser_simulate_error (parser);
6206 return error_mark_node;
6209 decl = cp_parser_lookup_name (parser, id_expression,
6210 none_type,
6211 template_p,
6212 /*is_namespace=*/false,
6213 /*check_dependency=*/true,
6214 &ambiguous_decls,
6215 id_expression.get_location ());
6216 /* If the lookup was ambiguous, an error will already have
6217 been issued. */
6218 if (ambiguous_decls)
6219 return error_mark_node;
6221 /* In Objective-C++, we may have an Objective-C 2.0
6222 dot-syntax for classes here. */
6223 if (c_dialect_objc ()
6224 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6225 && TREE_CODE (decl) == TYPE_DECL
6226 && objc_is_class_name (decl))
6228 tree component;
6229 cp_lexer_consume_token (parser->lexer);
6230 component = cp_parser_identifier (parser);
6231 if (component == error_mark_node)
6232 return error_mark_node;
6234 tree result = objc_build_class_component_ref (id_expression,
6235 component);
6236 /* Build a location of the form:
6237 expr.component
6238 ~~~~~^~~~~~~~~
6239 with caret at the start of the component name (at
6240 input_location), ranging from the start of the id_expression
6241 to the end of the component name. */
6242 location_t combined_loc
6243 = make_location (input_location, id_expression.get_start (),
6244 get_finish (input_location));
6245 protected_set_expr_location (result, combined_loc);
6246 return result;
6249 /* In Objective-C++, an instance variable (ivar) may be preferred
6250 to whatever cp_parser_lookup_name() found.
6251 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6252 rest of c-family, we have to do a little extra work to preserve
6253 any location information in cp_expr "decl". Given that
6254 objc_lookup_ivar is implemented in "c-family" and "objc", we
6255 have a trip through the pure "tree" type, rather than cp_expr.
6256 Naively copying it back to "decl" would implicitly give the
6257 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6258 store an EXPR_LOCATION. Hence we only update "decl" (and
6259 hence its location_t) if we get back a different tree node. */
6260 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6261 id_expression);
6262 if (decl_tree != decl.get_value ())
6263 decl = cp_expr (decl_tree);
6265 /* If name lookup gives us a SCOPE_REF, then the
6266 qualifying scope was dependent. */
6267 if (TREE_CODE (decl) == SCOPE_REF)
6269 /* At this point, we do not know if DECL is a valid
6270 integral constant expression. We assume that it is
6271 in fact such an expression, so that code like:
6273 template <int N> struct A {
6274 int a[B<N>::i];
6277 is accepted. At template-instantiation time, we
6278 will check that B<N>::i is actually a constant. */
6279 return decl;
6281 /* Check to see if DECL is a local variable in a context
6282 where that is forbidden. */
6283 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6284 && local_variable_p (decl)
6285 /* DR 2082 permits local variables in unevaluated contexts
6286 within a default argument. */
6287 && !cp_unevaluated_operand)
6289 const char *msg
6290 = (TREE_CODE (decl) == PARM_DECL
6291 ? G_("parameter %qD may not appear in this context")
6292 : G_("local variable %qD may not appear in this context"));
6293 error_at (id_expression.get_location (), msg,
6294 decl.get_value ());
6295 return error_mark_node;
6299 decl = (finish_id_expression
6300 (id_expression, decl, parser->scope,
6301 idk,
6302 parser->integral_constant_expression_p,
6303 parser->allow_non_integral_constant_expression_p,
6304 &parser->non_integral_constant_expression_p,
6305 template_p, done, address_p,
6306 template_arg_p,
6307 &error_msg,
6308 id_expression.get_location ()));
6309 if (error_msg)
6310 cp_parser_error (parser, error_msg);
6311 /* Build a location for an id-expression of the form:
6312 ::ns::id
6313 ~~~~~~^~
6317 i.e. from the start of the first token to the end of the final
6318 token, with the caret at the start of the unqualified-id. */
6319 location_t caret_loc = get_pure_location (id_expression.get_location ());
6320 location_t start_loc = get_start (id_expr_token->location);
6321 location_t finish_loc = get_finish (id_expression.get_location ());
6322 location_t combined_loc
6323 = make_location (caret_loc, start_loc, finish_loc);
6325 decl.set_location (combined_loc);
6326 return decl;
6329 /* Anything else is an error. */
6330 default:
6331 cp_parser_error (parser, "expected primary-expression");
6332 return error_mark_node;
6336 static inline cp_expr
6337 cp_parser_primary_expression (cp_parser *parser,
6338 bool address_p,
6339 bool cast_p,
6340 bool template_arg_p,
6341 cp_id_kind *idk)
6343 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6344 /*decltype*/false, idk);
6347 /* Complain about missing template keyword when naming a dependent
6348 member template. */
6350 static void
6351 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6353 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6354 return;
6356 gcc_rich_location richloc (loc);
6357 richloc.add_fixit_insert_before ("template");
6358 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6359 "expected %qs keyword before dependent "
6360 "template name", "template");
6361 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6364 /* Parse an id-expression.
6366 id-expression:
6367 unqualified-id
6368 qualified-id
6370 qualified-id:
6371 :: [opt] nested-name-specifier template [opt] unqualified-id
6372 :: identifier
6373 :: operator-function-id
6374 :: template-id
6376 Return a representation of the unqualified portion of the
6377 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6378 a `::' or nested-name-specifier.
6380 Often, if the id-expression was a qualified-id, the caller will
6381 want to make a SCOPE_REF to represent the qualified-id. This
6382 function does not do this in order to avoid wastefully creating
6383 SCOPE_REFs when they are not required.
6385 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6386 `template' keyword.
6388 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6389 uninstantiated templates.
6391 If *TEMPLATE_P is non-NULL, it is set to true iff the
6392 `template' keyword is used to explicitly indicate that the entity
6393 named is a template.
6395 If DECLARATOR_P is true, the id-expression is appearing as part of
6396 a declarator, rather than as part of an expression. */
6398 static cp_expr
6399 cp_parser_id_expression (cp_parser *parser,
6400 bool template_keyword_p,
6401 bool check_dependency_p,
6402 bool *template_p,
6403 bool declarator_p,
6404 bool optional_p)
6406 bool global_scope_p;
6407 bool nested_name_specifier_p;
6409 /* Assume the `template' keyword was not used. */
6410 if (template_p)
6411 *template_p = template_keyword_p;
6413 /* Look for the optional `::' operator. */
6414 global_scope_p
6415 = (!template_keyword_p
6416 && (cp_parser_global_scope_opt (parser,
6417 /*current_scope_valid_p=*/false)
6418 != NULL_TREE));
6420 /* Look for the optional nested-name-specifier. */
6421 nested_name_specifier_p
6422 = (cp_parser_nested_name_specifier_opt (parser,
6423 /*typename_keyword_p=*/false,
6424 check_dependency_p,
6425 /*type_p=*/false,
6426 declarator_p,
6427 template_keyword_p)
6428 != NULL_TREE);
6430 cp_expr id = NULL_TREE;
6431 tree scope = parser->scope;
6433 /* Peek at the next token. */
6434 cp_token *token = cp_lexer_peek_token (parser->lexer);
6436 /* If there is a nested-name-specifier, then we are looking at
6437 the first qualified-id production. */
6438 if (nested_name_specifier_p)
6440 tree saved_object_scope;
6441 tree saved_qualifying_scope;
6443 /* See if the next token is the `template' keyword. */
6444 if (!template_p)
6445 template_p = &template_keyword_p;
6446 *template_p = cp_parser_optional_template_keyword (parser);
6447 /* Name lookup we do during the processing of the
6448 unqualified-id might obliterate SCOPE. */
6449 saved_object_scope = parser->object_scope;
6450 saved_qualifying_scope = parser->qualifying_scope;
6451 /* Process the final unqualified-id. */
6452 id = cp_parser_unqualified_id (parser, *template_p,
6453 check_dependency_p,
6454 declarator_p,
6455 /*optional_p=*/false);
6456 /* Restore the SAVED_SCOPE for our caller. */
6457 parser->scope = scope;
6458 parser->object_scope = saved_object_scope;
6459 parser->qualifying_scope = saved_qualifying_scope;
6461 /* Otherwise, if we are in global scope, then we are looking at one
6462 of the other qualified-id productions. */
6463 else if (global_scope_p)
6465 /* If it's an identifier, and the next token is not a "<", then
6466 we can avoid the template-id case. This is an optimization
6467 for this common case. */
6468 if (token->type == CPP_NAME
6469 && !cp_parser_nth_token_starts_template_argument_list_p
6470 (parser, 2))
6471 return cp_parser_identifier (parser);
6473 cp_parser_parse_tentatively (parser);
6474 /* Try a template-id. */
6475 id = cp_parser_template_id_expr (parser,
6476 /*template_keyword_p=*/false,
6477 /*check_dependency_p=*/true,
6478 declarator_p);
6479 /* If that worked, we're done. */
6480 if (cp_parser_parse_definitely (parser))
6481 return id;
6483 /* Peek at the next token. (Changes in the token buffer may
6484 have invalidated the pointer obtained above.) */
6485 token = cp_lexer_peek_token (parser->lexer);
6487 switch (token->type)
6489 case CPP_NAME:
6490 id = cp_parser_identifier (parser);
6491 break;
6493 case CPP_KEYWORD:
6494 if (token->keyword == RID_OPERATOR)
6496 id = cp_parser_operator_function_id (parser);
6497 break;
6499 /* Fall through. */
6501 default:
6502 cp_parser_error (parser, "expected id-expression");
6503 return error_mark_node;
6506 else
6508 if (!scope)
6509 scope = parser->context->object_type;
6510 id = cp_parser_unqualified_id (parser, template_keyword_p,
6511 /*check_dependency_p=*/true,
6512 declarator_p,
6513 optional_p);
6516 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6517 && warn_missing_template_keyword
6518 && !template_keyword_p
6519 /* Don't warn if we're looking inside templates. */
6520 && check_dependency_p
6521 /* In a template argument list a > could be closing
6522 the enclosing targs. */
6523 && !parser->in_template_argument_list_p
6524 && scope && dependentish_scope_p (scope)
6525 /* Don't confuse an ill-formed constructor declarator for a missing
6526 template keyword in a return type. */
6527 && !(declarator_p && constructor_name_p (id, scope))
6528 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6529 && warning_enabled_at (token->location,
6530 OPT_Wmissing_template_keyword))
6532 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6533 if (cp_parser_skip_entire_template_parameter_list (parser)
6534 /* An operator after the > suggests that the > ends a
6535 template-id; a name or literal suggests that the > is an
6536 operator. */
6537 && (cp_lexer_peek_token (parser->lexer)->type
6538 <= CPP_LAST_PUNCTUATOR))
6539 missing_template_diag (token->location);
6542 return id;
6545 /* Parse an unqualified-id.
6547 unqualified-id:
6548 identifier
6549 operator-function-id
6550 conversion-function-id
6551 ~ class-name
6552 template-id
6554 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6555 keyword, in a construct like `A::template ...'.
6557 Returns a representation of unqualified-id. For the `identifier'
6558 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6559 production a BIT_NOT_EXPR is returned; the operand of the
6560 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6561 other productions, see the documentation accompanying the
6562 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6563 names are looked up in uninstantiated templates. If DECLARATOR_P
6564 is true, the unqualified-id is appearing as part of a declarator,
6565 rather than as part of an expression. */
6567 static cp_expr
6568 cp_parser_unqualified_id (cp_parser* parser,
6569 bool template_keyword_p,
6570 bool check_dependency_p,
6571 bool declarator_p,
6572 bool optional_p)
6574 cp_token *token;
6576 /* Peek at the next token. */
6577 token = cp_lexer_peek_token (parser->lexer);
6579 switch ((int) token->type)
6581 case CPP_NAME:
6583 tree id;
6585 /* We don't know yet whether or not this will be a
6586 template-id. */
6587 cp_parser_parse_tentatively (parser);
6588 /* Try a template-id. */
6589 id = cp_parser_template_id_expr (parser, template_keyword_p,
6590 check_dependency_p,
6591 declarator_p);
6592 /* If it worked, we're done. */
6593 if (cp_parser_parse_definitely (parser))
6594 return id;
6595 /* Otherwise, it's an ordinary identifier. */
6596 return cp_parser_identifier (parser);
6599 case CPP_TEMPLATE_ID:
6600 return cp_parser_template_id_expr (parser, template_keyword_p,
6601 check_dependency_p,
6602 declarator_p);
6604 case CPP_COMPL:
6606 tree type_decl;
6607 tree qualifying_scope;
6608 tree object_scope;
6609 tree scope;
6610 bool done;
6611 location_t tilde_loc = token->location;
6613 /* Consume the `~' token. */
6614 cp_lexer_consume_token (parser->lexer);
6615 /* Parse the class-name. The standard, as written, seems to
6616 say that:
6618 template <typename T> struct S { ~S (); };
6619 template <typename T> S<T>::~S() {}
6621 is invalid, since `~' must be followed by a class-name, but
6622 `S<T>' is dependent, and so not known to be a class.
6623 That's not right; we need to look in uninstantiated
6624 templates. A further complication arises from:
6626 template <typename T> void f(T t) {
6627 t.T::~T();
6630 Here, it is not possible to look up `T' in the scope of `T'
6631 itself. We must look in both the current scope, and the
6632 scope of the containing complete expression.
6634 Yet another issue is:
6636 struct S {
6637 int S;
6638 ~S();
6641 S::~S() {}
6643 The standard does not seem to say that the `S' in `~S'
6644 should refer to the type `S' and not the data member
6645 `S::S'. */
6647 /* DR 244 says that we look up the name after the "~" in the
6648 same scope as we looked up the qualifying name. That idea
6649 isn't fully worked out; it's more complicated than that. */
6650 scope = parser->scope;
6651 object_scope = parser->object_scope;
6652 qualifying_scope = parser->qualifying_scope;
6654 /* Check for invalid scopes. */
6655 if (scope == error_mark_node)
6657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6658 cp_lexer_consume_token (parser->lexer);
6659 return error_mark_node;
6661 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6663 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6664 error_at (token->location,
6665 "scope %qT before %<~%> is not a class-name",
6666 scope);
6667 cp_parser_simulate_error (parser);
6668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6669 cp_lexer_consume_token (parser->lexer);
6670 return error_mark_node;
6672 if (template_keyword_p)
6674 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6675 error_at (tilde_loc, "%<template%> keyword not permitted in "
6676 "destructor name");
6677 cp_parser_simulate_error (parser);
6678 return error_mark_node;
6681 gcc_assert (!scope || TYPE_P (scope));
6683 token = cp_lexer_peek_token (parser->lexer);
6685 /* Create a location with caret == start at the tilde,
6686 finishing at the end of the peeked token, e.g:
6687 ~token
6688 ^~~~~~. */
6689 location_t loc
6690 = make_location (tilde_loc, tilde_loc, token->location);
6692 /* If the name is of the form "X::~X" it's OK even if X is a
6693 typedef. */
6695 if (scope
6696 && token->type == CPP_NAME
6697 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6698 != CPP_LESS)
6699 && (token->u.value == TYPE_IDENTIFIER (scope)
6700 || (CLASS_TYPE_P (scope)
6701 && constructor_name_p (token->u.value, scope))))
6703 cp_lexer_consume_token (parser->lexer);
6704 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6707 /* ~auto means the destructor of whatever the object is. */
6708 if (cp_parser_is_keyword (token, RID_AUTO))
6710 if (cxx_dialect < cxx14)
6711 pedwarn (loc, OPT_Wc__14_extensions,
6712 "%<~auto%> only available with "
6713 "%<-std=c++14%> or %<-std=gnu++14%>");
6714 cp_lexer_consume_token (parser->lexer);
6715 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6718 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6719 declarator-id of a constructor or destructor. */
6720 if (token->type == CPP_TEMPLATE_ID && declarator_p
6721 && cxx_dialect >= cxx20)
6723 if (!cp_parser_simulate_error (parser))
6724 error_at (tilde_loc, "template-id not allowed for destructor");
6725 return error_mark_node;
6728 /* If there was an explicit qualification (S::~T), first look
6729 in the scope given by the qualification (i.e., S).
6731 Note: in the calls to cp_parser_class_name below we pass
6732 typename_type so that lookup finds the injected-class-name
6733 rather than the constructor. */
6734 done = false;
6735 type_decl = NULL_TREE;
6736 if (scope)
6738 cp_parser_parse_tentatively (parser);
6739 type_decl = cp_parser_class_name (parser,
6740 /*typename_keyword_p=*/false,
6741 /*template_keyword_p=*/false,
6742 typename_type,
6743 /*check_dependency=*/false,
6744 /*class_head_p=*/false,
6745 declarator_p);
6746 if (cp_parser_parse_definitely (parser))
6747 done = true;
6749 /* In "N::S::~S", look in "N" as well. */
6750 if (!done && scope && qualifying_scope)
6752 cp_parser_parse_tentatively (parser);
6753 parser->scope = qualifying_scope;
6754 parser->object_scope = NULL_TREE;
6755 parser->qualifying_scope = NULL_TREE;
6756 type_decl
6757 = cp_parser_class_name (parser,
6758 /*typename_keyword_p=*/false,
6759 /*template_keyword_p=*/false,
6760 typename_type,
6761 /*check_dependency=*/false,
6762 /*class_head_p=*/false,
6763 declarator_p);
6764 if (cp_parser_parse_definitely (parser))
6765 done = true;
6767 /* In "p->S::~T", look in the scope given by "*p" as well. */
6768 else if (!done && object_scope)
6770 cp_parser_parse_tentatively (parser);
6771 parser->scope = object_scope;
6772 parser->object_scope = NULL_TREE;
6773 parser->qualifying_scope = NULL_TREE;
6774 type_decl
6775 = cp_parser_class_name (parser,
6776 /*typename_keyword_p=*/false,
6777 /*template_keyword_p=*/false,
6778 typename_type,
6779 /*check_dependency=*/false,
6780 /*class_head_p=*/false,
6781 declarator_p);
6782 if (cp_parser_parse_definitely (parser))
6783 done = true;
6785 /* Look in the surrounding context. */
6786 if (!done)
6788 parser->scope = NULL_TREE;
6789 parser->object_scope = NULL_TREE;
6790 parser->qualifying_scope = NULL_TREE;
6791 if (processing_template_decl)
6792 cp_parser_parse_tentatively (parser);
6793 type_decl
6794 = cp_parser_class_name (parser,
6795 /*typename_keyword_p=*/false,
6796 /*template_keyword_p=*/false,
6797 typename_type,
6798 /*check_dependency=*/false,
6799 /*class_head_p=*/false,
6800 declarator_p);
6801 if (processing_template_decl
6802 && ! cp_parser_parse_definitely (parser))
6804 /* We couldn't find a type with this name. If we're parsing
6805 tentatively, fail and try something else. */
6806 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6808 cp_parser_simulate_error (parser);
6809 return error_mark_node;
6811 /* Otherwise, accept it and check for a match at instantiation
6812 time. */
6813 type_decl = cp_parser_identifier (parser);
6814 if (type_decl != error_mark_node)
6815 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6816 return type_decl;
6819 /* If an error occurred, assume that the name of the
6820 destructor is the same as the name of the qualifying
6821 class. That allows us to keep parsing after running
6822 into ill-formed destructor names. */
6823 if (type_decl == error_mark_node && scope)
6824 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6825 else if (type_decl == error_mark_node)
6826 return error_mark_node;
6828 /* Check that destructor name and scope match. */
6829 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6831 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6832 error_at (loc,
6833 "declaration of %<~%T%> as member of %qT",
6834 type_decl, scope);
6835 cp_parser_simulate_error (parser);
6836 return error_mark_node;
6839 /* [class.dtor]
6841 A typedef-name that names a class shall not be used as the
6842 identifier in the declarator for a destructor declaration. */
6843 if (declarator_p
6844 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6845 && !DECL_SELF_REFERENCE_P (type_decl)
6846 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6847 error_at (loc,
6848 "typedef-name %qD used as destructor declarator",
6849 type_decl);
6851 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6854 case CPP_KEYWORD:
6855 if (token->keyword == RID_OPERATOR)
6857 cp_expr id;
6859 /* This could be a template-id, so we try that first. */
6860 cp_parser_parse_tentatively (parser);
6861 /* Try a template-id. */
6862 id = cp_parser_template_id_expr (parser, template_keyword_p,
6863 /*check_dependency_p=*/true,
6864 declarator_p);
6865 /* If that worked, we're done. */
6866 if (cp_parser_parse_definitely (parser))
6867 return id;
6868 /* We still don't know whether we're looking at an
6869 operator-function-id or a conversion-function-id. */
6870 cp_parser_parse_tentatively (parser);
6871 /* Try an operator-function-id. */
6872 id = cp_parser_operator_function_id (parser);
6873 /* If that didn't work, try a conversion-function-id. */
6874 if (!cp_parser_parse_definitely (parser))
6875 id = cp_parser_conversion_function_id (parser);
6877 return id;
6879 /* Fall through. */
6881 default:
6882 if (optional_p)
6883 return NULL_TREE;
6884 cp_parser_error (parser, "expected unqualified-id");
6885 return error_mark_node;
6889 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6890 be a template-id or the name shall refer to a class template or an
6891 alias template. */
6893 static void
6894 check_template_keyword_in_nested_name_spec (tree name)
6896 if (CLASS_TYPE_P (name)
6897 && ((CLASSTYPE_USE_TEMPLATE (name)
6898 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6899 || CLASSTYPE_IS_TEMPLATE (name)))
6900 return;
6902 if (TREE_CODE (name) == TYPENAME_TYPE
6903 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6904 return;
6905 /* Alias templates are also OK. */
6906 else if (alias_template_specialization_p (name, nt_opaque))
6907 return;
6909 permerror (input_location, TYPE_P (name)
6910 ? G_("%qT is not a template")
6911 : G_("%qD is not a template"),
6912 name);
6915 /* Parse an (optional) nested-name-specifier.
6917 nested-name-specifier: [C++98]
6918 class-or-namespace-name :: nested-name-specifier [opt]
6919 class-or-namespace-name :: template nested-name-specifier [opt]
6921 nested-name-specifier: [C++0x]
6922 type-name ::
6923 namespace-name ::
6924 nested-name-specifier identifier ::
6925 nested-name-specifier template [opt] simple-template-id ::
6927 PARSER->SCOPE should be set appropriately before this function is
6928 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6929 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6930 in name lookups.
6932 Sets PARSER->SCOPE to the class (TYPE) or namespace
6933 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6934 it unchanged if there is no nested-name-specifier. Returns the new
6935 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6937 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6939 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6940 part of a declaration and/or decl-specifier. */
6942 static tree
6943 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6944 bool typename_keyword_p,
6945 bool check_dependency_p,
6946 bool type_p,
6947 bool is_declaration,
6948 bool template_keyword_p /* = false */)
6950 bool success = false;
6951 cp_token_position start = 0;
6952 cp_token *token;
6954 /* Remember where the nested-name-specifier starts. */
6955 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6956 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6958 start = cp_lexer_token_position (parser->lexer, false);
6959 push_deferring_access_checks (dk_deferred);
6962 while (true)
6964 tree new_scope;
6965 tree old_scope;
6966 tree saved_qualifying_scope;
6968 /* Spot cases that cannot be the beginning of a
6969 nested-name-specifier. */
6970 token = cp_lexer_peek_token (parser->lexer);
6972 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6973 the already parsed nested-name-specifier. */
6974 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6976 /* Grab the nested-name-specifier and continue the loop. */
6977 cp_parser_pre_parsed_nested_name_specifier (parser);
6978 /* If we originally encountered this nested-name-specifier
6979 with CHECK_DEPENDENCY_P set to true, we will not have
6980 resolved TYPENAME_TYPEs, so we must do so here. */
6981 if (is_declaration
6982 && !check_dependency_p
6983 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6985 new_scope = resolve_typename_type (parser->scope,
6986 /*only_current_p=*/false);
6987 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6988 parser->scope = new_scope;
6990 success = true;
6991 continue;
6994 /* Spot cases that cannot be the beginning of a
6995 nested-name-specifier. On the second and subsequent times
6996 through the loop, we look for the `template' keyword. */
6997 if (success && token->keyword == RID_TEMPLATE)
6999 /* A template-id can start a nested-name-specifier. */
7000 else if (token->type == CPP_TEMPLATE_ID)
7002 /* DR 743: decltype can be used in a nested-name-specifier. */
7003 else if (token_is_decltype (token))
7005 else
7007 /* If the next token is not an identifier, then it is
7008 definitely not a type-name or namespace-name. */
7009 if (token->type != CPP_NAME)
7010 break;
7011 /* If the following token is neither a `<' (to begin a
7012 template-id), nor a `::', then we are not looking at a
7013 nested-name-specifier. */
7014 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7016 if (token->type == CPP_COLON
7017 && parser->colon_corrects_to_scope_p
7018 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
7019 /* name:name is a valid sequence in an Objective C message. */
7020 && !parser->objective_c_message_context_p)
7022 gcc_rich_location richloc (token->location);
7023 richloc.add_fixit_replace ("::");
7024 error_at (&richloc,
7025 "found %<:%> in nested-name-specifier, "
7026 "expected %<::%>");
7027 token->type = CPP_SCOPE;
7030 if (token->type != CPP_SCOPE
7031 && !cp_parser_nth_token_starts_template_argument_list_p
7032 (parser, 2))
7033 break;
7036 /* The nested-name-specifier is optional, so we parse
7037 tentatively. */
7038 cp_parser_parse_tentatively (parser);
7040 /* Look for the optional `template' keyword, if this isn't the
7041 first time through the loop. */
7042 if (success)
7044 template_keyword_p = cp_parser_optional_template_keyword (parser);
7045 /* DR1710: "In a qualified-id used as the name in
7046 a typename-specifier, elaborated-type-specifier, using-declaration,
7047 or class-or-decltype, an optional keyword template appearing at
7048 the top level is ignored." */
7049 if (!template_keyword_p
7050 && typename_keyword_p
7051 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
7052 template_keyword_p = true;
7055 /* Save the old scope since the name lookup we are about to do
7056 might destroy it. */
7057 old_scope = parser->scope;
7058 saved_qualifying_scope = parser->qualifying_scope;
7059 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
7060 look up names in "X<T>::I" in order to determine that "Y" is
7061 a template. So, if we have a typename at this point, we make
7062 an effort to look through it. */
7063 if (is_declaration
7064 && !check_dependency_p
7065 && !typename_keyword_p
7066 && parser->scope
7067 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7068 parser->scope = resolve_typename_type (parser->scope,
7069 /*only_current_p=*/false);
7070 /* Parse the qualifying entity. */
7071 new_scope
7072 = cp_parser_qualifying_entity (parser,
7073 typename_keyword_p,
7074 template_keyword_p,
7075 check_dependency_p,
7076 type_p,
7077 is_declaration);
7078 /* Look for the `::' token. */
7079 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7081 /* If we found what we wanted, we keep going; otherwise, we're
7082 done. */
7083 if (!cp_parser_parse_definitely (parser))
7085 bool error_p = false;
7087 /* Restore the OLD_SCOPE since it was valid before the
7088 failed attempt at finding the last
7089 class-or-namespace-name. */
7090 parser->scope = old_scope;
7091 parser->qualifying_scope = saved_qualifying_scope;
7093 /* If the next token is a decltype, and the one after that is a
7094 `::', then the decltype has failed to resolve to a class or
7095 enumeration type. Give this error even when parsing
7096 tentatively since it can't possibly be valid--and we're going
7097 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7098 won't get another chance.*/
7099 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7100 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7101 == CPP_SCOPE))
7103 token = cp_lexer_consume_token (parser->lexer);
7104 tree dtype = token->u.tree_check_value->value;
7105 if (dtype != error_mark_node)
7106 error_at (token->location, "%<decltype%> evaluates to %qT, "
7107 "which is not a class or enumeration type",
7108 dtype);
7109 parser->scope = error_mark_node;
7110 error_p = true;
7111 /* As below. */
7112 success = true;
7113 cp_lexer_consume_token (parser->lexer);
7116 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7117 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7119 /* If we have a non-type template-id followed by ::, it can't
7120 possibly be valid. */
7121 token = cp_lexer_peek_token (parser->lexer);
7122 tree tid = token->u.tree_check_value->value;
7123 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7124 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7126 tree tmpl = NULL_TREE;
7127 if (is_overloaded_fn (tid))
7129 tree fns = get_fns (tid);
7130 if (OVL_SINGLE_P (fns))
7131 tmpl = OVL_FIRST (fns);
7132 if (function_concept_p (fns))
7133 error_at (token->location, "concept-id %qD "
7134 "in nested-name-specifier", tid);
7135 else
7136 error_at (token->location, "function template-id "
7137 "%qD in nested-name-specifier", tid);
7139 else
7141 tmpl = TREE_OPERAND (tid, 0);
7142 if (variable_concept_p (tmpl)
7143 || standard_concept_p (tmpl))
7144 error_at (token->location, "concept-id %qD "
7145 "in nested-name-specifier", tid);
7146 else
7148 /* Variable template. */
7149 gcc_assert (variable_template_p (tmpl));
7150 error_at (token->location, "variable template-id "
7151 "%qD in nested-name-specifier", tid);
7154 if (tmpl)
7155 inform (DECL_SOURCE_LOCATION (tmpl),
7156 "%qD declared here", tmpl);
7158 parser->scope = error_mark_node;
7159 error_p = true;
7160 /* As below. */
7161 success = true;
7162 cp_lexer_consume_token (parser->lexer);
7163 cp_lexer_consume_token (parser->lexer);
7167 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7168 break;
7169 /* If the next token is an identifier, and the one after
7170 that is a `::', then any valid interpretation would have
7171 found a class-or-namespace-name. */
7172 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7173 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7174 == CPP_SCOPE)
7175 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7176 != CPP_COMPL))
7178 token = cp_lexer_consume_token (parser->lexer);
7179 if (!error_p)
7181 if (!token->error_reported)
7183 tree decl;
7184 tree ambiguous_decls;
7186 decl = cp_parser_lookup_name (parser, token->u.value,
7187 none_type,
7188 /*is_template=*/false,
7189 /*is_namespace=*/false,
7190 /*check_dependency=*/true,
7191 &ambiguous_decls,
7192 token->location);
7193 if (TREE_CODE (decl) == TEMPLATE_DECL)
7194 error_at (token->location,
7195 "%qD used without template arguments",
7196 decl);
7197 else if (ambiguous_decls)
7199 // cp_parser_lookup_name has the same diagnostic,
7200 // thus make sure to emit it at most once.
7201 if (cp_parser_uncommitted_to_tentative_parse_p
7202 (parser))
7204 error_at (token->location,
7205 "reference to %qD is ambiguous",
7206 token->u.value);
7207 print_candidates (ambiguous_decls);
7209 decl = error_mark_node;
7211 else
7213 if (cxx_dialect != cxx98)
7214 cp_parser_name_lookup_error
7215 (parser, token->u.value, decl, NLE_NOT_CXX98,
7216 token->location);
7217 else
7218 cp_parser_name_lookup_error
7219 (parser, token->u.value, decl, NLE_CXX98,
7220 token->location);
7223 parser->scope = error_mark_node;
7224 error_p = true;
7225 /* Treat this as a successful nested-name-specifier
7226 due to:
7228 [basic.lookup.qual]
7230 If the name found is not a class-name (clause
7231 _class_) or namespace-name (_namespace.def_), the
7232 program is ill-formed. */
7233 success = true;
7235 cp_lexer_consume_token (parser->lexer);
7237 break;
7239 /* We've found one valid nested-name-specifier. */
7240 success = true;
7241 /* Name lookup always gives us a DECL. */
7242 if (TREE_CODE (new_scope) == TYPE_DECL)
7243 new_scope = TREE_TYPE (new_scope);
7244 /* Uses of "template" must be followed by actual templates. */
7245 if (template_keyword_p)
7246 check_template_keyword_in_nested_name_spec (new_scope);
7247 /* If it is a class scope, try to complete it; we are about to
7248 be looking up names inside the class. */
7249 if (TYPE_P (new_scope)
7250 /* Since checking types for dependency can be expensive,
7251 avoid doing it if the type is already complete. */
7252 && !COMPLETE_TYPE_P (new_scope)
7253 /* Do not try to complete dependent types. */
7254 && !dependent_type_p (new_scope))
7256 new_scope = complete_type (new_scope);
7257 /* If it is a typedef to current class, use the current
7258 class instead, as the typedef won't have any names inside
7259 it yet. */
7260 if (!COMPLETE_TYPE_P (new_scope)
7261 && currently_open_class (new_scope))
7262 new_scope = TYPE_MAIN_VARIANT (new_scope);
7264 /* Make sure we look in the right scope the next time through
7265 the loop. */
7266 parser->scope = new_scope;
7269 /* If parsing tentatively, replace the sequence of tokens that makes
7270 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7271 token. That way, should we re-parse the token stream, we will
7272 not have to repeat the effort required to do the parse, nor will
7273 we issue duplicate error messages. */
7274 if (success && start)
7276 cp_token *token;
7278 token = cp_lexer_token_at (parser->lexer, start);
7279 /* Reset the contents of the START token. */
7280 token->type = CPP_NESTED_NAME_SPECIFIER;
7281 /* Retrieve any deferred checks. Do not pop this access checks yet
7282 so the memory will not be reclaimed during token replacing below. */
7283 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7284 token->tree_check_p = true;
7285 token->u.tree_check_value->value = parser->scope;
7286 token->u.tree_check_value->checks = get_deferred_access_checks ();
7287 token->u.tree_check_value->qualifying_scope =
7288 parser->qualifying_scope;
7289 token->keyword = RID_MAX;
7291 /* Purge all subsequent tokens. */
7292 cp_lexer_purge_tokens_after (parser->lexer, start);
7295 if (start)
7296 pop_to_parent_deferring_access_checks ();
7298 return success ? parser->scope : NULL_TREE;
7301 /* Parse a nested-name-specifier. See
7302 cp_parser_nested_name_specifier_opt for details. This function
7303 behaves identically, except that it will an issue an error if no
7304 nested-name-specifier is present. */
7306 static tree
7307 cp_parser_nested_name_specifier (cp_parser *parser,
7308 bool typename_keyword_p,
7309 bool check_dependency_p,
7310 bool type_p,
7311 bool is_declaration)
7313 tree scope;
7315 /* Look for the nested-name-specifier. */
7316 scope = cp_parser_nested_name_specifier_opt (parser,
7317 typename_keyword_p,
7318 check_dependency_p,
7319 type_p,
7320 is_declaration);
7321 /* If it was not present, issue an error message. */
7322 if (!scope)
7324 cp_parser_error (parser, "expected nested-name-specifier");
7325 parser->scope = NULL_TREE;
7328 return scope;
7331 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7332 this is either a class-name or a namespace-name (which corresponds
7333 to the class-or-namespace-name production in the grammar). For
7334 C++0x, it can also be a type-name that refers to an enumeration
7335 type or a simple-template-id.
7337 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7338 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7339 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7340 TYPE_P is TRUE iff the next name should be taken as a class-name,
7341 even the same name is declared to be another entity in the same
7342 scope.
7344 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7345 specified by the class-or-namespace-name. If neither is found the
7346 ERROR_MARK_NODE is returned. */
7348 static tree
7349 cp_parser_qualifying_entity (cp_parser *parser,
7350 bool typename_keyword_p,
7351 bool template_keyword_p,
7352 bool check_dependency_p,
7353 bool type_p,
7354 bool is_declaration)
7356 tree saved_scope;
7357 tree saved_qualifying_scope;
7358 tree saved_object_scope;
7359 tree scope;
7360 bool only_class_p;
7361 bool successful_parse_p;
7363 /* DR 743: decltype can appear in a nested-name-specifier. */
7364 if (cp_lexer_next_token_is_decltype (parser->lexer))
7366 scope = cp_parser_decltype (parser);
7367 if (TREE_CODE (scope) != ENUMERAL_TYPE
7368 && !MAYBE_CLASS_TYPE_P (scope))
7370 cp_parser_simulate_error (parser);
7371 return error_mark_node;
7373 if (TYPE_NAME (scope))
7374 scope = TYPE_NAME (scope);
7375 return scope;
7378 /* Before we try to parse the class-name, we must save away the
7379 current PARSER->SCOPE since cp_parser_class_name will destroy
7380 it. */
7381 saved_scope = parser->scope;
7382 saved_qualifying_scope = parser->qualifying_scope;
7383 saved_object_scope = parser->object_scope;
7384 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7385 there is no need to look for a namespace-name. */
7386 only_class_p = template_keyword_p
7387 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7388 if (!only_class_p)
7389 cp_parser_parse_tentatively (parser);
7390 scope = cp_parser_class_name (parser,
7391 typename_keyword_p,
7392 template_keyword_p,
7393 type_p ? class_type : none_type,
7394 check_dependency_p,
7395 /*class_head_p=*/false,
7396 is_declaration,
7397 /*enum_ok=*/cxx_dialect > cxx98);
7398 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7399 /* If that didn't work, try for a namespace-name. */
7400 if (!only_class_p && !successful_parse_p)
7402 /* Restore the saved scope. */
7403 parser->scope = saved_scope;
7404 parser->qualifying_scope = saved_qualifying_scope;
7405 parser->object_scope = saved_object_scope;
7406 /* If we are not looking at an identifier followed by the scope
7407 resolution operator, then this is not part of a
7408 nested-name-specifier. (Note that this function is only used
7409 to parse the components of a nested-name-specifier.) */
7410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7411 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7412 return error_mark_node;
7413 scope = cp_parser_namespace_name (parser);
7416 return scope;
7419 /* Return true if we are looking at a compound-literal, false otherwise. */
7421 static bool
7422 cp_parser_compound_literal_p (cp_parser *parser)
7424 cp_lexer_save_tokens (parser->lexer);
7426 /* Skip tokens until the next token is a closing parenthesis.
7427 If we find the closing `)', and the next token is a `{', then
7428 we are looking at a compound-literal. */
7429 bool compound_literal_p
7430 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7431 /*consume_paren=*/true)
7432 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7434 /* Roll back the tokens we skipped. */
7435 cp_lexer_rollback_tokens (parser->lexer);
7437 return compound_literal_p;
7440 /* Return true if EXPR is the integer constant zero or a complex constant
7441 of zero, without any folding, but ignoring location wrappers. */
7443 bool
7444 literal_integer_zerop (const_tree expr)
7446 return (location_wrapper_p (expr)
7447 && integer_zerop (TREE_OPERAND (expr, 0)));
7450 /* Parse a postfix-expression.
7452 postfix-expression:
7453 primary-expression
7454 postfix-expression [ expression ]
7455 postfix-expression ( expression-list [opt] )
7456 simple-type-specifier ( expression-list [opt] )
7457 typename :: [opt] nested-name-specifier identifier
7458 ( expression-list [opt] )
7459 typename :: [opt] nested-name-specifier template [opt] template-id
7460 ( expression-list [opt] )
7461 postfix-expression . template [opt] id-expression
7462 postfix-expression -> template [opt] id-expression
7463 postfix-expression . pseudo-destructor-name
7464 postfix-expression -> pseudo-destructor-name
7465 postfix-expression ++
7466 postfix-expression --
7467 dynamic_cast < type-id > ( expression )
7468 static_cast < type-id > ( expression )
7469 reinterpret_cast < type-id > ( expression )
7470 const_cast < type-id > ( expression )
7471 typeid ( expression )
7472 typeid ( type-id )
7474 GNU Extension:
7476 postfix-expression:
7477 ( type-id ) { initializer-list , [opt] }
7479 This extension is a GNU version of the C99 compound-literal
7480 construct. (The C99 grammar uses `type-name' instead of `type-id',
7481 but they are essentially the same concept.)
7483 If ADDRESS_P is true, the postfix expression is the operand of the
7484 `&' operator. CAST_P is true if this expression is the target of a
7485 cast.
7487 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7488 class member access expressions [expr.ref].
7490 Returns a representation of the expression. */
7492 static cp_expr
7493 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7494 bool member_access_only_p, bool decltype_p,
7495 cp_id_kind * pidk_return)
7497 cp_token *token;
7498 location_t loc;
7499 enum rid keyword;
7500 cp_id_kind idk = CP_ID_KIND_NONE;
7501 cp_expr postfix_expression = NULL_TREE;
7502 bool is_member_access = false;
7504 /* Peek at the next token. */
7505 token = cp_lexer_peek_token (parser->lexer);
7506 loc = token->location;
7507 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7509 /* Some of the productions are determined by keywords. */
7510 keyword = token->keyword;
7511 switch (keyword)
7513 case RID_DYNCAST:
7514 case RID_STATCAST:
7515 case RID_REINTCAST:
7516 case RID_CONSTCAST:
7518 tree type;
7519 cp_expr expression;
7520 const char *saved_message;
7521 bool saved_in_type_id_in_expr_p;
7523 /* All of these can be handled in the same way from the point
7524 of view of parsing. Begin by consuming the token
7525 identifying the cast. */
7526 cp_lexer_consume_token (parser->lexer);
7528 /* New types cannot be defined in the cast. */
7529 saved_message = parser->type_definition_forbidden_message;
7530 parser->type_definition_forbidden_message
7531 = G_("types may not be defined in casts");
7533 /* Look for the opening `<'. */
7534 cp_parser_require (parser, CPP_LESS, RT_LESS);
7535 /* Parse the type to which we are casting. */
7536 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7537 parser->in_type_id_in_expr_p = true;
7538 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7539 NULL);
7540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7541 /* Look for the closing `>'. */
7542 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7543 /* Restore the old message. */
7544 parser->type_definition_forbidden_message = saved_message;
7546 bool saved_greater_than_is_operator_p
7547 = parser->greater_than_is_operator_p;
7548 parser->greater_than_is_operator_p = true;
7550 /* And the expression which is being cast. */
7551 matching_parens parens;
7552 parens.require_open (parser);
7553 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7554 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7555 RT_CLOSE_PAREN);
7556 location_t end_loc = close_paren ?
7557 close_paren->location : UNKNOWN_LOCATION;
7559 parser->greater_than_is_operator_p
7560 = saved_greater_than_is_operator_p;
7562 /* Only type conversions to integral or enumeration types
7563 can be used in constant-expressions. */
7564 if (!cast_valid_in_integral_constant_expression_p (type)
7565 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7567 postfix_expression = error_mark_node;
7568 break;
7571 /* Construct a location e.g. :
7572 reinterpret_cast <int *> (expr)
7573 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7574 ranging from the start of the "*_cast" token to the final closing
7575 paren, with the caret at the start. */
7576 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7578 switch (keyword)
7580 case RID_DYNCAST:
7581 postfix_expression
7582 = build_dynamic_cast (cp_cast_loc, type, expression,
7583 tf_warning_or_error);
7584 break;
7585 case RID_STATCAST:
7586 postfix_expression
7587 = build_static_cast (cp_cast_loc, type, expression,
7588 tf_warning_or_error);
7589 break;
7590 case RID_REINTCAST:
7591 postfix_expression
7592 = build_reinterpret_cast (cp_cast_loc, type, expression,
7593 tf_warning_or_error);
7594 break;
7595 case RID_CONSTCAST:
7596 postfix_expression
7597 = build_const_cast (cp_cast_loc, type, expression,
7598 tf_warning_or_error);
7599 break;
7600 default:
7601 gcc_unreachable ();
7604 break;
7606 case RID_TYPEID:
7608 tree type;
7609 const char *saved_message;
7610 bool saved_in_type_id_in_expr_p;
7612 /* Consume the `typeid' token. */
7613 cp_lexer_consume_token (parser->lexer);
7614 /* Look for the `(' token. */
7615 matching_parens parens;
7616 parens.require_open (parser);
7617 /* Types cannot be defined in a `typeid' expression. */
7618 saved_message = parser->type_definition_forbidden_message;
7619 parser->type_definition_forbidden_message
7620 = G_("types may not be defined in a %<typeid%> expression");
7621 /* We can't be sure yet whether we're looking at a type-id or an
7622 expression. */
7623 cp_parser_parse_tentatively (parser);
7624 /* Try a type-id first. */
7625 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7626 parser->in_type_id_in_expr_p = true;
7627 type = cp_parser_type_id (parser);
7628 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7629 /* Look for the `)' token. Otherwise, we can't be sure that
7630 we're not looking at an expression: consider `typeid (int
7631 (3))', for example. */
7632 cp_token *close_paren = parens.require_close (parser);
7633 /* If all went well, simply lookup the type-id. */
7634 if (cp_parser_parse_definitely (parser))
7635 postfix_expression = get_typeid (type, tf_warning_or_error);
7636 /* Otherwise, fall back to the expression variant. */
7637 else
7639 tree expression;
7641 /* Look for an expression. */
7642 expression = cp_parser_expression (parser, & idk);
7643 /* Compute its typeid. */
7644 postfix_expression = build_typeid (expression, tf_warning_or_error);
7645 /* Look for the `)' token. */
7646 close_paren = parens.require_close (parser);
7648 /* Restore the saved message. */
7649 parser->type_definition_forbidden_message = saved_message;
7650 /* `typeid' may not appear in an integral constant expression. */
7651 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7652 postfix_expression = error_mark_node;
7654 /* Construct a location e.g. :
7655 typeid (expr)
7656 ^~~~~~~~~~~~~
7657 ranging from the start of the "typeid" token to the final closing
7658 paren, with the caret at the start. */
7659 if (close_paren)
7661 location_t typeid_loc
7662 = make_location (start_loc, start_loc, close_paren->location);
7663 postfix_expression.set_location (typeid_loc);
7664 postfix_expression.maybe_add_location_wrapper ();
7667 break;
7669 case RID_TYPENAME:
7671 tree type;
7672 /* The syntax permitted here is the same permitted for an
7673 elaborated-type-specifier. */
7674 ++parser->prevent_constrained_type_specifiers;
7675 type = cp_parser_elaborated_type_specifier (parser,
7676 /*is_friend=*/false,
7677 /*is_declaration=*/false);
7678 --parser->prevent_constrained_type_specifiers;
7679 postfix_expression = cp_parser_functional_cast (parser, type);
7681 break;
7683 case RID_ADDRESSOF:
7684 case RID_BUILTIN_SHUFFLE:
7685 case RID_BUILTIN_SHUFFLEVECTOR:
7686 case RID_BUILTIN_LAUNDER:
7687 case RID_BUILTIN_ASSOC_BARRIER:
7689 vec<tree, va_gc> *vec;
7691 cp_lexer_consume_token (parser->lexer);
7692 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7693 /*cast_p=*/false, /*allow_expansion_p=*/true,
7694 /*non_constant_p=*/NULL);
7695 if (vec == NULL)
7697 postfix_expression = error_mark_node;
7698 break;
7701 for (tree p : *vec)
7702 mark_exp_read (p);
7704 switch (keyword)
7706 case RID_ADDRESSOF:
7707 if (vec->length () == 1)
7708 postfix_expression
7709 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7710 else
7712 error_at (loc, "wrong number of arguments to "
7713 "%<__builtin_addressof%>");
7714 postfix_expression = error_mark_node;
7716 break;
7718 case RID_BUILTIN_LAUNDER:
7719 if (vec->length () == 1)
7720 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7721 tf_warning_or_error);
7722 else
7724 error_at (loc, "wrong number of arguments to "
7725 "%<__builtin_launder%>");
7726 postfix_expression = error_mark_node;
7728 break;
7730 case RID_BUILTIN_ASSOC_BARRIER:
7731 if (vec->length () == 1)
7732 postfix_expression = build1_loc (loc, PAREN_EXPR,
7733 TREE_TYPE ((*vec)[0]),
7734 (*vec)[0]);
7735 else
7737 error_at (loc, "wrong number of arguments to "
7738 "%<__builtin_assoc_barrier%>");
7739 postfix_expression = error_mark_node;
7741 break;
7743 case RID_BUILTIN_SHUFFLE:
7744 if (vec->length () == 2)
7745 postfix_expression
7746 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7747 (*vec)[1], tf_warning_or_error);
7748 else if (vec->length () == 3)
7749 postfix_expression
7750 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7751 (*vec)[2], tf_warning_or_error);
7752 else
7754 error_at (loc, "wrong number of arguments to "
7755 "%<__builtin_shuffle%>");
7756 postfix_expression = error_mark_node;
7758 break;
7760 case RID_BUILTIN_SHUFFLEVECTOR:
7761 if (vec->length () < 3)
7763 error_at (loc, "wrong number of arguments to "
7764 "%<__builtin_shufflevector%>");
7765 postfix_expression = error_mark_node;
7767 else
7769 postfix_expression
7770 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7772 break;
7774 default:
7775 gcc_unreachable ();
7777 break;
7780 case RID_BUILTIN_CONVERTVECTOR:
7782 tree expression;
7783 tree type;
7784 /* Consume the `__builtin_convertvector' token. */
7785 cp_lexer_consume_token (parser->lexer);
7786 /* Look for the opening `('. */
7787 matching_parens parens;
7788 parens.require_open (parser);
7789 /* Now, parse the assignment-expression. */
7790 expression = cp_parser_assignment_expression (parser);
7791 /* Look for the `,'. */
7792 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7793 location_t type_location
7794 = cp_lexer_peek_token (parser->lexer)->location;
7795 /* Parse the type-id. */
7797 type_id_in_expr_sentinel s (parser);
7798 type = cp_parser_type_id (parser);
7800 /* Look for the closing `)'. */
7801 parens.require_close (parser);
7802 postfix_expression
7803 = cp_build_vec_convert (expression, type_location, type,
7804 tf_warning_or_error);
7805 break;
7808 case RID_BUILTIN_BIT_CAST:
7810 tree expression;
7811 tree type;
7812 /* Consume the `__builtin_bit_cast' token. */
7813 cp_lexer_consume_token (parser->lexer);
7814 /* Look for the opening `('. */
7815 matching_parens parens;
7816 parens.require_open (parser);
7817 location_t type_location
7818 = cp_lexer_peek_token (parser->lexer)->location;
7819 /* Parse the type-id. */
7821 type_id_in_expr_sentinel s (parser);
7822 type = cp_parser_type_id (parser);
7824 /* Look for the `,'. */
7825 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7826 /* Now, parse the assignment-expression. */
7827 expression = cp_parser_assignment_expression (parser);
7828 /* Look for the closing `)'. */
7829 parens.require_close (parser);
7830 postfix_expression
7831 = cp_build_bit_cast (type_location, type, expression,
7832 tf_warning_or_error);
7833 break;
7836 default:
7838 tree type;
7840 /* If the next thing is a simple-type-specifier, we may be
7841 looking at a functional cast. We could also be looking at
7842 an id-expression. So, we try the functional cast, and if
7843 that doesn't work we fall back to the primary-expression. */
7844 cp_parser_parse_tentatively (parser);
7845 /* Look for the simple-type-specifier. */
7846 ++parser->prevent_constrained_type_specifiers;
7847 type = cp_parser_simple_type_specifier (parser,
7848 /*decl_specs=*/NULL,
7849 CP_PARSER_FLAGS_NONE);
7850 --parser->prevent_constrained_type_specifiers;
7851 /* Parse the cast itself. */
7852 if (!cp_parser_error_occurred (parser))
7853 postfix_expression
7854 = cp_parser_functional_cast (parser, type);
7855 /* If that worked, we're done. */
7856 if (cp_parser_parse_definitely (parser))
7857 break;
7859 /* If the functional-cast didn't work out, try a
7860 compound-literal. */
7861 if (cp_parser_allow_gnu_extensions_p (parser)
7862 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7864 cp_expr initializer = NULL_TREE;
7866 cp_parser_parse_tentatively (parser);
7868 matching_parens parens;
7869 parens.consume_open (parser);
7871 /* Avoid calling cp_parser_type_id pointlessly, see comment
7872 in cp_parser_cast_expression about c++/29234. */
7873 if (!cp_parser_compound_literal_p (parser))
7874 cp_parser_simulate_error (parser);
7875 else
7877 /* Parse the type. */
7878 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7879 parser->in_type_id_in_expr_p = true;
7880 type = cp_parser_type_id (parser);
7881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7882 parens.require_close (parser);
7885 /* If things aren't going well, there's no need to
7886 keep going. */
7887 if (!cp_parser_error_occurred (parser))
7888 /* Parse the brace-enclosed initializer list. */
7889 initializer = cp_parser_braced_list (parser);
7890 /* If that worked, we're definitely looking at a
7891 compound-literal expression. */
7892 if (cp_parser_parse_definitely (parser))
7894 /* Warn the user that a compound literal is not
7895 allowed in standard C++. */
7896 pedwarn (input_location, OPT_Wpedantic,
7897 "ISO C++ forbids compound-literals");
7898 /* For simplicity, we disallow compound literals in
7899 constant-expressions. We could
7900 allow compound literals of integer type, whose
7901 initializer was a constant, in constant
7902 expressions. Permitting that usage, as a further
7903 extension, would not change the meaning of any
7904 currently accepted programs. (Of course, as
7905 compound literals are not part of ISO C++, the
7906 standard has nothing to say.) */
7907 if (cp_parser_non_integral_constant_expression (parser,
7908 NIC_NCC))
7910 postfix_expression = error_mark_node;
7911 break;
7913 /* Form the representation of the compound-literal. */
7914 postfix_expression
7915 = finish_compound_literal (type, initializer,
7916 tf_warning_or_error, fcl_c99);
7917 postfix_expression.set_location (initializer.get_location ());
7918 break;
7922 /* It must be a primary-expression. */
7923 postfix_expression
7924 = cp_parser_primary_expression (parser, address_p, cast_p,
7925 /*template_arg_p=*/false,
7926 decltype_p,
7927 &idk);
7929 break;
7932 /* Note that we don't need to worry about calling build_cplus_new on a
7933 class-valued CALL_EXPR in decltype when it isn't the end of the
7934 postfix-expression; unary_complex_lvalue will take care of that for
7935 all these cases. */
7937 /* Keep looping until the postfix-expression is complete. */
7938 while (true)
7940 if (idk == CP_ID_KIND_UNQUALIFIED
7941 && identifier_p (postfix_expression)
7942 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7943 /* It is not a Koenig lookup function call. */
7944 postfix_expression
7945 = unqualified_name_lookup_error (postfix_expression);
7947 /* Peek at the next token. */
7948 token = cp_lexer_peek_token (parser->lexer);
7950 switch (token->type)
7952 case CPP_OPEN_SQUARE:
7953 if (cp_next_tokens_can_be_std_attribute_p (parser))
7955 cp_parser_error (parser,
7956 "two consecutive %<[%> shall "
7957 "only introduce an attribute");
7958 return error_mark_node;
7960 postfix_expression
7961 = cp_parser_postfix_open_square_expression (parser,
7962 postfix_expression,
7963 false,
7964 decltype_p);
7965 postfix_expression.set_range (start_loc,
7966 postfix_expression.get_location ());
7968 idk = CP_ID_KIND_NONE;
7969 is_member_access = false;
7970 break;
7972 case CPP_OPEN_PAREN:
7973 /* postfix-expression ( expression-list [opt] ) */
7975 bool koenig_p;
7976 bool is_builtin_constant_p;
7977 bool saved_integral_constant_expression_p = false;
7978 bool saved_non_integral_constant_expression_p = false;
7979 tsubst_flags_t complain = complain_flags (decltype_p);
7980 vec<tree, va_gc> *args;
7981 location_t close_paren_loc = UNKNOWN_LOCATION;
7982 location_t combined_loc = UNKNOWN_LOCATION;
7984 is_member_access = false;
7986 tree stripped_expression
7987 = tree_strip_any_location_wrapper (postfix_expression);
7988 is_builtin_constant_p
7989 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7990 if (is_builtin_constant_p)
7992 /* The whole point of __builtin_constant_p is to allow
7993 non-constant expressions to appear as arguments. */
7994 saved_integral_constant_expression_p
7995 = parser->integral_constant_expression_p;
7996 saved_non_integral_constant_expression_p
7997 = parser->non_integral_constant_expression_p;
7998 parser->integral_constant_expression_p = false;
8000 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
8001 && fndecl_built_in_p (stripped_expression,
8002 BUILT_IN_CLASSIFY_TYPE))
8004 /* __builtin_classify_type (type) */
8005 auto cl1 = make_temp_override
8006 (parser->type_definition_forbidden_message,
8007 G_("types may not be defined in "
8008 "%<__builtin_classify_type%> calls"));
8009 auto cl2 = make_temp_override
8010 (parser->type_definition_forbidden_message_arg,
8011 NULL);
8012 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
8013 true);
8014 cp_unevaluated uev;
8015 cp_parser_parse_tentatively (parser);
8016 matching_parens parens;
8017 parens.consume_open (parser);
8018 tree type = cp_parser_type_id (parser);
8019 parens.require_close (parser);
8020 if (cp_parser_parse_definitely (parser))
8022 if (dependent_type_p (type))
8024 postfix_expression = build_vl_exp (CALL_EXPR, 4);
8025 CALL_EXPR_FN (postfix_expression)
8026 = stripped_expression;
8027 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
8028 CALL_EXPR_ARG (postfix_expression, 0)
8029 = build_min (SIZEOF_EXPR, size_type_node, type);
8030 TREE_TYPE (postfix_expression) = integer_type_node;
8032 else
8034 postfix_expression
8035 = build_int_cst (integer_type_node,
8036 type_to_class (type));
8038 break;
8041 args = (cp_parser_parenthesized_expression_list
8042 (parser, non_attr,
8043 /*cast_p=*/false, /*allow_expansion_p=*/true,
8044 /*non_constant_p=*/NULL,
8045 /*close_paren_loc=*/&close_paren_loc,
8046 /*wrap_locations_p=*/true));
8047 if (is_builtin_constant_p)
8049 parser->integral_constant_expression_p
8050 = saved_integral_constant_expression_p;
8051 parser->non_integral_constant_expression_p
8052 = saved_non_integral_constant_expression_p;
8055 if (args == NULL)
8057 postfix_expression = error_mark_node;
8058 break;
8061 /* Function calls are not permitted in
8062 constant-expressions. */
8063 if (! builtin_valid_in_constant_expr_p (postfix_expression)
8064 && cp_parser_non_integral_constant_expression (parser,
8065 NIC_FUNC_CALL))
8067 postfix_expression = error_mark_node;
8068 release_tree_vector (args);
8069 break;
8072 koenig_p = false;
8073 if (idk == CP_ID_KIND_UNQUALIFIED
8074 || idk == CP_ID_KIND_TEMPLATE_ID)
8076 if (identifier_p (postfix_expression)
8077 /* In C++20, we may need to perform ADL for a template
8078 name. */
8079 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
8080 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
8082 if (!args->is_empty ())
8084 koenig_p = true;
8085 if (!any_type_dependent_arguments_p (args))
8086 postfix_expression
8087 = perform_koenig_lookup (postfix_expression, args,
8088 complain);
8090 else
8091 postfix_expression
8092 = unqualified_fn_lookup_error (postfix_expression);
8094 /* We do not perform argument-dependent lookup if
8095 normal lookup finds a non-function, in accordance
8096 with the expected resolution of DR 218. */
8097 else if (!args->is_empty ()
8098 && is_overloaded_fn (postfix_expression))
8100 /* Do not do argument dependent lookup if regular
8101 lookup finds a member function or a block-scope
8102 function declaration. [basic.lookup.argdep]/3 */
8103 bool do_adl_p = true;
8104 tree fns = get_fns (postfix_expression);
8105 for (lkp_iterator iter (fns); iter; ++iter)
8107 tree fn = STRIP_TEMPLATE (*iter);
8108 if ((TREE_CODE (fn) == USING_DECL
8109 && DECL_DEPENDENT_P (fn))
8110 || DECL_FUNCTION_MEMBER_P (fn)
8111 || DECL_LOCAL_DECL_P (fn))
8113 do_adl_p = false;
8114 break;
8118 if (do_adl_p)
8120 koenig_p = true;
8121 if (!any_type_dependent_arguments_p (args))
8122 postfix_expression
8123 = perform_koenig_lookup (postfix_expression, args,
8124 complain);
8129 /* Temporarily set input_location to the combined location
8130 with call expression range, as e.g. build_out_target_exprs
8131 called from convert_default_arg relies on input_location,
8132 so updating it only when the call is fully built results
8133 in inconsistencies between location handling in templates
8134 and outside of templates. */
8135 if (close_paren_loc != UNKNOWN_LOCATION)
8136 combined_loc = make_location (token->location, start_loc,
8137 close_paren_loc);
8138 iloc_sentinel ils (combined_loc);
8140 if (TREE_CODE (postfix_expression) == OFFSET_REF
8141 || TREE_CODE (postfix_expression) == MEMBER_REF
8142 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8143 postfix_expression = (build_offset_ref_call_from_tree
8144 (postfix_expression, &args,
8145 complain));
8146 else
8147 /* All other function calls. */
8149 if (DECL_P (postfix_expression)
8150 && parser->omp_for_parse_state
8151 && parser->omp_for_parse_state->in_intervening_code
8152 && omp_runtime_api_call (postfix_expression))
8154 error_at (loc, "calls to the OpenMP runtime API are "
8155 "not permitted in intervening code");
8156 parser->omp_for_parse_state->fail = true;
8158 bool disallow_virtual = (idk == CP_ID_KIND_QUALIFIED);
8159 postfix_expression
8160 = finish_call_expr (postfix_expression, &args,
8161 disallow_virtual,
8162 koenig_p,
8163 complain);
8166 if (close_paren_loc != UNKNOWN_LOCATION)
8167 postfix_expression.set_location (combined_loc);
8169 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8170 idk = CP_ID_KIND_NONE;
8172 release_tree_vector (args);
8174 break;
8176 case CPP_DOT:
8177 case CPP_DEREF:
8178 /* postfix-expression . template [opt] id-expression
8179 postfix-expression . pseudo-destructor-name
8180 postfix-expression -> template [opt] id-expression
8181 postfix-expression -> pseudo-destructor-name */
8183 /* Consume the `.' or `->' operator. */
8184 cp_lexer_consume_token (parser->lexer);
8186 postfix_expression
8187 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8188 postfix_expression,
8189 false, &idk, loc);
8191 is_member_access = true;
8192 break;
8194 case CPP_PLUS_PLUS:
8195 /* postfix-expression ++ */
8196 /* Consume the `++' token. */
8197 cp_lexer_consume_token (parser->lexer);
8198 /* Generate a representation for the complete expression. */
8199 postfix_expression
8200 = finish_increment_expr (postfix_expression,
8201 POSTINCREMENT_EXPR);
8202 /* Increments may not appear in constant-expressions. */
8203 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8204 postfix_expression = error_mark_node;
8205 idk = CP_ID_KIND_NONE;
8206 is_member_access = false;
8207 break;
8209 case CPP_MINUS_MINUS:
8210 /* postfix-expression -- */
8211 /* Consume the `--' token. */
8212 cp_lexer_consume_token (parser->lexer);
8213 /* Generate a representation for the complete expression. */
8214 postfix_expression
8215 = finish_increment_expr (postfix_expression,
8216 POSTDECREMENT_EXPR);
8217 /* Decrements may not appear in constant-expressions. */
8218 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8219 postfix_expression = error_mark_node;
8220 idk = CP_ID_KIND_NONE;
8221 is_member_access = false;
8222 break;
8224 default:
8225 if (pidk_return != NULL)
8226 * pidk_return = idk;
8227 if (member_access_only_p)
8228 return is_member_access
8229 ? postfix_expression
8230 : cp_expr (error_mark_node);
8231 else
8232 return postfix_expression;
8237 /* Helper function for cp_parser_parenthesized_expression_list and
8238 cp_parser_postfix_open_square_expression. Parse a single element
8239 of parenthesized expression list. */
8241 static cp_expr
8242 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8243 bool allow_expansion_p,
8244 bool *non_constant_p)
8246 cp_expr expr (NULL_TREE);
8247 bool expr_non_constant_p;
8249 /* Parse the next assignment-expression. */
8250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8252 /* A braced-init-list. */
8253 cp_lexer_set_source_position (parser->lexer);
8254 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8255 expr = cp_parser_braced_list (parser,
8256 (non_constant_p != nullptr
8257 ? &expr_non_constant_p
8258 : nullptr));
8259 if (non_constant_p && expr_non_constant_p)
8260 *non_constant_p = true;
8262 else if (non_constant_p)
8264 expr = cp_parser_constant_expression (parser,
8265 /*allow_non_constant_p=*/true,
8266 &expr_non_constant_p);
8267 if (expr_non_constant_p)
8268 *non_constant_p = true;
8270 else
8271 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8273 /* If we have an ellipsis, then this is an expression expansion. */
8274 if (allow_expansion_p
8275 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8277 /* Consume the `...'. */
8278 cp_lexer_consume_token (parser->lexer);
8280 /* Build the argument pack. */
8281 expr = make_pack_expansion (expr);
8283 return expr;
8286 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8287 by cp_parser_builtin_offsetof. We're looking for
8289 postfix-expression [ expression ]
8290 postfix-expression [ braced-init-list ] (C++11)
8291 postfix-expression [ expression-list[opt] ] (C++23)
8293 FOR_OFFSETOF is set if we're being called in that context, which
8294 changes how we deal with integer constant expressions. */
8296 static tree
8297 cp_parser_postfix_open_square_expression (cp_parser *parser,
8298 tree postfix_expression,
8299 bool for_offsetof,
8300 bool decltype_p)
8302 tree index = NULL_TREE;
8303 releasing_vec expression_list = NULL;
8304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8305 bool saved_greater_than_is_operator_p;
8306 bool saved_colon_corrects_to_scope_p;
8308 /* Consume the `[' token. */
8309 cp_lexer_consume_token (parser->lexer);
8311 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8312 parser->greater_than_is_operator_p = true;
8314 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8315 if (parser->omp_array_section_p)
8316 parser->colon_corrects_to_scope_p = false;
8318 /* Parse the index expression. */
8319 /* ??? For offsetof, there is a question of what to allow here. If
8320 offsetof is not being used in an integral constant expression context,
8321 then we *could* get the right answer by computing the value at runtime.
8322 If we are in an integral constant expression context, then we might
8323 could accept any constant expression; hard to say without analysis.
8324 Rather than open the barn door too wide right away, allow only integer
8325 constant expressions here. */
8326 if (for_offsetof)
8327 index = cp_parser_constant_expression (parser);
8328 else if (!parser->omp_array_section_p
8329 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8331 if (cxx_dialect >= cxx23
8332 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8333 *&expression_list = make_tree_vector ();
8334 else if (cxx_dialect >= cxx23)
8336 while (true)
8338 cp_expr expr
8339 = cp_parser_parenthesized_expression_list_elt (parser,
8340 /*cast_p=*/
8341 false,
8342 /*allow_exp_p=*/
8343 true,
8344 /*non_cst_p=*/
8345 NULL);
8347 if (expr == error_mark_node)
8348 index = error_mark_node;
8349 else if (expression_list.get () == NULL
8350 && !PACK_EXPANSION_P (expr.get_value ()))
8351 index = expr.get_value ();
8352 else
8353 vec_safe_push (expression_list, expr.get_value ());
8355 /* If the next token isn't a `,', then we are done. */
8356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8357 break;
8359 if (expression_list.get () == NULL && index != error_mark_node)
8361 *&expression_list = make_tree_vector_single (index);
8362 index = NULL_TREE;
8365 /* Otherwise, consume the `,' and keep going. */
8366 cp_lexer_consume_token (parser->lexer);
8368 if (expression_list.get () && index == error_mark_node)
8369 expression_list.release ();
8371 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8373 cp_lexer_set_source_position (parser->lexer);
8374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8375 index = cp_parser_braced_list (parser);
8377 else
8378 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8379 /*decltype_p=*/false,
8380 /*warn_comma_p=*/warn_comma_subscript);
8383 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8385 if (cxx_dialect >= cxx23
8386 && parser->omp_array_section_p
8387 && expression_list.get () != NULL
8388 && vec_safe_length (expression_list) > 1)
8390 error_at (loc, "cannot use multidimensional subscript in OpenMP array "
8391 "section");
8392 index = error_mark_node;
8394 if (parser->omp_array_section_p
8395 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8397 cp_lexer_consume_token (parser->lexer);
8398 tree length = NULL_TREE;
8399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8401 if (cxx_dialect >= cxx23)
8403 cp_expr expr
8404 = cp_parser_parenthesized_expression_list_elt (parser,
8405 /*cast_p=*/
8406 false,
8407 /*allow_exp_p=*/
8408 true,
8409 /*non_cst_p=*/
8410 NULL);
8412 if (expr == error_mark_node)
8413 length = error_mark_node;
8414 else
8415 length = expr.get_value ();
8417 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8419 error_at (loc, "cannot use multidimensional subscript in "
8420 "OpenMP array section");
8421 length = error_mark_node;
8424 else
8425 length
8426 = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8427 /*decltype_p=*/false,
8428 /*warn_comma_p=*/warn_comma_subscript);
8431 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8433 if (index == error_mark_node || length == error_mark_node)
8435 cp_parser_skip_to_closing_square_bracket (parser);
8436 return error_mark_node;
8438 else
8439 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8441 return grok_omp_array_section (input_location, postfix_expression, index,
8442 length);
8445 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8447 /* Look for the closing `]'. */
8448 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8450 /* Build the ARRAY_REF. */
8451 postfix_expression = grok_array_decl (loc, postfix_expression,
8452 index, &expression_list,
8453 tf_warning_or_error
8454 | (decltype_p ? tf_decltype : 0));
8456 /* When not doing offsetof, array references are not permitted in
8457 constant-expressions. */
8458 if (!for_offsetof
8459 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8460 postfix_expression = error_mark_node;
8462 return postfix_expression;
8465 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8466 dereference of incomplete type, returns true if error_mark_node should
8467 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8468 and *DEPENDENT_P. */
8470 bool
8471 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8472 bool *dependent_p)
8474 /* In a template, be permissive by treating an object expression
8475 of incomplete type as dependent (after a pedwarn). */
8476 diagnostic_t kind = (processing_template_decl
8477 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8479 switch (TREE_CODE (*postfix_expression))
8481 case CAST_EXPR:
8482 case REINTERPRET_CAST_EXPR:
8483 case CONST_CAST_EXPR:
8484 case STATIC_CAST_EXPR:
8485 case DYNAMIC_CAST_EXPR:
8486 case IMPLICIT_CONV_EXPR:
8487 case VIEW_CONVERT_EXPR:
8488 case NON_LVALUE_EXPR:
8489 kind = DK_ERROR;
8490 break;
8491 case OVERLOAD:
8492 /* Don't emit any diagnostic for OVERLOADs. */
8493 kind = DK_IGNORED;
8494 break;
8495 default:
8496 /* Avoid clobbering e.g. DECLs. */
8497 if (!EXPR_P (*postfix_expression))
8498 kind = DK_ERROR;
8499 break;
8502 if (kind == DK_IGNORED)
8503 return false;
8505 location_t exploc = location_of (*postfix_expression);
8506 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8507 if (!MAYBE_CLASS_TYPE_P (*scope))
8508 return true;
8509 if (kind == DK_ERROR)
8510 *scope = *postfix_expression = error_mark_node;
8511 else if (processing_template_decl)
8513 *dependent_p = true;
8514 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8516 return false;
8519 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8520 by cp_parser_builtin_offsetof. We're looking for
8522 postfix-expression . template [opt] id-expression
8523 postfix-expression . pseudo-destructor-name
8524 postfix-expression -> template [opt] id-expression
8525 postfix-expression -> pseudo-destructor-name
8527 FOR_OFFSETOF is set if we're being called in that context. That sorta
8528 limits what of the above we'll actually accept, but nevermind.
8529 TOKEN_TYPE is the "." or "->" token, which will already have been
8530 removed from the stream. */
8532 static tree
8533 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8534 enum cpp_ttype token_type,
8535 cp_expr postfix_expression,
8536 bool for_offsetof, cp_id_kind *idk,
8537 location_t location)
8539 tree name;
8540 bool dependent_p;
8541 bool pseudo_destructor_p;
8542 tree scope = NULL_TREE;
8543 location_t start_loc = postfix_expression.get_start ();
8545 /* If this is a `->' operator, dereference the pointer. */
8546 if (token_type == CPP_DEREF)
8547 postfix_expression = build_x_arrow (location, postfix_expression,
8548 tf_warning_or_error);
8549 /* Check to see whether or not the expression is type-dependent and
8550 not the current instantiation. */
8551 dependent_p = type_dependent_object_expression_p (postfix_expression);
8552 /* The identifier following the `->' or `.' is not qualified. */
8553 parser->scope = NULL_TREE;
8554 parser->qualifying_scope = NULL_TREE;
8555 parser->object_scope = NULL_TREE;
8556 *idk = CP_ID_KIND_NONE;
8558 /* Enter the scope corresponding to the type of the object
8559 given by the POSTFIX_EXPRESSION. */
8560 if (!dependent_p)
8562 scope = TREE_TYPE (postfix_expression);
8563 /* According to the standard, no expression should ever have
8564 reference type. Unfortunately, we do not currently match
8565 the standard in this respect in that our internal representation
8566 of an expression may have reference type even when the standard
8567 says it does not. Therefore, we have to manually obtain the
8568 underlying type here. */
8569 scope = non_reference (scope);
8570 /* The type of the POSTFIX_EXPRESSION must be complete. */
8571 /* Unlike the object expression in other contexts, *this is not
8572 required to be of complete type for purposes of class member
8573 access (5.2.5) outside the member function body. */
8574 if (postfix_expression != current_class_ref
8575 && scope != error_mark_node
8576 && !currently_open_class (scope))
8578 scope = complete_type (scope);
8579 if (!COMPLETE_TYPE_P (scope)
8580 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8581 &dependent_p))
8582 return error_mark_node;
8585 if (!dependent_p)
8587 /* Let the name lookup machinery know that we are processing a
8588 class member access expression. */
8589 parser->context->object_type = scope;
8590 /* If something went wrong, we want to be able to discern that case,
8591 as opposed to the case where there was no SCOPE due to the type
8592 of expression being dependent. */
8593 if (!scope)
8594 scope = error_mark_node;
8595 /* If the SCOPE was erroneous, make the various semantic analysis
8596 functions exit quickly -- and without issuing additional error
8597 messages. */
8598 if (scope == error_mark_node)
8599 postfix_expression = error_mark_node;
8603 if (dependent_p)
8605 tree type = TREE_TYPE (postfix_expression);
8606 /* If we don't have a (type-dependent) object of class type, use
8607 typeof to figure out the type of the object. */
8608 if (type == NULL_TREE || is_auto (type))
8609 type = finish_typeof (postfix_expression);
8610 parser->context->object_type = type;
8613 /* Assume this expression is not a pseudo-destructor access. */
8614 pseudo_destructor_p = false;
8616 /* If the SCOPE is a scalar type, then, if this is a valid program,
8617 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8618 is type dependent, it can be pseudo-destructor-name or something else.
8619 Try to parse it as pseudo-destructor-name first. */
8620 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8622 tree s;
8623 tree type;
8625 cp_parser_parse_tentatively (parser);
8626 /* Parse the pseudo-destructor-name. */
8627 s = NULL_TREE;
8628 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8629 &s, &type);
8630 if (dependent_p
8631 && (cp_parser_error_occurred (parser)
8632 || !SCALAR_TYPE_P (type)))
8633 cp_parser_abort_tentative_parse (parser);
8634 else if (cp_parser_parse_definitely (parser))
8636 pseudo_destructor_p = true;
8637 postfix_expression
8638 = finish_pseudo_destructor_expr (postfix_expression,
8639 s, type, location);
8643 if (!pseudo_destructor_p)
8645 /* If the SCOPE is not a scalar type, we are looking at an
8646 ordinary class member access expression, rather than a
8647 pseudo-destructor-name. */
8648 bool template_p;
8649 cp_token *token = cp_lexer_peek_token (parser->lexer);
8650 /* Parse the id-expression. */
8651 name = (cp_parser_id_expression
8652 (parser,
8653 cp_parser_optional_template_keyword (parser),
8654 /*check_dependency_p=*/true,
8655 &template_p,
8656 /*declarator_p=*/false,
8657 /*optional_p=*/false));
8658 /* In general, build a SCOPE_REF if the member name is qualified.
8659 However, if the name was not dependent and has already been
8660 resolved; there is no need to build the SCOPE_REF. For example;
8662 struct X { void f(); };
8663 template <typename T> void f(T* t) { t->X::f(); }
8665 Even though "t" is dependent, "X::f" is not and has been resolved
8666 to a BASELINK; there is no need to include scope information. */
8668 /* But we do need to remember that there was an explicit scope for
8669 virtual function calls. */
8670 if (parser->scope)
8671 *idk = CP_ID_KIND_QUALIFIED;
8673 /* If the name is a template-id that names a type, we will get a
8674 TYPE_DECL here. That is invalid code. */
8675 if (TREE_CODE (name) == TYPE_DECL)
8677 error_at (token->location, "invalid use of %qD", name);
8678 postfix_expression = error_mark_node;
8680 else
8682 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8684 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8686 error_at (token->location, "%<%D::%D%> is not a class member",
8687 parser->scope, name);
8688 postfix_expression = error_mark_node;
8690 else
8691 name = build_qualified_name (/*type=*/NULL_TREE,
8692 parser->scope,
8693 name,
8694 template_p);
8695 parser->scope = NULL_TREE;
8696 parser->qualifying_scope = NULL_TREE;
8697 parser->object_scope = NULL_TREE;
8699 if (parser->scope && name && BASELINK_P (name))
8700 adjust_result_of_qualified_name_lookup
8701 (name, parser->scope, scope);
8702 postfix_expression
8703 = finish_class_member_access_expr (postfix_expression, name,
8704 template_p,
8705 tf_warning_or_error);
8706 /* Build a location e.g.:
8707 ptr->access_expr
8708 ~~~^~~~~~~~~~~~~
8709 where the caret is at the deref token, ranging from
8710 the start of postfix_expression to the end of the access expr. */
8711 location_t combined_loc
8712 = make_location (input_location, start_loc, parser->lexer);
8713 protected_set_expr_location (postfix_expression, combined_loc);
8717 /* We no longer need to look up names in the scope of the object on
8718 the left-hand side of the `.' or `->' operator. */
8719 parser->context->object_type = NULL_TREE;
8721 /* Outside of offsetof, these operators may not appear in
8722 constant-expressions. */
8723 if (!for_offsetof
8724 && (cp_parser_non_integral_constant_expression
8725 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8726 postfix_expression = error_mark_node;
8728 return postfix_expression;
8731 /* Parse a parenthesized expression-list.
8733 expression-list:
8734 assignment-expression
8735 expression-list, assignment-expression
8737 attribute-list:
8738 expression-list
8739 identifier
8740 identifier, expression-list
8742 CAST_P is true if this expression is the target of a cast.
8744 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8745 argument pack.
8747 WRAP_LOCATIONS_P is true if expressions within this list for which
8748 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8749 their source locations.
8751 Returns a vector of trees. Each element is a representation of an
8752 assignment-expression. NULL is returned if the ( and or ) are
8753 missing. An empty, but allocated, vector is returned on no
8754 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8755 if we are parsing an attribute list for an attribute that wants a
8756 plain identifier argument, normal_attr for an attribute that wants
8757 an expression, or non_attr if we aren't parsing an attribute list. If
8758 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8759 not all of the expressions in the list were constant.
8760 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8761 will be written to with the location of the closing parenthesis. If
8762 an error occurs, it may or may not be written to. */
8764 static vec<tree, va_gc> *
8765 cp_parser_parenthesized_expression_list (cp_parser* parser,
8766 int is_attribute_list,
8767 bool cast_p,
8768 bool allow_expansion_p,
8769 bool *non_constant_p,
8770 location_t *close_paren_loc,
8771 bool wrap_locations_p)
8773 vec<tree, va_gc> *expression_list;
8774 bool saved_greater_than_is_operator_p;
8775 bool saved_omp_array_section_p;
8777 /* Assume all the expressions will be constant. */
8778 if (non_constant_p)
8779 *non_constant_p = false;
8781 matching_parens parens;
8782 if (!parens.require_open (parser))
8783 return NULL;
8785 expression_list = make_tree_vector ();
8787 /* Within a parenthesized expression, a `>' token is always
8788 the greater-than operator. */
8789 saved_greater_than_is_operator_p
8790 = parser->greater_than_is_operator_p;
8791 parser->greater_than_is_operator_p = true;
8793 saved_omp_array_section_p = parser->omp_array_section_p;
8794 parser->omp_array_section_p = false;
8796 cp_expr expr (NULL_TREE);
8798 /* Consume expressions until there are no more. */
8799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8800 while (true)
8802 /* At the beginning of attribute lists, check to see if the
8803 next token is an identifier. */
8804 if (is_attribute_list == id_attr
8805 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8806 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8807 else if (is_attribute_list == assume_attr)
8808 expr = cp_parser_conditional_expression (parser);
8809 else if (is_attribute_list == uneval_string_attr)
8810 expr = cp_parser_unevaluated_string_literal (parser);
8811 else
8812 expr
8813 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8814 allow_expansion_p,
8815 non_constant_p);
8817 if (wrap_locations_p)
8818 expr.maybe_add_location_wrapper ();
8820 /* Add it to the list. We add error_mark_node
8821 expressions to the list, so that we can still tell if
8822 the correct form for a parenthesized expression-list
8823 is found. That gives better errors. */
8824 vec_safe_push (expression_list, expr.get_value ());
8826 if (expr == error_mark_node)
8827 goto skip_comma;
8829 /* After the first item, attribute lists look the same as
8830 expression lists. */
8831 is_attribute_list = non_attr;
8833 get_comma:;
8834 /* If the next token isn't a `,', then we are done. */
8835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8836 break;
8838 /* Otherwise, consume the `,' and keep going. */
8839 cp_lexer_consume_token (parser->lexer);
8842 if (close_paren_loc)
8843 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8845 if (!parens.require_close (parser))
8847 int ending;
8849 skip_comma:;
8850 /* We try and resync to an unnested comma, as that will give the
8851 user better diagnostics. */
8852 ending = cp_parser_skip_to_closing_parenthesis (parser,
8853 /*recovering=*/true,
8854 /*or_comma=*/true,
8855 /*consume_paren=*/true);
8856 if (ending < 0)
8857 goto get_comma;
8858 if (!ending)
8860 parser->greater_than_is_operator_p
8861 = saved_greater_than_is_operator_p;
8862 parser->omp_array_section_p = saved_omp_array_section_p;
8863 return NULL;
8867 parser->greater_than_is_operator_p
8868 = saved_greater_than_is_operator_p;
8869 parser->omp_array_section_p = saved_omp_array_section_p;
8871 return expression_list;
8874 /* Parse a pseudo-destructor-name.
8876 pseudo-destructor-name:
8877 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8878 :: [opt] nested-name-specifier template template-id :: ~ type-name
8879 :: [opt] nested-name-specifier [opt] ~ type-name
8881 If either of the first two productions is used, sets *SCOPE to the
8882 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8883 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8884 or ERROR_MARK_NODE if the parse fails. */
8886 static void
8887 cp_parser_pseudo_destructor_name (cp_parser* parser,
8888 tree object,
8889 tree* scope,
8890 tree* type)
8892 bool nested_name_specifier_p;
8894 /* Handle ~auto. */
8895 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8896 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8897 && !type_dependent_expression_p (object))
8899 if (cxx_dialect < cxx14)
8900 pedwarn (input_location, OPT_Wc__14_extensions,
8901 "%<~auto%> only available with "
8902 "%<-std=c++14%> or %<-std=gnu++14%>");
8903 cp_lexer_consume_token (parser->lexer);
8904 cp_lexer_consume_token (parser->lexer);
8905 *scope = NULL_TREE;
8906 *type = TREE_TYPE (object);
8907 return;
8910 /* Assume that things will not work out. */
8911 *type = error_mark_node;
8913 /* Look for the optional `::' operator. */
8914 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8915 /* Look for the optional nested-name-specifier. */
8916 nested_name_specifier_p
8917 = (cp_parser_nested_name_specifier_opt (parser,
8918 /*typename_keyword_p=*/false,
8919 /*check_dependency_p=*/true,
8920 /*type_p=*/false,
8921 /*is_declaration=*/false)
8922 != NULL_TREE);
8923 /* Now, if we saw a nested-name-specifier, we might be doing the
8924 second production. */
8925 if (nested_name_specifier_p
8926 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8928 /* Consume the `template' keyword. */
8929 cp_lexer_consume_token (parser->lexer);
8930 /* Parse the template-id. */
8931 cp_parser_template_id (parser,
8932 /*template_keyword_p=*/true,
8933 /*check_dependency_p=*/false,
8934 class_type,
8935 /*is_declaration=*/true);
8936 /* Look for the `::' token. */
8937 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8939 /* If the next token is not a `~', then there might be some
8940 additional qualification. */
8941 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8943 /* At this point, we're looking for "type-name :: ~". The type-name
8944 must not be a class-name, since this is a pseudo-destructor. So,
8945 it must be either an enum-name, or a typedef-name -- both of which
8946 are just identifiers. So, we peek ahead to check that the "::"
8947 and "~" tokens are present; if they are not, then we can avoid
8948 calling type_name. */
8949 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8950 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8951 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8953 cp_parser_error (parser, "non-scalar type");
8954 return;
8957 /* Look for the type-name. */
8958 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8959 if (*scope == error_mark_node)
8960 return;
8962 /* Look for the `::' token. */
8963 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8965 else
8966 *scope = NULL_TREE;
8968 /* Look for the `~'. */
8969 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8971 /* Once we see the ~, this has to be a pseudo-destructor. */
8972 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8973 cp_parser_commit_to_topmost_tentative_parse (parser);
8975 /* Look for the type-name again. We are not responsible for
8976 checking that it matches the first type-name. */
8977 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8980 /* Parse a unary-expression.
8982 unary-expression:
8983 postfix-expression
8984 ++ cast-expression
8985 -- cast-expression
8986 await-expression
8987 unary-operator cast-expression
8988 sizeof unary-expression
8989 sizeof ( type-id )
8990 alignof ( type-id ) [C++0x]
8991 new-expression
8992 delete-expression
8994 GNU Extensions:
8996 unary-expression:
8997 __extension__ cast-expression
8998 __alignof__ unary-expression
8999 __alignof__ ( type-id )
9000 alignof unary-expression [C++0x]
9001 __real__ cast-expression
9002 __imag__ cast-expression
9003 && identifier
9004 sizeof ( type-id ) { initializer-list , [opt] }
9005 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
9006 __alignof__ ( type-id ) { initializer-list , [opt] }
9008 ADDRESS_P is true iff the unary-expression is appearing as the
9009 operand of the `&' operator. CAST_P is true if this expression is
9010 the target of a cast.
9012 Returns a representation of the expression. */
9014 static cp_expr
9015 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
9016 bool address_p, bool cast_p, bool decltype_p)
9018 cp_token *token;
9019 enum tree_code unary_operator;
9021 /* Peek at the next token. */
9022 token = cp_lexer_peek_token (parser->lexer);
9023 /* Some keywords give away the kind of expression. */
9024 if (token->type == CPP_KEYWORD)
9026 enum rid keyword = token->keyword;
9028 switch (keyword)
9030 case RID_ALIGNOF:
9031 case RID_SIZEOF:
9033 tree operand, ret;
9034 enum tree_code op;
9035 location_t start_loc = token->location;
9037 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
9038 bool std_alignof = id_equal (token->u.value, "alignof");
9040 /* Consume the token. */
9041 cp_lexer_consume_token (parser->lexer);
9042 /* Parse the operand. */
9043 operand = cp_parser_sizeof_operand (parser, keyword);
9045 /* Construct a location e.g. :
9046 alignof (expr)
9047 ^~~~~~~~~~~~~~
9048 with start == caret at the start of the "alignof"/"sizeof"
9049 token, with the endpoint at the final closing paren. */
9050 location_t compound_loc
9051 = make_location (start_loc, start_loc, parser->lexer);
9053 if (TYPE_P (operand))
9054 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
9055 std_alignof, true);
9056 else
9058 /* ISO C++ defines alignof only with types, not with
9059 expressions. So pedwarn if alignof is used with a non-
9060 type expression. However, __alignof__ is ok. */
9061 if (std_alignof)
9062 pedwarn (token->location, OPT_Wpedantic,
9063 "ISO C++ does not allow %<alignof%> "
9064 "with a non-type");
9066 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
9067 std_alignof, true);
9069 /* For SIZEOF_EXPR, just issue diagnostics, but keep
9070 SIZEOF_EXPR with the original operand. */
9071 if (op == SIZEOF_EXPR && ret != error_mark_node)
9073 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
9075 if (!processing_template_decl && TYPE_P (operand))
9077 ret = build_min (SIZEOF_EXPR, size_type_node,
9078 build1 (NOP_EXPR, operand,
9079 error_mark_node));
9080 SIZEOF_EXPR_TYPE_P (ret) = 1;
9082 else
9083 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
9084 TREE_SIDE_EFFECTS (ret) = 0;
9085 TREE_READONLY (ret) = 1;
9086 SET_EXPR_LOCATION (ret, compound_loc);
9090 cp_expr ret_expr (ret, compound_loc);
9091 ret_expr = ret_expr.maybe_add_location_wrapper ();
9092 return ret_expr;
9095 case RID_BUILTIN_HAS_ATTRIBUTE:
9096 return cp_parser_has_attribute_expression (parser);
9098 case RID_NEW:
9099 return cp_parser_new_expression (parser);
9101 case RID_DELETE:
9102 return cp_parser_delete_expression (parser);
9104 case RID_EXTENSION:
9106 /* The saved value of the PEDANTIC flag. */
9107 int saved_pedantic;
9108 tree expr;
9110 /* Save away the PEDANTIC flag. */
9111 cp_parser_extension_opt (parser, &saved_pedantic);
9112 /* Parse the cast-expression. */
9113 expr = cp_parser_simple_cast_expression (parser);
9114 /* Restore the PEDANTIC flag. */
9115 pedantic = saved_pedantic;
9117 return expr;
9120 case RID_REALPART:
9121 case RID_IMAGPART:
9123 tree expression;
9125 /* Consume the `__real__' or `__imag__' token. */
9126 cp_lexer_consume_token (parser->lexer);
9127 /* Parse the cast-expression. */
9128 expression = cp_parser_simple_cast_expression (parser);
9129 /* Create the complete representation. */
9130 return build_x_unary_op (token->location,
9131 (keyword == RID_REALPART
9132 ? REALPART_EXPR : IMAGPART_EXPR),
9133 expression, NULL_TREE,
9134 tf_warning_or_error);
9136 break;
9138 case RID_TRANSACTION_ATOMIC:
9139 case RID_TRANSACTION_RELAXED:
9140 return cp_parser_transaction_expression (parser, keyword);
9142 case RID_NOEXCEPT:
9144 tree expr;
9145 const char *saved_message;
9146 bool saved_integral_constant_expression_p;
9147 bool saved_non_integral_constant_expression_p;
9148 bool saved_greater_than_is_operator_p;
9150 location_t start_loc = token->location;
9152 cp_lexer_consume_token (parser->lexer);
9153 matching_parens parens;
9154 parens.require_open (parser);
9156 saved_message = parser->type_definition_forbidden_message;
9157 parser->type_definition_forbidden_message
9158 = G_("types may not be defined in %<noexcept%> expressions");
9160 saved_integral_constant_expression_p
9161 = parser->integral_constant_expression_p;
9162 saved_non_integral_constant_expression_p
9163 = parser->non_integral_constant_expression_p;
9164 parser->integral_constant_expression_p = false;
9166 saved_greater_than_is_operator_p
9167 = parser->greater_than_is_operator_p;
9168 parser->greater_than_is_operator_p = true;
9170 ++cp_unevaluated_operand;
9171 ++c_inhibit_evaluation_warnings;
9172 ++cp_noexcept_operand;
9173 expr = cp_parser_expression (parser);
9174 --cp_noexcept_operand;
9175 --c_inhibit_evaluation_warnings;
9176 --cp_unevaluated_operand;
9178 parser->greater_than_is_operator_p
9179 = saved_greater_than_is_operator_p;
9181 parser->integral_constant_expression_p
9182 = saved_integral_constant_expression_p;
9183 parser->non_integral_constant_expression_p
9184 = saved_non_integral_constant_expression_p;
9186 parser->type_definition_forbidden_message = saved_message;
9188 parens.require_close (parser);
9190 /* Construct a location of the form:
9191 noexcept (expr)
9192 ^~~~~~~~~~~~~~~
9193 with start == caret, finishing at the close-paren. */
9194 location_t noexcept_loc
9195 = make_location (start_loc, start_loc, parser->lexer);
9197 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9198 noexcept_loc);
9201 case RID_CO_AWAIT:
9203 tree expr;
9204 location_t kw_loc = token->location;
9206 /* Consume the `co_await' token. */
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Parse its cast-expression. */
9209 expr = cp_parser_simple_cast_expression (parser);
9210 if (expr == error_mark_node)
9211 return error_mark_node;
9213 /* Handle [expr.await]. */
9214 return cp_expr (finish_co_await_expr (kw_loc, expr));
9217 default:
9218 break;
9222 /* Look for the `:: new' and `:: delete', which also signal the
9223 beginning of a new-expression, or delete-expression,
9224 respectively. If the next token is `::', then it might be one of
9225 these. */
9226 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9228 enum rid keyword;
9230 /* See if the token after the `::' is one of the keywords in
9231 which we're interested. */
9232 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9233 /* If it's `new', we have a new-expression. */
9234 if (keyword == RID_NEW)
9235 return cp_parser_new_expression (parser);
9236 /* Similarly, for `delete'. */
9237 else if (keyword == RID_DELETE)
9238 return cp_parser_delete_expression (parser);
9241 /* Look for a unary operator. */
9242 unary_operator = cp_parser_unary_operator (token);
9243 /* The `++' and `--' operators can be handled similarly, even though
9244 they are not technically unary-operators in the grammar. */
9245 if (unary_operator == ERROR_MARK)
9247 if (token->type == CPP_PLUS_PLUS)
9248 unary_operator = PREINCREMENT_EXPR;
9249 else if (token->type == CPP_MINUS_MINUS)
9250 unary_operator = PREDECREMENT_EXPR;
9251 /* Handle the GNU address-of-label extension. */
9252 else if (cp_parser_allow_gnu_extensions_p (parser)
9253 && token->type == CPP_AND_AND)
9255 tree identifier;
9256 tree expression;
9257 location_t start_loc = token->location;
9259 /* Consume the '&&' token. */
9260 cp_lexer_consume_token (parser->lexer);
9261 /* Look for the identifier. */
9262 identifier = cp_parser_identifier (parser);
9263 /* Construct a location of the form:
9264 &&label
9265 ^~~~~~~
9266 with caret==start at the "&&", finish at the end of the label. */
9267 location_t combined_loc
9268 = make_location (start_loc, start_loc, parser->lexer);
9269 /* Create an expression representing the address. */
9270 expression = finish_label_address_expr (identifier, combined_loc);
9271 if (TREE_CODE (expression) == ADDR_EXPR)
9272 mark_label_addressed (identifier);
9273 if (cp_parser_non_integral_constant_expression (parser,
9274 NIC_ADDR_LABEL))
9275 expression = error_mark_node;
9276 return expression;
9279 if (unary_operator != ERROR_MARK)
9281 cp_expr cast_expression;
9282 cp_expr expression = error_mark_node;
9283 non_integral_constant non_constant_p = NIC_NONE;
9284 location_t loc = token->location;
9285 tsubst_flags_t complain = complain_flags (decltype_p);
9287 /* Consume the operator token. */
9288 token = cp_lexer_consume_token (parser->lexer);
9289 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9291 /* Parse the cast-expression. */
9292 cast_expression
9293 = cp_parser_cast_expression (parser,
9294 unary_operator == ADDR_EXPR,
9295 /*cast_p=*/false,
9296 /*decltype*/false,
9297 pidk);
9299 /* Make a location:
9300 OP_TOKEN CAST_EXPRESSION
9301 ^~~~~~~~~~~~~~~~~~~~~~~~~
9302 with start==caret at the operator token, and
9303 extending to the end of the cast_expression. */
9304 loc = make_location (loc, loc, cast_expression.get_finish ());
9306 /* Now, build an appropriate representation. */
9307 switch (unary_operator)
9309 case INDIRECT_REF:
9310 non_constant_p = NIC_STAR;
9311 expression = build_x_indirect_ref (loc, cast_expression,
9312 RO_UNARY_STAR, NULL_TREE,
9313 complain);
9314 /* TODO: build_x_indirect_ref does not always honor the
9315 location, so ensure it is set. */
9316 expression.set_location (loc);
9317 break;
9319 case ADDR_EXPR:
9320 non_constant_p = NIC_ADDR;
9321 /* Fall through. */
9322 case BIT_NOT_EXPR:
9323 expression = build_x_unary_op (loc, unary_operator,
9324 cast_expression,
9325 NULL_TREE, complain);
9326 /* TODO: build_x_unary_op does not always honor the location,
9327 so ensure it is set. */
9328 expression.set_location (loc);
9329 break;
9331 case PREINCREMENT_EXPR:
9332 case PREDECREMENT_EXPR:
9333 non_constant_p = unary_operator == PREINCREMENT_EXPR
9334 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9335 /* Fall through. */
9336 case NEGATE_EXPR:
9337 /* Immediately fold negation of a constant, unless the constant is 0
9338 (since -0 == 0) or it would overflow. */
9339 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9341 tree stripped_expr
9342 = tree_strip_any_location_wrapper (cast_expression);
9343 if (CONSTANT_CLASS_P (stripped_expr)
9344 && !integer_zerop (stripped_expr)
9345 && !TREE_OVERFLOW (stripped_expr))
9347 tree folded = fold_build1 (unary_operator,
9348 TREE_TYPE (stripped_expr),
9349 stripped_expr);
9350 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9352 expression = maybe_wrap_with_location (folded, loc);
9353 break;
9357 /* Fall through. */
9358 case UNARY_PLUS_EXPR:
9359 case TRUTH_NOT_EXPR:
9360 expression = finish_unary_op_expr (loc, unary_operator,
9361 cast_expression, complain);
9362 break;
9364 default:
9365 gcc_unreachable ();
9368 if (non_constant_p != NIC_NONE
9369 && cp_parser_non_integral_constant_expression (parser,
9370 non_constant_p))
9371 expression = error_mark_node;
9373 return expression;
9376 return cp_parser_postfix_expression (parser, address_p, cast_p,
9377 /*member_access_only_p=*/false,
9378 decltype_p,
9379 pidk);
9382 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9383 unary-operator, the corresponding tree code is returned. */
9385 static enum tree_code
9386 cp_parser_unary_operator (cp_token* token)
9388 switch (token->type)
9390 case CPP_MULT:
9391 return INDIRECT_REF;
9393 case CPP_AND:
9394 return ADDR_EXPR;
9396 case CPP_PLUS:
9397 return UNARY_PLUS_EXPR;
9399 case CPP_MINUS:
9400 return NEGATE_EXPR;
9402 case CPP_NOT:
9403 return TRUTH_NOT_EXPR;
9405 case CPP_COMPL:
9406 return BIT_NOT_EXPR;
9408 default:
9409 return ERROR_MARK;
9413 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9414 Returns a representation of the expression. */
9416 static tree
9417 cp_parser_has_attribute_expression (cp_parser *parser)
9419 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9421 /* Consume the __builtin_has_attribute token. */
9422 cp_lexer_consume_token (parser->lexer);
9424 matching_parens parens;
9425 if (!parens.require_open (parser))
9426 return error_mark_node;
9428 /* Types cannot be defined in a `sizeof' expression. Save away the
9429 old message. */
9430 const char *saved_message = parser->type_definition_forbidden_message;
9431 const char *saved_message_arg
9432 = parser->type_definition_forbidden_message_arg;
9433 parser->type_definition_forbidden_message
9434 = G_("types may not be defined in %qs expressions");
9435 parser->type_definition_forbidden_message_arg
9436 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9438 /* The restrictions on constant-expressions do not apply inside
9439 sizeof expressions. */
9440 bool saved_integral_constant_expression_p
9441 = parser->integral_constant_expression_p;
9442 bool saved_non_integral_constant_expression_p
9443 = parser->non_integral_constant_expression_p;
9444 parser->integral_constant_expression_p = false;
9446 /* Do not actually evaluate the expression. */
9447 ++cp_unevaluated_operand;
9448 ++c_inhibit_evaluation_warnings;
9450 tree oper = NULL_TREE;
9452 /* We can't be sure yet whether we're looking at a type-id or an
9453 expression. */
9454 cp_parser_parse_tentatively (parser);
9456 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9457 parser->in_type_id_in_expr_p = true;
9458 /* Look for the type-id. */
9459 oper = cp_parser_type_id (parser);
9460 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9462 cp_parser_parse_definitely (parser);
9464 /* If the type-id production did not work out, then we must be
9465 looking at an expression. */
9466 if (!oper || oper == error_mark_node)
9467 oper = cp_parser_assignment_expression (parser);
9469 STRIP_ANY_LOCATION_WRAPPER (oper);
9471 /* Go back to evaluating expressions. */
9472 --cp_unevaluated_operand;
9473 --c_inhibit_evaluation_warnings;
9475 /* And restore the old one. */
9476 parser->type_definition_forbidden_message = saved_message;
9477 parser->type_definition_forbidden_message_arg = saved_message_arg;
9478 parser->integral_constant_expression_p
9479 = saved_integral_constant_expression_p;
9480 parser->non_integral_constant_expression_p
9481 = saved_non_integral_constant_expression_p;
9483 /* Consume the comma if it's there. */
9484 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9486 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9487 /*consume_paren=*/true);
9488 return error_mark_node;
9491 /* Parse the attribute specification. */
9492 bool ret = false;
9493 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9494 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9496 if (oper == error_mark_node)
9497 /* Nothing. */;
9498 else if (processing_template_decl && uses_template_parms (oper))
9499 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9500 "not supported yet");
9501 else
9503 /* Fold constant expressions used in attributes first. */
9504 cp_check_const_attributes (attr);
9506 /* Finally, see if OPER has been declared with ATTR. */
9507 ret = has_attribute (atloc, oper, attr, default_conversion);
9510 parens.require_close (parser);
9512 else
9514 error_at (atloc, "expected identifier");
9515 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9518 /* Construct a location e.g. :
9519 __builtin_has_attribute (oper, attr)
9520 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9521 with start == caret at the start of the built-in token,
9522 and with the endpoint at the final closing paren. */
9523 location_t compound_loc
9524 = make_location (start_loc, start_loc, parser->lexer);
9526 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9527 ret_expr.set_location (compound_loc);
9528 ret_expr = ret_expr.maybe_add_location_wrapper ();
9529 return ret_expr;
9532 /* Parse a new-expression.
9534 new-expression:
9535 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9536 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9538 Returns a representation of the expression. */
9540 static tree
9541 cp_parser_new_expression (cp_parser* parser)
9543 bool global_scope_p;
9544 vec<tree, va_gc> *placement;
9545 tree type;
9546 vec<tree, va_gc> *initializer;
9547 tree nelts = NULL_TREE;
9548 tree ret;
9550 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9552 /* Look for the optional `::' operator. */
9553 global_scope_p
9554 = (cp_parser_global_scope_opt (parser,
9555 /*current_scope_valid_p=*/false)
9556 != NULL_TREE);
9557 /* Look for the `new' operator. */
9558 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9559 /* There's no easy way to tell a new-placement from the
9560 `( type-id )' construct. */
9561 cp_parser_parse_tentatively (parser);
9562 /* Look for a new-placement. */
9563 placement = cp_parser_new_placement (parser);
9564 /* If that didn't work out, there's no new-placement. */
9565 if (!cp_parser_parse_definitely (parser))
9567 if (placement != NULL)
9568 release_tree_vector (placement);
9569 placement = NULL;
9572 /* If the next token is a `(', then we have a parenthesized
9573 type-id. */
9574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9576 cp_token *token;
9577 const char *saved_message = parser->type_definition_forbidden_message;
9579 /* Consume the `('. */
9580 matching_parens parens;
9581 parens.consume_open (parser);
9583 /* Parse the type-id. */
9584 parser->type_definition_forbidden_message
9585 = G_("types may not be defined in a new-expression");
9587 type_id_in_expr_sentinel s (parser);
9588 type = cp_parser_type_id (parser);
9590 parser->type_definition_forbidden_message = saved_message;
9592 /* Look for the closing `)'. */
9593 parens.require_close (parser);
9594 token = cp_lexer_peek_token (parser->lexer);
9595 /* There should not be a direct-new-declarator in this production,
9596 but GCC used to allowed this, so we check and emit a sensible error
9597 message for this case. */
9598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9600 error_at (token->location,
9601 "array bound forbidden after parenthesized type-id");
9602 inform (token->location,
9603 "try removing the parentheses around the type-id");
9604 cp_parser_direct_new_declarator (parser);
9607 /* Otherwise, there must be a new-type-id. */
9608 else
9609 type = cp_parser_new_type_id (parser, &nelts);
9611 /* If the next token is a `(' or '{', then we have a new-initializer. */
9612 cp_token *token = cp_lexer_peek_token (parser->lexer);
9613 if (token->type == CPP_OPEN_PAREN
9614 || token->type == CPP_OPEN_BRACE)
9615 initializer = cp_parser_new_initializer (parser);
9616 else
9617 initializer = NULL;
9619 /* A new-expression may not appear in an integral constant
9620 expression. */
9621 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9622 ret = error_mark_node;
9623 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9624 of a new-type-id or type-id of a new-expression, the new-expression shall
9625 contain a new-initializer of the form ( assignment-expression )".
9626 Additionally, consistently with the spirit of DR 1467, we want to accept
9627 'new auto { 2 }' too. */
9628 else if ((ret = type_uses_auto (type))
9629 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9630 && (vec_safe_length (initializer) != 1
9631 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9632 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9634 error_at (token->location,
9635 "initialization of new-expression for type %<auto%> "
9636 "requires exactly one element");
9637 ret = error_mark_node;
9639 else
9641 /* Construct a location e.g.:
9642 ptr = new int[100]
9643 ^~~~~~~~~~~~
9644 with caret == start at the start of the "new" token, and the end
9645 at the end of the final token we consumed. */
9646 location_t combined_loc = make_location (start_loc, start_loc,
9647 parser->lexer);
9648 /* Create a representation of the new-expression. */
9649 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9650 global_scope_p, tf_warning_or_error);
9653 if (placement != NULL)
9654 release_tree_vector (placement);
9655 if (initializer != NULL)
9656 release_tree_vector (initializer);
9658 return ret;
9661 /* Parse a new-placement.
9663 new-placement:
9664 ( expression-list )
9666 Returns the same representation as for an expression-list. */
9668 static vec<tree, va_gc> *
9669 cp_parser_new_placement (cp_parser* parser)
9671 vec<tree, va_gc> *expression_list;
9673 /* Parse the expression-list. */
9674 expression_list = (cp_parser_parenthesized_expression_list
9675 (parser, non_attr, /*cast_p=*/false,
9676 /*allow_expansion_p=*/true,
9677 /*non_constant_p=*/NULL));
9679 if (expression_list && expression_list->is_empty ())
9680 error ("expected expression-list or type-id");
9682 return expression_list;
9685 /* Parse a new-type-id.
9687 new-type-id:
9688 type-specifier-seq new-declarator [opt]
9690 Returns the TYPE allocated. If the new-type-id indicates an array
9691 type, *NELTS is set to the number of elements in the last array
9692 bound; the TYPE will not include the last array bound. */
9694 static tree
9695 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9697 cp_decl_specifier_seq type_specifier_seq;
9698 cp_declarator *new_declarator;
9699 cp_declarator *declarator;
9700 cp_declarator *outer_declarator;
9701 const char *saved_message;
9703 /* The type-specifier sequence must not contain type definitions.
9704 (It cannot contain declarations of new types either, but if they
9705 are not definitions we will catch that because they are not
9706 complete.) */
9707 saved_message = parser->type_definition_forbidden_message;
9708 parser->type_definition_forbidden_message
9709 = G_("types may not be defined in a new-type-id");
9710 /* Parse the type-specifier-seq. */
9711 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9712 /*is_declaration=*/false,
9713 /*is_trailing_return=*/false,
9714 &type_specifier_seq);
9715 /* Restore the old message. */
9716 parser->type_definition_forbidden_message = saved_message;
9718 if (type_specifier_seq.type == error_mark_node)
9719 return error_mark_node;
9721 /* Parse the new-declarator. */
9722 new_declarator = cp_parser_new_declarator_opt (parser);
9724 /* Determine the number of elements in the last array dimension, if
9725 any. */
9726 *nelts = NULL_TREE;
9727 /* Skip down to the last array dimension. */
9728 declarator = new_declarator;
9729 outer_declarator = NULL;
9730 while (declarator && (declarator->kind == cdk_pointer
9731 || declarator->kind == cdk_ptrmem))
9733 outer_declarator = declarator;
9734 declarator = declarator->declarator;
9736 while (declarator
9737 && declarator->kind == cdk_array
9738 && declarator->declarator
9739 && declarator->declarator->kind == cdk_array)
9741 outer_declarator = declarator;
9742 declarator = declarator->declarator;
9745 if (declarator && declarator->kind == cdk_array)
9747 *nelts = declarator->u.array.bounds;
9748 if (*nelts == error_mark_node)
9749 *nelts = integer_one_node;
9751 if (*nelts == NULL_TREE)
9752 /* Leave [] in the declarator. */;
9753 else if (outer_declarator)
9754 outer_declarator->declarator = declarator->declarator;
9755 else
9756 new_declarator = NULL;
9759 return groktypename (&type_specifier_seq, new_declarator, false);
9762 /* Parse an (optional) new-declarator.
9764 new-declarator:
9765 ptr-operator new-declarator [opt]
9766 direct-new-declarator
9768 Returns the declarator. */
9770 static cp_declarator *
9771 cp_parser_new_declarator_opt (cp_parser* parser)
9773 enum tree_code code;
9774 tree type, std_attributes = NULL_TREE;
9775 cp_cv_quals cv_quals;
9777 /* We don't know if there's a ptr-operator next, or not. */
9778 cp_parser_parse_tentatively (parser);
9779 /* Look for a ptr-operator. */
9780 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9781 /* If that worked, look for more new-declarators. */
9782 if (cp_parser_parse_definitely (parser))
9784 cp_declarator *declarator;
9786 /* Parse another optional declarator. */
9787 declarator = cp_parser_new_declarator_opt (parser);
9789 declarator = cp_parser_make_indirect_declarator
9790 (code, type, cv_quals, declarator, std_attributes);
9792 return declarator;
9795 /* If the next token is a `[', there is a direct-new-declarator. */
9796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9797 return cp_parser_direct_new_declarator (parser);
9799 return NULL;
9802 /* Parse a direct-new-declarator.
9804 direct-new-declarator:
9805 [ expression ]
9806 direct-new-declarator [constant-expression]
9810 static cp_declarator *
9811 cp_parser_direct_new_declarator (cp_parser* parser)
9813 cp_declarator *declarator = NULL;
9814 bool first_p = true;
9816 while (true)
9818 tree expression;
9819 cp_token *token;
9821 /* Look for the opening `['. */
9822 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9824 token = cp_lexer_peek_token (parser->lexer);
9825 if (token->type == CPP_CLOSE_SQUARE && first_p)
9826 expression = NULL_TREE;
9827 else
9828 expression = cp_parser_expression (parser);
9829 /* The standard requires that the expression have integral
9830 type. DR 74 adds enumeration types. We believe that the
9831 real intent is that these expressions be handled like the
9832 expression in a `switch' condition, which also allows
9833 classes with a single conversion to integral or
9834 enumeration type. */
9835 if (expression && !processing_template_decl)
9837 expression
9838 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9839 expression,
9840 /*complain=*/true);
9841 if (!expression)
9843 error_at (token->location,
9844 "expression in new-declarator must have integral "
9845 "or enumeration type");
9846 expression = error_mark_node;
9850 /* Look for the closing `]'. */
9851 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9853 /* Add this bound to the declarator. */
9854 declarator = make_array_declarator (declarator, expression);
9856 /* If the next token is not a `[', then there are no more
9857 bounds. */
9858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9859 break;
9860 first_p = false;
9863 return declarator;
9866 /* Parse a new-initializer.
9868 new-initializer:
9869 ( expression-list [opt] )
9870 braced-init-list
9872 Returns a representation of the expression-list. */
9874 static vec<tree, va_gc> *
9875 cp_parser_new_initializer (cp_parser* parser)
9877 vec<tree, va_gc> *expression_list;
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9881 cp_lexer_set_source_position (parser->lexer);
9882 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9883 tree t = cp_parser_braced_list (parser);
9884 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9885 expression_list = make_tree_vector_single (t);
9887 else
9888 expression_list = (cp_parser_parenthesized_expression_list
9889 (parser, non_attr, /*cast_p=*/false,
9890 /*allow_expansion_p=*/true,
9891 /*non_constant_p=*/NULL));
9893 return expression_list;
9896 /* Parse a delete-expression.
9898 delete-expression:
9899 :: [opt] delete cast-expression
9900 :: [opt] delete [ ] cast-expression
9902 Returns a representation of the expression. */
9904 static tree
9905 cp_parser_delete_expression (cp_parser* parser)
9907 bool global_scope_p;
9908 bool array_p;
9909 tree expression;
9910 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9912 /* Look for the optional `::' operator. */
9913 global_scope_p
9914 = (cp_parser_global_scope_opt (parser,
9915 /*current_scope_valid_p=*/false)
9916 != NULL_TREE);
9917 /* Look for the `delete' keyword. */
9918 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9919 /* See if the array syntax is in use. */
9920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9922 /* Consume the `[' token. */
9923 cp_lexer_consume_token (parser->lexer);
9924 /* Look for the `]' token. */
9925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9926 /* Remember that this is the `[]' construct. */
9927 array_p = true;
9929 else
9930 array_p = false;
9932 /* Parse the cast-expression. */
9933 expression = cp_parser_simple_cast_expression (parser);
9935 /* A delete-expression may not appear in an integral constant
9936 expression. */
9937 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9938 return error_mark_node;
9940 /* Construct a location e.g.:
9941 delete [ ] ptr
9942 ^~~~~~~~~~~~~~
9943 with caret == start at the start of the "delete" token, and
9944 the end at the end of the final token we consumed. */
9945 location_t combined_loc = make_location (start_loc, start_loc,
9946 parser->lexer);
9947 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9948 global_scope_p, tf_warning_or_error);
9950 return expression;
9953 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9954 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9955 0 otherwise. */
9957 static int
9958 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9960 cp_token *token = cp_lexer_peek_token (parser->lexer);
9961 switch (token->type)
9963 case CPP_COMMA:
9964 case CPP_SEMICOLON:
9965 case CPP_QUERY:
9966 case CPP_COLON:
9967 case CPP_CLOSE_SQUARE:
9968 case CPP_CLOSE_PAREN:
9969 case CPP_CLOSE_BRACE:
9970 case CPP_OPEN_BRACE:
9971 case CPP_DOT:
9972 case CPP_DOT_STAR:
9973 case CPP_DEREF:
9974 case CPP_DEREF_STAR:
9975 case CPP_DIV:
9976 case CPP_MOD:
9977 case CPP_LSHIFT:
9978 case CPP_RSHIFT:
9979 case CPP_LESS:
9980 case CPP_GREATER:
9981 case CPP_LESS_EQ:
9982 case CPP_GREATER_EQ:
9983 case CPP_EQ_EQ:
9984 case CPP_NOT_EQ:
9985 case CPP_EQ:
9986 case CPP_MULT_EQ:
9987 case CPP_DIV_EQ:
9988 case CPP_MOD_EQ:
9989 case CPP_PLUS_EQ:
9990 case CPP_MINUS_EQ:
9991 case CPP_RSHIFT_EQ:
9992 case CPP_LSHIFT_EQ:
9993 case CPP_AND_EQ:
9994 case CPP_XOR_EQ:
9995 case CPP_OR_EQ:
9996 case CPP_XOR:
9997 case CPP_OR:
9998 case CPP_OR_OR:
9999 case CPP_EOF:
10000 case CPP_ELLIPSIS:
10001 return 0;
10003 case CPP_OPEN_PAREN:
10004 /* In ((type ()) () the last () isn't a valid cast-expression,
10005 so the whole must be parsed as postfix-expression. */
10006 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
10007 != CPP_CLOSE_PAREN;
10009 case CPP_OPEN_SQUARE:
10010 /* '[' may start a primary-expression in obj-c++ and in C++11,
10011 as a lambda-expression, eg, '(void)[]{}'. */
10012 if (cxx_dialect >= cxx11)
10013 return -1;
10014 return c_dialect_objc ();
10016 case CPP_PLUS_PLUS:
10017 case CPP_MINUS_MINUS:
10018 /* '++' and '--' may or may not start a cast-expression:
10020 struct T { void operator++(int); };
10021 void f() { (T())++; }
10025 int a;
10026 (int)++a; */
10027 return -1;
10029 default:
10030 return 1;
10034 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
10035 in the order: const_cast, static_cast, reinterpret_cast.
10037 Don't suggest dynamic_cast.
10039 Return the first legal cast kind found, or NULL otherwise. */
10041 static const char *
10042 get_cast_suggestion (tree dst_type, tree orig_expr)
10044 tree trial;
10046 /* Reuse the parser logic by attempting to build the various kinds of
10047 cast, with "complain" disabled.
10048 Identify the first such cast that is valid. */
10050 /* Don't attempt to run such logic within template processing. */
10051 if (processing_template_decl)
10052 return NULL;
10054 /* First try const_cast. */
10055 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
10056 if (trial != error_mark_node)
10057 return "const_cast";
10059 /* If that fails, try static_cast. */
10060 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
10061 if (trial != error_mark_node)
10062 return "static_cast";
10064 /* Finally, try reinterpret_cast. */
10065 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
10066 tf_none);
10067 if (trial != error_mark_node)
10068 return "reinterpret_cast";
10070 /* No such cast possible. */
10071 return NULL;
10074 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
10075 suggesting how to convert a C-style cast of the form:
10077 (DST_TYPE)ORIG_EXPR
10079 to a C++-style cast.
10081 The primary range of RICHLOC is asssumed to be that of the original
10082 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
10083 of the parens in the C-style cast. */
10085 static void
10086 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
10087 location_t close_paren_loc, tree orig_expr,
10088 tree dst_type)
10090 /* This function is non-trivial, so bail out now if the warning isn't
10091 going to be emitted. */
10092 if (!warn_old_style_cast)
10093 return;
10095 /* Try to find a legal C++ cast, trying them in order:
10096 const_cast, static_cast, reinterpret_cast. */
10097 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
10098 if (!cast_suggestion)
10099 return;
10101 /* Replace the open paren with "CAST_SUGGESTION<". */
10102 pretty_printer pp;
10103 pp_string (&pp, cast_suggestion);
10104 pp_less (&pp);
10105 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
10107 /* Replace the close paren with "> (". */
10108 rich_loc->add_fixit_replace (close_paren_loc, "> (");
10110 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
10111 rich_loc->add_fixit_insert_after (")");
10115 /* Parse a cast-expression.
10117 cast-expression:
10118 unary-expression
10119 ( type-id ) cast-expression
10121 ADDRESS_P is true iff the unary-expression is appearing as the
10122 operand of the `&' operator. CAST_P is true if this expression is
10123 the target of a cast.
10125 Returns a representation of the expression. */
10127 static cp_expr
10128 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10129 bool decltype_p, cp_id_kind * pidk)
10131 /* If it's a `(', then we might be looking at a cast. */
10132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10134 tree type = NULL_TREE;
10135 cp_expr expr (NULL_TREE);
10136 int cast_expression = 0;
10137 const char *saved_message;
10139 /* There's no way to know yet whether or not this is a cast.
10140 For example, `(int (3))' is a unary-expression, while `(int)
10141 3' is a cast. So, we resort to parsing tentatively. */
10142 cp_parser_parse_tentatively (parser);
10143 /* Types may not be defined in a cast. */
10144 saved_message = parser->type_definition_forbidden_message;
10145 parser->type_definition_forbidden_message
10146 = G_("types may not be defined in casts");
10147 /* Consume the `('. */
10148 matching_parens parens;
10149 cp_token *open_paren = parens.consume_open (parser);
10150 location_t open_paren_loc = open_paren->location;
10151 location_t close_paren_loc = UNKNOWN_LOCATION;
10153 /* A very tricky bit is that `(struct S) { 3 }' is a
10154 compound-literal (which we permit in C++ as an extension).
10155 But, that construct is not a cast-expression -- it is a
10156 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10157 is legal; if the compound-literal were a cast-expression,
10158 you'd need an extra set of parentheses.) But, if we parse
10159 the type-id, and it happens to be a class-specifier, then we
10160 will commit to the parse at that point, because we cannot
10161 undo the action that is done when creating a new class. So,
10162 then we cannot back up and do a postfix-expression.
10164 Another tricky case is the following (c++/29234):
10166 struct S { void operator () (); };
10168 void foo ()
10170 ( S()() );
10173 As a type-id we parse the parenthesized S()() as a function
10174 returning a function, groktypename complains and we cannot
10175 back up in this case either.
10177 Therefore, we scan ahead to the closing `)', and check to see
10178 if the tokens after the `)' can start a cast-expression. Otherwise
10179 we are dealing with an unary-expression, a postfix-expression
10180 or something else.
10182 Yet another tricky case, in C++11, is the following (c++/54891):
10184 (void)[]{};
10186 The issue is that usually, besides the case of lambda-expressions,
10187 the parenthesized type-id cannot be followed by '[', and, eg, we
10188 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10189 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10190 we don't commit, we try a cast-expression, then an unary-expression.
10192 Save tokens so that we can put them back. */
10193 cp_lexer_save_tokens (parser->lexer);
10195 /* We may be looking at a cast-expression. */
10196 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10197 /*consume_paren=*/true))
10198 cast_expression
10199 = cp_parser_tokens_start_cast_expression (parser);
10201 /* Roll back the tokens we skipped. */
10202 cp_lexer_rollback_tokens (parser->lexer);
10203 /* If we aren't looking at a cast-expression, simulate an error so
10204 that the call to cp_parser_error_occurred below returns true. */
10205 if (!cast_expression)
10206 cp_parser_simulate_error (parser);
10207 else
10209 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10210 parser->in_type_id_in_expr_p = true;
10211 /* Look for the type-id. */
10212 type = cp_parser_type_id (parser);
10213 /* Look for the closing `)'. */
10214 cp_token *close_paren = parens.require_close (parser);
10215 if (close_paren)
10216 close_paren_loc = close_paren->location;
10217 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10220 /* Restore the saved message. */
10221 parser->type_definition_forbidden_message = saved_message;
10223 /* At this point this can only be either a cast or a
10224 parenthesized ctor such as `(T ())' that looks like a cast to
10225 function returning T. */
10226 if (!cp_parser_error_occurred (parser))
10228 /* Only commit if the cast-expression doesn't start with
10229 '++', '--', or '[' in C++11. */
10230 if (cast_expression > 0)
10231 cp_parser_commit_to_topmost_tentative_parse (parser);
10233 expr = cp_parser_cast_expression (parser,
10234 /*address_p=*/false,
10235 /*cast_p=*/true,
10236 /*decltype_p=*/false,
10237 pidk);
10239 if (cp_parser_parse_definitely (parser))
10241 /* Warn about old-style casts, if so requested. */
10242 if (warn_old_style_cast
10243 && !in_system_header_at (input_location)
10244 && !VOID_TYPE_P (type)
10245 && current_lang_name != lang_name_c)
10247 gcc_rich_location rich_loc (input_location);
10248 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10249 expr, type);
10250 warning_at (&rich_loc, OPT_Wold_style_cast,
10251 "use of old-style cast to %q#T", type);
10254 /* Only type conversions to integral or enumeration types
10255 can be used in constant-expressions. */
10256 if (!cast_valid_in_integral_constant_expression_p (type)
10257 && cp_parser_non_integral_constant_expression (parser,
10258 NIC_CAST))
10259 return error_mark_node;
10261 /* Perform the cast. */
10262 /* Make a location:
10263 (TYPE) EXPR
10264 ^~~~~~~~~~~
10265 with start==caret at the open paren, extending to the
10266 end of "expr". */
10267 location_t cast_loc = make_location (open_paren_loc,
10268 open_paren_loc,
10269 expr.get_finish ());
10270 expr = build_c_cast (cast_loc, type, expr);
10271 return expr;
10274 else
10275 cp_parser_abort_tentative_parse (parser);
10278 /* If we get here, then it's not a cast, so it must be a
10279 unary-expression. */
10280 return cp_parser_unary_expression (parser, pidk, address_p,
10281 cast_p, decltype_p);
10284 /* Parse a binary expression of the general form:
10286 pm-expression:
10287 cast-expression
10288 pm-expression .* cast-expression
10289 pm-expression ->* cast-expression
10291 multiplicative-expression:
10292 pm-expression
10293 multiplicative-expression * pm-expression
10294 multiplicative-expression / pm-expression
10295 multiplicative-expression % pm-expression
10297 additive-expression:
10298 multiplicative-expression
10299 additive-expression + multiplicative-expression
10300 additive-expression - multiplicative-expression
10302 shift-expression:
10303 additive-expression
10304 shift-expression << additive-expression
10305 shift-expression >> additive-expression
10307 relational-expression:
10308 shift-expression
10309 relational-expression < shift-expression
10310 relational-expression > shift-expression
10311 relational-expression <= shift-expression
10312 relational-expression >= shift-expression
10314 GNU Extension:
10316 relational-expression:
10317 relational-expression <? shift-expression
10318 relational-expression >? shift-expression
10320 equality-expression:
10321 relational-expression
10322 equality-expression == relational-expression
10323 equality-expression != relational-expression
10325 and-expression:
10326 equality-expression
10327 and-expression & equality-expression
10329 exclusive-or-expression:
10330 and-expression
10331 exclusive-or-expression ^ and-expression
10333 inclusive-or-expression:
10334 exclusive-or-expression
10335 inclusive-or-expression | exclusive-or-expression
10337 logical-and-expression:
10338 inclusive-or-expression
10339 logical-and-expression && inclusive-or-expression
10341 logical-or-expression:
10342 logical-and-expression
10343 logical-or-expression || logical-and-expression
10345 All these are implemented with a single function like:
10347 binary-expression:
10348 simple-cast-expression
10349 binary-expression <token> binary-expression
10351 CAST_P is true if this expression is the target of a cast.
10353 The binops_by_token map is used to get the tree codes for each <token> type.
10354 binary-expressions are associated according to a precedence table. */
10356 #define TOKEN_PRECEDENCE(token) \
10357 (((token->type == CPP_GREATER \
10358 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10359 && !parser->greater_than_is_operator_p) \
10360 ? PREC_NOT_OPERATOR \
10361 : binops_by_token[token->type].prec)
10363 static cp_expr
10364 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10365 bool no_toplevel_fold_p,
10366 bool decltype_p,
10367 enum cp_parser_prec prec,
10368 cp_id_kind * pidk)
10370 cp_parser_expression_stack stack;
10371 cp_parser_expression_stack_entry *sp = &stack[0];
10372 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10373 cp_parser_expression_stack_entry current;
10374 cp_expr rhs;
10375 cp_token *token;
10376 enum tree_code rhs_type;
10377 enum cp_parser_prec new_prec, lookahead_prec;
10378 tree overload;
10380 /* Parse the first expression. */
10381 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10382 ? TRUTH_NOT_EXPR : ERROR_MARK);
10383 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10384 cast_p, decltype_p, pidk);
10385 current.prec = prec;
10387 if (cp_parser_error_occurred (parser))
10388 return error_mark_node;
10390 for (;;)
10392 /* Get an operator token. */
10393 token = cp_lexer_peek_token (parser->lexer);
10395 if (warn_cxx11_compat
10396 && token->type == CPP_RSHIFT
10397 && !parser->greater_than_is_operator_p)
10399 if (warning_at (token->location, OPT_Wc__11_compat,
10400 "%<>>%> operator is treated"
10401 " as two right angle brackets in C++11"))
10402 inform (token->location,
10403 "suggest parentheses around %<>>%> expression");
10406 new_prec = TOKEN_PRECEDENCE (token);
10407 if (new_prec != PREC_NOT_OPERATOR
10408 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10409 /* This is a fold-expression; handle it later. */
10410 new_prec = PREC_NOT_OPERATOR;
10412 /* Popping an entry off the stack means we completed a subexpression:
10413 - either we found a token which is not an operator (`>' where it is not
10414 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10415 will happen repeatedly;
10416 - or, we found an operator which has lower priority. This is the case
10417 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10418 parsing `3 * 4'. */
10419 if (new_prec <= current.prec)
10421 if (sp == stack)
10422 break;
10423 else
10424 goto pop;
10427 get_rhs:
10428 current.tree_type = binops_by_token[token->type].tree_type;
10429 current.loc = token->location;
10430 current.flags = token->flags;
10432 /* We used the operator token. */
10433 cp_lexer_consume_token (parser->lexer);
10435 /* For "false && x" or "true || x", x will never be executed;
10436 disable warnings while evaluating it. */
10437 if ((current.tree_type == TRUTH_ANDIF_EXPR
10438 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10439 || (current.tree_type == TRUTH_ORIF_EXPR
10440 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10442 disable_warnings_sp = sp;
10443 ++c_inhibit_evaluation_warnings;
10446 /* Extract another operand. It may be the RHS of this expression
10447 or the LHS of a new, higher priority expression. */
10448 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10449 ? TRUTH_NOT_EXPR : ERROR_MARK);
10450 rhs = cp_parser_simple_cast_expression (parser);
10452 /* Get another operator token. Look up its precedence to avoid
10453 building a useless (immediately popped) stack entry for common
10454 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10455 token = cp_lexer_peek_token (parser->lexer);
10456 lookahead_prec = TOKEN_PRECEDENCE (token);
10457 if (lookahead_prec != PREC_NOT_OPERATOR
10458 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10459 lookahead_prec = PREC_NOT_OPERATOR;
10460 if (lookahead_prec > new_prec)
10462 /* ... and prepare to parse the RHS of the new, higher priority
10463 expression. Since precedence levels on the stack are
10464 monotonically increasing, we do not have to care about
10465 stack overflows. */
10466 *sp = current;
10467 ++sp;
10468 current.lhs = rhs;
10469 current.lhs_type = rhs_type;
10470 current.prec = new_prec;
10471 new_prec = lookahead_prec;
10472 goto get_rhs;
10474 pop:
10475 lookahead_prec = new_prec;
10476 /* If the stack is not empty, we have parsed into LHS the right side
10477 (`4' in the example above) of an expression we had suspended.
10478 We can use the information on the stack to recover the LHS (`3')
10479 from the stack together with the tree code (`MULT_EXPR'), and
10480 the precedence of the higher level subexpression
10481 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10482 which will be used to actually build the additive expression. */
10483 rhs = current.lhs;
10484 rhs_type = current.lhs_type;
10485 --sp;
10486 current = *sp;
10489 /* Undo the disabling of warnings done above. */
10490 if (sp == disable_warnings_sp)
10492 disable_warnings_sp = NULL;
10493 --c_inhibit_evaluation_warnings;
10496 if (warn_logical_not_paren
10497 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10498 && current.lhs_type == TRUTH_NOT_EXPR
10499 /* Avoid warning for !!x == y. */
10500 && (TREE_CODE (current.lhs) != NE_EXPR
10501 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10502 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10503 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10504 /* Avoid warning for !b == y where b is boolean. */
10505 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10506 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10507 != BOOLEAN_TYPE))))
10508 /* Avoid warning for !!b == y where b is boolean. */
10509 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10510 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10511 && DECL_P (tree_strip_any_location_wrapper
10512 (TREE_OPERAND (current.lhs, 0)))))
10513 || TREE_TYPE (current.lhs) == NULL_TREE
10514 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10515 warn_logical_not_parentheses (current.loc, current.tree_type,
10516 current.lhs, maybe_constant_value (rhs));
10518 if (warn_xor_used_as_pow
10519 && current.tree_type == BIT_XOR_EXPR
10520 /* Don't warn for named "xor" (as opposed to '^'). */
10521 && !(current.flags & NAMED_OP)
10522 && current.lhs.decimal_p ()
10523 && rhs.decimal_p ())
10524 check_for_xor_used_as_pow
10525 (current.lhs.get_location (),
10526 tree_strip_any_location_wrapper (current.lhs),
10527 current.loc,
10528 rhs.get_location (),
10529 tree_strip_any_location_wrapper (rhs));
10531 overload = NULL;
10533 location_t combined_loc = make_location (current.loc,
10534 current.lhs.get_start (),
10535 rhs.get_finish ());
10537 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10538 ERROR_MARK for everything that is not a binary expression.
10539 This makes warn_about_parentheses miss some warnings that
10540 involve unary operators. For unary expressions we should
10541 pass the correct tree_code unless the unary expression was
10542 surrounded by parentheses.
10544 if (no_toplevel_fold_p
10545 && lookahead_prec <= current.prec
10546 && sp == stack)
10548 if (current.lhs == error_mark_node || rhs == error_mark_node)
10549 current.lhs = error_mark_node;
10550 else
10552 current.lhs.maybe_add_location_wrapper ();
10553 rhs.maybe_add_location_wrapper ();
10554 current.lhs
10555 = build_min (current.tree_type,
10556 TREE_CODE_CLASS (current.tree_type)
10557 == tcc_comparison
10558 ? boolean_type_node : TREE_TYPE (current.lhs),
10559 current.lhs.get_value (), rhs.get_value ());
10560 SET_EXPR_LOCATION (current.lhs, combined_loc);
10563 else
10565 op_location_t op_loc (current.loc, combined_loc);
10566 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10567 current.lhs, current.lhs_type,
10568 rhs, rhs_type, NULL_TREE, &overload,
10569 complain_flags (decltype_p));
10570 /* TODO: build_x_binary_op doesn't always honor the location. */
10571 current.lhs.set_location (combined_loc);
10573 current.lhs_type = current.tree_type;
10575 /* If the binary operator required the use of an overloaded operator,
10576 then this expression cannot be an integral constant-expression.
10577 An overloaded operator can be used even if both operands are
10578 otherwise permissible in an integral constant-expression if at
10579 least one of the operands is of enumeration type. */
10581 if (overload
10582 && cp_parser_non_integral_constant_expression (parser,
10583 NIC_OVERLOADED))
10584 return error_mark_node;
10587 return current.lhs;
10590 static cp_expr
10591 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10592 bool no_toplevel_fold_p,
10593 enum cp_parser_prec prec,
10594 cp_id_kind * pidk)
10596 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10597 /*decltype*/false, prec, pidk);
10600 /* Parse the `? expression : assignment-expression' part of a
10601 conditional-expression. The LOGICAL_OR_EXPR is the
10602 logical-or-expression that started the conditional-expression.
10603 Returns a representation of the entire conditional-expression.
10605 This routine is used by cp_parser_assignment_expression
10606 and cp_parser_conditional_expression.
10608 ? expression : assignment-expression
10610 GNU Extensions:
10612 ? : assignment-expression */
10614 static tree
10615 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10617 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10618 cp_expr assignment_expr;
10619 struct cp_token *token;
10620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10622 /* Consume the `?' token. */
10623 cp_lexer_consume_token (parser->lexer);
10624 token = cp_lexer_peek_token (parser->lexer);
10625 if (cp_parser_allow_gnu_extensions_p (parser)
10626 && token->type == CPP_COLON)
10628 pedwarn (token->location, OPT_Wpedantic,
10629 "ISO C++ does not allow %<?:%> with omitted middle operand");
10630 /* Implicit true clause. */
10631 expr = NULL_TREE;
10632 c_inhibit_evaluation_warnings +=
10633 folded_logical_or_expr == truthvalue_true_node;
10634 warn_for_omitted_condop (token->location, logical_or_expr);
10636 else
10638 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10639 parser->colon_corrects_to_scope_p = false;
10640 /* Parse the expression. */
10641 c_inhibit_evaluation_warnings +=
10642 folded_logical_or_expr == truthvalue_false_node;
10643 expr = cp_parser_expression (parser);
10644 c_inhibit_evaluation_warnings +=
10645 ((folded_logical_or_expr == truthvalue_true_node)
10646 - (folded_logical_or_expr == truthvalue_false_node));
10647 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10650 /* The next token should be a `:'. */
10651 cp_parser_require (parser, CPP_COLON, RT_COLON);
10652 /* Parse the assignment-expression. */
10653 assignment_expr = cp_parser_assignment_expression (parser);
10654 c_inhibit_evaluation_warnings -=
10655 folded_logical_or_expr == truthvalue_true_node;
10657 /* Make a location:
10658 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10659 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10660 with the caret at the "?", ranging from the start of
10661 the logical_or_expr to the end of the assignment_expr. */
10662 loc = make_location (loc,
10663 logical_or_expr.get_start (),
10664 assignment_expr.get_finish ());
10666 /* Build the conditional-expression. */
10667 return build_x_conditional_expr (loc, logical_or_expr,
10668 expr,
10669 assignment_expr,
10670 tf_warning_or_error);
10673 /* Parse a conditional-expression.
10675 conditional-expression:
10676 logical-or-expression
10677 logical-or-expression ? expression : assignment-expression
10679 GNU Extensions:
10681 logical-or-expression ? : assignment-expression */
10683 static cp_expr
10684 cp_parser_conditional_expression (cp_parser *parser)
10686 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10687 PREC_NOT_OPERATOR, NULL);
10688 /* If the next token is a `?' then we're actually looking at
10689 a conditional-expression; otherwise we're done. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10691 return cp_parser_question_colon_clause (parser, expr);
10692 return expr;
10695 /* Parse an assignment-expression.
10697 assignment-expression:
10698 conditional-expression
10699 logical-or-expression assignment-operator assignment_expression
10700 throw-expression
10701 yield-expression
10703 CAST_P is true if this expression is the target of a cast.
10704 DECLTYPE_P is true if this expression is the operand of decltype.
10706 Returns a representation for the expression. */
10708 static cp_expr
10709 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10710 bool cast_p, bool decltype_p)
10712 cp_expr expr;
10714 /* If the next token is the `throw' keyword, then we're looking at
10715 a throw-expression. */
10716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10717 expr = cp_parser_throw_expression (parser);
10718 /* If the next token is the `co_yield' keyword, then we're looking at
10719 a yield-expression. */
10720 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10721 expr = cp_parser_yield_expression (parser);
10722 /* Otherwise, it must be that we are looking at a
10723 logical-or-expression. */
10724 else
10726 /* Parse the binary expressions (logical-or-expression). */
10727 expr = cp_parser_binary_expression (parser, cast_p, false,
10728 decltype_p,
10729 PREC_NOT_OPERATOR, pidk);
10730 /* If the next token is a `?' then we're actually looking at a
10731 conditional-expression. */
10732 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10733 return cp_parser_question_colon_clause (parser, expr);
10734 else
10736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10738 /* If it's an assignment-operator, we're using the second
10739 production. */
10740 enum tree_code assignment_operator
10741 = cp_parser_assignment_operator_opt (parser);
10742 if (assignment_operator != ERROR_MARK)
10744 /* Parse the right-hand side of the assignment. */
10745 cp_expr rhs = cp_parser_initializer_clause (parser);
10747 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10750 /* An assignment may not appear in a
10751 constant-expression. */
10752 if (cp_parser_non_integral_constant_expression (parser,
10753 NIC_ASSIGNMENT))
10754 return error_mark_node;
10755 /* Build the assignment expression. Its default
10756 location:
10757 LHS = RHS
10758 ~~~~^~~~~
10759 is the location of the '=' token as the
10760 caret, ranging from the start of the lhs to the
10761 end of the rhs. */
10762 loc = make_location (loc,
10763 expr.get_start (),
10764 rhs.get_finish ());
10765 expr = build_x_modify_expr (loc, expr,
10766 assignment_operator,
10767 rhs, NULL_TREE,
10768 complain_flags (decltype_p));
10769 /* TODO: build_x_modify_expr doesn't honor the location,
10770 so we must set it here. */
10771 expr.set_location (loc);
10776 return expr;
10779 /* Parse an (optional) assignment-operator.
10781 assignment-operator: one of
10782 = *= /= %= += -= >>= <<= &= ^= |=
10784 GNU Extension:
10786 assignment-operator: one of
10787 <?= >?=
10789 If the next token is an assignment operator, the corresponding tree
10790 code is returned, and the token is consumed. For example, for
10791 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10792 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10793 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10794 operator, ERROR_MARK is returned. */
10796 static enum tree_code
10797 cp_parser_assignment_operator_opt (cp_parser* parser)
10799 enum tree_code op;
10800 cp_token *token;
10802 /* Peek at the next token. */
10803 token = cp_lexer_peek_token (parser->lexer);
10805 switch (token->type)
10807 case CPP_EQ:
10808 op = NOP_EXPR;
10809 break;
10811 case CPP_MULT_EQ:
10812 op = MULT_EXPR;
10813 break;
10815 case CPP_DIV_EQ:
10816 op = TRUNC_DIV_EXPR;
10817 break;
10819 case CPP_MOD_EQ:
10820 op = TRUNC_MOD_EXPR;
10821 break;
10823 case CPP_PLUS_EQ:
10824 op = PLUS_EXPR;
10825 break;
10827 case CPP_MINUS_EQ:
10828 op = MINUS_EXPR;
10829 break;
10831 case CPP_RSHIFT_EQ:
10832 op = RSHIFT_EXPR;
10833 break;
10835 case CPP_LSHIFT_EQ:
10836 op = LSHIFT_EXPR;
10837 break;
10839 case CPP_AND_EQ:
10840 op = BIT_AND_EXPR;
10841 break;
10843 case CPP_XOR_EQ:
10844 op = BIT_XOR_EXPR;
10845 break;
10847 case CPP_OR_EQ:
10848 op = BIT_IOR_EXPR;
10849 break;
10851 default:
10852 /* Nothing else is an assignment operator. */
10853 op = ERROR_MARK;
10856 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10857 if (op != ERROR_MARK
10858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10859 op = ERROR_MARK;
10861 /* If it was an assignment operator, consume it. */
10862 if (op != ERROR_MARK)
10863 cp_lexer_consume_token (parser->lexer);
10865 return op;
10868 /* Parse an expression.
10870 expression:
10871 assignment-expression
10872 expression , assignment-expression
10874 CAST_P is true if this expression is the target of a cast.
10875 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10876 except possibly parenthesized or on the RHS of a comma (N3276).
10877 WARN_COMMA_P is true if a comma should be diagnosed.
10879 Returns a representation of the expression. */
10881 static cp_expr
10882 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10883 bool cast_p, bool decltype_p, bool warn_comma_p)
10885 cp_expr expression = NULL_TREE;
10886 location_t loc = UNKNOWN_LOCATION;
10888 while (true)
10890 cp_expr assignment_expression;
10892 /* Parse the next assignment-expression. */
10893 assignment_expression
10894 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10896 /* We don't create a temporary for a call that is the immediate operand
10897 of decltype or on the RHS of a comma. But when we see a comma, we
10898 need to create a temporary for a call on the LHS. */
10899 if (decltype_p && !processing_template_decl
10900 && TREE_CODE (assignment_expression) == CALL_EXPR
10901 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10902 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10903 assignment_expression
10904 = build_cplus_new (TREE_TYPE (assignment_expression),
10905 assignment_expression, tf_warning_or_error);
10907 /* If this is the first assignment-expression, we can just
10908 save it away. */
10909 if (!expression)
10910 expression = assignment_expression;
10911 else
10913 /* Create a location with caret at the comma, ranging
10914 from the start of the LHS to the end of the RHS. */
10915 loc = make_location (loc,
10916 expression.get_start (),
10917 assignment_expression.get_finish ());
10918 expression = build_x_compound_expr (loc, expression,
10919 assignment_expression, NULL_TREE,
10920 complain_flags (decltype_p));
10921 expression.set_location (loc);
10923 /* If the next token is not a comma, or we're in a fold-expression, then
10924 we are done with the expression. */
10925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10926 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10927 break;
10928 /* Consume the `,'. */
10929 loc = cp_lexer_peek_token (parser->lexer)->location;
10930 if (warn_comma_p)
10932 /* [depr.comma.subscript]: A comma expression appearing as
10933 the expr-or-braced-init-list of a subscripting expression
10934 is deprecated. A parenthesized comma expression is not
10935 deprecated. */
10936 warning_at (loc, OPT_Wcomma_subscript,
10937 "top-level comma expression in array subscript "
10938 "is deprecated");
10939 warn_comma_p = false;
10941 cp_lexer_consume_token (parser->lexer);
10942 /* A comma operator cannot appear in a constant-expression. */
10943 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10944 expression = error_mark_node;
10947 return expression;
10950 /* Parse a constant-expression.
10952 constant-expression:
10953 conditional-expression
10955 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10956 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10957 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10958 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10959 greater than 1, this isn't really a constant-expression, only a
10960 potentially constant-evaluated expression. If STRICT_P is true,
10961 only parse a conditional-expression, otherwise parse an
10962 assignment-expression. See below for rationale. */
10964 static cp_expr
10965 cp_parser_constant_expression (cp_parser* parser,
10966 int allow_non_constant_p /* = 0 */,
10967 bool *non_constant_p /* = NULL */,
10968 bool strict_p /* = false */)
10970 /* It might seem that we could simply parse the
10971 conditional-expression, and then check to see if it were
10972 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10973 one that the compiler can figure out is constant, possibly after
10974 doing some simplifications or optimizations. The standard has a
10975 precise definition of constant-expression, and we must honor
10976 that, even though it is somewhat more restrictive.
10978 For example:
10980 int i[(2, 3)];
10982 is not a legal declaration, because `(2, 3)' is not a
10983 constant-expression. The `,' operator is forbidden in a
10984 constant-expression. However, GCC's constant-folding machinery
10985 will fold this operation to an INTEGER_CST for `3'. */
10987 /* Save the old settings. */
10988 bool saved_integral_constant_expression_p
10989 = parser->integral_constant_expression_p;
10990 bool saved_allow_non_integral_constant_expression_p
10991 = parser->allow_non_integral_constant_expression_p;
10992 bool saved_non_integral_constant_expression_p
10993 = parser->non_integral_constant_expression_p;
10994 /* We are now parsing a constant-expression. */
10995 parser->integral_constant_expression_p = true;
10996 parser->allow_non_integral_constant_expression_p
10997 = (allow_non_constant_p || cxx_dialect >= cxx11);
10998 parser->non_integral_constant_expression_p = false;
11000 /* A manifestly constant-evaluated expression is evaluated even in an
11001 unevaluated operand. */
11002 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
11004 /* Although the grammar says "conditional-expression", when not STRICT_P,
11005 we parse an "assignment-expression", which also permits
11006 "throw-expression" and the use of assignment operators. In the case
11007 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
11008 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
11009 actually essential that we look for an assignment-expression.
11010 For example, cp_parser_initializer_clauses uses this function to
11011 determine whether a particular assignment-expression is in fact
11012 constant. */
11013 cp_expr expression;
11014 if (strict_p)
11015 expression = cp_parser_conditional_expression (parser);
11016 else
11017 expression = cp_parser_assignment_expression (parser);
11018 /* Restore the old settings. */
11019 parser->integral_constant_expression_p
11020 = saved_integral_constant_expression_p;
11021 parser->allow_non_integral_constant_expression_p
11022 = saved_allow_non_integral_constant_expression_p;
11023 if (cxx_dialect >= cxx11
11024 && (!allow_non_constant_p || non_constant_p))
11026 /* Require an rvalue constant expression here; that's what our
11027 callers expect. Reference constant expressions are handled
11028 separately in e.g. cp_parser_template_argument. */
11029 tree decay = expression;
11030 if (TREE_TYPE (expression)
11031 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
11032 decay = build_address (expression);
11033 bool is_const = is_rvalue_constant_expression (decay);
11034 parser->non_integral_constant_expression_p = !is_const;
11035 if (!is_const && !allow_non_constant_p)
11036 require_rvalue_constant_expression (decay);
11038 if (allow_non_constant_p && non_constant_p)
11039 *non_constant_p = parser->non_integral_constant_expression_p;
11040 parser->non_integral_constant_expression_p
11041 = saved_non_integral_constant_expression_p;
11043 return expression;
11046 /* Parse __builtin_offsetof.
11048 offsetof-expression:
11049 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
11051 offsetof-member-designator:
11052 id-expression
11053 | offsetof-member-designator "." id-expression
11054 | offsetof-member-designator "[" expression "]"
11055 | offsetof-member-designator "->" id-expression */
11057 static cp_expr
11058 cp_parser_builtin_offsetof (cp_parser *parser)
11060 int save_ice_p, save_non_ice_p;
11061 tree type;
11062 cp_expr expr;
11063 cp_id_kind dummy;
11064 cp_token *token;
11065 location_t finish_loc;
11067 /* We're about to accept non-integral-constant things, but will
11068 definitely yield an integral constant expression. Save and
11069 restore these values around our local parsing. */
11070 save_ice_p = parser->integral_constant_expression_p;
11071 save_non_ice_p = parser->non_integral_constant_expression_p;
11073 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11075 /* Consume the "__builtin_offsetof" token. */
11076 cp_lexer_consume_token (parser->lexer);
11077 /* Consume the opening `('. */
11078 matching_parens parens;
11079 parens.require_open (parser);
11080 /* Parse the type-id. */
11081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11083 const char *saved_message = parser->type_definition_forbidden_message;
11084 parser->type_definition_forbidden_message
11085 = G_("types may not be defined within %<__builtin_offsetof%>");
11086 type = cp_parser_type_id (parser);
11087 parser->type_definition_forbidden_message = saved_message;
11089 /* Look for the `,'. */
11090 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11091 token = cp_lexer_peek_token (parser->lexer);
11093 /* Build the (type *)null that begins the traditional offsetof macro. */
11094 tree object_ptr
11095 = build_static_cast (input_location, build_pointer_type (type),
11096 null_pointer_node, tf_warning_or_error);
11098 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
11099 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
11100 true, &dummy, token->location);
11101 while (true)
11103 token = cp_lexer_peek_token (parser->lexer);
11104 switch (token->type)
11106 case CPP_OPEN_SQUARE:
11107 /* offsetof-member-designator "[" expression "]" */
11108 expr = cp_parser_postfix_open_square_expression (parser, expr,
11109 true, false);
11110 break;
11112 case CPP_DEREF:
11113 /* offsetof-member-designator "->" identifier */
11114 expr = grok_array_decl (token->location, expr, integer_zero_node,
11115 NULL, tf_warning_or_error);
11116 /* FALLTHRU */
11118 case CPP_DOT:
11119 /* offsetof-member-designator "." identifier */
11120 cp_lexer_consume_token (parser->lexer);
11121 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
11122 expr, true, &dummy,
11123 token->location);
11124 break;
11126 case CPP_CLOSE_PAREN:
11127 /* Consume the ")" token. */
11128 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11129 cp_lexer_consume_token (parser->lexer);
11130 goto success;
11132 default:
11133 /* Error. We know the following require will fail, but
11134 that gives the proper error message. */
11135 parens.require_close (parser);
11136 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11137 expr = error_mark_node;
11138 goto failure;
11142 success:
11143 /* Make a location of the form:
11144 __builtin_offsetof (struct s, f)
11145 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11146 with caret at the type-id, ranging from the start of the
11147 "_builtin_offsetof" token to the close paren. */
11148 loc = make_location (loc, start_loc, finish_loc);
11149 /* The result will be an INTEGER_CST, so we need to explicitly
11150 preserve the location. */
11151 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11153 failure:
11154 parser->integral_constant_expression_p = save_ice_p;
11155 parser->non_integral_constant_expression_p = save_non_ice_p;
11157 expr = expr.maybe_add_location_wrapper ();
11158 return expr;
11161 /* Parse a builtin trait expression or type. */
11163 static cp_expr
11164 cp_parser_trait (cp_parser* parser, const cp_trait* trait)
11166 const cp_trait_kind kind = trait->kind;
11167 tree type1, type2 = NULL_TREE;
11168 const bool binary = (trait->arity == 2);
11169 const bool variadic = (trait->arity == -1);
11170 const bool type = trait->type;
11172 /* Get location of initial token. */
11173 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11175 /* Consume the token. */
11176 cp_lexer_consume_token (parser->lexer);
11178 matching_parens parens;
11179 if (kind == CPTK_TYPE_PACK_ELEMENT)
11180 cp_parser_require (parser, CPP_LESS, RT_LESS);
11181 else
11182 parens.require_open (parser);
11184 if (kind == CPTK_IS_DEDUCIBLE)
11186 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11187 type1 = cp_parser_id_expression (parser,
11188 /*template_keyword_p=*/false,
11189 /*check_dependency_p=*/true,
11190 nullptr,
11191 /*declarator_p=*/false,
11192 /*optional_p=*/false);
11193 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11195 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11196 /* __type_pack_element takes an expression as its first argument and uses
11197 template-id syntax instead of function call syntax (for consistency
11198 with Clang). We special case these properties of __type_pack_element
11199 here and elsewhere. */
11200 type1 = cp_parser_constant_expression (parser);
11201 else
11203 type_id_in_expr_sentinel s (parser);
11204 type1 = cp_parser_type_id (parser);
11207 if (type1 == error_mark_node)
11208 return error_mark_node;
11210 if (kind == CPTK_TYPE_PACK_ELEMENT)
11212 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11213 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11214 for (tree elt : tree_vec_range (trailing))
11216 if (!TYPE_P (elt))
11218 error_at (cp_expr_loc_or_input_loc (elt),
11219 "trailing argument to %<__type_pack_element%> "
11220 "is not a type");
11221 return error_mark_node;
11224 type2 = trailing;
11226 else if (binary)
11228 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11231 type_id_in_expr_sentinel s (parser);
11232 type2 = cp_parser_type_id (parser);
11235 if (type2 == error_mark_node)
11236 return error_mark_node;
11238 else if (variadic)
11240 auto_vec<tree, 4> trailing;
11241 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11243 cp_lexer_consume_token (parser->lexer);
11244 tree elt = cp_parser_type_id (parser);
11245 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11247 cp_lexer_consume_token (parser->lexer);
11248 elt = make_pack_expansion (elt);
11250 if (elt == error_mark_node)
11251 return error_mark_node;
11252 trailing.safe_push (elt);
11254 type2 = make_tree_vec (trailing.length ());
11255 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11256 TREE_VEC_ELT (type2, i) = trailing[i];
11259 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11260 if (kind == CPTK_TYPE_PACK_ELEMENT)
11261 /* cp_parser_enclosed_template_argument_list above already took care
11262 of parsing the closing '>'. */;
11263 else
11264 parens.require_close (parser);
11266 /* Construct a location of the form:
11267 __is_trivially_copyable(_Tp)
11268 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11269 with start == caret, finishing at the close-paren. */
11270 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11272 /* Complete the trait expression, which may mean either processing
11273 the trait expr now or saving it for template instantiation. */
11274 switch (kind)
11276 case CPTK_BASES:
11277 return cp_expr (finish_bases (type1, false), trait_loc);
11278 case CPTK_DIRECT_BASES:
11279 return cp_expr (finish_bases (type1, true), trait_loc);
11280 default:
11281 if (type)
11282 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11283 else
11284 return finish_trait_expr (trait_loc, kind, type1, type2);
11288 /* Parse a lambda expression.
11290 lambda-expression:
11291 lambda-introducer lambda-declarator [opt] compound-statement
11292 lambda-introducer < template-parameter-list > requires-clause [opt]
11293 lambda-declarator [opt] compound-statement
11295 Returns a representation of the expression. */
11297 static cp_expr
11298 cp_parser_lambda_expression (cp_parser* parser)
11300 tree lambda_expr = build_lambda_expr ();
11301 tree type;
11302 bool ok = true;
11303 cp_token *token = cp_lexer_peek_token (parser->lexer);
11304 cp_token_position start = 0;
11306 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11308 if (cxx_dialect >= cxx20)
11310 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11311 non-type parameter is nonsensical.
11313 Distinguish a lambda in the parameter type from a lambda in the
11314 default argument by looking at local_variables_forbidden_p, which is
11315 only set in default arguments. */
11316 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11318 error_at (token->location,
11319 "lambda-expression in template parameter type");
11320 token->error_reported = true;
11321 ok = false;
11324 else if (cp_unevaluated_operand)
11326 if (!token->error_reported)
11328 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11329 "lambda-expression in unevaluated context"
11330 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11331 token->error_reported = true;
11333 ok = false;
11335 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11337 if (!token->error_reported)
11339 error_at (token->location, "lambda-expression in template-argument"
11340 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11341 token->error_reported = true;
11343 ok = false;
11346 /* We may be in the middle of deferred access check. Disable
11347 it now. */
11348 push_deferring_access_checks (dk_no_deferred);
11350 cp_parser_lambda_introducer (parser, lambda_expr);
11351 if (cp_parser_error_occurred (parser))
11352 return error_mark_node;
11354 type = begin_lambda_type (lambda_expr);
11355 if (type == error_mark_node)
11356 return error_mark_node;
11358 record_lambda_scope (lambda_expr);
11359 record_lambda_scope_discriminator (lambda_expr);
11361 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11362 determine_visibility (TYPE_NAME (type));
11364 /* Now that we've started the type, add the capture fields for any
11365 explicit captures. */
11366 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11369 /* Inside the class, surrounding template-parameter-lists do not apply. */
11370 unsigned int saved_num_template_parameter_lists
11371 = parser->num_template_parameter_lists;
11372 unsigned char in_statement = parser->in_statement;
11373 bool in_switch_statement_p = parser->in_switch_statement_p;
11374 bool fully_implicit_function_template_p
11375 = parser->fully_implicit_function_template_p;
11376 tree implicit_template_parms = parser->implicit_template_parms;
11377 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11378 bool auto_is_implicit_function_template_parm_p
11379 = parser->auto_is_implicit_function_template_parm_p;
11380 bool saved_omp_array_section_p = parser->omp_array_section_p;
11382 parser->num_template_parameter_lists = 0;
11383 parser->in_statement = 0;
11384 parser->in_switch_statement_p = false;
11385 parser->fully_implicit_function_template_p = false;
11386 parser->implicit_template_parms = 0;
11387 parser->implicit_template_scope = 0;
11388 parser->auto_is_implicit_function_template_parm_p = false;
11389 parser->omp_array_section_p = false;
11391 /* The body of a lambda in a discarded statement is not discarded. */
11392 bool discarded = in_discarded_stmt;
11393 in_discarded_stmt = 0;
11395 /* Similarly the body of a lambda in immediate function context is not
11396 in immediate function context. */
11397 bool save_in_consteval_if_p = in_consteval_if_p;
11398 in_consteval_if_p = false;
11400 /* By virtue of defining a local class, a lambda expression has access to
11401 the private variables of enclosing classes. */
11403 if (cp_parser_start_tentative_firewall (parser))
11404 start = token;
11406 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11408 if (ok && cp_parser_error_occurred (parser))
11409 ok = false;
11411 if (ok)
11412 cp_parser_lambda_body (parser, lambda_expr);
11413 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11415 if (cp_parser_skip_to_closing_brace (parser))
11416 cp_lexer_consume_token (parser->lexer);
11419 /* The capture list was built up in reverse order; fix that now. */
11420 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11421 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11423 if (ok)
11424 maybe_add_lambda_conv_op (type);
11426 finish_struct (type, /*attributes=*/NULL_TREE);
11428 in_consteval_if_p = save_in_consteval_if_p;
11429 in_discarded_stmt = discarded;
11431 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11432 parser->in_statement = in_statement;
11433 parser->in_switch_statement_p = in_switch_statement_p;
11434 parser->fully_implicit_function_template_p
11435 = fully_implicit_function_template_p;
11436 parser->implicit_template_parms = implicit_template_parms;
11437 parser->implicit_template_scope = implicit_template_scope;
11438 parser->auto_is_implicit_function_template_parm_p
11439 = auto_is_implicit_function_template_parm_p;
11440 parser->omp_array_section_p = saved_omp_array_section_p;
11443 /* This field is only used during parsing of the lambda. */
11444 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11446 /* This lambda shouldn't have any proxies left at this point. */
11447 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11448 /* And now that we're done, push proxies for an enclosing lambda. */
11449 insert_pending_capture_proxies ();
11451 /* Update the lambda expression to a range. */
11452 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11453 token->location,
11454 parser->lexer);
11456 if (ok)
11457 lambda_expr = build_lambda_object (lambda_expr);
11458 else
11459 lambda_expr = error_mark_node;
11461 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11463 pop_deferring_access_checks ();
11465 return lambda_expr;
11468 /* Parse the beginning of a lambda expression.
11470 lambda-introducer:
11471 [ lambda-capture [opt] ]
11473 LAMBDA_EXPR is the current representation of the lambda expression. */
11475 static void
11476 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11478 /* Need commas after the first capture. */
11479 bool first = true;
11481 /* Eat the leading `['. */
11482 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11484 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11485 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11486 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11487 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11488 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11489 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11490 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11491 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11493 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11495 cp_lexer_consume_token (parser->lexer);
11496 first = false;
11498 if (!(at_function_scope_p () || parsing_nsdmi ()))
11499 error ("non-local lambda expression cannot have a capture-default");
11502 hash_set<tree, true> ids;
11503 tree first_capture_id = NULL_TREE;
11504 unsigned name_independent_cnt = 0;
11505 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11507 cp_token* capture_token;
11508 tree capture_id;
11509 tree capture_init_expr;
11510 cp_id_kind idk = CP_ID_KIND_NONE;
11511 bool explicit_init_p = false;
11513 enum capture_kind_type
11515 BY_COPY,
11516 BY_REFERENCE
11518 enum capture_kind_type capture_kind = BY_COPY;
11520 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11522 error ("expected end of capture-list");
11523 return;
11526 if (first)
11527 first = false;
11528 else
11529 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11531 /* Possibly capture `this'. */
11532 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11534 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11535 if (cxx_dialect < cxx20 && pedantic
11536 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11537 pedwarn (loc, OPT_Wc__20_extensions,
11538 "explicit by-copy capture of %<this%> "
11539 "with by-copy capture default only available with "
11540 "%<-std=c++20%> or %<-std=gnu++20%>");
11541 cp_lexer_consume_token (parser->lexer);
11542 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11543 pedwarn (input_location, 0,
11544 "already captured %qD in lambda expression",
11545 this_identifier);
11546 else
11547 add_capture (lambda_expr, /*id=*/this_identifier,
11548 /*initializer=*/finish_this_expr (),
11549 /*by_reference_p=*/true, explicit_init_p, NULL);
11550 continue;
11553 /* Possibly capture `*this'. */
11554 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11555 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11557 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11558 if (cxx_dialect < cxx17)
11559 pedwarn (loc, OPT_Wc__17_extensions,
11560 "%<*this%> capture only available with "
11561 "%<-std=c++17%> or %<-std=gnu++17%>");
11562 cp_lexer_consume_token (parser->lexer);
11563 cp_lexer_consume_token (parser->lexer);
11564 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11565 pedwarn (input_location, 0,
11566 "already captured %qD in lambda expression",
11567 this_identifier);
11568 else
11569 add_capture (lambda_expr, /*id=*/this_identifier,
11570 /*initializer=*/finish_this_expr (),
11571 /*by_reference_p=*/false, explicit_init_p, NULL);
11572 continue;
11575 /* But reject `&this'. */
11576 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11577 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11579 error_at (cp_lexer_peek_token (parser->lexer)->location,
11580 "%<this%> cannot be captured by reference");
11581 cp_lexer_consume_token (parser->lexer);
11582 cp_lexer_consume_token (parser->lexer);
11583 continue;
11586 /* Remember whether we want to capture as a reference or not. */
11587 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11589 capture_kind = BY_REFERENCE;
11590 cp_lexer_consume_token (parser->lexer);
11593 bool init_pack_expansion = false;
11594 location_t ellipsis_loc = UNKNOWN_LOCATION;
11595 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11597 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11598 if (cxx_dialect < cxx20)
11599 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11600 "pack init-capture only available with "
11601 "%<-std=c++20%> or %<-std=gnu++20%>");
11602 cp_lexer_consume_token (parser->lexer);
11603 init_pack_expansion = true;
11606 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11607 if (init_pack_expansion && capture_kind != BY_REFERENCE
11608 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11610 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11611 0, "%<&%> should come before %<...%>");
11612 capture_kind = BY_REFERENCE;
11613 cp_lexer_consume_token (parser->lexer);
11616 /* Get the identifier. */
11617 capture_token = cp_lexer_peek_token (parser->lexer);
11618 capture_id = cp_parser_identifier (parser);
11620 if (capture_id == error_mark_node)
11621 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11622 delimiters, but I modified this to stop on unnested ']' as well. It
11623 was already changed to stop on unnested '}', so the
11624 "closing_parenthesis" name is no more misleading with my change. */
11626 cp_parser_skip_to_closing_parenthesis (parser,
11627 /*recovering=*/true,
11628 /*or_comma=*/true,
11629 /*consume_paren=*/true);
11630 break;
11633 /* Find the initializer for this capture. */
11634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11635 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11636 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11638 /* An explicit initializer exists. */
11639 if (cxx_dialect < cxx14)
11640 pedwarn (input_location, OPT_Wc__14_extensions,
11641 "lambda capture initializers "
11642 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11643 capture_init_expr = cp_parser_initializer (parser,
11644 /*direct_init=*/nullptr,
11645 /*non_constant=*/nullptr,
11646 /*subexpression_p=*/true);
11647 explicit_init_p = true;
11648 if (capture_init_expr == NULL_TREE)
11650 error ("empty initializer for lambda init-capture");
11651 capture_init_expr = error_mark_node;
11653 if (init_pack_expansion)
11654 capture_init_expr = make_pack_expansion (capture_init_expr);
11656 else
11658 const char* error_msg;
11660 /* Turn the identifier into an id-expression. */
11661 capture_init_expr
11662 = cp_parser_lookup_name_simple (parser, capture_id,
11663 capture_token->location);
11665 if (capture_init_expr == error_mark_node)
11667 unqualified_name_lookup_error (capture_id);
11668 continue;
11670 else if (!VAR_P (capture_init_expr)
11671 && TREE_CODE (capture_init_expr) != PARM_DECL)
11673 error_at (capture_token->location,
11674 "capture of non-variable %qE",
11675 capture_init_expr);
11676 if (DECL_P (capture_init_expr))
11677 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11678 "%q#D declared here", capture_init_expr);
11679 continue;
11681 if (VAR_P (capture_init_expr)
11682 && decl_storage_duration (capture_init_expr) != dk_auto)
11684 if (pedwarn (capture_token->location, 0, "capture of variable "
11685 "%qD with non-automatic storage duration",
11686 capture_init_expr))
11687 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11688 "%q#D declared here", capture_init_expr);
11689 continue;
11692 capture_init_expr
11693 = finish_id_expression
11694 (capture_id,
11695 capture_init_expr,
11696 parser->scope,
11697 &idk,
11698 /*integral_constant_expression_p=*/false,
11699 /*allow_non_integral_constant_expression_p=*/false,
11700 /*non_integral_constant_expression_p=*/NULL,
11701 /*template_p=*/false,
11702 /*done=*/true,
11703 /*address_p=*/false,
11704 /*template_arg_p=*/false,
11705 &error_msg,
11706 capture_token->location);
11708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11711 cp_lexer_consume_token (parser->lexer);
11712 capture_init_expr = make_pack_expansion (capture_init_expr);
11713 if (init_pack_expansion)
11715 /* If what follows is an initializer, the second '...' is
11716 invalid. But for cases like [...xs...], the first one
11717 is invalid. */
11718 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11720 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11721 ellipsis_loc = loc;
11722 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11723 continue;
11728 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11729 && !explicit_init_p)
11731 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11732 && capture_kind == BY_COPY)
11733 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11734 "of %qD redundant with by-copy capture default",
11735 capture_id);
11736 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11737 && capture_kind == BY_REFERENCE)
11738 pedwarn (capture_token->location, 0, "explicit by-reference "
11739 "capture of %qD redundant with by-reference capture "
11740 "default", capture_id);
11743 /* Check for duplicates.
11744 Optimize for the zero or one explicit captures cases and only create
11745 the hash_set after adding second capture. */
11746 bool found = false;
11747 if (!ids.is_empty ())
11748 found = ids.add (capture_id);
11749 else if (first_capture_id == NULL_TREE)
11750 first_capture_id = capture_id;
11751 else if (capture_id == first_capture_id)
11752 found = true;
11753 else
11755 ids.add (first_capture_id);
11756 ids.add (capture_id);
11758 if (found && explicit_init_p && id_equal (capture_id, "_"))
11759 found = false;
11760 if (found)
11761 pedwarn (input_location, 0,
11762 "already captured %qD in lambda expression", capture_id);
11763 else
11764 add_capture (lambda_expr, capture_id, capture_init_expr,
11765 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11766 explicit_init_p, &name_independent_cnt);
11768 /* If there is any qualification still in effect, clear it
11769 now; we will be starting fresh with the next capture. */
11770 parser->scope = NULL_TREE;
11771 parser->qualifying_scope = NULL_TREE;
11772 parser->object_scope = NULL_TREE;
11775 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11778 /* Parse the (optional) middle of a lambda expression.
11780 lambda-declarator:
11781 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11782 lambda-specifiers (C++23)
11784 lambda-specifiers:
11785 decl-specifier-seq [opt] noexcept-specifier [opt]
11786 attribute-specifier-seq [opt] trailing-return-type [opt]
11788 LAMBDA_EXPR is the current representation of the lambda expression. */
11790 static bool
11791 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11793 /* 5.1.1.4 of the standard says:
11794 If a lambda-expression does not include a lambda-declarator, it is as if
11795 the lambda-declarator were ().
11796 This means an empty parameter list, no attributes, and no exception
11797 specification. */
11798 tree param_list = void_list_node;
11799 tree std_attrs = NULL_TREE;
11800 tree gnu_attrs = NULL_TREE;
11801 tree exception_spec = NULL_TREE;
11802 tree template_param_list = NULL_TREE;
11803 tree tx_qual = NULL_TREE;
11804 tree return_type = NULL_TREE;
11805 tree trailing_requires_clause = NULL_TREE;
11806 bool has_param_list = false;
11807 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11808 cp_decl_specifier_seq lambda_specs;
11809 clear_decl_specs (&lambda_specs);
11810 /* A lambda op() is const unless explicitly 'mutable'. */
11811 cp_cv_quals quals = TYPE_QUAL_CONST;
11813 /* The template-parameter-list is optional, but must begin with
11814 an opening angle if present. */
11815 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11817 if (cxx_dialect < cxx20
11818 && (pedantic || cxx_dialect < cxx14))
11819 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11820 "lambda templates are only available with "
11821 "%<-std=c++20%> or %<-std=gnu++20%>");
11823 cp_lexer_consume_token (parser->lexer);
11825 template_param_list = cp_parser_template_parameter_list (parser);
11826 cp_parser_require_end_of_template_parameter_list (parser);
11828 /* We may have a constrained generic lambda; parse the requires-clause
11829 immediately after the template-parameter-list and combine with any
11830 shorthand constraints present. */
11831 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11832 if (flag_concepts)
11834 tree reqs = get_shorthand_constraints (current_template_parms);
11835 if (dreqs)
11836 reqs = combine_constraint_expressions (reqs, dreqs);
11837 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11840 /* We just processed one more parameter list. */
11841 ++parser->num_template_parameter_lists;
11844 /* Committee discussion supports allowing attributes here. */
11845 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11847 /* The parameter-declaration-clause is optional (unless
11848 template-parameter-list was given), but must begin with an
11849 opening parenthesis if present. */
11850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11852 matching_parens parens;
11853 parens.consume_open (parser);
11855 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11857 /* Parse parameters. */
11858 param_list
11859 = cp_parser_parameter_declaration_clause
11860 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11862 /* Default arguments shall not be specified in the
11863 parameter-declaration-clause of a lambda-declarator. */
11864 if (pedantic && cxx_dialect < cxx14)
11865 for (tree t = param_list; t; t = TREE_CHAIN (t))
11866 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11867 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11868 OPT_Wc__14_extensions,
11869 "default argument specified for lambda parameter");
11871 parens.require_close (parser);
11872 has_param_list = true;
11874 else if (cxx_dialect < cxx23)
11875 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11877 /* [expr.prim.lambda.general]
11878 lambda-specifier:
11879 consteval, constexpr, mutable, static
11880 [4] A lambda-specifier-seq shall contain at most one of each
11881 lambda-specifier and shall not contain both constexpr and consteval.
11882 The lambda-specifier-seq shall not contain both mutable and static. */
11883 int declares_class_or_enum;
11884 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11885 cp_parser_decl_specifier_seq (parser,
11886 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11887 &lambda_specs, &declares_class_or_enum);
11889 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11891 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11892 "parameter declaration before lambda declaration "
11893 "specifiers only optional with %<-std=c++2b%> or "
11894 "%<-std=gnu++2b%>");
11895 omitted_parms_loc = UNKNOWN_LOCATION;
11897 /* Peek at the params, see if we have an xobj parameter. */
11898 if (param_list && TREE_PURPOSE (param_list) == this_identifier)
11900 quals = TYPE_UNQUALIFIED;
11901 /* We still need grokdeclarator to see that this is an xobj function
11902 and finish the rest of the work, don't mutate it. */
11903 tree const xobj_param = TREE_VALUE (param_list);
11904 tree const param_type = TREE_TYPE (xobj_param);
11905 /* [expr.prim.lambda.closure-5]
11906 Given a lambda with a lambda-capture, the type of the explicit object
11907 parameter, if any, of the lambda's function call operator (possibly
11908 instantiated from a function call operator template) shall be either:
11909 -- the closure type,
11910 -- a class type derived from the closure type, or
11911 -- a reference to a possibly cv-qualified such type. */
11912 bool const unrelated_with_captures
11913 = (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11914 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11915 /* Since a lambda's type is anonymous, we can assume an xobj
11916 parameter is unrelated to the closure if it is non-dependent.
11917 If it is dependent we handle it at instantiation time. */
11918 && !WILDCARD_TYPE_P (non_reference (param_type));
11919 if (unrelated_with_captures)
11921 error_at (DECL_SOURCE_LOCATION (xobj_param),
11922 "a lambda with captures may not have an explicit object "
11923 "parameter of an unrelated type");
11924 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = NULL_TREE;
11927 /* [expr.prim.lambda.general-4]
11928 If the lambda-declarator contains an explicit object parameter
11929 ([dcl.fct]), then no lambda-specifier in the lambda-specifier-seq
11930 shall be mutable or static. */
11931 if (lambda_specs.storage_class == sc_mutable)
11933 auto_diagnostic_group d;
11934 error_at (lambda_specs.locations[ds_storage_class],
11935 "%<mutable%> lambda specifier "
11936 "with explicit object parameter");
11937 /* Tell the user how to do what they probably meant, maybe fixits
11938 would be appropriate later? */
11939 if (unrelated_with_captures)
11940 /* The following hints don't make sense when we already have an
11941 unrelated type with captures, don't emit them. */;
11942 else if (!TYPE_REF_P (param_type))
11943 inform (DECL_SOURCE_LOCATION (xobj_param),
11944 "the passed in closure object will not be mutated because "
11945 "it is taken by value");
11946 else if (TYPE_READONLY (TREE_TYPE (param_type)))
11947 inform (DECL_SOURCE_LOCATION (xobj_param),
11948 "declare the explicit object parameter as non-const "
11949 "reference instead");
11950 else
11951 inform (DECL_SOURCE_LOCATION (xobj_param),
11952 "explicit object parameter is already a mutable "
11953 "reference");
11955 else if (lambda_specs.storage_class == sc_static)
11957 auto_diagnostic_group d;
11958 error_at (lambda_specs.locations[ds_storage_class],
11959 "%<static%> lambda specifier "
11960 "with explicit object parameter");
11961 inform (DECL_SOURCE_LOCATION (xobj_param),
11962 "explicit object parameter declared here");
11965 else if (lambda_specs.storage_class == sc_mutable)
11967 quals = TYPE_UNQUALIFIED;
11969 else if (lambda_specs.storage_class == sc_static)
11971 /* [expr.prim.lambda.general-4]
11972 If the lambda-specifier-seq contains static, there shall be no
11973 lambda-capture. */
11974 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11975 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11976 error_at (lambda_specs.locations[ds_storage_class],
11977 "%<static%> lambda specifier with lambda capture");
11978 else
11980 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11981 quals = TYPE_UNQUALIFIED;
11985 tx_qual = cp_parser_tx_qualifier_opt (parser);
11986 if (omitted_parms_loc && tx_qual)
11988 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11989 "parameter declaration before lambda transaction "
11990 "qualifier only optional with %<-std=c++2b%> or "
11991 "%<-std=gnu++2b%>");
11992 omitted_parms_loc = UNKNOWN_LOCATION;
11995 /* Parse optional exception specification. */
11996 exception_spec
11997 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11999 if (omitted_parms_loc && exception_spec)
12001 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12002 "parameter declaration before lambda exception "
12003 "specification only optional with %<-std=c++2b%> or "
12004 "%<-std=gnu++2b%>");
12005 omitted_parms_loc = UNKNOWN_LOCATION;
12008 /* GCC 8 accepted attributes here, and this is the place for standard C++11
12009 attributes that appertain to the function type. */
12010 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12011 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
12012 else
12013 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12015 /* Parse optional trailing return type. */
12016 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
12018 if (omitted_parms_loc)
12019 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12020 "parameter declaration before lambda trailing "
12021 "return type only optional with %<-std=c++2b%> or "
12022 "%<-std=gnu++2b%>");
12023 cp_lexer_consume_token (parser->lexer);
12024 return_type = cp_parser_trailing_type_id (parser);
12027 /* Also allow GNU attributes at the very end of the declaration, the usual
12028 place for GNU attributes. */
12029 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12030 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
12032 if (has_param_list)
12034 /* Parse optional trailing requires clause. */
12035 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
12037 /* The function parameters must be in scope all the way until after the
12038 trailing-return-type in case of decltype. */
12039 pop_bindings_and_leave_scope ();
12042 /* Create the function call operator.
12044 Messing with declarators like this is no uglier than building up the
12045 FUNCTION_DECL by hand, and this is less likely to get out of sync with
12046 other code. */
12048 cp_decl_specifier_seq return_type_specs;
12049 cp_declarator* declarator;
12050 tree fco;
12051 void *p;
12053 clear_decl_specs (&return_type_specs);
12054 return_type_specs.type = make_auto ();
12056 if (lambda_specs.locations[ds_constexpr])
12058 if (cxx_dialect >= cxx17)
12059 return_type_specs.locations[ds_constexpr]
12060 = lambda_specs.locations[ds_constexpr];
12061 else
12062 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
12063 "lambda only available with %<-std=c++17%> or "
12064 "%<-std=gnu++17%>");
12066 if (lambda_specs.locations[ds_consteval])
12067 return_type_specs.locations[ds_consteval]
12068 = lambda_specs.locations[ds_consteval];
12069 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
12071 return_type_specs.storage_class = sc_static;
12072 return_type_specs.locations[ds_storage_class]
12073 = lambda_specs.locations[ds_storage_class];
12076 p = obstack_alloc (&declarator_obstack, 0);
12078 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
12079 LAMBDA_EXPR_LOCATION (lambda_expr));
12081 declarator = make_call_declarator (declarator, param_list, quals,
12082 VIRT_SPEC_UNSPECIFIED,
12083 REF_QUAL_NONE,
12084 tx_qual,
12085 exception_spec,
12086 return_type,
12087 trailing_requires_clause,
12088 std_attrs,
12089 UNKNOWN_LOCATION);
12091 fco = grokmethod (&return_type_specs,
12092 declarator,
12093 chainon (gnu_attrs, lambda_specs.attributes));
12094 if (fco != error_mark_node)
12096 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
12097 DECL_ARTIFICIAL (fco) = 1;
12098 if (DECL_IOBJ_MEMBER_FUNCTION_P (fco))
12099 /* Give the object parameter a different name. */
12100 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
12101 DECL_SET_LAMBDA_FUNCTION (fco, true);
12103 if (template_param_list)
12105 fco = finish_member_template_decl (fco);
12106 finish_template_decl (template_param_list);
12107 --parser->num_template_parameter_lists;
12109 else if (parser->fully_implicit_function_template_p)
12110 fco = finish_fully_implicit_template (parser, fco);
12112 finish_member_declaration (fco);
12113 record_lambda_scope_sig_discriminator (lambda_expr, fco);
12115 obstack_free (&declarator_obstack, p);
12117 return (fco != error_mark_node);
12121 /* Parse the body of a lambda expression, which is simply
12123 compound-statement
12125 but which requires special handling.
12126 LAMBDA_EXPR is the current representation of the lambda expression. */
12128 static void
12129 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
12131 bool nested = (current_function_decl != NULL_TREE);
12132 unsigned char local_variables_forbidden_p
12133 = parser->local_variables_forbidden_p;
12134 bool in_function_body = parser->in_function_body;
12136 /* The body of a lambda-expression is not a subexpression of the enclosing
12137 expression. */
12138 cp_evaluated ev;
12140 if (nested)
12141 push_function_context ();
12142 else
12143 /* Still increment function_depth so that we don't GC in the
12144 middle of an expression. */
12145 ++function_depth;
12147 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
12148 auto ord = make_temp_override (parser->oacc_routine, NULL);
12149 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
12150 vec<tree> omp_privatization_save;
12151 save_omp_privatization_clauses (omp_privatization_save);
12152 /* Clear this in case we're in the middle of a default argument. */
12153 parser->local_variables_forbidden_p = 0;
12154 parser->in_function_body = true;
12157 local_specialization_stack s (lss_copy);
12158 tree fco = lambda_function (lambda_expr);
12159 tree body = start_lambda_function (fco, lambda_expr);
12161 /* Originally C++11 required us to peek for 'return expr'; and
12162 process it specially here to deduce the return type. N3638
12163 removed the need for that. */
12164 cp_parser_function_body (parser, false);
12166 finish_lambda_function (body);
12169 restore_omp_privatization_clauses (omp_privatization_save);
12170 parser->local_variables_forbidden_p = local_variables_forbidden_p;
12171 parser->in_function_body = in_function_body;
12172 if (nested)
12173 pop_function_context();
12174 else
12175 --function_depth;
12178 /* Statements [gram.stmt.stmt] */
12180 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
12182 static void
12183 add_debug_begin_stmt (location_t loc)
12185 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
12186 return;
12187 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
12188 /* A concept is never expanded normally. */
12189 return;
12191 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12192 SET_EXPR_LOCATION (stmt, loc);
12193 add_stmt (stmt);
12196 struct cp_omp_attribute_data
12198 cp_token_cache *tokens;
12199 const c_omp_directive *dir;
12200 c_omp_directive_kind kind;
12203 /* Handle omp::directive and omp::sequence attributes in ATTRS
12204 (if any) at the start of a statement or in attribute-declaration. */
12206 static tree
12207 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12209 if (!flag_openmp && !flag_openmp_simd)
12210 return attrs;
12212 auto_vec<cp_omp_attribute_data, 16> vec;
12213 int cnt = 0;
12214 int tokens = 0;
12215 bool bad = false;
12216 for (tree *pa = &attrs; *pa; )
12217 if (get_attribute_namespace (*pa) == omp_identifier
12218 && is_attribute_p ("directive", get_attribute_name (*pa)))
12220 cnt++;
12221 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12223 tree d = TREE_VALUE (a);
12224 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12225 cp_token *first = DEFPARSE_TOKENS (d)->first;
12226 cp_token *last = DEFPARSE_TOKENS (d)->last;
12227 if (parser->omp_attrs_forbidden_p)
12229 error_at (first->location,
12230 "mixing OpenMP directives with attribute and pragma "
12231 "syntax on the same statement");
12232 parser->omp_attrs_forbidden_p = false;
12233 bad = true;
12235 else if (TREE_PUBLIC (d))
12237 error_at (first->location,
12238 "OpenMP %<omp::decl%> attribute on a statement");
12239 bad = true;
12241 const char *directive[3] = {};
12242 for (int i = 0; i < 3; i++)
12244 tree id = NULL_TREE;
12245 if (first + i == last)
12246 break;
12247 if (first[i].type == CPP_NAME)
12248 id = first[i].u.value;
12249 else if (first[i].type == CPP_KEYWORD)
12250 id = ridpointers[(int) first[i].keyword];
12251 else
12252 break;
12253 directive[i] = IDENTIFIER_POINTER (id);
12255 const c_omp_directive *dir = NULL;
12256 if (directive[0])
12257 dir = c_omp_categorize_directive (directive[0], directive[1],
12258 directive[2]);
12259 if (dir == NULL)
12261 error_at (first->location,
12262 "unknown OpenMP directive name in %qs attribute "
12263 "argument",
12264 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12265 continue;
12267 c_omp_directive_kind kind = dir->kind;
12268 if (dir->id == PRAGMA_OMP_ORDERED)
12270 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12271 depend/doacross clause. */
12272 if (directive[1]
12273 && (strcmp (directive[1], "depend") == 0
12274 || strcmp (directive[1], "doacross") == 0))
12275 kind = C_OMP_DIR_STANDALONE;
12276 else if (first + 2 < last
12277 && first[1].type == CPP_COMMA
12278 && first[2].type == CPP_NAME
12279 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12280 "depend") == 0
12281 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12282 "doacross") == 0))
12283 kind = C_OMP_DIR_STANDALONE;
12285 else if (dir->id == PRAGMA_OMP_ERROR)
12287 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12288 int paren_depth = 0;
12289 for (int i = 1; first + i < last; i++)
12290 if (first[i].type == CPP_OPEN_PAREN)
12291 paren_depth++;
12292 else if (first[i].type == CPP_CLOSE_PAREN)
12293 paren_depth--;
12294 else if (paren_depth == 0
12295 && first + i + 2 < last
12296 && first[i].type == CPP_NAME
12297 && first[i + 1].type == CPP_OPEN_PAREN
12298 && first[i + 2].type == CPP_NAME
12299 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12300 "at")
12301 && !strcmp (IDENTIFIER_POINTER (first[i
12302 + 2].u.value),
12303 "execution"))
12305 kind = C_OMP_DIR_STANDALONE;
12306 break;
12309 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12310 vec.safe_push (v);
12311 if (flag_openmp || dir->simd)
12312 tokens += (last - first) + 1;
12314 cp_omp_attribute_data v = {};
12315 vec.safe_push (v);
12316 *pa = TREE_CHAIN (*pa);
12318 else
12319 pa = &TREE_CHAIN (*pa);
12321 if (bad)
12322 return attrs;
12324 unsigned int i;
12325 cp_omp_attribute_data *v;
12326 cp_omp_attribute_data *construct_seen = nullptr;
12327 cp_omp_attribute_data *standalone_seen = nullptr;
12328 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12329 FOR_EACH_VEC_ELT (vec, i, v)
12330 if (v->tokens)
12332 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12333 construct_seen = v;
12334 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12335 standalone_seen = v;
12337 else
12339 if (standalone_seen && !prev_standalone_seen)
12341 prev_standalone_seen = standalone_seen;
12342 standalone_seen = nullptr;
12346 if (cnt > 1 && construct_seen)
12348 error_at (construct_seen->tokens->first->location,
12349 "OpenMP construct among %<omp::directive%> attributes"
12350 " requires all %<omp::directive%> attributes on the"
12351 " same statement to be in the same %<omp::sequence%>");
12352 return attrs;
12354 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12356 error_at (standalone_seen->tokens->first->location,
12357 "multiple OpenMP standalone directives among"
12358 " %<omp::directive%> attributes must be all within the"
12359 " same %<omp::sequence%>");
12360 return attrs;
12363 if (prev_standalone_seen)
12364 standalone_seen = prev_standalone_seen;
12365 if (standalone_seen
12366 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12368 error_at (standalone_seen->tokens->first->location,
12369 "standalone OpenMP directives in %<omp::directive%> attribute"
12370 " can only appear on an empty statement");
12371 return attrs;
12373 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12375 cp_token *token = cp_lexer_peek_token (parser->lexer);
12376 enum pragma_kind kind = cp_parser_pragma_kind (token);
12377 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12379 error_at (token->location,
12380 "mixing OpenMP directives with attribute and pragma "
12381 "syntax on the same statement");
12382 return attrs;
12386 if (!tokens)
12387 return attrs;
12388 tokens++;
12389 cp_lexer *lexer = cp_lexer_alloc ();
12390 lexer->debugging_p = parser->lexer->debugging_p;
12391 vec_safe_reserve (lexer->buffer, tokens, true);
12392 FOR_EACH_VEC_ELT (vec, i, v)
12394 if (!v->tokens)
12395 continue;
12396 if (!flag_openmp && !v->dir->simd)
12397 continue;
12398 cp_token *first = v->tokens->first;
12399 cp_token *last = v->tokens->last;
12400 cp_token tok = {};
12401 tok.type = CPP_PRAGMA;
12402 tok.keyword = RID_MAX;
12403 tok.u.value = build_int_cst (NULL, v->dir->id);
12404 tok.location = first->location;
12405 lexer->buffer->quick_push (tok);
12406 while (++first < last)
12407 lexer->buffer->quick_push (*first);
12408 tok = {};
12409 tok.type = CPP_PRAGMA_EOL;
12410 tok.keyword = RID_MAX;
12411 tok.location = last->location;
12412 lexer->buffer->quick_push (tok);
12414 cp_token tok = {};
12415 tok.type = CPP_EOF;
12416 tok.keyword = RID_MAX;
12417 tok.location = lexer->buffer->last ().location;
12418 lexer->buffer->quick_push (tok);
12419 lexer->next = parser->lexer;
12420 lexer->next_token = lexer->buffer->address ();
12421 lexer->last_token = lexer->next_token
12422 + lexer->buffer->length ()
12423 - 1;
12424 lexer->in_omp_attribute_pragma = true;
12425 parser->lexer = lexer;
12426 /* Move the current source position to that of the first token in the
12427 new lexer. */
12428 cp_lexer_set_source_position_from_token (lexer->next_token);
12429 return attrs;
12432 /* True if and only if the name is one of the contract types. */
12434 static bool
12435 contract_attribute_p (const_tree id)
12437 return is_attribute_p ("assert", id)
12438 || is_attribute_p ("pre", id)
12439 || is_attribute_p ("post", id);
12442 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12443 (if any) at the start or after declaration-id of a declaration. */
12445 static void
12446 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12447 cp_omp_declare_simd_data *data,
12448 bool start)
12450 if (!flag_openmp && !flag_openmp_simd)
12451 return;
12453 int cnt = 0;
12454 bool bad = false;
12455 bool variant_p = false;
12456 location_t loc = UNKNOWN_LOCATION;
12457 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12458 if (get_attribute_namespace (pa) == omp_identifier
12459 && is_attribute_p ("directive", get_attribute_name (pa)))
12461 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12463 tree d = TREE_VALUE (a);
12464 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12465 cp_token *first = DEFPARSE_TOKENS (d)->first;
12466 cp_token *last = DEFPARSE_TOKENS (d)->last;
12467 const char *directive[3] = {};
12468 for (int i = 0; i < 3; i++)
12470 tree id = NULL_TREE;
12471 if (first + i == last)
12472 break;
12473 if (first[i].type == CPP_NAME)
12474 id = first[i].u.value;
12475 else if (first[i].type == CPP_KEYWORD)
12476 id = ridpointers[(int) first[i].keyword];
12477 else
12478 break;
12479 directive[i] = IDENTIFIER_POINTER (id);
12481 const c_omp_directive *dir = NULL;
12482 if (directive[0])
12483 dir = c_omp_categorize_directive (directive[0], directive[1],
12484 directive[2]);
12485 if (dir == NULL)
12486 continue;
12487 if (dir->id == PRAGMA_OMP_DECLARE
12488 && (strcmp (directive[1], "simd") == 0
12489 || strcmp (directive[1], "variant") == 0))
12491 if (cnt++ == 0)
12493 variant_p = strcmp (directive[1], "variant") == 0;
12494 loc = first->location;
12496 if (start && parser->omp_declare_simd && !bad)
12498 error_at (first->location,
12499 "mixing OpenMP directives with attribute and "
12500 "pragma syntax on the same declaration");
12501 bad = true;
12507 if (bad)
12509 for (tree *pa = pattrs; *pa; )
12510 if (get_attribute_namespace (*pa) == omp_identifier
12511 && is_attribute_p ("directive", get_attribute_name (*pa)))
12512 *pa = TREE_CHAIN (*pa);
12513 else
12514 pa = &TREE_CHAIN (*pa);
12515 return;
12517 if (cnt == 0)
12518 return;
12520 if (parser->omp_declare_simd == NULL)
12522 data->error_seen = false;
12523 data->fndecl_seen = false;
12524 data->variant_p = variant_p;
12525 data->loc = loc;
12526 data->tokens = vNULL;
12527 data->attribs[0] = NULL;
12528 data->attribs[1] = NULL;
12529 parser->omp_declare_simd = data;
12531 parser->omp_declare_simd->attribs[!start] = pattrs;
12534 /* Parse a statement.
12536 statement:
12537 labeled-statement
12538 expression-statement
12539 compound-statement
12540 selection-statement
12541 iteration-statement
12542 jump-statement
12543 declaration-statement
12544 try-block
12546 C++11:
12548 statement:
12549 labeled-statement
12550 attribute-specifier-seq (opt) expression-statement
12551 attribute-specifier-seq (opt) compound-statement
12552 attribute-specifier-seq (opt) selection-statement
12553 attribute-specifier-seq (opt) iteration-statement
12554 attribute-specifier-seq (opt) jump-statement
12555 declaration-statement
12556 attribute-specifier-seq (opt) try-block
12558 init-statement:
12559 expression-statement
12560 simple-declaration
12561 alias-declaration
12563 TM Extension:
12565 statement:
12566 atomic-statement
12568 IN_COMPOUND is true when the statement is nested inside a
12569 cp_parser_compound_statement.
12571 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12572 is a (possibly labeled) if statement which is not enclosed in braces
12573 and has an else clause. This is used to implement -Wparentheses.
12575 CHAIN is a vector of if-else-if conditions.
12577 Note that this version of parsing restricts assertions to be attached to
12578 empty statements. */
12580 static void
12581 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12582 const bool in_compound, bool *if_p, vec<tree> *chain,
12583 location_t *loc_after_labels)
12585 tree statement, std_attrs = NULL_TREE;
12586 cp_token *token;
12587 location_t statement_location, attrs_loc;
12588 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12589 bool has_std_attrs;
12590 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12591 This matters for certain pragmas. */
12592 bool in_compound_for_pragma = in_compound;
12594 restart:
12595 if (if_p != NULL)
12596 *if_p = false;
12597 /* There is no statement yet. */
12598 statement = NULL_TREE;
12600 saved_token_sentinel saved_tokens (parser->lexer);
12601 token = cp_lexer_peek_token (parser->lexer);
12602 attrs_loc = token->location;
12603 if (c_dialect_objc ())
12604 /* In obj-c++, seeing '[[' might be the either the beginning of
12605 c++11 attributes, or a nested objc-message-expression. So
12606 let's parse the c++11 attributes tentatively. */
12607 cp_parser_parse_tentatively (parser);
12608 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12609 if (std_attrs)
12610 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12611 if (c_dialect_objc ())
12613 if (!cp_parser_parse_definitely (parser))
12614 std_attrs = NULL_TREE;
12616 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12618 /* Peek at the next token. */
12619 token = cp_lexer_peek_token (parser->lexer);
12621 /* If we have contracts, check that they're valid in this context. */
12622 if (std_attrs != error_mark_node)
12624 if (tree pre = lookup_attribute ("pre", std_attrs))
12625 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12626 "preconditions cannot be statements");
12627 else if (tree post = lookup_attribute ("post", std_attrs))
12628 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12629 "postconditions cannot be statements");
12631 /* Check that assertions are null statements. */
12632 if (cp_contract_assertion_p (std_attrs))
12633 if (token->type != CPP_SEMICOLON)
12634 error_at (token->location, "assertions must be followed by %<;%>");
12637 bool omp_attrs_forbidden_p;
12638 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12640 if (std_attrs && (flag_openmp || flag_openmp_simd))
12642 bool handle_omp_attribs = false;
12643 if (token->type == CPP_KEYWORD)
12644 switch (token->keyword)
12646 case RID_IF:
12647 case RID_SWITCH:
12648 case RID_WHILE:
12649 case RID_DO:
12650 case RID_FOR:
12651 case RID_BREAK:
12652 case RID_CONTINUE:
12653 case RID_RETURN:
12654 case RID_CO_RETURN:
12655 case RID_GOTO:
12656 case RID_AT_TRY:
12657 case RID_AT_CATCH:
12658 case RID_AT_FINALLY:
12659 case RID_AT_SYNCHRONIZED:
12660 case RID_AT_THROW:
12661 case RID_TRY:
12662 case RID_TRANSACTION_ATOMIC:
12663 case RID_TRANSACTION_RELAXED:
12664 case RID_SYNCHRONIZED:
12665 case RID_ATOMIC_NOEXCEPT:
12666 case RID_ATOMIC_CANCEL:
12667 case RID_TRANSACTION_CANCEL:
12668 handle_omp_attribs = true;
12669 break;
12670 default:
12671 break;
12673 else if (token->type == CPP_SEMICOLON
12674 || token->type == CPP_OPEN_BRACE
12675 || token->type == CPP_PRAGMA)
12676 handle_omp_attribs = true;
12677 if (handle_omp_attribs)
12679 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12680 std_attrs);
12681 token = cp_lexer_peek_token (parser->lexer);
12684 parser->omp_attrs_forbidden_p = false;
12686 /* Remember the location of the first token in the statement. */
12687 cp_token *statement_token = token;
12688 statement_location = token->location;
12689 add_debug_begin_stmt (statement_location);
12690 /* If this is a keyword, then that will often determine what kind of
12691 statement we have. */
12692 if (token->type == CPP_KEYWORD)
12694 enum rid keyword = token->keyword;
12696 switch (keyword)
12698 case RID_CASE:
12699 case RID_DEFAULT:
12700 /* Looks like a labeled-statement with a case label.
12701 Parse the label, and then use tail recursion to parse
12702 the statement. */
12703 cp_parser_label_for_labeled_statement (parser, std_attrs);
12704 in_compound_for_pragma = false;
12705 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12706 goto restart;
12708 case RID_IF:
12709 case RID_SWITCH:
12710 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12711 statement = cp_parser_selection_statement (parser, if_p, chain);
12712 break;
12714 case RID_WHILE:
12715 case RID_DO:
12716 case RID_FOR:
12717 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12718 statement = cp_parser_iteration_statement (parser, if_p, false,
12719 NULL_TREE, false);
12720 break;
12722 case RID_BREAK:
12723 case RID_CONTINUE:
12724 case RID_RETURN:
12725 case RID_CO_RETURN:
12726 case RID_GOTO:
12727 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12728 statement = cp_parser_jump_statement (parser);
12729 break;
12731 /* Objective-C++ exception-handling constructs. */
12732 case RID_AT_TRY:
12733 case RID_AT_CATCH:
12734 case RID_AT_FINALLY:
12735 case RID_AT_SYNCHRONIZED:
12736 case RID_AT_THROW:
12737 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12738 statement = cp_parser_objc_statement (parser);
12739 break;
12741 case RID_TRY:
12742 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12743 statement = cp_parser_try_block (parser);
12744 break;
12746 case RID_NAMESPACE:
12747 /* This must be a namespace alias definition. */
12748 if (has_std_attrs)
12750 /* Attributes should be parsed as part of the
12751 declaration, so let's un-parse them. */
12752 saved_tokens.rollback();
12753 std_attrs = NULL_TREE;
12755 cp_parser_declaration_statement (parser);
12756 return;
12758 case RID_TRANSACTION_ATOMIC:
12759 case RID_TRANSACTION_RELAXED:
12760 case RID_SYNCHRONIZED:
12761 case RID_ATOMIC_NOEXCEPT:
12762 case RID_ATOMIC_CANCEL:
12763 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12764 statement = cp_parser_transaction (parser, token);
12765 break;
12766 case RID_TRANSACTION_CANCEL:
12767 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12768 statement = cp_parser_transaction_cancel (parser);
12769 break;
12771 default:
12772 /* It might be a keyword like `int' that can start a
12773 declaration-statement. */
12774 break;
12777 else if (token->type == CPP_NAME)
12779 /* If the next token is a `:', then we are looking at a
12780 labeled-statement. */
12781 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12782 if (token->type == CPP_COLON)
12784 /* Looks like a labeled-statement with an ordinary label.
12785 Parse the label, and then use tail recursion to parse
12786 the statement. */
12788 cp_parser_label_for_labeled_statement (parser, std_attrs);
12790 /* If there's no statement, it's not a labeled-statement, just
12791 a label. That's allowed in C++23, but only if we're at the
12792 end of a compound-statement. */
12793 if (in_compound
12794 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12796 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12797 if (cxx_dialect < cxx23)
12798 pedwarn (loc, OPT_Wc__23_extensions,
12799 "label at end of compound statement only available "
12800 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12801 return;
12803 in_compound_for_pragma = false;
12804 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12805 goto restart;
12808 /* Anything that starts with a `{' must be a compound-statement. */
12809 else if (token->type == CPP_OPEN_BRACE)
12811 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12812 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12814 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12815 a statement all its own. */
12816 else if (token->type == CPP_PRAGMA)
12818 do_pragma:;
12819 cp_lexer *lexer = parser->lexer;
12820 bool do_restart = false;
12821 /* Only certain OpenMP pragmas are attached to statements, and thus
12822 are considered statements themselves. All others are not. In
12823 the context of a compound, accept the pragma as a "statement" and
12824 return so that we can check for a close brace. Otherwise we
12825 require a real statement and must go back and read one. */
12826 if (in_compound_for_pragma)
12828 if (cp_parser_pragma (parser, pragma_compound, if_p)
12829 && parser->omp_for_parse_state)
12830 check_omp_intervening_code (parser);
12832 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12833 do_restart = true;
12834 else if (parser->omp_for_parse_state)
12835 check_omp_intervening_code (parser);
12836 if (parser->lexer != lexer
12837 && lexer->in_omp_attribute_pragma
12838 && (!in_omp_attribute_pragma || lexer->orphan_p))
12840 if (saved_tokens.lexer == lexer)
12842 if (saved_tokens.mode == STS_COMMIT)
12843 cp_lexer_commit_tokens (lexer);
12844 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12845 saved_tokens.lexer = parser->lexer;
12846 saved_tokens.mode = STS_DONOTHING;
12847 saved_tokens.len = parser->lexer->saved_tokens.length ();
12849 cp_lexer_destroy (lexer);
12850 lexer = parser->lexer;
12852 if (do_restart)
12853 goto restart;
12854 if (parser->lexer == lexer
12855 && lexer->in_omp_attribute_pragma
12856 && !in_omp_attribute_pragma)
12857 parser->lexer->orphan_p = true;
12858 return;
12860 else if (token->type == CPP_EOF)
12862 cp_parser_error (parser, "expected statement");
12863 return;
12866 /* Everything else must be a declaration-statement or an
12867 expression-statement. Try for the declaration-statement
12868 first, unless we are looking at a `;', in which case we know that
12869 we have an expression-statement. */
12870 if (!statement)
12872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12874 if (has_std_attrs)
12875 /* Attributes should be parsed as part of the declaration,
12876 so let's un-parse them. */
12877 saved_tokens.rollback();
12879 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12880 cp_parser_parse_tentatively (parser);
12881 /* Try to parse the declaration-statement. */
12882 cp_parser_declaration_statement (parser);
12883 parser->omp_attrs_forbidden_p = false;
12884 /* If that worked, we're done. */
12885 if (cp_parser_parse_definitely (parser))
12886 return;
12887 /* It didn't work, restore the post-attribute position. */
12888 if (has_std_attrs)
12890 cp_lexer_set_token_position (parser->lexer, statement_token);
12891 if (flag_openmp || flag_openmp_simd)
12893 size_t i = 1;
12894 bool handle_omp_attribs = true;
12895 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12896 == RID_EXTENSION)
12897 i++;
12898 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12900 case RID_ASM:
12901 case RID_NAMESPACE:
12902 case RID_USING:
12903 case RID_LABEL:
12904 case RID_STATIC_ASSERT:
12905 /* Don't handle OpenMP attribs on keywords that
12906 always start a declaration statement but don't
12907 accept attribute before it and therefore
12908 the tentative cp_parser_declaration_statement
12909 fails to parse because of that. */
12910 handle_omp_attribs = false;
12911 break;
12912 default:
12913 break;
12916 if (handle_omp_attribs)
12918 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12919 std_attrs
12920 = cp_parser_handle_statement_omp_attributes
12921 (parser, std_attrs);
12922 parser->omp_attrs_forbidden_p = false;
12923 token = cp_lexer_peek_token (parser->lexer);
12924 if (token->type == CPP_PRAGMA)
12925 goto do_pragma;
12930 /* All preceding labels have been parsed at this point. */
12931 if (loc_after_labels != NULL)
12932 *loc_after_labels = statement_location;
12934 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12936 /* Look for an expression-statement instead. */
12937 statement = cp_parser_expression_statement (parser, in_statement_expr);
12939 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12940 attrs_loc);
12942 /* Handle [[fallthrough]];. */
12943 if (attribute_fallthrough_p (std_attrs))
12945 /* The next token after the fallthrough attribute is ';'. */
12946 if (statement == NULL_TREE)
12948 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12949 statement = build_call_expr_internal_loc (statement_location,
12950 IFN_FALLTHROUGH,
12951 void_type_node, 0);
12952 finish_expr_stmt (statement);
12954 else
12955 warning_at (statement_location, OPT_Wattributes,
12956 "%<fallthrough%> attribute not followed by %<;%>");
12957 std_attrs = NULL_TREE;
12960 /* Handle [[assert: ...]]; */
12961 if (cp_contract_assertion_p (std_attrs))
12963 /* Add the assertion as a statement in the current block. */
12964 gcc_assert (!statement || statement == error_mark_node);
12965 emit_assertion (std_attrs);
12966 std_attrs = NULL_TREE;
12970 /* Set the line number for the statement. */
12971 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12972 SET_EXPR_LOCATION (statement, statement_location);
12974 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12975 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
12976 warning_at (attrs_loc, OPT_Wattributes,
12977 "attributes at the beginning of statement are ignored");
12980 /* Append ATTR to attribute list ATTRS. */
12982 tree
12983 attr_chainon (tree attrs, tree attr)
12985 if (attrs == error_mark_node)
12986 return error_mark_node;
12987 if (attr == error_mark_node)
12988 return error_mark_node;
12989 return chainon (attrs, attr);
12992 /* Parse the label for a labeled-statement, i.e.
12994 label:
12995 attribute-specifier-seq[opt] identifier :
12996 attribute-specifier-seq[opt] case constant-expression :
12997 attribute-specifier-seq[opt] default :
12999 labeled-statement:
13000 label statement
13002 GNU Extension:
13003 case constant-expression ... constant-expression : statement
13005 When a label is parsed without errors, the label is added to the
13006 parse tree by the finish_* functions, so this function doesn't
13007 have to return the label. */
13009 static void
13010 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
13012 cp_token *token;
13013 tree label = NULL_TREE;
13014 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13016 /* The next token should be an identifier. */
13017 token = cp_lexer_peek_token (parser->lexer);
13018 if (token->type != CPP_NAME
13019 && token->type != CPP_KEYWORD)
13021 cp_parser_error (parser, "expected labeled-statement");
13022 return;
13025 /* Remember whether this case or a user-defined label is allowed to fall
13026 through to. */
13027 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
13029 parser->colon_corrects_to_scope_p = false;
13030 switch (token->keyword)
13032 case RID_CASE:
13034 tree expr, expr_hi;
13035 cp_token *ellipsis;
13037 /* Consume the `case' token. */
13038 cp_lexer_consume_token (parser->lexer);
13039 /* Parse the constant-expression. */
13040 expr = cp_parser_constant_expression (parser);
13041 if (check_for_bare_parameter_packs (expr))
13042 expr = error_mark_node;
13044 ellipsis = cp_lexer_peek_token (parser->lexer);
13045 if (ellipsis->type == CPP_ELLIPSIS)
13047 /* Consume the `...' token. */
13048 cp_lexer_consume_token (parser->lexer);
13049 expr_hi = cp_parser_constant_expression (parser);
13050 if (check_for_bare_parameter_packs (expr_hi))
13051 expr_hi = error_mark_node;
13053 /* We don't need to emit warnings here, as the common code
13054 will do this for us. */
13056 else
13057 expr_hi = NULL_TREE;
13059 if (parser->in_switch_statement_p)
13061 tree l = finish_case_label (token->location, expr, expr_hi);
13062 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13064 label = CASE_LABEL (l);
13065 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13068 else
13069 error_at (token->location,
13070 "case label %qE not within a switch statement",
13071 expr);
13073 break;
13075 case RID_DEFAULT:
13076 /* Consume the `default' token. */
13077 cp_lexer_consume_token (parser->lexer);
13079 if (parser->in_switch_statement_p)
13081 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
13082 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13084 label = CASE_LABEL (l);
13085 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13088 else
13089 error_at (token->location, "case label not within a switch statement");
13090 break;
13092 default:
13093 /* Anything else must be an ordinary label. */
13094 label = finish_label_stmt (cp_parser_identifier (parser));
13095 if (label && TREE_CODE (label) == LABEL_DECL)
13096 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13097 break;
13100 /* Require the `:' token. */
13101 cp_parser_require (parser, CPP_COLON, RT_COLON);
13103 /* An ordinary label may optionally be followed by attributes.
13104 However, this is only permitted if the attributes are then
13105 followed by a semicolon. This is because, for backward
13106 compatibility, when parsing
13107 lab: __attribute__ ((unused)) int i;
13108 we want the attribute to attach to "i", not "lab". */
13109 if (label != NULL_TREE
13110 && cp_next_tokens_can_be_gnu_attribute_p (parser))
13112 tree attrs;
13113 cp_parser_parse_tentatively (parser);
13114 attrs = cp_parser_gnu_attributes_opt (parser);
13115 if (attrs == NULL_TREE
13116 /* And fallthrough always binds to the expression-statement. */
13117 || attribute_fallthrough_p (attrs)
13118 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13119 cp_parser_abort_tentative_parse (parser);
13120 else if (!cp_parser_parse_definitely (parser))
13122 else
13123 attributes = attr_chainon (attributes, attrs);
13126 if (attributes != NULL_TREE)
13127 cplus_decl_attributes (&label, attributes, 0);
13129 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13132 /* Parse an expression-statement.
13134 expression-statement:
13135 expression [opt] ;
13137 Returns the new EXPR_STMT -- or NULL_TREE if the expression
13138 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
13139 indicates whether this expression-statement is part of an
13140 expression statement. */
13142 static tree
13143 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
13145 tree statement = NULL_TREE;
13146 cp_token *token = cp_lexer_peek_token (parser->lexer);
13147 location_t loc = token->location;
13149 /* There might be attribute fallthrough. */
13150 tree attr = cp_parser_gnu_attributes_opt (parser);
13152 /* If the next token is a ';', then there is no expression
13153 statement. */
13154 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13156 statement = cp_parser_expression (parser);
13157 if (statement == error_mark_node
13158 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13160 /* If we ran into a problem, make sure we complained. */
13161 gcc_assert (seen_error ());
13163 cp_parser_skip_to_end_of_block_or_statement (parser);
13164 return error_mark_node;
13168 attr = process_stmt_assume_attribute (attr, statement, loc);
13170 /* Handle [[fallthrough]];. */
13171 if (attribute_fallthrough_p (attr))
13173 /* The next token after the fallthrough attribute is ';'. */
13174 if (statement == NULL_TREE)
13175 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
13176 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
13177 void_type_node, 0);
13178 else
13179 warning_at (loc, OPT_Wattributes,
13180 "%<fallthrough%> attribute not followed by %<;%>");
13181 attr = NULL_TREE;
13184 /* Allow "[[fallthrough]];", but warn otherwise. */
13185 if (attr != NULL_TREE && any_nonignored_attribute_p (attr))
13186 warning_at (loc, OPT_Wattributes,
13187 "attributes at the beginning of statement are ignored");
13189 /* Give a helpful message for "A<T>::type t;" and the like. */
13190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13191 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13193 if (TREE_CODE (statement) == SCOPE_REF)
13194 error_at (token->location, "need %<typename%> before %qE because "
13195 "%qT is a dependent scope",
13196 statement, TREE_OPERAND (statement, 0));
13197 else if (is_overloaded_fn (statement)
13198 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13200 /* A::A a; */
13201 tree fn = get_first_fn (statement);
13202 error_at (token->location,
13203 "%<%T::%D%> names the constructor, not the type",
13204 DECL_CONTEXT (fn), DECL_NAME (fn));
13208 /* Consume the final `;'. */
13209 cp_parser_consume_semicolon_at_end_of_statement (parser);
13211 if (in_statement_expr
13212 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13213 /* This is the final expression statement of a statement
13214 expression. */
13215 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13216 else if (statement)
13217 statement = finish_expr_stmt (statement);
13219 return statement;
13222 /* Parse a compound-statement.
13224 compound-statement:
13225 { statement-seq [opt] label-seq [opt] }
13227 label-seq:
13228 label
13229 label-seq label
13231 GNU extension:
13233 compound-statement:
13234 { label-declaration-seq [opt] statement-seq [opt] }
13236 label-declaration-seq:
13237 label-declaration
13238 label-declaration-seq label-declaration
13240 Returns a tree representing the statement. */
13242 static tree
13243 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13244 int bcs_flags, bool function_body)
13246 tree compound_stmt;
13247 matching_braces braces;
13249 /* Consume the `{'. */
13250 if (!braces.require_open (parser))
13251 return error_mark_node;
13252 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13253 && !function_body && cxx_dialect < cxx14)
13254 pedwarn (input_location, OPT_Wpedantic,
13255 "compound-statement in %<constexpr%> function");
13256 /* Begin the compound-statement. */
13257 compound_stmt = begin_compound_stmt (bcs_flags);
13258 /* If the next keyword is `__label__' we have a label declaration. */
13259 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13260 cp_parser_label_declaration (parser);
13261 /* Parse an (optional) statement-seq. */
13262 cp_parser_statement_seq_opt (parser, in_statement_expr);
13264 /* Consume the `}'. */
13265 braces.require_close (parser);
13267 /* Finish the compound-statement. */
13268 finish_compound_stmt (compound_stmt);
13270 return compound_stmt;
13273 /* Diagnose errors related to imperfectly nested loops in an OMP
13274 loop construct. This function is called when such code is seen.
13275 Only issue one such diagnostic no matter how much invalid
13276 intervening code there is in the loop.
13277 FIXME: maybe the location associated with the diagnostic should
13278 be the current parser token instead of the location of the outer loop
13279 nest. */
13281 static void
13282 check_omp_intervening_code (cp_parser *parser)
13284 struct omp_for_parse_data *omp_for_parse_state
13285 = parser->omp_for_parse_state;
13286 gcc_assert (omp_for_parse_state);
13288 if (!omp_for_parse_state->in_intervening_code)
13289 return;
13290 omp_for_parse_state->saw_intervening_code = true;
13292 /* Only diagnose errors related to perfect nesting once. */
13293 if (!omp_for_parse_state->perfect_nesting_fail)
13295 if (omp_for_parse_state->code == OACC_LOOP)
13297 error_at (omp_for_parse_state->for_loc,
13298 "inner loops must be perfectly nested in "
13299 "%<#pragma acc loop%>");
13300 omp_for_parse_state->perfect_nesting_fail = true;
13302 else if (omp_for_parse_state->ordered)
13304 error_at (omp_for_parse_state->for_loc,
13305 "inner loops must be perfectly nested with "
13306 "%<ordered%> clause");
13307 omp_for_parse_state->perfect_nesting_fail = true;
13309 else if (omp_for_parse_state->inscan)
13311 error_at (omp_for_parse_state->for_loc,
13312 "inner loops must be perfectly nested with "
13313 "%<reduction%> %<inscan%> clause");
13314 omp_for_parse_state->perfect_nesting_fail = true;
13316 /* TODO: Also reject loops with TILE directive. */
13317 if (omp_for_parse_state->perfect_nesting_fail)
13318 omp_for_parse_state->fail = true;
13322 /* Parse an (optional) statement-seq.
13324 statement-seq:
13325 statement
13326 statement-seq [opt] statement */
13328 static void
13329 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13331 struct omp_for_parse_data *omp_for_parse_state
13332 = parser->omp_for_parse_state;
13333 bool in_omp_loop_block
13334 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13336 /* Scan statements until there aren't any more. */
13337 while (true)
13339 cp_token *token = cp_lexer_peek_token (parser->lexer);
13341 /* If we are looking at a `}', then we have run out of
13342 statements; the same is true if we have reached the end
13343 of file, or have stumbled upon a stray '@end'. */
13344 if (token->type == CPP_CLOSE_BRACE
13345 || token->type == CPP_EOF
13346 || token->type == CPP_PRAGMA_EOL
13347 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13348 break;
13350 /* If we are in a compound statement and find 'else' then
13351 something went wrong. */
13352 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13354 if (parser->in_statement & IN_IF_STMT)
13355 break;
13356 else
13358 token = cp_lexer_consume_token (parser->lexer);
13359 error_at (token->location, "%<else%> without a previous %<if%>");
13363 /* Handle special cases for OMP FOR canonical loop syntax. */
13364 else if (in_omp_loop_block)
13366 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13367 if (want_nested_loop
13368 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13370 /* Found the nested loop. */
13371 omp_for_parse_state->depth++;
13372 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13373 omp_for_parse_state->depth--;
13375 else if (token->type == CPP_SEMICOLON)
13377 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13378 loop form, GCC used to accept an empty statements as not
13379 being intervening code. Continue to do that, as an
13380 extension. */
13381 /* FIXME: Maybe issue a warning or something here? */
13382 cp_lexer_consume_token (parser->lexer);
13384 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13385 /* The nested compound statement may contain the next loop, or
13386 it might just be intervening code. */
13388 cp_parser_statement (parser, in_statement_expr, true, NULL);
13389 if (omp_for_parse_state->want_nested_loop)
13390 check_omp_intervening_code (parser);
13392 else
13394 /* This must be intervening code. */
13395 omp_for_parse_state->want_nested_loop = false;
13396 /* Defer calling check_omp_intervening_code on pragmas until
13397 cp_parser_statement, because we can't know until we parse
13398 it whether or not the pragma is a statement. */
13399 if (token->type != CPP_PRAGMA)
13400 check_omp_intervening_code (parser);
13401 cp_parser_statement (parser, in_statement_expr, true, NULL);
13402 omp_for_parse_state->want_nested_loop = want_nested_loop;
13404 continue;
13407 /* Parse the statement. */
13408 cp_parser_statement (parser, in_statement_expr, true, NULL);
13412 /* Return true if this is the C++20 version of range-based-for with
13413 init-statement. */
13415 static bool
13416 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13418 bool r = false;
13420 /* Save tokens so that we can put them back. */
13421 cp_lexer_save_tokens (parser->lexer);
13423 /* There has to be an unnested ; followed by an unnested :. */
13424 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13425 /*recovering=*/false,
13426 CPP_SEMICOLON,
13427 /*consume_paren=*/false) != -1)
13428 goto out;
13430 /* We found the semicolon, eat it now. */
13431 cp_lexer_consume_token (parser->lexer);
13433 /* Now look for ':' that is not nested in () or {}. */
13434 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13435 /*recovering=*/false,
13436 CPP_COLON,
13437 /*consume_paren=*/false) == -1);
13439 out:
13440 /* Roll back the tokens we skipped. */
13441 cp_lexer_rollback_tokens (parser->lexer);
13443 return r;
13446 /* Return true if we're looking at (init; cond), false otherwise. */
13448 static bool
13449 cp_parser_init_statement_p (cp_parser *parser)
13451 /* Save tokens so that we can put them back. */
13452 cp_lexer_save_tokens (parser->lexer);
13454 /* Look for ';' that is not nested in () or {}. */
13455 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13456 /*recovering=*/false,
13457 CPP_SEMICOLON,
13458 /*consume_paren=*/false);
13460 /* Roll back the tokens we skipped. */
13461 cp_lexer_rollback_tokens (parser->lexer);
13463 return ret == -1;
13466 /* Parse a selection-statement.
13468 selection-statement:
13469 if ( init-statement [opt] condition ) statement
13470 if ( init-statement [opt] condition ) statement else statement
13471 switch ( init-statement [opt] condition ) statement
13473 Returns the new IF_STMT or SWITCH_STMT.
13475 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13476 is a (possibly labeled) if statement which is not enclosed in
13477 braces and has an else clause. This is used to implement
13478 -Wparentheses.
13480 CHAIN is a vector of if-else-if conditions. This is used to implement
13481 -Wduplicated-cond. */
13483 static tree
13484 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13485 vec<tree> *chain)
13487 cp_token *token;
13488 enum rid keyword;
13489 token_indent_info guard_tinfo;
13491 if (if_p != NULL)
13492 *if_p = false;
13494 /* Peek at the next token. */
13495 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13496 guard_tinfo = get_token_indent_info (token);
13498 /* See what kind of keyword it is. */
13499 keyword = token->keyword;
13500 switch (keyword)
13502 case RID_IF:
13503 case RID_SWITCH:
13505 tree statement;
13506 tree condition;
13508 bool cx = false;
13509 if (keyword == RID_IF
13510 && cp_lexer_next_token_is_keyword (parser->lexer,
13511 RID_CONSTEXPR))
13513 cx = true;
13514 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13515 if (cxx_dialect < cxx17)
13516 pedwarn (tok->location, OPT_Wc__17_extensions,
13517 "%<if constexpr%> only available with "
13518 "%<-std=c++17%> or %<-std=gnu++17%>");
13520 int ce = 0;
13521 if (keyword == RID_IF && !cx)
13523 if (cp_lexer_next_token_is_keyword (parser->lexer,
13524 RID_CONSTEVAL))
13525 ce = 1;
13526 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13527 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13528 RID_CONSTEVAL))
13530 ce = -1;
13531 cp_lexer_consume_token (parser->lexer);
13534 if (ce)
13536 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13537 if (cxx_dialect < cxx23)
13538 pedwarn (tok->location, OPT_Wc__23_extensions,
13539 "%<if consteval%> only available with "
13540 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13542 bool save_in_consteval_if_p = in_consteval_if_p;
13543 statement = begin_if_stmt ();
13544 IF_STMT_CONSTEVAL_P (statement) = true;
13545 condition = finish_if_stmt_cond (boolean_false_node, statement);
13547 gcc_rich_location richloc (tok->location);
13548 bool non_compound_stmt_p = false;
13549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13551 non_compound_stmt_p = true;
13552 richloc.add_fixit_insert_after (tok->location, "{");
13555 in_consteval_if_p |= ce > 0;
13556 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13558 if (non_compound_stmt_p)
13560 location_t before_loc
13561 = cp_lexer_peek_token (parser->lexer)->location;
13562 richloc.add_fixit_insert_before (before_loc, "}");
13563 error_at (&richloc,
13564 "%<if consteval%> requires compound statement");
13565 non_compound_stmt_p = false;
13568 finish_then_clause (statement);
13570 /* If the next token is `else', parse the else-clause. */
13571 if (cp_lexer_next_token_is_keyword (parser->lexer,
13572 RID_ELSE))
13574 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13575 gcc_rich_location else_richloc (else_tok->location);
13576 guard_tinfo = get_token_indent_info (else_tok);
13577 /* Consume the `else' keyword. */
13578 cp_lexer_consume_token (parser->lexer);
13580 begin_else_clause (statement);
13582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13584 non_compound_stmt_p = true;
13585 else_richloc.add_fixit_insert_after (else_tok->location,
13586 "{");
13589 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13590 cp_parser_implicitly_scoped_statement (parser, NULL,
13591 guard_tinfo);
13593 if (non_compound_stmt_p)
13595 location_t before_loc
13596 = cp_lexer_peek_token (parser->lexer)->location;
13597 else_richloc.add_fixit_insert_before (before_loc, "}");
13598 error_at (&else_richloc,
13599 "%<if consteval%> requires compound statement");
13602 finish_else_clause (statement);
13605 in_consteval_if_p = save_in_consteval_if_p;
13606 if (ce < 0)
13608 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13609 if (THEN_CLAUSE (statement) == NULL_TREE)
13610 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13613 finish_if_stmt (statement);
13614 return statement;
13617 /* Look for the `('. */
13618 matching_parens parens;
13619 if (!parens.require_open (parser))
13621 cp_parser_skip_to_end_of_statement (parser);
13622 return error_mark_node;
13625 /* Begin the selection-statement. */
13626 if (keyword == RID_IF)
13628 statement = begin_if_stmt ();
13629 IF_STMT_CONSTEXPR_P (statement) = cx;
13631 else
13632 statement = begin_switch_stmt ();
13634 /* Parse the optional init-statement. */
13635 if (cp_parser_init_statement_p (parser))
13637 tree decl;
13638 if (cxx_dialect < cxx17)
13639 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13640 OPT_Wc__17_extensions,
13641 "init-statement in selection statements only available "
13642 "with %<-std=c++17%> or %<-std=gnu++17%>");
13643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13644 /* A non-empty init-statement can have arbitrary side
13645 effects. */
13646 vec_free (chain);
13647 cp_parser_init_statement (parser, &decl);
13650 /* Parse the condition. */
13651 condition = cp_parser_condition (parser);
13652 /* Look for the `)'. */
13653 if (!parens.require_close (parser))
13654 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13655 /*consume_paren=*/true);
13657 if (keyword == RID_IF)
13659 bool nested_if;
13660 unsigned char in_statement;
13662 /* Add the condition. */
13663 condition = finish_if_stmt_cond (condition, statement);
13665 if (warn_duplicated_cond)
13666 warn_duplicated_cond_add_or_warn (token->location, condition,
13667 &chain);
13669 /* Parse the then-clause. */
13670 in_statement = parser->in_statement;
13671 parser->in_statement |= IN_IF_STMT;
13673 /* Outside a template, the non-selected branch of a constexpr
13674 if is a 'discarded statement', i.e. unevaluated. */
13675 bool was_discarded = in_discarded_stmt;
13676 bool discard_then = (cx && !processing_template_decl
13677 && integer_zerop (condition));
13678 if (discard_then)
13680 in_discarded_stmt = true;
13681 ++c_inhibit_evaluation_warnings;
13684 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13685 guard_tinfo);
13687 parser->in_statement = in_statement;
13689 finish_then_clause (statement);
13691 if (discard_then)
13693 THEN_CLAUSE (statement) = NULL_TREE;
13694 in_discarded_stmt = was_discarded;
13695 --c_inhibit_evaluation_warnings;
13698 /* If the next token is `else', parse the else-clause. */
13699 if (cp_lexer_next_token_is_keyword (parser->lexer,
13700 RID_ELSE))
13702 bool discard_else = (cx && !processing_template_decl
13703 && integer_nonzerop (condition));
13704 if (discard_else)
13706 in_discarded_stmt = true;
13707 ++c_inhibit_evaluation_warnings;
13710 guard_tinfo
13711 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13712 /* Consume the `else' keyword. */
13713 cp_lexer_consume_token (parser->lexer);
13714 if (warn_duplicated_cond)
13716 if (cp_lexer_next_token_is_keyword (parser->lexer,
13717 RID_IF)
13718 && chain == NULL)
13720 /* We've got "if (COND) else if (COND2)". Start
13721 the condition chain and add COND as the first
13722 element. */
13723 chain = new vec<tree> ();
13724 if (!CONSTANT_CLASS_P (condition)
13725 && !TREE_SIDE_EFFECTS (condition))
13727 /* Wrap it in a NOP_EXPR so that we can set the
13728 location of the condition. */
13729 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13730 condition);
13731 SET_EXPR_LOCATION (e, token->location);
13732 chain->safe_push (e);
13735 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13736 RID_IF))
13737 /* This is if-else without subsequent if. Zap the
13738 condition chain; we would have already warned at
13739 this point. */
13740 vec_free (chain);
13742 begin_else_clause (statement);
13743 /* Parse the else-clause. */
13744 cp_parser_implicitly_scoped_statement (parser, NULL,
13745 guard_tinfo, chain);
13747 finish_else_clause (statement);
13749 /* If we are currently parsing a then-clause, then
13750 IF_P will not be NULL. We set it to true to
13751 indicate that this if statement has an else clause.
13752 This may trigger the Wparentheses warning below
13753 when we get back up to the parent if statement. */
13754 if (if_p != NULL)
13755 *if_p = true;
13757 if (discard_else)
13759 ELSE_CLAUSE (statement) = NULL_TREE;
13760 in_discarded_stmt = was_discarded;
13761 --c_inhibit_evaluation_warnings;
13764 else
13766 /* This if statement does not have an else clause. If
13767 NESTED_IF is true, then the then-clause has an if
13768 statement which does have an else clause. We warn
13769 about the potential ambiguity. */
13770 if (nested_if)
13771 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13772 "suggest explicit braces to avoid ambiguous"
13773 " %<else%>");
13774 if (warn_duplicated_cond)
13775 /* We don't need the condition chain anymore. */
13776 vec_free (chain);
13779 /* Now we're all done with the if-statement. */
13780 finish_if_stmt (statement);
13782 else
13784 bool in_switch_statement_p;
13785 unsigned char in_statement;
13787 /* Add the condition. */
13788 finish_switch_cond (condition, statement);
13790 /* Parse the body of the switch-statement. */
13791 in_switch_statement_p = parser->in_switch_statement_p;
13792 in_statement = parser->in_statement;
13793 parser->in_switch_statement_p = true;
13794 parser->in_statement |= IN_SWITCH_STMT;
13795 cp_parser_implicitly_scoped_statement (parser, if_p,
13796 guard_tinfo);
13797 parser->in_switch_statement_p = in_switch_statement_p;
13798 parser->in_statement = in_statement;
13800 /* Now we're all done with the switch-statement. */
13801 finish_switch_stmt (statement);
13804 return statement;
13806 break;
13808 default:
13809 cp_parser_error (parser, "expected selection-statement");
13810 return error_mark_node;
13814 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13815 If we have seen at least one decl-specifier, and the next token is not
13816 a parenthesis (after "int (" we might be looking at a functional cast)
13817 neither we are dealing with a concept-check expression then we must be
13818 looking at a declaration. */
13820 static void
13821 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13822 cp_decl_specifier_seq *decl_specs)
13824 if (decl_specs->any_specifiers_p
13825 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13826 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13827 && !cp_parser_error_occurred (parser)
13828 && !(decl_specs->type
13829 && TREE_CODE (decl_specs->type) == TYPE_DECL
13830 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13831 cp_parser_commit_to_tentative_parse (parser);
13834 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13835 The declarator shall not specify a function or an array. Returns
13836 TRUE if the declarator is valid, FALSE otherwise. */
13838 static bool
13839 cp_parser_check_condition_declarator (cp_parser* parser,
13840 cp_declarator *declarator,
13841 location_t loc)
13843 if (declarator == cp_error_declarator
13844 || function_declarator_p (declarator)
13845 || declarator->kind == cdk_array)
13847 if (declarator == cp_error_declarator)
13848 /* Already complained. */;
13849 else if (declarator->kind == cdk_array)
13850 error_at (loc, "condition declares an array");
13851 else
13852 error_at (loc, "condition declares a function");
13853 if (parser->fully_implicit_function_template_p)
13854 abort_fully_implicit_template (parser);
13855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13856 /*or_comma=*/false,
13857 /*consume_paren=*/false);
13858 return false;
13860 else
13861 return true;
13864 /* Parse a condition.
13866 condition:
13867 expression
13868 type-specifier-seq declarator = initializer-clause
13869 type-specifier-seq declarator braced-init-list
13871 GNU Extension:
13873 condition:
13874 type-specifier-seq declarator asm-specification [opt]
13875 attributes [opt] = assignment-expression
13877 Returns the expression that should be tested. */
13879 static tree
13880 cp_parser_condition (cp_parser* parser)
13882 cp_decl_specifier_seq type_specifiers;
13883 const char *saved_message;
13884 int declares_class_or_enum;
13886 /* Try the declaration first. */
13887 cp_parser_parse_tentatively (parser);
13888 /* New types are not allowed in the type-specifier-seq for a
13889 condition. */
13890 saved_message = parser->type_definition_forbidden_message;
13891 parser->type_definition_forbidden_message
13892 = G_("types may not be defined in conditions");
13893 /* Parse the type-specifier-seq. */
13894 cp_parser_decl_specifier_seq (parser,
13895 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13896 &type_specifiers,
13897 &declares_class_or_enum);
13898 /* Restore the saved message. */
13899 parser->type_definition_forbidden_message = saved_message;
13901 /* Gather the attributes that were provided with the
13902 decl-specifiers. */
13903 tree prefix_attributes = type_specifiers.attributes;
13905 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13907 /* If all is well, we might be looking at a declaration. */
13908 if (!cp_parser_error_occurred (parser))
13910 tree decl;
13911 tree asm_specification;
13912 tree attributes;
13913 cp_declarator *declarator;
13914 tree initializer = NULL_TREE;
13915 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13917 /* Parse the declarator. */
13918 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13919 CP_PARSER_FLAGS_NONE,
13920 /*ctor_dtor_or_conv_p=*/NULL,
13921 /*parenthesized_p=*/NULL,
13922 /*member_p=*/false,
13923 /*friend_p=*/false,
13924 /*static_p=*/false);
13925 /* Parse the attributes. */
13926 attributes = cp_parser_attributes_opt (parser);
13927 /* Parse the asm-specification. */
13928 asm_specification = cp_parser_asm_specification_opt (parser);
13929 /* If the next token is not an `=' or '{', then we might still be
13930 looking at an expression. For example:
13932 if (A(a).x)
13934 looks like a decl-specifier-seq and a declarator -- but then
13935 there is no `=', so this is an expression. */
13936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13937 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13938 cp_parser_simulate_error (parser);
13940 /* If we did see an `=' or '{', then we are looking at a declaration
13941 for sure. */
13942 if (cp_parser_parse_definitely (parser))
13944 tree pushed_scope;
13945 bool non_constant_p = false;
13946 int flags = LOOKUP_ONLYCONVERTING;
13948 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13949 return error_mark_node;
13951 /* Create the declaration. */
13952 decl = start_decl (declarator, &type_specifiers,
13953 /*initialized_p=*/true,
13954 attributes, prefix_attributes,
13955 &pushed_scope);
13957 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13958 /* Parse the initializer. */
13959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13961 initializer = cp_parser_braced_list (parser, &non_constant_p);
13962 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13963 flags = 0;
13965 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13967 /* Consume the `='. */
13968 cp_lexer_consume_token (parser->lexer);
13969 initializer = cp_parser_initializer_clause (parser,
13970 &non_constant_p);
13972 else
13974 cp_parser_error (parser, "expected initializer");
13975 initializer = error_mark_node;
13977 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13978 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13980 /* Process the initializer. */
13981 cp_finish_decl (decl,
13982 initializer, !non_constant_p,
13983 asm_specification,
13984 flags);
13986 if (pushed_scope)
13987 pop_scope (pushed_scope);
13989 return convert_from_reference (decl);
13992 /* If we didn't even get past the declarator successfully, we are
13993 definitely not looking at a declaration. */
13994 else
13995 cp_parser_abort_tentative_parse (parser);
13997 /* Otherwise, we are looking at an expression. */
13998 return cp_parser_expression (parser);
14001 /* Parses a for-statement or range-for-statement until the closing ')',
14002 not included. */
14004 static tree
14005 cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
14007 tree init, scope, decl;
14008 bool is_range_for;
14010 /* Begin the for-statement. */
14011 scope = begin_for_scope (&init);
14013 /* Maybe parse the optional init-statement in a range-based for loop. */
14014 if (cp_parser_range_based_for_with_init_p (parser)
14015 /* Checked for diagnostic purposes only. */
14016 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14018 tree dummy;
14019 cp_parser_init_statement (parser, &dummy);
14020 if (cxx_dialect < cxx20)
14022 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14023 OPT_Wc__20_extensions,
14024 "range-based %<for%> loops with initializer only "
14025 "available with %<-std=c++20%> or %<-std=gnu++20%>");
14026 decl = error_mark_node;
14030 /* Parse the initialization. */
14031 is_range_for = cp_parser_init_statement (parser, &decl);
14033 if (is_range_for)
14034 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
14035 novector, false);
14036 else
14037 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
14040 static tree
14041 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
14042 tree unroll, bool novector)
14044 /* Normal for loop */
14045 tree condition = NULL_TREE;
14046 tree expression = NULL_TREE;
14047 tree stmt;
14049 stmt = begin_for_stmt (scope, init);
14050 /* The init-statement has already been parsed in
14051 cp_parser_init_statement, so no work is needed here. */
14052 finish_init_stmt (stmt);
14054 /* If there's a condition, process it. */
14055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14056 condition = cp_parser_condition (parser);
14057 else if (ivdep)
14059 cp_parser_error (parser, "missing loop condition in loop with "
14060 "%<GCC ivdep%> pragma");
14061 condition = error_mark_node;
14063 else if (unroll)
14065 cp_parser_error (parser, "missing loop condition in loop with "
14066 "%<GCC unroll%> pragma");
14067 condition = error_mark_node;
14069 finish_for_cond (condition, stmt, ivdep, unroll, novector);
14070 /* Look for the `;'. */
14071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14073 /* If there's an expression, process it. */
14074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14075 expression = cp_parser_expression (parser);
14076 finish_for_expr (expression, stmt);
14078 return stmt;
14081 /* Tries to parse a range-based for-statement:
14083 range-based-for:
14084 decl-specifier-seq declarator : expression
14086 The decl-specifier-seq declarator and the `:' are already parsed by
14087 cp_parser_init_statement. If processing_template_decl it returns a
14088 newly created RANGE_FOR_STMT; if not, it is converted to a
14089 regular FOR_STMT. */
14091 static tree
14092 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
14093 bool ivdep, tree unroll, bool novector, bool is_omp)
14095 tree stmt, range_expr;
14096 auto_vec <cxx_binding *, 16> bindings;
14097 auto_vec <tree, 16> names;
14098 cp_decomp decomp_d, *decomp = NULL;
14100 /* Get the range declaration momentarily out of the way so that
14101 the range expression doesn't clash with it. */
14102 if (range_decl != error_mark_node)
14104 if (DECL_HAS_VALUE_EXPR_P (range_decl))
14106 tree v = DECL_VALUE_EXPR (range_decl);
14107 /* For decomposition declaration get all of the corresponding
14108 declarations out of the way. */
14109 if (TREE_CODE (v) == ARRAY_REF
14110 && VAR_P (TREE_OPERAND (v, 0))
14111 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
14113 tree d = range_decl;
14114 range_decl = TREE_OPERAND (v, 0);
14115 decomp = &decomp_d;
14116 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
14117 decomp->decl = d;
14118 for (unsigned int i = 0; i < decomp->count;
14119 i++, d = DECL_CHAIN (d))
14121 tree name = DECL_NAME (d);
14122 names.safe_push (name);
14123 bindings.safe_push (IDENTIFIER_BINDING (name));
14124 IDENTIFIER_BINDING (name)
14125 = IDENTIFIER_BINDING (name)->previous;
14129 if (names.is_empty ())
14131 tree name = DECL_NAME (range_decl);
14132 names.safe_push (name);
14133 bindings.safe_push (IDENTIFIER_BINDING (name));
14134 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
14138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14139 range_expr = cp_parser_braced_list (parser);
14140 else
14141 range_expr = cp_parser_expression (parser);
14143 /* Put the range declaration(s) back into scope. */
14144 for (unsigned int i = 0; i < names.length (); i++)
14146 cxx_binding *binding = bindings[i];
14147 binding->previous = IDENTIFIER_BINDING (names[i]);
14148 IDENTIFIER_BINDING (names[i]) = binding;
14151 /* finish_omp_for has its own code for the following, so just
14152 return the range_expr instead. */
14153 if (is_omp)
14154 return range_expr;
14156 /* If in template, STMT is converted to a normal for-statement
14157 at instantiation. If not, it is done just ahead. */
14158 if (processing_template_decl)
14160 if (check_for_bare_parameter_packs (range_expr))
14161 range_expr = error_mark_node;
14162 stmt = begin_range_for_stmt (scope, init);
14163 if (ivdep)
14164 RANGE_FOR_IVDEP (stmt) = 1;
14165 if (unroll)
14166 RANGE_FOR_UNROLL (stmt) = unroll;
14167 if (novector)
14168 RANGE_FOR_NOVECTOR (stmt) = 1;
14169 finish_range_for_decl (stmt, range_decl, range_expr);
14170 if (!type_dependent_expression_p (range_expr)
14171 /* do_auto_deduction doesn't mess with template init-lists. */
14172 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14173 do_range_for_auto_deduction (range_decl, range_expr, decomp);
14175 else
14177 stmt = begin_for_stmt (scope, init);
14178 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
14179 ivdep, unroll, novector);
14181 return stmt;
14184 /* Subroutine of cp_convert_range_for: given the initializer expression,
14185 builds up the range temporary. */
14187 static tree
14188 build_range_temp (tree range_expr)
14190 /* Find out the type deduced by the declaration
14191 `auto &&__range = range_expr'. */
14192 tree auto_node = make_auto ();
14193 tree range_type = cp_build_reference_type (auto_node, true);
14194 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14196 /* Create the __range variable. */
14197 tree range_temp = build_decl (input_location, VAR_DECL,
14198 for_range__identifier, range_type);
14199 TREE_USED (range_temp) = 1;
14200 DECL_ARTIFICIAL (range_temp) = 1;
14202 return range_temp;
14205 /* Used by cp_parser_range_for in template context: we aren't going to
14206 do a full conversion yet, but we still need to resolve auto in the
14207 type of the for-range-declaration if present. This is basically
14208 a shortcut version of cp_convert_range_for. */
14210 static void
14211 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14213 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14214 if (auto_node)
14216 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14217 range_temp = convert_from_reference (build_range_temp (range_expr));
14218 iter_type = (cp_parser_perform_range_for_lookup
14219 (range_temp, &begin_dummy, &end_dummy));
14220 if (iter_type)
14222 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14223 iter_type);
14224 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14225 RO_UNARY_STAR, NULL_TREE,
14226 tf_warning_or_error);
14227 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14228 iter_decl, auto_node,
14229 tf_warning_or_error,
14230 adc_variable_type);
14231 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
14232 cp_finish_decomp (decl, decomp);
14237 /* Warns when the loop variable should be changed to a reference type to
14238 avoid unnecessary copying. I.e., from
14240 for (const auto x : range)
14242 where range returns a reference, to
14244 for (const auto &x : range)
14246 if this version doesn't make a copy.
14248 This function also warns when the loop variable is initialized with
14249 a value of a different type resulting in a copy:
14251 int arr[10];
14252 for (const double &x : arr)
14254 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14255 This function is never called when processing_template_decl is on. */
14257 static void
14258 warn_for_range_copy (tree decl, tree expr)
14260 if (!warn_range_loop_construct
14261 || decl == error_mark_node)
14262 return;
14264 location_t loc = DECL_SOURCE_LOCATION (decl);
14265 tree type = TREE_TYPE (decl);
14267 if (from_macro_expansion_at (loc))
14268 return;
14270 if (TYPE_REF_P (type))
14272 if (glvalue_p (expr)
14273 && ref_conv_binds_to_temporary (type, expr).is_true ())
14275 auto_diagnostic_group d;
14276 if (warning_at (loc, OPT_Wrange_loop_construct,
14277 "loop variable %qD of type %qT binds to a temporary "
14278 "constructed from type %qT", decl, type,
14279 TREE_TYPE (expr)))
14281 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14282 TYPE_QUAL_CONST);
14283 ref = cp_build_reference_type (ref, /*rval*/false);
14284 inform (loc, "use non-reference type %qT to make the copy "
14285 "explicit or %qT to prevent copying",
14286 non_reference (type), ref);
14289 return;
14291 else if (!CP_TYPE_CONST_P (type))
14292 return;
14294 /* Since small trivially copyable types are cheap to copy, we suppress the
14295 warning for them. 64B is a common size of a cache line. */
14296 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14297 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14298 && trivially_copyable_p (type)))
14299 return;
14301 /* If we can initialize a reference directly, suggest that to avoid the
14302 copy. */
14303 tree rtype = cp_build_reference_type (type, /*rval*/false);
14304 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14306 auto_diagnostic_group d;
14307 if (warning_at (loc, OPT_Wrange_loop_construct,
14308 "loop variable %qD creates a copy from type %qT",
14309 decl, type))
14311 gcc_rich_location richloc (loc);
14312 richloc.add_fixit_insert_before ("&");
14313 inform (&richloc, "use reference type to prevent copying");
14318 /* Converts a range-based for-statement into a normal
14319 for-statement, as per the definition.
14321 for (RANGE_DECL : RANGE_EXPR)
14322 BLOCK
14324 should be equivalent to:
14327 auto &&__range = RANGE_EXPR;
14328 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14329 __begin != __end;
14330 ++__begin)
14332 RANGE_DECL = *__begin;
14333 BLOCK
14337 If RANGE_EXPR is an array:
14338 BEGIN_EXPR = __range
14339 END_EXPR = __range + ARRAY_SIZE(__range)
14340 Else if RANGE_EXPR has a member 'begin' or 'end':
14341 BEGIN_EXPR = __range.begin()
14342 END_EXPR = __range.end()
14343 Else:
14344 BEGIN_EXPR = begin(__range)
14345 END_EXPR = end(__range);
14347 If __range has a member 'begin' but not 'end', or vice versa, we must
14348 still use the second alternative (it will surely fail, however).
14349 When calling begin()/end() in the third alternative we must use
14350 argument dependent lookup, but always considering 'std' as an associated
14351 namespace. */
14353 tree
14354 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14355 cp_decomp *decomp, bool ivdep, tree unroll,
14356 bool novector)
14358 tree begin, end;
14359 tree iter_type, begin_expr, end_expr;
14360 tree condition, expression;
14362 range_expr = mark_lvalue_use (range_expr);
14364 if (range_decl == error_mark_node || range_expr == error_mark_node)
14365 /* If an error happened previously do nothing or else a lot of
14366 unhelpful errors would be issued. */
14367 begin_expr = end_expr = iter_type = error_mark_node;
14368 else
14370 tree range_temp;
14372 if (VAR_P (range_expr)
14373 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14374 /* Can't bind a reference to an array of runtime bound. */
14375 range_temp = range_expr;
14376 else
14378 range_temp = build_range_temp (range_expr);
14379 pushdecl (range_temp);
14380 cp_finish_decl (range_temp, range_expr,
14381 /*is_constant_init*/false, NULL_TREE,
14382 LOOKUP_ONLYCONVERTING);
14383 range_temp = convert_from_reference (range_temp);
14385 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14386 &begin_expr, &end_expr);
14389 /* The new for initialization statement. */
14390 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14391 iter_type);
14392 TREE_USED (begin) = 1;
14393 DECL_ARTIFICIAL (begin) = 1;
14394 pushdecl (begin);
14395 cp_finish_decl (begin, begin_expr,
14396 /*is_constant_init*/false, NULL_TREE,
14397 LOOKUP_ONLYCONVERTING);
14399 if (cxx_dialect >= cxx17)
14400 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14401 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14402 TREE_USED (end) = 1;
14403 DECL_ARTIFICIAL (end) = 1;
14404 pushdecl (end);
14405 cp_finish_decl (end, end_expr,
14406 /*is_constant_init*/false, NULL_TREE,
14407 LOOKUP_ONLYCONVERTING);
14409 finish_init_stmt (statement);
14411 /* The new for condition. */
14412 condition = build_x_binary_op (input_location, NE_EXPR,
14413 begin, ERROR_MARK,
14414 end, ERROR_MARK,
14415 NULL_TREE, NULL, tf_warning_or_error);
14416 finish_for_cond (condition, statement, ivdep, unroll, novector);
14418 /* The new increment expression. */
14419 expression = finish_unary_op_expr (input_location,
14420 PREINCREMENT_EXPR, begin,
14421 tf_warning_or_error);
14422 finish_for_expr (expression, statement);
14424 /* The declaration is initialized with *__begin inside the loop body. */
14425 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14426 NULL_TREE, tf_warning_or_error);
14427 cp_finish_decl (range_decl, deref_begin,
14428 /*is_constant_init*/false, NULL_TREE,
14429 LOOKUP_ONLYCONVERTING, decomp);
14430 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14431 cp_finish_decomp (range_decl, decomp);
14433 warn_for_range_copy (range_decl, deref_begin);
14435 return statement;
14438 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14439 We need to solve both at the same time because the method used
14440 depends on the existence of members begin or end.
14441 Returns the type deduced for the iterator expression. */
14443 static tree
14444 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14446 if (error_operand_p (range))
14448 *begin = *end = error_mark_node;
14449 return error_mark_node;
14452 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14454 error ("range-based %<for%> expression of type %qT "
14455 "has incomplete type", TREE_TYPE (range));
14456 *begin = *end = error_mark_node;
14457 return error_mark_node;
14459 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14461 /* If RANGE is an array, we will use pointer arithmetic. */
14462 *begin = decay_conversion (range, tf_warning_or_error);
14463 *end = build_binary_op (input_location, PLUS_EXPR,
14464 range,
14465 array_type_nelts_top (TREE_TYPE (range)),
14466 false);
14467 return TREE_TYPE (*begin);
14469 else
14471 /* If it is not an array, we must do a bit of magic. */
14472 tree id_begin, id_end;
14473 tree member_begin, member_end;
14475 *begin = *end = error_mark_node;
14477 id_begin = get_identifier ("begin");
14478 id_end = get_identifier ("end");
14479 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14480 /*protect=*/2, /*want_type=*/false,
14481 tf_warning_or_error);
14482 member_end = lookup_member (TREE_TYPE (range), id_end,
14483 /*protect=*/2, /*want_type=*/false,
14484 tf_warning_or_error);
14486 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14488 /* Use the member functions. */
14489 *begin = cp_parser_range_for_member_function (range, id_begin);
14490 *end = cp_parser_range_for_member_function (range, id_end);
14492 else
14494 /* Use global functions with ADL. */
14495 releasing_vec vec;
14497 vec_safe_push (vec, range);
14499 member_begin = perform_koenig_lookup (id_begin, vec,
14500 tf_warning_or_error);
14501 *begin = finish_call_expr (member_begin, &vec, false, true,
14502 tf_warning_or_error);
14503 member_end = perform_koenig_lookup (id_end, vec,
14504 tf_warning_or_error);
14505 *end = finish_call_expr (member_end, &vec, false, true,
14506 tf_warning_or_error);
14509 /* Last common checks. */
14510 if (*begin == error_mark_node || *end == error_mark_node)
14512 /* If one of the expressions is an error do no more checks. */
14513 *begin = *end = error_mark_node;
14514 return error_mark_node;
14516 else if (type_dependent_expression_p (*begin)
14517 || type_dependent_expression_p (*end))
14518 /* Can happen, when, eg, in a template context, Koenig lookup
14519 can't resolve begin/end (c++/58503). */
14520 return NULL_TREE;
14521 else
14523 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14524 /* The unqualified type of the __begin and __end temporaries should
14525 be the same, as required by the multiple auto declaration. */
14526 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14528 if (cxx_dialect >= cxx17
14529 && (build_x_binary_op (input_location, NE_EXPR,
14530 *begin, ERROR_MARK,
14531 *end, ERROR_MARK,
14532 NULL_TREE, NULL, tf_none)
14533 != error_mark_node))
14534 /* P0184R0 allows __begin and __end to have different types,
14535 but make sure they are comparable so we can give a better
14536 diagnostic. */;
14537 else
14538 error ("inconsistent begin/end types in range-based %<for%> "
14539 "statement: %qT and %qT",
14540 TREE_TYPE (*begin), TREE_TYPE (*end));
14542 return iter_type;
14547 /* Helper function for cp_parser_perform_range_for_lookup.
14548 Builds a tree for RANGE.IDENTIFIER(). */
14550 static tree
14551 cp_parser_range_for_member_function (tree range, tree identifier)
14553 tree member, res;
14555 member = finish_class_member_access_expr (range, identifier,
14556 false, tf_warning_or_error);
14557 if (member == error_mark_node)
14558 return error_mark_node;
14560 releasing_vec vec;
14561 res = finish_call_expr (member, &vec,
14562 /*disallow_virtual=*/false,
14563 /*koenig_p=*/false,
14564 tf_warning_or_error);
14565 return res;
14568 /* Parse an iteration-statement.
14570 iteration-statement:
14571 while ( condition ) statement
14572 do statement while ( expression ) ;
14573 for ( init-statement condition [opt] ; expression [opt] )
14574 statement
14576 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14578 static tree
14579 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14580 tree unroll, bool novector)
14582 cp_token *token;
14583 enum rid keyword;
14584 tree statement;
14585 unsigned char in_statement;
14586 token_indent_info guard_tinfo;
14588 /* Peek at the next token. */
14589 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14590 if (!token)
14591 return error_mark_node;
14593 guard_tinfo = get_token_indent_info (token);
14595 /* Remember whether or not we are already within an iteration
14596 statement. */
14597 in_statement = parser->in_statement;
14599 /* Special case for OMP loop intervening code. Parsing of permitted
14600 collapsed loop nests is handled elsewhere. */
14601 if (parser->omp_for_parse_state)
14603 error_at (token->location,
14604 "loop not permitted in intervening code in OpenMP loop body");
14605 parser->omp_for_parse_state->fail = true;
14608 /* See what kind of keyword it is. */
14609 keyword = token->keyword;
14610 switch (keyword)
14612 case RID_WHILE:
14614 tree condition;
14616 /* Begin the while-statement. */
14617 statement = begin_while_stmt ();
14618 /* Look for the `('. */
14619 matching_parens parens;
14620 parens.require_open (parser);
14621 /* Parse the condition. */
14622 condition = cp_parser_condition (parser);
14623 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14624 /* Look for the `)'. */
14625 parens.require_close (parser);
14626 /* Parse the dependent statement. */
14627 parser->in_statement = IN_ITERATION_STMT;
14628 bool prev = note_iteration_stmt_body_start ();
14629 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14630 note_iteration_stmt_body_end (prev);
14631 parser->in_statement = in_statement;
14632 /* We're done with the while-statement. */
14633 finish_while_stmt (statement);
14635 break;
14637 case RID_DO:
14639 tree expression;
14641 /* Begin the do-statement. */
14642 statement = begin_do_stmt ();
14643 /* Parse the body of the do-statement. */
14644 parser->in_statement = IN_ITERATION_STMT;
14645 bool prev = note_iteration_stmt_body_start ();
14646 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14647 note_iteration_stmt_body_end (prev);
14648 parser->in_statement = in_statement;
14649 finish_do_body (statement);
14650 /* Look for the `while' keyword. */
14651 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14652 /* Look for the `('. */
14653 matching_parens parens;
14654 parens.require_open (parser);
14655 /* Parse the expression. */
14656 expression = cp_parser_expression (parser);
14657 /* We're done with the do-statement. */
14658 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14659 /* Look for the `)'. */
14660 parens.require_close (parser);
14661 /* Look for the `;'. */
14662 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14664 break;
14666 case RID_FOR:
14668 /* Look for the `('. */
14669 matching_parens parens;
14670 parens.require_open (parser);
14672 statement = cp_parser_for (parser, ivdep, unroll, novector);
14674 /* Look for the `)'. */
14675 parens.require_close (parser);
14677 /* Parse the body of the for-statement. */
14678 parser->in_statement = IN_ITERATION_STMT;
14679 bool prev = note_iteration_stmt_body_start ();
14680 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14681 note_iteration_stmt_body_end (prev);
14682 parser->in_statement = in_statement;
14684 /* We're done with the for-statement. */
14685 finish_for_stmt (statement);
14687 break;
14689 default:
14690 cp_parser_error (parser, "expected iteration-statement");
14691 statement = error_mark_node;
14692 break;
14695 return statement;
14698 /* Parse an init-statement or the declarator of a range-based-for.
14699 Returns true if a range-based-for declaration is seen.
14701 init-statement:
14702 expression-statement
14703 simple-declaration
14704 alias-declaration */
14706 static bool
14707 cp_parser_init_statement (cp_parser *parser, tree *decl)
14709 /* If the next token is a `;', then we have an empty
14710 expression-statement. Grammatically, this is also a
14711 simple-declaration, but an invalid one, because it does not
14712 declare anything. Therefore, if we did not handle this case
14713 specially, we would issue an error message about an invalid
14714 declaration. */
14715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14717 bool is_range_for = false;
14718 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14720 /* A colon is used in range-based for. */
14721 parser->colon_corrects_to_scope_p = false;
14723 /* We're going to speculatively look for a declaration, falling back
14724 to an expression, if necessary. */
14725 cp_parser_parse_tentatively (parser);
14726 bool expect_semicolon_p = true;
14727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14729 cp_parser_alias_declaration (parser);
14730 expect_semicolon_p = false;
14731 if (cxx_dialect < cxx23
14732 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14733 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14734 OPT_Wc__23_extensions,
14735 "alias-declaration in init-statement only "
14736 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14738 else
14739 /* Parse the declaration. */
14740 cp_parser_simple_declaration (parser,
14741 /*function_definition_allowed_p=*/false,
14742 decl);
14743 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14744 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14746 /* It is a range-for, consume the ':'. */
14747 cp_lexer_consume_token (parser->lexer);
14748 is_range_for = true;
14749 if (cxx_dialect < cxx11)
14750 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14751 OPT_Wc__11_extensions,
14752 "range-based %<for%> loops only available with "
14753 "%<-std=c++11%> or %<-std=gnu++11%>");
14755 else if (expect_semicolon_p)
14756 /* The ';' is not consumed yet because we told
14757 cp_parser_simple_declaration not to. */
14758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14760 if (cp_parser_parse_definitely (parser))
14761 return is_range_for;
14762 /* If the tentative parse failed, then we shall need to look for an
14763 expression-statement. */
14765 /* If we are here, it is an expression-statement. */
14766 cp_parser_expression_statement (parser, NULL_TREE);
14767 return false;
14770 /* Parse a jump-statement.
14772 jump-statement:
14773 break ;
14774 continue ;
14775 return expression [opt] ;
14776 return braced-init-list ;
14777 coroutine-return-statement;
14778 goto identifier ;
14780 GNU extension:
14782 jump-statement:
14783 goto * expression ;
14785 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14787 static tree
14788 cp_parser_jump_statement (cp_parser* parser)
14790 tree statement = error_mark_node;
14791 cp_token *token;
14792 enum rid keyword;
14793 unsigned char in_statement;
14795 /* Peek at the next token. */
14796 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14797 if (!token)
14798 return error_mark_node;
14800 /* See what kind of keyword it is. */
14801 keyword = token->keyword;
14802 switch (keyword)
14804 case RID_BREAK:
14805 in_statement = parser->in_statement & ~IN_IF_STMT;
14806 switch (in_statement)
14808 case 0:
14809 error_at (token->location, "break statement not within loop or switch");
14810 break;
14811 default:
14812 gcc_assert ((in_statement & IN_SWITCH_STMT)
14813 || in_statement == IN_ITERATION_STMT);
14814 statement = finish_break_stmt ();
14815 if (in_statement == IN_ITERATION_STMT)
14816 break_maybe_infinite_loop ();
14817 break;
14818 case IN_OMP_BLOCK:
14819 error_at (token->location, "invalid exit from OpenMP structured block");
14820 break;
14821 case IN_OMP_FOR:
14822 error_at (token->location, "break statement used with OpenMP for loop");
14823 break;
14825 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14826 break;
14828 case RID_CONTINUE:
14829 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14831 case 0:
14832 error_at (token->location, "continue statement not within a loop");
14833 break;
14834 /* Fall through. */
14835 case IN_ITERATION_STMT:
14836 case IN_OMP_FOR:
14837 statement = finish_continue_stmt ();
14838 break;
14839 case IN_OMP_BLOCK:
14840 error_at (token->location, "invalid exit from OpenMP structured block");
14841 break;
14842 default:
14843 gcc_unreachable ();
14845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14846 break;
14848 case RID_CO_RETURN:
14849 case RID_RETURN:
14851 tree expr;
14853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14855 cp_lexer_set_source_position (parser->lexer);
14856 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14857 expr = cp_parser_braced_list (parser);
14859 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14860 expr = cp_parser_expression (parser);
14861 else
14862 /* If the next token is a `;', then there is no
14863 expression. */
14864 expr = NULL_TREE;
14865 /* Build the return-statement, check co-return first, since type
14866 deduction is not valid there. */
14867 if (keyword == RID_CO_RETURN)
14868 statement = finish_co_return_stmt (token->location, expr);
14869 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14870 /* Don't deduce from a discarded return statement. */;
14871 else
14872 statement = finish_return_stmt (expr);
14873 /* Look for the final `;'. */
14874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14876 break;
14878 case RID_GOTO:
14879 if (parser->in_function_body
14880 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14881 && cxx_dialect < cxx23)
14883 error ("%<goto%> in %<constexpr%> function only available with "
14884 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14885 cp_function_chain->invalid_constexpr = true;
14888 /* Create the goto-statement. */
14889 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14891 /* Issue a warning about this use of a GNU extension. */
14892 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14893 /* Consume the '*' token. */
14894 cp_lexer_consume_token (parser->lexer);
14895 /* Parse the dependent expression. */
14896 finish_goto_stmt (cp_parser_expression (parser));
14898 else
14899 finish_goto_stmt (cp_parser_identifier (parser));
14900 /* Look for the final `;'. */
14901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14902 break;
14904 default:
14905 cp_parser_error (parser, "expected jump-statement");
14906 break;
14909 return statement;
14912 /* Parse a declaration-statement.
14914 declaration-statement:
14915 block-declaration */
14917 static void
14918 cp_parser_declaration_statement (cp_parser* parser)
14920 void *p;
14922 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14923 p = obstack_alloc (&declarator_obstack, 0);
14925 /* Parse the block-declaration. */
14926 cp_parser_block_declaration (parser, /*statement_p=*/true);
14928 /* Free any declarators allocated. */
14929 obstack_free (&declarator_obstack, p);
14932 /* Some dependent statements (like `if (cond) statement'), are
14933 implicitly in their own scope. In other words, if the statement is
14934 a single statement (as opposed to a compound-statement), it is
14935 none-the-less treated as if it were enclosed in braces. Any
14936 declarations appearing in the dependent statement are out of scope
14937 after control passes that point. This function parses a statement,
14938 but ensures that is in its own scope, even if it is not a
14939 compound-statement.
14941 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14942 is a (possibly labeled) if statement which is not enclosed in
14943 braces and has an else clause. This is used to implement
14944 -Wparentheses.
14946 CHAIN is a vector of if-else-if conditions. This is used to implement
14947 -Wduplicated-cond.
14949 Returns the new statement. */
14951 static tree
14952 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14953 const token_indent_info &guard_tinfo,
14954 vec<tree> *chain)
14956 tree statement;
14957 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14958 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14959 token_indent_info body_tinfo
14960 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14962 if (if_p != NULL)
14963 *if_p = false;
14965 /* Mark if () ; with a special NOP_EXPR. */
14966 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14968 cp_lexer_consume_token (parser->lexer);
14969 statement = add_stmt (build_empty_stmt (body_loc));
14971 if (guard_tinfo.keyword == RID_IF
14972 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14973 warning_at (body_loc, OPT_Wempty_body,
14974 "suggest braces around empty body in an %<if%> statement");
14975 else if (guard_tinfo.keyword == RID_ELSE)
14976 warning_at (body_loc, OPT_Wempty_body,
14977 "suggest braces around empty body in an %<else%> statement");
14979 /* if a compound is opened, we simply parse the statement directly. */
14980 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14981 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14982 /* If the token is not a `{', then we must take special action. */
14983 else
14985 /* Create a compound-statement. */
14986 statement = begin_compound_stmt (0);
14987 /* Parse the dependent-statement. */
14988 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14989 &body_loc_after_labels);
14990 /* Finish the dummy compound-statement. */
14991 finish_compound_stmt (statement);
14994 token_indent_info next_tinfo
14995 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14996 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14998 if (body_loc_after_labels != UNKNOWN_LOCATION
14999 && next_tinfo.type != CPP_SEMICOLON)
15000 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
15001 guard_tinfo.location, guard_tinfo.keyword);
15003 /* Return the statement. */
15004 return statement;
15007 /* For some dependent statements (like `while (cond) statement'), we
15008 have already created a scope. Therefore, even if the dependent
15009 statement is a compound-statement, we do not want to create another
15010 scope. */
15012 static void
15013 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
15014 const token_indent_info &guard_tinfo)
15016 /* If the token is a `{', then we must take special action. */
15017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15019 token_indent_info body_tinfo
15020 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15021 location_t loc_after_labels = UNKNOWN_LOCATION;
15023 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
15024 &loc_after_labels);
15025 token_indent_info next_tinfo
15026 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15027 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15029 if (loc_after_labels != UNKNOWN_LOCATION
15030 && next_tinfo.type != CPP_SEMICOLON)
15031 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
15032 guard_tinfo.location,
15033 guard_tinfo.keyword);
15035 else
15037 /* Avoid calling cp_parser_compound_statement, so that we
15038 don't create a new scope. Do everything else by hand. */
15039 matching_braces braces;
15040 braces.require_open (parser);
15041 /* If the next keyword is `__label__' we have a label declaration. */
15042 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
15043 cp_parser_label_declaration (parser);
15044 /* Parse an (optional) statement-seq. */
15045 cp_parser_statement_seq_opt (parser, NULL_TREE);
15046 braces.require_close (parser);
15050 /* Modules */
15052 /* Parse a module-name or module-partition.
15054 module-name:
15055 module-name-qualifier [opt] identifier
15057 module-partition:
15058 : module-name-qualifier [opt] identifier
15060 module-name-qualifier:
15061 identifier .
15062 module-name-qualifier identifier .
15064 Returns a pointer to the module object, or NULL on failure.
15065 For PARTITION_P, PARENT is the module this is a partition of. */
15067 static module_state *
15068 cp_parser_module_name (cp_parser *parser, bool partition_p = false,
15069 module_state *parent = NULL)
15071 if (partition_p
15072 && cp_lexer_consume_token (parser->lexer)->type != CPP_COLON)
15073 return NULL;
15075 for (;;)
15077 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
15079 if (partition_p)
15080 cp_parser_error (parser, "expected module-partition");
15081 else
15082 cp_parser_error (parser, "expected module-name");
15083 return NULL;
15086 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
15087 parent = get_module (name, parent, partition_p);
15088 if (cp_lexer_peek_token (parser->lexer)->type != CPP_DOT)
15089 break;
15091 cp_lexer_consume_token (parser->lexer);
15094 return parent;
15097 /* Parse a module-partition. Defers to cp_parser_module_name. */
15099 static module_state *
15100 cp_parser_module_partition (cp_parser *parser, module_state *parent = NULL)
15102 return cp_parser_module_name (parser, /*partition_p=*/true, parent);
15105 /* Named module-declaration
15106 __module ; PRAGMA_EOL
15107 __module : private ; PRAGMA_EOL (unimplemented)
15108 [__export] __module module-name module-partition [opt]
15109 attr-spec-seq-opt ; PRAGMA_EOL
15112 static module_parse
15113 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
15114 bool exporting)
15116 /* We're a pseudo pragma. */
15117 parser->lexer->in_pragma = true;
15118 cp_token *token = cp_lexer_consume_token (parser->lexer);
15120 if (flag_header_unit)
15122 error_at (token->location,
15123 "module-declaration not permitted in header-unit");
15124 goto skip_eol;
15126 else if (mp_state == MP_FIRST && !exporting
15127 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15129 /* Start global module fragment. */
15130 cp_lexer_consume_token (parser->lexer);
15131 module_kind = MK_NAMED;
15132 mp_state = MP_GLOBAL;
15133 cp_parser_require_pragma_eol (parser, token);
15135 else if (!exporting
15136 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15137 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
15138 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
15140 cp_lexer_consume_token (parser->lexer);
15141 cp_lexer_consume_token (parser->lexer);
15142 cp_lexer_consume_token (parser->lexer);
15143 cp_parser_require_pragma_eol (parser, token);
15145 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
15146 && module_has_cmi_p ())
15148 mp_state = MP_PRIVATE_IMPORTS;
15149 sorry_at (token->location, "private module fragment");
15151 else
15152 error_at (token->location,
15153 "private module fragment only permitted in purview"
15154 " of module interface or partition");
15156 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
15158 /* Neither the first declaration, nor in a GMF. */
15159 error_at (token->location, "module-declaration only permitted as first"
15160 " declaration, or ending a global module fragment");
15161 skip_eol:
15162 cp_parser_skip_to_pragma_eol (parser, token);
15164 else
15166 module_state *mod = cp_parser_module_name (parser);
15167 if (mod && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
15168 mod = cp_parser_module_partition (parser, mod);
15169 tree attrs = cp_parser_attributes_opt (parser);
15171 if (mod)
15172 mp_state = MP_PURVIEW_IMPORTS;
15173 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15174 goto skip_eol;
15176 declare_module (mod, token->location, exporting, attrs, parse_in);
15177 cp_parser_require_pragma_eol (parser, token);
15180 return mp_state;
15183 /* Import-declaration
15184 __import module-name attr-spec-seq-opt ; PRAGMA_EOL
15185 __import module-partition attr-spec-seq-opt ; PRAGMA_EOL
15186 __import header-name attr-spec-seq-opt ; PRAGMA_EOL
15189 static void
15190 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
15191 bool exporting)
15193 /* We're a pseudo pragma. */
15194 parser->lexer->in_pragma = true;
15195 cp_token *token = cp_lexer_consume_token (parser->lexer);
15197 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
15199 error_at (token->location, "post-module-declaration"
15200 " imports must be contiguous");
15201 note_lexer:
15202 inform (token->location, "perhaps insert a line break, or other"
15203 " disambiguation, to prevent this being considered a"
15204 " module control-line");
15205 skip_eol:
15206 cp_parser_skip_to_pragma_eol (parser, token);
15208 else if (current_scope () != global_namespace)
15210 error_at (token->location, "import-declaration must be at global scope");
15211 goto note_lexer;
15213 else
15215 module_state *mod = NULL;
15216 cp_token *next = cp_lexer_peek_token (parser->lexer);
15217 if (next->type == CPP_HEADER_NAME)
15219 cp_lexer_consume_token (parser->lexer);
15220 mod = get_module (next->u.value);
15222 else if (next->type == CPP_COLON)
15224 /* An import specifying a module-partition shall only appear after the
15225 module-declaration in a module unit: [module.import]/4. */
15226 if (named_module_p ()
15227 && (mp_state == MP_PURVIEW_IMPORTS
15228 || mp_state == MP_PRIVATE_IMPORTS))
15229 mod = cp_parser_module_partition (parser);
15230 else
15231 error_at (next->location, "import specifying a module-partition"
15232 " must appear after a named module-declaration");
15234 else
15235 mod = cp_parser_module_name (parser);
15236 tree attrs = cp_parser_attributes_opt (parser);
15238 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15239 goto skip_eol;
15240 cp_parser_require_pragma_eol (parser, token);
15242 if (parser->in_unbraced_linkage_specification_p)
15243 error_at (token->location, "import cannot appear directly in"
15244 " a linkage-specification");
15246 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15248 /* Module-purview imports must not be from source inclusion
15249 [cpp.import]/7 */
15250 if (attrs
15251 && private_lookup_attribute ("__translated",
15252 strlen ("__translated"), attrs))
15253 error_at (token->location, "post-module-declaration imports"
15254 " must not be include-translated");
15255 else if (!token->main_source_p)
15256 error_at (token->location, "post-module-declaration imports"
15257 " must not be from header inclusion");
15260 import_module (mod, token->location, exporting, attrs, parse_in);
15264 /* export-declaration.
15266 export declaration
15267 export { declaration-seq-opt } */
15269 static void
15270 cp_parser_module_export (cp_parser *parser)
15272 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15273 cp_token *token = cp_lexer_consume_token (parser->lexer);
15275 if (!module_interface_p ())
15276 error_at (token->location,
15277 "%qE may only occur after a module interface declaration",
15278 token->u.value);
15280 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15282 unsigned mk = module_kind;
15283 if (module_exporting_p ())
15284 error_at (token->location,
15285 "%qE may only occur once in an export declaration",
15286 token->u.value);
15287 module_kind |= MK_EXPORTING;
15289 if (braced)
15291 cp_ensure_no_omp_declare_simd (parser);
15292 cp_ensure_no_oacc_routine (parser);
15294 cp_lexer_consume_token (parser->lexer);
15295 cp_parser_declaration_seq_opt (parser);
15296 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15298 else
15300 /* Explicitly check if the next tokens might be a
15301 module-directive line, so we can give a clearer error message
15302 about why the directive will be rejected. */
15303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15304 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15305 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15306 error_at (token->location, "%<export%> not part of following"
15307 " module-directive");
15308 cp_parser_declaration (parser, NULL_TREE);
15311 module_kind = mk;
15314 /* Declarations [gram.dcl.dcl] */
15316 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15317 is the top-level declaration sequence. That affects whether we
15318 deal with module-preamble.
15320 declaration-seq:
15321 declaration
15322 declaration-seq declaration */
15324 static void
15325 cp_parser_declaration_seq_opt (cp_parser* parser)
15327 while (true)
15329 cp_token *token = cp_lexer_peek_token (parser->lexer);
15331 if (token->type == CPP_CLOSE_BRACE
15332 || token->type == CPP_EOF)
15333 break;
15334 else
15335 cp_parser_toplevel_declaration (parser);
15339 /* Parse a declaration.
15341 declaration:
15342 block-declaration
15343 function-definition
15344 template-declaration
15345 explicit-instantiation
15346 explicit-specialization
15347 linkage-specification
15348 namespace-definition
15350 C++17:
15351 deduction-guide
15353 modules:
15354 (all these are only allowed at the outermost level, check
15355 that semantically, for better diagnostics)
15356 module-declaration
15357 module-export-declaration
15358 module-import-declaration
15359 export-declaration
15361 GNU extension:
15363 declaration:
15364 __extension__ declaration */
15366 static void
15367 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15369 int saved_pedantic;
15371 /* Check for the `__extension__' keyword. */
15372 if (cp_parser_extension_opt (parser, &saved_pedantic))
15374 /* Parse the qualified declaration. */
15375 cp_parser_declaration (parser, prefix_attrs);
15376 /* Restore the PEDANTIC flag. */
15377 pedantic = saved_pedantic;
15379 return;
15382 /* Try to figure out what kind of declaration is present. */
15383 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15384 cp_token *token2 = (token1->type == CPP_EOF
15385 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15387 if (token1->type == CPP_SEMICOLON)
15389 cp_lexer_consume_token (parser->lexer);
15390 /* A declaration consisting of a single semicolon is invalid
15391 * before C++11. Allow it unless we're being pedantic. */
15392 if (cxx_dialect < cxx11)
15393 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15394 return;
15396 else if (cp_lexer_nth_token_is (parser->lexer,
15397 cp_parser_skip_std_attribute_spec_seq (parser,
15399 CPP_SEMICOLON))
15401 location_t attrs_loc = token1->location;
15402 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15404 if (std_attrs && (flag_openmp || flag_openmp_simd))
15406 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15407 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15408 std_attrs);
15409 if (parser->lexer->in_omp_attribute_pragma)
15411 cp_lexer *lexer = parser->lexer;
15412 while (parser->lexer->in_omp_attribute_pragma)
15414 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15415 CPP_PRAGMA));
15416 cp_parser_pragma (parser, pragma_external, NULL);
15418 cp_lexer_destroy (lexer);
15422 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
15423 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15424 OPT_Wattributes, "attribute ignored");
15425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15426 cp_lexer_consume_token (parser->lexer);
15427 return;
15430 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15431 void *p = obstack_alloc (&declarator_obstack, 0);
15433 tree attributes = NULL_TREE;
15435 /* Conditionally, allow attributes to precede a linkage specification. */
15436 if (token1->keyword == RID_ATTRIBUTE)
15438 cp_lexer_save_tokens (parser->lexer);
15439 attributes = cp_parser_attributes_opt (parser);
15440 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15441 cp_token *t2 = (t1->type == CPP_EOF
15442 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15443 if (t1->keyword == RID_EXTERN
15444 && cp_parser_is_pure_string_literal (t2))
15446 cp_lexer_commit_tokens (parser->lexer);
15447 /* We might have already been here. */
15448 if (!c_dialect_objc ())
15450 location_t where = get_finish (t2->location);
15451 warning_at (token1->location, OPT_Wattributes, "attributes are"
15452 " not permitted in this position");
15453 where = linemap_position_for_loc_and_offset (line_table,
15454 where, 1);
15455 inform (where, "attributes may be inserted here");
15456 attributes = NULL_TREE;
15458 token1 = t1;
15459 token2 = t2;
15461 else
15463 cp_lexer_rollback_tokens (parser->lexer);
15464 attributes = NULL_TREE;
15467 /* If we already had some attributes, and we've added more, then prepend.
15468 Otherwise attributes just contains any that we just read. */
15469 if (prefix_attrs)
15471 if (attributes)
15472 TREE_CHAIN (prefix_attrs) = attributes;
15473 attributes = prefix_attrs;
15476 /* If the next token is `extern' and the following token is a string
15477 literal, then we have a linkage specification. */
15478 if (token1->keyword == RID_EXTERN
15479 && cp_parser_is_pure_string_literal (token2))
15480 cp_parser_linkage_specification (parser, attributes);
15481 /* If the next token is `template', then we have either a template
15482 declaration, an explicit instantiation, or an explicit
15483 specialization. */
15484 else if (token1->keyword == RID_TEMPLATE)
15486 /* `template <>' indicates a template specialization. */
15487 if (token2->type == CPP_LESS
15488 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15489 cp_parser_explicit_specialization (parser);
15490 /* `template <' indicates a template declaration. */
15491 else if (token2->type == CPP_LESS)
15492 cp_parser_template_declaration (parser, /*member_p=*/false);
15493 /* Anything else must be an explicit instantiation. */
15494 else
15495 cp_parser_explicit_instantiation (parser);
15497 /* If the next token is `export', it's new-style modules or
15498 old-style template. */
15499 else if (token1->keyword == RID_EXPORT)
15501 if (!modules_p ())
15502 cp_parser_template_declaration (parser, /*member_p=*/false);
15503 else
15504 cp_parser_module_export (parser);
15506 else if (cp_token_is_module_directive (token1))
15508 bool exporting = token1->keyword == RID__EXPORT;
15509 cp_token *next = exporting ? token2 : token1;
15510 if (exporting)
15511 cp_lexer_consume_token (parser->lexer);
15512 // In module purview this will be ill-formed.
15513 auto state = (!named_module_p () ? MP_NOT_MODULE
15514 : module_purview_p () ? MP_PURVIEW
15515 : MP_GLOBAL);
15516 if (next->keyword == RID__MODULE)
15517 cp_parser_module_declaration (parser, state, exporting);
15518 else
15519 cp_parser_import_declaration (parser, state, exporting);
15521 /* If the next token is `extern', 'static' or 'inline' and the one
15522 after that is `template', we have a GNU extended explicit
15523 instantiation directive. */
15524 else if (cp_parser_allow_gnu_extensions_p (parser)
15525 && token2->keyword == RID_TEMPLATE
15526 && (token1->keyword == RID_EXTERN
15527 || token1->keyword == RID_STATIC
15528 || token1->keyword == RID_INLINE))
15529 cp_parser_explicit_instantiation (parser);
15530 /* If the next token is `namespace', check for a named or unnamed
15531 namespace definition. */
15532 else if (token1->keyword == RID_NAMESPACE
15533 && (/* A named namespace definition. */
15534 (token2->type == CPP_NAME
15535 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15536 != CPP_EQ))
15537 || (token2->type == CPP_OPEN_SQUARE
15538 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15539 == CPP_OPEN_SQUARE)
15540 /* An unnamed namespace definition. */
15541 || token2->type == CPP_OPEN_BRACE
15542 || token2->keyword == RID_ATTRIBUTE))
15543 cp_parser_namespace_definition (parser);
15544 /* An inline (associated) namespace definition. */
15545 else if (token2->keyword == RID_NAMESPACE
15546 && token1->keyword == RID_INLINE)
15547 cp_parser_namespace_definition (parser);
15548 /* Objective-C++ declaration/definition. */
15549 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15550 cp_parser_objc_declaration (parser, attributes);
15551 else if (c_dialect_objc ()
15552 && token1->keyword == RID_ATTRIBUTE
15553 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15554 cp_parser_objc_declaration (parser, attributes);
15555 /* At this point we may have a template declared by a concept
15556 introduction. */
15557 else if (flag_concepts
15558 && cp_parser_template_declaration_after_export (parser,
15559 /*member_p=*/false))
15560 /* We did. */;
15561 else
15562 /* Try to parse a block-declaration, or a function-definition. */
15563 cp_parser_block_declaration (parser, /*statement_p=*/false);
15565 /* Free any declarators allocated. */
15566 obstack_free (&declarator_obstack, p);
15569 /* Parse a namespace-scope declaration. */
15571 static void
15572 cp_parser_toplevel_declaration (cp_parser* parser)
15574 cp_token *token = cp_lexer_peek_token (parser->lexer);
15576 if (token->type == CPP_PRAGMA)
15577 /* A top-level declaration can consist solely of a #pragma. A
15578 nested declaration cannot, so this is done here and not in
15579 cp_parser_declaration. (A #pragma at block scope is
15580 handled in cp_parser_statement.) */
15581 cp_parser_pragma (parser, pragma_external, NULL);
15582 else
15583 /* Parse the declaration itself. */
15584 cp_parser_declaration (parser, NULL_TREE);
15587 /* Parse a block-declaration.
15589 block-declaration:
15590 simple-declaration
15591 asm-definition
15592 namespace-alias-definition
15593 using-declaration
15594 using-directive
15596 GNU Extension:
15598 block-declaration:
15599 __extension__ block-declaration
15601 C++0x Extension:
15603 block-declaration:
15604 static_assert-declaration
15606 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15607 part of a declaration-statement. */
15609 static void
15610 cp_parser_block_declaration (cp_parser *parser,
15611 bool statement_p)
15613 int saved_pedantic;
15615 /* Check for the `__extension__' keyword. */
15616 if (cp_parser_extension_opt (parser, &saved_pedantic))
15618 /* Parse the qualified declaration. */
15619 cp_parser_block_declaration (parser, statement_p);
15620 /* Restore the PEDANTIC flag. */
15621 pedantic = saved_pedantic;
15623 return;
15626 /* Peek at the next token to figure out which kind of declaration is
15627 present. */
15628 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15630 /* If the next keyword is `asm', we have an asm-definition. */
15631 if (token1->keyword == RID_ASM)
15633 if (statement_p)
15634 cp_parser_commit_to_tentative_parse (parser);
15635 cp_parser_asm_definition (parser);
15637 /* If the next keyword is `namespace', we have a
15638 namespace-alias-definition. */
15639 else if (token1->keyword == RID_NAMESPACE)
15640 cp_parser_namespace_alias_definition (parser);
15641 /* If the next keyword is `using', we have a
15642 using-declaration, a using-directive, or an alias-declaration. */
15643 else if (token1->keyword == RID_USING)
15645 cp_token *token2;
15647 if (statement_p)
15648 cp_parser_commit_to_tentative_parse (parser);
15649 /* If the token after `using' is `namespace', then we have a
15650 using-directive. */
15651 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15652 if (token2->keyword == RID_NAMESPACE)
15653 cp_parser_using_directive (parser);
15654 else if (token2->keyword == RID_ENUM)
15655 cp_parser_using_enum (parser);
15656 /* If the second token after 'using' is '=', then we have an
15657 alias-declaration. */
15658 else if (cxx_dialect >= cxx11
15659 && token2->type == CPP_NAME
15660 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15661 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15662 cp_parser_alias_declaration (parser);
15663 /* Otherwise, it's a using-declaration. */
15664 else
15665 cp_parser_using_declaration (parser,
15666 /*access_declaration_p=*/false);
15668 /* If the next keyword is `__label__' we have a misplaced label
15669 declaration. */
15670 else if (token1->keyword == RID_LABEL)
15672 cp_lexer_consume_token (parser->lexer);
15673 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15674 cp_parser_skip_to_end_of_statement (parser);
15675 /* If the next token is now a `;', consume it. */
15676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15677 cp_lexer_consume_token (parser->lexer);
15679 /* If the next token is `static_assert' we have a static assertion. */
15680 else if (token1->keyword == RID_STATIC_ASSERT)
15681 cp_parser_static_assert (parser, /*member_p=*/false);
15682 else
15684 size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1);
15685 cp_token *after_attr = NULL;
15686 if (attr_idx != 1)
15687 after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx);
15688 /* If the next tokens after attributes is `using namespace', then we have
15689 a using-directive. */
15690 if (after_attr
15691 && after_attr->keyword == RID_USING
15692 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15693 RID_NAMESPACE))
15695 if (statement_p)
15696 cp_parser_commit_to_tentative_parse (parser);
15697 cp_parser_using_directive (parser);
15699 /* If the next token after attributes is `asm', then we have
15700 an asm-definition. */
15701 else if (after_attr && after_attr->keyword == RID_ASM)
15703 if (statement_p)
15704 cp_parser_commit_to_tentative_parse (parser);
15705 cp_parser_asm_definition (parser);
15707 /* Anything else must be a simple-declaration. */
15708 else
15709 cp_parser_simple_declaration (parser, !statement_p,
15710 /*maybe_range_for_decl*/NULL);
15714 /* Parse a simple-declaration.
15716 simple-declaration:
15717 decl-specifier-seq [opt] init-declarator-list [opt] ;
15718 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15719 brace-or-equal-initializer ;
15721 init-declarator-list:
15722 init-declarator
15723 init-declarator-list , init-declarator
15725 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15726 function-definition as a simple-declaration.
15728 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15729 parsed declaration if it is an uninitialized single declarator not followed
15730 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15731 if present, will not be consumed. */
15733 static void
15734 cp_parser_simple_declaration (cp_parser* parser,
15735 bool function_definition_allowed_p,
15736 tree *maybe_range_for_decl)
15738 cp_decl_specifier_seq decl_specifiers;
15739 int declares_class_or_enum;
15740 bool saw_declarator;
15741 location_t comma_loc = UNKNOWN_LOCATION;
15742 location_t init_loc = UNKNOWN_LOCATION;
15744 if (maybe_range_for_decl)
15745 *maybe_range_for_decl = NULL_TREE;
15747 /* Defer access checks until we know what is being declared; the
15748 checks for names appearing in the decl-specifier-seq should be
15749 done as if we were in the scope of the thing being declared. */
15750 push_deferring_access_checks (dk_deferred);
15752 /* Parse the decl-specifier-seq. We have to keep track of whether
15753 or not the decl-specifier-seq declares a named class or
15754 enumeration type, since that is the only case in which the
15755 init-declarator-list is allowed to be empty.
15757 [dcl.dcl]
15759 In a simple-declaration, the optional init-declarator-list can be
15760 omitted only when declaring a class or enumeration, that is when
15761 the decl-specifier-seq contains either a class-specifier, an
15762 elaborated-type-specifier, or an enum-specifier. */
15763 cp_parser_decl_specifier_seq (parser,
15764 CP_PARSER_FLAGS_OPTIONAL,
15765 &decl_specifiers,
15766 &declares_class_or_enum);
15767 /* We no longer need to defer access checks. */
15768 stop_deferring_access_checks ();
15770 cp_omp_declare_simd_data odsd;
15771 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15772 cp_parser_handle_directive_omp_attributes (parser,
15773 &decl_specifiers.attributes,
15774 &odsd, true);
15776 /* In a block scope, a valid declaration must always have a
15777 decl-specifier-seq. By not trying to parse declarators, we can
15778 resolve the declaration/expression ambiguity more quickly. */
15779 if (!function_definition_allowed_p
15780 && !decl_specifiers.any_specifiers_p)
15782 cp_parser_error (parser, "expected declaration");
15783 goto done;
15786 /* If the next two tokens are both identifiers, the code is
15787 erroneous. The usual cause of this situation is code like:
15789 T t;
15791 where "T" should name a type -- but does not. */
15792 if (!decl_specifiers.any_type_specifiers_p
15793 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15795 /* If parsing tentatively, we should commit; we really are
15796 looking at a declaration. */
15797 cp_parser_commit_to_tentative_parse (parser);
15798 /* Give up. */
15799 goto done;
15802 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15804 /* Look for C++17 decomposition declaration. */
15805 for (size_t n = 1; ; n++)
15806 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15807 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15808 continue;
15809 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15810 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15811 && decl_specifiers.any_specifiers_p)
15813 tree decl
15814 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15815 maybe_range_for_decl,
15816 &init_loc);
15818 /* The next token should be either a `,' or a `;'. */
15819 cp_token *token = cp_lexer_peek_token (parser->lexer);
15820 /* If it's a `;', we are done. */
15821 if (token->type == CPP_SEMICOLON)
15822 goto finish;
15823 else if (maybe_range_for_decl)
15825 if (*maybe_range_for_decl == NULL_TREE)
15826 *maybe_range_for_decl = error_mark_node;
15827 goto finish;
15829 /* Anything else is an error. */
15830 else
15832 /* If we have already issued an error message we don't need
15833 to issue another one. */
15834 if ((decl != error_mark_node
15835 && DECL_INITIAL (decl) != error_mark_node)
15836 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15837 cp_parser_error (parser, "expected %<;%>");
15838 /* Skip tokens until we reach the end of the statement. */
15839 cp_parser_skip_to_end_of_statement (parser);
15840 /* If the next token is now a `;', consume it. */
15841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15842 cp_lexer_consume_token (parser->lexer);
15843 goto done;
15846 else
15847 break;
15849 tree last_type;
15850 bool auto_specifier_p;
15851 /* NULL_TREE if both variable and function declaration are allowed,
15852 error_mark_node if function declaration are not allowed and
15853 a FUNCTION_DECL that should be diagnosed if it is followed by
15854 variable declarations. */
15855 tree auto_function_declaration;
15857 last_type = NULL_TREE;
15858 auto_specifier_p
15859 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15860 auto_function_declaration = NULL_TREE;
15862 /* Keep going until we hit the `;' at the end of the simple
15863 declaration. */
15864 saw_declarator = false;
15865 while (cp_lexer_next_token_is_not (parser->lexer,
15866 CPP_SEMICOLON))
15868 cp_token *token;
15869 bool function_definition_p;
15870 tree decl;
15871 tree auto_result = NULL_TREE;
15873 if (saw_declarator)
15875 /* If we are processing next declarator, comma is expected */
15876 token = cp_lexer_peek_token (parser->lexer);
15877 gcc_assert (token->type == CPP_COMMA);
15878 cp_lexer_consume_token (parser->lexer);
15879 if (maybe_range_for_decl)
15881 *maybe_range_for_decl = error_mark_node;
15882 if (comma_loc == UNKNOWN_LOCATION)
15883 comma_loc = token->location;
15886 else
15887 saw_declarator = true;
15889 /* Parse the init-declarator. */
15890 decl = cp_parser_init_declarator (parser,
15891 CP_PARSER_FLAGS_NONE,
15892 &decl_specifiers,
15893 /*checks=*/NULL,
15894 function_definition_allowed_p,
15895 /*member_p=*/false,
15896 declares_class_or_enum,
15897 &function_definition_p,
15898 maybe_range_for_decl,
15899 &init_loc,
15900 &auto_result);
15901 const bool fndecl_p = TREE_CODE (decl) == FUNCTION_DECL;
15902 /* If an error occurred while parsing tentatively, exit quickly.
15903 (That usually happens when in the body of a function; each
15904 statement is treated as a declaration-statement until proven
15905 otherwise.) */
15906 if (cp_parser_error_occurred (parser))
15907 goto done;
15909 if (auto_specifier_p && cxx_dialect >= cxx14)
15911 /* If the init-declarator-list contains more than one
15912 init-declarator, they shall all form declarations of
15913 variables. */
15914 if (auto_function_declaration == NULL_TREE)
15915 auto_function_declaration = fndecl_p ? decl : error_mark_node;
15916 else if (fndecl_p || auto_function_declaration != error_mark_node)
15918 error_at (decl_specifiers.locations[ds_type_spec],
15919 "non-variable %qD in declaration with more than one "
15920 "declarator with placeholder type",
15921 fndecl_p ? decl : auto_function_declaration);
15922 auto_function_declaration = error_mark_node;
15926 if (auto_result
15927 && (!processing_template_decl || !type_uses_auto (auto_result)))
15929 if (last_type
15930 && last_type != error_mark_node
15931 && !same_type_p (auto_result, last_type))
15933 /* If the list of declarators contains more than one declarator,
15934 the type of each declared variable is determined as described
15935 above. If the type deduced for the template parameter U is not
15936 the same in each deduction, the program is ill-formed. */
15937 error_at (decl_specifiers.locations[ds_type_spec],
15938 "inconsistent deduction for %qT: %qT and then %qT",
15939 decl_specifiers.type, last_type, auto_result);
15940 last_type = error_mark_node;
15942 else
15943 last_type = auto_result;
15946 /* Handle function definitions specially. */
15947 if (function_definition_p)
15949 /* If the next token is a `,', then we are probably
15950 processing something like:
15952 void f() {}, *p;
15954 which is erroneous. */
15955 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15957 cp_token *token = cp_lexer_peek_token (parser->lexer);
15958 error_at (token->location,
15959 "mixing"
15960 " declarations and function-definitions is forbidden");
15962 /* Otherwise, we're done with the list of declarators. */
15963 else
15965 pop_deferring_access_checks ();
15966 cp_finalize_omp_declare_simd (parser, &odsd);
15967 return;
15970 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15971 *maybe_range_for_decl = decl;
15972 /* The next token should be either a `,' or a `;'. */
15973 token = cp_lexer_peek_token (parser->lexer);
15974 /* If it's a `,', there are more declarators to come. */
15975 if (token->type == CPP_COMMA)
15976 /* will be consumed next time around */;
15977 /* If it's a `;', we are done. */
15978 else if (token->type == CPP_SEMICOLON)
15979 break;
15980 else if (maybe_range_for_decl)
15982 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15983 permerror (decl_specifiers.locations[ds_type_spec],
15984 "types may not be defined in a for-range-declaration");
15985 break;
15987 /* Anything else is an error. */
15988 else
15990 /* If we have already issued an error message we don't need
15991 to issue another one. */
15992 if ((decl != error_mark_node
15993 /* grokfndecl sets DECL_INITIAL to error_mark_node for
15994 functions. */
15995 && (fndecl_p || DECL_INITIAL (decl) != error_mark_node))
15996 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15997 cp_parser_error (parser, "expected %<,%> or %<;%>");
15998 /* Skip tokens until we reach the end of the statement. */
15999 cp_parser_skip_to_end_of_statement (parser);
16000 /* If the next token is now a `;', consume it. */
16001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16002 cp_lexer_consume_token (parser->lexer);
16003 goto done;
16005 /* After the first time around, a function-definition is not
16006 allowed -- even if it was OK at first. For example:
16008 int i, f() {}
16010 is not valid. */
16011 function_definition_allowed_p = false;
16014 /* Issue an error message if no declarators are present, and the
16015 decl-specifier-seq does not itself declare a class or
16016 enumeration: [dcl.dcl]/3. */
16017 if (!saw_declarator)
16019 if (cp_parser_declares_only_class_p (parser))
16021 if (!declares_class_or_enum
16022 && decl_specifiers.type
16023 && OVERLOAD_TYPE_P (decl_specifiers.type))
16024 /* Ensure an error is issued anyway when finish_decltype_type,
16025 called via cp_parser_decl_specifier_seq, returns a class or
16026 an enumeration (c++/51786). */
16027 decl_specifiers.type = NULL_TREE;
16028 shadow_tag (&decl_specifiers);
16030 /* Perform any deferred access checks. */
16031 perform_deferred_access_checks (tf_warning_or_error);
16034 /* Consume the `;'. */
16035 finish:
16036 if (!maybe_range_for_decl)
16037 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16038 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16040 if (init_loc != UNKNOWN_LOCATION)
16041 error_at (init_loc, "initializer in range-based %<for%> loop");
16042 if (comma_loc != UNKNOWN_LOCATION)
16043 error_at (comma_loc,
16044 "multiple declarations in range-based %<for%> loop");
16047 done:
16048 pop_deferring_access_checks ();
16049 cp_finalize_omp_declare_simd (parser, &odsd);
16052 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
16053 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
16054 initializer ; */
16056 static tree
16057 cp_parser_decomposition_declaration (cp_parser *parser,
16058 cp_decl_specifier_seq *decl_specifiers,
16059 tree *maybe_range_for_decl,
16060 location_t *init_loc)
16062 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
16063 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16064 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
16066 /* Parse the identifier-list. */
16067 auto_vec<cp_expr, 10> v;
16068 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16069 while (true)
16071 cp_expr e = cp_parser_identifier (parser);
16072 if (e.get_value () == error_mark_node)
16073 break;
16074 v.safe_push (e);
16075 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16076 break;
16077 cp_lexer_consume_token (parser->lexer);
16080 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
16081 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16083 end_loc = UNKNOWN_LOCATION;
16084 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
16085 false);
16086 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16087 cp_lexer_consume_token (parser->lexer);
16088 else
16090 cp_parser_skip_to_end_of_statement (parser);
16091 return error_mark_node;
16095 if (cxx_dialect < cxx17)
16096 pedwarn (loc, OPT_Wc__17_extensions,
16097 "structured bindings only available with "
16098 "%<-std=c++17%> or %<-std=gnu++17%>");
16100 tree pushed_scope;
16101 cp_declarator *declarator = make_declarator (cdk_decomp);
16102 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
16103 declarator->id_loc = loc;
16104 if (ref_qual != REF_QUAL_NONE)
16105 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
16106 ref_qual == REF_QUAL_RVALUE,
16107 NULL_TREE);
16108 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
16109 NULL_TREE, decl_specifiers->attributes,
16110 &pushed_scope);
16111 tree orig_decl = decl;
16113 unsigned int i;
16114 cp_expr e;
16115 cp_decl_specifier_seq decl_specs;
16116 clear_decl_specs (&decl_specs);
16117 decl_specs.type = make_auto ();
16118 if (decl_specifiers->storage_class == sc_static)
16119 decl_specs.storage_class = sc_static;
16120 tree prev = decl;
16121 FOR_EACH_VEC_ELT (v, i, e)
16123 if (i == 0)
16124 declarator = make_id_declarator (NULL_TREE, e.get_value (),
16125 sfk_none, e.get_location ());
16126 else
16128 declarator->u.id.unqualified_name = e.get_value ();
16129 declarator->id_loc = e.get_location ();
16131 tree elt_pushed_scope;
16132 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
16133 NULL_TREE, NULL_TREE, &elt_pushed_scope);
16134 if (decl2 == error_mark_node)
16135 decl = error_mark_node;
16136 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
16138 /* Ensure we've diagnosed redeclaration if we aren't creating
16139 a new VAR_DECL. */
16140 gcc_assert (errorcount);
16141 decl = error_mark_node;
16143 else
16144 prev = decl2;
16145 if (elt_pushed_scope)
16146 pop_scope (elt_pushed_scope);
16149 if (v.is_empty ())
16151 error_at (loc, "empty structured binding declaration");
16152 decl = error_mark_node;
16155 if (maybe_range_for_decl == NULL
16156 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16158 bool non_constant_p = false, is_direct_init = false;
16159 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
16160 tree initializer = cp_parser_initializer (parser, &is_direct_init,
16161 &non_constant_p);
16162 if (initializer == NULL_TREE
16163 || (TREE_CODE (initializer) == TREE_LIST
16164 && TREE_CHAIN (initializer))
16165 || (is_direct_init
16166 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
16167 && CONSTRUCTOR_NELTS (initializer) != 1))
16169 error_at (loc, "invalid initializer for structured binding "
16170 "declaration");
16171 initializer = error_mark_node;
16174 if (decl != error_mark_node)
16176 cp_decomp decomp = { prev, v.length () };
16177 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
16178 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
16179 &decomp);
16180 cp_finish_decomp (decl, &decomp);
16183 else if (decl != error_mark_node)
16185 *maybe_range_for_decl = prev;
16186 cp_decomp decomp = { prev, v.length () };
16187 /* Ensure DECL_VALUE_EXPR is created for all the decls but
16188 the underlying DECL. */
16189 cp_finish_decomp (decl, &decomp);
16192 if (pushed_scope)
16193 pop_scope (pushed_scope);
16195 if (decl == error_mark_node && DECL_P (orig_decl))
16197 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
16198 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
16201 return decl;
16204 /* Names of storage classes. */
16206 static const char *const
16207 cp_storage_class_name[] = {
16208 "", "auto", "register", "static", "extern", "mutable"
16211 /* Parse a decl-specifier-seq.
16213 decl-specifier-seq:
16214 decl-specifier-seq [opt] decl-specifier
16215 decl-specifier attribute-specifier-seq [opt] (C++11)
16217 decl-specifier:
16218 storage-class-specifier
16219 type-specifier
16220 function-specifier
16221 friend
16222 typedef
16224 GNU Extension:
16226 decl-specifier:
16227 attributes
16229 Concepts Extension:
16231 decl-specifier:
16232 concept
16234 Set *DECL_SPECS to a representation of the decl-specifier-seq.
16236 The parser flags FLAGS is used to control type-specifier parsing.
16238 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16239 flags:
16241 1: one of the decl-specifiers is an elaborated-type-specifier
16242 (i.e., a type declaration)
16243 2: one of the decl-specifiers is an enum-specifier or a
16244 class-specifier (i.e., a type definition)
16248 static void
16249 cp_parser_decl_specifier_seq (cp_parser* parser,
16250 cp_parser_flags flags,
16251 cp_decl_specifier_seq *decl_specs,
16252 int* declares_class_or_enum)
16254 bool constructor_possible_p = !parser->in_declarator_p;
16255 bool found_decl_spec = false;
16256 cp_token *start_token = NULL;
16257 cp_decl_spec ds;
16259 /* Clear DECL_SPECS. */
16260 clear_decl_specs (decl_specs);
16262 /* Assume no class or enumeration type is declared. */
16263 *declares_class_or_enum = 0;
16265 /* Keep a token that additionally will be used for diagnostics. */
16266 cp_token *first_specifier = NULL;
16267 /* Keep reading specifiers until there are no more to read. */
16268 while (true)
16270 bool constructor_p;
16271 cp_token *token;
16272 ds = ds_last;
16274 /* Peek at the next token. */
16275 token = cp_lexer_peek_token (parser->lexer);
16277 /* Save the first token of the decl spec list for error
16278 reporting. */
16279 if (!start_token)
16280 start_token = token;
16281 /* Handle attributes. */
16282 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16283 && cp_next_tokens_can_be_attribute_p (parser))
16285 /* Parse the attributes. */
16286 tree attrs = cp_parser_attributes_opt (parser);
16288 /* In a sequence of declaration specifiers, c++11 attributes
16289 appertain to the type that precede them. In that case
16290 [dcl.spec]/1 says:
16292 The attribute-specifier-seq affects the type only for
16293 the declaration it appears in, not other declarations
16294 involving the same type.
16296 But for now let's force the user to position the
16297 attribute either at the beginning of the declaration or
16298 after the declarator-id, which would clearly mean that it
16299 applies to the declarator. */
16300 if (cxx11_attribute_p (attrs))
16302 if (!found_decl_spec)
16303 /* The c++11 attribute is at the beginning of the
16304 declaration. It appertains to the entity being
16305 declared. */;
16306 else
16308 if (find_contract (attrs))
16310 diagnose_misapplied_contracts (attrs);
16311 attrs = NULL_TREE;
16313 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16315 /* This is an attribute following a
16316 class-specifier. */
16317 if (decl_specs->type_definition_p)
16318 warn_misplaced_attr_for_class_type (token->location,
16319 decl_specs->type);
16320 attrs = NULL_TREE;
16322 else
16324 decl_specs->std_attributes
16325 = attr_chainon (decl_specs->std_attributes, attrs);
16326 if (decl_specs->locations[ds_std_attribute] == 0)
16327 decl_specs->locations[ds_std_attribute] = token->location;
16329 continue;
16333 decl_specs->attributes
16334 = attr_chainon (decl_specs->attributes, attrs);
16335 if (decl_specs->locations[ds_attribute] == 0)
16336 decl_specs->locations[ds_attribute] = token->location;
16337 continue;
16339 /* We know by this point that the token is not part of an attribute. */
16340 if (!first_specifier)
16341 first_specifier = token;
16342 /* Special case for "this" specifier, indicating a parm is an xobj parm.
16343 The "this" specifier must be the first specifier in the declaration,
16344 after any attributes. */
16345 if (token->keyword == RID_THIS)
16347 cp_lexer_consume_token (parser->lexer);
16348 if (token != first_specifier)
16350 /* Don't emit diagnostics if we have already seen "this",
16351 leave it for set_and_check_decl_spec_loc. */
16352 if (decl_specs->locations[ds_this] == 0)
16354 auto_diagnostic_group d;
16355 gcc_rich_location richloc (token->location);
16356 /* Works, need to add tests for it though. */
16357 richloc.add_fixit_remove ();
16358 richloc.add_fixit_insert_before (first_specifier->location,
16359 "this ");
16360 error_at (&richloc,
16361 "%<this%> must be the first specifier "
16362 "in a parameter declaration");
16365 set_and_check_decl_spec_loc (decl_specs, ds_this, token);
16366 continue;
16369 /* Assume we will find a decl-specifier keyword. */
16370 found_decl_spec = true;
16371 /* If the next token is an appropriate keyword, we can simply
16372 add it to the list. */
16373 switch (token->keyword)
16375 /* decl-specifier:
16376 friend
16377 constexpr
16378 constinit */
16379 case RID_FRIEND:
16380 if (!at_class_scope_p ())
16382 gcc_rich_location richloc (token->location);
16383 richloc.add_fixit_remove ();
16384 error_at (&richloc, "%<friend%> used outside of class");
16385 cp_lexer_purge_token (parser->lexer);
16387 else
16389 ds = ds_friend;
16390 /* Consume the token. */
16391 cp_lexer_consume_token (parser->lexer);
16393 break;
16395 case RID_CONSTEXPR:
16396 ds = ds_constexpr;
16397 cp_lexer_consume_token (parser->lexer);
16398 break;
16400 case RID_CONSTINIT:
16401 ds = ds_constinit;
16402 cp_lexer_consume_token (parser->lexer);
16403 break;
16405 case RID_CONSTEVAL:
16406 ds = ds_consteval;
16407 cp_lexer_consume_token (parser->lexer);
16408 break;
16410 case RID_CONCEPT:
16411 ds = ds_concept;
16412 cp_lexer_consume_token (parser->lexer);
16414 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16415 break;
16417 /* Warn for concept as a decl-specifier. We'll rewrite these as
16418 concept declarations later. */
16419 if (!flag_concepts_ts)
16421 cp_token *next = cp_lexer_peek_token (parser->lexer);
16422 if (next->keyword == RID_BOOL)
16423 permerror (next->location, "the %<bool%> keyword is not "
16424 "allowed in a C++20 concept definition");
16425 else
16426 error_at (token->location, "C++20 concept definition syntax "
16427 "is %<concept <name> = <expr>%>");
16430 /* In C++20 a concept definition is just 'concept name = expr;'
16431 Support that syntax as a TS extension by pretending we've seen
16432 the 'bool' specifier. */
16433 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16434 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16435 && !decl_specs->any_type_specifiers_p)
16437 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16438 token, /*type_definition*/false);
16439 decl_specs->any_type_specifiers_p = true;
16441 break;
16443 /* function-specifier:
16444 inline
16445 virtual
16446 explicit */
16447 case RID_INLINE:
16448 case RID_VIRTUAL:
16449 case RID_EXPLICIT:
16450 cp_parser_function_specifier_opt (parser, decl_specs);
16451 break;
16453 /* decl-specifier:
16454 typedef */
16455 case RID_TYPEDEF:
16456 ds = ds_typedef;
16457 /* Consume the token. */
16458 cp_lexer_consume_token (parser->lexer);
16460 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16461 break;
16463 /* A constructor declarator cannot appear in a typedef. */
16464 constructor_possible_p = false;
16465 /* The "typedef" keyword can only occur in a declaration; we
16466 may as well commit at this point. */
16467 cp_parser_commit_to_tentative_parse (parser);
16469 if (decl_specs->storage_class != sc_none)
16471 if (decl_specs->conflicting_specifiers_p)
16472 break;
16473 gcc_rich_location richloc (token->location);
16474 location_t oloc = decl_specs->locations[ds_storage_class];
16475 richloc.add_location_if_nearby (oloc);
16476 error_at (&richloc,
16477 "%<typedef%> specifier conflicts with %qs",
16478 cp_storage_class_name[decl_specs->storage_class]);
16479 decl_specs->conflicting_specifiers_p = true;
16481 break;
16483 /* storage-class-specifier:
16484 auto
16485 register
16486 static
16487 extern
16488 mutable
16490 GNU Extension:
16491 thread */
16492 case RID_AUTO:
16493 if (cxx_dialect == cxx98)
16495 /* Consume the token. */
16496 cp_lexer_consume_token (parser->lexer);
16498 /* Complain about `auto' as a storage specifier, if
16499 we're complaining about C++0x compatibility. */
16500 gcc_rich_location richloc (token->location);
16501 richloc.add_fixit_remove ();
16502 warning_at (&richloc, OPT_Wc__11_compat,
16503 "%<auto%> changes meaning in C++11; "
16504 "please remove it");
16506 /* Set the storage class anyway. */
16507 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16508 token);
16510 else
16511 /* C++0x auto type-specifier. */
16512 found_decl_spec = false;
16513 break;
16515 case RID_REGISTER:
16516 case RID_STATIC:
16517 case RID_EXTERN:
16518 case RID_MUTABLE:
16519 /* Consume the token. */
16520 cp_lexer_consume_token (parser->lexer);
16521 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16522 token);
16523 break;
16524 case RID_THREAD:
16525 /* Consume the token. */
16526 ds = ds_thread;
16527 cp_lexer_consume_token (parser->lexer);
16528 break;
16530 default:
16531 /* We did not yet find a decl-specifier yet. */
16532 found_decl_spec = false;
16533 break;
16536 if (found_decl_spec
16537 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16538 && token->keyword != RID_CONSTEXPR)
16539 error ("%qD invalid in condition", ridpointers[token->keyword]);
16541 if (found_decl_spec
16542 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16543 && token->keyword != RID_MUTABLE
16544 && token->keyword != RID_CONSTEXPR
16545 && token->keyword != RID_CONSTEVAL)
16547 if (token->keyword != RID_STATIC)
16548 error_at (token->location, "%qD invalid in lambda",
16549 ridpointers[token->keyword]);
16550 else if (cxx_dialect < cxx23)
16551 pedwarn (token->location, OPT_Wc__23_extensions,
16552 "%qD only valid in lambda with %<-std=c++23%> or "
16553 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16556 if (ds != ds_last)
16557 set_and_check_decl_spec_loc (decl_specs, ds, token);
16559 /* Constructors are a special case. The `S' in `S()' is not a
16560 decl-specifier; it is the beginning of the declarator. */
16561 constructor_p
16562 = (!found_decl_spec
16563 && constructor_possible_p
16564 && (cp_parser_constructor_declarator_p
16565 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16566 ds_friend))));
16568 /* If we don't have a DECL_SPEC yet, then we must be looking at
16569 a type-specifier. */
16570 if (!found_decl_spec && !constructor_p)
16572 int decl_spec_declares_class_or_enum;
16573 bool is_cv_qualifier;
16574 tree type_spec;
16576 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16577 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16579 type_spec
16580 = cp_parser_type_specifier (parser, flags,
16581 decl_specs,
16582 /*is_declaration=*/true,
16583 &decl_spec_declares_class_or_enum,
16584 &is_cv_qualifier);
16585 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16587 /* If this type-specifier referenced a user-defined type
16588 (a typedef, class-name, etc.), then we can't allow any
16589 more such type-specifiers henceforth.
16591 [dcl.spec]
16593 The longest sequence of decl-specifiers that could
16594 possibly be a type name is taken as the
16595 decl-specifier-seq of a declaration. The sequence shall
16596 be self-consistent as described below.
16598 [dcl.type]
16600 As a general rule, at most one type-specifier is allowed
16601 in the complete decl-specifier-seq of a declaration. The
16602 only exceptions are the following:
16604 -- const or volatile can be combined with any other
16605 type-specifier.
16607 -- signed or unsigned can be combined with char, long,
16608 short, or int.
16610 -- ..
16612 Example:
16614 typedef char* Pc;
16615 void g (const int Pc);
16617 Here, Pc is *not* part of the decl-specifier seq; it's
16618 the declarator. Therefore, once we see a type-specifier
16619 (other than a cv-qualifier), we forbid any additional
16620 user-defined types. We *do* still allow things like `int
16621 int' to be considered a decl-specifier-seq, and issue the
16622 error message later. */
16623 if (type_spec && !is_cv_qualifier)
16624 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16625 /* A constructor declarator cannot follow a type-specifier. */
16626 if (type_spec)
16628 constructor_possible_p = false;
16629 found_decl_spec = true;
16630 if (!is_cv_qualifier)
16631 decl_specs->any_type_specifiers_p = true;
16633 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16634 error_at (token->location, "type-specifier invalid in lambda");
16638 /* If we still do not have a DECL_SPEC, then there are no more
16639 decl-specifiers. */
16640 if (!found_decl_spec)
16641 break;
16643 if (decl_specs->std_attributes)
16645 error_at (decl_specs->locations[ds_std_attribute],
16646 "standard attributes in middle of decl-specifiers");
16647 inform (decl_specs->locations[ds_std_attribute],
16648 "standard attributes must precede the decl-specifiers to "
16649 "apply to the declaration, or follow them to apply to "
16650 "the type");
16653 decl_specs->any_specifiers_p = true;
16654 /* After we see one decl-specifier, further decl-specifiers are
16655 always optional. */
16656 flags |= CP_PARSER_FLAGS_OPTIONAL;
16659 /* Don't allow a friend specifier with a class definition. */
16660 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16661 && (*declares_class_or_enum & 2))
16662 error_at (decl_specs->locations[ds_friend],
16663 "class definition may not be declared a friend");
16666 /* Parse an (optional) storage-class-specifier.
16668 storage-class-specifier:
16669 auto
16670 register
16671 static
16672 extern
16673 mutable
16675 GNU Extension:
16677 storage-class-specifier:
16678 thread
16680 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16682 static tree
16683 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16685 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16687 case RID_AUTO:
16688 if (cxx_dialect != cxx98)
16689 return NULL_TREE;
16690 /* Fall through for C++98. */
16691 gcc_fallthrough ();
16693 case RID_REGISTER:
16694 case RID_STATIC:
16695 case RID_EXTERN:
16696 case RID_MUTABLE:
16697 case RID_THREAD:
16698 /* Consume the token. */
16699 return cp_lexer_consume_token (parser->lexer)->u.value;
16701 default:
16702 return NULL_TREE;
16706 /* Parse an (optional) function-specifier.
16708 function-specifier:
16709 inline
16710 virtual
16711 explicit
16713 C++20 Extension:
16714 explicit(constant-expression)
16716 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16717 Updates DECL_SPECS, if it is non-NULL. */
16719 static tree
16720 cp_parser_function_specifier_opt (cp_parser* parser,
16721 cp_decl_specifier_seq *decl_specs)
16723 cp_token *token = cp_lexer_peek_token (parser->lexer);
16724 switch (token->keyword)
16726 case RID_INLINE:
16727 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16728 break;
16730 case RID_VIRTUAL:
16731 /* 14.5.2.3 [temp.mem]
16733 A member function template shall not be virtual. */
16734 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16735 && current_class_type)
16736 error_at (token->location, "templates may not be %<virtual%>");
16737 else
16738 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16739 break;
16741 case RID_EXPLICIT:
16743 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16744 /* If we see '(', it's C++20 explicit(bool). */
16745 tree expr;
16746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16748 matching_parens parens;
16749 parens.consume_open (parser);
16751 /* New types are not allowed in an explicit-specifier. */
16752 const char *saved_message
16753 = parser->type_definition_forbidden_message;
16754 parser->type_definition_forbidden_message
16755 = G_("types may not be defined in explicit-specifier");
16757 if (cxx_dialect < cxx20)
16758 pedwarn (token->location, OPT_Wc__20_extensions,
16759 "%<explicit(bool)%> only available with %<-std=c++20%> "
16760 "or %<-std=gnu++20%>");
16762 /* Parse the constant-expression. */
16763 expr = cp_parser_constant_expression (parser);
16765 /* Restore the saved message. */
16766 parser->type_definition_forbidden_message = saved_message;
16767 parens.require_close (parser);
16769 else
16770 /* The explicit-specifier explicit without a constant-expression is
16771 equivalent to the explicit-specifier explicit(true). */
16772 expr = boolean_true_node;
16774 /* [dcl.fct.spec]
16775 "the constant-expression, if supplied, shall be a contextually
16776 converted constant expression of type bool." */
16777 expr = build_explicit_specifier (expr, tf_warning_or_error);
16778 /* We could evaluate it -- mark the decl as appropriate. */
16779 if (expr == boolean_true_node)
16780 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16781 else if (expr == boolean_false_node)
16782 /* Don't mark the decl as explicit. */;
16783 else if (decl_specs)
16784 /* The expression was value-dependent. Remember it so that we can
16785 substitute it later. */
16786 decl_specs->explicit_specifier = expr;
16787 return id;
16790 default:
16791 return NULL_TREE;
16794 /* Consume the token. */
16795 return cp_lexer_consume_token (parser->lexer)->u.value;
16798 /* Parse a linkage-specification.
16800 linkage-specification:
16801 extern string-literal { declaration-seq [opt] }
16802 extern string-literal declaration */
16804 static void
16805 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16807 /* Look for the `extern' keyword. */
16808 cp_token *extern_token
16809 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16811 /* Look for the string-literal. */
16812 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16813 tree linkage;
16814 if (cxx_dialect >= cxx26)
16815 linkage = cp_parser_unevaluated_string_literal (parser);
16816 else
16817 linkage = cp_parser_string_literal (parser, /*translate=*/false,
16818 /*wide_ok=*/false);
16820 /* Transform the literal into an identifier. If the literal is a
16821 wide-character string, or contains embedded NULs, then we can't
16822 handle it as the user wants. */
16823 if (linkage == error_mark_node
16824 || strlen (TREE_STRING_POINTER (linkage))
16825 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16827 cp_parser_error (parser, "invalid linkage-specification");
16828 /* Assume C++ linkage. */
16829 linkage = lang_name_cplusplus;
16831 else
16832 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16834 /* We're now using the new linkage. */
16835 unsigned saved_module = module_kind;
16836 module_kind &= ~MK_ATTACH;
16837 push_lang_context (linkage);
16839 /* Preserve the location of the innermost linkage specification,
16840 tracking the locations of nested specifications via a local. */
16841 location_t saved_location
16842 = parser->innermost_linkage_specification_location;
16843 /* Construct a location ranging from the start of the "extern" to
16844 the end of the string-literal, with the caret at the start, e.g.:
16845 extern "C" {
16846 ^~~~~~~~~~
16848 parser->innermost_linkage_specification_location
16849 = make_location (extern_token->location,
16850 extern_token->location,
16851 get_finish (string_token->location));
16853 /* If the next token is a `{', then we're using the first
16854 production. */
16855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16857 cp_ensure_no_omp_declare_simd (parser);
16858 cp_ensure_no_oacc_routine (parser);
16860 /* Consume the `{' token. */
16861 matching_braces braces;
16862 braces.consume_open (parser);
16863 /* Parse the declarations. */
16864 cp_parser_declaration_seq_opt (parser);
16865 /* Look for the closing `}'. */
16866 braces.require_close (parser);
16868 /* Otherwise, there's just one declaration. */
16869 else
16871 bool saved_in_unbraced_linkage_specification_p;
16873 saved_in_unbraced_linkage_specification_p
16874 = parser->in_unbraced_linkage_specification_p;
16875 parser->in_unbraced_linkage_specification_p = true;
16876 cp_parser_declaration (parser, prefix_attr);
16877 parser->in_unbraced_linkage_specification_p
16878 = saved_in_unbraced_linkage_specification_p;
16881 /* We're done with the linkage-specification. */
16882 pop_lang_context ();
16883 module_kind = saved_module;
16885 /* Restore location of parent linkage specification, if any. */
16886 parser->innermost_linkage_specification_location = saved_location;
16889 /* Parse a static_assert-declaration.
16891 static_assert-declaration:
16892 static_assert ( constant-expression , string-literal ) ;
16893 static_assert ( constant-expression ) ; (C++17)
16894 static_assert ( constant-expression, conditional-expression ) ; (C++26)
16896 If MEMBER_P, this static_assert is a class member. */
16898 static void
16899 cp_parser_static_assert (cp_parser *parser, bool member_p)
16901 cp_expr condition;
16902 location_t token_loc;
16903 tree message;
16905 /* Peek at the `static_assert' token so we can keep track of exactly
16906 where the static assertion started. */
16907 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16909 /* Look for the `static_assert' keyword. */
16910 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16911 RT_STATIC_ASSERT))
16912 return;
16914 /* We know we are in a static assertion; commit to any tentative
16915 parse. */
16916 if (cp_parser_parsing_tentatively (parser))
16917 cp_parser_commit_to_tentative_parse (parser);
16919 /* Parse the `(' starting the static assertion condition. */
16920 matching_parens parens;
16921 parens.require_open (parser);
16923 /* Parse the constant-expression. Allow a non-constant expression
16924 here in order to give better diagnostics in finish_static_assert. */
16925 condition
16926 = cp_parser_constant_expression (parser,
16927 /*allow_non_constant_p=*/true,
16928 /*non_constant_p=*/nullptr);
16930 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16932 if (pedantic && cxx_dialect < cxx17)
16933 pedwarn (input_location, OPT_Wc__17_extensions,
16934 "%<static_assert%> without a message "
16935 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16936 /* Eat the ')' */
16937 cp_lexer_consume_token (parser->lexer);
16938 message = build_string (1, "");
16939 TREE_TYPE (message) = char_array_type_node;
16940 fix_string_type (message);
16942 else
16944 /* Parse the separating `,'. */
16945 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16947 /* Parse the message expression. */
16948 bool string_lit = true;
16949 for (unsigned int i = 1; ; ++i)
16951 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, i);
16952 if (cp_parser_is_pure_string_literal (tok))
16953 continue;
16954 else if (tok->type == CPP_CLOSE_PAREN)
16955 break;
16956 string_lit = false;
16957 break;
16959 if (!string_lit)
16961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16962 if (cxx_dialect < cxx26)
16963 pedwarn (loc, OPT_Wc__26_extensions,
16964 "%<static_assert%> with non-string message only "
16965 "available with %<-std=c++2c%> or %<-std=gnu++2c%>");
16967 message = cp_parser_conditional_expression (parser);
16968 if (TREE_CODE (message) == STRING_CST)
16969 message = build1_loc (loc, PAREN_EXPR, TREE_TYPE (message),
16970 message);
16972 else if (cxx_dialect >= cxx26)
16973 message = cp_parser_unevaluated_string_literal (parser);
16974 else
16975 message = cp_parser_string_literal (parser, /*translate=*/false,
16976 /*wide_ok=*/true);
16978 /* A `)' completes the static assertion. */
16979 if (!parens.require_close (parser))
16980 cp_parser_skip_to_closing_parenthesis (parser,
16981 /*recovering=*/true,
16982 /*or_comma=*/false,
16983 /*consume_paren=*/true);
16986 /* A semicolon terminates the declaration. */
16987 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16989 /* Get the location for the static assertion. Use that of the
16990 condition if available, otherwise, use that of the "static_assert"
16991 token. */
16992 location_t assert_loc = condition.get_location ();
16993 if (assert_loc == UNKNOWN_LOCATION)
16994 assert_loc = token_loc;
16996 /* Complete the static assertion, which may mean either processing
16997 the static assert now or saving it for template instantiation. */
16998 finish_static_assert (condition, message, assert_loc, member_p,
16999 /*show_expr_p=*/false);
17002 /* Parse the expression in decltype ( expression ). */
17004 static tree
17005 cp_parser_decltype_expr (cp_parser *parser,
17006 bool &id_expression_or_member_access_p)
17008 cp_token *id_expr_start_token;
17009 tree expr;
17011 /* First, try parsing an id-expression. */
17012 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
17013 cp_parser_parse_tentatively (parser);
17014 expr = cp_parser_id_expression (parser,
17015 /*template_keyword_p=*/false,
17016 /*check_dependency_p=*/true,
17017 /*template_p=*/NULL,
17018 /*declarator_p=*/false,
17019 /*optional_p=*/false);
17021 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
17023 bool non_integral_constant_expression_p = false;
17024 tree id_expression = expr;
17025 cp_id_kind idk;
17026 const char *error_msg;
17028 if (identifier_p (expr))
17029 /* Lookup the name we got back from the id-expression. */
17030 expr = cp_parser_lookup_name_simple (parser, expr,
17031 id_expr_start_token->location);
17033 if (expr
17034 && expr != error_mark_node
17035 && TREE_CODE (expr) != TYPE_DECL
17036 && (TREE_CODE (expr) != BIT_NOT_EXPR
17037 || !TYPE_P (TREE_OPERAND (expr, 0)))
17038 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17040 /* Complete lookup of the id-expression. */
17041 expr = (finish_id_expression
17042 (id_expression, expr, parser->scope, &idk,
17043 /*integral_constant_expression_p=*/false,
17044 /*allow_non_integral_constant_expression_p=*/true,
17045 &non_integral_constant_expression_p,
17046 /*template_p=*/false,
17047 /*done=*/true,
17048 /*address_p=*/false,
17049 /*template_arg_p=*/false,
17050 &error_msg,
17051 id_expr_start_token->location));
17053 if (error_msg)
17055 /* We found an id-expression, but it was something that we
17056 should not have found. This is an error, not something
17057 we can recover from, so report the error we found and
17058 we'll recover as gracefully as possible. */
17059 cp_parser_parse_definitely (parser);
17060 cp_parser_error (parser, error_msg);
17061 id_expression_or_member_access_p = true;
17062 return error_mark_node;
17066 if (expr
17067 && expr != error_mark_node
17068 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17069 /* We have an id-expression. */
17070 id_expression_or_member_access_p = true;
17073 if (!id_expression_or_member_access_p)
17075 /* Abort the id-expression parse. */
17076 cp_parser_abort_tentative_parse (parser);
17078 /* Parsing tentatively, again. */
17079 cp_parser_parse_tentatively (parser);
17081 /* Parse a class member access. */
17082 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
17083 /*cast_p=*/false, /*decltype*/true,
17084 /*member_access_only_p=*/true, NULL);
17086 if (expr
17087 && expr != error_mark_node
17088 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17089 /* We have an id-expression. */
17090 id_expression_or_member_access_p = true;
17093 if (id_expression_or_member_access_p)
17094 /* We have parsed the complete id-expression or member access. */
17095 cp_parser_parse_definitely (parser);
17096 else
17098 /* Abort our attempt to parse an id-expression or member access
17099 expression. */
17100 cp_parser_abort_tentative_parse (parser);
17102 /* Parse a full expression. */
17103 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
17104 /*decltype_p=*/true);
17107 return expr;
17110 /* Parse a `decltype' type. Returns the type.
17112 decltype-specifier:
17113 decltype ( expression )
17114 C++14:
17115 decltype ( auto ) */
17117 static tree
17118 cp_parser_decltype (cp_parser *parser)
17120 bool id_expression_or_member_access_p = false;
17121 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
17123 if (start_token->type == CPP_DECLTYPE)
17125 /* Already parsed. */
17126 cp_lexer_consume_token (parser->lexer);
17127 return saved_checks_value (start_token->u.tree_check_value);
17130 /* Look for the `decltype' token. */
17131 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
17132 return error_mark_node;
17134 /* Parse the opening `('. */
17135 matching_parens parens;
17136 if (!parens.require_open (parser))
17137 return error_mark_node;
17139 /* Since we're going to preserve any side-effects from this parse, set up a
17140 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17141 in the expression. */
17142 tentative_firewall firewall (parser);
17144 /* If in_declarator_p, a reparse as an expression might succeed (60361).
17145 Otherwise, commit now for better diagnostics. */
17146 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
17147 && !parser->in_declarator_p)
17148 cp_parser_commit_to_topmost_tentative_parse (parser);
17150 push_deferring_access_checks (dk_deferred);
17152 tree expr = NULL_TREE;
17154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
17155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
17157 /* decltype (auto) */
17158 cp_lexer_consume_token (parser->lexer);
17159 if (cxx_dialect < cxx14)
17161 error_at (start_token->location,
17162 "%<decltype(auto)%> type specifier only available with "
17163 "%<-std=c++14%> or %<-std=gnu++14%>");
17164 expr = error_mark_node;
17167 else
17169 /* decltype (expression) */
17171 /* Types cannot be defined in a `decltype' expression. Save away the
17172 old message and set the new one. */
17173 const char *saved_message = parser->type_definition_forbidden_message;
17174 parser->type_definition_forbidden_message
17175 = G_("types may not be defined in %<decltype%> expressions");
17177 /* The restrictions on constant-expressions do not apply inside
17178 decltype expressions. */
17179 bool saved_integral_constant_expression_p
17180 = parser->integral_constant_expression_p;
17181 bool saved_non_integral_constant_expression_p
17182 = parser->non_integral_constant_expression_p;
17183 parser->integral_constant_expression_p = false;
17185 /* Within a parenthesized expression, a `>' token is always
17186 the greater-than operator. */
17187 bool saved_greater_than_is_operator_p
17188 = parser->greater_than_is_operator_p;
17189 parser->greater_than_is_operator_p = true;
17191 /* Don't synthesize an implicit template type parameter here. This
17192 could happen with C++23 code like
17194 void f(decltype(new auto{0}));
17196 where we want to deduce the auto right away so that the parameter
17197 is of type 'int *'. */
17198 auto cleanup = make_temp_override
17199 (parser->auto_is_implicit_function_template_parm_p, false);
17201 /* Do not actually evaluate the expression. */
17202 ++cp_unevaluated_operand;
17204 /* Do not warn about problems with the expression. */
17205 ++c_inhibit_evaluation_warnings;
17207 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
17208 STRIP_ANY_LOCATION_WRAPPER (expr);
17210 /* Go back to evaluating expressions. */
17211 --cp_unevaluated_operand;
17212 --c_inhibit_evaluation_warnings;
17214 /* The `>' token might be the end of a template-id or
17215 template-parameter-list now. */
17216 parser->greater_than_is_operator_p
17217 = saved_greater_than_is_operator_p;
17219 /* Restore the old message and the integral constant expression
17220 flags. */
17221 parser->type_definition_forbidden_message = saved_message;
17222 parser->integral_constant_expression_p
17223 = saved_integral_constant_expression_p;
17224 parser->non_integral_constant_expression_p
17225 = saved_non_integral_constant_expression_p;
17228 /* Parse to the closing `)'. */
17229 if (expr == error_mark_node || !parens.require_close (parser))
17231 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17232 /*consume_paren=*/true);
17233 expr = error_mark_node;
17236 /* If we got a parse error while tentative, bail out now. */
17237 if (cp_parser_error_occurred (parser))
17239 pop_deferring_access_checks ();
17240 return error_mark_node;
17243 if (!expr)
17244 /* Build auto. */
17245 expr = make_decltype_auto ();
17246 else
17247 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
17248 tf_warning_or_error);
17250 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
17251 it again. */
17252 start_token->type = CPP_DECLTYPE;
17253 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17254 start_token->tree_check_p = true;
17255 start_token->u.tree_check_value->value = expr;
17256 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
17257 start_token->keyword = RID_MAX;
17259 location_t loc = start_token->location;
17260 loc = make_location (loc, loc, parser->lexer);
17261 start_token->location = loc;
17263 cp_lexer_purge_tokens_after (parser->lexer, start_token);
17265 pop_to_parent_deferring_access_checks ();
17267 return expr;
17270 /* Special member functions [gram.special] */
17272 /* Parse a conversion-function-id.
17274 conversion-function-id:
17275 operator conversion-type-id
17277 Returns an IDENTIFIER_NODE representing the operator. */
17279 static tree
17280 cp_parser_conversion_function_id (cp_parser* parser)
17282 tree type;
17283 tree saved_scope;
17284 tree saved_qualifying_scope;
17285 tree saved_object_scope;
17286 tree pushed_scope = NULL_TREE;
17288 /* Look for the `operator' token. */
17289 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17290 return error_mark_node;
17291 /* When we parse the conversion-type-id, the current scope will be
17292 reset. However, we need that information in able to look up the
17293 conversion function later, so we save it here. */
17294 saved_scope = parser->scope;
17295 saved_qualifying_scope = parser->qualifying_scope;
17296 saved_object_scope = parser->object_scope;
17297 /* We must enter the scope of the class so that the names of
17298 entities declared within the class are available in the
17299 conversion-type-id. For example, consider:
17301 struct S {
17302 typedef int I;
17303 operator I();
17306 S::operator I() { ... }
17308 In order to see that `I' is a type-name in the definition, we
17309 must be in the scope of `S'. */
17310 if (saved_scope)
17311 pushed_scope = push_scope (saved_scope);
17312 /* Parse the conversion-type-id. */
17313 type = cp_parser_conversion_type_id (parser);
17314 /* Leave the scope of the class, if any. */
17315 if (pushed_scope)
17316 pop_scope (pushed_scope);
17317 /* Restore the saved scope. */
17318 parser->scope = saved_scope;
17319 parser->qualifying_scope = saved_qualifying_scope;
17320 parser->object_scope = saved_object_scope;
17321 /* If the TYPE is invalid, indicate failure. */
17322 if (type == error_mark_node)
17323 return error_mark_node;
17324 return make_conv_op_name (type);
17327 /* Parse a conversion-type-id:
17329 conversion-type-id:
17330 type-specifier-seq conversion-declarator [opt]
17332 Returns the TYPE specified. */
17334 static tree
17335 cp_parser_conversion_type_id (cp_parser* parser)
17337 tree attributes;
17338 cp_decl_specifier_seq type_specifiers;
17339 cp_declarator *declarator;
17340 tree type_specified;
17341 const char *saved_message;
17343 /* Parse the attributes. */
17344 attributes = cp_parser_attributes_opt (parser);
17346 saved_message = parser->type_definition_forbidden_message;
17347 parser->type_definition_forbidden_message
17348 = G_("types may not be defined in a conversion-type-id");
17350 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17351 optional in conversion-type-id. */
17352 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17353 /*is_declaration=*/false,
17354 /*is_trailing_return=*/false,
17355 &type_specifiers);
17357 parser->type_definition_forbidden_message = saved_message;
17359 /* If that didn't work, stop. */
17360 if (type_specifiers.type == error_mark_node)
17361 return error_mark_node;
17362 /* Parse the conversion-declarator. */
17363 declarator = cp_parser_conversion_declarator_opt (parser);
17365 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17366 /*initialized=*/0, &attributes);
17367 if (attributes)
17368 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17370 /* Don't give this error when parsing tentatively. This happens to
17371 work because we always parse this definitively once. */
17372 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17373 && type_uses_auto (type_specified))
17375 if (cxx_dialect < cxx14)
17377 error ("invalid use of %<auto%> in conversion operator");
17378 return error_mark_node;
17380 else if (template_parm_scope_p ())
17381 warning (0, "use of %<auto%> in member template "
17382 "conversion operator can never be deduced");
17385 return type_specified;
17388 /* Parse an (optional) conversion-declarator.
17390 conversion-declarator:
17391 ptr-operator conversion-declarator [opt]
17395 static cp_declarator *
17396 cp_parser_conversion_declarator_opt (cp_parser* parser)
17398 enum tree_code code;
17399 tree class_type, std_attributes = NULL_TREE;
17400 cp_cv_quals cv_quals;
17402 /* We don't know if there's a ptr-operator next, or not. */
17403 cp_parser_parse_tentatively (parser);
17404 /* Try the ptr-operator. */
17405 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17406 &std_attributes);
17407 /* If it worked, look for more conversion-declarators. */
17408 if (cp_parser_parse_definitely (parser))
17410 cp_declarator *declarator;
17412 /* Parse another optional declarator. */
17413 declarator = cp_parser_conversion_declarator_opt (parser);
17415 declarator = cp_parser_make_indirect_declarator
17416 (code, class_type, cv_quals, declarator, std_attributes);
17418 return declarator;
17421 return NULL;
17424 /* Parse an (optional) ctor-initializer.
17426 ctor-initializer:
17427 : mem-initializer-list */
17429 static void
17430 cp_parser_ctor_initializer_opt (cp_parser* parser)
17432 /* If the next token is not a `:', then there is no
17433 ctor-initializer. */
17434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17436 /* Do default initialization of any bases and members. */
17437 if (DECL_CONSTRUCTOR_P (current_function_decl))
17438 finish_mem_initializers (NULL_TREE);
17439 return;
17442 /* Consume the `:' token. */
17443 cp_lexer_consume_token (parser->lexer);
17444 /* And the mem-initializer-list. */
17445 cp_parser_mem_initializer_list (parser);
17448 /* Parse a mem-initializer-list.
17450 mem-initializer-list:
17451 mem-initializer ... [opt]
17452 mem-initializer ... [opt] , mem-initializer-list */
17454 static void
17455 cp_parser_mem_initializer_list (cp_parser* parser)
17457 tree mem_initializer_list = NULL_TREE;
17458 tree target_ctor = error_mark_node;
17459 cp_token *token = cp_lexer_peek_token (parser->lexer);
17461 /* Let the semantic analysis code know that we are starting the
17462 mem-initializer-list. */
17463 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17464 error_at (token->location,
17465 "only constructors take member initializers");
17467 /* Loop through the list. */
17468 while (true)
17470 tree mem_initializer;
17472 token = cp_lexer_peek_token (parser->lexer);
17473 /* Parse the mem-initializer. */
17474 mem_initializer = cp_parser_mem_initializer (parser);
17475 /* If the next token is a `...', we're expanding member initializers. */
17476 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17477 if (ellipsis
17478 || (mem_initializer != error_mark_node
17479 && check_for_bare_parameter_packs (TREE_PURPOSE
17480 (mem_initializer))))
17482 /* Consume the `...'. */
17483 if (ellipsis)
17484 cp_lexer_consume_token (parser->lexer);
17486 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17487 can be expanded but members cannot. */
17488 if (mem_initializer != error_mark_node
17489 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17491 error_at (token->location,
17492 "cannot expand initializer for member %qD",
17493 TREE_PURPOSE (mem_initializer));
17494 mem_initializer = error_mark_node;
17497 /* Construct the pack expansion type. */
17498 if (mem_initializer != error_mark_node)
17499 mem_initializer = make_pack_expansion (mem_initializer);
17501 if (target_ctor != error_mark_node
17502 && mem_initializer != error_mark_node)
17504 error ("mem-initializer for %qD follows constructor delegation",
17505 TREE_PURPOSE (mem_initializer));
17506 mem_initializer = error_mark_node;
17508 /* Look for a target constructor. */
17509 if (mem_initializer != error_mark_node
17510 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17511 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17513 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17514 if (mem_initializer_list)
17516 error ("constructor delegation follows mem-initializer for %qD",
17517 TREE_PURPOSE (mem_initializer_list));
17518 mem_initializer = error_mark_node;
17520 target_ctor = mem_initializer;
17522 /* Add it to the list, unless it was erroneous. */
17523 if (mem_initializer != error_mark_node)
17525 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17526 mem_initializer_list = mem_initializer;
17528 /* If the next token is not a `,', we're done. */
17529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17530 break;
17531 /* Consume the `,' token. */
17532 cp_lexer_consume_token (parser->lexer);
17535 /* Perform semantic analysis. */
17536 if (DECL_CONSTRUCTOR_P (current_function_decl))
17537 finish_mem_initializers (mem_initializer_list);
17540 /* Parse a mem-initializer.
17542 mem-initializer:
17543 mem-initializer-id ( expression-list [opt] )
17544 mem-initializer-id braced-init-list
17546 GNU extension:
17548 mem-initializer:
17549 ( expression-list [opt] )
17551 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17552 class) or FIELD_DECL (for a non-static data member) to initialize;
17553 the TREE_VALUE is the expression-list. An empty initialization
17554 list is represented by void_list_node. */
17556 static tree
17557 cp_parser_mem_initializer (cp_parser* parser)
17559 tree mem_initializer_id;
17560 tree expression_list;
17561 tree member;
17562 cp_token *token = cp_lexer_peek_token (parser->lexer);
17564 /* Find out what is being initialized. */
17565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17567 permerror (token->location,
17568 "anachronistic old-style base class initializer");
17569 mem_initializer_id = NULL_TREE;
17571 else
17573 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17574 if (mem_initializer_id == error_mark_node)
17575 return mem_initializer_id;
17577 member = expand_member_init (mem_initializer_id);
17578 if (member && !DECL_P (member))
17579 in_base_initializer = 1;
17581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17583 cp_lexer_set_source_position (parser->lexer);
17584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17585 expression_list = cp_parser_braced_list (parser);
17586 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17587 expression_list = build_tree_list (NULL_TREE, expression_list);
17589 else
17591 vec<tree, va_gc> *vec;
17592 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17593 /*cast_p=*/false,
17594 /*allow_expansion_p=*/true,
17595 /*non_constant_p=*/NULL,
17596 /*close_paren_loc=*/NULL,
17597 /*wrap_locations_p=*/true);
17598 if (vec == NULL)
17599 return error_mark_node;
17600 expression_list = build_tree_list_vec (vec);
17601 release_tree_vector (vec);
17604 if (expression_list == error_mark_node)
17605 return error_mark_node;
17606 if (!expression_list)
17607 expression_list = void_type_node;
17609 in_base_initializer = 0;
17611 if (!member)
17612 return error_mark_node;
17613 tree node = build_tree_list (member, expression_list);
17615 /* We can't attach the source location of this initializer directly to
17616 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17617 within the TREE_TYPE of the list node. */
17618 location_t loc
17619 = make_location (token->location, token->location, parser->lexer);
17620 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17621 SET_EXPR_LOCATION (dummy, loc);
17622 TREE_TYPE (node) = dummy;
17624 return node;
17627 /* Parse a mem-initializer-id.
17629 mem-initializer-id:
17630 :: [opt] nested-name-specifier [opt] class-name
17631 decltype-specifier (C++11)
17632 identifier
17634 Returns a TYPE indicating the class to be initialized for the first
17635 production (and the second in C++11). Returns an IDENTIFIER_NODE
17636 indicating the data member to be initialized for the last production. */
17638 static tree
17639 cp_parser_mem_initializer_id (cp_parser* parser)
17641 bool global_scope_p;
17642 bool nested_name_specifier_p;
17643 bool template_p = false;
17644 tree id;
17646 cp_token *token = cp_lexer_peek_token (parser->lexer);
17648 /* `typename' is not allowed in this context ([temp.res]). */
17649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17651 error_at (token->location,
17652 "keyword %<typename%> not allowed in this context (a qualified "
17653 "member initializer is implicitly a type)");
17654 cp_lexer_consume_token (parser->lexer);
17656 /* Look for the optional `::' operator. */
17657 global_scope_p
17658 = (cp_parser_global_scope_opt (parser,
17659 /*current_scope_valid_p=*/false)
17660 != NULL_TREE);
17661 /* Look for the optional nested-name-specifier. The simplest way to
17662 implement:
17664 [temp.res]
17666 The keyword `typename' is not permitted in a base-specifier or
17667 mem-initializer; in these contexts a qualified name that
17668 depends on a template-parameter is implicitly assumed to be a
17669 type name.
17671 is to assume that we have seen the `typename' keyword at this
17672 point. */
17673 nested_name_specifier_p
17674 = (cp_parser_nested_name_specifier_opt (parser,
17675 /*typename_keyword_p=*/true,
17676 /*check_dependency_p=*/true,
17677 /*type_p=*/true,
17678 /*is_declaration=*/true)
17679 != NULL_TREE);
17680 if (nested_name_specifier_p)
17681 template_p = cp_parser_optional_template_keyword (parser);
17682 /* If there is a `::' operator or a nested-name-specifier, then we
17683 are definitely looking for a class-name. */
17684 if (global_scope_p || nested_name_specifier_p)
17685 return cp_parser_class_name (parser,
17686 /*typename_keyword_p=*/true,
17687 /*template_keyword_p=*/template_p,
17688 typename_type,
17689 /*check_dependency_p=*/true,
17690 /*class_head_p=*/false,
17691 /*is_declaration=*/true);
17692 /* Otherwise, we could also be looking for an ordinary identifier. */
17693 cp_parser_parse_tentatively (parser);
17694 if (cp_lexer_next_token_is_decltype (parser->lexer))
17695 /* Try a decltype-specifier. */
17696 id = cp_parser_decltype (parser);
17697 else
17698 /* Otherwise, try a class-name. */
17699 id = cp_parser_class_name (parser,
17700 /*typename_keyword_p=*/true,
17701 /*template_keyword_p=*/false,
17702 none_type,
17703 /*check_dependency_p=*/true,
17704 /*class_head_p=*/false,
17705 /*is_declaration=*/true);
17706 /* If we found one, we're done. */
17707 if (cp_parser_parse_definitely (parser))
17708 return id;
17709 /* Otherwise, look for an ordinary identifier. */
17710 return cp_parser_identifier (parser);
17713 /* Overloading [gram.over] */
17715 /* Parse an operator-function-id.
17717 operator-function-id:
17718 operator operator
17720 Returns an IDENTIFIER_NODE for the operator which is a
17721 human-readable spelling of the identifier, e.g., `operator +'. */
17723 static cp_expr
17724 cp_parser_operator_function_id (cp_parser* parser)
17726 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17727 /* Look for the `operator' keyword. */
17728 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17729 return error_mark_node;
17730 /* And then the name of the operator itself. */
17731 return cp_parser_operator (parser, start_loc);
17734 /* Return an identifier node for a user-defined literal operator.
17735 The suffix identifier is chained to the operator name identifier. */
17737 tree
17738 cp_literal_operator_id (const char* name)
17740 tree identifier;
17741 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17742 + strlen (name) + 10);
17743 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17744 identifier = get_identifier (buffer);
17745 XDELETEVEC (buffer);
17747 return identifier;
17750 /* Parse an operator.
17752 operator:
17753 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17754 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17755 || ++ -- , ->* -> () []
17757 GNU Extensions:
17759 operator:
17760 <? >? <?= >?=
17762 Returns an IDENTIFIER_NODE for the operator which is a
17763 human-readable spelling of the identifier, e.g., `operator +'. */
17765 static cp_expr
17766 cp_parser_operator (cp_parser* parser, location_t start_loc)
17768 tree id = NULL_TREE;
17769 cp_token *token;
17770 bool utf8 = false;
17772 /* Peek at the next token. */
17773 token = cp_lexer_peek_token (parser->lexer);
17775 location_t end_loc = token->location;
17777 /* Figure out which operator we have. */
17778 enum tree_code op = ERROR_MARK;
17779 bool assop = false;
17780 bool consumed = false;
17781 switch (token->type)
17783 case CPP_KEYWORD:
17785 /* The keyword should be either `new', `delete' or `co_await'. */
17786 if (token->keyword == RID_NEW)
17787 op = NEW_EXPR;
17788 else if (token->keyword == RID_DELETE)
17789 op = DELETE_EXPR;
17790 else if (token->keyword == RID_CO_AWAIT)
17791 op = CO_AWAIT_EXPR;
17792 else
17793 break;
17795 /* Consume the `new', `delete' or co_await token. */
17796 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17798 /* Peek at the next token. */
17799 token = cp_lexer_peek_token (parser->lexer);
17800 /* If it's a `[' token then this is the array variant of the
17801 operator. */
17802 if (token->type == CPP_OPEN_SQUARE
17803 && op != CO_AWAIT_EXPR)
17805 /* Consume the `[' token. */
17806 cp_lexer_consume_token (parser->lexer);
17807 /* Look for the `]' token. */
17808 if (cp_token *close_token
17809 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17810 end_loc = close_token->location;
17811 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17813 consumed = true;
17814 break;
17817 case CPP_PLUS:
17818 op = PLUS_EXPR;
17819 break;
17821 case CPP_MINUS:
17822 op = MINUS_EXPR;
17823 break;
17825 case CPP_MULT:
17826 op = MULT_EXPR;
17827 break;
17829 case CPP_DIV:
17830 op = TRUNC_DIV_EXPR;
17831 break;
17833 case CPP_MOD:
17834 op = TRUNC_MOD_EXPR;
17835 break;
17837 case CPP_XOR:
17838 op = BIT_XOR_EXPR;
17839 break;
17841 case CPP_AND:
17842 op = BIT_AND_EXPR;
17843 break;
17845 case CPP_OR:
17846 op = BIT_IOR_EXPR;
17847 break;
17849 case CPP_COMPL:
17850 op = BIT_NOT_EXPR;
17851 break;
17853 case CPP_NOT:
17854 op = TRUTH_NOT_EXPR;
17855 break;
17857 case CPP_EQ:
17858 assop = true;
17859 op = NOP_EXPR;
17860 break;
17862 case CPP_LESS:
17863 op = LT_EXPR;
17864 break;
17866 case CPP_GREATER:
17867 op = GT_EXPR;
17868 break;
17870 case CPP_PLUS_EQ:
17871 assop = true;
17872 op = PLUS_EXPR;
17873 break;
17875 case CPP_MINUS_EQ:
17876 assop = true;
17877 op = MINUS_EXPR;
17878 break;
17880 case CPP_MULT_EQ:
17881 assop = true;
17882 op = MULT_EXPR;
17883 break;
17885 case CPP_DIV_EQ:
17886 assop = true;
17887 op = TRUNC_DIV_EXPR;
17888 break;
17890 case CPP_MOD_EQ:
17891 assop = true;
17892 op = TRUNC_MOD_EXPR;
17893 break;
17895 case CPP_XOR_EQ:
17896 assop = true;
17897 op = BIT_XOR_EXPR;
17898 break;
17900 case CPP_AND_EQ:
17901 assop = true;
17902 op = BIT_AND_EXPR;
17903 break;
17905 case CPP_OR_EQ:
17906 assop = true;
17907 op = BIT_IOR_EXPR;
17908 break;
17910 case CPP_LSHIFT:
17911 op = LSHIFT_EXPR;
17912 break;
17914 case CPP_RSHIFT:
17915 op = RSHIFT_EXPR;
17916 break;
17918 case CPP_LSHIFT_EQ:
17919 assop = true;
17920 op = LSHIFT_EXPR;
17921 break;
17923 case CPP_RSHIFT_EQ:
17924 assop = true;
17925 op = RSHIFT_EXPR;
17926 break;
17928 case CPP_EQ_EQ:
17929 op = EQ_EXPR;
17930 break;
17932 case CPP_NOT_EQ:
17933 op = NE_EXPR;
17934 break;
17936 case CPP_LESS_EQ:
17937 op = LE_EXPR;
17938 break;
17940 case CPP_GREATER_EQ:
17941 op = GE_EXPR;
17942 break;
17944 case CPP_SPACESHIP:
17945 op = SPACESHIP_EXPR;
17946 break;
17948 case CPP_AND_AND:
17949 op = TRUTH_ANDIF_EXPR;
17950 break;
17952 case CPP_OR_OR:
17953 op = TRUTH_ORIF_EXPR;
17954 break;
17956 case CPP_PLUS_PLUS:
17957 op = POSTINCREMENT_EXPR;
17958 break;
17960 case CPP_MINUS_MINUS:
17961 op = PREDECREMENT_EXPR;
17962 break;
17964 case CPP_COMMA:
17965 op = COMPOUND_EXPR;
17966 break;
17968 case CPP_DEREF_STAR:
17969 op = MEMBER_REF;
17970 break;
17972 case CPP_DEREF:
17973 op = COMPONENT_REF;
17974 break;
17976 case CPP_QUERY:
17977 op = COND_EXPR;
17978 /* Consume the `?'. */
17979 cp_lexer_consume_token (parser->lexer);
17980 /* Look for the matching `:'. */
17981 cp_parser_require (parser, CPP_COLON, RT_COLON);
17982 consumed = true;
17983 break;
17985 case CPP_OPEN_PAREN:
17987 /* Consume the `('. */
17988 matching_parens parens;
17989 parens.consume_open (parser);
17990 /* Look for the matching `)'. */
17991 token = parens.require_close (parser);
17992 if (token)
17993 end_loc = token->location;
17994 op = CALL_EXPR;
17995 consumed = true;
17996 break;
17999 case CPP_OPEN_SQUARE:
18000 /* Consume the `['. */
18001 cp_lexer_consume_token (parser->lexer);
18002 /* Look for the matching `]'. */
18003 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18004 if (token)
18005 end_loc = token->location;
18006 op = ARRAY_REF;
18007 consumed = true;
18008 break;
18010 case CPP_UTF8STRING:
18011 case CPP_UTF8STRING_USERDEF:
18012 utf8 = true;
18013 /* FALLTHRU */
18014 case CPP_STRING:
18015 case CPP_WSTRING:
18016 case CPP_STRING16:
18017 case CPP_STRING32:
18018 case CPP_STRING_USERDEF:
18019 case CPP_WSTRING_USERDEF:
18020 case CPP_STRING16_USERDEF:
18021 case CPP_STRING32_USERDEF:
18023 tree string_tree;
18024 int sz, len;
18026 if (cxx_dialect == cxx98)
18027 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
18029 /* Consume the string. */
18030 cp_expr str = cp_parser_userdef_string_literal (parser,
18031 /*lookup_udlit=*/false);
18032 if (str == error_mark_node)
18033 return error_mark_node;
18034 else if (TREE_CODE (str) == USERDEF_LITERAL)
18036 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
18037 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
18038 end_loc = str.get_location ();
18040 else
18042 string_tree = str;
18043 /* Look for the suffix identifier. */
18044 token = cp_lexer_peek_token (parser->lexer);
18045 if (token->type == CPP_NAME)
18047 id = cp_parser_identifier (parser);
18048 end_loc = token->location;
18050 else if (token->type == CPP_KEYWORD)
18052 error ("unexpected keyword;"
18053 " remove space between quotes and suffix identifier");
18054 return error_mark_node;
18056 else
18058 error ("expected suffix identifier");
18059 return error_mark_node;
18062 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
18063 (TREE_TYPE (TREE_TYPE (string_tree))));
18064 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
18065 if (len != 0)
18067 error ("expected empty string after %<operator%> keyword");
18068 return error_mark_node;
18070 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
18071 != char_type_node)
18073 error ("invalid encoding prefix in literal operator");
18074 return error_mark_node;
18076 if (id != error_mark_node)
18078 const char *name = IDENTIFIER_POINTER (id);
18079 id = cp_literal_operator_id (name);
18081 /* Generate a location of the form:
18082 "" _suffix_identifier
18083 ^~~~~~~~~~~~~~~~~~~~~
18084 with caret == start at the start token, finish at the end of the
18085 suffix identifier. */
18086 location_t combined_loc
18087 = make_location (start_loc, start_loc, parser->lexer);
18088 return cp_expr (id, combined_loc);
18091 default:
18092 /* Anything else is an error. */
18093 break;
18096 /* If we have selected an identifier, we need to consume the
18097 operator token. */
18098 if (op != ERROR_MARK)
18100 id = ovl_op_identifier (assop, op);
18101 if (!consumed)
18102 cp_lexer_consume_token (parser->lexer);
18104 /* Otherwise, no valid operator name was present. */
18105 else
18107 cp_parser_error (parser, "expected operator");
18108 id = error_mark_node;
18111 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
18112 return cp_expr (id, start_loc);
18115 /* Parse a template-declaration.
18117 template-declaration:
18118 export [opt] template < template-parameter-list > declaration
18120 If MEMBER_P is TRUE, this template-declaration occurs within a
18121 class-specifier.
18123 The grammar rule given by the standard isn't correct. What
18124 is really meant is:
18126 template-declaration:
18127 export [opt] template-parameter-list-seq
18128 decl-specifier-seq [opt] init-declarator [opt] ;
18129 export [opt] template-parameter-list-seq
18130 function-definition
18132 template-parameter-list-seq:
18133 template-parameter-list-seq [opt]
18134 template < template-parameter-list >
18136 Concept Extensions:
18138 template-parameter-list-seq:
18139 template < template-parameter-list > requires-clause [opt]
18141 requires-clause:
18142 requires logical-or-expression */
18144 static void
18145 cp_parser_template_declaration (cp_parser* parser, bool member_p)
18147 /* Check for `export'. */
18148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
18150 /* Consume the `export' token. */
18151 cp_lexer_consume_token (parser->lexer);
18152 /* Warn that this use of export is deprecated. */
18153 if (cxx_dialect < cxx11)
18154 warning (0, "keyword %<export%> not implemented, and will be ignored");
18155 else if (cxx_dialect < cxx20)
18156 warning (0, "keyword %<export%> is deprecated, and is ignored");
18157 else
18158 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
18161 cp_parser_template_declaration_after_export (parser, member_p);
18164 /* Parse a template-parameter-list.
18166 template-parameter-list:
18167 template-parameter
18168 template-parameter-list , template-parameter
18170 Returns a TREE_LIST. Each node represents a template parameter.
18171 The nodes are connected via their TREE_CHAINs. */
18173 static tree
18174 cp_parser_template_parameter_list (cp_parser* parser)
18176 tree parameter_list = NULL_TREE;
18178 /* Don't create wrapper nodes within a template-parameter-list,
18179 since we don't want to have different types based on the
18180 spelling location of constants and decls within them. */
18181 auto_suppress_location_wrappers sentinel;
18183 begin_template_parm_list ();
18185 /* The loop below parses the template parms. We first need to know
18186 the total number of template parms to be able to compute proper
18187 canonical types of each dependent type. So after the loop, when
18188 we know the total number of template parms,
18189 end_template_parm_list computes the proper canonical types and
18190 fixes up the dependent types accordingly. */
18191 while (true)
18193 tree parameter;
18194 bool is_non_type;
18195 bool is_parameter_pack;
18196 location_t parm_loc;
18198 /* Parse the template-parameter. */
18199 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
18200 parameter = cp_parser_template_parameter (parser,
18201 &is_non_type,
18202 &is_parameter_pack);
18203 /* Add it to the list. */
18204 if (parameter != error_mark_node)
18205 parameter_list = process_template_parm (parameter_list,
18206 parm_loc,
18207 parameter,
18208 is_non_type,
18209 is_parameter_pack);
18210 else
18212 tree err_parm = build_tree_list (parameter, parameter);
18213 parameter_list = chainon (parameter_list, err_parm);
18216 /* If the next token is not a `,', we're done. */
18217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18218 break;
18219 /* Otherwise, consume the `,' token. */
18220 cp_lexer_consume_token (parser->lexer);
18223 return end_template_parm_list (parameter_list);
18226 /* Parse a introduction-list.
18228 introduction-list:
18229 introduced-parameter
18230 introduction-list , introduced-parameter
18232 introduced-parameter:
18233 ...[opt] identifier
18235 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
18236 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
18237 WILDCARD_DECL will also have DECL_NAME set and token location in
18238 DECL_SOURCE_LOCATION. */
18240 static tree
18241 cp_parser_introduction_list (cp_parser *parser)
18243 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
18245 while (true)
18247 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
18248 if (is_pack)
18249 cp_lexer_consume_token (parser->lexer);
18251 tree identifier = cp_parser_identifier (parser);
18252 if (identifier == error_mark_node)
18253 break;
18255 /* Build placeholder. */
18256 tree parm = build_nt (WILDCARD_DECL);
18257 DECL_SOURCE_LOCATION (parm)
18258 = cp_lexer_peek_token (parser->lexer)->location;
18259 DECL_NAME (parm) = identifier;
18260 WILDCARD_PACK_P (parm) = is_pack;
18261 vec_safe_push (introduction_vec, parm);
18263 /* If the next token is not a `,', we're done. */
18264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18265 break;
18266 /* Otherwise, consume the `,' token. */
18267 cp_lexer_consume_token (parser->lexer);
18270 /* Convert the vec into a TREE_VEC. */
18271 tree introduction_list = make_tree_vec (introduction_vec->length ());
18272 unsigned int n;
18273 tree parm;
18274 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
18275 TREE_VEC_ELT (introduction_list, n) = parm;
18277 release_tree_vector (introduction_vec);
18278 return introduction_list;
18281 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
18282 is an abstract declarator. */
18284 static inline cp_declarator*
18285 get_id_declarator (cp_declarator *declarator)
18287 cp_declarator *d = declarator;
18288 while (d && d->kind != cdk_id)
18289 d = d->declarator;
18290 return d;
18293 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
18294 is an abstract declarator. */
18296 static inline tree
18297 get_unqualified_id (cp_declarator *declarator)
18299 declarator = get_id_declarator (declarator);
18300 if (declarator)
18301 return declarator->u.id.unqualified_name;
18302 else
18303 return NULL_TREE;
18306 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18308 static inline bool
18309 is_constrained_parameter (tree type)
18311 return (type
18312 && TREE_CODE (type) == TYPE_DECL
18313 && CONSTRAINED_PARM_CONCEPT (type)
18314 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18317 /* Returns true if PARM declares a constrained-parameter. */
18319 static inline bool
18320 is_constrained_parameter (cp_parameter_declarator *parm)
18322 return is_constrained_parameter (parm->decl_specifiers.type);
18325 /* Check that the type parameter is only a declarator-id, and that its
18326 type is not cv-qualified. */
18328 bool
18329 cp_parser_check_constrained_type_parm (cp_parser *parser,
18330 cp_parameter_declarator *parm)
18332 if (!parm->declarator)
18333 return true;
18335 if (parm->declarator->kind != cdk_id)
18337 cp_parser_error (parser, "invalid constrained type parameter");
18338 return false;
18341 /* Don't allow cv-qualified type parameters. */
18342 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18343 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18345 cp_parser_error (parser, "cv-qualified type parameter");
18346 return false;
18349 return true;
18352 /* Finish parsing/processing a template type parameter and checking
18353 various restrictions. */
18355 static inline tree
18356 cp_parser_constrained_type_template_parm (cp_parser *parser,
18357 tree id,
18358 cp_parameter_declarator* parmdecl)
18360 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18361 return finish_template_type_parm (class_type_node, id);
18362 else
18363 return error_mark_node;
18366 static tree
18367 finish_constrained_template_template_parm (tree proto, tree id)
18369 /* FIXME: This should probably be copied, and we may need to adjust
18370 the template parameter depths. */
18371 tree saved_parms = current_template_parms;
18372 begin_template_parm_list ();
18373 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18374 end_template_parm_list ();
18376 tree parm = finish_template_template_parm (class_type_node, id);
18377 current_template_parms = saved_parms;
18379 return parm;
18382 /* Finish parsing/processing a template template parameter by borrowing
18383 the template parameter list from the prototype parameter. */
18385 static tree
18386 cp_parser_constrained_template_template_parm (cp_parser *parser,
18387 tree proto,
18388 tree id,
18389 cp_parameter_declarator *parmdecl)
18391 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18392 return error_mark_node;
18393 return finish_constrained_template_template_parm (proto, id);
18396 /* Create a new non-type template parameter from the given PARM
18397 declarator. */
18399 static tree
18400 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18401 cp_parameter_declarator *parm)
18403 *is_non_type = true;
18404 cp_declarator *decl = parm->declarator;
18405 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18406 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18407 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18410 /* Build a constrained template parameter based on the PARMDECL
18411 declarator. The type of PARMDECL is the constrained type, which
18412 refers to the prototype template parameter that ultimately
18413 specifies the type of the declared parameter. */
18415 static tree
18416 finish_constrained_parameter (cp_parser *parser,
18417 cp_parameter_declarator *parmdecl,
18418 bool *is_non_type)
18420 tree decl = parmdecl->decl_specifiers.type;
18421 tree id = get_unqualified_id (parmdecl->declarator);
18422 tree def = parmdecl->default_argument;
18423 tree proto = DECL_INITIAL (decl);
18425 /* Build the parameter. Return an error if the declarator was invalid. */
18426 tree parm;
18427 if (TREE_CODE (proto) == TYPE_DECL)
18428 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18429 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18430 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18431 parmdecl);
18432 else
18433 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18434 if (parm == error_mark_node)
18435 return error_mark_node;
18437 /* Finish the parameter decl and create a node attaching the
18438 default argument and constraint. */
18439 parm = build_tree_list (def, parm);
18440 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18442 return parm;
18445 /* Returns true if the parsed type actually represents the declaration
18446 of a type template-parameter. */
18448 static bool
18449 declares_constrained_type_template_parameter (tree type)
18451 return (is_constrained_parameter (type)
18452 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18455 /* Returns true if the parsed type actually represents the declaration of
18456 a template template-parameter. */
18458 static bool
18459 declares_constrained_template_template_parameter (tree type)
18461 return (is_constrained_parameter (type)
18462 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18465 /* Parse a default argument for a type template-parameter.
18466 Note that diagnostics are handled in cp_parser_template_parameter. */
18468 static tree
18469 cp_parser_default_type_template_argument (cp_parser *parser)
18471 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18473 /* Consume the `=' token. */
18474 cp_lexer_consume_token (parser->lexer);
18476 cp_token *token = cp_lexer_peek_token (parser->lexer);
18478 /* Tell cp_parser_lambda_expression this is a default argument. */
18479 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18480 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18482 /* Parse the default-argument. */
18483 push_deferring_access_checks (dk_no_deferred);
18484 tree default_argument = cp_parser_type_id (parser,
18485 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18486 NULL);
18487 pop_deferring_access_checks ();
18489 if (flag_concepts && type_uses_auto (default_argument))
18491 error_at (token->location,
18492 "invalid use of %<auto%> in default template argument");
18493 return error_mark_node;
18496 return default_argument;
18499 /* Parse a default argument for a template template-parameter. */
18501 static tree
18502 cp_parser_default_template_template_argument (cp_parser *parser)
18504 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18506 bool is_template;
18508 /* Consume the `='. */
18509 cp_lexer_consume_token (parser->lexer);
18510 /* Parse the id-expression. */
18511 push_deferring_access_checks (dk_no_deferred);
18512 /* save token before parsing the id-expression, for error
18513 reporting */
18514 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18515 tree default_argument
18516 = cp_parser_id_expression (parser,
18517 /*template_keyword_p=*/false,
18518 /*check_dependency_p=*/true,
18519 /*template_p=*/&is_template,
18520 /*declarator_p=*/false,
18521 /*optional_p=*/false);
18522 if (TREE_CODE (default_argument) == TYPE_DECL)
18523 /* If the id-expression was a template-id that refers to
18524 a template-class, we already have the declaration here,
18525 so no further lookup is needed. */
18527 else
18528 /* Look up the name. */
18529 default_argument
18530 = cp_parser_lookup_name (parser, default_argument,
18531 none_type,
18532 /*is_template=*/is_template,
18533 /*is_namespace=*/false,
18534 /*check_dependency=*/true,
18535 /*ambiguous_decls=*/NULL,
18536 token->location);
18537 /* See if the default argument is valid. */
18538 default_argument = check_template_template_default_arg (default_argument);
18539 pop_deferring_access_checks ();
18540 return default_argument;
18543 /* Parse a template-parameter.
18545 template-parameter:
18546 type-parameter
18547 parameter-declaration
18549 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18550 the parameter. The TREE_PURPOSE is the default value, if any.
18551 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18552 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18553 set to true iff this parameter is a parameter pack. */
18555 static tree
18556 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18557 bool *is_parameter_pack)
18559 cp_token *token;
18560 cp_parameter_declarator *parameter_declarator;
18561 tree parm;
18563 /* Assume it is a type parameter or a template parameter. */
18564 *is_non_type = false;
18565 /* Assume it not a parameter pack. */
18566 *is_parameter_pack = false;
18567 /* Peek at the next token. */
18568 token = cp_lexer_peek_token (parser->lexer);
18569 /* If it is `template', we have a type-parameter. */
18570 if (token->keyword == RID_TEMPLATE)
18571 return cp_parser_type_parameter (parser, is_parameter_pack);
18572 /* If it is `class' or `typename' we do not know yet whether it is a
18573 type parameter or a non-type parameter. Consider:
18575 template <typename T, typename T::X X> ...
18579 template <class C, class D*> ...
18581 Here, the first parameter is a type parameter, and the second is
18582 a non-type parameter. We can tell by looking at the token after
18583 the identifier -- if it is a `,', `=', or `>' then we have a type
18584 parameter. */
18585 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18587 /* Peek at the token after `class' or `typename'. */
18588 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18589 /* If it's an ellipsis, we have a template type parameter
18590 pack. */
18591 if (token->type == CPP_ELLIPSIS)
18592 return cp_parser_type_parameter (parser, is_parameter_pack);
18593 /* If it's an identifier, skip it. */
18594 if (token->type == CPP_NAME)
18595 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18596 /* Now, see if the token looks like the end of a template
18597 parameter. */
18598 if (token->type == CPP_COMMA
18599 || token->type == CPP_EQ
18600 || token->type == CPP_GREATER)
18601 return cp_parser_type_parameter (parser, is_parameter_pack);
18604 /* Otherwise, it is a non-type parameter or a constrained parameter.
18606 [temp.param]
18608 When parsing a default template-argument for a non-type
18609 template-parameter, the first non-nested `>' is taken as the end
18610 of the template parameter-list rather than a greater-than
18611 operator. */
18612 parameter_declarator
18613 = cp_parser_parameter_declaration (parser,
18614 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18615 /*template_parm_p=*/true,
18616 /*parenthesized_p=*/NULL);
18618 if (!parameter_declarator)
18619 return error_mark_node;
18621 /* If the parameter declaration is marked as a parameter pack, set
18622 *IS_PARAMETER_PACK to notify the caller. */
18623 if (parameter_declarator->template_parameter_pack_p)
18624 *is_parameter_pack = true;
18626 if (parameter_declarator->default_argument)
18628 /* Can happen in some cases of erroneous input (c++/34892). */
18629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18630 /* Consume the `...' for better error recovery. */
18631 cp_lexer_consume_token (parser->lexer);
18634 /* The parameter may have been constrained type parameter. */
18635 if (is_constrained_parameter (parameter_declarator))
18636 return finish_constrained_parameter (parser,
18637 parameter_declarator,
18638 is_non_type);
18640 // Now we're sure that the parameter is a non-type parameter.
18641 *is_non_type = true;
18643 parm = grokdeclarator (parameter_declarator->declarator,
18644 &parameter_declarator->decl_specifiers,
18645 TPARM, /*initialized=*/0,
18646 /*attrlist=*/NULL);
18647 if (parm == error_mark_node)
18648 return error_mark_node;
18650 return build_tree_list (parameter_declarator->default_argument, parm);
18653 /* Parse a type-parameter.
18655 type-parameter:
18656 class identifier [opt]
18657 class identifier [opt] = type-id
18658 typename identifier [opt]
18659 typename identifier [opt] = type-id
18660 template < template-parameter-list > class identifier [opt]
18661 template < template-parameter-list > class identifier [opt]
18662 = id-expression
18664 GNU Extension (variadic templates):
18666 type-parameter:
18667 class ... identifier [opt]
18668 typename ... identifier [opt]
18670 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18671 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18672 the declaration of the parameter.
18674 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18676 static tree
18677 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18679 cp_token *token;
18680 tree parameter;
18682 /* Look for a keyword to tell us what kind of parameter this is. */
18683 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18684 if (!token)
18685 return error_mark_node;
18687 switch (token->keyword)
18689 case RID_CLASS:
18690 case RID_TYPENAME:
18692 tree identifier;
18693 tree default_argument;
18695 /* If the next token is an ellipsis, we have a template
18696 argument pack. */
18697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18699 /* Consume the `...' token. */
18700 cp_lexer_consume_token (parser->lexer);
18701 maybe_warn_variadic_templates ();
18703 *is_parameter_pack = true;
18706 /* If the next token is an identifier, then it names the
18707 parameter. */
18708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18709 identifier = cp_parser_identifier (parser);
18710 else
18711 identifier = NULL_TREE;
18713 /* Create the parameter. */
18714 parameter = finish_template_type_parm (class_type_node, identifier);
18716 /* If the next token is an `=', we have a default argument. */
18717 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18719 default_argument
18720 = cp_parser_default_type_template_argument (parser);
18722 /* Template parameter packs cannot have default
18723 arguments. */
18724 if (*is_parameter_pack)
18726 if (identifier)
18727 error_at (token->location,
18728 "template parameter pack %qD cannot have a "
18729 "default argument", identifier);
18730 else
18731 error_at (token->location,
18732 "template parameter packs cannot have "
18733 "default arguments");
18734 default_argument = NULL_TREE;
18736 else if (check_for_bare_parameter_packs (default_argument))
18737 default_argument = error_mark_node;
18739 else
18740 default_argument = NULL_TREE;
18742 /* Create the combined representation of the parameter and the
18743 default argument. */
18744 parameter = build_tree_list (default_argument, parameter);
18746 break;
18748 case RID_TEMPLATE:
18750 tree identifier;
18751 tree default_argument;
18753 /* Look for the `<'. */
18754 cp_parser_require (parser, CPP_LESS, RT_LESS);
18755 /* Parse the template-parameter-list. */
18756 cp_parser_template_parameter_list (parser);
18757 /* Look for the `>'. */
18758 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18760 /* If template requirements are present, parse them. */
18761 if (flag_concepts)
18763 tree reqs = get_shorthand_constraints (current_template_parms);
18764 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18765 reqs = combine_constraint_expressions (reqs, dreqs);
18766 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18769 /* Look for the `class' or 'typename' keywords. */
18770 cp_parser_type_parameter_key (parser);
18771 /* If the next token is an ellipsis, we have a template
18772 argument pack. */
18773 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18775 /* Consume the `...' token. */
18776 cp_lexer_consume_token (parser->lexer);
18777 maybe_warn_variadic_templates ();
18779 *is_parameter_pack = true;
18781 /* If the next token is an `=', then there is a
18782 default-argument. If the next token is a `>', we are at
18783 the end of the parameter-list. If the next token is a `,',
18784 then we are at the end of this parameter. */
18785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18786 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18787 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18789 identifier = cp_parser_identifier (parser);
18790 /* Treat invalid names as if the parameter were nameless. */
18791 if (identifier == error_mark_node)
18792 identifier = NULL_TREE;
18794 else
18795 identifier = NULL_TREE;
18797 /* Create the template parameter. */
18798 parameter = finish_template_template_parm (class_type_node,
18799 identifier);
18801 /* If the next token is an `=', then there is a
18802 default-argument. */
18803 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18805 default_argument
18806 = cp_parser_default_template_template_argument (parser);
18808 /* Template parameter packs cannot have default
18809 arguments. */
18810 if (*is_parameter_pack)
18812 if (identifier)
18813 error_at (token->location,
18814 "template parameter pack %qD cannot "
18815 "have a default argument",
18816 identifier);
18817 else
18818 error_at (token->location, "template parameter packs cannot "
18819 "have default arguments");
18820 default_argument = NULL_TREE;
18823 else
18824 default_argument = NULL_TREE;
18826 /* Create the combined representation of the parameter and the
18827 default argument. */
18828 parameter = build_tree_list (default_argument, parameter);
18830 break;
18832 default:
18833 gcc_unreachable ();
18834 break;
18837 return parameter;
18840 /* Parse a template-id.
18842 template-id:
18843 template-name < template-argument-list [opt] >
18845 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18846 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18847 returned. Otherwise, if the template-name names a function, or set
18848 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18849 names a class, returns a TYPE_DECL for the specialization.
18851 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18852 uninstantiated templates. */
18854 static tree
18855 cp_parser_template_id (cp_parser *parser,
18856 bool template_keyword_p,
18857 bool check_dependency_p,
18858 enum tag_types tag_type,
18859 bool is_declaration)
18861 tree templ;
18862 tree arguments;
18863 tree template_id;
18864 cp_token_position start_of_id = 0;
18865 cp_token *next_token = NULL, *next_token_2 = NULL;
18866 bool is_identifier;
18868 /* If the next token corresponds to a template-id, there is no need
18869 to reparse it. */
18870 cp_token *token = cp_lexer_peek_token (parser->lexer);
18872 if (token->type == CPP_TEMPLATE_ID)
18874 cp_lexer_consume_token (parser->lexer);
18875 return saved_checks_value (token->u.tree_check_value);
18878 /* Avoid performing name lookup if there is no possibility of
18879 finding a template-id. */
18880 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18881 || (token->type == CPP_NAME
18882 && !cp_parser_nth_token_starts_template_argument_list_p
18883 (parser, 2)))
18885 cp_parser_error (parser, "expected template-id");
18886 return error_mark_node;
18889 /* Remember where the template-id starts. */
18890 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18891 start_of_id = cp_lexer_token_position (parser->lexer, false);
18893 push_deferring_access_checks (dk_deferred);
18895 /* Parse the template-name. */
18896 is_identifier = false;
18897 templ = cp_parser_template_name (parser, template_keyword_p,
18898 check_dependency_p,
18899 is_declaration,
18900 tag_type,
18901 &is_identifier);
18903 /* Push any access checks inside the firewall we're about to create. */
18904 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18905 pop_deferring_access_checks ();
18906 if (templ == error_mark_node || is_identifier)
18907 return templ;
18909 /* Since we're going to preserve any side-effects from this parse, set up a
18910 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18911 in the template arguments. */
18912 tentative_firewall firewall (parser);
18913 reopen_deferring_access_checks (checks);
18915 /* If we find the sequence `[:' after a template-name, it's probably
18916 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18917 parse correctly the argument list. */
18918 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18919 == CPP_OPEN_SQUARE)
18920 && next_token->flags & DIGRAPH
18921 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18922 == CPP_COLON)
18923 && !(next_token_2->flags & PREV_WHITE))
18925 cp_parser_parse_tentatively (parser);
18926 /* Change `:' into `::'. */
18927 next_token_2->type = CPP_SCOPE;
18928 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18929 CPP_LESS. */
18930 cp_lexer_consume_token (parser->lexer);
18932 /* Parse the arguments. */
18933 arguments = cp_parser_enclosed_template_argument_list (parser);
18934 if (!cp_parser_parse_definitely (parser))
18936 /* If we couldn't parse an argument list, then we revert our changes
18937 and return simply an error. Maybe this is not a template-id
18938 after all. */
18939 next_token_2->type = CPP_COLON;
18940 cp_parser_error (parser, "expected %<<%>");
18941 pop_deferring_access_checks ();
18942 return error_mark_node;
18944 /* Otherwise, emit an error about the invalid digraph, but continue
18945 parsing because we got our argument list. */
18946 if (permerror (next_token->location,
18947 "%<<::%> cannot begin a template-argument list"))
18949 static bool hint = false;
18950 inform (next_token->location,
18951 "%<<:%> is an alternate spelling for %<[%>."
18952 " Insert whitespace between %<<%> and %<::%>");
18953 if (!hint && !flag_permissive)
18955 inform (next_token->location, "(if you use %<-fpermissive%> "
18956 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18957 "accept your code)");
18958 hint = true;
18962 else
18964 /* Look for the `<' that starts the template-argument-list. */
18965 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18967 pop_deferring_access_checks ();
18968 return error_mark_node;
18970 /* Parse the arguments. */
18971 arguments = cp_parser_enclosed_template_argument_list (parser);
18973 if ((cxx_dialect > cxx17)
18974 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18975 && !template_keyword_p
18976 && (cp_parser_error_occurred (parser)
18977 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18979 /* This didn't go well. */
18980 if (TREE_CODE (templ) == FUNCTION_DECL)
18982 /* C++20 says that "function-name < a;" is now ill-formed. */
18983 if (cp_parser_error_occurred (parser))
18985 error_at (token->location, "invalid template-argument-list");
18986 inform (token->location, "function name as the left hand "
18987 "operand of %<<%> is ill-formed in C++20; wrap the "
18988 "function name in %<()%>");
18990 else
18991 /* We expect "f<targs>" to be followed by "(args)". */
18992 error_at (cp_lexer_peek_token (parser->lexer)->location,
18993 "expected %<(%> after template-argument-list");
18994 if (start_of_id)
18995 /* Purge all subsequent tokens. */
18996 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18998 else
18999 cp_parser_simulate_error (parser);
19000 pop_deferring_access_checks ();
19001 return error_mark_node;
19005 /* Set the location to be of the form:
19006 template-name < template-argument-list [opt] >
19007 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19008 with caret == start at the start of the template-name,
19009 ranging until the closing '>'. */
19010 location_t combined_loc
19011 = make_location (token->location, token->location, parser->lexer);
19013 /* Check for concepts autos where they don't belong. We could
19014 identify types in some cases of identifier TEMPL, looking ahead
19015 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
19016 types. We reject them in functions, but if what we have is an
19017 identifier, even with none_type we can't conclude it's NOT a
19018 type, we have to wait for template substitution. */
19019 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
19020 template_id = error_mark_node;
19021 /* Build a representation of the specialization. */
19022 else if (identifier_p (templ))
19023 template_id = build_min_nt_loc (combined_loc,
19024 TEMPLATE_ID_EXPR,
19025 templ, arguments);
19026 else if (DECL_TYPE_TEMPLATE_P (templ)
19027 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
19029 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
19030 template (rather than some instantiation thereof) only if
19031 is not nested within some other construct. For example, in
19032 "template <typename T> void f(T) { A<T>::", A<T> is just an
19033 instantiation of A. */
19034 bool entering_scope
19035 = (template_parm_scope_p ()
19036 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
19037 template_id
19038 = finish_template_type (templ, arguments, entering_scope);
19040 else if (concept_definition_p (templ))
19042 /* The caller will decide whether this is a concept check or type
19043 constraint. */
19044 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
19045 boolean_type_node, templ, arguments);
19047 else if (variable_template_p (templ))
19049 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
19050 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19051 SET_EXPR_LOCATION (template_id, combined_loc);
19053 else if (TREE_CODE (templ) == TYPE_DECL
19054 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
19056 /* Some type template in dependent scope. */
19057 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
19058 name = build_min_nt_loc (combined_loc,
19059 TEMPLATE_ID_EXPR,
19060 name, arguments);
19061 template_id = templ;
19063 else
19065 /* If it's not a class-template or a template-template, it should be
19066 a function-template. */
19067 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
19069 template_id = lookup_template_function (templ, arguments);
19070 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19071 SET_EXPR_LOCATION (template_id, combined_loc);
19074 /* If parsing tentatively, replace the sequence of tokens that makes
19075 up the template-id with a CPP_TEMPLATE_ID token. That way,
19076 should we re-parse the token stream, we will not have to repeat
19077 the effort required to do the parse, nor will we issue duplicate
19078 error messages about problems during instantiation of the
19079 template. */
19080 if (start_of_id
19081 /* Don't do this if we had a parse error in a declarator; re-parsing
19082 might succeed if a name changes meaning (60361). */
19083 && !(cp_parser_error_occurred (parser)
19084 && cp_parser_parsing_tentatively (parser)
19085 && parser->in_declarator_p))
19087 /* Reset the contents of the START_OF_ID token. */
19088 token->type = CPP_TEMPLATE_ID;
19089 token->location = combined_loc;
19091 /* Retrieve any deferred checks. Do not pop this access checks yet
19092 so the memory will not be reclaimed during token replacing below. */
19093 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
19094 token->tree_check_p = true;
19095 token->u.tree_check_value->value = template_id;
19096 token->u.tree_check_value->checks = get_deferred_access_checks ();
19097 token->keyword = RID_MAX;
19099 /* Purge all subsequent tokens. */
19100 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19102 /* ??? Can we actually assume that, if template_id ==
19103 error_mark_node, we will have issued a diagnostic to the
19104 user, as opposed to simply marking the tentative parse as
19105 failed? */
19106 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
19107 error_at (token->location, "parse error in template argument list");
19110 pop_to_parent_deferring_access_checks ();
19111 return template_id;
19114 /* Like cp_parser_template_id, called in non-type context. */
19116 static tree
19117 cp_parser_template_id_expr (cp_parser *parser,
19118 bool template_keyword_p,
19119 bool check_dependency_p,
19120 bool is_declaration)
19122 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
19123 none_type, is_declaration);
19124 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
19125 && concept_check_p (x))
19126 /* We didn't check the arguments in cp_parser_template_id; do that now. */
19127 return build_concept_id (x);
19128 return x;
19131 /* Parse a template-name.
19133 template-name:
19134 identifier
19136 The standard should actually say:
19138 template-name:
19139 identifier
19140 operator-function-id
19142 A defect report has been filed about this issue.
19144 A conversion-function-id cannot be a template name because they cannot
19145 be part of a template-id. In fact, looking at this code:
19147 a.operator K<int>()
19149 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
19150 It is impossible to call a templated conversion-function-id with an
19151 explicit argument list, since the only allowed template parameter is
19152 the type to which it is converting.
19154 If TEMPLATE_KEYWORD_P is true, then we have just seen the
19155 `template' keyword, in a construction like:
19157 T::template f<3>()
19159 In that case `f' is taken to be a template-name, even though there
19160 is no way of knowing for sure.
19162 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
19163 name refers to a set of overloaded functions, at least one of which
19164 is a template, or an IDENTIFIER_NODE with the name of the template,
19165 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
19166 names are looked up inside uninstantiated templates. */
19168 static tree
19169 cp_parser_template_name (cp_parser* parser,
19170 bool template_keyword_p,
19171 bool check_dependency_p,
19172 bool is_declaration,
19173 enum tag_types tag_type,
19174 bool *is_identifier)
19176 tree identifier;
19177 tree decl;
19178 cp_token *token = cp_lexer_peek_token (parser->lexer);
19180 /* If the next token is `operator', then we have either an
19181 operator-function-id or a conversion-function-id. */
19182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
19184 /* We don't know whether we're looking at an
19185 operator-function-id or a conversion-function-id. */
19186 cp_parser_parse_tentatively (parser);
19187 /* Try an operator-function-id. */
19188 identifier = cp_parser_operator_function_id (parser);
19189 /* If that didn't work, try a conversion-function-id. */
19190 if (!cp_parser_parse_definitely (parser))
19192 cp_parser_error (parser, "expected template-name");
19193 return error_mark_node;
19196 /* Look for the identifier. */
19197 else
19198 identifier = cp_parser_identifier (parser);
19200 /* If we didn't find an identifier, we don't have a template-id. */
19201 if (identifier == error_mark_node)
19202 return error_mark_node;
19204 /* If the name immediately followed the `template' keyword, then it
19205 is a template-name. However, if the next token is not `<', then
19206 we do not treat it as a template-name, since it is not being used
19207 as part of a template-id. This enables us to handle constructs
19208 like:
19210 template <typename T> struct S { S(); };
19211 template <typename T> S<T>::S();
19213 correctly. We would treat `S' as a template -- if it were `S<T>'
19214 -- but we do not if there is no `<'. */
19216 if (processing_template_decl
19217 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19219 /* In a declaration, in a dependent context, we pretend that the
19220 "template" keyword was present in order to improve error
19221 recovery. For example, given:
19223 template <typename T> void f(T::X<int>);
19225 we want to treat "X<int>" as a template-id. */
19226 if (is_declaration
19227 && !template_keyword_p
19228 && parser->scope && TYPE_P (parser->scope)
19229 && check_dependency_p
19230 && dependent_scope_p (parser->scope)
19231 /* Do not do this for dtors (or ctors), since they never
19232 need the template keyword before their name. */
19233 && !constructor_name_p (identifier, parser->scope))
19235 cp_token_position start = 0;
19237 /* Explain what went wrong. */
19238 error_at (token->location, "non-template %qD used as template",
19239 identifier);
19240 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
19241 parser->scope, identifier);
19242 /* If parsing tentatively, find the location of the "<" token. */
19243 if (cp_parser_simulate_error (parser))
19244 start = cp_lexer_token_position (parser->lexer, true);
19245 /* Parse the template arguments so that we can issue error
19246 messages about them. */
19247 cp_lexer_consume_token (parser->lexer);
19248 cp_parser_enclosed_template_argument_list (parser);
19249 /* Skip tokens until we find a good place from which to
19250 continue parsing. */
19251 cp_parser_skip_to_closing_parenthesis (parser,
19252 /*recovering=*/true,
19253 /*or_comma=*/true,
19254 /*consume_paren=*/false);
19255 /* If parsing tentatively, permanently remove the
19256 template argument list. That will prevent duplicate
19257 error messages from being issued about the missing
19258 "template" keyword. */
19259 if (start)
19260 cp_lexer_purge_tokens_after (parser->lexer, start);
19261 if (is_identifier)
19262 *is_identifier = true;
19263 parser->context->object_type = NULL_TREE;
19264 return identifier;
19267 /* If the "template" keyword is present, then there is generally
19268 no point in doing name-lookup, so we just return IDENTIFIER.
19269 But, if the qualifying scope is non-dependent then we can
19270 (and must) do name-lookup normally. */
19271 if (template_keyword_p)
19273 tree scope = (parser->scope ? parser->scope
19274 : parser->context->object_type);
19275 if (scope && TYPE_P (scope)
19276 && (!CLASS_TYPE_P (scope)
19277 || (check_dependency_p && dependent_scope_p (scope))))
19279 /* We're optimizing away the call to cp_parser_lookup_name, but
19280 we still need to do this. */
19281 parser->object_scope = parser->context->object_type;
19282 parser->context->object_type = NULL_TREE;
19283 return identifier;
19288 /* cp_parser_lookup_name clears OBJECT_TYPE. */
19289 tree scope = (parser->scope ? parser->scope
19290 : parser->context->object_type);
19292 /* Look up the name. */
19293 decl = cp_parser_lookup_name (parser, identifier,
19294 tag_type,
19295 /*is_template=*/1 + template_keyword_p,
19296 /*is_namespace=*/false,
19297 check_dependency_p,
19298 /*ambiguous_decls=*/NULL,
19299 token->location);
19301 decl = strip_using_decl (decl);
19303 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19304 template-argument-list if it follows a name that is not a
19305 conversion-function-id and
19306 - that follows the keyword template or a ~ after a nested-name-specifier or
19307 in a class member access expression, or
19308 - for which name lookup finds the injected-class-name of a class template
19309 or finds any declaration of a template, or
19310 - that is an unqualified name for which name lookup either finds one or
19311 more functions or finds nothing, or
19312 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19313 (9.3.4), or in a type-only context other than a nested-name-specifier
19314 (13.8). */
19316 /* Handle injected-class-name. */
19317 decl = maybe_get_template_decl_from_type_decl (decl);
19319 /* If DECL is a template, then the name was a template-name. */
19320 if (TREE_CODE (decl) == TEMPLATE_DECL)
19322 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19323 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19325 tree d = DECL_TEMPLATE_RESULT (decl);
19326 tree attr;
19327 if (TREE_CODE (d) == TYPE_DECL)
19328 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19329 else
19330 attr = DECL_ATTRIBUTES (d);
19331 if (TREE_UNAVAILABLE (decl))
19333 attr = lookup_attribute ("unavailable", attr);
19334 error_unavailable_use (decl, attr);
19336 else if (TREE_DEPRECATED (decl)
19337 && deprecated_state != DEPRECATED_SUPPRESS)
19339 attr = lookup_attribute ("deprecated", attr);
19340 warn_deprecated_use (decl, attr);
19344 else
19346 /* Look through an overload set for any templates. */
19347 bool found = false;
19349 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19350 !found && iter; ++iter)
19351 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19352 found = true;
19354 /* "an unqualified name for which name lookup either finds one or more
19355 functions or finds nothing". */
19356 if (!found
19357 && (cxx_dialect > cxx17)
19358 && !scope
19359 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19360 && tag_type == none_type)
19362 /* The "more functions" case. Just use the OVERLOAD as normally.
19363 We don't use is_overloaded_fn here to avoid considering
19364 BASELINKs. */
19365 if (TREE_CODE (decl) == OVERLOAD
19366 /* Name lookup found one function. */
19367 || TREE_CODE (decl) == FUNCTION_DECL
19368 /* Name lookup found nothing. */
19369 || decl == error_mark_node)
19370 found = true;
19373 /* "that follows the keyword template"..."in a type-only context" */
19374 if (!found && scope
19375 && (template_keyword_p || tag_type != none_type)
19376 && dependentish_scope_p (scope)
19377 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19378 found = true;
19380 if (!found)
19382 /* The name does not name a template. */
19383 cp_parser_error (parser, "expected template-name");
19384 return error_mark_node;
19386 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19387 || TREE_CODE (decl) == USING_DECL
19388 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19389 || (TREE_CODE (decl) == TYPE_DECL
19390 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19391 /* Repeat the lookup at instantiation time. */
19392 decl = identifier;
19395 return decl;
19398 /* Parse a template-argument-list.
19400 template-argument-list:
19401 template-argument ... [opt]
19402 template-argument-list , template-argument ... [opt]
19404 Returns a TREE_VEC containing the arguments. */
19406 static tree
19407 cp_parser_template_argument_list (cp_parser* parser)
19409 bool saved_in_template_argument_list_p;
19410 bool saved_ice_p;
19411 bool saved_non_ice_p;
19413 /* Don't create location wrapper nodes within a template-argument-list. */
19414 auto_suppress_location_wrappers sentinel;
19416 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19417 parser->in_template_argument_list_p = true;
19418 /* Even if the template-id appears in an integral
19419 constant-expression, the contents of the argument list do
19420 not. */
19421 saved_ice_p = parser->integral_constant_expression_p;
19422 parser->integral_constant_expression_p = false;
19423 saved_non_ice_p = parser->non_integral_constant_expression_p;
19424 parser->non_integral_constant_expression_p = false;
19426 /* Parse the arguments. */
19427 auto_vec<tree, 10> args;
19430 if (!args.is_empty ())
19431 /* Consume the comma. */
19432 cp_lexer_consume_token (parser->lexer);
19434 /* Parse the template-argument. */
19435 tree argument = cp_parser_template_argument (parser);
19437 /* If the next token is an ellipsis, we're expanding a template
19438 argument pack. */
19439 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19441 if (argument == error_mark_node)
19443 cp_token *token = cp_lexer_peek_token (parser->lexer);
19444 error_at (token->location,
19445 "expected parameter pack before %<...%>");
19447 /* Consume the `...' token. */
19448 cp_lexer_consume_token (parser->lexer);
19450 /* Make the argument into a TYPE_PACK_EXPANSION or
19451 EXPR_PACK_EXPANSION. */
19452 argument = make_pack_expansion (argument);
19455 args.safe_push (argument);
19457 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19459 int n_args = args.length ();
19460 tree vec = make_tree_vec (n_args);
19462 for (int i = 0; i < n_args; i++)
19463 TREE_VEC_ELT (vec, i) = args[i];
19465 parser->non_integral_constant_expression_p = saved_non_ice_p;
19466 parser->integral_constant_expression_p = saved_ice_p;
19467 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19468 if (CHECKING_P)
19469 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19470 return vec;
19473 /* Parse a template-argument.
19475 template-argument:
19476 assignment-expression
19477 type-id
19478 id-expression
19480 The representation is that of an assignment-expression, type-id, or
19481 id-expression -- except that the qualified id-expression is
19482 evaluated, so that the value returned is either a DECL or an
19483 OVERLOAD.
19485 Although the standard says "assignment-expression", it forbids
19486 throw-expressions or assignments in the template argument.
19487 Therefore, we use "conditional-expression" instead. */
19489 static tree
19490 cp_parser_template_argument (cp_parser* parser)
19492 tree argument;
19493 bool template_p;
19494 bool address_p;
19495 bool maybe_type_id = false;
19496 cp_token *token = NULL, *argument_start_token = NULL;
19497 location_t loc = 0;
19498 cp_id_kind idk;
19500 /* There's really no way to know what we're looking at, so we just
19501 try each alternative in order.
19503 [temp.arg]
19505 In a template-argument, an ambiguity between a type-id and an
19506 expression is resolved to a type-id, regardless of the form of
19507 the corresponding template-parameter.
19509 Therefore, we try a type-id first. */
19510 cp_parser_parse_tentatively (parser);
19511 argument = cp_parser_template_type_arg (parser);
19512 /* If there was no error parsing the type-id but the next token is a
19513 '>>', our behavior depends on which dialect of C++ we're
19514 parsing. In C++98, we probably found a typo for '> >'. But there
19515 are type-id which are also valid expressions. For instance:
19517 struct X { int operator >> (int); };
19518 template <int V> struct Foo {};
19519 Foo<X () >> 5> r;
19521 Here 'X()' is a valid type-id of a function type, but the user just
19522 wanted to write the expression "X() >> 5". Thus, we remember that we
19523 found a valid type-id, but we still try to parse the argument as an
19524 expression to see what happens.
19526 In C++0x, the '>>' will be considered two separate '>'
19527 tokens. */
19528 if (!cp_parser_error_occurred (parser)
19529 && ((cxx_dialect == cxx98
19530 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19531 /* Similarly for >= which
19532 cp_parser_next_token_ends_template_argument_p treats for
19533 diagnostics purposes as mistyped > =, but can be valid
19534 after a type-id. */
19535 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19537 maybe_type_id = true;
19538 cp_parser_abort_tentative_parse (parser);
19540 else
19542 /* If the next token isn't a `,' or a `>', then this argument wasn't
19543 really finished. This means that the argument is not a valid
19544 type-id. */
19545 if (!cp_parser_next_token_ends_template_argument_p (parser))
19546 cp_parser_error (parser, "expected template-argument");
19547 /* If that worked, we're done. */
19548 if (cp_parser_parse_definitely (parser))
19549 return argument;
19551 /* We're still not sure what the argument will be. */
19552 cp_parser_parse_tentatively (parser);
19553 /* Try a template. */
19554 argument_start_token = cp_lexer_peek_token (parser->lexer);
19555 argument = cp_parser_id_expression (parser,
19556 /*template_keyword_p=*/false,
19557 /*check_dependency_p=*/true,
19558 &template_p,
19559 /*declarator_p=*/false,
19560 /*optional_p=*/false);
19561 /* If the next token isn't a `,' or a `>', then this argument wasn't
19562 really finished. */
19563 if (!cp_parser_next_token_ends_template_argument_p (parser))
19564 cp_parser_error (parser, "expected template-argument");
19565 if (!cp_parser_error_occurred (parser))
19567 /* Figure out what is being referred to. If the id-expression
19568 was for a class template specialization, then we will have a
19569 TYPE_DECL at this point. There is no need to do name lookup
19570 at this point in that case. */
19571 if (TREE_CODE (argument) != TYPE_DECL)
19572 argument = cp_parser_lookup_name (parser, argument,
19573 none_type,
19574 /*is_template=*/template_p,
19575 /*is_namespace=*/false,
19576 /*check_dependency=*/true,
19577 /*ambiguous_decls=*/NULL,
19578 argument_start_token->location);
19579 if (TREE_CODE (argument) != TEMPLATE_DECL
19580 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19581 cp_parser_error (parser, "expected template-name");
19583 if (cp_parser_parse_definitely (parser))
19585 if (TREE_UNAVAILABLE (argument))
19586 error_unavailable_use (argument, NULL_TREE);
19587 else if (TREE_DEPRECATED (argument))
19588 warn_deprecated_use (argument, NULL_TREE);
19589 return argument;
19591 /* It must be a non-type argument. In C++17 any constant-expression is
19592 allowed. */
19593 if (cxx_dialect > cxx14)
19594 goto general_expr;
19596 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19598 -- an integral constant-expression of integral or enumeration
19599 type; or
19601 -- the name of a non-type template-parameter; or
19603 -- the name of an object or function with external linkage...
19605 -- the address of an object or function with external linkage...
19607 -- a pointer to member... */
19608 /* Look for a non-type template parameter. */
19609 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19611 cp_parser_parse_tentatively (parser);
19612 argument = cp_parser_primary_expression (parser,
19613 /*address_p=*/false,
19614 /*cast_p=*/false,
19615 /*template_arg_p=*/true,
19616 &idk);
19617 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19618 || !cp_parser_next_token_ends_template_argument_p (parser))
19619 cp_parser_simulate_error (parser);
19620 if (cp_parser_parse_definitely (parser))
19621 return argument;
19624 /* If the next token is "&", the argument must be the address of an
19625 object or function with external linkage. */
19626 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19627 if (address_p)
19629 loc = cp_lexer_peek_token (parser->lexer)->location;
19630 cp_lexer_consume_token (parser->lexer);
19632 /* See if we might have an id-expression. */
19633 token = cp_lexer_peek_token (parser->lexer);
19634 if (token->type == CPP_NAME
19635 || token->keyword == RID_OPERATOR
19636 || token->type == CPP_SCOPE
19637 || token->type == CPP_TEMPLATE_ID
19638 || token->type == CPP_NESTED_NAME_SPECIFIER)
19640 cp_parser_parse_tentatively (parser);
19641 argument = cp_parser_primary_expression (parser,
19642 address_p,
19643 /*cast_p=*/false,
19644 /*template_arg_p=*/true,
19645 &idk);
19646 if (cp_parser_error_occurred (parser)
19647 || !cp_parser_next_token_ends_template_argument_p (parser))
19648 cp_parser_abort_tentative_parse (parser);
19649 else
19651 tree probe;
19653 if (INDIRECT_REF_P (argument))
19655 /* Strip the dereference temporarily. */
19656 gcc_assert (REFERENCE_REF_P (argument));
19657 argument = TREE_OPERAND (argument, 0);
19660 /* If we're in a template, we represent a qualified-id referring
19661 to a static data member as a SCOPE_REF even if the scope isn't
19662 dependent so that we can check access control later. */
19663 probe = argument;
19664 if (TREE_CODE (probe) == SCOPE_REF)
19665 probe = TREE_OPERAND (probe, 1);
19666 if (VAR_P (probe))
19668 /* A variable without external linkage might still be a
19669 valid constant-expression, so no error is issued here
19670 if the external-linkage check fails. */
19671 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19672 cp_parser_simulate_error (parser);
19674 else if (is_overloaded_fn (argument))
19675 /* All overloaded functions are allowed; if the external
19676 linkage test does not pass, an error will be issued
19677 later. */
19679 else if (address_p
19680 && (TREE_CODE (argument) == OFFSET_REF
19681 || TREE_CODE (argument) == SCOPE_REF))
19682 /* A pointer-to-member. */
19684 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19686 else
19687 cp_parser_simulate_error (parser);
19689 if (cp_parser_parse_definitely (parser))
19691 if (address_p)
19692 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19693 NULL_TREE, tf_warning_or_error);
19694 else
19695 argument = convert_from_reference (argument);
19696 return argument;
19700 /* If the argument started with "&", there are no other valid
19701 alternatives at this point. */
19702 if (address_p)
19704 cp_parser_error (parser, "invalid non-type template argument");
19705 return error_mark_node;
19708 general_expr:
19709 /* If the argument wasn't successfully parsed as a type-id followed
19710 by '>>', the argument can only be a constant expression now.
19711 Otherwise, we try parsing the constant-expression tentatively,
19712 because the argument could really be a type-id. */
19713 if (maybe_type_id)
19714 cp_parser_parse_tentatively (parser);
19716 if (cxx_dialect <= cxx14)
19717 argument = cp_parser_constant_expression (parser);
19718 else
19720 /* In C++20, we can encounter a braced-init-list. */
19721 if (cxx_dialect >= cxx20
19722 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19723 return cp_parser_braced_list (parser);
19725 /* With C++17 generalized non-type template arguments we need to handle
19726 lvalue constant expressions, too. */
19727 argument = cp_parser_assignment_expression (parser);
19728 require_potential_constant_expression (argument);
19731 if (!maybe_type_id)
19732 return argument;
19733 if (!cp_parser_next_token_ends_template_argument_p (parser))
19734 cp_parser_error (parser, "expected template-argument");
19735 if (cp_parser_parse_definitely (parser))
19736 return argument;
19737 /* We did our best to parse the argument as a non type-id, but that
19738 was the only alternative that matched (albeit with a '>' after
19739 it). We can assume it's just a typo from the user, and a
19740 diagnostic will then be issued. */
19741 return cp_parser_template_type_arg (parser);
19744 /* Parse an explicit-instantiation.
19746 explicit-instantiation:
19747 template declaration
19749 Although the standard says `declaration', what it really means is:
19751 explicit-instantiation:
19752 template decl-specifier-seq [opt] declarator [opt] ;
19754 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19755 supposed to be allowed. A defect report has been filed about this
19756 issue.
19758 GNU Extension:
19760 explicit-instantiation:
19761 storage-class-specifier template
19762 decl-specifier-seq [opt] declarator [opt] ;
19763 function-specifier template
19764 decl-specifier-seq [opt] declarator [opt] ; */
19766 static void
19767 cp_parser_explicit_instantiation (cp_parser* parser)
19769 int declares_class_or_enum;
19770 cp_decl_specifier_seq decl_specifiers;
19771 tree extension_specifier = NULL_TREE;
19773 auto_timevar tv (TV_TEMPLATE_INST);
19775 /* Look for an (optional) storage-class-specifier or
19776 function-specifier. */
19777 if (cp_parser_allow_gnu_extensions_p (parser))
19779 extension_specifier
19780 = cp_parser_storage_class_specifier_opt (parser);
19781 if (!extension_specifier)
19782 extension_specifier
19783 = cp_parser_function_specifier_opt (parser,
19784 /*decl_specs=*/NULL);
19787 /* Look for the `template' keyword. */
19788 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19789 /* Let the front end know that we are processing an explicit
19790 instantiation. */
19791 begin_explicit_instantiation ();
19792 /* [temp.explicit] says that we are supposed to ignore access
19793 control while processing explicit instantiation directives. */
19794 push_deferring_access_checks (dk_no_check);
19795 /* Parse a decl-specifier-seq. */
19796 cp_parser_decl_specifier_seq (parser,
19797 CP_PARSER_FLAGS_OPTIONAL,
19798 &decl_specifiers,
19799 &declares_class_or_enum);
19801 cp_omp_declare_simd_data odsd;
19802 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19803 cp_parser_handle_directive_omp_attributes (parser,
19804 &decl_specifiers.attributes,
19805 &odsd, true);
19807 /* If there was exactly one decl-specifier, and it declared a class,
19808 and there's no declarator, then we have an explicit type
19809 instantiation. */
19810 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19812 tree type = check_tag_decl (&decl_specifiers,
19813 /*explicit_type_instantiation_p=*/true);
19814 /* Turn access control back on for names used during
19815 template instantiation. */
19816 pop_deferring_access_checks ();
19817 if (type)
19818 do_type_instantiation (type, extension_specifier,
19819 /*complain=*/tf_error);
19821 else
19823 cp_declarator *declarator;
19824 tree decl;
19826 /* Parse the declarator. */
19827 declarator
19828 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19829 CP_PARSER_FLAGS_NONE,
19830 /*ctor_dtor_or_conv_p=*/NULL,
19831 /*parenthesized_p=*/NULL,
19832 /*member_p=*/false,
19833 /*friend_p=*/false,
19834 /*static_p=*/false);
19835 if (declares_class_or_enum & 2)
19836 cp_parser_check_for_definition_in_return_type (declarator,
19837 decl_specifiers.type,
19838 decl_specifiers.locations[ds_type_spec]);
19839 if (declarator != cp_error_declarator)
19841 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19842 permerror (decl_specifiers.locations[ds_inline],
19843 "explicit instantiation shall not use"
19844 " %<inline%> specifier");
19845 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19846 permerror (decl_specifiers.locations[ds_constexpr],
19847 "explicit instantiation shall not use"
19848 " %<constexpr%> specifier");
19849 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19850 permerror (decl_specifiers.locations[ds_consteval],
19851 "explicit instantiation shall not use"
19852 " %<consteval%> specifier");
19854 decl = grokdeclarator (declarator, &decl_specifiers,
19855 NORMAL, 0, &decl_specifiers.attributes);
19856 /* Turn access control back on for names used during
19857 template instantiation. */
19858 pop_deferring_access_checks ();
19859 /* Do the explicit instantiation. */
19860 do_decl_instantiation (decl, extension_specifier);
19862 else
19864 pop_deferring_access_checks ();
19865 /* Skip the body of the explicit instantiation. */
19866 cp_parser_skip_to_end_of_statement (parser);
19869 /* We're done with the instantiation. */
19870 end_explicit_instantiation ();
19872 cp_parser_consume_semicolon_at_end_of_statement (parser);
19874 cp_finalize_omp_declare_simd (parser, &odsd);
19877 /* Parse an explicit-specialization.
19879 explicit-specialization:
19880 template < > declaration
19882 Although the standard says `declaration', what it really means is:
19884 explicit-specialization:
19885 template <> decl-specifier [opt] init-declarator [opt] ;
19886 template <> function-definition
19887 template <> explicit-specialization
19888 template <> template-declaration */
19890 static void
19891 cp_parser_explicit_specialization (cp_parser* parser)
19893 cp_token *token = cp_lexer_peek_token (parser->lexer);
19895 /* Look for the `template' keyword. */
19896 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19897 /* Look for the `<'. */
19898 cp_parser_require (parser, CPP_LESS, RT_LESS);
19899 /* Look for the `>'. */
19900 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19901 /* We have processed another parameter list. */
19902 ++parser->num_template_parameter_lists;
19904 /* [temp]
19906 A template ... explicit specialization ... shall not have C
19907 linkage. */
19908 bool need_lang_pop = current_lang_name == lang_name_c;
19909 if (need_lang_pop)
19911 error_at (token->location, "template specialization with C linkage");
19912 maybe_show_extern_c_location ();
19914 /* Give it C++ linkage to avoid confusing other parts of the
19915 front end. */
19916 push_lang_context (lang_name_cplusplus);
19919 /* Let the front end know that we are beginning a specialization. */
19920 if (begin_specialization ())
19922 /* If the next keyword is `template', we need to figure out
19923 whether or not we're looking a template-declaration. */
19924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19926 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19927 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19928 cp_parser_template_declaration_after_export (parser,
19929 /*member_p=*/false);
19930 else
19931 cp_parser_explicit_specialization (parser);
19933 else
19934 /* Parse the dependent declaration. */
19935 cp_parser_single_declaration (parser,
19936 /*checks=*/NULL,
19937 /*member_p=*/false,
19938 /*explicit_specialization_p=*/true,
19939 /*friend_p=*/NULL);
19942 /* We're done with the specialization. */
19943 end_specialization ();
19945 /* For the erroneous case of a template with C linkage, we pushed an
19946 implicit C++ linkage scope; exit that scope now. */
19947 if (need_lang_pop)
19948 pop_lang_context ();
19950 /* We're done with this parameter list. */
19951 --parser->num_template_parameter_lists;
19954 /* Preserve the attributes across a garbage collect (by making it a GC
19955 root), which can occur when parsing a member function. */
19957 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19959 /* Parse a type-specifier.
19961 type-specifier:
19962 simple-type-specifier
19963 class-specifier
19964 enum-specifier
19965 elaborated-type-specifier
19966 cv-qualifier
19968 GNU Extension:
19970 type-specifier:
19971 __complex__
19973 Returns a representation of the type-specifier. For a
19974 class-specifier, enum-specifier, or elaborated-type-specifier, a
19975 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19977 The parser flags FLAGS is used to control type-specifier parsing.
19979 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19980 in a decl-specifier-seq.
19982 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19983 class-specifier, enum-specifier, or elaborated-type-specifier, then
19984 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19985 if a type is declared; 2 if it is defined. Otherwise, it is set to
19986 zero.
19988 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19989 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19990 is set to FALSE. */
19992 static tree
19993 cp_parser_type_specifier (cp_parser* parser,
19994 cp_parser_flags flags,
19995 cp_decl_specifier_seq *decl_specs,
19996 bool is_declaration,
19997 int* declares_class_or_enum,
19998 bool* is_cv_qualifier)
20000 tree type_spec = NULL_TREE;
20001 cp_token *token;
20002 enum rid keyword;
20003 cp_decl_spec ds = ds_last;
20005 /* Assume this type-specifier does not declare a new type. */
20006 if (declares_class_or_enum)
20007 *declares_class_or_enum = 0;
20008 /* And that it does not specify a cv-qualifier. */
20009 if (is_cv_qualifier)
20010 *is_cv_qualifier = false;
20011 /* Peek at the next token. */
20012 token = cp_lexer_peek_token (parser->lexer);
20014 /* If we're looking at a keyword, we can use that to guide the
20015 production we choose. */
20016 keyword = token->keyword;
20017 switch (keyword)
20019 case RID_ENUM:
20020 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20021 goto elaborated_type_specifier;
20023 /* Look for the enum-specifier. */
20024 type_spec = cp_parser_enum_specifier (parser);
20025 /* If that worked, we're done. */
20026 if (type_spec)
20028 if (declares_class_or_enum)
20029 *declares_class_or_enum = 2;
20030 if (decl_specs)
20031 cp_parser_set_decl_spec_type (decl_specs,
20032 type_spec,
20033 token,
20034 /*type_definition_p=*/true);
20035 return type_spec;
20037 else
20038 goto elaborated_type_specifier;
20040 /* Any of these indicate either a class-specifier, or an
20041 elaborated-type-specifier. */
20042 case RID_CLASS:
20043 case RID_STRUCT:
20044 case RID_UNION:
20045 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20046 goto elaborated_type_specifier;
20048 /* Parse tentatively so that we can back up if we don't find a
20049 class-specifier. */
20050 cp_parser_parse_tentatively (parser);
20051 if (decl_specs->attributes)
20052 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
20053 /* Look for the class-specifier. */
20054 type_spec = cp_parser_class_specifier (parser);
20055 if (decl_specs->attributes)
20056 cp_parser_decl_specs_attrs->pop ();
20057 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
20058 /* If that worked, we're done. */
20059 if (cp_parser_parse_definitely (parser))
20061 if (declares_class_or_enum)
20062 *declares_class_or_enum = 2;
20063 if (decl_specs)
20064 cp_parser_set_decl_spec_type (decl_specs,
20065 type_spec,
20066 token,
20067 /*type_definition_p=*/true);
20068 return type_spec;
20071 /* Fall through. */
20072 elaborated_type_specifier:
20073 /* We're declaring (not defining) a class or enum. */
20074 if (declares_class_or_enum)
20075 *declares_class_or_enum = 1;
20077 /* Fall through. */
20078 case RID_TYPENAME:
20079 /* Look for an elaborated-type-specifier. */
20080 type_spec
20081 = (cp_parser_elaborated_type_specifier
20082 (parser,
20083 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
20084 is_declaration));
20085 if (decl_specs)
20086 cp_parser_set_decl_spec_type (decl_specs,
20087 type_spec,
20088 token,
20089 /*type_definition_p=*/false);
20090 return type_spec;
20092 case RID_CONST:
20093 ds = ds_const;
20094 if (is_cv_qualifier)
20095 *is_cv_qualifier = true;
20096 break;
20098 case RID_VOLATILE:
20099 ds = ds_volatile;
20100 if (is_cv_qualifier)
20101 *is_cv_qualifier = true;
20102 break;
20104 case RID_RESTRICT:
20105 ds = ds_restrict;
20106 if (is_cv_qualifier)
20107 *is_cv_qualifier = true;
20108 break;
20110 case RID_COMPLEX:
20111 /* The `__complex__' keyword is a GNU extension. */
20112 ds = ds_complex;
20113 break;
20115 default:
20116 break;
20119 /* Handle simple keywords. */
20120 if (ds != ds_last)
20122 if (decl_specs)
20124 set_and_check_decl_spec_loc (decl_specs, ds, token);
20125 decl_specs->any_specifiers_p = true;
20127 return cp_lexer_consume_token (parser->lexer)->u.value;
20130 /* If we do not already have a type-specifier, assume we are looking
20131 at a simple-type-specifier. */
20132 type_spec = cp_parser_simple_type_specifier (parser,
20133 decl_specs,
20134 flags);
20136 /* If we didn't find a type-specifier, and a type-specifier was not
20137 optional in this context, issue an error message. */
20138 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20140 cp_parser_error (parser, "expected type specifier");
20141 return error_mark_node;
20144 return type_spec;
20147 /* Parse a simple-type-specifier.
20149 simple-type-specifier:
20150 :: [opt] nested-name-specifier [opt] type-name
20151 :: [opt] nested-name-specifier template template-id
20152 char
20153 wchar_t
20154 bool
20155 short
20157 long
20158 signed
20159 unsigned
20160 float
20161 double
20162 void
20164 C++11 Extension:
20166 simple-type-specifier:
20167 auto
20168 decltype ( expression )
20169 char16_t
20170 char32_t
20171 __underlying_type ( type-id )
20173 C++17 extension:
20175 nested-name-specifier(opt) template-name
20177 GNU Extension:
20179 simple-type-specifier:
20180 __int128
20181 __typeof__ unary-expression
20182 __typeof__ ( type-id )
20183 __typeof__ ( type-id ) { initializer-list , [opt] }
20185 Concepts Extension:
20187 simple-type-specifier:
20188 constrained-type-specifier
20190 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
20191 appropriately updated. */
20193 static tree
20194 cp_parser_simple_type_specifier (cp_parser* parser,
20195 cp_decl_specifier_seq *decl_specs,
20196 cp_parser_flags flags)
20198 tree type = NULL_TREE;
20199 cp_token *token;
20200 int idx;
20202 /* Peek at the next token. */
20203 token = cp_lexer_peek_token (parser->lexer);
20205 /* If we're looking at a keyword, things are easy. */
20206 switch (token->keyword)
20208 case RID_CHAR:
20209 if (decl_specs)
20210 decl_specs->explicit_char_p = true;
20211 type = char_type_node;
20212 break;
20213 case RID_CHAR8:
20214 type = char8_type_node;
20215 break;
20216 case RID_CHAR16:
20217 type = char16_type_node;
20218 break;
20219 case RID_CHAR32:
20220 type = char32_type_node;
20221 break;
20222 case RID_WCHAR:
20223 type = wchar_type_node;
20224 break;
20225 case RID_BOOL:
20226 type = boolean_type_node;
20227 break;
20228 case RID_SHORT:
20229 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
20230 type = short_integer_type_node;
20231 break;
20232 case RID_INT:
20233 if (decl_specs)
20234 decl_specs->explicit_int_p = true;
20235 type = integer_type_node;
20236 break;
20237 case RID_INT_N_0:
20238 case RID_INT_N_1:
20239 case RID_INT_N_2:
20240 case RID_INT_N_3:
20241 idx = token->keyword - RID_INT_N_0;
20242 if (! int_n_enabled_p [idx])
20243 break;
20244 if (decl_specs)
20246 decl_specs->explicit_intN_p = true;
20247 decl_specs->int_n_idx = idx;
20248 /* Check if the alternate "__intN__" form has been used instead of
20249 "__intN". */
20250 if (startswith (IDENTIFIER_POINTER (token->u.value)
20251 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
20252 decl_specs->int_n_alt = true;
20254 type = int_n_trees [idx].signed_type;
20255 break;
20256 case RID_LONG:
20257 if (decl_specs)
20258 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
20259 type = long_integer_type_node;
20260 break;
20261 case RID_SIGNED:
20262 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
20263 type = integer_type_node;
20264 break;
20265 case RID_UNSIGNED:
20266 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
20267 type = unsigned_type_node;
20268 break;
20269 case RID_FLOAT:
20270 type = float_type_node;
20271 break;
20272 case RID_DOUBLE:
20273 type = double_type_node;
20274 break;
20275 CASE_RID_FLOATN_NX:
20276 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
20277 if (type == NULL_TREE)
20278 error ("%<_Float%d%s%> is not supported on this target",
20279 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
20280 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
20281 ? "x" : "");
20282 break;
20283 case RID_VOID:
20284 type = void_type_node;
20285 break;
20287 case RID_AUTO:
20288 maybe_warn_cpp0x (CPP0X_AUTO);
20289 if (parser->auto_is_implicit_function_template_parm_p)
20291 /* The 'auto' might be the placeholder return type for a function decl
20292 with trailing return type. */
20293 bool have_trailing_return_fn_decl = false;
20295 cp_parser_parse_tentatively (parser);
20296 cp_lexer_consume_token (parser->lexer);
20297 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
20298 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
20299 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20300 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
20302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20304 cp_lexer_consume_token (parser->lexer);
20305 cp_parser_skip_to_closing_parenthesis (parser,
20306 /*recovering*/false,
20307 /*or_comma*/false,
20308 /*consume_paren*/true);
20309 continue;
20312 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20314 have_trailing_return_fn_decl = true;
20315 break;
20318 cp_lexer_consume_token (parser->lexer);
20320 cp_parser_abort_tentative_parse (parser);
20322 if (have_trailing_return_fn_decl)
20324 type = make_auto ();
20325 break;
20328 if (cxx_dialect >= cxx14)
20330 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20331 type = TREE_TYPE (type);
20333 else
20334 type = error_mark_node;
20336 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20338 if (cxx_dialect < cxx14)
20339 error_at (token->location,
20340 "use of %<auto%> in lambda parameter declaration "
20341 "only available with "
20342 "%<-std=c++14%> or %<-std=gnu++14%>");
20344 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20345 pedwarn (token->location, 0,
20346 "use of %<auto%> in template argument "
20347 "only available with %<-fconcepts-ts%>");
20348 else if (!flag_concepts)
20349 pedwarn (token->location, 0,
20350 "use of %<auto%> in parameter declaration "
20351 "only available with %<-std=c++20%> or %<-fconcepts%>");
20352 else if (cxx_dialect < cxx14)
20353 error_at (token->location,
20354 "use of %<auto%> in parameter declaration "
20355 "only available with "
20356 "%<-std=c++14%> or %<-std=gnu++14%>");
20358 else
20359 type = make_auto ();
20360 break;
20362 case RID_DECLTYPE:
20363 /* Since DR 743, decltype can either be a simple-type-specifier by
20364 itself or begin a nested-name-specifier. Parsing it will replace
20365 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20366 handling below decide what to do. */
20367 cp_parser_decltype (parser);
20368 cp_lexer_set_token_position (parser->lexer, token);
20369 break;
20371 case RID_TYPEOF:
20372 /* Consume the `typeof' token. */
20373 cp_lexer_consume_token (parser->lexer);
20374 /* Parse the operand to `typeof'. */
20375 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20376 /* If it is not already a TYPE, take its type. */
20377 if (!TYPE_P (type))
20378 type = finish_typeof (type);
20380 if (decl_specs)
20381 cp_parser_set_decl_spec_type (decl_specs, type,
20382 token,
20383 /*type_definition_p=*/false);
20385 return type;
20387 default:
20388 /* If token is a type-yielding built-in traits, parse it. */
20389 const cp_trait* trait = cp_lexer_peek_trait_type (parser->lexer);
20390 if (trait)
20392 type = cp_parser_trait (parser, trait);
20393 if (decl_specs)
20394 cp_parser_set_decl_spec_type (decl_specs, type,
20395 token,
20396 /*type_definition_p=*/false);
20398 return type;
20401 break;
20404 /* If token is an already-parsed decltype not followed by ::,
20405 it's a simple-type-specifier. */
20406 if (token->type == CPP_DECLTYPE
20407 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20409 type = saved_checks_value (token->u.tree_check_value);
20410 if (decl_specs)
20412 cp_parser_set_decl_spec_type (decl_specs, type,
20413 token,
20414 /*type_definition_p=*/false);
20415 /* Remember that we are handling a decltype in order to
20416 implement the resolution of DR 1510 when the argument
20417 isn't instantiation dependent. */
20418 decl_specs->decltype_p = true;
20420 cp_lexer_consume_token (parser->lexer);
20421 return type;
20424 /* If the type-specifier was for a built-in type, we're done. */
20425 if (type)
20427 /* Record the type. */
20428 if (decl_specs
20429 && (token->keyword != RID_SIGNED
20430 && token->keyword != RID_UNSIGNED
20431 && token->keyword != RID_SHORT
20432 && token->keyword != RID_LONG))
20433 cp_parser_set_decl_spec_type (decl_specs,
20434 type,
20435 token,
20436 /*type_definition_p=*/false);
20437 if (decl_specs)
20438 decl_specs->any_specifiers_p = true;
20440 /* Consume the token. */
20441 cp_lexer_consume_token (parser->lexer);
20443 if (type == error_mark_node)
20444 return error_mark_node;
20446 /* There is no valid C++ program where a non-template type is
20447 followed by a "<". That usually indicates that the user thought
20448 that the type was a template. */
20449 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20450 token->location);
20452 return TYPE_NAME (type);
20455 /* The type-specifier must be a user-defined type. */
20456 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20458 bool qualified_p;
20459 bool global_p;
20460 const bool typename_p = (cxx_dialect >= cxx20
20461 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20463 /* Don't gobble tokens or issue error messages if this is an
20464 optional type-specifier. */
20465 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20466 cp_parser_parse_tentatively (parser);
20468 /* Remember current tentative parsing state -- if we know we need
20469 a type, we can give better diagnostics here. */
20470 bool tent = cp_parser_parsing_tentatively (parser);
20472 token = cp_lexer_peek_token (parser->lexer);
20474 /* Look for the optional `::' operator. */
20475 global_p
20476 = (cp_parser_global_scope_opt (parser,
20477 /*current_scope_valid_p=*/false)
20478 != NULL_TREE);
20479 /* Look for the nested-name specifier. */
20480 qualified_p
20481 = (cp_parser_nested_name_specifier_opt (parser,
20482 /*typename_keyword_p=*/false,
20483 /*check_dependency_p=*/true,
20484 /*type_p=*/false,
20485 /*is_declaration=*/false)
20486 != NULL_TREE);
20487 /* If we have seen a nested-name-specifier, and the next token
20488 is `template', then we are using the template-id production. */
20489 if (parser->scope
20490 && cp_parser_optional_template_keyword (parser))
20492 /* Look for the template-id. */
20493 type = cp_parser_template_id (parser,
20494 /*template_keyword_p=*/true,
20495 /*check_dependency_p=*/true,
20496 none_type,
20497 /*is_declaration=*/false);
20498 /* If the template-id did not name a type, we are out of
20499 luck. */
20500 if (TREE_CODE (type) != TYPE_DECL)
20502 /* ...unless we pretend we have seen 'typename'. */
20503 if (typename_p)
20504 type = cp_parser_make_typename_type (parser, type,
20505 token->location);
20506 else
20508 cp_parser_error (parser, "expected template-id for type");
20509 type = error_mark_node;
20513 /* DR 1812: A < following a qualified-id in a typename-specifier
20514 could safely be assumed to begin a template argument list, so
20515 the template keyword should be optional. */
20516 else if (parser->scope
20517 && qualified_p
20518 && typename_p
20519 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20521 cp_parser_parse_tentatively (parser);
20523 type = cp_parser_template_id (parser,
20524 /*template_keyword_p=*/true,
20525 /*check_dependency_p=*/true,
20526 none_type,
20527 /*is_declaration=*/false);
20528 /* This is handled below, so back off. */
20529 if (type && concept_check_p (type))
20530 cp_parser_simulate_error (parser);
20532 if (!cp_parser_parse_definitely (parser))
20533 type = NULL_TREE;
20534 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20535 type = make_typename_type (parser->scope, type, typename_type,
20536 /*complain=*/tf_error);
20537 else if (TREE_CODE (type) != TYPE_DECL)
20538 type = NULL_TREE;
20541 /* Otherwise, look for a type-name. */
20542 if (!type)
20544 if (cxx_dialect >= cxx17 || flag_concepts)
20545 cp_parser_parse_tentatively (parser);
20547 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20549 if ((cxx_dialect >= cxx17 || flag_concepts)
20550 && !cp_parser_parse_definitely (parser))
20551 type = NULL_TREE;
20554 if (!type && flag_concepts && decl_specs)
20556 /* Try for a type-constraint with template arguments. We check
20557 decl_specs here to avoid trying this for a functional cast. */
20559 cp_parser_parse_tentatively (parser);
20561 type = cp_parser_template_id (parser,
20562 /*template_keyword_p=*/false,
20563 /*check_dependency_p=*/true,
20564 none_type,
20565 /*is_declaration=*/false);
20566 if (type && concept_check_p (type))
20568 location_t loc = EXPR_LOCATION (type);
20569 type = cp_parser_placeholder_type_specifier (parser, loc,
20570 type, tent);
20571 if (tent && type == error_mark_node)
20572 /* Perhaps it's a concept-check expression. */
20573 cp_parser_simulate_error (parser);
20575 else
20576 cp_parser_simulate_error (parser);
20578 if (!cp_parser_parse_definitely (parser))
20579 type = NULL_TREE;
20582 if (!type && cxx_dialect >= cxx17)
20584 /* Try class template argument deduction or type-constraint without
20585 template arguments. */
20586 tree name = cp_parser_identifier (parser);
20587 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20588 && parser->scope != error_mark_node)
20590 location_t loc
20591 = cp_lexer_previous_token (parser->lexer)->location;
20592 tree tmpl = cp_parser_lookup_name (parser, name,
20593 none_type,
20594 /*is_template=*/false,
20595 /*is_namespace=*/false,
20596 /*check_dependency=*/true,
20597 /*ambiguous_decls=*/NULL,
20598 token->location);
20599 if (tmpl && tmpl != error_mark_node
20600 && ctad_template_p (tmpl))
20601 type = make_template_placeholder (tmpl);
20602 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20603 type = cp_parser_placeholder_type_specifier (parser, loc,
20604 tmpl, tent);
20605 else
20607 type = error_mark_node;
20608 if (!cp_parser_simulate_error (parser))
20609 cp_parser_name_lookup_error (parser, name, tmpl,
20610 NLE_TYPE, token->location);
20613 else
20614 type = error_mark_node;
20617 /* If it didn't work out, we don't have a TYPE. */
20618 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20619 && !cp_parser_parse_definitely (parser))
20620 type = NULL_TREE;
20622 /* Keep track of all name-lookups performed in class scopes. */
20623 if (type
20624 && !global_p
20625 && !qualified_p
20626 && TREE_CODE (type) == TYPE_DECL
20627 && identifier_p (DECL_NAME (type)))
20628 maybe_note_name_used_in_class (DECL_NAME (type), type);
20630 if (type && decl_specs)
20631 cp_parser_set_decl_spec_type (decl_specs, type,
20632 token,
20633 /*type_definition_p=*/false);
20636 /* If we didn't get a type-name, issue an error message. */
20637 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20639 cp_parser_error (parser, "expected type-name");
20640 return error_mark_node;
20643 if (type && type != error_mark_node)
20645 /* See if TYPE is an Objective-C type, and if so, parse and
20646 accept any protocol references following it. Do this before
20647 the cp_parser_check_for_invalid_template_id() call, because
20648 Objective-C types can be followed by '<...>' which would
20649 enclose protocol names rather than template arguments, and so
20650 everything is fine. */
20651 if (c_dialect_objc () && !parser->scope
20652 && (objc_is_id (type) || objc_is_class_name (type)))
20654 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20655 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20657 /* Clobber the "unqualified" type previously entered into
20658 DECL_SPECS with the new, improved protocol-qualified version. */
20659 if (decl_specs)
20660 decl_specs->type = qual_type;
20662 return qual_type;
20665 /* There is no valid C++ program where a non-template type is
20666 followed by a "<". That usually indicates that the user
20667 thought that the type was a template. */
20668 cp_parser_check_for_invalid_template_id (parser, type,
20669 none_type,
20670 token->location);
20673 return type;
20676 /* Parse the remainder of a placholder-type-specifier.
20678 placeholder-type-specifier:
20679 type-constraint_opt auto
20680 type-constraint_opt decltype(auto)
20682 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20683 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20684 parses the placeholder type, and performs some contextual syntactic analysis.
20686 LOC provides the location of the template name.
20688 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20689 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20690 might actually be a concept-check,
20692 Note that the Concepts TS allows the auto or decltype(auto) to be
20693 omitted in a constrained-type-specifier. */
20695 static tree
20696 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20697 tree tmpl, bool tentative)
20699 if (tmpl == error_mark_node)
20700 return error_mark_node;
20702 tree orig_tmpl = tmpl;
20704 /* Get the arguments as written for subsequent analysis. */
20705 tree args = NULL_TREE;
20706 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20708 args = TREE_OPERAND (tmpl, 1);
20709 tmpl = TREE_OPERAND (tmpl, 0);
20711 else
20712 /* A concept-name with no arguments can't be an expression. */
20713 tentative = false;
20715 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20717 /* Get the concept and prototype parameter for the constraint. */
20718 tree_pair info = finish_type_constraints (tmpl, args, complain);
20719 tree con = info.first;
20720 tree proto = info.second;
20721 if (con == error_mark_node)
20722 return error_mark_node;
20724 /* As per the standard, require auto or decltype(auto), except in some
20725 cases (template parameter lists, -fconcepts-ts enabled). */
20726 cp_token *placeholder = NULL, *close_paren = NULL;
20727 if (cxx_dialect >= cxx20)
20729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20730 placeholder = cp_lexer_consume_token (parser->lexer);
20731 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20733 placeholder = cp_lexer_consume_token (parser->lexer);
20734 matching_parens parens;
20735 parens.require_open (parser);
20736 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20737 close_paren = parens.require_close (parser);
20741 /* A type constraint constrains a contextually determined type or type
20742 parameter pack. However, the Concepts TS does allow concepts
20743 to introduce non-type and template template parameters. */
20744 if (TREE_CODE (proto) != TYPE_DECL)
20746 if (!flag_concepts_ts
20747 || !processing_template_parmlist)
20749 if (!tentative)
20751 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20752 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20754 return error_mark_node;
20758 /* In a template parameter list, a type-parameter can be introduced
20759 by type-constraints alone. */
20760 if (processing_template_parmlist && !placeholder)
20762 /* In a default argument we may not be creating new parameters. */
20763 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20765 /* If this assert turns out to be false, do error() instead. */
20766 gcc_assert (tentative);
20767 return error_mark_node;
20769 return build_constrained_parameter (con, proto, args);
20772 /* Diagnose issues placeholder issues. */
20773 if (!flag_concepts_ts
20774 && !parser->in_result_type_constraint_p
20775 && !placeholder)
20777 if (tentative)
20778 /* Perhaps it's a concept-check expression (c++/91073). */
20779 return error_mark_node;
20781 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20782 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20783 error_at (input_location,
20784 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20785 /* Fall through. This is an error of omission. */
20787 else if (parser->in_result_type_constraint_p && placeholder)
20789 /* A trailing return type only allows type-constraints. */
20790 error_at (input_location,
20791 "unexpected placeholder in constrained result type");
20794 /* In a parameter-declaration-clause, a placeholder-type-specifier
20795 results in an invented template parameter. */
20796 if (parser->auto_is_implicit_function_template_parm_p)
20798 if (close_paren)
20800 location_t loc = make_location (placeholder->location,
20801 placeholder->location,
20802 close_paren->location);
20803 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20804 return error_mark_node;
20806 tree parm = build_constrained_parameter (con, proto, args);
20807 return synthesize_implicit_template_parm (parser, parm);
20810 /* Determine if the type should be deduced using template argument
20811 deduction or decltype deduction. Note that the latter is always
20812 used for type-constraints in trailing return types. */
20813 bool decltype_p = placeholder
20814 ? placeholder->keyword == RID_DECLTYPE
20815 : parser->in_result_type_constraint_p;
20817 /* Otherwise, this is the type of a variable or return type. */
20818 if (decltype_p)
20819 return make_constrained_decltype_auto (con, args);
20820 else
20821 return make_constrained_auto (con, args);
20824 /* Parse a type-name.
20826 type-name:
20827 class-name
20828 enum-name
20829 typedef-name
20830 simple-template-id [in c++0x]
20832 enum-name:
20833 identifier
20835 typedef-name:
20836 identifier
20838 Concepts:
20840 type-name:
20841 concept-name
20842 partial-concept-id
20844 concept-name:
20845 identifier
20847 Returns a TYPE_DECL for the type. */
20849 static tree
20850 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20852 tree type_decl;
20854 /* We can't know yet whether it is a class-name or not. */
20855 cp_parser_parse_tentatively (parser);
20856 /* Try a class-name. */
20857 type_decl = cp_parser_class_name (parser,
20858 typename_keyword_p,
20859 /*template_keyword_p=*/false,
20860 none_type,
20861 /*check_dependency_p=*/true,
20862 /*class_head_p=*/false,
20863 /*is_declaration=*/false);
20864 /* If it's not a class-name, keep looking. */
20865 if (!cp_parser_parse_definitely (parser))
20867 if (cxx_dialect < cxx11)
20868 /* It must be a typedef-name or an enum-name. */
20869 return cp_parser_nonclass_name (parser);
20871 cp_parser_parse_tentatively (parser);
20872 /* It is either a simple-template-id representing an
20873 instantiation of an alias template... */
20874 type_decl = cp_parser_template_id (parser,
20875 /*template_keyword_p=*/false,
20876 /*check_dependency_p=*/true,
20877 none_type,
20878 /*is_declaration=*/false);
20879 /* Note that this must be an instantiation of an alias template
20880 because [temp.names]/6 says:
20882 A template-id that names an alias template specialization
20883 is a type-name.
20885 Whereas [temp.names]/7 says:
20887 A simple-template-id that names a class template
20888 specialization is a class-name.
20890 With concepts, this could also be a partial-concept-id that
20891 declares a non-type template parameter. */
20892 if (type_decl != NULL_TREE
20893 && TREE_CODE (type_decl) == TYPE_DECL
20894 && TYPE_DECL_ALIAS_P (type_decl))
20895 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20896 else
20897 cp_parser_simulate_error (parser);
20899 if (!cp_parser_parse_definitely (parser))
20900 /* ... Or a typedef-name or an enum-name. */
20901 return cp_parser_nonclass_name (parser);
20904 return type_decl;
20907 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20908 or a concept-name.
20910 enum-name:
20911 identifier
20913 typedef-name:
20914 identifier
20916 concept-name:
20917 identifier
20919 Returns a TYPE_DECL for the type. */
20921 static tree
20922 cp_parser_nonclass_name (cp_parser* parser)
20924 tree type_decl;
20925 tree identifier;
20927 cp_token *token = cp_lexer_peek_token (parser->lexer);
20928 identifier = cp_parser_identifier (parser);
20929 if (identifier == error_mark_node)
20930 return error_mark_node;
20932 /* Look up the type-name. */
20933 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20935 type_decl = strip_using_decl (type_decl);
20937 if (TREE_CODE (type_decl) != TYPE_DECL
20938 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20940 /* See if this is an Objective-C type. */
20941 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20942 tree type = objc_get_protocol_qualified_type (identifier, protos);
20943 if (type)
20944 type_decl = TYPE_NAME (type);
20947 /* Issue an error if we did not find a type-name. */
20948 if (TREE_CODE (type_decl) != TYPE_DECL
20949 /* In Objective-C, we have the complication that class names are
20950 normally type names and start declarations (eg, the
20951 "NSObject" in "NSObject *object;"), but can be used in an
20952 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20953 is an expression. So, a classname followed by a dot is not a
20954 valid type-name. */
20955 || (objc_is_class_name (TREE_TYPE (type_decl))
20956 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20958 if (!cp_parser_simulate_error (parser))
20959 cp_parser_name_lookup_error (parser, identifier, type_decl,
20960 NLE_TYPE, token->location);
20961 return error_mark_node;
20963 /* Remember that the name was used in the definition of the
20964 current class so that we can check later to see if the
20965 meaning would have been different after the class was
20966 entirely defined. */
20967 else if (type_decl != error_mark_node
20968 && !parser->scope)
20969 maybe_note_name_used_in_class (identifier, type_decl);
20971 return type_decl;
20974 /* Parse an elaborated-type-specifier. Note that the grammar given
20975 here incorporates the resolution to DR68.
20977 elaborated-type-specifier:
20978 class-key :: [opt] nested-name-specifier [opt] identifier
20979 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20980 enum-key :: [opt] nested-name-specifier [opt] identifier
20981 typename :: [opt] nested-name-specifier identifier
20982 typename :: [opt] nested-name-specifier template [opt]
20983 template-id
20985 GNU extension:
20987 elaborated-type-specifier:
20988 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20989 class-key attributes :: [opt] nested-name-specifier [opt]
20990 template [opt] template-id
20991 enum attributes :: [opt] nested-name-specifier [opt] identifier
20993 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20994 declared `friend'. If IS_DECLARATION is TRUE, then this
20995 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20996 something is being declared.
20998 Returns the TYPE specified. */
21000 static tree
21001 cp_parser_elaborated_type_specifier (cp_parser* parser,
21002 bool is_friend,
21003 bool is_declaration)
21005 enum tag_types tag_type;
21006 tree identifier;
21007 tree type = NULL_TREE;
21008 tree attributes = NULL_TREE;
21009 tree globalscope;
21010 cp_token *token = NULL;
21012 /* For class and enum types the location of the class-key or enum-key. */
21013 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
21014 /* For a scoped enum, the 'class' or 'struct' keyword id. */
21015 rid scoped_key = RID_MAX;
21017 /* See if we're looking at the `enum' keyword. */
21018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
21020 /* Consume the `enum' token. */
21021 cp_lexer_consume_token (parser->lexer);
21022 /* Remember that it's an enumeration type. */
21023 tag_type = enum_type;
21024 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
21025 enums) is used here. */
21026 cp_token *token = cp_lexer_peek_token (parser->lexer);
21027 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
21028 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
21030 location_t loc = token->location;
21031 gcc_rich_location richloc (loc);
21032 richloc.add_range (input_location);
21033 richloc.add_fixit_remove ();
21034 pedwarn (&richloc, 0, "elaborated-type-specifier for "
21035 "a scoped enum must not use the %qD keyword",
21036 token->u.value);
21037 /* Consume the `struct' or `class' and parse it anyway. */
21038 cp_lexer_consume_token (parser->lexer);
21039 /* Create a combined location for the whole scoped-enum-key. */
21040 key_loc = make_location (key_loc, key_loc, loc);
21042 else
21043 scoped_key = RID_MAX;
21045 /* Parse the attributes. */
21046 attributes = cp_parser_attributes_opt (parser);
21048 /* Or, it might be `typename'. */
21049 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21050 RID_TYPENAME))
21052 /* Consume the `typename' token. */
21053 cp_lexer_consume_token (parser->lexer);
21054 /* Remember that it's a `typename' type. */
21055 tag_type = typename_type;
21057 /* Otherwise it must be a class-key. */
21058 else
21060 key_loc = cp_lexer_peek_token (parser->lexer)->location;
21061 tag_type = cp_parser_class_key (parser);
21062 if (tag_type == none_type)
21063 return error_mark_node;
21064 /* Parse the attributes. */
21065 attributes = cp_parser_attributes_opt (parser);
21068 /* Look for the `::' operator. */
21069 globalscope = cp_parser_global_scope_opt (parser,
21070 /*current_scope_valid_p=*/false);
21071 /* Look for the nested-name-specifier. */
21072 tree nested_name_specifier;
21073 if (tag_type == typename_type && !globalscope)
21075 nested_name_specifier
21076 = cp_parser_nested_name_specifier (parser,
21077 /*typename_keyword_p=*/true,
21078 /*check_dependency_p=*/true,
21079 /*type_p=*/true,
21080 is_declaration);
21081 if (!nested_name_specifier)
21082 return error_mark_node;
21084 else
21085 /* Even though `typename' is not present, the proposed resolution
21086 to Core Issue 180 says that in `class A<T>::B', `B' should be
21087 considered a type-name, even if `A<T>' is dependent. */
21088 nested_name_specifier
21089 = cp_parser_nested_name_specifier_opt (parser,
21090 /*typename_keyword_p=*/true,
21091 /*check_dependency_p=*/true,
21092 /*type_p=*/true,
21093 is_declaration);
21094 /* For everything but enumeration types, consider a template-id.
21095 For an enumeration type, consider only a plain identifier. */
21096 if (tag_type != enum_type)
21098 bool template_p = false;
21099 tree decl;
21101 /* Allow the `template' keyword. */
21102 template_p = cp_parser_optional_template_keyword (parser);
21103 /* If we didn't see `template', we don't know if there's a
21104 template-id or not. */
21105 if (!template_p)
21106 cp_parser_parse_tentatively (parser);
21107 /* The `template' keyword must follow a nested-name-specifier. */
21108 else if (!nested_name_specifier && !globalscope)
21110 cp_parser_error (parser, "%<template%> must follow a nested-"
21111 "name-specifier");
21112 return error_mark_node;
21115 /* Parse the template-id. */
21116 token = cp_lexer_peek_token (parser->lexer);
21117 decl = cp_parser_template_id (parser, template_p,
21118 /*check_dependency_p=*/true,
21119 tag_type,
21120 is_declaration);
21121 /* If we didn't find a template-id, look for an ordinary
21122 identifier. */
21123 if (!template_p && !cp_parser_parse_definitely (parser))
21125 /* We can get here when cp_parser_template_id, called by
21126 cp_parser_class_name with tag_type == none_type, succeeds
21127 and caches a BASELINK. Then, when called again here,
21128 instead of failing and returning an error_mark_node
21129 returns it (see template/typename17.C in C++11).
21130 ??? Could we diagnose this earlier? */
21131 else if (tag_type == typename_type && BASELINK_P (decl))
21133 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
21134 type = error_mark_node;
21136 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
21137 in effect, then we must assume that, upon instantiation, the
21138 template will correspond to a class. */
21139 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21140 && tag_type == typename_type)
21141 type = make_typename_type (parser->scope, decl,
21142 typename_type,
21143 /*complain=*/tf_error);
21144 /* If the `typename' keyword is in effect and DECL is not a type
21145 decl, then type is non existent. */
21146 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
21148 else if (TREE_CODE (decl) == TYPE_DECL)
21150 type = check_elaborated_type_specifier (tag_type, decl,
21151 /*allow_template_p=*/true);
21153 /* If the next token is a semicolon, this must be a specialization,
21154 instantiation, or friend declaration. Check the scope while we
21155 still know whether or not we had a nested-name-specifier. */
21156 if (type != error_mark_node
21157 && !nested_name_specifier && !is_friend
21158 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21159 check_unqualified_spec_or_inst (type, token->location);
21161 else if (decl == error_mark_node)
21162 type = error_mark_node;
21165 if (!type)
21167 token = cp_lexer_peek_token (parser->lexer);
21168 identifier = cp_parser_identifier (parser);
21170 if (identifier == error_mark_node)
21172 parser->scope = NULL_TREE;
21173 return error_mark_node;
21176 /* For a `typename', we needn't call xref_tag. */
21177 if (tag_type == typename_type
21178 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21179 return cp_parser_make_typename_type (parser, identifier,
21180 token->location);
21182 /* Template parameter lists apply only if we are not within a
21183 function parameter list. */
21184 bool template_parm_lists_apply
21185 = parser->num_template_parameter_lists;
21186 if (template_parm_lists_apply)
21187 for (cp_binding_level *s = current_binding_level;
21188 s && s->kind != sk_template_parms;
21189 s = s->level_chain)
21190 if (s->kind == sk_function_parms)
21191 template_parm_lists_apply = false;
21193 /* Look up a qualified name in the usual way. */
21194 if (parser->scope)
21196 tree decl;
21197 tree ambiguous_decls;
21199 decl = cp_parser_lookup_name (parser, identifier,
21200 tag_type,
21201 /*is_template=*/false,
21202 /*is_namespace=*/false,
21203 /*check_dependency=*/true,
21204 &ambiguous_decls,
21205 token->location);
21207 /* If the lookup was ambiguous, an error will already have been
21208 issued. */
21209 if (ambiguous_decls)
21210 return error_mark_node;
21212 /* If we are parsing friend declaration, DECL may be a
21213 TEMPLATE_DECL tree node here. However, we need to check
21214 whether this TEMPLATE_DECL results in valid code. Consider
21215 the following example:
21217 namespace N {
21218 template <class T> class C {};
21220 class X {
21221 template <class T> friend class N::C; // #1, valid code
21223 template <class T> class Y {
21224 friend class N::C; // #2, invalid code
21227 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
21228 name lookup of `N::C'. We see that friend declaration must
21229 be template for the code to be valid. Note that
21230 processing_template_decl does not work here since it is
21231 always 1 for the above two cases. */
21233 decl = (cp_parser_maybe_treat_template_as_class
21234 (decl, /*tag_name_p=*/is_friend
21235 && template_parm_lists_apply));
21237 if (TREE_CODE (decl) != TYPE_DECL)
21239 cp_parser_diagnose_invalid_type_name (parser,
21240 identifier,
21241 token->location);
21242 return error_mark_node;
21245 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21247 bool allow_template = (template_parm_lists_apply
21248 || DECL_SELF_REFERENCE_P (decl));
21249 type = check_elaborated_type_specifier (tag_type, decl,
21250 allow_template);
21252 if (type == error_mark_node)
21253 return error_mark_node;
21256 /* Forward declarations of nested types, such as
21258 class C1::C2;
21259 class C1::C2::C3;
21261 are invalid unless all components preceding the final '::'
21262 are complete. If all enclosing types are complete, these
21263 declarations become merely pointless.
21265 Invalid forward declarations of nested types are errors
21266 caught elsewhere in parsing. Those that are pointless arrive
21267 here. */
21269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21270 && !is_friend && is_declaration
21271 && !processing_explicit_instantiation)
21272 warning (0, "declaration %qD does not declare anything", decl);
21274 type = TREE_TYPE (decl);
21276 else
21278 /* An elaborated-type-specifier sometimes introduces a new type and
21279 sometimes names an existing type. Normally, the rule is that it
21280 introduces a new type only if there is not an existing type of
21281 the same name already in scope. For example, given:
21283 struct S {};
21284 void f() { struct S s; }
21286 the `struct S' in the body of `f' is the same `struct S' as in
21287 the global scope; the existing definition is used. However, if
21288 there were no global declaration, this would introduce a new
21289 local class named `S'.
21291 An exception to this rule applies to the following code:
21293 namespace N { struct S; }
21295 Here, the elaborated-type-specifier names a new type
21296 unconditionally; even if there is already an `S' in the
21297 containing scope this declaration names a new type.
21298 This exception only applies if the elaborated-type-specifier
21299 forms the complete declaration:
21301 [class.name]
21303 A declaration consisting solely of `class-key identifier ;' is
21304 either a redeclaration of the name in the current scope or a
21305 forward declaration of the identifier as a class name. It
21306 introduces the name into the current scope.
21308 We are in this situation precisely when the next token is a `;'.
21310 An exception to the exception is that a `friend' declaration does
21311 *not* name a new type; i.e., given:
21313 struct S { friend struct T; };
21315 `T' is not a new type in the scope of `S'.
21317 Also, `new struct S' or `sizeof (struct S)' never results in the
21318 definition of a new type; a new type can only be declared in a
21319 declaration context. */
21321 TAG_how how;
21323 if (is_friend)
21324 /* Friends have special name lookup rules. */
21325 how = TAG_how::HIDDEN_FRIEND;
21326 else if (is_declaration
21327 && cp_lexer_next_token_is (parser->lexer,
21328 CPP_SEMICOLON))
21329 /* This is a `class-key identifier ;' */
21330 how = TAG_how::CURRENT_ONLY;
21331 else
21332 how = TAG_how::GLOBAL;
21334 bool template_p =
21335 (template_parm_lists_apply
21336 && (cp_parser_next_token_starts_class_definition_p (parser)
21337 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21338 /* An unqualified name was used to reference this type, so
21339 there were no qualifying templates. */
21340 if (template_parm_lists_apply
21341 && !cp_parser_check_template_parameters (parser,
21342 /*num_templates=*/0,
21343 /*template_id*/false,
21344 token->location,
21345 /*declarator=*/NULL))
21346 return error_mark_node;
21348 type = xref_tag (tag_type, identifier, how, template_p);
21352 if (type == error_mark_node)
21353 return error_mark_node;
21355 /* Allow attributes on forward declarations of classes. */
21356 if (attributes)
21358 if (TREE_CODE (type) == TYPENAME_TYPE)
21359 warning (OPT_Wattributes,
21360 "attributes ignored on uninstantiated type");
21361 else if (tag_type != enum_type
21362 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21363 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21364 && ! processing_explicit_instantiation)
21365 warning (OPT_Wattributes,
21366 "attributes ignored on template instantiation");
21367 else if (is_friend && cxx11_attribute_p (attributes))
21369 if (warning (OPT_Wattributes, "attribute ignored"))
21370 inform (input_location, "an attribute that appertains to a friend "
21371 "declaration that is not a definition is ignored");
21373 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21374 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21375 else
21376 warning (OPT_Wattributes,
21377 "attributes ignored on elaborated-type-specifier that is "
21378 "not a forward declaration");
21381 if (tag_type == enum_type)
21382 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21383 else
21385 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21386 for alias definition. */
21387 bool decl_class = (is_declaration
21388 && cp_parser_declares_only_class_p (parser));
21389 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21390 decl_class);
21392 /* Indicate whether this class was declared as a `class' or as a
21393 `struct'. */
21394 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21395 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21398 /* A "<" cannot follow an elaborated type specifier. If that
21399 happens, the user was probably trying to form a template-id. */
21400 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21401 token->location);
21403 return type;
21406 /* Parse an enum-specifier.
21408 enum-specifier:
21409 enum-head { enumerator-list [opt] }
21410 enum-head { enumerator-list , } [C++0x]
21412 enum-head:
21413 enum-key identifier [opt] enum-base [opt]
21414 enum-key nested-name-specifier identifier enum-base [opt]
21416 enum-key:
21417 enum
21418 enum class [C++0x]
21419 enum struct [C++0x]
21421 enum-base: [C++0x]
21422 : type-specifier-seq
21424 opaque-enum-specifier:
21425 enum-key identifier enum-base [opt] ;
21427 GNU Extensions:
21428 enum-key attributes[opt] identifier [opt] enum-base [opt]
21429 { enumerator-list [opt] }attributes[opt]
21430 enum-key attributes[opt] identifier [opt] enum-base [opt]
21431 { enumerator-list, }attributes[opt] [C++0x]
21433 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21434 if the token stream isn't an enum-specifier after all. */
21436 static tree
21437 cp_parser_enum_specifier (cp_parser* parser)
21439 tree identifier;
21440 tree type = NULL_TREE;
21441 tree prev_scope;
21442 tree nested_name_specifier = NULL_TREE;
21443 tree attributes;
21444 bool scoped_enum_p = false;
21445 bool has_underlying_type = false;
21446 bool nested_being_defined = false;
21447 bool new_value_list = false;
21448 bool is_new_type = false;
21449 bool is_unnamed = false;
21450 tree underlying_type = NULL_TREE;
21451 cp_token *type_start_token = NULL;
21452 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21454 /* Parse tentatively so that we can back up if we don't find a
21455 enum-specifier. */
21456 cp_parser_parse_tentatively (parser);
21458 /* Caller guarantees that the current token is 'enum', an identifier
21459 possibly follows, and the token after that is an opening brace.
21460 If we don't have an identifier, fabricate an anonymous name for
21461 the enumeration being defined. */
21462 cp_lexer_consume_token (parser->lexer);
21464 /* Parse the "class" or "struct", which indicates a scoped
21465 enumeration type in C++0x. */
21466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21467 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21469 if (cxx_dialect < cxx11)
21470 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21472 /* Consume the `struct' or `class' token. */
21473 cp_lexer_consume_token (parser->lexer);
21475 scoped_enum_p = true;
21478 attributes = cp_parser_attributes_opt (parser);
21480 /* Clear the qualification. */
21481 parser->scope = NULL_TREE;
21482 parser->qualifying_scope = NULL_TREE;
21483 parser->object_scope = NULL_TREE;
21485 /* Figure out in what scope the declaration is being placed. */
21486 prev_scope = current_scope ();
21488 type_start_token = cp_lexer_peek_token (parser->lexer);
21490 push_deferring_access_checks (dk_no_check);
21491 nested_name_specifier
21492 = cp_parser_nested_name_specifier_opt (parser,
21493 /*typename_keyword_p=*/true,
21494 /*check_dependency_p=*/false,
21495 /*type_p=*/false,
21496 /*is_declaration=*/false);
21498 if (nested_name_specifier)
21500 tree name;
21502 identifier = cp_parser_identifier (parser);
21503 name = cp_parser_lookup_name (parser, identifier,
21504 enum_type,
21505 /*is_template=*/false,
21506 /*is_namespace=*/false,
21507 /*check_dependency=*/true,
21508 /*ambiguous_decls=*/NULL,
21509 input_location);
21510 if (name && name != error_mark_node)
21512 type = TREE_TYPE (name);
21513 if (TREE_CODE (type) == TYPENAME_TYPE)
21515 /* Are template enums allowed in ISO? */
21516 if (template_parm_scope_p ())
21517 pedwarn (type_start_token->location, OPT_Wpedantic,
21518 "%qD is an enumeration template", name);
21519 /* ignore a typename reference, for it will be solved by name
21520 in start_enum. */
21521 type = NULL_TREE;
21524 else if (nested_name_specifier == error_mark_node)
21525 /* We already issued an error. */;
21526 else
21528 error_at (type_start_token->location,
21529 "%qD does not name an enumeration in %qT",
21530 identifier, nested_name_specifier);
21531 nested_name_specifier = error_mark_node;
21534 else
21536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21537 identifier = cp_parser_identifier (parser);
21538 else
21540 identifier = make_anon_name ();
21541 is_unnamed = true;
21542 if (scoped_enum_p)
21543 error_at (type_start_token->location,
21544 "unnamed scoped enum is not allowed");
21547 pop_deferring_access_checks ();
21549 /* Check for the `:' that denotes a specified underlying type in C++0x.
21550 Note that a ':' could also indicate a bitfield width, however. */
21551 location_t colon_loc = UNKNOWN_LOCATION;
21552 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21554 cp_decl_specifier_seq type_specifiers;
21556 /* Consume the `:'. */
21557 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21558 cp_lexer_consume_token (parser->lexer);
21560 auto tdf
21561 = make_temp_override (parser->type_definition_forbidden_message,
21562 G_("types may not be defined in enum-base"));
21564 /* Parse the type-specifier-seq. */
21565 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21566 /*is_declaration=*/false,
21567 /*is_trailing_return=*/false,
21568 &type_specifiers);
21570 /* At this point this is surely not elaborated type specifier. */
21571 if (!cp_parser_parse_definitely (parser))
21572 return NULL_TREE;
21574 if (cxx_dialect < cxx11)
21575 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21577 has_underlying_type = true;
21579 /* If that didn't work, stop. */
21580 if (type_specifiers.type != error_mark_node)
21582 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21583 /*initialized=*/0, NULL);
21584 if (underlying_type == error_mark_node
21585 || check_for_bare_parameter_packs (underlying_type))
21586 underlying_type = NULL_TREE;
21590 /* Look for the `{' but don't consume it yet. */
21591 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21593 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21595 if (has_underlying_type)
21596 cp_parser_commit_to_tentative_parse (parser);
21597 cp_parser_error (parser, "expected %<{%>");
21598 if (has_underlying_type)
21599 return error_mark_node;
21601 /* An opaque-enum-specifier must have a ';' here. */
21602 if ((scoped_enum_p || underlying_type)
21603 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21605 if (has_underlying_type)
21606 pedwarn (colon_loc,
21607 OPT_Welaborated_enum_base,
21608 "declaration of enumeration with "
21609 "fixed underlying type and no enumerator list is "
21610 "only permitted as a standalone declaration");
21611 else
21612 cp_parser_error (parser, "expected %<;%> or %<{%>");
21616 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21617 return NULL_TREE;
21619 if (nested_name_specifier)
21621 if (CLASS_TYPE_P (nested_name_specifier))
21623 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21624 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21625 push_scope (nested_name_specifier);
21627 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21628 push_nested_namespace (nested_name_specifier);
21631 /* Issue an error message if type-definitions are forbidden here. */
21632 if (!cp_parser_check_type_definition (parser))
21633 type = error_mark_node;
21634 else
21635 /* Create the new type. We do this before consuming the opening
21636 brace so the enum will be recorded as being on the line of its
21637 tag (or the 'enum' keyword, if there is no tag). */
21638 type = start_enum (identifier, type, underlying_type,
21639 attributes, scoped_enum_p, &is_new_type);
21641 /* If the next token is not '{' it is an opaque-enum-specifier or an
21642 elaborated-type-specifier. */
21643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21645 auto_timevar tv (TV_PARSE_ENUM);
21647 if (nested_name_specifier
21648 && nested_name_specifier != error_mark_node)
21650 /* The following catches invalid code such as:
21651 enum class S<int>::E { A, B, C }; */
21652 if (!processing_specialization
21653 && CLASS_TYPE_P (nested_name_specifier)
21654 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21655 error_at (type_start_token->location, "cannot add an enumerator "
21656 "list to a template instantiation");
21658 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21660 error_at (type_start_token->location,
21661 "%<%T::%E%> has not been declared",
21662 TYPE_CONTEXT (nested_name_specifier),
21663 nested_name_specifier);
21664 type = error_mark_node;
21666 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21667 && !CLASS_TYPE_P (nested_name_specifier))
21669 error_at (type_start_token->location, "nested name specifier "
21670 "%qT for enum declaration does not name a class "
21671 "or namespace", nested_name_specifier);
21672 type = error_mark_node;
21674 /* If that scope does not contain the scope in which the
21675 class was originally declared, the program is invalid. */
21676 else if (prev_scope && !is_ancestor (prev_scope,
21677 nested_name_specifier))
21679 if (at_namespace_scope_p ())
21680 error_at (type_start_token->location,
21681 "declaration of %qD in namespace %qD which does not "
21682 "enclose %qD",
21683 type, prev_scope, nested_name_specifier);
21684 else
21685 error_at (type_start_token->location,
21686 "declaration of %qD in %qD which does not "
21687 "enclose %qD",
21688 type, prev_scope, nested_name_specifier);
21689 type = error_mark_node;
21691 /* If that scope is the scope where the declaration is being placed
21692 the program is invalid. */
21693 else if (CLASS_TYPE_P (nested_name_specifier)
21694 && CLASS_TYPE_P (prev_scope)
21695 && same_type_p (nested_name_specifier, prev_scope))
21697 permerror (type_start_token->location,
21698 "extra qualification not allowed");
21699 nested_name_specifier = NULL_TREE;
21703 if (scoped_enum_p)
21704 begin_scope (sk_scoped_enum, type);
21706 /* Consume the opening brace. */
21707 matching_braces braces;
21708 braces.consume_open (parser);
21710 if (type == error_mark_node)
21711 ; /* Nothing to add */
21712 else if (OPAQUE_ENUM_P (type)
21713 || (cxx_dialect > cxx98 && processing_specialization))
21715 new_value_list = true;
21716 SET_OPAQUE_ENUM_P (type, false);
21717 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21719 else
21721 error_at (type_start_token->location,
21722 "multiple definition of %q#T", type);
21723 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21724 "previous definition here");
21725 type = error_mark_node;
21728 if (type == error_mark_node)
21729 cp_parser_skip_to_end_of_block_or_statement (parser);
21730 /* If the next token is not '}', then there are some enumerators. */
21731 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21733 if (is_unnamed && !scoped_enum_p
21734 /* Don't warn for enum {} a; here. */
21735 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21736 pedwarn (type_start_token->location, OPT_Wpedantic,
21737 "ISO C++ forbids empty unnamed enum");
21739 else
21741 /* We've seen a '{' so we know we're in an enum-specifier.
21742 Commit to any tentative parse to get syntax errors. */
21743 cp_parser_commit_to_tentative_parse (parser);
21744 cp_parser_enumerator_list (parser, type);
21747 /* Consume the final '}'. */
21748 braces.require_close (parser);
21750 if (scoped_enum_p)
21751 finish_scope ();
21753 else
21755 /* If a ';' follows, then it is an opaque-enum-specifier
21756 and additional restrictions apply. */
21757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21759 if (is_unnamed)
21760 error_at (type_start_token->location,
21761 "opaque-enum-specifier without name");
21762 else if (nested_name_specifier)
21763 error_at (type_start_token->location,
21764 "opaque-enum-specifier must use a simple identifier");
21768 /* Look for trailing attributes to apply to this enumeration, and
21769 apply them if appropriate. */
21770 if (cp_parser_allow_gnu_extensions_p (parser))
21772 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21773 cplus_decl_attributes (&type,
21774 trailing_attr,
21775 (int) ATTR_FLAG_TYPE_IN_PLACE);
21778 /* Finish up the enumeration. */
21779 if (type != error_mark_node)
21781 if (new_value_list)
21782 finish_enum_value_list (type);
21783 if (is_new_type)
21784 finish_enum (type);
21787 if (nested_name_specifier)
21789 if (CLASS_TYPE_P (nested_name_specifier))
21791 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21792 pop_scope (nested_name_specifier);
21794 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21795 pop_nested_namespace (nested_name_specifier);
21797 return type;
21800 /* Parse an enumerator-list. The enumerators all have the indicated
21801 TYPE.
21803 enumerator-list:
21804 enumerator-definition
21805 enumerator-list , enumerator-definition */
21807 static void
21808 cp_parser_enumerator_list (cp_parser* parser, tree type)
21810 while (true)
21812 /* Parse an enumerator-definition. */
21813 cp_parser_enumerator_definition (parser, type);
21815 /* If the next token is not a ',', we've reached the end of
21816 the list. */
21817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21818 break;
21819 /* Otherwise, consume the `,' and keep going. */
21820 cp_lexer_consume_token (parser->lexer);
21821 /* If the next token is a `}', there is a trailing comma. */
21822 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21824 if (cxx_dialect < cxx11)
21825 pedwarn (input_location, OPT_Wpedantic,
21826 "comma at end of enumerator list");
21827 break;
21832 /* Parse an enumerator-definition. The enumerator has the indicated
21833 TYPE.
21835 enumerator-definition:
21836 enumerator
21837 enumerator = constant-expression
21839 enumerator:
21840 identifier
21842 GNU Extensions:
21844 enumerator-definition:
21845 enumerator attributes [opt]
21846 enumerator attributes [opt] = constant-expression */
21848 static void
21849 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21851 tree identifier;
21852 tree value;
21853 location_t loc;
21855 /* Save the input location because we are interested in the location
21856 of the identifier and not the location of the explicit value. */
21857 loc = cp_lexer_peek_token (parser->lexer)->location;
21859 /* Look for the identifier. */
21860 identifier = cp_parser_identifier (parser);
21861 if (identifier == error_mark_node)
21862 return;
21864 /* Parse any specified attributes. */
21865 tree attrs = cp_parser_attributes_opt (parser);
21867 /* If the next token is an '=', then there is an explicit value. */
21868 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21870 /* Consume the `=' token. */
21871 cp_lexer_consume_token (parser->lexer);
21872 /* Parse the value. */
21873 value = cp_parser_constant_expression (parser);
21875 else
21876 value = NULL_TREE;
21878 /* If we are processing a template, make sure the initializer of the
21879 enumerator doesn't contain any bare template parameter pack. */
21880 if (current_lambda_expr ())
21882 /* In a lambda it should work, but doesn't currently. */
21883 if (uses_parameter_packs (value))
21885 sorry ("unexpanded parameter pack in enumerator in lambda");
21886 value = error_mark_node;
21889 else if (check_for_bare_parameter_packs (value))
21890 value = error_mark_node;
21892 /* Create the enumerator. */
21893 build_enumerator (identifier, value, type, attrs, loc);
21896 /* Parse a namespace-name.
21898 namespace-name:
21899 original-namespace-name
21900 namespace-alias
21902 Returns the NAMESPACE_DECL for the namespace. */
21904 static tree
21905 cp_parser_namespace_name (cp_parser* parser)
21907 tree identifier;
21908 tree namespace_decl;
21910 cp_token *token = cp_lexer_peek_token (parser->lexer);
21912 /* Get the name of the namespace. */
21913 identifier = cp_parser_identifier (parser);
21914 if (identifier == error_mark_node)
21915 return error_mark_node;
21917 /* Look up the identifier in the currently active scope. Look only
21918 for namespaces, due to:
21920 [basic.lookup.udir]
21922 When looking up a namespace-name in a using-directive or alias
21923 definition, only namespace names are considered.
21925 And:
21927 [basic.lookup.qual]
21929 During the lookup of a name preceding the :: scope resolution
21930 operator, object, function, and enumerator names are ignored.
21932 (Note that cp_parser_qualifying_entity only calls this
21933 function if the token after the name is the scope resolution
21934 operator.) */
21935 namespace_decl = cp_parser_lookup_name (parser, identifier,
21936 none_type,
21937 /*is_template=*/false,
21938 /*is_namespace=*/true,
21939 /*check_dependency=*/true,
21940 /*ambiguous_decls=*/NULL,
21941 token->location);
21942 /* If it's not a namespace, issue an error. */
21943 if (namespace_decl == error_mark_node
21944 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21946 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21948 auto_diagnostic_group d;
21949 name_hint hint;
21950 if (namespace_decl == error_mark_node
21951 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21952 hint = suggest_alternative_in_explicit_scope (token->location,
21953 identifier,
21954 parser->scope);
21955 if (const char *suggestion = hint.suggestion ())
21957 gcc_rich_location richloc (token->location);
21958 richloc.add_fixit_replace (suggestion);
21959 error_at (&richloc,
21960 "%qD is not a namespace-name; did you mean %qs?",
21961 identifier, suggestion);
21963 else
21964 error_at (token->location, "%qD is not a namespace-name",
21965 identifier);
21967 else
21968 cp_parser_error (parser, "expected namespace-name");
21969 namespace_decl = error_mark_node;
21972 return namespace_decl;
21975 /* Parse a namespace-definition.
21977 namespace-definition:
21978 named-namespace-definition
21979 unnamed-namespace-definition
21981 named-namespace-definition:
21982 original-namespace-definition
21983 extension-namespace-definition
21985 original-namespace-definition:
21986 namespace identifier { namespace-body }
21988 extension-namespace-definition:
21989 namespace original-namespace-name { namespace-body }
21991 unnamed-namespace-definition:
21992 namespace { namespace-body } */
21994 static void
21995 cp_parser_namespace_definition (cp_parser* parser)
21997 tree identifier;
21998 int nested_definition_count = 0;
22000 cp_ensure_no_omp_declare_simd (parser);
22001 cp_ensure_no_oacc_routine (parser);
22003 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
22004 const bool topmost_inline_p = is_inline;
22006 if (is_inline)
22008 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
22009 cp_lexer_consume_token (parser->lexer);
22012 /* Look for the `namespace' keyword. */
22013 cp_token* token
22014 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22016 /* Parse any specified attributes before the identifier. */
22017 tree attribs = cp_parser_attributes_opt (parser);
22019 for (;;)
22021 identifier = NULL_TREE;
22023 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
22024 RID_INLINE);
22025 if (nested_inline_p && nested_definition_count != 0)
22027 if (pedantic && cxx_dialect < cxx20)
22028 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
22029 OPT_Wc__20_extensions, "nested inline namespace "
22030 "definitions only available with %<-std=c++20%> or "
22031 "%<-std=gnu++20%>");
22032 cp_lexer_consume_token (parser->lexer);
22035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22037 identifier = cp_parser_identifier (parser);
22039 if (cp_next_tokens_can_be_std_attribute_p (parser))
22040 pedwarn (input_location, OPT_Wpedantic,
22041 "standard attributes on namespaces must precede "
22042 "the namespace name");
22044 /* Parse any attributes specified after the identifier. */
22045 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
22048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22050 /* Don't forget that the innermost namespace might have been
22051 marked as inline. Use |= because we cannot overwrite
22052 IS_INLINE in case the outermost namespace is inline, but
22053 there are no nested inlines. */
22054 is_inline |= nested_inline_p;
22055 break;
22058 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
22059 pedwarn (input_location, OPT_Wc__17_extensions,
22060 "nested namespace definitions only available with "
22061 "%<-std=c++17%> or %<-std=gnu++17%>");
22063 /* Nested namespace names can create new namespaces (unlike
22064 other qualified-ids). */
22065 if (int count = (identifier
22066 ? push_namespace (identifier, nested_inline_p)
22067 : 0))
22068 nested_definition_count += count;
22069 else
22070 cp_parser_error (parser, "nested namespace name required");
22071 cp_lexer_consume_token (parser->lexer);
22074 if (nested_definition_count && !identifier)
22075 cp_parser_error (parser, "namespace name required");
22077 if (nested_definition_count && attribs)
22078 error_at (token->location,
22079 "a nested namespace definition cannot have attributes");
22080 if (nested_definition_count && topmost_inline_p)
22081 error_at (token->location,
22082 "a nested namespace definition cannot be inline");
22084 /* Start the namespace. */
22085 nested_definition_count += push_namespace (identifier, is_inline);
22087 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
22089 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
22091 /* Look for the `{' to validate starting the namespace. */
22092 matching_braces braces;
22093 if (braces.require_open (parser))
22095 /* Parse the body of the namespace. */
22096 cp_parser_namespace_body (parser);
22098 /* Look for the final `}'. */
22099 braces.require_close (parser);
22102 if (has_visibility)
22103 pop_visibility (1);
22105 /* Pop the nested namespace definitions. */
22106 while (nested_definition_count--)
22107 pop_namespace ();
22110 /* Parse a namespace-body.
22112 namespace-body:
22113 declaration-seq [opt] */
22115 static void
22116 cp_parser_namespace_body (cp_parser* parser)
22118 cp_parser_declaration_seq_opt (parser);
22121 /* Parse a namespace-alias-definition.
22123 namespace-alias-definition:
22124 namespace identifier = qualified-namespace-specifier ; */
22126 static void
22127 cp_parser_namespace_alias_definition (cp_parser* parser)
22129 tree identifier;
22130 tree namespace_specifier;
22132 cp_token *token = cp_lexer_peek_token (parser->lexer);
22134 /* Look for the `namespace' keyword. */
22135 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22136 /* Look for the identifier. */
22137 identifier = cp_parser_identifier (parser);
22138 if (identifier == error_mark_node)
22139 return;
22140 /* Look for the `=' token. */
22141 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
22142 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22144 error_at (token->location, "%<namespace%> definition is not allowed here");
22145 /* Skip the definition. */
22146 cp_lexer_consume_token (parser->lexer);
22147 if (cp_parser_skip_to_closing_brace (parser))
22148 cp_lexer_consume_token (parser->lexer);
22149 return;
22151 cp_parser_require (parser, CPP_EQ, RT_EQ);
22152 /* Look for the qualified-namespace-specifier. */
22153 namespace_specifier
22154 = cp_parser_qualified_namespace_specifier (parser);
22155 cp_warn_deprecated_use_scopes (namespace_specifier);
22156 /* Look for the `;' token. */
22157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22159 /* Register the alias in the symbol table. */
22160 do_namespace_alias (identifier, namespace_specifier);
22163 /* Parse a qualified-namespace-specifier.
22165 qualified-namespace-specifier:
22166 :: [opt] nested-name-specifier [opt] namespace-name
22168 Returns a NAMESPACE_DECL corresponding to the specified
22169 namespace. */
22171 static tree
22172 cp_parser_qualified_namespace_specifier (cp_parser* parser)
22174 /* Look for the optional `::'. */
22175 cp_parser_global_scope_opt (parser,
22176 /*current_scope_valid_p=*/false);
22178 /* Look for the optional nested-name-specifier. */
22179 cp_parser_nested_name_specifier_opt (parser,
22180 /*typename_keyword_p=*/false,
22181 /*check_dependency_p=*/true,
22182 /*type_p=*/false,
22183 /*is_declaration=*/true);
22185 return cp_parser_namespace_name (parser);
22188 /* Subroutine of cp_parser_using_declaration. */
22190 static tree
22191 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
22193 tree decl = NULL_TREE;
22194 if (at_class_scope_p ())
22196 /* Create the USING_DECL. */
22197 decl = do_class_using_decl (qscope, identifier);
22199 if (check_for_bare_parameter_packs (decl))
22200 return error_mark_node;
22202 if (decl && typename_p)
22203 USING_DECL_TYPENAME_P (decl) = 1;
22205 /* Add it to the list of members in this class. */
22206 finish_member_declaration (decl);
22208 else
22209 finish_nonmember_using_decl (qscope, identifier);
22210 return decl;
22213 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
22214 access declaration.
22216 using-declaration:
22217 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
22218 using :: unqualified-id ;
22220 access-declaration:
22221 qualified-id ;
22225 static bool
22226 cp_parser_using_declaration (cp_parser* parser,
22227 bool access_declaration_p)
22229 cp_token *token;
22230 bool typename_p = false;
22231 bool global_scope_p;
22232 tree identifier;
22233 tree qscope;
22234 int oldcount = errorcount;
22235 cp_token *diag_token = NULL;
22237 if (access_declaration_p)
22239 diag_token = cp_lexer_peek_token (parser->lexer);
22240 cp_parser_parse_tentatively (parser);
22242 else
22244 /* Look for the `using' keyword. */
22245 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22247 again:
22248 /* Peek at the next token. */
22249 token = cp_lexer_peek_token (parser->lexer);
22250 /* See if it's `typename'. */
22251 if (token->keyword == RID_TYPENAME)
22253 /* Remember that we've seen it. */
22254 typename_p = true;
22255 /* Consume the `typename' token. */
22256 cp_lexer_consume_token (parser->lexer);
22260 /* Look for the optional global scope qualification. */
22261 global_scope_p
22262 = (cp_parser_global_scope_opt (parser,
22263 /*current_scope_valid_p=*/false)
22264 != NULL_TREE);
22266 /* If we saw `typename', or didn't see `::', then there must be a
22267 nested-name-specifier present. */
22268 if (typename_p || !global_scope_p)
22270 qscope = cp_parser_nested_name_specifier (parser, typename_p,
22271 /*check_dependency_p=*/true,
22272 /*type_p=*/false,
22273 /*is_declaration=*/true);
22274 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
22276 cp_parser_skip_to_end_of_block_or_statement (parser);
22277 return false;
22280 /* Otherwise, we could be in either of the two productions. In that
22281 case, treat the nested-name-specifier as optional. */
22282 else
22283 qscope = cp_parser_nested_name_specifier_opt (parser,
22284 /*typename_keyword_p=*/false,
22285 /*check_dependency_p=*/true,
22286 /*type_p=*/false,
22287 /*is_declaration=*/true);
22288 if (!qscope)
22289 qscope = global_namespace;
22291 cp_warn_deprecated_use_scopes (qscope);
22293 if (access_declaration_p
22294 && !MAYBE_CLASS_TYPE_P (qscope)
22295 && TREE_CODE (qscope) != ENUMERAL_TYPE)
22296 /* If the qualifying scope of an access-declaration isn't a class
22297 or enumeration type then it can't be valid. */
22298 cp_parser_simulate_error (parser);
22300 if (access_declaration_p && cp_parser_error_occurred (parser))
22301 /* Something has already gone wrong; there's no need to parse
22302 further. Since an error has occurred, the return value of
22303 cp_parser_parse_definitely will be false, as required. */
22304 return cp_parser_parse_definitely (parser);
22306 token = cp_lexer_peek_token (parser->lexer);
22307 /* Parse the unqualified-id. */
22308 identifier = cp_parser_unqualified_id (parser,
22309 /*template_keyword_p=*/false,
22310 /*check_dependency_p=*/true,
22311 /*declarator_p=*/true,
22312 /*optional_p=*/false);
22314 if (access_declaration_p)
22316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22317 cp_parser_simulate_error (parser);
22318 if (!cp_parser_parse_definitely (parser))
22319 return false;
22321 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22323 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22324 if (cxx_dialect < cxx17)
22325 pedwarn (ell->location, OPT_Wc__17_extensions,
22326 "pack expansion in using-declaration only available "
22327 "with %<-std=c++17%> or %<-std=gnu++17%>");
22329 /* A parameter pack can appear in the qualifying scope, and/or in the
22330 terminal name (if naming a conversion function). Logically they're
22331 part of a single pack expansion of the overall USING_DECL, but we
22332 express them as separate pack expansions within the USING_DECL since
22333 we can't create a pack expansion over a USING_DECL. */
22334 bool saw_parm_pack = false;
22335 if (uses_parameter_packs (qscope))
22337 qscope = make_pack_expansion (qscope);
22338 saw_parm_pack = true;
22340 if (identifier_p (identifier)
22341 && IDENTIFIER_CONV_OP_P (identifier)
22342 && uses_parameter_packs (TREE_TYPE (identifier)))
22344 identifier = make_conv_op_name (make_pack_expansion
22345 (TREE_TYPE (identifier)));
22346 saw_parm_pack = true;
22348 if (!saw_parm_pack)
22350 /* Issue an error in terms using a SCOPE_REF that includes both
22351 components. */
22352 tree name
22353 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22354 make_pack_expansion (name);
22355 gcc_assert (seen_error ());
22356 qscope = identifier = error_mark_node;
22360 /* The function we call to handle a using-declaration is different
22361 depending on what scope we are in. */
22362 if (qscope == error_mark_node || identifier == error_mark_node)
22364 else if (!identifier_p (identifier)
22365 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22366 /* [namespace.udecl]
22368 A using declaration shall not name a template-id. */
22369 error_at (token->location,
22370 "a template-id may not appear in a using-declaration");
22371 else
22373 tree decl = finish_using_decl (qscope, identifier, typename_p);
22375 if (decl == error_mark_node)
22377 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22378 return false;
22382 if (!access_declaration_p
22383 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22385 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22386 if (cxx_dialect < cxx17)
22387 pedwarn (comma->location, OPT_Wc__17_extensions,
22388 "comma-separated list in using-declaration only available "
22389 "with %<-std=c++17%> or %<-std=gnu++17%>");
22390 goto again;
22393 /* Look for the final `;'. */
22394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22396 if (access_declaration_p && errorcount == oldcount)
22397 warning_at (diag_token->location, OPT_Wdeprecated,
22398 "access declarations are deprecated "
22399 "in favour of using-declarations; "
22400 "suggestion: add the %<using%> keyword");
22402 return true;
22405 /* C++20 using enum declaration.
22407 using-enum-declaration :
22408 using elaborated-enum-specifier ; */
22410 static void
22411 cp_parser_using_enum (cp_parser *parser)
22413 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22415 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22416 breaks one of the motivating examples in using-enum-5.C.
22417 cp_parser_simple_type_specifier seems to be closer to what we actually
22418 want, though that hasn't been properly specified yet. */
22420 /* Consume 'enum'. */
22421 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22422 cp_lexer_consume_token (parser->lexer);
22424 cp_token *start = cp_lexer_peek_token (parser->lexer);
22426 tree type = (cp_parser_simple_type_specifier
22427 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22429 cp_token *end = cp_lexer_previous_token (parser->lexer);
22431 if (type == error_mark_node
22432 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22434 cp_parser_skip_to_end_of_block_or_statement (parser);
22435 return;
22437 if (TREE_CODE (type) == TYPE_DECL)
22438 type = TREE_TYPE (type);
22440 /* The elaborated-enum-specifier shall not name a dependent type and the type
22441 shall have a reachable enum-specifier. */
22442 const char *msg = nullptr;
22443 if (cxx_dialect < cxx20)
22444 msg = G_("%<using enum%> "
22445 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22446 else if (dependent_type_p (type))
22447 msg = G_("%<using enum%> of dependent type %qT");
22448 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22449 msg = G_("%<using enum%> of non-enumeration type %q#T");
22450 else if (!COMPLETE_TYPE_P (type))
22451 msg = G_("%<using enum%> of incomplete type %qT");
22452 else if (OPAQUE_ENUM_P (type))
22453 msg = G_("%<using enum%> of %qT before its enum-specifier");
22454 if (msg)
22456 location_t loc = make_location (start, start, end);
22457 auto_diagnostic_group g;
22458 error_at (loc, msg, type);
22459 loc = location_of (type);
22460 if (cxx_dialect < cxx20 || loc == input_location)
22462 else if (OPAQUE_ENUM_P (type))
22463 inform (loc, "opaque-enum-declaration here");
22464 else
22465 inform (loc, "declared here");
22468 /* A using-enum-declaration introduces the enumerator names of the named
22469 enumeration as if by a using-declaration for each enumerator. */
22470 if (TREE_CODE (type) == ENUMERAL_TYPE)
22471 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22472 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22475 /* Parse an alias-declaration.
22477 alias-declaration:
22478 using identifier attribute-specifier-seq [opt] = type-id */
22480 static tree
22481 cp_parser_alias_declaration (cp_parser* parser)
22483 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22484 location_t id_location, type_location;
22485 cp_declarator *declarator;
22486 cp_decl_specifier_seq decl_specs;
22487 bool member_p;
22488 const char *saved_message = NULL;
22490 /* Look for the `using' keyword. */
22491 cp_token *using_token
22492 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22493 if (using_token == NULL)
22494 return error_mark_node;
22496 id_location = cp_lexer_peek_token (parser->lexer)->location;
22497 id = cp_parser_identifier (parser);
22498 if (id == error_mark_node)
22499 return error_mark_node;
22501 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22502 attributes = cp_parser_attributes_opt (parser);
22503 if (attributes == error_mark_node)
22504 return error_mark_node;
22506 cp_parser_require (parser, CPP_EQ, RT_EQ);
22508 if (cp_parser_error_occurred (parser))
22509 return error_mark_node;
22511 cp_parser_commit_to_tentative_parse (parser);
22513 /* Now we are going to parse the type-id of the declaration. */
22516 [dcl.type]/3 says:
22518 "A type-specifier-seq shall not define a class or enumeration
22519 unless it appears in the type-id of an alias-declaration (7.1.3) that
22520 is not the declaration of a template-declaration."
22522 In other words, if we currently are in an alias template, the
22523 type-id should not define a type.
22525 So let's set parser->type_definition_forbidden_message in that
22526 case; cp_parser_check_type_definition (called by
22527 cp_parser_class_specifier) will then emit an error if a type is
22528 defined in the type-id. */
22529 if (parser->num_template_parameter_lists)
22531 saved_message = parser->type_definition_forbidden_message;
22532 parser->type_definition_forbidden_message =
22533 G_("types may not be defined in alias template declarations");
22536 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22537 &type_location);
22539 /* Restore the error message if need be. */
22540 if (parser->num_template_parameter_lists)
22541 parser->type_definition_forbidden_message = saved_message;
22543 if (type == error_mark_node
22544 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22546 cp_parser_skip_to_end_of_block_or_statement (parser);
22547 return error_mark_node;
22550 /* A typedef-name can also be introduced by an alias-declaration. The
22551 identifier following the using keyword becomes a typedef-name. It has
22552 the same semantics as if it were introduced by the typedef
22553 specifier. In particular, it does not define a new type and it shall
22554 not appear in the type-id. */
22556 clear_decl_specs (&decl_specs);
22557 decl_specs.type = type;
22558 if (attributes != NULL_TREE)
22560 decl_specs.attributes = attributes;
22561 set_and_check_decl_spec_loc (&decl_specs,
22562 ds_attribute,
22563 attrs_token);
22565 set_and_check_decl_spec_loc (&decl_specs,
22566 ds_typedef,
22567 using_token);
22568 set_and_check_decl_spec_loc (&decl_specs,
22569 ds_alias,
22570 using_token);
22571 decl_specs.locations[ds_type_spec] = type_location;
22573 if (parser->num_template_parameter_lists
22574 && !cp_parser_check_template_parameters (parser,
22575 /*num_templates=*/0,
22576 /*template_id*/false,
22577 id_location,
22578 /*declarator=*/NULL))
22579 return error_mark_node;
22581 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22583 member_p = at_class_scope_p ();
22584 if (member_p)
22585 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22586 NULL_TREE, attributes);
22587 else
22588 decl = start_decl (declarator, &decl_specs, 0,
22589 attributes, NULL_TREE, &pushed_scope);
22590 if (decl == error_mark_node)
22591 return decl;
22593 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22595 if (pushed_scope)
22596 pop_scope (pushed_scope);
22598 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22599 added into the symbol table; otherwise, return the TYPE_DECL. */
22600 if (DECL_LANG_SPECIFIC (decl)
22601 && DECL_TEMPLATE_INFO (decl)
22602 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22604 decl = DECL_TI_TEMPLATE (decl);
22605 if (member_p)
22606 check_member_template (decl);
22609 return decl;
22612 /* Parse a using-directive.
22614 using-directive:
22615 attribute-specifier-seq [opt] using namespace :: [opt]
22616 nested-name-specifier [opt] namespace-name ; */
22618 static void
22619 cp_parser_using_directive (cp_parser* parser)
22621 tree namespace_decl;
22622 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22625 /* Error during attribute parsing that resulted in skipping
22626 to next semicolon. */
22627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22628 return;
22631 /* Look for the `using' keyword. */
22632 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22633 /* And the `namespace' keyword. */
22634 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22635 /* Look for the optional `::' operator. */
22636 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22637 /* And the optional nested-name-specifier. */
22638 cp_parser_nested_name_specifier_opt (parser,
22639 /*typename_keyword_p=*/false,
22640 /*check_dependency_p=*/true,
22641 /*type_p=*/false,
22642 /*is_declaration=*/true);
22643 /* Get the namespace being used. */
22644 namespace_decl = cp_parser_namespace_name (parser);
22645 cp_warn_deprecated_use_scopes (namespace_decl);
22646 /* And any specified GNU attributes. */
22647 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22648 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22650 /* Update the symbol table. */
22651 finish_using_directive (namespace_decl, attribs);
22653 /* Look for the final `;'. */
22654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22657 /* Parse an asm-definition.
22659 asm-qualifier:
22660 volatile
22661 inline
22662 goto
22664 asm-qualifier-list:
22665 asm-qualifier
22666 asm-qualifier-list asm-qualifier
22668 asm-definition:
22669 asm ( string-literal ) ;
22671 GNU Extension:
22673 asm-definition:
22674 asm asm-qualifier-list [opt] ( string-literal ) ;
22675 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22676 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22677 : asm-operand-list [opt] ) ;
22678 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22679 : asm-operand-list [opt]
22680 : asm-clobber-list [opt] ) ;
22681 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22682 : asm-clobber-list [opt]
22683 : asm-goto-list ) ;
22685 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22686 contains goto, and is the only allowed form in that case. No duplicates are
22687 allowed in an asm-qualifier-list. */
22689 static void
22690 cp_parser_asm_definition (cp_parser* parser)
22692 tree outputs = NULL_TREE;
22693 tree inputs = NULL_TREE;
22694 tree clobbers = NULL_TREE;
22695 tree labels = NULL_TREE;
22696 tree asm_stmt;
22697 bool extended_p = false;
22698 bool invalid_inputs_p = false;
22699 bool invalid_outputs_p = false;
22700 required_token missing = RT_NONE;
22701 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
22702 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22704 /* Look for the `asm' keyword. */
22705 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22707 /* In C++20, unevaluated inline assembly is permitted in constexpr
22708 functions. */
22709 if (parser->in_function_body
22710 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22711 && cxx_dialect < cxx20)
22712 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22713 "function only available with %<-std=c++20%> or "
22714 "%<-std=gnu++20%>");
22716 /* Handle the asm-qualifier-list. */
22717 location_t volatile_loc = UNKNOWN_LOCATION;
22718 location_t inline_loc = UNKNOWN_LOCATION;
22719 location_t goto_loc = UNKNOWN_LOCATION;
22720 location_t first_loc = UNKNOWN_LOCATION;
22722 if (cp_parser_allow_gnu_extensions_p (parser))
22723 for (;;)
22725 cp_token *token = cp_lexer_peek_token (parser->lexer);
22726 location_t loc = token->location;
22727 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22729 case RID_VOLATILE:
22730 if (volatile_loc)
22732 error_at (loc, "duplicate %<asm%> qualifier %qT",
22733 token->u.value);
22734 inform (volatile_loc, "first seen here");
22736 else
22738 if (!parser->in_function_body)
22739 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22740 "outside of function body", token->u.value);
22741 volatile_loc = loc;
22743 cp_lexer_consume_token (parser->lexer);
22744 continue;
22746 case RID_INLINE:
22747 if (inline_loc)
22749 error_at (loc, "duplicate %<asm%> qualifier %qT",
22750 token->u.value);
22751 inform (inline_loc, "first seen here");
22753 else
22754 inline_loc = loc;
22755 if (!first_loc)
22756 first_loc = loc;
22757 cp_lexer_consume_token (parser->lexer);
22758 continue;
22760 case RID_GOTO:
22761 if (goto_loc)
22763 error_at (loc, "duplicate %<asm%> qualifier %qT",
22764 token->u.value);
22765 inform (goto_loc, "first seen here");
22767 else
22768 goto_loc = loc;
22769 if (!first_loc)
22770 first_loc = loc;
22771 cp_lexer_consume_token (parser->lexer);
22772 continue;
22774 case RID_CONST:
22775 case RID_RESTRICT:
22776 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22777 cp_lexer_consume_token (parser->lexer);
22778 continue;
22780 default:
22781 break;
22783 break;
22786 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22787 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22788 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22790 if (!parser->in_function_body && (inline_p || goto_p))
22792 error_at (first_loc, "%<asm%> qualifier outside of function body");
22793 inline_p = goto_p = false;
22796 /* Look for the opening `('. */
22797 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22798 return;
22799 /* Look for the string. */
22800 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22801 /*wide_ok=*/false);
22802 if (string == error_mark_node)
22804 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22805 /*consume_paren=*/true);
22806 return;
22809 /* If we're allowing GNU extensions, check for the extended assembly
22810 syntax. Unfortunately, the `:' tokens need not be separated by
22811 a space in C, and so, for compatibility, we tolerate that here
22812 too. Doing that means that we have to treat the `::' operator as
22813 two `:' tokens. */
22814 if (cp_parser_allow_gnu_extensions_p (parser)
22815 && parser->in_function_body
22816 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22817 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22819 bool inputs_p = false;
22820 bool clobbers_p = false;
22821 bool labels_p = false;
22823 /* The extended syntax was used. */
22824 extended_p = true;
22826 /* Look for outputs. */
22827 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22829 /* Consume the `:'. */
22830 cp_lexer_consume_token (parser->lexer);
22831 /* Parse the output-operands. */
22832 if (cp_lexer_next_token_is_not (parser->lexer,
22833 CPP_COLON)
22834 && cp_lexer_next_token_is_not (parser->lexer,
22835 CPP_SCOPE)
22836 && cp_lexer_next_token_is_not (parser->lexer,
22837 CPP_CLOSE_PAREN))
22839 outputs = cp_parser_asm_operand_list (parser);
22840 if (outputs == error_mark_node)
22841 invalid_outputs_p = true;
22844 /* If the next token is `::', there are no outputs, and the
22845 next token is the beginning of the inputs. */
22846 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22847 /* The inputs are coming next. */
22848 inputs_p = true;
22850 /* Look for inputs. */
22851 if (inputs_p
22852 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22854 /* Consume the `:' or `::'. */
22855 cp_lexer_consume_token (parser->lexer);
22856 /* Parse the output-operands. */
22857 if (cp_lexer_next_token_is_not (parser->lexer,
22858 CPP_COLON)
22859 && cp_lexer_next_token_is_not (parser->lexer,
22860 CPP_SCOPE)
22861 && cp_lexer_next_token_is_not (parser->lexer,
22862 CPP_CLOSE_PAREN))
22864 inputs = cp_parser_asm_operand_list (parser);
22865 if (inputs == error_mark_node)
22866 invalid_inputs_p = true;
22869 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22870 /* The clobbers are coming next. */
22871 clobbers_p = true;
22873 /* Look for clobbers. */
22874 if (clobbers_p
22875 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22877 clobbers_p = true;
22878 /* Consume the `:' or `::'. */
22879 cp_lexer_consume_token (parser->lexer);
22880 /* Parse the clobbers. */
22881 if (cp_lexer_next_token_is_not (parser->lexer,
22882 CPP_COLON)
22883 && cp_lexer_next_token_is_not (parser->lexer,
22884 CPP_CLOSE_PAREN))
22885 clobbers = cp_parser_asm_clobber_list (parser);
22887 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22888 /* The labels are coming next. */
22889 labels_p = true;
22891 /* Look for labels. */
22892 if (labels_p
22893 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22895 labels_p = true;
22896 /* Consume the `:' or `::'. */
22897 cp_lexer_consume_token (parser->lexer);
22898 /* Parse the labels. */
22899 labels = cp_parser_asm_label_list (parser);
22902 if (goto_p && !labels_p)
22903 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22905 else if (goto_p)
22906 missing = RT_COLON_SCOPE;
22908 /* Look for the closing `)'. */
22909 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22910 missing ? missing : RT_CLOSE_PAREN))
22911 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22912 /*consume_paren=*/true);
22913 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22915 if (!invalid_inputs_p && !invalid_outputs_p)
22917 /* Create the ASM_EXPR. */
22918 if (parser->in_function_body)
22920 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22921 inputs, clobbers, labels, inline_p);
22922 /* If the extended syntax was not used, mark the ASM_EXPR. */
22923 if (!extended_p)
22925 tree temp = asm_stmt;
22926 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22927 temp = TREE_OPERAND (temp, 0);
22929 ASM_INPUT_P (temp) = 1;
22932 else
22933 symtab->finalize_toplevel_asm (string);
22936 if (std_attrs && any_nonignored_attribute_p (std_attrs))
22937 warning_at (asm_loc, OPT_Wattributes,
22938 "attributes ignored on %<asm%> declaration");
22941 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22942 type that comes from the decl-specifier-seq. */
22944 static tree
22945 strip_declarator_types (tree type, cp_declarator *declarator)
22947 for (cp_declarator *d = declarator; d;)
22948 switch (d->kind)
22950 case cdk_id:
22951 case cdk_decomp:
22952 case cdk_error:
22953 d = NULL;
22954 break;
22956 default:
22957 if (TYPE_PTRMEMFUNC_P (type))
22958 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22959 type = TREE_TYPE (type);
22960 d = d->declarator;
22961 break;
22964 return type;
22967 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22968 a construct looks like a variable definition but is actually a function
22969 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22970 is the declarator for this function declaration. */
22972 static void
22973 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22974 const cp_declarator *declarator)
22976 /* Only warn if we are declaring a function at block scope. */
22977 if (!at_function_scope_p ())
22978 return;
22980 /* And only if there is no storage class specified. */
22981 if (decl_specifiers->storage_class != sc_none
22982 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22983 return;
22985 if (declarator->kind != cdk_function
22986 || !declarator->declarator
22987 || declarator->declarator->kind != cdk_id
22988 || !identifier_p (get_unqualified_id
22989 (const_cast<cp_declarator *>(declarator))))
22990 return;
22992 /* Don't warn when the whole declarator (not just the declarator-id!)
22993 was parenthesized. That is, don't warn for int(n()) but do warn
22994 for int(f)(). */
22995 if (declarator->parenthesized != UNKNOWN_LOCATION)
22996 return;
22998 tree type;
22999 if (decl_specifiers->type)
23001 type = decl_specifiers->type;
23002 if (TREE_CODE (type) == TYPE_DECL)
23003 type = TREE_TYPE (type);
23005 /* If the return type is void there is no ambiguity. */
23006 if (same_type_p (type, void_type_node))
23007 return;
23009 else if (decl_specifiers->any_type_specifiers_p)
23010 /* Code like long f(); will have null ->type. If we have any
23011 type-specifiers, pretend we've seen int. */
23012 type = integer_type_node;
23013 else
23014 return;
23016 auto_diagnostic_group d;
23017 location_t loc = declarator->u.function.parens_loc;
23018 tree params = declarator->u.function.parameters;
23019 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
23021 /* The T t() case. */
23022 if (params == void_list_node)
23024 if (warning_at (loc, OPT_Wvexing_parse,
23025 "empty parentheses were disambiguated as a function "
23026 "declaration"))
23028 /* () means value-initialization (C++03 and up); {} (C++11 and up)
23029 means value-initialization or aggregate-initialization, nothing
23030 means default-initialization. We can only suggest removing the
23031 parentheses/adding {} if T has a default constructor. */
23032 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
23034 gcc_rich_location iloc (loc);
23035 iloc.add_fixit_remove ();
23036 inform (&iloc, "remove parentheses to default-initialize "
23037 "a variable");
23038 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23040 if (CP_AGGREGATE_TYPE_P (type))
23041 inform (loc, "or replace parentheses with braces to "
23042 "aggregate-initialize a variable");
23043 else
23044 inform (loc, "or replace parentheses with braces to "
23045 "value-initialize a variable");
23049 return;
23052 /* If we had (...) or the parameter-list wasn't parenthesized,
23053 we're done. */
23054 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
23055 return;
23057 /* The T t(X()) case. */
23058 if (list_length (params) == 2)
23060 if (warning_at (loc, OPT_Wvexing_parse,
23061 "parentheses were disambiguated as a function "
23062 "declaration"))
23064 gcc_rich_location iloc (loc);
23065 /* {}-initialization means that we can use an initializer-list
23066 constructor if no default constructor is available, so don't
23067 suggest using {} for classes that have an initializer_list
23068 constructor. */
23069 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23071 iloc.add_fixit_replace (get_start (loc), "{");
23072 iloc.add_fixit_replace (get_finish (loc), "}");
23073 inform (&iloc, "replace parentheses with braces to declare a "
23074 "variable");
23076 else
23078 iloc.add_fixit_insert_after (get_start (loc), "(");
23079 iloc.add_fixit_insert_before (get_finish (loc), ")");
23080 inform (&iloc, "add parentheses to declare a variable");
23084 /* The T t(X(), X()) case. */
23085 else if (warning_at (loc, OPT_Wvexing_parse,
23086 "parentheses were disambiguated as a function "
23087 "declaration"))
23089 gcc_rich_location iloc (loc);
23090 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23092 iloc.add_fixit_replace (get_start (loc), "{");
23093 iloc.add_fixit_replace (get_finish (loc), "}");
23094 inform (&iloc, "replace parentheses with braces to declare a "
23095 "variable");
23100 /* If DECLARATOR with DECL_SPECS is a function declarator that has
23101 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
23102 has the same meaning as in cp_parser_declarator. */
23104 static void
23105 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
23106 cp_decl_specifier_seq *decl_specs,
23107 cp_declarator *declarator,
23108 int *ctor_dtor_or_conv_p)
23110 if (cxx_dialect >= cxx17
23111 && *ctor_dtor_or_conv_p <= 0
23112 && !decl_specs->type
23113 && !decl_specs->any_type_specifiers_p
23114 && function_declarator_p (declarator))
23116 cp_declarator *id = get_id_declarator (declarator);
23117 tree name = id->u.id.unqualified_name;
23118 parser->scope = id->u.id.qualifying_scope;
23119 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
23120 if (tmpl
23121 && (DECL_CLASS_TEMPLATE_P (tmpl)
23122 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
23124 id->u.id.unqualified_name = dguide_name (tmpl);
23125 id->u.id.sfk = sfk_deduction_guide;
23126 *ctor_dtor_or_conv_p = 1;
23131 /* Declarators [gram.dcl.decl] */
23133 /* Parse an init-declarator.
23135 init-declarator:
23136 declarator initializer [opt]
23138 GNU Extension:
23140 init-declarator:
23141 declarator asm-specification [opt] attributes [opt] initializer [opt]
23143 function-definition:
23144 decl-specifier-seq [opt] declarator ctor-initializer [opt]
23145 function-body
23146 decl-specifier-seq [opt] declarator function-try-block
23148 GNU Extension:
23150 function-definition:
23151 __extension__ function-definition
23153 TM Extension:
23155 function-definition:
23156 decl-specifier-seq [opt] declarator function-transaction-block
23158 The parser flags FLAGS is used to control type-specifier parsing.
23160 The DECL_SPECIFIERS apply to this declarator. Returns a
23161 representation of the entity declared. If MEMBER_P is TRUE, then
23162 this declarator appears in a class scope. The new DECL created by
23163 this declarator is returned.
23165 The CHECKS are access checks that should be performed once we know
23166 what entity is being declared (and, therefore, what classes have
23167 befriended it).
23169 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
23170 for a function-definition here as well. If the declarator is a
23171 declarator for a function-definition, *FUNCTION_DEFINITION_P will
23172 be TRUE upon return. By that point, the function-definition will
23173 have been completely parsed.
23175 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
23176 is FALSE.
23178 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
23179 parsed declaration if it is an uninitialized single declarator not followed
23180 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
23181 if present, will not be consumed. If returned, this declarator will be
23182 created with SD_INITIALIZED but will not call cp_finish_decl.
23184 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
23185 and there is an initializer, the pointed location_t is set to the
23186 location of the '=' or `(', or '{' in C++11 token introducing the
23187 initializer. */
23189 static tree
23190 cp_parser_init_declarator (cp_parser* parser,
23191 cp_parser_flags flags,
23192 cp_decl_specifier_seq *decl_specifiers,
23193 vec<deferred_access_check, va_gc> *checks,
23194 bool function_definition_allowed_p,
23195 bool member_p,
23196 int declares_class_or_enum,
23197 bool* function_definition_p,
23198 tree* maybe_range_for_decl,
23199 location_t* init_loc,
23200 tree* auto_result)
23202 cp_token *token = NULL, *asm_spec_start_token = NULL,
23203 *attributes_start_token = NULL;
23204 cp_declarator *declarator;
23205 tree prefix_attributes;
23206 tree attributes = NULL;
23207 tree asm_specification;
23208 tree initializer;
23209 tree decl = NULL_TREE;
23210 tree scope;
23211 int is_initialized;
23212 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
23213 initialized with "= ..", CPP_OPEN_PAREN if initialized with
23214 "(...)". */
23215 enum cpp_ttype initialization_kind;
23216 bool is_direct_init = false;
23217 bool is_non_constant_init;
23218 int ctor_dtor_or_conv_p;
23219 bool friend_p = cp_parser_friend_p (decl_specifiers);
23220 bool static_p = decl_specifiers->storage_class == sc_static;
23221 tree pushed_scope = NULL_TREE;
23222 bool range_for_decl_p = false;
23223 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23224 location_t tmp_init_loc = UNKNOWN_LOCATION;
23226 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
23227 flags |= CP_PARSER_FLAGS_CONSTEVAL;
23229 /* Assume that this is not the declarator for a function
23230 definition. */
23231 if (function_definition_p)
23232 *function_definition_p = false;
23234 /* Default arguments are only permitted for function parameters. */
23235 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23236 parser->default_arg_ok_p = false;
23238 /* Defer access checks while parsing the declarator; we cannot know
23239 what names are accessible until we know what is being
23240 declared. */
23241 resume_deferring_access_checks ();
23243 token = cp_lexer_peek_token (parser->lexer);
23245 /* Parse the declarator. */
23246 declarator
23247 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23248 flags, &ctor_dtor_or_conv_p,
23249 /*parenthesized_p=*/NULL,
23250 member_p, friend_p, static_p);
23251 /* Gather up the deferred checks. */
23252 stop_deferring_access_checks ();
23254 parser->default_arg_ok_p = saved_default_arg_ok_p;
23256 /* If the DECLARATOR was erroneous, there's no need to go
23257 further. */
23258 if (declarator == cp_error_declarator)
23259 return error_mark_node;
23261 /* Check that the number of template-parameter-lists is OK. */
23262 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
23263 token->location))
23264 return error_mark_node;
23266 if (declares_class_or_enum & 2)
23267 cp_parser_check_for_definition_in_return_type (declarator,
23268 decl_specifiers->type,
23269 decl_specifiers->locations[ds_type_spec]);
23271 /* Figure out what scope the entity declared by the DECLARATOR is
23272 located in. `grokdeclarator' sometimes changes the scope, so
23273 we compute it now. */
23274 scope = get_scope_of_declarator (declarator);
23276 /* Perform any lookups in the declared type which were thought to be
23277 dependent, but are not in the scope of the declarator. */
23278 decl_specifiers->type
23279 = maybe_update_decl_type (decl_specifiers->type, scope);
23281 /* If we're allowing GNU extensions, look for an
23282 asm-specification. */
23283 if (cp_parser_allow_gnu_extensions_p (parser))
23285 /* Look for an asm-specification. */
23286 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
23287 asm_specification = cp_parser_asm_specification_opt (parser);
23289 else
23290 asm_specification = NULL_TREE;
23292 /* Gather the attributes that were provided with the
23293 decl-specifiers. */
23294 prefix_attributes = decl_specifiers->attributes;
23296 /* Look for attributes. */
23297 attributes_start_token = cp_lexer_peek_token (parser->lexer);
23298 attributes = cp_parser_attributes_opt (parser);
23300 /* Peek at the next token. */
23301 token = cp_lexer_peek_token (parser->lexer);
23303 bool bogus_implicit_tmpl = false;
23305 if (function_declarator_p (declarator))
23307 /* Handle C++17 deduction guides. Note that class-scope
23308 non-template deduction guides are instead handled in
23309 cp_parser_member_declaration. */
23310 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23311 decl_specifiers,
23312 declarator,
23313 &ctor_dtor_or_conv_p);
23315 if (!member_p && !cp_parser_error_occurred (parser))
23316 warn_about_ambiguous_parse (decl_specifiers, declarator);
23318 /* Check to see if the token indicates the start of a
23319 function-definition. */
23320 if (cp_parser_token_starts_function_definition_p (token))
23322 if (!function_definition_allowed_p)
23324 /* If a function-definition should not appear here, issue an
23325 error message. */
23326 cp_parser_error (parser,
23327 "a function-definition is not allowed here");
23328 return error_mark_node;
23331 location_t func_brace_location
23332 = cp_lexer_peek_token (parser->lexer)->location;
23334 /* Neither attributes nor an asm-specification are allowed
23335 on a function-definition. */
23336 if (asm_specification)
23337 error_at (asm_spec_start_token->location,
23338 "an %<asm%> specification is not allowed "
23339 "on a function-definition");
23340 if (attributes)
23341 error_at (attributes_start_token->location,
23342 "attributes are not allowed "
23343 "on a function-definition");
23344 /* This is a function-definition. */
23345 *function_definition_p = true;
23347 /* Parse the function definition. */
23348 if (member_p)
23349 decl = cp_parser_save_member_function_body (parser,
23350 decl_specifiers,
23351 declarator,
23352 prefix_attributes);
23353 else
23354 decl =
23355 (cp_parser_function_definition_from_specifiers_and_declarator
23356 (parser, decl_specifiers, prefix_attributes, declarator));
23358 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23360 /* This is where the prologue starts... */
23361 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23362 = func_brace_location;
23365 return decl;
23368 else if (parser->fully_implicit_function_template_p)
23370 /* A non-template declaration involving a function parameter list
23371 containing an implicit template parameter will be made into a
23372 template. If the resulting declaration is not going to be an
23373 actual function then finish the template scope here to prevent it.
23374 An error message will be issued once we have a decl to talk about.
23376 FIXME probably we should do type deduction rather than create an
23377 implicit template, but the standard currently doesn't allow it. */
23378 bogus_implicit_tmpl = true;
23379 finish_fully_implicit_template (parser, NULL_TREE);
23382 /* [dcl.dcl]
23384 Only in function declarations for constructors, destructors, type
23385 conversions, and deduction guides can the decl-specifier-seq be omitted.
23387 We explicitly postpone this check past the point where we handle
23388 function-definitions because we tolerate function-definitions
23389 that are missing their return types in some modes. */
23390 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23392 cp_parser_error (parser,
23393 "expected constructor, destructor, or type conversion");
23394 return error_mark_node;
23397 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23398 an initializer as well. */
23399 if (token->type == CPP_EQ
23400 || token->type == CPP_OPEN_PAREN
23401 || token->type == CPP_OPEN_BRACE)
23403 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23404 It isn't; it's an expression. (Here '(i)' would have already been
23405 parsed as a declarator.) */
23406 if (token->type == CPP_OPEN_PAREN
23407 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23409 cp_lexer_save_tokens (parser->lexer);
23410 cp_lexer_consume_token (parser->lexer);
23411 cp_parser_skip_to_closing_parenthesis (parser,
23412 /*recovering*/false,
23413 /*or_comma*/false,
23414 /*consume_paren*/true);
23415 /* If this is an initializer, only a ',' or ';' can follow: either
23416 we have another init-declarator, or we're at the end of an
23417 init-declarator-list which can only be followed by a ';'. */
23418 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23419 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23420 cp_lexer_rollback_tokens (parser->lexer);
23421 if (UNLIKELY (!ok))
23422 /* Not an init-declarator. */
23423 return error_mark_node;
23425 is_initialized = SD_INITIALIZED;
23426 initialization_kind = token->type;
23427 declarator->init_loc = token->location;
23428 if (maybe_range_for_decl)
23429 *maybe_range_for_decl = error_mark_node;
23430 tmp_init_loc = token->location;
23431 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23432 *init_loc = tmp_init_loc;
23434 if (token->type == CPP_EQ
23435 && function_declarator_p (declarator))
23437 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23438 if (t2->keyword == RID_DEFAULT)
23439 is_initialized = SD_DEFAULTED;
23440 else if (t2->keyword == RID_DELETE)
23441 is_initialized = SD_DELETED;
23444 else
23446 /* If the init-declarator isn't initialized and isn't followed by a
23447 `,' or `;', it's not a valid init-declarator. */
23448 if (token->type != CPP_COMMA
23449 && token->type != CPP_SEMICOLON)
23451 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23452 range_for_decl_p = true;
23453 else
23455 if (!maybe_range_for_decl)
23456 cp_parser_error (parser, "expected initializer");
23457 return error_mark_node;
23460 is_initialized = SD_UNINITIALIZED;
23461 initialization_kind = CPP_EOF;
23464 /* Because start_decl has side-effects, we should only call it if we
23465 know we're going ahead. By this point, we know that we cannot
23466 possibly be looking at any other construct. */
23467 cp_parser_commit_to_tentative_parse (parser);
23469 /* Enter the newly declared entry in the symbol table. If we're
23470 processing a declaration in a class-specifier, we wait until
23471 after processing the initializer. */
23472 if (!member_p)
23474 if (parser->in_unbraced_linkage_specification_p)
23475 decl_specifiers->storage_class = sc_extern;
23476 decl = start_decl (declarator, decl_specifiers,
23477 range_for_decl_p? SD_INITIALIZED : is_initialized,
23478 attributes, prefix_attributes, &pushed_scope);
23479 cp_finalize_omp_declare_simd (parser, decl);
23480 cp_finalize_oacc_routine (parser, decl, false);
23481 /* Adjust location of decl if declarator->id_loc is more appropriate:
23482 set, and decl wasn't merged with another decl, in which case its
23483 location would be different from input_location, and more accurate. */
23484 if (DECL_P (decl)
23485 && declarator->id_loc != UNKNOWN_LOCATION
23486 && DECL_SOURCE_LOCATION (decl) == input_location)
23487 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23489 else if (scope)
23490 /* Enter the SCOPE. That way unqualified names appearing in the
23491 initializer will be looked up in SCOPE. */
23492 pushed_scope = push_scope (scope);
23494 /* Perform deferred access control checks, now that we know in which
23495 SCOPE the declared entity resides. */
23496 if (!member_p && decl)
23498 tree saved_current_function_decl = NULL_TREE;
23500 /* If the entity being declared is a function, pretend that we
23501 are in its scope. If it is a `friend', it may have access to
23502 things that would not otherwise be accessible. */
23503 if (TREE_CODE (decl) == FUNCTION_DECL)
23505 saved_current_function_decl = current_function_decl;
23506 current_function_decl = decl;
23509 /* Perform access checks for template parameters. */
23510 cp_parser_perform_template_parameter_access_checks (checks);
23512 /* Perform the access control checks for the declarator and the
23513 decl-specifiers. */
23514 perform_deferred_access_checks (tf_warning_or_error);
23516 /* Restore the saved value. */
23517 if (TREE_CODE (decl) == FUNCTION_DECL)
23518 current_function_decl = saved_current_function_decl;
23521 /* Parse the initializer. */
23522 initializer = NULL_TREE;
23523 is_direct_init = false;
23524 is_non_constant_init = true;
23525 if (is_initialized)
23527 if (function_declarator_p (declarator))
23529 if (initialization_kind == CPP_EQ)
23530 initializer = cp_parser_pure_specifier (parser);
23531 else
23533 /* If the declaration was erroneous, we don't really
23534 know what the user intended, so just silently
23535 consume the initializer. */
23536 if (decl != error_mark_node)
23537 error_at (tmp_init_loc, "initializer provided for function");
23538 cp_parser_skip_to_closing_parenthesis (parser,
23539 /*recovering=*/true,
23540 /*or_comma=*/false,
23541 /*consume_paren=*/true);
23544 else
23546 /* We want to record the extra mangling scope for in-class
23547 initializers of class members and initializers of static
23548 data member templates and namespace-scope initializers.
23549 The former involves deferring parsing of the initializer
23550 until end of class as with default arguments. So right
23551 here we only handle the latter two. */
23552 bool has_lambda_scope = false;
23554 if (decl != error_mark_node
23555 && !member_p
23556 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23557 has_lambda_scope = true;
23559 if (has_lambda_scope)
23560 start_lambda_scope (decl);
23561 initializer = cp_parser_initializer (parser,
23562 &is_direct_init,
23563 &is_non_constant_init);
23564 if (has_lambda_scope)
23565 finish_lambda_scope ();
23566 if (initializer == error_mark_node)
23567 cp_parser_skip_to_end_of_statement (parser);
23571 /* The old parser allows attributes to appear after a parenthesized
23572 initializer. Mark Mitchell proposed removing this functionality
23573 on the GCC mailing lists on 2002-08-13. This parser accepts the
23574 attributes -- but ignores them. Made a permerror in GCC 8. */
23575 if (cp_parser_allow_gnu_extensions_p (parser)
23576 && initialization_kind == CPP_OPEN_PAREN
23577 && cp_parser_attributes_opt (parser)
23578 && permerror (input_location,
23579 "attributes after parenthesized initializer ignored"))
23581 static bool hint;
23582 if (flag_permissive && !hint)
23584 hint = true;
23585 inform (input_location,
23586 "this flexibility is deprecated and will be removed");
23590 /* And now complain about a non-function implicit template. */
23591 if (bogus_implicit_tmpl && decl != error_mark_node)
23592 error_at (DECL_SOURCE_LOCATION (decl),
23593 "non-function %qD declared as implicit template", decl);
23595 /* For an in-class declaration, use `grokfield' to create the
23596 declaration. */
23597 if (member_p)
23599 if (pushed_scope)
23601 pop_scope (pushed_scope);
23602 pushed_scope = NULL_TREE;
23604 decl = grokfield (declarator, decl_specifiers,
23605 initializer, !is_non_constant_init,
23606 /*asmspec=*/NULL_TREE,
23607 attr_chainon (attributes, prefix_attributes));
23608 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23609 cp_parser_save_default_args (parser, decl);
23610 cp_finalize_omp_declare_simd (parser, decl);
23611 cp_finalize_oacc_routine (parser, decl, false);
23614 /* Finish processing the declaration. But, skip member
23615 declarations. */
23616 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23618 cp_finish_decl (decl,
23619 initializer, !is_non_constant_init,
23620 asm_specification,
23621 /* If the initializer is in parentheses, then this is
23622 a direct-initialization, which means that an
23623 `explicit' constructor is OK. Otherwise, an
23624 `explicit' constructor cannot be used. */
23625 ((is_direct_init || !is_initialized)
23626 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23628 else if ((cxx_dialect != cxx98) && friend_p
23629 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23630 /* Core issue #226 (C++0x only): A default template-argument
23631 shall not be specified in a friend class template
23632 declaration. */
23633 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23634 /*is_partial=*/false, /*is_friend_decl=*/1);
23636 if (!friend_p && pushed_scope)
23637 pop_scope (pushed_scope);
23639 if (function_declarator_p (declarator)
23640 && parser->fully_implicit_function_template_p)
23642 if (member_p)
23643 decl = finish_fully_implicit_template (parser, decl);
23644 else
23645 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23648 if (auto_result && is_initialized && decl_specifiers->type
23649 && type_uses_auto (decl_specifiers->type))
23650 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23652 return decl;
23655 /* Parse a declarator.
23657 declarator:
23658 direct-declarator
23659 ptr-operator declarator
23661 abstract-declarator:
23662 ptr-operator abstract-declarator [opt]
23663 direct-abstract-declarator
23665 GNU Extensions:
23667 declarator:
23668 attributes [opt] direct-declarator
23669 attributes [opt] ptr-operator declarator
23671 abstract-declarator:
23672 attributes [opt] ptr-operator abstract-declarator [opt]
23673 attributes [opt] direct-abstract-declarator
23675 The parser flags FLAGS is used to control type-specifier parsing.
23677 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23678 detect constructors, destructors, deduction guides, or conversion operators.
23679 It is set to -1 if the declarator is a name, and +1 if it is a
23680 function. Otherwise it is set to zero. Usually you just want to
23681 test for >0, but internally the negative value is used.
23683 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23684 a decl-specifier-seq unless it declares a constructor, destructor,
23685 or conversion. It might seem that we could check this condition in
23686 semantic analysis, rather than parsing, but that makes it difficult
23687 to handle something like `f()'. We want to notice that there are
23688 no decl-specifiers, and therefore realize that this is an
23689 expression, not a declaration.)
23691 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23692 the declarator is a direct-declarator of the form "(...)".
23694 MEMBER_P is true iff this declarator is a member-declarator.
23696 FRIEND_P is true iff this declarator is a friend.
23698 STATIC_P is true iff the keyword static was seen. */
23700 static cp_declarator *
23701 cp_parser_declarator (cp_parser* parser,
23702 cp_parser_declarator_kind dcl_kind,
23703 cp_parser_flags flags,
23704 int* ctor_dtor_or_conv_p,
23705 bool* parenthesized_p,
23706 bool member_p, bool friend_p, bool static_p)
23708 cp_declarator *declarator;
23709 enum tree_code code;
23710 cp_cv_quals cv_quals;
23711 tree class_type;
23712 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23714 /* Assume this is not a constructor, destructor, or type-conversion
23715 operator. */
23716 if (ctor_dtor_or_conv_p)
23717 *ctor_dtor_or_conv_p = 0;
23719 if (cp_parser_allow_gnu_extensions_p (parser))
23720 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23722 /* Check for the ptr-operator production. */
23723 cp_parser_parse_tentatively (parser);
23724 /* Parse the ptr-operator. */
23725 code = cp_parser_ptr_operator (parser,
23726 &class_type,
23727 &cv_quals,
23728 &std_attributes);
23730 /* If that worked, then we have a ptr-operator. */
23731 if (cp_parser_parse_definitely (parser))
23733 /* If a ptr-operator was found, then this declarator was not
23734 parenthesized. */
23735 if (parenthesized_p)
23736 *parenthesized_p = false;
23737 /* The dependent declarator is optional if we are parsing an
23738 abstract-declarator. */
23739 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23740 cp_parser_parse_tentatively (parser);
23742 /* Parse the dependent declarator. */
23743 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23744 /*ctor_dtor_or_conv_p=*/NULL,
23745 /*parenthesized_p=*/NULL,
23746 member_p, friend_p, static_p);
23748 /* If we are parsing an abstract-declarator, we must handle the
23749 case where the dependent declarator is absent. */
23750 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23751 && !cp_parser_parse_definitely (parser))
23752 declarator = NULL;
23754 declarator = cp_parser_make_indirect_declarator
23755 (code, class_type, cv_quals, declarator, std_attributes);
23757 /* Everything else is a direct-declarator. */
23758 else
23760 if (parenthesized_p)
23761 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23762 CPP_OPEN_PAREN);
23763 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23764 flags, ctor_dtor_or_conv_p,
23765 member_p, friend_p, static_p);
23768 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23769 declarator->attributes = gnu_attributes;
23770 return declarator;
23773 /* Parse a direct-declarator or direct-abstract-declarator.
23775 direct-declarator:
23776 declarator-id
23777 direct-declarator ( parameter-declaration-clause )
23778 cv-qualifier-seq [opt]
23779 ref-qualifier [opt]
23780 exception-specification [opt]
23781 direct-declarator [ constant-expression [opt] ]
23782 ( declarator )
23784 direct-abstract-declarator:
23785 direct-abstract-declarator [opt]
23786 ( parameter-declaration-clause )
23787 cv-qualifier-seq [opt]
23788 ref-qualifier [opt]
23789 exception-specification [opt]
23790 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23791 ( abstract-declarator )
23793 Returns a representation of the declarator. DCL_KIND is
23794 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23795 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23796 we are parsing a direct-declarator. It is
23797 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23798 of ambiguity we prefer an abstract declarator, as per
23799 [dcl.ambig.res].
23800 The parser flags FLAGS is used to control type-specifier parsing.
23801 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23802 as for cp_parser_declarator. */
23804 static cp_declarator *
23805 cp_parser_direct_declarator (cp_parser* parser,
23806 cp_parser_declarator_kind dcl_kind,
23807 cp_parser_flags flags,
23808 int* ctor_dtor_or_conv_p,
23809 bool member_p, bool friend_p, bool static_p)
23811 cp_token *token;
23812 cp_declarator *declarator = NULL;
23813 tree scope = NULL_TREE;
23814 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23815 bool saved_in_declarator_p = parser->in_declarator_p;
23816 bool first = true;
23817 tree pushed_scope = NULL_TREE;
23818 cp_token *open_paren = NULL, *close_paren = NULL;
23820 while (true)
23822 /* Peek at the next token. */
23823 token = cp_lexer_peek_token (parser->lexer);
23824 if (token->type == CPP_OPEN_PAREN)
23826 /* This is either a parameter-declaration-clause, or a
23827 parenthesized declarator. When we know we are parsing a
23828 named declarator, it must be a parenthesized declarator
23829 if FIRST is true. For instance, `(int)' is a
23830 parameter-declaration-clause, with an omitted
23831 direct-abstract-declarator. But `((*))', is a
23832 parenthesized abstract declarator. Finally, when T is a
23833 template parameter `(T)' is a
23834 parameter-declaration-clause, and not a parenthesized
23835 named declarator.
23837 We first try and parse a parameter-declaration-clause,
23838 and then try a nested declarator (if FIRST is true).
23840 It is not an error for it not to be a
23841 parameter-declaration-clause, even when FIRST is
23842 false. Consider,
23844 int i (int);
23845 int i (3);
23847 The first is the declaration of a function while the
23848 second is the definition of a variable, including its
23849 initializer.
23851 Having seen only the parenthesis, we cannot know which of
23852 these two alternatives should be selected. Even more
23853 complex are examples like:
23855 int i (int (a));
23856 int i (int (3));
23858 The former is a function-declaration; the latter is a
23859 variable initialization.
23861 Thus again, we try a parameter-declaration-clause, and if
23862 that fails, we back out and return. */
23864 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23866 tree params;
23867 bool is_declarator = false;
23869 open_paren = NULL;
23871 /* In a member-declarator, the only valid interpretation
23872 of a parenthesis is the start of a
23873 parameter-declaration-clause. (It is invalid to
23874 initialize a static data member with a parenthesized
23875 initializer; only the "=" form of initialization is
23876 permitted.) */
23877 if (!member_p)
23878 cp_parser_parse_tentatively (parser);
23880 /* Consume the `('. */
23881 const location_t parens_start = token->location;
23882 matching_parens parens;
23883 parens.consume_open (parser);
23884 if (first)
23886 /* If this is going to be an abstract declarator, we're
23887 in a declarator and we can't have default args. */
23888 parser->default_arg_ok_p = false;
23889 parser->in_declarator_p = true;
23892 begin_scope (sk_function_parms, NULL_TREE);
23894 /* Parse the parameter-declaration-clause. */
23895 params
23896 = cp_parser_parameter_declaration_clause (parser, flags);
23897 const location_t parens_end
23898 = cp_lexer_peek_token (parser->lexer)->location;
23900 /* Consume the `)'. */
23901 parens.require_close (parser);
23903 /* For code like
23904 int x(auto(42));
23905 A a(auto(i), 42);
23906 we have synthesized an implicit template parameter and marked
23907 what we thought was a function as an implicit function template.
23908 But now, having seen the whole parameter list, we know it's not
23909 a function declaration, so undo that. */
23910 if (cp_parser_error_occurred (parser)
23911 && parser->fully_implicit_function_template_p
23912 /* Don't do this for the inner (). */
23913 && parser->default_arg_ok_p)
23914 abort_fully_implicit_template (parser);
23916 /* If all went well, parse the cv-qualifier-seq,
23917 ref-qualifier and the exception-specification. */
23918 if (member_p || cp_parser_parse_definitely (parser))
23920 cp_cv_quals cv_quals;
23921 cp_virt_specifiers virt_specifiers;
23922 cp_ref_qualifier ref_qual;
23923 tree exception_specification;
23924 tree late_return;
23925 tree attrs;
23926 bool memfn = (member_p || (pushed_scope
23927 && CLASS_TYPE_P (pushed_scope)));
23928 unsigned char local_variables_forbidden_p
23929 = parser->local_variables_forbidden_p;
23930 /* 'this' is not allowed in static member functions. */
23931 if (static_p || friend_p)
23932 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23934 is_declarator = true;
23936 if (ctor_dtor_or_conv_p)
23937 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23938 first = false;
23940 /* Parse the cv-qualifier-seq. */
23941 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23942 /* Parse the ref-qualifier. */
23943 ref_qual = cp_parser_ref_qualifier_opt (parser);
23944 /* Parse the tx-qualifier. */
23945 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23947 tree save_ccp = current_class_ptr;
23948 tree save_ccr = current_class_ref;
23949 if (memfn && !friend_p && !static_p)
23950 /* DR 1207: 'this' is in scope after the cv-quals. */
23951 inject_this_parameter (current_class_type, cv_quals);
23953 /* If it turned out that this is e.g. a pointer to a
23954 function, we don't want to delay noexcept parsing. */
23955 if (declarator == NULL || declarator->kind != cdk_id)
23956 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23958 /* Parse the exception-specification. */
23959 exception_specification
23960 = cp_parser_exception_specification_opt (parser,
23961 flags);
23963 attrs = cp_parser_std_attribute_spec_seq (parser);
23965 cp_omp_declare_simd_data odsd;
23966 if ((flag_openmp || flag_openmp_simd)
23967 && declarator
23968 && declarator->std_attributes
23969 && declarator->kind == cdk_id)
23971 tree *pa = &declarator->std_attributes;
23972 cp_parser_handle_directive_omp_attributes (parser, pa,
23973 &odsd, false);
23976 /* In here, we handle cases where attribute is used after
23977 the function declaration. For example:
23978 void func (int x) __attribute__((vector(..))); */
23979 tree gnu_attrs = NULL_TREE;
23980 tree requires_clause = NULL_TREE;
23981 late_return
23982 = cp_parser_late_return_type_opt (parser, declarator,
23983 requires_clause);
23985 cp_finalize_omp_declare_simd (parser, &odsd);
23987 /* Parse the virt-specifier-seq. */
23988 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23990 location_t parens_loc = make_location (parens_start,
23991 parens_start,
23992 parens_end);
23993 /* Create the function-declarator. */
23994 declarator = make_call_declarator (declarator,
23995 params,
23996 cv_quals,
23997 virt_specifiers,
23998 ref_qual,
23999 tx_qual,
24000 exception_specification,
24001 late_return,
24002 requires_clause,
24003 attrs,
24004 parens_loc);
24005 declarator->attributes = gnu_attrs;
24006 /* Any subsequent parameter lists are to do with
24007 return type, so are not those of the declared
24008 function. */
24009 parser->default_arg_ok_p = false;
24011 current_class_ptr = save_ccp;
24012 current_class_ref = save_ccr;
24014 /* Restore the state of local_variables_forbidden_p. */
24015 parser->local_variables_forbidden_p
24016 = local_variables_forbidden_p;
24019 /* Remove the function parms from scope. */
24020 pop_bindings_and_leave_scope ();
24022 if (is_declarator)
24023 /* Repeat the main loop. */
24024 continue;
24027 /* If this is the first, we can try a parenthesized
24028 declarator. */
24029 if (first)
24031 bool saved_in_type_id_in_expr_p;
24033 parser->default_arg_ok_p = saved_default_arg_ok_p;
24034 parser->in_declarator_p = saved_in_declarator_p;
24036 open_paren = token;
24037 /* Consume the `('. */
24038 matching_parens parens;
24039 parens.consume_open (parser);
24040 /* Parse the nested declarator. */
24041 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24042 parser->in_type_id_in_expr_p = true;
24043 declarator
24044 = cp_parser_declarator (parser, dcl_kind, flags,
24045 ctor_dtor_or_conv_p,
24046 /*parenthesized_p=*/NULL,
24047 member_p, friend_p,
24048 /*static_p=*/false);
24049 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24050 first = false;
24051 /* Expect a `)'. */
24052 close_paren = cp_lexer_peek_token (parser->lexer);
24053 if (!parens.require_close (parser))
24054 declarator = cp_error_declarator;
24055 if (declarator == cp_error_declarator)
24056 break;
24058 goto handle_declarator;
24060 /* Otherwise, we must be done. */
24061 else
24062 break;
24064 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
24065 && token->type == CPP_OPEN_SQUARE
24066 && !cp_next_tokens_can_be_attribute_p (parser))
24068 /* Parse an array-declarator. */
24069 tree bounds, attrs;
24071 if (ctor_dtor_or_conv_p)
24072 *ctor_dtor_or_conv_p = 0;
24074 open_paren = NULL;
24075 first = false;
24076 parser->default_arg_ok_p = false;
24077 parser->in_declarator_p = true;
24078 /* Consume the `['. */
24079 cp_lexer_consume_token (parser->lexer);
24080 /* Peek at the next token. */
24081 token = cp_lexer_peek_token (parser->lexer);
24082 /* If the next token is `]', then there is no
24083 constant-expression. */
24084 if (token->type != CPP_CLOSE_SQUARE)
24086 bool non_constant_p;
24087 bounds
24088 = cp_parser_constant_expression (parser,
24089 /*allow_non_constant=*/true,
24090 &non_constant_p);
24091 if (!non_constant_p)
24092 /* OK */;
24093 else if (error_operand_p (bounds))
24094 /* Already gave an error. */;
24095 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24096 /* Let compute_array_index_type diagnose this. */;
24097 else if (!parser->in_function_body
24098 || parsing_function_declarator ())
24100 /* Normally, the array bound must be an integral constant
24101 expression. However, as an extension, we allow VLAs
24102 in function scopes as long as they aren't part of a
24103 parameter declaration. */
24104 cp_parser_error (parser,
24105 "array bound is not an integer constant");
24106 bounds = error_mark_node;
24108 else if (processing_template_decl
24109 && !type_dependent_expression_p (bounds))
24111 /* Remember this wasn't a constant-expression. */
24112 bounds = build_nop (TREE_TYPE (bounds), bounds);
24113 TREE_SIDE_EFFECTS (bounds) = 1;
24116 else
24117 bounds = NULL_TREE;
24118 /* Look for the closing `]'. */
24119 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24121 declarator = cp_error_declarator;
24122 break;
24125 attrs = cp_parser_std_attribute_spec_seq (parser);
24126 declarator = make_array_declarator (declarator, bounds);
24127 declarator->std_attributes = attrs;
24129 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
24132 tree qualifying_scope;
24133 tree unqualified_name;
24134 tree attrs;
24135 special_function_kind sfk;
24136 bool abstract_ok;
24137 bool pack_expansion_p = false;
24138 cp_token *declarator_id_start_token;
24140 /* Parse a declarator-id */
24141 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
24142 if (abstract_ok)
24144 cp_parser_parse_tentatively (parser);
24146 /* If we see an ellipsis, we should be looking at a
24147 parameter pack. */
24148 if (token->type == CPP_ELLIPSIS)
24150 /* Consume the `...' */
24151 cp_lexer_consume_token (parser->lexer);
24153 pack_expansion_p = true;
24157 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
24158 unqualified_name
24159 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
24160 qualifying_scope = parser->scope;
24161 if (abstract_ok)
24163 bool okay = false;
24165 if (!unqualified_name && pack_expansion_p)
24167 /* Check whether an error occurred. */
24168 okay = !cp_parser_error_occurred (parser);
24170 /* We already consumed the ellipsis to mark a
24171 parameter pack, but we have no way to report it,
24172 so abort the tentative parse. We will be exiting
24173 immediately anyway. */
24174 cp_parser_abort_tentative_parse (parser);
24176 else
24177 okay = cp_parser_parse_definitely (parser);
24179 if (!okay)
24180 unqualified_name = error_mark_node;
24181 else if (unqualified_name
24182 && (qualifying_scope
24183 || (!identifier_p (unqualified_name))))
24185 cp_parser_error (parser, "expected unqualified-id");
24186 unqualified_name = error_mark_node;
24190 if (!unqualified_name)
24191 return NULL;
24192 if (unqualified_name == error_mark_node)
24194 declarator = cp_error_declarator;
24195 pack_expansion_p = false;
24196 declarator->parameter_pack_p = false;
24197 break;
24200 attrs = cp_parser_std_attribute_spec_seq (parser);
24202 if (qualifying_scope && at_namespace_scope_p ()
24203 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
24205 /* In the declaration of a member of a template class
24206 outside of the class itself, the SCOPE will sometimes
24207 be a TYPENAME_TYPE. For example, given:
24209 template <typename T>
24210 int S<T>::R::i = 3;
24212 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
24213 this context, we must resolve S<T>::R to an ordinary
24214 type, rather than a typename type.
24216 The reason we normally avoid resolving TYPENAME_TYPEs
24217 is that a specialization of `S' might render
24218 `S<T>::R' not a type. However, if `S' is
24219 specialized, then this `i' will not be used, so there
24220 is no harm in resolving the types here. */
24221 tree type;
24223 /* Resolve the TYPENAME_TYPE. */
24224 type = resolve_typename_type (qualifying_scope,
24225 /*only_current_p=*/false);
24226 /* If that failed, the declarator is invalid. */
24227 if (TREE_CODE (type) == TYPENAME_TYPE)
24229 if (typedef_variant_p (type))
24230 error_at (declarator_id_start_token->location,
24231 "cannot define member of dependent typedef "
24232 "%qT", type);
24233 else
24234 error_at (declarator_id_start_token->location,
24235 "%<%T::%E%> is not a type",
24236 TYPE_CONTEXT (qualifying_scope),
24237 TYPE_IDENTIFIER (qualifying_scope));
24239 qualifying_scope = type;
24242 sfk = sfk_none;
24244 if (unqualified_name)
24246 tree class_type;
24248 if (qualifying_scope
24249 && CLASS_TYPE_P (qualifying_scope))
24250 class_type = qualifying_scope;
24251 else
24252 class_type = current_class_type;
24254 if (TREE_CODE (unqualified_name) == TYPE_DECL)
24256 tree name_type = TREE_TYPE (unqualified_name);
24258 if (!class_type || !same_type_p (name_type, class_type))
24260 /* We do not attempt to print the declarator
24261 here because we do not have enough
24262 information about its original syntactic
24263 form. */
24264 cp_parser_error (parser, "invalid declarator");
24265 declarator = cp_error_declarator;
24266 break;
24268 else if (qualifying_scope
24269 && CLASSTYPE_USE_TEMPLATE (name_type))
24271 error_at (declarator_id_start_token->location,
24272 "invalid use of constructor as a template");
24273 inform (declarator_id_start_token->location,
24274 "use %<%T::%D%> instead of %<%T::%D%> to "
24275 "name the constructor in a qualified name",
24276 class_type,
24277 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
24278 class_type, name_type);
24279 declarator = cp_error_declarator;
24280 break;
24282 unqualified_name = constructor_name (class_type);
24285 if (class_type)
24287 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
24288 sfk = sfk_destructor;
24289 else if (identifier_p (unqualified_name)
24290 && IDENTIFIER_CONV_OP_P (unqualified_name))
24291 sfk = sfk_conversion;
24292 else if (/* There's no way to declare a constructor
24293 for an unnamed type, even if the type
24294 got a name for linkage purposes. */
24295 !TYPE_WAS_UNNAMED (class_type)
24296 /* Handle correctly (c++/19200):
24298 struct S {
24299 struct T{};
24300 friend void S(T);
24303 and also:
24305 namespace N {
24306 void S();
24309 struct S {
24310 friend void N::S();
24311 }; */
24312 && (!friend_p || class_type == qualifying_scope)
24313 && constructor_name_p (unqualified_name,
24314 class_type))
24315 sfk = sfk_constructor;
24316 else if (is_overloaded_fn (unqualified_name)
24317 && DECL_CONSTRUCTOR_P (get_first_fn
24318 (unqualified_name)))
24319 sfk = sfk_constructor;
24321 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24322 *ctor_dtor_or_conv_p = -1;
24325 declarator = make_id_declarator (qualifying_scope,
24326 unqualified_name,
24327 sfk, token->location);
24328 declarator->std_attributes = attrs;
24329 declarator->parameter_pack_p = pack_expansion_p;
24331 if (pack_expansion_p)
24332 maybe_warn_variadic_templates ();
24334 /* We're looking for this case in [temp.res]:
24335 A qualified-id is assumed to name a type if [...]
24336 - it is a decl-specifier of the decl-specifier-seq of a
24337 parameter-declaration in a declarator of a function or
24338 function template declaration, ... */
24339 if (cxx_dialect >= cxx20
24340 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24341 && declarator->kind == cdk_id
24342 && !at_class_scope_p ()
24343 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24345 /* ...whose declarator-id is qualified. If it isn't, never
24346 assume the parameters to refer to types. */
24347 if (qualifying_scope == NULL_TREE)
24348 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24349 else
24351 /* Now we have something like
24352 template <typename T> int C::x(S::p);
24353 which can be a function template declaration or a
24354 variable template definition. If name lookup for
24355 the declarator-id C::x finds one or more function
24356 templates, assume S::p to name a type. Otherwise,
24357 don't. */
24358 tree decl
24359 = cp_parser_lookup_name (parser, unqualified_name,
24360 none_type,
24361 /*is_template=*/false,
24362 /*is_namespace=*/false,
24363 /*check_dependency=*/false,
24364 /*ambiguous_decls=*/NULL,
24365 token->location);
24367 if (!is_overloaded_fn (decl)
24368 /* Allow
24369 template<typename T>
24370 A<T>::A(T::type) { } */
24371 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24372 && constructor_name_p (unqualified_name,
24373 qualifying_scope)))
24374 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24379 handle_declarator:;
24380 scope = get_scope_of_declarator (declarator);
24381 if (scope)
24383 /* Any names that appear after the declarator-id for a
24384 member are looked up in the containing scope. */
24385 if (at_function_scope_p ())
24387 /* But declarations with qualified-ids can't appear in a
24388 function. */
24389 cp_parser_error (parser, "qualified-id in declaration");
24390 declarator = cp_error_declarator;
24391 break;
24393 pushed_scope = push_scope (scope);
24395 parser->in_declarator_p = true;
24396 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24397 || (declarator && declarator->kind == cdk_id))
24398 /* Default args are only allowed on function
24399 declarations. */
24400 parser->default_arg_ok_p = saved_default_arg_ok_p;
24401 else
24402 parser->default_arg_ok_p = false;
24404 first = false;
24406 /* We're done. */
24407 else
24408 break;
24411 /* For an abstract declarator, we might wind up with nothing at this
24412 point. That's an error; the declarator is not optional. */
24413 if (!declarator)
24414 cp_parser_error (parser, "expected declarator");
24415 else if (open_paren)
24417 /* Record overly parenthesized declarator so we can give a
24418 diagnostic about confusing decl/expr disambiguation. */
24419 if (declarator->kind == cdk_array)
24421 /* If the open and close parens are on different lines, this
24422 is probably a formatting thing, so ignore. */
24423 expanded_location open = expand_location (open_paren->location);
24424 expanded_location close = expand_location (close_paren->location);
24425 if (open.line != close.line || open.file != close.file)
24426 open_paren = NULL;
24428 if (open_paren)
24429 declarator->parenthesized = make_location (open_paren->location,
24430 open_paren->location,
24431 close_paren->location);
24434 /* If we entered a scope, we must exit it now. */
24435 if (pushed_scope)
24436 pop_scope (pushed_scope);
24438 parser->default_arg_ok_p = saved_default_arg_ok_p;
24439 parser->in_declarator_p = saved_in_declarator_p;
24441 return declarator;
24444 /* Parse a ptr-operator.
24446 ptr-operator:
24447 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24448 * cv-qualifier-seq [opt]
24450 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24451 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24453 GNU Extension:
24455 ptr-operator:
24456 & cv-qualifier-seq [opt]
24458 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24459 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24460 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24461 filled in with the TYPE containing the member. *CV_QUALS is
24462 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24463 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24464 Note that the tree codes returned by this function have nothing
24465 to do with the types of trees that will be eventually be created
24466 to represent the pointer or reference type being parsed. They are
24467 just constants with suggestive names. */
24468 static enum tree_code
24469 cp_parser_ptr_operator (cp_parser* parser,
24470 tree* type,
24471 cp_cv_quals *cv_quals,
24472 tree *attributes)
24474 enum tree_code code = ERROR_MARK;
24475 cp_token *token;
24476 tree attrs = NULL_TREE;
24478 /* Assume that it's not a pointer-to-member. */
24479 *type = NULL_TREE;
24480 /* And that there are no cv-qualifiers. */
24481 *cv_quals = TYPE_UNQUALIFIED;
24483 /* Peek at the next token. */
24484 token = cp_lexer_peek_token (parser->lexer);
24486 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24487 if (token->type == CPP_MULT)
24488 code = INDIRECT_REF;
24489 else if (token->type == CPP_AND)
24490 code = ADDR_EXPR;
24491 else if ((cxx_dialect != cxx98) &&
24492 token->type == CPP_AND_AND) /* C++0x only */
24493 code = NON_LVALUE_EXPR;
24495 if (code != ERROR_MARK)
24497 /* Consume the `*', `&' or `&&'. */
24498 cp_lexer_consume_token (parser->lexer);
24500 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24501 `&', if we are allowing GNU extensions. (The only qualifier
24502 that can legally appear after `&' is `restrict', but that is
24503 enforced during semantic analysis. */
24504 if (code == INDIRECT_REF
24505 || cp_parser_allow_gnu_extensions_p (parser))
24506 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24508 attrs = cp_parser_std_attribute_spec_seq (parser);
24509 if (attributes != NULL)
24510 *attributes = attrs;
24512 else
24514 /* Try the pointer-to-member case. */
24515 cp_parser_parse_tentatively (parser);
24516 /* Look for the optional `::' operator. */
24517 cp_parser_global_scope_opt (parser,
24518 /*current_scope_valid_p=*/false);
24519 /* Look for the nested-name specifier. */
24520 token = cp_lexer_peek_token (parser->lexer);
24521 cp_parser_nested_name_specifier (parser,
24522 /*typename_keyword_p=*/false,
24523 /*check_dependency_p=*/true,
24524 /*type_p=*/false,
24525 /*is_declaration=*/false);
24526 /* If we found it, and the next token is a `*', then we are
24527 indeed looking at a pointer-to-member operator. */
24528 if (!cp_parser_error_occurred (parser)
24529 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24531 /* Indicate that the `*' operator was used. */
24532 code = INDIRECT_REF;
24534 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24535 error_at (token->location, "%qD is a namespace", parser->scope);
24536 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24537 error_at (token->location, "cannot form pointer to member of "
24538 "non-class %q#T", parser->scope);
24539 else
24541 /* The type of which the member is a member is given by the
24542 current SCOPE. */
24543 *type = parser->scope;
24544 /* The next name will not be qualified. */
24545 parser->scope = NULL_TREE;
24546 parser->qualifying_scope = NULL_TREE;
24547 parser->object_scope = NULL_TREE;
24548 /* Look for optional c++11 attributes. */
24549 attrs = cp_parser_std_attribute_spec_seq (parser);
24550 if (attributes != NULL)
24551 *attributes = attrs;
24552 /* Look for the optional cv-qualifier-seq. */
24553 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24556 /* If that didn't work we don't have a ptr-operator. */
24557 if (!cp_parser_parse_definitely (parser))
24558 cp_parser_error (parser, "expected ptr-operator");
24561 return code;
24564 /* Parse an (optional) cv-qualifier-seq.
24566 cv-qualifier-seq:
24567 cv-qualifier cv-qualifier-seq [opt]
24569 cv-qualifier:
24570 const
24571 volatile
24573 GNU Extension:
24575 cv-qualifier:
24576 __restrict__
24578 Returns a bitmask representing the cv-qualifiers. */
24580 static cp_cv_quals
24581 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24583 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24585 while (true)
24587 cp_token *token;
24588 cp_cv_quals cv_qualifier;
24590 /* Peek at the next token. */
24591 token = cp_lexer_peek_token (parser->lexer);
24592 /* See if it's a cv-qualifier. */
24593 switch (token->keyword)
24595 case RID_CONST:
24596 cv_qualifier = TYPE_QUAL_CONST;
24597 break;
24599 case RID_VOLATILE:
24600 cv_qualifier = TYPE_QUAL_VOLATILE;
24601 break;
24603 case RID_RESTRICT:
24604 cv_qualifier = TYPE_QUAL_RESTRICT;
24605 break;
24607 default:
24608 cv_qualifier = TYPE_UNQUALIFIED;
24609 break;
24612 if (!cv_qualifier)
24613 break;
24615 if (cv_quals & cv_qualifier)
24617 gcc_rich_location richloc (token->location);
24618 richloc.add_fixit_remove ();
24619 error_at (&richloc, "duplicate cv-qualifier");
24620 cp_lexer_purge_token (parser->lexer);
24622 else
24624 cp_lexer_consume_token (parser->lexer);
24625 cv_quals |= cv_qualifier;
24629 return cv_quals;
24632 /* Parse an (optional) ref-qualifier
24634 ref-qualifier:
24638 Returns cp_ref_qualifier representing ref-qualifier. */
24640 static cp_ref_qualifier
24641 cp_parser_ref_qualifier_opt (cp_parser* parser)
24643 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24645 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24646 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24647 return ref_qual;
24649 while (true)
24651 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24652 cp_token *token = cp_lexer_peek_token (parser->lexer);
24654 switch (token->type)
24656 case CPP_AND:
24657 curr_ref_qual = REF_QUAL_LVALUE;
24658 break;
24660 case CPP_AND_AND:
24661 curr_ref_qual = REF_QUAL_RVALUE;
24662 break;
24664 default:
24665 curr_ref_qual = REF_QUAL_NONE;
24666 break;
24669 if (!curr_ref_qual)
24670 break;
24671 else if (ref_qual)
24673 error_at (token->location, "multiple ref-qualifiers");
24674 cp_lexer_purge_token (parser->lexer);
24676 else
24678 ref_qual = curr_ref_qual;
24679 cp_lexer_consume_token (parser->lexer);
24683 return ref_qual;
24686 /* Parse an optional tx-qualifier.
24688 tx-qualifier:
24689 transaction_safe
24690 transaction_safe_dynamic */
24692 static tree
24693 cp_parser_tx_qualifier_opt (cp_parser *parser)
24695 cp_token *token = cp_lexer_peek_token (parser->lexer);
24696 if (token->type == CPP_NAME)
24698 tree name = token->u.value;
24699 const char *p = IDENTIFIER_POINTER (name);
24700 const int len = strlen ("transaction_safe");
24701 if (startswith (p, "transaction_safe"))
24703 p += len;
24704 if (*p == '\0'
24705 || !strcmp (p, "_dynamic"))
24707 cp_lexer_consume_token (parser->lexer);
24708 if (!flag_tm)
24710 error ("%qE requires %<-fgnu-tm%>", name);
24711 return NULL_TREE;
24713 else
24714 return name;
24718 return NULL_TREE;
24721 /* Parse an (optional) virt-specifier-seq.
24723 virt-specifier-seq:
24724 virt-specifier virt-specifier-seq [opt]
24726 virt-specifier:
24727 override
24728 final
24730 Returns a bitmask representing the virt-specifiers. */
24732 static cp_virt_specifiers
24733 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24735 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24737 while (true)
24739 cp_token *token;
24740 cp_virt_specifiers virt_specifier;
24742 /* Peek at the next token. */
24743 token = cp_lexer_peek_token (parser->lexer);
24744 /* See if it's a virt-specifier-qualifier. */
24745 if (token->type != CPP_NAME)
24746 break;
24747 if (id_equal (token->u.value, "override"))
24749 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24750 virt_specifier = VIRT_SPEC_OVERRIDE;
24752 else if (id_equal (token->u.value, "final"))
24754 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24755 virt_specifier = VIRT_SPEC_FINAL;
24757 else if (id_equal (token->u.value, "__final"))
24759 virt_specifier = VIRT_SPEC_FINAL;
24761 else
24762 break;
24764 if (virt_specifiers & virt_specifier)
24766 gcc_rich_location richloc (token->location);
24767 richloc.add_fixit_remove ();
24768 error_at (&richloc, "duplicate virt-specifier");
24769 cp_lexer_purge_token (parser->lexer);
24771 else
24773 cp_lexer_consume_token (parser->lexer);
24774 virt_specifiers |= virt_specifier;
24777 return virt_specifiers;
24780 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24781 is in scope even though it isn't real. */
24783 void
24784 inject_this_parameter (tree ctype, cp_cv_quals quals)
24786 tree this_parm;
24788 if (current_class_ptr)
24790 /* We don't clear this between NSDMIs. Is it already what we want? */
24791 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24792 if (DECL_P (current_class_ptr)
24793 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24794 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24795 && cp_type_quals (type) == quals)
24796 return;
24799 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24800 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24801 current_class_ptr = NULL_TREE;
24802 current_class_ref
24803 = cp_build_fold_indirect_ref (this_parm);
24804 current_class_ptr = this_parm;
24807 /* Return true iff our current scope is a non-static data member
24808 initializer. */
24810 bool
24811 parsing_nsdmi (void)
24813 /* We recognize NSDMI context by the context-less 'this' pointer set up
24814 by the function above. */
24815 if (current_class_ptr
24816 && TREE_CODE (current_class_ptr) == PARM_DECL
24817 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24818 return true;
24819 return false;
24822 /* True if we're parsing a function declarator. */
24824 bool
24825 parsing_function_declarator ()
24827 /* this_entity is NULL for a function parameter scope while parsing the
24828 declarator; it is set when parsing the body of the function. */
24829 return (current_binding_level->kind == sk_function_parms
24830 && !current_binding_level->this_entity);
24833 /* Parse a late-specified return type, if any. This is not a separate
24834 non-terminal, but part of a function declarator, which looks like
24836 -> trailing-type-specifier-seq abstract-declarator(opt)
24838 Returns the type indicated by the type-id.
24840 In addition to this, parse any queued up #pragma omp declare simd
24841 clauses, and #pragma acc routine clauses.
24843 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24844 function. */
24846 static tree
24847 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24848 tree& requires_clause)
24850 cp_token *token;
24851 tree type = NULL_TREE;
24852 bool declare_simd_p = (parser->omp_declare_simd
24853 && declarator
24854 && declarator->kind == cdk_id);
24856 bool oacc_routine_p = (parser->oacc_routine
24857 && declarator
24858 && declarator->kind == cdk_id);
24860 /* Peek at the next token. */
24861 token = cp_lexer_peek_token (parser->lexer);
24862 /* A late-specified return type is indicated by an initial '->'. */
24863 if (token->type != CPP_DEREF
24864 && token->keyword != RID_REQUIRES
24865 && !(token->type == CPP_NAME
24866 && token->u.value == ridpointers[RID_REQUIRES])
24867 && !(declare_simd_p || oacc_routine_p))
24868 return NULL_TREE;
24870 if (token->type == CPP_DEREF)
24872 /* Consume the ->. */
24873 cp_lexer_consume_token (parser->lexer);
24875 type = cp_parser_trailing_type_id (parser);
24878 /* Function declarations may be followed by a trailing
24879 requires-clause. */
24880 requires_clause = cp_parser_requires_clause_opt (parser, false);
24882 if (declare_simd_p)
24883 declarator->attributes
24884 = cp_parser_late_parsing_omp_declare_simd (parser,
24885 declarator->attributes);
24886 if (oacc_routine_p)
24887 declarator->attributes
24888 = cp_parser_late_parsing_oacc_routine (parser,
24889 declarator->attributes);
24891 return type;
24894 /* Parse a declarator-id.
24896 declarator-id:
24897 id-expression
24898 :: [opt] nested-name-specifier [opt] type-name
24900 In the `id-expression' case, the value returned is as for
24901 cp_parser_id_expression if the id-expression was an unqualified-id.
24902 If the id-expression was a qualified-id, then a SCOPE_REF is
24903 returned. The first operand is the scope (either a NAMESPACE_DECL
24904 or TREE_TYPE), but the second is still just a representation of an
24905 unqualified-id. */
24907 static tree
24908 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24910 tree id;
24911 /* The expression must be an id-expression. Assume that qualified
24912 names are the names of types so that:
24914 template <class T>
24915 int S<T>::R::i = 3;
24917 will work; we must treat `S<T>::R' as the name of a type.
24918 Similarly, assume that qualified names are templates, where
24919 required, so that:
24921 template <class T>
24922 int S<T>::R<T>::i = 3;
24924 will work, too. */
24925 id = cp_parser_id_expression (parser,
24926 /*template_keyword_p=*/false,
24927 /*check_dependency_p=*/false,
24928 /*template_p=*/NULL,
24929 /*declarator_p=*/true,
24930 optional_p);
24931 if (id && BASELINK_P (id))
24932 id = BASELINK_FUNCTIONS (id);
24933 return id;
24936 /* Parse a type-id.
24938 type-id:
24939 type-specifier-seq abstract-declarator [opt]
24941 The parser flags FLAGS is used to control type-specifier parsing.
24943 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24945 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24946 i.e. we've just seen "->".
24948 Returns the TYPE specified. */
24950 static tree
24951 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24952 bool is_template_arg, bool is_trailing_return,
24953 location_t *type_location)
24955 cp_decl_specifier_seq type_specifier_seq;
24956 cp_declarator *abstract_declarator;
24958 /* Parse the type-specifier-seq. */
24959 cp_parser_type_specifier_seq (parser, flags,
24960 /*is_declaration=*/false,
24961 is_trailing_return,
24962 &type_specifier_seq);
24963 if (type_location)
24964 *type_location = type_specifier_seq.locations[ds_type_spec];
24966 if (is_template_arg && type_specifier_seq.type
24967 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24968 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24969 /* A bare template name as a template argument is a template template
24970 argument, not a placeholder, so fail parsing it as a type argument. */
24972 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24973 cp_parser_simulate_error (parser);
24974 return error_mark_node;
24976 if (type_specifier_seq.type == error_mark_node)
24977 return error_mark_node;
24979 /* There might or might not be an abstract declarator. */
24980 cp_parser_parse_tentatively (parser);
24981 /* Look for the declarator. */
24982 abstract_declarator
24983 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24984 CP_PARSER_FLAGS_NONE, NULL,
24985 /*parenthesized_p=*/NULL,
24986 /*member_p=*/false,
24987 /*friend_p=*/false,
24988 /*static_p=*/false);
24989 /* Check to see if there really was a declarator. */
24990 if (!cp_parser_parse_definitely (parser))
24991 abstract_declarator = NULL;
24993 bool auto_typeid_ok = false;
24994 /* The concepts TS allows 'auto' as a type-id. */
24995 if (flag_concepts_ts)
24996 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24997 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24998 outside the template-argument-list context here only for the sake of
24999 diagnostic: grokdeclarator then can emit a better error message for
25000 e.g. using T = auto. */
25001 else if (flag_concepts)
25002 auto_typeid_ok = (!parser->in_type_id_in_expr_p
25003 && !parser->in_template_argument_list_p);
25005 if (type_specifier_seq.type
25006 && !auto_typeid_ok
25007 /* None of the valid uses of 'auto' in C++14 involve the type-id
25008 nonterminal, but it is valid in a trailing-return-type. */
25009 && !(cxx_dialect >= cxx14 && is_trailing_return))
25010 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
25012 /* A type-id with type 'auto' is only ok if the abstract declarator
25013 is a function declarator with a late-specified return type.
25015 A type-id with 'auto' is also valid in a trailing-return-type
25016 in a compound-requirement. */
25017 if (abstract_declarator
25018 && abstract_declarator->kind == cdk_function
25019 && abstract_declarator->u.function.late_return_type)
25020 /* OK */;
25021 else if (parser->in_result_type_constraint_p)
25022 /* OK */;
25023 else
25025 if (!cp_parser_simulate_error (parser))
25027 location_t loc = type_specifier_seq.locations[ds_type_spec];
25028 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25030 auto_diagnostic_group g;
25031 gcc_rich_location richloc (loc);
25032 richloc.add_fixit_insert_after ("<>");
25033 error_at (&richloc, "missing template arguments after %qE",
25034 tmpl);
25035 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
25036 tmpl);
25038 else if (parser->in_template_argument_list_p)
25039 error_at (loc, "%qT not permitted in template argument",
25040 auto_node);
25041 else
25042 error_at (loc, "invalid use of %qT", auto_node);
25044 return error_mark_node;
25048 return groktypename (&type_specifier_seq, abstract_declarator,
25049 is_template_arg);
25052 /* Wrapper for cp_parser_type_id_1. */
25054 static tree
25055 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
25056 location_t *type_location)
25058 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
25061 /* Wrapper for cp_parser_type_id_1. */
25063 static tree
25064 cp_parser_template_type_arg (cp_parser *parser)
25066 tree r;
25067 const char *saved_message = parser->type_definition_forbidden_message;
25068 parser->type_definition_forbidden_message
25069 = G_("types may not be defined in template arguments");
25070 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
25071 parser->type_definition_forbidden_message = saved_message;
25072 return r;
25075 /* Wrapper for cp_parser_type_id_1. */
25077 static tree
25078 cp_parser_trailing_type_id (cp_parser *parser)
25080 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
25081 false, true, NULL);
25084 /* Parse a type-specifier-seq.
25086 type-specifier-seq:
25087 type-specifier type-specifier-seq [opt]
25089 GNU extension:
25091 type-specifier-seq:
25092 attributes type-specifier-seq [opt]
25094 The parser flags FLAGS is used to control type-specifier parsing.
25096 If IS_DECLARATION is true, we are at the start of a "condition" or
25097 exception-declaration, so we might be followed by a declarator-id.
25099 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25100 i.e. we've just seen "->".
25102 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
25104 static void
25105 cp_parser_type_specifier_seq (cp_parser* parser,
25106 cp_parser_flags flags,
25107 bool is_declaration,
25108 bool is_trailing_return,
25109 cp_decl_specifier_seq *type_specifier_seq)
25111 bool seen_type_specifier = false;
25112 cp_token *start_token = NULL;
25114 /* Clear the TYPE_SPECIFIER_SEQ. */
25115 clear_decl_specs (type_specifier_seq);
25117 flags |= CP_PARSER_FLAGS_OPTIONAL;
25118 /* In the context of a trailing return type, enum E { } is an
25119 elaborated-type-specifier followed by a function-body, not an
25120 enum-specifier. */
25121 if (is_trailing_return)
25122 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
25124 /* Parse the type-specifiers and attributes. */
25125 while (true)
25127 tree type_specifier;
25128 bool is_cv_qualifier;
25130 /* Check for attributes first. */
25131 if (cp_next_tokens_can_be_attribute_p (parser))
25133 /* GNU attributes at the end of a declaration apply to the
25134 declaration as a whole, not to the trailing return type. So look
25135 ahead to see if these attributes are at the end. */
25136 if (seen_type_specifier && is_trailing_return
25137 && cp_next_tokens_can_be_gnu_attribute_p (parser))
25139 size_t n = cp_parser_skip_attributes_opt (parser, 1);
25140 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
25141 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
25142 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
25143 break;
25145 type_specifier_seq->attributes
25146 = attr_chainon (type_specifier_seq->attributes,
25147 cp_parser_attributes_opt (parser));
25148 continue;
25151 /* record the token of the beginning of the type specifier seq,
25152 for error reporting purposes*/
25153 if (!start_token)
25154 start_token = cp_lexer_peek_token (parser->lexer);
25156 /* Look for the type-specifier. */
25157 type_specifier = cp_parser_type_specifier (parser,
25158 flags,
25159 type_specifier_seq,
25160 /*is_declaration=*/false,
25161 NULL,
25162 &is_cv_qualifier);
25163 if (!type_specifier)
25165 /* If the first type-specifier could not be found, this is not a
25166 type-specifier-seq at all. */
25167 if (!seen_type_specifier)
25169 /* Set in_declarator_p to avoid skipping to the semicolon. */
25170 int in_decl = parser->in_declarator_p;
25171 parser->in_declarator_p = true;
25173 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
25174 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
25175 cp_parser_error (parser, "expected type-specifier");
25177 parser->in_declarator_p = in_decl;
25179 type_specifier_seq->type = error_mark_node;
25180 return;
25182 /* If subsequent type-specifiers could not be found, the
25183 type-specifier-seq is complete. */
25184 break;
25187 seen_type_specifier = true;
25188 /* The standard says that a condition can be:
25190 type-specifier-seq declarator = assignment-expression
25192 However, given:
25194 struct S {};
25195 if (int S = ...)
25197 we should treat the "S" as a declarator, not as a
25198 type-specifier. The standard doesn't say that explicitly for
25199 type-specifier-seq, but it does say that for
25200 decl-specifier-seq in an ordinary declaration. Perhaps it
25201 would be clearer just to allow a decl-specifier-seq here, and
25202 then add a semantic restriction that if any decl-specifiers
25203 that are not type-specifiers appear, the program is invalid. */
25204 if (is_declaration && !is_cv_qualifier)
25205 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
25209 /* Return whether the function currently being declared has an associated
25210 template parameter list. */
25212 static bool
25213 function_being_declared_is_template_p (cp_parser* parser)
25215 if (!current_template_parms || processing_template_parmlist)
25216 return false;
25218 if (parser->implicit_template_scope)
25219 return true;
25221 if (at_class_scope_p ()
25222 && TYPE_BEING_DEFINED (current_class_type))
25223 return parser->num_template_parameter_lists != 0;
25225 return ((int) parser->num_template_parameter_lists > template_class_depth
25226 (current_class_type));
25229 /* Parse a parameter-declaration-clause.
25231 parameter-declaration-clause:
25232 parameter-declaration-list [opt] ... [opt]
25233 parameter-declaration-list , ...
25235 The parser flags FLAGS is used to control type-specifier parsing.
25237 Returns a representation for the parameter declarations. A return
25238 value of NULL indicates a parameter-declaration-clause consisting
25239 only of an ellipsis. */
25241 static tree
25242 cp_parser_parameter_declaration_clause (cp_parser* parser,
25243 cp_parser_flags flags)
25245 tree parameters;
25246 cp_token *token;
25247 bool ellipsis_p;
25249 auto cleanup = make_temp_override
25250 (parser->auto_is_implicit_function_template_parm_p);
25252 if (!processing_specialization
25253 && !processing_template_parmlist
25254 && !processing_explicit_instantiation
25255 /* default_arg_ok_p tracks whether this is a parameter-clause for an
25256 actual function or a random abstract declarator. */
25257 && parser->default_arg_ok_p)
25258 if (!current_function_decl
25259 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
25260 parser->auto_is_implicit_function_template_parm_p = true;
25262 /* Peek at the next token. */
25263 token = cp_lexer_peek_token (parser->lexer);
25264 /* Check for trivial parameter-declaration-clauses. */
25265 if (token->type == CPP_ELLIPSIS)
25267 /* Consume the `...' token. */
25268 cp_lexer_consume_token (parser->lexer);
25269 return NULL_TREE;
25271 else if (token->type == CPP_CLOSE_PAREN)
25272 /* There are no parameters. */
25273 return void_list_node;
25274 /* Check for `(void)', too, which is a special case. */
25275 else if (token->keyword == RID_VOID
25276 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25277 == CPP_CLOSE_PAREN))
25279 /* Consume the `void' token. */
25280 cp_lexer_consume_token (parser->lexer);
25281 /* There are no parameters. */
25282 return explicit_void_list_node;
25285 /* A vector of parameters that haven't been pushed yet. */
25286 auto_vec<tree> pending_decls;
25288 /* Parse the parameter-declaration-list. */
25289 parameters = cp_parser_parameter_declaration_list (parser, flags,
25290 &pending_decls);
25291 /* If a parse error occurred while parsing the
25292 parameter-declaration-list, then the entire
25293 parameter-declaration-clause is erroneous. */
25294 if (parameters == error_mark_node)
25295 return NULL_TREE;
25297 /* Peek at the next token. */
25298 token = cp_lexer_peek_token (parser->lexer);
25299 /* If it's a `,', the clause should terminate with an ellipsis. */
25300 if (token->type == CPP_COMMA)
25302 /* Consume the `,'. */
25303 cp_lexer_consume_token (parser->lexer);
25304 /* Expect an ellipsis. */
25305 ellipsis_p
25306 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
25308 /* It might also be `...' if the optional trailing `,' was
25309 omitted. */
25310 else if (token->type == CPP_ELLIPSIS)
25312 /* Consume the `...' token. */
25313 cp_lexer_consume_token (parser->lexer);
25314 /* And remember that we saw it. */
25315 ellipsis_p = true;
25317 else
25318 ellipsis_p = false;
25320 /* A valid parameter-declaration-clause can only be followed by a ')'.
25321 So it's time to push all the parameters we have seen now that we
25322 know we have a valid declaration. Note that here we may not have
25323 committed yet, nor should we. Pushing here will detect the error
25324 of redefining a parameter. */
25325 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25327 for (tree p : pending_decls)
25328 pushdecl (p);
25330 /* Delayed checking of auto parameters. */
25331 if (!parser->auto_is_implicit_function_template_parm_p
25332 && cxx_dialect >= cxx14)
25333 for (tree p = parameters; p; p = TREE_CHAIN (p))
25334 if (type_uses_auto (TREE_TYPE (TREE_VALUE (p))))
25336 error_at (location_of (TREE_VALUE (p)),
25337 "%<auto%> parameter not permitted in this context");
25338 TREE_TYPE (TREE_VALUE (p)) = error_mark_node;
25342 /* Finish the parameter list. */
25343 if (!ellipsis_p)
25344 parameters = chainon (parameters, void_list_node);
25346 return parameters;
25349 /* Parse a parameter-declaration-list.
25351 parameter-declaration-list:
25352 parameter-declaration
25353 parameter-declaration-list , parameter-declaration
25355 The parser flags FLAGS is used to control type-specifier parsing.
25356 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25358 Returns a representation of the parameter-declaration-list, as for
25359 cp_parser_parameter_declaration_clause. However, the
25360 `void_list_node' is never appended to the list. */
25362 static tree
25363 cp_parser_parameter_declaration_list (cp_parser* parser,
25364 cp_parser_flags flags,
25365 auto_vec<tree> *pending_decls)
25367 tree parameters = NULL_TREE;
25368 tree *tail = &parameters;
25369 bool saved_in_unbraced_linkage_specification_p;
25370 int index = 0;
25372 /* The special considerations that apply to a function within an
25373 unbraced linkage specifications do not apply to the parameters
25374 to the function. */
25375 saved_in_unbraced_linkage_specification_p
25376 = parser->in_unbraced_linkage_specification_p;
25377 parser->in_unbraced_linkage_specification_p = false;
25379 /* Look for more parameters. */
25380 while (true)
25382 cp_parameter_declarator *parameter;
25383 tree decl = error_mark_node;
25384 bool parenthesized_p = false;
25386 /* Parse the parameter. */
25387 parameter
25388 = cp_parser_parameter_declaration (parser, flags,
25389 /*template_parm_p=*/false,
25390 &parenthesized_p);
25392 /* We don't know yet if the enclosing context is unavailable or deprecated,
25393 so wait and deal with it in grokparms if appropriate. */
25394 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25396 if (parameter && !cp_parser_error_occurred (parser))
25398 decl = grokdeclarator (parameter->declarator,
25399 &parameter->decl_specifiers,
25400 PARM,
25401 parameter->default_argument != NULL_TREE,
25402 &parameter->decl_specifiers.attributes);
25403 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25404 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25407 deprecated_state = DEPRECATED_NORMAL;
25409 /* If a parse error occurred parsing the parameter declaration,
25410 then the entire parameter-declaration-list is erroneous. */
25411 if (decl == error_mark_node)
25413 parameters = error_mark_node;
25414 break;
25417 if (parameter->decl_specifiers.attributes)
25418 cplus_decl_attributes (&decl,
25419 parameter->decl_specifiers.attributes,
25421 if (DECL_NAME (decl))
25423 /* We cannot always pushdecl while parsing tentatively because
25424 it may have side effects and we can't be sure yet if we're
25425 parsing a declaration, e.g.:
25427 S foo(int(x), int(x), int{x});
25429 where it's not clear if we're dealing with a constructor call
25430 or a function declaration until we've seen the last argument
25431 which breaks it up.
25432 It's safe to pushdecl so long as it doesn't result in a clash
25433 with an already-pushed parameter. But we don't delay pushing
25434 different parameters to handle
25436 S foo(int(i), decltype(i) j = 42);
25438 which is valid. */
25439 if (pending_decls
25440 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25441 /* See if PARAMETERS already contains a parameter with the same
25442 DECL_NAME as DECL. */
25443 && [parameters, decl] {
25444 for (tree p = parameters; p; p = TREE_CHAIN (p))
25445 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25446 return true;
25447 return false;
25448 }())
25449 pending_decls->safe_push (decl);
25450 else
25451 decl = pushdecl (decl);
25454 if (decl != error_mark_node)
25456 retrofit_lang_decl (decl);
25457 DECL_PARM_INDEX (decl) = ++index;
25458 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25461 /* Add the new parameter to the list. */
25462 *tail = build_tree_list (parameter->default_argument, decl);
25463 tail = &TREE_CHAIN (*tail);
25465 /* If the parameters were parenthesized, it's the case of
25466 T foo(X(x)) which looks like a variable definition but
25467 is a function declaration. */
25468 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25469 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25471 /* Peek at the next token. */
25472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25473 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25474 /* These are for Objective-C++ */
25475 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25476 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25477 /* The parameter-declaration-list is complete. */
25478 break;
25479 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25481 cp_token *token;
25483 /* Peek at the next token. */
25484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25485 /* If it's an ellipsis, then the list is complete. */
25486 if (token->type == CPP_ELLIPSIS)
25487 break;
25488 /* Otherwise, there must be more parameters. Consume the
25489 `,'. */
25490 cp_lexer_consume_token (parser->lexer);
25491 /* When parsing something like:
25493 int i(float f, double d)
25495 we can tell after seeing the declaration for "f" that we
25496 are not looking at an initialization of a variable "i",
25497 but rather at the declaration of a function "i".
25499 Due to the fact that the parsing of template arguments
25500 (as specified to a template-id) requires backtracking we
25501 cannot use this technique when inside a template argument
25502 list. */
25503 if (!parser->in_template_argument_list_p
25504 && !parser->in_type_id_in_expr_p
25505 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25506 /* However, a parameter-declaration of the form
25507 "float(f)" (which is a valid declaration of a
25508 parameter "f") can also be interpreted as an
25509 expression (the conversion of "f" to "float"). */
25510 && !parenthesized_p)
25511 cp_parser_commit_to_tentative_parse (parser);
25513 else
25515 cp_parser_error (parser, "expected %<,%> or %<...%>");
25516 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25517 cp_parser_skip_to_closing_parenthesis (parser,
25518 /*recovering=*/true,
25519 /*or_comma=*/false,
25520 /*consume_paren=*/false);
25521 break;
25525 parser->in_unbraced_linkage_specification_p
25526 = saved_in_unbraced_linkage_specification_p;
25528 /* Reset implicit_template_scope if we are about to leave the function
25529 parameter list that introduced it. Note that for out-of-line member
25530 definitions, there will be one or more class scopes before we get to
25531 the template parameter scope. */
25533 if (cp_binding_level *its = parser->implicit_template_scope)
25534 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25536 while (maybe_its->kind == sk_class)
25537 maybe_its = maybe_its->level_chain;
25538 if (maybe_its == its)
25540 parser->implicit_template_parms = 0;
25541 parser->implicit_template_scope = 0;
25545 return parameters;
25548 /* Parse a parameter declaration.
25550 parameter-declaration:
25551 decl-specifier-seq ... [opt] declarator
25552 decl-specifier-seq declarator = assignment-expression
25553 decl-specifier-seq ... [opt] abstract-declarator [opt]
25554 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25556 The parser flags FLAGS is used to control type-specifier parsing.
25558 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25559 declares a template parameter. (In that case, a non-nested `>'
25560 token encountered during the parsing of the assignment-expression
25561 is not interpreted as a greater-than operator.)
25563 Returns a representation of the parameter, or NULL if an error
25564 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25565 true iff the declarator is of the form "(p)". */
25567 static cp_parameter_declarator *
25568 cp_parser_parameter_declaration (cp_parser *parser,
25569 cp_parser_flags flags,
25570 bool template_parm_p,
25571 bool *parenthesized_p)
25573 int declares_class_or_enum;
25574 cp_decl_specifier_seq decl_specifiers;
25575 cp_declarator *declarator;
25576 tree default_argument;
25577 cp_token *token = NULL, *declarator_token_start = NULL;
25578 const char *saved_message;
25579 bool template_parameter_pack_p = false;
25581 /* In a template parameter, `>' is not an operator.
25583 [temp.param]
25585 When parsing a default template-argument for a non-type
25586 template-parameter, the first non-nested `>' is taken as the end
25587 of the template parameter-list rather than a greater-than
25588 operator. */
25590 /* Type definitions may not appear in parameter types. */
25591 saved_message = parser->type_definition_forbidden_message;
25592 parser->type_definition_forbidden_message
25593 = G_("types may not be defined in parameter types");
25595 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25596 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25597 (current_template_parms)) : 0);
25599 /* Parse the declaration-specifiers. */
25600 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25601 cp_parser_decl_specifier_seq (parser,
25602 flags,
25603 &decl_specifiers,
25604 &declares_class_or_enum);
25606 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25607 type-constraint opt auto can be used as a decl-specifier of the
25608 decl-specifier-seq of a parameter-declaration of a function declaration
25609 or lambda-expression..." but we must not synthesize an implicit template
25610 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25611 we want to synthesize only the first auto. */
25612 auto cleanup = make_temp_override
25613 (parser->auto_is_implicit_function_template_parm_p, false);
25615 /* Complain about missing 'typename' or other invalid type names. */
25616 if (!decl_specifiers.any_type_specifiers_p
25617 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25618 decl_specifiers.type = error_mark_node;
25620 /* If an error occurred, there's no reason to attempt to parse the
25621 rest of the declaration. */
25622 if (cp_parser_error_occurred (parser))
25624 parser->type_definition_forbidden_message = saved_message;
25625 return NULL;
25628 /* Peek at the next token. */
25629 token = cp_lexer_peek_token (parser->lexer);
25631 /* If the next token is a `)', `,', `=', `>', or `...', then there
25632 is no declarator. However, when variadic templates are enabled,
25633 there may be a declarator following `...'. */
25634 if (token->type == CPP_CLOSE_PAREN
25635 || token->type == CPP_COMMA
25636 || token->type == CPP_EQ
25637 || token->type == CPP_GREATER)
25639 declarator = NULL;
25640 if (parenthesized_p)
25641 *parenthesized_p = false;
25643 /* Otherwise, there should be a declarator. */
25644 else
25646 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25647 parser->default_arg_ok_p = false;
25649 /* After seeing a decl-specifier-seq, if the next token is not a
25650 "(" or "{", there is no possibility that the code is a valid
25651 expression. Therefore, if parsing tentatively, we commit at
25652 this point. */
25653 if (!parser->in_template_argument_list_p
25654 /* In an expression context, having seen:
25656 (int((char ...
25658 we cannot be sure whether we are looking at a
25659 function-type (taking a "char" as a parameter) or a cast
25660 of some object of type "char" to "int". */
25661 && !parser->in_type_id_in_expr_p
25662 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25663 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25667 if (decl_specifiers.type
25668 && template_placeholder_p (decl_specifiers.type))
25669 /* This is a CTAD expression, not a parameter declaration. */
25670 cp_parser_simulate_error (parser);
25672 else
25673 cp_parser_commit_to_tentative_parse (parser);
25675 /* Parse the declarator. */
25676 declarator_token_start = token;
25677 declarator = cp_parser_declarator (parser,
25678 CP_PARSER_DECLARATOR_EITHER,
25679 CP_PARSER_FLAGS_NONE,
25680 /*ctor_dtor_or_conv_p=*/NULL,
25681 parenthesized_p,
25682 /*member_p=*/false,
25683 /*friend_p=*/false,
25684 /*static_p=*/false);
25685 parser->default_arg_ok_p = saved_default_arg_ok_p;
25686 /* After the declarator, allow more attributes. */
25687 decl_specifiers.attributes
25688 = attr_chainon (decl_specifiers.attributes,
25689 cp_parser_attributes_opt (parser));
25691 /* If the declarator is a template parameter pack, remember that and
25692 clear the flag in the declarator itself so we don't get errors
25693 from grokdeclarator. */
25694 if (template_parm_p && declarator && declarator->parameter_pack_p)
25696 declarator->parameter_pack_p = false;
25697 template_parameter_pack_p = true;
25701 /* If the next token is an ellipsis, and we have not seen a declarator
25702 name, and if either the type of the declarator contains parameter
25703 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25704 for, eg, abbreviated integral type names), then we actually have a
25705 parameter pack expansion expression. Otherwise, leave the ellipsis
25706 for a C-style variadic function. */
25707 token = cp_lexer_peek_token (parser->lexer);
25709 /* If a function parameter pack was specified and an implicit template
25710 parameter was introduced during cp_parser_parameter_declaration,
25711 change any implicit parameters introduced into packs. */
25712 if (parser->implicit_template_parms
25713 && ((token->type == CPP_ELLIPSIS
25714 && declarator_can_be_parameter_pack (declarator))
25715 || (declarator && declarator->parameter_pack_p)))
25717 int latest_template_parm_idx = TREE_VEC_LENGTH
25718 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25720 if (latest_template_parm_idx != template_parm_idx)
25721 decl_specifiers.type = convert_generic_types_to_packs
25722 (decl_specifiers.type,
25723 template_parm_idx, latest_template_parm_idx);
25726 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25728 tree type = decl_specifiers.type;
25730 if (type && DECL_P (type))
25731 type = TREE_TYPE (type);
25733 if (((type
25734 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25735 && (template_parm_p || uses_parameter_packs (type)))
25736 || (!type && template_parm_p))
25737 && declarator_can_be_parameter_pack (declarator))
25739 /* Consume the `...'. */
25740 cp_lexer_consume_token (parser->lexer);
25741 maybe_warn_variadic_templates ();
25743 /* Build a pack expansion type */
25744 if (template_parm_p)
25745 template_parameter_pack_p = true;
25746 else if (declarator)
25747 declarator->parameter_pack_p = true;
25748 else
25749 decl_specifiers.type = make_pack_expansion (type);
25753 /* The restriction on defining new types applies only to the type
25754 of the parameter, not to the default argument. */
25755 parser->type_definition_forbidden_message = saved_message;
25756 cp_token *eq_token = NULL;
25757 /* If the next token is `=', then process a default argument. */
25758 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25760 tree type = decl_specifiers.type;
25761 token = cp_lexer_peek_token (parser->lexer);
25762 /* Used for diagnostics with an xobj parameter. */
25763 eq_token = token;
25764 if (declarator)
25765 declarator->init_loc = token->location;
25766 /* If we are defining a class, then the tokens that make up the
25767 default argument must be saved and processed later. */
25768 if (!template_parm_p && at_class_scope_p ()
25769 && TYPE_BEING_DEFINED (current_class_type)
25770 && !LAMBDA_TYPE_P (current_class_type))
25771 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25773 /* A constrained-type-specifier may declare a type
25774 template-parameter. */
25775 else if (declares_constrained_type_template_parameter (type))
25776 default_argument
25777 = cp_parser_default_type_template_argument (parser);
25779 /* A constrained-type-specifier may declare a
25780 template-template-parameter. */
25781 else if (declares_constrained_template_template_parameter (type))
25782 default_argument
25783 = cp_parser_default_template_template_argument (parser);
25785 /* Outside of a class definition, we can just parse the
25786 assignment-expression. */
25787 else
25788 default_argument
25789 = cp_parser_default_argument (parser, template_parm_p);
25791 if (!parser->default_arg_ok_p)
25793 permerror (token->location,
25794 "default arguments are only "
25795 "permitted for function parameters");
25797 else if ((declarator && declarator->parameter_pack_p)
25798 || template_parameter_pack_p
25799 || (decl_specifiers.type
25800 && PACK_EXPANSION_P (decl_specifiers.type)))
25802 /* Find the name of the parameter pack. */
25803 cp_declarator *id_declarator = declarator;
25804 while (id_declarator && id_declarator->kind != cdk_id)
25805 id_declarator = id_declarator->declarator;
25807 if (id_declarator && id_declarator->kind == cdk_id)
25808 error_at (declarator_token_start->location,
25809 template_parm_p
25810 ? G_("template parameter pack %qD "
25811 "cannot have a default argument")
25812 : G_("parameter pack %qD cannot have "
25813 "a default argument"),
25814 id_declarator->u.id.unqualified_name);
25815 else
25816 error_at (declarator_token_start->location,
25817 template_parm_p
25818 ? G_("template parameter pack cannot have "
25819 "a default argument")
25820 : G_("parameter pack cannot have a "
25821 "default argument"));
25823 default_argument = NULL_TREE;
25826 else
25827 default_argument = NULL_TREE;
25829 if (default_argument)
25830 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25832 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_this))
25834 if (default_argument)
25836 /* If there is a default_argument, eq_token should always be set. */
25837 gcc_assert (eq_token);
25838 location_t param_with_init_loc
25839 = make_location (eq_token->location,
25840 decl_spec_token_start->location,
25841 input_location);
25842 error_at (param_with_init_loc,
25843 "an explicit object parameter "
25844 "may not have a default argument");
25846 /* Xobj parameters can not have default arguments, thus
25847 we can reuse the default argument field to flag the param as such. */
25848 default_argument = this_identifier;
25851 /* Generate a location for the parameter, ranging from the start of the
25852 initial token to the end of the final token (using input_location for
25853 the latter, set up by cp_lexer_set_source_position_from_token when
25854 consuming tokens).
25856 If we have a identifier, then use it for the caret location, e.g.
25858 extern int callee (int one, int (*two)(int, int), float three);
25859 ~~~~~~^~~~~~~~~~~~~~
25861 otherwise, reuse the start location for the caret location e.g.:
25863 extern int callee (int one, int (*)(int, int), float three);
25864 ^~~~~~~~~~~~~~~~~
25867 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25868 ? declarator->id_loc
25869 : decl_spec_token_start->location);
25870 location_t param_loc = make_location (caret_loc,
25871 decl_spec_token_start->location,
25872 input_location);
25874 return make_parameter_declarator (&decl_specifiers,
25875 declarator,
25876 default_argument,
25877 param_loc,
25878 template_parameter_pack_p);
25881 /* Parse a default argument and return it.
25883 TEMPLATE_PARM_P is true if this is a default argument for a
25884 non-type template parameter. */
25885 static tree
25886 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25888 tree default_argument = NULL_TREE;
25889 bool saved_greater_than_is_operator_p;
25890 unsigned char saved_local_variables_forbidden_p;
25892 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25893 set correctly. */
25894 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25895 parser->greater_than_is_operator_p = !template_parm_p;
25896 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25897 auto ord = make_temp_override (parser->oacc_routine, NULL);
25898 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25900 /* Local variable names (and the `this' keyword) may not
25901 appear in a default argument. */
25902 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25903 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25904 /* Parse the assignment-expression. */
25905 if (template_parm_p)
25906 push_deferring_access_checks (dk_no_deferred);
25907 tree saved_class_ptr = NULL_TREE;
25908 tree saved_class_ref = NULL_TREE;
25909 /* The "this" pointer is not valid in a default argument. */
25910 if (cfun)
25912 saved_class_ptr = current_class_ptr;
25913 cp_function_chain->x_current_class_ptr = NULL_TREE;
25914 saved_class_ref = current_class_ref;
25915 cp_function_chain->x_current_class_ref = NULL_TREE;
25917 default_argument = cp_parser_initializer (parser);
25918 /* Restore the "this" pointer. */
25919 if (cfun)
25921 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25922 cp_function_chain->x_current_class_ref = saved_class_ref;
25924 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25925 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25926 if (template_parm_p)
25927 pop_deferring_access_checks ();
25928 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25929 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25931 return default_argument;
25934 /* Parse a function-body.
25936 function-body:
25937 compound_statement */
25939 static void
25940 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25942 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25943 ? BCS_TRY_BLOCK : BCS_NORMAL),
25944 true);
25947 /* Parse a ctor-initializer-opt followed by a function-body. Return
25948 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25949 is true we are parsing a function-try-block. */
25951 static void
25952 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25953 bool in_function_try_block)
25955 tree body, list;
25956 const bool check_body_p
25957 = (DECL_CONSTRUCTOR_P (current_function_decl)
25958 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25959 tree last = NULL;
25961 if (in_function_try_block
25962 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25963 && cxx_dialect < cxx20)
25965 if (DECL_CONSTRUCTOR_P (current_function_decl))
25966 pedwarn (input_location, OPT_Wc__20_extensions,
25967 "function-try-block body of %<constexpr%> constructor only "
25968 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25969 else
25970 pedwarn (input_location, OPT_Wc__20_extensions,
25971 "function-try-block body of %<constexpr%> function only "
25972 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25975 /* Begin the function body. */
25976 body = begin_function_body ();
25977 /* Parse the optional ctor-initializer. */
25978 cp_parser_ctor_initializer_opt (parser);
25980 /* If we're parsing a constexpr constructor definition, we need
25981 to check that the constructor body is indeed empty. However,
25982 before we get to cp_parser_function_body lot of junk has been
25983 generated, so we can't just check that we have an empty block.
25984 Rather we take a snapshot of the outermost block, and check whether
25985 cp_parser_function_body changed its state. */
25986 if (check_body_p)
25988 list = cur_stmt_list;
25989 if (STATEMENT_LIST_TAIL (list))
25990 last = STATEMENT_LIST_TAIL (list)->stmt;
25992 /* Parse the function-body. */
25993 cp_parser_function_body (parser, in_function_try_block);
25994 if (check_body_p)
25995 check_constexpr_ctor_body (last, list, /*complain=*/true);
25996 /* Finish the function body. */
25997 finish_function_body (body);
26000 /* Parse an initializer.
26002 initializer:
26003 = initializer-clause
26004 ( expression-list )
26006 Returns an expression representing the initializer. If no
26007 initializer is present, NULL_TREE is returned.
26009 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
26010 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
26011 set to TRUE if there is no initializer present. If there is an
26012 initializer, and it is not a constant-expression, *NON_CONSTANT_P
26013 is set to true; otherwise it is set to false. */
26015 static tree
26016 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
26017 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
26019 cp_token *token;
26020 tree init;
26022 /* Peek at the next token. */
26023 token = cp_lexer_peek_token (parser->lexer);
26025 /* Let our caller know whether or not this initializer was
26026 parenthesized. */
26027 if (is_direct_init)
26028 *is_direct_init = (token->type != CPP_EQ);
26029 /* Assume that the initializer is constant. */
26030 if (non_constant_p)
26031 *non_constant_p = false;
26033 if (token->type == CPP_EQ)
26035 /* Consume the `='. */
26036 cp_lexer_consume_token (parser->lexer);
26037 /* Parse the initializer-clause. */
26038 init = cp_parser_initializer_clause (parser, non_constant_p);
26040 else if (token->type == CPP_OPEN_PAREN)
26042 vec<tree, va_gc> *vec;
26043 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26044 /*cast_p=*/false,
26045 /*allow_expansion_p=*/true,
26046 non_constant_p);
26047 if (vec == NULL)
26048 return error_mark_node;
26049 init = build_tree_list_vec (vec);
26050 release_tree_vector (vec);
26052 else if (token->type == CPP_OPEN_BRACE)
26054 cp_lexer_set_source_position (parser->lexer);
26055 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26056 init = cp_parser_braced_list (parser, non_constant_p);
26057 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
26059 else
26061 /* Anything else is an error. */
26062 cp_parser_error (parser, "expected initializer");
26063 init = error_mark_node;
26066 if (!subexpression_p && check_for_bare_parameter_packs (init))
26067 init = error_mark_node;
26069 return init;
26072 /* Parse an initializer-clause.
26074 initializer-clause:
26075 assignment-expression
26076 braced-init-list
26078 Returns an expression representing the initializer.
26080 If the `assignment-expression' production is used the value
26081 returned is simply a representation for the expression.
26083 Otherwise, calls cp_parser_braced_list. */
26085 static cp_expr
26086 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
26088 cp_expr initializer;
26090 /* Assume the expression is constant. */
26091 if (non_constant_p)
26092 *non_constant_p = false;
26094 /* If it is not a `{', then we are looking at an
26095 assignment-expression. */
26096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26098 initializer
26099 = cp_parser_constant_expression (parser,
26100 /*allow_non_constant_p=*/2,
26101 non_constant_p);
26103 else
26104 initializer = cp_parser_braced_list (parser, non_constant_p);
26106 return initializer;
26109 /* Parse a brace-enclosed initializer list.
26111 braced-init-list:
26112 { initializer-list , [opt] }
26113 { designated-initializer-list , [opt] }
26116 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
26117 the elements of the initializer-list (or NULL, if the last
26118 production is used). The TREE_TYPE for the CONSTRUCTOR will be
26119 NULL_TREE. There is no way to detect whether or not the optional
26120 trailing `,' was provided. NON_CONSTANT_P is as for
26121 cp_parser_initializer. */
26123 static cp_expr
26124 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
26126 tree initializer;
26127 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26128 auto oas = make_temp_override (parser->omp_array_section_p, false);
26130 /* Consume the `{' token. */
26131 matching_braces braces;
26132 braces.require_open (parser);
26133 /* Create a CONSTRUCTOR to represent the braced-initializer. */
26134 initializer = make_node (CONSTRUCTOR);
26135 /* If it's not a `}', then there is a non-trivial initializer. */
26136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
26138 bool designated;
26139 /* Parse the initializer list. */
26140 CONSTRUCTOR_ELTS (initializer)
26141 = cp_parser_initializer_list (parser, non_constant_p, &designated);
26142 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
26143 /* A trailing `,' token is allowed. */
26144 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26145 cp_lexer_consume_token (parser->lexer);
26147 else if (non_constant_p)
26148 *non_constant_p = false;
26149 /* Now, there should be a trailing `}'. */
26150 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
26151 braces.require_close (parser);
26152 TREE_TYPE (initializer) = init_list_type_node;
26153 recompute_constructor_flags (initializer);
26155 cp_expr result (initializer);
26156 /* Build a location of the form:
26157 { ... }
26158 ^~~~~~~
26159 with caret==start at the open brace, finish at the close brace. */
26160 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26161 result.set_location (combined_loc);
26162 return result;
26165 /* Consume tokens up to, but not including, the next non-nested closing `]'.
26166 Returns true iff we found a closing `]'. */
26168 static bool
26169 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
26171 unsigned square_depth = 0;
26173 while (true)
26175 cp_token * token = cp_lexer_peek_token (parser->lexer);
26177 switch (token->type)
26179 case CPP_PRAGMA_EOL:
26180 if (!parser->lexer->in_pragma)
26181 break;
26182 /* FALLTHRU */
26184 case CPP_EOF:
26185 /* If we've run out of tokens, then there is no closing `]'. */
26186 return false;
26188 case CPP_OPEN_SQUARE:
26189 ++square_depth;
26190 break;
26192 case CPP_CLOSE_SQUARE:
26193 if (!square_depth--)
26194 return true;
26195 break;
26197 default:
26198 break;
26201 /* Consume the current token, skipping it. */
26202 cp_lexer_consume_token (parser->lexer);
26206 /* Consume tokens up to, and including, the next non-nested closing `]'.
26207 Returns true iff we found a closing `]'. */
26209 static bool
26210 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
26212 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
26213 if (found)
26214 cp_lexer_consume_token (parser->lexer);
26215 return found;
26218 /* Return true if we are looking at an array-designator, false otherwise. */
26220 static bool
26221 cp_parser_array_designator_p (cp_parser *parser)
26223 /* Consume the `['. */
26224 cp_lexer_consume_token (parser->lexer);
26226 cp_lexer_save_tokens (parser->lexer);
26228 /* Skip tokens until the next token is a closing square bracket.
26229 If we find the closing `]', and the next token is a `=', then
26230 we are looking at an array designator. */
26231 bool array_designator_p
26232 = (cp_parser_skip_to_closing_square_bracket (parser)
26233 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
26235 /* Roll back the tokens we skipped. */
26236 cp_lexer_rollback_tokens (parser->lexer);
26238 return array_designator_p;
26241 /* Parse an initializer-list.
26243 initializer-list:
26244 initializer-clause ... [opt]
26245 initializer-list , initializer-clause ... [opt]
26247 C++20 Extension:
26249 designated-initializer-list:
26250 designated-initializer-clause
26251 designated-initializer-list , designated-initializer-clause
26253 designated-initializer-clause:
26254 designator brace-or-equal-initializer
26256 designator:
26257 . identifier
26259 GNU Extension:
26261 initializer-list:
26262 designation initializer-clause ...[opt]
26263 initializer-list , designation initializer-clause ...[opt]
26265 designation:
26266 . identifier =
26267 identifier :
26268 [ constant-expression ] =
26270 Returns a vec of constructor_elt. The VALUE of each elt is an expression
26271 for the initializer. If the INDEX of the elt is non-NULL, it is the
26272 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
26273 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
26274 are any designators. */
26276 static vec<constructor_elt, va_gc> *
26277 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
26278 bool *designated)
26280 vec<constructor_elt, va_gc> *v = NULL;
26281 bool first_p = true;
26282 tree first_designator = NULL_TREE;
26284 /* Assume all of the expressions are constant. */
26285 if (non_constant_p)
26286 *non_constant_p = false;
26288 unsigned nelts = 0;
26289 int suppress = suppress_location_wrappers;
26291 /* Parse the rest of the list. */
26292 while (true)
26294 cp_token *token;
26295 tree designator;
26296 tree initializer;
26297 bool clause_non_constant_p;
26298 bool direct_p = false;
26299 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26301 /* Handle the C++20 syntax, '. id ='. */
26302 if ((cxx_dialect >= cxx20
26303 || cp_parser_allow_gnu_extensions_p (parser))
26304 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
26305 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
26306 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
26307 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
26308 == CPP_OPEN_BRACE)))
26310 if (pedantic && cxx_dialect < cxx20)
26311 pedwarn (loc, OPT_Wc__20_extensions,
26312 "C++ designated initializers only available with "
26313 "%<-std=c++20%> or %<-std=gnu++20%>");
26314 /* Consume the `.'. */
26315 cp_lexer_consume_token (parser->lexer);
26316 /* Consume the identifier. */
26317 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26318 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26319 /* Consume the `='. */
26320 cp_lexer_consume_token (parser->lexer);
26321 else
26322 direct_p = true;
26324 /* Also, if the next token is an identifier and the following one is a
26325 colon, we are looking at the GNU designated-initializer
26326 syntax. */
26327 else if (cp_parser_allow_gnu_extensions_p (parser)
26328 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
26329 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26330 == CPP_COLON))
26332 /* Warn the user that they are using an extension. */
26333 pedwarn (loc, OPT_Wpedantic,
26334 "ISO C++ does not allow GNU designated initializers");
26335 /* Consume the identifier. */
26336 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26337 /* Consume the `:'. */
26338 cp_lexer_consume_token (parser->lexer);
26340 /* Also handle C99 array designators, '[ const ] ='. */
26341 else if (cp_parser_allow_gnu_extensions_p (parser)
26342 && !c_dialect_objc ()
26343 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26345 /* In C++11, [ could start a lambda-introducer. */
26346 bool non_const = false;
26348 cp_parser_parse_tentatively (parser);
26350 if (!cp_parser_array_designator_p (parser))
26352 cp_parser_simulate_error (parser);
26353 designator = NULL_TREE;
26355 else
26357 designator = cp_parser_constant_expression (parser, true,
26358 &non_const);
26359 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26360 cp_parser_require (parser, CPP_EQ, RT_EQ);
26363 if (!cp_parser_parse_definitely (parser))
26364 designator = NULL_TREE;
26365 else if (non_const
26366 && (!require_potential_rvalue_constant_expression
26367 (designator)))
26368 designator = NULL_TREE;
26369 if (designator)
26370 /* Warn the user that they are using an extension. */
26371 pedwarn (loc, OPT_Wpedantic,
26372 "ISO C++ does not allow C99 designated initializers");
26374 else
26375 designator = NULL_TREE;
26377 if (first_p)
26379 first_designator = designator;
26380 first_p = false;
26382 else if (cxx_dialect >= cxx20
26383 && first_designator != error_mark_node
26384 && (!first_designator != !designator))
26386 error_at (loc, "either all initializer clauses should be designated "
26387 "or none of them should be");
26388 first_designator = error_mark_node;
26390 else if (cxx_dialect < cxx20 && !first_designator)
26391 first_designator = designator;
26393 /* Parse the initializer. */
26394 initializer = cp_parser_initializer_clause (parser,
26395 (non_constant_p != nullptr
26396 ? &clause_non_constant_p
26397 : nullptr));
26398 /* If any clause is non-constant, so is the entire initializer. */
26399 if (non_constant_p && clause_non_constant_p)
26400 *non_constant_p = true;
26402 if (TREE_CODE (initializer) == CONSTRUCTOR)
26403 /* This uses |= rather than = because C_I_D_I could have been set in
26404 cp_parser_functional_cast so we must be careful not to clear the
26405 flag. */
26406 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26408 /* If we have an ellipsis, this is an initializer pack
26409 expansion. */
26410 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26414 /* Consume the `...'. */
26415 cp_lexer_consume_token (parser->lexer);
26417 if (designator && cxx_dialect >= cxx20)
26418 error_at (loc,
26419 "%<...%> not allowed in designated initializer list");
26421 /* Turn the initializer into an initializer expansion. */
26422 initializer = make_pack_expansion (initializer);
26425 /* Add it to the vector. */
26426 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26428 /* If the next token is not a comma, we have reached the end of
26429 the list. */
26430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26431 break;
26433 /* Peek at the next token. */
26434 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26435 /* If the next token is a `}', then we're still done. An
26436 initializer-clause can have a trailing `,' after the
26437 initializer-list and before the closing `}'. */
26438 if (token->type == CPP_CLOSE_BRACE)
26439 break;
26441 /* Suppress location wrappers in a long initializer to save memory
26442 (14179). The cutoff is chosen arbitrarily. */
26443 const unsigned loc_max = 256;
26444 unsigned incr = 1;
26445 if (TREE_CODE (initializer) == CONSTRUCTOR)
26446 /* Look one level down because it's easy. Looking deeper would require
26447 passing down a nelts pointer, and I don't think multi-level massive
26448 initializers are common enough to justify this. */
26449 incr = CONSTRUCTOR_NELTS (initializer);
26450 nelts += incr;
26451 if (nelts >= loc_max && (nelts - incr) < loc_max)
26452 ++suppress_location_wrappers;
26454 /* Consume the `,' token. */
26455 cp_lexer_consume_token (parser->lexer);
26458 /* The same identifier shall not appear in multiple designators
26459 of a designated-initializer-list. */
26460 if (first_designator)
26462 unsigned int i;
26463 tree designator, val;
26464 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26465 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26467 if (IDENTIFIER_MARKED (designator))
26469 error_at (cp_expr_loc_or_input_loc (val),
26470 "%<.%s%> designator used multiple times in "
26471 "the same initializer list",
26472 IDENTIFIER_POINTER (designator));
26473 (*v)[i].index = error_mark_node;
26475 else
26476 IDENTIFIER_MARKED (designator) = 1;
26478 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26479 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26480 IDENTIFIER_MARKED (designator) = 0;
26483 suppress_location_wrappers = suppress;
26485 *designated = first_designator != NULL_TREE;
26486 return v;
26489 /* Classes [gram.class] */
26491 /* Parse a class-name.
26493 class-name:
26494 identifier
26495 template-id
26497 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26498 to indicate that names looked up in dependent types should be
26499 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26500 keyword has been used to indicate that the name that appears next
26501 is a template. TAG_TYPE indicates the explicit tag given before
26502 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26503 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26504 is the class being defined in a class-head. If ENUM_OK is TRUE,
26505 enum-names are also accepted.
26507 Returns the TYPE_DECL representing the class. */
26509 static tree
26510 cp_parser_class_name (cp_parser *parser,
26511 bool typename_keyword_p,
26512 bool template_keyword_p,
26513 enum tag_types tag_type,
26514 bool check_dependency_p,
26515 bool class_head_p,
26516 bool is_declaration,
26517 bool enum_ok)
26519 tree decl;
26520 tree identifier = NULL_TREE;
26522 /* All class-names start with an identifier. */
26523 cp_token *token = cp_lexer_peek_token (parser->lexer);
26524 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26526 cp_parser_error (parser, "expected class-name");
26527 return error_mark_node;
26530 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26531 to a template-id, so we save it here. Consider object scope too,
26532 so that make_typename_type below can use it (cp_parser_template_name
26533 considers object scope also). This may happen with code like
26535 p->template A<T>::a()
26537 where we first want to look up A<T>::a in the class of the object
26538 expression, as per [basic.lookup.classref]. */
26539 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26540 /* This only checks parser->scope to avoid duplicate errors; if
26541 ->object_type is erroneous, go on to give a parse error. */
26542 if (parser->scope == error_mark_node)
26543 return error_mark_node;
26545 /* Any name names a type if we're following the `typename' keyword
26546 in a qualified name where the enclosing scope is type-dependent. */
26547 const bool typename_p = (typename_keyword_p
26548 && parser->scope
26549 && TYPE_P (parser->scope)
26550 && dependent_scope_p (parser->scope));
26551 /* Handle the common case (an identifier, but not a template-id)
26552 efficiently. */
26553 if (token->type == CPP_NAME
26554 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26556 cp_token *identifier_token;
26557 bool ambiguous_p;
26559 /* Look for the identifier. */
26560 identifier_token = cp_lexer_peek_token (parser->lexer);
26561 ambiguous_p = identifier_token->error_reported;
26562 identifier = cp_parser_identifier (parser);
26563 /* If the next token isn't an identifier, we are certainly not
26564 looking at a class-name. */
26565 if (identifier == error_mark_node)
26566 decl = error_mark_node;
26567 /* If we know this is a type-name, there's no need to look it
26568 up. */
26569 else if (typename_p)
26570 decl = identifier;
26571 else
26573 tree ambiguous_decls;
26574 /* If we already know that this lookup is ambiguous, then
26575 we've already issued an error message; there's no reason
26576 to check again. */
26577 if (ambiguous_p)
26579 cp_parser_simulate_error (parser);
26580 return error_mark_node;
26582 /* If the next token is a `::', then the name must be a type
26583 name.
26585 [basic.lookup.qual]
26587 During the lookup for a name preceding the :: scope
26588 resolution operator, object, function, and enumerator
26589 names are ignored. */
26590 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26591 tag_type = scope_type;
26592 /* Look up the name. */
26593 decl = cp_parser_lookup_name (parser, identifier,
26594 tag_type,
26595 /*is_template=*/false,
26596 /*is_namespace=*/false,
26597 check_dependency_p,
26598 &ambiguous_decls,
26599 identifier_token->location);
26600 if (ambiguous_decls)
26602 if (cp_parser_parsing_tentatively (parser))
26603 cp_parser_simulate_error (parser);
26604 return error_mark_node;
26608 else
26610 /* Try a template-id. */
26611 decl = cp_parser_template_id (parser, template_keyword_p,
26612 check_dependency_p,
26613 tag_type,
26614 is_declaration);
26615 if (decl == error_mark_node)
26616 return error_mark_node;
26619 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26621 /* If this is a typename, create a TYPENAME_TYPE. */
26622 if (typename_p && decl != error_mark_node)
26624 decl = make_typename_type (scope, decl, typename_type,
26625 /*complain=*/tf_error);
26626 if (decl != error_mark_node)
26627 decl = TYPE_NAME (decl);
26630 decl = strip_using_decl (decl);
26632 /* Check to see that it is really the name of a class. */
26633 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26634 && identifier_p (TREE_OPERAND (decl, 0))
26635 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26636 /* Situations like this:
26638 template <typename T> struct A {
26639 typename T::template X<int>::I i;
26642 are problematic. Is `T::template X<int>' a class-name? The
26643 standard does not seem to be definitive, but there is no other
26644 valid interpretation of the following `::'. Therefore, those
26645 names are considered class-names. */
26647 decl = make_typename_type (scope, decl, tag_type, tf_error);
26648 if (decl != error_mark_node)
26649 decl = TYPE_NAME (decl);
26651 else if (TREE_CODE (decl) != TYPE_DECL
26652 || TREE_TYPE (decl) == error_mark_node
26653 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26654 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26655 /* In Objective-C 2.0, a classname followed by '.' starts a
26656 dot-syntax expression, and it's not a type-name. */
26657 || (c_dialect_objc ()
26658 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26659 && objc_is_class_name (decl)))
26660 decl = error_mark_node;
26662 if (decl == error_mark_node)
26663 cp_parser_error (parser, "expected class-name");
26664 else if (identifier && !parser->scope)
26665 maybe_note_name_used_in_class (identifier, decl);
26667 return decl;
26670 /* Make sure that any member-function parameters are in scope.
26671 For instance, a function's noexcept-specifier can use the function's
26672 parameters:
26674 struct S {
26675 void fn (int p) noexcept(noexcept(p));
26678 so we need to make sure name lookup can find them. This is used
26679 when we delay parsing of the noexcept-specifier. */
26681 static void
26682 inject_parm_decls (tree decl)
26684 begin_scope (sk_function_parms, decl);
26685 tree args = DECL_ARGUMENTS (decl);
26687 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26689 if (args && is_this_parameter (args))
26691 gcc_checking_assert (current_class_ptr == NULL_TREE);
26692 current_class_ref = cp_build_fold_indirect_ref (args);
26693 current_class_ptr = args;
26697 /* Undo the effects of inject_parm_decls. */
26699 static void
26700 pop_injected_parms (void)
26702 pop_bindings_and_leave_scope ();
26703 current_class_ptr = current_class_ref = NULL_TREE;
26706 /* Parse a class-specifier.
26708 class-specifier:
26709 class-head { member-specification [opt] }
26711 Returns the TREE_TYPE representing the class. */
26713 tree
26714 cp_parser_class_specifier (cp_parser* parser)
26716 auto_timevar tv (TV_PARSE_STRUCT);
26718 tree type;
26719 tree attributes = NULL_TREE;
26720 bool nested_name_specifier_p;
26721 unsigned saved_num_template_parameter_lists;
26722 bool saved_in_function_body;
26723 unsigned char in_statement;
26724 bool in_switch_statement_p;
26725 bool saved_in_unbraced_linkage_specification_p;
26726 tree old_scope = NULL_TREE;
26727 tree scope = NULL_TREE;
26728 cp_token *closing_brace;
26730 push_deferring_access_checks (dk_no_deferred);
26732 /* Parse the class-head. */
26733 type = cp_parser_class_head (parser,
26734 &nested_name_specifier_p);
26735 /* If the class-head was a semantic disaster, skip the entire body
26736 of the class. */
26737 if (!type)
26739 cp_parser_skip_to_end_of_block_or_statement (parser);
26740 pop_deferring_access_checks ();
26741 return error_mark_node;
26744 /* Look for the `{'. */
26745 matching_braces braces;
26746 if (!braces.require_open (parser))
26748 pop_deferring_access_checks ();
26749 return error_mark_node;
26752 cp_ensure_no_omp_declare_simd (parser);
26753 cp_ensure_no_oacc_routine (parser);
26755 /* Issue an error message if type-definitions are forbidden here. */
26756 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26757 /* Remember that we are defining one more class. */
26758 ++parser->num_classes_being_defined;
26759 /* Inside the class, surrounding template-parameter-lists do not
26760 apply. */
26761 saved_num_template_parameter_lists
26762 = parser->num_template_parameter_lists;
26763 parser->num_template_parameter_lists = 0;
26764 /* We are not in a function body. */
26765 saved_in_function_body = parser->in_function_body;
26766 parser->in_function_body = false;
26767 /* Or in a loop. */
26768 in_statement = parser->in_statement;
26769 parser->in_statement = 0;
26770 /* Or in a switch. */
26771 in_switch_statement_p = parser->in_switch_statement_p;
26772 parser->in_switch_statement_p = false;
26773 /* We are not immediately inside an extern "lang" block. */
26774 saved_in_unbraced_linkage_specification_p
26775 = parser->in_unbraced_linkage_specification_p;
26776 parser->in_unbraced_linkage_specification_p = false;
26777 /* 'this' from an enclosing non-static member function is unavailable. */
26778 tree saved_ccp = current_class_ptr;
26779 tree saved_ccr = current_class_ref;
26780 current_class_ptr = NULL_TREE;
26781 current_class_ref = NULL_TREE;
26783 /* Start the class. */
26784 if (nested_name_specifier_p)
26786 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26787 /* SCOPE must be a scope nested inside current scope. */
26788 if (is_nested_namespace (current_namespace,
26789 decl_namespace_context (scope)))
26790 old_scope = push_inner_scope (scope);
26791 else
26792 nested_name_specifier_p = false;
26794 type = begin_class_definition (type);
26796 if (type == error_mark_node)
26797 /* If the type is erroneous, skip the entire body of the class. */
26798 cp_parser_skip_to_closing_brace (parser);
26799 else
26800 /* Parse the member-specification. */
26801 cp_parser_member_specification_opt (parser);
26803 /* Look for the trailing `}'. */
26804 closing_brace = braces.require_close (parser);
26805 /* Look for trailing attributes to apply to this class. */
26806 if (cp_parser_allow_gnu_extensions_p (parser))
26807 attributes = cp_parser_gnu_attributes_opt (parser);
26808 if (type != error_mark_node)
26809 type = finish_struct (type, attributes);
26810 if (nested_name_specifier_p)
26811 pop_inner_scope (old_scope, scope);
26813 /* We've finished a type definition. Check for the common syntax
26814 error of forgetting a semicolon after the definition. We need to
26815 be careful, as we can't just check for not-a-semicolon and be done
26816 with it; the user might have typed:
26818 class X { } c = ...;
26819 class X { } *p = ...;
26821 and so forth. Instead, enumerate all the possible tokens that
26822 might follow this production; if we don't see one of them, then
26823 complain and silently insert the semicolon. */
26825 cp_token *token = cp_lexer_peek_token (parser->lexer);
26826 bool want_semicolon = true;
26828 if (cp_next_tokens_can_be_std_attribute_p (parser))
26829 /* Don't try to parse c++11 attributes here. As per the
26830 grammar, that should be a task for
26831 cp_parser_decl_specifier_seq. */
26832 want_semicolon = false;
26834 switch (token->type)
26836 case CPP_NAME:
26837 case CPP_SEMICOLON:
26838 case CPP_MULT:
26839 case CPP_AND:
26840 case CPP_OPEN_PAREN:
26841 case CPP_CLOSE_PAREN:
26842 case CPP_COMMA:
26843 case CPP_SCOPE:
26844 want_semicolon = false;
26845 break;
26847 /* While it's legal for type qualifiers and storage class
26848 specifiers to follow type definitions in the grammar, only
26849 compiler testsuites contain code like that. Assume that if
26850 we see such code, then what we're really seeing is a case
26851 like:
26853 class X { }
26854 const <type> var = ...;
26858 class Y { }
26859 static <type> func (...) ...
26861 i.e. the qualifier or specifier applies to the next
26862 declaration. To do so, however, we need to look ahead one
26863 more token to see if *that* token is a type specifier.
26865 This code could be improved to handle:
26867 class Z { }
26868 static const <type> var = ...; */
26869 case CPP_KEYWORD:
26870 if (keyword_is_decl_specifier (token->keyword))
26872 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26874 /* Handling user-defined types here would be nice, but very
26875 tricky. */
26876 want_semicolon
26877 = (lookahead->type == CPP_KEYWORD
26878 && keyword_begins_type_specifier (lookahead->keyword));
26880 break;
26881 default:
26882 break;
26885 /* If we don't have a type, then something is very wrong and we
26886 shouldn't try to do anything clever. Likewise for not seeing the
26887 closing brace. */
26888 if (closing_brace && TYPE_P (type) && want_semicolon)
26890 /* Locate the closing brace. */
26891 cp_token_position prev
26892 = cp_lexer_previous_token_position (parser->lexer);
26893 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26894 location_t loc = prev_token->location;
26896 /* We want to suggest insertion of a ';' immediately *after* the
26897 closing brace, so, if we can, offset the location by 1 column. */
26898 location_t next_loc = loc;
26899 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26900 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26902 rich_location richloc (line_table, next_loc);
26904 /* If we successfully offset the location, suggest the fix-it. */
26905 if (next_loc != loc)
26906 richloc.add_fixit_insert_before (next_loc, ";");
26908 if (CLASSTYPE_DECLARED_CLASS (type))
26909 error_at (&richloc,
26910 "expected %<;%> after class definition");
26911 else if (TREE_CODE (type) == RECORD_TYPE)
26912 error_at (&richloc,
26913 "expected %<;%> after struct definition");
26914 else if (TREE_CODE (type) == UNION_TYPE)
26915 error_at (&richloc,
26916 "expected %<;%> after union definition");
26917 else
26918 gcc_unreachable ();
26920 /* Unget one token and smash it to look as though we encountered
26921 a semicolon in the input stream. */
26922 cp_lexer_set_token_position (parser->lexer, prev);
26923 token = cp_lexer_peek_token (parser->lexer);
26924 token->type = CPP_SEMICOLON;
26925 token->keyword = RID_MAX;
26929 /* If this class is not itself within the scope of another class,
26930 then we need to parse the bodies of all of the queued function
26931 definitions. Note that the queued functions defined in a class
26932 are not always processed immediately following the
26933 class-specifier for that class. Consider:
26935 struct A {
26936 struct B { void f() { sizeof (A); } };
26939 If `f' were processed before the processing of `A' were
26940 completed, there would be no way to compute the size of `A'.
26941 Note that the nesting we are interested in here is lexical --
26942 not the semantic nesting given by TYPE_CONTEXT. In particular,
26943 for:
26945 struct A { struct B; };
26946 struct A::B { void f() { } };
26948 there is no need to delay the parsing of `A::B::f'. */
26949 if (--parser->num_classes_being_defined == 0)
26951 tree decl;
26952 tree class_type = NULL_TREE;
26953 tree pushed_scope = NULL_TREE;
26954 unsigned ix;
26955 cp_default_arg_entry *e;
26957 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26959 /* Skip default arguments, NSDMIs, etc, in order to improve
26960 error recovery (c++/71169, c++/71832). */
26961 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26962 vec_safe_truncate (unparsed_nsdmis, 0);
26963 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26966 /* In a first pass, parse default arguments to the functions.
26967 Then, in a second pass, parse the bodies of the functions.
26968 This two-phased approach handles cases like:
26970 struct S {
26971 void f() { g(); }
26972 void g(int i = 3);
26976 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26978 decl = e->decl;
26979 /* If there are default arguments that have not yet been processed,
26980 take care of them now. */
26981 if (class_type != e->class_type)
26983 if (pushed_scope)
26984 pop_scope (pushed_scope);
26985 class_type = e->class_type;
26986 pushed_scope = push_scope (class_type);
26988 /* Make sure that any template parameters are in scope. */
26989 maybe_begin_member_template_processing (decl);
26990 /* Parse the default argument expressions. */
26991 cp_parser_late_parsing_default_args (parser, decl);
26992 /* Remove any template parameters from the symbol table. */
26993 maybe_end_member_template_processing ();
26995 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26997 /* If there are noexcept-specifiers that have not yet been processed,
26998 take care of them now. Do this before processing NSDMIs as they
26999 may depend on noexcept-specifiers already having been processed. */
27000 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
27002 tree ctx = DECL_CONTEXT (decl);
27003 if (class_type != ctx)
27005 if (pushed_scope)
27006 pop_scope (pushed_scope);
27007 class_type = ctx;
27008 pushed_scope = push_scope (class_type);
27011 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
27012 def_parse = TREE_PURPOSE (def_parse);
27014 /* Make sure that any template parameters are in scope. */
27015 maybe_begin_member_template_processing (decl);
27017 /* Make sure that any member-function parameters are in scope.
27018 This function doesn't expect ccp to be set. */
27019 current_class_ptr = current_class_ref = NULL_TREE;
27020 inject_parm_decls (decl);
27022 /* 'this' is not allowed in static member functions. */
27023 unsigned char local_variables_forbidden_p
27024 = parser->local_variables_forbidden_p;
27025 if (DECL_THIS_STATIC (decl))
27026 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27028 /* Now we can parse the noexcept-specifier. */
27029 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
27031 if (spec == error_mark_node)
27032 spec = NULL_TREE;
27034 /* Update the fn's type directly -- it might have escaped
27035 beyond this decl :( */
27036 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
27037 /* Update any instantiations we've already created. We must
27038 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
27039 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
27040 in the pattern. */
27041 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
27042 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
27043 = spec ? TREE_PURPOSE (spec) : error_mark_node;
27045 /* Restore the state of local_variables_forbidden_p. */
27046 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27048 /* The finish_struct call above performed various override checking,
27049 but it skipped unparsed noexcept-specifier operands. Now that we
27050 have resolved them, check again. */
27051 noexcept_override_late_checks (decl);
27053 /* Remove any member-function parameters from the symbol table. */
27054 pop_injected_parms ();
27056 /* Remove any template parameters from the symbol table. */
27057 maybe_end_member_template_processing ();
27059 vec_safe_truncate (unparsed_noexcepts, 0);
27061 /* Now parse any NSDMIs. */
27062 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
27064 tree ctx = type_context_for_name_lookup (decl);
27065 if (class_type != ctx)
27067 if (pushed_scope)
27068 pop_scope (pushed_scope);
27069 class_type = ctx;
27070 pushed_scope = push_scope (class_type);
27072 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
27073 cp_parser_late_parsing_nsdmi (parser, decl);
27075 vec_safe_truncate (unparsed_nsdmis, 0);
27077 /* Now contract attributes. */
27078 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
27080 tree ctx = DECL_CONTEXT (decl);
27081 if (class_type != ctx)
27083 if (pushed_scope)
27084 pop_scope (pushed_scope);
27085 class_type = ctx;
27086 pushed_scope = push_scope (class_type);
27089 temp_override<tree> cfd(current_function_decl, decl);
27091 /* Make sure that any template parameters are in scope. */
27092 maybe_begin_member_template_processing (decl);
27094 /* Make sure that any member-function parameters are in scope.
27095 This function doesn't expect ccp to be set. */
27096 current_class_ptr = current_class_ref = NULL_TREE;
27097 inject_parm_decls (decl);
27099 /* 'this' is not allowed in static member functions. */
27100 unsigned char local_variables_forbidden_p
27101 = parser->local_variables_forbidden_p;
27102 if (DECL_THIS_STATIC (decl))
27103 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27105 /* Now we can parse contract conditions. */
27106 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
27108 if (cxx_contract_attribute_p (a))
27109 cp_parser_late_contract_condition (parser, decl, a);
27112 /* Restore the state of local_variables_forbidden_p. */
27113 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27115 /* Remove any member-function parameters from the symbol table. */
27116 pop_injected_parms ();
27118 /* Remove any template parameters from the symbol table. */
27119 maybe_end_member_template_processing ();
27121 /* Perform any deferred contract matching. */
27122 match_deferred_contracts (decl);
27124 vec_safe_truncate (unparsed_contracts, 0);
27126 current_class_ptr = NULL_TREE;
27127 current_class_ref = NULL_TREE;
27128 if (pushed_scope)
27129 pop_scope (pushed_scope);
27131 /* Now parse the body of the functions. */
27132 if (flag_openmp)
27134 /* OpenMP UDRs need to be parsed before all other functions. */
27135 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27136 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
27137 cp_parser_late_parsing_for_member (parser, decl);
27138 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27139 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
27140 cp_parser_late_parsing_for_member (parser, decl);
27142 else
27143 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27144 cp_parser_late_parsing_for_member (parser, decl);
27145 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27148 /* Put back any saved access checks. */
27149 pop_deferring_access_checks ();
27151 /* Restore saved state. */
27152 parser->in_switch_statement_p = in_switch_statement_p;
27153 parser->in_statement = in_statement;
27154 parser->in_function_body = saved_in_function_body;
27155 parser->num_template_parameter_lists
27156 = saved_num_template_parameter_lists;
27157 parser->in_unbraced_linkage_specification_p
27158 = saved_in_unbraced_linkage_specification_p;
27159 current_class_ptr = saved_ccp;
27160 current_class_ref = saved_ccr;
27162 return type;
27165 /* Parse a class-head.
27167 class-head:
27168 class-key identifier [opt] base-clause [opt]
27169 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
27170 class-key nested-name-specifier [opt] template-id
27171 base-clause [opt]
27173 class-virt-specifier:
27174 final
27176 GNU Extensions:
27177 class-key attributes identifier [opt] base-clause [opt]
27178 class-key attributes nested-name-specifier identifier base-clause [opt]
27179 class-key attributes nested-name-specifier [opt] template-id
27180 base-clause [opt]
27182 Upon return BASES is initialized to the list of base classes (or
27183 NULL, if there are none) in the same form returned by
27184 cp_parser_base_clause.
27186 Returns the TYPE of the indicated class. Sets
27187 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
27188 involving a nested-name-specifier was used, and FALSE otherwise.
27190 Returns error_mark_node if this is not a class-head.
27192 Returns NULL_TREE if the class-head is syntactically valid, but
27193 semantically invalid in a way that means we should skip the entire
27194 body of the class. */
27196 static tree
27197 cp_parser_class_head (cp_parser* parser,
27198 bool* nested_name_specifier_p)
27200 tree nested_name_specifier;
27201 enum tag_types class_key;
27202 tree id = NULL_TREE;
27203 tree type = NULL_TREE;
27204 tree attributes;
27205 tree bases;
27206 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
27207 bool template_id_p = false;
27208 bool qualified_p = false;
27209 bool invalid_nested_name_p = false;
27210 bool invalid_explicit_specialization_p = false;
27211 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27212 tree pushed_scope = NULL_TREE;
27213 unsigned num_templates;
27214 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
27215 /* Assume no nested-name-specifier will be present. */
27216 *nested_name_specifier_p = false;
27217 /* Assume no template parameter lists will be used in defining the
27218 type. */
27219 num_templates = 0;
27220 parser->colon_corrects_to_scope_p = false;
27222 /* Look for the class-key. */
27223 class_key = cp_parser_class_key (parser);
27224 if (class_key == none_type)
27225 return error_mark_node;
27227 location_t class_head_start_location = input_location;
27229 /* Parse the attributes. */
27230 attributes = cp_parser_attributes_opt (parser);
27231 if (find_contract (attributes))
27232 diagnose_misapplied_contracts (attributes);
27234 /* If the next token is `::', that is invalid -- but sometimes
27235 people do try to write:
27237 struct ::S {};
27239 Handle this gracefully by accepting the extra qualifier, and then
27240 issuing an error about it later if this really is a
27241 class-head. If it turns out just to be an elaborated type
27242 specifier, remain silent. */
27243 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
27244 qualified_p = true;
27246 /* It is OK to define an inaccessible class; for example:
27248 class A { class B; };
27249 class A::B {};
27251 So we want to ignore access when parsing the class name.
27252 However, we might be tentatively parsing what is really an
27253 elaborated-type-specifier naming a template-id, e.g.
27255 struct C<&D::m> c;
27257 In this case the tentative parse as a class-head will fail, but not
27258 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
27259 Since dk_no_check is sticky, we must instead use dk_deferred so that
27260 any such CPP_TEMPLATE_ID token created during this tentative parse
27261 will correctly capture the access checks imposed by the template-id . */
27262 push_deferring_access_checks (dk_deferred);
27264 /* Determine the name of the class. Begin by looking for an
27265 optional nested-name-specifier. */
27266 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
27267 nested_name_specifier
27268 = cp_parser_nested_name_specifier_opt (parser,
27269 /*typename_keyword_p=*/false,
27270 /*check_dependency_p=*/false,
27271 /*type_p=*/true,
27272 /*is_declaration=*/false);
27273 /* If there was a nested-name-specifier, then there *must* be an
27274 identifier. */
27276 cp_token *bad_template_keyword = NULL;
27278 if (nested_name_specifier)
27280 type_start_token = cp_lexer_peek_token (parser->lexer);
27281 /* Although the grammar says `identifier', it really means
27282 `class-name' or `template-name'. You are only allowed to
27283 define a class that has already been declared with this
27284 syntax.
27286 The proposed resolution for Core Issue 180 says that wherever
27287 you see `class T::X' you should treat `X' as a type-name.
27289 We do not know if we will see a class-name, or a
27290 template-name. We look for a class-name first, in case the
27291 class-name is a template-id; if we looked for the
27292 template-name first we would stop after the template-name. */
27293 cp_parser_parse_tentatively (parser);
27294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27295 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
27296 type = cp_parser_class_name (parser,
27297 /*typename_keyword_p=*/false,
27298 /*template_keyword_p=*/false,
27299 class_type,
27300 /*check_dependency_p=*/false,
27301 /*class_head_p=*/true,
27302 /*is_declaration=*/false);
27303 /* If that didn't work, ignore the nested-name-specifier. */
27304 if (!cp_parser_parse_definitely (parser))
27306 invalid_nested_name_p = true;
27307 type_start_token = cp_lexer_peek_token (parser->lexer);
27308 id = cp_parser_identifier (parser);
27309 if (id == error_mark_node)
27310 id = NULL_TREE;
27312 /* If we could not find a corresponding TYPE, treat this
27313 declaration like an unqualified declaration. */
27314 if (type == error_mark_node)
27315 nested_name_specifier = NULL_TREE;
27316 /* Otherwise, count the number of templates used in TYPE and its
27317 containing scopes. */
27318 else
27319 num_templates = num_template_headers_for_class (TREE_TYPE (type));
27321 /* Otherwise, the identifier is optional. */
27322 else
27324 /* We don't know whether what comes next is a template-id,
27325 an identifier, or nothing at all. */
27326 cp_parser_parse_tentatively (parser);
27327 /* Check for a template-id. */
27328 type_start_token = cp_lexer_peek_token (parser->lexer);
27329 id = cp_parser_template_id (parser,
27330 /*template_keyword_p=*/false,
27331 /*check_dependency_p=*/true,
27332 class_key,
27333 /*is_declaration=*/true);
27334 /* If that didn't work, it could still be an identifier. */
27335 if (!cp_parser_parse_definitely (parser))
27337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27339 type_start_token = cp_lexer_peek_token (parser->lexer);
27340 id = cp_parser_identifier (parser);
27342 else
27343 id = NULL_TREE;
27345 else
27347 template_id_p = true;
27348 ++num_templates;
27352 pop_deferring_access_checks ();
27354 if (id)
27356 cp_parser_check_for_invalid_template_id (parser, id,
27357 class_key,
27358 type_start_token->location);
27360 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27362 /* If it's not a `:' or a `{' then we can't really be looking at a
27363 class-head, since a class-head only appears as part of a
27364 class-specifier. We have to detect this situation before calling
27365 xref_tag, since that has irreversible side-effects. */
27366 if (!cp_parser_next_token_starts_class_definition_p (parser))
27368 cp_parser_error (parser, "expected %<{%> or %<:%>");
27369 type = error_mark_node;
27370 goto out;
27373 /* At this point, we're going ahead with the class-specifier, even
27374 if some other problem occurs. */
27375 cp_parser_commit_to_tentative_parse (parser);
27376 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27378 cp_parser_error (parser,
27379 "cannot specify %<override%> for a class");
27380 type = error_mark_node;
27381 goto out;
27383 /* Issue the error about the overly-qualified name now. */
27384 if (qualified_p)
27386 cp_parser_error (parser,
27387 "global qualification of class name is invalid");
27388 type = error_mark_node;
27389 goto out;
27391 else if (invalid_nested_name_p)
27393 cp_parser_error (parser,
27394 "qualified name does not name a class");
27395 type = error_mark_node;
27396 goto out;
27398 else if (nested_name_specifier)
27400 tree scope;
27402 if (bad_template_keyword)
27403 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27404 keyword template shall not appear at the top level. */
27405 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27406 "keyword %<template%> not allowed in class-head-name");
27408 /* Reject typedef-names in class heads. */
27409 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27411 error_at (type_start_token->location,
27412 "invalid class name in declaration of %qD",
27413 type);
27414 type = NULL_TREE;
27415 goto done;
27418 /* Figure out in what scope the declaration is being placed. */
27419 scope = current_scope ();
27420 /* If that scope does not contain the scope in which the
27421 class was originally declared, the program is invalid. */
27422 if (scope && !is_ancestor (scope, nested_name_specifier))
27424 if (at_namespace_scope_p ())
27425 error_at (type_start_token->location,
27426 "declaration of %qD in namespace %qD which does not "
27427 "enclose %qD",
27428 type, scope, nested_name_specifier);
27429 else
27430 error_at (type_start_token->location,
27431 "declaration of %qD in %qD which does not enclose %qD",
27432 type, scope, nested_name_specifier);
27433 type = NULL_TREE;
27434 goto done;
27436 /* [dcl.meaning]
27438 A declarator-id shall not be qualified except for the
27439 definition of a ... nested class outside of its class
27440 ... [or] the definition or explicit instantiation of a
27441 class member of a namespace outside of its namespace. */
27442 if (scope == nested_name_specifier)
27443 permerror (nested_name_specifier_token_start->location,
27444 "extra qualification not allowed");
27446 /* An explicit-specialization must be preceded by "template <>". If
27447 it is not, try to recover gracefully. */
27448 if (at_namespace_scope_p ()
27449 && parser->num_template_parameter_lists == 0
27450 && !processing_template_parmlist
27451 && template_id_p)
27453 /* Build a location of this form:
27454 struct typename <ARGS>
27455 ^~~~~~~~~~~~~~~~~~~~~~
27456 with caret==start at the start token, and
27457 finishing at the end of the type. */
27458 location_t reported_loc
27459 = make_location (class_head_start_location,
27460 class_head_start_location,
27461 get_finish (type_start_token->location));
27462 rich_location richloc (line_table, reported_loc);
27463 richloc.add_fixit_insert_before (class_head_start_location,
27464 "template <> ");
27465 error_at (&richloc,
27466 "an explicit specialization must be preceded by"
27467 " %<template <>%>");
27468 invalid_explicit_specialization_p = true;
27469 /* Take the same action that would have been taken by
27470 cp_parser_explicit_specialization. */
27471 ++parser->num_template_parameter_lists;
27472 begin_specialization ();
27474 /* There must be no "return" statements between this point and the
27475 end of this function; set "type "to the correct return value and
27476 use "goto done;" to return. */
27477 /* Make sure that the right number of template parameters were
27478 present. */
27479 if (!cp_parser_check_template_parameters (parser, num_templates,
27480 template_id_p,
27481 type_start_token->location,
27482 /*declarator=*/NULL))
27484 /* If something went wrong, there is no point in even trying to
27485 process the class-definition. */
27486 type = NULL_TREE;
27487 goto done;
27490 /* Look up the type. */
27491 if (template_id_p)
27493 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27494 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27495 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27497 error_at (type_start_token->location,
27498 "function template %qD redeclared as a class template", id);
27499 type = error_mark_node;
27501 else
27503 type = TREE_TYPE (id);
27504 type = maybe_process_partial_specialization (type);
27506 /* Check the scope while we still know whether or not we had a
27507 nested-name-specifier. */
27508 if (type != error_mark_node)
27509 check_unqualified_spec_or_inst (type, type_start_token->location);
27511 if (nested_name_specifier)
27512 pushed_scope = push_scope (nested_name_specifier);
27514 else if (nested_name_specifier)
27516 type = TREE_TYPE (type);
27518 /* Given:
27520 template <typename T> struct S { struct T };
27521 template <typename T> struct S<T>::T { };
27523 we will get a TYPENAME_TYPE when processing the definition of
27524 `S::T'. We need to resolve it to the actual type before we
27525 try to define it. */
27526 if (TREE_CODE (type) == TYPENAME_TYPE)
27528 type = resolve_typename_type (type, /*only_current_p=*/false);
27529 if (TREE_CODE (type) == TYPENAME_TYPE)
27531 cp_parser_error (parser, "could not resolve typename type");
27532 type = error_mark_node;
27536 type = maybe_process_partial_specialization (type);
27537 if (type == error_mark_node)
27539 type = NULL_TREE;
27540 goto done;
27543 /* Enter the scope indicated by the nested-name-specifier. */
27544 pushed_scope = push_scope (nested_name_specifier);
27545 /* Get the canonical version of this type. */
27546 type = TYPE_MAIN_DECL (type);
27547 /* Call push_template_decl if it seems like we should be defining a
27548 template either from the template headers or the type we're
27549 defining, so that we diagnose both extra and missing headers. */
27550 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27551 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27552 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27554 type = push_template_decl (type);
27555 if (type == error_mark_node)
27557 type = NULL_TREE;
27558 goto done;
27562 type = TREE_TYPE (type);
27563 *nested_name_specifier_p = true;
27565 else /* The name is not a nested name. */
27567 /* If the class was unnamed, create a dummy name. */
27568 if (!id)
27569 id = make_anon_name ();
27570 TAG_how how = (parser->in_type_id_in_expr_p
27571 ? TAG_how::INNERMOST_NON_CLASS
27572 : TAG_how::CURRENT_ONLY);
27573 type = xref_tag (class_key, id, how,
27574 parser->num_template_parameter_lists);
27577 /* Diagnose class/struct/union mismatches. */
27578 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27579 true, true);
27581 /* Indicate whether this class was declared as a `class' or as a
27582 `struct'. */
27583 if (TREE_CODE (type) == RECORD_TYPE)
27584 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27586 /* If this type was already complete, and we see another definition,
27587 that's an error. Likewise if the type is already being defined:
27588 this can happen, eg, when it's defined from within an expression
27589 (c++/84605). */
27590 if (type != error_mark_node
27591 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27593 error_at (type_start_token->location, "redefinition of %q#T",
27594 type);
27595 inform (location_of (type), "previous definition of %q#T",
27596 type);
27597 type = NULL_TREE;
27598 goto done;
27600 else if (type == error_mark_node)
27601 type = NULL_TREE;
27603 if (type)
27605 if (current_lambda_expr ()
27606 && uses_parameter_packs (attributes))
27608 /* In a lambda this should work, but doesn't currently. */
27609 sorry ("unexpanded parameter pack in local class in lambda");
27610 attributes = NULL_TREE;
27613 /* Apply attributes now, before any use of the class as a template
27614 argument in its base list. */
27615 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27616 fixup_attribute_variants (type);
27619 /* Associate constraints with the type. */
27620 if (flag_concepts)
27621 type = associate_classtype_constraints (type);
27623 /* We will have entered the scope containing the class; the names of
27624 base classes should be looked up in that context. For example:
27626 struct A { struct B {}; struct C; };
27627 struct A::C : B {};
27629 is valid. */
27631 /* Get the list of base-classes, if there is one. Defer access checking
27632 until the entire list has been seen, as per [class.access.general]. */
27633 push_deferring_access_checks (dk_deferred);
27634 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27636 if (type)
27637 pushclass (type);
27638 bases = cp_parser_base_clause (parser);
27639 if (type)
27640 popclass ();
27642 else
27643 bases = NULL_TREE;
27645 /* If we're really defining a class, process the base classes.
27646 If they're invalid, fail. */
27647 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27648 xref_basetypes (type, bases);
27650 /* Now that all bases have been seen and attached to the class, check
27651 accessibility of the types named in the base-clause. This must be
27652 done relative to the class scope, so that we accept e.g.
27654 struct A { protected: struct B {}; };
27655 struct C : A::B, A {}; // OK: A::B is accessible via base A
27657 as per [class.access.general]. */
27658 if (type)
27659 pushclass (type);
27660 pop_to_parent_deferring_access_checks ();
27661 if (type)
27662 popclass ();
27664 done:
27665 /* Leave the scope given by the nested-name-specifier. We will
27666 enter the class scope itself while processing the members. */
27667 if (pushed_scope)
27668 pop_scope (pushed_scope);
27670 if (invalid_explicit_specialization_p)
27672 end_specialization ();
27673 --parser->num_template_parameter_lists;
27676 if (type)
27677 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27678 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27679 CLASSTYPE_FINAL (type) = 1;
27680 out:
27681 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27682 return type;
27685 /* Parse a class-key.
27687 class-key:
27688 class
27689 struct
27690 union
27692 Returns the kind of class-key specified, or none_type to indicate
27693 error. */
27695 static enum tag_types
27696 cp_parser_class_key (cp_parser* parser)
27698 cp_token *token;
27699 enum tag_types tag_type;
27701 /* Look for the class-key. */
27702 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27703 if (!token)
27704 return none_type;
27706 /* Check to see if the TOKEN is a class-key. */
27707 tag_type = cp_parser_token_is_class_key (token);
27708 if (!tag_type)
27709 cp_parser_error (parser, "expected class-key");
27710 return tag_type;
27713 /* Parse a type-parameter-key.
27715 type-parameter-key:
27716 class
27717 typename
27720 static void
27721 cp_parser_type_parameter_key (cp_parser* parser)
27723 /* Look for the type-parameter-key. */
27724 enum tag_types tag_type = none_type;
27725 cp_token *token = cp_lexer_peek_token (parser->lexer);
27726 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27728 cp_lexer_consume_token (parser->lexer);
27729 if (pedantic && tag_type == typename_type
27730 && cxx_dialect < cxx17)
27731 /* typename is not allowed in a template template parameter
27732 by the standard until C++17. */
27733 pedwarn (token->location, OPT_Wc__17_extensions,
27734 "ISO C++ forbids typename key in template template parameter;"
27735 " use %<-std=c++17%> or %<-std=gnu++17%>");
27737 else
27738 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27740 return;
27743 /* Parse an (optional) member-specification.
27745 member-specification:
27746 member-declaration member-specification [opt]
27747 access-specifier : member-specification [opt] */
27749 static void
27750 cp_parser_member_specification_opt (cp_parser* parser)
27752 while (true)
27754 cp_token *token;
27755 enum rid keyword;
27757 /* Peek at the next token. */
27758 token = cp_lexer_peek_token (parser->lexer);
27759 /* If it's a `}', or EOF then we've seen all the members. */
27760 if (token->type == CPP_CLOSE_BRACE
27761 || token->type == CPP_EOF
27762 || token->type == CPP_PRAGMA_EOL)
27763 break;
27765 /* See if this token is a keyword. */
27766 keyword = token->keyword;
27767 switch (keyword)
27769 case RID_PUBLIC:
27770 case RID_PROTECTED:
27771 case RID_PRIVATE:
27772 /* Consume the access-specifier. */
27773 cp_lexer_consume_token (parser->lexer);
27774 /* Remember which access-specifier is active. */
27775 current_access_specifier = token->u.value;
27776 /* Look for the `:'. */
27777 cp_parser_require (parser, CPP_COLON, RT_COLON);
27778 break;
27780 default:
27781 /* Accept #pragmas at class scope. */
27782 if (token->type == CPP_PRAGMA)
27784 cp_parser_pragma (parser, pragma_member, NULL);
27785 break;
27788 /* Otherwise, the next construction must be a
27789 member-declaration. */
27790 cp_parser_member_declaration (parser);
27795 /* Parse a member-declaration.
27797 member-declaration:
27798 decl-specifier-seq [opt] member-declarator-list [opt] ;
27799 function-definition ; [opt]
27800 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27801 using-declaration
27802 template-declaration
27803 alias-declaration
27805 member-declarator-list:
27806 member-declarator
27807 member-declarator-list , member-declarator
27809 member-declarator:
27810 declarator pure-specifier [opt]
27811 declarator constant-initializer [opt]
27812 identifier [opt] : constant-expression
27814 GNU Extensions:
27816 member-declaration:
27817 __extension__ member-declaration
27819 member-declarator:
27820 declarator attributes [opt] pure-specifier [opt]
27821 declarator attributes [opt] constant-initializer [opt]
27822 identifier [opt] attributes [opt] : constant-expression
27824 C++0x Extensions:
27826 member-declaration:
27827 static_assert-declaration */
27829 static void
27830 cp_parser_member_declaration (cp_parser* parser)
27832 cp_decl_specifier_seq decl_specifiers;
27833 tree prefix_attributes;
27834 tree decl;
27835 int declares_class_or_enum;
27836 bool friend_p;
27837 cp_token *token = NULL;
27838 cp_token *decl_spec_token_start = NULL;
27839 cp_token *initializer_token_start = NULL;
27840 int saved_pedantic;
27841 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27843 /* Check for the `__extension__' keyword. */
27844 if (cp_parser_extension_opt (parser, &saved_pedantic))
27846 /* Recurse. */
27847 cp_parser_member_declaration (parser);
27848 /* Restore the old value of the PEDANTIC flag. */
27849 pedantic = saved_pedantic;
27851 return;
27854 /* Check for a template-declaration. */
27855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27857 /* An explicit specialization here is an error condition, and we
27858 expect the specialization handler to detect and report this. */
27859 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27860 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27861 cp_parser_explicit_specialization (parser);
27862 else
27863 cp_parser_template_declaration (parser, /*member_p=*/true);
27865 return;
27867 /* Check for a template introduction. */
27868 else if (cp_parser_template_declaration_after_export (parser, true))
27869 return;
27871 /* Check for a using-declaration. */
27872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27874 if (cxx_dialect < cxx11)
27875 /* Parse the using-declaration. */
27876 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27877 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27878 cp_parser_using_enum (parser);
27879 else
27881 tree decl;
27882 bool alias_decl_expected;
27883 cp_parser_parse_tentatively (parser);
27884 decl = cp_parser_alias_declaration (parser);
27885 /* Note that if we actually see the '=' token after the
27886 identifier, cp_parser_alias_declaration commits the
27887 tentative parse. In that case, we really expect an
27888 alias-declaration. Otherwise, we expect a using
27889 declaration. */
27890 alias_decl_expected =
27891 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27892 cp_parser_parse_definitely (parser);
27894 if (alias_decl_expected)
27895 finish_member_declaration (decl);
27896 else
27897 cp_parser_using_declaration (parser,
27898 /*access_declaration_p=*/false);
27900 return;
27903 /* Check for @defs. */
27904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27906 tree ivar, member;
27907 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27908 ivar = ivar_chains;
27909 while (ivar)
27911 member = ivar;
27912 ivar = TREE_CHAIN (member);
27913 TREE_CHAIN (member) = NULL_TREE;
27914 finish_member_declaration (member);
27916 return;
27919 /* If the next token is `static_assert' we have a static assertion. */
27920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27922 cp_parser_static_assert (parser, /*member_p=*/true);
27923 return;
27926 parser->colon_corrects_to_scope_p = false;
27928 cp_omp_declare_simd_data odsd;
27929 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27930 goto out;
27932 /* Parse the decl-specifier-seq. */
27933 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27934 cp_parser_decl_specifier_seq (parser,
27935 (CP_PARSER_FLAGS_OPTIONAL
27936 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27937 &decl_specifiers,
27938 &declares_class_or_enum);
27940 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27941 cp_parser_handle_directive_omp_attributes (parser,
27942 &decl_specifiers.attributes,
27943 &odsd, true);
27945 /* Check for an invalid type-name. */
27946 if (!decl_specifiers.any_type_specifiers_p
27947 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27948 goto out;
27949 /* If there is no declarator, then the decl-specifier-seq should
27950 specify a type. */
27951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27953 /* If there was no decl-specifier-seq, and the next token is a
27954 `;', then we have something like:
27956 struct S { ; };
27958 [class.mem]
27960 Each member-declaration shall declare at least one member
27961 name of the class. */
27962 if (!decl_specifiers.any_specifiers_p)
27964 cp_token *token = cp_lexer_peek_token (parser->lexer);
27965 if (!in_system_header_at (token->location))
27967 gcc_rich_location richloc (token->location);
27968 richloc.add_fixit_remove ();
27969 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27972 else
27974 /* See if this declaration is a friend. */
27975 friend_p = cp_parser_friend_p (&decl_specifiers);
27976 /* If there were decl-specifiers, check to see if there was
27977 a class-declaration. */
27978 tree type = check_tag_decl (&decl_specifiers,
27979 /*explicit_type_instantiation_p=*/false);
27980 /* Nested classes have already been added to the class, but
27981 a `friend' needs to be explicitly registered. */
27982 if (friend_p)
27984 /* If the `friend' keyword was present, the friend must
27985 be introduced with a class-key. */
27986 if (!declares_class_or_enum && cxx_dialect < cxx11)
27987 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27988 "in C++03 a class-key must be used "
27989 "when declaring a friend");
27990 /* In this case:
27992 template <typename T> struct A {
27993 friend struct A<T>::B;
27996 A<T>::B will be represented by a TYPENAME_TYPE, and
27997 therefore not recognized by check_tag_decl. */
27998 if (!type)
28000 type = decl_specifiers.type;
28001 if (type && TREE_CODE (type) == TYPE_DECL)
28002 type = TREE_TYPE (type);
28004 /* Warn if an attribute cannot appear here, as per
28005 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
28006 we ignore attributes in elaborated-type-specifiers. */
28007 if (!declares_class_or_enum
28008 && cxx11_attribute_p (decl_specifiers.attributes))
28010 decl_specifiers.attributes = NULL_TREE;
28011 if (warning_at (decl_spec_token_start->location,
28012 OPT_Wattributes, "attribute ignored"))
28013 inform (decl_spec_token_start->location, "an attribute "
28014 "that appertains to a friend declaration that "
28015 "is not a definition is ignored");
28017 if (!type || !TYPE_P (type))
28018 error_at (decl_spec_token_start->location,
28019 "friend declaration does not name a class or "
28020 "function");
28021 else
28022 make_friend_class (current_class_type, type,
28023 /*complain=*/true);
28025 /* If there is no TYPE, an error message will already have
28026 been issued. */
28027 else if (!type || type == error_mark_node)
28029 /* An anonymous aggregate has to be handled specially; such
28030 a declaration really declares a data member (with a
28031 particular type), as opposed to a nested class. */
28032 else if (ANON_AGGR_TYPE_P (type))
28034 /* C++11 9.5/6. */
28035 if (decl_specifiers.storage_class != sc_none)
28036 error_at (decl_spec_token_start->location,
28037 "a storage class on an anonymous aggregate "
28038 "in class scope is not allowed");
28040 /* Remove constructors and such from TYPE, now that we
28041 know it is an anonymous aggregate. */
28042 fixup_anonymous_aggr (type);
28043 /* And make the corresponding data member. */
28044 decl = build_decl (decl_spec_token_start->location,
28045 FIELD_DECL, NULL_TREE, type);
28046 /* Add it to the class. */
28047 finish_member_declaration (decl);
28049 else
28050 cp_parser_check_access_in_redeclaration
28051 (TYPE_NAME (type),
28052 decl_spec_token_start->location);
28055 else
28057 bool assume_semicolon = false;
28059 /* Clear attributes from the decl_specifiers but keep them
28060 around as prefix attributes that apply them to the entity
28061 being declared. */
28062 prefix_attributes = decl_specifiers.attributes;
28063 decl_specifiers.attributes = NULL_TREE;
28064 if (parser->omp_declare_simd
28065 && (parser->omp_declare_simd->attribs[0]
28066 == &decl_specifiers.attributes))
28067 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
28069 /* See if these declarations will be friends. */
28070 friend_p = cp_parser_friend_p (&decl_specifiers);
28072 /* Keep going until we hit the `;' at the end of the
28073 declaration. */
28074 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28076 tree attributes = NULL_TREE;
28077 tree first_attribute;
28078 tree initializer;
28079 bool named_bitfld = false;
28081 /* Peek at the next token. */
28082 token = cp_lexer_peek_token (parser->lexer);
28084 /* The following code wants to know early if it is a bit-field
28085 or some other declaration. Attributes can appear before
28086 the `:' token. Skip over them without consuming any tokens
28087 to peek if they are followed by `:'. */
28088 if (cp_next_tokens_can_be_attribute_p (parser)
28089 || (token->type == CPP_NAME
28090 && cp_nth_tokens_can_be_attribute_p (parser, 2)
28091 && (named_bitfld = true)))
28093 size_t n
28094 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
28095 token = cp_lexer_peek_nth_token (parser->lexer, n);
28098 /* Check for a bitfield declaration. */
28099 if (token->type == CPP_COLON
28100 || (token->type == CPP_NAME
28101 && token == cp_lexer_peek_token (parser->lexer)
28102 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
28103 && (named_bitfld = true)))
28105 tree identifier;
28106 tree width;
28107 tree late_attributes = NULL_TREE;
28108 location_t id_location
28109 = cp_lexer_peek_token (parser->lexer)->location;
28111 if (named_bitfld)
28112 identifier = cp_parser_identifier (parser);
28113 else
28114 identifier = NULL_TREE;
28116 /* Look for attributes that apply to the bitfield. */
28117 attributes = cp_parser_attributes_opt (parser);
28119 /* Consume the `:' token. */
28120 cp_lexer_consume_token (parser->lexer);
28122 /* Get the width of the bitfield. */
28123 width = cp_parser_constant_expression (parser, false, NULL,
28124 cxx_dialect >= cxx11);
28126 /* In C++20 and as extension for C++11 and above we allow
28127 default member initializers for bit-fields. */
28128 initializer = NULL_TREE;
28129 if (cxx_dialect >= cxx11
28130 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
28131 || cp_lexer_next_token_is (parser->lexer,
28132 CPP_OPEN_BRACE)))
28134 location_t loc
28135 = cp_lexer_peek_token (parser->lexer)->location;
28136 if (cxx_dialect < cxx20
28137 && identifier != NULL_TREE)
28138 pedwarn (loc, OPT_Wc__20_extensions,
28139 "default member initializers for bit-fields "
28140 "only available with %<-std=c++20%> or "
28141 "%<-std=gnu++20%>");
28143 initializer = cp_parser_save_nsdmi (parser);
28144 if (identifier == NULL_TREE)
28146 error_at (loc, "default member initializer for "
28147 "unnamed bit-field");
28148 initializer = NULL_TREE;
28151 else
28153 /* Look for attributes that apply to the bitfield after
28154 the `:' token and width. This is where GCC used to
28155 parse attributes in the past, pedwarn if there is
28156 a std attribute. */
28157 if (cp_next_tokens_can_be_std_attribute_p (parser))
28158 pedwarn (input_location, OPT_Wpedantic,
28159 "ISO C++ allows bit-field attributes only "
28160 "before the %<:%> token");
28162 late_attributes = cp_parser_attributes_opt (parser);
28165 attributes = attr_chainon (attributes, late_attributes);
28167 /* Remember which attributes are prefix attributes and
28168 which are not. */
28169 first_attribute = attributes;
28170 /* Combine the attributes. */
28171 attributes = attr_chainon (prefix_attributes, attributes);
28173 /* Create the bitfield declaration. */
28174 decl = grokbitfield (identifier
28175 ? make_id_declarator (NULL_TREE,
28176 identifier,
28177 sfk_none,
28178 id_location)
28179 : NULL,
28180 &decl_specifiers,
28181 width, initializer,
28182 attributes);
28184 else
28186 cp_declarator *declarator;
28187 tree asm_specification;
28188 int ctor_dtor_or_conv_p;
28189 bool static_p = (decl_specifiers.storage_class == sc_static);
28190 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
28191 /* We can't delay parsing for friends,
28192 alias-declarations, and typedefs, even though the
28193 standard seems to require it. */
28194 if (!friend_p
28195 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28196 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
28198 /* Parse the declarator. */
28199 declarator
28200 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28201 flags,
28202 &ctor_dtor_or_conv_p,
28203 /*parenthesized_p=*/NULL,
28204 /*member_p=*/true,
28205 friend_p, static_p);
28207 /* If something went wrong parsing the declarator, make sure
28208 that we at least consume some tokens. */
28209 if (declarator == cp_error_declarator)
28211 /* Skip to the end of the statement. */
28212 cp_parser_skip_to_end_of_statement (parser);
28213 /* If the next token is not a semicolon, that is
28214 probably because we just skipped over the body of
28215 a function. So, we consume a semicolon if
28216 present, but do not issue an error message if it
28217 is not present. */
28218 if (cp_lexer_next_token_is (parser->lexer,
28219 CPP_SEMICOLON))
28220 cp_lexer_consume_token (parser->lexer);
28221 goto out;
28224 /* Handle class-scope non-template C++17 deduction guides. */
28225 cp_parser_maybe_adjust_declarator_for_dguide (parser,
28226 &decl_specifiers,
28227 declarator,
28228 &ctor_dtor_or_conv_p);
28230 if (declares_class_or_enum & 2)
28231 cp_parser_check_for_definition_in_return_type
28232 (declarator, decl_specifiers.type,
28233 decl_specifiers.locations[ds_type_spec]);
28235 /* Look for an asm-specification. */
28236 asm_specification = cp_parser_asm_specification_opt (parser);
28237 /* Look for attributes that apply to the declaration. */
28238 attributes = cp_parser_attributes_opt (parser);
28239 /* Remember which attributes are prefix attributes and
28240 which are not. */
28241 first_attribute = attributes;
28242 /* Combine the attributes. */
28243 attributes = attr_chainon (prefix_attributes, attributes);
28245 /* If it's an `=', then we have a constant-initializer or a
28246 pure-specifier. It is not correct to parse the
28247 initializer before registering the member declaration
28248 since the member declaration should be in scope while
28249 its initializer is processed. However, the rest of the
28250 front end does not yet provide an interface that allows
28251 us to handle this correctly. */
28252 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28254 /* In [class.mem]:
28256 A pure-specifier shall be used only in the declaration of
28257 a virtual function.
28259 A member-declarator can contain a constant-initializer
28260 only if it declares a static member of integral or
28261 enumeration type.
28263 Therefore, if the DECLARATOR is for a function, we look
28264 for a pure-specifier; otherwise, we look for a
28265 constant-initializer. When we call `grokfield', it will
28266 perform more stringent semantics checks. */
28267 initializer_token_start = cp_lexer_peek_token (parser->lexer);
28268 declarator->init_loc = initializer_token_start->location;
28269 if (function_declarator_p (declarator)
28270 || (decl_specifiers.type
28271 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
28272 && declarator->kind == cdk_id
28273 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
28274 == FUNCTION_TYPE)))
28275 initializer = cp_parser_pure_specifier (parser);
28276 else if (decl_specifiers.storage_class != sc_static)
28277 initializer = cp_parser_save_nsdmi (parser);
28278 else if (cxx_dialect >= cxx11)
28280 /* Don't require a constant rvalue in C++11, since we
28281 might want a reference constant. We'll enforce
28282 constancy later. */
28283 cp_lexer_consume_token (parser->lexer);
28284 /* Parse the initializer. */
28285 initializer = cp_parser_initializer_clause (parser);
28287 else
28288 /* Parse the initializer. */
28289 initializer = cp_parser_constant_initializer (parser);
28291 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
28292 && !function_declarator_p (declarator))
28294 declarator->init_loc
28295 = cp_lexer_peek_token (parser->lexer)->location;
28296 if (decl_specifiers.storage_class != sc_static)
28297 initializer = cp_parser_save_nsdmi (parser);
28298 else
28299 initializer = cp_parser_initializer (parser);
28301 /* Detect invalid bit-field cases such as
28303 int *p : 4;
28304 int &&r : 3;
28306 and similar. */
28307 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
28308 /* If there were no type specifiers, it was a
28309 constructor. */
28310 && decl_specifiers.any_type_specifiers_p)
28312 /* This is called for a decent diagnostic only. */
28313 tree d = grokdeclarator (declarator, &decl_specifiers,
28314 BITFIELD, /*initialized=*/false,
28315 &attributes);
28316 if (!error_operand_p (d))
28317 error_at (DECL_SOURCE_LOCATION (d),
28318 "bit-field %qD has non-integral type %qT",
28319 d, TREE_TYPE (d));
28320 cp_parser_skip_to_end_of_statement (parser);
28321 /* Avoid "extra ;" pedwarns. */
28322 if (cp_lexer_next_token_is (parser->lexer,
28323 CPP_SEMICOLON))
28324 cp_lexer_consume_token (parser->lexer);
28325 goto out;
28327 /* Otherwise, there is no initializer. */
28328 else
28329 initializer = NULL_TREE;
28331 /* See if we are probably looking at a function
28332 definition. We are certainly not looking at a
28333 member-declarator. Calling `grokfield' has
28334 side-effects, so we must not do it unless we are sure
28335 that we are looking at a member-declarator. */
28336 if (cp_parser_token_starts_function_definition_p
28337 (cp_lexer_peek_token (parser->lexer)))
28339 /* The grammar does not allow a pure-specifier to be
28340 used when a member function is defined. (It is
28341 possible that this fact is an oversight in the
28342 standard, since a pure function may be defined
28343 outside of the class-specifier. */
28344 if (initializer && initializer_token_start)
28345 error_at (initializer_token_start->location,
28346 "pure-specifier on function-definition");
28347 decl = cp_parser_save_member_function_body (parser,
28348 &decl_specifiers,
28349 declarator,
28350 attributes);
28352 if (parser->fully_implicit_function_template_p)
28353 decl = finish_fully_implicit_template (parser, decl);
28354 /* If the member was not a friend, declare it here. */
28355 if (!friend_p)
28356 finish_member_declaration (decl);
28357 /* Peek at the next token. */
28358 token = cp_lexer_peek_token (parser->lexer);
28359 /* If the next token is a semicolon, consume it. */
28360 if (token->type == CPP_SEMICOLON)
28362 location_t semicolon_loc
28363 = cp_lexer_consume_token (parser->lexer)->location;
28364 gcc_rich_location richloc (semicolon_loc);
28365 richloc.add_fixit_remove ();
28366 warning_at (&richloc, OPT_Wextra_semi,
28367 "extra %<;%> after in-class "
28368 "function definition");
28370 goto out;
28372 else
28373 if (declarator->kind == cdk_function)
28374 declarator->id_loc = token->location;
28376 /* Create the declaration. */
28377 decl = grokfield (declarator, &decl_specifiers,
28378 initializer, /*init_const_expr_p=*/true,
28379 asm_specification, attributes);
28381 if (parser->fully_implicit_function_template_p)
28383 if (friend_p)
28384 finish_fully_implicit_template (parser, 0);
28385 else
28386 decl = finish_fully_implicit_template (parser, decl);
28390 cp_finalize_omp_declare_simd (parser, decl);
28391 cp_finalize_oacc_routine (parser, decl, false);
28393 /* Reset PREFIX_ATTRIBUTES. */
28394 if (attributes != error_mark_node)
28396 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28397 attributes = TREE_CHAIN (attributes);
28398 if (attributes)
28399 TREE_CHAIN (attributes) = NULL_TREE;
28402 /* If there is any qualification still in effect, clear it
28403 now; we will be starting fresh with the next declarator. */
28404 parser->scope = NULL_TREE;
28405 parser->qualifying_scope = NULL_TREE;
28406 parser->object_scope = NULL_TREE;
28407 /* If it's a `,', then there are more declarators. */
28408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28410 cp_lexer_consume_token (parser->lexer);
28411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28413 cp_token *token = cp_lexer_previous_token (parser->lexer);
28414 gcc_rich_location richloc (token->location);
28415 richloc.add_fixit_remove ();
28416 error_at (&richloc, "stray %<,%> at end of "
28417 "member declaration");
28420 /* If the next token isn't a `;', then we have a parse error. */
28421 else if (cp_lexer_next_token_is_not (parser->lexer,
28422 CPP_SEMICOLON))
28424 /* The next token might be a ways away from where the
28425 actual semicolon is missing. Find the previous token
28426 and use that for our error position. */
28427 cp_token *token = cp_lexer_previous_token (parser->lexer);
28428 gcc_rich_location richloc (token->location);
28429 richloc.add_fixit_insert_after (";");
28430 error_at (&richloc, "expected %<;%> at end of "
28431 "member declaration");
28433 /* Assume that the user meant to provide a semicolon. If
28434 we were to cp_parser_skip_to_end_of_statement, we might
28435 skip to a semicolon inside a member function definition
28436 and issue nonsensical error messages. */
28437 assume_semicolon = true;
28440 if (decl)
28442 /* Add DECL to the list of members. */
28443 if (!friend_p
28444 /* Explicitly include, eg, NSDMIs, for better error
28445 recovery (c++/58650). */
28446 || !DECL_DECLARES_FUNCTION_P (decl))
28447 finish_member_declaration (decl);
28449 if (DECL_DECLARES_FUNCTION_P (decl))
28450 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28451 else if (TREE_CODE (decl) == FIELD_DECL
28452 && DECL_INITIAL (decl))
28453 /* Add DECL to the queue of NSDMI to be parsed later. */
28454 vec_safe_push (unparsed_nsdmis, decl);
28457 if (assume_semicolon)
28458 goto out;
28462 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28463 out:
28464 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28465 cp_finalize_omp_declare_simd (parser, &odsd);
28468 /* Parse a pure-specifier.
28470 pure-specifier:
28473 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28474 Otherwise, ERROR_MARK_NODE is returned. */
28476 static tree
28477 cp_parser_pure_specifier (cp_parser* parser)
28479 cp_token *token;
28481 /* Look for the `=' token. */
28482 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28483 return error_mark_node;
28484 /* Look for the `0' token. */
28485 token = cp_lexer_peek_token (parser->lexer);
28487 if (token->type == CPP_EOF
28488 || token->type == CPP_PRAGMA_EOL)
28489 return error_mark_node;
28491 cp_lexer_consume_token (parser->lexer);
28493 /* Accept = default or = delete in c++0x mode. */
28494 if (token->keyword == RID_DEFAULT
28495 || token->keyword == RID_DELETE)
28497 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28498 return token->u.value;
28501 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28502 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28504 cp_parser_error (parser,
28505 "invalid pure specifier (only %<= 0%> is allowed)");
28506 cp_parser_skip_to_end_of_statement (parser);
28507 return error_mark_node;
28509 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28511 error_at (token->location, "templates may not be %<virtual%>");
28512 return error_mark_node;
28515 return integer_zero_node;
28518 /* Parse a constant-initializer.
28520 constant-initializer:
28521 = constant-expression
28523 Returns a representation of the constant-expression. */
28525 static tree
28526 cp_parser_constant_initializer (cp_parser* parser)
28528 /* Look for the `=' token. */
28529 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28530 return error_mark_node;
28532 /* It is invalid to write:
28534 struct S { static const int i = { 7 }; };
28537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28539 cp_parser_error (parser,
28540 "a brace-enclosed initializer is not allowed here");
28541 /* Consume the opening brace. */
28542 matching_braces braces;
28543 braces.consume_open (parser);
28544 /* Skip the initializer. */
28545 cp_parser_skip_to_closing_brace (parser);
28546 /* Look for the trailing `}'. */
28547 braces.require_close (parser);
28549 return error_mark_node;
28552 return cp_parser_constant_expression (parser);
28555 /* Derived classes [gram.class.derived] */
28557 /* Parse a base-clause.
28559 base-clause:
28560 : base-specifier-list
28562 base-specifier-list:
28563 base-specifier ... [opt]
28564 base-specifier-list , base-specifier ... [opt]
28566 Returns a TREE_LIST representing the base-classes, in the order in
28567 which they were declared. The representation of each node is as
28568 described by cp_parser_base_specifier.
28570 In the case that no bases are specified, this function will return
28571 NULL_TREE, not ERROR_MARK_NODE. */
28573 static tree
28574 cp_parser_base_clause (cp_parser* parser)
28576 tree bases = NULL_TREE;
28578 /* Look for the `:' that begins the list. */
28579 cp_parser_require (parser, CPP_COLON, RT_COLON);
28581 /* Scan the base-specifier-list. */
28582 while (true)
28584 cp_token *token;
28585 tree base;
28586 bool pack_expansion_p = false;
28588 /* Look for the base-specifier. */
28589 base = cp_parser_base_specifier (parser);
28590 /* Look for the (optional) ellipsis. */
28591 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28593 /* Consume the `...'. */
28594 cp_lexer_consume_token (parser->lexer);
28596 pack_expansion_p = true;
28599 /* Add BASE to the front of the list. */
28600 if (base && base != error_mark_node)
28602 if (pack_expansion_p)
28603 /* Make this a pack expansion type. */
28604 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28606 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28608 TREE_CHAIN (base) = bases;
28609 bases = base;
28612 /* Peek at the next token. */
28613 token = cp_lexer_peek_token (parser->lexer);
28614 /* If it's not a comma, then the list is complete. */
28615 if (token->type != CPP_COMMA)
28616 break;
28617 /* Consume the `,'. */
28618 cp_lexer_consume_token (parser->lexer);
28621 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28622 base class had a qualified name. However, the next name that
28623 appears is certainly not qualified. */
28624 parser->scope = NULL_TREE;
28625 parser->qualifying_scope = NULL_TREE;
28626 parser->object_scope = NULL_TREE;
28628 return nreverse (bases);
28631 /* Parse a base-specifier.
28633 base-specifier:
28634 :: [opt] nested-name-specifier [opt] class-name
28635 virtual access-specifier [opt] :: [opt] nested-name-specifier
28636 [opt] class-name
28637 access-specifier virtual [opt] :: [opt] nested-name-specifier
28638 [opt] class-name
28640 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28641 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28642 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28643 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28645 static tree
28646 cp_parser_base_specifier (cp_parser* parser)
28648 cp_token *token;
28649 bool done = false;
28650 bool virtual_p = false;
28651 bool duplicate_virtual_error_issued_p = false;
28652 bool duplicate_access_error_issued_p = false;
28653 bool class_scope_p, template_p;
28654 tree access = access_default_node;
28655 tree type;
28657 /* Process the optional `virtual' and `access-specifier'. */
28658 while (!done)
28660 /* Peek at the next token. */
28661 token = cp_lexer_peek_token (parser->lexer);
28662 /* Process `virtual'. */
28663 switch (token->keyword)
28665 case RID_VIRTUAL:
28666 /* If `virtual' appears more than once, issue an error. */
28667 if (virtual_p && !duplicate_virtual_error_issued_p)
28669 cp_parser_error (parser,
28670 "%<virtual%> specified more than once in base-specifier");
28671 duplicate_virtual_error_issued_p = true;
28674 virtual_p = true;
28676 /* Consume the `virtual' token. */
28677 cp_lexer_consume_token (parser->lexer);
28679 break;
28681 case RID_PUBLIC:
28682 case RID_PROTECTED:
28683 case RID_PRIVATE:
28684 /* If more than one access specifier appears, issue an
28685 error. */
28686 if (access != access_default_node
28687 && !duplicate_access_error_issued_p)
28689 cp_parser_error (parser,
28690 "more than one access specifier in base-specifier");
28691 duplicate_access_error_issued_p = true;
28694 access = ridpointers[(int) token->keyword];
28696 /* Consume the access-specifier. */
28697 cp_lexer_consume_token (parser->lexer);
28699 break;
28701 default:
28702 done = true;
28703 break;
28706 /* It is not uncommon to see programs mechanically, erroneously, use
28707 the 'typename' keyword to denote (dependent) qualified types
28708 as base classes. */
28709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28711 token = cp_lexer_peek_token (parser->lexer);
28712 if (!processing_template_decl)
28713 error_at (token->location,
28714 "keyword %<typename%> not allowed outside of templates");
28715 else
28716 error_at (token->location,
28717 "keyword %<typename%> not allowed in this context "
28718 "(the base class is implicitly a type)");
28719 cp_lexer_consume_token (parser->lexer);
28722 /* Look for the optional `::' operator. */
28723 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28724 /* Look for the nested-name-specifier. The simplest way to
28725 implement:
28727 [temp.res]
28729 The keyword `typename' is not permitted in a base-specifier or
28730 mem-initializer; in these contexts a qualified name that
28731 depends on a template-parameter is implicitly assumed to be a
28732 type name.
28734 is to pretend that we have seen the `typename' keyword at this
28735 point. */
28736 cp_parser_nested_name_specifier_opt (parser,
28737 /*typename_keyword_p=*/true,
28738 /*check_dependency_p=*/true,
28739 /*type_p=*/true,
28740 /*is_declaration=*/true);
28741 /* If the base class is given by a qualified name, assume that names
28742 we see are type names or templates, as appropriate. */
28743 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28744 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28746 if (!parser->scope
28747 && cp_lexer_next_token_is_decltype (parser->lexer))
28748 /* DR 950 allows decltype as a base-specifier. */
28749 type = cp_parser_decltype (parser);
28750 else
28752 /* Otherwise, look for the class-name. */
28753 type = cp_parser_class_name (parser,
28754 class_scope_p,
28755 template_p,
28756 typename_type,
28757 /*check_dependency_p=*/true,
28758 /*class_head_p=*/false,
28759 /*is_declaration=*/true);
28760 type = TREE_TYPE (type);
28763 if (type == error_mark_node)
28764 return error_mark_node;
28766 return finish_base_specifier (type, access, virtual_p);
28769 /* Exception handling [gram.exception] */
28771 /* Save the tokens that make up the noexcept-specifier for a member-function.
28772 Returns a DEFERRED_PARSE. */
28774 static tree
28775 cp_parser_save_noexcept (cp_parser *parser)
28777 cp_token *first = parser->lexer->next_token;
28778 /* We want everything up to, including, the final ')'. */
28779 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28780 cp_token *last = parser->lexer->next_token;
28782 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28783 to carry the information we will need. */
28784 tree expr = make_node (DEFERRED_PARSE);
28785 /* Save away the noexcept-specifier; we will process it when the
28786 class is complete. */
28787 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28788 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28789 expr = build_tree_list (expr, NULL_TREE);
28790 return expr;
28793 /* Used for late processing of noexcept-specifiers of member-functions.
28794 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28795 we saved for later; parse it now. DECL is the declaration of the
28796 member function. */
28798 static tree
28799 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28801 /* Make sure we've gotten something that hasn't been parsed yet. */
28802 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28804 push_unparsed_function_queues (parser);
28806 /* Push the saved tokens for the noexcept-specifier onto the parser's
28807 lexer stack. */
28808 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28809 cp_parser_push_lexer_for_tokens (parser, tokens);
28811 /* Parse the cached noexcept-specifier. */
28812 tree parsed_arg
28813 = cp_parser_noexcept_specification_opt (parser,
28814 CP_PARSER_FLAGS_NONE,
28815 /*require_constexpr=*/true,
28816 /*consumed_expr=*/NULL,
28817 /*return_cond=*/false);
28819 /* Revert to the main lexer. */
28820 cp_parser_pop_lexer (parser);
28822 /* Restore the queue. */
28823 pop_unparsed_function_queues (parser);
28825 /* And we're done. */
28826 return parsed_arg;
28829 /* Perform late checking of overriding function with respect to their
28830 noexcept-specifiers. FNDECL is the member function that potentially
28831 overrides some virtual function with the same signature. */
28833 static void
28834 noexcept_override_late_checks (tree fndecl)
28836 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28837 tree base_binfo;
28839 if (DECL_STATIC_FUNCTION_P (fndecl))
28840 return;
28842 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28844 tree basetype = BINFO_TYPE (base_binfo);
28846 if (!TYPE_POLYMORPHIC_P (basetype))
28847 continue;
28849 tree fn = look_for_overrides_here (basetype, fndecl);
28850 if (fn)
28851 maybe_check_overriding_exception_spec (fndecl, fn);
28855 /* Parse an (optional) noexcept-specification.
28857 noexcept-specification:
28858 noexcept ( constant-expression ) [opt]
28860 If no noexcept-specification is present, returns NULL_TREE.
28861 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28862 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28863 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28864 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28865 in which case a boolean condition is returned instead. The parser flags
28866 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28867 the (member) function is `const'. */
28869 static tree
28870 cp_parser_noexcept_specification_opt (cp_parser* parser,
28871 cp_parser_flags flags,
28872 bool require_constexpr,
28873 bool* consumed_expr,
28874 bool return_cond)
28876 cp_token *token;
28877 const char *saved_message;
28879 /* Peek at the next token. */
28880 token = cp_lexer_peek_token (parser->lexer);
28882 /* Is it a noexcept-specification? */
28883 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28885 tree expr;
28887 /* [class.mem]/6 says that a noexcept-specifer (within the
28888 member-specification of the class) is a complete-class context of
28889 a class. So, if the noexcept-specifier has the optional expression,
28890 just save the tokens, and reparse this after we're done with the
28891 class. */
28893 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28894 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28895 /* No need to delay parsing for a number literal or true/false. */
28896 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28897 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28898 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28899 && at_class_scope_p ()
28900 && TYPE_BEING_DEFINED (current_class_type)
28901 && !LAMBDA_TYPE_P (current_class_type))
28902 return cp_parser_save_noexcept (parser);
28904 cp_lexer_consume_token (parser->lexer);
28906 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28908 matching_parens parens;
28909 parens.consume_open (parser);
28911 if (require_constexpr)
28913 /* Types may not be defined in an exception-specification. */
28914 saved_message = parser->type_definition_forbidden_message;
28915 parser->type_definition_forbidden_message
28916 = G_("types may not be defined in an exception-specification");
28918 bool non_constant_p;
28919 expr
28920 = cp_parser_constant_expression (parser,
28921 /*allow_non_constant=*/true,
28922 &non_constant_p);
28923 if (non_constant_p
28924 && !require_potential_rvalue_constant_expression (expr))
28926 expr = NULL_TREE;
28927 return_cond = true;
28930 /* Restore the saved message. */
28931 parser->type_definition_forbidden_message = saved_message;
28933 else
28935 expr = cp_parser_expression (parser);
28936 *consumed_expr = true;
28939 parens.require_close (parser);
28941 else
28943 expr = boolean_true_node;
28944 if (!require_constexpr)
28945 *consumed_expr = false;
28948 /* We cannot build a noexcept-spec right away because this will check
28949 that expr is a constexpr. */
28950 if (!return_cond)
28951 return build_noexcept_spec (expr, tf_warning_or_error);
28952 else
28953 return expr;
28955 else
28956 return NULL_TREE;
28959 /* Parse an (optional) exception-specification.
28961 exception-specification:
28962 throw ( type-id-list [opt] )
28964 Returns a TREE_LIST representing the exception-specification. The
28965 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28966 control parsing. QUALS are qualifiers indicating whether the (member)
28967 function is `const'. */
28969 static tree
28970 cp_parser_exception_specification_opt (cp_parser* parser,
28971 cp_parser_flags flags)
28973 cp_token *token;
28974 tree type_id_list;
28975 const char *saved_message;
28977 /* Peek at the next token. */
28978 token = cp_lexer_peek_token (parser->lexer);
28980 /* Is it a noexcept-specification? */
28981 type_id_list
28982 = cp_parser_noexcept_specification_opt (parser, flags,
28983 /*require_constexpr=*/true,
28984 /*consumed_expr=*/NULL,
28985 /*return_cond=*/false);
28986 if (type_id_list != NULL_TREE)
28987 return type_id_list;
28989 /* If it's not `throw', then there's no exception-specification. */
28990 if (!cp_parser_is_keyword (token, RID_THROW))
28991 return NULL_TREE;
28993 location_t loc = token->location;
28995 /* Consume the `throw'. */
28996 cp_lexer_consume_token (parser->lexer);
28998 /* Look for the `('. */
28999 matching_parens parens;
29000 parens.require_open (parser);
29002 /* Peek at the next token. */
29003 token = cp_lexer_peek_token (parser->lexer);
29004 /* If it's not a `)', then there is a type-id-list. */
29005 if (token->type != CPP_CLOSE_PAREN)
29007 /* Types may not be defined in an exception-specification. */
29008 saved_message = parser->type_definition_forbidden_message;
29009 parser->type_definition_forbidden_message
29010 = G_("types may not be defined in an exception-specification");
29011 /* Parse the type-id-list. */
29012 type_id_list = cp_parser_type_id_list (parser);
29013 /* Restore the saved message. */
29014 parser->type_definition_forbidden_message = saved_message;
29016 if (cxx_dialect >= cxx17)
29018 error_at (loc, "ISO C++17 does not allow dynamic exception "
29019 "specifications");
29020 type_id_list = NULL_TREE;
29022 else if (cxx_dialect >= cxx11)
29023 warning_at (loc, OPT_Wdeprecated,
29024 "dynamic exception specifications are deprecated in "
29025 "C++11");
29027 /* In C++17, throw() is equivalent to noexcept (true). throw()
29028 is deprecated in C++11 and above as well, but is still widely used,
29029 so don't warn about it yet. */
29030 else if (cxx_dialect >= cxx17)
29031 type_id_list = noexcept_true_spec;
29032 else
29033 type_id_list = empty_except_spec;
29035 /* Look for the `)'. */
29036 parens.require_close (parser);
29038 return type_id_list;
29041 /* Parse an (optional) type-id-list.
29043 type-id-list:
29044 type-id ... [opt]
29045 type-id-list , type-id ... [opt]
29047 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
29048 in the order that the types were presented. */
29050 static tree
29051 cp_parser_type_id_list (cp_parser* parser)
29053 tree types = NULL_TREE;
29055 while (true)
29057 cp_token *token;
29058 tree type;
29060 token = cp_lexer_peek_token (parser->lexer);
29062 /* Get the next type-id. */
29063 type = cp_parser_type_id (parser);
29064 /* Check for invalid 'auto'. */
29065 if (flag_concepts && type_uses_auto (type))
29067 error_at (token->location,
29068 "invalid use of %<auto%> in exception-specification");
29069 type = error_mark_node;
29071 /* Parse the optional ellipsis. */
29072 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29074 /* Consume the `...'. */
29075 cp_lexer_consume_token (parser->lexer);
29077 /* Turn the type into a pack expansion expression. */
29078 type = make_pack_expansion (type);
29080 /* Add it to the list. */
29081 types = add_exception_specifier (types, type, /*complain=*/1);
29082 /* Peek at the next token. */
29083 token = cp_lexer_peek_token (parser->lexer);
29084 /* If it is not a `,', we are done. */
29085 if (token->type != CPP_COMMA)
29086 break;
29087 /* Consume the `,'. */
29088 cp_lexer_consume_token (parser->lexer);
29091 return nreverse (types);
29094 /* Parse a try-block.
29096 try-block:
29097 try compound-statement handler-seq */
29099 static tree
29100 cp_parser_try_block (cp_parser* parser)
29102 tree try_block;
29104 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
29105 if (parser->in_function_body
29106 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
29107 && cxx_dialect < cxx20)
29108 pedwarn (input_location, OPT_Wc__20_extensions,
29109 "%<try%> in %<constexpr%> function only "
29110 "available with %<-std=c++20%> or %<-std=gnu++20%>");
29112 try_block = begin_try_block ();
29113 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
29114 finish_try_block (try_block);
29115 cp_parser_handler_seq (parser);
29116 finish_handler_sequence (try_block);
29118 return try_block;
29121 /* Parse a function-try-block.
29123 function-try-block:
29124 try ctor-initializer [opt] function-body handler-seq */
29126 static void
29127 cp_parser_function_try_block (cp_parser* parser)
29129 tree compound_stmt;
29130 tree try_block;
29132 /* Look for the `try' keyword. */
29133 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
29134 return;
29135 /* Let the rest of the front end know where we are. */
29136 try_block = begin_function_try_block (&compound_stmt);
29137 /* Parse the function-body. */
29138 cp_parser_ctor_initializer_opt_and_function_body
29139 (parser, /*in_function_try_block=*/true);
29140 /* We're done with the `try' part. */
29141 finish_function_try_block (try_block);
29142 /* Parse the handlers. */
29143 cp_parser_handler_seq (parser);
29144 /* We're done with the handlers. */
29145 finish_function_handler_sequence (try_block, compound_stmt);
29148 /* Parse a handler-seq.
29150 handler-seq:
29151 handler handler-seq [opt] */
29153 static void
29154 cp_parser_handler_seq (cp_parser* parser)
29156 while (true)
29158 cp_token *token;
29160 /* Parse the handler. */
29161 cp_parser_handler (parser);
29162 /* Peek at the next token. */
29163 token = cp_lexer_peek_token (parser->lexer);
29164 /* If it's not `catch' then there are no more handlers. */
29165 if (!cp_parser_is_keyword (token, RID_CATCH))
29166 break;
29170 /* Parse a handler.
29172 handler:
29173 catch ( exception-declaration ) compound-statement */
29175 static void
29176 cp_parser_handler (cp_parser* parser)
29178 tree handler;
29179 tree declaration;
29181 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
29182 handler = begin_handler ();
29183 matching_parens parens;
29184 parens.require_open (parser);
29185 declaration = cp_parser_exception_declaration (parser);
29186 finish_handler_parms (declaration, handler);
29187 parens.require_close (parser);
29188 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29189 finish_handler (handler);
29192 /* Parse an exception-declaration.
29194 exception-declaration:
29195 type-specifier-seq declarator
29196 type-specifier-seq abstract-declarator
29197 type-specifier-seq
29200 Returns a VAR_DECL for the declaration, or NULL_TREE if the
29201 ellipsis variant is used. */
29203 static tree
29204 cp_parser_exception_declaration (cp_parser* parser)
29206 cp_decl_specifier_seq type_specifiers;
29207 cp_declarator *declarator;
29208 const char *saved_message;
29210 /* If it's an ellipsis, it's easy to handle. */
29211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29213 /* Consume the `...' token. */
29214 cp_lexer_consume_token (parser->lexer);
29215 return NULL_TREE;
29218 /* Types may not be defined in exception-declarations. */
29219 saved_message = parser->type_definition_forbidden_message;
29220 parser->type_definition_forbidden_message
29221 = G_("types may not be defined in exception-declarations");
29223 /* Parse the type-specifier-seq. */
29224 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
29225 /*is_declaration=*/true,
29226 /*is_trailing_return=*/false,
29227 &type_specifiers);
29228 /* If it's a `)', then there is no declarator. */
29229 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29230 declarator = NULL;
29231 else
29232 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
29233 CP_PARSER_FLAGS_NONE,
29234 /*ctor_dtor_or_conv_p=*/NULL,
29235 /*parenthesized_p=*/NULL,
29236 /*member_p=*/false,
29237 /*friend_p=*/false,
29238 /*static_p=*/false);
29240 /* Restore the saved message. */
29241 parser->type_definition_forbidden_message = saved_message;
29243 if (!type_specifiers.any_specifiers_p)
29244 return error_mark_node;
29246 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
29249 /* Parse a throw-expression.
29251 throw-expression:
29252 throw assignment-expression [opt]
29254 Returns a THROW_EXPR representing the throw-expression. */
29256 static tree
29257 cp_parser_throw_expression (cp_parser* parser)
29259 tree expression;
29260 cp_token* token;
29261 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29263 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
29264 token = cp_lexer_peek_token (parser->lexer);
29265 /* Figure out whether or not there is an assignment-expression
29266 following the "throw" keyword. */
29267 if (token->type == CPP_COMMA
29268 || token->type == CPP_SEMICOLON
29269 || token->type == CPP_CLOSE_PAREN
29270 || token->type == CPP_CLOSE_SQUARE
29271 || token->type == CPP_CLOSE_BRACE
29272 || token->type == CPP_COLON)
29273 expression = NULL_TREE;
29274 else
29275 expression = cp_parser_assignment_expression (parser);
29277 /* Construct a location e.g.:
29278 throw x
29279 ^~~~~~~
29280 with caret == start at the start of the "throw" token, and
29281 the end at the end of the final token we consumed. */
29282 location_t combined_loc = make_location (start_loc, start_loc,
29283 parser->lexer);
29284 expression = build_throw (combined_loc, expression);
29286 return expression;
29289 /* Parse a yield-expression.
29291 yield-expression:
29292 co_yield assignment-expression
29293 co_yield braced-init-list
29295 Returns a CO_YIELD_EXPR representing the yield-expression. */
29297 static tree
29298 cp_parser_yield_expression (cp_parser* parser)
29300 tree expr;
29302 cp_token *token = cp_lexer_peek_token (parser->lexer);
29303 location_t kw_loc = token->location; /* Save for later. */
29305 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
29307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29309 cp_lexer_set_source_position (parser->lexer);
29310 /* ??? : probably a moot point? */
29311 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29312 expr = cp_parser_braced_list (parser);
29314 else
29315 expr = cp_parser_assignment_expression (parser);
29317 if (expr == error_mark_node)
29318 return expr;
29320 return finish_co_yield_expr (kw_loc, expr);
29323 /* GNU Extensions */
29325 /* Parse an (optional) asm-specification.
29327 asm-specification:
29328 asm ( string-literal )
29330 If the asm-specification is present, returns a STRING_CST
29331 corresponding to the string-literal. Otherwise, returns
29332 NULL_TREE. */
29334 static tree
29335 cp_parser_asm_specification_opt (cp_parser* parser)
29337 /* Peek at the next token. */
29338 cp_token *token = cp_lexer_peek_token (parser->lexer);
29339 /* If the next token isn't the `asm' keyword, then there's no
29340 asm-specification. */
29341 if (!cp_parser_is_keyword (token, RID_ASM))
29342 return NULL_TREE;
29344 /* Consume the `asm' token. */
29345 cp_lexer_consume_token (parser->lexer);
29346 /* Look for the `('. */
29347 matching_parens parens;
29348 parens.require_open (parser);
29350 /* Look for the string-literal. */
29351 tree asm_specification = cp_parser_string_literal (parser,
29352 /*translate=*/false,
29353 /*wide_ok=*/false);
29355 /* Look for the `)'. */
29356 parens.require_close (parser);
29358 return asm_specification;
29361 /* Parse an asm-operand-list.
29363 asm-operand-list:
29364 asm-operand
29365 asm-operand-list , asm-operand
29367 asm-operand:
29368 string-literal ( expression )
29369 [ string-literal ] string-literal ( expression )
29371 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29372 each node is the expression. The TREE_PURPOSE is itself a
29373 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29374 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29375 is a STRING_CST for the string literal before the parenthesis. Returns
29376 ERROR_MARK_NODE if any of the operands are invalid. */
29378 static tree
29379 cp_parser_asm_operand_list (cp_parser* parser)
29381 tree asm_operands = NULL_TREE;
29382 bool invalid_operands = false;
29384 while (true)
29386 tree name;
29388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29390 /* Consume the `[' token. */
29391 cp_lexer_consume_token (parser->lexer);
29392 /* Read the operand name. */
29393 name = cp_parser_identifier (parser);
29394 if (name != error_mark_node)
29395 name = build_string (IDENTIFIER_LENGTH (name),
29396 IDENTIFIER_POINTER (name));
29397 /* Look for the closing `]'. */
29398 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29400 else
29401 name = NULL_TREE;
29402 /* Look for the string-literal. */
29403 tree string_literal = cp_parser_string_literal (parser,
29404 /*translate=*/false,
29405 /*wide_ok=*/false);
29407 /* Look for the `('. */
29408 matching_parens parens;
29409 parens.require_open (parser);
29410 /* Parse the expression. */
29411 tree expression = cp_parser_expression (parser);
29412 /* Look for the `)'. */
29413 parens.require_close (parser);
29415 if (name == error_mark_node
29416 || string_literal == error_mark_node
29417 || expression == error_mark_node)
29418 invalid_operands = true;
29420 /* Add this operand to the list. */
29421 asm_operands = tree_cons (build_tree_list (name, string_literal),
29422 expression,
29423 asm_operands);
29424 /* If the next token is not a `,', there are no more
29425 operands. */
29426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29427 break;
29428 /* Consume the `,'. */
29429 cp_lexer_consume_token (parser->lexer);
29432 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29435 /* Parse an asm-clobber-list.
29437 asm-clobber-list:
29438 string-literal
29439 asm-clobber-list , string-literal
29441 Returns a TREE_LIST, indicating the clobbers in the order that they
29442 appeared. The TREE_VALUE of each node is a STRING_CST. */
29444 static tree
29445 cp_parser_asm_clobber_list (cp_parser* parser)
29447 tree clobbers = NULL_TREE;
29449 while (true)
29451 /* Look for the string literal. */
29452 tree string_literal = cp_parser_string_literal (parser,
29453 /*translate=*/false,
29454 /*wide_ok=*/false);
29455 /* Add it to the list. */
29456 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29457 /* If the next token is not a `,', then the list is
29458 complete. */
29459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29460 break;
29461 /* Consume the `,' token. */
29462 cp_lexer_consume_token (parser->lexer);
29465 return clobbers;
29468 /* Parse an asm-label-list.
29470 asm-label-list:
29471 identifier
29472 asm-label-list , identifier
29474 Returns a TREE_LIST, indicating the labels in the order that they
29475 appeared. The TREE_VALUE of each node is a label. */
29477 static tree
29478 cp_parser_asm_label_list (cp_parser* parser)
29480 tree labels = NULL_TREE;
29482 while (true)
29484 tree identifier, label, name;
29486 /* Look for the identifier. */
29487 identifier = cp_parser_identifier (parser);
29488 if (!error_operand_p (identifier))
29490 label = lookup_label (identifier);
29491 if (TREE_CODE (label) == LABEL_DECL)
29493 TREE_USED (label) = 1;
29494 check_goto (label);
29495 name = build_string (IDENTIFIER_LENGTH (identifier),
29496 IDENTIFIER_POINTER (identifier));
29497 labels = tree_cons (name, label, labels);
29500 /* If the next token is not a `,', then the list is
29501 complete. */
29502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29503 break;
29504 /* Consume the `,' token. */
29505 cp_lexer_consume_token (parser->lexer);
29508 return nreverse (labels);
29511 /* Return TRUE iff the next tokens in the stream are possibly the
29512 beginning of a GNU extension attribute. */
29514 static bool
29515 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29517 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29520 /* Return TRUE iff the next tokens in the stream are possibly the
29521 beginning of a standard C++-11 attribute specifier. */
29523 static bool
29524 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29526 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29529 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29530 beginning of a standard C++-11 attribute specifier. */
29532 static bool
29533 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29535 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29537 return (cxx_dialect >= cxx11
29538 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29539 || (token->type == CPP_OPEN_SQUARE
29540 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29541 && token->type == CPP_OPEN_SQUARE)));
29544 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29545 beginning of a GNU extension attribute. */
29547 static bool
29548 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29550 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29552 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29555 /* Return true iff the next tokens can be the beginning of either a
29556 GNU attribute list, or a standard C++11 attribute sequence. */
29558 static bool
29559 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29561 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29562 || cp_next_tokens_can_be_std_attribute_p (parser));
29565 /* Return true iff the next Nth tokens can be the beginning of either
29566 a GNU attribute list, or a standard C++11 attribute sequence. */
29568 static bool
29569 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29571 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29572 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29575 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29576 of GNU attributes, or return NULL. */
29578 static tree
29579 cp_parser_attributes_opt (cp_parser *parser)
29581 tree attrs = NULL_TREE;
29582 while (true)
29584 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29585 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29586 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29587 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29588 else
29589 break;
29591 return attrs;
29594 /* Parse an (optional) series of attributes.
29596 attributes:
29597 attributes attribute
29599 attribute:
29600 __attribute__ (( attribute-list [opt] ))
29602 The return value is as for cp_parser_gnu_attribute_list. */
29604 static tree
29605 cp_parser_gnu_attributes_opt (cp_parser* parser)
29607 tree attributes = NULL_TREE;
29609 auto cleanup = make_temp_override
29610 (parser->auto_is_implicit_function_template_parm_p, false);
29612 while (true)
29614 cp_token *token;
29615 tree attribute_list;
29616 bool ok = true;
29618 /* Peek at the next token. */
29619 token = cp_lexer_peek_token (parser->lexer);
29620 /* If it's not `__attribute__', then we're done. */
29621 if (token->keyword != RID_ATTRIBUTE)
29622 break;
29624 /* Consume the `__attribute__' keyword. */
29625 cp_lexer_consume_token (parser->lexer);
29626 /* Look for the two `(' tokens. */
29627 matching_parens outer_parens;
29628 if (!outer_parens.require_open (parser))
29629 ok = false;
29630 matching_parens inner_parens;
29631 if (!inner_parens.require_open (parser))
29632 ok = false;
29634 /* Peek at the next token. */
29635 token = cp_lexer_peek_token (parser->lexer);
29636 if (token->type != CPP_CLOSE_PAREN)
29637 /* Parse the attribute-list. */
29638 attribute_list = cp_parser_gnu_attribute_list (parser);
29639 else
29640 /* If the next token is a `)', then there is no attribute
29641 list. */
29642 attribute_list = NULL;
29644 /* Look for the two `)' tokens. */
29645 if (!inner_parens.require_close (parser))
29646 ok = false;
29647 if (!outer_parens.require_close (parser))
29648 ok = false;
29649 if (!ok)
29650 cp_parser_skip_to_end_of_statement (parser);
29652 /* Add these new attributes to the list. */
29653 attributes = attr_chainon (attributes, attribute_list);
29656 return attributes;
29659 /* Parse a GNU attribute-list.
29661 attribute-list:
29662 attribute
29663 attribute-list , attribute
29665 attribute:
29666 identifier
29667 identifier ( identifier )
29668 identifier ( identifier , expression-list )
29669 identifier ( expression-list )
29671 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29672 to an attribute. The TREE_PURPOSE of each node is the identifier
29673 indicating which attribute is in use. The TREE_VALUE represents
29674 the arguments, if any. */
29676 static tree
29677 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29679 tree attribute_list = NULL_TREE;
29680 bool save_translate_strings_p = parser->translate_strings_p;
29682 /* Don't create wrapper nodes within attributes: the
29683 handlers don't know how to handle them. */
29684 auto_suppress_location_wrappers sentinel;
29686 parser->translate_strings_p = false;
29687 while (true)
29689 cp_token *token;
29690 tree identifier;
29691 tree attribute;
29693 /* Look for the identifier. We also allow keywords here; for
29694 example `__attribute__ ((const))' is legal. */
29695 token = cp_lexer_peek_token (parser->lexer);
29696 if (token->type == CPP_NAME
29697 || token->type == CPP_KEYWORD)
29699 tree arguments = NULL_TREE;
29701 /* Consume the token, but save it since we need it for the
29702 SIMD enabled function parsing. */
29703 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29705 /* Save away the identifier that indicates which attribute
29706 this is. */
29707 identifier = (token->type == CPP_KEYWORD)
29708 /* For keywords, use the canonical spelling, not the
29709 parsed identifier. */
29710 ? ridpointers[(int) token->keyword]
29711 : id_token->u.value;
29713 identifier = canonicalize_attr_name (identifier);
29714 attribute = build_tree_list (identifier, NULL_TREE);
29716 /* Peek at the next token. */
29717 token = cp_lexer_peek_token (parser->lexer);
29718 /* If it's an `(', then parse the attribute arguments. */
29719 if (token->type == CPP_OPEN_PAREN)
29721 vec<tree, va_gc> *vec;
29722 int attr_flag = (attribute_takes_identifier_p (identifier)
29723 ? id_attr : normal_attr);
29724 if (is_attribute_p ("assume", identifier))
29725 attr_flag = assume_attr;
29726 vec = cp_parser_parenthesized_expression_list
29727 (parser, attr_flag, /*cast_p=*/false,
29728 /*allow_expansion_p=*/false,
29729 /*non_constant_p=*/NULL);
29730 if (vec == NULL)
29731 arguments = error_mark_node;
29732 else
29734 arguments = build_tree_list_vec (vec);
29735 release_tree_vector (vec);
29737 /* Save the arguments away. */
29738 TREE_VALUE (attribute) = arguments;
29741 if (arguments != error_mark_node)
29743 /* Add this attribute to the list. */
29744 TREE_CHAIN (attribute) = attribute_list;
29745 attribute_list = attribute;
29748 token = cp_lexer_peek_token (parser->lexer);
29750 /* Unless EXACTLY_ONE is set look for more attributes.
29751 If the next token isn't a `,', we're done. */
29752 if (exactly_one || token->type != CPP_COMMA)
29753 break;
29755 /* Consume the comma and keep going. */
29756 cp_lexer_consume_token (parser->lexer);
29758 parser->translate_strings_p = save_translate_strings_p;
29760 /* We built up the list in reverse order. */
29761 return nreverse (attribute_list);
29764 /* Parse arguments of omp::directive attribute.
29766 ( directive-name ,[opt] clause-list[opt] )
29768 For directive just remember the first/last tokens for subsequent
29769 parsing. */
29771 static void
29772 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
29774 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29775 if (first->type == CPP_CLOSE_PAREN)
29777 cp_lexer_consume_token (parser->lexer);
29778 error_at (first->location, "expected OpenMP directive name");
29779 cp_lexer_consume_token (parser->lexer);
29780 TREE_VALUE (attribute) = NULL_TREE;
29781 return;
29783 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29784 if (n == 1)
29786 cp_lexer_consume_token (parser->lexer);
29787 error_at (first->location, "expected attribute argument as balanced "
29788 "token sequence");
29789 TREE_VALUE (attribute) = NULL_TREE;
29790 return;
29792 for (n = n - 2; n; --n)
29793 cp_lexer_consume_token (parser->lexer);
29794 cp_token *last = cp_lexer_peek_token (parser->lexer);
29795 cp_lexer_consume_token (parser->lexer);
29796 tree arg = make_node (DEFERRED_PARSE);
29797 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29798 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29799 if (decl_p)
29800 TREE_PUBLIC (arg) = 1;
29801 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29804 /* Parse arguments of omp::sequence attribute.
29806 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29808 static void
29809 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29811 matching_parens parens;
29812 parens.consume_open (parser);
29815 cp_token *token = cp_lexer_peek_token (parser->lexer);
29816 if (token->type == CPP_NAME
29817 && token->u.value == omp_identifier
29818 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29820 cp_lexer_consume_token (parser->lexer);
29821 cp_lexer_consume_token (parser->lexer);
29822 token = cp_lexer_peek_token (parser->lexer);
29824 bool directive = false;
29825 const char *p;
29826 if (token->type != CPP_NAME)
29827 p = "";
29828 else
29829 p = IDENTIFIER_POINTER (token->u.value);
29830 if (strcmp (p, "directive") == 0)
29831 directive = true;
29832 else if (strcmp (p, "sequence") != 0)
29834 error_at (token->location, "expected %<directive%> or %<sequence%>");
29835 cp_parser_skip_to_closing_parenthesis (parser,
29836 /*recovering=*/true,
29837 /*or_comma=*/true,
29838 /*consume_paren=*/false);
29839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29840 break;
29841 cp_lexer_consume_token (parser->lexer);
29843 cp_lexer_consume_token (parser->lexer);
29844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29845 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29846 UNKNOWN_LOCATION);
29847 else if (directive)
29848 cp_parser_omp_directive_args (parser, attribute, false);
29849 else
29850 cp_parser_omp_sequence_args (parser, attribute);
29851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29852 break;
29853 cp_lexer_consume_token (parser->lexer);
29855 while (1);
29856 if (!parens.require_close (parser))
29857 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29858 /*consume_paren=*/true);
29861 /* Parse a standard C++11 attribute.
29863 The returned representation is a TREE_LIST which TREE_PURPOSE is
29864 the scoped name of the attribute, and the TREE_VALUE is its
29865 arguments list.
29867 Note that the scoped name of the attribute is itself a TREE_LIST
29868 which TREE_PURPOSE is the namespace of the attribute, and
29869 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29870 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29871 and which TREE_PURPOSE is directly the attribute name.
29873 Clients of the attribute code should use get_attribute_namespace
29874 and get_attribute_name to get the actual namespace and name of
29875 attributes, regardless of their being GNU or C++11 attributes.
29877 attribute:
29878 attribute-token attribute-argument-clause [opt]
29880 attribute-token:
29881 identifier
29882 attribute-scoped-token
29884 attribute-scoped-token:
29885 attribute-namespace :: identifier
29887 attribute-namespace:
29888 identifier
29890 attribute-argument-clause:
29891 ( balanced-token-seq )
29893 balanced-token-seq:
29894 balanced-token [opt]
29895 balanced-token-seq balanced-token
29897 balanced-token:
29898 ( balanced-token-seq )
29899 [ balanced-token-seq ]
29900 { balanced-token-seq }. */
29902 static tree
29903 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29905 tree attribute, attr_id = NULL_TREE, arguments;
29906 cp_token *token;
29908 auto cleanup = make_temp_override
29909 (parser->auto_is_implicit_function_template_parm_p, false);
29911 /* First, parse name of the attribute, a.k.a attribute-token. */
29913 token = cp_lexer_peek_token (parser->lexer);
29914 if (token->type == CPP_NAME)
29915 attr_id = token->u.value;
29916 else if (token->type == CPP_KEYWORD)
29917 attr_id = ridpointers[(int) token->keyword];
29918 else if (token->flags & NAMED_OP)
29919 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29921 if (attr_id == NULL_TREE)
29922 return NULL_TREE;
29924 cp_lexer_consume_token (parser->lexer);
29926 token = cp_lexer_peek_token (parser->lexer);
29927 if (token->type == CPP_SCOPE)
29929 /* We are seeing a scoped attribute token. */
29931 cp_lexer_consume_token (parser->lexer);
29932 if (attr_ns)
29933 error_at (token->location, "attribute using prefix used together "
29934 "with scoped attribute token");
29935 attr_ns = attr_id;
29937 token = cp_lexer_peek_token (parser->lexer);
29938 if (token->type == CPP_NAME)
29939 attr_id = token->u.value;
29940 else if (token->type == CPP_KEYWORD)
29941 attr_id = ridpointers[(int) token->keyword];
29942 else if (token->flags & NAMED_OP)
29943 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29944 else
29946 error_at (token->location,
29947 "expected an identifier for the attribute name");
29948 return error_mark_node;
29950 cp_lexer_consume_token (parser->lexer);
29952 attr_ns = canonicalize_attr_name (attr_ns);
29953 attr_id = canonicalize_attr_name (attr_id);
29954 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29955 NULL_TREE);
29956 token = cp_lexer_peek_token (parser->lexer);
29958 else if (attr_ns)
29960 attr_ns = canonicalize_attr_name (attr_ns);
29961 attr_id = canonicalize_attr_name (attr_id);
29962 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29963 NULL_TREE);
29965 else
29967 attr_id = canonicalize_attr_name (attr_id);
29968 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29969 NULL_TREE);
29971 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29972 but no longer: we have to be able to tell [[noreturn]] and
29973 __attribute__((noreturn)) apart. */
29974 /* C++14 deprecated attribute is equivalent to GNU's. */
29975 if (is_attribute_p ("deprecated", attr_id))
29976 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29977 /* C++17 fallthrough attribute is equivalent to GNU's. */
29978 else if (is_attribute_p ("fallthrough", attr_id))
29979 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29980 /* C++23 assume attribute is equivalent to GNU's. */
29981 else if (is_attribute_p ("assume", attr_id))
29982 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29983 /* Transactional Memory TS optimize_for_synchronized attribute is
29984 equivalent to GNU transaction_callable. */
29985 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29986 TREE_PURPOSE (attribute)
29987 = get_identifier ("transaction_callable");
29988 /* Transactional Memory attributes are GNU attributes. */
29989 else if (tm_attr_to_mask (attr_id))
29990 TREE_PURPOSE (attribute) = attr_id;
29993 /* Now parse the optional argument clause of the attribute. */
29995 if (token->type != CPP_OPEN_PAREN)
29997 if ((flag_openmp || flag_openmp_simd)
29998 && attr_ns == omp_identifier
29999 && (is_attribute_p ("directive", attr_id)
30000 || is_attribute_p ("sequence", attr_id)
30001 || is_attribute_p ("decl", attr_id)))
30003 error_at (token->location, "%<omp::%E%> attribute requires argument",
30004 attr_id);
30005 return NULL_TREE;
30007 return attribute;
30011 vec<tree, va_gc> *vec;
30012 int attr_flag = normal_attr;
30014 /* Maybe we don't expect to see any arguments for this attribute. */
30015 const attribute_spec *as
30016 = lookup_attribute_spec (TREE_PURPOSE (attribute));
30017 if (as && as->max_length == 0)
30019 error_at (token->location, "%qE attribute does not take any arguments",
30020 attr_id);
30021 cp_parser_skip_to_closing_parenthesis (parser,
30022 /*recovering=*/true,
30023 /*or_comma=*/false,
30024 /*consume_paren=*/true);
30025 return error_mark_node;
30028 if (is_attribute_p ("assume", attr_id)
30029 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
30030 /* The assume attribute needs special handling of the argument. */
30031 attr_flag = assume_attr;
30032 else if (attr_ns == gnu_identifier
30033 && attribute_takes_identifier_p (attr_id))
30034 /* A GNU attribute that takes an identifier in parameter. */
30035 attr_flag = id_attr;
30036 else if (attr_ns == NULL_TREE
30037 && cxx_dialect >= cxx26
30038 && (is_attribute_p ("deprecated", attr_id)
30039 || is_attribute_p ("nodiscard", attr_id)))
30040 attr_flag = uneval_string_attr;
30042 /* If this is a fake attribute created to handle -Wno-attributes,
30043 we must skip parsing the arguments. */
30044 if (as == NULL || attribute_ignored_p (as))
30046 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
30048 if (is_attribute_p ("directive", attr_id))
30050 cp_parser_omp_directive_args (parser, attribute, false);
30051 return attribute;
30053 else if (is_attribute_p ("decl", attr_id))
30055 TREE_VALUE (TREE_PURPOSE (attribute))
30056 = get_identifier ("directive");
30057 cp_parser_omp_directive_args (parser, attribute, true);
30058 return attribute;
30060 else if (is_attribute_p ("sequence", attr_id))
30062 TREE_VALUE (TREE_PURPOSE (attribute))
30063 = get_identifier ("directive");
30064 cp_parser_omp_sequence_args (parser, attribute);
30065 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
30066 return attribute;
30070 /* For unknown attributes, just skip balanced tokens instead of
30071 trying to parse the arguments. Set TREE_VALUE (attribute) to
30072 error_mark_node to distinguish skipped arguments from attributes
30073 with no arguments. */
30074 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
30075 cp_lexer_consume_token (parser->lexer);
30076 TREE_VALUE (attribute) = error_mark_node;
30077 return attribute;
30080 vec = cp_parser_parenthesized_expression_list
30081 (parser, attr_flag, /*cast_p=*/false,
30082 /*allow_expansion_p=*/true,
30083 /*non_constant_p=*/NULL);
30084 if (vec == NULL)
30085 arguments = error_mark_node;
30086 else
30088 if (vec->is_empty ())
30089 /* e.g. [[attr()]]. */
30090 error_at (token->location, "parentheses must be omitted if "
30091 "%qE attribute argument list is empty",
30092 attr_id);
30093 arguments = build_tree_list_vec (vec);
30094 release_tree_vector (vec);
30097 if (arguments == error_mark_node)
30098 attribute = error_mark_node;
30099 else
30100 TREE_VALUE (attribute) = arguments;
30103 return attribute;
30106 /* Warn if the attribute ATTRIBUTE appears more than once in the
30107 attribute-list ATTRIBUTES. This used to be enforced for certain
30108 attributes, but the restriction was removed in P2156.
30109 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
30110 found in ATTRIBUTES. */
30112 static bool
30113 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
30115 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
30116 "likely", "unlikely", "fallthrough",
30117 "no_unique_address", "carries_dependency" };
30118 if (attributes)
30119 for (const auto &a : alist)
30120 if (is_attribute_p (a, get_attribute_name (attribute))
30121 && is_attribute_namespace_p ("", attribute)
30122 && lookup_attribute ("", a, attributes))
30124 if (!from_macro_expansion_at (loc))
30125 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
30126 "multiple times", a);
30127 return false;
30129 return true;
30132 /* Parse a list of standard C++-11 attributes.
30134 attribute-list:
30135 attribute [opt]
30136 attribute-list , attribute[opt]
30137 attribute ...
30138 attribute-list , attribute ...
30141 static tree
30142 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
30144 tree attributes = NULL_TREE, attribute = NULL_TREE;
30145 cp_token *token = NULL;
30147 while (true)
30149 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30150 attribute = cp_parser_std_attribute (parser, attr_ns);
30151 if (attribute == error_mark_node)
30152 break;
30153 if (attribute != NULL_TREE)
30155 if (cp_parser_check_std_attribute (loc, attributes, attribute))
30157 TREE_CHAIN (attribute) = attributes;
30158 attributes = attribute;
30161 token = cp_lexer_peek_token (parser->lexer);
30162 if (token->type == CPP_ELLIPSIS)
30164 cp_lexer_consume_token (parser->lexer);
30165 if (attribute == NULL_TREE)
30166 error_at (token->location,
30167 "expected attribute before %<...%>");
30168 else if (TREE_VALUE (attribute) == NULL_TREE)
30170 error_at (token->location, "attribute with no arguments "
30171 "contains no parameter packs");
30172 return error_mark_node;
30174 else if (TREE_VALUE (attribute) != error_mark_node)
30176 tree pack = make_pack_expansion (TREE_VALUE (attribute));
30177 if (pack == error_mark_node)
30178 return error_mark_node;
30179 TREE_VALUE (attribute) = pack;
30181 token = cp_lexer_peek_token (parser->lexer);
30183 if (token->type != CPP_COMMA)
30184 break;
30185 cp_lexer_consume_token (parser->lexer);
30187 attributes = nreverse (attributes);
30188 return attributes;
30191 /* Optionally parse a C++20 contract role. A NULL return means that no
30192 contract role was specified.
30194 contract-role:
30195 % default
30196 % identifier
30198 If the identifier does not name a known contract role, it will
30199 be assumed to be default. Returns the identifier for the role
30200 token. */
30202 static tree
30203 cp_parser_contract_role (cp_parser *parser)
30205 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
30206 cp_lexer_consume_token (parser->lexer);
30208 cp_token *token = cp_lexer_peek_token (parser->lexer);
30209 tree role_id = NULL_TREE;
30210 if (token->type == CPP_NAME)
30211 role_id = token->u.value;
30212 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30213 role_id = get_identifier ("default");
30214 else
30216 error_at (token->location, "expected contract-role");
30217 return error_mark_node;
30219 cp_lexer_consume_token (parser->lexer);
30221 /* FIXME: Warn about invalid/unknown roles? */
30222 return role_id;
30225 /* Parse an optional contract mode.
30227 contract-mode:
30228 contract-semantic
30229 [contract-level] [contract-role]
30231 contract-semantic:
30232 check_never_continue
30233 check_maybe_continue
30234 check_always_continue
30236 contract-level:
30237 default
30238 audit
30239 axiom
30241 contract-role:
30242 default
30243 identifier
30245 This grammar is taken from P1332R0. During parsing, this sets options
30246 on the MODE object to determine the configuration of the contract.
30248 Returns a tree containing the identifiers used in the configuration.
30249 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
30250 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
30251 contract-role, if any. NULL_TREE is returned if no information is
30252 given (i.e., all defaults selected). */
30254 static tree
30255 cp_parser_contract_mode_opt (cp_parser *parser,
30256 bool postcondition_p)
30258 /* The mode is empty; the level and role are default. */
30259 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30260 return NULL_TREE;
30262 /* There is only a role; the level is default. */
30263 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30265 tree role_id = cp_parser_contract_role (parser);
30266 return build_tree_list (role_id, get_identifier ("default"));
30269 /* Otherwise, match semantic or level. */
30270 cp_token *token = cp_lexer_peek_token (parser->lexer);
30271 contract_level level = CONTRACT_INVALID;
30272 contract_semantic semantic = CCS_INVALID;
30273 tree config_id;
30274 if (token->type == CPP_NAME)
30276 config_id = token->u.value;
30278 /* Either a named level, a concrete semantic, or an identifier
30279 for a postcondition. */
30280 const char *ident = IDENTIFIER_POINTER (token->u.value);
30281 level = map_contract_level (ident);
30282 semantic = map_contract_semantic (ident);
30284 /* The identifier is the return value for a postcondition. */
30285 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
30286 && postcondition_p)
30287 return NULL_TREE;
30289 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30291 config_id = get_identifier ("default");
30292 level = CONTRACT_DEFAULT;
30294 else
30296 /* We got some other token other than a ':'. */
30297 error_at (token->location, "expected contract semantic or level");
30298 return NULL_TREE;
30301 /* Consume the literal semantic or level token. */
30302 cp_lexer_consume_token (parser->lexer);
30304 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
30306 error_at (token->location,
30307 "expected contract level: "
30308 "%<default%>, %<audit%>, or %<axiom%>");
30309 return NULL_TREE;
30312 /* We matched an explicit semantic. */
30313 if (semantic != CCS_INVALID)
30315 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30317 error ("invalid use of contract role for explicit semantic");
30318 cp_lexer_consume_token (parser->lexer);
30319 cp_lexer_consume_token (parser->lexer);
30321 return config_id;
30324 /* We matched a level, there may be a role; otherwise this is default. */
30325 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30327 tree role_id = cp_parser_contract_role (parser);
30328 return build_tree_list (role_id, config_id);
30331 return build_tree_list (NULL_TREE, config_id);
30334 static tree
30335 find_error (tree *tp, int *, void *)
30337 if (*tp == error_mark_node)
30338 return *tp;
30339 return NULL_TREE;
30342 static bool
30343 contains_error_p (tree t)
30345 return walk_tree (&t, find_error, NULL, NULL);
30348 /* Parse a standard C++20 contract attribute specifier.
30350 contract-attribute-specifier:
30351 [ [ assert contract-level [opt] : conditional-expression ] ]
30352 [ [ pre contract-level [opt] : conditional-expression ] ]
30353 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
30355 For free functions, we cannot determine the type of the postcondition
30356 identifier because the we haven't called grokdeclarator yet. In those
30357 cases we parse the postcondition as if the identifier was declared as
30358 'auto <identifier>'. We then instantiate the postcondition once the
30359 return type is known.
30361 For member functions, contracts are in the complete-class context, so the
30362 parse is deferred. We also have the return type avaialable (unless it's
30363 deduced), so we don't need to parse the postcondition in terms of a
30364 placeholder. */
30366 static tree
30367 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30369 gcc_assert (contract_attribute_p (attribute));
30370 cp_token *token = cp_lexer_consume_token (parser->lexer);
30371 location_t loc = token->location;
30373 bool assertion_p = is_attribute_p ("assert", attribute);
30374 bool postcondition_p = is_attribute_p ("post", attribute);
30376 /* Parse the optional mode. */
30377 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30379 /* Check for postcondition identifiers. */
30380 cp_expr identifier;
30381 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30382 identifier = cp_parser_identifier (parser);
30383 if (identifier == error_mark_node)
30384 return error_mark_node;
30386 cp_parser_require (parser, CPP_COLON, RT_COLON);
30388 /* Defer the parsing of pre/post contracts inside class definitions. */
30389 tree contract;
30390 if (!assertion_p &&
30391 current_class_type &&
30392 TYPE_BEING_DEFINED (current_class_type))
30394 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30395 that doesn't close the attribute, return an error and let the attribute
30396 handling code emit an error for missing ']]'. */
30397 cp_token *first = cp_lexer_peek_token (parser->lexer);
30398 cp_parser_skip_to_closing_parenthesis_1 (parser,
30399 /*recovering=*/false,
30400 CPP_CLOSE_SQUARE,
30401 /*consume_paren=*/false);
30402 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30403 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30404 return error_mark_node;
30405 cp_token *last = cp_lexer_peek_token (parser->lexer);
30407 /* Build a deferred-parse node. */
30408 tree condition = make_node (DEFERRED_PARSE);
30409 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30410 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30412 /* And its corresponding contract. */
30413 contract = grok_contract (attribute, mode, identifier, condition, loc);
30415 else
30417 /* Enable location wrappers when parsing contracts. */
30418 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30420 /* Build a fake variable for the result identifier. */
30421 tree result = NULL_TREE;
30422 if (identifier)
30424 begin_scope (sk_block, NULL_TREE);
30425 result = make_postcondition_variable (identifier);
30426 ++processing_template_decl;
30429 /* Parse the condition, ensuring that parameters or the return variable
30430 aren't flagged for use outside the body of a function. */
30431 ++processing_contract_condition;
30432 cp_expr condition = cp_parser_conditional_expression (parser);
30433 --processing_contract_condition;
30435 /* Try to recover from errors by scanning up to the end of the
30436 attribute. Sometimes we get partially parsed expressions, so
30437 we need to search the condition for errors. */
30438 if (contains_error_p (condition))
30439 cp_parser_skip_up_to_closing_square_bracket (parser);
30441 /* Build the contract. */
30442 contract = grok_contract (attribute, mode, result, condition, loc);
30444 /* Leave our temporary scope for the postcondition result. */
30445 if (result)
30447 --processing_template_decl;
30448 pop_bindings_and_leave_scope ();
30452 if (!flag_contracts)
30454 error_at (loc, "contracts are only available with %<-fcontracts%>");
30455 return error_mark_node;
30458 return finish_contract_attribute (attribute, contract);
30461 /* Parse a contract condition for a deferred contract. */
30463 void cp_parser_late_contract_condition (cp_parser *parser,
30464 tree fn,
30465 tree attribute)
30467 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30469 /* Make sure we've gotten something that hasn't been parsed yet or that
30470 we're not parsing an invalid contract. */
30471 tree condition = CONTRACT_CONDITION (contract);
30472 if (TREE_CODE (condition) != DEFERRED_PARSE)
30473 return;
30475 tree identifier = NULL_TREE;
30476 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30477 identifier = POSTCONDITION_IDENTIFIER (contract);
30479 /* Build a fake variable for the result identifier. */
30480 tree result = NULL_TREE;
30481 if (identifier)
30483 /* TODO: Can we guarantee that the identifier has a location? */
30484 location_t loc = cp_expr_location (contract);
30485 tree type = TREE_TYPE (TREE_TYPE (fn));
30486 if (!check_postcondition_result (fn, type, loc))
30488 invalidate_contract (contract);
30489 return;
30492 begin_scope (sk_block, NULL_TREE);
30493 result = make_postcondition_variable (identifier, type);
30494 ++processing_template_decl;
30497 /* 'this' is not allowed in preconditions of constructors or in postconditions
30498 of destructors. Note that the previous value of this variable is
30499 established by the calling function, so we need to save it here. */
30500 tree saved_ccr = current_class_ref;
30501 tree saved_ccp = current_class_ptr;
30502 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30503 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30505 current_class_ref = current_class_ptr = NULL_TREE;
30506 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30509 push_unparsed_function_queues (parser);
30511 /* Push the saved tokens onto the parser's lexer stack. */
30512 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30513 cp_parser_push_lexer_for_tokens (parser, tokens);
30515 /* Parse the condition, ensuring that parameters or the return variable
30516 aren't flagged for use outside the body of a function. */
30517 ++processing_contract_condition;
30518 condition = cp_parser_conditional_expression (parser);
30519 --processing_contract_condition;
30521 /* Revert to the main lexer. */
30522 cp_parser_pop_lexer (parser);
30524 /* Restore the queue. */
30525 pop_unparsed_function_queues (parser);
30527 current_class_ref = saved_ccr;
30528 current_class_ptr = saved_ccp;
30530 /* Commit to changes. */
30531 update_late_contract (contract, result, condition);
30533 /* Leave our temporary scope for the postcondition result. */
30534 if (result)
30536 --processing_template_decl;
30537 pop_bindings_and_leave_scope ();
30541 /* Parse a standard C++-11 attribute specifier.
30543 attribute-specifier:
30544 [ [ attribute-using-prefix [opt] attribute-list ] ]
30545 contract-attribute-specifier
30546 alignment-specifier
30548 attribute-using-prefix:
30549 using attribute-namespace :
30551 alignment-specifier:
30552 alignas ( type-id ... [opt] )
30553 alignas ( alignment-expression ... [opt] ).
30555 Extensions for contracts:
30557 contract-attribute-specifier:
30558 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30559 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30560 [ [ post : contract-mode [opt] identifier [opt] :
30561 conditional-expression ] ]
30563 Return void_list_node if the current token doesn't start an
30564 attribute-specifier to differentiate from NULL_TREE returned e.g.
30565 for [ [ ] ]. */
30567 static tree
30568 cp_parser_std_attribute_spec (cp_parser *parser)
30570 tree attributes = NULL_TREE;
30571 cp_token *token = cp_lexer_peek_token (parser->lexer);
30573 if (token->type == CPP_OPEN_SQUARE
30574 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30576 tree attr_ns = NULL_TREE;
30577 tree attr_name = NULL_TREE;
30579 cp_lexer_consume_token (parser->lexer);
30580 cp_lexer_consume_token (parser->lexer);
30582 token = cp_lexer_peek_token (parser->lexer);
30583 if (token->type == CPP_NAME)
30585 attr_name = token->u.value;
30586 attr_name = canonicalize_attr_name (attr_name);
30589 /* Handle contract-attribute-specs specially. */
30590 if (attr_name && contract_attribute_p (attr_name))
30592 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30593 if (attrs != error_mark_node)
30594 attributes = attrs;
30595 goto finish_attrs;
30598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30600 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30601 if (token->type == CPP_NAME)
30602 attr_ns = token->u.value;
30603 else if (token->type == CPP_KEYWORD)
30604 attr_ns = ridpointers[(int) token->keyword];
30605 else if (token->flags & NAMED_OP)
30606 attr_ns = get_identifier (cpp_type2name (token->type,
30607 token->flags));
30608 if (attr_ns
30609 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30611 if (cxx_dialect < cxx17)
30612 pedwarn (input_location, OPT_Wc__17_extensions,
30613 "attribute using prefix only available "
30614 "with %<-std=c++17%> or %<-std=gnu++17%>");
30616 cp_lexer_consume_token (parser->lexer);
30617 cp_lexer_consume_token (parser->lexer);
30618 cp_lexer_consume_token (parser->lexer);
30620 else
30621 attr_ns = NULL_TREE;
30624 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30626 finish_attrs:
30627 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30628 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30629 cp_parser_skip_to_end_of_statement (parser);
30630 else
30631 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30632 when we are sure that we have actually parsed them. */
30633 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30635 else
30637 tree alignas_expr;
30639 /* Look for an alignment-specifier. */
30641 token = cp_lexer_peek_token (parser->lexer);
30643 if (token->type != CPP_KEYWORD
30644 || token->keyword != RID_ALIGNAS)
30645 return void_list_node;
30647 cp_lexer_consume_token (parser->lexer);
30648 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30650 matching_parens parens;
30651 if (!parens.require_open (parser))
30652 return error_mark_node;
30654 cp_parser_parse_tentatively (parser);
30655 alignas_expr = cp_parser_type_id (parser);
30657 if (!cp_parser_parse_definitely (parser))
30659 alignas_expr = cp_parser_assignment_expression (parser);
30660 if (alignas_expr == error_mark_node)
30661 cp_parser_skip_to_end_of_statement (parser);
30662 if (alignas_expr == NULL_TREE
30663 || alignas_expr == error_mark_node)
30664 return alignas_expr;
30667 alignas_expr = cxx_alignas_expr (alignas_expr);
30668 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30670 /* Handle alignas (pack...). */
30671 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30673 cp_lexer_consume_token (parser->lexer);
30674 alignas_expr = make_pack_expansion (alignas_expr);
30677 /* Something went wrong, so don't build the attribute. */
30678 if (alignas_expr == error_mark_node)
30679 return error_mark_node;
30681 /* Missing ')' means the code cannot possibly be valid; go ahead
30682 and commit to make sure we issue a hard error. */
30683 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30684 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30685 cp_parser_commit_to_tentative_parse (parser);
30687 if (!parens.require_close (parser))
30688 return error_mark_node;
30690 /* Build the C++-11 representation of an 'aligned'
30691 attribute. */
30692 attributes
30693 = build_tree_list (build_tree_list (gnu_identifier,
30694 aligned_identifier), alignas_expr);
30697 return attributes;
30700 /* Parse a standard C++-11 attribute-specifier-seq.
30702 attribute-specifier-seq:
30703 attribute-specifier-seq [opt] attribute-specifier */
30705 static tree
30706 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30708 tree attr_specs = NULL_TREE;
30709 tree attr_last = NULL_TREE;
30711 /* Don't create wrapper nodes within attributes: the
30712 handlers don't know how to handle them. */
30713 auto_suppress_location_wrappers sentinel;
30715 while (true)
30717 tree attr_spec = cp_parser_std_attribute_spec (parser);
30718 if (attr_spec == void_list_node)
30719 break;
30720 /* Accept [[]][[]]; for which cp_parser_std_attribute_spec
30721 returns NULL_TREE as there are no attributes. */
30722 if (attr_spec == NULL_TREE)
30723 continue;
30724 if (attr_spec == error_mark_node)
30725 return error_mark_node;
30727 if (attr_last)
30728 TREE_CHAIN (attr_last) = attr_spec;
30729 else
30730 attr_specs = attr_last = attr_spec;
30731 attr_last = tree_last (attr_last);
30734 return attr_specs;
30737 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30738 return index of the first token after balanced-token, or N on failure. */
30740 static size_t
30741 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30743 size_t orig_n = n;
30744 int nparens = 0, nbraces = 0, nsquares = 0;
30746 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30748 case CPP_PRAGMA_EOL:
30749 if (!parser->lexer->in_pragma)
30750 break;
30751 /* FALLTHRU */
30752 case CPP_EOF:
30753 /* Ran out of tokens. */
30754 return orig_n;
30755 case CPP_OPEN_PAREN:
30756 ++nparens;
30757 break;
30758 case CPP_OPEN_BRACE:
30759 ++nbraces;
30760 break;
30761 case CPP_OPEN_SQUARE:
30762 ++nsquares;
30763 break;
30764 case CPP_CLOSE_PAREN:
30765 --nparens;
30766 break;
30767 case CPP_CLOSE_BRACE:
30768 --nbraces;
30769 break;
30770 case CPP_CLOSE_SQUARE:
30771 --nsquares;
30772 break;
30773 default:
30774 break;
30776 while (nparens || nbraces || nsquares);
30777 return n;
30780 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30781 return index of the first token after the GNU attribute tokens, or N on
30782 failure. */
30784 static size_t
30785 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30787 while (true)
30789 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30790 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30791 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30792 break;
30794 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30795 if (n2 == n + 2)
30796 break;
30797 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30798 break;
30799 n = n2 + 1;
30801 return n;
30804 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30805 next token), return index of the first token after the standard C++11
30806 attribute tokens, or N on failure. */
30808 static size_t
30809 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30811 while (true)
30813 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30814 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30816 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30817 if (n2 == n + 1)
30818 break;
30819 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30820 break;
30821 n = n2 + 1;
30823 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30824 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30826 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30827 if (n2 == n + 1)
30828 break;
30829 n = n2;
30831 else
30832 break;
30834 return n;
30837 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30838 as the next token), return index of the first token after the attribute
30839 tokens, or N on failure. */
30841 static size_t
30842 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30844 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30845 return cp_parser_skip_gnu_attributes_opt (parser, n);
30846 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30849 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30850 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30851 current value of the PEDANTIC flag, regardless of whether or not
30852 the `__extension__' keyword is present. The caller is responsible
30853 for restoring the value of the PEDANTIC flag. */
30855 static bool
30856 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30858 /* Save the old value of the PEDANTIC flag. */
30859 *saved_pedantic = pedantic;
30861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30863 /* Consume the `__extension__' token. */
30864 cp_lexer_consume_token (parser->lexer);
30865 /* We're not being pedantic while the `__extension__' keyword is
30866 in effect. */
30867 pedantic = 0;
30869 return true;
30872 return false;
30875 /* Parse a label declaration.
30877 label-declaration:
30878 __label__ label-declarator-seq ;
30880 label-declarator-seq:
30881 identifier , label-declarator-seq
30882 identifier */
30884 static void
30885 cp_parser_label_declaration (cp_parser* parser)
30887 /* Look for the `__label__' keyword. */
30888 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30890 while (true)
30892 tree identifier;
30894 /* Look for an identifier. */
30895 identifier = cp_parser_identifier (parser);
30896 /* If we failed, stop. */
30897 if (identifier == error_mark_node)
30898 break;
30899 /* Declare it as a label. */
30900 finish_label_decl (identifier);
30901 /* If the next token is a `;', stop. */
30902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30903 break;
30904 /* Look for the `,' separating the label declarations. */
30905 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30908 /* Look for the final `;'. */
30909 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30912 // -------------------------------------------------------------------------- //
30913 // Concept definitions
30915 static tree
30916 cp_parser_concept_definition (cp_parser *parser)
30918 /* A concept definition is an unevaluated context. */
30919 cp_unevaluated u;
30921 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30922 cp_lexer_consume_token (parser->lexer);
30924 cp_expr id = cp_parser_identifier (parser);
30925 if (id == error_mark_node)
30927 cp_parser_skip_to_end_of_statement (parser);
30928 cp_parser_consume_semicolon_at_end_of_statement (parser);
30929 return NULL_TREE;
30932 tree attrs = cp_parser_attributes_opt (parser);
30934 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30936 cp_parser_skip_to_end_of_statement (parser);
30937 cp_parser_consume_semicolon_at_end_of_statement (parser);
30938 return error_mark_node;
30941 processing_constraint_expression_sentinel parsing_constraint;
30942 tree init = cp_parser_constraint_expression (parser);
30943 if (init == error_mark_node)
30944 cp_parser_skip_to_end_of_statement (parser);
30946 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30947 but continue as if it were. */
30948 cp_parser_consume_semicolon_at_end_of_statement (parser);
30950 return finish_concept_definition (id, init, attrs);
30953 // -------------------------------------------------------------------------- //
30954 // Requires Clause
30956 /* Diagnose an expression that should appear in ()'s within a requires-clause
30957 and suggest where to place those parentheses. */
30959 static void
30960 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30962 error_at (loc, "expression must be enclosed in parentheses");
30965 static void
30966 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30968 gcc_rich_location richloc (loc);
30969 richloc.add_fixit_insert_before ("(");
30970 richloc.add_fixit_insert_after (")");
30971 error_at (&richloc, "expression must be enclosed in parentheses");
30974 /* Characterizes the likely kind of expression intended by a mis-written
30975 primary constraint. */
30976 enum primary_constraint_error
30978 pce_ok,
30979 pce_maybe_operator,
30980 pce_maybe_postfix
30983 /* Returns true if the token(s) following a primary-expression in a
30984 constraint-logical-* expression would require parentheses. */
30986 static primary_constraint_error
30987 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30989 cp_token *token = cp_lexer_peek_token (parser->lexer);
30990 switch (token->type)
30992 default:
30993 return pce_ok;
30995 case CPP_EQ:
30997 /* An equal sign may be part of the definition of a function,
30998 and not an assignment operator, when parsing the expression
30999 for a trailing requires-clause. For example:
31001 template<typename T>
31002 struct S {
31003 S() requires C<T> = default;
31006 Don't try to reparse this a binary operator. */
31007 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
31008 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
31009 return pce_ok;
31011 gcc_fallthrough ();
31014 /* Arithmetic operators. */
31015 case CPP_PLUS:
31016 case CPP_MINUS:
31017 case CPP_MULT:
31018 case CPP_DIV:
31019 case CPP_MOD:
31020 /* Bitwise operators. */
31021 case CPP_AND:
31022 case CPP_OR:
31023 case CPP_XOR:
31024 case CPP_RSHIFT:
31025 case CPP_LSHIFT:
31026 /* Relational operators. */
31027 case CPP_EQ_EQ:
31028 case CPP_NOT_EQ:
31029 case CPP_LESS:
31030 case CPP_GREATER:
31031 case CPP_LESS_EQ:
31032 case CPP_GREATER_EQ:
31033 case CPP_SPACESHIP:
31034 /* Pointer-to-member. */
31035 case CPP_DOT_STAR:
31036 case CPP_DEREF_STAR:
31037 /* Assignment operators. */
31038 case CPP_PLUS_EQ:
31039 case CPP_MINUS_EQ:
31040 case CPP_MULT_EQ:
31041 case CPP_DIV_EQ:
31042 case CPP_MOD_EQ:
31043 case CPP_AND_EQ:
31044 case CPP_OR_EQ:
31045 case CPP_XOR_EQ:
31046 case CPP_RSHIFT_EQ:
31047 case CPP_LSHIFT_EQ:
31048 /* Conditional operator */
31049 case CPP_QUERY:
31050 /* Unenclosed binary or conditional operator. */
31051 return pce_maybe_operator;
31053 case CPP_OPEN_PAREN:
31055 /* A primary constraint that precedes the parameter-list of a
31056 lambda expression is followed by an open paren.
31058 []<typename T> requires C (T a, T b) { ... }
31060 Don't try to re-parse this as a postfix expression. */
31061 if (lambda_p)
31062 return pce_ok;
31064 gcc_fallthrough ();
31066 case CPP_OPEN_SQUARE:
31068 /* A primary-constraint-expression followed by a '[[' is not a
31069 postfix expression. */
31070 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
31071 return pce_ok;
31073 gcc_fallthrough ();
31075 case CPP_PLUS_PLUS:
31076 case CPP_MINUS_MINUS:
31077 case CPP_DOT:
31078 /* Unenclosed postfix operator. */
31079 return pce_maybe_postfix;
31081 case CPP_DEREF:
31082 /* A primary constraint that precedes the lambda-declarator of a
31083 lambda expression is followed by trailing return type.
31085 []<typename T> requires C -> void {}
31087 Don't try to re-parse this as a postfix expression in
31088 C++23 and later. In C++20 ( needs to come in between but we
31089 allow it to be omitted with pedwarn. */
31090 if (lambda_p)
31091 return pce_ok;
31092 /* Unenclosed postfix operator. */
31093 return pce_maybe_postfix;
31097 /* Returns true if the next token begins a unary expression, preceded by
31098 an operator or keyword. */
31100 static bool
31101 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
31103 cp_token *token = cp_lexer_peek_token (parser->lexer);
31104 switch (token->type)
31106 case CPP_NOT:
31107 case CPP_PLUS:
31108 case CPP_MINUS:
31109 case CPP_MULT:
31110 case CPP_COMPL:
31111 case CPP_PLUS_PLUS:
31112 case CPP_MINUS_MINUS:
31113 return true;
31115 case CPP_KEYWORD:
31117 switch (token->keyword)
31119 case RID_STATCAST:
31120 case RID_DYNCAST:
31121 case RID_REINTCAST:
31122 case RID_CONSTCAST:
31123 case RID_TYPEID:
31124 case RID_SIZEOF:
31125 case RID_ALIGNOF:
31126 case RID_NOEXCEPT:
31127 case RID_NEW:
31128 case RID_DELETE:
31129 case RID_THROW:
31130 return true;
31132 default:
31133 break;
31137 default:
31138 break;
31141 return false;
31144 /* Parse a primary expression within a constraint. */
31146 static cp_expr
31147 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
31149 /* If this looks like a unary expression, parse it as such, but diagnose
31150 it as ill-formed; it requires parens. */
31151 if (cp_parser_unary_constraint_requires_parens (parser))
31153 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
31154 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
31155 return e;
31158 cp_lexer_save_tokens (parser->lexer);
31159 cp_id_kind idk;
31160 location_t loc = input_location;
31161 cp_expr expr = cp_parser_primary_expression (parser,
31162 /*address_p=*/false,
31163 /*cast_p=*/false,
31164 /*template_arg_p=*/false,
31165 &idk);
31166 expr.maybe_add_location_wrapper ();
31168 primary_constraint_error pce = pce_ok;
31169 if (expr != error_mark_node)
31171 /* The primary-expression could be part of an unenclosed non-logical
31172 compound expression. */
31173 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
31175 if (pce == pce_ok)
31177 cp_lexer_commit_tokens (parser->lexer);
31178 return finish_constraint_primary_expr (expr);
31181 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
31182 error, but return the expression as if it were valid. */
31183 cp_lexer_rollback_tokens (parser->lexer);
31184 cp_parser_parse_tentatively (parser);
31185 if (pce == pce_maybe_operator)
31186 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31187 else
31188 expr = cp_parser_simple_cast_expression (parser);
31189 if (cp_parser_parse_definitely (parser))
31191 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
31192 return expr;
31195 /* Otherwise, something has gone very wrong, and we can't generate a more
31196 meaningful diagnostic or recover. */
31197 cp_parser_diagnose_ungrouped_constraint_plain (loc);
31198 return error_mark_node;
31201 /* Parse a constraint-logical-and-expression.
31203 constraint-logical-and-expression:
31204 primary-expression
31205 constraint-logical-and-expression '&&' primary-expression */
31207 static cp_expr
31208 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
31210 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31211 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
31213 cp_token *op = cp_lexer_consume_token (parser->lexer);
31214 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31215 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
31217 return lhs;
31220 /* Parse a constraint-logical-or-expression.
31222 constraint-logical-or-expression:
31223 constraint-logical-and-expression
31224 constraint-logical-or-expression '||' constraint-logical-and-expression */
31226 static cp_expr
31227 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
31229 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31230 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
31232 cp_token *op = cp_lexer_consume_token (parser->lexer);
31233 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31234 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
31236 return lhs;
31239 /* Parse the expression after a requires-clause. This has a different grammar
31240 than that in the concepts TS. */
31242 static tree
31243 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
31245 processing_constraint_expression_sentinel parsing_constraint;
31246 ++processing_template_decl;
31247 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
31248 --processing_template_decl;
31249 if (check_for_bare_parameter_packs (expr))
31250 expr = error_mark_node;
31251 return expr;
31254 /* Parse a expression after a requires clause.
31256 constraint-expression:
31257 logical-or-expression
31259 The required logical-or-expression must be a constant expression. Note
31260 that we don't check that the expression is constepxr here. We defer until
31261 we analyze constraints and then, we only check atomic constraints. */
31263 static tree
31264 cp_parser_constraint_expression (cp_parser *parser)
31266 processing_constraint_expression_sentinel parsing_constraint;
31267 ++processing_template_decl;
31268 cp_expr expr = cp_parser_binary_expression (parser, false, true,
31269 PREC_NOT_OPERATOR, NULL);
31270 --processing_template_decl;
31271 if (check_for_bare_parameter_packs (expr))
31272 expr = error_mark_node;
31273 expr.maybe_add_location_wrapper ();
31274 return expr;
31277 /* Optionally parse a requires clause:
31279 requires-clause:
31280 `requires` constraint-logical-or-expression.
31281 [ConceptsTS]
31282 `requires constraint-expression.
31284 LAMBDA_P is true when the requires-clause is parsed before the
31285 parameter-list of a lambda-declarator. */
31287 static tree
31288 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
31290 /* A requires clause is an unevaluated context. */
31291 cp_unevaluated u;
31293 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31294 if (tok->keyword != RID_REQUIRES)
31296 if (!flag_concepts && tok->type == CPP_NAME
31297 && tok->u.value == ridpointers[RID_REQUIRES])
31299 error_at (cp_lexer_peek_token (parser->lexer)->location,
31300 "%<requires%> only available with "
31301 "%<-std=c++20%> or %<-fconcepts%>");
31302 /* Parse and discard the requires-clause. */
31303 cp_lexer_consume_token (parser->lexer);
31304 cp_parser_constraint_expression (parser);
31306 return NULL_TREE;
31309 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
31310 if (tok2->type == CPP_OPEN_BRACE)
31312 /* An opening brace following the start of a requires-clause is
31313 ill-formed; the user likely forgot the second `requires' that
31314 would start a requires-expression. */
31315 gcc_rich_location richloc (tok2->location);
31316 richloc.add_fixit_insert_after (tok->location, " requires");
31317 error_at (&richloc, "missing additional %<requires%> to start "
31318 "a requires-expression");
31319 /* Don't consume the `requires', so that it's reused as the start of a
31320 requires-expression. */
31322 else
31323 cp_lexer_consume_token (parser->lexer);
31325 if (!flag_concepts_ts)
31326 return cp_parser_requires_clause_expression (parser, lambda_p);
31327 else
31328 return cp_parser_constraint_expression (parser);
31331 /*---------------------------------------------------------------------------
31332 Requires expressions
31333 ---------------------------------------------------------------------------*/
31335 /* Parse a requires expression
31337 requirement-expression:
31338 'requires' requirement-parameter-list [opt] requirement-body */
31340 static tree
31341 cp_parser_requires_expression (cp_parser *parser)
31343 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31344 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
31346 /* Avoid committing to outer tentative parse. */
31347 tentative_firewall firewall (parser);
31349 /* This is definitely a requires-expression. */
31350 cp_parser_commit_to_tentative_parse (parser);
31352 tree parms, reqs;
31354 /* Local parameters are delared as variables within the scope
31355 of the expression. They are not visible past the end of
31356 the expression. Expressions within the requires-expression
31357 are unevaluated. */
31358 struct scope_sentinel
31360 scope_sentinel ()
31362 ++cp_unevaluated_operand;
31363 begin_scope (sk_function_parms, NULL_TREE);
31364 current_binding_level->requires_expression = true;
31367 ~scope_sentinel ()
31369 pop_bindings_and_leave_scope ();
31370 --cp_unevaluated_operand;
31372 } s;
31374 /* Parse the optional parameter list. */
31375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31377 parms = cp_parser_requirement_parameter_list (parser);
31378 if (parms == error_mark_node)
31379 return error_mark_node;
31381 else
31382 parms = NULL_TREE;
31384 /* Parse the requirement body. */
31385 ++processing_template_decl;
31386 reqs = cp_parser_requirement_body (parser);
31387 --processing_template_decl;
31388 if (reqs == error_mark_node)
31389 return error_mark_node;
31392 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31393 the parm chain. */
31394 grokparms (parms, &parms);
31395 loc = make_location (loc, loc, parser->lexer);
31396 tree expr = finish_requires_expr (loc, parms, reqs);
31397 if (!processing_template_decl)
31399 /* Perform semantic processing now to diagnose any invalid types and
31400 expressions. */
31401 int saved_errorcount = errorcount;
31402 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31403 if (errorcount > saved_errorcount)
31404 return error_mark_node;
31406 return expr;
31409 /* Parse a parameterized requirement.
31411 requirement-parameter-list:
31412 '(' parameter-declaration-clause ')' */
31414 static tree
31415 cp_parser_requirement_parameter_list (cp_parser *parser)
31417 matching_parens parens;
31418 if (!parens.require_open (parser))
31419 return error_mark_node;
31421 tree parms = (cp_parser_parameter_declaration_clause
31422 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31424 if (!parens.require_close (parser))
31425 return error_mark_node;
31427 /* Modify the declared parameters by removing their context
31428 so they don't refer to the enclosing scope and explicitly
31429 indicating that they are constraint variables. */
31430 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31432 if (parm == void_list_node || parm == explicit_void_list_node)
31433 break;
31434 tree decl = TREE_VALUE (parm);
31435 if (decl != error_mark_node)
31437 DECL_CONTEXT (decl) = NULL_TREE;
31438 CONSTRAINT_VAR_P (decl) = true;
31442 return parms;
31445 /* Parse the body of a requirement.
31447 requirement-body:
31448 '{' requirement-list '}' */
31449 static tree
31450 cp_parser_requirement_body (cp_parser *parser)
31452 matching_braces braces;
31453 if (!braces.require_open (parser))
31454 return error_mark_node;
31456 tree reqs = cp_parser_requirement_seq (parser);
31458 if (!braces.require_close (parser))
31459 return error_mark_node;
31461 return reqs;
31464 /* Parse a sequence of requirements.
31466 requirement-seq:
31467 requirement
31468 requirement-seq requirement */
31470 static tree
31471 cp_parser_requirement_seq (cp_parser *parser)
31473 tree result = NULL_TREE;
31476 tree req = cp_parser_requirement (parser);
31477 if (req != error_mark_node)
31478 result = tree_cons (NULL_TREE, req, result);
31480 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31481 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31483 /* If there are no valid requirements, this is not a valid expression. */
31484 if (!result)
31485 return error_mark_node;
31487 /* Reverse the order of requirements so they are analyzed in order. */
31488 return nreverse (result);
31491 /* Parse a syntactic requirement or type requirement.
31493 requirement:
31494 simple-requirement
31495 compound-requirement
31496 type-requirement
31497 nested-requirement */
31499 static tree
31500 cp_parser_requirement (cp_parser *parser)
31502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31503 return cp_parser_compound_requirement (parser);
31504 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31506 /* It's probably a type-requirement. */
31507 cp_parser_parse_tentatively (parser);
31508 tree req = cp_parser_type_requirement (parser);
31509 if (cp_parser_parse_definitely (parser))
31510 return req;
31511 /* No, maybe it's something like typename T::type(); */
31512 cp_parser_parse_tentatively (parser);
31513 req = cp_parser_simple_requirement (parser);
31514 if (cp_parser_parse_definitely (parser))
31515 return req;
31516 /* Non-tentative for the error. */
31517 return cp_parser_type_requirement (parser);
31519 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31520 return cp_parser_nested_requirement (parser);
31521 else
31522 return cp_parser_simple_requirement (parser);
31525 /* Parse a simple requirement.
31527 simple-requirement:
31528 expression ';' */
31530 static tree
31531 cp_parser_simple_requirement (cp_parser *parser)
31533 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31534 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31535 if (expr == error_mark_node)
31536 cp_parser_skip_to_end_of_statement (parser);
31538 cp_parser_consume_semicolon_at_end_of_statement (parser);
31540 if (!expr || expr == error_mark_node)
31541 return error_mark_node;
31543 /* Sometimes we don't get locations, so use the cached token location
31544 as a reasonable approximation. */
31545 if (expr.get_location() == UNKNOWN_LOCATION)
31546 expr.set_location (start);
31548 for (tree t = expr; ; )
31550 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31551 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31553 t = TREE_OPERAND (t, 0);
31554 continue;
31556 if (concept_check_p (t))
31558 gcc_rich_location richloc (get_start (start));
31559 richloc.add_fixit_insert_before (start, "requires ");
31560 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31561 "if a concept-id is a valid expression; add "
31562 "%<requires%> to check satisfaction");
31564 break;
31567 return finish_simple_requirement (expr.get_location (), expr);
31570 /* Parse a type requirement
31572 type-requirement
31573 nested-name-specifier [opt] required-type-name ';'
31575 required-type-name:
31576 type-name
31577 'template' [opt] simple-template-id */
31579 static tree
31580 cp_parser_type_requirement (cp_parser *parser)
31582 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31583 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31585 // Save the scope before parsing name specifiers.
31586 tree saved_scope = parser->scope;
31587 tree saved_object_scope = parser->object_scope;
31588 tree saved_qualifying_scope = parser->qualifying_scope;
31589 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31590 cp_parser_nested_name_specifier_opt (parser,
31591 /*typename_keyword_p=*/true,
31592 /*check_dependency_p=*/true,
31593 /*type_p=*/true,
31594 /*is_declaration=*/false);
31596 tree type;
31597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31599 cp_lexer_consume_token (parser->lexer);
31600 type = cp_parser_template_id (parser,
31601 /*template_keyword_p=*/true,
31602 /*check_dependency_p=*/true,
31603 /*tag_type=*/none_type,
31604 /*is_declaration=*/false);
31605 type = make_typename_type (parser->scope, type, typename_type,
31606 /*complain=*/tf_error);
31608 else
31609 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31611 if (TREE_CODE (type) == TYPE_DECL)
31612 type = TREE_TYPE (type);
31614 parser->scope = saved_scope;
31615 parser->object_scope = saved_object_scope;
31616 parser->qualifying_scope = saved_qualifying_scope;
31618 if (type == error_mark_node)
31619 cp_parser_skip_to_end_of_statement (parser);
31621 cp_parser_consume_semicolon_at_end_of_statement (parser);
31623 if (type == error_mark_node)
31624 return error_mark_node;
31626 loc = make_location (loc, start_tok->location, parser->lexer);
31627 return finish_type_requirement (loc, type);
31630 /* Parse a compound requirement
31632 compound-requirement:
31633 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31635 static tree
31636 cp_parser_compound_requirement (cp_parser *parser)
31638 /* Parse an expression enclosed in '{ }'s. */
31639 matching_braces braces;
31640 if (!braces.require_open (parser))
31641 return error_mark_node;
31643 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31645 tree expr = cp_parser_expression (parser, NULL, false, false);
31646 if (expr == error_mark_node)
31647 cp_parser_skip_to_closing_brace (parser);
31649 if (!braces.require_close (parser))
31651 cp_parser_skip_to_end_of_statement (parser);
31652 cp_parser_consume_semicolon_at_end_of_statement (parser);
31653 return error_mark_node;
31656 /* If the expression was invalid, skip the remainder of the requirement. */
31657 if (!expr || expr == error_mark_node)
31659 cp_parser_skip_to_end_of_statement (parser);
31660 cp_parser_consume_semicolon_at_end_of_statement (parser);
31661 return error_mark_node;
31664 /* Parse the optional noexcept. */
31665 bool noexcept_p = false;
31666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31668 cp_lexer_consume_token (parser->lexer);
31669 noexcept_p = true;
31672 /* Parse the optional trailing return type. */
31673 tree type = NULL_TREE;
31674 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31676 cp_lexer_consume_token (parser->lexer);
31677 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31679 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31680 parser->in_result_type_constraint_p = true;
31681 /* C++20 allows either a type-id or a type-constraint. Parsing
31682 a type-id will subsume the parsing for a type-constraint but
31683 allow for more syntactic forms (e.g., const C<T>*). */
31684 type = cp_parser_trailing_type_id (parser);
31685 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31686 if (type == error_mark_node)
31687 return error_mark_node;
31689 location_t type_loc = make_location (tok->location, tok->location,
31690 parser->lexer);
31692 /* Check that we haven't written something like 'const C<T>*'. */
31693 if (type_uses_auto (type))
31695 if (!is_auto (type))
31697 error_at (type_loc,
31698 "result type is not a plain type-constraint");
31699 cp_parser_consume_semicolon_at_end_of_statement (parser);
31700 return error_mark_node;
31703 else if (!flag_concepts_ts)
31704 /* P1452R2 removed the trailing-return-type option. */
31705 error_at (type_loc,
31706 "return-type-requirement is not a type-constraint");
31709 location_t loc = make_location (expr_token->location,
31710 braces.open_location (),
31711 parser->lexer);
31713 cp_parser_consume_semicolon_at_end_of_statement (parser);
31715 if (expr == error_mark_node || type == error_mark_node)
31716 return error_mark_node;
31718 return finish_compound_requirement (loc, expr, type, noexcept_p);
31721 /* Parse a nested requirement. This is the same as a requires clause.
31723 nested-requirement:
31724 requires-clause */
31726 static tree
31727 cp_parser_nested_requirement (cp_parser *parser)
31729 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31730 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31731 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31732 tree req = cp_parser_constraint_expression (parser);
31733 if (req == error_mark_node)
31734 cp_parser_skip_to_end_of_statement (parser);
31735 loc = make_location (loc, tok->location, parser->lexer);
31736 cp_parser_consume_semicolon_at_end_of_statement (parser);
31737 if (req == error_mark_node)
31738 return error_mark_node;
31739 return finish_nested_requirement (loc, req);
31742 /* Support Functions */
31744 /* Return the appropriate prefer_type argument for lookup_name based on
31745 tag_type. */
31747 static inline LOOK_want
31748 prefer_type_arg (tag_types tag_type)
31750 switch (tag_type)
31752 case none_type: return LOOK_want::NORMAL; // No preference.
31753 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31754 default: return LOOK_want::TYPE; // Type only.
31758 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31759 NAME should have one of the representations used for an
31760 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31761 is returned. If PARSER->SCOPE is a dependent type, then a
31762 SCOPE_REF is returned.
31764 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31765 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31766 was formed. Abstractly, such entities should not be passed to this
31767 function, because they do not need to be looked up, but it is
31768 simpler to check for this special case here, rather than at the
31769 call-sites.
31771 In cases not explicitly covered above, this function returns a
31772 DECL, OVERLOAD, or baselink representing the result of the lookup.
31773 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31774 is returned.
31776 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31777 (e.g., "struct") that was used. In that case bindings that do not
31778 refer to types are ignored.
31780 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31781 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31783 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31784 are ignored.
31786 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31787 types.
31789 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31790 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31791 NULL_TREE otherwise. */
31793 static cp_expr
31794 cp_parser_lookup_name (cp_parser *parser, tree name,
31795 enum tag_types tag_type,
31796 int is_template,
31797 bool is_namespace,
31798 bool check_dependency,
31799 tree *ambiguous_decls,
31800 location_t name_location)
31802 tree decl;
31803 tree object_type = parser->context->object_type;
31805 /* Assume that the lookup will be unambiguous. */
31806 if (ambiguous_decls)
31807 *ambiguous_decls = NULL_TREE;
31809 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31810 no longer valid. Note that if we are parsing tentatively, and
31811 the parse fails, OBJECT_TYPE will be automatically restored. */
31812 parser->context->object_type = NULL_TREE;
31814 if (name == error_mark_node)
31815 return error_mark_node;
31817 /* A template-id has already been resolved; there is no lookup to
31818 do. */
31819 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31820 return name;
31821 if (BASELINK_P (name))
31823 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31824 == TEMPLATE_ID_EXPR);
31825 return name;
31828 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31829 it should already have been checked to make sure that the name
31830 used matches the type being destroyed. */
31831 if (TREE_CODE (name) == BIT_NOT_EXPR)
31833 tree type;
31835 /* Figure out to which type this destructor applies. */
31836 if (parser->scope)
31837 type = parser->scope;
31838 else if (object_type)
31839 type = object_type;
31840 else
31841 type = current_class_type;
31842 /* If that's not a class type, there is no destructor. */
31843 if (!type || !CLASS_TYPE_P (type))
31844 return error_mark_node;
31846 /* In a non-static member function, check implicit this->. */
31847 if (current_class_ref)
31848 return lookup_destructor (current_class_ref, parser->scope, name,
31849 tf_warning_or_error);
31851 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31852 lazily_declare_fn (sfk_destructor, type);
31854 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31855 return dtor;
31857 return error_mark_node;
31860 /* By this point, the NAME should be an ordinary identifier. If
31861 the id-expression was a qualified name, the qualifying scope is
31862 stored in PARSER->SCOPE at this point. */
31863 gcc_assert (identifier_p (name));
31865 /* Perform the lookup. */
31866 if (parser->scope)
31868 bool dependent_p;
31870 if (parser->scope == error_mark_node)
31871 return error_mark_node;
31873 /* If the SCOPE is dependent, the lookup must be deferred until
31874 the template is instantiated -- unless we are explicitly
31875 looking up names in uninstantiated templates. Even then, we
31876 cannot look up the name if the scope is not a class type; it
31877 might, for example, be a template type parameter. */
31878 dependent_p = (TYPE_P (parser->scope)
31879 && dependent_scope_p (parser->scope));
31880 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31881 && dependent_p)
31882 /* Defer lookup. */
31883 decl = error_mark_node;
31884 else
31886 tree pushed_scope = NULL_TREE;
31888 /* If PARSER->SCOPE is a dependent type, then it must be a
31889 class type, and we must not be checking dependencies;
31890 otherwise, we would have processed this lookup above. So
31891 that PARSER->SCOPE is not considered a dependent base by
31892 lookup_member, we must enter the scope here. */
31893 if (dependent_p)
31894 pushed_scope = push_scope (parser->scope);
31896 /* If the PARSER->SCOPE is a template specialization, it
31897 may be instantiated during name lookup. In that case,
31898 errors may be issued. Even if we rollback the current
31899 tentative parse, those errors are valid. */
31900 decl = lookup_qualified_name (parser->scope, name,
31901 prefer_type_arg (tag_type),
31902 /*complain=*/true);
31904 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31905 lookup result and the nested-name-specifier nominates a class C:
31906 * if the name specified after the nested-name-specifier, when
31907 looked up in C, is the injected-class-name of C (Clause 9), or
31908 * if the name specified after the nested-name-specifier is the
31909 same as the identifier or the simple-template-id's template-
31910 name in the last component of the nested-name-specifier,
31911 the name is instead considered to name the constructor of
31912 class C. [ Note: for example, the constructor is not an
31913 acceptable lookup result in an elaborated-type-specifier so
31914 the constructor would not be used in place of the
31915 injected-class-name. --end note ] Such a constructor name
31916 shall be used only in the declarator-id of a declaration that
31917 names a constructor or in a using-declaration. */
31918 if (tag_type == none_type
31919 && DECL_SELF_REFERENCE_P (decl)
31920 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31921 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31922 prefer_type_arg (tag_type),
31923 /*complain=*/true);
31925 if (pushed_scope)
31926 pop_scope (pushed_scope);
31929 /* If the scope is a dependent type and either we deferred lookup or
31930 we did lookup but didn't find the name, rememeber the name. */
31931 if (decl == error_mark_node && TYPE_P (parser->scope)
31932 && dependent_type_p (parser->scope))
31934 if (tag_type)
31936 tree type;
31938 /* The resolution to Core Issue 180 says that `struct
31939 A::B' should be considered a type-name, even if `A'
31940 is dependent. */
31941 type = make_typename_type (parser->scope, name, tag_type,
31942 /*complain=*/tf_error);
31943 if (type != error_mark_node)
31944 decl = TYPE_NAME (type);
31946 else if (is_template
31947 && (cp_parser_next_token_ends_template_argument_p (parser)
31948 || cp_lexer_next_token_is (parser->lexer,
31949 CPP_CLOSE_PAREN)))
31950 decl = make_unbound_class_template (parser->scope,
31951 name, NULL_TREE,
31952 /*complain=*/tf_error);
31953 else
31954 decl = build_qualified_name (/*type=*/NULL_TREE,
31955 parser->scope, name,
31956 is_template);
31958 parser->qualifying_scope = parser->scope;
31959 parser->object_scope = NULL_TREE;
31961 else if (object_type)
31963 bool dep = dependent_scope_p (object_type);
31965 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31966 OBJECT_TYPE is not a class. */
31967 if (!dep && CLASS_TYPE_P (object_type))
31968 /* If the OBJECT_TYPE is a template specialization, it may
31969 be instantiated during name lookup. In that case, errors
31970 may be issued. Even if we rollback the current tentative
31971 parse, those errors are valid. */
31972 decl = lookup_member (object_type,
31973 name,
31974 /*protect=*/0,
31975 /*prefer_type=*/tag_type != none_type,
31976 tf_warning_or_error);
31977 else
31978 decl = NULL_TREE;
31980 /* If we didn't find a member and have dependent bases, the member lookup
31981 is now dependent. */
31982 if (!dep && !decl && any_dependent_bases_p (object_type))
31983 dep = true;
31985 if (dep && is_template == 2)
31986 /* The template keyword specifies a dependent template. */;
31987 else if (!decl)
31988 /* Look it up in the enclosing context. DR 141: When looking for a
31989 template-name after -> or ., only consider class templates. */
31990 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31991 /* DR 141: When looking in the
31992 current enclosing context for a
31993 template-name after -> or ., only
31994 consider class templates. */
31995 : is_template ? LOOK_want::TYPE
31996 : prefer_type_arg (tag_type));
31998 /* If we did unqualified lookup of a dependent member-qualified name and
31999 found something, do we want to use it? P1787 clarified that we need
32000 to look in the object scope first even if it's dependent, but for now
32001 let's still use it in some cases.
32002 FIXME remember unqualified lookup result to use if member lookup fails
32003 at instantiation time. */
32004 if (decl && dep && is_template)
32006 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
32007 /* Only use the unqualified class template lookup if we're actually
32008 looking at a template arg list. */
32009 if (!cp_parser_skip_entire_template_parameter_list (parser))
32010 decl = NULL_TREE;
32013 /* If we know we're looking for a type (e.g. A in p->A::x),
32014 mock up a typename. */
32015 if (!decl && dep && tag_type != none_type)
32017 tree type = build_typename_type (object_type, name, name,
32018 typename_type);
32019 decl = TYPE_NAME (type);
32022 parser->object_scope = object_type;
32023 parser->qualifying_scope = NULL_TREE;
32025 else
32027 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32028 : prefer_type_arg (tag_type));
32029 parser->qualifying_scope = NULL_TREE;
32030 parser->object_scope = NULL_TREE;
32033 /* If the lookup failed, let our caller know. */
32034 if (!decl || decl == error_mark_node)
32035 return error_mark_node;
32037 /* If we have resolved the name of a member declaration, check to
32038 see if the declaration is accessible. When the name resolves to
32039 set of overloaded functions, accessibility is checked when
32040 overload resolution is done. If we have a TREE_LIST, then the lookup
32041 is either ambiguous or it found multiple injected-class-names, the
32042 accessibility of which is trivially satisfied.
32044 During an explicit instantiation, access is not checked at all,
32045 as per [temp.explicit]. */
32046 if (DECL_P (decl))
32047 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
32048 tf_warning_or_error);
32050 /* Pull out the template from an injected-class-name (or multiple). */
32051 if (is_template)
32052 decl = maybe_get_template_decl_from_type_decl (decl);
32054 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
32055 if (TREE_CODE (decl) == TREE_LIST)
32057 if (ambiguous_decls)
32058 *ambiguous_decls = decl;
32059 /* The error message we have to print is too complicated for
32060 cp_parser_error, so we incorporate its actions directly. */
32061 if (!cp_parser_simulate_error (parser))
32063 error_at (name_location, "reference to %qD is ambiguous",
32064 name);
32065 print_candidates (decl);
32067 return error_mark_node;
32070 gcc_assert (DECL_P (decl)
32071 || TREE_CODE (decl) == OVERLOAD
32072 || TREE_CODE (decl) == SCOPE_REF
32073 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
32074 || BASELINK_P (decl));
32076 maybe_record_typedef_use (decl);
32078 return cp_expr (decl, name_location);
32081 /* Like cp_parser_lookup_name, but for use in the typical case where
32082 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
32083 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
32085 static tree
32086 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
32088 return cp_parser_lookup_name (parser, name,
32089 none_type,
32090 /*is_template=*/false,
32091 /*is_namespace=*/false,
32092 /*check_dependency=*/true,
32093 /*ambiguous_decls=*/NULL,
32094 location);
32097 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
32098 the current context, return the TYPE_DECL. If TAG_NAME_P is
32099 true, the DECL indicates the class being defined in a class-head,
32100 or declared in an elaborated-type-specifier.
32102 Otherwise, return DECL. */
32104 static tree
32105 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
32107 /* If the TEMPLATE_DECL is being declared as part of a class-head,
32108 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
32110 struct A {
32111 template <typename T> struct B;
32114 template <typename T> struct A::B {};
32116 Similarly, in an elaborated-type-specifier:
32118 namespace N { struct X{}; }
32120 struct A {
32121 template <typename T> friend struct N::X;
32124 However, if the DECL refers to a class type, and we are in
32125 the scope of the class, then the name lookup automatically
32126 finds the TYPE_DECL created by build_self_reference rather
32127 than a TEMPLATE_DECL. For example, in:
32129 template <class T> struct S {
32130 S s;
32133 there is no need to handle such case. */
32135 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
32136 return DECL_TEMPLATE_RESULT (decl);
32138 return decl;
32141 /* If too many, or too few, template-parameter lists apply to the
32142 declarator, issue an error message. Returns TRUE if all went well,
32143 and FALSE otherwise. */
32145 static bool
32146 cp_parser_check_declarator_template_parameters (cp_parser* parser,
32147 cp_declarator *declarator,
32148 location_t declarator_location)
32150 switch (declarator->kind)
32152 case cdk_id:
32154 unsigned num_templates = 0;
32155 tree scope = declarator->u.id.qualifying_scope;
32156 bool template_id_p = false;
32158 if (scope)
32159 num_templates = num_template_headers_for_class (scope);
32160 else if (TREE_CODE (declarator->u.id.unqualified_name)
32161 == TEMPLATE_ID_EXPR)
32163 /* If the DECLARATOR has the form `X<y>' then it uses one
32164 additional level of template parameters. */
32165 ++num_templates;
32166 template_id_p = true;
32169 return cp_parser_check_template_parameters
32170 (parser, num_templates, template_id_p, declarator_location,
32171 declarator);
32174 case cdk_function:
32175 case cdk_array:
32176 case cdk_pointer:
32177 case cdk_reference:
32178 case cdk_ptrmem:
32179 return (cp_parser_check_declarator_template_parameters
32180 (parser, declarator->declarator, declarator_location));
32182 case cdk_decomp:
32183 case cdk_error:
32184 return true;
32186 default:
32187 gcc_unreachable ();
32189 return false;
32192 /* NUM_TEMPLATES were used in the current declaration. If that is
32193 invalid, return FALSE and issue an error messages. Otherwise,
32194 return TRUE. If DECLARATOR is non-NULL, then we are checking a
32195 declarator and we can print more accurate diagnostics. */
32197 static bool
32198 cp_parser_check_template_parameters (cp_parser* parser,
32199 unsigned num_templates,
32200 bool template_id_p,
32201 location_t location,
32202 cp_declarator *declarator)
32204 /* If there are the same number of template classes and parameter
32205 lists, that's OK. */
32206 if (parser->num_template_parameter_lists == num_templates)
32207 return true;
32208 /* If there are more, but only one more, and the name ends in an identifier,
32209 then we are declaring a primary template. That's OK too. */
32210 if (!template_id_p
32211 && parser->num_template_parameter_lists == num_templates + 1)
32212 return true;
32214 if (cp_parser_simulate_error (parser))
32215 return false;
32217 /* If there are more template classes than parameter lists, we have
32218 something like:
32220 template <class T> void S<T>::R<T>::f (); */
32221 if (parser->num_template_parameter_lists < num_templates)
32223 if (declarator && !current_function_decl)
32224 error_at (location, "specializing member %<%T::%E%> "
32225 "requires %<template<>%> syntax",
32226 declarator->u.id.qualifying_scope,
32227 declarator->u.id.unqualified_name);
32228 else if (declarator)
32229 error_at (location, "invalid declaration of %<%T::%E%>",
32230 declarator->u.id.qualifying_scope,
32231 declarator->u.id.unqualified_name);
32232 else
32233 error_at (location, "too few template-parameter-lists");
32234 return false;
32236 /* Otherwise, there are too many template parameter lists. We have
32237 something like:
32239 template <class T> template <class U> void S::f(); */
32240 error_at (location, "too many template-parameter-lists");
32241 return false;
32244 /* Parse an optional `::' token indicating that the following name is
32245 from the global namespace. If so, PARSER->SCOPE is set to the
32246 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
32247 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
32248 Returns the new value of PARSER->SCOPE, if the `::' token is
32249 present, and NULL_TREE otherwise. */
32251 static tree
32252 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
32254 cp_token *token;
32256 /* Peek at the next token. */
32257 token = cp_lexer_peek_token (parser->lexer);
32258 /* If we're looking at a `::' token then we're starting from the
32259 global namespace, not our current location. */
32260 if (token->type == CPP_SCOPE)
32262 /* Consume the `::' token. */
32263 cp_lexer_consume_token (parser->lexer);
32264 /* Set the SCOPE so that we know where to start the lookup. */
32265 parser->scope = global_namespace;
32266 parser->qualifying_scope = global_namespace;
32267 parser->object_scope = NULL_TREE;
32269 return parser->scope;
32271 else if (!current_scope_valid_p)
32273 parser->scope = NULL_TREE;
32274 parser->qualifying_scope = NULL_TREE;
32275 parser->object_scope = NULL_TREE;
32278 return NULL_TREE;
32281 /* Returns TRUE if the upcoming token sequence is the start of a
32282 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
32283 declarator is preceded by the `friend' specifier. The parser flags FLAGS
32284 is used to control type-specifier parsing. */
32286 static bool
32287 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
32288 bool friend_p)
32290 bool constructor_p;
32291 bool outside_class_specifier_p;
32292 tree nested_name_specifier;
32293 cp_token *next_token;
32295 /* The common case is that this is not a constructor declarator, so
32296 try to avoid doing lots of work if at all possible. It's not
32297 valid declare a constructor at function scope. */
32298 if (parser->in_function_body)
32299 return false;
32300 /* And only certain tokens can begin a constructor declarator. */
32301 next_token = cp_lexer_peek_token (parser->lexer);
32302 if (next_token->type != CPP_NAME
32303 && next_token->type != CPP_SCOPE
32304 && next_token->type != CPP_NESTED_NAME_SPECIFIER
32305 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
32306 declarator-id of a constructor or destructor. */
32307 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
32308 return false;
32310 /* Parse tentatively; we are going to roll back all of the tokens
32311 consumed here. */
32312 cp_parser_parse_tentatively (parser);
32313 /* Assume that we are looking at a constructor declarator. */
32314 constructor_p = true;
32316 /* Look for the optional `::' operator. */
32317 cp_parser_global_scope_opt (parser,
32318 /*current_scope_valid_p=*/false);
32319 /* Look for the nested-name-specifier. */
32320 nested_name_specifier
32321 = (cp_parser_nested_name_specifier_opt (parser,
32322 /*typename_keyword_p=*/false,
32323 /*check_dependency_p=*/false,
32324 /*type_p=*/false,
32325 /*is_declaration=*/false));
32327 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
32328 if (nested_name_specifier
32329 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
32331 tree s = resolve_typename_type (nested_name_specifier,
32332 /*only_current_p=*/false);
32333 if (TREE_CODE (s) != TYPENAME_TYPE)
32334 nested_name_specifier = s;
32337 outside_class_specifier_p = (!at_class_scope_p ()
32338 || !TYPE_BEING_DEFINED (current_class_type)
32339 || friend_p);
32341 /* Outside of a class-specifier, there must be a
32342 nested-name-specifier. Except in C++17 mode, where we
32343 might be declaring a guiding declaration. */
32344 if (!nested_name_specifier && outside_class_specifier_p
32345 && cxx_dialect < cxx17)
32346 constructor_p = false;
32347 else if (nested_name_specifier == error_mark_node)
32348 constructor_p = false;
32350 /* If we have a class scope, this is easy; DR 147 says that S::S always
32351 names the constructor, and no other qualified name could. */
32352 if (constructor_p && nested_name_specifier
32353 && CLASS_TYPE_P (nested_name_specifier))
32355 tree id = cp_parser_unqualified_id (parser,
32356 /*template_keyword_p=*/false,
32357 /*check_dependency_p=*/false,
32358 /*declarator_p=*/true,
32359 /*optional_p=*/false);
32360 if (is_overloaded_fn (id))
32361 id = DECL_NAME (get_first_fn (id));
32362 if (!constructor_name_p (id, nested_name_specifier))
32363 constructor_p = false;
32365 /* If we still think that this might be a constructor-declarator,
32366 look for a class-name. */
32367 else if (constructor_p)
32369 /* If we have:
32371 template <typename T> struct S {
32372 S();
32375 we must recognize that the nested `S' names a class. */
32376 if (cxx_dialect >= cxx17)
32377 cp_parser_parse_tentatively (parser);
32379 tree type_decl;
32380 type_decl = cp_parser_class_name (parser,
32381 /*typename_keyword_p=*/false,
32382 /*template_keyword_p=*/false,
32383 none_type,
32384 /*check_dependency_p=*/false,
32385 /*class_head_p=*/false,
32386 /*is_declaration=*/false);
32388 if (cxx_dialect >= cxx17
32389 && !cp_parser_parse_definitely (parser))
32391 type_decl = NULL_TREE;
32392 tree tmpl = cp_parser_template_name (parser,
32393 /*template_keyword*/false,
32394 /*check_dependency_p*/false,
32395 /*is_declaration*/false,
32396 none_type,
32397 /*is_identifier*/NULL);
32398 if (DECL_CLASS_TEMPLATE_P (tmpl)
32399 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32400 /* It's a deduction guide, return true. */;
32401 else
32402 cp_parser_simulate_error (parser);
32405 /* If there was no class-name, then this is not a constructor.
32406 Otherwise, if we are in a class-specifier and we aren't
32407 handling a friend declaration, check that its type matches
32408 current_class_type (c++/38313). Note: error_mark_node
32409 is left alone for error recovery purposes. */
32410 constructor_p = (!cp_parser_error_occurred (parser)
32411 && (outside_class_specifier_p
32412 || type_decl == NULL_TREE
32413 || type_decl == error_mark_node
32414 || same_type_p (current_class_type,
32415 TREE_TYPE (type_decl))));
32417 /* If we're still considering a constructor, we have to see a `(',
32418 to begin the parameter-declaration-clause, followed by either a
32419 `)', an `...', or a decl-specifier. We need to check for a
32420 type-specifier to avoid being fooled into thinking that:
32422 S (f) (int);
32424 is a constructor. (It is actually a function named `f' that
32425 takes one parameter (of type `int') and returns a value of type
32426 `S'. */
32427 if (constructor_p
32428 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32429 constructor_p = false;
32431 if (constructor_p
32432 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32433 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32434 /* A parameter declaration begins with a decl-specifier,
32435 which is either the "attribute" keyword, a storage class
32436 specifier, or (usually) a type-specifier. */
32437 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32438 /* GNU attributes can actually appear both at the start of
32439 a parameter and parenthesized declarator.
32440 S (__attribute__((unused)) int);
32441 is a constructor, but
32442 S (__attribute__((unused)) foo) (int);
32443 is a function declaration. [[attribute]] can appear in the
32444 first form too, but not in the second form. */
32445 && !cp_next_tokens_can_be_std_attribute_p (parser))
32447 tree type;
32448 tree pushed_scope = NULL_TREE;
32449 unsigned saved_num_template_parameter_lists;
32451 if (cp_parser_allow_gnu_extensions_p (parser)
32452 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32454 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32455 while (--n)
32456 cp_lexer_consume_token (parser->lexer);
32459 /* Names appearing in the type-specifier should be looked up
32460 in the scope of the class. */
32461 if (current_class_type)
32462 type = NULL_TREE;
32463 else if (type_decl)
32465 type = TREE_TYPE (type_decl);
32466 if (TREE_CODE (type) == TYPENAME_TYPE)
32468 type = resolve_typename_type (type,
32469 /*only_current_p=*/false);
32470 if (TREE_CODE (type) == TYPENAME_TYPE)
32472 cp_parser_abort_tentative_parse (parser);
32473 return false;
32476 pushed_scope = push_scope (type);
32479 /* Inside the constructor parameter list, surrounding
32480 template-parameter-lists do not apply. */
32481 saved_num_template_parameter_lists
32482 = parser->num_template_parameter_lists;
32483 parser->num_template_parameter_lists = 0;
32485 /* Look for the type-specifier. It's not optional, but its typename
32486 might be. Unless this is a friend declaration; we don't want to
32487 treat
32489 friend S (T::fn)(int);
32491 as a constructor, but with P0634, we might assume a type when
32492 looking for the type-specifier. It is actually a function named
32493 `T::fn' that takes one parameter (of type `int') and returns a
32494 value of type `S'. Constructors can be friends, but they must
32495 use a qualified name.
32497 Parse with an empty set of declaration specifiers since we're
32498 trying to match a decl-specifier-seq of the first parameter.
32499 This must be non-null so that cp_parser_simple_type_specifier
32500 will recognize a constrained placeholder type such as:
32501 'C<int> auto' where C is a type concept. */
32502 cp_decl_specifier_seq ctor_specs;
32503 clear_decl_specs (&ctor_specs);
32504 cp_parser_type_specifier (parser,
32505 (friend_p ? CP_PARSER_FLAGS_NONE
32506 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32507 /*decl_specs=*/&ctor_specs,
32508 /*is_declarator=*/true,
32509 /*declares_class_or_enum=*/NULL,
32510 /*is_cv_qualifier=*/NULL);
32512 parser->num_template_parameter_lists
32513 = saved_num_template_parameter_lists;
32515 /* Leave the scope of the class. */
32516 if (pushed_scope)
32517 pop_scope (pushed_scope);
32519 constructor_p = !cp_parser_error_occurred (parser);
32523 /* We did not really want to consume any tokens. */
32524 cp_parser_abort_tentative_parse (parser);
32526 return constructor_p;
32529 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32530 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32531 they must be performed once we are in the scope of the function.
32533 Returns the function defined. */
32535 static tree
32536 cp_parser_function_definition_from_specifiers_and_declarator
32537 (cp_parser* parser,
32538 cp_decl_specifier_seq *decl_specifiers,
32539 tree attributes,
32540 const cp_declarator *declarator)
32542 tree fn;
32543 bool success_p;
32545 /* Begin the function-definition. */
32546 success_p = start_function (decl_specifiers, declarator, attributes);
32548 /* The things we're about to see are not directly qualified by any
32549 template headers we've seen thus far. */
32550 reset_specialization ();
32552 /* If there were names looked up in the decl-specifier-seq that we
32553 did not check, check them now. We must wait until we are in the
32554 scope of the function to perform the checks, since the function
32555 might be a friend. */
32556 perform_deferred_access_checks (tf_warning_or_error);
32558 if (success_p)
32560 cp_finalize_omp_declare_simd (parser, current_function_decl);
32561 parser->omp_declare_simd = NULL;
32562 cp_finalize_oacc_routine (parser, current_function_decl, true);
32563 parser->oacc_routine = NULL;
32566 if (!success_p)
32568 /* Skip the entire function. */
32569 cp_parser_skip_to_end_of_block_or_statement (parser);
32570 fn = error_mark_node;
32572 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32574 /* Seen already, skip it. An error message has already been output. */
32575 cp_parser_skip_to_end_of_block_or_statement (parser);
32576 fn = current_function_decl;
32577 current_function_decl = NULL_TREE;
32578 /* If this is a function from a class, pop the nested class. */
32579 if (current_class_name)
32580 pop_nested_class ();
32582 else
32584 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32585 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32586 fn = cp_parser_function_definition_after_declarator (parser,
32587 /*inline_p=*/false);
32590 return fn;
32593 /* Parse the part of a function-definition that follows the
32594 declarator. INLINE_P is TRUE iff this function is an inline
32595 function defined within a class-specifier.
32597 Returns the function defined. */
32599 static tree
32600 cp_parser_function_definition_after_declarator (cp_parser* parser,
32601 bool inline_p)
32603 tree fn;
32604 bool saved_in_unbraced_linkage_specification_p;
32605 bool saved_in_function_body;
32606 unsigned saved_num_template_parameter_lists;
32607 cp_token *token;
32608 bool fully_implicit_function_template_p
32609 = parser->fully_implicit_function_template_p;
32610 parser->fully_implicit_function_template_p = false;
32611 tree implicit_template_parms
32612 = parser->implicit_template_parms;
32613 parser->implicit_template_parms = 0;
32614 cp_binding_level* implicit_template_scope
32615 = parser->implicit_template_scope;
32616 parser->implicit_template_scope = 0;
32618 saved_in_function_body = parser->in_function_body;
32619 parser->in_function_body = true;
32620 /* If the next token is `return', then the code may be trying to
32621 make use of the "named return value" extension that G++ used to
32622 support. */
32623 token = cp_lexer_peek_token (parser->lexer);
32624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32626 /* Consume the `return' keyword. */
32627 cp_lexer_consume_token (parser->lexer);
32628 /* Look for the identifier that indicates what value is to be
32629 returned. */
32630 cp_parser_identifier (parser);
32631 /* Issue an error message. */
32632 error_at (token->location,
32633 "named return values are no longer supported");
32634 /* Skip tokens until we reach the start of the function body. */
32635 while (true)
32637 cp_token *token = cp_lexer_peek_token (parser->lexer);
32638 if (token->type == CPP_OPEN_BRACE
32639 || token->type == CPP_EOF
32640 || token->type == CPP_PRAGMA_EOL)
32641 break;
32642 cp_lexer_consume_token (parser->lexer);
32645 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32646 anything declared inside `f'. */
32647 saved_in_unbraced_linkage_specification_p
32648 = parser->in_unbraced_linkage_specification_p;
32649 parser->in_unbraced_linkage_specification_p = false;
32650 /* Inside the function, surrounding template-parameter-lists do not
32651 apply. */
32652 saved_num_template_parameter_lists
32653 = parser->num_template_parameter_lists;
32654 parser->num_template_parameter_lists = 0;
32656 /* If the next token is `try', `__transaction_atomic', or
32657 `__transaction_relaxed`, then we are looking at either function-try-block
32658 or function-transaction-block. Note that all of these include the
32659 function-body. */
32660 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32661 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32662 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32663 RID_TRANSACTION_RELAXED))
32664 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32665 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32666 cp_parser_function_try_block (parser);
32667 else
32668 cp_parser_ctor_initializer_opt_and_function_body
32669 (parser, /*in_function_try_block=*/false);
32671 /* Finish the function. */
32672 fn = finish_function (inline_p);
32674 if (modules_p ()
32675 && !inline_p
32676 && TYPE_P (DECL_CONTEXT (fn))
32677 && (DECL_DECLARED_INLINE_P (fn)
32678 || processing_template_decl))
32679 set_defining_module (fn);
32681 /* Generate code for it, if necessary. */
32682 expand_or_defer_fn (fn);
32684 /* Restore the saved values. */
32685 parser->in_unbraced_linkage_specification_p
32686 = saved_in_unbraced_linkage_specification_p;
32687 parser->num_template_parameter_lists
32688 = saved_num_template_parameter_lists;
32689 parser->in_function_body = saved_in_function_body;
32691 parser->fully_implicit_function_template_p
32692 = fully_implicit_function_template_p;
32693 parser->implicit_template_parms
32694 = implicit_template_parms;
32695 parser->implicit_template_scope
32696 = implicit_template_scope;
32698 if (parser->fully_implicit_function_template_p)
32699 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32701 return fn;
32704 /* Parse a template-declaration body (following argument list). */
32706 static void
32707 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32708 tree parameter_list,
32709 bool member_p)
32711 tree decl = NULL_TREE;
32712 bool friend_p = false;
32714 /* We just processed one more parameter list. */
32715 ++parser->num_template_parameter_lists;
32717 /* Get the deferred access checks from the parameter list. These
32718 will be checked once we know what is being declared, as for a
32719 member template the checks must be performed in the scope of the
32720 class containing the member. */
32721 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32723 /* Tentatively parse for a new template parameter list, which can either be
32724 the template keyword or a template introduction. */
32725 if (cp_parser_template_declaration_after_export (parser, member_p))
32726 /* OK */;
32727 else if (cxx_dialect >= cxx11
32728 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32729 decl = cp_parser_alias_declaration (parser);
32730 else if (flag_concepts
32731 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32732 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32733 /* -fconcept-ts 'concept bool' syntax is handled below, in
32734 cp_parser_single_declaration. */
32735 decl = cp_parser_concept_definition (parser);
32736 else
32738 cp_token *token = cp_lexer_peek_token (parser->lexer);
32739 decl = cp_parser_single_declaration (parser,
32740 checks,
32741 member_p,
32742 /*explicit_specialization_p=*/false,
32743 &friend_p);
32745 /* If this is a member template declaration, let the front
32746 end know. */
32747 if (member_p && !friend_p && decl)
32749 if (TREE_CODE (decl) == TYPE_DECL)
32750 cp_parser_check_access_in_redeclaration (decl, token->location);
32752 decl = finish_member_template_decl (decl);
32754 else if (friend_p && decl
32755 && DECL_DECLARES_TYPE_P (decl))
32756 make_friend_class (current_class_type, TREE_TYPE (decl),
32757 /*complain=*/true);
32759 /* We are done with the current parameter list. */
32760 --parser->num_template_parameter_lists;
32762 pop_deferring_access_checks ();
32764 /* Finish up. */
32765 finish_template_decl (parameter_list);
32767 /* Check the template arguments for a literal operator template. */
32768 if (decl
32769 && DECL_DECLARES_FUNCTION_P (decl)
32770 && UDLIT_OPER_P (DECL_NAME (decl)))
32772 bool ok = true;
32773 if (parameter_list == NULL_TREE)
32774 ok = false;
32775 else
32777 int num_parms = TREE_VEC_LENGTH (parameter_list);
32778 if (num_parms == 1)
32780 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32781 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32782 if (TREE_CODE (parm) != PARM_DECL)
32783 ok = false;
32784 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32785 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32786 /* OK, C++20 string literal operator template. We don't need
32787 to warn in lower dialects here because we will have already
32788 warned about the template parameter. */;
32789 else if (TREE_TYPE (parm) != char_type_node
32790 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32791 ok = false;
32793 else if (num_parms == 2 && cxx_dialect >= cxx14)
32795 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32796 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32797 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32798 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32799 if (TREE_CODE (parm) != PARM_DECL
32800 || TREE_TYPE (parm) != TREE_TYPE (type)
32801 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32802 ok = false;
32803 else
32804 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32805 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32806 "ISO C++ did not adopt string literal operator templa"
32807 "tes taking an argument pack of characters");
32809 else
32810 ok = false;
32812 if (!ok)
32814 if (cxx_dialect > cxx17)
32815 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32816 "template %qD has invalid parameter list; expected "
32817 "non-type template parameter pack %<<char...>%> or "
32818 "single non-type parameter of class type",
32819 decl);
32820 else
32821 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32822 "template %qD has invalid parameter list; expected "
32823 "non-type template parameter pack %<<char...>%>",
32824 decl);
32828 /* Register member declarations. */
32829 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32830 finish_member_declaration (decl);
32831 /* If DECL is a function template, we must return to parse it later.
32832 (Even though there is no definition, there might be default
32833 arguments that need handling.) */
32834 if (member_p && decl
32835 && DECL_DECLARES_FUNCTION_P (decl))
32836 vec_safe_push (unparsed_funs_with_definitions, decl);
32839 /* Parse a template introduction header for a template-declaration. Returns
32840 false if tentative parse fails. */
32842 static bool
32843 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32845 cp_parser_parse_tentatively (parser);
32847 tree saved_scope = parser->scope;
32848 tree saved_object_scope = parser->object_scope;
32849 tree saved_qualifying_scope = parser->qualifying_scope;
32850 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32852 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32854 /* In classes don't parse valid unnamed bitfields as invalid
32855 template introductions. */
32856 if (member_p)
32857 parser->colon_corrects_to_scope_p = false;
32859 /* Look for the optional `::' operator. */
32860 cp_parser_global_scope_opt (parser,
32861 /*current_scope_valid_p=*/false);
32862 /* Look for the nested-name-specifier. */
32863 cp_parser_nested_name_specifier_opt (parser,
32864 /*typename_keyword_p=*/false,
32865 /*check_dependency_p=*/true,
32866 /*type_p=*/false,
32867 /*is_declaration=*/false);
32869 cp_token *token = cp_lexer_peek_token (parser->lexer);
32870 tree concept_name = cp_parser_identifier (parser);
32872 /* Look up the concept for which we will be matching
32873 template parameters. */
32874 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32875 token->location);
32876 parser->scope = saved_scope;
32877 parser->object_scope = saved_object_scope;
32878 parser->qualifying_scope = saved_qualifying_scope;
32879 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32881 if (concept_name == error_mark_node
32882 || (seen_error () && !concept_definition_p (tmpl_decl)))
32883 cp_parser_simulate_error (parser);
32885 /* Look for opening brace for introduction. */
32886 matching_braces braces;
32887 braces.require_open (parser);
32888 location_t open_loc = input_location;
32890 if (!cp_parser_parse_definitely (parser))
32891 return false;
32893 push_deferring_access_checks (dk_deferred);
32895 /* Build vector of placeholder parameters and grab
32896 matching identifiers. */
32897 tree introduction_list = cp_parser_introduction_list (parser);
32899 /* Look for closing brace for introduction. */
32900 if (!braces.require_close (parser))
32901 return true;
32903 /* The introduction-list shall not be empty. */
32904 int nargs = TREE_VEC_LENGTH (introduction_list);
32905 if (nargs == 0)
32907 /* In cp_parser_introduction_list we have already issued an error. */
32908 return true;
32911 if (tmpl_decl == error_mark_node)
32913 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32914 token->location);
32915 return true;
32918 /* Build and associate the constraint. */
32919 location_t introduction_loc = make_location (open_loc,
32920 start_token->location,
32921 parser->lexer);
32922 tree parms = finish_template_introduction (tmpl_decl,
32923 introduction_list,
32924 introduction_loc);
32925 if (parms && parms != error_mark_node)
32927 if (!flag_concepts_ts)
32928 pedwarn (introduction_loc, 0, "template-introductions"
32929 " are not part of C++20 concepts; use %qs to enable",
32930 "-fconcepts-ts");
32932 cp_parser_template_declaration_after_parameters (parser, parms,
32933 member_p);
32934 return true;
32937 if (parms == NULL_TREE)
32938 error_at (token->location, "no matching concept for template-introduction");
32940 return true;
32943 /* Parse a normal template-declaration following the template keyword. */
32945 static void
32946 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32948 tree parameter_list;
32949 bool need_lang_pop;
32950 location_t location = input_location;
32952 /* Look for the `<' token. */
32953 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32954 return;
32955 if (at_class_scope_p () && current_function_decl)
32957 /* 14.5.2.2 [temp.mem]
32959 A local class shall not have member templates. */
32960 error_at (location,
32961 "invalid declaration of member template in local class");
32962 cp_parser_skip_to_end_of_block_or_statement (parser);
32963 return;
32965 /* [temp]
32967 A template ... shall not have C linkage. */
32968 if (current_lang_name == lang_name_c)
32970 error_at (location, "template with C linkage");
32971 maybe_show_extern_c_location ();
32972 /* Give it C++ linkage to avoid confusing other parts of the
32973 front end. */
32974 push_lang_context (lang_name_cplusplus);
32975 need_lang_pop = true;
32977 else
32978 need_lang_pop = false;
32980 /* We cannot perform access checks on the template parameter
32981 declarations until we know what is being declared, just as we
32982 cannot check the decl-specifier list. */
32983 push_deferring_access_checks (dk_deferred);
32985 /* If the next token is `>', then we have an invalid
32986 specialization. Rather than complain about an invalid template
32987 parameter, issue an error message here. */
32988 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32990 cp_parser_error (parser, "invalid explicit specialization");
32991 begin_specialization ();
32992 parameter_list = NULL_TREE;
32994 else
32996 /* Parse the template parameters. */
32997 parameter_list = cp_parser_template_parameter_list (parser);
33000 /* Look for the `>'. */
33001 cp_parser_require_end_of_template_parameter_list (parser);
33003 /* Manage template requirements */
33004 if (flag_concepts)
33006 tree reqs = get_shorthand_constraints (current_template_parms);
33007 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
33008 reqs = combine_constraint_expressions (reqs, treqs);
33009 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
33012 cp_parser_template_declaration_after_parameters (parser, parameter_list,
33013 member_p);
33015 /* For the erroneous case of a template with C linkage, we pushed an
33016 implicit C++ linkage scope; exit that scope now. */
33017 if (need_lang_pop)
33018 pop_lang_context ();
33021 /* Parse a template-declaration, assuming that the `export' (and
33022 `extern') keywords, if present, has already been scanned. MEMBER_P
33023 is as for cp_parser_template_declaration. */
33025 static bool
33026 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
33028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33030 cp_lexer_consume_token (parser->lexer);
33031 cp_parser_explicit_template_declaration (parser, member_p);
33032 return true;
33034 else if (flag_concepts)
33035 return cp_parser_template_introduction (parser, member_p);
33037 return false;
33040 /* Perform the deferred access checks from a template-parameter-list.
33041 CHECKS is a TREE_LIST of access checks, as returned by
33042 get_deferred_access_checks. */
33044 static void
33045 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
33047 ++processing_template_parmlist;
33048 perform_access_checks (checks, tf_warning_or_error);
33049 --processing_template_parmlist;
33052 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
33053 `function-definition' sequence that follows a template header.
33054 If MEMBER_P is true, this declaration appears in a class scope.
33056 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
33057 *FRIEND_P is set to TRUE iff the declaration is a friend. */
33059 static tree
33060 cp_parser_single_declaration (cp_parser* parser,
33061 vec<deferred_access_check, va_gc> *checks,
33062 bool member_p,
33063 bool explicit_specialization_p,
33064 bool* friend_p)
33066 int declares_class_or_enum;
33067 tree decl = NULL_TREE;
33068 cp_decl_specifier_seq decl_specifiers;
33069 bool function_definition_p = false;
33070 cp_token *decl_spec_token_start;
33072 /* This function is only used when processing a template
33073 declaration. */
33074 gcc_assert (innermost_scope_kind () == sk_template_parms
33075 || innermost_scope_kind () == sk_template_spec);
33077 /* Defer access checks until we know what is being declared. */
33078 push_deferring_access_checks (dk_deferred);
33080 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
33081 alternative. */
33082 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
33083 cp_parser_decl_specifier_seq (parser,
33084 (CP_PARSER_FLAGS_OPTIONAL
33085 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
33086 &decl_specifiers,
33087 &declares_class_or_enum);
33089 cp_omp_declare_simd_data odsd;
33090 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
33091 cp_parser_handle_directive_omp_attributes (parser,
33092 &decl_specifiers.attributes,
33093 &odsd, true);
33095 if (friend_p)
33096 *friend_p = cp_parser_friend_p (&decl_specifiers);
33098 /* There are no template typedefs. */
33099 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
33101 error_at (decl_spec_token_start->location,
33102 "template declaration of %<typedef%>");
33103 decl = error_mark_node;
33106 /* Gather up the access checks that occurred the
33107 decl-specifier-seq. */
33108 stop_deferring_access_checks ();
33110 /* Check for the declaration of a template class. */
33111 if (declares_class_or_enum)
33113 if (cp_parser_declares_only_class_p (parser)
33114 || (declares_class_or_enum & 2))
33116 decl = shadow_tag (&decl_specifiers);
33118 /* In this case:
33120 struct C {
33121 friend template <typename T> struct A<T>::B;
33124 A<T>::B will be represented by a TYPENAME_TYPE, and
33125 therefore not recognized by shadow_tag. */
33126 if (friend_p && *friend_p
33127 && !decl
33128 && decl_specifiers.type
33129 && TYPE_P (decl_specifiers.type))
33130 decl = decl_specifiers.type;
33132 if (decl && decl != error_mark_node)
33133 decl = TYPE_NAME (decl);
33134 else
33135 decl = error_mark_node;
33137 /* If this is a declaration, but not a definition, associate
33138 any constraints with the type declaration. Constraints
33139 are associated with definitions in cp_parser_class_specifier. */
33140 if (declares_class_or_enum == 1)
33141 associate_classtype_constraints (TREE_TYPE (decl));
33143 /* Perform access checks for template parameters. */
33144 cp_parser_perform_template_parameter_access_checks (checks);
33146 /* Give a helpful diagnostic for
33147 template <class T> struct A { } a;
33148 if we aren't already recovering from an error. */
33149 if (!cp_parser_declares_only_class_p (parser)
33150 && !seen_error ())
33152 error_at (cp_lexer_peek_token (parser->lexer)->location,
33153 "a class template declaration must not declare "
33154 "anything else");
33155 cp_parser_skip_to_end_of_block_or_statement (parser);
33156 goto out;
33161 /* Complain about missing 'typename' or other invalid type names. */
33162 if (!decl_specifiers.any_type_specifiers_p
33163 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
33165 /* cp_parser_parse_and_diagnose_invalid_type_name calls
33166 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
33167 the rest of this declaration. */
33168 decl = error_mark_node;
33169 goto out;
33172 /* If it's not a template class, try for a template function. If
33173 the next token is a `;', then this declaration does not declare
33174 anything. But, if there were errors in the decl-specifiers, then
33175 the error might well have come from an attempted class-specifier.
33176 In that case, there's no need to warn about a missing declarator. */
33177 if (!decl
33178 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
33179 || decl_specifiers.type != error_mark_node))
33181 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
33182 /* We don't delay parsing for friends, though CWG 2510 may change
33183 that. */
33184 if (member_p && !(friend_p && *friend_p))
33185 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
33186 decl = cp_parser_init_declarator (parser,
33187 flags,
33188 &decl_specifiers,
33189 checks,
33190 /*function_definition_allowed_p=*/true,
33191 member_p,
33192 declares_class_or_enum,
33193 &function_definition_p,
33194 NULL, NULL, NULL);
33196 /* 7.1.1-1 [dcl.stc]
33198 A storage-class-specifier shall not be specified in an explicit
33199 specialization... */
33200 if (decl
33201 && explicit_specialization_p
33202 && decl_specifiers.storage_class != sc_none)
33204 error_at (decl_spec_token_start->location,
33205 "explicit template specialization cannot have a storage class");
33206 decl = error_mark_node;
33209 if (decl && VAR_P (decl))
33210 check_template_variable (decl);
33213 /* Look for a trailing `;' after the declaration. */
33214 if (!function_definition_p
33215 && (decl == error_mark_node
33216 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
33217 cp_parser_skip_to_end_of_block_or_statement (parser);
33219 out:
33220 pop_deferring_access_checks ();
33222 /* Clear any current qualification; whatever comes next is the start
33223 of something new. */
33224 parser->scope = NULL_TREE;
33225 parser->qualifying_scope = NULL_TREE;
33226 parser->object_scope = NULL_TREE;
33228 cp_finalize_omp_declare_simd (parser, &odsd);
33230 return decl;
33233 /* Parse a cast-expression that is not the operand of a unary "&". */
33235 static cp_expr
33236 cp_parser_simple_cast_expression (cp_parser *parser)
33238 return cp_parser_cast_expression (parser, /*address_p=*/false,
33239 /*cast_p=*/false, /*decltype*/false, NULL);
33242 /* Parse a functional cast to TYPE. Returns an expression
33243 representing the cast. */
33245 static cp_expr
33246 cp_parser_functional_cast (cp_parser* parser, tree type)
33248 vec<tree, va_gc> *vec;
33249 tree expression_list;
33250 cp_expr cast;
33252 location_t start_loc = input_location;
33254 if (!type)
33255 type = error_mark_node;
33257 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33259 cp_lexer_set_source_position (parser->lexer);
33260 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33261 expression_list = cp_parser_braced_list (parser);
33262 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
33263 if (TREE_CODE (type) == TYPE_DECL)
33264 type = TREE_TYPE (type);
33266 cast = finish_compound_literal (type, expression_list,
33267 tf_warning_or_error, fcl_functional);
33268 /* Create a location of the form:
33269 type_name{i, f}
33270 ^~~~~~~~~~~~~~~
33271 with caret == start at the start of the type name,
33272 finishing at the closing brace. */
33273 location_t combined_loc = make_location (start_loc, start_loc,
33274 parser->lexer);
33275 cast.set_location (combined_loc);
33276 return cast;
33280 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
33281 /*cast_p=*/true,
33282 /*allow_expansion_p=*/true,
33283 /*non_constant_p=*/NULL);
33284 if (vec == NULL)
33285 expression_list = error_mark_node;
33286 else
33288 expression_list = build_tree_list_vec (vec);
33289 release_tree_vector (vec);
33292 /* Create a location of the form:
33293 float(i)
33294 ^~~~~~~~
33295 with caret == start at the start of the type name,
33296 finishing at the closing paren. */
33297 location_t combined_loc = make_location (start_loc, start_loc,
33298 parser->lexer);
33299 cast = build_functional_cast (combined_loc, type, expression_list,
33300 tf_warning_or_error);
33302 /* [expr.const]/1: In an integral constant expression "only type
33303 conversions to integral or enumeration type can be used". */
33304 if (TREE_CODE (type) == TYPE_DECL)
33305 type = TREE_TYPE (type);
33306 if (cast != error_mark_node
33307 && !cast_valid_in_integral_constant_expression_p (type)
33308 && cp_parser_non_integral_constant_expression (parser,
33309 NIC_CONSTRUCTOR))
33310 return error_mark_node;
33312 return cast;
33315 /* Save the tokens that make up the body of a member function defined
33316 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
33317 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
33318 specifiers applied to the declaration. Returns the FUNCTION_DECL
33319 for the member function. */
33321 static tree
33322 cp_parser_save_member_function_body (cp_parser* parser,
33323 cp_decl_specifier_seq *decl_specifiers,
33324 cp_declarator *declarator,
33325 tree attributes)
33327 cp_token *first;
33328 cp_token *last;
33329 tree fn;
33330 bool function_try_block = false;
33332 /* Create the FUNCTION_DECL. */
33333 fn = grokmethod (decl_specifiers, declarator, attributes);
33334 cp_finalize_omp_declare_simd (parser, fn);
33335 cp_finalize_oacc_routine (parser, fn, true);
33336 /* If something went badly wrong, bail out now. */
33337 if (fn == error_mark_node)
33339 /* If there's a function-body, skip it. */
33340 if (cp_parser_token_starts_function_definition_p
33341 (cp_lexer_peek_token (parser->lexer)))
33342 cp_parser_skip_to_end_of_block_or_statement (parser);
33343 return error_mark_node;
33346 /* Remember it, if there are default args to post process. */
33347 cp_parser_save_default_args (parser, fn);
33349 /* Save away the tokens that make up the body of the
33350 function. */
33351 first = parser->lexer->next_token;
33353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
33354 cp_lexer_consume_token (parser->lexer);
33355 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33356 RID_TRANSACTION_ATOMIC))
33358 cp_lexer_consume_token (parser->lexer);
33359 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
33360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
33361 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
33362 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
33363 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
33364 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
33365 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
33367 cp_lexer_consume_token (parser->lexer);
33368 cp_lexer_consume_token (parser->lexer);
33369 cp_lexer_consume_token (parser->lexer);
33370 cp_lexer_consume_token (parser->lexer);
33371 cp_lexer_consume_token (parser->lexer);
33373 else
33374 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33375 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33377 cp_lexer_consume_token (parser->lexer);
33378 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33379 break;
33383 /* Handle function try blocks. */
33384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33386 cp_lexer_consume_token (parser->lexer);
33387 function_try_block = true;
33389 /* We can have braced-init-list mem-initializers before the fn body. */
33390 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33392 cp_lexer_consume_token (parser->lexer);
33393 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33395 /* cache_group will stop after an un-nested { } pair, too. */
33396 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33397 break;
33399 /* variadic mem-inits have ... after the ')'. */
33400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33401 cp_lexer_consume_token (parser->lexer);
33404 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33405 /* Handle function try blocks. */
33406 if (function_try_block)
33407 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33408 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33409 last = parser->lexer->next_token;
33411 /* Save away the inline definition; we will process it when the
33412 class is complete. */
33413 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33414 DECL_PENDING_INLINE_P (fn) = 1;
33416 /* We need to know that this was defined in the class, so that
33417 friend templates are handled correctly. */
33418 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33420 /* Add FN to the queue of functions to be parsed later. */
33421 vec_safe_push (unparsed_funs_with_definitions, fn);
33423 return fn;
33426 /* Save the tokens that make up the in-class initializer for a non-static
33427 data member. Returns a DEFERRED_PARSE. */
33429 static tree
33430 cp_parser_save_nsdmi (cp_parser* parser)
33432 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33435 /* Parse a template-argument-list, as well as the trailing ">" (but
33436 not the opening "<"). See cp_parser_template_argument_list for the
33437 return value. */
33439 static tree
33440 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33442 tree arguments;
33443 tree saved_scope;
33444 tree saved_qualifying_scope;
33445 tree saved_object_scope;
33446 bool saved_greater_than_is_operator_p;
33448 /* [temp.names]
33450 When parsing a template-id, the first non-nested `>' is taken as
33451 the end of the template-argument-list rather than a greater-than
33452 operator. */
33453 saved_greater_than_is_operator_p
33454 = parser->greater_than_is_operator_p;
33455 parser->greater_than_is_operator_p = false;
33456 /* Parsing the argument list may modify SCOPE, so we save it
33457 here. */
33458 saved_scope = parser->scope;
33459 saved_qualifying_scope = parser->qualifying_scope;
33460 saved_object_scope = parser->object_scope;
33461 /* We need to evaluate the template arguments, even though this
33462 template-id may be nested within a "sizeof". */
33463 cp_evaluated ev;
33464 /* Parse the template-argument-list itself. */
33465 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33466 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33467 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33468 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33470 arguments = make_tree_vec (0);
33471 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33473 else
33474 arguments = cp_parser_template_argument_list (parser);
33475 /* Look for the `>' that ends the template-argument-list. If we find
33476 a '>>' instead, it's probably just a typo. */
33477 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33479 if (cxx_dialect != cxx98)
33481 /* In C++0x, a `>>' in a template argument list or cast
33482 expression is considered to be two separate `>'
33483 tokens. So, change the current token to a `>', but don't
33484 consume it: it will be consumed later when the outer
33485 template argument list (or cast expression) is parsed.
33486 Note that this replacement of `>' for `>>' is necessary
33487 even if we are parsing tentatively: in the tentative
33488 case, after calling
33489 cp_parser_enclosed_template_argument_list we will always
33490 throw away all of the template arguments and the first
33491 closing `>', either because the template argument list
33492 was erroneous or because we are replacing those tokens
33493 with a CPP_TEMPLATE_ID token. The second `>' (which will
33494 not have been thrown away) is needed either to close an
33495 outer template argument list or to complete a new-style
33496 cast. */
33497 cp_token *token = cp_lexer_peek_token (parser->lexer);
33498 token->type = CPP_GREATER;
33500 else if (!saved_greater_than_is_operator_p)
33502 /* If we're in a nested template argument list, the '>>' has
33503 to be a typo for '> >'. We emit the error message, but we
33504 continue parsing and we push a '>' as next token, so that
33505 the argument list will be parsed correctly. Note that the
33506 global source location is still on the token before the
33507 '>>', so we need to say explicitly where we want it. */
33508 cp_token *token = cp_lexer_peek_token (parser->lexer);
33509 gcc_rich_location richloc (token->location);
33510 richloc.add_fixit_replace ("> >");
33511 error_at (&richloc, "%<>>%> should be %<> >%> "
33512 "within a nested template argument list");
33514 token->type = CPP_GREATER;
33516 else
33518 /* If this is not a nested template argument list, the '>>'
33519 is a typo for '>'. Emit an error message and continue.
33520 Same deal about the token location, but here we can get it
33521 right by consuming the '>>' before issuing the diagnostic. */
33522 cp_token *token = cp_lexer_consume_token (parser->lexer);
33523 error_at (token->location,
33524 "spurious %<>>%>, use %<>%> to terminate "
33525 "a template argument list");
33528 /* Similarly for >>= and >=. */
33529 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33530 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33532 cp_token *token = cp_lexer_consume_token (parser->lexer);
33533 gcc_rich_location richloc (token->location);
33534 enum cpp_ttype new_type;
33535 const char *replacement;
33536 if (token->type == CPP_GREATER_EQ)
33538 replacement = "> =";
33539 new_type = CPP_EQ;
33541 else if (!saved_greater_than_is_operator_p)
33543 if (cxx_dialect != cxx98)
33544 replacement = ">> =";
33545 else
33546 replacement = "> > =";
33547 new_type = CPP_GREATER;
33549 else
33551 replacement = "> >=";
33552 new_type = CPP_GREATER_EQ;
33554 richloc.add_fixit_replace (replacement);
33555 error_at (&richloc, "%qs should be %qs to terminate a template "
33556 "argument list",
33557 cpp_type2name (token->type, token->flags), replacement);
33558 token->type = new_type;
33560 else
33561 cp_parser_require_end_of_template_parameter_list (parser);
33562 /* The `>' token might be a greater-than operator again now. */
33563 parser->greater_than_is_operator_p
33564 = saved_greater_than_is_operator_p;
33565 /* Restore the SAVED_SCOPE. */
33566 parser->scope = saved_scope;
33567 parser->qualifying_scope = saved_qualifying_scope;
33568 parser->object_scope = saved_object_scope;
33570 return arguments;
33573 /* MEMBER_FUNCTION is a member function, or a friend. If default
33574 arguments, or the body of the function have not yet been parsed,
33575 parse them now. */
33577 static void
33578 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33580 auto_timevar tv (TV_PARSE_INMETH);
33582 /* If this member is a template, get the underlying
33583 FUNCTION_DECL. */
33584 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33585 member_function = DECL_TEMPLATE_RESULT (member_function);
33587 /* There should not be any class definitions in progress at this
33588 point; the bodies of members are only parsed outside of all class
33589 definitions. */
33590 gcc_assert (parser->num_classes_being_defined == 0);
33591 /* While we're parsing the member functions we might encounter more
33592 classes. We want to handle them right away, but we don't want
33593 them getting mixed up with functions that are currently in the
33594 queue. */
33595 push_unparsed_function_queues (parser);
33597 /* Make sure that any template parameters are in scope. */
33598 maybe_begin_member_template_processing (member_function);
33600 /* If the body of the function has not yet been parsed, parse it
33601 now. Except if the tokens have been purged (PR c++/39751). */
33602 if (DECL_PENDING_INLINE_P (member_function)
33603 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33605 tree function_scope;
33606 cp_token_cache *tokens;
33608 /* The function is no longer pending; we are processing it. */
33609 tokens = DECL_PENDING_INLINE_INFO (member_function);
33610 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33611 DECL_PENDING_INLINE_P (member_function) = 0;
33613 /* If this is a local class, enter the scope of the containing
33614 function. */
33615 function_scope = current_function_decl;
33616 if (function_scope)
33617 push_function_context ();
33619 /* Push the body of the function onto the lexer stack. */
33620 cp_parser_push_lexer_for_tokens (parser, tokens);
33622 /* Let the front end know that we going to be defining this
33623 function. */
33624 start_preparsed_function (member_function, NULL_TREE,
33625 SF_PRE_PARSED | SF_INCLASS_INLINE);
33627 /* #pragma omp declare reduction needs special parsing. */
33628 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33630 parser->lexer->in_pragma = true;
33631 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33632 finish_function (/*inline_p=*/true);
33633 cp_check_omp_declare_reduction (member_function);
33635 else
33636 /* Now, parse the body of the function. */
33637 cp_parser_function_definition_after_declarator (parser,
33638 /*inline_p=*/true);
33640 /* Leave the scope of the containing function. */
33641 if (function_scope)
33642 pop_function_context ();
33643 cp_parser_pop_lexer (parser);
33646 /* Remove any template parameters from the symbol table. */
33647 maybe_end_member_template_processing ();
33649 /* Restore the queue. */
33650 pop_unparsed_function_queues (parser);
33653 /* If DECL contains any default args, remember it on the unparsed
33654 functions queue. */
33656 static void
33657 cp_parser_save_default_args (cp_parser* parser, tree decl)
33659 tree probe;
33661 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33662 probe;
33663 probe = TREE_CHAIN (probe))
33664 if (TREE_PURPOSE (probe))
33666 cp_default_arg_entry entry = {current_class_type, decl};
33667 vec_safe_push (unparsed_funs_with_default_args, entry);
33668 break;
33671 /* Remember if there is a noexcept-specifier to post process. */
33672 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33673 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33674 vec_safe_push (unparsed_noexcepts, decl);
33676 /* Contracts are deferred. */
33677 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33678 if (cxx_contract_attribute_p (attr))
33680 vec_safe_push (unparsed_contracts, decl);
33681 break;
33685 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33686 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33687 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33688 from the parameter-type-list. */
33690 static tree
33691 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33692 tree default_arg, tree parmtype)
33694 cp_token_cache *tokens;
33695 tree parsed_arg;
33697 if (default_arg == error_mark_node)
33698 return error_mark_node;
33700 /* Push the saved tokens for the default argument onto the parser's
33701 lexer stack. */
33702 tokens = DEFPARSE_TOKENS (default_arg);
33703 cp_parser_push_lexer_for_tokens (parser, tokens);
33705 start_lambda_scope (decl);
33707 /* Parse the default argument. */
33708 parsed_arg = cp_parser_initializer (parser);
33709 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33710 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33712 finish_lambda_scope ();
33714 if (parsed_arg == error_mark_node)
33715 cp_parser_skip_to_end_of_statement (parser);
33717 if (!processing_template_decl)
33719 /* In a non-template class, check conversions now. In a template,
33720 we'll wait and instantiate these as needed. */
33721 if (TREE_CODE (decl) == PARM_DECL)
33722 parsed_arg = check_default_argument (parmtype, parsed_arg,
33723 tf_warning_or_error);
33724 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33725 parsed_arg = error_mark_node;
33726 else
33727 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33730 /* If the token stream has not been completely used up, then
33731 there was extra junk after the end of the default
33732 argument. */
33733 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33735 if (TREE_CODE (decl) == PARM_DECL)
33736 cp_parser_error (parser, "expected %<,%>");
33737 else
33738 cp_parser_error (parser, "expected %<;%>");
33741 /* Revert to the main lexer. */
33742 cp_parser_pop_lexer (parser);
33744 return parsed_arg;
33747 /* FIELD is a non-static data member with an initializer which we saved for
33748 later; parse it now. */
33750 static void
33751 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33753 tree def;
33755 maybe_begin_member_template_processing (field);
33757 push_unparsed_function_queues (parser);
33758 def = cp_parser_late_parse_one_default_arg (parser, field,
33759 DECL_INITIAL (field),
33760 NULL_TREE);
33761 pop_unparsed_function_queues (parser);
33763 maybe_end_member_template_processing ();
33765 DECL_INITIAL (field) = def;
33768 /* FN is a FUNCTION_DECL which may contains a parameter with an
33769 unparsed DEFERRED_PARSE. Parse the default args now. This function
33770 assumes that the current scope is the scope in which the default
33771 argument should be processed. */
33773 static void
33774 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33776 unsigned char saved_local_variables_forbidden_p;
33778 /* While we're parsing the default args, we might (due to the
33779 statement expression extension) encounter more classes. We want
33780 to handle them right away, but we don't want them getting mixed
33781 up with default args that are currently in the queue. */
33782 push_unparsed_function_queues (parser);
33784 /* Local variable names (and the `this' keyword) may not appear
33785 in a default argument. */
33786 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33787 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33789 push_defarg_context (fn);
33791 begin_scope (sk_function_parms, fn);
33793 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33794 pushdecl clears DECL_CHAIN. */
33795 releasing_vec parms;
33796 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33797 parmdecl = DECL_CHAIN (parmdecl))
33798 vec_safe_push (parms, parmdecl);
33800 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33801 for (int i = 0;
33802 parm && parm != void_list_node;
33803 parm = TREE_CHAIN (parm),
33804 ++i)
33806 tree default_arg = TREE_PURPOSE (parm);
33807 tree parsed_arg;
33809 tree parmdecl = parms[i];
33810 pushdecl (parmdecl);
33812 if (!default_arg)
33813 continue;
33815 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33816 /* This can happen for a friend declaration for a function
33817 already declared with default arguments. */
33818 continue;
33820 parsed_arg
33821 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33822 default_arg,
33823 TREE_VALUE (parm));
33824 TREE_PURPOSE (parm) = parsed_arg;
33826 /* Update any instantiations we've already created. */
33827 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33828 TREE_PURPOSE (copy) = parsed_arg;
33831 pop_bindings_and_leave_scope ();
33833 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33834 parm = NULL_TREE;
33835 for (int i = parms->length () - 1; i >= 0; --i)
33837 DECL_CHAIN (parms[i]) = parm;
33838 parm = parms[i];
33841 pop_defarg_context ();
33843 /* Make sure no default arg is missing. */
33844 check_default_args (fn);
33846 /* Restore the state of local_variables_forbidden_p. */
33847 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33849 /* Restore the queue. */
33850 pop_unparsed_function_queues (parser);
33853 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33855 sizeof ... ( identifier )
33857 where the 'sizeof' token has already been consumed. */
33859 static tree
33860 cp_parser_sizeof_pack (cp_parser *parser)
33862 /* Consume the `...'. */
33863 cp_lexer_consume_token (parser->lexer);
33864 maybe_warn_variadic_templates ();
33866 matching_parens parens;
33867 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33868 if (paren)
33869 parens.consume_open (parser);
33870 else
33871 permerror (cp_lexer_peek_token (parser->lexer)->location,
33872 "%<sizeof...%> argument must be surrounded by parentheses");
33874 cp_token *token = cp_lexer_peek_token (parser->lexer);
33875 tree name = cp_parser_identifier (parser);
33876 if (name == error_mark_node)
33877 return error_mark_node;
33878 /* The name is not qualified. */
33879 parser->scope = NULL_TREE;
33880 parser->qualifying_scope = NULL_TREE;
33881 parser->object_scope = NULL_TREE;
33882 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33883 if (expr == error_mark_node)
33884 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33885 token->location);
33886 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33887 expr = TREE_TYPE (expr);
33888 else if (TREE_CODE (expr) == CONST_DECL)
33889 expr = DECL_INITIAL (expr);
33890 expr = make_pack_expansion (expr);
33891 if (expr != error_mark_node)
33892 PACK_EXPANSION_SIZEOF_P (expr) = true;
33894 if (paren)
33895 parens.require_close (parser);
33897 return expr;
33900 /* Parse the operand of `sizeof' (or a similar operator). Returns
33901 either a TYPE or an expression, depending on the form of the
33902 input. The KEYWORD indicates which kind of expression we have
33903 encountered. */
33905 static tree
33906 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33908 tree expr = NULL_TREE;
33909 const char *saved_message;
33910 const char *saved_message_arg;
33911 bool saved_integral_constant_expression_p;
33912 bool saved_non_integral_constant_expression_p;
33914 /* If it's a `...', then we are computing the length of a parameter
33915 pack. */
33916 if (keyword == RID_SIZEOF
33917 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33918 return cp_parser_sizeof_pack (parser);
33920 /* Types cannot be defined in a `sizeof' expression. Save away the
33921 old message. */
33922 saved_message = parser->type_definition_forbidden_message;
33923 saved_message_arg = parser->type_definition_forbidden_message_arg;
33924 parser->type_definition_forbidden_message
33925 = G_("types may not be defined in %qs expressions");
33926 parser->type_definition_forbidden_message_arg
33927 = IDENTIFIER_POINTER (ridpointers[keyword]);
33929 /* The restrictions on constant-expressions do not apply inside
33930 sizeof expressions. */
33931 saved_integral_constant_expression_p
33932 = parser->integral_constant_expression_p;
33933 saved_non_integral_constant_expression_p
33934 = parser->non_integral_constant_expression_p;
33935 parser->integral_constant_expression_p = false;
33937 auto cleanup = make_temp_override
33938 (parser->auto_is_implicit_function_template_parm_p, false);
33940 /* Do not actually evaluate the expression. */
33941 ++cp_unevaluated_operand;
33942 ++c_inhibit_evaluation_warnings;
33943 /* If it's a `(', then we might be looking at the type-id
33944 construction. */
33945 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33947 tree type = NULL_TREE;
33949 tentative_firewall firewall (parser);
33951 /* We can't be sure yet whether we're looking at a type-id or an
33952 expression. */
33953 cp_parser_parse_tentatively (parser);
33955 matching_parens parens;
33956 parens.consume_open (parser);
33958 /* Note: as a GNU Extension, compound literals are considered
33959 postfix-expressions as they are in C99, so they are valid
33960 arguments to sizeof. See comment in cp_parser_cast_expression
33961 for details. */
33962 if (cp_parser_compound_literal_p (parser))
33963 cp_parser_simulate_error (parser);
33964 else
33966 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33967 parser->in_type_id_in_expr_p = true;
33968 /* Look for the type-id. */
33969 type = cp_parser_type_id (parser);
33970 /* Look for the closing `)'. */
33971 parens.require_close (parser);
33972 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33975 /* If all went well, then we're done. */
33976 if (cp_parser_parse_definitely (parser))
33977 expr = type;
33978 else
33980 /* Commit to the tentative_firewall so we get syntax errors. */
33981 cp_parser_commit_to_tentative_parse (parser);
33983 expr = cp_parser_unary_expression (parser);
33986 else
33987 expr = cp_parser_unary_expression (parser);
33989 /* Go back to evaluating expressions. */
33990 --cp_unevaluated_operand;
33991 --c_inhibit_evaluation_warnings;
33993 /* And restore the old one. */
33994 parser->type_definition_forbidden_message = saved_message;
33995 parser->type_definition_forbidden_message_arg = saved_message_arg;
33996 parser->integral_constant_expression_p
33997 = saved_integral_constant_expression_p;
33998 parser->non_integral_constant_expression_p
33999 = saved_non_integral_constant_expression_p;
34001 return expr;
34004 /* If the current declaration has no declarator, return true. */
34006 static bool
34007 cp_parser_declares_only_class_p (cp_parser *parser)
34009 /* If the next token is a `;' or a `,' then there is no
34010 declarator. */
34011 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34012 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
34015 /* Update the DECL_SPECS to reflect the storage class indicated by
34016 KEYWORD. */
34018 static void
34019 cp_parser_set_storage_class (cp_parser *parser,
34020 cp_decl_specifier_seq *decl_specs,
34021 enum rid keyword,
34022 cp_token *token)
34024 cp_storage_class storage_class;
34026 switch (keyword)
34028 case RID_AUTO:
34029 storage_class = sc_auto;
34030 break;
34031 case RID_REGISTER:
34032 storage_class = sc_register;
34033 break;
34034 case RID_STATIC:
34035 storage_class = sc_static;
34036 break;
34037 case RID_EXTERN:
34038 storage_class = sc_extern;
34039 break;
34040 case RID_MUTABLE:
34041 storage_class = sc_mutable;
34042 break;
34043 default:
34044 gcc_unreachable ();
34047 if (parser->in_unbraced_linkage_specification_p)
34049 error_at (token->location, "invalid use of %qD in linkage specification",
34050 ridpointers[keyword]);
34051 return;
34053 else if (decl_specs->storage_class != sc_none)
34055 if (decl_specs->conflicting_specifiers_p)
34056 return;
34057 gcc_rich_location richloc (token->location);
34058 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
34059 if (decl_specs->storage_class == storage_class)
34060 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
34061 else
34062 error_at (&richloc,
34063 "%qD specifier conflicts with %qs",
34064 ridpointers[keyword],
34065 cp_storage_class_name[decl_specs->storage_class]);
34066 decl_specs->conflicting_specifiers_p = true;
34067 return;
34070 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
34071 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
34072 && decl_specs->gnu_thread_keyword_p)
34074 pedwarn (decl_specs->locations[ds_thread], 0,
34075 "%<__thread%> before %qD", ridpointers[keyword]);
34078 decl_specs->storage_class = storage_class;
34079 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
34081 /* A storage class specifier cannot be applied alongside a typedef
34082 specifier. If there is a typedef specifier present then set
34083 conflicting_specifiers_p which will trigger an error later
34084 on in grokdeclarator. */
34085 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34086 && !decl_specs->conflicting_specifiers_p)
34088 gcc_rich_location richloc (token->location);
34089 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
34090 error_at (&richloc,
34091 "%qD specifier conflicts with %<typedef%>",
34092 ridpointers[keyword]);
34093 decl_specs->conflicting_specifiers_p = true;
34097 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
34098 is true, the type is a class or enum definition. */
34100 static void
34101 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
34102 tree type_spec,
34103 cp_token *token,
34104 bool type_definition_p)
34106 decl_specs->any_specifiers_p = true;
34108 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
34109 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
34110 this is what happened. In system headers, we ignore these
34111 declarations so that G++ can work with system headers that are not
34112 C++-safe. */
34113 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34114 && !type_definition_p
34115 && TYPE_P (type_spec)
34116 && (type_spec == boolean_type_node
34117 || type_spec == char8_type_node
34118 || type_spec == char16_type_node
34119 || type_spec == char32_type_node
34120 || extended_float_type_p (type_spec)
34121 || type_spec == wchar_type_node)
34122 && (decl_specs->type
34123 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
34124 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
34125 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
34126 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
34128 decl_specs->redefined_builtin_type = type_spec;
34129 set_and_check_decl_spec_loc (decl_specs,
34130 ds_redefined_builtin_type_spec,
34131 token);
34132 if (!decl_specs->type)
34134 decl_specs->type = type_spec;
34135 decl_specs->type_definition_p = false;
34136 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
34139 else if (decl_specs->type)
34140 decl_specs->multiple_types_p = true;
34141 else
34143 decl_specs->type = type_spec;
34144 decl_specs->type_definition_p = type_definition_p;
34145 decl_specs->redefined_builtin_type = NULL_TREE;
34146 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
34150 /* True iff TOKEN is the GNU keyword __thread. */
34152 static bool
34153 token_is__thread (cp_token *token)
34155 gcc_assert (token->keyword == RID_THREAD);
34156 return id_equal (token->u.value, "__thread");
34159 /* Set the location for a declarator specifier and check if it is
34160 duplicated.
34162 DECL_SPECS is the sequence of declarator specifiers onto which to
34163 set the location.
34165 DS is the single declarator specifier to set which location is to
34166 be set onto the existing sequence of declarators.
34168 LOCATION is the location for the declarator specifier to
34169 consider. */
34171 static void
34172 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
34173 cp_decl_spec ds, cp_token *token)
34175 gcc_assert (ds < ds_last);
34177 if (decl_specs == NULL)
34178 return;
34180 location_t location = token->location;
34182 if (decl_specs->locations[ds] == 0)
34184 decl_specs->locations[ds] = location;
34185 if (ds == ds_thread)
34186 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
34188 else
34190 if (ds == ds_long)
34192 if (decl_specs->locations[ds_long_long] != 0)
34193 error_at (location,
34194 "%<long long long%> is too long for GCC");
34195 else
34197 decl_specs->locations[ds_long_long] = location;
34198 pedwarn_cxx98 (location,
34199 OPT_Wlong_long,
34200 "ISO C++ 1998 does not support %<long long%>");
34203 else if (ds == ds_thread)
34205 bool gnu = token_is__thread (token);
34206 gcc_rich_location richloc (location);
34207 if (gnu != decl_specs->gnu_thread_keyword_p)
34209 richloc.add_range (decl_specs->locations[ds_thread]);
34210 error_at (&richloc,
34211 "both %<__thread%> and %<thread_local%> specified");
34213 else
34215 richloc.add_fixit_remove ();
34216 error_at (&richloc, "duplicate %qD", token->u.value);
34219 else
34221 /* These correspond to cp-tree.h:cp_decl_spec,
34222 changes here should also be reflected there. */
34223 static const char *const decl_spec_names[] = {
34224 "signed",
34225 "unsigned",
34226 "short",
34227 "long",
34228 "const",
34229 "volatile",
34230 "restrict",
34231 "inline",
34232 "virtual",
34233 "explicit",
34234 "friend",
34235 "typedef",
34236 "using",
34237 "constexpr",
34238 "__complex",
34239 "constinit",
34240 "consteval",
34241 "this"
34243 gcc_rich_location richloc (location);
34244 richloc.add_fixit_remove ();
34245 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
34250 /* Return true iff the declarator specifier DS is present in the
34251 sequence of declarator specifiers DECL_SPECS. */
34253 bool
34254 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
34255 cp_decl_spec ds)
34257 gcc_assert (ds < ds_last);
34259 if (decl_specs == NULL)
34260 return false;
34262 return decl_specs->locations[ds] != 0;
34265 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
34266 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
34268 static bool
34269 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
34271 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
34274 /* Issue an error message indicating that TOKEN_DESC was expected.
34275 If KEYWORD is true, it indicated this function is called by
34276 cp_parser_require_keword and the required token can only be
34277 a indicated keyword.
34279 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34280 within any error as the location of an "opening" token matching
34281 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34282 RT_CLOSE_PAREN). */
34284 static void
34285 cp_parser_required_error (cp_parser *parser,
34286 required_token token_desc,
34287 bool keyword,
34288 location_t matching_location)
34290 if (cp_parser_simulate_error (parser))
34291 return;
34293 const char *gmsgid = NULL;
34294 switch (token_desc)
34296 case RT_NEW:
34297 gmsgid = G_("expected %<new%>");
34298 break;
34299 case RT_DELETE:
34300 gmsgid = G_("expected %<delete%>");
34301 break;
34302 case RT_RETURN:
34303 gmsgid = G_("expected %<return%>");
34304 break;
34305 case RT_WHILE:
34306 gmsgid = G_("expected %<while%>");
34307 break;
34308 case RT_EXTERN:
34309 gmsgid = G_("expected %<extern%>");
34310 break;
34311 case RT_STATIC_ASSERT:
34312 gmsgid = G_("expected %<static_assert%>");
34313 break;
34314 case RT_DECLTYPE:
34315 gmsgid = G_("expected %<decltype%>");
34316 break;
34317 case RT_OPERATOR:
34318 gmsgid = G_("expected %<operator%>");
34319 break;
34320 case RT_CLASS:
34321 gmsgid = G_("expected %<class%>");
34322 break;
34323 case RT_TEMPLATE:
34324 gmsgid = G_("expected %<template%>");
34325 break;
34326 case RT_NAMESPACE:
34327 gmsgid = G_("expected %<namespace%>");
34328 break;
34329 case RT_USING:
34330 gmsgid = G_("expected %<using%>");
34331 break;
34332 case RT_ASM:
34333 gmsgid = G_("expected %<asm%>");
34334 break;
34335 case RT_TRY:
34336 gmsgid = G_("expected %<try%>");
34337 break;
34338 case RT_CATCH:
34339 gmsgid = G_("expected %<catch%>");
34340 break;
34341 case RT_THROW:
34342 gmsgid = G_("expected %<throw%>");
34343 break;
34344 case RT_AUTO:
34345 gmsgid = G_("expected %<auto%>");
34346 break;
34347 case RT_LABEL:
34348 gmsgid = G_("expected %<__label__%>");
34349 break;
34350 case RT_AT_TRY:
34351 gmsgid = G_("expected %<@try%>");
34352 break;
34353 case RT_AT_SYNCHRONIZED:
34354 gmsgid = G_("expected %<@synchronized%>");
34355 break;
34356 case RT_AT_THROW:
34357 gmsgid = G_("expected %<@throw%>");
34358 break;
34359 case RT_TRANSACTION_ATOMIC:
34360 gmsgid = G_("expected %<__transaction_atomic%>");
34361 break;
34362 case RT_TRANSACTION_RELAXED:
34363 gmsgid = G_("expected %<__transaction_relaxed%>");
34364 break;
34365 case RT_CO_YIELD:
34366 gmsgid = G_("expected %<co_yield%>");
34367 break;
34368 default:
34369 break;
34372 if (!gmsgid && !keyword)
34374 switch (token_desc)
34376 case RT_SEMICOLON:
34377 gmsgid = G_("expected %<;%>");
34378 break;
34379 case RT_OPEN_PAREN:
34380 gmsgid = G_("expected %<(%>");
34381 break;
34382 case RT_CLOSE_BRACE:
34383 gmsgid = G_("expected %<}%>");
34384 break;
34385 case RT_OPEN_BRACE:
34386 gmsgid = G_("expected %<{%>");
34387 break;
34388 case RT_CLOSE_SQUARE:
34389 gmsgid = G_("expected %<]%>");
34390 break;
34391 case RT_OPEN_SQUARE:
34392 gmsgid = G_("expected %<[%>");
34393 break;
34394 case RT_COMMA:
34395 gmsgid = G_("expected %<,%>");
34396 break;
34397 case RT_SCOPE:
34398 gmsgid = G_("expected %<::%>");
34399 break;
34400 case RT_LESS:
34401 gmsgid = G_("expected %<<%>");
34402 break;
34403 case RT_GREATER:
34404 gmsgid = G_("expected %<>%>");
34405 break;
34406 case RT_EQ:
34407 gmsgid = G_("expected %<=%>");
34408 break;
34409 case RT_ELLIPSIS:
34410 gmsgid = G_("expected %<...%>");
34411 break;
34412 case RT_MULT:
34413 gmsgid = G_("expected %<*%>");
34414 break;
34415 case RT_COMPL:
34416 gmsgid = G_("expected %<~%>");
34417 break;
34418 case RT_COLON:
34419 gmsgid = G_("expected %<:%>");
34420 break;
34421 case RT_COLON_SCOPE:
34422 gmsgid = G_("expected %<:%> or %<::%>");
34423 break;
34424 case RT_CLOSE_PAREN:
34425 gmsgid = G_("expected %<)%>");
34426 break;
34427 case RT_COMMA_CLOSE_PAREN:
34428 gmsgid = G_("expected %<,%> or %<)%>");
34429 break;
34430 case RT_PRAGMA_EOL:
34431 gmsgid = G_("expected end of line");
34432 break;
34433 case RT_NAME:
34434 gmsgid = G_("expected identifier");
34435 break;
34436 case RT_SELECT:
34437 gmsgid = G_("expected selection-statement");
34438 break;
34439 case RT_ITERATION:
34440 gmsgid = G_("expected iteration-statement");
34441 break;
34442 case RT_JUMP:
34443 gmsgid = G_("expected jump-statement");
34444 break;
34445 case RT_CLASS_KEY:
34446 gmsgid = G_("expected class-key");
34447 break;
34448 case RT_CLASS_TYPENAME_TEMPLATE:
34449 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34450 break;
34451 default:
34452 gcc_unreachable ();
34456 if (gmsgid)
34457 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34461 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34462 issue an error message indicating that TOKEN_DESC was expected.
34464 Returns the token consumed, if the token had the appropriate type.
34465 Otherwise, returns NULL.
34467 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34468 within any error as the location of an "opening" token matching
34469 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34470 RT_CLOSE_PAREN). */
34472 static cp_token *
34473 cp_parser_require (cp_parser* parser,
34474 enum cpp_ttype type,
34475 required_token token_desc,
34476 location_t matching_location)
34478 if (cp_lexer_next_token_is (parser->lexer, type))
34479 return cp_lexer_consume_token (parser->lexer);
34480 else
34482 /* Output the MESSAGE -- unless we're parsing tentatively. */
34483 if (!cp_parser_simulate_error (parser))
34484 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34485 matching_location);
34486 return NULL;
34490 /* Skip an entire parameter list from start to finish. The next token must
34491 be the initial "<" of the parameter list. Returns true on success and
34492 false otherwise. */
34494 static bool
34495 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34497 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34498 requires it. */
34499 cp_lexer_consume_token (parser->lexer);
34500 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34503 /* Ensure we are at the end of a template parameter list. If we are, return.
34504 If we are not, something has gone wrong, in which case issue an error and
34505 skip to end of the parameter list. */
34507 static void
34508 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34510 /* Are we ready, yet? If not, issue error message. */
34511 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34512 return;
34514 cp_parser_skip_to_end_of_template_parameter_list (parser);
34517 /* You should only call this function from inside a template parameter list
34518 (i.e. the current token should at least be the initial "<" of the
34519 parameter list). If you are skipping the entire list, it may be better to
34520 use cp_parser_skip_entire_template_parameter_list.
34522 Tokens are skipped until the final ">" is found, or if we see
34523 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34525 Returns true if we successfully reached the end, and false if
34526 something unexpected happened (e.g. end of file). */
34528 static bool
34529 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34531 /* Current level of '< ... >'. */
34532 unsigned level = 0;
34533 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34534 unsigned nesting_depth = 0;
34536 /* Skip tokens until the desired token is found. */
34537 while (true)
34539 /* Peek at the next token. */
34540 switch (cp_lexer_peek_token (parser->lexer)->type)
34542 case CPP_LESS:
34543 if (!nesting_depth)
34544 ++level;
34545 break;
34547 case CPP_RSHIFT:
34548 if (cxx_dialect == cxx98)
34549 /* C++0x views the `>>' operator as two `>' tokens, but
34550 C++98 does not. */
34551 break;
34552 else if (!nesting_depth && level-- == 0)
34554 /* We've hit a `>>' where the first `>' closes the
34555 template argument list, and the second `>' is
34556 spurious. Just consume the `>>' and stop; we've
34557 already produced at least one error. */
34558 cp_lexer_consume_token (parser->lexer);
34559 return false;
34561 /* Fall through for C++0x, so we handle the second `>' in
34562 the `>>'. */
34563 gcc_fallthrough ();
34565 case CPP_GREATER:
34566 if (!nesting_depth && level-- == 0)
34568 /* We've reached the token we want, consume it and stop. */
34569 cp_lexer_consume_token (parser->lexer);
34570 return true;
34572 break;
34574 case CPP_OPEN_PAREN:
34575 case CPP_OPEN_SQUARE:
34576 ++nesting_depth;
34577 break;
34579 case CPP_CLOSE_PAREN:
34580 case CPP_CLOSE_SQUARE:
34581 if (nesting_depth-- == 0)
34582 return false;
34583 break;
34585 case CPP_EOF:
34586 case CPP_PRAGMA_EOL:
34587 case CPP_SEMICOLON:
34588 case CPP_OPEN_BRACE:
34589 case CPP_CLOSE_BRACE:
34590 /* The '>' was probably forgotten, don't look further. */
34591 return false;
34593 default:
34594 break;
34597 /* Consume this token. */
34598 cp_lexer_consume_token (parser->lexer);
34602 /* If the next token is the indicated keyword, consume it. Otherwise,
34603 issue an error message indicating that TOKEN_DESC was expected.
34605 Returns the token consumed, if the token had the appropriate type.
34606 Otherwise, returns NULL. */
34608 static cp_token *
34609 cp_parser_require_keyword (cp_parser* parser,
34610 enum rid keyword,
34611 required_token token_desc)
34613 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34615 if (token && token->keyword != keyword)
34617 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34618 UNKNOWN_LOCATION);
34619 return NULL;
34622 return token;
34625 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34626 function-definition. */
34628 static bool
34629 cp_parser_token_starts_function_definition_p (cp_token* token)
34631 return (/* An ordinary function-body begins with an `{'. */
34632 token->type == CPP_OPEN_BRACE
34633 /* A ctor-initializer begins with a `:'. */
34634 || token->type == CPP_COLON
34635 /* A function-try-block begins with `try'. */
34636 || token->keyword == RID_TRY
34637 /* A function-transaction-block begins with `__transaction_atomic'
34638 or `__transaction_relaxed'. */
34639 || token->keyword == RID_TRANSACTION_ATOMIC
34640 || token->keyword == RID_TRANSACTION_RELAXED
34641 /* The named return value extension begins with `return'. */
34642 || token->keyword == RID_RETURN);
34645 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34646 definition. */
34648 static bool
34649 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34651 cp_token *token;
34653 token = cp_lexer_peek_token (parser->lexer);
34654 return (token->type == CPP_OPEN_BRACE
34655 || (token->type == CPP_COLON
34656 && !parser->colon_doesnt_start_class_def_p));
34659 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34660 C++0x) ending a template-argument. */
34662 static bool
34663 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34665 cp_token *token;
34667 token = cp_lexer_peek_token (parser->lexer);
34668 return (token->type == CPP_COMMA
34669 || token->type == CPP_GREATER
34670 || token->type == CPP_ELLIPSIS
34671 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34672 /* For better diagnostics, treat >>= like that too, that
34673 shouldn't appear non-nested in template arguments. */
34674 || token->type == CPP_RSHIFT_EQ);
34677 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34678 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34680 static bool
34681 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34682 size_t n)
34684 cp_token *token;
34686 token = cp_lexer_peek_nth_token (parser->lexer, n);
34687 if (token->type == CPP_LESS)
34688 return true;
34689 /* Check for the sequence `<::' in the original code. It would be lexed as
34690 `[:', where `[' is a digraph, and there is no whitespace before
34691 `:'. */
34692 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34694 cp_token *token2;
34695 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34696 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34697 return true;
34699 return false;
34702 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34703 or none_type otherwise. */
34705 static enum tag_types
34706 cp_parser_token_is_class_key (cp_token* token)
34708 switch (token->keyword)
34710 case RID_CLASS:
34711 return class_type;
34712 case RID_STRUCT:
34713 return record_type;
34714 case RID_UNION:
34715 return union_type;
34717 default:
34718 return none_type;
34722 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34723 or none_type otherwise or if the token is null. */
34725 static enum tag_types
34726 cp_parser_token_is_type_parameter_key (cp_token* token)
34728 if (!token)
34729 return none_type;
34731 switch (token->keyword)
34733 case RID_CLASS:
34734 return class_type;
34735 case RID_TYPENAME:
34736 return typename_type;
34738 default:
34739 return none_type;
34743 /* Diagnose redundant enum-keys. */
34745 static void
34746 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34747 tree type, rid scoped_key)
34749 if (!warn_redundant_tags)
34750 return;
34752 tree type_decl = TYPE_MAIN_DECL (type);
34753 tree name = DECL_NAME (type_decl);
34754 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34755 push_deferring_access_checks (dk_no_check);
34756 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34757 pop_deferring_access_checks ();
34759 /* The enum-key is redundant for uses of the TYPE that are not
34760 declarations and for which name lookup returns just the type
34761 itself. */
34762 if (decl != type_decl)
34763 return;
34765 if (scoped_key != RID_CLASS
34766 && scoped_key != RID_STRUCT
34767 && current_lang_name != lang_name_cplusplus
34768 && current_namespace == global_namespace)
34770 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34771 enum tag in shared C/C++ code in files (such as headers) included
34772 in the main source file. */
34773 const line_map_ordinary *map = NULL;
34774 linemap_resolve_location (line_table, key_loc,
34775 LRK_MACRO_DEFINITION_LOCATION,
34776 &map);
34777 if (!MAIN_FILE_P (map))
34778 return;
34781 gcc_rich_location richloc (key_loc);
34782 richloc.add_fixit_remove (key_loc);
34783 warning_at (&richloc, OPT_Wredundant_tags,
34784 "redundant enum-key %<enum%s%> in reference to %q#T",
34785 (scoped_key == RID_CLASS ? " class"
34786 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34789 /* Describes the set of declarations of a struct, class, or class template
34790 or its specializations. Used for -Wmismatched-tags. */
34792 class class_decl_loc_t
34794 public:
34796 class_decl_loc_t ()
34797 : locvec (), idxdef (), def_class_key ()
34799 locvec.create (4);
34802 /* Constructs an object for a single declaration of a class with
34803 CLASS_KEY at the current location in the current function (or
34804 at another scope). KEY_REDUNDANT is true if the class-key may
34805 be omitted in the current context without an ambiguity with
34806 another symbol with the same name.
34807 DEF_P is true for a class declaration that is a definition.
34808 CURLOC is the associated location. */
34809 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34810 location_t curloc = input_location)
34811 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34813 locvec.create (4);
34814 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34815 key_redundant);
34816 locvec.quick_push (ckl);
34819 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34820 isn't safely copyable and assignable and doesn't release storage
34821 on its own. */
34822 class_decl_loc_t (const class_decl_loc_t &rhs)
34823 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34824 def_class_key (rhs.def_class_key)
34827 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34829 if (this == &rhs)
34830 return *this;
34831 locvec.release ();
34832 locvec = rhs.locvec.copy ();
34833 idxdef = rhs.idxdef;
34834 def_class_key = rhs.def_class_key;
34835 return *this;
34838 ~class_decl_loc_t ()
34840 locvec.release ();
34843 /* Issues -Wmismatched-tags for a single class. */
34844 void diag_mismatched_tags (tree);
34846 /* Issues -Wmismatched-tags for all classes. */
34847 static void diag_mismatched_tags ();
34849 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34850 redundant tags (if -Wredundant-tags is enabled). */
34851 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34853 /* Either adds this decl to the collection of class decls
34854 or diagnoses it, whichever is appropriate. */
34855 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34857 private:
34859 tree function (unsigned i) const
34861 return locvec[i].func;
34864 location_t location (unsigned i) const
34866 return locvec[i].loc;
34869 bool key_redundant (unsigned i) const
34871 return locvec[i].key_redundant;
34874 tag_types class_key (unsigned i) const
34876 return locvec[i].class_key;
34879 /* True if a definition for the class has been seen. */
34880 bool def_p () const
34882 return idxdef < locvec.length ();
34885 /* The location of a single mention of a class type with the given
34886 class-key. */
34887 struct class_key_loc_t
34889 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34890 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34893 /* The function the type is mentioned in. */
34894 tree func;
34895 /* The exact location. */
34896 location_t loc;
34897 /* The class-key used in the mention of the type. */
34898 tag_types class_key;
34899 /* True when the class-key could be omitted at this location
34900 without an ambiguity with another symbol of the same name. */
34901 bool key_redundant;
34903 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34904 vec <class_key_loc_t> locvec;
34905 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34906 unsigned idxdef;
34907 /* The class-key the class was last declared with or none_type when
34908 it has been declared with a mismatched key. */
34909 tag_types def_class_key;
34911 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34912 description above. */
34913 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34914 static class_to_loc_map_t class2loc;
34917 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34919 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34920 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34921 is set for a declaration of class TYPE and clear for a reference to
34922 it that is not a declaration of it. */
34924 static void
34925 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34926 tag_types class_key, tree type, bool def_p,
34927 bool decl_p)
34929 if (type == error_mark_node)
34930 return;
34932 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34933 if (seen_as_union != (class_key == union_type))
34935 if (permerror (input_location, "%qs tag used in naming %q#T",
34936 class_key == union_type ? "union"
34937 : class_key == record_type ? "struct" : "class",
34938 type))
34939 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34940 "%q#T was previously declared here", type);
34941 return;
34944 if (!warn_mismatched_tags && !warn_redundant_tags)
34945 return;
34947 /* Only consider the true class-keys below and ignore typename_type,
34948 etc. that are not C++ class-keys. */
34949 if (class_key != class_type
34950 && class_key != record_type
34951 && class_key != union_type)
34952 return;
34954 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34957 /* Returns the template or specialization of one to which the RECORD_TYPE
34958 TYPE corresponds. */
34960 static tree
34961 specialization_of (tree type)
34963 tree ret = type;
34965 /* Determine the template or its partial specialization to which TYPE
34966 corresponds. */
34967 if (tree ti = most_specialized_partial_spec (type, tf_none))
34968 if (ti != error_mark_node)
34969 ret = TREE_TYPE (TI_TEMPLATE (ti));
34971 if (ret == type)
34972 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34974 return TYPE_MAIN_DECL (ret);
34978 /* Adds the class TYPE to the collection of class decls and diagnoses
34979 redundant tags (if -Wredundant-tags is enabled).
34980 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34981 is set for a (likely, based on syntactic context) declaration of class
34982 TYPE and clear for a reference to it that is not a declaration of it. */
34984 void
34985 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34986 tag_types class_key, tree type, bool def_p, bool decl_p)
34988 tree type_decl = TYPE_MAIN_DECL (type);
34989 tree name = DECL_NAME (type_decl);
34990 /* Look up the NAME to see if it unambiguously refers to the TYPE
34991 and set KEY_REDUNDANT if so. */
34992 push_deferring_access_checks (dk_no_check);
34993 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34994 pop_deferring_access_checks ();
34996 /* The class-key is redundant for uses of the CLASS_TYPE that are
34997 neither definitions of it nor declarations, and for which name
34998 lookup returns just the type itself. */
34999 bool key_redundant = (!def_p && !decl_p
35000 && (decl == type_decl
35001 || TREE_CODE (decl) == TEMPLATE_DECL
35002 || (CLASS_TYPE_P (type)
35003 && TYPE_BEING_DEFINED (type))));
35005 if (key_redundant
35006 && class_key != class_type
35007 && current_lang_name != lang_name_cplusplus
35008 && current_namespace == global_namespace)
35010 /* Avoid issuing the diagnostic for apparently redundant struct
35011 and union class-keys in shared C/C++ code in files (such as
35012 headers) included in the main source file. */
35013 const line_map_ordinary *map = NULL;
35014 linemap_resolve_location (line_table, key_loc,
35015 LRK_MACRO_DEFINITION_LOCATION,
35016 &map);
35017 if (!MAIN_FILE_P (map))
35018 key_redundant = false;
35021 /* Set if a declaration of TYPE has previously been seen or if it must
35022 exist in a precompiled header. */
35023 bool exist;
35024 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
35025 if (!exist)
35027 tree type = TREE_TYPE (type_decl);
35028 if (def_p || !COMPLETE_TYPE_P (type))
35030 /* TYPE_DECL is the first declaration or definition of the type
35031 (outside precompiled headers -- see below). Just create
35032 a new entry for it and return unless it's a declaration
35033 involving a template that may need to be diagnosed by
35034 -Wredundant-tags. */
35035 *rdl = class_decl_loc_t (class_key, false, def_p);
35036 if (TREE_CODE (decl) != TEMPLATE_DECL)
35037 return;
35039 else
35041 /* TYPE was previously defined in some unknown precompiled header.
35042 Simply add a record of its definition at an unknown location and
35043 proceed below to add a reference to it at the current location.
35044 (Declarations in precompiled headers that are not definitions
35045 are ignored.) */
35046 tag_types def_key
35047 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
35048 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
35049 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
35050 exist = true;
35054 /* A prior declaration of TYPE_DECL has been seen. */
35056 if (key_redundant)
35058 gcc_rich_location richloc (key_loc);
35059 richloc.add_fixit_remove (key_loc);
35060 warning_at (&richloc, OPT_Wredundant_tags,
35061 "redundant class-key %qs in reference to %q#T",
35062 class_key == union_type ? "union"
35063 : class_key == record_type ? "struct" : "class",
35064 type);
35067 if (!exist)
35068 /* Do nothing if this is the first declaration of the type. */
35069 return;
35071 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
35072 /* Do nothing if the class-key in this declaration matches
35073 the definition. */
35074 return;
35076 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
35077 def_p);
35080 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
35081 of class decls or diagnoses it, whichever is appropriate. */
35083 void
35084 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
35085 tag_types class_key,
35086 bool redundant,
35087 bool def_p)
35089 /* Reset the CLASS_KEY associated with this type on mismatch.
35090 This is an optimization that lets the diagnostic code skip
35091 over classes that use the same class-key in all declarations. */
35092 if (def_class_key != class_key)
35093 def_class_key = none_type;
35095 /* Set IDXDEF to the index of the vector corresponding to
35096 the definition. */
35097 if (def_p)
35098 idxdef = locvec.length ();
35100 /* Append a record of this declaration to the vector. */
35101 class_key_loc_t ckl (current_function_decl, input_location, class_key,
35102 redundant);
35103 locvec.safe_push (ckl);
35105 if (idxdef == UINT_MAX)
35106 return;
35108 /* As a space optimization diagnose declarations of a class
35109 whose definition has been seen and purge the LOCVEC of
35110 all entries except the definition. */
35111 diag_mismatched_tags (type_decl);
35112 if (idxdef)
35114 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
35115 locvec.release ();
35116 locvec.reserve (2);
35117 locvec.safe_push (ent);
35118 idxdef = 0;
35120 else
35121 /* Pop the entry pushed above for this declaration. */
35122 locvec.pop ();
35125 /* Issues -Wmismatched-tags for a single class. */
35127 void
35128 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
35130 if (!warn_mismatched_tags)
35131 return;
35133 /* Number of uses of the class. */
35134 const unsigned ndecls = locvec.length ();
35136 /* The class (or template) declaration guiding the decisions about
35137 the diagnostic. For ordinary classes it's the same as THIS. For
35138 uses of instantiations of templates other than their declarations
35139 it points to the record for the declaration of the corresponding
35140 primary template or partial specialization. */
35141 class_decl_loc_t *cdlguide = this;
35143 tree type = TREE_TYPE (type_decl);
35144 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
35146 /* For implicit instantiations of a primary template look up
35147 the primary or partial specialization and use it as
35148 the expected class-key rather than using the class-key of
35149 the first reference to the instantiation. The primary must
35150 be (and inevitably is) at index zero. */
35151 tree spec = specialization_of (type);
35152 cdlguide = class2loc.get (spec);
35153 /* It's possible that we didn't find SPEC. Consider:
35155 template<typename T> struct A {
35156 template<typename U> struct W { };
35158 struct A<int>::W<int> w; // #1
35160 where while parsing A and #1 we've stashed
35161 A<T>
35162 A<T>::W<U>
35163 A<int>::W<int>
35164 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
35165 will yield A<int>::W<U> which may be in CLASS2LOC if we had
35166 an A<int> class specialization, but otherwise won't be in it.
35167 So try to look up A<T>::W<U>. */
35168 if (!cdlguide)
35170 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
35171 cdlguide = class2loc.get (spec);
35173 /* Now we really should have found something. */
35174 gcc_assert (cdlguide != NULL);
35176 /* Skip declarations that consistently use the same class-key. */
35177 else if (def_class_key != none_type)
35178 return;
35180 /* Set if a definition for the class has been seen. */
35181 const bool def_p = cdlguide->def_p ();
35183 /* The index of the declaration whose class-key this declaration
35184 is expected to match. It's either the class-key of the class
35185 definition if one exists or the first declaration otherwise. */
35186 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
35188 /* The class-key the class is expected to be declared with: it's
35189 either the key used in its definition or the first declaration
35190 if no definition has been provided.
35191 For implicit instantiations of a primary template it's
35192 the class-key used to declare the primary with. The primary
35193 must be at index zero. */
35194 const tag_types xpect_key = cdlguide->class_key (idxguide);
35196 unsigned idx = 0;
35197 /* Advance IDX to the first declaration that either is not
35198 a definition or that doesn't match the first declaration
35199 if no definition is provided. */
35200 while (class_key (idx) == xpect_key)
35201 if (++idx == ndecls)
35202 return;
35204 /* Save the current function before changing it below. */
35205 tree save_func = current_function_decl;
35206 /* Set the function declaration to print in diagnostic context. */
35207 current_function_decl = function (idx);
35209 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
35210 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
35212 location_t loc = location (idx);
35213 bool key_redundant_p = key_redundant (idx);
35214 auto_diagnostic_group d;
35215 /* Issue a warning for the first mismatched declaration.
35216 Avoid using "%#qT" since the class-key for the same type will
35217 be the same regardless of which one was used in the declaraion. */
35218 if (warning_at (loc, OPT_Wmismatched_tags,
35219 "%qT declared with a mismatched class-key %qs",
35220 type_decl, xmatchkstr))
35222 /* Suggest how to avoid the warning for each instance since
35223 the guidance may be different depending on context. */
35224 inform (loc,
35225 (key_redundant_p
35226 ? G_("remove the class-key or replace it with %qs")
35227 : G_("replace the class-key with %qs")),
35228 xpectkstr);
35230 /* Also point to the first declaration or definition that guided
35231 the decision to issue the warning above. */
35232 inform (cdlguide->location (idxguide),
35233 (def_p
35234 ? G_("%qT defined as %qs here")
35235 : G_("%qT first declared as %qs here")),
35236 type_decl, xpectkstr);
35239 /* Issue warnings for the remaining inconsistent declarations. */
35240 for (unsigned i = idx + 1; i != ndecls; ++i)
35242 tag_types clskey = class_key (i);
35243 /* Skip over the declarations that match either the definition
35244 if one was provided or the first declaration. */
35245 if (clskey == xpect_key)
35246 continue;
35248 loc = location (i);
35249 key_redundant_p = key_redundant (i);
35250 /* Set the function declaration to print in diagnostic context. */
35251 current_function_decl = function (i);
35252 if (warning_at (loc, OPT_Wmismatched_tags,
35253 "%qT declared with a mismatched class-key %qs",
35254 type_decl, xmatchkstr))
35255 /* Suggest how to avoid the warning for each instance since
35256 the guidance may be different depending on context. */
35257 inform (loc,
35258 (key_redundant_p
35259 ? G_("remove the class-key or replace it with %qs")
35260 : G_("replace the class-key with %qs")),
35261 xpectkstr);
35264 /* Restore the current function in case it was replaced above. */
35265 current_function_decl = save_func;
35268 /* Issues -Wmismatched-tags for all classes. Called at the end
35269 of processing a translation unit, after declarations of all class
35270 types and their uses have been recorded. */
35272 void
35273 class_decl_loc_t::diag_mismatched_tags ()
35275 /* CLASS2LOC should be empty if both -Wmismatched-tags and
35276 -Wredundant-tags are disabled. */
35277 gcc_assert (warn_mismatched_tags
35278 || warn_redundant_tags
35279 || class2loc.is_empty ());
35281 /* Save the current function before changing on return. It should
35282 be null at this point. */
35283 temp_override<tree> cleanup (current_function_decl);
35285 if (warn_mismatched_tags)
35287 /* Iterate over the collected class/struct/template declarations. */
35288 typedef class_to_loc_map_t::iterator iter_t;
35289 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
35291 tree type_decl = (*it).first;
35292 class_decl_loc_t &recloc = (*it).second;
35293 recloc.diag_mismatched_tags (type_decl);
35297 class2loc.empty ();
35300 /* Issue an error message if DECL is redeclared with different
35301 access than its original declaration [class.access.spec/3].
35302 This applies to nested classes, nested class templates and
35303 enumerations [class.mem/1]. */
35305 static void
35306 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
35308 if (!decl
35309 || (!CLASS_TYPE_P (TREE_TYPE (decl))
35310 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
35311 return;
35313 if ((TREE_PRIVATE (decl)
35314 != (current_access_specifier == access_private_node))
35315 || (TREE_PROTECTED (decl)
35316 != (current_access_specifier == access_protected_node)))
35317 error_at (location, "%qD redeclared with different access", decl);
35320 /* Look for the `template' keyword, as a syntactic disambiguator.
35321 Return TRUE iff it is present, in which case it will be
35322 consumed. */
35324 static bool
35325 cp_parser_optional_template_keyword (cp_parser *parser)
35327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
35329 /* In C++98 the `template' keyword can only be used within templates;
35330 outside templates the parser can always figure out what is a
35331 template and what is not. In C++11, per the resolution of DR 468,
35332 `template' is allowed in cases where it is not strictly necessary. */
35333 if (!processing_template_decl
35334 && pedantic && cxx_dialect == cxx98)
35336 cp_token *token = cp_lexer_peek_token (parser->lexer);
35337 pedwarn (token->location, OPT_Wpedantic,
35338 "in C++98 %<template%> (as a disambiguator) is only "
35339 "allowed within templates");
35340 /* If this part of the token stream is rescanned, the same
35341 error message would be generated. So, we purge the token
35342 from the stream. */
35343 cp_lexer_purge_token (parser->lexer);
35344 return false;
35346 else
35348 /* Consume the `template' keyword. */
35349 cp_lexer_consume_token (parser->lexer);
35350 return true;
35353 return false;
35356 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
35357 set PARSER->SCOPE, and perform other related actions. */
35359 static void
35360 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
35362 struct tree_check *check_value;
35364 /* Get the stored value. */
35365 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
35366 /* Set the scope from the stored value. */
35367 parser->scope = saved_checks_value (check_value);
35368 parser->qualifying_scope = check_value->qualifying_scope;
35369 parser->object_scope = parser->context->object_type;
35370 parser->context->object_type = NULL_TREE;
35373 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35374 encounter the end of a block before what we were looking for. */
35376 static bool
35377 cp_parser_cache_group (cp_parser *parser,
35378 enum cpp_ttype end,
35379 unsigned depth)
35381 while (true)
35383 cp_token *token = cp_lexer_peek_token (parser->lexer);
35385 /* Abort a parenthesized expression if we encounter a semicolon. */
35386 if ((end == CPP_CLOSE_PAREN || depth == 0)
35387 && token->type == CPP_SEMICOLON)
35388 return true;
35389 /* If we've reached the end of the file, stop. */
35390 if (token->type == CPP_EOF
35391 || (end != CPP_PRAGMA_EOL
35392 && token->type == CPP_PRAGMA_EOL))
35393 return true;
35394 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35395 /* We've hit the end of an enclosing block, so there's been some
35396 kind of syntax error. */
35397 return true;
35399 /* Consume the token. */
35400 cp_lexer_consume_token (parser->lexer);
35401 /* See if it starts a new group. */
35402 if (token->type == CPP_OPEN_BRACE)
35404 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35405 /* In theory this should probably check end == '}', but
35406 cp_parser_save_member_function_body needs it to exit
35407 after either '}' or ')' when called with ')'. */
35408 if (depth == 0)
35409 return false;
35411 else if (token->type == CPP_OPEN_PAREN)
35413 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35414 if (depth == 0 && end == CPP_CLOSE_PAREN)
35415 return false;
35417 else if (token->type == CPP_PRAGMA)
35418 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35419 else if (token->type == end)
35420 return false;
35424 /* Like above, for caching a default argument or NSDMI. Both of these are
35425 terminated by a non-nested comma, but it can be unclear whether or not a
35426 comma is nested in a template argument list unless we do more parsing.
35427 In order to handle this ambiguity, when we encounter a ',' after a '<'
35428 we try to parse what follows as a parameter-declaration-list (in the
35429 case of a default argument) or a member-declarator (in the case of an
35430 NSDMI). If that succeeds, then we stop caching. */
35432 static tree
35433 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35435 unsigned depth = 0;
35436 int maybe_template_id = 0;
35437 cp_token *first_token;
35438 cp_token *token;
35439 tree default_argument;
35441 /* Add tokens until we have processed the entire default
35442 argument. We add the range [first_token, token). */
35443 first_token = cp_lexer_peek_token (parser->lexer);
35444 if (first_token->type == CPP_OPEN_BRACE)
35446 /* For list-initialization, this is straightforward. */
35447 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35448 token = cp_lexer_peek_token (parser->lexer);
35450 else while (true)
35452 bool done = false;
35454 /* Peek at the next token. */
35455 token = cp_lexer_peek_token (parser->lexer);
35456 /* What we do depends on what token we have. */
35457 switch (token->type)
35459 /* In valid code, a default argument must be
35460 immediately followed by a `,' `)', or `...'. */
35461 case CPP_COMMA:
35462 if (depth == 0 && maybe_template_id)
35464 /* If we've seen a '<', we might be in a
35465 template-argument-list. Until Core issue 325 is
35466 resolved, we don't know how this situation ought
35467 to be handled, so try to DTRT. We check whether
35468 what comes after the comma is a valid parameter
35469 declaration list. If it is, then the comma ends
35470 the default argument; otherwise the default
35471 argument continues. */
35472 bool error = false;
35473 cp_token *peek;
35475 /* Set ITALP so cp_parser_parameter_declaration_list
35476 doesn't decide to commit to this parse. */
35477 bool saved_italp = parser->in_template_argument_list_p;
35478 parser->in_template_argument_list_p = true;
35480 cp_parser_parse_tentatively (parser);
35482 if (nsdmi)
35484 /* Parse declarators until we reach a non-comma or
35485 somthing that cannot be an initializer.
35486 Just checking whether we're looking at a single
35487 declarator is insufficient. Consider:
35488 int var = tuple<T,U>::x;
35489 The template parameter 'U' looks exactly like a
35490 declarator. */
35493 int ctor_dtor_or_conv_p;
35494 cp_lexer_consume_token (parser->lexer);
35495 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35496 CP_PARSER_FLAGS_NONE,
35497 &ctor_dtor_or_conv_p,
35498 /*parenthesized_p=*/NULL,
35499 /*member_p=*/true,
35500 /*friend_p=*/false,
35501 /*static_p=*/false);
35502 peek = cp_lexer_peek_token (parser->lexer);
35503 if (cp_parser_error_occurred (parser))
35504 break;
35506 while (peek->type == CPP_COMMA);
35507 /* If we met an '=' or ';' then the original comma
35508 was the end of the NSDMI. Otherwise assume
35509 we're still in the NSDMI. */
35510 error = (peek->type != CPP_EQ
35511 && peek->type != CPP_SEMICOLON);
35513 else
35515 cp_lexer_consume_token (parser->lexer);
35516 begin_scope (sk_function_parms, NULL_TREE);
35517 tree t = cp_parser_parameter_declaration_list
35518 (parser, CP_PARSER_FLAGS_NONE,
35519 /*pending_decls*/nullptr);
35520 if (t == error_mark_node)
35521 error = true;
35522 pop_bindings_and_leave_scope ();
35524 if (!cp_parser_error_occurred (parser) && !error)
35525 done = true;
35526 cp_parser_abort_tentative_parse (parser);
35528 parser->in_template_argument_list_p = saved_italp;
35529 break;
35531 /* FALLTHRU */
35532 case CPP_CLOSE_PAREN:
35533 case CPP_ELLIPSIS:
35534 /* If we run into a non-nested `;', `}', or `]',
35535 then the code is invalid -- but the default
35536 argument is certainly over. */
35537 case CPP_SEMICOLON:
35538 case CPP_CLOSE_BRACE:
35539 case CPP_CLOSE_SQUARE:
35540 if (depth == 0
35541 /* Handle correctly int n = sizeof ... ( p ); */
35542 && token->type != CPP_ELLIPSIS)
35543 done = true;
35544 /* Update DEPTH, if necessary. */
35545 else if (token->type == CPP_CLOSE_PAREN
35546 || token->type == CPP_CLOSE_BRACE
35547 || token->type == CPP_CLOSE_SQUARE)
35548 --depth;
35549 break;
35551 case CPP_OPEN_PAREN:
35552 case CPP_OPEN_SQUARE:
35553 case CPP_OPEN_BRACE:
35554 ++depth;
35555 break;
35557 case CPP_LESS:
35558 if (depth == 0)
35559 /* This might be the comparison operator, or it might
35560 start a template argument list. */
35561 ++maybe_template_id;
35562 break;
35564 case CPP_RSHIFT:
35565 if (cxx_dialect == cxx98)
35566 break;
35567 /* Fall through for C++0x, which treats the `>>'
35568 operator like two `>' tokens in certain
35569 cases. */
35570 gcc_fallthrough ();
35572 case CPP_GREATER:
35573 if (depth == 0)
35575 /* This might be an operator, or it might close a
35576 template argument list. But if a previous '<'
35577 started a template argument list, this will have
35578 closed it, so we can't be in one anymore. */
35579 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35580 if (maybe_template_id < 0)
35581 maybe_template_id = 0;
35583 break;
35585 /* If we run out of tokens, issue an error message. */
35586 case CPP_EOF:
35587 case CPP_PRAGMA_EOL:
35588 error_at (token->location, "file ends in default argument");
35589 return error_mark_node;
35591 case CPP_NAME:
35592 case CPP_SCOPE:
35593 /* In these cases, we should look for template-ids.
35594 For example, if the default argument is
35595 `X<int, double>()', we need to do name lookup to
35596 figure out whether or not `X' is a template; if
35597 so, the `,' does not end the default argument.
35599 That is not yet done. */
35600 break;
35602 default:
35603 break;
35606 /* If we've reached the end, stop. */
35607 if (done)
35608 break;
35610 /* Add the token to the token block. */
35611 token = cp_lexer_consume_token (parser->lexer);
35614 /* Create a DEFERRED_PARSE to represent the unparsed default
35615 argument. */
35616 default_argument = make_node (DEFERRED_PARSE);
35617 DEFPARSE_TOKENS (default_argument)
35618 = cp_token_cache_new (first_token, token);
35619 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35621 return default_argument;
35624 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35626 location_t
35627 defparse_location (tree default_argument)
35629 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35630 location_t start = tokens->first->location;
35631 location_t end = tokens->last->location;
35632 return make_location (start, start, end);
35635 /* Begin parsing tentatively. We always save tokens while parsing
35636 tentatively so that if the tentative parsing fails we can restore the
35637 tokens. */
35639 static void
35640 cp_parser_parse_tentatively (cp_parser* parser)
35642 /* Enter a new parsing context. */
35643 parser->context = cp_parser_context_new (parser->context);
35644 /* Begin saving tokens. */
35645 cp_lexer_save_tokens (parser->lexer);
35646 /* In order to avoid repetitive access control error messages,
35647 access checks are queued up until we are no longer parsing
35648 tentatively. */
35649 push_deferring_access_checks (dk_deferred);
35652 /* Commit to the currently active tentative parse. */
35654 static void
35655 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35657 cp_parser_context *context;
35658 cp_lexer *lexer;
35660 /* Mark all of the levels as committed. */
35661 lexer = parser->lexer;
35662 for (context = parser->context; context->next; context = context->next)
35664 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35665 break;
35666 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35667 while (!cp_lexer_saving_tokens (lexer))
35668 lexer = lexer->next;
35669 cp_lexer_commit_tokens (lexer);
35673 /* Commit to the topmost currently active tentative parse.
35675 Note that this function shouldn't be called when there are
35676 irreversible side-effects while in a tentative state. For
35677 example, we shouldn't create a permanent entry in the symbol
35678 table, or issue an error message that might not apply if the
35679 tentative parse is aborted. */
35681 static void
35682 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35684 cp_parser_context *context = parser->context;
35685 cp_lexer *lexer = parser->lexer;
35687 if (context)
35689 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35690 return;
35691 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35693 while (!cp_lexer_saving_tokens (lexer))
35694 lexer = lexer->next;
35695 cp_lexer_commit_tokens (lexer);
35699 /* Abort the currently active tentative parse. All consumed tokens
35700 will be rolled back, and no diagnostics will be issued. */
35702 static void
35703 cp_parser_abort_tentative_parse (cp_parser* parser)
35705 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35706 || errorcount > 0);
35707 cp_parser_simulate_error (parser);
35708 /* Now, pretend that we want to see if the construct was
35709 successfully parsed. */
35710 cp_parser_parse_definitely (parser);
35713 /* Stop parsing tentatively. If a parse error has occurred, restore the
35714 token stream. Otherwise, commit to the tokens we have consumed.
35715 Returns true if no error occurred; false otherwise. */
35717 static bool
35718 cp_parser_parse_definitely (cp_parser* parser)
35720 bool error_occurred;
35721 cp_parser_context *context;
35723 /* Remember whether or not an error occurred, since we are about to
35724 destroy that information. */
35725 error_occurred = cp_parser_error_occurred (parser);
35726 /* Remove the topmost context from the stack. */
35727 context = parser->context;
35728 parser->context = context->next;
35729 /* If no parse errors occurred, commit to the tentative parse. */
35730 if (!error_occurred)
35732 /* Commit to the tokens read tentatively, unless that was
35733 already done. */
35734 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35735 cp_lexer_commit_tokens (parser->lexer);
35737 pop_to_parent_deferring_access_checks ();
35739 /* Otherwise, if errors occurred, roll back our state so that things
35740 are just as they were before we began the tentative parse. */
35741 else
35743 cp_lexer_rollback_tokens (parser->lexer);
35744 pop_deferring_access_checks ();
35746 /* Add the context to the front of the free list. */
35747 context->next = cp_parser_context_free_list;
35748 cp_parser_context_free_list = context;
35750 return !error_occurred;
35753 /* Returns true if we are parsing tentatively and are not committed to
35754 this tentative parse. */
35756 static bool
35757 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35759 return (cp_parser_parsing_tentatively (parser)
35760 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35763 /* Returns nonzero iff an error has occurred during the most recent
35764 tentative parse. */
35766 static bool
35767 cp_parser_error_occurred (cp_parser* parser)
35769 return (cp_parser_parsing_tentatively (parser)
35770 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35773 /* Returns nonzero if GNU extensions are allowed. */
35775 static bool
35776 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35778 return parser->allow_gnu_extensions_p;
35781 /* Objective-C++ Productions */
35784 /* Parse an Objective-C expression, which feeds into a primary-expression
35785 above.
35787 objc-expression:
35788 objc-message-expression
35789 objc-string-literal
35790 objc-encode-expression
35791 objc-protocol-expression
35792 objc-selector-expression
35794 Returns a tree representation of the expression. */
35796 static cp_expr
35797 cp_parser_objc_expression (cp_parser* parser)
35799 /* Try to figure out what kind of declaration is present. */
35800 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35802 switch (kwd->type)
35804 case CPP_OPEN_SQUARE:
35805 return cp_parser_objc_message_expression (parser);
35807 case CPP_OBJC_STRING:
35808 kwd = cp_lexer_consume_token (parser->lexer);
35809 return objc_build_string_object (kwd->u.value);
35811 case CPP_KEYWORD:
35812 switch (kwd->keyword)
35814 case RID_AT_ENCODE:
35815 return cp_parser_objc_encode_expression (parser);
35817 case RID_AT_PROTOCOL:
35818 return cp_parser_objc_protocol_expression (parser);
35820 case RID_AT_SELECTOR:
35821 return cp_parser_objc_selector_expression (parser);
35823 default:
35824 break;
35826 /* FALLTHRU */
35827 default:
35828 error_at (kwd->location,
35829 "misplaced %<@%D%> Objective-C++ construct",
35830 kwd->u.value);
35831 cp_parser_skip_to_end_of_block_or_statement (parser);
35834 return error_mark_node;
35837 /* Parse an Objective-C message expression.
35839 objc-message-expression:
35840 [ objc-message-receiver objc-message-args ]
35842 Returns a representation of an Objective-C message. */
35844 static tree
35845 cp_parser_objc_message_expression (cp_parser* parser)
35847 tree receiver, messageargs;
35849 parser->objective_c_message_context_p = true;
35850 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35851 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35852 receiver = cp_parser_objc_message_receiver (parser);
35853 messageargs = cp_parser_objc_message_args (parser);
35854 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35855 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35857 tree result = objc_build_message_expr (receiver, messageargs);
35859 /* Construct a location e.g.
35860 [self func1:5]
35861 ^~~~~~~~~~~~~~
35862 ranging from the '[' to the ']', with the caret at the start. */
35863 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35864 protected_set_expr_location (result, combined_loc);
35866 parser->objective_c_message_context_p = false;
35867 return result;
35870 /* Parse an objc-message-receiver.
35872 objc-message-receiver:
35873 expression
35874 simple-type-specifier
35876 Returns a representation of the type or expression. */
35878 static tree
35879 cp_parser_objc_message_receiver (cp_parser* parser)
35881 tree rcv;
35883 /* An Objective-C message receiver may be either (1) a type
35884 or (2) an expression. */
35885 cp_parser_parse_tentatively (parser);
35886 rcv = cp_parser_expression (parser);
35888 /* If that worked out, fine. */
35889 if (cp_parser_parse_definitely (parser))
35890 return rcv;
35892 cp_parser_parse_tentatively (parser);
35893 rcv = cp_parser_simple_type_specifier (parser,
35894 /*decl_specs=*/NULL,
35895 CP_PARSER_FLAGS_NONE);
35897 if (cp_parser_parse_definitely (parser))
35898 return objc_get_class_reference (rcv);
35900 cp_parser_error (parser, "objective-c++ message receiver expected");
35901 return error_mark_node;
35904 /* Parse the arguments and selectors comprising an Objective-C message.
35906 objc-message-args:
35907 objc-selector
35908 objc-selector-args
35909 objc-selector-args , objc-comma-args
35911 objc-selector-args:
35912 objc-selector [opt] : assignment-expression
35913 objc-selector-args objc-selector [opt] : assignment-expression
35915 objc-comma-args:
35916 assignment-expression
35917 objc-comma-args , assignment-expression
35919 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35920 selector arguments and TREE_VALUE containing a list of comma
35921 arguments. */
35923 static tree
35924 cp_parser_objc_message_args (cp_parser* parser)
35926 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35927 bool maybe_unary_selector_p = true;
35928 cp_token *token = cp_lexer_peek_token (parser->lexer);
35930 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35932 tree selector = NULL_TREE, arg;
35934 if (token->type != CPP_COLON)
35935 selector = cp_parser_objc_selector (parser);
35937 /* Detect if we have a unary selector. */
35938 if (maybe_unary_selector_p
35939 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35940 return build_tree_list (selector, NULL_TREE);
35942 maybe_unary_selector_p = false;
35943 cp_parser_require (parser, CPP_COLON, RT_COLON);
35944 arg = cp_parser_assignment_expression (parser);
35946 sel_args
35947 = chainon (sel_args,
35948 build_tree_list (selector, arg));
35950 token = cp_lexer_peek_token (parser->lexer);
35953 /* Handle non-selector arguments, if any. */
35954 while (token->type == CPP_COMMA)
35956 tree arg;
35958 cp_lexer_consume_token (parser->lexer);
35959 arg = cp_parser_assignment_expression (parser);
35961 addl_args
35962 = chainon (addl_args,
35963 build_tree_list (NULL_TREE, arg));
35965 token = cp_lexer_peek_token (parser->lexer);
35968 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35970 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35971 return build_tree_list (error_mark_node, error_mark_node);
35974 return build_tree_list (sel_args, addl_args);
35977 /* Parse an Objective-C encode expression.
35979 objc-encode-expression:
35980 @encode objc-typename
35982 Returns an encoded representation of the type argument. */
35984 static cp_expr
35985 cp_parser_objc_encode_expression (cp_parser* parser)
35987 tree type;
35988 cp_token *token;
35989 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35991 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35992 matching_parens parens;
35993 parens.require_open (parser);
35994 token = cp_lexer_peek_token (parser->lexer);
35995 type = complete_type (cp_parser_type_id (parser));
35996 parens.require_close (parser);
35998 if (!type)
36000 error_at (token->location,
36001 "%<@encode%> must specify a type as an argument");
36002 return error_mark_node;
36005 /* This happens if we find @encode(T) (where T is a template
36006 typename or something dependent on a template typename) when
36007 parsing a template. In that case, we can't compile it
36008 immediately, but we rather create an AT_ENCODE_EXPR which will
36009 need to be instantiated when the template is used.
36011 if (dependent_type_p (type))
36013 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
36014 TREE_READONLY (value) = 1;
36015 return value;
36019 /* Build a location of the form:
36020 @encode(int)
36021 ^~~~~~~~~~~~
36022 with caret==start at the @ token, finishing at the close paren. */
36023 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36025 return cp_expr (objc_build_encode_expr (type), combined_loc);
36028 /* Parse an Objective-C @defs expression. */
36030 static tree
36031 cp_parser_objc_defs_expression (cp_parser *parser)
36033 tree name;
36035 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
36036 matching_parens parens;
36037 parens.require_open (parser);
36038 name = cp_parser_identifier (parser);
36039 parens.require_close (parser);
36041 return objc_get_class_ivars (name);
36044 /* Parse an Objective-C protocol expression.
36046 objc-protocol-expression:
36047 @protocol ( identifier )
36049 Returns a representation of the protocol expression. */
36051 static tree
36052 cp_parser_objc_protocol_expression (cp_parser* parser)
36054 tree proto;
36055 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36057 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36058 matching_parens parens;
36059 parens.require_open (parser);
36060 proto = cp_parser_identifier (parser);
36061 parens.require_close (parser);
36063 /* Build a location of the form:
36064 @protocol(prot)
36065 ^~~~~~~~~~~~~~~
36066 with caret==start at the @ token, finishing at the close paren. */
36067 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36068 tree result = objc_build_protocol_expr (proto);
36069 protected_set_expr_location (result, combined_loc);
36070 return result;
36073 /* Parse an Objective-C selector expression.
36075 objc-selector-expression:
36076 @selector ( objc-method-signature )
36078 objc-method-signature:
36079 objc-selector
36080 objc-selector-seq
36082 objc-selector-seq:
36083 objc-selector :
36084 objc-selector-seq objc-selector :
36086 Returns a representation of the method selector. */
36088 static tree
36089 cp_parser_objc_selector_expression (cp_parser* parser)
36091 tree sel_seq = NULL_TREE;
36092 bool maybe_unary_selector_p = true;
36093 cp_token *token;
36094 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36096 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
36097 matching_parens parens;
36098 parens.require_open (parser);
36099 token = cp_lexer_peek_token (parser->lexer);
36101 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
36102 || token->type == CPP_SCOPE)
36104 tree selector = NULL_TREE;
36106 if (token->type != CPP_COLON
36107 || token->type == CPP_SCOPE)
36108 selector = cp_parser_objc_selector (parser);
36110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
36111 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
36113 /* Detect if we have a unary selector. */
36114 if (maybe_unary_selector_p)
36116 sel_seq = selector;
36117 goto finish_selector;
36119 else
36121 cp_parser_error (parser, "expected %<:%>");
36124 maybe_unary_selector_p = false;
36125 token = cp_lexer_consume_token (parser->lexer);
36127 if (token->type == CPP_SCOPE)
36129 sel_seq
36130 = chainon (sel_seq,
36131 build_tree_list (selector, NULL_TREE));
36132 sel_seq
36133 = chainon (sel_seq,
36134 build_tree_list (NULL_TREE, NULL_TREE));
36136 else
36137 sel_seq
36138 = chainon (sel_seq,
36139 build_tree_list (selector, NULL_TREE));
36141 token = cp_lexer_peek_token (parser->lexer);
36144 finish_selector:
36145 parens.require_close (parser);
36148 /* Build a location of the form:
36149 @selector(func)
36150 ^~~~~~~~~~~~~~~
36151 with caret==start at the @ token, finishing at the close paren. */
36152 location_t combined_loc = make_location (loc, loc, parser->lexer);
36153 tree result = objc_build_selector_expr (combined_loc, sel_seq);
36154 /* TODO: objc_build_selector_expr doesn't always honor the location. */
36155 protected_set_expr_location (result, combined_loc);
36156 return result;
36159 /* Parse a list of identifiers.
36161 objc-identifier-list:
36162 identifier
36163 objc-identifier-list , identifier
36165 Returns a TREE_LIST of identifier nodes. */
36167 static tree
36168 cp_parser_objc_identifier_list (cp_parser* parser)
36170 tree identifier;
36171 tree list;
36172 cp_token *sep;
36174 identifier = cp_parser_identifier (parser);
36175 if (identifier == error_mark_node)
36176 return error_mark_node;
36178 list = build_tree_list (NULL_TREE, identifier);
36179 sep = cp_lexer_peek_token (parser->lexer);
36181 while (sep->type == CPP_COMMA)
36183 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36184 identifier = cp_parser_identifier (parser);
36185 if (identifier == error_mark_node)
36186 return list;
36188 list = chainon (list, build_tree_list (NULL_TREE,
36189 identifier));
36190 sep = cp_lexer_peek_token (parser->lexer);
36193 return list;
36196 /* Parse an Objective-C alias declaration.
36198 objc-alias-declaration:
36199 @compatibility_alias identifier identifier ;
36201 This function registers the alias mapping with the Objective-C front end.
36202 It returns nothing. */
36204 static void
36205 cp_parser_objc_alias_declaration (cp_parser* parser)
36207 tree alias, orig;
36209 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
36210 alias = cp_parser_identifier (parser);
36211 orig = cp_parser_identifier (parser);
36212 objc_declare_alias (alias, orig);
36213 cp_parser_consume_semicolon_at_end_of_statement (parser);
36216 /* Parse an Objective-C class forward-declaration.
36218 objc-class-declaration:
36219 @class objc-identifier-list ;
36221 The function registers the forward declarations with the Objective-C
36222 front end. It returns nothing. */
36224 static void
36225 cp_parser_objc_class_declaration (cp_parser* parser)
36227 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
36228 while (true)
36230 tree id;
36232 id = cp_parser_identifier (parser);
36233 if (id == error_mark_node)
36234 break;
36236 objc_declare_class (id);
36238 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36239 cp_lexer_consume_token (parser->lexer);
36240 else
36241 break;
36243 cp_parser_consume_semicolon_at_end_of_statement (parser);
36246 /* Parse a list of Objective-C protocol references.
36248 objc-protocol-refs-opt:
36249 objc-protocol-refs [opt]
36251 objc-protocol-refs:
36252 < objc-identifier-list >
36254 Returns a TREE_LIST of identifiers, if any. */
36256 static tree
36257 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
36259 tree protorefs = NULL_TREE;
36261 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
36263 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
36264 protorefs = cp_parser_objc_identifier_list (parser);
36265 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
36268 return protorefs;
36271 /* Parse a Objective-C visibility specification. */
36273 static void
36274 cp_parser_objc_visibility_spec (cp_parser* parser)
36276 cp_token *vis = cp_lexer_peek_token (parser->lexer);
36278 switch (vis->keyword)
36280 case RID_AT_PRIVATE:
36281 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
36282 break;
36283 case RID_AT_PROTECTED:
36284 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
36285 break;
36286 case RID_AT_PUBLIC:
36287 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
36288 break;
36289 case RID_AT_PACKAGE:
36290 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
36291 break;
36292 default:
36293 return;
36296 /* Eat '@private'/'@protected'/'@public'. */
36297 cp_lexer_consume_token (parser->lexer);
36300 /* Parse an Objective-C method type. Return 'true' if it is a class
36301 (+) method, and 'false' if it is an instance (-) method. */
36303 static inline bool
36304 cp_parser_objc_method_type (cp_parser* parser)
36306 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
36307 return true;
36308 else
36309 return false;
36312 /* Parse an Objective-C protocol qualifier. */
36314 static tree
36315 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
36317 tree quals = NULL_TREE, node;
36318 cp_token *token = cp_lexer_peek_token (parser->lexer);
36320 node = token->u.value;
36322 while (node && identifier_p (node)
36323 && (node == ridpointers [(int) RID_IN]
36324 || node == ridpointers [(int) RID_OUT]
36325 || node == ridpointers [(int) RID_INOUT]
36326 || node == ridpointers [(int) RID_BYCOPY]
36327 || node == ridpointers [(int) RID_BYREF]
36328 || node == ridpointers [(int) RID_ONEWAY]))
36330 quals = tree_cons (NULL_TREE, node, quals);
36331 cp_lexer_consume_token (parser->lexer);
36332 token = cp_lexer_peek_token (parser->lexer);
36333 node = token->u.value;
36336 return quals;
36339 /* Parse an Objective-C typename. */
36341 static tree
36342 cp_parser_objc_typename (cp_parser* parser)
36344 tree type_name = NULL_TREE;
36346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36348 tree proto_quals, cp_type = NULL_TREE;
36350 matching_parens parens;
36351 parens.consume_open (parser); /* Eat '('. */
36352 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
36354 /* An ObjC type name may consist of just protocol qualifiers, in which
36355 case the type shall default to 'id'. */
36356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36358 cp_type = cp_parser_type_id (parser);
36360 /* If the type could not be parsed, an error has already
36361 been produced. For error recovery, behave as if it had
36362 not been specified, which will use the default type
36363 'id'. */
36364 if (cp_type == error_mark_node)
36366 cp_type = NULL_TREE;
36367 /* We need to skip to the closing parenthesis as
36368 cp_parser_type_id() does not seem to do it for
36369 us. */
36370 cp_parser_skip_to_closing_parenthesis (parser,
36371 /*recovering=*/true,
36372 /*or_comma=*/false,
36373 /*consume_paren=*/false);
36377 parens.require_close (parser);
36378 type_name = build_tree_list (proto_quals, cp_type);
36381 return type_name;
36384 /* Check to see if TYPE refers to an Objective-C selector name. */
36386 static bool
36387 cp_parser_objc_selector_p (enum cpp_ttype type)
36389 return (type == CPP_NAME || type == CPP_KEYWORD
36390 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36391 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36392 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36393 || type == CPP_XOR || type == CPP_XOR_EQ);
36396 /* Parse an Objective-C selector. */
36398 static tree
36399 cp_parser_objc_selector (cp_parser* parser)
36401 cp_token *token = cp_lexer_consume_token (parser->lexer);
36403 if (!cp_parser_objc_selector_p (token->type))
36405 error_at (token->location, "invalid Objective-C++ selector name");
36406 return error_mark_node;
36409 /* C++ operator names are allowed to appear in ObjC selectors. */
36410 switch (token->type)
36412 case CPP_AND_AND: return get_identifier ("and");
36413 case CPP_AND_EQ: return get_identifier ("and_eq");
36414 case CPP_AND: return get_identifier ("bitand");
36415 case CPP_OR: return get_identifier ("bitor");
36416 case CPP_COMPL: return get_identifier ("compl");
36417 case CPP_NOT: return get_identifier ("not");
36418 case CPP_NOT_EQ: return get_identifier ("not_eq");
36419 case CPP_OR_OR: return get_identifier ("or");
36420 case CPP_OR_EQ: return get_identifier ("or_eq");
36421 case CPP_XOR: return get_identifier ("xor");
36422 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36423 default: return token->u.value;
36427 /* Parse an Objective-C params list. */
36429 static tree
36430 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36432 tree params = NULL_TREE;
36433 bool maybe_unary_selector_p = true;
36434 cp_token *token = cp_lexer_peek_token (parser->lexer);
36436 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36438 tree selector = NULL_TREE, type_name, identifier;
36439 tree parm_attr = NULL_TREE;
36441 if (token->keyword == RID_ATTRIBUTE)
36442 break;
36444 if (token->type != CPP_COLON)
36445 selector = cp_parser_objc_selector (parser);
36447 /* Detect if we have a unary selector. */
36448 if (maybe_unary_selector_p
36449 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36451 params = selector; /* Might be followed by attributes. */
36452 break;
36455 maybe_unary_selector_p = false;
36456 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36458 /* Something went quite wrong. There should be a colon
36459 here, but there is not. Stop parsing parameters. */
36460 break;
36462 type_name = cp_parser_objc_typename (parser);
36463 /* New ObjC allows attributes on parameters too. */
36464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36465 parm_attr = cp_parser_attributes_opt (parser);
36466 identifier = cp_parser_identifier (parser);
36468 params
36469 = chainon (params,
36470 objc_build_keyword_decl (selector,
36471 type_name,
36472 identifier,
36473 parm_attr));
36475 token = cp_lexer_peek_token (parser->lexer);
36478 if (params == NULL_TREE)
36480 cp_parser_error (parser, "objective-c++ method declaration is expected");
36481 return error_mark_node;
36484 /* We allow tail attributes for the method. */
36485 if (token->keyword == RID_ATTRIBUTE)
36487 *attributes = cp_parser_attributes_opt (parser);
36488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36489 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36490 return params;
36491 cp_parser_error (parser,
36492 "method attributes must be specified at the end");
36493 return error_mark_node;
36496 if (params == NULL_TREE)
36498 cp_parser_error (parser, "objective-c++ method declaration is expected");
36499 return error_mark_node;
36501 return params;
36504 /* Parse the non-keyword Objective-C params. */
36506 static tree
36507 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36508 tree* attributes)
36510 tree params = make_node (TREE_LIST);
36511 cp_token *token = cp_lexer_peek_token (parser->lexer);
36512 *ellipsisp = false; /* Initially, assume no ellipsis. */
36514 while (token->type == CPP_COMMA)
36516 cp_parameter_declarator *parmdecl;
36517 tree parm;
36519 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36520 token = cp_lexer_peek_token (parser->lexer);
36522 if (token->type == CPP_ELLIPSIS)
36524 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36525 *ellipsisp = true;
36526 token = cp_lexer_peek_token (parser->lexer);
36527 break;
36530 /* TODO: parse attributes for tail parameters. */
36531 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36532 false, NULL);
36533 parm = grokdeclarator (parmdecl->declarator,
36534 &parmdecl->decl_specifiers,
36535 PARM, /*initialized=*/0,
36536 /*attrlist=*/NULL);
36538 chainon (params, build_tree_list (NULL_TREE, parm));
36539 token = cp_lexer_peek_token (parser->lexer);
36542 /* We allow tail attributes for the method. */
36543 if (token->keyword == RID_ATTRIBUTE)
36545 if (*attributes == NULL_TREE)
36547 *attributes = cp_parser_attributes_opt (parser);
36548 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36549 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36550 return params;
36552 else
36553 /* We have an error, but parse the attributes, so that we can
36554 carry on. */
36555 *attributes = cp_parser_attributes_opt (parser);
36557 cp_parser_error (parser,
36558 "method attributes must be specified at the end");
36559 return error_mark_node;
36562 return params;
36565 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36567 static void
36568 cp_parser_objc_interstitial_code (cp_parser* parser)
36570 cp_token *token = cp_lexer_peek_token (parser->lexer);
36572 /* If the next token is `extern' and the following token is a string
36573 literal, then we have a linkage specification. */
36574 if (token->keyword == RID_EXTERN
36575 && cp_parser_is_pure_string_literal
36576 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36577 cp_parser_linkage_specification (parser, NULL_TREE);
36578 /* Handle #pragma, if any. */
36579 else if (token->type == CPP_PRAGMA)
36580 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36581 /* Allow stray semicolons. */
36582 else if (token->type == CPP_SEMICOLON)
36583 cp_lexer_consume_token (parser->lexer);
36584 /* Mark methods as optional or required, when building protocols. */
36585 else if (token->keyword == RID_AT_OPTIONAL)
36587 cp_lexer_consume_token (parser->lexer);
36588 objc_set_method_opt (true);
36590 else if (token->keyword == RID_AT_REQUIRED)
36592 cp_lexer_consume_token (parser->lexer);
36593 objc_set_method_opt (false);
36595 else if (token->keyword == RID_NAMESPACE)
36596 cp_parser_namespace_definition (parser);
36597 /* Other stray characters must generate errors. */
36598 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36600 cp_lexer_consume_token (parser->lexer);
36601 error ("stray %qs between Objective-C++ methods",
36602 token->type == CPP_OPEN_BRACE ? "{" : "}");
36604 /* Finally, try to parse a block-declaration, or a function-definition. */
36605 else
36606 cp_parser_block_declaration (parser, /*statement_p=*/false);
36609 /* Parse a method signature. */
36611 static tree
36612 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36614 tree rettype, kwdparms, optparms;
36615 bool ellipsis = false;
36616 bool is_class_method;
36618 is_class_method = cp_parser_objc_method_type (parser);
36619 rettype = cp_parser_objc_typename (parser);
36620 *attributes = NULL_TREE;
36621 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36622 if (kwdparms == error_mark_node)
36623 return error_mark_node;
36624 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36625 if (optparms == error_mark_node)
36626 return error_mark_node;
36628 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36631 static bool
36632 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36634 tree tattr;
36635 cp_lexer_save_tokens (parser->lexer);
36636 tattr = cp_parser_attributes_opt (parser);
36637 gcc_assert (tattr) ;
36639 /* If the attributes are followed by a method introducer, this is not allowed.
36640 Dump the attributes and flag the situation. */
36641 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36642 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36643 return true;
36645 /* Otherwise, the attributes introduce some interstitial code, possibly so
36646 rewind to allow that check. */
36647 cp_lexer_rollback_tokens (parser->lexer);
36648 return false;
36651 /* Parse an Objective-C method prototype list. */
36653 static void
36654 cp_parser_objc_method_prototype_list (cp_parser* parser)
36656 cp_token *token = cp_lexer_peek_token (parser->lexer);
36658 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36660 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36662 tree attributes, sig;
36663 bool is_class_method;
36664 if (token->type == CPP_PLUS)
36665 is_class_method = true;
36666 else
36667 is_class_method = false;
36668 sig = cp_parser_objc_method_signature (parser, &attributes);
36669 if (sig == error_mark_node)
36671 cp_parser_skip_to_end_of_block_or_statement (parser);
36672 token = cp_lexer_peek_token (parser->lexer);
36673 continue;
36675 objc_add_method_declaration (is_class_method, sig, attributes);
36676 cp_parser_consume_semicolon_at_end_of_statement (parser);
36678 else if (token->keyword == RID_AT_PROPERTY)
36679 cp_parser_objc_at_property_declaration (parser);
36680 else if (token->keyword == RID_ATTRIBUTE
36681 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36682 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36683 OPT_Wattributes,
36684 "prefix attributes are ignored for methods");
36685 else
36686 /* Allow for interspersed non-ObjC++ code. */
36687 cp_parser_objc_interstitial_code (parser);
36689 token = cp_lexer_peek_token (parser->lexer);
36692 if (token->type != CPP_EOF)
36693 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36694 else
36695 cp_parser_error (parser, "expected %<@end%>");
36697 objc_finish_interface ();
36700 /* Parse an Objective-C method definition list. */
36702 static void
36703 cp_parser_objc_method_definition_list (cp_parser* parser)
36705 for (;;)
36707 cp_token *token = cp_lexer_peek_token (parser->lexer);
36709 if (token->keyword == RID_AT_END)
36711 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36712 break;
36714 else if (token->type == CPP_EOF)
36716 cp_parser_error (parser, "expected %<@end%>");
36717 break;
36719 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36721 bool is_class_method = token->type == CPP_PLUS;
36723 push_deferring_access_checks (dk_deferred);
36724 tree attribute;
36725 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36726 if (sig == error_mark_node)
36727 cp_parser_skip_to_end_of_block_or_statement (parser);
36728 else
36730 objc_start_method_definition (is_class_method, sig,
36731 attribute, NULL_TREE);
36733 /* For historical reasons, we accept an optional semicolon. */
36734 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36735 cp_lexer_consume_token (parser->lexer);
36737 perform_deferred_access_checks (tf_warning_or_error);
36738 stop_deferring_access_checks ();
36739 tree meth
36740 = cp_parser_function_definition_after_declarator (parser, false);
36741 pop_deferring_access_checks ();
36742 objc_finish_method_definition (meth);
36745 /* The following case will be removed once @synthesize is
36746 completely implemented. */
36747 else if (token->keyword == RID_AT_PROPERTY)
36748 cp_parser_objc_at_property_declaration (parser);
36749 else if (token->keyword == RID_AT_SYNTHESIZE)
36750 cp_parser_objc_at_synthesize_declaration (parser);
36751 else if (token->keyword == RID_AT_DYNAMIC)
36752 cp_parser_objc_at_dynamic_declaration (parser);
36753 else if (token->keyword == RID_ATTRIBUTE
36754 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36755 warning_at (token->location, OPT_Wattributes,
36756 "prefix attributes are ignored for methods");
36757 else
36758 /* Allow for interspersed non-ObjC++ code. */
36759 cp_parser_objc_interstitial_code (parser);
36762 objc_finish_implementation ();
36765 /* Parse Objective-C ivars. */
36767 static void
36768 cp_parser_objc_class_ivars (cp_parser* parser)
36770 cp_token *token = cp_lexer_peek_token (parser->lexer);
36772 if (token->type != CPP_OPEN_BRACE)
36773 return; /* No ivars specified. */
36775 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36776 token = cp_lexer_peek_token (parser->lexer);
36778 while (token->type != CPP_CLOSE_BRACE
36779 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36781 cp_decl_specifier_seq declspecs;
36782 int decl_class_or_enum_p;
36783 tree prefix_attributes;
36785 cp_parser_objc_visibility_spec (parser);
36787 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36788 break;
36790 cp_parser_decl_specifier_seq (parser,
36791 CP_PARSER_FLAGS_OPTIONAL,
36792 &declspecs,
36793 &decl_class_or_enum_p);
36795 /* auto, register, static, extern, mutable. */
36796 if (declspecs.storage_class != sc_none)
36798 cp_parser_error (parser, "invalid type for instance variable");
36799 declspecs.storage_class = sc_none;
36802 /* thread_local. */
36803 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36805 cp_parser_error (parser, "invalid type for instance variable");
36806 declspecs.locations[ds_thread] = 0;
36809 /* typedef. */
36810 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36812 cp_parser_error (parser, "invalid type for instance variable");
36813 declspecs.locations[ds_typedef] = 0;
36816 prefix_attributes = declspecs.attributes;
36817 declspecs.attributes = NULL_TREE;
36819 /* Keep going until we hit the `;' at the end of the
36820 declaration. */
36821 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36823 tree width = NULL_TREE, attributes, first_attribute, decl;
36824 cp_declarator *declarator = NULL;
36825 int ctor_dtor_or_conv_p;
36827 /* Check for a (possibly unnamed) bitfield declaration. */
36828 token = cp_lexer_peek_token (parser->lexer);
36829 if (token->type == CPP_COLON)
36830 goto eat_colon;
36832 if (token->type == CPP_NAME
36833 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36834 == CPP_COLON))
36836 /* Get the name of the bitfield. */
36837 declarator = make_id_declarator (NULL_TREE,
36838 cp_parser_identifier (parser),
36839 sfk_none, token->location);
36841 eat_colon:
36842 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36843 /* Get the width of the bitfield. */
36844 width
36845 = cp_parser_constant_expression (parser);
36847 else
36849 /* Parse the declarator. */
36850 declarator
36851 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36852 CP_PARSER_FLAGS_NONE,
36853 &ctor_dtor_or_conv_p,
36854 /*parenthesized_p=*/NULL,
36855 /*member_p=*/false,
36856 /*friend_p=*/false,
36857 /*static_p=*/false);
36860 /* Look for attributes that apply to the ivar. */
36861 attributes = cp_parser_attributes_opt (parser);
36862 /* Remember which attributes are prefix attributes and
36863 which are not. */
36864 first_attribute = attributes;
36865 /* Combine the attributes. */
36866 attributes = attr_chainon (prefix_attributes, attributes);
36868 if (width)
36869 /* Create the bitfield declaration. */
36870 decl = grokbitfield (declarator, &declspecs,
36871 width, NULL_TREE, attributes);
36872 else
36873 decl = grokfield (declarator, &declspecs,
36874 NULL_TREE, /*init_const_expr_p=*/false,
36875 NULL_TREE, attributes);
36877 /* Add the instance variable. */
36878 if (decl != error_mark_node && decl != NULL_TREE)
36879 objc_add_instance_variable (decl);
36881 /* Reset PREFIX_ATTRIBUTES. */
36882 if (attributes != error_mark_node)
36884 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36885 attributes = TREE_CHAIN (attributes);
36886 if (attributes)
36887 TREE_CHAIN (attributes) = NULL_TREE;
36890 token = cp_lexer_peek_token (parser->lexer);
36892 if (token->type == CPP_COMMA)
36894 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36895 continue;
36897 break;
36900 cp_parser_consume_semicolon_at_end_of_statement (parser);
36901 token = cp_lexer_peek_token (parser->lexer);
36904 if (token->keyword == RID_AT_END)
36905 cp_parser_error (parser, "expected %<}%>");
36907 /* Do not consume the RID_AT_END, so it will be read again as terminating
36908 the @interface of @implementation. */
36909 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36910 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36912 /* For historical reasons, we accept an optional semicolon. */
36913 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36914 cp_lexer_consume_token (parser->lexer);
36917 /* Parse an Objective-C protocol declaration. */
36919 static void
36920 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36922 tree proto, protorefs;
36923 cp_token *tok;
36925 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36928 tok = cp_lexer_peek_token (parser->lexer);
36929 error_at (tok->location, "identifier expected after %<@protocol%>");
36930 cp_parser_consume_semicolon_at_end_of_statement (parser);
36931 return;
36934 /* See if we have a forward declaration or a definition. */
36935 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36937 /* Try a forward declaration first. */
36938 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36940 while (true)
36942 tree id;
36944 id = cp_parser_identifier (parser);
36945 if (id == error_mark_node)
36946 break;
36948 objc_declare_protocol (id, attributes);
36950 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36951 cp_lexer_consume_token (parser->lexer);
36952 else
36953 break;
36955 cp_parser_consume_semicolon_at_end_of_statement (parser);
36958 /* Ok, we got a full-fledged definition (or at least should). */
36959 else
36961 proto = cp_parser_identifier (parser);
36962 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36963 objc_start_protocol (proto, protorefs, attributes);
36964 cp_parser_objc_method_prototype_list (parser);
36968 /* Parse an Objective-C superclass or category. */
36970 static void
36971 cp_parser_objc_superclass_or_category (cp_parser *parser,
36972 bool iface_p,
36973 tree *super,
36974 tree *categ, bool *is_class_extension)
36976 cp_token *next = cp_lexer_peek_token (parser->lexer);
36978 *super = *categ = NULL_TREE;
36979 *is_class_extension = false;
36980 if (next->type == CPP_COLON)
36982 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36983 *super = cp_parser_identifier (parser);
36985 else if (next->type == CPP_OPEN_PAREN)
36987 matching_parens parens;
36988 parens.consume_open (parser); /* Eat '('. */
36990 /* If there is no category name, and this is an @interface, we
36991 have a class extension. */
36992 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36994 *categ = NULL_TREE;
36995 *is_class_extension = true;
36997 else
36998 *categ = cp_parser_identifier (parser);
37000 parens.require_close (parser);
37004 /* Parse an Objective-C class interface. */
37006 static void
37007 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
37009 tree name, super, categ, protos;
37010 bool is_class_extension;
37012 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
37013 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
37014 name = cp_parser_identifier (parser);
37015 if (name == error_mark_node)
37017 /* It's hard to recover because even if valid @interface stuff
37018 is to follow, we can't compile it (or validate it) if we
37019 don't even know which class it refers to. Let's assume this
37020 was a stray '@interface' token in the stream and skip it.
37022 return;
37024 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
37025 &is_class_extension);
37026 protos = cp_parser_objc_protocol_refs_opt (parser);
37028 /* We have either a class or a category on our hands. */
37029 if (categ || is_class_extension)
37030 objc_start_category_interface (name, categ, protos, attributes);
37031 else
37033 objc_start_class_interface (name, nam_loc, super, protos, attributes);
37034 /* Handle instance variable declarations, if any. */
37035 cp_parser_objc_class_ivars (parser);
37036 objc_continue_interface ();
37039 cp_parser_objc_method_prototype_list (parser);
37042 /* Parse an Objective-C class implementation. */
37044 static void
37045 cp_parser_objc_class_implementation (cp_parser* parser)
37047 tree name, super, categ;
37048 bool is_class_extension;
37050 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
37051 name = cp_parser_identifier (parser);
37052 if (name == error_mark_node)
37054 /* It's hard to recover because even if valid @implementation
37055 stuff is to follow, we can't compile it (or validate it) if
37056 we don't even know which class it refers to. Let's assume
37057 this was a stray '@implementation' token in the stream and
37058 skip it.
37060 return;
37062 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
37063 &is_class_extension);
37065 /* We have either a class or a category on our hands. */
37066 if (categ)
37067 objc_start_category_implementation (name, categ);
37068 else
37070 objc_start_class_implementation (name, super);
37071 /* Handle instance variable declarations, if any. */
37072 cp_parser_objc_class_ivars (parser);
37073 objc_continue_implementation ();
37076 cp_parser_objc_method_definition_list (parser);
37079 /* Consume the @end token and finish off the implementation. */
37081 static void
37082 cp_parser_objc_end_implementation (cp_parser* parser)
37084 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37085 objc_finish_implementation ();
37088 /* Parse an Objective-C declaration. */
37090 static void
37091 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
37093 /* Try to figure out what kind of declaration is present. */
37094 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37096 if (attributes)
37097 switch (kwd->keyword)
37099 case RID_AT_ALIAS:
37100 case RID_AT_CLASS:
37101 case RID_AT_END:
37102 error_at (kwd->location, "attributes may not be specified before"
37103 " the %<@%D%> Objective-C++ keyword",
37104 kwd->u.value);
37105 attributes = NULL;
37106 break;
37107 case RID_AT_IMPLEMENTATION:
37108 warning_at (kwd->location, OPT_Wattributes,
37109 "prefix attributes are ignored before %<@%D%>",
37110 kwd->u.value);
37111 attributes = NULL;
37112 default:
37113 break;
37116 switch (kwd->keyword)
37118 case RID_AT_ALIAS:
37119 cp_parser_objc_alias_declaration (parser);
37120 break;
37121 case RID_AT_CLASS:
37122 cp_parser_objc_class_declaration (parser);
37123 break;
37124 case RID_AT_PROTOCOL:
37125 cp_parser_objc_protocol_declaration (parser, attributes);
37126 break;
37127 case RID_AT_INTERFACE:
37128 cp_parser_objc_class_interface (parser, attributes);
37129 break;
37130 case RID_AT_IMPLEMENTATION:
37131 cp_parser_objc_class_implementation (parser);
37132 break;
37133 case RID_AT_END:
37134 cp_parser_objc_end_implementation (parser);
37135 break;
37136 default:
37137 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37138 kwd->u.value);
37139 cp_parser_skip_to_end_of_block_or_statement (parser);
37143 /* Parse an Objective-C try-catch-finally statement.
37145 objc-try-catch-finally-stmt:
37146 @try compound-statement objc-catch-clause-seq [opt]
37147 objc-finally-clause [opt]
37149 objc-catch-clause-seq:
37150 objc-catch-clause objc-catch-clause-seq [opt]
37152 objc-catch-clause:
37153 @catch ( objc-exception-declaration ) compound-statement
37155 objc-finally-clause:
37156 @finally compound-statement
37158 objc-exception-declaration:
37159 parameter-declaration
37160 '...'
37162 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
37164 Returns NULL_TREE.
37166 PS: This function is identical to c_parser_objc_try_catch_finally_statement
37167 for C. Keep them in sync. */
37169 static tree
37170 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
37172 location_t location;
37173 tree stmt;
37175 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
37176 location = cp_lexer_peek_token (parser->lexer)->location;
37177 objc_maybe_warn_exceptions (location);
37178 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
37179 node, lest it get absorbed into the surrounding block. */
37180 stmt = push_stmt_list ();
37181 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37182 objc_begin_try_stmt (location, pop_stmt_list (stmt));
37184 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
37186 cp_parameter_declarator *parm;
37187 tree parameter_declaration = error_mark_node;
37188 bool seen_open_paren = false;
37189 matching_parens parens;
37191 cp_lexer_consume_token (parser->lexer);
37192 if (parens.require_open (parser))
37193 seen_open_paren = true;
37194 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
37196 /* We have "@catch (...)" (where the '...' are literally
37197 what is in the code). Skip the '...'.
37198 parameter_declaration is set to NULL_TREE, and
37199 objc_being_catch_clauses() knows that that means
37200 '...'. */
37201 cp_lexer_consume_token (parser->lexer);
37202 parameter_declaration = NULL_TREE;
37204 else
37206 /* We have "@catch (NSException *exception)" or something
37207 like that. Parse the parameter declaration. */
37208 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
37209 false, NULL);
37210 if (parm == NULL)
37211 parameter_declaration = error_mark_node;
37212 else
37213 parameter_declaration = grokdeclarator (parm->declarator,
37214 &parm->decl_specifiers,
37215 PARM, /*initialized=*/0,
37216 /*attrlist=*/NULL);
37218 if (seen_open_paren)
37219 parens.require_close (parser);
37220 else
37222 /* If there was no open parenthesis, we are recovering from
37223 an error, and we are trying to figure out what mistake
37224 the user has made. */
37226 /* If there is an immediate closing parenthesis, the user
37227 probably forgot the opening one (ie, they typed "@catch
37228 NSException *e)". Parse the closing parenthesis and keep
37229 going. */
37230 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37231 cp_lexer_consume_token (parser->lexer);
37233 /* If these is no immediate closing parenthesis, the user
37234 probably doesn't know that parenthesis are required at
37235 all (ie, they typed "@catch NSException *e"). So, just
37236 forget about the closing parenthesis and keep going. */
37238 objc_begin_catch_clause (parameter_declaration);
37239 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37240 objc_finish_catch_clause ();
37242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
37244 cp_lexer_consume_token (parser->lexer);
37245 location = cp_lexer_peek_token (parser->lexer)->location;
37246 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
37247 node, lest it get absorbed into the surrounding block. */
37248 stmt = push_stmt_list ();
37249 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37250 objc_build_finally_clause (location, pop_stmt_list (stmt));
37253 return objc_finish_try_stmt ();
37256 /* Parse an Objective-C synchronized statement.
37258 objc-synchronized-stmt:
37259 @synchronized ( expression ) compound-statement
37261 Returns NULL_TREE. */
37263 static tree
37264 cp_parser_objc_synchronized_statement (cp_parser *parser)
37266 location_t location;
37267 tree lock, stmt;
37269 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
37271 location = cp_lexer_peek_token (parser->lexer)->location;
37272 objc_maybe_warn_exceptions (location);
37273 matching_parens parens;
37274 parens.require_open (parser);
37275 lock = cp_parser_expression (parser);
37276 parens.require_close (parser);
37278 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
37279 node, lest it get absorbed into the surrounding block. */
37280 stmt = push_stmt_list ();
37281 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37283 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
37286 /* Parse an Objective-C throw statement.
37288 objc-throw-stmt:
37289 @throw assignment-expression [opt] ;
37291 Returns a constructed '@throw' statement. */
37293 static tree
37294 cp_parser_objc_throw_statement (cp_parser *parser)
37296 tree expr = NULL_TREE;
37297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37299 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
37301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37302 expr = cp_parser_expression (parser);
37304 cp_parser_consume_semicolon_at_end_of_statement (parser);
37306 return objc_build_throw_stmt (loc, expr);
37309 /* Parse an Objective-C statement. */
37311 static tree
37312 cp_parser_objc_statement (cp_parser * parser)
37314 /* Try to figure out what kind of declaration is present. */
37315 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37317 switch (kwd->keyword)
37319 case RID_AT_TRY:
37320 return cp_parser_objc_try_catch_finally_statement (parser);
37321 case RID_AT_SYNCHRONIZED:
37322 return cp_parser_objc_synchronized_statement (parser);
37323 case RID_AT_THROW:
37324 return cp_parser_objc_throw_statement (parser);
37325 default:
37326 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37327 kwd->u.value);
37328 cp_parser_skip_to_end_of_block_or_statement (parser);
37331 return error_mark_node;
37334 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
37335 look ahead to see if an objc keyword follows the attributes. This
37336 is to detect the use of prefix attributes on ObjC @interface and
37337 @protocol. */
37339 static bool
37340 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
37342 cp_lexer_save_tokens (parser->lexer);
37343 tree addon = cp_parser_attributes_opt (parser);
37344 if (addon
37345 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
37347 cp_lexer_commit_tokens (parser->lexer);
37348 if (*attrib)
37349 TREE_CHAIN (*attrib) = addon;
37350 else
37351 *attrib = addon;
37352 return true;
37354 cp_lexer_rollback_tokens (parser->lexer);
37355 return false;
37358 /* This routine is a minimal replacement for
37359 c_parser_struct_declaration () used when parsing the list of
37360 types/names or ObjC++ properties. For example, when parsing the
37361 code
37363 @property (readonly) int a, b, c;
37365 this function is responsible for parsing "int a, int b, int c" and
37366 returning the declarations as CHAIN of DECLs.
37368 TODO: Share this code with cp_parser_objc_class_ivars. It's very
37369 similar parsing. */
37370 static tree
37371 cp_parser_objc_struct_declaration (cp_parser *parser)
37373 tree decls = NULL_TREE;
37374 cp_decl_specifier_seq declspecs;
37375 int decl_class_or_enum_p;
37376 tree prefix_attributes;
37378 cp_parser_decl_specifier_seq (parser,
37379 CP_PARSER_FLAGS_NONE,
37380 &declspecs,
37381 &decl_class_or_enum_p);
37383 if (declspecs.type == error_mark_node)
37384 return error_mark_node;
37386 /* auto, register, static, extern, mutable. */
37387 if (declspecs.storage_class != sc_none)
37389 cp_parser_error (parser, "invalid type for property");
37390 declspecs.storage_class = sc_none;
37393 /* thread_local. */
37394 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37396 cp_parser_error (parser, "invalid type for property");
37397 declspecs.locations[ds_thread] = 0;
37400 /* typedef. */
37401 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37403 cp_parser_error (parser, "invalid type for property");
37404 declspecs.locations[ds_typedef] = 0;
37407 prefix_attributes = declspecs.attributes;
37408 declspecs.attributes = NULL_TREE;
37410 /* Keep going until we hit the `;' at the end of the declaration. */
37411 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37413 tree attributes, first_attribute, decl;
37414 cp_declarator *declarator;
37415 cp_token *token;
37417 /* Parse the declarator. */
37418 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37419 CP_PARSER_FLAGS_NONE,
37420 NULL, NULL, false, false, false);
37422 /* Look for attributes that apply to the ivar. */
37423 attributes = cp_parser_attributes_opt (parser);
37424 /* Remember which attributes are prefix attributes and
37425 which are not. */
37426 first_attribute = attributes;
37427 /* Combine the attributes. */
37428 attributes = attr_chainon (prefix_attributes, attributes);
37430 decl = grokfield (declarator, &declspecs,
37431 NULL_TREE, /*init_const_expr_p=*/false,
37432 NULL_TREE, attributes);
37434 if (decl == error_mark_node || decl == NULL_TREE)
37435 return error_mark_node;
37437 /* Reset PREFIX_ATTRIBUTES. */
37438 if (attributes != error_mark_node)
37440 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37441 attributes = TREE_CHAIN (attributes);
37442 if (attributes)
37443 TREE_CHAIN (attributes) = NULL_TREE;
37446 DECL_CHAIN (decl) = decls;
37447 decls = decl;
37449 token = cp_lexer_peek_token (parser->lexer);
37450 if (token->type == CPP_COMMA)
37452 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37453 continue;
37455 else
37456 break;
37458 return decls;
37461 /* Parse an Objective-C @property declaration. The syntax is:
37463 objc-property-declaration:
37464 '@property' objc-property-attributes[opt] struct-declaration ;
37466 objc-property-attributes:
37467 '(' objc-property-attribute-list ')'
37469 objc-property-attribute-list:
37470 objc-property-attribute
37471 objc-property-attribute-list, objc-property-attribute
37473 objc-property-attribute
37474 'getter' = identifier
37475 'setter' = identifier
37476 'readonly'
37477 'readwrite'
37478 'assign'
37479 'retain'
37480 'copy'
37481 'nonatomic'
37483 For example:
37484 @property NSString *name;
37485 @property (readonly) id object;
37486 @property (retain, nonatomic, getter=getTheName) id name;
37487 @property int a, b, c;
37489 PS: This function is identical to
37490 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37491 static void
37492 cp_parser_objc_at_property_declaration (cp_parser *parser)
37494 /* Parse the optional attribute list.
37496 A list of parsed, but not verified, attributes. */
37497 auto_delete_vec<property_attribute_info> prop_attr_list;
37498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37500 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37502 /* Parse the optional attribute list... */
37503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37505 /* Eat the '('. */
37506 matching_parens parens;
37507 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37508 parens.consume_open (parser);
37509 bool syntax_error = false;
37511 /* Allow empty @property attribute lists, but with a warning. */
37512 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37513 location_t attr_comb;
37514 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37516 attr_comb = make_location (attr_end, attr_start, attr_end);
37517 warning_at (attr_comb, OPT_Wattributes,
37518 "empty property attribute list");
37520 else
37521 while (true)
37523 cp_token *token = cp_lexer_peek_token (parser->lexer);
37524 attr_start = token->location;
37525 attr_end = get_finish (token->location);
37526 attr_comb = make_location (attr_start, attr_start, attr_end);
37528 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37530 warning_at (attr_comb, OPT_Wattributes,
37531 "missing property attribute");
37532 if (token->type == CPP_CLOSE_PAREN)
37533 break;
37534 cp_lexer_consume_token (parser->lexer);
37535 continue;
37538 tree attr_name = NULL_TREE;
37539 if (identifier_p (token->u.value))
37540 attr_name = token->u.value;
37542 enum rid keyword;
37543 if (token->type == CPP_NAME)
37544 keyword = C_RID_CODE (token->u.value);
37545 else if (token->type == CPP_KEYWORD
37546 && token->keyword == RID_CLASS)
37547 /* Account for accepting the 'class' keyword in this context. */
37548 keyword = RID_CLASS;
37549 else
37550 keyword = RID_MAX; /* By definition, an unknown property. */
37551 cp_lexer_consume_token (parser->lexer);
37553 enum objc_property_attribute_kind prop_kind
37554 = objc_prop_attr_kind_for_rid (keyword);
37555 property_attribute_info *prop
37556 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37557 prop_attr_list.safe_push (prop);
37559 tree meth_name;
37560 switch (prop->prop_kind)
37562 default: break;
37563 case OBJC_PROPERTY_ATTR_UNKNOWN:
37564 if (attr_name)
37565 error_at (attr_start, "unknown property attribute %qE",
37566 attr_name);
37567 else
37568 error_at (attr_start, "unknown property attribute");
37569 prop->parse_error = syntax_error = true;
37570 break;
37572 case OBJC_PROPERTY_ATTR_GETTER:
37573 case OBJC_PROPERTY_ATTR_SETTER:
37574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37576 attr_comb = make_location (attr_end, attr_start, attr_end);
37577 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37578 attr_name);
37579 prop->parse_error = syntax_error = true;
37580 break;
37583 token = cp_lexer_peek_token (parser->lexer);
37584 attr_end = token->location;
37585 cp_lexer_consume_token (parser->lexer); /* eat the = */
37587 if (!cp_parser_objc_selector_p
37588 (cp_lexer_peek_token (parser->lexer)->type))
37590 attr_comb = make_location (attr_end, attr_start, attr_end);
37591 error_at (attr_comb, "expected %qE selector name",
37592 attr_name);
37593 prop->parse_error = syntax_error = true;
37594 break;
37597 /* Get the end of the method name, and consume the name. */
37598 token = cp_lexer_peek_token (parser->lexer);
37599 attr_end = get_finish (token->location);
37600 /* Because method names may contain C++ keywords, we have a
37601 routine to fetch them (this also consumes the token). */
37602 meth_name = cp_parser_objc_selector (parser);
37604 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37608 attr_comb = make_location (attr_end, attr_start,
37609 attr_end);
37610 error_at (attr_comb, "setter method names must"
37611 " terminate with %<:%>");
37612 prop->parse_error = syntax_error = true;
37614 else
37616 attr_end = get_finish (cp_lexer_peek_token
37617 (parser->lexer)->location);
37618 cp_lexer_consume_token (parser->lexer);
37620 attr_comb = make_location (attr_start, attr_start,
37621 attr_end);
37623 else
37624 attr_comb = make_location (attr_start, attr_start,
37625 attr_end);
37626 prop->ident = meth_name;
37627 /* Updated location including all that was successfully
37628 parsed. */
37629 prop->prop_loc = attr_comb;
37630 break;
37633 /* If we see a comma here, then keep going - even if we already
37634 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37635 this makes a more useful output and avoid spurious warnings
37636 about missing attributes that are, in fact, specified after the
37637 one with the syntax error. */
37638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37639 cp_lexer_consume_token (parser->lexer);
37640 else
37641 break;
37644 if (syntax_error || !parens.require_close (parser))
37645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37646 /*or_comma=*/false,
37647 /*consume_paren=*/true);
37650 /* 'properties' is the list of properties that we read. Usually a
37651 single one, but maybe more (eg, in "@property int a, b, c;" there
37652 are three).
37653 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37654 that we can issue a meaningful and consistent (between C/C++) error
37655 message from objc_add_property_declaration (). */
37656 tree properties = cp_parser_objc_struct_declaration (parser);
37658 if (properties == error_mark_node)
37659 cp_parser_skip_to_end_of_statement (parser);
37660 else if (properties == NULL_TREE)
37661 cp_parser_error (parser, "expected identifier");
37662 else
37664 /* Comma-separated properties are chained together in reverse order;
37665 add them one by one. */
37666 properties = nreverse (properties);
37667 for (; properties; properties = TREE_CHAIN (properties))
37668 objc_add_property_declaration (loc, copy_node (properties),
37669 prop_attr_list);
37672 cp_parser_consume_semicolon_at_end_of_statement (parser);
37675 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37677 objc-synthesize-declaration:
37678 @synthesize objc-synthesize-identifier-list ;
37680 objc-synthesize-identifier-list:
37681 objc-synthesize-identifier
37682 objc-synthesize-identifier-list, objc-synthesize-identifier
37684 objc-synthesize-identifier
37685 identifier
37686 identifier = identifier
37688 For example:
37689 @synthesize MyProperty;
37690 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37692 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37693 for C. Keep them in sync.
37695 static void
37696 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37698 tree list = NULL_TREE;
37699 location_t loc;
37700 loc = cp_lexer_peek_token (parser->lexer)->location;
37702 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37703 while (true)
37705 tree property, ivar;
37706 property = cp_parser_identifier (parser);
37707 if (property == error_mark_node)
37709 cp_parser_consume_semicolon_at_end_of_statement (parser);
37710 return;
37712 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37714 cp_lexer_consume_token (parser->lexer);
37715 ivar = cp_parser_identifier (parser);
37716 if (ivar == error_mark_node)
37718 cp_parser_consume_semicolon_at_end_of_statement (parser);
37719 return;
37722 else
37723 ivar = NULL_TREE;
37724 list = chainon (list, build_tree_list (ivar, property));
37725 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37726 cp_lexer_consume_token (parser->lexer);
37727 else
37728 break;
37730 cp_parser_consume_semicolon_at_end_of_statement (parser);
37731 objc_add_synthesize_declaration (loc, list);
37734 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37736 objc-dynamic-declaration:
37737 @dynamic identifier-list ;
37739 For example:
37740 @dynamic MyProperty;
37741 @dynamic MyProperty, AnotherProperty;
37743 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37744 for C. Keep them in sync.
37746 static void
37747 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37749 tree list = NULL_TREE;
37750 location_t loc;
37751 loc = cp_lexer_peek_token (parser->lexer)->location;
37753 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37754 while (true)
37756 tree property;
37757 property = cp_parser_identifier (parser);
37758 if (property == error_mark_node)
37760 cp_parser_consume_semicolon_at_end_of_statement (parser);
37761 return;
37763 list = chainon (list, build_tree_list (NULL, property));
37764 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37765 cp_lexer_consume_token (parser->lexer);
37766 else
37767 break;
37769 cp_parser_consume_semicolon_at_end_of_statement (parser);
37770 objc_add_dynamic_declaration (loc, list);
37774 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37776 /* Returns name of the next clause.
37777 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37778 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37779 returned and the token is consumed. */
37781 static pragma_omp_clause
37782 cp_parser_omp_clause_name (cp_parser *parser)
37784 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37787 result = PRAGMA_OACC_CLAUSE_AUTO;
37788 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37789 result = PRAGMA_OMP_CLAUSE_IF;
37790 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37791 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37792 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37793 result = PRAGMA_OACC_CLAUSE_DELETE;
37794 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37795 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37796 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37797 result = PRAGMA_OMP_CLAUSE_FOR;
37798 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37801 const char *p = IDENTIFIER_POINTER (id);
37803 switch (p[0])
37805 case 'a':
37806 if (!strcmp ("affinity", p))
37807 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37808 else if (!strcmp ("aligned", p))
37809 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37810 else if (!strcmp ("allocate", p))
37811 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37812 else if (!strcmp ("async", p))
37813 result = PRAGMA_OACC_CLAUSE_ASYNC;
37814 else if (!strcmp ("attach", p))
37815 result = PRAGMA_OACC_CLAUSE_ATTACH;
37816 break;
37817 case 'b':
37818 if (!strcmp ("bind", p))
37819 result = PRAGMA_OMP_CLAUSE_BIND;
37820 break;
37821 case 'c':
37822 if (!strcmp ("collapse", p))
37823 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37824 else if (!strcmp ("copy", p))
37825 result = PRAGMA_OACC_CLAUSE_COPY;
37826 else if (!strcmp ("copyin", p))
37827 result = PRAGMA_OMP_CLAUSE_COPYIN;
37828 else if (!strcmp ("copyout", p))
37829 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37830 else if (!strcmp ("copyprivate", p))
37831 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37832 else if (!strcmp ("create", p))
37833 result = PRAGMA_OACC_CLAUSE_CREATE;
37834 break;
37835 case 'd':
37836 if (!strcmp ("defaultmap", p))
37837 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37838 else if (!strcmp ("depend", p))
37839 result = PRAGMA_OMP_CLAUSE_DEPEND;
37840 else if (!strcmp ("detach", p))
37841 result = PRAGMA_OACC_CLAUSE_DETACH;
37842 else if (!strcmp ("device", p))
37843 result = PRAGMA_OMP_CLAUSE_DEVICE;
37844 else if (!strcmp ("deviceptr", p))
37845 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37846 else if (!strcmp ("device_resident", p))
37847 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37848 else if (!strcmp ("device_type", p))
37849 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37850 else if (!strcmp ("dist_schedule", p))
37851 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37852 else if (!strcmp ("doacross", p))
37853 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37854 break;
37855 case 'e':
37856 if (!strcmp ("enter", p))
37857 result = PRAGMA_OMP_CLAUSE_ENTER;
37858 break;
37859 case 'f':
37860 if (!strcmp ("filter", p))
37861 result = PRAGMA_OMP_CLAUSE_FILTER;
37862 else if (!strcmp ("final", p))
37863 result = PRAGMA_OMP_CLAUSE_FINAL;
37864 else if (!strcmp ("finalize", p))
37865 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37866 else if (!strcmp ("firstprivate", p))
37867 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37868 else if (!strcmp ("from", p))
37869 result = PRAGMA_OMP_CLAUSE_FROM;
37870 break;
37871 case 'g':
37872 if (!strcmp ("gang", p))
37873 result = PRAGMA_OACC_CLAUSE_GANG;
37874 else if (!strcmp ("grainsize", p))
37875 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37876 break;
37877 case 'h':
37878 if (!strcmp ("has_device_addr", p))
37879 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37880 else if (!strcmp ("hint", p))
37881 result = PRAGMA_OMP_CLAUSE_HINT;
37882 else if (!strcmp ("host", p))
37883 result = PRAGMA_OACC_CLAUSE_HOST;
37884 break;
37885 case 'i':
37886 if (!strcmp ("if_present", p))
37887 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37888 else if (!strcmp ("in_reduction", p))
37889 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37890 else if (!strcmp ("inbranch", p))
37891 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37892 else if (!strcmp ("independent", p))
37893 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37894 else if (!strcmp ("indirect", p))
37895 result = PRAGMA_OMP_CLAUSE_INDIRECT;
37896 else if (!strcmp ("is_device_ptr", p))
37897 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37898 break;
37899 case 'l':
37900 if (!strcmp ("lastprivate", p))
37901 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37902 else if (!strcmp ("linear", p))
37903 result = PRAGMA_OMP_CLAUSE_LINEAR;
37904 else if (!strcmp ("link", p))
37905 result = PRAGMA_OMP_CLAUSE_LINK;
37906 break;
37907 case 'm':
37908 if (!strcmp ("map", p))
37909 result = PRAGMA_OMP_CLAUSE_MAP;
37910 else if (!strcmp ("mergeable", p))
37911 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37912 break;
37913 case 'n':
37914 if (!strcmp ("no_create", p))
37915 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37916 else if (!strcmp ("nogroup", p))
37917 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37918 else if (!strcmp ("nohost", p))
37919 result = PRAGMA_OACC_CLAUSE_NOHOST;
37920 else if (!strcmp ("nontemporal", p))
37921 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37922 else if (!strcmp ("notinbranch", p))
37923 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37924 else if (!strcmp ("nowait", p))
37925 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37926 else if (!strcmp ("num_gangs", p))
37927 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37928 else if (!strcmp ("num_tasks", p))
37929 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37930 else if (!strcmp ("num_teams", p))
37931 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37932 else if (!strcmp ("num_threads", p))
37933 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37934 else if (!strcmp ("num_workers", p))
37935 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37936 break;
37937 case 'o':
37938 if (!strcmp ("ordered", p))
37939 result = PRAGMA_OMP_CLAUSE_ORDERED;
37940 else if (!strcmp ("order", p))
37941 result = PRAGMA_OMP_CLAUSE_ORDER;
37942 break;
37943 case 'p':
37944 if (!strcmp ("parallel", p))
37945 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37946 else if (!strcmp ("present", p))
37947 result = PRAGMA_OACC_CLAUSE_PRESENT;
37948 else if (!strcmp ("present_or_copy", p)
37949 || !strcmp ("pcopy", p))
37950 result = PRAGMA_OACC_CLAUSE_COPY;
37951 else if (!strcmp ("present_or_copyin", p)
37952 || !strcmp ("pcopyin", p))
37953 result = PRAGMA_OACC_CLAUSE_COPYIN;
37954 else if (!strcmp ("present_or_copyout", p)
37955 || !strcmp ("pcopyout", p))
37956 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37957 else if (!strcmp ("present_or_create", p)
37958 || !strcmp ("pcreate", p))
37959 result = PRAGMA_OACC_CLAUSE_CREATE;
37960 else if (!strcmp ("priority", p))
37961 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37962 else if (!strcmp ("proc_bind", p))
37963 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37964 break;
37965 case 'r':
37966 if (!strcmp ("reduction", p))
37967 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37968 break;
37969 case 's':
37970 if (!strcmp ("safelen", p))
37971 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37972 else if (!strcmp ("schedule", p))
37973 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37974 else if (!strcmp ("sections", p))
37975 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37976 else if (!strcmp ("self", p))
37977 result = PRAGMA_OACC_CLAUSE_SELF;
37978 else if (!strcmp ("seq", p))
37979 result = PRAGMA_OACC_CLAUSE_SEQ;
37980 else if (!strcmp ("shared", p))
37981 result = PRAGMA_OMP_CLAUSE_SHARED;
37982 else if (!strcmp ("simd", p))
37983 result = PRAGMA_OMP_CLAUSE_SIMD;
37984 else if (!strcmp ("simdlen", p))
37985 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37986 break;
37987 case 't':
37988 if (!strcmp ("task_reduction", p))
37989 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37990 else if (!strcmp ("taskgroup", p))
37991 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37992 else if (!strcmp ("thread_limit", p))
37993 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37994 else if (!strcmp ("threads", p))
37995 result = PRAGMA_OMP_CLAUSE_THREADS;
37996 else if (!strcmp ("tile", p))
37997 result = PRAGMA_OACC_CLAUSE_TILE;
37998 else if (!strcmp ("to", p))
37999 result = PRAGMA_OMP_CLAUSE_TO;
38000 break;
38001 case 'u':
38002 if (!strcmp ("uniform", p))
38003 result = PRAGMA_OMP_CLAUSE_UNIFORM;
38004 else if (!strcmp ("untied", p))
38005 result = PRAGMA_OMP_CLAUSE_UNTIED;
38006 else if (!strcmp ("use_device", p))
38007 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
38008 else if (!strcmp ("use_device_addr", p))
38009 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
38010 else if (!strcmp ("use_device_ptr", p))
38011 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
38012 break;
38013 case 'v':
38014 if (!strcmp ("vector", p))
38015 result = PRAGMA_OACC_CLAUSE_VECTOR;
38016 else if (!strcmp ("vector_length", p))
38017 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
38018 break;
38019 case 'w':
38020 if (!strcmp ("wait", p))
38021 result = PRAGMA_OACC_CLAUSE_WAIT;
38022 else if (!strcmp ("worker", p))
38023 result = PRAGMA_OACC_CLAUSE_WORKER;
38024 break;
38028 if (result != PRAGMA_OMP_CLAUSE_NONE)
38029 cp_lexer_consume_token (parser->lexer);
38031 return result;
38034 /* Validate that a clause of the given type does not already exist. */
38036 static void
38037 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
38038 const char *name, location_t location)
38040 if (omp_find_clause (clauses, code))
38041 error_at (location, "too many %qs clauses", name);
38044 /* OpenMP 2.5:
38045 variable-list:
38046 identifier
38047 variable-list , identifier
38049 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
38050 colon). An opening parenthesis will have been consumed by the caller.
38052 If KIND is nonzero, create the appropriate node and install the decl
38053 in OMP_CLAUSE_DECL and add the node to the head of the list.
38055 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
38056 return the list created.
38058 COLON can be NULL if only closing parenthesis should end the list,
38059 or pointer to bool which will receive false if the list is terminated
38060 by closing parenthesis or true if the list is terminated by colon.
38062 The optional ALLOW_DEREF argument is true if list items can use the deref
38063 (->) operator. */
38065 struct omp_dim
38067 tree low_bound, length;
38068 location_t loc;
38069 bool no_colon;
38070 omp_dim (tree lb, tree len, location_t lo, bool nc)
38071 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
38074 static tree
38075 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
38076 tree list, bool *colon,
38077 bool map_lvalue = false)
38079 auto_vec<omp_dim> dims;
38080 bool array_section_p;
38081 cp_token *token;
38082 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38083 if (colon)
38085 parser->colon_corrects_to_scope_p = false;
38086 *colon = false;
38088 while (1)
38090 tree name, decl;
38092 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38093 cp_parser_parse_tentatively (parser);
38094 /* This condition doesn't include OMP_CLAUSE_DEPEND or
38095 OMP_CLAUSE_AFFINITY since lvalue ("locator list") parsing for those is
38096 handled further down the function. */
38097 else if (map_lvalue
38098 && (kind == OMP_CLAUSE_MAP
38099 || kind == OMP_CLAUSE_TO
38100 || kind == OMP_CLAUSE_FROM))
38102 auto s = make_temp_override (parser->omp_array_section_p, true);
38103 token = cp_lexer_peek_token (parser->lexer);
38104 location_t loc = token->location;
38105 decl = cp_parser_assignment_expression (parser);
38107 /* This code rewrites a parsed expression containing various tree
38108 codes used to represent array accesses into a more uniform nest of
38109 OMP_ARRAY_SECTION nodes before it is processed by
38110 semantics.cc:handle_omp_array_sections_1. It might be more
38111 efficient to move this logic to that function instead, analysing
38112 the parsed expression directly rather than this preprocessed
38113 form. */
38114 dims.truncate (0);
38115 if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38117 while (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38119 tree low_bound = TREE_OPERAND (decl, 1);
38120 tree length = TREE_OPERAND (decl, 2);
38121 dims.safe_push (omp_dim (low_bound, length, loc, false));
38122 decl = TREE_OPERAND (decl, 0);
38125 while (TREE_CODE (decl) == ARRAY_REF
38126 || TREE_CODE (decl) == INDIRECT_REF
38127 || TREE_CODE (decl) == COMPOUND_EXPR)
38129 if (REFERENCE_REF_P (decl))
38130 break;
38132 if (TREE_CODE (decl) == COMPOUND_EXPR)
38134 decl = TREE_OPERAND (decl, 1);
38135 STRIP_NOPS (decl);
38137 else if (TREE_CODE (decl) == INDIRECT_REF)
38139 dims.safe_push (omp_dim (integer_zero_node,
38140 integer_one_node, loc, true));
38141 decl = TREE_OPERAND (decl, 0);
38143 else /* ARRAY_REF. */
38145 tree index = TREE_OPERAND (decl, 1);
38146 dims.safe_push (omp_dim (index, integer_one_node, loc,
38147 true));
38148 decl = TREE_OPERAND (decl, 0);
38152 /* Bare references have their own special handling, so remove
38153 the explicit dereference added by convert_from_reference. */
38154 if (REFERENCE_REF_P (decl))
38155 decl = TREE_OPERAND (decl, 0);
38157 for (int i = dims.length () - 1; i >= 0; i--)
38158 decl = grok_omp_array_section (loc, decl, dims[i].low_bound,
38159 dims[i].length);
38161 else if (TREE_CODE (decl) == INDIRECT_REF)
38163 bool ref_p = REFERENCE_REF_P (decl);
38165 /* If we have "*foo" and
38166 - it's an indirection of a reference, "unconvert" it, i.e.
38167 strip the indirection (to just "foo").
38168 - it's an indirection of a pointer, turn it into
38169 "foo[0:1]". */
38170 decl = TREE_OPERAND (decl, 0);
38171 STRIP_NOPS (decl);
38173 if (!ref_p)
38174 decl = grok_omp_array_section (loc, decl, integer_zero_node,
38175 integer_one_node);
38177 else if (TREE_CODE (decl) == ARRAY_REF)
38179 tree idx = TREE_OPERAND (decl, 1);
38181 decl = TREE_OPERAND (decl, 0);
38182 STRIP_NOPS (decl);
38184 decl = grok_omp_array_section (loc, decl, idx, integer_one_node);
38186 else if (TREE_CODE (decl) == NON_LVALUE_EXPR
38187 || CONVERT_EXPR_P (decl))
38188 decl = TREE_OPERAND (decl, 0);
38190 goto build_clause;
38192 token = cp_lexer_peek_token (parser->lexer);
38193 if (kind != 0
38194 && cp_parser_is_keyword (token, RID_THIS))
38196 decl = finish_this_expr ();
38197 if (TREE_CODE (decl) == NON_LVALUE_EXPR
38198 || CONVERT_EXPR_P (decl))
38199 decl = TREE_OPERAND (decl, 0);
38200 cp_lexer_consume_token (parser->lexer);
38202 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
38203 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
38204 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
38206 cp_id_kind idk;
38207 decl = cp_parser_primary_expression (parser, false, false, false,
38208 &idk);
38210 else if (kind == OMP_CLAUSE_DEPEND
38211 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
38212 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38213 || cp_lexer_nth_token_is (parser->lexer, 2,
38214 CPP_CLOSE_PAREN)))
38216 decl = ridpointers[RID_OMP_ALL_MEMORY];
38217 cp_lexer_consume_token (parser->lexer);
38219 else
38221 name = cp_parser_id_expression (parser, /*template_p=*/false,
38222 /*check_dependency_p=*/true,
38223 /*template_p=*/NULL,
38224 /*declarator_p=*/false,
38225 /*optional_p=*/false);
38226 if (name == error_mark_node)
38228 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38229 && cp_parser_simulate_error (parser))
38230 goto depend_lvalue;
38231 goto skip_comma;
38234 if (identifier_p (name))
38235 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38236 else
38237 decl = name;
38238 if (decl == error_mark_node)
38240 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38241 && cp_parser_simulate_error (parser))
38242 goto depend_lvalue;
38243 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38244 token->location);
38247 if (outer_automatic_var_p (decl))
38248 decl = process_outer_var_ref (decl, tf_warning_or_error);
38249 if (decl == error_mark_node)
38251 else if (kind != 0)
38253 switch (kind)
38255 case OMP_CLAUSE__CACHE_:
38256 /* The OpenACC cache directive explicitly only allows "array
38257 elements or subarrays". */
38258 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
38260 error_at (token->location, "expected %<[%>");
38261 decl = error_mark_node;
38262 break;
38264 /* FALLTHROUGH. */
38265 case OMP_CLAUSE_MAP:
38266 case OMP_CLAUSE_FROM:
38267 case OMP_CLAUSE_TO:
38268 start_component_ref:
38269 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38270 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
38272 cpp_ttype ttype
38273 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38274 ? CPP_DOT : CPP_DEREF;
38275 location_t loc
38276 = cp_lexer_peek_token (parser->lexer)->location;
38277 cp_id_kind idk = CP_ID_KIND_NONE;
38278 cp_lexer_consume_token (parser->lexer);
38279 decl = convert_from_reference (decl);
38280 decl = (cp_parser_postfix_dot_deref_expression
38281 (parser, ttype, cp_expr (decl, token->location),
38282 false, &idk, loc));
38284 /* FALLTHROUGH. */
38285 case OMP_CLAUSE_AFFINITY:
38286 case OMP_CLAUSE_DEPEND:
38287 case OMP_CLAUSE_REDUCTION:
38288 case OMP_CLAUSE_IN_REDUCTION:
38289 case OMP_CLAUSE_TASK_REDUCTION:
38290 case OMP_CLAUSE_HAS_DEVICE_ADDR:
38291 array_section_p = false;
38292 dims.truncate (0);
38293 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
38295 location_t loc = UNKNOWN_LOCATION;
38296 tree low_bound = NULL_TREE, length = NULL_TREE;
38297 bool no_colon = false;
38299 parser->colon_corrects_to_scope_p = false;
38300 cp_lexer_consume_token (parser->lexer);
38301 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38303 loc = cp_lexer_peek_token (parser->lexer)->location;
38304 low_bound = cp_parser_expression (parser);
38305 /* Later handling is not prepared to see through these. */
38306 gcc_checking_assert (!location_wrapper_p (low_bound));
38308 if (!colon)
38309 parser->colon_corrects_to_scope_p
38310 = saved_colon_corrects_to_scope_p;
38311 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
38313 length = integer_one_node;
38314 no_colon = true;
38316 else
38318 /* Look for `:'. */
38319 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38321 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38322 && cp_parser_simulate_error (parser))
38323 goto depend_lvalue;
38324 goto skip_comma;
38326 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38327 cp_parser_commit_to_tentative_parse (parser);
38328 else
38329 array_section_p = true;
38330 if (!cp_lexer_next_token_is (parser->lexer,
38331 CPP_CLOSE_SQUARE))
38333 length = cp_parser_expression (parser);
38334 /* Later handling is not prepared to see through these. */
38335 gcc_checking_assert (!location_wrapper_p (length));
38338 /* Look for the closing `]'. */
38339 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
38340 RT_CLOSE_SQUARE))
38342 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38343 && cp_parser_simulate_error (parser))
38344 goto depend_lvalue;
38345 goto skip_comma;
38348 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
38351 if ((kind == OMP_CLAUSE_MAP
38352 || kind == OMP_CLAUSE_FROM
38353 || kind == OMP_CLAUSE_TO)
38354 && !array_section_p
38355 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38356 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
38358 for (unsigned i = 0; i < dims.length (); i++)
38360 gcc_assert (dims[i].length == integer_one_node);
38361 decl = build_array_ref (dims[i].loc,
38362 decl, dims[i].low_bound);
38364 goto start_component_ref;
38366 else
38367 for (unsigned i = 0; i < dims.length (); i++)
38368 decl = build_omp_array_section (input_location, decl,
38369 dims[i].low_bound,
38370 dims[i].length);
38371 break;
38372 default:
38373 break;
38376 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38379 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
38380 && cp_parser_simulate_error (parser))
38382 depend_lvalue:
38383 cp_parser_abort_tentative_parse (parser);
38384 decl = cp_parser_assignment_expression (parser, NULL,
38385 false, false);
38387 else
38388 cp_parser_parse_definitely (parser);
38391 build_clause:
38392 tree u = build_omp_clause (token->location, kind);
38393 OMP_CLAUSE_DECL (u) = decl;
38394 OMP_CLAUSE_CHAIN (u) = list;
38395 list = u;
38397 else
38398 list = tree_cons (decl, NULL_TREE, list);
38400 get_comma:
38401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38402 break;
38403 cp_lexer_consume_token (parser->lexer);
38406 if (colon)
38407 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38409 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38411 *colon = true;
38412 cp_parser_require (parser, CPP_COLON, RT_COLON);
38413 return list;
38416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38418 int ending;
38420 /* Try to resync to an unnested comma. Copied from
38421 cp_parser_parenthesized_expression_list. */
38422 skip_comma:
38423 if (colon)
38424 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38425 ending = cp_parser_skip_to_closing_parenthesis (parser,
38426 /*recovering=*/true,
38427 /*or_comma=*/true,
38428 /*consume_paren=*/true);
38429 if (ending < 0)
38430 goto get_comma;
38433 return list;
38436 /* Similarly, but expect leading and trailing parenthesis. This is a very
38437 common case for omp clauses. */
38439 static tree
38440 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
38441 bool map_lvalue = false)
38443 if (parser->lexer->in_omp_decl_attribute)
38445 if (kind)
38447 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38448 tree u = build_omp_clause (loc, kind);
38449 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
38450 OMP_CLAUSE_CHAIN (u) = list;
38451 return u;
38453 else
38454 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
38455 list);
38458 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38459 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
38460 map_lvalue);
38461 return list;
38464 /* OpenACC 2.0:
38465 copy ( variable-list )
38466 copyin ( variable-list )
38467 copyout ( variable-list )
38468 create ( variable-list )
38469 delete ( variable-list )
38470 present ( variable-list )
38472 OpenACC 2.6:
38473 no_create ( variable-list )
38474 attach ( variable-list )
38475 detach ( variable-list ) */
38477 static tree
38478 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38479 tree list)
38481 enum gomp_map_kind kind;
38482 switch (c_kind)
38484 case PRAGMA_OACC_CLAUSE_ATTACH:
38485 kind = GOMP_MAP_ATTACH;
38486 break;
38487 case PRAGMA_OACC_CLAUSE_COPY:
38488 kind = GOMP_MAP_TOFROM;
38489 break;
38490 case PRAGMA_OACC_CLAUSE_COPYIN:
38491 kind = GOMP_MAP_TO;
38492 break;
38493 case PRAGMA_OACC_CLAUSE_COPYOUT:
38494 kind = GOMP_MAP_FROM;
38495 break;
38496 case PRAGMA_OACC_CLAUSE_CREATE:
38497 kind = GOMP_MAP_ALLOC;
38498 break;
38499 case PRAGMA_OACC_CLAUSE_DELETE:
38500 kind = GOMP_MAP_RELEASE;
38501 break;
38502 case PRAGMA_OACC_CLAUSE_DETACH:
38503 kind = GOMP_MAP_DETACH;
38504 break;
38505 case PRAGMA_OACC_CLAUSE_DEVICE:
38506 kind = GOMP_MAP_FORCE_TO;
38507 break;
38508 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38509 kind = GOMP_MAP_DEVICE_RESIDENT;
38510 break;
38511 case PRAGMA_OACC_CLAUSE_LINK:
38512 kind = GOMP_MAP_LINK;
38513 break;
38514 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38515 kind = GOMP_MAP_IF_PRESENT;
38516 break;
38517 case PRAGMA_OACC_CLAUSE_PRESENT:
38518 kind = GOMP_MAP_FORCE_PRESENT;
38519 break;
38520 case PRAGMA_OACC_CLAUSE_SELF:
38521 /* "The 'host' clause is a synonym for the 'self' clause." */
38522 case PRAGMA_OACC_CLAUSE_HOST:
38523 kind = GOMP_MAP_FORCE_FROM;
38524 break;
38525 default:
38526 gcc_unreachable ();
38528 tree nl, c;
38529 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, false);
38531 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38532 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38534 return nl;
38537 /* OpenACC 2.0:
38538 deviceptr ( variable-list ) */
38540 static tree
38541 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38543 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38544 tree vars, t;
38546 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38547 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38548 variable-list must only allow for pointer variables. */
38549 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38550 for (t = vars; t; t = TREE_CHAIN (t))
38552 tree v = TREE_PURPOSE (t);
38553 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38554 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38555 OMP_CLAUSE_DECL (u) = v;
38556 OMP_CLAUSE_CHAIN (u) = list;
38557 list = u;
38560 return list;
38563 /* OpenACC 2.5:
38564 auto
38565 finalize
38566 independent
38567 nohost
38568 seq */
38570 static tree
38571 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38572 tree list)
38574 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38576 tree c = build_omp_clause (loc, code);
38577 OMP_CLAUSE_CHAIN (c) = list;
38579 return c;
38582 /* OpenACC:
38583 num_gangs ( expression )
38584 num_workers ( expression )
38585 vector_length ( expression ) */
38587 static tree
38588 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38589 const char *str, tree list)
38591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38593 matching_parens parens;
38594 if (!parens.require_open (parser))
38595 return list;
38597 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38599 if (t == error_mark_node
38600 || !parens.require_close (parser))
38602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38603 /*or_comma=*/false,
38604 /*consume_paren=*/true);
38605 return list;
38608 check_no_duplicate_clause (list, code, str, loc);
38610 tree c = build_omp_clause (loc, code);
38611 OMP_CLAUSE_OPERAND (c, 0) = t;
38612 OMP_CLAUSE_CHAIN (c) = list;
38613 return c;
38616 /* OpenACC:
38618 gang [( gang-arg-list )]
38619 worker [( [num:] int-expr )]
38620 vector [( [length:] int-expr )]
38622 where gang-arg is one of:
38624 [num:] int-expr
38625 static: size-expr
38627 and size-expr may be:
38630 int-expr
38633 static tree
38634 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38635 omp_clause_code kind,
38636 const char *str, tree list)
38638 const char *id = "num";
38639 cp_lexer *lexer = parser->lexer;
38640 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38642 if (kind == OMP_CLAUSE_VECTOR)
38643 id = "length";
38645 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38647 matching_parens parens;
38648 parens.consume_open (parser);
38652 cp_token *next = cp_lexer_peek_token (lexer);
38653 int idx = 0;
38655 /* Gang static argument. */
38656 if (kind == OMP_CLAUSE_GANG
38657 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38659 cp_lexer_consume_token (lexer);
38661 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38662 goto cleanup_error;
38664 idx = 1;
38665 if (ops[idx] != NULL)
38667 cp_parser_error (parser, "too many %<static%> arguments");
38668 goto cleanup_error;
38671 /* Check for the '*' argument. */
38672 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38673 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38674 || cp_lexer_nth_token_is (parser->lexer, 2,
38675 CPP_CLOSE_PAREN)))
38677 cp_lexer_consume_token (lexer);
38678 ops[idx] = integer_minus_one_node;
38680 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38682 cp_lexer_consume_token (lexer);
38683 continue;
38685 else break;
38688 /* Worker num: argument and vector length: arguments. */
38689 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38690 && id_equal (next->u.value, id)
38691 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38693 cp_lexer_consume_token (lexer); /* id */
38694 cp_lexer_consume_token (lexer); /* ':' */
38697 /* Now collect the actual argument. */
38698 if (ops[idx] != NULL_TREE)
38700 cp_parser_error (parser, "unexpected argument");
38701 goto cleanup_error;
38704 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38705 false);
38706 if (expr == error_mark_node)
38707 goto cleanup_error;
38709 mark_exp_read (expr);
38710 ops[idx] = expr;
38712 if (kind == OMP_CLAUSE_GANG
38713 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38715 cp_lexer_consume_token (lexer);
38716 continue;
38718 break;
38720 while (1);
38722 if (!parens.require_close (parser))
38723 goto cleanup_error;
38726 check_no_duplicate_clause (list, kind, str, loc);
38728 c = build_omp_clause (loc, kind);
38730 if (ops[1])
38731 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38733 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38734 OMP_CLAUSE_CHAIN (c) = list;
38736 return c;
38738 cleanup_error:
38739 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38740 return list;
38743 /* OpenACC 2.0:
38744 tile ( size-expr-list ) */
38746 static tree
38747 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38749 tree c, expr = error_mark_node;
38750 tree tile = NULL_TREE;
38752 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38753 so, but the spec authors never considered such a case and have
38754 differing opinions on what it might mean, including 'not
38755 allowed'.) */
38756 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38757 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38758 clause_loc);
38760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38761 return list;
38765 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38766 return list;
38768 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38769 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38770 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38772 cp_lexer_consume_token (parser->lexer);
38773 expr = integer_zero_node;
38775 else
38776 expr = cp_parser_constant_expression (parser);
38778 tile = tree_cons (NULL_TREE, expr, tile);
38780 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38782 /* Consume the trailing ')'. */
38783 cp_lexer_consume_token (parser->lexer);
38785 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38786 tile = nreverse (tile);
38787 OMP_CLAUSE_TILE_LIST (c) = tile;
38788 OMP_CLAUSE_CHAIN (c) = list;
38789 return c;
38792 /* OpenACC 2.0
38793 Parse wait clause or directive parameters. */
38795 static tree
38796 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38798 vec<tree, va_gc> *args;
38799 tree t, args_tree;
38801 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38802 /*cast_p=*/false,
38803 /*allow_expansion_p=*/true,
38804 /*non_constant_p=*/NULL);
38806 if (args == NULL || args->length () == 0)
38808 if (args != NULL)
38810 cp_parser_error (parser, "expected integer expression list");
38811 release_tree_vector (args);
38813 return list;
38816 args_tree = build_tree_list_vec (args);
38818 release_tree_vector (args);
38820 for (t = args_tree; t; t = TREE_CHAIN (t))
38822 tree targ = TREE_VALUE (t);
38824 if (targ != error_mark_node)
38826 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38827 error ("%<wait%> expression must be integral");
38828 else
38830 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38832 targ = mark_rvalue_use (targ);
38833 OMP_CLAUSE_DECL (c) = targ;
38834 OMP_CLAUSE_CHAIN (c) = list;
38835 list = c;
38840 return list;
38843 /* OpenACC:
38844 wait [( int-expr-list )] */
38846 static tree
38847 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38849 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38851 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38852 list = cp_parser_oacc_wait_list (parser, location, list);
38853 else
38855 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38857 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38858 OMP_CLAUSE_CHAIN (c) = list;
38859 list = c;
38862 return list;
38865 /* OpenMP 3.0:
38866 collapse ( constant-expression ) */
38868 static tree
38869 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38871 tree c, num;
38872 location_t loc;
38873 HOST_WIDE_INT n;
38875 loc = cp_lexer_peek_token (parser->lexer)->location;
38876 matching_parens parens;
38877 if (!parens.require_open (parser))
38878 return list;
38880 num = cp_parser_constant_expression (parser);
38882 if (!parens.require_close (parser))
38883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38884 /*or_comma=*/false,
38885 /*consume_paren=*/true);
38887 if (num == error_mark_node)
38888 return list;
38889 num = fold_non_dependent_expr (num);
38890 if (!tree_fits_shwi_p (num)
38891 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38892 || (n = tree_to_shwi (num)) <= 0
38893 || (int) n != n)
38895 error_at (loc, "collapse argument needs positive constant integer expression");
38896 return list;
38899 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38900 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38901 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38902 OMP_CLAUSE_CHAIN (c) = list;
38903 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38905 return c;
38908 /* OpenMP 2.5:
38909 default ( none | shared )
38911 OpenMP 5.1:
38912 default ( private | firstprivate )
38914 OpenACC:
38915 default ( none | present ) */
38917 static tree
38918 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38919 location_t location, bool is_oacc)
38921 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38922 tree c;
38924 matching_parens parens;
38925 if (!parens.require_open (parser))
38926 return list;
38927 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38929 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38930 cp_lexer_consume_token (parser->lexer);
38932 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38935 const char *p = IDENTIFIER_POINTER (id);
38937 switch (p[0])
38939 case 'n':
38940 if (strcmp ("none", p) != 0)
38941 goto invalid_kind;
38942 kind = OMP_CLAUSE_DEFAULT_NONE;
38943 break;
38945 case 'p':
38946 if (strcmp ("present", p) != 0 || !is_oacc)
38947 goto invalid_kind;
38948 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38949 break;
38951 case 'f':
38952 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38953 goto invalid_kind;
38954 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38955 break;
38957 case 's':
38958 if (strcmp ("shared", p) != 0 || is_oacc)
38959 goto invalid_kind;
38960 kind = OMP_CLAUSE_DEFAULT_SHARED;
38961 break;
38963 default:
38964 goto invalid_kind;
38967 cp_lexer_consume_token (parser->lexer);
38969 else
38971 invalid_kind:
38972 if (is_oacc)
38973 cp_parser_error (parser, "expected %<none%> or %<present%>");
38974 else
38975 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38976 "%<private%> or %<firstprivate%>");
38979 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38980 || !parens.require_close (parser))
38981 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38982 /*or_comma=*/false,
38983 /*consume_paren=*/true);
38985 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38986 return list;
38988 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38989 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38990 OMP_CLAUSE_CHAIN (c) = list;
38991 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38993 return c;
38996 /* OpenMP 3.1:
38997 final ( expression ) */
38999 static tree
39000 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
39002 tree t, c;
39004 matching_parens parens;
39005 if (!parens.require_open (parser))
39006 return list;
39008 t = cp_parser_assignment_expression (parser);
39010 if (t == error_mark_node
39011 || !parens.require_close (parser))
39012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39013 /*or_comma=*/false,
39014 /*consume_paren=*/true);
39016 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
39018 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
39019 OMP_CLAUSE_FINAL_EXPR (c) = t;
39020 OMP_CLAUSE_CHAIN (c) = list;
39022 return c;
39025 /* OpenMP 5.1:
39026 indirect [( expression )]
39029 static tree
39030 cp_parser_omp_clause_indirect (cp_parser *parser, tree list,
39031 location_t location)
39033 tree t;
39035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39037 matching_parens parens;
39038 if (!parens.require_open (parser))
39039 return list;
39041 bool non_constant_p;
39042 t = cp_parser_constant_expression (parser, true, &non_constant_p);
39044 if (t != error_mark_node && non_constant_p)
39045 error_at (location, "expected constant logical expression");
39047 if (t == error_mark_node
39048 || !parens.require_close (parser))
39049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39050 /*or_comma=*/false,
39051 /*consume_paren=*/true);
39053 else
39054 t = integer_one_node;
39056 check_no_duplicate_clause (list, OMP_CLAUSE_INDIRECT, "indirect", location);
39058 tree c = build_omp_clause (location, OMP_CLAUSE_INDIRECT);
39059 OMP_CLAUSE_INDIRECT_EXPR (c) = t;
39060 OMP_CLAUSE_CHAIN (c) = list;
39062 return c;
39065 /* OpenMP 2.5:
39066 if ( expression )
39068 OpenMP 4.5:
39069 if ( directive-name-modifier : expression )
39071 directive-name-modifier:
39072 parallel | task | taskloop | target data | target | target update
39073 | target enter data | target exit data
39075 OpenMP 5.0:
39076 directive-name-modifier:
39077 ... | simd | cancel */
39079 static tree
39080 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
39081 bool is_omp)
39083 tree t, c;
39084 enum tree_code if_modifier = ERROR_MARK;
39086 matching_parens parens;
39087 if (!parens.require_open (parser))
39088 return list;
39090 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39093 const char *p = IDENTIFIER_POINTER (id);
39094 int n = 2;
39096 if (strcmp ("cancel", p) == 0)
39097 if_modifier = VOID_CST;
39098 else if (strcmp ("parallel", p) == 0)
39099 if_modifier = OMP_PARALLEL;
39100 else if (strcmp ("simd", p) == 0)
39101 if_modifier = OMP_SIMD;
39102 else if (strcmp ("task", p) == 0)
39103 if_modifier = OMP_TASK;
39104 else if (strcmp ("taskloop", p) == 0)
39105 if_modifier = OMP_TASKLOOP;
39106 else if (strcmp ("target", p) == 0)
39108 if_modifier = OMP_TARGET;
39109 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39111 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
39112 p = IDENTIFIER_POINTER (id);
39113 if (strcmp ("data", p) == 0)
39114 if_modifier = OMP_TARGET_DATA;
39115 else if (strcmp ("update", p) == 0)
39116 if_modifier = OMP_TARGET_UPDATE;
39117 else if (strcmp ("enter", p) == 0)
39118 if_modifier = OMP_TARGET_ENTER_DATA;
39119 else if (strcmp ("exit", p) == 0)
39120 if_modifier = OMP_TARGET_EXIT_DATA;
39121 if (if_modifier != OMP_TARGET)
39122 n = 3;
39123 else
39125 location_t loc
39126 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
39127 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
39128 "or %<exit%>");
39129 if_modifier = ERROR_MARK;
39131 if (if_modifier == OMP_TARGET_ENTER_DATA
39132 || if_modifier == OMP_TARGET_EXIT_DATA)
39134 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
39136 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39137 p = IDENTIFIER_POINTER (id);
39138 if (strcmp ("data", p) == 0)
39139 n = 4;
39141 if (n != 4)
39143 location_t loc
39144 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
39145 error_at (loc, "expected %<data%>");
39146 if_modifier = ERROR_MARK;
39151 if (if_modifier != ERROR_MARK)
39153 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
39155 while (n-- > 0)
39156 cp_lexer_consume_token (parser->lexer);
39158 else
39160 if (n > 2)
39162 location_t loc
39163 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
39164 error_at (loc, "expected %<:%>");
39166 if_modifier = ERROR_MARK;
39171 t = cp_parser_assignment_expression (parser);
39173 if (t == error_mark_node
39174 || !parens.require_close (parser))
39175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39176 /*or_comma=*/false,
39177 /*consume_paren=*/true);
39179 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39180 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
39182 if (if_modifier != ERROR_MARK
39183 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39185 const char *p = NULL;
39186 switch (if_modifier)
39188 case VOID_CST: p = "cancel"; break;
39189 case OMP_PARALLEL: p = "parallel"; break;
39190 case OMP_SIMD: p = "simd"; break;
39191 case OMP_TASK: p = "task"; break;
39192 case OMP_TASKLOOP: p = "taskloop"; break;
39193 case OMP_TARGET_DATA: p = "target data"; break;
39194 case OMP_TARGET: p = "target"; break;
39195 case OMP_TARGET_UPDATE: p = "target update"; break;
39196 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
39197 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
39198 default: gcc_unreachable ();
39200 error_at (location, "too many %<if%> clauses with %qs modifier",
39202 return list;
39204 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39206 if (!is_omp)
39207 error_at (location, "too many %<if%> clauses");
39208 else
39209 error_at (location, "too many %<if%> clauses without modifier");
39210 return list;
39212 else if (if_modifier == ERROR_MARK
39213 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
39215 error_at (location, "if any %<if%> clause has modifier, then all "
39216 "%<if%> clauses have to use modifier");
39217 return list;
39221 c = build_omp_clause (location, OMP_CLAUSE_IF);
39222 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
39223 OMP_CLAUSE_IF_EXPR (c) = t;
39224 OMP_CLAUSE_CHAIN (c) = list;
39226 return c;
39229 /* OpenMP 3.1:
39230 mergeable */
39232 static tree
39233 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
39234 tree list, location_t location)
39236 tree c;
39238 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
39239 location);
39241 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
39242 OMP_CLAUSE_CHAIN (c) = list;
39243 return c;
39246 /* OpenMP 2.5:
39247 nowait */
39249 static tree
39250 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
39251 tree list, location_t location)
39253 tree c;
39255 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
39257 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
39258 OMP_CLAUSE_CHAIN (c) = list;
39259 return c;
39262 /* OpenMP 2.5:
39263 num_threads ( expression ) */
39265 static tree
39266 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
39267 location_t location)
39269 tree t, c;
39271 matching_parens parens;
39272 if (!parens.require_open (parser))
39273 return list;
39275 t = cp_parser_assignment_expression (parser);
39277 if (t == error_mark_node
39278 || !parens.require_close (parser))
39279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39280 /*or_comma=*/false,
39281 /*consume_paren=*/true);
39283 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
39284 "num_threads", location);
39286 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
39287 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
39288 OMP_CLAUSE_CHAIN (c) = list;
39290 return c;
39293 /* OpenMP 4.5:
39294 num_tasks ( expression )
39296 OpenMP 5.1:
39297 num_tasks ( strict : expression ) */
39299 static tree
39300 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
39301 location_t location)
39303 tree t, c;
39305 matching_parens parens;
39306 if (!parens.require_open (parser))
39307 return list;
39309 bool strict = false;
39310 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39311 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39314 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39316 strict = true;
39317 cp_lexer_consume_token (parser->lexer);
39318 cp_lexer_consume_token (parser->lexer);
39322 t = cp_parser_assignment_expression (parser);
39324 if (t == error_mark_node
39325 || !parens.require_close (parser))
39326 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39327 /*or_comma=*/false,
39328 /*consume_paren=*/true);
39330 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
39331 "num_tasks", location);
39333 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
39334 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
39335 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
39336 OMP_CLAUSE_CHAIN (c) = list;
39338 return c;
39341 /* OpenMP 4.5:
39342 grainsize ( expression )
39344 OpenMP 5.1:
39345 grainsize ( strict : expression ) */
39347 static tree
39348 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
39349 location_t location)
39351 tree t, c;
39353 matching_parens parens;
39354 if (!parens.require_open (parser))
39355 return list;
39357 bool strict = false;
39358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39359 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39361 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39362 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39364 strict = true;
39365 cp_lexer_consume_token (parser->lexer);
39366 cp_lexer_consume_token (parser->lexer);
39370 t = cp_parser_assignment_expression (parser);
39372 if (t == error_mark_node
39373 || !parens.require_close (parser))
39374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39375 /*or_comma=*/false,
39376 /*consume_paren=*/true);
39378 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
39379 "grainsize", location);
39381 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
39382 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
39383 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
39384 OMP_CLAUSE_CHAIN (c) = list;
39386 return c;
39389 /* OpenMP 4.5:
39390 priority ( expression ) */
39392 static tree
39393 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
39394 location_t location)
39396 tree t, c;
39398 matching_parens parens;
39399 if (!parens.require_open (parser))
39400 return list;
39402 t = cp_parser_assignment_expression (parser);
39404 if (t == error_mark_node
39405 || !parens.require_close (parser))
39406 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39407 /*or_comma=*/false,
39408 /*consume_paren=*/true);
39410 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
39411 "priority", location);
39413 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
39414 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
39415 OMP_CLAUSE_CHAIN (c) = list;
39417 return c;
39420 /* OpenMP 4.5:
39421 hint ( expression ) */
39423 static tree
39424 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
39426 tree t, c;
39428 matching_parens parens;
39429 if (!parens.require_open (parser))
39430 return list;
39432 t = cp_parser_assignment_expression (parser);
39434 if (t != error_mark_node)
39436 t = fold_non_dependent_expr (t);
39437 if (!value_dependent_expression_p (t)
39438 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
39439 || !tree_fits_shwi_p (t)
39440 || tree_int_cst_sgn (t) == -1))
39441 error_at (location, "expected constant integer expression with "
39442 "valid sync-hint value");
39444 if (t == error_mark_node
39445 || !parens.require_close (parser))
39446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39447 /*or_comma=*/false,
39448 /*consume_paren=*/true);
39449 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
39451 c = build_omp_clause (location, OMP_CLAUSE_HINT);
39452 OMP_CLAUSE_HINT_EXPR (c) = t;
39453 OMP_CLAUSE_CHAIN (c) = list;
39455 return c;
39458 /* OpenMP 5.1:
39459 filter ( integer-expression ) */
39461 static tree
39462 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
39464 tree t, c;
39466 matching_parens parens;
39467 if (!parens.require_open (parser))
39468 return list;
39470 t = cp_parser_assignment_expression (parser);
39472 if (t == error_mark_node
39473 || !parens.require_close (parser))
39474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39475 /*or_comma=*/false,
39476 /*consume_paren=*/true);
39477 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
39479 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
39480 OMP_CLAUSE_FILTER_EXPR (c) = t;
39481 OMP_CLAUSE_CHAIN (c) = list;
39483 return c;
39486 /* OpenMP 4.5:
39487 defaultmap ( tofrom : scalar )
39489 OpenMP 5.0:
39490 defaultmap ( implicit-behavior [ : variable-category ] ) */
39492 static tree
39493 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
39494 location_t location)
39496 tree c, id;
39497 const char *p;
39498 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39499 enum omp_clause_defaultmap_kind category
39500 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
39502 matching_parens parens;
39503 if (!parens.require_open (parser))
39504 return list;
39506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
39507 p = "default";
39508 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39510 invalid_behavior:
39511 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
39512 "%<tofrom%>, %<firstprivate%>, %<none%> "
39513 "or %<default%>");
39514 goto out_err;
39516 else
39518 id = cp_lexer_peek_token (parser->lexer)->u.value;
39519 p = IDENTIFIER_POINTER (id);
39522 switch (p[0])
39524 case 'a':
39525 if (strcmp ("alloc", p) == 0)
39526 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39527 else
39528 goto invalid_behavior;
39529 break;
39531 case 'd':
39532 if (strcmp ("default", p) == 0)
39533 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39534 else
39535 goto invalid_behavior;
39536 break;
39538 case 'f':
39539 if (strcmp ("firstprivate", p) == 0)
39540 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39541 else if (strcmp ("from", p) == 0)
39542 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39543 else
39544 goto invalid_behavior;
39545 break;
39547 case 'n':
39548 if (strcmp ("none", p) == 0)
39549 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39550 else
39551 goto invalid_behavior;
39552 break;
39554 case 'p':
39555 if (strcmp ("present", p) == 0)
39556 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39557 else
39558 goto invalid_behavior;
39559 break;
39561 case 't':
39562 if (strcmp ("tofrom", p) == 0)
39563 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39564 else if (strcmp ("to", p) == 0)
39565 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39566 else
39567 goto invalid_behavior;
39568 break;
39570 default:
39571 goto invalid_behavior;
39573 cp_lexer_consume_token (parser->lexer);
39575 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39577 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39578 goto out_err;
39580 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39582 invalid_category:
39583 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39584 "%<all%>");
39585 goto out_err;
39587 id = cp_lexer_peek_token (parser->lexer)->u.value;
39588 p = IDENTIFIER_POINTER (id);
39590 switch (p[0])
39592 case 'a':
39593 if (strcmp ("aggregate", p) == 0)
39594 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39595 else if (strcmp ("all", p) == 0)
39596 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39597 else
39598 goto invalid_category;
39599 break;
39601 case 'p':
39602 if (strcmp ("pointer", p) == 0)
39603 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39604 else
39605 goto invalid_category;
39606 break;
39608 case 's':
39609 if (strcmp ("scalar", p) == 0)
39610 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39611 else
39612 goto invalid_category;
39613 break;
39615 default:
39616 goto invalid_category;
39619 cp_lexer_consume_token (parser->lexer);
39621 if (!parens.require_close (parser))
39622 goto out_err;
39624 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39625 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39626 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39627 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39628 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39629 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39630 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39631 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39632 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39634 enum omp_clause_defaultmap_kind cat = category;
39635 location_t loc = OMP_CLAUSE_LOCATION (c);
39636 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39637 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39638 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39639 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39640 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39641 p = NULL;
39642 switch (cat)
39644 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39645 p = NULL;
39646 break;
39647 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39648 p = "all";
39649 break;
39650 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39651 p = "aggregate";
39652 break;
39653 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39654 p = "pointer";
39655 break;
39656 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39657 p = "scalar";
39658 break;
39659 default:
39660 gcc_unreachable ();
39662 if (p)
39663 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39665 else
39666 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39667 "category");
39668 break;
39671 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39672 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39673 OMP_CLAUSE_CHAIN (c) = list;
39674 return c;
39676 out_err:
39677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39678 /*or_comma=*/false,
39679 /*consume_paren=*/true);
39680 return list;
39683 /* OpenMP 5.0:
39684 order ( concurrent )
39686 OpenMP 5.1:
39687 order ( order-modifier : concurrent )
39689 order-modifier:
39690 reproducible
39691 unconstrained */
39693 static tree
39694 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39696 tree c, id;
39697 const char *p;
39698 bool unconstrained = false;
39699 bool reproducible = false;
39701 matching_parens parens;
39702 if (!parens.require_open (parser))
39703 return list;
39705 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39706 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39708 id = cp_lexer_peek_token (parser->lexer)->u.value;
39709 p = IDENTIFIER_POINTER (id);
39710 if (strcmp (p, "unconstrained") == 0)
39711 unconstrained = true;
39712 else if (strcmp (p, "reproducible") == 0)
39713 reproducible = true;
39714 else
39716 cp_parser_error (parser, "expected %<reproducible%> or "
39717 "%<unconstrained%>");
39718 goto out_err;
39720 cp_lexer_consume_token (parser->lexer);
39721 cp_lexer_consume_token (parser->lexer);
39723 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39725 cp_parser_error (parser, "expected %<concurrent%>");
39726 goto out_err;
39728 else
39730 id = cp_lexer_peek_token (parser->lexer)->u.value;
39731 p = IDENTIFIER_POINTER (id);
39733 if (strcmp (p, "concurrent") != 0)
39735 cp_parser_error (parser, "expected %<concurrent%>");
39736 goto out_err;
39738 cp_lexer_consume_token (parser->lexer);
39739 if (!parens.require_close (parser))
39740 goto out_err;
39742 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39743 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39744 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39745 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39746 OMP_CLAUSE_CHAIN (c) = list;
39747 return c;
39749 out_err:
39750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39751 /*or_comma=*/false,
39752 /*consume_paren=*/true);
39753 return list;
39756 /* OpenMP 5.0:
39757 bind ( teams | parallel | thread ) */
39759 static tree
39760 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39761 location_t location)
39763 tree c;
39764 const char *p;
39765 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39767 matching_parens parens;
39768 if (!parens.require_open (parser))
39769 return list;
39771 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39773 invalid:
39774 cp_parser_error (parser,
39775 "expected %<teams%>, %<parallel%> or %<thread%>");
39776 goto out_err;
39778 else
39780 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39781 p = IDENTIFIER_POINTER (id);
39783 if (strcmp (p, "teams") == 0)
39784 kind = OMP_CLAUSE_BIND_TEAMS;
39785 else if (strcmp (p, "parallel") == 0)
39786 kind = OMP_CLAUSE_BIND_PARALLEL;
39787 else if (strcmp (p, "thread") != 0)
39788 goto invalid;
39789 cp_lexer_consume_token (parser->lexer);
39790 if (!parens.require_close (parser))
39791 goto out_err;
39793 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39794 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39795 OMP_CLAUSE_BIND_KIND (c) = kind;
39796 OMP_CLAUSE_CHAIN (c) = list;
39797 return c;
39799 out_err:
39800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39801 /*or_comma=*/false,
39802 /*consume_paren=*/true);
39803 return list;
39806 /* OpenMP 2.5:
39807 ordered
39809 OpenMP 4.5:
39810 ordered ( constant-expression ) */
39812 static tree
39813 cp_parser_omp_clause_ordered (cp_parser *parser,
39814 tree list, location_t location)
39816 tree c, num = NULL_TREE;
39817 HOST_WIDE_INT n;
39819 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39820 "ordered", location);
39822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39824 matching_parens parens;
39825 parens.consume_open (parser);
39827 num = cp_parser_constant_expression (parser);
39829 if (!parens.require_close (parser))
39830 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39831 /*or_comma=*/false,
39832 /*consume_paren=*/true);
39834 if (num == error_mark_node)
39835 return list;
39836 num = fold_non_dependent_expr (num);
39837 if (!tree_fits_shwi_p (num)
39838 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39839 || (n = tree_to_shwi (num)) <= 0
39840 || (int) n != n)
39842 error_at (location,
39843 "ordered argument needs positive constant integer "
39844 "expression");
39845 return list;
39849 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39850 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39851 OMP_CLAUSE_CHAIN (c) = list;
39852 return c;
39855 /* OpenMP 2.5:
39856 reduction ( reduction-operator : variable-list )
39858 reduction-operator:
39859 One of: + * - & ^ | && ||
39861 OpenMP 3.1:
39863 reduction-operator:
39864 One of: + * - & ^ | && || min max
39866 OpenMP 4.0:
39868 reduction-operator:
39869 One of: + * - & ^ | && ||
39870 id-expression
39872 OpenMP 5.0:
39873 reduction ( reduction-modifier, reduction-operator : variable-list )
39874 in_reduction ( reduction-operator : variable-list )
39875 task_reduction ( reduction-operator : variable-list ) */
39877 static tree
39878 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39879 bool is_omp, tree list)
39881 enum tree_code code = ERROR_MARK;
39882 tree nlist, c, id = NULL_TREE;
39883 bool task = false;
39884 bool inscan = false;
39886 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39887 return list;
39889 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39892 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39894 cp_lexer_consume_token (parser->lexer);
39895 cp_lexer_consume_token (parser->lexer);
39897 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39898 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39900 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39901 const char *p = IDENTIFIER_POINTER (id);
39902 if (strcmp (p, "task") == 0)
39903 task = true;
39904 else if (strcmp (p, "inscan") == 0)
39905 inscan = true;
39906 if (task || inscan)
39908 cp_lexer_consume_token (parser->lexer);
39909 cp_lexer_consume_token (parser->lexer);
39914 switch (cp_lexer_peek_token (parser->lexer)->type)
39916 case CPP_PLUS: code = PLUS_EXPR; break;
39917 case CPP_MULT: code = MULT_EXPR; break;
39918 case CPP_MINUS: code = MINUS_EXPR; break;
39919 case CPP_AND: code = BIT_AND_EXPR; break;
39920 case CPP_XOR: code = BIT_XOR_EXPR; break;
39921 case CPP_OR: code = BIT_IOR_EXPR; break;
39922 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39923 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39924 default: break;
39927 if (code != ERROR_MARK)
39928 cp_lexer_consume_token (parser->lexer);
39929 else
39931 bool saved_colon_corrects_to_scope_p;
39932 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39933 parser->colon_corrects_to_scope_p = false;
39934 id = cp_parser_id_expression (parser, /*template_p=*/false,
39935 /*check_dependency_p=*/true,
39936 /*template_p=*/NULL,
39937 /*declarator_p=*/false,
39938 /*optional_p=*/false);
39939 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39940 if (identifier_p (id))
39942 const char *p = IDENTIFIER_POINTER (id);
39944 if (strcmp (p, "min") == 0)
39945 code = MIN_EXPR;
39946 else if (strcmp (p, "max") == 0)
39947 code = MAX_EXPR;
39948 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39949 code = PLUS_EXPR;
39950 else if (id == ovl_op_identifier (false, MULT_EXPR))
39951 code = MULT_EXPR;
39952 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39953 code = MINUS_EXPR;
39954 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39955 code = BIT_AND_EXPR;
39956 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39957 code = BIT_IOR_EXPR;
39958 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39959 code = BIT_XOR_EXPR;
39960 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39961 code = TRUTH_ANDIF_EXPR;
39962 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39963 code = TRUTH_ORIF_EXPR;
39964 id = omp_reduction_id (code, id, NULL_TREE);
39965 tree scope = parser->scope;
39966 if (scope)
39967 id = build_qualified_name (NULL_TREE, scope, id, false);
39968 parser->scope = NULL_TREE;
39969 parser->qualifying_scope = NULL_TREE;
39970 parser->object_scope = NULL_TREE;
39972 else
39974 error ("invalid reduction-identifier");
39975 resync_fail:
39976 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39977 /*or_comma=*/false,
39978 /*consume_paren=*/true);
39979 return list;
39983 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39984 goto resync_fail;
39986 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39987 NULL);
39988 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39990 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39991 if (task)
39992 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39993 else if (inscan)
39994 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39995 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39998 return nlist;
40001 /* OpenMP 2.5:
40002 schedule ( schedule-kind )
40003 schedule ( schedule-kind , expression )
40005 schedule-kind:
40006 static | dynamic | guided | runtime | auto
40008 OpenMP 4.5:
40009 schedule ( schedule-modifier : schedule-kind )
40010 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
40012 schedule-modifier:
40013 simd
40014 monotonic
40015 nonmonotonic */
40017 static tree
40018 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
40020 tree c, t;
40021 int modifiers = 0, nmodifiers = 0;
40023 matching_parens parens;
40024 if (!parens.require_open (parser))
40025 return list;
40027 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
40029 location_t comma = UNKNOWN_LOCATION;
40030 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40032 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40033 const char *p = IDENTIFIER_POINTER (id);
40034 if (strcmp ("simd", p) == 0)
40035 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
40036 else if (strcmp ("monotonic", p) == 0)
40037 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
40038 else if (strcmp ("nonmonotonic", p) == 0)
40039 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
40040 else
40041 break;
40042 comma = UNKNOWN_LOCATION;
40043 cp_lexer_consume_token (parser->lexer);
40044 if (nmodifiers++ == 0
40045 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40047 comma = cp_lexer_peek_token (parser->lexer)->location;
40048 cp_lexer_consume_token (parser->lexer);
40050 else
40052 cp_parser_require (parser, CPP_COLON, RT_COLON);
40053 break;
40056 if (comma != UNKNOWN_LOCATION)
40057 error_at (comma, "expected %<:%>");
40059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40061 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40062 const char *p = IDENTIFIER_POINTER (id);
40064 switch (p[0])
40066 case 'd':
40067 if (strcmp ("dynamic", p) != 0)
40068 goto invalid_kind;
40069 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
40070 break;
40072 case 'g':
40073 if (strcmp ("guided", p) != 0)
40074 goto invalid_kind;
40075 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
40076 break;
40078 case 'r':
40079 if (strcmp ("runtime", p) != 0)
40080 goto invalid_kind;
40081 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
40082 break;
40084 default:
40085 goto invalid_kind;
40088 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40089 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
40090 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
40091 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
40092 else
40093 goto invalid_kind;
40094 cp_lexer_consume_token (parser->lexer);
40096 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
40097 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40098 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
40099 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40101 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
40102 "specified");
40103 modifiers = 0;
40106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40108 cp_token *token;
40109 cp_lexer_consume_token (parser->lexer);
40111 token = cp_lexer_peek_token (parser->lexer);
40112 t = cp_parser_assignment_expression (parser);
40114 if (t == error_mark_node)
40115 goto resync_fail;
40116 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
40117 error_at (token->location, "schedule %<runtime%> does not take "
40118 "a %<chunk_size%> parameter");
40119 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
40120 error_at (token->location, "schedule %<auto%> does not take "
40121 "a %<chunk_size%> parameter");
40122 else
40123 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
40125 if (!parens.require_close (parser))
40126 goto resync_fail;
40128 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40129 goto resync_fail;
40131 OMP_CLAUSE_SCHEDULE_KIND (c)
40132 = (enum omp_clause_schedule_kind)
40133 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
40135 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
40136 OMP_CLAUSE_CHAIN (c) = list;
40137 return c;
40139 invalid_kind:
40140 cp_parser_error (parser, "invalid schedule kind");
40141 resync_fail:
40142 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40143 /*or_comma=*/false,
40144 /*consume_paren=*/true);
40145 return list;
40148 /* OpenMP 3.0:
40149 untied */
40151 static tree
40152 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
40153 tree list, location_t location)
40155 tree c;
40157 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
40159 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
40160 OMP_CLAUSE_CHAIN (c) = list;
40161 return c;
40164 /* OpenMP 4.0:
40165 inbranch
40166 notinbranch */
40168 static tree
40169 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
40170 tree list, location_t location)
40172 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40173 tree c = build_omp_clause (location, code);
40174 OMP_CLAUSE_CHAIN (c) = list;
40175 return c;
40178 /* OpenMP 4.0:
40179 parallel
40181 sections
40182 taskgroup */
40184 static tree
40185 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
40186 enum omp_clause_code code,
40187 tree list, location_t location)
40189 tree c = build_omp_clause (location, code);
40190 OMP_CLAUSE_CHAIN (c) = list;
40191 return c;
40194 /* OpenMP 4.5:
40195 nogroup */
40197 static tree
40198 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
40199 tree list, location_t location)
40201 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
40202 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
40203 OMP_CLAUSE_CHAIN (c) = list;
40204 return c;
40207 /* OpenMP 4.5:
40208 simd
40209 threads */
40211 static tree
40212 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
40213 enum omp_clause_code code,
40214 tree list, location_t location)
40216 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40217 tree c = build_omp_clause (location, code);
40218 OMP_CLAUSE_CHAIN (c) = list;
40219 return c;
40222 /* OpenMP 4.0:
40223 num_teams ( expression )
40225 OpenMP 5.1:
40226 num_teams ( expression : expression ) */
40228 static tree
40229 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
40230 location_t location)
40232 tree upper, lower = NULL_TREE, c;
40234 matching_parens parens;
40235 if (!parens.require_open (parser))
40236 return list;
40238 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40239 parser->colon_corrects_to_scope_p = false;
40240 upper = cp_parser_assignment_expression (parser);
40241 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40243 if (upper != error_mark_node
40244 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40246 lower = upper;
40247 cp_lexer_consume_token (parser->lexer);
40248 upper = cp_parser_assignment_expression (parser);
40251 if (upper == error_mark_node
40252 || !parens.require_close (parser))
40253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40254 /*or_comma=*/false,
40255 /*consume_paren=*/true);
40257 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
40258 "num_teams", location);
40260 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
40261 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
40262 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
40263 OMP_CLAUSE_CHAIN (c) = list;
40265 return c;
40268 /* OpenMP 4.0:
40269 thread_limit ( expression ) */
40271 static tree
40272 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
40273 location_t location)
40275 tree t, c;
40277 matching_parens parens;
40278 if (!parens.require_open (parser))
40279 return list;
40281 t = cp_parser_assignment_expression (parser);
40283 if (t == error_mark_node
40284 || !parens.require_close (parser))
40285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40286 /*or_comma=*/false,
40287 /*consume_paren=*/true);
40289 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
40290 "thread_limit", location);
40292 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
40293 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
40294 OMP_CLAUSE_CHAIN (c) = list;
40296 return c;
40299 /* OpenMP 4.0:
40300 aligned ( variable-list )
40301 aligned ( variable-list : constant-expression ) */
40303 static tree
40304 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
40306 tree nlist, c, alignment = NULL_TREE;
40307 bool colon;
40309 matching_parens parens;
40310 if (!parens.require_open (parser))
40311 return list;
40313 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
40314 &colon);
40316 if (colon)
40318 alignment = cp_parser_constant_expression (parser);
40320 if (!parens.require_close (parser))
40321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40322 /*or_comma=*/false,
40323 /*consume_paren=*/true);
40325 if (alignment == error_mark_node)
40326 alignment = NULL_TREE;
40329 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40330 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
40332 return nlist;
40335 /* OpenMP 5.0:
40336 allocate ( variable-list )
40337 allocate ( expression : variable-list )
40339 OpenMP 5.1:
40340 allocate ( allocator-modifier : variable-list )
40341 allocate ( allocator-modifier , allocator-modifier : variable-list )
40343 allocator-modifier:
40344 allocator ( expression )
40345 align ( expression ) */
40347 static tree
40348 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
40350 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
40351 bool colon, has_modifiers = false;
40353 matching_parens parens;
40354 if (!parens.require_open (parser))
40355 return list;
40357 cp_parser_parse_tentatively (parser);
40358 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40359 parser->colon_corrects_to_scope_p = false;
40360 for (int mod = 0; mod < 2; mod++)
40361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40362 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40364 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40365 const char *p = IDENTIFIER_POINTER (id);
40366 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
40367 break;
40368 cp_lexer_consume_token (parser->lexer);
40369 matching_parens parens2;
40370 if (!parens2.require_open (parser))
40371 break;
40372 if (strcmp (p, "allocator") == 0)
40374 if (allocator != NULL_TREE)
40375 break;
40376 allocator = cp_parser_assignment_expression (parser);
40378 else
40380 if (align != NULL_TREE)
40381 break;
40382 align = cp_parser_assignment_expression (parser);
40384 if (!parens2.require_close (parser))
40385 break;
40386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40388 has_modifiers = true;
40389 break;
40391 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
40392 break;
40393 cp_lexer_consume_token (parser->lexer);
40395 else
40396 break;
40397 if (!has_modifiers)
40399 cp_parser_abort_tentative_parse (parser);
40400 align = NULL_TREE;
40401 allocator = NULL_TREE;
40402 cp_parser_parse_tentatively (parser);
40403 allocator = cp_parser_assignment_expression (parser);
40405 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40406 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40408 cp_parser_parse_definitely (parser);
40409 cp_lexer_consume_token (parser->lexer);
40410 if (allocator == error_mark_node)
40411 allocator = NULL_TREE;
40412 if (align == error_mark_node)
40413 align = NULL_TREE;
40415 else
40417 cp_parser_abort_tentative_parse (parser);
40418 allocator = NULL_TREE;
40419 align = NULL_TREE;
40422 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
40423 &colon);
40425 if (allocator || align)
40426 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40428 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40429 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
40432 return nlist;
40435 /* OpenMP 2.5:
40436 lastprivate ( variable-list )
40438 OpenMP 5.0:
40439 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
40441 static tree
40442 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
40444 bool conditional = false;
40446 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40447 return list;
40449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40450 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40452 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40453 const char *p = IDENTIFIER_POINTER (id);
40455 if (strcmp ("conditional", p) == 0)
40457 conditional = true;
40458 cp_lexer_consume_token (parser->lexer);
40459 cp_lexer_consume_token (parser->lexer);
40463 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
40464 list, NULL);
40466 if (conditional)
40467 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40468 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
40469 return nlist;
40472 /* OpenMP 4.0:
40473 linear ( variable-list )
40474 linear ( variable-list : expression )
40476 OpenMP 4.5:
40477 linear ( modifier ( variable-list ) )
40478 linear ( modifier ( variable-list ) : expression )
40480 modifier:
40483 uval
40485 OpenMP 5.2:
40486 linear ( variable-list : modifiers-list )
40488 modifiers:
40491 uval
40492 step ( expression ) */
40494 static tree
40495 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
40496 bool declare_simd)
40498 tree nlist, c, step = integer_one_node;
40499 bool colon;
40500 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
40501 bool old_linear_modifier = false;
40503 matching_parens parens;
40504 if (!parens.require_open (parser))
40505 return list;
40507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40510 const char *p = IDENTIFIER_POINTER (id);
40512 if (strcmp ("ref", p) == 0)
40513 kind = OMP_CLAUSE_LINEAR_REF;
40514 else if (strcmp ("val", p) == 0)
40515 kind = OMP_CLAUSE_LINEAR_VAL;
40516 else if (strcmp ("uval", p) == 0)
40517 kind = OMP_CLAUSE_LINEAR_UVAL;
40518 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40520 cp_lexer_consume_token (parser->lexer);
40521 old_linear_modifier = true;
40523 else
40524 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40527 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40528 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40529 &colon);
40530 else
40532 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40533 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40534 if (colon)
40535 cp_parser_require (parser, CPP_COLON, RT_COLON);
40536 else if (!parens.require_close (parser))
40537 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40538 /*or_comma=*/false,
40539 /*consume_paren=*/true);
40542 if (colon)
40544 bool has_modifiers = false;
40545 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40546 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40549 const char *p = IDENTIFIER_POINTER (id);
40550 size_t pos = 0;
40551 if (strcmp ("ref", p) == 0
40552 || strcmp ("val", p) == 0
40553 || strcmp ("uval", p) == 0)
40554 pos = 2;
40555 else if (strcmp ("step", p) == 0
40556 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40558 pos = cp_parser_skip_balanced_tokens (parser, 2);
40559 if (pos == 2)
40560 pos = 0;
40562 if (pos != 0
40563 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
40564 || cp_lexer_nth_token_is (parser->lexer, pos,
40565 CPP_CLOSE_PAREN)))
40566 has_modifiers = true;
40569 step = NULL_TREE;
40570 if (has_modifiers)
40572 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40575 const char *p = IDENTIFIER_POINTER (id);
40576 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
40577 if (strcmp ("ref", p) == 0)
40578 nkind = OMP_CLAUSE_LINEAR_REF;
40579 else if (strcmp ("val", p) == 0)
40580 nkind = OMP_CLAUSE_LINEAR_VAL;
40581 else if (strcmp ("uval", p) == 0)
40582 nkind = OMP_CLAUSE_LINEAR_UVAL;
40583 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
40585 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
40586 error_at (cp_lexer_peek_token (parser->lexer)->location,
40587 "multiple linear modifiers");
40588 kind = nkind;
40589 cp_lexer_consume_token (parser->lexer);
40591 else if (strcmp ("step", p) == 0)
40593 location_t step_loc
40594 = cp_lexer_peek_token (parser->lexer)->location;
40595 cp_lexer_consume_token (parser->lexer);
40596 matching_parens parens2;
40597 if (parens2.require_open (parser))
40599 if (step)
40600 error_at (step_loc, "multiple %<step%> modifiers");
40601 if (declare_simd
40602 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40603 && cp_lexer_nth_token_is (parser->lexer, 2,
40604 CPP_CLOSE_PAREN))
40606 cp_token *token
40607 = cp_lexer_peek_token (parser->lexer);
40608 location_t tok_loc = token->location;
40609 cp_parser_parse_tentatively (parser);
40610 step = cp_parser_id_expression (parser, false, true,
40611 NULL, false, false);
40612 if (step != error_mark_node)
40613 step = cp_parser_lookup_name_simple (parser, step,
40614 tok_loc);
40615 if (step == error_mark_node)
40617 step = NULL_TREE;
40618 cp_parser_abort_tentative_parse (parser);
40620 else if (!cp_parser_parse_definitely (parser))
40621 step = NULL_TREE;
40623 if (!step)
40624 step = cp_parser_assignment_expression (parser);
40625 if (!parens2.require_close (parser))
40626 cp_parser_skip_to_closing_parenthesis (parser, true,
40627 false, true);
40629 else
40630 break;
40632 else
40633 break;
40634 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40636 cp_lexer_consume_token (parser->lexer);
40637 continue;
40639 break;
40641 if (!step)
40642 step = integer_one_node;
40644 else if (declare_simd
40645 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40646 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40648 cp_token *token = cp_lexer_peek_token (parser->lexer);
40649 cp_parser_parse_tentatively (parser);
40650 step = cp_parser_id_expression (parser, /*template_p=*/false,
40651 /*check_dependency_p=*/true,
40652 /*template_p=*/NULL,
40653 /*declarator_p=*/false,
40654 /*optional_p=*/false);
40655 if (step != error_mark_node)
40656 step = cp_parser_lookup_name_simple (parser, step, token->location);
40657 if (step == error_mark_node)
40659 step = NULL_TREE;
40660 cp_parser_abort_tentative_parse (parser);
40662 else if (!cp_parser_parse_definitely (parser))
40663 step = NULL_TREE;
40665 if (!step)
40666 step = cp_parser_assignment_expression (parser);
40668 if (!parens.require_close (parser))
40669 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40670 /*or_comma=*/false,
40671 /*consume_paren=*/true);
40673 if (step == error_mark_node)
40674 return list;
40677 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40679 OMP_CLAUSE_LINEAR_STEP (c) = step;
40680 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40681 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40684 return nlist;
40687 /* OpenMP 4.0:
40688 safelen ( constant-expression ) */
40690 static tree
40691 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40692 location_t location)
40694 tree t, c;
40696 matching_parens parens;
40697 if (!parens.require_open (parser))
40698 return list;
40700 t = cp_parser_constant_expression (parser);
40702 if (t == error_mark_node
40703 || !parens.require_close (parser))
40704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40705 /*or_comma=*/false,
40706 /*consume_paren=*/true);
40708 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40710 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40711 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40712 OMP_CLAUSE_CHAIN (c) = list;
40714 return c;
40717 /* OpenMP 4.0:
40718 simdlen ( constant-expression ) */
40720 static tree
40721 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40722 location_t location)
40724 tree t, c;
40726 matching_parens parens;
40727 if (!parens.require_open (parser))
40728 return list;
40730 t = cp_parser_constant_expression (parser);
40732 if (t == error_mark_node
40733 || !parens.require_close (parser))
40734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40735 /*or_comma=*/false,
40736 /*consume_paren=*/true);
40738 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40740 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40741 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40742 OMP_CLAUSE_CHAIN (c) = list;
40744 return c;
40747 /* OpenMP 4.5:
40748 vec:
40749 identifier [+/- integer]
40750 vec , identifier [+/- integer]
40753 static tree
40754 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40755 tree list, bool depend_p)
40757 tree vec = NULL;
40759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40761 cp_parser_error (parser, "expected identifier");
40762 return list;
40765 if (!depend_p)
40767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40768 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40769 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40770 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40771 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40773 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40774 if (integer_onep (val))
40776 cp_lexer_consume_token (parser->lexer);
40777 cp_lexer_consume_token (parser->lexer);
40778 cp_lexer_consume_token (parser->lexer);
40779 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40780 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40781 OMP_CLAUSE_CHAIN (u) = list;
40782 return u;
40787 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40789 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40790 tree t, identifier = cp_parser_identifier (parser);
40791 tree addend = NULL;
40793 if (identifier == error_mark_node)
40794 t = error_mark_node;
40795 else
40797 t = cp_parser_lookup_name_simple
40798 (parser, identifier,
40799 cp_lexer_peek_token (parser->lexer)->location);
40800 if (t == error_mark_node)
40801 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40802 id_loc);
40805 bool neg = false;
40806 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40807 neg = true;
40808 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40810 addend = integer_zero_node;
40811 goto add_to_vector;
40813 cp_lexer_consume_token (parser->lexer);
40815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40817 cp_parser_error (parser, "expected integer");
40818 return list;
40821 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40822 if (TREE_CODE (addend) != INTEGER_CST)
40824 cp_parser_error (parser, "expected integer");
40825 return list;
40827 cp_lexer_consume_token (parser->lexer);
40829 add_to_vector:
40830 if (t != error_mark_node)
40832 vec = tree_cons (addend, t, vec);
40833 if (neg)
40834 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40838 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40839 break;
40841 cp_lexer_consume_token (parser->lexer);
40844 if (vec)
40846 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40847 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40848 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40849 OMP_CLAUSE_DECL (u) = nreverse (vec);
40850 OMP_CLAUSE_CHAIN (u) = list;
40851 return u;
40853 return list;
40856 /* OpenMP 5.0:
40857 detach ( event-handle ) */
40859 static tree
40860 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40862 matching_parens parens;
40864 if (!parens.require_open (parser))
40865 return list;
40867 cp_token *token;
40868 tree name, decl;
40870 token = cp_lexer_peek_token (parser->lexer);
40871 name = cp_parser_id_expression (parser, /*template_p=*/false,
40872 /*check_dependency_p=*/true,
40873 /*template_p=*/NULL,
40874 /*declarator_p=*/false,
40875 /*optional_p=*/false);
40876 if (name == error_mark_node)
40877 decl = error_mark_node;
40878 else
40880 if (identifier_p (name))
40881 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40882 else
40883 decl = name;
40884 if (decl == error_mark_node)
40885 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40886 token->location);
40889 if (decl == error_mark_node
40890 || !parens.require_close (parser))
40891 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40892 /*or_comma=*/false,
40893 /*consume_paren=*/true);
40895 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40896 OMP_CLAUSE_DECL (u) = decl;
40897 OMP_CLAUSE_CHAIN (u) = list;
40899 return u;
40902 /* OpenMP 5.0:
40903 iterators ( iterators-definition )
40905 iterators-definition:
40906 iterator-specifier
40907 iterator-specifier , iterators-definition
40909 iterator-specifier:
40910 identifier = range-specification
40911 iterator-type identifier = range-specification
40913 range-specification:
40914 begin : end
40915 begin : end : step */
40917 static tree
40918 cp_parser_omp_iterators (cp_parser *parser)
40920 tree ret = NULL_TREE, *last = &ret;
40921 cp_lexer_consume_token (parser->lexer);
40923 matching_parens parens;
40924 if (!parens.require_open (parser))
40925 return error_mark_node;
40927 bool saved_colon_corrects_to_scope_p
40928 = parser->colon_corrects_to_scope_p;
40929 bool saved_colon_doesnt_start_class_def_p
40930 = parser->colon_doesnt_start_class_def_p;
40934 tree iter_type;
40935 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40936 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40937 iter_type = integer_type_node;
40938 else
40940 const char *saved_message
40941 = parser->type_definition_forbidden_message;
40942 parser->type_definition_forbidden_message
40943 = G_("types may not be defined in iterator type");
40945 iter_type = cp_parser_type_id (parser);
40947 parser->type_definition_forbidden_message = saved_message;
40950 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40953 cp_parser_error (parser, "expected identifier");
40954 break;
40957 tree id = cp_parser_identifier (parser);
40958 if (id == error_mark_node)
40959 break;
40961 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40962 break;
40964 parser->colon_corrects_to_scope_p = false;
40965 parser->colon_doesnt_start_class_def_p = true;
40966 tree begin = cp_parser_assignment_expression (parser);
40968 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40969 break;
40971 tree end = cp_parser_assignment_expression (parser);
40973 tree step = integer_one_node;
40974 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40976 cp_lexer_consume_token (parser->lexer);
40977 step = cp_parser_assignment_expression (parser);
40980 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40981 DECL_ARTIFICIAL (iter_var) = 1;
40982 DECL_CONTEXT (iter_var) = current_function_decl;
40983 pushdecl (iter_var);
40985 *last = make_tree_vec (6);
40986 TREE_VEC_ELT (*last, 0) = iter_var;
40987 TREE_VEC_ELT (*last, 1) = begin;
40988 TREE_VEC_ELT (*last, 2) = end;
40989 TREE_VEC_ELT (*last, 3) = step;
40990 last = &TREE_CHAIN (*last);
40992 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40994 cp_lexer_consume_token (parser->lexer);
40995 continue;
40997 break;
40999 while (1);
41001 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41002 parser->colon_doesnt_start_class_def_p
41003 = saved_colon_doesnt_start_class_def_p;
41005 if (!parens.require_close (parser))
41006 cp_parser_skip_to_closing_parenthesis (parser,
41007 /*recovering=*/true,
41008 /*or_comma=*/false,
41009 /*consume_paren=*/true);
41011 return ret ? ret : error_mark_node;
41014 /* OpenMP 5.0:
41015 affinity ( [aff-modifier :] variable-list )
41016 aff-modifier:
41017 iterator ( iterators-definition ) */
41019 static tree
41020 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
41022 tree nlist, c, iterators = NULL_TREE;
41024 matching_parens parens;
41025 if (!parens.require_open (parser))
41026 return list;
41028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41030 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41031 const char *p = IDENTIFIER_POINTER (id);
41032 bool parse_iter = ((strcmp ("iterator", p) == 0)
41033 && (cp_lexer_nth_token_is (parser->lexer, 2,
41034 CPP_OPEN_PAREN)));
41035 if (parse_iter)
41037 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
41038 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
41040 if (parse_iter)
41042 begin_scope (sk_omp, NULL);
41043 iterators = cp_parser_omp_iterators (parser);
41044 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41046 if (iterators)
41047 poplevel (0, 1, 0);
41048 cp_parser_skip_to_closing_parenthesis (parser,
41049 /*recovering=*/true,
41050 /*or_comma=*/false,
41051 /*consume_paren=*/true);
41052 return list;
41056 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
41057 list, NULL);
41058 if (iterators)
41060 tree block = poplevel (1, 1, 0);
41061 if (iterators != error_mark_node)
41063 TREE_VEC_ELT (iterators, 5) = block;
41064 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41065 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
41066 OMP_CLAUSE_DECL (c));
41069 return nlist;
41072 /* OpenMP 4.0:
41073 depend ( depend-kind : variable-list )
41075 depend-kind:
41076 in | out | inout
41078 OpenMP 4.5:
41079 depend ( source )
41081 depend ( sink : vec )
41083 OpenMP 5.0:
41084 depend ( depend-modifier , depend-kind: variable-list )
41086 depend-kind:
41087 in | out | inout | mutexinoutset | depobj
41089 depend-modifier:
41090 iterator ( iterators-definition ) */
41092 static tree
41093 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
41095 tree nlist, c, iterators = NULL_TREE;
41096 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
41097 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
41099 matching_parens parens;
41100 if (!parens.require_open (parser))
41101 return list;
41105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41106 goto invalid_kind;
41108 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41109 const char *p = IDENTIFIER_POINTER (id);
41111 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
41113 begin_scope (sk_omp, NULL);
41114 iterators = cp_parser_omp_iterators (parser);
41115 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
41116 continue;
41118 if (strcmp ("in", p) == 0)
41119 kind = OMP_CLAUSE_DEPEND_IN;
41120 else if (strcmp ("inout", p) == 0)
41121 kind = OMP_CLAUSE_DEPEND_INOUT;
41122 else if (strcmp ("inoutset", p) == 0)
41123 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41124 else if (strcmp ("mutexinoutset", p) == 0)
41125 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41126 else if (strcmp ("out", p) == 0)
41127 kind = OMP_CLAUSE_DEPEND_OUT;
41128 else if (strcmp ("depobj", p) == 0)
41129 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
41130 else if (strcmp ("sink", p) == 0)
41131 dkind = OMP_CLAUSE_DOACROSS_SINK;
41132 else if (strcmp ("source", p) == 0)
41133 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
41134 else
41135 goto invalid_kind;
41136 break;
41138 while (1);
41140 cp_lexer_consume_token (parser->lexer);
41142 if (iterators
41143 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
41144 || dkind == OMP_CLAUSE_DOACROSS_SINK))
41146 poplevel (0, 1, 0);
41147 error_at (loc, "%<iterator%> modifier incompatible with %qs",
41148 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
41149 iterators = NULL_TREE;
41152 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
41154 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41155 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
41156 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
41157 OMP_CLAUSE_DECL (c) = NULL_TREE;
41158 OMP_CLAUSE_CHAIN (c) = list;
41159 if (!parens.require_close (parser))
41160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41161 /*or_comma=*/false,
41162 /*consume_paren=*/true);
41163 return c;
41166 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41167 goto resync_fail;
41169 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
41171 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
41172 if (!parens.require_close (parser))
41173 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41174 /*or_comma=*/false,
41175 /*consume_paren=*/true);
41177 else
41179 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
41180 list, NULL);
41182 if (iterators)
41184 tree block = poplevel (1, 1, 0);
41185 if (iterators == error_mark_node)
41186 iterators = NULL_TREE;
41187 else
41188 TREE_VEC_ELT (iterators, 5) = block;
41191 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41193 OMP_CLAUSE_DEPEND_KIND (c) = kind;
41194 if (iterators)
41195 OMP_CLAUSE_DECL (c)
41196 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
41199 return nlist;
41201 invalid_kind:
41202 cp_parser_error (parser, "invalid depend kind");
41203 resync_fail:
41204 if (iterators)
41205 poplevel (0, 1, 0);
41206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41207 /*or_comma=*/false,
41208 /*consume_paren=*/true);
41209 return list;
41212 /* OpenMP 5.2:
41213 doacross ( source : )
41214 doacross ( source : omp_cur_iteration )
41216 doacross ( sink : vec )
41217 doacross ( sink : omp_cur_iteration - logical_iteration ) */
41219 static tree
41220 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
41222 tree nlist;
41223 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
41225 matching_parens parens;
41226 if (!parens.require_open (parser))
41227 return list;
41229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41231 invalid_kind:
41232 cp_parser_error (parser, "invalid doacross kind");
41233 resync_fail:
41234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41235 /*or_comma=*/false,
41236 /*consume_paren=*/true);
41237 return list;
41240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41241 const char *p = IDENTIFIER_POINTER (id);
41243 if (strcmp ("sink", p) == 0)
41244 kind = OMP_CLAUSE_DOACROSS_SINK;
41245 else if (strcmp ("source", p) == 0)
41246 kind = OMP_CLAUSE_DOACROSS_SOURCE;
41247 else
41248 goto invalid_kind;
41250 cp_lexer_consume_token (parser->lexer);
41252 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41253 goto resync_fail;
41255 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
41257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41259 id = cp_lexer_peek_token (parser->lexer)->u.value;
41260 p = IDENTIFIER_POINTER (id);
41261 if (strcmp (p, "omp_cur_iteration") == 0)
41262 cp_lexer_consume_token (parser->lexer);
41264 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41265 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
41266 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
41267 OMP_CLAUSE_CHAIN (nlist) = list;
41269 else
41270 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
41272 if (!parens.require_close (parser))
41273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41274 /*or_comma=*/false,
41275 /*consume_paren=*/true);
41276 return nlist;
41279 /* OpenMP 4.0:
41280 from ( variable-list )
41281 to ( variable-list )
41283 OpenMP 5.1:
41284 from ( [present :] variable-list )
41285 to ( [present :] variable-list ) */
41287 static tree
41288 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
41289 tree list)
41291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41292 return list;
41294 bool present = false;
41295 cp_token *token = cp_lexer_peek_token (parser->lexer);
41297 if (token->type == CPP_NAME
41298 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
41299 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41301 present = true;
41302 cp_lexer_consume_token (parser->lexer);
41303 cp_lexer_consume_token (parser->lexer);
41306 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
41307 if (present)
41308 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
41309 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
41311 return nl;
41314 /* OpenMP 4.0:
41315 map ( map-kind : variable-list )
41316 map ( variable-list )
41318 map-kind:
41319 alloc | to | from | tofrom
41321 OpenMP 4.5:
41322 map-kind:
41323 alloc | to | from | tofrom | release | delete
41325 map ( always [,] map-kind: variable-list )
41327 OpenMP 5.0:
41328 map ( [map-type-modifier[,] ...] map-kind: variable-list )
41330 map-type-modifier:
41331 always | close */
41333 static tree
41334 cp_parser_omp_clause_map (cp_parser *parser, tree list)
41336 tree nlist, c;
41337 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
41339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41340 return list;
41342 int pos = 1;
41343 int map_kind_pos = 0;
41344 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
41345 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
41347 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
41349 map_kind_pos = pos;
41350 break;
41353 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
41354 pos++;
41355 pos++;
41358 bool always_modifier = false;
41359 bool close_modifier = false;
41360 bool present_modifier = false;
41361 for (int pos = 1; pos < map_kind_pos; ++pos)
41363 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41364 if (tok->type == CPP_COMMA)
41366 cp_lexer_consume_token (parser->lexer);
41367 continue;
41370 const char *p = IDENTIFIER_POINTER (tok->u.value);
41371 if (strcmp ("always", p) == 0)
41373 if (always_modifier)
41375 cp_parser_error (parser, "too many %<always%> modifiers");
41376 cp_parser_skip_to_closing_parenthesis (parser,
41377 /*recovering=*/true,
41378 /*or_comma=*/false,
41379 /*consume_paren=*/true);
41380 return list;
41382 always_modifier = true;
41384 else if (strcmp ("close", p) == 0)
41386 if (close_modifier)
41388 cp_parser_error (parser, "too many %<close%> modifiers");
41389 cp_parser_skip_to_closing_parenthesis (parser,
41390 /*recovering=*/true,
41391 /*or_comma=*/false,
41392 /*consume_paren=*/true);
41393 return list;
41395 close_modifier = true;
41397 else if (strcmp ("present", p) == 0)
41399 if (present_modifier)
41401 cp_parser_error (parser, "too many %<present%> modifiers");
41402 cp_parser_skip_to_closing_parenthesis (parser,
41403 /*recovering=*/true,
41404 /*or_comma=*/false,
41405 /*consume_paren=*/true);
41406 return list;
41408 present_modifier = true;
41410 else
41412 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
41413 " than %<always%>, %<close%> or %<present%>");
41414 cp_parser_skip_to_closing_parenthesis (parser,
41415 /*recovering=*/true,
41416 /*or_comma=*/false,
41417 /*consume_paren=*/true);
41418 return list;
41421 cp_lexer_consume_token (parser->lexer);
41424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41425 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41428 const char *p = IDENTIFIER_POINTER (id);
41429 int always_present_modifier = always_modifier && present_modifier;
41431 if (strcmp ("alloc", p) == 0)
41432 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
41433 else if (strcmp ("to", p) == 0)
41434 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
41435 : present_modifier ? GOMP_MAP_PRESENT_TO
41436 : always_modifier ? GOMP_MAP_ALWAYS_TO
41437 : GOMP_MAP_TO);
41438 else if (strcmp ("from", p) == 0)
41439 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
41440 : present_modifier ? GOMP_MAP_PRESENT_FROM
41441 : always_modifier ? GOMP_MAP_ALWAYS_FROM
41442 : GOMP_MAP_FROM);
41443 else if (strcmp ("tofrom", p) == 0)
41444 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
41445 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
41446 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
41447 : GOMP_MAP_TOFROM);
41448 else if (strcmp ("release", p) == 0)
41449 kind = GOMP_MAP_RELEASE;
41450 else
41452 cp_parser_error (parser, "invalid map kind");
41453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41454 /*or_comma=*/false,
41455 /*consume_paren=*/true);
41456 return list;
41458 cp_lexer_consume_token (parser->lexer);
41459 cp_lexer_consume_token (parser->lexer);
41461 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
41462 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41464 kind = GOMP_MAP_DELETE;
41465 cp_lexer_consume_token (parser->lexer);
41466 cp_lexer_consume_token (parser->lexer);
41469 /* We introduce a scope here so that errors parsing e.g. "always", "close"
41470 tokens do not propagate to later directives that might use them
41471 legally. */
41472 begin_scope (sk_omp, NULL);
41473 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
41474 NULL, true);
41475 finish_scope ();
41477 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41478 OMP_CLAUSE_SET_MAP_KIND (c, kind);
41480 return nlist;
41483 /* OpenMP 4.0:
41484 device ( expression )
41486 OpenMP 5.0:
41487 device ( [device-modifier :] integer-expression )
41489 device-modifier:
41490 ancestor | device_num */
41492 static tree
41493 cp_parser_omp_clause_device (cp_parser *parser, tree list,
41494 location_t location)
41496 tree t, c;
41497 bool ancestor = false;
41499 matching_parens parens;
41500 if (!parens.require_open (parser))
41501 return list;
41503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41504 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41506 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41507 const char *p = IDENTIFIER_POINTER (tok->u.value);
41508 if (strcmp ("ancestor", p) == 0)
41510 ancestor = true;
41512 /* A requires directive with the reverse_offload clause must be
41513 specified. */
41514 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
41516 error_at (tok->location, "%<ancestor%> device modifier not "
41517 "preceded by %<requires%> directive "
41518 "with %<reverse_offload%> clause");
41519 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41520 return list;
41523 else if (strcmp ("device_num", p) == 0)
41525 else
41527 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41528 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41529 return list;
41531 cp_lexer_consume_token (parser->lexer);
41532 cp_lexer_consume_token (parser->lexer);
41535 t = cp_parser_assignment_expression (parser);
41537 if (t == error_mark_node
41538 || !parens.require_close (parser))
41539 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41540 /*or_comma=*/false,
41541 /*consume_paren=*/true);
41543 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41544 "device", location);
41546 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41547 OMP_CLAUSE_DEVICE_ID (c) = t;
41548 OMP_CLAUSE_CHAIN (c) = list;
41549 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
41551 return c;
41554 /* OpenMP 4.0:
41555 dist_schedule ( static )
41556 dist_schedule ( static , expression ) */
41558 static tree
41559 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
41560 location_t location)
41562 tree c, t;
41564 matching_parens parens;
41565 if (!parens.require_open (parser))
41566 return list;
41568 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
41570 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
41571 goto invalid_kind;
41572 cp_lexer_consume_token (parser->lexer);
41574 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41576 cp_lexer_consume_token (parser->lexer);
41578 t = cp_parser_assignment_expression (parser);
41580 if (t == error_mark_node)
41581 goto resync_fail;
41582 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
41584 if (!parens.require_close (parser))
41585 goto resync_fail;
41587 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41588 goto resync_fail;
41590 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
41591 "dist_schedule", location); */
41592 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
41593 warning_at (location, OPT_Wopenmp, "too many %qs clauses", "dist_schedule");
41594 OMP_CLAUSE_CHAIN (c) = list;
41595 return c;
41597 invalid_kind:
41598 cp_parser_error (parser, "invalid dist_schedule kind");
41599 resync_fail:
41600 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41601 /*or_comma=*/false,
41602 /*consume_paren=*/true);
41603 return list;
41606 /* OpenMP 4.0:
41607 proc_bind ( proc-bind-kind )
41609 proc-bind-kind:
41610 primary | master | close | spread
41611 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41613 static tree
41614 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41615 location_t location)
41617 tree c;
41618 enum omp_clause_proc_bind_kind kind;
41620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41621 return list;
41623 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41625 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41626 const char *p = IDENTIFIER_POINTER (id);
41628 if (strcmp ("primary", p) == 0)
41629 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41630 else if (strcmp ("master", p) == 0)
41631 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41632 else if (strcmp ("close", p) == 0)
41633 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41634 else if (strcmp ("spread", p) == 0)
41635 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41636 else
41637 goto invalid_kind;
41639 else
41640 goto invalid_kind;
41642 cp_lexer_consume_token (parser->lexer);
41643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41644 goto resync_fail;
41646 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41647 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41648 location);
41649 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41650 OMP_CLAUSE_CHAIN (c) = list;
41651 return c;
41653 invalid_kind:
41654 cp_parser_error (parser, "invalid depend kind");
41655 resync_fail:
41656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41657 /*or_comma=*/false,
41658 /*consume_paren=*/true);
41659 return list;
41662 /* OpenMP 5.0:
41663 device_type ( host | nohost | any ) */
41665 static tree
41666 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41667 location_t location)
41669 tree c;
41670 enum omp_clause_device_type_kind kind;
41672 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41673 return list;
41675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41678 const char *p = IDENTIFIER_POINTER (id);
41680 if (strcmp ("host", p) == 0)
41681 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41682 else if (strcmp ("nohost", p) == 0)
41683 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41684 else if (strcmp ("any", p) == 0)
41685 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41686 else
41687 goto invalid_kind;
41689 else
41690 goto invalid_kind;
41692 cp_lexer_consume_token (parser->lexer);
41693 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41694 goto resync_fail;
41696 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41697 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41698 location);
41699 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41700 OMP_CLAUSE_CHAIN (c) = list;
41701 return c;
41703 invalid_kind:
41704 cp_parser_error (parser, "invalid depend kind");
41705 resync_fail:
41706 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41707 /*or_comma=*/false,
41708 /*consume_paren=*/true);
41709 return list;
41712 /* OpenACC:
41713 async [( int-expr )] */
41715 static tree
41716 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41718 tree c, t;
41719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41721 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41723 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41725 matching_parens parens;
41726 parens.consume_open (parser);
41728 t = cp_parser_assignment_expression (parser);
41729 if (t == error_mark_node
41730 || !parens.require_close (parser))
41731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41732 /*or_comma=*/false,
41733 /*consume_paren=*/true);
41736 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41738 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41739 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41740 OMP_CLAUSE_CHAIN (c) = list;
41741 list = c;
41743 return list;
41746 /* OpenACC 2.7:
41747 self [( expression )] */
41749 static tree
41750 cp_parser_oacc_compute_clause_self (cp_parser *parser, tree list)
41752 tree t;
41753 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41754 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41756 matching_parens parens;
41757 parens.consume_open (parser);
41758 t = cp_parser_assignment_expression (parser);
41759 if (t == error_mark_node
41760 || !parens.require_close (parser))
41762 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41763 /*or_comma=*/false,
41764 /*consume_paren=*/true);
41765 return list;
41768 else
41769 t = truthvalue_true_node;
41771 for (tree c = list; c; c = OMP_CLAUSE_CHAIN (c))
41772 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SELF)
41774 error_at (location, "too many %<self%> clauses");
41775 return list;
41778 tree c = build_omp_clause (location, OMP_CLAUSE_SELF);
41779 OMP_CLAUSE_SELF_EXPR (c) = t;
41780 OMP_CLAUSE_CHAIN (c) = list;
41781 return c;
41784 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41785 is a bitmask in MASK. Return the list of clauses found. */
41787 static tree
41788 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41789 const char *where, cp_token *pragma_tok,
41790 bool finish_p = true, bool target_p = false)
41792 tree clauses = NULL;
41793 bool first = true;
41795 /* Don't create location wrapper nodes within OpenACC clauses. */
41796 auto_suppress_location_wrappers sentinel;
41798 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41800 location_t here;
41801 pragma_omp_clause c_kind;
41802 omp_clause_code code;
41803 const char *c_name;
41804 tree prev = clauses;
41806 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41807 cp_lexer_consume_token (parser->lexer);
41809 here = cp_lexer_peek_token (parser->lexer)->location;
41810 c_kind = cp_parser_omp_clause_name (parser);
41812 switch (c_kind)
41814 case PRAGMA_OACC_CLAUSE_ASYNC:
41815 clauses = cp_parser_oacc_clause_async (parser, clauses);
41816 c_name = "async";
41817 break;
41818 case PRAGMA_OACC_CLAUSE_AUTO:
41819 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
41820 clauses);
41821 c_name = "auto";
41822 break;
41823 case PRAGMA_OACC_CLAUSE_ATTACH:
41824 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41825 c_name = "attach";
41826 break;
41827 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41828 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41829 c_name = "collapse";
41830 break;
41831 case PRAGMA_OACC_CLAUSE_COPY:
41832 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41833 c_name = "copy";
41834 break;
41835 case PRAGMA_OACC_CLAUSE_COPYIN:
41836 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41837 c_name = "copyin";
41838 break;
41839 case PRAGMA_OACC_CLAUSE_COPYOUT:
41840 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41841 c_name = "copyout";
41842 break;
41843 case PRAGMA_OACC_CLAUSE_CREATE:
41844 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41845 c_name = "create";
41846 break;
41847 case PRAGMA_OACC_CLAUSE_DELETE:
41848 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41849 c_name = "delete";
41850 break;
41851 case PRAGMA_OMP_CLAUSE_DEFAULT:
41852 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41853 c_name = "default";
41854 break;
41855 case PRAGMA_OACC_CLAUSE_DETACH:
41856 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41857 c_name = "detach";
41858 break;
41859 case PRAGMA_OACC_CLAUSE_DEVICE:
41860 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41861 c_name = "device";
41862 break;
41863 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41864 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41865 c_name = "deviceptr";
41866 break;
41867 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41868 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41869 c_name = "device_resident";
41870 break;
41871 case PRAGMA_OACC_CLAUSE_FINALIZE:
41872 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41873 clauses);
41874 c_name = "finalize";
41875 break;
41876 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41878 clauses);
41879 c_name = "firstprivate";
41880 break;
41881 case PRAGMA_OACC_CLAUSE_GANG:
41882 c_name = "gang";
41883 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41884 c_name, clauses);
41885 break;
41886 case PRAGMA_OACC_CLAUSE_HOST:
41887 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41888 c_name = "host";
41889 break;
41890 case PRAGMA_OACC_CLAUSE_IF:
41891 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41892 c_name = "if";
41893 break;
41894 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41895 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41896 clauses);
41897 c_name = "if_present";
41898 break;
41899 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41900 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41901 clauses);
41902 c_name = "independent";
41903 break;
41904 case PRAGMA_OACC_CLAUSE_LINK:
41905 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41906 c_name = "link";
41907 break;
41908 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41909 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41910 c_name = "no_create";
41911 break;
41912 case PRAGMA_OACC_CLAUSE_NOHOST:
41913 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41914 clauses);
41915 c_name = "nohost";
41916 break;
41917 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41918 code = OMP_CLAUSE_NUM_GANGS;
41919 c_name = "num_gangs";
41920 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41921 clauses);
41922 break;
41923 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41924 c_name = "num_workers";
41925 code = OMP_CLAUSE_NUM_WORKERS;
41926 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41927 clauses);
41928 break;
41929 case PRAGMA_OACC_CLAUSE_PRESENT:
41930 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41931 c_name = "present";
41932 break;
41933 case PRAGMA_OACC_CLAUSE_PRIVATE:
41934 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41935 clauses);
41936 c_name = "private";
41937 break;
41938 case PRAGMA_OACC_CLAUSE_REDUCTION:
41939 clauses
41940 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41941 false, clauses);
41942 c_name = "reduction";
41943 break;
41944 case PRAGMA_OACC_CLAUSE_SELF:
41945 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)) == 0)
41946 /* OpenACC compute construct */
41947 clauses = cp_parser_oacc_compute_clause_self (parser, clauses);
41948 else
41949 /* OpenACC 'update' directive */
41950 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41951 c_name = "self";
41952 break;
41953 case PRAGMA_OACC_CLAUSE_SEQ:
41954 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41955 clauses);
41956 c_name = "seq";
41957 break;
41958 case PRAGMA_OACC_CLAUSE_TILE:
41959 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41960 c_name = "tile";
41961 break;
41962 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41963 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41964 clauses);
41965 c_name = "use_device";
41966 break;
41967 case PRAGMA_OACC_CLAUSE_VECTOR:
41968 c_name = "vector";
41969 clauses = cp_parser_oacc_shape_clause (parser, here,
41970 OMP_CLAUSE_VECTOR,
41971 c_name, clauses);
41972 break;
41973 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41974 c_name = "vector_length";
41975 code = OMP_CLAUSE_VECTOR_LENGTH;
41976 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41977 clauses);
41978 break;
41979 case PRAGMA_OACC_CLAUSE_WAIT:
41980 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41981 c_name = "wait";
41982 break;
41983 case PRAGMA_OACC_CLAUSE_WORKER:
41984 c_name = "worker";
41985 clauses = cp_parser_oacc_shape_clause (parser, here,
41986 OMP_CLAUSE_WORKER,
41987 c_name, clauses);
41988 break;
41989 default:
41990 cp_parser_error (parser, "expected an OpenACC clause");
41991 goto saw_error;
41994 first = false;
41996 if (((mask >> c_kind) & 1) == 0)
41998 /* Remove the invalid clause(s) from the list to avoid
41999 confusing the rest of the compiler. */
42000 clauses = prev;
42001 error_at (here, "%qs is not valid for %qs", c_name, where);
42005 saw_error:
42006 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42008 if (finish_p)
42009 return finish_omp_clauses (clauses, target_p ? C_ORT_ACC_TARGET
42010 : C_ORT_ACC);
42012 return clauses;
42015 /* Parse all OpenMP clauses. The set clauses allowed by the directive
42016 is a bitmask in MASK. Return the list of clauses found.
42017 FINISH_P set if finish_omp_clauses should be called.
42018 NESTED non-zero if clauses should be terminated by closing paren instead
42019 of end of pragma. If it is 2, additionally commas are required in between
42020 the clauses. */
42022 static tree
42023 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
42024 const char *where, cp_token *pragma_tok,
42025 bool finish_p = true, int nested = 0)
42027 tree clauses = NULL;
42028 bool first = true;
42029 cp_token *token = NULL;
42031 /* Don't create location wrapper nodes within OpenMP clauses. */
42032 auto_suppress_location_wrappers sentinel;
42034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42036 pragma_omp_clause c_kind;
42037 const char *c_name;
42038 tree prev = clauses;
42040 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
42041 break;
42043 if (!first || nested != 2)
42045 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42046 cp_lexer_consume_token (parser->lexer);
42047 else if (nested == 2)
42048 error_at (cp_lexer_peek_token (parser->lexer)->location,
42049 "clauses in %<simd%> trait should be separated "
42050 "by %<,%>");
42053 token = cp_lexer_peek_token (parser->lexer);
42054 c_kind = cp_parser_omp_clause_name (parser);
42056 switch (c_kind)
42058 case PRAGMA_OMP_CLAUSE_BIND:
42059 clauses = cp_parser_omp_clause_bind (parser, clauses,
42060 token->location);
42061 c_name = "bind";
42062 break;
42063 case PRAGMA_OMP_CLAUSE_COLLAPSE:
42064 clauses = cp_parser_omp_clause_collapse (parser, clauses,
42065 token->location);
42066 c_name = "collapse";
42067 break;
42068 case PRAGMA_OMP_CLAUSE_COPYIN:
42069 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
42070 c_name = "copyin";
42071 break;
42072 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
42073 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
42074 clauses);
42075 c_name = "copyprivate";
42076 break;
42077 case PRAGMA_OMP_CLAUSE_DEFAULT:
42078 clauses = cp_parser_omp_clause_default (parser, clauses,
42079 token->location, false);
42080 c_name = "default";
42081 break;
42082 case PRAGMA_OMP_CLAUSE_FILTER:
42083 clauses = cp_parser_omp_clause_filter (parser, clauses,
42084 token->location);
42085 c_name = "filter";
42086 break;
42087 case PRAGMA_OMP_CLAUSE_FINAL:
42088 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
42089 c_name = "final";
42090 break;
42091 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
42092 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42093 clauses);
42094 c_name = "firstprivate";
42095 break;
42096 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
42097 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
42098 token->location);
42099 c_name = "grainsize";
42100 break;
42101 case PRAGMA_OMP_CLAUSE_HINT:
42102 clauses = cp_parser_omp_clause_hint (parser, clauses,
42103 token->location);
42104 c_name = "hint";
42105 break;
42106 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
42107 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
42108 token->location);
42109 c_name = "defaultmap";
42110 break;
42111 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
42112 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42113 clauses);
42114 c_name = "use_device_ptr";
42115 break;
42116 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
42117 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
42118 clauses);
42119 c_name = "use_device_addr";
42120 break;
42121 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
42122 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
42123 clauses);
42124 c_name = "is_device_ptr";
42125 break;
42126 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
42127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
42128 clauses);
42129 c_name = "has_device_addr";
42130 break;
42131 case PRAGMA_OMP_CLAUSE_IF:
42132 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
42133 true);
42134 c_name = "if";
42135 break;
42136 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
42137 clauses
42138 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
42139 true, clauses);
42140 c_name = "in_reduction";
42141 break;
42142 case PRAGMA_OMP_CLAUSE_INDIRECT:
42143 clauses = cp_parser_omp_clause_indirect (parser, clauses,
42144 token->location);
42145 c_name = "indirect";
42146 break;
42147 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
42148 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
42149 c_name = "lastprivate";
42150 break;
42151 case PRAGMA_OMP_CLAUSE_MERGEABLE:
42152 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
42153 token->location);
42154 c_name = "mergeable";
42155 break;
42156 case PRAGMA_OMP_CLAUSE_NOWAIT:
42157 clauses = cp_parser_omp_clause_nowait (parser, clauses,
42158 token->location);
42159 c_name = "nowait";
42160 break;
42161 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
42162 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
42163 token->location);
42164 c_name = "num_tasks";
42165 break;
42166 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
42167 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
42168 token->location);
42169 c_name = "num_threads";
42170 break;
42171 case PRAGMA_OMP_CLAUSE_ORDER:
42172 clauses = cp_parser_omp_clause_order (parser, clauses,
42173 token->location);
42174 c_name = "order";
42175 break;
42176 case PRAGMA_OMP_CLAUSE_ORDERED:
42177 clauses = cp_parser_omp_clause_ordered (parser, clauses,
42178 token->location);
42179 c_name = "ordered";
42180 break;
42181 case PRAGMA_OMP_CLAUSE_PRIORITY:
42182 clauses = cp_parser_omp_clause_priority (parser, clauses,
42183 token->location);
42184 c_name = "priority";
42185 break;
42186 case PRAGMA_OMP_CLAUSE_PRIVATE:
42187 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42188 clauses);
42189 c_name = "private";
42190 break;
42191 case PRAGMA_OMP_CLAUSE_REDUCTION:
42192 clauses
42193 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42194 true, clauses);
42195 c_name = "reduction";
42196 break;
42197 case PRAGMA_OMP_CLAUSE_SCHEDULE:
42198 clauses = cp_parser_omp_clause_schedule (parser, clauses,
42199 token->location);
42200 c_name = "schedule";
42201 break;
42202 case PRAGMA_OMP_CLAUSE_SHARED:
42203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
42204 clauses);
42205 c_name = "shared";
42206 break;
42207 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
42208 clauses
42209 = cp_parser_omp_clause_reduction (parser,
42210 OMP_CLAUSE_TASK_REDUCTION,
42211 true, clauses);
42212 c_name = "task_reduction";
42213 break;
42214 case PRAGMA_OMP_CLAUSE_UNTIED:
42215 clauses = cp_parser_omp_clause_untied (parser, clauses,
42216 token->location);
42217 c_name = "untied";
42218 break;
42219 case PRAGMA_OMP_CLAUSE_INBRANCH:
42220 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
42221 clauses, token->location);
42222 c_name = "inbranch";
42223 break;
42224 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
42225 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
42226 clauses);
42227 c_name = "nontemporal";
42228 break;
42229 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
42230 clauses = cp_parser_omp_clause_branch (parser,
42231 OMP_CLAUSE_NOTINBRANCH,
42232 clauses, token->location);
42233 c_name = "notinbranch";
42234 break;
42235 case PRAGMA_OMP_CLAUSE_PARALLEL:
42236 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
42237 clauses, token->location);
42238 c_name = "parallel";
42239 if (!first)
42241 clause_not_first:
42242 error_at (token->location, "%qs must be the first clause of %qs",
42243 c_name, where);
42244 clauses = prev;
42246 break;
42247 case PRAGMA_OMP_CLAUSE_FOR:
42248 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
42249 clauses, token->location);
42250 c_name = "for";
42251 if (!first)
42252 goto clause_not_first;
42253 break;
42254 case PRAGMA_OMP_CLAUSE_SECTIONS:
42255 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
42256 clauses, token->location);
42257 c_name = "sections";
42258 if (!first)
42259 goto clause_not_first;
42260 break;
42261 case PRAGMA_OMP_CLAUSE_TASKGROUP:
42262 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
42263 clauses, token->location);
42264 c_name = "taskgroup";
42265 if (!first)
42266 goto clause_not_first;
42267 break;
42268 case PRAGMA_OMP_CLAUSE_LINK:
42269 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
42270 c_name = "link";
42271 break;
42272 case PRAGMA_OMP_CLAUSE_TO:
42273 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
42275 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42276 clauses);
42277 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
42278 OMP_CLAUSE_ENTER_TO (c) = 1;
42279 clauses = nl;
42281 else
42282 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
42283 clauses);
42284 c_name = "to";
42285 break;
42286 case PRAGMA_OMP_CLAUSE_FROM:
42287 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
42288 clauses);
42289 c_name = "from";
42290 break;
42291 case PRAGMA_OMP_CLAUSE_UNIFORM:
42292 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
42293 clauses);
42294 c_name = "uniform";
42295 break;
42296 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
42297 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
42298 token->location);
42299 c_name = "num_teams";
42300 break;
42301 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
42302 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
42303 token->location);
42304 c_name = "thread_limit";
42305 break;
42306 case PRAGMA_OMP_CLAUSE_ALIGNED:
42307 clauses = cp_parser_omp_clause_aligned (parser, clauses);
42308 c_name = "aligned";
42309 break;
42310 case PRAGMA_OMP_CLAUSE_ALLOCATE:
42311 clauses = cp_parser_omp_clause_allocate (parser, clauses);
42312 c_name = "allocate";
42313 break;
42314 case PRAGMA_OMP_CLAUSE_LINEAR:
42316 bool declare_simd = false;
42317 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
42318 declare_simd = true;
42319 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
42321 c_name = "linear";
42322 break;
42323 case PRAGMA_OMP_CLAUSE_AFFINITY:
42324 clauses = cp_parser_omp_clause_affinity (parser, clauses);
42325 c_name = "affinity";
42326 break;
42327 case PRAGMA_OMP_CLAUSE_DEPEND:
42328 clauses = cp_parser_omp_clause_depend (parser, clauses,
42329 token->location);
42330 c_name = "depend";
42331 break;
42332 case PRAGMA_OMP_CLAUSE_DOACROSS:
42333 clauses = cp_parser_omp_clause_doacross (parser, clauses,
42334 token->location);
42335 c_name = "doacross";
42336 break;
42337 case PRAGMA_OMP_CLAUSE_DETACH:
42338 clauses = cp_parser_omp_clause_detach (parser, clauses);
42339 c_name = "detach";
42340 break;
42341 case PRAGMA_OMP_CLAUSE_MAP:
42342 clauses = cp_parser_omp_clause_map (parser, clauses);
42343 c_name = "map";
42344 break;
42345 case PRAGMA_OMP_CLAUSE_DEVICE:
42346 clauses = cp_parser_omp_clause_device (parser, clauses,
42347 token->location);
42348 c_name = "device";
42349 break;
42350 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
42351 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
42352 token->location);
42353 c_name = "dist_schedule";
42354 break;
42355 case PRAGMA_OMP_CLAUSE_PROC_BIND:
42356 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
42357 token->location);
42358 c_name = "proc_bind";
42359 break;
42360 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
42361 clauses = cp_parser_omp_clause_device_type (parser, clauses,
42362 token->location);
42363 c_name = "device_type";
42364 break;
42365 case PRAGMA_OMP_CLAUSE_SAFELEN:
42366 clauses = cp_parser_omp_clause_safelen (parser, clauses,
42367 token->location);
42368 c_name = "safelen";
42369 break;
42370 case PRAGMA_OMP_CLAUSE_SIMDLEN:
42371 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
42372 token->location);
42373 c_name = "simdlen";
42374 break;
42375 case PRAGMA_OMP_CLAUSE_NOGROUP:
42376 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
42377 token->location);
42378 c_name = "nogroup";
42379 break;
42380 case PRAGMA_OMP_CLAUSE_THREADS:
42381 clauses
42382 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
42383 clauses, token->location);
42384 c_name = "threads";
42385 break;
42386 case PRAGMA_OMP_CLAUSE_SIMD:
42387 clauses
42388 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
42389 clauses, token->location);
42390 c_name = "simd";
42391 break;
42392 case PRAGMA_OMP_CLAUSE_ENTER:
42393 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42394 clauses);
42395 c_name = "enter";
42396 break;
42397 default:
42398 cp_parser_error (parser, "expected an OpenMP clause");
42399 goto saw_error;
42402 first = false;
42404 if (((mask >> c_kind) & 1) == 0)
42406 /* Remove the invalid clause(s) from the list to avoid
42407 confusing the rest of the compiler. */
42408 clauses = prev;
42409 error_at (token->location, "%qs is not valid for %qs", c_name, where);
42412 saw_error:
42413 if (!nested)
42414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42415 if (finish_p)
42417 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
42418 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
42419 else
42420 return finish_omp_clauses (clauses, C_ORT_OMP);
42422 return clauses;
42425 /* OpenMP 2.5:
42426 structured-block:
42427 statement
42429 In practice, we're also interested in adding the statement to an
42430 outer node. So it is convenient if we work around the fact that
42431 cp_parser_statement calls add_stmt. */
42433 static unsigned
42434 cp_parser_begin_omp_structured_block (cp_parser *parser)
42436 unsigned save = parser->in_statement;
42438 /* Only move the values to IN_OMP_BLOCK if they weren't false.
42439 This preserves the "not within loop or switch" style error messages
42440 for nonsense cases like
42441 void foo() {
42442 #pragma omp single
42443 break;
42446 if (parser->in_statement)
42447 parser->in_statement = IN_OMP_BLOCK;
42449 return save;
42452 static void
42453 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
42455 parser->in_statement = save;
42458 static tree
42459 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
42461 tree stmt = begin_omp_structured_block ();
42462 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42464 parser->omp_attrs_forbidden_p = true;
42465 cp_parser_statement (parser, NULL_TREE, false, if_p);
42467 cp_parser_end_omp_structured_block (parser, save);
42468 return finish_omp_structured_block (stmt);
42471 /* OpenMP 5.x:
42472 # pragma omp allocate (list) clauses
42474 OpenMP 5.0 clause:
42475 allocator (omp_allocator_handle_t expression)
42477 OpenMP 5.1 additional clause:
42478 align (constant-expression)] */
42480 static void
42481 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
42483 tree allocator = NULL_TREE;
42484 tree alignment = NULL_TREE;
42485 location_t loc = pragma_tok->location;
42486 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
42490 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42491 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42492 cp_lexer_consume_token (parser->lexer);
42494 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42495 break;
42496 matching_parens parens;
42497 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42498 const char *p = IDENTIFIER_POINTER (id);
42499 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42500 cp_lexer_consume_token (parser->lexer);
42501 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
42503 error_at (cloc, "expected %<allocator%> or %<align%>");
42504 break;
42506 if (!parens.require_open (parser))
42507 break;
42508 tree expr = cp_parser_assignment_expression (parser);
42509 if (p[2] == 'i' && alignment)
42511 error_at (cloc, "too many %qs clauses", "align");
42512 break;
42514 else if (p[2] == 'i')
42516 if (expr != error_mark_node)
42517 alignment = expr;
42518 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
42519 if (alignment
42520 && !type_dependent_expression_p (alignment)
42521 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
42523 error_at (cloc, "%<align%> clause argument needs to be "
42524 "positive constant power of two integer "
42525 "expression");
42526 alignment = NULL_TREE;
42528 else if (alignment)
42530 alignment = mark_rvalue_use (alignment);
42531 if (!processing_template_decl)
42533 alignment = maybe_constant_value (alignment);
42534 if (TREE_CODE (alignment) != INTEGER_CST
42535 || !tree_fits_uhwi_p (alignment)
42536 || !integer_pow2p (alignment))
42538 error_at (cloc, "%<align%> clause argument needs to be "
42539 "positive constant power of two integer "
42540 "expression");
42541 alignment = NULL_TREE;
42546 else if (allocator)
42548 error_at (cloc, "too many %qs clauses", "allocator");
42549 break;
42551 else
42553 if (expr != error_mark_node)
42554 allocator = expr;
42556 parens.require_close (parser);
42557 } while (true);
42558 cp_parser_require_pragma_eol (parser, pragma_tok);
42560 if (allocator || alignment)
42561 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
42563 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
42564 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
42567 /* FIXME: When implementing properly, delete the align/allocate expr error
42568 check above and add one in semantics.cc (to properly handle templates).
42569 Base this on the allocator/align modifiers check for the 'allocate' clause
42570 in semantics.cc's finish_omp_clauses. */
42571 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
42574 /* OpenMP 2.5:
42575 # pragma omp atomic new-line
42576 expression-stmt
42578 expression-stmt:
42579 x binop= expr | x++ | ++x | x-- | --x
42580 binop:
42581 +, *, -, /, &, ^, |, <<, >>
42583 where x is an lvalue expression with scalar type.
42585 OpenMP 3.1:
42586 # pragma omp atomic new-line
42587 update-stmt
42589 # pragma omp atomic read new-line
42590 read-stmt
42592 # pragma omp atomic write new-line
42593 write-stmt
42595 # pragma omp atomic update new-line
42596 update-stmt
42598 # pragma omp atomic capture new-line
42599 capture-stmt
42601 # pragma omp atomic capture new-line
42602 capture-block
42604 read-stmt:
42605 v = x
42606 write-stmt:
42607 x = expr
42608 update-stmt:
42609 expression-stmt | x = x binop expr
42610 capture-stmt:
42611 v = expression-stmt
42612 capture-block:
42613 { v = x; update-stmt; } | { update-stmt; v = x; }
42615 OpenMP 4.0:
42616 update-stmt:
42617 expression-stmt | x = x binop expr | x = expr binop x
42618 capture-stmt:
42619 v = update-stmt
42620 capture-block:
42621 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
42623 OpenMP 5.1:
42624 # pragma omp atomic compare new-line
42625 conditional-update-atomic
42627 # pragma omp atomic compare capture new-line
42628 conditional-update-capture-atomic
42630 conditional-update-atomic:
42631 cond-expr-stmt | cond-update-stmt
42632 cond-expr-stmt:
42633 x = expr ordop x ? expr : x;
42634 x = x ordop expr ? expr : x;
42635 x = x == e ? d : x;
42636 cond-update-stmt:
42637 if (expr ordop x) { x = expr; }
42638 if (x ordop expr) { x = expr; }
42639 if (x == e) { x = d; }
42640 ordop:
42641 <, >
42642 conditional-update-capture-atomic:
42643 v = cond-expr-stmt
42644 { v = x; cond-expr-stmt }
42645 { cond-expr-stmt v = x; }
42646 { v = x; cond-update-stmt }
42647 { cond-update-stmt v = x; }
42648 if (x == e) { x = d; } else { v = x; }
42649 { r = x == e; if (r) { x = d; } }
42650 { r = x == e; if (r) { x = d; } else { v = x; } }
42652 where x, r and v are lvalue expressions with scalar type,
42653 expr, e and d are expressions with scalar type and e might be
42654 the same as v. */
42656 static void
42657 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42659 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42660 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42661 location_t loc = pragma_tok->location;
42662 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42663 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42664 bool structured_block = false;
42665 tree clauses = NULL_TREE;
42666 bool capture = false;
42667 bool compare = false;
42668 bool weak = false;
42669 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42670 bool no_semicolon = false;
42671 bool extra_scope = false;
42673 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42676 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42677 cp_lexer_consume_token (parser->lexer);
42679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42682 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42683 const char *p = IDENTIFIER_POINTER (id);
42684 enum tree_code new_code = ERROR_MARK;
42685 enum omp_memory_order new_memory_order
42686 = OMP_MEMORY_ORDER_UNSPECIFIED;
42687 bool new_capture = false;
42688 bool new_compare = false;
42689 bool new_weak = false;
42690 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42692 if (!strcmp (p, "read"))
42693 new_code = OMP_ATOMIC_READ;
42694 else if (!strcmp (p, "write"))
42695 new_code = NOP_EXPR;
42696 else if (!strcmp (p, "update"))
42697 new_code = OMP_ATOMIC;
42698 else if (openacc && !strcmp (p, "capture"))
42699 new_code = OMP_ATOMIC_CAPTURE_NEW;
42700 else if (openacc)
42702 p = NULL;
42703 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42704 "or %<capture%> clause");
42706 else if (!strcmp (p, "capture"))
42707 new_capture = true;
42708 else if (!strcmp (p, "compare"))
42709 new_compare = true;
42710 else if (!strcmp (p, "weak"))
42711 new_weak = true;
42712 else if (!strcmp (p, "fail"))
42714 matching_parens parens;
42716 cp_lexer_consume_token (parser->lexer);
42717 if (!parens.require_open (parser))
42718 continue;
42720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42722 id = cp_lexer_peek_token (parser->lexer)->u.value;
42723 const char *q = IDENTIFIER_POINTER (id);
42725 if (!strcmp (q, "seq_cst"))
42726 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42727 else if (!strcmp (q, "acquire"))
42728 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42729 else if (!strcmp (q, "relaxed"))
42730 new_fail = OMP_MEMORY_ORDER_RELAXED;
42733 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42735 cp_lexer_consume_token (parser->lexer);
42736 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42737 error_at (cloc, "too many %qs clauses", "fail");
42738 else
42739 fail = new_fail;
42741 else
42742 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42743 "or %<relaxed%>");
42744 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42745 || !parens.require_close (parser))
42746 cp_parser_skip_to_closing_parenthesis (parser,
42747 /*recovering=*/true,
42748 /*or_comma=*/false,
42749 /*consume_paren=*/true);
42750 continue;
42752 else if (!strcmp (p, "seq_cst"))
42753 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42754 else if (!strcmp (p, "acq_rel"))
42755 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42756 else if (!strcmp (p, "release"))
42757 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42758 else if (!strcmp (p, "acquire"))
42759 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42760 else if (!strcmp (p, "relaxed"))
42761 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42762 else if (!strcmp (p, "hint"))
42764 cp_lexer_consume_token (parser->lexer);
42765 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42766 continue;
42768 else
42770 p = NULL;
42771 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42772 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42773 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42774 "%<relaxed%> or %<hint%> clause");
42776 if (p)
42778 if (new_code != ERROR_MARK)
42780 /* OpenACC permits 'update capture'. */
42781 if (openacc
42782 && code == OMP_ATOMIC
42783 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42784 code = new_code;
42785 else if (code != ERROR_MARK)
42786 error_at (cloc, "too many atomic clauses");
42787 else
42788 code = new_code;
42790 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42792 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42793 error_at (cloc, "too many memory order clauses");
42794 else
42795 memory_order = new_memory_order;
42797 else if (new_capture)
42799 if (capture)
42800 error_at (cloc, "too many %qs clauses", "capture");
42801 else
42802 capture = true;
42804 else if (new_compare)
42806 if (compare)
42807 error_at (cloc, "too many %qs clauses", "compare");
42808 else
42809 compare = true;
42811 else if (new_weak)
42813 if (weak)
42814 error_at (cloc, "too many %qs clauses", "weak");
42815 else
42816 weak = true;
42818 cp_lexer_consume_token (parser->lexer);
42819 continue;
42822 break;
42824 cp_parser_require_pragma_eol (parser, pragma_tok);
42826 if (code == ERROR_MARK)
42827 code = OMP_ATOMIC;
42828 if (capture)
42830 if (code != OMP_ATOMIC)
42831 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42832 "clauses", "capture");
42833 else
42834 code = OMP_ATOMIC_CAPTURE_NEW;
42836 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42838 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42839 "clauses", "compare");
42840 compare = false;
42842 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42844 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42845 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42847 if (weak && !compare)
42849 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42850 weak = false;
42852 if (openacc)
42853 memory_order = OMP_MEMORY_ORDER_RELAXED;
42854 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42856 omp_requires_mask
42857 = (enum omp_requires) (omp_requires_mask
42858 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42859 switch ((enum omp_memory_order)
42860 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42862 case OMP_MEMORY_ORDER_UNSPECIFIED:
42863 case OMP_MEMORY_ORDER_RELAXED:
42864 memory_order = OMP_MEMORY_ORDER_RELAXED;
42865 break;
42866 case OMP_MEMORY_ORDER_SEQ_CST:
42867 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42868 break;
42869 case OMP_MEMORY_ORDER_ACQUIRE:
42870 if (code == NOP_EXPR) /* atomic write */
42872 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42873 "%<acquire%> clause implicitly provided by a "
42874 "%<requires%> directive");
42875 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42877 else
42878 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42879 break;
42880 case OMP_MEMORY_ORDER_RELEASE:
42881 if (code == OMP_ATOMIC_READ)
42883 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42884 "%<release%> clause implicitly provided by a "
42885 "%<requires%> directive");
42886 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42888 else
42889 memory_order = OMP_MEMORY_ORDER_RELEASE;
42890 break;
42891 case OMP_MEMORY_ORDER_ACQ_REL:
42892 switch (code)
42894 case OMP_ATOMIC_READ:
42895 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42896 break;
42897 case NOP_EXPR: /* atomic write */
42898 memory_order = OMP_MEMORY_ORDER_RELEASE;
42899 break;
42900 default:
42901 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42902 break;
42904 break;
42905 default:
42906 gcc_unreachable ();
42909 else
42910 switch (code)
42912 case OMP_ATOMIC_READ:
42913 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42915 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42916 "%<release%> clause");
42917 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42919 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42920 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42921 break;
42922 case NOP_EXPR: /* atomic write */
42923 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42925 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42926 "%<acquire%> clause");
42927 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42929 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42930 memory_order = OMP_MEMORY_ORDER_RELEASE;
42931 break;
42932 default:
42933 break;
42935 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42936 memory_order
42937 = (enum omp_memory_order) (memory_order
42938 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42940 switch (code)
42942 case OMP_ATOMIC_READ:
42943 case NOP_EXPR: /* atomic write */
42944 v = cp_parser_unary_expression (parser);
42945 if (v == error_mark_node)
42946 goto saw_error;
42947 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42948 goto saw_error;
42949 if (code == NOP_EXPR)
42950 lhs = cp_parser_expression (parser);
42951 else
42952 lhs = cp_parser_unary_expression (parser);
42953 if (lhs == error_mark_node)
42954 goto saw_error;
42955 if (code == NOP_EXPR)
42957 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42958 opcode. */
42959 code = OMP_ATOMIC;
42960 rhs = lhs;
42961 lhs = v;
42962 v = NULL_TREE;
42964 goto done;
42965 case OMP_ATOMIC_CAPTURE_NEW:
42966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42968 cp_lexer_consume_token (parser->lexer);
42969 structured_block = true;
42971 else if (compare
42972 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42973 break;
42974 else
42976 v = cp_parser_unary_expression (parser);
42977 if (v == error_mark_node)
42978 goto saw_error;
42979 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42980 goto saw_error;
42981 if (compare
42982 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42984 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42985 error_at (eloc, "expected expression");
42986 goto saw_error;
42989 default:
42990 break;
42993 restart:
42994 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42996 cp_lexer_consume_token (parser->lexer);
42998 matching_parens parens;
42999 if (!parens.require_open (parser))
43000 goto saw_error;
43001 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43002 tree cmp_expr;
43003 if (r)
43004 cmp_expr = cp_parser_unary_expression (parser);
43005 else
43006 cmp_expr = cp_parser_binary_expression (parser, false, true,
43007 PREC_NOT_OPERATOR, NULL);
43008 if (!parens.require_close (parser))
43009 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
43010 if (cmp_expr == error_mark_node)
43011 goto saw_error;
43012 if (r)
43014 if (!cp_tree_equal (cmp_expr, r))
43015 goto bad_if;
43016 cmp_expr = rhs;
43017 rhs = NULL_TREE;
43018 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
43020 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43022 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43024 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43025 "expected %<==%> comparison in %<if%> condition");
43026 goto saw_error;
43028 else if (TREE_CODE (cmp_expr) != GT_EXPR
43029 && TREE_CODE (cmp_expr) != LT_EXPR)
43031 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43032 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
43033 "condition");
43034 goto saw_error;
43036 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43037 goto saw_error;
43039 extra_scope = true;
43040 eloc = cp_lexer_peek_token (parser->lexer)->location;
43041 lhs = cp_parser_unary_expression (parser);
43042 orig_lhs = lhs;
43043 if (lhs == error_mark_node)
43044 goto saw_error;
43045 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43047 cp_parser_error (parser, "expected %<=%>");
43048 goto saw_error;
43050 cp_lexer_consume_token (parser->lexer);
43051 eloc = cp_lexer_peek_token (parser->lexer)->location;
43052 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43053 rhs1 = cp_parser_expression (parser);
43054 else
43055 rhs1 = cp_parser_simple_cast_expression (parser);
43057 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43058 goto saw_error;
43060 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43061 goto saw_error;
43063 extra_scope = false;
43064 no_semicolon = true;
43066 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
43068 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43070 opcode = COND_EXPR;
43071 rhs = TREE_OPERAND (cmp_expr, 1);
43073 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
43075 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43076 ? MIN_EXPR : MAX_EXPR);
43077 rhs = rhs1;
43078 rhs1 = TREE_OPERAND (cmp_expr, 0);
43080 else
43081 goto bad_if;
43083 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
43084 goto bad_if;
43085 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
43086 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
43088 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43089 ? MAX_EXPR : MIN_EXPR);
43090 rhs = rhs1;
43091 rhs1 = TREE_OPERAND (cmp_expr, 1);
43093 else
43095 bad_if:
43096 cp_parser_error (parser,
43097 "invalid form of %<#pragma omp atomic compare%>");
43098 goto saw_error;
43101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
43103 if (code != OMP_ATOMIC_CAPTURE_NEW
43104 || (structured_block && r == NULL_TREE)
43105 || TREE_CODE (cmp_expr) != EQ_EXPR)
43107 eloc = cp_lexer_peek_token (parser->lexer)->location;
43108 error_at (eloc, "unexpected %<else%>");
43109 goto saw_error;
43112 cp_lexer_consume_token (parser->lexer);
43114 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43115 goto saw_error;
43117 extra_scope = true;
43118 v = cp_parser_unary_expression (parser);
43119 if (v == error_mark_node)
43120 goto saw_error;
43121 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43122 goto saw_error;
43124 tree expr = cp_parser_simple_cast_expression (parser);
43126 if (!cp_tree_equal (expr, lhs))
43127 goto bad_if;
43129 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43130 goto saw_error;
43132 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43133 goto saw_error;
43135 extra_scope = false;
43136 code = OMP_ATOMIC_CAPTURE_OLD;
43137 if (r == NULL_TREE)
43138 /* Signal to c_finish_omp_atomic that in
43139 if (x == e) { x = d; } else { v = x; }
43140 case the store to v should be conditional. */
43141 r = void_list_node;
43143 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43145 cp_parser_error (parser, "expected %<else%>");
43146 goto saw_error;
43148 else if (code == OMP_ATOMIC_CAPTURE_NEW
43149 && r != NULL_TREE
43150 && v == NULL_TREE)
43151 code = OMP_ATOMIC;
43152 goto stmt_done;
43154 lhs = cp_parser_unary_expression (parser);
43155 orig_lhs = lhs;
43156 switch (TREE_CODE (lhs))
43158 case ERROR_MARK:
43159 goto saw_error;
43161 case POSTINCREMENT_EXPR:
43162 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43163 code = OMP_ATOMIC_CAPTURE_OLD;
43164 /* FALLTHROUGH */
43165 case PREINCREMENT_EXPR:
43166 lhs = TREE_OPERAND (lhs, 0);
43167 opcode = PLUS_EXPR;
43168 rhs = integer_one_node;
43169 if (compare)
43170 goto invalid_compare;
43171 break;
43173 case POSTDECREMENT_EXPR:
43174 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43175 code = OMP_ATOMIC_CAPTURE_OLD;
43176 /* FALLTHROUGH */
43177 case PREDECREMENT_EXPR:
43178 lhs = TREE_OPERAND (lhs, 0);
43179 opcode = MINUS_EXPR;
43180 rhs = integer_one_node;
43181 if (compare)
43182 goto invalid_compare;
43183 break;
43185 case COMPOUND_EXPR:
43186 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
43187 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
43188 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
43189 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
43190 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
43191 (TREE_OPERAND (lhs, 1), 0), 0)))
43192 == BOOLEAN_TYPE)
43193 /* Undo effects of boolean_increment for post {in,de}crement. */
43194 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
43195 /* FALLTHRU */
43196 case MODIFY_EXPR:
43197 if (TREE_CODE (lhs) == MODIFY_EXPR
43198 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
43200 /* Undo effects of boolean_increment. */
43201 if (integer_onep (TREE_OPERAND (lhs, 1)))
43203 /* This is pre or post increment. */
43204 rhs = TREE_OPERAND (lhs, 1);
43205 lhs = TREE_OPERAND (lhs, 0);
43206 opcode = NOP_EXPR;
43207 if (code == OMP_ATOMIC_CAPTURE_NEW
43208 && !structured_block
43209 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
43210 code = OMP_ATOMIC_CAPTURE_OLD;
43211 if (compare)
43212 goto invalid_compare;
43213 break;
43216 /* FALLTHRU */
43217 default:
43218 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43220 cp_parser_error (parser, "expected %<=%>");
43221 goto saw_error;
43223 switch (cp_lexer_peek_token (parser->lexer)->type)
43225 case CPP_MULT_EQ:
43226 opcode = MULT_EXPR;
43227 break;
43228 case CPP_DIV_EQ:
43229 opcode = TRUNC_DIV_EXPR;
43230 break;
43231 case CPP_PLUS_EQ:
43232 opcode = PLUS_EXPR;
43233 break;
43234 case CPP_MINUS_EQ:
43235 opcode = MINUS_EXPR;
43236 break;
43237 case CPP_LSHIFT_EQ:
43238 opcode = LSHIFT_EXPR;
43239 break;
43240 case CPP_RSHIFT_EQ:
43241 opcode = RSHIFT_EXPR;
43242 break;
43243 case CPP_AND_EQ:
43244 opcode = BIT_AND_EXPR;
43245 break;
43246 case CPP_OR_EQ:
43247 opcode = BIT_IOR_EXPR;
43248 break;
43249 case CPP_XOR_EQ:
43250 opcode = BIT_XOR_EXPR;
43251 break;
43252 case CPP_EQ:
43253 enum cp_parser_prec oprec;
43254 cp_token *token;
43255 cp_lexer_consume_token (parser->lexer);
43256 cp_parser_parse_tentatively (parser);
43257 rhs1 = cp_parser_simple_cast_expression (parser);
43258 if (rhs1 == error_mark_node)
43260 cp_parser_abort_tentative_parse (parser);
43261 cp_parser_simple_cast_expression (parser);
43262 goto saw_error;
43264 token = cp_lexer_peek_token (parser->lexer);
43265 if (token->type != CPP_SEMICOLON
43266 && (!compare || token->type != CPP_QUERY)
43267 && !cp_tree_equal (lhs, rhs1))
43269 cp_parser_abort_tentative_parse (parser);
43270 cp_parser_parse_tentatively (parser);
43271 rhs = cp_parser_binary_expression (parser, false, true,
43272 PREC_NOT_OPERATOR, NULL);
43273 if (rhs == error_mark_node)
43275 cp_parser_abort_tentative_parse (parser);
43276 cp_parser_binary_expression (parser, false, true,
43277 PREC_NOT_OPERATOR, NULL);
43278 goto saw_error;
43280 switch (TREE_CODE (rhs))
43282 case MULT_EXPR:
43283 case TRUNC_DIV_EXPR:
43284 case RDIV_EXPR:
43285 case PLUS_EXPR:
43286 case MINUS_EXPR:
43287 case LSHIFT_EXPR:
43288 case RSHIFT_EXPR:
43289 case BIT_AND_EXPR:
43290 case BIT_IOR_EXPR:
43291 case BIT_XOR_EXPR:
43292 if (compare)
43293 break;
43294 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
43296 if (cp_parser_parse_definitely (parser))
43298 opcode = TREE_CODE (rhs);
43299 rhs1 = TREE_OPERAND (rhs, 0);
43300 rhs = TREE_OPERAND (rhs, 1);
43301 goto stmt_done;
43303 else
43304 goto saw_error;
43306 break;
43307 case EQ_EXPR:
43308 if (!compare
43309 || code != OMP_ATOMIC_CAPTURE_NEW
43310 || !structured_block
43311 || v
43312 || r)
43313 break;
43314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
43315 && cp_lexer_nth_token_is_keyword (parser->lexer,
43316 2, RID_IF))
43318 if (cp_parser_parse_definitely (parser))
43320 r = lhs;
43321 lhs = NULL_TREE;
43322 rhs1 = NULL_TREE;
43323 cp_lexer_consume_token (parser->lexer);
43324 goto restart;
43327 break;
43328 case GT_EXPR:
43329 case LT_EXPR:
43330 if (compare
43331 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
43332 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
43333 && cp_parser_parse_definitely (parser))
43335 opcode = TREE_CODE (rhs);
43336 rhs1 = TREE_OPERAND (rhs, 0);
43337 rhs = TREE_OPERAND (rhs, 1);
43338 cond_expr:
43339 cp_lexer_consume_token (parser->lexer);
43340 bool saved_colon_corrects_to_scope_p
43341 = parser->colon_corrects_to_scope_p;
43342 parser->colon_corrects_to_scope_p = false;
43343 tree e1 = cp_parser_expression (parser);
43344 parser->colon_corrects_to_scope_p
43345 = saved_colon_corrects_to_scope_p;
43346 cp_parser_require (parser, CPP_COLON, RT_COLON);
43347 tree e2 = cp_parser_simple_cast_expression (parser);
43348 if (cp_tree_equal (lhs, e2))
43350 if (cp_tree_equal (lhs, rhs1))
43352 if (opcode == EQ_EXPR)
43354 opcode = COND_EXPR;
43355 rhs1 = e1;
43356 goto stmt_done;
43358 if (cp_tree_equal (rhs, e1))
43360 opcode
43361 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
43362 rhs = e1;
43363 goto stmt_done;
43366 else
43368 gcc_assert (opcode != EQ_EXPR);
43369 if (cp_tree_equal (rhs1, e1))
43371 opcode
43372 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
43373 rhs1 = rhs;
43374 rhs = e1;
43375 goto stmt_done;
43379 cp_parser_error (parser,
43380 "invalid form of "
43381 "%<#pragma omp atomic compare%>");
43382 goto saw_error;
43384 break;
43385 default:
43386 break;
43388 cp_parser_abort_tentative_parse (parser);
43389 if (structured_block
43390 && code == OMP_ATOMIC_CAPTURE_OLD
43391 && !compare)
43393 rhs = cp_parser_expression (parser);
43394 if (rhs == error_mark_node)
43395 goto saw_error;
43396 opcode = NOP_EXPR;
43397 rhs1 = NULL_TREE;
43398 goto stmt_done;
43400 cp_parser_error (parser,
43401 "invalid form of %<#pragma omp atomic%>");
43402 goto saw_error;
43404 if (!cp_parser_parse_definitely (parser))
43405 goto saw_error;
43406 switch (token->type)
43408 case CPP_SEMICOLON:
43409 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43411 code = OMP_ATOMIC_CAPTURE_OLD;
43412 v = lhs;
43413 lhs = NULL_TREE;
43414 lhs1 = rhs1;
43415 rhs1 = NULL_TREE;
43416 cp_lexer_consume_token (parser->lexer);
43417 goto restart;
43419 else if (structured_block && !compare)
43421 opcode = NOP_EXPR;
43422 rhs = rhs1;
43423 rhs1 = NULL_TREE;
43424 goto stmt_done;
43426 cp_parser_error (parser,
43427 "invalid form of %<#pragma omp atomic%>");
43428 goto saw_error;
43429 case CPP_MULT:
43430 opcode = MULT_EXPR;
43431 break;
43432 case CPP_DIV:
43433 opcode = TRUNC_DIV_EXPR;
43434 break;
43435 case CPP_PLUS:
43436 opcode = PLUS_EXPR;
43437 break;
43438 case CPP_MINUS:
43439 opcode = MINUS_EXPR;
43440 break;
43441 case CPP_LSHIFT:
43442 opcode = LSHIFT_EXPR;
43443 break;
43444 case CPP_RSHIFT:
43445 opcode = RSHIFT_EXPR;
43446 break;
43447 case CPP_AND:
43448 opcode = BIT_AND_EXPR;
43449 break;
43450 case CPP_OR:
43451 opcode = BIT_IOR_EXPR;
43452 break;
43453 case CPP_XOR:
43454 opcode = BIT_XOR_EXPR;
43455 break;
43456 case CPP_EQ_EQ:
43457 opcode = EQ_EXPR;
43458 break;
43459 case CPP_GREATER:
43460 opcode = GT_EXPR;
43461 break;
43462 case CPP_LESS:
43463 opcode = LT_EXPR;
43464 break;
43465 default:
43466 cp_parser_error (parser,
43467 "invalid operator for %<#pragma omp atomic%>");
43468 goto saw_error;
43470 if (compare
43471 && TREE_CODE_CLASS (opcode) != tcc_comparison)
43473 cp_parser_error (parser,
43474 "invalid form of "
43475 "%<#pragma omp atomic compare%>");
43476 goto saw_error;
43478 oprec = TOKEN_PRECEDENCE (token);
43479 gcc_assert (oprec != PREC_NOT_OPERATOR);
43480 if (commutative_tree_code (opcode))
43481 oprec = (enum cp_parser_prec) (oprec - 1);
43482 cp_lexer_consume_token (parser->lexer);
43483 rhs = cp_parser_binary_expression (parser, false, false,
43484 oprec, NULL);
43485 if (rhs == error_mark_node)
43486 goto saw_error;
43487 if (compare)
43489 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
43491 cp_parser_error (parser,
43492 "invalid form of "
43493 "%<#pragma omp atomic compare%>");
43494 goto saw_error;
43496 goto cond_expr;
43498 goto stmt_done;
43499 default:
43500 cp_parser_error (parser,
43501 "invalid operator for %<#pragma omp atomic%>");
43502 goto saw_error;
43504 cp_lexer_consume_token (parser->lexer);
43506 rhs = cp_parser_expression (parser);
43507 if (rhs == error_mark_node)
43508 goto saw_error;
43509 break;
43511 stmt_done:
43512 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
43514 if (!no_semicolon
43515 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43516 goto saw_error;
43517 no_semicolon = false;
43518 v = cp_parser_unary_expression (parser);
43519 if (v == error_mark_node)
43520 goto saw_error;
43521 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43522 goto saw_error;
43523 lhs1 = cp_parser_unary_expression (parser);
43524 if (lhs1 == error_mark_node)
43525 goto saw_error;
43527 if (structured_block)
43529 if (!no_semicolon)
43530 cp_parser_consume_semicolon_at_end_of_statement (parser);
43531 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
43533 done:
43534 if (weak && opcode != COND_EXPR)
43536 error_at (loc, "%<weak%> clause requires atomic equality comparison");
43537 weak = false;
43539 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43540 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
43541 rhs1, r, clauses, memory_order, weak);
43542 if (!structured_block && !no_semicolon)
43543 cp_parser_consume_semicolon_at_end_of_statement (parser);
43544 return;
43546 invalid_compare:
43547 error ("invalid form of %<pragma omp atomic compare%>");
43548 /* FALLTHRU */
43549 saw_error:
43550 cp_parser_skip_to_end_of_block_or_statement (parser);
43551 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43552 cp_lexer_consume_token (parser->lexer);
43553 if (structured_block)
43555 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43556 cp_lexer_consume_token (parser->lexer);
43557 else if (code == OMP_ATOMIC_CAPTURE_NEW)
43559 cp_parser_skip_to_end_of_block_or_statement (parser);
43560 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43561 cp_lexer_consume_token (parser->lexer);
43567 /* OpenMP 2.5:
43568 # pragma omp barrier new-line */
43570 static void
43571 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
43573 cp_parser_require_pragma_eol (parser, pragma_tok);
43574 finish_omp_barrier ();
43577 /* OpenMP 2.5:
43578 # pragma omp critical [(name)] new-line
43579 structured-block
43581 OpenMP 4.5:
43582 # pragma omp critical [(name) [hint(expression)]] new-line
43583 structured-block */
43585 #define OMP_CRITICAL_CLAUSE_MASK \
43586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
43588 static tree
43589 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43591 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
43593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43595 matching_parens parens;
43596 parens.consume_open (parser);
43598 name = cp_parser_identifier (parser);
43600 if (name == error_mark_node
43601 || !parens.require_close (parser))
43602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43603 /*or_comma=*/false,
43604 /*consume_paren=*/true);
43605 if (name == error_mark_node)
43606 name = NULL;
43608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43609 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43610 cp_lexer_consume_token (parser->lexer);
43613 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
43614 "#pragma omp critical", pragma_tok);
43616 stmt = cp_parser_omp_structured_block (parser, if_p);
43617 return c_finish_omp_critical (input_location, stmt, name, clauses);
43620 /* OpenMP 5.0:
43621 # pragma omp depobj ( depobj ) depobj-clause new-line
43623 depobj-clause:
43624 depend (dependence-type : locator)
43625 destroy
43626 update (dependence-type)
43628 OpenMP 5.2 additionally:
43629 destroy ( depobj )
43631 dependence-type:
43634 inout
43635 mutexinout */
43637 static void
43638 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
43640 location_t loc = pragma_tok->location;
43641 matching_parens parens;
43642 if (!parens.require_open (parser))
43644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43645 return;
43648 tree depobj = cp_parser_assignment_expression (parser);
43650 if (!parens.require_close (parser))
43651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43652 /*or_comma=*/false,
43653 /*consume_paren=*/true);
43655 tree clause = NULL_TREE;
43656 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
43657 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43658 cp_lexer_consume_token (parser->lexer);
43659 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
43660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43663 const char *p = IDENTIFIER_POINTER (id);
43665 cp_lexer_consume_token (parser->lexer);
43666 if (!strcmp ("depend", p))
43668 /* Don't create location wrapper nodes within the depend clause. */
43669 auto_suppress_location_wrappers sentinel;
43670 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
43671 if (clause)
43672 clause = finish_omp_clauses (clause, C_ORT_OMP);
43673 if (!clause)
43674 clause = error_mark_node;
43676 else if (!strcmp ("destroy", p))
43678 kind = OMP_CLAUSE_DEPEND_LAST;
43679 matching_parens c_parens;
43680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43681 && c_parens.require_open (parser))
43683 tree destobj = cp_parser_assignment_expression (parser);
43684 if (depobj != error_mark_node
43685 && destobj != error_mark_node
43686 && !operand_equal_p (destobj, depobj, OEP_MATCH_SIDE_EFFECTS
43687 | OEP_LEXICOGRAPHIC))
43688 warning_at (EXPR_LOC_OR_LOC (destobj, c_loc), OPT_Wopenmp,
43689 "the %<destroy%> expression %qE should be the same "
43690 "as the %<depobj%> argument %qE", destobj, depobj);
43691 if (!c_parens.require_close (parser))
43692 cp_parser_skip_to_closing_parenthesis (parser,
43693 /*recovering=*/true,
43694 /*or_comma=*/false,
43695 /*consume_paren=*/true);
43698 else if (!strcmp ("update", p))
43700 matching_parens c_parens;
43701 if (c_parens.require_open (parser))
43703 location_t c2_loc
43704 = cp_lexer_peek_token (parser->lexer)->location;
43705 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43707 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43708 const char *p2 = IDENTIFIER_POINTER (id2);
43710 cp_lexer_consume_token (parser->lexer);
43711 if (!strcmp ("in", p2))
43712 kind = OMP_CLAUSE_DEPEND_IN;
43713 else if (!strcmp ("out", p2))
43714 kind = OMP_CLAUSE_DEPEND_OUT;
43715 else if (!strcmp ("inout", p2))
43716 kind = OMP_CLAUSE_DEPEND_INOUT;
43717 else if (!strcmp ("mutexinoutset", p2))
43718 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43719 else if (!strcmp ("inoutset", p2))
43720 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43722 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43724 clause = error_mark_node;
43725 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43726 "%<mutexinoutset%> or %<inoutset%>");
43728 if (!c_parens.require_close (parser))
43729 cp_parser_skip_to_closing_parenthesis (parser,
43730 /*recovering=*/true,
43731 /*or_comma=*/false,
43732 /*consume_paren=*/true);
43734 else
43735 clause = error_mark_node;
43738 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43740 clause = error_mark_node;
43741 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43743 cp_parser_require_pragma_eol (parser, pragma_tok);
43745 finish_omp_depobj (loc, depobj, kind, clause);
43749 /* OpenMP 2.5:
43750 # pragma omp flush flush-vars[opt] new-line
43752 flush-vars:
43753 ( variable-list )
43755 OpenMP 5.0:
43756 # pragma omp flush memory-order-clause new-line */
43758 static void
43759 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43761 enum memmodel mo = MEMMODEL_LAST;
43762 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43763 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43764 cp_lexer_consume_token (parser->lexer);
43765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43768 const char *p = IDENTIFIER_POINTER (id);
43769 if (!strcmp (p, "seq_cst"))
43770 mo = MEMMODEL_SEQ_CST;
43771 else if (!strcmp (p, "acq_rel"))
43772 mo = MEMMODEL_ACQ_REL;
43773 else if (!strcmp (p, "release"))
43774 mo = MEMMODEL_RELEASE;
43775 else if (!strcmp (p, "acquire"))
43776 mo = MEMMODEL_ACQUIRE;
43777 else
43778 error_at (cp_lexer_peek_token (parser->lexer)->location,
43779 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43780 "%<acquire%>");
43781 cp_lexer_consume_token (parser->lexer);
43783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43785 if (mo != MEMMODEL_LAST)
43786 error_at (cp_lexer_peek_token (parser->lexer)->location,
43787 "%<flush%> list specified together with memory order "
43788 "clause");
43789 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43791 cp_parser_require_pragma_eol (parser, pragma_tok);
43793 finish_omp_flush (mo);
43796 /* Helper function, to parse omp for increment expression. */
43798 static tree
43799 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43801 tree cond = cp_parser_binary_expression (parser, false, true,
43802 PREC_NOT_OPERATOR, NULL);
43803 if (cond == error_mark_node
43804 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43806 cp_parser_skip_to_end_of_statement (parser);
43807 return error_mark_node;
43810 switch (TREE_CODE (cond))
43812 case GT_EXPR:
43813 case GE_EXPR:
43814 case LT_EXPR:
43815 case LE_EXPR:
43816 break;
43817 case NE_EXPR:
43818 if (code != OACC_LOOP)
43819 break;
43820 gcc_fallthrough ();
43821 default:
43822 return error_mark_node;
43825 /* If decl is an iterator, preserve LHS and RHS of the relational
43826 expr until finish_omp_for. */
43827 if (decl
43828 && (type_dependent_expression_p (decl)
43829 || CLASS_TYPE_P (TREE_TYPE (decl))))
43830 return cond;
43832 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
43833 TREE_CODE (cond),
43834 TREE_OPERAND (cond, 0), ERROR_MARK,
43835 TREE_OPERAND (cond, 1), ERROR_MARK,
43836 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
43839 /* Helper function, to parse omp for increment expression. */
43841 static tree
43842 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
43844 cp_token *token = cp_lexer_peek_token (parser->lexer);
43845 enum tree_code op;
43846 tree lhs, rhs;
43847 cp_id_kind idk;
43848 bool decl_first;
43850 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43852 op = (token->type == CPP_PLUS_PLUS
43853 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
43854 cp_lexer_consume_token (parser->lexer);
43855 lhs = cp_parser_simple_cast_expression (parser);
43856 if (lhs != decl
43857 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43858 return error_mark_node;
43859 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43862 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
43863 if (lhs != decl
43864 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43865 return error_mark_node;
43867 token = cp_lexer_peek_token (parser->lexer);
43868 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43870 op = (token->type == CPP_PLUS_PLUS
43871 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
43872 cp_lexer_consume_token (parser->lexer);
43873 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43876 op = cp_parser_assignment_operator_opt (parser);
43877 if (op == ERROR_MARK)
43878 return error_mark_node;
43880 if (op != NOP_EXPR)
43882 rhs = cp_parser_assignment_expression (parser);
43883 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43884 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43887 lhs = cp_parser_binary_expression (parser, false, false,
43888 PREC_ADDITIVE_EXPRESSION, NULL);
43889 token = cp_lexer_peek_token (parser->lexer);
43890 decl_first = (lhs == decl
43891 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43892 if (decl_first)
43893 lhs = NULL_TREE;
43894 if (token->type != CPP_PLUS
43895 && token->type != CPP_MINUS)
43896 return error_mark_node;
43900 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43901 cp_lexer_consume_token (parser->lexer);
43902 rhs = cp_parser_binary_expression (parser, false, false,
43903 PREC_ADDITIVE_EXPRESSION, NULL);
43904 token = cp_lexer_peek_token (parser->lexer);
43905 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43907 if (lhs == NULL_TREE)
43909 if (op == PLUS_EXPR)
43910 lhs = rhs;
43911 else
43912 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43913 NULL_TREE, tf_warning_or_error);
43915 else
43916 lhs = build_x_binary_op (input_location, op,
43917 lhs, ERROR_MARK,
43918 rhs, ERROR_MARK,
43919 NULL_TREE, NULL, tf_warning_or_error);
43922 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43924 if (!decl_first)
43926 if ((rhs != decl
43927 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43928 || op == MINUS_EXPR)
43929 return error_mark_node;
43930 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43932 else
43933 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43935 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43938 /* Parse the initialization statement of an OpenMP for loop. Range-for
43939 is handled separately in cp_convert_omp_range_for.
43941 On entry SL is the current statement list. Parsing of some forms
43942 of initialization pops this list and stores its contents in either INIT
43943 or THIS_PRE_BODY, and sets SL to null. Initialization for class
43944 iterators is added directly to SL and it is not popped until later.
43946 On return, DECL is set if the initialization is by binding the
43947 iteration variable. If the initialization is by assignment, REAL_DECL
43948 is set to point to a variable in an outer scope. ORIG_INIT is set
43949 if the iteration variable is of class type; this is a copy saved for
43950 error checking in finish_omp_for.
43952 Return true if the resulting construct should have an
43953 OMP_CLAUSE_PRIVATE added to it. */
43955 static tree
43956 cp_parser_omp_for_loop_init (cp_parser *parser,
43957 tree &this_pre_body,
43958 tree &sl,
43959 tree &init,
43960 tree &orig_init,
43961 tree &decl,
43962 tree &real_decl)
43964 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43965 return NULL_TREE;
43967 tree add_private_clause = NULL_TREE;
43969 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43971 init-expr:
43972 var = lb
43973 integer-type var = lb
43974 random-access-iterator-type var = lb
43975 pointer-type var = lb
43977 cp_decl_specifier_seq type_specifiers;
43979 /* First, try to parse as an initialized declaration. See
43980 cp_parser_condition, from whence the bulk of this is copied. */
43982 cp_parser_parse_tentatively (parser);
43983 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43984 /*is_declaration=*/true,
43985 /*is_trailing_return=*/false,
43986 &type_specifiers);
43987 if (cp_parser_parse_definitely (parser))
43989 /* If parsing a type specifier seq succeeded, then this
43990 MUST be a initialized declaration. */
43991 tree asm_specification, attributes;
43992 cp_declarator *declarator;
43994 declarator = cp_parser_declarator (parser,
43995 CP_PARSER_DECLARATOR_NAMED,
43996 CP_PARSER_FLAGS_NONE,
43997 /*ctor_dtor_or_conv_p=*/NULL,
43998 /*parenthesized_p=*/NULL,
43999 /*member_p=*/false,
44000 /*friend_p=*/false,
44001 /*static_p=*/false);
44002 attributes = cp_parser_attributes_opt (parser);
44003 asm_specification = cp_parser_asm_specification_opt (parser);
44005 if (declarator == cp_error_declarator)
44006 cp_parser_skip_to_end_of_statement (parser);
44008 else
44010 tree pushed_scope, auto_node;
44012 decl = start_decl (declarator, &type_specifiers,
44013 SD_INITIALIZED, attributes,
44014 /*prefix_attributes=*/NULL_TREE,
44015 &pushed_scope);
44017 auto_node = type_uses_auto (TREE_TYPE (decl));
44018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
44020 if (cp_lexer_next_token_is (parser->lexer,
44021 CPP_OPEN_PAREN))
44022 error ("parenthesized initialization is not allowed in "
44023 "OpenMP %<for%> loop");
44024 else
44025 /* Trigger an error. */
44026 cp_parser_require (parser, CPP_EQ, RT_EQ);
44028 init = error_mark_node;
44029 cp_parser_skip_to_end_of_statement (parser);
44031 else if (CLASS_TYPE_P (TREE_TYPE (decl))
44032 || type_dependent_expression_p (decl)
44033 || auto_node)
44035 bool is_non_constant_init;
44037 init = cp_parser_initializer (parser,
44038 /*is_direct_init=*/nullptr,
44039 &is_non_constant_init);
44041 if (auto_node)
44043 TREE_TYPE (decl)
44044 = do_auto_deduction (TREE_TYPE (decl), init,
44045 auto_node);
44047 if (!CLASS_TYPE_P (TREE_TYPE (decl))
44048 && !type_dependent_expression_p (decl))
44049 goto non_class;
44052 cp_finish_decl (decl, init, !is_non_constant_init,
44053 asm_specification,
44054 LOOKUP_ONLYCONVERTING);
44055 orig_init = init;
44057 /* In the case of a class iterator, do not pop sl here.
44058 Both class initialization and finalization must happen in
44059 the enclosing init block scope. For now set the init
44060 expression to null; it'll be filled in properly in
44061 finish_omp_for before stuffing it in the OMP_FOR. */
44062 if (CLASS_TYPE_P (TREE_TYPE (decl)))
44063 init = NULL_TREE;
44064 else /* It is a parameterized type. */
44066 init = pop_stmt_list (sl);
44067 sl = NULL_TREE;
44068 if (init && TREE_CODE (init) == STATEMENT_LIST)
44070 tree_stmt_iterator i = tsi_start (init);
44071 /* Move lambda DECL_EXPRs to the enclosing block. */
44072 while (!tsi_end_p (i))
44074 tree t = tsi_stmt (i);
44075 if (TREE_CODE (t) == DECL_EXPR
44076 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
44078 tsi_delink (&i);
44079 add_stmt (t);
44080 continue;
44082 break;
44084 if (tsi_one_before_end_p (i))
44086 tree t = tsi_stmt (i);
44087 tsi_delink (&i);
44088 free_stmt_list (init);
44089 init = t;
44094 else
44095 /* This is an initialized declaration of non-class,
44096 non-parameterized type iteration variable. */
44098 /* Consume '='. */
44099 cp_lexer_consume_token (parser->lexer);
44100 init = cp_parser_assignment_expression (parser);
44102 non_class:
44103 if (TYPE_REF_P (TREE_TYPE (decl)))
44104 init = error_mark_node;
44105 else
44106 cp_finish_decl (decl, NULL_TREE,
44107 /*init_const_expr_p=*/false,
44108 asm_specification,
44109 LOOKUP_ONLYCONVERTING);
44110 this_pre_body = pop_stmt_list (sl);
44111 sl = NULL_TREE;
44114 if (pushed_scope)
44115 pop_scope (pushed_scope);
44118 else
44120 cp_id_kind idk;
44121 /* If parsing a type specifier sequence failed, then
44122 this MUST be a simple expression. */
44123 cp_parser_parse_tentatively (parser);
44124 decl = cp_parser_primary_expression (parser, false, false,
44125 false, &idk);
44126 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
44127 if (!cp_parser_error_occurred (parser)
44128 && decl
44129 && (TREE_CODE (decl) == COMPONENT_REF
44130 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
44132 cp_parser_abort_tentative_parse (parser);
44133 cp_parser_parse_tentatively (parser);
44134 cp_token *token = cp_lexer_peek_token (parser->lexer);
44135 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
44136 /*check_dependency_p=*/true,
44137 /*template_p=*/NULL,
44138 /*declarator_p=*/false,
44139 /*optional_p=*/false);
44140 if (name != error_mark_node
44141 && last_tok == cp_lexer_peek_token (parser->lexer))
44143 decl = cp_parser_lookup_name_simple (parser, name,
44144 token->location);
44145 if (TREE_CODE (decl) == FIELD_DECL)
44146 add_private_clause = omp_privatize_field (decl, false);
44148 cp_parser_abort_tentative_parse (parser);
44149 cp_parser_parse_tentatively (parser);
44150 decl = cp_parser_primary_expression (parser, false, false,
44151 false, &idk);
44153 if (!cp_parser_error_occurred (parser)
44154 && decl
44155 && DECL_P (decl)
44156 && CLASS_TYPE_P (TREE_TYPE (decl)))
44158 tree rhs;
44160 cp_parser_parse_definitely (parser);
44161 cp_parser_require (parser, CPP_EQ, RT_EQ);
44162 rhs = cp_parser_assignment_expression (parser);
44163 orig_init = rhs;
44164 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
44165 decl, NOP_EXPR,
44166 rhs, NULL_TREE,
44167 tf_warning_or_error));
44168 if (!add_private_clause)
44169 add_private_clause = decl;
44171 else
44173 decl = NULL;
44174 cp_parser_abort_tentative_parse (parser);
44175 init = cp_parser_expression (parser);
44176 if (init)
44178 if (TREE_CODE (init) == MODIFY_EXPR
44179 || TREE_CODE (init) == MODOP_EXPR)
44180 real_decl = TREE_OPERAND (init, 0);
44183 this_pre_body = pop_stmt_list (sl);
44184 sl = NULL_TREE;
44186 return add_private_clause;
44189 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
44190 including introducing new temporaries for the range start and end,
44191 doing auto deduction, and processing decomposition variables.
44193 This function is also called from pt.cc during template instantiation.
44194 In that case SL is NULL_TREE, otherwise it is the current statement
44195 list. */
44196 void
44197 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
44198 tree &decl, tree &orig_decl, tree &init,
44199 tree &orig_init, tree &cond, tree &incr)
44201 tree begin, end, range_temp_decl = NULL_TREE;
44202 tree iter_type, begin_expr, end_expr;
44203 bool clear_has_value_expr = false;
44205 if (processing_template_decl)
44207 if (check_for_bare_parameter_packs (init))
44208 init = error_mark_node;
44209 if (!type_dependent_expression_p (init)
44210 /* do_auto_deduction doesn't mess with template init-lists. */
44211 && !BRACE_ENCLOSED_INITIALIZER_P (init))
44213 tree d = decl;
44214 cp_decomp decomp_d, *decomp = NULL;
44215 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
44217 tree v = DECL_VALUE_EXPR (decl);
44218 if (TREE_CODE (v) == ARRAY_REF
44219 && VAR_P (TREE_OPERAND (v, 0))
44220 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44222 d = TREE_OPERAND (v, 0);
44223 decomp = &decomp_d;
44224 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44225 decomp->decl = decl;
44228 do_range_for_auto_deduction (d, init, decomp);
44230 cond = global_namespace;
44231 incr = NULL_TREE;
44232 orig_init = init;
44233 if (sl)
44235 this_pre_body = pop_stmt_list (sl);
44236 sl = NULL_TREE;
44238 return;
44241 init = mark_lvalue_use (init);
44243 if (decl == error_mark_node || init == error_mark_node)
44244 /* If an error happened previously do nothing or else a lot of
44245 unhelpful errors would be issued. */
44246 begin_expr = end_expr = iter_type = error_mark_node;
44247 else
44249 tree range_temp;
44251 if (VAR_P (init)
44252 && array_of_runtime_bound_p (TREE_TYPE (init)))
44253 /* Can't bind a reference to an array of runtime bound. */
44254 range_temp = init;
44255 else
44257 range_temp = build_range_temp (init);
44258 DECL_NAME (range_temp) = NULL_TREE;
44259 pushdecl (range_temp);
44260 cp_finish_decl (range_temp, init,
44261 /*is_constant_init*/false, NULL_TREE,
44262 LOOKUP_ONLYCONVERTING);
44263 range_temp_decl = range_temp;
44264 range_temp = convert_from_reference (range_temp);
44266 iter_type = cp_parser_perform_range_for_lookup (range_temp,
44267 &begin_expr, &end_expr);
44270 tree end_iter_type = iter_type;
44271 if (cxx_dialect >= cxx17)
44272 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
44273 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
44274 TREE_USED (end) = 1;
44275 DECL_ARTIFICIAL (end) = 1;
44276 pushdecl (end);
44277 cp_finish_decl (end, end_expr,
44278 /*is_constant_init*/false, NULL_TREE,
44279 LOOKUP_ONLYCONVERTING);
44281 /* The new for initialization statement. */
44282 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
44283 TREE_USED (begin) = 1;
44284 DECL_ARTIFICIAL (begin) = 1;
44285 pushdecl (begin);
44286 orig_init = init;
44287 if (CLASS_TYPE_P (iter_type))
44288 init = NULL_TREE;
44289 else
44291 init = begin_expr;
44292 begin_expr = NULL_TREE;
44294 cp_finish_decl (begin, begin_expr,
44295 /*is_constant_init*/false, NULL_TREE,
44296 LOOKUP_ONLYCONVERTING);
44298 /* The new for condition. */
44299 if (CLASS_TYPE_P (iter_type))
44300 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
44301 else
44302 cond = build_x_binary_op (input_location, NE_EXPR,
44303 begin, ERROR_MARK,
44304 end, ERROR_MARK,
44305 NULL_TREE, NULL, tf_warning_or_error);
44307 /* The new increment expression. */
44308 if (CLASS_TYPE_P (iter_type))
44309 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
44310 else
44311 incr = finish_unary_op_expr (input_location,
44312 PREINCREMENT_EXPR, begin,
44313 tf_warning_or_error);
44315 orig_decl = decl;
44316 decl = begin;
44317 /* Defer popping sl here. */
44319 cp_decomp decomp_d, *decomp = NULL;
44320 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
44322 tree v = DECL_VALUE_EXPR (orig_decl);
44323 if (TREE_CODE (v) == ARRAY_REF
44324 && VAR_P (TREE_OPERAND (v, 0))
44325 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44327 tree d = orig_decl;
44328 orig_decl = TREE_OPERAND (v, 0);
44329 decomp = &decomp_d;
44330 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44331 decomp->decl = d;
44335 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
44336 if (auto_node)
44338 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44339 NULL_TREE, tf_none);
44340 if (!error_operand_p (t))
44342 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
44343 t, auto_node);
44344 if (decomp)
44346 ++processing_template_decl;
44347 cp_finish_decomp (orig_decl, decomp);
44348 --processing_template_decl;
44349 if (!processing_template_decl)
44350 clear_has_value_expr = true;
44355 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
44356 that holds decls for variables implementing the iterator, represented
44357 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
44358 of the vector are decls of scratch variables for the range start and
44359 end that will eventually be bound in the implicit scope surrounding
44360 the whole loop nest. The remaining elements are decls of derived
44361 decomposition variables that are bound inside the loop body. This
44362 structure is further mangled by finish_omp_for into the form required
44363 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
44364 unsigned decomp_cnt = decomp ? decomp->count : 0;
44365 tree v = make_tree_vec (decomp_cnt + 3);
44366 TREE_VEC_ELT (v, 0) = range_temp_decl;
44367 TREE_VEC_ELT (v, 1) = end;
44368 TREE_VEC_ELT (v, 2) = orig_decl;
44369 if (clear_has_value_expr)
44370 TREE_PUBLIC (v) = 1;
44371 for (unsigned i = 0; i < decomp_cnt; i++)
44373 if (clear_has_value_expr)
44375 /* If cp_finish_decomp was called with processing_template_decl
44376 temporarily set to 1, then decomp names will have deduced
44377 name but the DECL_VALUE_EXPR will be dependent. Hide those
44378 from folding of other loop initializers e.g. for warning
44379 purposes until cp_finish_omp_range_for. */
44380 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
44381 || (TREE_TYPE (decomp->decl)
44382 == error_mark_node));
44383 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
44385 TREE_VEC_ELT (v, i + 3) = decomp->decl;
44386 decomp->decl = DECL_CHAIN (decomp->decl);
44388 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
44391 /* Helper for cp_parser_omp_for_loop, finalize part of range for
44392 inside of the collapsed body. */
44394 void
44395 cp_finish_omp_range_for (tree orig, tree begin)
44397 gcc_assert (TREE_CODE (orig) == TREE_LIST
44398 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
44399 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
44400 cp_decomp decomp_d, *decomp = NULL;
44402 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44404 decomp = &decomp_d;
44405 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
44406 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
44407 if (TREE_PUBLIC (TREE_CHAIN (orig)))
44409 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
44410 by cp_convert_omp_range_for above. */
44411 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
44412 tree d = decomp_d.decl;
44413 for (unsigned i = 0; i < decomp_d.count; i++)
44415 if (TREE_TYPE (d) != error_mark_node)
44416 DECL_HAS_VALUE_EXPR_P (d) = 1;
44417 d = DECL_CHAIN (d);
44422 /* The declaration is initialized with *__begin inside the loop body. */
44423 cp_finish_decl (decl,
44424 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44425 NULL_TREE, tf_warning_or_error),
44426 /*is_constant_init*/false, NULL_TREE,
44427 LOOKUP_ONLYCONVERTING, decomp);
44428 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44429 cp_finish_decomp (decl, decomp);
44432 /* Return true if next tokens contain a standard attribute that contains
44433 omp::directive (DIRECTIVE). */
44435 static bool
44436 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
44437 bool tentative)
44439 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
44440 if (n < 10)
44441 return false;
44442 for (i = 5; i < n - 4; i++)
44443 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
44444 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
44445 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
44447 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
44448 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
44449 if (strcmp (IDENTIFIER_POINTER (first), "directive")
44450 && strcmp (IDENTIFIER_POINTER (first), "__directive__"))
44451 continue;
44452 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
44453 break;
44455 if (i == n - 4)
44456 return false;
44457 cp_parser_parse_tentatively (parser);
44458 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
44459 location_t last_loc
44460 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
44461 location_t middle_loc = UNKNOWN_LOCATION;
44462 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
44463 int cnt = 0;
44464 bool seen = false;
44465 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
44466 if (get_attribute_namespace (attr) == omp_identifier
44467 && is_attribute_p ("directive", get_attribute_name (attr)))
44469 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
44471 tree d = TREE_VALUE (a);
44472 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
44473 cp_token *first = DEFPARSE_TOKENS (d)->first;
44474 cnt++;
44475 if (first->type == CPP_NAME
44476 && strcmp (IDENTIFIER_POINTER (first->u.value),
44477 directive) == 0)
44479 seen = true;
44480 if (middle_loc == UNKNOWN_LOCATION)
44481 middle_loc = first->location;
44485 if (!seen || tentative)
44487 cp_parser_abort_tentative_parse (parser);
44488 return seen;
44490 if (cnt != 1 || TREE_CHAIN (std_attrs))
44492 error_at (make_location (first_loc, last_loc, middle_loc),
44493 "%<[[omp::directive(%s)]]%> must be the only specified "
44494 "attribute on a statement", directive);
44495 cp_parser_abort_tentative_parse (parser);
44496 return false;
44498 if (!cp_parser_parse_definitely (parser))
44499 return false;
44500 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
44501 return true;
44504 /* Parse an OpenMP structured block sequence. KIND is the corresponding
44505 separating directive. */
44507 static tree
44508 cp_parser_omp_structured_block_sequence (cp_parser *parser,
44509 enum pragma_kind kind)
44511 tree stmt = begin_omp_structured_block ();
44512 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44514 cp_parser_statement (parser, NULL_TREE, false, NULL);
44515 while (true)
44517 cp_token *token = cp_lexer_peek_token (parser->lexer);
44519 if (token->type == CPP_CLOSE_BRACE
44520 || token->type == CPP_EOF
44521 || token->type == CPP_PRAGMA_EOL
44522 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
44523 || (kind != PRAGMA_NONE
44524 && cp_parser_pragma_kind (token) == kind))
44525 break;
44527 if (kind != PRAGMA_NONE
44528 && cp_parser_omp_section_scan (parser,
44529 kind == PRAGMA_OMP_SCAN
44530 ? "scan" : "section", false))
44531 break;
44533 cp_parser_statement (parser, NULL_TREE, false, NULL);
44536 cp_parser_end_omp_structured_block (parser, save);
44537 return finish_omp_structured_block (stmt);
44541 /* OpenMP 5.0:
44543 scan-loop-body:
44544 { structured-block scan-directive structured-block } */
44546 static void
44547 cp_parser_omp_scan_loop_body (cp_parser *parser)
44549 tree substmt, clauses = NULL_TREE;
44550 bool found_scan = false;
44552 matching_braces braces;
44553 if (!braces.require_open (parser))
44554 return;
44556 cp_token *tok = cp_lexer_peek_token (parser->lexer);
44557 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
44558 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
44559 else
44561 warning_at (tok->location, OPT_Wopenmp,
44562 "%<#pragma omp scan%> with zero preceding executable "
44563 "statements");
44564 substmt = build_empty_stmt (tok->location);
44566 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
44567 add_stmt (substmt);
44569 tok = cp_lexer_peek_token (parser->lexer);
44570 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
44572 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
44573 found_scan = true;
44575 cp_lexer_consume_token (parser->lexer);
44577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44578 cp_lexer_consume_token (parser->lexer);
44580 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44582 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44583 const char *p = IDENTIFIER_POINTER (id);
44584 if (strcmp (p, "inclusive") == 0)
44585 clause = OMP_CLAUSE_INCLUSIVE;
44586 else if (strcmp (p, "exclusive") == 0)
44587 clause = OMP_CLAUSE_EXCLUSIVE;
44589 if (clause != OMP_CLAUSE_ERROR)
44591 cp_lexer_consume_token (parser->lexer);
44592 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
44594 else
44595 cp_parser_error (parser, "expected %<inclusive%> or "
44596 "%<exclusive%> clause");
44598 cp_parser_require_pragma_eol (parser, tok);
44600 else
44601 error ("expected %<#pragma omp scan%>");
44603 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
44604 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44605 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
44606 else
44608 if (found_scan)
44609 warning_at (tok->location, OPT_Wopenmp,
44610 "%<#pragma omp scan%> with zero succeeding executable "
44611 "statements");
44612 substmt = build_empty_stmt (tok->location);
44614 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
44615 clauses);
44616 add_stmt (substmt);
44618 braces.require_close (parser);
44622 /* This function parses a single level of a loop nest, invoking itself
44623 recursively if necessary.
44625 loop-nest :: for (...) loop-body
44626 loop-body :: loop-nest
44627 | { [intervening-code] loop-body [intervening-code] }
44628 | final-loop-body
44629 intervening-code :: structured-block-sequence
44630 final-loop-body :: structured-block
44632 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
44633 all the iterator information from the inner loops into vectors in the
44634 parser->omp_for_parse_state structure.
44636 In the "range for" case, it is transformed into a regular "for" iterator
44637 by introducing some temporary variables for the begin/end,
44638 as well as bindings of the actual iteration variables which are
44639 injected into the body of the loop.
44641 Initialization code for iterator variables may end up either in the
44642 init vector (simple assignments), in omp_for_parse_state->pre_body
44643 (decl_exprs for iterators bound in the for statement), or in the
44644 scope surrounding this level of loop initialization.
44646 The scopes of class iterator variables and their finalizers need to
44647 be adjusted after parsing so that all of the initialization happens
44648 in a scope surrounding all of the intervening and body code. For
44649 this reason we separately store the initialization and body blocks
44650 for each level of loops in the omp_for_parse_state structure and
44651 reassemble/reorder them in cp_parser_omp_for. See additional
44652 comments there about the use of placeholders, etc. */
44654 static tree
44655 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
44657 tree decl, cond, incr, init;
44658 tree orig_init, real_decl, orig_decl;
44659 tree init_block, body_block;
44660 tree init_placeholder, body_placeholder;
44661 tree init_scope;
44662 tree this_pre_body = NULL_TREE;
44663 bool moreloops;
44664 unsigned char save_in_statement;
44665 tree add_private_clause = NULL_TREE;
44666 location_t loc;
44667 bool is_range_for = false;
44668 tree sl = NULL_TREE;
44669 struct omp_for_parse_data *omp_for_parse_state
44670 = parser->omp_for_parse_state;
44671 gcc_assert (omp_for_parse_state);
44672 int depth = omp_for_parse_state->depth;
44674 /* We have already matched the FOR token but not consumed it yet. */
44675 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
44676 loc = cp_lexer_consume_token (parser->lexer)->location;
44678 /* Forbid break/continue in the loop initializer, condition, and
44679 increment expressions. */
44680 save_in_statement = parser->in_statement;
44681 parser->in_statement = IN_OMP_BLOCK;
44683 /* We are not in intervening code now. */
44684 omp_for_parse_state->in_intervening_code = false;
44686 /* Don't create location wrapper nodes within an OpenMP "for"
44687 statement. */
44688 auto_suppress_location_wrappers sentinel;
44690 matching_parens parens;
44691 if (!parens.require_open (parser))
44692 return NULL;
44694 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44696 init_placeholder = build_stmt (input_location, EXPR_STMT,
44697 integer_zero_node);
44698 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44700 /* The init_block acts as a container for this level of loop goo. */
44701 init_block = push_stmt_list ();
44702 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44704 /* Wrap a scope around this entire level of loop to hold bindings
44705 of loop iteration variables. We can't insert them directly
44706 in the containing scope because that would cause their visibility to
44707 be incorrect with respect to intervening code after this loop.
44708 We will combine the nested init_scopes in postprocessing after the
44709 entire loop is parsed. */
44710 init_scope = begin_compound_stmt (0);
44712 /* Now we need another level of statement list container to capture the
44713 initialization (and possible finalization) bits. In some cases this
44714 container may be popped off during initializer parsing to store code in
44715 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44716 we have a class iterator we will pop it at the end of parsing this
44717 level, so the cleanups are handled correctly. */
44718 sl = push_stmt_list ();
44720 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44722 /* Save tokens so that we can put them back. */
44723 cp_lexer_save_tokens (parser->lexer);
44725 /* Look for ':' that is not nested in () or {}. */
44726 is_range_for
44727 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44728 /*recovering=*/false,
44729 CPP_COLON,
44730 /*consume_paren=*/
44731 false) == -1);
44733 /* Roll back the tokens we skipped. */
44734 cp_lexer_rollback_tokens (parser->lexer);
44736 if (is_range_for)
44738 bool saved_colon_corrects_to_scope_p
44739 = parser->colon_corrects_to_scope_p;
44741 /* A colon is used in range-based for. */
44742 parser->colon_corrects_to_scope_p = false;
44744 /* Parse the declaration. */
44745 cp_parser_simple_declaration (parser,
44746 /*function_definition_allowed_p=*/
44747 false, &decl);
44748 parser->colon_corrects_to_scope_p
44749 = saved_colon_corrects_to_scope_p;
44751 cp_parser_require (parser, CPP_COLON, RT_COLON);
44753 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44754 false, NULL_TREE, false, true);
44756 cp_convert_omp_range_for (this_pre_body, sl, decl,
44757 orig_decl, init, orig_init,
44758 cond, incr);
44760 if (omp_for_parse_state->ordered_cl)
44761 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44762 "%<ordered%> clause with parameter on "
44763 "range-based %<for%> loop");
44765 goto parse_close_paren;
44769 add_private_clause
44770 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44771 init, orig_init, decl, real_decl);
44773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44775 /* If the iteration variable was introduced via a declaration in the
44776 for statement, DECL points at it. Otherwise DECL is null and
44777 REAL_DECL is a variable previously declared in an outer scope.
44778 Make REAL_DECL point at the iteration variable no matter where it
44779 was introduced. */
44780 if (decl)
44781 real_decl = decl;
44783 /* Some clauses treat iterator variables specially. */
44784 if (omp_for_parse_state->cclauses != NULL
44785 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44786 && real_decl != NULL_TREE
44787 && omp_for_parse_state->code != OMP_LOOP)
44789 tree *c;
44790 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44791 *c ; )
44792 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44793 && OMP_CLAUSE_DECL (*c) == real_decl)
44795 error_at (loc, "iteration variable %qD"
44796 " should not be firstprivate", real_decl);
44797 *c = OMP_CLAUSE_CHAIN (*c);
44799 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44800 && OMP_CLAUSE_DECL (*c) == real_decl)
44802 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44803 tree l = *c;
44804 *c = OMP_CLAUSE_CHAIN (*c);
44805 if (omp_for_parse_state->code == OMP_SIMD)
44807 OMP_CLAUSE_CHAIN (l)
44808 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44809 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44811 else
44813 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
44814 omp_for_parse_state->clauses = l;
44816 add_private_clause = NULL_TREE;
44818 else
44820 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
44821 && OMP_CLAUSE_DECL (*c) == real_decl)
44822 add_private_clause = NULL_TREE;
44823 c = &OMP_CLAUSE_CHAIN (*c);
44827 if (add_private_clause)
44829 tree c;
44830 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
44832 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
44833 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
44834 && OMP_CLAUSE_DECL (c) == decl)
44835 break;
44836 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
44837 && OMP_CLAUSE_DECL (c) == decl)
44838 error_at (loc, "iteration variable %qD "
44839 "should not be firstprivate",
44840 decl);
44841 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
44842 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44843 && OMP_CLAUSE_DECL (c) == decl)
44844 error_at (loc, "iteration variable %qD should not be reduction",
44845 decl);
44847 if (c == NULL)
44849 if ((omp_for_parse_state->code == OMP_SIMD
44850 && omp_for_parse_state->count != 1)
44851 || omp_for_parse_state->code == OMP_LOOP)
44852 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
44853 else if (omp_for_parse_state->code != OMP_SIMD)
44854 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
44855 else
44856 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
44857 OMP_CLAUSE_DECL (c) = add_private_clause;
44858 c = finish_omp_clauses (c, C_ORT_OMP);
44859 if (c)
44861 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
44862 omp_for_parse_state->clauses = c;
44863 /* For linear, signal that we need to fill up
44864 the so far unknown linear step. */
44865 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
44866 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
44871 cond = NULL;
44872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44873 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
44874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44876 incr = NULL;
44877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
44879 /* If decl is an iterator, preserve the operator on decl
44880 until finish_omp_for. */
44881 if (real_decl
44882 && ((processing_template_decl
44883 && (TREE_TYPE (real_decl) == NULL_TREE
44884 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
44885 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
44886 incr = cp_parser_omp_for_incr (parser, real_decl);
44887 else
44888 incr = cp_parser_expression (parser);
44889 protected_set_expr_location_if_unset (incr, input_location);
44892 parse_close_paren:
44893 if (!parens.require_close (parser))
44894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44895 /*or_comma=*/false,
44896 /*consume_paren=*/true);
44898 /* We've parsed all the for (...) stuff now. Store the bits. */
44899 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
44900 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
44901 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
44902 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
44903 if (orig_init)
44905 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
44906 omp_for_parse_state->orig_inits[depth] = orig_init;
44908 if (orig_decl)
44910 if (!omp_for_parse_state->orig_declv)
44911 omp_for_parse_state->orig_declv
44912 = copy_node (omp_for_parse_state->declv);
44913 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
44915 else if (omp_for_parse_state->orig_declv)
44916 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
44917 if (this_pre_body)
44918 append_to_statement_list_force (this_pre_body,
44919 &(omp_for_parse_state->pre_body));
44921 /* Start a nested block for the loop body. */
44922 body_placeholder = build_stmt (input_location, EXPR_STMT,
44923 integer_zero_node);
44924 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
44925 body_block = push_stmt_list ();
44926 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
44928 moreloops = depth < omp_for_parse_state->count - 1;
44929 omp_for_parse_state->want_nested_loop = moreloops;
44930 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44932 omp_for_parse_state->depth++;
44933 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
44934 omp_for_parse_state->depth--;
44936 else if (moreloops
44937 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44939 /* This is the open brace in the loop-body grammar production. Rather
44940 than trying to special-case braces, just parse it as a compound
44941 statement and handle the nested loop-body case there. Note that
44942 when we see a further open brace inside the compound statement
44943 loop-body, we don't know whether it is the start of intervening
44944 code that is a compound statement, or a level of braces
44945 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
44946 bit to ensure we have only one nested loop at each level. */
44948 omp_for_parse_state->in_intervening_code = true;
44949 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
44950 omp_for_parse_state->in_intervening_code = false;
44952 if (omp_for_parse_state->want_nested_loop)
44954 /* We have already parsed the whole loop body and not found a
44955 nested loop. */
44956 error_at (omp_for_parse_state->for_loc,
44957 "not enough nested loops");
44958 omp_for_parse_state->fail = true;
44960 if_p = NULL;
44962 else
44964 /* This is the final-loop-body case in the grammar: we have something
44965 that is not a FOR and not an open brace. */
44966 if (moreloops)
44968 /* If we were expecting a nested loop, give an error and mark
44969 that parsing has failed, and try to recover by parsing the
44970 body as regular code without further collapsing. */
44971 error_at (omp_for_parse_state->for_loc,
44972 "not enough nested loops");
44973 omp_for_parse_state->fail = true;
44975 parser->in_statement = IN_OMP_FOR;
44977 /* Generate the parts of range for that belong in the loop body,
44978 to be executed on every iteration. This includes setting the
44979 user-declared decomposition variables from the compiler-generated
44980 temporaries that are the real iteration variables for OMP_FOR.
44981 FIXME: Not sure if this is correct with respect to visibility
44982 of the variables from intervening code. However, putting this
44983 code in each level of loop instead of all around the innermost
44984 body also makes the decomposition variables visible to the
44985 inner for init/bound/step exressions, which is not supposed to
44986 happen and causes test failures. */
44987 if (omp_for_parse_state->orig_declv)
44988 for (int i = 0; i < omp_for_parse_state->count; i++)
44990 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
44991 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
44992 if (o != d)
44993 cp_finish_omp_range_for (o, d);
44996 /* Now parse the final-loop-body for the innermost loop. */
44997 parser->omp_for_parse_state = NULL;
44998 if (omp_for_parse_state->inscan)
44999 cp_parser_omp_scan_loop_body (parser);
45000 else
45001 cp_parser_statement (parser, NULL_TREE, false, if_p);
45002 parser->omp_for_parse_state = omp_for_parse_state;
45004 parser->in_statement = save_in_statement;
45005 omp_for_parse_state->want_nested_loop = false;
45006 omp_for_parse_state->in_intervening_code = true;
45008 /* Pop and remember the body block. Add the body placeholder
45009 to the surrounding statement list instead. This is just a unique
45010 token that will be replaced when we reassemble the generated
45011 code for the entire omp for statement. */
45012 body_block = pop_stmt_list (body_block);
45013 omp_for_parse_state->body_blockv[depth] = body_block;
45014 add_stmt (body_placeholder);
45016 /* Pop and remember the init block. */
45017 if (sl)
45018 add_stmt (pop_stmt_list (sl));
45019 finish_compound_stmt (init_scope);
45020 init_block = pop_stmt_list (init_block);
45021 omp_for_parse_state->init_blockv[depth] = init_block;
45023 /* Return the init placeholder rather than the remembered init block.
45024 Again, this is just a unique cookie that will be used to reassemble
45025 code pieces when the entire omp for statement has been parsed. */
45026 return init_placeholder;
45029 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
45030 and ITER is the element that is or contains a nested loop. This
45031 function moves the statements before and after ITER into
45032 OMP_STRUCTURED_BLOCKs and modifies *TP. */
45033 static void
45034 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
45036 tree sl = push_stmt_list ();
45037 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
45038 if (i == iter)
45040 sl = pop_stmt_list (sl);
45041 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45042 tsi_link_before (&i,
45043 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45044 TSI_SAME_STMT);
45045 i++;
45046 sl = push_stmt_list ();
45048 else
45050 tree s = tsi_stmt (i);
45051 tsi_delink (&i); /* Advances i to next statement. */
45052 add_stmt (s);
45054 sl = pop_stmt_list (sl);
45055 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45056 tsi_link_after (&iter,
45057 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45058 TSI_SAME_STMT);
45061 /* Helper to find and mark structured blocks in intervening code for a
45062 single loop level with markers for later error checking. *TP is the
45063 piece of code to be marked and INNER is the inner loop placeholder.
45064 Returns true if INNER was found (recursively) in *TP. */
45065 static bool
45066 find_structured_blocks (tree *tp, tree inner)
45068 if (*tp == inner)
45069 return true;
45070 else if (TREE_CODE (*tp) == BIND_EXPR)
45071 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
45072 else if (TREE_CODE (*tp) == STATEMENT_LIST)
45074 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
45076 tree *p = tsi_stmt_ptr (i);
45077 /* The normal case is that there is no intervening code and we
45078 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
45079 if (find_structured_blocks (p, inner))
45081 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
45082 insert_structured_blocks (tp, i);
45083 return true;
45086 return false;
45088 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
45089 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
45090 else if (TREE_CODE (*tp) == CLEANUP_STMT)
45091 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
45092 else
45093 return false;
45096 /* Helpers used for relinking tree structures: In tree rooted at
45097 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
45098 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
45100 struct sit_data {
45101 tree orig;
45102 tree repl;
45103 bool flatten;
45106 static tree
45107 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
45108 void *dp)
45110 struct sit_data *sit = (struct sit_data *)dp;
45111 if (*tp == sit->orig)
45113 *tp = sit->repl;
45114 return *tp;
45116 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
45117 trying to flatten. */
45118 else if (TREE_CODE (*tp) == BIND_EXPR
45119 && BIND_EXPR_BODY (*tp) == sit->orig
45120 && !BIND_EXPR_VARS (*tp)
45121 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
45123 *tp = sit->repl;
45124 return *tp;
45126 else if (sit->flatten
45127 && TREE_CODE (*tp) == BIND_EXPR
45128 && TREE_CODE (sit->repl) == BIND_EXPR)
45130 if (BIND_EXPR_BODY (*tp) == sit->orig)
45132 /* Merge binding lists for two directly nested BIND_EXPRs,
45133 keeping the outer one. */
45134 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45135 BIND_EXPR_VARS (sit->repl));
45136 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
45137 return *tp;
45139 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
45140 /* There might be a statement list containing cleanup_points
45141 etc between the two levels of BIND_EXPR. We can still merge
45142 them, again keeping the outer BIND_EXPR. */
45143 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
45144 !tsi_end_p (i); ++i)
45146 tree *p = tsi_stmt_ptr (i);
45147 if (*p == sit->orig)
45149 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45150 BIND_EXPR_VARS (sit->repl));
45151 *p = BIND_EXPR_BODY (sit->repl);
45152 return *tp;
45156 return NULL;
45159 static void
45160 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
45162 struct sit_data data;
45164 gcc_assert (*context && orig && repl);
45165 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
45166 repl = BIND_EXPR_BODY (repl);
45167 data.orig = orig;
45168 data.repl = repl;
45169 data.flatten = flatten;
45171 tree result = cp_walk_tree (context, substitute_in_tree_walker,
45172 (void *)&data, NULL);
45173 gcc_assert (result != NULL_TREE);
45176 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
45177 *DP is is the parent block. */
45179 static tree
45180 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
45182 tree superblock = *(tree *)dp;
45184 /* BIND_EXPR_BLOCK may be null if the expression is not a
45185 full-expression; if there's no block, no patching is necessary
45186 for this node. */
45187 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
45189 tree block = BIND_EXPR_BLOCK (*tp);
45190 if (superblock)
45192 BLOCK_SUPERCONTEXT (block) = superblock;
45193 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
45194 BLOCK_SUBBLOCKS (superblock) = block;
45196 BLOCK_SUBBLOCKS (block) = NULL_TREE;
45197 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
45198 (void *)&block, NULL);
45199 *walk_subtrees = 0;
45202 return NULL;
45205 /* Parse the restricted form of the for statement allowed by OpenMP. */
45207 static tree
45208 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
45209 tree *cclauses, bool *if_p)
45211 tree ret;
45212 tree cl, ordered_cl = NULL_TREE;
45213 int collapse = 1, ordered = 0;
45214 unsigned int count;
45215 bool tiling = false;
45216 bool inscan = false;
45217 struct omp_for_parse_data data;
45218 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
45219 tree result;
45220 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
45222 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
45223 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
45224 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
45225 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
45227 tiling = true;
45228 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
45230 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
45231 && OMP_CLAUSE_ORDERED_EXPR (cl))
45233 ordered_cl = cl;
45234 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
45236 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
45237 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
45238 && (code == OMP_SIMD || code == OMP_FOR))
45239 inscan = true;
45241 if (ordered && ordered < collapse)
45243 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
45244 "%<ordered%> clause parameter is less than %<collapse%>");
45245 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
45246 = build_int_cst (NULL_TREE, collapse);
45247 ordered = collapse;
45250 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
45251 count = ordered ? ordered : collapse;
45253 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45255 cp_parser_error (parser, "for statement expected");
45256 return NULL;
45259 /* Initialize parse state for recursive descent. */
45260 data.declv = make_tree_vec (count);
45261 data.initv = make_tree_vec (count);
45262 data.condv = make_tree_vec (count);
45263 data.incrv = make_tree_vec (count);
45264 data.pre_body = NULL_TREE;
45265 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
45266 data.count = count;
45267 data.depth = 0;
45268 data.want_nested_loop = true;
45269 data.ordered = ordered > 0;
45270 data.in_intervening_code = false;
45271 data.perfect_nesting_fail = false;
45272 data.fail = false;
45273 data.inscan = inscan;
45274 data.saw_intervening_code = false;
45275 data.code = code;
45276 data.orig_declv = NULL_TREE;
45277 data.clauses = clauses;
45278 data.cclauses = cclauses;
45279 data.ordered_cl = ordered_cl;
45280 parser->omp_for_parse_state = &data;
45282 cp_parser_omp_loop_nest (parser, if_p);
45284 /* Bomb out early if there was an error (not enough loops, etc). */
45285 if (data.fail || data.declv == NULL_TREE)
45287 parser->omp_for_parse_state = save_data;
45288 return NULL_TREE;
45291 /* Relink the init and body blocks that were built during parsing. At
45292 this point we have a structure nested like
45293 init 0
45294 body 0
45295 init 1
45296 body 1
45297 init 2
45298 body 2
45299 and we want to turn it into
45300 init 0
45301 init 1
45302 init 2
45303 omp_for
45304 body 0
45305 body 1
45306 body 2
45307 We also need to flatten the init blocks, as some code for later
45308 processing of combined directives gets confused otherwise. */
45310 gcc_assert (vec_safe_length (data.init_blockv) == count);
45311 gcc_assert (vec_safe_length (data.body_blockv) == count);
45312 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
45313 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
45315 /* First insert markers for structured blocks for intervening code in
45316 the loop bodies. */
45317 for (unsigned int i = 0; i < count - 1; i++)
45319 bool good = find_structured_blocks (&(data.body_blockv[i]),
45320 data.init_placeholderv[i+1]);
45321 gcc_assert (good);
45324 /* Do the substitution from the inside out. */
45325 for (unsigned int i = count - 1; i > 0; i--)
45327 substitute_in_tree (&(data.body_blockv[i-1]),
45328 data.init_placeholderv[i],
45329 data.body_blockv[i], false);
45330 substitute_in_tree (&(data.init_blockv[i-1]),
45331 data.body_placeholderv[i-1],
45332 data.init_blockv[i], true);
45335 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
45336 (and possibly other stuff) to the current statement list but
45337 returns a pointer to the OMP_FOR itself, or null in case of error. */
45338 result = push_stmt_list ();
45339 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
45340 data.initv, data.condv, data.incrv,
45341 data.body_blockv[0],
45342 data.pre_body, &data.orig_inits, data.clauses);
45343 result = pop_stmt_list (result);
45345 /* Check for errors involving lb/ub/incr expressions referencing
45346 variables declared in intervening code. */
45347 if (data.saw_intervening_code
45348 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
45349 ret = NULL_TREE;
45351 if (ret)
45353 /* Splice the omp_for into the nest of init blocks. */
45354 substitute_in_tree (&(data.init_blockv[0]),
45355 data.body_placeholderv[count - 1],
45356 result, true);
45358 /* Some later processing for combined directives assumes
45359 that the BIND_EXPR containing range for variables appears
45360 at top level in the OMP_FOR body. Fix that up if it's
45361 not the case, e.g. because there is intervening code. */
45362 if (code != OACC_LOOP)
45363 finish_omp_for_block (data.init_blockv[0], ret);
45365 /* Clean up the block subblock/superblock links. Per comment in
45366 begin_compound_stmt, "we don't build BLOCK nodes when processing
45367 templates", so skip this step in that case. */
45368 if (!processing_template_decl)
45370 tree superblock = NULL_TREE;
45371 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
45372 (void *)&superblock, NULL);
45375 /* Finally record the result. */
45376 add_stmt (data.init_blockv[0]);
45379 parser->omp_for_parse_state = save_data;
45380 return ret;
45383 /* Helper function for OpenMP parsing, split clauses and call
45384 finish_omp_clauses on each of the set of clauses afterwards. */
45386 static void
45387 cp_omp_split_clauses (location_t loc, enum tree_code code,
45388 omp_clause_mask mask, tree clauses, tree *cclauses)
45390 int i;
45391 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
45392 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
45393 if (cclauses[i])
45394 cclauses[i] = finish_omp_clauses (cclauses[i],
45395 i == C_OMP_CLAUSE_SPLIT_TARGET
45396 ? C_ORT_OMP_TARGET : C_ORT_OMP);
45399 /* OpenMP 5.0:
45400 #pragma omp loop loop-clause[optseq] new-line
45401 for-loop */
45403 #define OMP_LOOP_CLAUSE_MASK \
45404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
45409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45411 static tree
45412 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
45413 char *p_name, omp_clause_mask mask, tree *cclauses,
45414 bool *if_p)
45416 tree clauses, sb, ret;
45417 unsigned int save;
45418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45420 strcat (p_name, " loop");
45421 mask |= OMP_LOOP_CLAUSE_MASK;
45423 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45424 cclauses == NULL);
45425 if (cclauses)
45427 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
45428 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
45431 keep_next_level (true);
45432 sb = begin_omp_structured_block ();
45433 save = cp_parser_begin_omp_structured_block (parser);
45435 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
45437 cp_parser_end_omp_structured_block (parser, save);
45438 add_stmt (finish_omp_structured_block (sb));
45440 return ret;
45443 /* OpenMP 4.0:
45444 #pragma omp simd simd-clause[optseq] new-line
45445 for-loop */
45447 #define OMP_SIMD_CLAUSE_MASK \
45448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
45449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
45458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45460 static tree
45461 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
45462 char *p_name, omp_clause_mask mask, tree *cclauses,
45463 bool *if_p)
45465 tree clauses, sb, ret;
45466 unsigned int save;
45467 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45469 strcat (p_name, " simd");
45470 mask |= OMP_SIMD_CLAUSE_MASK;
45472 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45473 cclauses == NULL);
45474 if (cclauses)
45476 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
45477 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
45480 keep_next_level (true);
45481 sb = begin_omp_structured_block ();
45482 save = cp_parser_begin_omp_structured_block (parser);
45484 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
45486 cp_parser_end_omp_structured_block (parser, save);
45487 add_stmt (finish_omp_structured_block (sb));
45489 return ret;
45492 /* OpenMP 2.5:
45493 #pragma omp for for-clause[optseq] new-line
45494 for-loop
45496 OpenMP 4.0:
45497 #pragma omp for simd for-simd-clause[optseq] new-line
45498 for-loop */
45500 #define OMP_FOR_CLAUSE_MASK \
45501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
45507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
45508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45513 static tree
45514 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
45515 char *p_name, omp_clause_mask mask, tree *cclauses,
45516 bool *if_p)
45518 tree clauses, sb, ret;
45519 unsigned int save;
45520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45522 strcat (p_name, " for");
45523 mask |= OMP_FOR_CLAUSE_MASK;
45524 /* parallel for{, simd} disallows nowait clause, but for
45525 target {teams distribute ,}parallel for{, simd} it should be accepted. */
45526 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
45527 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45528 /* Composite distribute parallel for{, simd} disallows ordered clause. */
45529 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45530 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
45532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45535 const char *p = IDENTIFIER_POINTER (id);
45537 if (strcmp (p, "simd") == 0)
45539 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45540 if (cclauses == NULL)
45541 cclauses = cclauses_buf;
45543 cp_lexer_consume_token (parser->lexer);
45544 if (!flag_openmp) /* flag_openmp_simd */
45545 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45546 cclauses, if_p);
45547 sb = begin_omp_structured_block ();
45548 save = cp_parser_begin_omp_structured_block (parser);
45549 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45550 cclauses, if_p);
45551 cp_parser_end_omp_structured_block (parser, save);
45552 tree body = finish_omp_structured_block (sb);
45553 if (ret == NULL)
45554 return ret;
45555 ret = make_node (OMP_FOR);
45556 TREE_TYPE (ret) = void_type_node;
45557 OMP_FOR_BODY (ret) = body;
45558 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45559 SET_EXPR_LOCATION (ret, loc);
45560 add_stmt (ret);
45561 return ret;
45564 if (!flag_openmp) /* flag_openmp_simd */
45566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45567 return NULL_TREE;
45570 /* Composite distribute parallel for disallows linear clause. */
45571 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45572 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
45574 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45575 cclauses == NULL);
45576 if (cclauses)
45578 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
45579 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45582 keep_next_level (true);
45583 sb = begin_omp_structured_block ();
45584 save = cp_parser_begin_omp_structured_block (parser);
45586 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
45588 cp_parser_end_omp_structured_block (parser, save);
45589 add_stmt (finish_omp_structured_block (sb));
45591 return ret;
45594 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
45595 omp_clause_mask, tree *, bool *);
45597 /* OpenMP 2.5:
45598 # pragma omp master new-line
45599 structured-block */
45601 static tree
45602 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
45603 char *p_name, omp_clause_mask mask, tree *cclauses,
45604 bool *if_p)
45606 tree clauses, sb, ret;
45607 unsigned int save;
45608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45610 strcat (p_name, " master");
45612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45614 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45615 const char *p = IDENTIFIER_POINTER (id);
45617 if (strcmp (p, "taskloop") == 0)
45619 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45620 if (cclauses == NULL)
45621 cclauses = cclauses_buf;
45623 cp_lexer_consume_token (parser->lexer);
45624 if (!flag_openmp) /* flag_openmp_simd */
45625 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45626 cclauses, if_p);
45627 sb = begin_omp_structured_block ();
45628 save = cp_parser_begin_omp_structured_block (parser);
45629 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45630 cclauses, if_p);
45631 cp_parser_end_omp_structured_block (parser, save);
45632 tree body = finish_omp_structured_block (sb);
45633 if (ret == NULL)
45634 return ret;
45635 ret = c_finish_omp_master (loc, body);
45636 OMP_MASTER_COMBINED (ret) = 1;
45637 return ret;
45640 if (!flag_openmp) /* flag_openmp_simd */
45642 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45643 return NULL_TREE;
45646 if (cclauses)
45648 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45649 false);
45650 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45652 else
45653 cp_parser_require_pragma_eol (parser, pragma_tok);
45655 return c_finish_omp_master (loc,
45656 cp_parser_omp_structured_block (parser, if_p));
45659 /* OpenMP 5.1:
45660 # pragma omp masked masked-clauses new-line
45661 structured-block */
45663 #define OMP_MASKED_CLAUSE_MASK \
45664 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
45666 static tree
45667 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
45668 char *p_name, omp_clause_mask mask, tree *cclauses,
45669 bool *if_p)
45671 tree clauses, sb, ret;
45672 unsigned int save;
45673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45675 strcat (p_name, " masked");
45676 mask |= OMP_MASKED_CLAUSE_MASK;
45678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45681 const char *p = IDENTIFIER_POINTER (id);
45683 if (strcmp (p, "taskloop") == 0)
45685 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45686 if (cclauses == NULL)
45687 cclauses = cclauses_buf;
45689 cp_lexer_consume_token (parser->lexer);
45690 if (!flag_openmp) /* flag_openmp_simd */
45691 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45692 cclauses, if_p);
45693 sb = begin_omp_structured_block ();
45694 save = cp_parser_begin_omp_structured_block (parser);
45695 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45696 cclauses, if_p);
45697 cp_parser_end_omp_structured_block (parser, save);
45698 tree body = finish_omp_structured_block (sb);
45699 if (ret == NULL)
45700 return ret;
45701 ret = c_finish_omp_masked (loc, body,
45702 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45703 OMP_MASKED_COMBINED (ret) = 1;
45704 return ret;
45707 if (!flag_openmp) /* flag_openmp_simd */
45709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45710 return NULL_TREE;
45713 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45714 cclauses == NULL);
45715 if (cclauses)
45717 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45718 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45721 return c_finish_omp_masked (loc,
45722 cp_parser_omp_structured_block (parser, if_p),
45723 clauses);
45726 /* OpenMP 2.5:
45727 # pragma omp ordered new-line
45728 structured-block
45730 OpenMP 4.5:
45731 # pragma omp ordered ordered-clauses new-line
45732 structured-block */
45734 #define OMP_ORDERED_CLAUSE_MASK \
45735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45738 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45739 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45742 static bool
45743 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45744 enum pragma_context context, bool *if_p)
45746 location_t loc = pragma_tok->location;
45747 int n = 1;
45749 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45750 n = 2;
45752 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45754 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45755 const char *p = IDENTIFIER_POINTER (id);
45757 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45759 if (!flag_openmp) /* flag_openmp_simd */
45761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45762 return false;
45764 if (context == pragma_stmt)
45766 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45767 "%qs clause may only be used in compound "
45768 "statements", p);
45769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45770 return true;
45772 tree clauses
45773 = cp_parser_omp_all_clauses (parser,
45774 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45775 "#pragma omp ordered", pragma_tok);
45776 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45777 return false;
45781 tree clauses
45782 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45783 "#pragma omp ordered", pragma_tok);
45785 if (!flag_openmp /* flag_openmp_simd */
45786 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45787 return false;
45789 c_finish_omp_ordered (loc, clauses,
45790 cp_parser_omp_structured_block (parser, if_p));
45791 return true;
45794 /* OpenMP 2.5:
45796 section-scope:
45797 { section-sequence }
45799 section-sequence:
45800 section-directive[opt] structured-block
45801 section-sequence section-directive structured-block */
45803 static tree
45804 cp_parser_omp_sections_scope (cp_parser *parser)
45806 tree stmt, substmt;
45807 bool error_suppress = false;
45808 cp_token *tok;
45810 matching_braces braces;
45811 if (!braces.require_open (parser))
45812 return NULL_TREE;
45814 stmt = push_stmt_list ();
45816 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45817 != PRAGMA_OMP_SECTION
45818 && !cp_parser_omp_section_scan (parser, "section", true))
45820 substmt = cp_parser_omp_structured_block_sequence (parser,
45821 PRAGMA_OMP_SECTION);
45822 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45823 add_stmt (substmt);
45826 while (1)
45828 tok = cp_lexer_peek_token (parser->lexer);
45829 if (tok->type == CPP_CLOSE_BRACE)
45830 break;
45831 if (tok->type == CPP_EOF)
45832 break;
45834 if (cp_parser_omp_section_scan (parser, "section", false))
45835 tok = cp_lexer_peek_token (parser->lexer);
45836 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
45838 cp_lexer_consume_token (parser->lexer);
45839 cp_parser_require_pragma_eol (parser, tok);
45840 error_suppress = false;
45842 else if (!error_suppress)
45844 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
45845 error_suppress = true;
45848 substmt = cp_parser_omp_structured_block_sequence (parser,
45849 PRAGMA_OMP_SECTION);
45850 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45851 add_stmt (substmt);
45853 braces.require_close (parser);
45855 substmt = pop_stmt_list (stmt);
45857 stmt = make_node (OMP_SECTIONS);
45858 TREE_TYPE (stmt) = void_type_node;
45859 OMP_SECTIONS_BODY (stmt) = substmt;
45861 add_stmt (stmt);
45862 return stmt;
45865 /* OpenMP 2.5:
45866 # pragma omp sections sections-clause[optseq] newline
45867 sections-scope */
45869 #define OMP_SECTIONS_CLAUSE_MASK \
45870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45877 static tree
45878 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
45879 char *p_name, omp_clause_mask mask, tree *cclauses)
45881 tree clauses, ret;
45882 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45884 strcat (p_name, " sections");
45885 mask |= OMP_SECTIONS_CLAUSE_MASK;
45886 if (cclauses)
45887 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45890 cclauses == NULL);
45891 if (cclauses)
45893 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
45894 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
45897 ret = cp_parser_omp_sections_scope (parser);
45898 if (ret)
45899 OMP_SECTIONS_CLAUSES (ret) = clauses;
45901 return ret;
45904 /* OpenMP 2.5:
45905 # pragma omp parallel parallel-clause[optseq] new-line
45906 structured-block
45907 # pragma omp parallel for parallel-for-clause[optseq] new-line
45908 structured-block
45909 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
45910 structured-block
45912 OpenMP 4.0:
45913 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
45914 structured-block */
45916 #define OMP_PARALLEL_CLAUSE_MASK \
45917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
45923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
45925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
45928 static tree
45929 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
45930 char *p_name, omp_clause_mask mask, tree *cclauses,
45931 bool *if_p)
45933 tree stmt, clauses, block;
45934 unsigned int save;
45935 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45937 strcat (p_name, " parallel");
45938 mask |= OMP_PARALLEL_CLAUSE_MASK;
45939 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
45940 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
45941 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
45942 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
45944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45946 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45947 if (cclauses == NULL)
45948 cclauses = cclauses_buf;
45950 cp_lexer_consume_token (parser->lexer);
45951 if (!flag_openmp) /* flag_openmp_simd */
45952 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45953 if_p);
45954 block = begin_omp_parallel ();
45955 save = cp_parser_begin_omp_structured_block (parser);
45956 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45957 if_p);
45958 cp_parser_end_omp_structured_block (parser, save);
45959 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45960 block);
45961 if (ret == NULL_TREE)
45962 return ret;
45963 OMP_PARALLEL_COMBINED (stmt) = 1;
45964 return stmt;
45966 /* When combined with distribute, parallel has to be followed by for.
45967 #pragma omp target parallel is allowed though. */
45968 else if (cclauses
45969 && (mask & (OMP_CLAUSE_MASK_1
45970 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45972 error_at (loc, "expected %<for%> after %qs", p_name);
45973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45974 return NULL_TREE;
45976 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45978 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45979 const char *p = IDENTIFIER_POINTER (id);
45980 if (cclauses == NULL && strcmp (p, "masked") == 0)
45982 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45983 cclauses = cclauses_buf;
45985 cp_lexer_consume_token (parser->lexer);
45986 if (!flag_openmp) /* flag_openmp_simd */
45987 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45988 cclauses, if_p);
45989 block = begin_omp_parallel ();
45990 save = cp_parser_begin_omp_structured_block (parser);
45991 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45992 cclauses, if_p);
45993 cp_parser_end_omp_structured_block (parser, save);
45994 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45995 block);
45996 if (ret == NULL_TREE)
45997 return ret;
45998 /* masked does have just filter clause, but during gimplification
45999 isn't represented by a gimplification omp context, so for
46000 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
46001 so that
46002 #pragma omp parallel masked
46003 #pragma omp taskloop simd lastprivate (x)
46004 isn't confused with
46005 #pragma omp parallel masked taskloop simd lastprivate (x) */
46006 if (OMP_MASKED_COMBINED (ret))
46007 OMP_PARALLEL_COMBINED (stmt) = 1;
46008 return stmt;
46010 else if (cclauses == NULL && strcmp (p, "master") == 0)
46012 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46013 cclauses = cclauses_buf;
46015 cp_lexer_consume_token (parser->lexer);
46016 if (!flag_openmp) /* flag_openmp_simd */
46017 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46018 cclauses, if_p);
46019 block = begin_omp_parallel ();
46020 save = cp_parser_begin_omp_structured_block (parser);
46021 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46022 cclauses, if_p);
46023 cp_parser_end_omp_structured_block (parser, save);
46024 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46025 block);
46026 if (ret == NULL_TREE)
46027 return ret;
46028 /* master doesn't have any clauses and during gimplification
46029 isn't represented by a gimplification omp context, so for
46030 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
46031 so that
46032 #pragma omp parallel master
46033 #pragma omp taskloop simd lastprivate (x)
46034 isn't confused with
46035 #pragma omp parallel master taskloop simd lastprivate (x) */
46036 if (OMP_MASTER_COMBINED (ret))
46037 OMP_PARALLEL_COMBINED (stmt) = 1;
46038 return stmt;
46040 else if (strcmp (p, "loop") == 0)
46042 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46043 if (cclauses == NULL)
46044 cclauses = cclauses_buf;
46046 cp_lexer_consume_token (parser->lexer);
46047 if (!flag_openmp) /* flag_openmp_simd */
46048 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46049 cclauses, if_p);
46050 block = begin_omp_parallel ();
46051 save = cp_parser_begin_omp_structured_block (parser);
46052 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46053 cclauses, if_p);
46054 cp_parser_end_omp_structured_block (parser, save);
46055 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46056 block);
46057 if (ret == NULL_TREE)
46058 return ret;
46059 OMP_PARALLEL_COMBINED (stmt) = 1;
46060 return stmt;
46062 else if (!flag_openmp) /* flag_openmp_simd */
46064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46065 return NULL_TREE;
46067 else if (cclauses == NULL && strcmp (p, "sections") == 0)
46069 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46070 cclauses = cclauses_buf;
46072 cp_lexer_consume_token (parser->lexer);
46073 block = begin_omp_parallel ();
46074 save = cp_parser_begin_omp_structured_block (parser);
46075 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
46076 cp_parser_end_omp_structured_block (parser, save);
46077 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46078 block);
46079 OMP_PARALLEL_COMBINED (stmt) = 1;
46080 return stmt;
46083 else if (!flag_openmp) /* flag_openmp_simd */
46085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46086 return NULL_TREE;
46089 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46090 cclauses == NULL);
46091 if (cclauses)
46093 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
46094 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
46097 block = begin_omp_parallel ();
46098 save = cp_parser_begin_omp_structured_block (parser);
46099 parser->omp_attrs_forbidden_p = true;
46100 cp_parser_statement (parser, NULL_TREE, false, if_p);
46101 cp_parser_end_omp_structured_block (parser, save);
46102 stmt = finish_omp_parallel (clauses, block);
46103 return stmt;
46106 /* OpenMP 2.5:
46107 # pragma omp single single-clause[optseq] new-line
46108 structured-block */
46110 #define OMP_SINGLE_CLAUSE_MASK \
46111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
46114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46117 static tree
46118 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46120 tree stmt = make_node (OMP_SINGLE);
46121 TREE_TYPE (stmt) = void_type_node;
46122 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46124 OMP_SINGLE_CLAUSES (stmt)
46125 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
46126 "#pragma omp single", pragma_tok);
46127 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46129 return add_stmt (stmt);
46132 /* OpenMP 5.1:
46133 # pragma omp scope scope-clause[optseq] new-line
46134 structured-block */
46136 #define OMP_SCOPE_CLAUSE_MASK \
46137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46143 static tree
46144 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46146 tree stmt = make_node (OMP_SCOPE);
46147 TREE_TYPE (stmt) = void_type_node;
46148 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46150 OMP_SCOPE_CLAUSES (stmt)
46151 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
46152 "#pragma omp scope", pragma_tok);
46153 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46155 return add_stmt (stmt);
46158 /* OpenMP 3.0:
46159 # pragma omp task task-clause[optseq] new-line
46160 structured-block */
46162 #define OMP_TASK_CLAUSE_MASK \
46163 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
46176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
46178 static tree
46179 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46181 tree clauses, block;
46182 unsigned int save;
46184 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
46185 "#pragma omp task", pragma_tok);
46186 block = begin_omp_task ();
46187 save = cp_parser_begin_omp_structured_block (parser);
46188 parser->omp_attrs_forbidden_p = true;
46189 cp_parser_statement (parser, NULL_TREE, false, if_p);
46190 cp_parser_end_omp_structured_block (parser, save);
46191 return finish_omp_task (clauses, block);
46194 /* OpenMP 3.0:
46195 # pragma omp taskwait new-line
46197 OpenMP 5.0:
46198 # pragma omp taskwait taskwait-clause[opt] new-line */
46200 #define OMP_TASKWAIT_CLAUSE_MASK \
46201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46204 static void
46205 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
46207 tree clauses
46208 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
46209 "#pragma omp taskwait", pragma_tok);
46211 if (clauses)
46213 tree stmt = make_node (OMP_TASK);
46214 TREE_TYPE (stmt) = void_node;
46215 OMP_TASK_CLAUSES (stmt) = clauses;
46216 OMP_TASK_BODY (stmt) = NULL_TREE;
46217 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46218 add_stmt (stmt);
46220 else
46221 finish_omp_taskwait ();
46224 /* OpenMP 3.1:
46225 # pragma omp taskyield new-line */
46227 static void
46228 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
46230 cp_parser_require_pragma_eol (parser, pragma_tok);
46231 finish_omp_taskyield ();
46234 /* OpenMP 4.0:
46235 # pragma omp taskgroup new-line
46236 structured-block
46238 OpenMP 5.0:
46239 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
46241 #define OMP_TASKGROUP_CLAUSE_MASK \
46242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
46245 static tree
46246 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46248 tree clauses
46249 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
46250 "#pragma omp taskgroup", pragma_tok);
46251 return c_finish_omp_taskgroup (input_location,
46252 cp_parser_omp_structured_block (parser,
46253 if_p),
46254 clauses);
46258 /* OpenMP 2.5:
46259 # pragma omp threadprivate (variable-list) */
46261 static void
46262 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
46264 tree vars;
46266 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
46267 cp_parser_require_pragma_eol (parser, pragma_tok);
46269 finish_omp_threadprivate (vars);
46272 /* OpenMP 4.0:
46273 # pragma omp cancel cancel-clause[optseq] new-line */
46275 #define OMP_CANCEL_CLAUSE_MASK \
46276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
46280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
46282 static void
46283 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
46285 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
46286 "#pragma omp cancel", pragma_tok);
46287 finish_omp_cancel (clauses);
46290 /* OpenMP 4.0:
46291 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
46293 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
46294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
46299 static bool
46300 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
46301 enum pragma_context context)
46303 tree clauses;
46304 bool point_seen = false;
46306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46309 const char *p = IDENTIFIER_POINTER (id);
46311 if (strcmp (p, "point") == 0)
46313 cp_lexer_consume_token (parser->lexer);
46314 point_seen = true;
46317 if (!point_seen)
46319 cp_parser_error (parser, "expected %<point%>");
46320 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46321 return false;
46324 if (context != pragma_compound)
46326 if (context == pragma_stmt)
46327 error_at (pragma_tok->location,
46328 "%<#pragma %s%> may only be used in compound statements",
46329 "omp cancellation point");
46330 else
46331 cp_parser_error (parser, "expected declaration specifiers");
46332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46333 return true;
46336 clauses = cp_parser_omp_all_clauses (parser,
46337 OMP_CANCELLATION_POINT_CLAUSE_MASK,
46338 "#pragma omp cancellation point",
46339 pragma_tok);
46340 finish_omp_cancellation_point (clauses);
46341 return true;
46344 /* OpenMP 4.0:
46345 #pragma omp distribute distribute-clause[optseq] new-line
46346 for-loop */
46348 #define OMP_DISTRIBUTE_CLAUSE_MASK \
46349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
46353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46357 static tree
46358 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
46359 char *p_name, omp_clause_mask mask, tree *cclauses,
46360 bool *if_p)
46362 tree clauses, sb, ret;
46363 unsigned int save;
46364 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46366 strcat (p_name, " distribute");
46367 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
46369 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46371 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46372 const char *p = IDENTIFIER_POINTER (id);
46373 bool simd = false;
46374 bool parallel = false;
46376 if (strcmp (p, "simd") == 0)
46377 simd = true;
46378 else
46379 parallel = strcmp (p, "parallel") == 0;
46380 if (parallel || simd)
46382 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46383 if (cclauses == NULL)
46384 cclauses = cclauses_buf;
46385 cp_lexer_consume_token (parser->lexer);
46386 if (!flag_openmp) /* flag_openmp_simd */
46388 if (simd)
46389 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46390 cclauses, if_p);
46391 else
46392 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46393 cclauses, if_p);
46395 sb = begin_omp_structured_block ();
46396 save = cp_parser_begin_omp_structured_block (parser);
46397 if (simd)
46398 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46399 cclauses, if_p);
46400 else
46401 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46402 cclauses, if_p);
46403 cp_parser_end_omp_structured_block (parser, save);
46404 tree body = finish_omp_structured_block (sb);
46405 if (ret == NULL)
46406 return ret;
46407 ret = make_node (OMP_DISTRIBUTE);
46408 TREE_TYPE (ret) = void_type_node;
46409 OMP_FOR_BODY (ret) = body;
46410 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46411 SET_EXPR_LOCATION (ret, loc);
46412 add_stmt (ret);
46413 return ret;
46416 if (!flag_openmp) /* flag_openmp_simd */
46418 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46419 return NULL_TREE;
46422 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46423 cclauses == NULL);
46424 if (cclauses)
46426 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
46427 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46430 keep_next_level (true);
46431 sb = begin_omp_structured_block ();
46432 save = cp_parser_begin_omp_structured_block (parser);
46434 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
46436 cp_parser_end_omp_structured_block (parser, save);
46437 add_stmt (finish_omp_structured_block (sb));
46439 return ret;
46442 /* OpenMP 4.0:
46443 # pragma omp teams teams-clause[optseq] new-line
46444 structured-block */
46446 #define OMP_TEAMS_CLAUSE_MASK \
46447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
46452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
46456 static tree
46457 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
46458 char *p_name, omp_clause_mask mask, tree *cclauses,
46459 bool *if_p)
46461 tree clauses, sb, ret;
46462 unsigned int save;
46463 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46465 strcat (p_name, " teams");
46466 mask |= OMP_TEAMS_CLAUSE_MASK;
46468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46471 const char *p = IDENTIFIER_POINTER (id);
46472 if (strcmp (p, "distribute") == 0)
46474 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46475 if (cclauses == NULL)
46476 cclauses = cclauses_buf;
46478 cp_lexer_consume_token (parser->lexer);
46479 if (!flag_openmp) /* flag_openmp_simd */
46480 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46481 cclauses, if_p);
46482 keep_next_level (true);
46483 sb = begin_omp_structured_block ();
46484 save = cp_parser_begin_omp_structured_block (parser);
46485 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46486 cclauses, if_p);
46487 cp_parser_end_omp_structured_block (parser, save);
46488 tree body = finish_omp_structured_block (sb);
46489 if (ret == NULL)
46490 return ret;
46491 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46492 ret = make_node (OMP_TEAMS);
46493 TREE_TYPE (ret) = void_type_node;
46494 OMP_TEAMS_CLAUSES (ret) = clauses;
46495 OMP_TEAMS_BODY (ret) = body;
46496 OMP_TEAMS_COMBINED (ret) = 1;
46497 SET_EXPR_LOCATION (ret, loc);
46498 return add_stmt (ret);
46500 else if (strcmp (p, "loop") == 0)
46502 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46503 if (cclauses == NULL)
46504 cclauses = cclauses_buf;
46506 cp_lexer_consume_token (parser->lexer);
46507 if (!flag_openmp) /* flag_openmp_simd */
46508 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46509 cclauses, if_p);
46510 keep_next_level (true);
46511 sb = begin_omp_structured_block ();
46512 save = cp_parser_begin_omp_structured_block (parser);
46513 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46514 cclauses, if_p);
46515 cp_parser_end_omp_structured_block (parser, save);
46516 tree body = finish_omp_structured_block (sb);
46517 if (ret == NULL)
46518 return ret;
46519 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46520 ret = make_node (OMP_TEAMS);
46521 TREE_TYPE (ret) = void_type_node;
46522 OMP_TEAMS_CLAUSES (ret) = clauses;
46523 OMP_TEAMS_BODY (ret) = body;
46524 OMP_TEAMS_COMBINED (ret) = 1;
46525 SET_EXPR_LOCATION (ret, loc);
46526 return add_stmt (ret);
46529 if (!flag_openmp) /* flag_openmp_simd */
46531 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46532 return NULL_TREE;
46535 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46536 cclauses == NULL);
46537 if (cclauses)
46539 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
46540 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46543 tree stmt = make_node (OMP_TEAMS);
46544 TREE_TYPE (stmt) = void_type_node;
46545 OMP_TEAMS_CLAUSES (stmt) = clauses;
46546 keep_next_level (true);
46547 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46548 SET_EXPR_LOCATION (stmt, loc);
46550 return add_stmt (stmt);
46553 /* OpenMP 4.0:
46554 # pragma omp target data target-data-clause[optseq] new-line
46555 structured-block */
46557 #define OMP_TARGET_DATA_CLAUSE_MASK \
46558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
46562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
46564 static tree
46565 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46567 if (flag_openmp)
46568 omp_requires_mask
46569 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46571 tree clauses
46572 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
46573 "#pragma omp target data", pragma_tok);
46574 c_omp_adjust_map_clauses (clauses, false);
46575 int map_seen = 0;
46576 for (tree *pc = &clauses; *pc;)
46578 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46579 switch (OMP_CLAUSE_MAP_KIND (*pc))
46581 case GOMP_MAP_TO:
46582 case GOMP_MAP_ALWAYS_TO:
46583 case GOMP_MAP_PRESENT_TO:
46584 case GOMP_MAP_ALWAYS_PRESENT_TO:
46585 case GOMP_MAP_FROM:
46586 case GOMP_MAP_ALWAYS_FROM:
46587 case GOMP_MAP_PRESENT_FROM:
46588 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46589 case GOMP_MAP_TOFROM:
46590 case GOMP_MAP_ALWAYS_TOFROM:
46591 case GOMP_MAP_PRESENT_TOFROM:
46592 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46593 case GOMP_MAP_ALLOC:
46594 case GOMP_MAP_PRESENT_ALLOC:
46595 map_seen = 3;
46596 break;
46597 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46598 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46599 case GOMP_MAP_ALWAYS_POINTER:
46600 case GOMP_MAP_ATTACH_DETACH:
46601 case GOMP_MAP_ATTACH:
46602 break;
46603 default:
46604 map_seen |= 1;
46605 error_at (OMP_CLAUSE_LOCATION (*pc),
46606 "%<#pragma omp target data%> with map-type other "
46607 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
46608 "on %<map%> clause");
46609 *pc = OMP_CLAUSE_CHAIN (*pc);
46610 continue;
46612 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
46613 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
46614 map_seen = 3;
46615 pc = &OMP_CLAUSE_CHAIN (*pc);
46618 if (map_seen != 3)
46620 if (map_seen == 0)
46621 error_at (pragma_tok->location,
46622 "%<#pragma omp target data%> must contain at least "
46623 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
46624 "clause");
46625 return NULL_TREE;
46628 tree stmt = make_node (OMP_TARGET_DATA);
46629 TREE_TYPE (stmt) = void_type_node;
46630 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
46632 keep_next_level (true);
46633 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46635 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46636 return add_stmt (stmt);
46639 /* OpenMP 4.5:
46640 # pragma omp target enter data target-enter-data-clause[optseq] new-line
46641 structured-block */
46643 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
46644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46650 static bool
46651 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
46652 enum pragma_context context)
46654 bool data_seen = false;
46655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46658 const char *p = IDENTIFIER_POINTER (id);
46660 if (strcmp (p, "data") == 0)
46662 cp_lexer_consume_token (parser->lexer);
46663 data_seen = true;
46666 if (!data_seen)
46668 cp_parser_error (parser, "expected %<data%>");
46669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46670 return false;
46673 if (context == pragma_stmt)
46675 error_at (pragma_tok->location,
46676 "%<#pragma %s%> may only be used in compound statements",
46677 "omp target enter data");
46678 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46679 return true;
46682 if (flag_openmp)
46683 omp_requires_mask
46684 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46686 tree clauses
46687 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
46688 "#pragma omp target enter data", pragma_tok);
46689 c_omp_adjust_map_clauses (clauses, false);
46690 int map_seen = 0;
46691 for (tree *pc = &clauses; *pc;)
46693 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46694 switch (OMP_CLAUSE_MAP_KIND (*pc))
46696 case GOMP_MAP_TO:
46697 case GOMP_MAP_ALWAYS_TO:
46698 case GOMP_MAP_PRESENT_TO:
46699 case GOMP_MAP_ALWAYS_PRESENT_TO:
46700 case GOMP_MAP_ALLOC:
46701 case GOMP_MAP_PRESENT_ALLOC:
46702 map_seen = 3;
46703 break;
46704 case GOMP_MAP_TOFROM:
46705 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46706 map_seen = 3;
46707 break;
46708 case GOMP_MAP_ALWAYS_TOFROM:
46709 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46710 map_seen = 3;
46711 break;
46712 case GOMP_MAP_PRESENT_TOFROM:
46713 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46714 map_seen = 3;
46715 break;
46716 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46717 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46718 map_seen = 3;
46719 break;
46720 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46721 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46722 case GOMP_MAP_ALWAYS_POINTER:
46723 case GOMP_MAP_ATTACH_DETACH:
46724 case GOMP_MAP_ATTACH:
46725 break;
46726 default:
46727 map_seen |= 1;
46728 error_at (OMP_CLAUSE_LOCATION (*pc),
46729 "%<#pragma omp target enter data%> with map-type other "
46730 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46731 *pc = OMP_CLAUSE_CHAIN (*pc);
46732 continue;
46734 pc = &OMP_CLAUSE_CHAIN (*pc);
46737 if (map_seen != 3)
46739 if (map_seen == 0)
46740 error_at (pragma_tok->location,
46741 "%<#pragma omp target enter data%> must contain at least "
46742 "one %<map%> clause");
46743 return true;
46746 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46747 TREE_TYPE (stmt) = void_type_node;
46748 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46749 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46750 add_stmt (stmt);
46751 return true;
46754 /* OpenMP 4.5:
46755 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46756 structured-block */
46758 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46765 static bool
46766 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46767 enum pragma_context context)
46769 bool data_seen = false;
46770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46772 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46773 const char *p = IDENTIFIER_POINTER (id);
46775 if (strcmp (p, "data") == 0)
46777 cp_lexer_consume_token (parser->lexer);
46778 data_seen = true;
46781 if (!data_seen)
46783 cp_parser_error (parser, "expected %<data%>");
46784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46785 return false;
46788 if (context == pragma_stmt)
46790 error_at (pragma_tok->location,
46791 "%<#pragma %s%> may only be used in compound statements",
46792 "omp target exit data");
46793 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46794 return true;
46797 if (flag_openmp)
46798 omp_requires_mask
46799 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46801 tree clauses
46802 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46803 "#pragma omp target exit data", pragma_tok,
46804 false);
46805 clauses = finish_omp_clauses (clauses, C_ORT_OMP_EXIT_DATA);
46806 c_omp_adjust_map_clauses (clauses, false);
46807 int map_seen = 0;
46808 for (tree *pc = &clauses; *pc;)
46810 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46811 switch (OMP_CLAUSE_MAP_KIND (*pc))
46813 case GOMP_MAP_FROM:
46814 case GOMP_MAP_ALWAYS_FROM:
46815 case GOMP_MAP_PRESENT_FROM:
46816 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46817 case GOMP_MAP_RELEASE:
46818 case GOMP_MAP_DELETE:
46819 map_seen = 3;
46820 break;
46821 case GOMP_MAP_TOFROM:
46822 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
46823 map_seen = 3;
46824 break;
46825 case GOMP_MAP_ALWAYS_TOFROM:
46826 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
46827 map_seen = 3;
46828 break;
46829 case GOMP_MAP_PRESENT_TOFROM:
46830 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
46831 map_seen = 3;
46832 break;
46833 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46834 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
46835 map_seen = 3;
46836 break;
46837 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46838 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46839 case GOMP_MAP_ALWAYS_POINTER:
46840 case GOMP_MAP_ATTACH_DETACH:
46841 case GOMP_MAP_DETACH:
46842 break;
46843 default:
46844 map_seen |= 1;
46845 error_at (OMP_CLAUSE_LOCATION (*pc),
46846 "%<#pragma omp target exit data%> with map-type other "
46847 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
46848 "on %<map%> clause");
46849 *pc = OMP_CLAUSE_CHAIN (*pc);
46850 continue;
46852 pc = &OMP_CLAUSE_CHAIN (*pc);
46855 if (map_seen != 3)
46857 if (map_seen == 0)
46858 error_at (pragma_tok->location,
46859 "%<#pragma omp target exit data%> must contain at least "
46860 "one %<map%> clause");
46861 return true;
46864 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
46865 TREE_TYPE (stmt) = void_type_node;
46866 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
46867 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46868 add_stmt (stmt);
46869 return true;
46872 /* OpenMP 4.0:
46873 # pragma omp target update target-update-clause[optseq] new-line */
46875 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
46876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
46877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46883 static bool
46884 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
46885 enum pragma_context context)
46887 if (context == pragma_stmt)
46889 error_at (pragma_tok->location,
46890 "%<#pragma %s%> may only be used in compound statements",
46891 "omp target update");
46892 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46893 return true;
46896 tree clauses
46897 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
46898 "#pragma omp target update", pragma_tok);
46899 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
46900 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
46902 error_at (pragma_tok->location,
46903 "%<#pragma omp target update%> must contain at least one "
46904 "%<from%> or %<to%> clauses");
46905 return true;
46908 if (flag_openmp)
46909 omp_requires_mask
46910 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46912 tree stmt = make_node (OMP_TARGET_UPDATE);
46913 TREE_TYPE (stmt) = void_type_node;
46914 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
46915 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46916 add_stmt (stmt);
46917 return true;
46920 /* OpenMP 4.0:
46921 # pragma omp target target-clause[optseq] new-line
46922 structured-block */
46924 #define OMP_TARGET_CLAUSE_MASK \
46925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
46933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
46937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
46939 static bool
46940 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
46941 enum pragma_context context, bool *if_p)
46943 if (flag_openmp)
46944 omp_requires_mask
46945 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46949 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46950 const char *p = IDENTIFIER_POINTER (id);
46951 enum tree_code ccode = ERROR_MARK;
46953 if (strcmp (p, "teams") == 0)
46954 ccode = OMP_TEAMS;
46955 else if (strcmp (p, "parallel") == 0)
46956 ccode = OMP_PARALLEL;
46957 else if (strcmp (p, "simd") == 0)
46958 ccode = OMP_SIMD;
46959 if (ccode != ERROR_MARK)
46961 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
46962 char p_name[sizeof ("#pragma omp target teams distribute "
46963 "parallel for simd")];
46965 cp_lexer_consume_token (parser->lexer);
46966 strcpy (p_name, "#pragma omp target");
46967 if (!flag_openmp) /* flag_openmp_simd */
46969 tree stmt;
46970 switch (ccode)
46972 case OMP_TEAMS:
46973 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
46974 OMP_TARGET_CLAUSE_MASK,
46975 cclauses, if_p);
46976 break;
46977 case OMP_PARALLEL:
46978 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46979 OMP_TARGET_CLAUSE_MASK,
46980 cclauses, if_p);
46981 break;
46982 case OMP_SIMD:
46983 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
46984 OMP_TARGET_CLAUSE_MASK,
46985 cclauses, if_p);
46986 break;
46987 default:
46988 gcc_unreachable ();
46990 return stmt != NULL_TREE;
46992 keep_next_level (true);
46993 tree sb = begin_omp_structured_block (), ret;
46994 unsigned save = cp_parser_begin_omp_structured_block (parser);
46995 switch (ccode)
46997 case OMP_TEAMS:
46998 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
46999 OMP_TARGET_CLAUSE_MASK, cclauses,
47000 if_p);
47001 break;
47002 case OMP_PARALLEL:
47003 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47004 OMP_TARGET_CLAUSE_MASK, cclauses,
47005 if_p);
47006 break;
47007 case OMP_SIMD:
47008 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
47009 OMP_TARGET_CLAUSE_MASK, cclauses,
47010 if_p);
47011 break;
47012 default:
47013 gcc_unreachable ();
47015 cp_parser_end_omp_structured_block (parser, save);
47016 tree body = finish_omp_structured_block (sb);
47017 if (ret == NULL_TREE)
47018 return false;
47019 if (ccode == OMP_TEAMS && !processing_template_decl)
47020 /* For combined target teams, ensure the num_teams and
47021 thread_limit clause expressions are evaluated on the host,
47022 before entering the target construct. */
47023 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47024 c; c = OMP_CLAUSE_CHAIN (c))
47025 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
47026 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
47027 for (int i = 0;
47028 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
47029 if (OMP_CLAUSE_OPERAND (c, i)
47030 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
47032 tree expr = OMP_CLAUSE_OPERAND (c, i);
47033 expr = force_target_expr (TREE_TYPE (expr), expr,
47034 tf_none);
47035 if (expr == error_mark_node)
47036 continue;
47037 tree tmp = TARGET_EXPR_SLOT (expr);
47038 add_stmt (expr);
47039 OMP_CLAUSE_OPERAND (c, i) = expr;
47040 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
47041 OMP_CLAUSE_FIRSTPRIVATE);
47042 OMP_CLAUSE_DECL (tc) = tmp;
47043 OMP_CLAUSE_CHAIN (tc)
47044 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
47045 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
47047 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
47048 finish_omp_target (pragma_tok->location,
47049 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
47050 return true;
47052 else if (!flag_openmp) /* flag_openmp_simd */
47054 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47055 return false;
47057 else if (strcmp (p, "data") == 0)
47059 cp_lexer_consume_token (parser->lexer);
47060 cp_parser_omp_target_data (parser, pragma_tok, if_p);
47061 return true;
47063 else if (strcmp (p, "enter") == 0)
47065 cp_lexer_consume_token (parser->lexer);
47066 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
47068 else if (strcmp (p, "exit") == 0)
47070 cp_lexer_consume_token (parser->lexer);
47071 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
47073 else if (strcmp (p, "update") == 0)
47075 cp_lexer_consume_token (parser->lexer);
47076 return cp_parser_omp_target_update (parser, pragma_tok, context);
47079 if (!flag_openmp) /* flag_openmp_simd */
47081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47082 return false;
47085 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
47086 "#pragma omp target", pragma_tok,
47087 false);
47088 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47089 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
47091 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
47092 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
47093 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
47094 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
47095 OMP_CLAUSE_CHAIN (c) = nc;
47097 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
47099 c_omp_adjust_map_clauses (clauses, true);
47100 keep_next_level (true);
47101 tree body = cp_parser_omp_structured_block (parser, if_p);
47103 finish_omp_target (pragma_tok->location, clauses, body, false);
47104 return true;
47107 /* OpenACC 2.0:
47108 # pragma acc cache (variable-list) new-line
47111 static tree
47112 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
47114 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
47115 clauses. */
47116 auto_suppress_location_wrappers sentinel;
47118 tree stmt, clauses;
47120 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
47121 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47123 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
47125 stmt = make_node (OACC_CACHE);
47126 TREE_TYPE (stmt) = void_type_node;
47127 OACC_CACHE_CLAUSES (stmt) = clauses;
47128 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47129 add_stmt (stmt);
47131 return stmt;
47134 /* OpenACC 2.0:
47135 # pragma acc data oacc-data-clause[optseq] new-line
47136 structured-block */
47138 #define OACC_DATA_CLAUSE_MASK \
47139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47151 static tree
47152 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47154 tree stmt, clauses, block;
47155 unsigned int save;
47157 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
47158 "#pragma acc data", pragma_tok);
47160 block = begin_omp_parallel ();
47161 save = cp_parser_begin_omp_structured_block (parser);
47162 cp_parser_statement (parser, NULL_TREE, false, if_p);
47163 cp_parser_end_omp_structured_block (parser, save);
47164 stmt = finish_oacc_data (clauses, block);
47165 return stmt;
47168 /* OpenACC 2.0:
47169 # pragma acc host_data <clauses> new-line
47170 structured-block */
47172 #define OACC_HOST_DATA_CLAUSE_MASK \
47173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
47174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
47177 static tree
47178 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47180 tree stmt, clauses, block;
47181 unsigned int save;
47183 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
47184 "#pragma acc host_data", pragma_tok,
47185 false);
47186 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
47188 error_at (pragma_tok->location,
47189 "%<host_data%> construct requires %<use_device%> clause");
47190 return error_mark_node;
47192 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47193 block = begin_omp_parallel ();
47194 save = cp_parser_begin_omp_structured_block (parser);
47195 cp_parser_statement (parser, NULL_TREE, false, if_p);
47196 cp_parser_end_omp_structured_block (parser, save);
47197 stmt = finish_oacc_host_data (clauses, block);
47198 return stmt;
47201 /* OpenACC 2.0:
47202 # pragma acc declare oacc-data-clause[optseq] new-line
47205 #define OACC_DECLARE_CLAUSE_MASK \
47206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
47212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
47213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47215 static tree
47216 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
47218 tree clauses, stmt;
47219 bool error = false;
47220 bool found_in_scope = global_bindings_p ();
47222 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
47223 "#pragma acc declare", pragma_tok);
47226 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47228 error_at (pragma_tok->location,
47229 "no valid clauses specified in %<#pragma acc declare%>");
47230 return NULL_TREE;
47233 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
47235 location_t loc = OMP_CLAUSE_LOCATION (t);
47236 tree decl = OMP_CLAUSE_DECL (t);
47237 if (!DECL_P (decl))
47239 error_at (loc, "array section in %<#pragma acc declare%>");
47240 error = true;
47241 continue;
47243 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
47244 switch (OMP_CLAUSE_MAP_KIND (t))
47246 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47247 case GOMP_MAP_ALLOC:
47248 case GOMP_MAP_TO:
47249 case GOMP_MAP_FORCE_DEVICEPTR:
47250 case GOMP_MAP_DEVICE_RESIDENT:
47251 break;
47253 case GOMP_MAP_LINK:
47254 if (!global_bindings_p ()
47255 && (TREE_STATIC (decl)
47256 || !DECL_EXTERNAL (decl)))
47258 error_at (loc,
47259 "%qD must be a global variable in "
47260 "%<#pragma acc declare link%>",
47261 decl);
47262 error = true;
47263 continue;
47265 break;
47267 default:
47268 if (global_bindings_p ())
47270 error_at (loc, "invalid OpenACC clause at file scope");
47271 error = true;
47272 continue;
47274 if (DECL_EXTERNAL (decl))
47276 error_at (loc,
47277 "invalid use of %<extern%> variable %qD "
47278 "in %<#pragma acc declare%>", decl);
47279 error = true;
47280 continue;
47282 else if (TREE_PUBLIC (decl))
47284 error_at (loc,
47285 "invalid use of %<global%> variable %qD "
47286 "in %<#pragma acc declare%>", decl);
47287 error = true;
47288 continue;
47290 break;
47293 if (!found_in_scope)
47294 /* This seems to ignore the existence of cleanup scopes?
47295 What is the meaning for local extern decls? The local
47296 extern is in this scope, but it is referring to a decl that
47297 is namespace scope. */
47298 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
47299 if (d == decl)
47301 found_in_scope = true;
47302 break;
47304 if (!found_in_scope)
47306 error_at (loc,
47307 "%qD must be a variable declared in the same scope as "
47308 "%<#pragma acc declare%>", decl);
47309 error = true;
47310 continue;
47313 if (!error)
47315 if (DECL_LOCAL_DECL_P (decl))
47316 /* We need to mark the aliased decl, as that is the entity
47317 that is being referred to. This won't work for
47318 dependent variables, but it didn't work for them before
47319 DECL_LOCAL_DECL_P was a thing either. But then
47320 dependent local extern variable decls are as rare as
47321 hen's teeth. */
47322 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
47323 if (alias != error_mark_node)
47324 decl = alias;
47326 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
47327 || lookup_attribute ("omp declare target link",
47328 DECL_ATTRIBUTES (decl)))
47330 error_at (loc, "variable %qD used more than once with "
47331 "%<#pragma acc declare%>", decl);
47332 error = true;
47333 continue;
47336 tree id;
47337 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
47338 id = get_identifier ("omp declare target link");
47339 else
47340 id = get_identifier ("omp declare target");
47342 DECL_ATTRIBUTES (decl)
47343 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
47344 if (current_binding_level->kind == sk_namespace)
47346 symtab_node *node = symtab_node::get (decl);
47347 if (node != NULL)
47349 node->offloadable = 1;
47350 if (ENABLE_OFFLOADING)
47352 g->have_offload = true;
47353 if (is_a <varpool_node *> (node))
47354 vec_safe_push (offload_vars, decl);
47361 if (error || current_binding_level->kind == sk_namespace)
47362 return NULL_TREE;
47364 stmt = make_node (OACC_DECLARE);
47365 TREE_TYPE (stmt) = void_type_node;
47366 OACC_DECLARE_CLAUSES (stmt) = clauses;
47367 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47369 add_stmt (stmt);
47371 return NULL_TREE;
47374 /* OpenACC 2.0:
47375 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
47379 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
47381 LOC is the location of the #pragma token.
47384 #define OACC_ENTER_DATA_CLAUSE_MASK \
47385 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47392 #define OACC_EXIT_DATA_CLAUSE_MASK \
47393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
47397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
47399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47401 static tree
47402 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
47403 bool enter)
47405 location_t loc = pragma_tok->location;
47406 tree stmt, clauses;
47407 const char *p = "";
47409 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47410 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47412 if (strcmp (p, "data") != 0)
47414 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
47415 enter ? "enter" : "exit");
47416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47417 return NULL_TREE;
47420 cp_lexer_consume_token (parser->lexer);
47422 if (enter)
47423 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
47424 "#pragma acc enter data", pragma_tok);
47425 else
47426 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
47427 "#pragma acc exit data", pragma_tok);
47429 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47431 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
47432 enter ? "enter" : "exit");
47433 return NULL_TREE;
47436 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
47437 TREE_TYPE (stmt) = void_type_node;
47438 OMP_STANDALONE_CLAUSES (stmt) = clauses;
47439 SET_EXPR_LOCATION (stmt, loc);
47440 add_stmt (stmt);
47441 return stmt;
47444 /* OpenACC 2.0:
47445 # pragma acc loop oacc-loop-clause[optseq] new-line
47446 structured-block */
47448 #define OACC_LOOP_CLAUSE_MASK \
47449 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
47450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
47456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
47457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
47460 static tree
47461 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
47462 omp_clause_mask mask, tree *cclauses, bool *if_p)
47464 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
47466 strcat (p_name, " loop");
47467 mask |= OACC_LOOP_CLAUSE_MASK;
47469 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47470 /*finish_p=*/cclauses == NULL,
47471 /*target=*/is_parallel);
47472 if (cclauses)
47474 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
47475 if (*cclauses)
47476 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC_TARGET);
47477 if (clauses)
47478 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47481 tree block = begin_omp_structured_block ();
47482 int save = cp_parser_begin_omp_structured_block (parser);
47483 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
47484 cp_parser_end_omp_structured_block (parser, save);
47486 /* Later processing of combined acc loop constructs gets confused
47487 by an extra level of empty nested BIND_EXPRs, so flatten them. */
47488 block = finish_omp_structured_block (block);
47489 if (TREE_CODE (block) == BIND_EXPR
47490 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
47491 && !BIND_EXPR_VARS (block))
47492 block = BIND_EXPR_BODY (block);
47493 add_stmt (block);
47495 return stmt;
47498 /* OpenACC 2.0:
47499 # pragma acc kernels oacc-kernels-clause[optseq] new-line
47500 structured-block
47504 # pragma acc parallel oacc-parallel-clause[optseq] new-line
47505 structured-block
47507 OpenACC 2.6:
47509 # pragma acc serial oacc-serial-clause[optseq] new-line
47512 #define OACC_KERNELS_CLAUSE_MASK \
47513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47530 #define OACC_PARALLEL_CLAUSE_MASK \
47531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47551 #define OACC_SERIAL_CLAUSE_MASK \
47552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47569 static tree
47570 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
47571 char *p_name, bool *if_p)
47573 omp_clause_mask mask;
47574 enum tree_code code;
47575 switch (cp_parser_pragma_kind (pragma_tok))
47577 case PRAGMA_OACC_KERNELS:
47578 strcat (p_name, " kernels");
47579 mask = OACC_KERNELS_CLAUSE_MASK;
47580 code = OACC_KERNELS;
47581 break;
47582 case PRAGMA_OACC_PARALLEL:
47583 strcat (p_name, " parallel");
47584 mask = OACC_PARALLEL_CLAUSE_MASK;
47585 code = OACC_PARALLEL;
47586 break;
47587 case PRAGMA_OACC_SERIAL:
47588 strcat (p_name, " serial");
47589 mask = OACC_SERIAL_CLAUSE_MASK;
47590 code = OACC_SERIAL;
47591 break;
47592 default:
47593 gcc_unreachable ();
47596 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47598 const char *p
47599 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47600 if (strcmp (p, "loop") == 0)
47602 cp_lexer_consume_token (parser->lexer);
47603 tree block = begin_omp_parallel ();
47604 tree clauses;
47605 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
47606 &clauses, if_p);
47607 protected_set_expr_location (stmt, pragma_tok->location);
47608 return finish_omp_construct (code, block, clauses);
47612 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47613 /*finish_p=*/true,
47614 /*target=*/true);
47616 tree block = begin_omp_parallel ();
47617 unsigned int save = cp_parser_begin_omp_structured_block (parser);
47618 cp_parser_statement (parser, NULL_TREE, false, if_p);
47619 cp_parser_end_omp_structured_block (parser, save);
47620 return finish_omp_construct (code, block, clauses);
47623 /* OpenACC 2.0:
47624 # pragma acc update oacc-update-clause[optseq] new-line
47627 #define OACC_UPDATE_CLAUSE_MASK \
47628 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
47630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
47631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
47633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
47636 static tree
47637 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
47639 tree stmt, clauses;
47641 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
47642 "#pragma acc update", pragma_tok);
47644 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47646 error_at (pragma_tok->location,
47647 "%<#pragma acc update%> must contain at least one "
47648 "%<device%> or %<host%> or %<self%> clause");
47649 return NULL_TREE;
47652 stmt = make_node (OACC_UPDATE);
47653 TREE_TYPE (stmt) = void_type_node;
47654 OACC_UPDATE_CLAUSES (stmt) = clauses;
47655 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47656 add_stmt (stmt);
47657 return stmt;
47660 /* OpenACC 2.0:
47661 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
47663 LOC is the location of the #pragma token.
47666 #define OACC_WAIT_CLAUSE_MASK \
47667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
47669 static tree
47670 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
47672 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
47673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47675 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47676 list = cp_parser_oacc_wait_list (parser, loc, list);
47678 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
47679 "#pragma acc wait", pragma_tok);
47681 stmt = c_finish_oacc_wait (loc, list, clauses);
47682 stmt = finish_expr_stmt (stmt);
47684 return stmt;
47687 /* OpenMP 4.0:
47688 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
47690 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
47691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
47692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
47693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
47694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
47695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
47696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
47698 static void
47699 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
47700 enum pragma_context context,
47701 bool variant_p)
47703 bool first_p = parser->omp_declare_simd == NULL;
47704 cp_omp_declare_simd_data data;
47705 if (first_p)
47707 data.error_seen = false;
47708 data.fndecl_seen = false;
47709 data.variant_p = variant_p;
47710 data.tokens = vNULL;
47711 data.attribs[0] = NULL;
47712 data.attribs[1] = NULL;
47713 data.loc = UNKNOWN_LOCATION;
47714 /* It is safe to take the address of a local variable; it will only be
47715 used while this scope is live. */
47716 parser->omp_declare_simd = &data;
47718 else if (parser->omp_declare_simd->variant_p != variant_p)
47720 error_at (pragma_tok->location,
47721 "%<#pragma omp declare %s%> followed by "
47722 "%<#pragma omp declare %s%>",
47723 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47724 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47725 parser->omp_declare_simd->error_seen = true;
47728 /* Store away all pragma tokens. */
47729 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47730 cp_lexer_consume_token (parser->lexer);
47731 cp_parser_require_pragma_eol (parser, pragma_tok);
47732 struct cp_token_cache *cp
47733 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47734 parser->omp_declare_simd->tokens.safe_push (cp);
47736 if (first_p)
47738 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47739 cp_parser_pragma (parser, context, NULL);
47740 switch (context)
47742 case pragma_external:
47743 cp_parser_declaration (parser, NULL_TREE);
47744 break;
47745 case pragma_member:
47746 cp_parser_member_declaration (parser);
47747 break;
47748 case pragma_objc_icode:
47749 cp_parser_block_declaration (parser, /*statement_p=*/false);
47750 break;
47751 default:
47752 cp_parser_declaration_statement (parser);
47753 break;
47755 if (parser->omp_declare_simd
47756 && !parser->omp_declare_simd->error_seen
47757 && !parser->omp_declare_simd->fndecl_seen)
47758 error_at (pragma_tok->location,
47759 "%<#pragma omp declare %s%> not immediately followed by "
47760 "function declaration or definition",
47761 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47762 data.tokens.release ();
47763 parser->omp_declare_simd = NULL;
47767 /* OpenMP 5.0:
47769 trait-selector:
47770 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47772 trait-score:
47773 score(score-expression)
47775 Note that this function returns a list of trait selectors for the
47776 trait-selector-set SET. */
47778 static tree
47779 cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
47780 bool has_parms_p)
47782 tree ret = NULL_TREE;
47785 tree selector;
47786 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47787 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47788 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47789 else
47791 cp_parser_error (parser, "expected trait selector name");
47792 return error_mark_node;
47795 enum omp_ts_code sel
47796 = omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
47798 if (sel == OMP_TRAIT_INVALID)
47800 /* Per the spec, "Implementations can ignore specified selectors
47801 that are not those described in this section"; however, we
47802 must record such selectors because they cause match failures. */
47803 warning_at (cp_lexer_peek_token (parser->lexer)->location,
47804 OPT_Wopenmp,
47805 "unknown selector %qs for context selector set %qs",
47806 IDENTIFIER_POINTER (selector), omp_tss_map[set]);
47807 cp_lexer_consume_token (parser->lexer);
47808 ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
47809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47810 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47811 n; --n)
47812 cp_lexer_consume_token (parser->lexer);
47813 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47815 cp_lexer_consume_token (parser->lexer);
47816 continue;
47818 else
47819 break;
47822 cp_lexer_consume_token (parser->lexer);
47824 tree properties = NULL_TREE;
47825 tree scoreval = NULL_TREE;
47826 enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
47827 bool allow_score = omp_ts_map[sel].allow_score;
47828 tree t;
47830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47832 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
47834 error ("selector %qs does not accept any properties",
47835 IDENTIFIER_POINTER (selector));
47836 return error_mark_node;
47839 matching_parens parens;
47840 parens.consume_open (parser);
47842 cp_token *token = cp_lexer_peek_token (parser->lexer);
47843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47844 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
47845 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
47847 cp_lexer_save_tokens (parser->lexer);
47848 cp_lexer_consume_token (parser->lexer);
47849 cp_lexer_consume_token (parser->lexer);
47850 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
47851 true)
47852 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
47854 cp_lexer_rollback_tokens (parser->lexer);
47855 cp_lexer_consume_token (parser->lexer);
47857 matching_parens parens2;
47858 parens2.require_open (parser);
47859 tree score = cp_parser_constant_expression (parser);
47860 if (!parens2.require_close (parser))
47861 cp_parser_skip_to_closing_parenthesis (parser, true,
47862 false, true);
47863 cp_parser_require (parser, CPP_COLON, RT_COLON);
47864 if (!allow_score)
47865 error_at (token->location,
47866 "%<score%> cannot be specified in traits "
47867 "in the %qs trait-selector-set",
47868 omp_tss_map[set]);
47869 else if (score != error_mark_node)
47871 score = fold_non_dependent_expr (score);
47872 if (value_dependent_expression_p (score))
47873 scoreval = score;
47874 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
47875 || TREE_CODE (score) != INTEGER_CST)
47876 error_at (token->location, "%<score%> argument must "
47877 "be constant integer expression");
47878 else if (tree_int_cst_sgn (score) < 0)
47879 error_at (token->location, "%<score%> argument must "
47880 "be non-negative");
47881 else
47882 scoreval = score;
47885 else
47886 cp_lexer_rollback_tokens (parser->lexer);
47888 token = cp_lexer_peek_token (parser->lexer);
47891 switch (property_kind)
47893 case OMP_TRAIT_PROPERTY_ID:
47894 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47895 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47897 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
47898 cp_lexer_consume_token (parser->lexer);
47899 properties = make_trait_property (prop, NULL_TREE,
47900 properties);
47902 else
47904 cp_parser_error (parser, "expected identifier");
47905 return error_mark_node;
47907 break;
47908 case OMP_TRAIT_PROPERTY_NAME_LIST:
47911 tree prop = OMP_TP_NAMELIST_NODE;
47912 tree value = NULL_TREE;
47913 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47914 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47916 value = cp_lexer_peek_token (parser->lexer)->u.value;
47917 cp_lexer_consume_token (parser->lexer);
47919 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
47920 value = cp_parser_string_literal (parser,
47921 /*translate=*/false,
47922 /*wide_ok=*/false);
47923 else
47925 cp_parser_error (parser, "expected identifier or "
47926 "string literal");
47927 return error_mark_node;
47930 properties = make_trait_property (prop, value, properties);
47932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47933 cp_lexer_consume_token (parser->lexer);
47934 else
47935 break;
47937 while (1);
47938 break;
47939 case OMP_TRAIT_PROPERTY_EXPR:
47940 /* FIXME: this is bogus, the expression need
47941 not be constant. */
47942 t = cp_parser_constant_expression (parser);
47943 if (t != error_mark_node)
47945 t = fold_non_dependent_expr (t);
47946 if (!value_dependent_expression_p (t)
47947 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47948 || !tree_fits_shwi_p (t)))
47949 error_at (token->location, "property must be "
47950 "constant integer expression");
47951 else
47952 properties = make_trait_property (NULL_TREE, t,
47953 properties);
47955 else
47956 return error_mark_node;
47957 break;
47958 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
47959 if (sel == OMP_TRAIT_CONSTRUCT_SIMD)
47961 if (!has_parms_p)
47963 error_at (token->location, "properties for %<simd%> "
47964 "selector may not be specified in "
47965 "%<metadirective%>");
47966 return error_mark_node;
47968 properties
47969 = cp_parser_omp_all_clauses (parser,
47970 OMP_DECLARE_SIMD_CLAUSE_MASK,
47971 "simd", NULL, true, 2);
47973 else if (sel == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
47975 /* FIXME: The "requires" selector was added in OpenMP 5.1.
47976 Currently only the now-deprecated syntax
47977 from OpenMP 5.0 is supported. */
47978 sorry_at (token->location,
47979 "%<requires%> selector is not supported yet");
47980 return error_mark_node;
47982 else
47983 gcc_unreachable ();
47984 break;
47985 default:
47986 gcc_unreachable ();
47989 if (!parens.require_close (parser))
47990 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
47992 properties = nreverse (properties);
47994 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
47995 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
47996 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
47998 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
47999 return error_mark_node;
48002 ret = make_trait_selector (sel, scoreval, properties, ret);
48004 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48005 cp_lexer_consume_token (parser->lexer);
48006 else
48007 break;
48009 while (1);
48011 return nreverse (ret);
48014 /* OpenMP 5.0:
48016 trait-set-selector[,trait-set-selector[,...]]
48018 trait-set-selector:
48019 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
48021 trait-set-selector-name:
48022 constructor
48023 device
48024 implementation
48025 user */
48027 static tree
48028 cp_parser_omp_context_selector_specification (cp_parser *parser,
48029 bool has_parms_p)
48031 tree ret = NULL_TREE;
48034 const char *setp = "";
48035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48036 setp
48037 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48038 enum omp_tss_code set = omp_lookup_tss_code (setp);
48040 if (set == OMP_TRAIT_SET_INVALID)
48042 cp_parser_error (parser, "expected context selector set name");
48043 return error_mark_node;
48046 cp_lexer_consume_token (parser->lexer);
48048 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
48049 return error_mark_node;
48051 matching_braces braces;
48052 if (!braces.require_open (parser))
48053 return error_mark_node;
48055 tree selectors
48056 = cp_parser_omp_context_selector (parser, set, has_parms_p);
48057 if (selectors == error_mark_node)
48059 cp_parser_skip_to_closing_brace (parser);
48060 ret = error_mark_node;
48062 else if (ret != error_mark_node)
48063 ret = make_trait_set_selector (set, selectors, ret);
48065 braces.require_close (parser);
48067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48068 cp_lexer_consume_token (parser->lexer);
48069 else
48070 break;
48072 while (1);
48074 if (ret == error_mark_node)
48075 return ret;
48076 return nreverse (ret);
48079 /* Assumption clauses:
48080 OpenMP 5.1
48081 absent (directive-name-list)
48082 contains (directive-name-list)
48083 holds (expression)
48084 no_openmp
48085 no_openmp_routines
48086 no_parallelism */
48088 static void
48089 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
48090 bool is_assume)
48092 bool no_openmp = false;
48093 bool no_openmp_routines = false;
48094 bool no_parallelism = false;
48095 bitmap_head absent_head, contains_head;
48097 bitmap_obstack_initialize (NULL);
48098 bitmap_initialize (&absent_head, &bitmap_default_obstack);
48099 bitmap_initialize (&contains_head, &bitmap_default_obstack);
48101 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48102 error_at (cp_lexer_peek_token (parser->lexer)->location,
48103 "expected at least one assumption clause");
48105 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48107 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48108 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48109 cp_lexer_consume_token (parser->lexer);
48111 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48112 break;
48114 const char *p
48115 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48116 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48118 if (!strcmp (p, "no_openmp"))
48120 cp_lexer_consume_token (parser->lexer);
48121 if (no_openmp)
48122 error_at (cloc, "too many %qs clauses", "no_openmp");
48123 no_openmp = true;
48125 else if (!strcmp (p, "no_openmp_routines"))
48127 cp_lexer_consume_token (parser->lexer);
48128 if (no_openmp_routines)
48129 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
48130 no_openmp_routines = true;
48132 else if (!strcmp (p, "no_parallelism"))
48134 cp_lexer_consume_token (parser->lexer);
48135 if (no_parallelism)
48136 error_at (cloc, "too many %qs clauses", "no_parallelism");
48137 no_parallelism = true;
48139 else if (!strcmp (p, "holds"))
48141 cp_lexer_consume_token (parser->lexer);
48142 matching_parens parens;
48143 if (parens.require_open (parser))
48145 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
48146 tree t = cp_parser_assignment_expression (parser);
48147 if (!type_dependent_expression_p (t))
48148 t = contextual_conv_bool (t, tf_warning_or_error);
48149 if (is_assume && !error_operand_p (t))
48150 finish_expr_stmt (build_assume_call (eloc, t));
48151 if (!parens.require_close (parser))
48152 cp_parser_skip_to_closing_parenthesis (parser,
48153 /*recovering=*/true,
48154 /*or_comma=*/false,
48155 /*consume_paren=*/true);
48158 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
48160 cp_lexer_consume_token (parser->lexer);
48161 matching_parens parens;
48162 if (parens.require_open (parser))
48166 const char *directive[3] = {};
48167 int i;
48168 location_t dloc
48169 = cp_lexer_peek_token (parser->lexer)->location;
48170 for (i = 0; i < 3; i++)
48172 tree id;
48173 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
48174 id = cp_lexer_peek_nth_token (parser->lexer,
48175 i + 1)->u.value;
48176 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
48177 CPP_KEYWORD))
48179 enum rid rid
48180 = cp_lexer_peek_nth_token (parser->lexer,
48181 i + 1)->keyword;
48182 id = ridpointers[rid];
48184 else
48185 break;
48186 directive[i] = IDENTIFIER_POINTER (id);
48188 if (i == 0)
48189 error_at (dloc, "expected directive name");
48190 else
48192 const struct c_omp_directive *dir
48193 = c_omp_categorize_directive (directive[0],
48194 directive[1],
48195 directive[2]);
48196 if (dir == NULL
48197 || dir->kind == C_OMP_DIR_DECLARATIVE
48198 || dir->kind == C_OMP_DIR_INFORMATIONAL
48199 || dir->id == PRAGMA_OMP_END
48200 || (!dir->second && directive[1])
48201 || (!dir->third && directive[2]))
48202 error_at (dloc, "unknown OpenMP directive name in "
48203 "%qs clause argument", p);
48204 else
48206 int id = dir - c_omp_directives;
48207 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
48208 : &absent_head, id))
48209 error_at (dloc, "%<%s%s%s%s%s%> directive "
48210 "mentioned in both %<absent%> and "
48211 "%<contains%> clauses",
48212 directive[0],
48213 directive[1] ? " " : "",
48214 directive[1] ? directive[1] : "",
48215 directive[2] ? " " : "",
48216 directive[2] ? directive[2] : "");
48217 else if (!bitmap_set_bit (p[0] == 'a'
48218 ? &absent_head
48219 : &contains_head, id))
48220 error_at (dloc, "%<%s%s%s%s%s%> directive "
48221 "mentioned multiple times in %qs "
48222 "clauses",
48223 directive[0],
48224 directive[1] ? " " : "",
48225 directive[1] ? directive[1] : "",
48226 directive[2] ? " " : "",
48227 directive[2] ? directive[2] : "", p);
48229 for (; i; --i)
48230 cp_lexer_consume_token (parser->lexer);
48232 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48233 cp_lexer_consume_token (parser->lexer);
48234 else
48235 break;
48237 while (1);
48238 if (!parens.require_close (parser))
48239 cp_parser_skip_to_closing_parenthesis (parser,
48240 /*recovering=*/true,
48241 /*or_comma=*/false,
48242 /*consume_paren=*/true);
48245 else if (startswith (p, "ext_"))
48247 warning_at (cloc, OPT_Wopenmp, "unknown assumption clause %qs", p);
48248 cp_lexer_consume_token (parser->lexer);
48249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48250 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48251 n; --n)
48252 cp_lexer_consume_token (parser->lexer);
48254 else
48256 cp_lexer_consume_token (parser->lexer);
48257 error_at (cloc, "expected assumption clause");
48258 break;
48261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48264 /* OpenMP 5.1
48265 # pragma omp assume clauses[optseq] new-line */
48267 static void
48268 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48270 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
48271 add_stmt (cp_parser_omp_structured_block (parser, if_p));
48274 /* OpenMP 5.1
48275 # pragma omp assumes clauses[optseq] new-line */
48277 static bool
48278 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
48280 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48281 return false;
48284 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
48285 that into "omp declare variant base" attribute. */
48287 static tree
48288 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
48289 tree attrs)
48291 matching_parens parens;
48292 if (!parens.require_open (parser))
48294 fail:
48295 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48296 return attrs;
48299 bool template_p;
48300 cp_id_kind idk = CP_ID_KIND_NONE;
48301 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
48302 cp_expr varid
48303 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
48304 /*check_dependency_p=*/true,
48305 /*template_p=*/&template_p,
48306 /*declarator_p=*/false,
48307 /*optional_p=*/false);
48308 parens.require_close (parser);
48310 tree variant;
48311 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
48312 || TREE_CODE (varid) == TYPE_DECL
48313 || varid == error_mark_node)
48314 variant = varid;
48315 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
48316 variant = NULL_TREE;
48317 else
48319 tree ambiguous_decls;
48320 variant = cp_parser_lookup_name (parser, varid, none_type,
48321 template_p, /*is_namespace=*/false,
48322 /*check_dependency=*/true,
48323 &ambiguous_decls,
48324 varid.get_location ());
48325 if (ambiguous_decls)
48326 variant = NULL_TREE;
48328 if (variant == NULL_TREE)
48329 variant = error_mark_node;
48330 else if (TREE_CODE (variant) != SCOPE_REF)
48332 const char *error_msg;
48333 variant
48334 = finish_id_expression (varid, variant, parser->scope,
48335 &idk, false, true,
48336 &parser->non_integral_constant_expression_p,
48337 template_p, true, false, false, &error_msg,
48338 varid.get_location ());
48339 if (error_msg)
48340 cp_parser_error (parser, error_msg);
48342 location_t caret_loc = get_pure_location (varid.get_location ());
48343 location_t start_loc = get_start (varid_token->location);
48344 location_t finish_loc = get_finish (varid.get_location ());
48345 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
48347 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48348 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48349 cp_lexer_consume_token (parser->lexer);
48351 const char *clause = "";
48352 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
48353 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48354 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48355 if (strcmp (clause, "match"))
48357 cp_parser_error (parser, "expected %<match%>");
48358 goto fail;
48361 cp_lexer_consume_token (parser->lexer);
48363 if (!parens.require_open (parser))
48364 goto fail;
48366 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
48367 if (ctx == error_mark_node)
48368 goto fail;
48369 ctx = omp_check_context_selector (match_loc, ctx);
48370 if (ctx != error_mark_node && variant != error_mark_node)
48372 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
48373 match_loc);
48374 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
48375 loc_node = tree_cons (match_loc_node,
48376 build_int_cst (integer_type_node, idk),
48377 build_tree_list (loc_node, integer_zero_node));
48378 attrs = tree_cons (get_identifier ("omp declare variant base"),
48379 tree_cons (variant, ctx, loc_node), attrs);
48380 if (processing_template_decl)
48381 ATTR_IS_DEPENDENT (attrs) = 1;
48384 parens.require_close (parser);
48385 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48386 return attrs;
48390 /* Finalize #pragma omp declare simd clauses after direct declarator has
48391 been parsed, and put that into "omp declare simd" attribute. */
48393 static tree
48394 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
48396 struct cp_token_cache *ce;
48397 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
48398 int i;
48400 if (!data->error_seen && data->fndecl_seen)
48402 error ("%<#pragma omp declare %s%> not immediately followed by "
48403 "a single function declaration or definition",
48404 data->variant_p ? "variant" : "simd");
48405 data->error_seen = true;
48407 if (data->error_seen)
48408 return attrs;
48410 FOR_EACH_VEC_ELT (data->tokens, i, ce)
48412 tree c, cl;
48414 cp_parser_push_lexer_for_tokens (parser, ce);
48415 parser->lexer->in_pragma = true;
48416 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48417 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48419 const char *kind = IDENTIFIER_POINTER (id);
48420 cp_lexer_consume_token (parser->lexer);
48421 if (strcmp (kind, "simd") == 0)
48423 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
48424 "#pragma omp declare simd",
48425 pragma_tok);
48426 if (cl)
48427 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48428 c = build_tree_list (get_identifier ("omp declare simd"), cl);
48429 TREE_CHAIN (c) = attrs;
48430 if (processing_template_decl)
48431 ATTR_IS_DEPENDENT (c) = 1;
48432 attrs = c;
48434 else
48436 gcc_assert (strcmp (kind, "variant") == 0);
48437 attrs
48438 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
48440 cp_parser_pop_lexer (parser);
48443 cp_lexer *lexer = NULL;
48444 for (int i = 0; i < 2; i++)
48446 if (data->attribs[i] == NULL)
48447 continue;
48448 for (tree *pa = data->attribs[i]; *pa; )
48449 if (get_attribute_namespace (*pa) == omp_identifier
48450 && is_attribute_p ("directive", get_attribute_name (*pa)))
48452 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
48454 tree d = TREE_VALUE (a);
48455 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48456 cp_token *first = DEFPARSE_TOKENS (d)->first;
48457 cp_token *last = DEFPARSE_TOKENS (d)->last;
48458 const char *directive[3] = {};
48459 for (int j = 0; j < 3; j++)
48461 tree id = NULL_TREE;
48462 if (first + j == last)
48463 break;
48464 if (first[j].type == CPP_NAME)
48465 id = first[j].u.value;
48466 else if (first[j].type == CPP_KEYWORD)
48467 id = ridpointers[(int) first[j].keyword];
48468 else
48469 break;
48470 directive[j] = IDENTIFIER_POINTER (id);
48472 const c_omp_directive *dir = NULL;
48473 if (directive[0])
48474 dir = c_omp_categorize_directive (directive[0], directive[1],
48475 directive[2]);
48476 if (dir == NULL)
48478 error_at (first->location,
48479 "unknown OpenMP directive name in "
48480 "%qs attribute argument",
48481 TREE_PUBLIC (d)
48482 ? "omp::decl" : "omp::directive");
48483 continue;
48485 if (dir->id != PRAGMA_OMP_DECLARE
48486 || (strcmp (directive[1], "simd") != 0
48487 && strcmp (directive[1], "variant") != 0))
48489 error_at (first->location,
48490 "OpenMP directive other than %<declare simd%> "
48491 "or %<declare variant%> appertains to a "
48492 "declaration");
48493 continue;
48496 if (parser->omp_attrs_forbidden_p)
48498 error_at (first->location,
48499 "mixing OpenMP directives with attribute and "
48500 "pragma syntax on the same statement");
48501 parser->omp_attrs_forbidden_p = false;
48504 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
48505 continue;
48506 if (lexer == NULL)
48508 lexer = cp_lexer_alloc ();
48509 lexer->debugging_p = parser->lexer->debugging_p;
48511 vec_safe_reserve (lexer->buffer, (last - first) + 2);
48512 cp_token tok = {};
48513 tok.type = CPP_PRAGMA;
48514 tok.keyword = RID_MAX;
48515 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
48516 tok.location = first->location;
48517 lexer->buffer->quick_push (tok);
48518 while (++first < last)
48519 lexer->buffer->quick_push (*first);
48520 tok = {};
48521 tok.type = CPP_PRAGMA_EOL;
48522 tok.keyword = RID_MAX;
48523 tok.location = last->location;
48524 lexer->buffer->quick_push (tok);
48525 tok = {};
48526 tok.type = CPP_EOF;
48527 tok.keyword = RID_MAX;
48528 tok.location = last->location;
48529 lexer->buffer->quick_push (tok);
48530 lexer->next = parser->lexer;
48531 lexer->next_token = lexer->buffer->address ();
48532 lexer->last_token = lexer->next_token
48533 + lexer->buffer->length ()
48534 - 1;
48535 lexer->in_omp_attribute_pragma = true;
48536 parser->lexer = lexer;
48537 /* Move the current source position to that of the first token
48538 in the new lexer. */
48539 cp_lexer_set_source_position_from_token (lexer->next_token);
48541 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48543 const char *kind = IDENTIFIER_POINTER (id);
48544 cp_lexer_consume_token (parser->lexer);
48546 tree c, cl;
48547 if (strcmp (kind, "simd") == 0)
48549 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48550 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48551 cp_lexer_consume_token (parser->lexer);
48553 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
48554 cl = cp_parser_omp_all_clauses (parser, mask,
48555 "#pragma omp declare simd",
48556 pragma_tok);
48557 if (cl)
48558 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48559 c = build_tree_list (get_identifier ("omp declare simd"),
48560 cl);
48561 TREE_CHAIN (c) = attrs;
48562 if (processing_template_decl)
48563 ATTR_IS_DEPENDENT (c) = 1;
48564 attrs = c;
48566 else
48568 gcc_assert (strcmp (kind, "variant") == 0);
48569 attrs
48570 = cp_finish_omp_declare_variant (parser, pragma_tok,
48571 attrs);
48573 gcc_assert (parser->lexer != lexer);
48574 vec_safe_truncate (lexer->buffer, 0);
48576 *pa = TREE_CHAIN (*pa);
48578 else
48579 pa = &TREE_CHAIN (*pa);
48581 if (lexer)
48582 cp_lexer_destroy (lexer);
48584 data->fndecl_seen = true;
48585 return attrs;
48588 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
48589 a threadprivate, groupprivate, allocate or declare target directive,
48590 return true and parse it for DECL. */
48592 bool
48593 cp_maybe_parse_omp_decl (tree decl, tree d)
48595 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48596 cp_token *first = DEFPARSE_TOKENS (d)->first;
48597 cp_token *last = DEFPARSE_TOKENS (d)->last;
48598 const char *directive[3] = {};
48599 for (int j = 0; j < 3; j++)
48601 tree id = NULL_TREE;
48602 if (first + j == last)
48603 break;
48604 if (first[j].type == CPP_NAME)
48605 id = first[j].u.value;
48606 else if (first[j].type == CPP_KEYWORD)
48607 id = ridpointers[(int) first[j].keyword];
48608 else
48609 break;
48610 directive[j] = IDENTIFIER_POINTER (id);
48612 const c_omp_directive *dir = NULL;
48613 if (directive[0])
48614 dir = c_omp_categorize_directive (directive[0], directive[1],
48615 directive[2]);
48616 if (dir == NULL)
48618 error_at (first->location,
48619 "unknown OpenMP directive name in "
48620 "%qs attribute argument", "omp::decl");
48621 return false;
48623 if (dir->id != PRAGMA_OMP_THREADPRIVATE
48624 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
48625 && dir->id != PRAGMA_OMP_ALLOCATE
48626 && (dir->id != PRAGMA_OMP_DECLARE
48627 || strcmp (directive[1], "target") != 0))
48628 return false;
48630 if (!flag_openmp && !dir->simd)
48631 return true;
48633 cp_parser *parser = the_parser;
48634 cp_lexer *lexer = cp_lexer_alloc ();
48635 lexer->debugging_p = parser->lexer->debugging_p;
48636 lexer->in_omp_decl_attribute = decl;
48637 vec_safe_reserve (lexer->buffer, last - first + 3, true);
48638 cp_token tok = {};
48639 tok.type = CPP_PRAGMA;
48640 tok.keyword = RID_MAX;
48641 tok.u.value = build_int_cst (NULL, dir->id);
48642 tok.location = first->location;
48643 lexer->buffer->quick_push (tok);
48644 while (++first < last)
48645 lexer->buffer->quick_push (*first);
48646 tok = {};
48647 tok.type = CPP_PRAGMA_EOL;
48648 tok.keyword = RID_MAX;
48649 tok.location = last->location;
48650 lexer->buffer->quick_push (tok);
48651 tok = {};
48652 tok.type = CPP_EOF;
48653 tok.keyword = RID_MAX;
48654 tok.location = last->location;
48655 lexer->buffer->quick_push (tok);
48656 lexer->next = parser->lexer;
48657 lexer->next_token = lexer->buffer->address ();
48658 lexer->last_token = lexer->next_token
48659 + lexer->buffer->length ()
48660 - 1;
48661 lexer->in_omp_attribute_pragma = true;
48662 parser->lexer = lexer;
48663 /* Move the current source position to that of the first token in the
48664 new lexer. */
48665 cp_lexer_set_source_position_from_token (lexer->next_token);
48666 cp_parser_pragma (parser, pragma_external, NULL);
48668 return true;
48671 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
48672 on #pragma omp declare target. Return false if errors were reported. */
48674 static bool
48675 handle_omp_declare_target_clause (tree c, tree t, int device_type,
48676 bool indirect)
48678 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
48679 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
48680 tree id;
48681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
48683 id = get_identifier ("omp declare target link");
48684 std::swap (at1, at2);
48686 else
48687 id = get_identifier ("omp declare target");
48688 if (at2)
48690 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
48691 error_at (OMP_CLAUSE_LOCATION (c),
48692 "%qD specified both in declare target %<link%> and %qs"
48693 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
48694 else
48695 error_at (OMP_CLAUSE_LOCATION (c),
48696 "%qD specified both in declare target %<link%> and "
48697 "%<to%> or %<enter%> clauses", t);
48698 return false;
48700 if (!at1)
48702 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48703 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
48704 return true;
48706 symtab_node *node = symtab_node::get (t);
48707 if (node != NULL)
48709 node->offloadable = 1;
48710 if (ENABLE_OFFLOADING)
48712 g->have_offload = true;
48713 if (is_a <varpool_node *> (node))
48714 vec_safe_push (offload_vars, t);
48718 if (TREE_CODE (t) != FUNCTION_DECL)
48719 return true;
48720 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
48722 tree at3 = lookup_attribute ("omp declare target host",
48723 DECL_ATTRIBUTES (t));
48724 if (at3 == NULL_TREE)
48726 id = get_identifier ("omp declare target host");
48727 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48730 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48732 tree at3 = lookup_attribute ("omp declare target nohost",
48733 DECL_ATTRIBUTES (t));
48734 if (at3 == NULL_TREE)
48736 id = get_identifier ("omp declare target nohost");
48737 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48740 if (indirect)
48742 tree at4 = lookup_attribute ("omp declare target indirect",
48743 DECL_ATTRIBUTES (t));
48744 if (at4 == NULL_TREE)
48746 id = get_identifier ("omp declare target indirect");
48747 DECL_ATTRIBUTES (t)
48748 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48751 return true;
48754 /* OpenMP 4.0:
48755 # pragma omp declare target new-line
48756 declarations and definitions
48757 # pragma omp end declare target new-line
48759 OpenMP 4.5:
48760 # pragma omp declare target ( extended-list ) new-line
48762 # pragma omp declare target declare-target-clauses[seq] new-line */
48764 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48771 static void
48772 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48774 tree clauses = NULL_TREE;
48775 int device_type = 0;
48776 bool indirect = false;
48777 bool only_device_type_or_indirect = true;
48778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48779 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48780 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48781 clauses
48782 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48783 "#pragma omp declare target", pragma_tok);
48784 else if (parser->lexer->in_omp_decl_attribute
48785 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48787 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48788 clauses);
48789 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
48790 cp_parser_require_pragma_eol (parser, pragma_tok);
48792 else
48794 cp_omp_declare_target_attr a
48795 = { parser->lexer->in_omp_attribute_pragma, -1, false };
48796 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48797 cp_parser_require_pragma_eol (parser, pragma_tok);
48798 return;
48800 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48802 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48803 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48804 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48805 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48807 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48809 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE
48810 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48811 continue;
48812 tree t = OMP_CLAUSE_DECL (c);
48813 only_device_type_or_indirect = false;
48814 if (!handle_omp_declare_target_clause (c, t, device_type, indirect))
48815 continue;
48816 if (VAR_OR_FUNCTION_DECL_P (t)
48817 && DECL_LOCAL_DECL_P (t)
48818 && DECL_LANG_SPECIFIC (t)
48819 && DECL_LOCAL_DECL_ALIAS (t)
48820 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
48821 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
48822 device_type, indirect);
48824 if ((device_type || indirect) && only_device_type_or_indirect)
48825 error_at (OMP_CLAUSE_LOCATION (clauses),
48826 "directive with only %<device_type%> or %<indirect%> clauses");
48827 if (indirect && device_type && device_type != OMP_CLAUSE_DEVICE_TYPE_ANY)
48828 error_at (OMP_CLAUSE_LOCATION (clauses),
48829 "%<device_type%> clause must specify 'any' when used with "
48830 "an %<indirect%> clause");
48833 /* OpenMP 5.1
48834 # pragma omp begin assumes clauses[optseq] new-line
48836 # pragma omp begin declare target clauses[optseq] new-line */
48838 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
48839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48842 static void
48843 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
48845 const char *p = "";
48846 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48850 p = IDENTIFIER_POINTER (id);
48852 if (strcmp (p, "declare") == 0)
48854 cp_lexer_consume_token (parser->lexer);
48855 p = "";
48856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48859 p = IDENTIFIER_POINTER (id);
48861 if (strcmp (p, "target") == 0)
48863 cp_lexer_consume_token (parser->lexer);
48864 tree clauses
48865 = cp_parser_omp_all_clauses (parser,
48866 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
48867 "#pragma omp begin declare target",
48868 pragma_tok);
48869 int device_type = 0;
48870 bool indirect = 0;
48871 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48873 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48874 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48875 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48876 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48878 cp_omp_declare_target_attr a
48879 = { in_omp_attribute_pragma, device_type, indirect };
48880 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48882 else
48884 cp_parser_error (parser, "expected %<target%>");
48885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48888 else if (strcmp (p, "assumes") == 0)
48890 cp_lexer_consume_token (parser->lexer);
48891 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48892 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
48893 vec_safe_push (scope_chain->omp_begin_assumes, a);
48895 else
48897 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
48898 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48902 /* OpenMP 4.0:
48903 # pragma omp end declare target new-line
48905 OpenMP 5.1:
48906 # pragma omp end assumes new-line */
48908 static void
48909 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
48911 const char *p = "";
48912 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48916 p = IDENTIFIER_POINTER (id);
48918 if (strcmp (p, "declare") == 0)
48920 cp_lexer_consume_token (parser->lexer);
48921 p = "";
48922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48925 p = IDENTIFIER_POINTER (id);
48927 if (strcmp (p, "target") == 0)
48928 cp_lexer_consume_token (parser->lexer);
48929 else
48931 cp_parser_error (parser, "expected %<target%>");
48932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48933 return;
48935 cp_parser_require_pragma_eol (parser, pragma_tok);
48936 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
48937 error_at (pragma_tok->location,
48938 "%<#pragma omp end declare target%> without corresponding "
48939 "%<#pragma omp declare target%> or "
48940 "%<#pragma omp begin declare target%>");
48941 else
48943 cp_omp_declare_target_attr
48944 a = scope_chain->omp_declare_target_attribute->pop ();
48945 if (a.attr_syntax != in_omp_attribute_pragma)
48947 if (a.attr_syntax)
48948 error_at (pragma_tok->location,
48949 "%qs in attribute syntax terminated "
48950 "with %qs in pragma syntax",
48951 a.device_type >= 0 ? "begin declare target"
48952 : "declare target",
48953 "end declare target");
48954 else
48955 error_at (pragma_tok->location,
48956 "%qs in pragma syntax terminated "
48957 "with %qs in attribute syntax",
48958 a.device_type >= 0 ? "begin declare target"
48959 : "declare target",
48960 "end declare target");
48964 else if (strcmp (p, "assumes") == 0)
48966 cp_lexer_consume_token (parser->lexer);
48967 cp_parser_require_pragma_eol (parser, pragma_tok);
48968 if (!vec_safe_length (scope_chain->omp_begin_assumes))
48969 error_at (pragma_tok->location,
48970 "%qs without corresponding %qs",
48971 "#pragma omp end assumes", "#pragma omp begin assumes");
48972 else
48974 cp_omp_begin_assumes_data
48975 a = scope_chain->omp_begin_assumes->pop ();
48976 if (a.attr_syntax != in_omp_attribute_pragma)
48978 if (a.attr_syntax)
48979 error_at (pragma_tok->location,
48980 "%qs in attribute syntax terminated "
48981 "with %qs in pragma syntax",
48982 "begin assumes", "end assumes");
48983 else
48984 error_at (pragma_tok->location,
48985 "%qs in pragma syntax terminated "
48986 "with %qs in attribute syntax",
48987 "begin assumes", "end assumes");
48991 else
48993 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
48994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48995 return;
48999 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
49000 expression and optional initializer clause of
49001 #pragma omp declare reduction. We store the expression(s) as
49002 either 3, 6 or 7 special statements inside of the artificial function's
49003 body. The first two statements are DECL_EXPRs for the artificial
49004 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
49005 expression that uses those variables.
49006 If there was any INITIALIZER clause, this is followed by further statements,
49007 the fourth and fifth statements are DECL_EXPRs for the artificial
49008 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
49009 constructor variant (first token after open paren is not omp_priv),
49010 then the sixth statement is a statement with the function call expression
49011 that uses the OMP_PRIV and optionally OMP_ORIG variable.
49012 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
49013 to initialize the OMP_PRIV artificial variable and there is seventh
49014 statement, a DECL_EXPR of the OMP_PRIV statement again. */
49016 static bool
49017 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
49019 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
49020 gcc_assert (TYPE_REF_P (type));
49021 type = TREE_TYPE (type);
49022 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
49023 DECL_ARTIFICIAL (omp_out) = 1;
49024 pushdecl (omp_out);
49025 add_decl_expr (omp_out);
49026 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
49027 DECL_ARTIFICIAL (omp_in) = 1;
49028 pushdecl (omp_in);
49029 add_decl_expr (omp_in);
49030 tree combiner;
49031 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
49033 keep_next_level (true);
49034 tree block = begin_omp_structured_block ();
49035 combiner = cp_parser_expression (parser);
49036 finish_expr_stmt (combiner);
49037 block = finish_omp_structured_block (block);
49038 if (processing_template_decl)
49039 block = build_stmt (input_location, EXPR_STMT, block);
49040 add_stmt (block);
49042 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
49043 return false;
49045 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49046 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49047 cp_lexer_consume_token (parser->lexer);
49049 const char *p = "";
49050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49053 p = IDENTIFIER_POINTER (id);
49056 if (strcmp (p, "initializer") == 0)
49058 cp_lexer_consume_token (parser->lexer);
49059 matching_parens parens;
49060 if (!parens.require_open (parser))
49061 return false;
49063 p = "";
49064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49067 p = IDENTIFIER_POINTER (id);
49070 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
49071 DECL_ARTIFICIAL (omp_priv) = 1;
49072 pushdecl (omp_priv);
49073 add_decl_expr (omp_priv);
49074 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
49075 DECL_ARTIFICIAL (omp_orig) = 1;
49076 pushdecl (omp_orig);
49077 add_decl_expr (omp_orig);
49079 keep_next_level (true);
49080 block = begin_omp_structured_block ();
49082 bool ctor = false;
49083 if (strcmp (p, "omp_priv") == 0)
49085 bool is_non_constant_init;
49086 ctor = true;
49087 cp_lexer_consume_token (parser->lexer);
49088 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
49089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
49090 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49091 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
49092 == CPP_CLOSE_PAREN
49093 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
49094 == CPP_CLOSE_PAREN))
49096 finish_omp_structured_block (block);
49097 error ("invalid initializer clause");
49098 return false;
49100 initializer = cp_parser_initializer (parser,
49101 /*is_direct_init=*/nullptr,
49102 &is_non_constant_init);
49103 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
49104 NULL_TREE, LOOKUP_ONLYCONVERTING);
49106 else
49108 cp_parser_parse_tentatively (parser);
49109 /* Don't create location wrapper nodes here. */
49110 auto_suppress_location_wrappers sentinel;
49111 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
49112 /*check_dependency_p=*/true,
49113 /*template_p=*/NULL,
49114 /*declarator_p=*/false,
49115 /*optional_p=*/false);
49116 vec<tree, va_gc> *args;
49117 if (fn_name == error_mark_node
49118 || cp_parser_error_occurred (parser)
49119 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49120 || ((args = cp_parser_parenthesized_expression_list
49121 (parser, non_attr, /*cast_p=*/false,
49122 /*allow_expansion_p=*/true,
49123 /*non_constant_p=*/NULL)),
49124 cp_parser_error_occurred (parser)))
49126 finish_omp_structured_block (block);
49127 cp_parser_abort_tentative_parse (parser);
49128 cp_parser_error (parser, "expected id-expression (arguments)");
49129 return false;
49131 unsigned int i;
49132 tree arg;
49133 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
49134 if (arg == omp_priv
49135 || (TREE_CODE (arg) == ADDR_EXPR
49136 && TREE_OPERAND (arg, 0) == omp_priv))
49137 break;
49138 cp_parser_abort_tentative_parse (parser);
49139 if (arg == NULL_TREE)
49140 error ("one of the initializer call arguments should be %<omp_priv%>"
49141 " or %<&omp_priv%>");
49142 initializer = cp_parser_postfix_expression (parser, false, false, false,
49143 false, NULL);
49144 finish_expr_stmt (initializer);
49147 block = finish_omp_structured_block (block);
49148 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
49149 if (processing_template_decl)
49150 block = build_stmt (input_location, EXPR_STMT, block);
49151 add_stmt (block);
49153 if (ctor)
49154 add_decl_expr (omp_orig);
49156 if (!parens.require_close (parser))
49157 return false;
49160 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
49161 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
49162 UNKNOWN_LOCATION);
49164 return true;
49167 /* OpenMP 4.0
49168 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49169 initializer-clause[opt] new-line
49171 initializer-clause:
49172 initializer (omp_priv initializer)
49173 initializer (function-name (argument-list)) */
49175 static void
49176 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
49177 enum pragma_context)
49179 auto_vec<tree> types;
49180 enum tree_code reduc_code = ERROR_MARK;
49181 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
49182 unsigned int i;
49183 cp_token *first_token;
49184 cp_token_cache *cp;
49185 int errs;
49186 void *p;
49188 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
49189 p = obstack_alloc (&declarator_obstack, 0);
49191 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
49192 goto fail;
49194 switch (cp_lexer_peek_token (parser->lexer)->type)
49196 case CPP_PLUS:
49197 reduc_code = PLUS_EXPR;
49198 break;
49199 case CPP_MULT:
49200 reduc_code = MULT_EXPR;
49201 break;
49202 case CPP_MINUS:
49203 reduc_code = MINUS_EXPR;
49204 break;
49205 case CPP_AND:
49206 reduc_code = BIT_AND_EXPR;
49207 break;
49208 case CPP_XOR:
49209 reduc_code = BIT_XOR_EXPR;
49210 break;
49211 case CPP_OR:
49212 reduc_code = BIT_IOR_EXPR;
49213 break;
49214 case CPP_AND_AND:
49215 reduc_code = TRUTH_ANDIF_EXPR;
49216 break;
49217 case CPP_OR_OR:
49218 reduc_code = TRUTH_ORIF_EXPR;
49219 break;
49220 case CPP_NAME:
49221 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
49222 break;
49223 default:
49224 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
49225 "%<|%>, %<&&%>, %<||%> or identifier");
49226 goto fail;
49229 if (reduc_code != ERROR_MARK)
49230 cp_lexer_consume_token (parser->lexer);
49232 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
49233 if (reduc_id == error_mark_node)
49234 goto fail;
49236 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
49237 goto fail;
49239 /* Types may not be defined in declare reduction type list. */
49240 const char *saved_message;
49241 saved_message = parser->type_definition_forbidden_message;
49242 parser->type_definition_forbidden_message
49243 = G_("types may not be defined in declare reduction type list");
49244 bool saved_colon_corrects_to_scope_p;
49245 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
49246 parser->colon_corrects_to_scope_p = false;
49247 bool saved_colon_doesnt_start_class_def_p;
49248 saved_colon_doesnt_start_class_def_p
49249 = parser->colon_doesnt_start_class_def_p;
49250 parser->colon_doesnt_start_class_def_p = true;
49252 while (true)
49254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49255 type = cp_parser_type_id (parser);
49256 if (type == error_mark_node)
49258 else if (ARITHMETIC_TYPE_P (type)
49259 && (orig_reduc_id == NULL_TREE
49260 || (TREE_CODE (type) != COMPLEX_TYPE
49261 && (id_equal (orig_reduc_id, "min")
49262 || id_equal (orig_reduc_id, "max")))))
49263 error_at (loc, "predeclared arithmetic type %qT in "
49264 "%<#pragma omp declare reduction%>", type);
49265 else if (FUNC_OR_METHOD_TYPE_P (type)
49266 || TREE_CODE (type) == ARRAY_TYPE)
49267 error_at (loc, "function or array type %qT in "
49268 "%<#pragma omp declare reduction%>", type);
49269 else if (TYPE_REF_P (type))
49270 error_at (loc, "reference type %qT in "
49271 "%<#pragma omp declare reduction%>", type);
49272 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
49273 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
49274 "type %qT in %<#pragma omp declare reduction%>", type);
49275 else
49276 types.safe_push (type);
49278 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
49279 cp_lexer_consume_token (parser->lexer);
49280 else
49281 break;
49284 /* Restore the saved message. */
49285 parser->type_definition_forbidden_message = saved_message;
49286 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
49287 parser->colon_doesnt_start_class_def_p
49288 = saved_colon_doesnt_start_class_def_p;
49290 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
49291 || types.is_empty ())
49293 fail:
49294 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49295 goto done;
49298 first_token = cp_lexer_peek_token (parser->lexer);
49299 cp = NULL;
49300 errs = errorcount;
49301 FOR_EACH_VEC_ELT (types, i, type)
49303 tree fntype
49304 = build_function_type_list (void_type_node,
49305 cp_build_reference_type (type, false),
49306 NULL_TREE);
49307 tree this_reduc_id = reduc_id;
49308 if (!dependent_type_p (type))
49309 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
49310 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
49311 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
49312 DECL_ARTIFICIAL (fndecl) = 1;
49313 DECL_EXTERNAL (fndecl) = 1;
49314 DECL_DECLARED_INLINE_P (fndecl) = 1;
49315 DECL_IGNORED_P (fndecl) = 1;
49316 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
49317 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
49318 DECL_ATTRIBUTES (fndecl)
49319 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
49320 DECL_ATTRIBUTES (fndecl));
49321 bool block_scope = false;
49322 if (current_function_decl)
49324 block_scope = true;
49325 DECL_CONTEXT (fndecl) = current_function_decl;
49326 DECL_LOCAL_DECL_P (fndecl) = true;
49329 if (processing_template_decl)
49330 fndecl = push_template_decl (fndecl);
49332 if (block_scope)
49334 if (!processing_template_decl)
49335 pushdecl (fndecl);
49337 else if (current_class_type)
49339 if (cp == NULL)
49341 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49342 cp_lexer_consume_token (parser->lexer);
49343 cp = cp_token_cache_new (first_token,
49344 cp_lexer_peek_nth_token (parser->lexer,
49345 2));
49347 DECL_STATIC_FUNCTION_P (fndecl) = 1;
49348 finish_member_declaration (fndecl);
49349 DECL_PENDING_INLINE_INFO (fndecl) = cp;
49350 DECL_PENDING_INLINE_P (fndecl) = 1;
49351 vec_safe_push (unparsed_funs_with_definitions, fndecl);
49352 continue;
49354 else
49356 DECL_CONTEXT (fndecl) = current_namespace;
49357 tree d = pushdecl (fndecl);
49358 /* We should never meet a matched duplicate decl. */
49359 gcc_checking_assert (d == error_mark_node || d == fndecl);
49362 tree block = NULL_TREE;
49363 if (!block_scope)
49364 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
49365 else
49366 block = begin_omp_structured_block ();
49367 if (cp)
49369 cp_parser_push_lexer_for_tokens (parser, cp);
49370 parser->lexer->in_pragma = true;
49373 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
49375 if (cp)
49376 cp_parser_pop_lexer (parser);
49377 if (!block_scope)
49378 finish_function (/*inline_p=*/false);
49379 else
49381 DECL_CONTEXT (fndecl) = current_function_decl;
49382 if (DECL_TEMPLATE_INFO (fndecl))
49383 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
49385 if (!ok)
49386 goto fail;
49388 if (block_scope)
49390 block = finish_omp_structured_block (block);
49391 if (TREE_CODE (block) == BIND_EXPR)
49392 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
49393 else if (TREE_CODE (block) == STATEMENT_LIST)
49394 DECL_SAVED_TREE (fndecl) = block;
49395 if (processing_template_decl)
49396 add_decl_expr (fndecl);
49399 cp_check_omp_declare_reduction (fndecl);
49400 if (cp == NULL && types.length () > 1)
49401 cp = cp_token_cache_new (first_token,
49402 cp_lexer_peek_nth_token (parser->lexer, 2));
49403 if (errs != errorcount)
49404 break;
49407 cp_parser_require_pragma_eol (parser, pragma_tok);
49409 done:
49410 /* Free any declarators allocated. */
49411 obstack_free (&declarator_obstack, p);
49414 /* OpenMP 4.0
49415 #pragma omp declare simd declare-simd-clauses[optseq] new-line
49416 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49417 initializer-clause[opt] new-line
49418 #pragma omp declare target new-line
49420 OpenMP 5.0
49421 #pragma omp declare variant (identifier) match (context-selector) */
49423 static bool
49424 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
49425 enum pragma_context context)
49427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49429 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49430 const char *p = IDENTIFIER_POINTER (id);
49432 if (strcmp (p, "simd") == 0)
49434 cp_lexer_consume_token (parser->lexer);
49435 cp_parser_omp_declare_simd (parser, pragma_tok,
49436 context, false);
49437 return true;
49439 if (flag_openmp && strcmp (p, "variant") == 0)
49441 cp_lexer_consume_token (parser->lexer);
49442 cp_parser_omp_declare_simd (parser, pragma_tok,
49443 context, true);
49444 return true;
49446 cp_ensure_no_omp_declare_simd (parser);
49447 if (strcmp (p, "reduction") == 0)
49449 cp_lexer_consume_token (parser->lexer);
49450 cp_parser_omp_declare_reduction (parser, pragma_tok,
49451 context);
49452 return false;
49454 if (!flag_openmp) /* flag_openmp_simd */
49456 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49457 return false;
49459 if (strcmp (p, "target") == 0)
49461 cp_lexer_consume_token (parser->lexer);
49462 cp_parser_omp_declare_target (parser, pragma_tok);
49463 return false;
49466 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
49467 "%<target%> or %<variant%>");
49468 cp_parser_require_pragma_eol (parser, pragma_tok);
49469 return false;
49472 /* OpenMP 5.0
49473 #pragma omp requires clauses[optseq] new-line */
49475 static bool
49476 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
49478 enum omp_requires new_req = (enum omp_requires) 0;
49480 location_t loc = pragma_tok->location;
49481 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49483 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49484 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49485 cp_lexer_consume_token (parser->lexer);
49487 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49489 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49490 const char *p = IDENTIFIER_POINTER (id);
49491 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49492 enum omp_requires this_req = (enum omp_requires) 0;
49494 if (!strcmp (p, "unified_address"))
49495 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
49496 else if (!strcmp (p, "unified_shared_memory"))
49497 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
49498 else if (!strcmp (p, "dynamic_allocators"))
49499 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
49500 else if (!strcmp (p, "reverse_offload"))
49501 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
49502 else if (!strcmp (p, "atomic_default_mem_order"))
49504 cp_lexer_consume_token (parser->lexer);
49506 matching_parens parens;
49507 if (parens.require_open (parser))
49509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49511 id = cp_lexer_peek_token (parser->lexer)->u.value;
49512 p = IDENTIFIER_POINTER (id);
49514 if (!strcmp (p, "seq_cst"))
49515 this_req
49516 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
49517 else if (!strcmp (p, "relaxed"))
49518 this_req
49519 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
49520 else if (!strcmp (p, "release"))
49521 this_req
49522 = (enum omp_requires) OMP_MEMORY_ORDER_RELEASE;
49523 else if (!strcmp (p, "acq_rel"))
49524 this_req
49525 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
49526 else if (!strcmp (p, "acquire"))
49527 this_req
49528 = (enum omp_requires) OMP_MEMORY_ORDER_ACQUIRE;
49530 if (this_req == 0)
49532 error_at (cp_lexer_peek_token (parser->lexer)->location,
49533 "expected %<acq_rel%>, %<acquire%>, "
49534 "%<relaxed%>, %<release%> or %<seq_cst%>");
49535 switch (cp_lexer_peek_token (parser->lexer)->type)
49537 case CPP_EOF:
49538 case CPP_PRAGMA_EOL:
49539 case CPP_CLOSE_PAREN:
49540 break;
49541 default:
49542 if (cp_lexer_nth_token_is (parser->lexer, 2,
49543 CPP_CLOSE_PAREN))
49544 cp_lexer_consume_token (parser->lexer);
49545 break;
49548 else
49549 cp_lexer_consume_token (parser->lexer);
49551 if (!parens.require_close (parser))
49552 cp_parser_skip_to_closing_parenthesis (parser,
49553 /*recovering=*/true,
49554 /*or_comma=*/false,
49555 /*consume_paren=*/
49556 true);
49558 if (this_req == 0)
49560 cp_parser_require_pragma_eol (parser, pragma_tok);
49561 return false;
49564 p = NULL;
49566 else
49568 error_at (cloc, "expected %<unified_address%>, "
49569 "%<unified_shared_memory%>, "
49570 "%<dynamic_allocators%>, "
49571 "%<reverse_offload%> "
49572 "or %<atomic_default_mem_order%> clause");
49573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49574 return false;
49576 if (p)
49577 cp_lexer_consume_token (parser->lexer);
49578 if (this_req)
49580 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49582 if ((this_req & new_req) != 0)
49583 error_at (cloc, "too many %qs clauses", p);
49584 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
49585 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
49586 error_at (cloc, "%qs clause used lexically after first "
49587 "target construct or offloading API", p);
49589 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49591 error_at (cloc, "too many %qs clauses",
49592 "atomic_default_mem_order");
49593 this_req = (enum omp_requires) 0;
49595 else if ((omp_requires_mask
49596 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49598 error_at (cloc, "more than one %<atomic_default_mem_order%>"
49599 " clause in a single compilation unit");
49600 this_req
49601 = (enum omp_requires)
49602 (omp_requires_mask
49603 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
49605 else if ((omp_requires_mask
49606 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
49607 error_at (cloc, "%<atomic_default_mem_order%> clause used "
49608 "lexically after first %<atomic%> construct "
49609 "without memory order clause");
49610 new_req = (enum omp_requires) (new_req | this_req);
49611 omp_requires_mask
49612 = (enum omp_requires) (omp_requires_mask | this_req);
49613 continue;
49616 break;
49618 cp_parser_require_pragma_eol (parser, pragma_tok);
49620 if (new_req == 0)
49621 error_at (loc, "%<pragma omp requires%> requires at least one clause");
49622 return false;
49626 /* OpenMP 5.1:
49627 #pragma omp nothing new-line */
49629 static void
49630 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
49632 cp_parser_require_pragma_eol (parser, pragma_tok);
49636 /* OpenMP 5.1
49637 #pragma omp error clauses[optseq] new-line */
49639 static bool
49640 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
49641 enum pragma_context context)
49643 int at_compilation = -1;
49644 int severity_fatal = -1;
49645 tree message = NULL_TREE;
49646 bool bad = false;
49647 location_t loc = pragma_tok->location;
49649 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49651 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49652 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49653 cp_lexer_consume_token (parser->lexer);
49655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
49656 break;
49658 const char *p
49659 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49660 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49661 static const char *args[] = {
49662 "execution", "compilation", "warning", "fatal"
49664 int *v = NULL;
49665 int idx = 0, n = -1;
49666 tree m = NULL_TREE;
49668 if (!strcmp (p, "at"))
49669 v = &at_compilation;
49670 else if (!strcmp (p, "severity"))
49672 v = &severity_fatal;
49673 idx += 2;
49675 else if (strcmp (p, "message"))
49677 error_at (cloc,
49678 "expected %<at%>, %<severity%> or %<message%> clause");
49679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49680 return false;
49683 cp_lexer_consume_token (parser->lexer);
49685 matching_parens parens;
49686 if (parens.require_open (parser))
49688 if (v == NULL)
49690 m = cp_parser_assignment_expression (parser);
49691 if (type_dependent_expression_p (m))
49692 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
49693 else
49694 m = perform_implicit_conversion_flags (const_string_type_node, m,
49695 tf_warning_or_error,
49696 LOOKUP_NORMAL);
49698 else
49700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49702 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
49703 const char *q = IDENTIFIER_POINTER (val);
49705 if (!strcmp (q, args[idx]))
49706 n = 0;
49707 else if (!strcmp (q, args[idx + 1]))
49708 n = 1;
49710 if (n == -1)
49712 error_at (cp_lexer_peek_token (parser->lexer)->location,
49713 "expected %qs or %qs", args[idx], args[idx + 1]);
49714 bad = true;
49715 switch (cp_lexer_peek_token (parser->lexer)->type)
49717 case CPP_EOF:
49718 case CPP_PRAGMA_EOL:
49719 case CPP_CLOSE_PAREN:
49720 break;
49721 default:
49722 if (cp_lexer_nth_token_is (parser->lexer, 2,
49723 CPP_CLOSE_PAREN))
49724 cp_lexer_consume_token (parser->lexer);
49725 break;
49728 else
49729 cp_lexer_consume_token (parser->lexer);
49732 if (!parens.require_close (parser))
49733 cp_parser_skip_to_closing_parenthesis (parser,
49734 /*recovering=*/true,
49735 /*or_comma=*/false,
49736 /*consume_paren=*/
49737 true);
49739 if (v == NULL)
49741 if (message)
49743 error_at (cloc, "too many %qs clauses", p);
49744 bad = true;
49746 else
49747 message = m;
49749 else if (n != -1)
49751 if (*v != -1)
49753 error_at (cloc, "too many %qs clauses", p);
49754 bad = true;
49756 else
49757 *v = n;
49760 else
49761 bad = true;
49763 cp_parser_require_pragma_eol (parser, pragma_tok);
49764 if (bad)
49765 return true;
49767 if (at_compilation == -1)
49768 at_compilation = 1;
49769 if (severity_fatal == -1)
49770 severity_fatal = 1;
49771 if (!at_compilation)
49773 if (context != pragma_compound)
49775 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49776 "may only be used in compound statements");
49777 return true;
49779 tree fndecl
49780 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49781 : BUILT_IN_GOMP_WARNING);
49782 if (!message)
49783 message = build_zero_cst (const_string_type_node);
49784 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49785 build_all_ones_cst (size_type_node));
49786 add_stmt (stmt);
49787 return true;
49790 if (in_discarded_stmt)
49791 return false;
49793 const char *msg = NULL;
49794 if (message)
49796 msg = c_getstr (fold_for_warn (message));
49797 if (msg == NULL)
49798 msg = _("<message unknown at compile time>");
49800 if (msg)
49801 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49802 "%<pragma omp error%> encountered: %s", msg);
49803 else
49804 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49805 "%<pragma omp error%> encountered");
49806 return false;
49809 /* OpenMP 4.5:
49810 #pragma omp taskloop taskloop-clause[optseq] new-line
49811 for-loop
49813 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
49814 for-loop */
49816 #define OMP_TASKLOOP_CLAUSE_MASK \
49817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
49818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
49819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
49820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
49821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
49822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
49823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
49824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
49825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
49826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
49827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
49828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
49829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
49830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
49831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
49832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
49833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
49835 static tree
49836 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
49837 char *p_name, omp_clause_mask mask, tree *cclauses,
49838 bool *if_p)
49840 tree clauses, sb, ret;
49841 unsigned int save;
49842 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49844 strcat (p_name, " taskloop");
49845 mask |= OMP_TASKLOOP_CLAUSE_MASK;
49846 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
49847 clause. */
49848 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
49849 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
49851 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49853 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49854 const char *p = IDENTIFIER_POINTER (id);
49856 if (strcmp (p, "simd") == 0)
49858 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
49859 if (cclauses == NULL)
49860 cclauses = cclauses_buf;
49862 cp_lexer_consume_token (parser->lexer);
49863 if (!flag_openmp) /* flag_openmp_simd */
49864 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49865 cclauses, if_p);
49866 sb = begin_omp_structured_block ();
49867 save = cp_parser_begin_omp_structured_block (parser);
49868 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49869 cclauses, if_p);
49870 cp_parser_end_omp_structured_block (parser, save);
49871 tree body = finish_omp_structured_block (sb);
49872 if (ret == NULL)
49873 return ret;
49874 ret = make_node (OMP_TASKLOOP);
49875 TREE_TYPE (ret) = void_type_node;
49876 OMP_FOR_BODY (ret) = body;
49877 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49878 SET_EXPR_LOCATION (ret, loc);
49879 add_stmt (ret);
49880 return ret;
49883 if (!flag_openmp) /* flag_openmp_simd */
49885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49886 return NULL_TREE;
49889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
49890 cclauses == NULL);
49891 if (cclauses)
49893 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
49894 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49897 keep_next_level (true);
49898 sb = begin_omp_structured_block ();
49899 save = cp_parser_begin_omp_structured_block (parser);
49901 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
49902 if_p);
49904 cp_parser_end_omp_structured_block (parser, save);
49905 add_stmt (finish_omp_structured_block (sb));
49907 return ret;
49911 /* OpenACC 2.0:
49912 # pragma acc routine oacc-routine-clause[optseq] new-line
49913 function-definition
49915 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
49918 #define OACC_ROUTINE_CLAUSE_MASK \
49919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
49920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
49921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
49922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
49923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
49925 /* Parse the OpenACC routine pragma. This has an optional '( name )'
49926 component, which must resolve to a declared namespace-scope
49927 function. The clauses are either processed directly (for a named
49928 function), or defered until the immediatley following declaration
49929 is parsed. */
49931 static void
49932 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
49933 enum pragma_context context)
49935 gcc_checking_assert (context == pragma_external);
49936 /* The checking for "another pragma following this one" in the "no optional
49937 '( name )'" case makes sure that we dont re-enter. */
49938 gcc_checking_assert (parser->oacc_routine == NULL);
49940 cp_oacc_routine_data data;
49941 data.error_seen = false;
49942 data.fndecl_seen = false;
49943 data.tokens = vNULL;
49944 data.clauses = NULL_TREE;
49945 data.loc = pragma_tok->location;
49946 /* It is safe to take the address of a local variable; it will only be
49947 used while this scope is live. */
49948 parser->oacc_routine = &data;
49950 /* Look for optional '( name )'. */
49951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49953 matching_parens parens;
49954 parens.consume_open (parser); /* '(' */
49956 /* We parse the name as an id-expression. If it resolves to
49957 anything other than a non-overloaded function at namespace
49958 scope, it's an error. */
49959 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
49960 tree name = cp_parser_id_expression (parser,
49961 /*template_keyword_p=*/false,
49962 /*check_dependency_p=*/false,
49963 /*template_p=*/NULL,
49964 /*declarator_p=*/false,
49965 /*optional_p=*/false);
49966 tree decl = (identifier_p (name)
49967 ? cp_parser_lookup_name_simple (parser, name, name_loc)
49968 : name);
49969 if (name != error_mark_node && decl == error_mark_node)
49970 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
49972 if (decl == error_mark_node
49973 || !parens.require_close (parser))
49975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49976 parser->oacc_routine = NULL;
49977 return;
49980 data.clauses
49981 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49982 "#pragma acc routine",
49983 cp_lexer_peek_token (parser->lexer));
49984 /* The clauses are in reverse order; fix that to make later diagnostic
49985 emission easier. */
49986 data.clauses = nreverse (data.clauses);
49988 if (decl && is_overloaded_fn (decl)
49989 && (TREE_CODE (decl) != FUNCTION_DECL
49990 || DECL_FUNCTION_TEMPLATE_P (decl)))
49992 error_at (name_loc,
49993 "%<#pragma acc routine%> names a set of overloads");
49994 parser->oacc_routine = NULL;
49995 return;
49998 /* Perhaps we should use the same rule as declarations in different
49999 namespaces? */
50000 if (!DECL_NAMESPACE_SCOPE_P (decl))
50002 error_at (name_loc,
50003 "%qD does not refer to a namespace scope function", decl);
50004 parser->oacc_routine = NULL;
50005 return;
50008 if (TREE_CODE (decl) != FUNCTION_DECL)
50010 error_at (name_loc, "%qD does not refer to a function", decl);
50011 parser->oacc_routine = NULL;
50012 return;
50015 cp_finalize_oacc_routine (parser, decl, false);
50016 parser->oacc_routine = NULL;
50018 else /* No optional '( name )'. */
50020 /* Store away all pragma tokens. */
50021 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50022 cp_lexer_consume_token (parser->lexer);
50023 cp_parser_require_pragma_eol (parser, pragma_tok);
50024 struct cp_token_cache *cp
50025 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
50026 parser->oacc_routine->tokens.safe_push (cp);
50028 /* Emit a helpful diagnostic if there's another pragma following this
50029 one. */
50030 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
50032 cp_ensure_no_oacc_routine (parser);
50033 data.tokens.release ();
50034 /* ..., and then just keep going. */
50035 return;
50038 /* We only have to consider the pragma_external case here. */
50039 cp_parser_declaration (parser, NULL_TREE);
50040 if (parser->oacc_routine
50041 && !parser->oacc_routine->fndecl_seen)
50042 cp_ensure_no_oacc_routine (parser);
50043 else
50044 parser->oacc_routine = NULL;
50045 data.tokens.release ();
50049 /* Finalize #pragma acc routine clauses after direct declarator has
50050 been parsed. */
50052 static tree
50053 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
50055 struct cp_token_cache *ce;
50056 cp_oacc_routine_data *data = parser->oacc_routine;
50058 if (!data->error_seen && data->fndecl_seen)
50060 error_at (data->loc,
50061 "%<#pragma acc routine%> not immediately followed by "
50062 "a single function declaration or definition");
50063 data->error_seen = true;
50065 if (data->error_seen)
50066 return attrs;
50068 gcc_checking_assert (data->tokens.length () == 1);
50069 ce = data->tokens[0];
50071 cp_parser_push_lexer_for_tokens (parser, ce);
50072 parser->lexer->in_pragma = true;
50073 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
50075 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
50076 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
50077 parser->oacc_routine->clauses
50078 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50079 "#pragma acc routine", pragma_tok);
50080 /* The clauses are in reverse order; fix that to make later diagnostic
50081 emission easier. */
50082 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
50083 cp_parser_pop_lexer (parser);
50084 /* Later, cp_finalize_oacc_routine will process the clauses. */
50085 parser->oacc_routine->fndecl_seen = true;
50087 return attrs;
50090 /* Apply any saved OpenACC routine clauses to a just-parsed
50091 declaration. */
50093 static void
50094 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
50096 if (UNLIKELY (parser->oacc_routine != NULL))
50098 /* Keep going if we're in error reporting mode. */
50099 if (parser->oacc_routine->error_seen
50100 || fndecl == error_mark_node)
50101 return;
50103 if (TREE_CODE (fndecl) != FUNCTION_DECL)
50105 if (parser->oacc_routine->fndecl_seen)
50107 error_at (parser->oacc_routine->loc,
50108 "%<#pragma acc routine%> not immediately followed by"
50109 " a single function declaration or definition");
50110 parser->oacc_routine = NULL;
50111 return;
50114 cp_ensure_no_oacc_routine (parser);
50115 return;
50118 int compatible
50119 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
50120 parser->oacc_routine->loc,
50121 "#pragma acc routine");
50122 if (compatible < 0)
50124 parser->oacc_routine = NULL;
50125 return;
50127 if (compatible > 0)
50130 else
50132 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
50134 error_at (parser->oacc_routine->loc,
50135 TREE_USED (fndecl)
50136 ? G_("%<#pragma acc routine%> must be applied before"
50137 " use")
50138 : G_("%<#pragma acc routine%> must be applied before"
50139 " definition"));
50140 parser->oacc_routine = NULL;
50141 return;
50144 /* Set the routine's level of parallelism. */
50145 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
50146 oacc_replace_fn_attrib (fndecl, dims);
50148 /* Add an "omp declare target" attribute. */
50149 DECL_ATTRIBUTES (fndecl)
50150 = tree_cons (get_identifier ("omp declare target"),
50151 parser->oacc_routine->clauses,
50152 DECL_ATTRIBUTES (fndecl));
50157 /* Main entry point to OpenMP statement pragmas. */
50159 static void
50160 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
50162 tree stmt;
50163 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
50164 omp_clause_mask mask (0);
50166 switch (cp_parser_pragma_kind (pragma_tok))
50168 case PRAGMA_OACC_ATOMIC:
50169 cp_parser_omp_atomic (parser, pragma_tok, true);
50170 return;
50171 case PRAGMA_OACC_CACHE:
50172 stmt = cp_parser_oacc_cache (parser, pragma_tok);
50173 break;
50174 case PRAGMA_OACC_DATA:
50175 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
50176 break;
50177 case PRAGMA_OACC_ENTER_DATA:
50178 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
50179 break;
50180 case PRAGMA_OACC_EXIT_DATA:
50181 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
50182 break;
50183 case PRAGMA_OACC_HOST_DATA:
50184 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
50185 break;
50186 case PRAGMA_OACC_KERNELS:
50187 case PRAGMA_OACC_PARALLEL:
50188 case PRAGMA_OACC_SERIAL:
50189 strcpy (p_name, "#pragma acc");
50190 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
50191 break;
50192 case PRAGMA_OACC_LOOP:
50193 strcpy (p_name, "#pragma acc");
50194 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
50195 if_p);
50196 break;
50197 case PRAGMA_OACC_UPDATE:
50198 stmt = cp_parser_oacc_update (parser, pragma_tok);
50199 break;
50200 case PRAGMA_OACC_WAIT:
50201 stmt = cp_parser_oacc_wait (parser, pragma_tok);
50202 break;
50203 case PRAGMA_OMP_ALLOCATE:
50204 cp_parser_omp_allocate (parser, pragma_tok);
50205 return;
50206 case PRAGMA_OMP_ATOMIC:
50207 cp_parser_omp_atomic (parser, pragma_tok, false);
50208 return;
50209 case PRAGMA_OMP_CRITICAL:
50210 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
50211 break;
50212 case PRAGMA_OMP_DISTRIBUTE:
50213 strcpy (p_name, "#pragma omp");
50214 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
50215 if_p);
50216 break;
50217 case PRAGMA_OMP_FOR:
50218 strcpy (p_name, "#pragma omp");
50219 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
50220 if_p);
50221 break;
50222 case PRAGMA_OMP_LOOP:
50223 strcpy (p_name, "#pragma omp");
50224 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
50225 if_p);
50226 break;
50227 case PRAGMA_OMP_MASKED:
50228 strcpy (p_name, "#pragma omp");
50229 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
50230 if_p);
50231 break;
50232 case PRAGMA_OMP_MASTER:
50233 strcpy (p_name, "#pragma omp");
50234 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
50235 if_p);
50236 break;
50237 case PRAGMA_OMP_PARALLEL:
50238 strcpy (p_name, "#pragma omp");
50239 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
50240 if_p);
50241 break;
50242 case PRAGMA_OMP_SCOPE:
50243 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
50244 break;
50245 case PRAGMA_OMP_SECTIONS:
50246 strcpy (p_name, "#pragma omp");
50247 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
50248 break;
50249 case PRAGMA_OMP_SIMD:
50250 strcpy (p_name, "#pragma omp");
50251 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
50252 if_p);
50253 break;
50254 case PRAGMA_OMP_SINGLE:
50255 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
50256 break;
50257 case PRAGMA_OMP_TASK:
50258 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
50259 break;
50260 case PRAGMA_OMP_TASKGROUP:
50261 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
50262 break;
50263 case PRAGMA_OMP_TASKLOOP:
50264 strcpy (p_name, "#pragma omp");
50265 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
50266 if_p);
50267 break;
50268 case PRAGMA_OMP_TEAMS:
50269 strcpy (p_name, "#pragma omp");
50270 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
50271 if_p);
50272 break;
50273 case PRAGMA_OMP_ASSUME:
50274 cp_parser_omp_assume (parser, pragma_tok, if_p);
50275 return;
50276 default:
50277 gcc_unreachable ();
50280 protected_set_expr_location (stmt, pragma_tok->location);
50283 /* Transactional Memory parsing routines. */
50285 /* Parse a transaction attribute.
50287 txn-attribute:
50288 attribute
50289 [ [ identifier ] ]
50291 We use this instead of cp_parser_attributes_opt for transactions to avoid
50292 the pedwarn in C++98 mode. */
50294 static tree
50295 cp_parser_txn_attribute_opt (cp_parser *parser)
50297 cp_token *token;
50298 tree attr_name, attr = NULL;
50300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
50301 return cp_parser_attributes_opt (parser);
50303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
50304 return NULL_TREE;
50305 cp_lexer_consume_token (parser->lexer);
50306 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
50307 goto error1;
50309 token = cp_lexer_peek_token (parser->lexer);
50310 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
50312 token = cp_lexer_consume_token (parser->lexer);
50314 attr_name = (token->type == CPP_KEYWORD
50315 /* For keywords, use the canonical spelling,
50316 not the parsed identifier. */
50317 ? ridpointers[(int) token->keyword]
50318 : token->u.value);
50319 attr = build_tree_list (attr_name, NULL_TREE);
50321 else
50322 cp_parser_error (parser, "expected identifier");
50324 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50325 error1:
50326 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50327 return attr;
50330 /* Parse a __transaction_atomic or __transaction_relaxed statement.
50332 transaction-statement:
50333 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
50334 compound-statement
50335 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
50338 static tree
50339 cp_parser_transaction (cp_parser *parser, cp_token *token)
50341 unsigned char old_in = parser->in_transaction;
50342 unsigned char this_in = 1, new_in;
50343 enum rid keyword = token->keyword;
50344 tree stmt, attrs, noex;
50346 cp_lexer_consume_token (parser->lexer);
50348 if (keyword == RID_TRANSACTION_RELAXED
50349 || keyword == RID_SYNCHRONIZED)
50350 this_in |= TM_STMT_ATTR_RELAXED;
50351 else
50353 attrs = cp_parser_txn_attribute_opt (parser);
50354 if (attrs)
50355 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50358 /* Parse a noexcept specification. */
50359 if (keyword == RID_ATOMIC_NOEXCEPT)
50360 noex = boolean_true_node;
50361 else if (keyword == RID_ATOMIC_CANCEL)
50363 /* cancel-and-throw is unimplemented. */
50364 sorry ("%<atomic_cancel%>");
50365 noex = NULL_TREE;
50367 else
50368 noex = cp_parser_noexcept_specification_opt (parser,
50369 CP_PARSER_FLAGS_NONE,
50370 /*require_constexpr=*/true,
50371 /*consumed_expr=*/NULL,
50372 /*return_cond=*/true);
50374 /* Keep track if we're in the lexical scope of an outer transaction. */
50375 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
50377 stmt = begin_transaction_stmt (token->location, NULL, this_in);
50379 parser->in_transaction = new_in;
50380 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
50381 parser->in_transaction = old_in;
50383 finish_transaction_stmt (stmt, NULL, this_in, noex);
50385 return stmt;
50388 /* Parse a __transaction_atomic or __transaction_relaxed expression.
50390 transaction-expression:
50391 __transaction_atomic txn-noexcept-spec[opt] ( expression )
50392 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
50395 static tree
50396 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
50398 unsigned char old_in = parser->in_transaction;
50399 unsigned char this_in = 1;
50400 cp_token *token;
50401 tree expr, noex;
50402 bool noex_expr;
50403 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50405 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50406 || keyword == RID_TRANSACTION_RELAXED);
50408 if (!flag_tm)
50409 error_at (loc,
50410 keyword == RID_TRANSACTION_RELAXED
50411 ? G_("%<__transaction_relaxed%> without transactional memory "
50412 "support enabled")
50413 : G_("%<__transaction_atomic%> without transactional memory "
50414 "support enabled"));
50416 token = cp_parser_require_keyword (parser, keyword,
50417 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50418 : RT_TRANSACTION_RELAXED));
50419 gcc_assert (token != NULL);
50421 if (keyword == RID_TRANSACTION_RELAXED)
50422 this_in |= TM_STMT_ATTR_RELAXED;
50424 /* Set this early. This might mean that we allow transaction_cancel in
50425 an expression that we find out later actually has to be a constexpr.
50426 However, we expect that cxx_constant_value will be able to deal with
50427 this; also, if the noexcept has no constexpr, then what we parse next
50428 really is a transaction's body. */
50429 parser->in_transaction = this_in;
50431 /* Parse a noexcept specification. */
50432 noex = cp_parser_noexcept_specification_opt (parser,
50433 CP_PARSER_FLAGS_NONE,
50434 /*require_constexpr=*/false,
50435 &noex_expr,
50436 /*return_cond=*/true);
50438 if (!noex || !noex_expr
50439 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
50441 matching_parens parens;
50442 parens.require_open (parser);
50444 expr = cp_parser_expression (parser);
50445 expr = finish_parenthesized_expr (expr);
50447 parens.require_close (parser);
50449 else
50451 /* The only expression that is available got parsed for the noexcept
50452 already. noexcept is true then. */
50453 expr = noex;
50454 noex = boolean_true_node;
50457 expr = build_transaction_expr (token->location, expr, this_in, noex);
50458 parser->in_transaction = old_in;
50460 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
50461 return error_mark_node;
50463 return (flag_tm ? expr : error_mark_node);
50466 /* Parse a function-transaction-block.
50468 function-transaction-block:
50469 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
50470 function-body
50471 __transaction_atomic txn-attribute[opt] function-try-block
50472 __transaction_relaxed ctor-initializer[opt] function-body
50473 __transaction_relaxed function-try-block
50476 static void
50477 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
50479 unsigned char old_in = parser->in_transaction;
50480 unsigned char new_in = 1;
50481 tree compound_stmt, stmt, attrs;
50482 cp_token *token;
50484 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50485 || keyword == RID_TRANSACTION_RELAXED);
50486 token = cp_parser_require_keyword (parser, keyword,
50487 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50488 : RT_TRANSACTION_RELAXED));
50489 gcc_assert (token != NULL);
50491 if (keyword == RID_TRANSACTION_RELAXED)
50492 new_in |= TM_STMT_ATTR_RELAXED;
50493 else
50495 attrs = cp_parser_txn_attribute_opt (parser);
50496 if (attrs)
50497 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50500 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
50502 parser->in_transaction = new_in;
50504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
50505 cp_parser_function_try_block (parser);
50506 else
50507 cp_parser_ctor_initializer_opt_and_function_body
50508 (parser, /*in_function_try_block=*/false);
50510 parser->in_transaction = old_in;
50512 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
50515 /* Parse a __transaction_cancel statement.
50517 cancel-statement:
50518 __transaction_cancel txn-attribute[opt] ;
50519 __transaction_cancel txn-attribute[opt] throw-expression ;
50521 ??? Cancel and throw is not yet implemented. */
50523 static tree
50524 cp_parser_transaction_cancel (cp_parser *parser)
50526 cp_token *token;
50527 bool is_outer = false;
50528 tree stmt, attrs;
50530 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
50531 RT_TRANSACTION_CANCEL);
50532 gcc_assert (token != NULL);
50534 attrs = cp_parser_txn_attribute_opt (parser);
50535 if (attrs)
50536 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
50538 /* ??? Parse cancel-and-throw here. */
50540 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
50542 if (!flag_tm)
50544 error_at (token->location, "%<__transaction_cancel%> without "
50545 "transactional memory support enabled");
50546 return error_mark_node;
50548 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
50550 error_at (token->location, "%<__transaction_cancel%> within a "
50551 "%<__transaction_relaxed%>");
50552 return error_mark_node;
50554 else if (is_outer)
50556 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
50557 && !is_tm_may_cancel_outer (current_function_decl))
50559 error_at (token->location, "outer %<__transaction_cancel%> not "
50560 "within outer %<__transaction_atomic%>");
50561 error_at (token->location,
50562 " or a %<transaction_may_cancel_outer%> function");
50563 return error_mark_node;
50566 else if (parser->in_transaction == 0)
50568 error_at (token->location, "%<__transaction_cancel%> not within "
50569 "%<__transaction_atomic%>");
50570 return error_mark_node;
50573 stmt = build_tm_abort_call (token->location, is_outer);
50574 add_stmt (stmt);
50576 return stmt;
50580 /* Special handling for the first token or line in the file. The first
50581 thing in the file might be #pragma GCC pch_preprocess, which loads a
50582 PCH file, which is a GC collection point. So we need to handle this
50583 first pragma without benefit of an existing lexer structure.
50585 Always returns one token to the caller in *FIRST_TOKEN. This is
50586 either the true first token of the file, or the first token after
50587 the initial pragma. */
50589 static void
50590 cp_parser_initial_pragma (cp_token *first_token)
50592 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
50593 return;
50595 cp_lexer_get_preprocessor_token (0, first_token);
50597 tree name = NULL;
50598 if (first_token->type == CPP_STRING)
50600 name = first_token->u.value;
50602 cp_lexer_get_preprocessor_token (0, first_token);
50605 /* Skip to the end of the pragma. */
50606 if (first_token->type != CPP_PRAGMA_EOL)
50608 error_at (first_token->location,
50609 "malformed %<#pragma GCC pch_preprocess%>");
50611 cp_lexer_get_preprocessor_token (0, first_token);
50612 while (first_token->type != CPP_PRAGMA_EOL);
50615 /* Now actually load the PCH file. */
50616 if (name)
50617 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
50619 /* Read one more token to return to our caller. We have to do this
50620 after reading the PCH file in, since its pointers have to be
50621 live. */
50622 cp_lexer_get_preprocessor_token (0, first_token);
50625 /* Parse a pragma GCC ivdep. */
50627 static bool
50628 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
50630 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50631 return true;
50634 /* Parse a pragma GCC unroll. */
50636 static tree
50637 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
50639 location_t location = cp_lexer_peek_token (parser->lexer)->location;
50640 tree unroll = cp_parser_constant_expression (parser);
50641 unroll = cp_check_pragma_unroll (location, fold_non_dependent_expr (unroll));
50642 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50643 return unroll;
50646 /* Parse a pragma GCC novector. */
50648 static bool
50649 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
50651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50652 return true;
50655 /* Normal parsing of a pragma token. Here we can (and must) use the
50656 regular lexer. */
50658 static bool
50659 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
50661 cp_token *pragma_tok;
50662 unsigned int id;
50663 tree stmt;
50664 bool ret = false;
50666 pragma_tok = cp_lexer_consume_token (parser->lexer);
50667 gcc_assert (pragma_tok->type == CPP_PRAGMA);
50668 parser->lexer->in_pragma = true;
50670 id = cp_parser_pragma_kind (pragma_tok);
50671 if (parser->omp_for_parse_state
50672 && parser->omp_for_parse_state->in_intervening_code
50673 && id >= PRAGMA_OMP__START_
50674 && id <= PRAGMA_OMP__LAST_)
50676 error_at (pragma_tok->location,
50677 "intervening code must not contain OpenMP directives");
50678 parser->omp_for_parse_state->fail = true;
50679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50680 return false;
50682 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
50683 cp_ensure_no_omp_declare_simd (parser);
50684 switch (id)
50686 case PRAGMA_GCC_PCH_PREPROCESS:
50687 error_at (pragma_tok->location,
50688 "%<#pragma GCC pch_preprocess%> must be first");
50689 break;
50691 case PRAGMA_OMP_BARRIER:
50692 switch (context)
50694 case pragma_compound:
50695 cp_parser_omp_barrier (parser, pragma_tok);
50696 return false;
50697 case pragma_stmt:
50698 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50699 "used in compound statements", "omp barrier");
50700 ret = true;
50701 break;
50702 default:
50703 goto bad_stmt;
50705 break;
50707 case PRAGMA_OMP_DEPOBJ:
50708 switch (context)
50710 case pragma_compound:
50711 cp_parser_omp_depobj (parser, pragma_tok);
50712 return false;
50713 case pragma_stmt:
50714 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50715 "used in compound statements", "omp depobj");
50716 ret = true;
50717 break;
50718 default:
50719 goto bad_stmt;
50721 break;
50723 case PRAGMA_OMP_FLUSH:
50724 switch (context)
50726 case pragma_compound:
50727 cp_parser_omp_flush (parser, pragma_tok);
50728 return false;
50729 case pragma_stmt:
50730 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50731 "used in compound statements", "omp flush");
50732 ret = true;
50733 break;
50734 default:
50735 goto bad_stmt;
50737 break;
50739 case PRAGMA_OMP_TASKWAIT:
50740 switch (context)
50742 case pragma_compound:
50743 cp_parser_omp_taskwait (parser, pragma_tok);
50744 return false;
50745 case pragma_stmt:
50746 error_at (pragma_tok->location,
50747 "%<#pragma %s%> may only be used in compound statements",
50748 "omp taskwait");
50749 ret = true;
50750 break;
50751 default:
50752 goto bad_stmt;
50754 break;
50756 case PRAGMA_OMP_TASKYIELD:
50757 switch (context)
50759 case pragma_compound:
50760 cp_parser_omp_taskyield (parser, pragma_tok);
50761 return false;
50762 case pragma_stmt:
50763 error_at (pragma_tok->location,
50764 "%<#pragma %s%> may only be used in compound statements",
50765 "omp taskyield");
50766 ret = true;
50767 break;
50768 default:
50769 goto bad_stmt;
50771 break;
50773 case PRAGMA_OMP_CANCEL:
50774 switch (context)
50776 case pragma_compound:
50777 cp_parser_omp_cancel (parser, pragma_tok);
50778 return false;
50779 case pragma_stmt:
50780 error_at (pragma_tok->location,
50781 "%<#pragma %s%> may only be used in compound statements",
50782 "omp cancel");
50783 ret = true;
50784 break;
50785 default:
50786 goto bad_stmt;
50788 break;
50790 case PRAGMA_OMP_CANCELLATION_POINT:
50791 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
50793 case PRAGMA_OMP_THREADPRIVATE:
50794 cp_parser_omp_threadprivate (parser, pragma_tok);
50795 return false;
50797 case PRAGMA_OMP_DECLARE:
50798 return cp_parser_omp_declare (parser, pragma_tok, context);
50800 case PRAGMA_OACC_DECLARE:
50801 cp_parser_oacc_declare (parser, pragma_tok);
50802 return false;
50804 case PRAGMA_OACC_ENTER_DATA:
50805 if (context == pragma_stmt)
50807 error_at (pragma_tok->location,
50808 "%<#pragma %s%> may only be used in compound statements",
50809 "acc enter data");
50810 ret = true;
50811 break;
50813 else if (context != pragma_compound)
50814 goto bad_stmt;
50815 cp_parser_omp_construct (parser, pragma_tok, if_p);
50816 return true;
50818 case PRAGMA_OACC_EXIT_DATA:
50819 if (context == pragma_stmt)
50821 error_at (pragma_tok->location,
50822 "%<#pragma %s%> may only be used in compound statements",
50823 "acc exit data");
50824 ret = true;
50825 break;
50827 else if (context != pragma_compound)
50828 goto bad_stmt;
50829 cp_parser_omp_construct (parser, pragma_tok, if_p);
50830 return true;
50832 case PRAGMA_OACC_ROUTINE:
50833 if (context != pragma_external)
50835 error_at (pragma_tok->location,
50836 "%<#pragma acc routine%> must be at file scope");
50837 ret = true;
50838 break;
50840 cp_parser_oacc_routine (parser, pragma_tok, context);
50841 return false;
50843 case PRAGMA_OACC_UPDATE:
50844 if (context == pragma_stmt)
50846 error_at (pragma_tok->location,
50847 "%<#pragma %s%> may only be used in compound statements",
50848 "acc update");
50849 ret = true;
50850 break;
50852 else if (context != pragma_compound)
50853 goto bad_stmt;
50854 cp_parser_omp_construct (parser, pragma_tok, if_p);
50855 return true;
50857 case PRAGMA_OACC_WAIT:
50858 if (context == pragma_stmt)
50860 error_at (pragma_tok->location,
50861 "%<#pragma %s%> may only be used in compound statements",
50862 "acc wait");
50863 ret = true;
50864 break;
50866 else if (context != pragma_compound)
50867 goto bad_stmt;
50868 cp_parser_omp_construct (parser, pragma_tok, if_p);
50869 return true;
50870 case PRAGMA_OMP_ALLOCATE:
50871 cp_parser_omp_allocate (parser, pragma_tok);
50872 return false;
50873 case PRAGMA_OACC_ATOMIC:
50874 case PRAGMA_OACC_CACHE:
50875 case PRAGMA_OACC_DATA:
50876 case PRAGMA_OACC_HOST_DATA:
50877 case PRAGMA_OACC_KERNELS:
50878 case PRAGMA_OACC_LOOP:
50879 case PRAGMA_OACC_PARALLEL:
50880 case PRAGMA_OACC_SERIAL:
50881 case PRAGMA_OMP_ASSUME:
50882 case PRAGMA_OMP_ATOMIC:
50883 case PRAGMA_OMP_CRITICAL:
50884 case PRAGMA_OMP_DISTRIBUTE:
50885 case PRAGMA_OMP_FOR:
50886 case PRAGMA_OMP_LOOP:
50887 case PRAGMA_OMP_MASKED:
50888 case PRAGMA_OMP_MASTER:
50889 case PRAGMA_OMP_PARALLEL:
50890 case PRAGMA_OMP_SCOPE:
50891 case PRAGMA_OMP_SECTIONS:
50892 case PRAGMA_OMP_SIMD:
50893 case PRAGMA_OMP_SINGLE:
50894 case PRAGMA_OMP_TASK:
50895 case PRAGMA_OMP_TASKGROUP:
50896 case PRAGMA_OMP_TASKLOOP:
50897 case PRAGMA_OMP_TEAMS:
50898 if (context != pragma_stmt && context != pragma_compound)
50899 goto bad_stmt;
50900 stmt = push_omp_privatization_clauses (false);
50901 cp_parser_omp_construct (parser, pragma_tok, if_p);
50902 pop_omp_privatization_clauses (stmt);
50903 return true;
50905 case PRAGMA_OMP_REQUIRES:
50906 if (context != pragma_external)
50908 error_at (pragma_tok->location,
50909 "%<#pragma omp requires%> may only be used at file or "
50910 "namespace scope");
50911 ret = true;
50912 break;
50914 return cp_parser_omp_requires (parser, pragma_tok);
50916 case PRAGMA_OMP_ASSUMES:
50917 if (context != pragma_external)
50919 error_at (pragma_tok->location,
50920 "%<#pragma omp assumes%> may only be used at file or "
50921 "namespace scope");
50922 ret = true;
50923 break;
50925 return cp_parser_omp_assumes (parser, pragma_tok);
50927 case PRAGMA_OMP_NOTHING:
50928 cp_parser_omp_nothing (parser, pragma_tok);
50929 return false;
50931 case PRAGMA_OMP_ERROR:
50932 return cp_parser_omp_error (parser, pragma_tok, context);
50934 case PRAGMA_OMP_ORDERED:
50935 if (context != pragma_stmt && context != pragma_compound)
50936 goto bad_stmt;
50937 stmt = push_omp_privatization_clauses (false);
50938 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
50939 pop_omp_privatization_clauses (stmt);
50940 return ret;
50942 case PRAGMA_OMP_TARGET:
50943 if (context != pragma_stmt && context != pragma_compound)
50944 goto bad_stmt;
50945 stmt = push_omp_privatization_clauses (false);
50946 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
50947 pop_omp_privatization_clauses (stmt);
50948 return ret;
50950 case PRAGMA_OMP_BEGIN:
50951 cp_parser_omp_begin (parser, pragma_tok);
50952 return false;
50954 case PRAGMA_OMP_END:
50955 cp_parser_omp_end (parser, pragma_tok);
50956 return false;
50958 case PRAGMA_OMP_SCAN:
50959 error_at (pragma_tok->location,
50960 "%<#pragma omp scan%> may only be used in "
50961 "a loop construct with %<inscan%> %<reduction%> clause");
50962 break;
50964 case PRAGMA_OMP_SECTION:
50965 error_at (pragma_tok->location,
50966 "%<#pragma omp section%> may only be used in "
50967 "%<#pragma omp sections%> construct");
50968 break;
50970 case PRAGMA_IVDEP:
50971 case PRAGMA_UNROLL:
50972 case PRAGMA_NOVECTOR:
50974 bool ivdep;
50975 tree unroll = NULL_TREE;
50976 bool novector = false;
50977 const char *pragma_str;
50979 switch (id)
50981 case PRAGMA_IVDEP:
50982 pragma_str = "ivdep";
50983 break;
50984 case PRAGMA_UNROLL:
50985 pragma_str = "unroll";
50986 break;
50987 case PRAGMA_NOVECTOR:
50988 pragma_str = "novector";
50989 break;
50990 default:
50991 gcc_unreachable ();
50994 if (context == pragma_external)
50996 error_at (pragma_tok->location,
50997 "%<#pragma GCC %s%> must be inside a function",
50998 pragma_str);
50999 break;
51002 cp_token *tok = pragma_tok;
51003 bool has_more = true;
51006 switch (cp_parser_pragma_kind (tok))
51008 case PRAGMA_IVDEP:
51010 if (tok != pragma_tok)
51011 tok = cp_lexer_consume_token (parser->lexer);
51012 ivdep = cp_parser_pragma_ivdep (parser, tok);
51013 break;
51015 case PRAGMA_UNROLL:
51017 if (tok != pragma_tok)
51018 tok = cp_lexer_consume_token (parser->lexer);
51019 unroll = cp_parser_pragma_unroll (parser, tok);
51020 break;
51022 case PRAGMA_NOVECTOR:
51024 if (tok != pragma_tok)
51025 tok = cp_lexer_consume_token (parser->lexer);
51026 novector = cp_parser_pragma_novector (parser, tok);
51027 break;
51029 default:
51030 has_more = false;
51031 break;
51033 tok = cp_lexer_peek_token (the_parser->lexer);
51034 has_more = has_more && tok->type == CPP_PRAGMA;
51036 while (has_more);
51038 if (tok->type != CPP_KEYWORD
51039 || (tok->keyword != RID_FOR
51040 && tok->keyword != RID_WHILE
51041 && tok->keyword != RID_DO))
51043 cp_parser_error (parser, "for, while or do statement expected");
51044 return false;
51046 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
51047 return true;
51050 default:
51051 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
51052 c_invoke_pragma_handler (id);
51053 break;
51055 bad_stmt:
51056 cp_parser_error (parser, "expected declaration specifiers");
51057 break;
51060 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51061 return ret;
51064 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
51065 populated the lexer with any tokens (the tokens rather being read by
51066 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
51067 a pragma. */
51068 static void
51069 maybe_read_tokens_for_pragma_lex ()
51071 const auto lexer = the_parser->lexer;
51072 if (!lexer->buffer->is_empty ())
51073 return;
51075 /* Read the rest of the tokens comprising the pragma line. */
51076 cp_token *tok;
51079 tok = vec_safe_push (lexer->buffer, cp_token ());
51080 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
51081 gcc_assert (tok->type != CPP_EOF);
51082 } while (tok->type != CPP_PRAGMA_EOL);
51083 lexer->next_token = lexer->buffer->address ();
51084 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
51087 /* The interface the pragma parsers have to the lexer. */
51089 enum cpp_ttype
51090 pragma_lex (tree *value, location_t *loc)
51092 if (flag_preprocess_only)
51093 maybe_read_tokens_for_pragma_lex ();
51095 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
51096 enum cpp_ttype ret = tok->type;
51098 *value = tok->u.value;
51099 if (loc)
51100 *loc = tok->location;
51102 if (ret == CPP_PRAGMA_EOL)
51103 ret = CPP_EOF;
51104 else if (ret == CPP_STRING)
51105 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
51106 /*wide_ok=*/false);
51107 else
51109 if (ret == CPP_KEYWORD)
51110 ret = CPP_NAME;
51111 cp_lexer_consume_token (the_parser->lexer);
51114 return ret;
51117 void
51118 pragma_lex_discard_to_eol ()
51120 /* We have already read all the tokens, so we just need to discard
51121 them here. */
51122 const auto lexer = the_parser->lexer;
51123 lexer->next_token = lexer->last_token;
51124 lexer->buffer->truncate (0);
51128 /* External interface. */
51130 /* Parse one entire translation unit. */
51132 void
51133 c_parse_file (void)
51135 static bool already_called = false;
51137 if (already_called)
51138 fatal_error (input_location,
51139 "multi-source compilation not implemented for C++");
51140 already_called = true;
51142 /* cp_lexer_new_main is called before doing any GC allocation
51143 because tokenization might load a PCH file. */
51144 cp_lexer_new_main ();
51146 cp_parser_translation_unit (the_parser);
51147 class_decl_loc_t::diag_mismatched_tags ();
51149 the_parser = NULL;
51151 finish_translation_unit ();
51154 /* Create an identifier for a generic parameter type (a synthesized
51155 template parameter implied by `auto' or a concept identifier). */
51157 static GTY(()) int generic_parm_count;
51158 static tree
51159 make_generic_type_name ()
51161 char buf[32];
51162 sprintf (buf, "auto:%d", ++generic_parm_count);
51163 return get_identifier (buf);
51166 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
51167 (creating a new template parameter list if necessary). Returns the newly
51168 created template type parm. */
51170 static tree
51171 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
51173 /* A requires-clause is not a function and cannot have placeholders. */
51174 if (current_binding_level->requires_expression)
51176 error ("placeholder type not allowed in this context");
51177 return error_mark_node;
51180 gcc_assert (current_binding_level->kind == sk_function_parms);
51182 /* We are either continuing a function template that already contains implicit
51183 template parameters, creating a new fully-implicit function template, or
51184 extending an existing explicit function template with implicit template
51185 parameters. */
51187 cp_binding_level *const entry_scope = current_binding_level;
51189 bool become_template = false;
51190 cp_binding_level *parent_scope = 0;
51192 if (parser->implicit_template_scope)
51194 gcc_assert (parser->implicit_template_parms);
51196 current_binding_level = parser->implicit_template_scope;
51198 else
51200 /* Roll back to the existing template parameter scope (in the case of
51201 extending an explicit function template) or introduce a new template
51202 parameter scope ahead of the function parameter scope (or class scope
51203 in the case of out-of-line member definitions). The function scope is
51204 added back after template parameter synthesis below. */
51206 cp_binding_level *scope = entry_scope;
51208 while (scope->kind == sk_function_parms)
51210 parent_scope = scope;
51211 scope = scope->level_chain;
51213 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
51215 /* If not defining a class, then any class scope is a scope level in
51216 an out-of-line member definition. In this case simply wind back
51217 beyond the first such scope to inject the template parameter list.
51218 Otherwise wind back to the class being defined. The latter can
51219 occur in class member friend declarations such as:
51221 class A {
51222 void foo (auto);
51224 class B {
51225 friend void A::foo (auto);
51228 The template parameter list synthesized for the friend declaration
51229 must be injected in the scope of 'B'. This can also occur in
51230 erroneous cases such as:
51232 struct A {
51233 struct B {
51234 void foo (auto);
51236 void B::foo (auto) {}
51239 Here the attempted definition of 'B::foo' within 'A' is ill-formed
51240 but, nevertheless, the template parameter list synthesized for the
51241 declarator should be injected into the scope of 'A' as if the
51242 ill-formed template was specified explicitly. */
51244 while (scope->kind == sk_class && !scope->defining_class_p)
51246 parent_scope = scope;
51247 scope = scope->level_chain;
51251 current_binding_level = scope;
51253 if (scope->kind != sk_template_parms
51254 || !function_being_declared_is_template_p (parser))
51256 /* Introduce a new template parameter list for implicit template
51257 parameters. */
51259 become_template = true;
51261 parser->implicit_template_scope
51262 = begin_scope (sk_template_parms, NULL);
51264 ++processing_template_decl;
51266 parser->fully_implicit_function_template_p = true;
51267 ++parser->num_template_parameter_lists;
51269 else
51271 /* Synthesize implicit template parameters at the end of the explicit
51272 template parameter list. */
51274 gcc_assert (current_template_parms);
51276 parser->implicit_template_scope = scope;
51278 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51279 parser->implicit_template_parms
51280 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
51284 /* Synthesize a new template parameter and track the current template
51285 parameter chain with implicit_template_parms. */
51287 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
51288 tree synth_id = make_generic_type_name ();
51289 bool non_type = false;
51291 /* Synthesize the type template parameter. */
51292 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
51293 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
51295 if (become_template)
51296 current_template_parms = tree_cons (size_int (current_template_depth + 1),
51297 NULL_TREE, current_template_parms);
51299 /* Attach the constraint to the parm before processing. */
51300 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
51301 TREE_TYPE (node) = constr;
51302 tree new_parm
51303 = process_template_parm (parser->implicit_template_parms,
51304 input_location,
51305 node,
51306 /*non_type=*/non_type,
51307 /*param_pack=*/false);
51308 // Process_template_parm returns the list of parms, and
51309 // parser->implicit_template_parms holds the final node of the parm
51310 // list. We really want to manipulate the newly appended element.
51311 gcc_checking_assert (!parser->implicit_template_parms
51312 || parser->implicit_template_parms == new_parm);
51313 if (parser->implicit_template_parms)
51314 new_parm = TREE_CHAIN (new_parm);
51315 gcc_checking_assert (!TREE_CHAIN (new_parm));
51317 // Record the last implicit parm node
51318 parser->implicit_template_parms = new_parm;
51320 /* Mark the synthetic declaration "virtual". This is used when
51321 comparing template-heads to determine if whether an abbreviated
51322 function template is equivalent to an explicit template.
51324 Note that DECL_ARTIFICIAL is used elsewhere for template
51325 parameters. */
51326 if (TREE_VALUE (new_parm) != error_mark_node)
51327 DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (new_parm)) = true;
51329 tree new_decl = get_local_decls ();
51330 if (non_type)
51331 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
51332 new_decl = DECL_INITIAL (new_decl);
51334 /* If creating a fully implicit function template, start the new implicit
51335 template parameter list with this synthesized type, otherwise grow the
51336 current template parameter list. */
51338 if (become_template)
51340 parent_scope->level_chain = current_binding_level;
51342 tree new_parms = make_tree_vec (1);
51343 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
51344 TREE_VALUE (current_template_parms) = new_parms;
51346 else
51348 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51349 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
51350 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
51351 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
51354 /* If the new parameter was constrained, we need to add that to the
51355 constraints in the template parameter list. */
51356 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
51358 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
51359 reqs = combine_constraint_expressions (reqs, req);
51360 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
51363 current_binding_level = entry_scope;
51365 return new_decl;
51368 /* Finish the declaration of a fully implicit function template. Such a
51369 template has no explicit template parameter list so has not been through the
51370 normal template head and tail processing. synthesize_implicit_template_parm
51371 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
51372 provided if the declaration is a class member such that its template
51373 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
51374 form is returned. Otherwise NULL_TREE is returned. */
51376 static tree
51377 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
51379 gcc_assert (parser->fully_implicit_function_template_p);
51381 if (member_decl_opt && member_decl_opt != error_mark_node
51382 && DECL_VIRTUAL_P (member_decl_opt))
51384 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
51385 "implicit templates may not be %<virtual%>");
51386 DECL_VIRTUAL_P (member_decl_opt) = false;
51389 if (member_decl_opt)
51390 member_decl_opt = finish_member_template_decl (member_decl_opt);
51391 end_template_decl ();
51393 parser->fully_implicit_function_template_p = false;
51394 parser->implicit_template_parms = 0;
51395 parser->implicit_template_scope = 0;
51396 --parser->num_template_parameter_lists;
51398 return member_decl_opt;
51401 /* Like finish_fully_implicit_template, but to be used in error
51402 recovery, rearranging scopes so that we restore the state we had
51403 before synthesize_implicit_template_parm inserted the implement
51404 template parms scope. */
51406 static void
51407 abort_fully_implicit_template (cp_parser *parser)
51409 cp_binding_level *return_to_scope = current_binding_level;
51411 if (parser->implicit_template_scope
51412 && return_to_scope != parser->implicit_template_scope)
51414 cp_binding_level *child = return_to_scope;
51415 for (cp_binding_level *scope = child->level_chain;
51416 scope != parser->implicit_template_scope;
51417 scope = child->level_chain)
51418 child = scope;
51419 child->level_chain = parser->implicit_template_scope->level_chain;
51420 parser->implicit_template_scope->level_chain = return_to_scope;
51421 current_binding_level = parser->implicit_template_scope;
51423 else
51424 return_to_scope = return_to_scope->level_chain;
51426 finish_fully_implicit_template (parser, NULL);
51428 gcc_assert (current_binding_level == return_to_scope);
51431 /* Helper function for diagnostics that have complained about things
51432 being used with 'extern "C"' linkage.
51434 Attempt to issue a note showing where the 'extern "C"' linkage began. */
51436 void
51437 maybe_show_extern_c_location (void)
51439 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
51440 inform (the_parser->innermost_linkage_specification_location,
51441 "%<extern \"C\"%> linkage started here");
51444 #include "gt-cp-parser.h"