c++: Diagnose this specifier on template parameters [PR113929]
[official-gcc.git] / gcc / cp / parser.cc
blobb2ed2baa3a5701598f1c3cdb315591982751da2d
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 " HOST_SIZE_T_PRINT_DEC " at token: ",
1314 (fmt_size_t) n);
1316 --n;
1317 token = lexer->next_token;
1318 while (n && token->type != CPP_EOF)
1320 ++token;
1321 if (!token->purged_p)
1322 --n;
1325 if (cp_lexer_debugging_p (lexer))
1327 cp_lexer_print_token (cp_lexer_debug_stream, token);
1328 putc ('\n', cp_lexer_debug_stream);
1331 return token;
1334 /* Return the next token, and advance the lexer's next_token pointer
1335 to point to the next non-purged token. */
1337 static cp_token *
1338 cp_lexer_consume_token (cp_lexer* lexer)
1340 cp_token *token = lexer->next_token;
1344 gcc_assert (token->type != CPP_EOF);
1345 lexer->next_token++;
1347 while (lexer->next_token->purged_p);
1349 cp_lexer_set_source_position_from_token (token);
1351 /* Provide debugging output. */
1352 if (cp_lexer_debugging_p (lexer))
1354 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1355 cp_lexer_print_token (cp_lexer_debug_stream, token);
1356 putc ('\n', cp_lexer_debug_stream);
1359 return token;
1362 /* Permanently remove the next token from the token stream, and
1363 advance the next_token pointer to refer to the next non-purged
1364 token. */
1366 static void
1367 cp_lexer_purge_token (cp_lexer *lexer)
1369 cp_token *tok = lexer->next_token;
1371 gcc_assert (tok->type != CPP_EOF);
1372 tok->purged_p = true;
1373 tok->location = UNKNOWN_LOCATION;
1374 tok->u.value = NULL_TREE;
1375 tok->keyword = RID_MAX;
1378 tok++;
1379 while (tok->purged_p);
1380 lexer->next_token = tok;
1383 /* Permanently remove all tokens after TOK, up to, but not
1384 including, the token that will be returned next by
1385 cp_lexer_peek_token. */
1387 static void
1388 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1390 cp_token *peek = lexer->next_token;
1392 gcc_assert (tok < peek);
1394 for (tok++; tok != peek; tok++)
1396 tok->purged_p = true;
1397 tok->location = UNKNOWN_LOCATION;
1398 tok->u.value = NULL_TREE;
1399 tok->keyword = RID_MAX;
1403 /* Begin saving tokens. All tokens consumed after this point will be
1404 preserved. */
1406 static void
1407 cp_lexer_save_tokens (cp_lexer* lexer)
1409 /* Provide debugging output. */
1410 if (cp_lexer_debugging_p (lexer))
1411 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1413 lexer->saved_tokens.safe_push (lexer->next_token);
1416 /* Commit to the portion of the token stream most recently saved. */
1418 static void
1419 cp_lexer_commit_tokens (cp_lexer* lexer)
1421 /* Provide debugging output. */
1422 if (cp_lexer_debugging_p (lexer))
1423 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1425 lexer->saved_tokens.pop ();
1428 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1429 to the token stream. Stop saving tokens. */
1431 static void
1432 cp_lexer_rollback_tokens (cp_lexer* lexer)
1434 /* Provide debugging output. */
1435 if (cp_lexer_debugging_p (lexer))
1436 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1438 lexer->next_token = lexer->saved_tokens.pop ();
1441 /* Determines what saved_token_sentinel does when going out of scope. */
1443 enum saved_token_sentinel_mode {
1444 STS_COMMIT,
1445 STS_ROLLBACK,
1446 STS_DONOTHING
1449 /* RAII wrapper around the above functions, with sanity checking (the token
1450 stream should be the same at the point of instantiation as it is at the
1451 point of destruction).
1453 Creating a variable saves tokens. MODE determines what happens when the
1454 object is destroyed. STS_COMMIT commits tokens (default),
1455 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1456 rollback() will immediately roll-back tokens and set MODE to
1457 STS_DONOTHING. */
1459 struct saved_token_sentinel
1461 cp_lexer *lexer;
1462 unsigned len;
1463 saved_token_sentinel_mode mode;
1464 saved_token_sentinel (cp_lexer *_lexer,
1465 saved_token_sentinel_mode _mode = STS_COMMIT)
1466 : lexer (_lexer), mode (_mode)
1468 len = lexer->saved_tokens.length ();
1469 cp_lexer_save_tokens (lexer);
1471 void rollback ()
1473 cp_lexer_rollback_tokens (lexer);
1474 cp_lexer_set_source_position_from_token
1475 (cp_lexer_previous_token (lexer));
1476 mode = STS_DONOTHING;
1478 ~saved_token_sentinel ()
1480 if (mode == STS_COMMIT)
1481 cp_lexer_commit_tokens (lexer);
1482 else if (mode == STS_ROLLBACK)
1483 rollback ();
1485 gcc_assert (lexer->saved_tokens.length () == len);
1489 /* Print a representation of the TOKEN on the STREAM. */
1491 static void
1492 cp_lexer_print_token (FILE * stream, cp_token *token)
1494 /* We don't use cpp_type2name here because the parser defines
1495 a few tokens of its own. */
1496 static const char *const token_names[] = {
1497 /* cpplib-defined token types */
1498 #define OP(e, s) #e,
1499 #define TK(e, s) #e,
1500 TTYPE_TABLE
1501 #undef OP
1502 #undef TK
1503 /* C++ parser token types - see "Manifest constants", above. */
1504 "KEYWORD",
1505 "TEMPLATE_ID",
1506 "NESTED_NAME_SPECIFIER",
1509 /* For some tokens, print the associated data. */
1510 switch (token->type)
1512 case CPP_KEYWORD:
1513 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1514 For example, `struct' is mapped to an INTEGER_CST. */
1515 if (!identifier_p (token->u.value))
1516 break;
1517 /* fall through */
1518 case CPP_NAME:
1519 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1520 break;
1522 case CPP_STRING:
1523 case CPP_STRING16:
1524 case CPP_STRING32:
1525 case CPP_WSTRING:
1526 case CPP_UTF8STRING:
1527 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1528 break;
1530 case CPP_NUMBER:
1531 print_generic_expr (stream, token->u.value);
1532 break;
1534 default:
1535 /* If we have a name for the token, print it out. Otherwise, we
1536 simply give the numeric code. */
1537 if (token->type < ARRAY_SIZE(token_names))
1538 fputs (token_names[token->type], stream);
1539 else
1540 fprintf (stream, "[%d]", token->type);
1541 break;
1545 DEBUG_FUNCTION void
1546 debug (cp_token &ref)
1548 cp_lexer_print_token (stderr, &ref);
1549 fprintf (stderr, "\n");
1552 DEBUG_FUNCTION void
1553 debug (cp_token *ptr)
1555 if (ptr)
1556 debug (*ptr);
1557 else
1558 fprintf (stderr, "<nil>\n");
1562 /* Start emitting debugging information. */
1564 static void
1565 cp_lexer_start_debugging (cp_lexer* lexer)
1567 if (!LEXER_DEBUGGING_ENABLED_P)
1568 fatal_error (input_location,
1569 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1571 lexer->debugging_p = true;
1572 cp_lexer_debug_stream = stderr;
1575 /* Stop emitting debugging information. */
1577 static void
1578 cp_lexer_stop_debugging (cp_lexer* lexer)
1580 if (!LEXER_DEBUGGING_ENABLED_P)
1581 fatal_error (input_location,
1582 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1584 lexer->debugging_p = false;
1585 cp_lexer_debug_stream = NULL;
1588 /* Create a new cp_token_cache, representing a range of tokens. */
1590 static cp_token_cache *
1591 cp_token_cache_new (cp_token *first, cp_token *last)
1593 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1594 cache->first = first;
1595 cache->last = last;
1596 return cache;
1599 /* Diagnose if #pragma omp declare simd isn't followed immediately
1600 by function declaration or definition. */
1602 static inline void
1603 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1605 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1607 error ("%<#pragma omp declare %s%> not immediately followed by "
1608 "function declaration or definition",
1609 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1610 parser->omp_declare_simd = NULL;
1614 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1615 and put that into "omp declare simd" attribute. */
1617 static inline void
1618 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1620 if (UNLIKELY (parser->omp_declare_simd != NULL))
1622 if (fndecl == error_mark_node)
1624 parser->omp_declare_simd = NULL;
1625 return;
1627 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1629 cp_ensure_no_omp_declare_simd (parser);
1630 return;
1635 /* Similarly, but for use in declaration parsing functions
1636 which call cp_parser_handle_directive_omp_attributes. */
1638 static inline void
1639 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1641 if (parser->omp_declare_simd != data)
1642 return;
1644 if (!parser->omp_declare_simd->error_seen
1645 && !parser->omp_declare_simd->fndecl_seen)
1646 error_at (parser->omp_declare_simd->loc,
1647 "%<declare %s%> directive not immediately followed by "
1648 "function declaration or definition",
1649 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1650 parser->omp_declare_simd = NULL;
1653 /* Diagnose if #pragma acc routine isn't followed immediately by function
1654 declaration or definition. */
1656 static inline void
1657 cp_ensure_no_oacc_routine (cp_parser *parser)
1659 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1661 error_at (parser->oacc_routine->loc,
1662 "%<#pragma acc routine%> not immediately followed by "
1663 "function declaration or definition");
1664 parser->oacc_routine = NULL;
1668 /* Decl-specifiers. */
1670 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1672 static void
1673 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1675 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1678 /* Declarators. */
1680 /* Nothing other than the parser should be creating declarators;
1681 declarators are a semi-syntactic representation of C++ entities.
1682 Other parts of the front end that need to create entities (like
1683 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1685 static cp_declarator *make_call_declarator
1686 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1687 tree, tree, tree, tree, tree, location_t);
1688 static cp_declarator *make_array_declarator
1689 (cp_declarator *, tree);
1690 static cp_declarator *make_pointer_declarator
1691 (cp_cv_quals, cp_declarator *, tree);
1692 static cp_declarator *make_reference_declarator
1693 (cp_cv_quals, cp_declarator *, bool, tree);
1694 static cp_declarator *make_ptrmem_declarator
1695 (cp_cv_quals, tree, cp_declarator *, tree);
1697 /* An erroneous declarator. */
1698 static cp_declarator *cp_error_declarator;
1700 /* The obstack on which declarators and related data structures are
1701 allocated. */
1702 static struct obstack declarator_obstack;
1704 /* Alloc BYTES from the declarator memory pool. */
1706 static inline void *
1707 alloc_declarator (size_t bytes)
1709 return obstack_alloc (&declarator_obstack, bytes);
1712 /* Allocate a declarator of the indicated KIND. Clear fields that are
1713 common to all declarators. */
1715 static cp_declarator *
1716 make_declarator (cp_declarator_kind kind)
1718 cp_declarator *declarator;
1720 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1721 declarator->kind = kind;
1722 declarator->parenthesized = UNKNOWN_LOCATION;
1723 declarator->attributes = NULL_TREE;
1724 declarator->std_attributes = NULL_TREE;
1725 declarator->declarator = NULL;
1726 declarator->parameter_pack_p = false;
1727 declarator->id_loc = UNKNOWN_LOCATION;
1728 declarator->init_loc = UNKNOWN_LOCATION;
1730 return declarator;
1733 /* Make a declarator for a generalized identifier. If
1734 QUALIFYING_SCOPE is non-NULL, the identifier is
1735 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1736 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1737 is, if any. */
1739 static cp_declarator *
1740 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1741 special_function_kind sfk, location_t id_location)
1743 cp_declarator *declarator;
1745 /* It is valid to write:
1747 class C { void f(); };
1748 typedef C D;
1749 void D::f();
1751 The standard is not clear about whether `typedef const C D' is
1752 legal; as of 2002-09-15 the committee is considering that
1753 question. EDG 3.0 allows that syntax. Therefore, we do as
1754 well. */
1755 if (qualifying_scope && TYPE_P (qualifying_scope))
1756 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1758 gcc_assert (identifier_p (unqualified_name)
1759 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1760 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1762 declarator = make_declarator (cdk_id);
1763 declarator->u.id.qualifying_scope = qualifying_scope;
1764 declarator->u.id.unqualified_name = unqualified_name;
1765 declarator->u.id.sfk = sfk;
1766 declarator->id_loc = id_location;
1768 return declarator;
1771 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1772 of modifiers such as const or volatile to apply to the pointer
1773 type, represented as identifiers. ATTRIBUTES represent the attributes that
1774 appertain to the pointer or reference. */
1776 cp_declarator *
1777 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1778 tree attributes)
1780 cp_declarator *declarator;
1782 declarator = make_declarator (cdk_pointer);
1783 declarator->declarator = target;
1784 declarator->u.pointer.qualifiers = cv_qualifiers;
1785 declarator->u.pointer.class_type = NULL_TREE;
1786 if (target)
1788 declarator->id_loc = target->id_loc;
1789 declarator->parameter_pack_p = target->parameter_pack_p;
1790 target->parameter_pack_p = false;
1792 else
1793 declarator->parameter_pack_p = false;
1795 declarator->std_attributes = attributes;
1797 return declarator;
1800 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1801 represent the attributes that appertain to the pointer or
1802 reference. */
1804 cp_declarator *
1805 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1806 bool rvalue_ref, tree attributes)
1808 cp_declarator *declarator;
1810 declarator = make_declarator (cdk_reference);
1811 declarator->declarator = target;
1812 declarator->u.reference.qualifiers = cv_qualifiers;
1813 declarator->u.reference.rvalue_ref = rvalue_ref;
1814 if (target)
1816 declarator->id_loc = target->id_loc;
1817 declarator->parameter_pack_p = target->parameter_pack_p;
1818 target->parameter_pack_p = false;
1820 else
1821 declarator->parameter_pack_p = false;
1823 declarator->std_attributes = attributes;
1825 return declarator;
1828 /* Like make_pointer_declarator -- but for a pointer to a non-static
1829 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1830 appertain to the pointer or reference. */
1832 cp_declarator *
1833 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1834 cp_declarator *pointee,
1835 tree attributes)
1837 cp_declarator *declarator;
1839 declarator = make_declarator (cdk_ptrmem);
1840 declarator->declarator = pointee;
1841 declarator->u.pointer.qualifiers = cv_qualifiers;
1842 declarator->u.pointer.class_type = class_type;
1844 if (pointee)
1846 declarator->parameter_pack_p = pointee->parameter_pack_p;
1847 pointee->parameter_pack_p = false;
1849 else
1850 declarator->parameter_pack_p = false;
1852 declarator->std_attributes = attributes;
1854 return declarator;
1857 /* Make a declarator for the function given by TARGET, with the
1858 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1859 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1860 indicates what exceptions can be thrown. STD_ATTRS contains
1861 attributes that appertain to the function type. */
1863 cp_declarator *
1864 make_call_declarator (cp_declarator *target,
1865 tree parms,
1866 cp_cv_quals cv_qualifiers,
1867 cp_virt_specifiers virt_specifiers,
1868 cp_ref_qualifier ref_qualifier,
1869 tree tx_qualifier,
1870 tree exception_specification,
1871 tree late_return_type,
1872 tree requires_clause,
1873 tree std_attrs,
1874 location_t parens_loc)
1876 cp_declarator *declarator;
1878 declarator = make_declarator (cdk_function);
1879 declarator->declarator = target;
1880 declarator->u.function.parameters = parms;
1881 declarator->u.function.qualifiers = cv_qualifiers;
1882 declarator->u.function.virt_specifiers = virt_specifiers;
1883 declarator->u.function.ref_qualifier = ref_qualifier;
1884 declarator->u.function.tx_qualifier = tx_qualifier;
1885 declarator->u.function.exception_specification = exception_specification;
1886 declarator->u.function.late_return_type = late_return_type;
1887 declarator->u.function.requires_clause = requires_clause;
1888 declarator->u.function.parens_loc = parens_loc;
1889 if (target)
1891 declarator->id_loc = target->id_loc;
1892 declarator->parameter_pack_p = target->parameter_pack_p;
1893 target->parameter_pack_p = false;
1895 else
1896 declarator->parameter_pack_p = false;
1898 declarator->std_attributes = std_attrs;
1900 return declarator;
1903 /* Make a declarator for an array of BOUNDS elements, each of which is
1904 defined by ELEMENT. */
1906 cp_declarator *
1907 make_array_declarator (cp_declarator *element, tree bounds)
1909 cp_declarator *declarator;
1911 declarator = make_declarator (cdk_array);
1912 declarator->declarator = element;
1913 declarator->u.array.bounds = bounds;
1914 if (element)
1916 declarator->id_loc = element->id_loc;
1917 declarator->parameter_pack_p = element->parameter_pack_p;
1918 element->parameter_pack_p = false;
1920 else
1921 declarator->parameter_pack_p = false;
1923 return declarator;
1926 /* Determine whether the declarator we've seen so far can be a
1927 parameter pack, when followed by an ellipsis. */
1928 static bool
1929 declarator_can_be_parameter_pack (cp_declarator *declarator)
1931 if (declarator && declarator->parameter_pack_p)
1932 /* We already saw an ellipsis. */
1933 return false;
1935 /* Search for a declarator name, or any other declarator that goes
1936 after the point where the ellipsis could appear in a parameter
1937 pack. If we find any of these, then this declarator cannot be
1938 made into a parameter pack. */
1939 bool found = false;
1940 while (declarator && !found)
1942 switch ((int)declarator->kind)
1944 case cdk_id:
1945 case cdk_array:
1946 case cdk_decomp:
1947 found = true;
1948 break;
1950 case cdk_error:
1951 return true;
1953 default:
1954 declarator = declarator->declarator;
1955 break;
1959 return !found;
1962 cp_parameter_declarator *no_parameters;
1964 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1965 DECLARATOR and DEFAULT_ARGUMENT. */
1967 cp_parameter_declarator *
1968 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1969 cp_declarator *declarator,
1970 tree default_argument,
1971 location_t loc,
1972 bool template_parameter_pack_p = false)
1974 cp_parameter_declarator *parameter;
1976 parameter = ((cp_parameter_declarator *)
1977 alloc_declarator (sizeof (cp_parameter_declarator)));
1978 parameter->next = NULL;
1979 if (decl_specifiers)
1980 parameter->decl_specifiers = *decl_specifiers;
1981 else
1982 clear_decl_specs (&parameter->decl_specifiers);
1983 parameter->declarator = declarator;
1984 parameter->default_argument = default_argument;
1985 parameter->template_parameter_pack_p = template_parameter_pack_p;
1986 parameter->loc = loc;
1988 return parameter;
1991 /* Returns true iff DECLARATOR is a declaration for a function. */
1993 static bool
1994 function_declarator_p (const cp_declarator *declarator)
1996 while (declarator)
1998 if (declarator->kind == cdk_function
1999 && declarator->declarator->kind == cdk_id)
2000 return true;
2001 if (declarator->kind == cdk_id
2002 || declarator->kind == cdk_decomp
2003 || declarator->kind == cdk_error)
2004 return false;
2005 declarator = declarator->declarator;
2007 return false;
2010 /* The parser. */
2012 /* Overview
2013 --------
2015 A cp_parser parses the token stream as specified by the C++
2016 grammar. Its job is purely parsing, not semantic analysis. For
2017 example, the parser breaks the token stream into declarators,
2018 expressions, statements, and other similar syntactic constructs.
2019 It does not check that the types of the expressions on either side
2020 of an assignment-statement are compatible, or that a function is
2021 not declared with a parameter of type `void'.
2023 The parser invokes routines elsewhere in the compiler to perform
2024 semantic analysis and to build up the abstract syntax tree for the
2025 code processed.
2027 The parser (and the template instantiation code, which is, in a
2028 way, a close relative of parsing) are the only parts of the
2029 compiler that should be calling push_scope and pop_scope, or
2030 related functions. The parser (and template instantiation code)
2031 keeps track of what scope is presently active; everything else
2032 should simply honor that. (The code that generates static
2033 initializers may also need to set the scope, in order to check
2034 access control correctly when emitting the initializers.)
2036 Methodology
2037 -----------
2039 The parser is of the standard recursive-descent variety. Upcoming
2040 tokens in the token stream are examined in order to determine which
2041 production to use when parsing a non-terminal. Some C++ constructs
2042 require arbitrary look ahead to disambiguate. For example, it is
2043 impossible, in the general case, to tell whether a statement is an
2044 expression or declaration without scanning the entire statement.
2045 Therefore, the parser is capable of "parsing tentatively." When the
2046 parser is not sure what construct comes next, it enters this mode.
2047 Then, while we attempt to parse the construct, the parser queues up
2048 error messages, rather than issuing them immediately, and saves the
2049 tokens it consumes. If the construct is parsed successfully, the
2050 parser "commits", i.e., it issues any queued error messages and
2051 the tokens that were being preserved are permanently discarded.
2052 If, however, the construct is not parsed successfully, the parser
2053 rolls back its state completely so that it can resume parsing using
2054 a different alternative.
2056 Future Improvements
2057 -------------------
2059 The performance of the parser could probably be improved substantially.
2060 We could often eliminate the need to parse tentatively by looking ahead
2061 a little bit. In some places, this approach might not entirely eliminate
2062 the need to parse tentatively, but it might still speed up the average
2063 case. */
2065 /* Flags that are passed to some parsing functions. These values can
2066 be bitwise-ored together. */
2068 enum
2070 /* No flags. */
2071 CP_PARSER_FLAGS_NONE = 0x0,
2072 /* The construct is optional. If it is not present, then no error
2073 should be issued. */
2074 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2075 /* When parsing a type-specifier, treat user-defined type-names
2076 as non-type identifiers. */
2077 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2078 /* When parsing a type-specifier, do not try to parse a class-specifier
2079 or enum-specifier. */
2080 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2081 /* When parsing a decl-specifier-seq, only allow type-specifier or
2082 constexpr. */
2083 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2084 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2085 for C++20 consteval or for C++23 static. */
2086 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2087 /* When parsing a decl-specifier-seq, allow missing typename. */
2088 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2089 /* When parsing of the noexcept-specifier should be delayed. */
2090 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2091 /* When parsing a consteval declarator. */
2092 CP_PARSER_FLAGS_CONSTEVAL = 0x80,
2093 /* When parsing a parameter declaration. */
2094 CP_PARSER_FLAGS_PARAMETER = 0x100
2097 /* This type is used for parameters and variables which hold
2098 combinations of the above flags. */
2099 typedef int cp_parser_flags;
2101 /* The different kinds of declarators we want to parse. */
2103 enum cp_parser_declarator_kind
2105 /* We want an abstract declarator. */
2106 CP_PARSER_DECLARATOR_ABSTRACT,
2107 /* We want a named declarator. */
2108 CP_PARSER_DECLARATOR_NAMED,
2109 /* We don't mind, but the name must be an unqualified-id. */
2110 CP_PARSER_DECLARATOR_EITHER
2113 /* The precedence values used to parse binary expressions. The minimum value
2114 of PREC must be 1, because zero is reserved to quickly discriminate
2115 binary operators from other tokens. */
2117 enum cp_parser_prec
2119 PREC_NOT_OPERATOR,
2120 PREC_LOGICAL_OR_EXPRESSION,
2121 PREC_LOGICAL_AND_EXPRESSION,
2122 PREC_INCLUSIVE_OR_EXPRESSION,
2123 PREC_EXCLUSIVE_OR_EXPRESSION,
2124 PREC_AND_EXPRESSION,
2125 PREC_EQUALITY_EXPRESSION,
2126 PREC_RELATIONAL_EXPRESSION,
2127 PREC_SPACESHIP_EXPRESSION,
2128 PREC_SHIFT_EXPRESSION,
2129 PREC_ADDITIVE_EXPRESSION,
2130 PREC_MULTIPLICATIVE_EXPRESSION,
2131 PREC_PM_EXPRESSION,
2132 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2135 /* A mapping from a token type to a corresponding tree node type, with a
2136 precedence value. */
2138 struct cp_parser_binary_operations_map_node
2140 /* The token type. */
2141 enum cpp_ttype token_type;
2142 /* The corresponding tree code. */
2143 enum tree_code tree_type;
2144 /* The precedence of this operator. */
2145 enum cp_parser_prec prec;
2148 struct cp_parser_expression_stack_entry
2150 /* Left hand side of the binary operation we are currently
2151 parsing. */
2152 cp_expr lhs;
2153 /* Original tree code for left hand side, if it was a binary
2154 expression itself (used for -Wparentheses). */
2155 enum tree_code lhs_type;
2156 /* Tree code for the binary operation we are parsing. */
2157 enum tree_code tree_type;
2158 /* Precedence of the binary operation we are parsing. */
2159 enum cp_parser_prec prec;
2160 /* Location of the binary operation we are parsing. */
2161 location_t loc;
2162 /* Flags from the operator token. */
2163 unsigned char flags;
2166 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2167 entries because precedence levels on the stack are monotonically
2168 increasing. */
2169 typedef struct cp_parser_expression_stack_entry
2170 cp_parser_expression_stack[NUM_PREC_VALUES];
2172 /* Used for parsing OMP for loops.
2174 Some notes on flags used for context:
2175 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2176 construct, except for the final-loop-body.
2177 The want_nested_loop flag is true if inside a {} sequence where
2178 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2179 but has not yet been seen. It's false when parsing intervening code
2180 statements or their substatements that cannot contain a loop-nest.
2181 The in_intervening_code flag is true when parsing any intervening code,
2182 including substatements, and whether or not want_nested_loop is true.
2184 And, about error handling:
2185 The saw_intervening_code flag is set if the loop is not perfectly
2186 nested, even in the usual case where this is not an error.
2187 perfect_nesting_fail is set if an error has been diagnosed because an
2188 imperfectly-nested loop was found where a perfectly-nested one is
2189 required (we diagnose this only once).
2190 fail is set if any kind of structural error in the loop nest
2191 has been found and diagnosed.
2193 struct omp_for_parse_data {
2194 enum tree_code code;
2195 tree declv, condv, incrv, initv;
2196 tree pre_body;
2197 tree orig_declv;
2198 auto_vec<tree, 4> orig_inits;
2199 int count; /* Expected nesting depth. */
2200 int depth; /* Current nesting depth. */
2201 location_t for_loc;
2202 releasing_vec init_blockv;
2203 releasing_vec body_blockv;
2204 releasing_vec init_placeholderv;
2205 releasing_vec body_placeholderv;
2206 bool ordered : 1;
2207 bool inscan : 1;
2208 bool want_nested_loop : 1;
2209 bool in_intervening_code : 1;
2210 bool saw_intervening_code : 1;
2211 bool perfect_nesting_fail : 1;
2212 bool fail : 1;
2213 tree clauses;
2214 tree *cclauses;
2215 tree ordered_cl;
2218 /* Prototypes. */
2220 /* Constructors and destructors. */
2222 static cp_parser_context *cp_parser_context_new
2223 (cp_parser_context *);
2225 /* Class variables. */
2227 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2229 /* The operator-precedence table used by cp_parser_binary_expression.
2230 Transformed into an associative array (binops_by_token) by
2231 cp_parser_new. */
2233 static const cp_parser_binary_operations_map_node binops[] = {
2234 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2235 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2237 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2238 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2239 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2241 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2242 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2244 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2245 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2247 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2249 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2250 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2251 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2252 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2254 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2255 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2257 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2259 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2261 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2263 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2265 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2268 /* The same as binops, but initialized by cp_parser_new so that
2269 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2270 for speed. */
2271 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2273 /* Constructors and destructors. */
2275 /* Construct a new context. The context below this one on the stack
2276 is given by NEXT. */
2278 static cp_parser_context *
2279 cp_parser_context_new (cp_parser_context* next)
2281 cp_parser_context *context;
2283 /* Allocate the storage. */
2284 if (cp_parser_context_free_list != NULL)
2286 /* Pull the first entry from the free list. */
2287 context = cp_parser_context_free_list;
2288 cp_parser_context_free_list = context->next;
2289 memset (context, 0, sizeof (*context));
2291 else
2292 context = ggc_cleared_alloc<cp_parser_context> ();
2294 /* No errors have occurred yet in this context. */
2295 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2296 /* If this is not the bottommost context, copy information that we
2297 need from the previous context. */
2298 if (next)
2300 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2301 expression, then we are parsing one in this context, too. */
2302 context->object_type = next->object_type;
2303 /* Thread the stack. */
2304 context->next = next;
2307 return context;
2310 /* Managing the unparsed function queues. */
2312 #define unparsed_funs_with_default_args \
2313 parser->unparsed_queues->last ().funs_with_default_args
2314 #define unparsed_funs_with_definitions \
2315 parser->unparsed_queues->last ().funs_with_definitions
2316 #define unparsed_nsdmis \
2317 parser->unparsed_queues->last ().nsdmis
2318 #define unparsed_noexcepts \
2319 parser->unparsed_queues->last ().noexcepts
2320 #define unparsed_contracts \
2321 parser->unparsed_queues->last ().contracts
2323 static void
2324 push_unparsed_function_queues (cp_parser *parser)
2326 cp_unparsed_functions_entry e
2327 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2328 vec_safe_push (parser->unparsed_queues, e);
2331 static void
2332 pop_unparsed_function_queues (cp_parser *parser)
2334 release_tree_vector (unparsed_funs_with_definitions);
2335 parser->unparsed_queues->pop ();
2338 /* Prototypes. */
2340 /* Routines to parse various constructs.
2342 Those that return `tree' will return the error_mark_node (rather
2343 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2344 Sometimes, they will return an ordinary node if error-recovery was
2345 attempted, even though a parse error occurred. So, to check
2346 whether or not a parse error occurred, you should always use
2347 cp_parser_error_occurred. If the construct is optional (indicated
2348 either by an `_opt' in the name of the function that does the
2349 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2350 the construct is not present. */
2352 /* Lexical conventions [gram.lex] */
2354 static tree finish_userdef_string_literal
2355 (tree);
2357 /* Basic concepts [gram.basic] */
2359 static void cp_parser_translation_unit (cp_parser *);
2361 /* Expressions [gram.expr] */
2363 static cp_expr cp_parser_primary_expression
2364 (cp_parser *, bool, bool, bool, cp_id_kind *);
2365 static cp_expr cp_parser_id_expression
2366 (cp_parser *, bool, bool, bool *, bool, bool);
2367 static cp_expr cp_parser_unqualified_id
2368 (cp_parser *, bool, bool, bool, bool);
2369 static tree cp_parser_nested_name_specifier_opt
2370 (cp_parser *, bool, bool, bool, bool, bool = false);
2371 static tree cp_parser_nested_name_specifier
2372 (cp_parser *, bool, bool, bool, bool);
2373 static tree cp_parser_qualifying_entity
2374 (cp_parser *, bool, bool, bool, bool, bool);
2375 static cp_expr cp_parser_postfix_expression
2376 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2377 static tree cp_parser_postfix_open_square_expression
2378 (cp_parser *, tree, bool, bool);
2379 static tree cp_parser_postfix_dot_deref_expression
2380 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2381 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2382 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2383 bool = false);
2384 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2385 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3,
2386 uneval_string_attr = 4 };
2387 static void cp_parser_pseudo_destructor_name
2388 (cp_parser *, tree, tree *, tree *);
2389 static cp_expr cp_parser_unary_expression
2390 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2391 static enum tree_code cp_parser_unary_operator
2392 (cp_token *);
2393 static tree cp_parser_has_attribute_expression
2394 (cp_parser *);
2395 static tree cp_parser_new_expression
2396 (cp_parser *);
2397 static vec<tree, va_gc> *cp_parser_new_placement
2398 (cp_parser *);
2399 static tree cp_parser_new_type_id
2400 (cp_parser *, tree *);
2401 static cp_declarator *cp_parser_new_declarator_opt
2402 (cp_parser *);
2403 static cp_declarator *cp_parser_direct_new_declarator
2404 (cp_parser *);
2405 static vec<tree, va_gc> *cp_parser_new_initializer
2406 (cp_parser *);
2407 static tree cp_parser_delete_expression
2408 (cp_parser *);
2409 static cp_expr cp_parser_cast_expression
2410 (cp_parser *, bool, bool, bool, cp_id_kind *);
2411 static cp_expr cp_parser_binary_expression
2412 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2413 static tree cp_parser_question_colon_clause
2414 (cp_parser *, cp_expr);
2415 static cp_expr cp_parser_conditional_expression (cp_parser *);
2416 static cp_expr cp_parser_assignment_expression
2417 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2418 static enum tree_code cp_parser_assignment_operator_opt
2419 (cp_parser *);
2420 static cp_expr cp_parser_expression
2421 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2422 static cp_expr cp_parser_constant_expression
2423 (cp_parser *, int = 0, bool * = NULL, bool = false);
2424 static cp_expr cp_parser_builtin_offsetof
2425 (cp_parser *);
2426 static cp_expr cp_parser_lambda_expression
2427 (cp_parser *);
2428 static void cp_parser_lambda_introducer
2429 (cp_parser *, tree);
2430 static bool cp_parser_lambda_declarator_opt
2431 (cp_parser *, tree);
2432 static void cp_parser_lambda_body
2433 (cp_parser *, tree);
2435 /* Statements [gram.stmt.stmt] */
2437 static void cp_parser_statement
2438 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2439 static void cp_parser_label_for_labeled_statement
2440 (cp_parser *, tree);
2441 static tree cp_parser_expression_statement
2442 (cp_parser *, tree);
2443 static tree cp_parser_compound_statement
2444 (cp_parser *, tree, int, bool);
2445 static void cp_parser_statement_seq_opt
2446 (cp_parser *, tree);
2447 static tree cp_parser_selection_statement
2448 (cp_parser *, bool *, vec<tree> *);
2449 static tree cp_parser_condition
2450 (cp_parser *);
2451 static tree cp_parser_iteration_statement
2452 (cp_parser *, bool *, bool, tree, bool);
2453 static bool cp_parser_init_statement
2454 (cp_parser *, tree *decl);
2455 static tree cp_parser_for
2456 (cp_parser *, bool, tree, bool);
2457 static tree cp_parser_c_for
2458 (cp_parser *, tree, tree, bool, tree, bool);
2459 static tree cp_parser_range_for
2460 (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
2461 static void do_range_for_auto_deduction
2462 (tree, tree, cp_decomp *);
2463 static tree cp_parser_perform_range_for_lookup
2464 (tree, tree *, tree *);
2465 static tree cp_parser_range_for_member_function
2466 (tree, tree);
2467 static tree cp_parser_jump_statement
2468 (cp_parser *);
2469 static void cp_parser_declaration_statement
2470 (cp_parser *);
2472 static tree cp_parser_implicitly_scoped_statement
2473 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2474 static void cp_parser_already_scoped_statement
2475 (cp_parser *, bool *, const token_indent_info &);
2477 /* State of module-declaration parsing. */
2478 enum module_parse
2480 MP_NOT_MODULE, /* Not a module. */
2482 _MP_UNUSED,
2484 MP_FIRST, /* First declaration of TU. */
2485 MP_GLOBAL, /* Global Module Fragment. */
2487 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2488 MP_PURVIEW, /* Purview of a named module. */
2490 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2491 MP_PRIVATE, /* Private Module Fragment. */
2494 static module_parse cp_parser_module_declaration
2495 (cp_parser *parser, module_parse, bool exporting);
2496 static void cp_parser_import_declaration
2497 (cp_parser *parser, module_parse, bool exporting);
2499 /* Declarations [gram.dcl.dcl] */
2501 static void cp_parser_declaration_seq_opt
2502 (cp_parser *);
2503 static void cp_parser_declaration
2504 (cp_parser *, tree);
2505 static void cp_parser_toplevel_declaration
2506 (cp_parser *);
2507 static void cp_parser_block_declaration
2508 (cp_parser *, bool);
2509 static void cp_parser_simple_declaration
2510 (cp_parser *, bool, tree *);
2511 static void cp_parser_decl_specifier_seq
2512 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2513 static tree cp_parser_storage_class_specifier_opt
2514 (cp_parser *);
2515 static tree cp_parser_function_specifier_opt
2516 (cp_parser *, cp_decl_specifier_seq *);
2517 static tree cp_parser_type_specifier
2518 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2519 int *, bool *);
2520 static tree cp_parser_simple_type_specifier
2521 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2522 static tree cp_parser_placeholder_type_specifier
2523 (cp_parser *, location_t, tree, bool);
2524 static tree cp_parser_type_name
2525 (cp_parser *, bool);
2526 static tree cp_parser_nonclass_name
2527 (cp_parser* parser);
2528 static tree cp_parser_elaborated_type_specifier
2529 (cp_parser *, bool, bool);
2530 static tree cp_parser_enum_specifier
2531 (cp_parser *);
2532 static void cp_parser_enumerator_list
2533 (cp_parser *, tree);
2534 static void cp_parser_enumerator_definition
2535 (cp_parser *, tree);
2536 static tree cp_parser_namespace_name
2537 (cp_parser *);
2538 static void cp_parser_namespace_definition
2539 (cp_parser *);
2540 static void cp_parser_namespace_body
2541 (cp_parser *);
2542 static tree cp_parser_qualified_namespace_specifier
2543 (cp_parser *);
2544 static void cp_parser_namespace_alias_definition
2545 (cp_parser *);
2546 static bool cp_parser_using_declaration
2547 (cp_parser *, bool);
2548 static void cp_parser_using_directive
2549 (cp_parser *);
2550 static void cp_parser_using_enum
2551 (cp_parser *);
2552 static tree cp_parser_alias_declaration
2553 (cp_parser *);
2554 static void cp_parser_asm_definition
2555 (cp_parser *);
2556 static void cp_parser_linkage_specification
2557 (cp_parser *, tree);
2558 static void cp_parser_static_assert
2559 (cp_parser *, bool);
2560 static tree cp_parser_decltype
2561 (cp_parser *);
2562 static tree cp_parser_decomposition_declaration
2563 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2565 /* Declarators [gram.dcl.decl] */
2567 static tree cp_parser_init_declarator
2568 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2569 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2570 location_t *, tree *);
2571 static cp_declarator *cp_parser_declarator
2572 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2573 bool, bool, bool);
2574 static cp_declarator *cp_parser_direct_declarator
2575 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2576 bool);
2577 static enum tree_code cp_parser_ptr_operator
2578 (cp_parser *, tree *, cp_cv_quals *, tree *);
2579 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2580 (cp_parser *);
2581 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2582 (cp_parser *);
2583 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2584 (cp_parser *);
2585 static tree cp_parser_tx_qualifier_opt
2586 (cp_parser *);
2587 static tree cp_parser_late_return_type_opt
2588 (cp_parser *, cp_declarator *, tree &);
2589 static tree cp_parser_declarator_id
2590 (cp_parser *, bool);
2591 static tree cp_parser_type_id
2592 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2593 static tree cp_parser_template_type_arg
2594 (cp_parser *);
2595 static tree cp_parser_trailing_type_id (cp_parser *);
2596 static tree cp_parser_type_id_1
2597 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2598 static void cp_parser_type_specifier_seq
2599 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2600 static tree cp_parser_parameter_declaration_clause
2601 (cp_parser *, cp_parser_flags);
2602 static tree cp_parser_parameter_declaration_list
2603 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2604 static cp_parameter_declarator *cp_parser_parameter_declaration
2605 (cp_parser *, cp_parser_flags, bool, bool *);
2606 static tree cp_parser_default_argument
2607 (cp_parser *, bool);
2608 static void cp_parser_function_body
2609 (cp_parser *, bool);
2610 static tree cp_parser_initializer
2611 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2612 static cp_expr cp_parser_initializer_clause
2613 (cp_parser *, bool * = nullptr);
2614 static cp_expr cp_parser_braced_list
2615 (cp_parser*, bool * = nullptr);
2616 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2617 (cp_parser *, bool *, bool *);
2619 static void cp_parser_ctor_initializer_opt_and_function_body
2620 (cp_parser *, bool);
2622 static tree cp_parser_late_parsing_omp_declare_simd
2623 (cp_parser *, tree);
2625 static tree cp_parser_late_parsing_oacc_routine
2626 (cp_parser *, tree);
2628 static tree synthesize_implicit_template_parm
2629 (cp_parser *, tree);
2630 static tree finish_fully_implicit_template
2631 (cp_parser *, tree);
2632 static void abort_fully_implicit_template
2633 (cp_parser *);
2635 /* Classes [gram.class] */
2637 static tree cp_parser_class_name
2638 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2639 static tree cp_parser_class_specifier
2640 (cp_parser *);
2641 static tree cp_parser_class_head
2642 (cp_parser *, bool *);
2643 static enum tag_types cp_parser_class_key
2644 (cp_parser *);
2645 static void cp_parser_type_parameter_key
2646 (cp_parser* parser);
2647 static void cp_parser_member_specification_opt
2648 (cp_parser *);
2649 static void cp_parser_member_declaration
2650 (cp_parser *);
2651 static tree cp_parser_pure_specifier
2652 (cp_parser *);
2653 static tree cp_parser_constant_initializer
2654 (cp_parser *);
2656 /* Derived classes [gram.class.derived] */
2658 static tree cp_parser_base_clause
2659 (cp_parser *);
2660 static tree cp_parser_base_specifier
2661 (cp_parser *);
2663 /* Special member functions [gram.special] */
2665 static tree cp_parser_conversion_function_id
2666 (cp_parser *);
2667 static tree cp_parser_conversion_type_id
2668 (cp_parser *);
2669 static cp_declarator *cp_parser_conversion_declarator_opt
2670 (cp_parser *);
2671 static void cp_parser_ctor_initializer_opt
2672 (cp_parser *);
2673 static void cp_parser_mem_initializer_list
2674 (cp_parser *);
2675 static tree cp_parser_mem_initializer
2676 (cp_parser *);
2677 static tree cp_parser_mem_initializer_id
2678 (cp_parser *);
2680 /* Overloading [gram.over] */
2682 static cp_expr cp_parser_operator_function_id
2683 (cp_parser *);
2684 static cp_expr cp_parser_operator
2685 (cp_parser *, location_t);
2687 /* Templates [gram.temp] */
2689 static void cp_parser_template_declaration
2690 (cp_parser *, bool);
2691 static tree cp_parser_template_parameter_list
2692 (cp_parser *);
2693 static tree cp_parser_template_parameter
2694 (cp_parser *, bool *, bool *);
2695 static tree cp_parser_type_parameter
2696 (cp_parser *, bool *);
2697 static tree cp_parser_template_id
2698 (cp_parser *, bool, bool, enum tag_types, bool);
2699 static tree cp_parser_template_id_expr
2700 (cp_parser *, bool, bool, bool);
2701 static tree cp_parser_template_name
2702 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2703 static tree cp_parser_template_argument_list
2704 (cp_parser *);
2705 static tree cp_parser_template_argument
2706 (cp_parser *);
2707 static void cp_parser_explicit_instantiation
2708 (cp_parser *);
2709 static void cp_parser_explicit_specialization
2710 (cp_parser *);
2712 /* Exception handling [gram.except] */
2714 static tree cp_parser_try_block
2715 (cp_parser *);
2716 static void cp_parser_function_try_block
2717 (cp_parser *);
2718 static void cp_parser_handler_seq
2719 (cp_parser *);
2720 static void cp_parser_handler
2721 (cp_parser *);
2722 static tree cp_parser_exception_declaration
2723 (cp_parser *);
2724 static tree cp_parser_throw_expression
2725 (cp_parser *);
2726 static tree cp_parser_exception_specification_opt
2727 (cp_parser *, cp_parser_flags);
2728 static tree cp_parser_type_id_list
2729 (cp_parser *);
2730 static tree cp_parser_noexcept_specification_opt
2731 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2733 /* GNU Extensions */
2735 static tree cp_parser_asm_specification_opt
2736 (cp_parser *);
2737 static tree cp_parser_asm_operand_list
2738 (cp_parser *);
2739 static tree cp_parser_asm_clobber_list
2740 (cp_parser *);
2741 static tree cp_parser_asm_label_list
2742 (cp_parser *);
2743 static bool cp_next_tokens_can_be_attribute_p
2744 (cp_parser *);
2745 static bool cp_next_tokens_can_be_gnu_attribute_p
2746 (cp_parser *);
2747 static bool cp_next_tokens_can_be_std_attribute_p
2748 (cp_parser *);
2749 static bool cp_nth_tokens_can_be_std_attribute_p
2750 (cp_parser *, size_t);
2751 static bool cp_nth_tokens_can_be_gnu_attribute_p
2752 (cp_parser *, size_t);
2753 static bool cp_nth_tokens_can_be_attribute_p
2754 (cp_parser *, size_t);
2755 static tree cp_parser_attributes_opt
2756 (cp_parser *);
2757 static tree cp_parser_gnu_attributes_opt
2758 (cp_parser *);
2759 static tree cp_parser_gnu_attribute_list
2760 (cp_parser *, bool = false);
2761 static tree cp_parser_std_attribute
2762 (cp_parser *, tree);
2763 static tree cp_parser_std_attribute_spec
2764 (cp_parser *);
2765 static tree cp_parser_std_attribute_spec_seq
2766 (cp_parser *);
2767 static size_t cp_parser_skip_std_attribute_spec_seq
2768 (cp_parser *, size_t);
2769 static size_t cp_parser_skip_attributes_opt
2770 (cp_parser *, size_t);
2771 static bool cp_parser_extension_opt
2772 (cp_parser *, int *);
2773 static void cp_parser_label_declaration
2774 (cp_parser *);
2776 /* Concept Extensions */
2778 static tree cp_parser_concept_definition
2779 (cp_parser *);
2780 static tree cp_parser_constraint_expression
2781 (cp_parser *);
2782 static tree cp_parser_requires_clause_opt
2783 (cp_parser *, bool);
2784 static tree cp_parser_requires_expression
2785 (cp_parser *);
2786 static tree cp_parser_requirement_parameter_list
2787 (cp_parser *);
2788 static tree cp_parser_requirement_body
2789 (cp_parser *);
2790 static tree cp_parser_requirement_seq
2791 (cp_parser *);
2792 static tree cp_parser_requirement
2793 (cp_parser *);
2794 static tree cp_parser_simple_requirement
2795 (cp_parser *);
2796 static tree cp_parser_compound_requirement
2797 (cp_parser *);
2798 static tree cp_parser_type_requirement
2799 (cp_parser *);
2800 static tree cp_parser_nested_requirement
2801 (cp_parser *);
2803 /* Transactional Memory Extensions */
2805 static tree cp_parser_transaction
2806 (cp_parser *, cp_token *);
2807 static tree cp_parser_transaction_expression
2808 (cp_parser *, enum rid);
2809 static void cp_parser_function_transaction
2810 (cp_parser *, enum rid);
2811 static tree cp_parser_transaction_cancel
2812 (cp_parser *);
2814 /* Coroutine extensions. */
2816 static tree cp_parser_yield_expression
2817 (cp_parser *);
2819 /* Contracts */
2821 static void cp_parser_late_contract_condition
2822 (cp_parser *, tree, tree);
2824 enum pragma_context {
2825 pragma_external,
2826 pragma_member,
2827 pragma_objc_icode,
2828 pragma_stmt,
2829 pragma_compound
2831 static bool cp_parser_pragma
2832 (cp_parser *, enum pragma_context, bool *);
2834 /* Objective-C++ Productions */
2836 static tree cp_parser_objc_message_receiver
2837 (cp_parser *);
2838 static tree cp_parser_objc_message_args
2839 (cp_parser *);
2840 static tree cp_parser_objc_message_expression
2841 (cp_parser *);
2842 static cp_expr cp_parser_objc_encode_expression
2843 (cp_parser *);
2844 static tree cp_parser_objc_defs_expression
2845 (cp_parser *);
2846 static tree cp_parser_objc_protocol_expression
2847 (cp_parser *);
2848 static tree cp_parser_objc_selector_expression
2849 (cp_parser *);
2850 static cp_expr cp_parser_objc_expression
2851 (cp_parser *);
2852 static bool cp_parser_objc_selector_p
2853 (enum cpp_ttype);
2854 static tree cp_parser_objc_selector
2855 (cp_parser *);
2856 static tree cp_parser_objc_protocol_refs_opt
2857 (cp_parser *);
2858 static void cp_parser_objc_declaration
2859 (cp_parser *, tree);
2860 static tree cp_parser_objc_statement
2861 (cp_parser *);
2862 static bool cp_parser_objc_valid_prefix_attributes
2863 (cp_parser *, tree *);
2864 static void cp_parser_objc_at_property_declaration
2865 (cp_parser *) ;
2866 static void cp_parser_objc_at_synthesize_declaration
2867 (cp_parser *) ;
2868 static void cp_parser_objc_at_dynamic_declaration
2869 (cp_parser *) ;
2870 static tree cp_parser_objc_struct_declaration
2871 (cp_parser *) ;
2873 /* Utility Routines */
2875 static cp_expr cp_parser_lookup_name
2876 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2877 static tree cp_parser_lookup_name_simple
2878 (cp_parser *, tree, location_t);
2879 static tree cp_parser_maybe_treat_template_as_class
2880 (tree, bool);
2881 static bool cp_parser_check_declarator_template_parameters
2882 (cp_parser *, cp_declarator *, location_t);
2883 static bool cp_parser_check_template_parameters
2884 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2885 static cp_expr cp_parser_simple_cast_expression
2886 (cp_parser *);
2887 static tree cp_parser_global_scope_opt
2888 (cp_parser *, bool);
2889 static bool cp_parser_constructor_declarator_p
2890 (cp_parser *, cp_parser_flags, bool);
2891 static tree cp_parser_function_definition_from_specifiers_and_declarator
2892 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2893 static tree cp_parser_function_definition_after_declarator
2894 (cp_parser *, bool);
2895 static bool cp_parser_template_declaration_after_export
2896 (cp_parser *, bool);
2897 static void cp_parser_perform_template_parameter_access_checks
2898 (vec<deferred_access_check, va_gc> *);
2899 static tree cp_parser_single_declaration
2900 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2901 static cp_expr cp_parser_functional_cast
2902 (cp_parser *, tree);
2903 static tree cp_parser_save_member_function_body
2904 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2905 static tree cp_parser_save_nsdmi
2906 (cp_parser *);
2907 static tree cp_parser_enclosed_template_argument_list
2908 (cp_parser *);
2909 static void cp_parser_save_default_args
2910 (cp_parser *, tree);
2911 static void cp_parser_late_parsing_for_member
2912 (cp_parser *, tree);
2913 static tree cp_parser_late_parse_one_default_arg
2914 (cp_parser *, tree, tree, tree);
2915 static void cp_parser_late_parsing_nsdmi
2916 (cp_parser *, tree);
2917 static void cp_parser_late_parsing_default_args
2918 (cp_parser *, tree);
2919 static tree cp_parser_sizeof_operand
2920 (cp_parser *, enum rid);
2921 static cp_expr cp_parser_trait
2922 (cp_parser *, const cp_trait *);
2923 static bool cp_parser_declares_only_class_p
2924 (cp_parser *);
2925 static void cp_parser_set_storage_class
2926 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2927 static void cp_parser_set_decl_spec_type
2928 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2929 static void set_and_check_decl_spec_loc
2930 (cp_decl_specifier_seq *decl_specs,
2931 cp_decl_spec ds, cp_token *);
2932 static bool cp_parser_friend_p
2933 (const cp_decl_specifier_seq *);
2934 static void cp_parser_required_error
2935 (cp_parser *, required_token, bool, location_t);
2936 static cp_token *cp_parser_require
2937 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2938 static cp_token *cp_parser_require_keyword
2939 (cp_parser *, enum rid, required_token);
2940 static bool cp_parser_token_starts_function_definition_p
2941 (cp_token *);
2942 static bool cp_parser_next_token_starts_class_definition_p
2943 (cp_parser *);
2944 static bool cp_parser_next_token_ends_template_argument_p
2945 (cp_parser *);
2946 static bool cp_parser_nth_token_starts_template_argument_list_p
2947 (cp_parser *, size_t);
2948 static enum tag_types cp_parser_token_is_class_key
2949 (cp_token *);
2950 static enum tag_types cp_parser_token_is_type_parameter_key
2951 (cp_token *);
2952 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2953 static void cp_parser_check_class_key
2954 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2955 static void cp_parser_check_access_in_redeclaration
2956 (tree type, location_t location);
2957 static bool cp_parser_optional_template_keyword
2958 (cp_parser *);
2959 static void cp_parser_pre_parsed_nested_name_specifier
2960 (cp_parser *);
2961 static bool cp_parser_cache_group
2962 (cp_parser *, enum cpp_ttype, unsigned);
2963 static tree cp_parser_cache_defarg
2964 (cp_parser *parser, bool nsdmi);
2965 static void cp_parser_parse_tentatively
2966 (cp_parser *);
2967 static void cp_parser_commit_to_tentative_parse
2968 (cp_parser *);
2969 static void cp_parser_commit_to_topmost_tentative_parse
2970 (cp_parser *);
2971 static void cp_parser_abort_tentative_parse
2972 (cp_parser *);
2973 static bool cp_parser_parse_definitely
2974 (cp_parser *);
2975 static inline bool cp_parser_parsing_tentatively
2976 (cp_parser *);
2977 static bool cp_parser_uncommitted_to_tentative_parse_p
2978 (cp_parser *);
2979 static void cp_parser_error
2980 (cp_parser *, const char *);
2981 static void cp_parser_name_lookup_error
2982 (cp_parser *, tree, tree, name_lookup_error, location_t);
2983 static bool cp_parser_simulate_error
2984 (cp_parser *);
2985 static bool cp_parser_check_type_definition
2986 (cp_parser *);
2987 static void cp_parser_check_for_definition_in_return_type
2988 (cp_declarator *, tree, location_t type_location);
2989 static void cp_parser_check_for_invalid_template_id
2990 (cp_parser *, tree, enum tag_types, location_t location);
2991 static bool cp_parser_non_integral_constant_expression
2992 (cp_parser *, non_integral_constant);
2993 static void cp_parser_diagnose_invalid_type_name
2994 (cp_parser *, tree, location_t);
2995 static bool cp_parser_parse_and_diagnose_invalid_type_name
2996 (cp_parser *);
2997 static int cp_parser_skip_to_closing_parenthesis
2998 (cp_parser *, bool, bool, bool);
2999 static void cp_parser_skip_to_end_of_statement
3000 (cp_parser *);
3001 static void cp_parser_consume_semicolon_at_end_of_statement
3002 (cp_parser *);
3003 static void cp_parser_skip_to_end_of_block_or_statement
3004 (cp_parser *);
3005 static bool cp_parser_skip_to_closing_brace
3006 (cp_parser *);
3007 static bool cp_parser_skip_entire_template_parameter_list
3008 (cp_parser *);
3009 static void cp_parser_require_end_of_template_parameter_list
3010 (cp_parser *);
3011 static bool cp_parser_skip_to_end_of_template_parameter_list
3012 (cp_parser *);
3013 static void cp_parser_skip_to_pragma_eol
3014 (cp_parser*, cp_token *);
3015 static bool cp_parser_error_occurred
3016 (cp_parser *);
3017 static bool cp_parser_allow_gnu_extensions_p
3018 (cp_parser *);
3019 static bool cp_parser_is_pure_string_literal
3020 (cp_token *);
3021 static bool cp_parser_is_string_literal
3022 (cp_token *);
3023 static bool cp_parser_is_keyword
3024 (cp_token *, enum rid);
3025 static tree cp_parser_make_typename_type
3026 (cp_parser *, tree, location_t location);
3027 static cp_declarator * cp_parser_make_indirect_declarator
3028 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
3029 static bool cp_parser_compound_literal_p
3030 (cp_parser *);
3031 static bool cp_parser_array_designator_p
3032 (cp_parser *);
3033 static bool cp_parser_init_statement_p
3034 (cp_parser *);
3035 static bool cp_parser_skip_up_to_closing_square_bracket
3036 (cp_parser *);
3037 static bool cp_parser_skip_to_closing_square_bracket
3038 (cp_parser *);
3039 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
3040 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
3042 // -------------------------------------------------------------------------- //
3043 // Unevaluated Operand Guard
3045 // Implementation of an RAII helper for unevaluated operand parsing.
3046 cp_unevaluated::cp_unevaluated ()
3048 ++cp_unevaluated_operand;
3049 ++c_inhibit_evaluation_warnings;
3052 cp_unevaluated::~cp_unevaluated ()
3054 --c_inhibit_evaluation_warnings;
3055 --cp_unevaluated_operand;
3058 // -------------------------------------------------------------------------- //
3059 // Tentative Parsing
3061 /* Returns nonzero if we are parsing tentatively. */
3063 static inline bool
3064 cp_parser_parsing_tentatively (cp_parser* parser)
3066 return parser->context->next != NULL;
3069 /* Returns nonzero if TOKEN is a string literal. */
3071 static bool
3072 cp_parser_is_pure_string_literal (cp_token* token)
3074 return (token->type == CPP_STRING ||
3075 token->type == CPP_STRING16 ||
3076 token->type == CPP_STRING32 ||
3077 token->type == CPP_WSTRING ||
3078 token->type == CPP_UTF8STRING);
3081 /* Returns nonzero if TOKEN is a string literal
3082 of a user-defined string literal. */
3084 static bool
3085 cp_parser_is_string_literal (cp_token* token)
3087 return (cp_parser_is_pure_string_literal (token) ||
3088 token->type == CPP_STRING_USERDEF ||
3089 token->type == CPP_STRING16_USERDEF ||
3090 token->type == CPP_STRING32_USERDEF ||
3091 token->type == CPP_WSTRING_USERDEF ||
3092 token->type == CPP_UTF8STRING_USERDEF);
3095 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3097 static bool
3098 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3100 return token->keyword == keyword;
3103 /* Helper function for cp_parser_error.
3104 Having peeked a token of kind TOK1_KIND that might signify
3105 a conflict marker, peek successor tokens to determine
3106 if we actually do have a conflict marker.
3107 Specifically, we consider a run of 7 '<', '=' or '>' characters
3108 at the start of a line as a conflict marker.
3109 These come through the lexer as three pairs and a single,
3110 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3111 If it returns true, *OUT_LOC is written to with the location/range
3112 of the marker. */
3114 static bool
3115 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3116 location_t *out_loc)
3118 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3119 if (token2->type != tok1_kind)
3120 return false;
3121 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3122 if (token3->type != tok1_kind)
3123 return false;
3124 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3125 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3126 return false;
3128 /* It must be at the start of the line. */
3129 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3130 if (LOCATION_COLUMN (start_loc) != 1)
3131 return false;
3133 /* We have a conflict marker. Construct a location of the form:
3134 <<<<<<<
3135 ^~~~~~~
3136 with start == caret, finishing at the end of the marker. */
3137 location_t finish_loc = get_finish (token4->location);
3138 *out_loc = make_location (start_loc, start_loc, finish_loc);
3140 return true;
3143 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3144 RT_CLOSE_PAREN. */
3146 static const char *
3147 get_matching_symbol (required_token token_desc)
3149 switch (token_desc)
3151 default:
3152 gcc_unreachable ();
3153 return "";
3154 case RT_CLOSE_BRACE:
3155 return "{";
3156 case RT_CLOSE_PAREN:
3157 return "(";
3161 /* Attempt to convert TOKEN_DESC from a required_token to an
3162 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3164 static enum cpp_ttype
3165 get_required_cpp_ttype (required_token token_desc)
3167 switch (token_desc)
3169 case RT_SEMICOLON:
3170 return CPP_SEMICOLON;
3171 case RT_OPEN_PAREN:
3172 return CPP_OPEN_PAREN;
3173 case RT_CLOSE_BRACE:
3174 return CPP_CLOSE_BRACE;
3175 case RT_OPEN_BRACE:
3176 return CPP_OPEN_BRACE;
3177 case RT_CLOSE_SQUARE:
3178 return CPP_CLOSE_SQUARE;
3179 case RT_OPEN_SQUARE:
3180 return CPP_OPEN_SQUARE;
3181 case RT_COMMA:
3182 return CPP_COMMA;
3183 case RT_COLON:
3184 return CPP_COLON;
3185 case RT_CLOSE_PAREN:
3186 return CPP_CLOSE_PAREN;
3188 default:
3189 /* Use CPP_EOF as a "no completions possible" code. */
3190 return CPP_EOF;
3195 /* Subroutine of cp_parser_error and cp_parser_required_error.
3197 Issue a diagnostic of the form
3198 FILE:LINE: MESSAGE before TOKEN
3199 where TOKEN is the next token in the input stream. MESSAGE
3200 (specified by the caller) is usually of the form "expected
3201 OTHER-TOKEN".
3203 This bypasses the check for tentative passing, and potentially
3204 adds material needed by cp_parser_required_error.
3206 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3207 suggesting insertion of the missing token.
3209 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3210 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3211 location. */
3213 static void
3214 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3215 required_token missing_token_desc,
3216 location_t matching_location)
3218 cp_token *token = cp_lexer_peek_token (parser->lexer);
3219 /* This diagnostic makes more sense if it is tagged to the line
3220 of the token we just peeked at. */
3221 cp_lexer_set_source_position_from_token (token);
3223 if (token->type == CPP_PRAGMA)
3225 error_at (token->location,
3226 "%<#pragma%> is not allowed here");
3227 cp_parser_skip_to_pragma_eol (parser, token);
3228 return;
3231 /* If this is actually a conflict marker, report it as such. */
3232 if (token->type == CPP_LSHIFT
3233 || token->type == CPP_RSHIFT
3234 || token->type == CPP_EQ_EQ)
3236 location_t loc;
3237 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3239 error_at (loc, "version control conflict marker in file");
3240 expanded_location token_exploc = expand_location (token->location);
3241 /* Consume tokens until the end of the source line. */
3242 for (;;)
3244 cp_lexer_consume_token (parser->lexer);
3245 cp_token *next = cp_lexer_peek_token (parser->lexer);
3246 if (next->type == CPP_EOF)
3247 break;
3248 if (next->location == UNKNOWN_LOCATION
3249 || loc == UNKNOWN_LOCATION)
3250 break;
3252 expanded_location next_exploc = expand_location (next->location);
3253 if (next_exploc.file != token_exploc.file)
3254 break;
3255 if (next_exploc.line != token_exploc.line)
3256 break;
3258 return;
3262 auto_diagnostic_group d;
3263 gcc_rich_location richloc (input_location);
3265 bool added_matching_location = false;
3267 if (missing_token_desc != RT_NONE)
3268 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3270 /* Potentially supply a fix-it hint, suggesting to add the
3271 missing token immediately after the *previous* token.
3272 This may move the primary location within richloc. */
3273 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3274 location_t prev_token_loc = prev_token->location;
3275 maybe_suggest_missing_token_insertion (&richloc, ttype,
3276 prev_token_loc);
3278 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3279 Attempt to consolidate diagnostics by printing it as a
3280 secondary range within the main diagnostic. */
3281 if (matching_location != UNKNOWN_LOCATION)
3282 added_matching_location
3283 = richloc.add_location_if_nearby (matching_location);
3286 /* If we were parsing a string-literal and there is an unknown name
3287 token right after, then check to see if that could also have been
3288 a literal string by checking the name against a list of known
3289 standard string literal constants defined in header files. If
3290 there is one, then add that as an hint to the error message. */
3291 name_hint h;
3292 if (token->type == CPP_NAME)
3293 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3294 if (cp_parser_is_string_literal (prev_token))
3296 tree name = token->u.value;
3297 const char *token_name = IDENTIFIER_POINTER (name);
3298 const char *header_hint
3299 = get_cp_stdlib_header_for_string_macro_name (token_name);
3300 if (header_hint != NULL)
3301 h = name_hint (NULL, new suggest_missing_header (token->location,
3302 token_name,
3303 header_hint));
3306 /* Actually emit the error. */
3307 c_parse_error (gmsgid,
3308 /* Because c_parser_error does not understand
3309 CPP_KEYWORD, keywords are treated like
3310 identifiers. */
3311 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3312 token->u.value, token->flags, &richloc);
3314 if (missing_token_desc != RT_NONE)
3316 /* If we weren't able to consolidate matching_location, then
3317 print it as a secondary diagnostic. */
3318 if (matching_location != UNKNOWN_LOCATION
3319 && !added_matching_location)
3320 inform (matching_location, "to match this %qs",
3321 get_matching_symbol (missing_token_desc));
3325 /* If not parsing tentatively, issue a diagnostic of the form
3326 FILE:LINE: MESSAGE before TOKEN
3327 where TOKEN is the next token in the input stream. MESSAGE
3328 (specified by the caller) is usually of the form "expected
3329 OTHER-TOKEN". */
3331 static void
3332 cp_parser_error (cp_parser* parser, const char* gmsgid)
3334 if (!cp_parser_simulate_error (parser))
3335 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3338 /* Issue an error about name-lookup failing. NAME is the
3339 IDENTIFIER_NODE DECL is the result of
3340 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3341 the thing that we hoped to find. */
3343 static void
3344 cp_parser_name_lookup_error (cp_parser* parser,
3345 tree name,
3346 tree decl,
3347 name_lookup_error desired,
3348 location_t location)
3350 /* If name lookup completely failed, tell the user that NAME was not
3351 declared. */
3352 if (decl == error_mark_node)
3354 if (parser->scope && parser->scope != global_namespace)
3355 error_at (location, "%<%E::%E%> has not been declared",
3356 parser->scope, name);
3357 else if (parser->scope == global_namespace)
3358 error_at (location, "%<::%E%> has not been declared", name);
3359 else if (parser->object_scope
3360 && !CLASS_TYPE_P (parser->object_scope))
3361 error_at (location, "request for member %qE in non-class type %qT",
3362 name, parser->object_scope);
3363 else if (parser->object_scope)
3364 error_at (location, "%<%T::%E%> has not been declared",
3365 parser->object_scope, name);
3366 else
3367 error_at (location, "%qE has not been declared", name);
3369 else if (parser->scope && parser->scope != global_namespace)
3371 switch (desired)
3373 case NLE_TYPE:
3374 error_at (location, "%<%E::%E%> is not a type",
3375 parser->scope, name);
3376 break;
3377 case NLE_CXX98:
3378 error_at (location, "%<%E::%E%> is not a class or namespace",
3379 parser->scope, name);
3380 break;
3381 case NLE_NOT_CXX98:
3382 error_at (location,
3383 "%<%E::%E%> is not a class, namespace, or enumeration",
3384 parser->scope, name);
3385 break;
3386 default:
3387 gcc_unreachable ();
3391 else if (parser->scope == global_namespace)
3393 switch (desired)
3395 case NLE_TYPE:
3396 error_at (location, "%<::%E%> is not a type", name);
3397 break;
3398 case NLE_CXX98:
3399 error_at (location, "%<::%E%> is not a class or namespace", name);
3400 break;
3401 case NLE_NOT_CXX98:
3402 error_at (location,
3403 "%<::%E%> is not a class, namespace, or enumeration",
3404 name);
3405 break;
3406 default:
3407 gcc_unreachable ();
3410 else
3412 switch (desired)
3414 case NLE_TYPE:
3415 error_at (location, "%qE is not a type", name);
3416 break;
3417 case NLE_CXX98:
3418 error_at (location, "%qE is not a class or namespace", name);
3419 break;
3420 case NLE_NOT_CXX98:
3421 error_at (location,
3422 "%qE is not a class, namespace, or enumeration", name);
3423 break;
3424 default:
3425 gcc_unreachable ();
3430 /* If we are parsing tentatively, remember that an error has occurred
3431 during this tentative parse. Returns true if the error was
3432 simulated; false if a message should be issued by the caller. */
3434 static bool
3435 cp_parser_simulate_error (cp_parser* parser)
3437 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3439 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3440 return true;
3442 return false;
3445 /* This function is called when a type is defined. If type
3446 definitions are forbidden at this point, an error message is
3447 issued. */
3449 static bool
3450 cp_parser_check_type_definition (cp_parser* parser)
3452 /* If types are forbidden here, issue a message. */
3453 if (parser->type_definition_forbidden_message)
3455 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3456 or %qs in the message need to be interpreted. */
3457 error (parser->type_definition_forbidden_message,
3458 parser->type_definition_forbidden_message_arg);
3459 return false;
3461 return true;
3464 /* This function is called when the DECLARATOR is processed. The TYPE
3465 was a type defined in the decl-specifiers. If it is invalid to
3466 define a type in the decl-specifiers for DECLARATOR, an error is
3467 issued. TYPE_LOCATION is the location of TYPE and is used
3468 for error reporting. */
3470 static void
3471 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3472 tree type, location_t type_location)
3474 /* [dcl.fct] forbids type definitions in return types.
3475 Unfortunately, it's not easy to know whether or not we are
3476 processing a return type until after the fact. */
3477 while (declarator
3478 && (declarator->kind == cdk_pointer
3479 || declarator->kind == cdk_reference
3480 || declarator->kind == cdk_ptrmem))
3481 declarator = declarator->declarator;
3482 if (declarator
3483 && declarator->kind == cdk_function)
3485 error_at (type_location,
3486 "new types may not be defined in a return type");
3487 inform (type_location,
3488 "(perhaps a semicolon is missing after the definition of %qT)",
3489 type);
3493 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3494 "<" in any valid C++ program. If the next token is indeed "<",
3495 issue a message warning the user about what appears to be an
3496 invalid attempt to form a template-id. LOCATION is the location
3497 of the type-specifier (TYPE) */
3499 static void
3500 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3501 tree type,
3502 enum tag_types tag_type,
3503 location_t location)
3505 cp_token_position start = 0;
3507 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3509 if (TREE_CODE (type) == TYPE_DECL)
3510 type = TREE_TYPE (type);
3511 if (TYPE_P (type) && !template_placeholder_p (type))
3512 error_at (location, "%qT is not a template", type);
3513 else if (identifier_p (type))
3515 if (tag_type != none_type)
3516 error_at (location, "%qE is not a class template", type);
3517 else
3518 error_at (location, "%qE is not a template", type);
3520 else
3521 error_at (location, "invalid template-id");
3522 /* Remember the location of the invalid "<". */
3523 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3524 start = cp_lexer_token_position (parser->lexer, true);
3525 /* Consume the "<". */
3526 cp_lexer_consume_token (parser->lexer);
3527 /* Parse the template arguments. */
3528 cp_parser_enclosed_template_argument_list (parser);
3529 /* Permanently remove the invalid template arguments so that
3530 this error message is not issued again. */
3531 if (start)
3532 cp_lexer_purge_tokens_after (parser->lexer, start);
3536 /* If parsing an integral constant-expression, issue an error message
3537 about the fact that THING appeared and return true. Otherwise,
3538 return false. In either case, set
3539 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3541 static bool
3542 cp_parser_non_integral_constant_expression (cp_parser *parser,
3543 non_integral_constant thing)
3545 parser->non_integral_constant_expression_p = true;
3546 if (parser->integral_constant_expression_p)
3548 if (!parser->allow_non_integral_constant_expression_p)
3550 const char *msg = NULL;
3551 switch (thing)
3553 case NIC_FLOAT:
3554 pedwarn (input_location, OPT_Wpedantic,
3555 "ISO C++ forbids using a floating-point literal "
3556 "in a constant-expression");
3557 return true;
3558 case NIC_CAST:
3559 error ("a cast to a type other than an integral or "
3560 "enumeration type cannot appear in a "
3561 "constant-expression");
3562 return true;
3563 case NIC_TYPEID:
3564 error ("%<typeid%> operator "
3565 "cannot appear in a constant-expression");
3566 return true;
3567 case NIC_NCC:
3568 error ("non-constant compound literals "
3569 "cannot appear in a constant-expression");
3570 return true;
3571 case NIC_FUNC_CALL:
3572 error ("a function call "
3573 "cannot appear in a constant-expression");
3574 return true;
3575 case NIC_INC:
3576 error ("an increment "
3577 "cannot appear in a constant-expression");
3578 return true;
3579 case NIC_DEC:
3580 error ("an decrement "
3581 "cannot appear in a constant-expression");
3582 return true;
3583 case NIC_ARRAY_REF:
3584 error ("an array reference "
3585 "cannot appear in a constant-expression");
3586 return true;
3587 case NIC_ADDR_LABEL:
3588 error ("the address of a label "
3589 "cannot appear in a constant-expression");
3590 return true;
3591 case NIC_OVERLOADED:
3592 error ("calls to overloaded operators "
3593 "cannot appear in a constant-expression");
3594 return true;
3595 case NIC_ASSIGNMENT:
3596 error ("an assignment cannot appear in a constant-expression");
3597 return true;
3598 case NIC_COMMA:
3599 error ("a comma operator "
3600 "cannot appear in a constant-expression");
3601 return true;
3602 case NIC_CONSTRUCTOR:
3603 error ("a call to a constructor "
3604 "cannot appear in a constant-expression");
3605 return true;
3606 case NIC_TRANSACTION:
3607 error ("a transaction expression "
3608 "cannot appear in a constant-expression");
3609 return true;
3610 case NIC_THIS:
3611 msg = "this";
3612 break;
3613 case NIC_FUNC_NAME:
3614 msg = "__FUNCTION__";
3615 break;
3616 case NIC_PRETTY_FUNC:
3617 msg = "__PRETTY_FUNCTION__";
3618 break;
3619 case NIC_C99_FUNC:
3620 msg = "__func__";
3621 break;
3622 case NIC_VA_ARG:
3623 msg = "va_arg";
3624 break;
3625 case NIC_ARROW:
3626 msg = "->";
3627 break;
3628 case NIC_POINT:
3629 msg = ".";
3630 break;
3631 case NIC_STAR:
3632 msg = "*";
3633 break;
3634 case NIC_ADDR:
3635 msg = "&";
3636 break;
3637 case NIC_PREINCREMENT:
3638 msg = "++";
3639 break;
3640 case NIC_PREDECREMENT:
3641 msg = "--";
3642 break;
3643 case NIC_NEW:
3644 msg = "new";
3645 break;
3646 case NIC_DEL:
3647 msg = "delete";
3648 break;
3649 default:
3650 gcc_unreachable ();
3652 if (msg)
3653 error ("%qs cannot appear in a constant-expression", msg);
3654 return true;
3657 return false;
3660 /* Emit a diagnostic for an invalid type name. This function commits
3661 to the current active tentative parse, if any. (Otherwise, the
3662 problematic construct might be encountered again later, resulting
3663 in duplicate error messages.) LOCATION is the location of ID. */
3665 static void
3666 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3667 location_t location)
3669 tree decl, ambiguous_decls;
3670 cp_parser_commit_to_tentative_parse (parser);
3671 /* Try to lookup the identifier. */
3672 decl = cp_parser_lookup_name (parser, id, none_type,
3673 /*is_template=*/false,
3674 /*is_namespace=*/false,
3675 /*check_dependency=*/true,
3676 &ambiguous_decls, location);
3677 if (ambiguous_decls)
3678 /* If the lookup was ambiguous, an error will already have
3679 been issued. */
3680 return;
3681 /* If the lookup found a template-name, it means that the user forgot
3682 to specify an argument list. Emit a useful error message. */
3683 if (DECL_TYPE_TEMPLATE_P (decl))
3685 auto_diagnostic_group d;
3686 error_at (location,
3687 "invalid use of template-name %qE without an argument list",
3688 decl);
3689 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3690 inform (location, "class template argument deduction is only available "
3691 "with %<-std=c++17%> or %<-std=gnu++17%>");
3692 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3694 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3695 error_at (location, "invalid use of destructor %qD as a type", id);
3696 else if (TREE_CODE (decl) == TYPE_DECL)
3697 /* Something like 'unsigned A a;' */
3698 error_at (location, "invalid combination of multiple type-specifiers");
3699 else if (!parser->scope)
3701 /* Issue an error message. */
3702 auto_diagnostic_group d;
3703 name_hint hint;
3704 if (TREE_CODE (id) == IDENTIFIER_NODE)
3705 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3706 if (const char *suggestion = hint.suggestion ())
3708 gcc_rich_location richloc (location);
3709 richloc.add_fixit_replace (suggestion);
3710 error_at (&richloc,
3711 "%qE does not name a type; did you mean %qs?",
3712 id, suggestion);
3714 else
3715 error_at (location, "%qE does not name a type", id);
3716 /* If we're in a template class, it's possible that the user was
3717 referring to a type from a base class. For example:
3719 template <typename T> struct A { typedef T X; };
3720 template <typename T> struct B : public A<T> { X x; };
3722 The user should have said "typename A<T>::X". */
3723 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3724 inform (location, "C++11 %<constexpr%> only available with "
3725 "%<-std=c++11%> or %<-std=gnu++11%>");
3726 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3727 inform (location, "C++11 %<noexcept%> only available with "
3728 "%<-std=c++11%> or %<-std=gnu++11%>");
3729 else if (TREE_CODE (id) == IDENTIFIER_NODE
3730 && (id_equal (id, "module") || id_equal (id, "import")))
3732 if (modules_p ())
3733 inform (location, "%qE is not recognized as a module control-line",
3734 id);
3735 else if (cxx_dialect < cxx20)
3736 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3737 id);
3738 else
3739 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3740 ", which is not yet enabled with %<-std=c++20%>", id);
3742 else if (cxx_dialect < cxx11
3743 && TREE_CODE (id) == IDENTIFIER_NODE
3744 && id_equal (id, "thread_local"))
3745 inform (location, "C++11 %<thread_local%> only available with "
3746 "%<-std=c++11%> or %<-std=gnu++11%>");
3747 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3748 inform (location, "C++20 %<constinit%> only available with "
3749 "%<-std=c++20%> or %<-std=gnu++20%>");
3750 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3751 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3752 "%<-fconcepts%>");
3753 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3754 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3755 "%<-fconcepts%>");
3756 else if (processing_template_decl && current_class_type
3757 && TYPE_BINFO (current_class_type))
3759 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3760 b; b = TREE_CHAIN (b))
3762 tree base_type = BINFO_TYPE (b);
3763 if (CLASS_TYPE_P (base_type)
3764 && dependent_type_p (base_type))
3766 /* Go from a particular instantiation of the
3767 template (which will have an empty TYPE_FIELDs),
3768 to the main version. */
3769 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3770 for (tree field = TYPE_FIELDS (base_type);
3771 field; field = DECL_CHAIN (field))
3772 if (TREE_CODE (field) == TYPE_DECL
3773 && DECL_NAME (field) == id)
3775 inform (location,
3776 "(perhaps %<typename %T::%E%> was intended)",
3777 BINFO_TYPE (b), id);
3778 goto found;
3782 found:;
3785 /* Here we diagnose qualified-ids where the scope is actually correct,
3786 but the identifier does not resolve to a valid type name. */
3787 else if (parser->scope != error_mark_node)
3789 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3791 auto_diagnostic_group d;
3792 name_hint hint;
3793 if (decl == error_mark_node)
3794 hint = suggest_alternative_in_explicit_scope (location, id,
3795 parser->scope);
3796 const char *suggestion = hint.suggestion ();
3797 gcc_rich_location richloc (location_of (id));
3798 if (suggestion)
3799 richloc.add_fixit_replace (suggestion);
3800 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3802 if (suggestion)
3803 error_at (&richloc,
3804 "%qE in namespace %qE does not name a template"
3805 " type; did you mean %qs?",
3806 id, parser->scope, suggestion);
3807 else
3808 error_at (&richloc,
3809 "%qE in namespace %qE does not name a template type",
3810 id, parser->scope);
3812 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3814 if (suggestion)
3815 error_at (&richloc,
3816 "%qE in namespace %qE does not name a template"
3817 " type; did you mean %qs?",
3818 TREE_OPERAND (id, 0), parser->scope, suggestion);
3819 else
3820 error_at (&richloc,
3821 "%qE in namespace %qE does not name a template"
3822 " type",
3823 TREE_OPERAND (id, 0), parser->scope);
3825 else
3827 if (suggestion)
3828 error_at (&richloc,
3829 "%qE in namespace %qE does not name a type"
3830 "; did you mean %qs?",
3831 id, parser->scope, suggestion);
3832 else
3833 error_at (&richloc,
3834 "%qE in namespace %qE does not name a type",
3835 id, parser->scope);
3837 if (DECL_P (decl))
3838 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3840 else if (CLASS_TYPE_P (parser->scope)
3841 && constructor_name_p (id, parser->scope))
3843 /* A<T>::A<T>() */
3844 auto_diagnostic_group d;
3845 error_at (location, "%<%T::%E%> names the constructor, not"
3846 " the type", parser->scope, id);
3847 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3848 error_at (location, "and %qT has no template constructors",
3849 parser->scope);
3851 else if (TYPE_P (parser->scope)
3852 && dependent_scope_p (parser->scope))
3854 gcc_rich_location richloc (location);
3855 richloc.add_fixit_insert_before ("typename ");
3856 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3857 error_at (&richloc,
3858 "need %<typename%> before %<%T::%D::%E%> because "
3859 "%<%T::%D%> is a dependent scope",
3860 TYPE_CONTEXT (parser->scope),
3861 TYPENAME_TYPE_FULLNAME (parser->scope),
3863 TYPE_CONTEXT (parser->scope),
3864 TYPENAME_TYPE_FULLNAME (parser->scope));
3865 else
3866 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3867 "%qT is a dependent scope",
3868 parser->scope, id, parser->scope);
3870 else if (TYPE_P (parser->scope))
3872 auto_diagnostic_group d;
3873 if (!COMPLETE_TYPE_P (parser->scope))
3874 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3875 parser->scope);
3876 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3877 error_at (location_of (id),
3878 "%qE in %q#T does not name a template type",
3879 id, parser->scope);
3880 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3881 error_at (location_of (id),
3882 "%qE in %q#T does not name a template type",
3883 TREE_OPERAND (id, 0), parser->scope);
3884 else
3885 error_at (location_of (id),
3886 "%qE in %q#T does not name a type",
3887 id, parser->scope);
3888 if (DECL_P (decl))
3889 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3891 else
3892 gcc_unreachable ();
3896 /* Check for a common situation where a type-name should be present,
3897 but is not, and issue a sensible error message. Returns true if an
3898 invalid type-name was detected.
3900 The situation handled by this function are variable declarations of the
3901 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3902 Usually, `ID' should name a type, but if we got here it means that it
3903 does not. We try to emit the best possible error message depending on
3904 how exactly the id-expression looks like. */
3906 static bool
3907 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3909 tree id;
3910 cp_token *token = cp_lexer_peek_token (parser->lexer);
3912 /* Avoid duplicate error about ambiguous lookup. */
3913 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3915 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3916 if (next->type == CPP_NAME && next->error_reported)
3917 goto out;
3920 cp_parser_parse_tentatively (parser);
3921 id = cp_parser_id_expression (parser,
3922 /*template_keyword_p=*/false,
3923 /*check_dependency_p=*/true,
3924 /*template_p=*/NULL,
3925 /*declarator_p=*/true,
3926 /*optional_p=*/false);
3927 /* If the next token is a (, this is a function with no explicit return
3928 type, i.e. constructor, destructor or conversion op. */
3929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3930 || TREE_CODE (id) == TYPE_DECL)
3932 cp_parser_abort_tentative_parse (parser);
3933 return false;
3935 if (!cp_parser_parse_definitely (parser))
3936 return false;
3938 /* Emit a diagnostic for the invalid type. */
3939 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3940 out:
3941 /* If we aren't in the middle of a declarator (i.e. in a
3942 parameter-declaration-clause), skip to the end of the declaration;
3943 there's no point in trying to process it. */
3944 if (!parser->in_declarator_p)
3945 cp_parser_skip_to_end_of_block_or_statement (parser);
3946 return true;
3949 /* Consume tokens up to, and including, the next non-nested closing `)'.
3950 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3951 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3952 found an unnested token of that type. */
3954 static int
3955 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3956 bool recovering,
3957 cpp_ttype or_ttype,
3958 bool consume_paren)
3960 unsigned paren_depth = 0;
3961 unsigned brace_depth = 0;
3962 unsigned square_depth = 0;
3963 unsigned condop_depth = 0;
3965 if (recovering && or_ttype == CPP_EOF
3966 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3967 return 0;
3969 while (true)
3971 cp_token * token = cp_lexer_peek_token (parser->lexer);
3973 /* Have we found what we're looking for before the closing paren? */
3974 if (token->type == or_ttype && or_ttype != CPP_EOF
3975 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3976 return -1;
3978 switch (token->type)
3980 case CPP_PRAGMA_EOL:
3981 if (!parser->lexer->in_pragma)
3982 break;
3983 /* FALLTHRU */
3984 case CPP_EOF:
3985 /* If we've run out of tokens, then there is no closing `)'. */
3986 return 0;
3988 /* This is good for lambda expression capture-lists. */
3989 case CPP_OPEN_SQUARE:
3990 ++square_depth;
3991 break;
3992 case CPP_CLOSE_SQUARE:
3993 if (!square_depth--)
3994 return 0;
3995 break;
3997 case CPP_SEMICOLON:
3998 /* This matches the processing in skip_to_end_of_statement. */
3999 if (!brace_depth)
4000 return 0;
4001 break;
4003 case CPP_OPEN_BRACE:
4004 ++brace_depth;
4005 break;
4006 case CPP_CLOSE_BRACE:
4007 if (!brace_depth--)
4008 return 0;
4009 break;
4011 case CPP_OPEN_PAREN:
4012 if (!brace_depth)
4013 ++paren_depth;
4014 break;
4016 case CPP_CLOSE_PAREN:
4017 if (!brace_depth && !paren_depth--)
4019 if (consume_paren)
4020 cp_lexer_consume_token (parser->lexer);
4021 return 1;
4023 break;
4025 case CPP_QUERY:
4026 if (!brace_depth && !paren_depth && !square_depth)
4027 ++condop_depth;
4028 break;
4030 case CPP_COLON:
4031 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
4032 condop_depth--;
4033 break;
4035 case CPP_KEYWORD:
4036 if (!cp_token_is_module_directive (token))
4037 break;
4038 /* FALLTHROUGH */
4040 case CPP_PRAGMA:
4041 /* We fell into a pragma. Skip it, and continue. */
4042 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
4043 continue;
4045 default:
4046 break;
4049 /* Consume the token. */
4050 cp_lexer_consume_token (parser->lexer);
4054 /* Consume tokens up to, and including, the next non-nested closing `)'.
4055 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
4056 are doing error recovery. Returns -1 if OR_COMMA is true and we
4057 found an unnested token of that type. */
4059 static int
4060 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
4061 bool recovering,
4062 bool or_comma,
4063 bool consume_paren)
4065 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4066 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4067 ttype, consume_paren);
4070 /* Consume tokens until we reach the end of the current statement.
4071 Normally, that will be just before consuming a `;'. However, if a
4072 non-nested `}' comes first, then we stop before consuming that. */
4074 static void
4075 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4077 unsigned nesting_depth = 0;
4079 /* Unwind generic function template scope if necessary. */
4080 if (parser->fully_implicit_function_template_p)
4081 abort_fully_implicit_template (parser);
4083 while (true)
4085 cp_token *token = cp_lexer_peek_token (parser->lexer);
4087 switch (token->type)
4089 case CPP_PRAGMA_EOL:
4090 if (!parser->lexer->in_pragma)
4091 break;
4092 /* FALLTHRU */
4093 case CPP_EOF:
4094 /* If we've run out of tokens, stop. */
4095 return;
4097 case CPP_SEMICOLON:
4098 /* If the next token is a `;', we have reached the end of the
4099 statement. */
4100 if (!nesting_depth)
4101 return;
4102 break;
4104 case CPP_CLOSE_BRACE:
4105 /* If this is a non-nested '}', stop before consuming it.
4106 That way, when confronted with something like:
4108 { 3 + }
4110 we stop before consuming the closing '}', even though we
4111 have not yet reached a `;'. */
4112 if (nesting_depth == 0)
4113 return;
4115 /* If it is the closing '}' for a block that we have
4116 scanned, stop -- but only after consuming the token.
4117 That way given:
4119 void f g () { ... }
4120 typedef int I;
4122 we will stop after the body of the erroneously declared
4123 function, but before consuming the following `typedef'
4124 declaration. */
4125 if (--nesting_depth == 0)
4127 cp_lexer_consume_token (parser->lexer);
4128 return;
4130 break;
4132 case CPP_OPEN_BRACE:
4133 ++nesting_depth;
4134 break;
4136 case CPP_KEYWORD:
4137 if (!cp_token_is_module_directive (token))
4138 break;
4139 /* FALLTHROUGH */
4141 case CPP_PRAGMA:
4142 /* We fell into a pragma. Skip it, and continue or return. */
4143 cp_parser_skip_to_pragma_eol (parser, token);
4144 if (!nesting_depth)
4145 return;
4146 continue;
4148 default:
4149 break;
4152 /* Consume the token. */
4153 cp_lexer_consume_token (parser->lexer);
4157 /* This function is called at the end of a statement or declaration.
4158 If the next token is a semicolon, it is consumed; otherwise, error
4159 recovery is attempted. */
4161 static void
4162 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4164 /* Look for the trailing `;'. */
4165 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4167 /* If there is additional (erroneous) input, skip to the end of
4168 the statement. */
4169 cp_parser_skip_to_end_of_statement (parser);
4170 /* If the next token is now a `;', consume it. */
4171 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4172 cp_lexer_consume_token (parser->lexer);
4176 /* Skip tokens until we have consumed an entire block, or until we
4177 have consumed a non-nested `;'. */
4179 static void
4180 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4182 int nesting_depth = 0;
4184 /* Unwind generic function template scope if necessary. */
4185 if (parser->fully_implicit_function_template_p)
4186 abort_fully_implicit_template (parser);
4188 while (nesting_depth >= 0)
4190 cp_token *token = cp_lexer_peek_token (parser->lexer);
4192 switch (token->type)
4194 case CPP_PRAGMA_EOL:
4195 if (!parser->lexer->in_pragma)
4196 break;
4197 /* FALLTHRU */
4198 case CPP_EOF:
4199 /* If we've run out of tokens, stop. */
4200 return;
4202 case CPP_SEMICOLON:
4203 /* Stop if this is an unnested ';'. */
4204 if (!nesting_depth)
4205 nesting_depth = -1;
4206 break;
4208 case CPP_CLOSE_BRACE:
4209 /* Stop if this is an unnested '}', or closes the outermost
4210 nesting level. */
4211 nesting_depth--;
4212 if (nesting_depth < 0)
4213 return;
4214 if (!nesting_depth)
4215 nesting_depth = -1;
4216 break;
4218 case CPP_OPEN_BRACE:
4219 /* Nest. */
4220 nesting_depth++;
4221 break;
4223 case CPP_KEYWORD:
4224 if (!cp_token_is_module_directive (token))
4225 break;
4226 /* FALLTHROUGH */
4228 case CPP_PRAGMA:
4229 /* Skip it, and continue or return. */
4230 cp_parser_skip_to_pragma_eol (parser, token);
4231 if (!nesting_depth)
4232 return;
4233 continue;
4235 default:
4236 break;
4239 /* Consume the token. */
4240 cp_lexer_consume_token (parser->lexer);
4244 /* Skip tokens until a non-nested closing curly brace is the next
4245 token, or there are no more tokens. Return true in the first case,
4246 false otherwise. */
4248 static bool
4249 cp_parser_skip_to_closing_brace (cp_parser *parser)
4251 unsigned nesting_depth = 0;
4253 while (true)
4255 cp_token *token = cp_lexer_peek_token (parser->lexer);
4257 switch (token->type)
4259 case CPP_PRAGMA_EOL:
4260 if (!parser->lexer->in_pragma)
4261 break;
4262 /* FALLTHRU */
4263 case CPP_EOF:
4264 /* If we've run out of tokens, stop. */
4265 return false;
4267 case CPP_CLOSE_BRACE:
4268 /* If the next token is a non-nested `}', then we have reached
4269 the end of the current block. */
4270 if (nesting_depth-- == 0)
4271 return true;
4272 break;
4274 case CPP_OPEN_BRACE:
4275 /* If it the next token is a `{', then we are entering a new
4276 block. Consume the entire block. */
4277 ++nesting_depth;
4278 break;
4280 default:
4281 break;
4284 /* Consume the token. */
4285 cp_lexer_consume_token (parser->lexer);
4289 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4290 parameter is the PRAGMA token, allowing us to purge the entire pragma
4291 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4292 forwards (not error recovery). */
4294 static void
4295 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4297 cp_token *token;
4301 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4302 before an EOF token, even when the EOF is on the pragma line.
4303 We should never get here without being inside a deferred
4304 pragma. */
4305 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4306 token = cp_lexer_consume_token (parser->lexer);
4308 while (token->type != CPP_PRAGMA_EOL);
4310 if (pragma_tok)
4312 parser->lexer->in_pragma = false;
4313 if (parser->lexer->in_omp_attribute_pragma
4314 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4316 parser->lexer = parser->lexer->next;
4317 /* Put the current source position back where it was before this
4318 lexer was pushed. */
4319 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4324 /* Require pragma end of line, resyncing with it as necessary. The
4325 arguments are as for cp_parser_skip_to_pragma_eol. */
4327 static void
4328 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4330 parser->lexer->in_pragma = false;
4331 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4333 else if (parser->lexer->in_omp_attribute_pragma
4334 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4336 parser->lexer = parser->lexer->next;
4337 /* Put the current source position back where it was before this
4338 lexer was pushed. */
4339 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4343 /* This is a simple wrapper around make_typename_type. When the id is
4344 an unresolved identifier node, we can provide a superior diagnostic
4345 using cp_parser_diagnose_invalid_type_name. */
4347 static tree
4348 cp_parser_make_typename_type (cp_parser *parser, tree id,
4349 location_t id_location)
4351 tree result;
4352 if (identifier_p (id))
4354 result = make_typename_type (parser->scope, id, typename_type,
4355 /*complain=*/tf_none);
4356 if (result == error_mark_node)
4357 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4358 return result;
4360 return make_typename_type (parser->scope, id, typename_type, tf_error);
4363 /* This is a wrapper around the
4364 make_{pointer,ptrmem,reference}_declarator functions that decides
4365 which one to call based on the CODE and CLASS_TYPE arguments. The
4366 CODE argument should be one of the values returned by
4367 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4368 appertain to the pointer or reference. */
4370 static cp_declarator *
4371 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4372 cp_cv_quals cv_qualifiers,
4373 cp_declarator *target,
4374 tree attributes)
4376 if (code == ERROR_MARK || target == cp_error_declarator)
4377 return cp_error_declarator;
4379 if (code == INDIRECT_REF)
4380 if (class_type == NULL_TREE)
4381 return make_pointer_declarator (cv_qualifiers, target, attributes);
4382 else
4383 return make_ptrmem_declarator (cv_qualifiers, class_type,
4384 target, attributes);
4385 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4386 return make_reference_declarator (cv_qualifiers, target,
4387 false, attributes);
4388 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4389 return make_reference_declarator (cv_qualifiers, target,
4390 true, attributes);
4391 gcc_unreachable ();
4394 /* Create a new C++ parser. */
4396 static cp_parser *
4397 cp_parser_new (cp_lexer *lexer)
4399 /* Initialize the binops_by_token so that we can get the tree
4400 directly from the token. */
4401 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4402 binops_by_token[binops[i].token_type] = binops[i];
4404 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4405 parser->lexer = lexer;
4406 parser->context = cp_parser_context_new (NULL);
4408 /* For now, we always accept GNU extensions. */
4409 parser->allow_gnu_extensions_p = 1;
4411 /* The `>' token is a greater-than operator, not the end of a
4412 template-id. */
4413 parser->greater_than_is_operator_p = true;
4415 parser->default_arg_ok_p = true;
4417 /* We are not parsing a constant-expression. */
4418 parser->integral_constant_expression_p = false;
4419 parser->allow_non_integral_constant_expression_p = false;
4420 parser->non_integral_constant_expression_p = false;
4422 /* Local variable names are not forbidden. */
4423 parser->local_variables_forbidden_p = 0;
4425 /* We are not processing an `extern "C"' declaration. */
4426 parser->in_unbraced_linkage_specification_p = false;
4428 /* We are not processing a declarator. */
4429 parser->in_declarator_p = false;
4431 /* We are not processing a template-argument-list. */
4432 parser->in_template_argument_list_p = false;
4434 /* We are not in an iteration statement. */
4435 parser->in_statement = 0;
4437 /* We are not in a switch statement. */
4438 parser->in_switch_statement_p = false;
4440 /* We are not parsing a type-id inside an expression. */
4441 parser->in_type_id_in_expr_p = false;
4443 /* String literals should be translated to the execution character set. */
4444 parser->translate_strings_p = true;
4446 /* We are not parsing a function body. */
4447 parser->in_function_body = false;
4449 /* We can correct until told otherwise. */
4450 parser->colon_corrects_to_scope_p = true;
4452 /* The unparsed function queue is empty. */
4453 push_unparsed_function_queues (parser);
4455 /* There are no classes being defined. */
4456 parser->num_classes_being_defined = 0;
4458 /* No template parameters apply. */
4459 parser->num_template_parameter_lists = 0;
4461 /* Special parsing data structures. */
4462 parser->omp_declare_simd = NULL;
4463 parser->oacc_routine = NULL;
4465 /* Disallow OpenMP array sections in expressions. */
4466 parser->omp_array_section_p = false;
4468 /* Not declaring an implicit function template. */
4469 parser->auto_is_implicit_function_template_parm_p = false;
4470 parser->fully_implicit_function_template_p = false;
4471 parser->implicit_template_parms = 0;
4472 parser->implicit_template_scope = 0;
4474 /* Allow constrained-type-specifiers. */
4475 parser->prevent_constrained_type_specifiers = 0;
4477 /* We haven't yet seen an 'extern "C"'. */
4478 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4480 return parser;
4483 /* Create a cp_lexer structure which will emit the tokens in CACHE
4484 and push it onto the parser's lexer stack. This is used for delayed
4485 parsing of in-class method bodies and default arguments, and should
4486 not be confused with tentative parsing. */
4487 static void
4488 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4490 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4491 lexer->next = parser->lexer;
4492 parser->lexer = lexer;
4494 /* Move the current source position to that of the first token in the
4495 new lexer. */
4496 cp_lexer_set_source_position_from_token (lexer->next_token);
4499 /* Pop the top lexer off the parser stack. This is never used for the
4500 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4501 static void
4502 cp_parser_pop_lexer (cp_parser *parser)
4504 cp_lexer *lexer = parser->lexer;
4505 parser->lexer = lexer->next;
4506 cp_lexer_destroy (lexer);
4508 /* Put the current source position back where it was before this
4509 lexer was pushed. */
4510 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4513 /* Lexical conventions [gram.lex] */
4515 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4516 identifier. */
4518 static cp_expr
4519 cp_parser_identifier (cp_parser* parser)
4521 cp_token *token;
4523 /* Look for the identifier. */
4524 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4525 /* Return the value. */
4526 if (token)
4527 return cp_expr (token->u.value, token->location);
4528 else
4529 return error_mark_node;
4532 /* Worker for cp_parser_string_literal, cp_parser_userdef_string_literal
4533 and cp_parser_unevaluated_string_literal.
4534 Do not call this directly; use either of the above.
4536 Parse a sequence of adjacent string constants. Return a
4537 TREE_STRING representing the combined, nul-terminated string
4538 constant. If TRANSLATE is true, translate the string to the
4539 execution character set. If WIDE_OK is true, a wide string is
4540 valid here. If UDL_OK is true, a string literal with user-defined
4541 suffix can be used in this context. If UNEVAL is true, diagnose
4542 numeric and conditional escape sequences in it if pedantic.
4544 C++98 [lex.string] says that if a narrow string literal token is
4545 adjacent to a wide string literal token, the behavior is undefined.
4546 However, C99 6.4.5p4 says that this results in a wide string literal.
4547 We follow C99 here, for consistency with the C front end.
4549 This code is largely lifted from lex_string() in c-lex.cc.
4551 FUTURE: ObjC++ will need to handle @-strings here. */
4553 static cp_expr
4554 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4555 bool wide_ok, bool udl_ok,
4556 bool lookup_udlit, bool uneval)
4558 tree value;
4559 size_t count;
4560 struct obstack str_ob;
4561 struct obstack loc_ob;
4562 cpp_string str, istr, *strs;
4563 cp_token *tok;
4564 enum cpp_ttype type, curr_type;
4565 int have_suffix_p = 0;
4566 tree string_tree;
4567 tree suffix_id = NULL_TREE;
4568 bool curr_tok_is_userdef_p = false;
4570 tok = cp_lexer_peek_token (parser->lexer);
4571 if (!cp_parser_is_string_literal (tok))
4573 cp_parser_error (parser, "expected string-literal");
4574 return error_mark_node;
4577 location_t loc = tok->location;
4579 if (cpp_userdef_string_p (tok->type))
4581 if (!udl_ok)
4583 error_at (loc, "string literal with user-defined suffix "
4584 "is invalid in this context");
4585 return error_mark_node;
4587 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4588 curr_type = cpp_userdef_string_remove_type (tok->type);
4589 curr_tok_is_userdef_p = true;
4591 else
4593 string_tree = tok->u.value;
4594 curr_type = tok->type;
4596 type = curr_type;
4598 /* Try to avoid the overhead of creating and destroying an obstack
4599 for the common case of just one string. */
4600 if (!cp_parser_is_string_literal
4601 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4603 cp_lexer_consume_token (parser->lexer);
4605 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4606 str.len = TREE_STRING_LENGTH (string_tree);
4607 count = 1;
4609 if (curr_tok_is_userdef_p)
4611 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4612 have_suffix_p = 1;
4613 curr_type = cpp_userdef_string_remove_type (tok->type);
4615 else
4616 curr_type = tok->type;
4618 strs = &str;
4620 else
4622 location_t last_tok_loc = tok->location;
4623 gcc_obstack_init (&str_ob);
4624 gcc_obstack_init (&loc_ob);
4625 count = 0;
4629 cp_lexer_consume_token (parser->lexer);
4630 count++;
4631 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4632 str.len = TREE_STRING_LENGTH (string_tree);
4634 if (curr_tok_is_userdef_p)
4636 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4637 if (have_suffix_p == 0)
4639 suffix_id = curr_suffix_id;
4640 have_suffix_p = 1;
4642 else if (have_suffix_p == 1
4643 && curr_suffix_id != suffix_id)
4645 error ("inconsistent user-defined literal suffixes"
4646 " %qD and %qD in string literal",
4647 suffix_id, curr_suffix_id);
4648 have_suffix_p = -1;
4650 curr_type = cpp_userdef_string_remove_type (tok->type);
4652 else
4653 curr_type = tok->type;
4655 if (type != curr_type)
4657 if (type == CPP_STRING)
4658 type = curr_type;
4659 else if (curr_type != CPP_STRING)
4661 rich_location rich_loc (line_table, tok->location);
4662 rich_loc.add_range (last_tok_loc);
4663 error_at (&rich_loc,
4664 "concatenation of string literals with "
4665 "conflicting encoding prefixes");
4669 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4670 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4672 last_tok_loc = tok->location;
4674 tok = cp_lexer_peek_token (parser->lexer);
4675 if (cpp_userdef_string_p (tok->type))
4677 if (!udl_ok)
4679 error_at (loc, "string literal with user-defined suffix "
4680 "is invalid in this context");
4681 return error_mark_node;
4683 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4684 curr_type = cpp_userdef_string_remove_type (tok->type);
4685 curr_tok_is_userdef_p = true;
4687 else
4689 string_tree = tok->u.value;
4690 curr_type = tok->type;
4691 curr_tok_is_userdef_p = false;
4694 while (cp_parser_is_string_literal (tok));
4696 /* A string literal built by concatenation has its caret=start at
4697 the start of the initial string, and its finish at the finish of
4698 the final string literal. */
4699 loc = make_location (loc, loc, get_finish (last_tok_loc));
4701 strs = (cpp_string *) obstack_finish (&str_ob);
4704 if (type != CPP_STRING && !wide_ok)
4706 cp_parser_error (parser, "a wide string is invalid in this context");
4707 type = CPP_STRING;
4709 if (uneval)
4710 type = CPP_UNEVAL_STRING;
4712 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4713 (parse_in, strs, count, &istr, type))
4715 value = build_string (istr.len, (const char *)istr.text);
4716 free (CONST_CAST (unsigned char *, istr.text));
4717 if (count > 1)
4719 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4720 gcc_assert (g_string_concat_db);
4721 g_string_concat_db->record_string_concatenation (count, locs);
4724 switch (type)
4726 default:
4727 case CPP_STRING:
4728 TREE_TYPE (value) = char_array_type_node;
4729 break;
4730 case CPP_UTF8STRING:
4731 if (flag_char8_t)
4732 TREE_TYPE (value) = char8_array_type_node;
4733 else
4734 TREE_TYPE (value) = char_array_type_node;
4735 break;
4736 case CPP_STRING16:
4737 TREE_TYPE (value) = char16_array_type_node;
4738 break;
4739 case CPP_STRING32:
4740 TREE_TYPE (value) = char32_array_type_node;
4741 break;
4742 case CPP_WSTRING:
4743 TREE_TYPE (value) = wchar_array_type_node;
4744 break;
4747 value = fix_string_type (value);
4749 if (have_suffix_p)
4751 tree literal = build_userdef_literal (suffix_id, value,
4752 OT_NONE, NULL_TREE);
4753 if (lookup_udlit)
4754 value = finish_userdef_string_literal (literal);
4755 else
4756 value = literal;
4759 else
4760 /* cpp_interpret_string has issued an error. */
4761 value = error_mark_node;
4763 if (count > 1)
4765 obstack_free (&str_ob, 0);
4766 obstack_free (&loc_ob, 0);
4769 return cp_expr (value, loc);
4772 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4773 representing the combined, nul-terminated string constant. If
4774 TRANSLATE is true, translate the string to the execution character set.
4775 If WIDE_OK is true, a wide string is valid here.
4777 This function issues an error if a user defined string literal is
4778 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4780 static inline cp_expr
4781 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4783 return cp_parser_string_literal_common (parser, translate, wide_ok,
4784 /*udl_ok=*/false,
4785 /*lookup_udlit=*/false,
4786 /*uneval=*/false);
4789 /* Parse a string literal or user defined string literal.
4791 user-defined-string-literal :
4792 string-literal ud-suffix
4794 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4796 static inline cp_expr
4797 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4799 return cp_parser_string_literal_common (parser, /*translate=*/true,
4800 /*wide_ok=*/true, /*udl_ok=*/true,
4801 lookup_udlit, /*uneval=*/false);
4804 /* Parse an unevaluated string literal.
4806 unevaluated-string:
4807 string-literal */
4809 static inline cp_expr
4810 cp_parser_unevaluated_string_literal (cp_parser *parser)
4812 return cp_parser_string_literal_common (parser, /*translate=*/false,
4813 /*wide_ok=*/false, /*udl_ok=*/false,
4814 /*lookup_udlit=*/false,
4815 /*uneval=*/true);
4818 /* Look up a literal operator with the name and the exact arguments. */
4820 static tree
4821 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4823 tree decl = lookup_name (name);
4824 if (!decl || !is_overloaded_fn (decl))
4825 return error_mark_node;
4827 for (lkp_iterator iter (decl); iter; ++iter)
4829 tree fn = *iter;
4831 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4833 unsigned int ix;
4834 bool found = true;
4836 for (ix = 0;
4837 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4838 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4840 tree tparm = TREE_VALUE (parmtypes);
4841 tree targ = TREE_TYPE ((*args)[ix]);
4842 bool ptr = TYPE_PTR_P (tparm);
4843 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4844 if ((ptr || arr || !same_type_p (tparm, targ))
4845 && (!ptr || !arr
4846 || !same_type_p (TREE_TYPE (tparm),
4847 TREE_TYPE (targ))))
4848 found = false;
4851 if (found
4852 && ix == vec_safe_length (args)
4853 /* May be this should be sufficient_parms_p instead,
4854 depending on how exactly should user-defined literals
4855 work in presence of default arguments on the literal
4856 operator parameters. */
4857 && parmtypes == void_list_node)
4858 return decl;
4862 return error_mark_node;
4865 /* Parse a user-defined char constant. Returns a call to a user-defined
4866 literal operator taking the character as an argument. */
4868 static cp_expr
4869 cp_parser_userdef_char_literal (cp_parser *parser)
4871 cp_token *token = cp_lexer_consume_token (parser->lexer);
4872 tree literal = token->u.value;
4873 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4874 tree value = USERDEF_LITERAL_VALUE (literal);
4875 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4876 tree decl, result;
4878 /* Build up a call to the user-defined operator */
4879 /* Lookup the name we got back from the id-expression. */
4880 releasing_vec args;
4881 vec_safe_push (args, value);
4882 decl = lookup_literal_operator (name, args);
4883 if (!decl || decl == error_mark_node)
4885 error ("unable to find character literal operator %qD with %qT argument",
4886 name, TREE_TYPE (value));
4887 return error_mark_node;
4889 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4890 return result;
4893 /* A subroutine of cp_parser_userdef_numeric_literal to
4894 create a char... template parameter pack from a string node. */
4896 static tree
4897 make_char_string_pack (tree value)
4899 tree charvec;
4900 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4901 const unsigned char *str
4902 = (const unsigned char *) TREE_STRING_POINTER (value);
4903 int i, len = TREE_STRING_LENGTH (value) - 1;
4904 tree argvec = make_tree_vec (1);
4906 /* Fill in CHARVEC with all of the parameters. */
4907 charvec = make_tree_vec (len);
4908 for (i = 0; i < len; ++i)
4910 unsigned char s[3] = { '\'', str[i], '\'' };
4911 cpp_string in = { 3, s };
4912 cpp_string out = { 0, 0 };
4913 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4914 return NULL_TREE;
4915 gcc_assert (out.len == 2);
4916 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4917 out.text[0]);
4920 /* Build the argument packs. */
4921 ARGUMENT_PACK_ARGS (argpack) = charvec;
4923 TREE_VEC_ELT (argvec, 0) = argpack;
4925 return argvec;
4928 /* A subroutine of cp_parser_userdef_numeric_literal to
4929 create a char... template parameter pack from a string node. */
4931 static tree
4932 make_string_pack (tree value)
4934 tree charvec;
4935 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4936 const unsigned char *str
4937 = (const unsigned char *) TREE_STRING_POINTER (value);
4938 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4939 int len = TREE_STRING_LENGTH (value) / sz - 1;
4940 tree argvec = make_tree_vec (2);
4942 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4943 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4945 /* First template parm is character type. */
4946 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4948 /* Fill in CHARVEC with all of the parameters. */
4949 charvec = make_tree_vec (len);
4950 for (int i = 0; i < len; ++i)
4951 TREE_VEC_ELT (charvec, i)
4952 = double_int_to_tree (str_char_type_node,
4953 double_int::from_buffer (str + i * sz, sz));
4955 /* Build the argument packs. */
4956 ARGUMENT_PACK_ARGS (argpack) = charvec;
4958 TREE_VEC_ELT (argvec, 1) = argpack;
4960 return argvec;
4963 /* Parse a user-defined numeric constant. returns a call to a user-defined
4964 literal operator. */
4966 static cp_expr
4967 cp_parser_userdef_numeric_literal (cp_parser *parser)
4969 cp_token *token = cp_lexer_consume_token (parser->lexer);
4970 tree literal = token->u.value;
4971 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4972 tree value = USERDEF_LITERAL_VALUE (literal);
4973 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4974 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4975 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4976 tree decl, result;
4978 /* Look for a literal operator taking the exact type of numeric argument
4979 as the literal value. */
4980 releasing_vec args;
4981 vec_safe_push (args, value);
4982 decl = lookup_literal_operator (name, args);
4983 if (decl && decl != error_mark_node)
4985 result = finish_call_expr (decl, &args, false, true,
4986 tf_warning_or_error);
4988 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4990 warning_at (token->location, OPT_Woverflow,
4991 "integer literal exceeds range of %qT type",
4992 long_long_unsigned_type_node);
4994 else
4996 if (overflow > 0)
4997 warning_at (token->location, OPT_Woverflow,
4998 "floating literal exceeds range of %qT type",
4999 long_double_type_node);
5000 else if (overflow < 0)
5001 warning_at (token->location, OPT_Woverflow,
5002 "floating literal truncated to zero");
5005 return result;
5008 /* If the numeric argument didn't work, look for a raw literal
5009 operator taking a const char* argument consisting of the number
5010 in string format. */
5011 args->truncate (0);
5012 vec_safe_push (args, num_string);
5013 decl = lookup_literal_operator (name, args);
5014 if (decl && decl != error_mark_node)
5016 result = finish_call_expr (decl, &args, false, true,
5017 tf_warning_or_error);
5018 return result;
5021 /* If the raw literal didn't work, look for a non-type template
5022 function with parameter pack char.... Call the function with
5023 template parameter characters representing the number. */
5024 args->truncate (0);
5025 decl = lookup_literal_operator (name, args);
5026 if (decl && decl != error_mark_node)
5028 tree tmpl_args = make_char_string_pack (num_string);
5029 if (tmpl_args == NULL_TREE)
5031 error ("failed to translate literal to execution character set %qT",
5032 num_string);
5033 return error_mark_node;
5035 decl = lookup_template_function (decl, tmpl_args);
5036 result = finish_call_expr (decl, &args, false, true,
5037 tf_warning_or_error);
5038 return result;
5041 /* In C++14 the standard library defines complex number suffixes that
5042 conflict with GNU extensions. Prefer them if <complex> is #included. */
5043 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
5044 bool i14 = (cxx_dialect > cxx11
5045 && (id_equal (suffix_id, "i")
5046 || id_equal (suffix_id, "if")
5047 || id_equal (suffix_id, "il")));
5048 diagnostic_t kind = DK_ERROR;
5049 int opt = 0;
5051 if (i14 && ext)
5053 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
5054 LOOK_want::NORMAL, false);
5055 if (cxlit == error_mark_node)
5057 /* No <complex>, so pedwarn and use GNU semantics. */
5058 kind = DK_PEDWARN;
5059 opt = OPT_Wpedantic;
5063 bool complained
5064 = emit_diagnostic (kind, input_location, opt,
5065 "unable to find numeric literal operator %qD", name);
5067 if (!complained)
5068 /* Don't inform either. */;
5069 else if (i14)
5071 inform (token->location, "add %<using namespace std::complex_literals%> "
5072 "(from %<<complex>%>) to enable the C++14 user-defined literal "
5073 "suffixes");
5074 if (ext)
5075 inform (token->location, "or use %<j%> instead of %<i%> for the "
5076 "GNU built-in suffix");
5078 else if (!ext)
5079 inform (token->location, "use %<-fext-numeric-literals%> "
5080 "to enable more built-in suffixes");
5082 if (kind == DK_ERROR)
5083 value = error_mark_node;
5084 else
5086 /* Use the built-in semantics. */
5087 tree type;
5088 if (id_equal (suffix_id, "i"))
5090 if (TREE_CODE (value) == INTEGER_CST)
5091 type = integer_type_node;
5092 else
5093 type = double_type_node;
5095 else if (id_equal (suffix_id, "if"))
5096 type = float_type_node;
5097 else /* if (id_equal (suffix_id, "il")) */
5098 type = long_double_type_node;
5100 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5101 build_zero_cst (type), fold_convert (type, value));
5104 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5105 /* Avoid repeated diagnostics. */
5106 token->u.value = value;
5107 return value;
5110 /* Parse a user-defined string constant. Returns a call to a user-defined
5111 literal operator taking a character pointer and the length of the string
5112 as arguments. */
5114 static tree
5115 finish_userdef_string_literal (tree literal)
5117 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5118 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5119 tree value = USERDEF_LITERAL_VALUE (literal);
5120 int len = TREE_STRING_LENGTH (value)
5121 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5122 tree decl;
5124 /* Build up a call to the user-defined operator. */
5125 /* Lookup the name we got back from the id-expression. */
5126 releasing_vec args;
5127 vec_safe_push (args, value);
5128 vec_safe_push (args, build_int_cst (size_type_node, len));
5129 decl = lookup_literal_operator (name, args);
5131 if (decl && decl != error_mark_node)
5132 return finish_call_expr (decl, &args, false, true,
5133 tf_warning_or_error);
5135 /* Look for a suitable template function, either (C++20) with a single
5136 parameter of class type, or (N3599) with typename parameter CharT and
5137 parameter pack CharT... */
5138 args->truncate (0);
5139 decl = lookup_literal_operator (name, args);
5140 if (decl && decl != error_mark_node)
5142 /* Use resolve_nondeduced_context to try to choose one form of template
5143 or the other. */
5144 tree tmpl_args = make_tree_vec (1);
5145 TREE_VEC_ELT (tmpl_args, 0) = value;
5146 decl = lookup_template_function (decl, tmpl_args);
5147 tree res = resolve_nondeduced_context (decl, tf_none);
5148 if (DECL_P (res))
5149 decl = res;
5150 else
5152 TREE_OPERAND (decl, 1) = make_string_pack (value);
5153 res = resolve_nondeduced_context (decl, tf_none);
5154 if (DECL_P (res))
5155 decl = res;
5157 if (!DECL_P (decl) && cxx_dialect > cxx17)
5158 TREE_OPERAND (decl, 1) = tmpl_args;
5159 return finish_call_expr (decl, &args, false, true,
5160 tf_warning_or_error);
5163 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5164 name, TREE_TYPE (value), size_type_node);
5165 return error_mark_node;
5169 /* Basic concepts [gram.basic] */
5171 /* Parse a translation-unit.
5173 translation-unit:
5174 declaration-seq [opt] */
5176 static void
5177 cp_parser_translation_unit (cp_parser* parser)
5179 gcc_checking_assert (!cp_error_declarator);
5181 /* Create the declarator obstack. */
5182 gcc_obstack_init (&declarator_obstack);
5183 /* Create the error declarator. */
5184 cp_error_declarator = make_declarator (cdk_error);
5185 /* Create the empty parameter list. */
5186 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5187 UNKNOWN_LOCATION);
5188 /* Remember where the base of the declarator obstack lies. */
5189 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5191 push_deferring_access_checks (flag_access_control
5192 ? dk_no_deferred : dk_no_check);
5194 module_parse mp_state = MP_NOT_MODULE;
5195 if (modules_p () && !header_module_p ())
5196 mp_state = MP_FIRST;
5198 bool implicit_extern_c = false;
5200 /* Parse until EOF. */
5201 for (;;)
5203 cp_token *token = cp_lexer_peek_token (parser->lexer);
5205 /* If we're entering or exiting a region that's implicitly
5206 extern "C", modify the lang context appropriately. This is
5207 so horrible. Please die. */
5208 if (implicit_extern_c
5209 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5211 implicit_extern_c = !implicit_extern_c;
5212 if (implicit_extern_c)
5213 push_lang_context (lang_name_c);
5214 else
5215 pop_lang_context ();
5218 if (token->type == CPP_EOF)
5219 break;
5221 if (modules_p ())
5223 /* Top-level module declarations are ok, and change the
5224 portion of file we're in. Top-level import declarations
5225 are significant for the import portions. */
5227 cp_token *next = token;
5228 bool exporting = token->keyword == RID__EXPORT;
5229 if (exporting)
5231 cp_lexer_consume_token (parser->lexer);
5232 next = cp_lexer_peek_token (parser->lexer);
5234 if (next->keyword == RID__MODULE)
5236 mp_state
5237 = cp_parser_module_declaration (parser, mp_state, exporting);
5238 continue;
5240 else if (next->keyword == RID__IMPORT)
5242 if (mp_state == MP_FIRST)
5243 mp_state = MP_NOT_MODULE;
5244 cp_parser_import_declaration (parser, mp_state, exporting);
5245 continue;
5247 else
5248 gcc_checking_assert (!exporting);
5250 if (mp_state == MP_GLOBAL && token->main_source_p)
5252 static bool warned = false;
5253 if (!warned)
5255 warned = true;
5256 error_at (token->location,
5257 "global module fragment contents must be"
5258 " from preprocessor inclusion");
5263 /* This relies on the ordering of module_parse values. */
5264 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5265 /* We're no longer in the import portion of a named module. */
5266 mp_state = module_parse (mp_state + 1);
5267 else if (mp_state == MP_FIRST)
5268 mp_state = MP_NOT_MODULE;
5270 if (token->type == CPP_CLOSE_BRACE)
5272 cp_parser_error (parser, "expected declaration");
5273 cp_lexer_consume_token (parser->lexer);
5274 /* If the next token is now a `;', consume it. */
5275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5276 cp_lexer_consume_token (parser->lexer);
5278 else
5279 cp_parser_toplevel_declaration (parser);
5282 /* Get rid of the token array; we don't need it any more. */
5283 cp_lexer_destroy (parser->lexer);
5284 parser->lexer = NULL;
5286 /* The EOF should have reset this. */
5287 gcc_checking_assert (!implicit_extern_c);
5289 /* Make sure the declarator obstack was fully cleaned up. */
5290 gcc_assert (obstack_next_free (&declarator_obstack)
5291 == declarator_obstack_base);
5294 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5295 decltype context. */
5297 static inline tsubst_flags_t
5298 complain_flags (bool decltype_p)
5300 tsubst_flags_t complain = tf_warning_or_error;
5301 if (decltype_p)
5302 complain |= tf_decltype;
5303 return complain;
5306 /* We're about to parse a collection of statements. If we're currently
5307 parsing tentatively, set up a firewall so that any nested
5308 cp_parser_commit_to_tentative_parse won't affect the current context. */
5310 static cp_token_position
5311 cp_parser_start_tentative_firewall (cp_parser *parser)
5313 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5314 return 0;
5316 cp_parser_parse_tentatively (parser);
5317 cp_parser_commit_to_topmost_tentative_parse (parser);
5318 return cp_lexer_token_position (parser->lexer, false);
5321 /* We've finished parsing the collection of statements. Wrap up the
5322 firewall and replace the relevant tokens with the parsed form. */
5324 static void
5325 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5326 tree expr)
5328 if (!start)
5329 return;
5331 /* Finish the firewall level. */
5332 cp_parser_parse_definitely (parser);
5333 /* And remember the result of the parse for when we try again. */
5334 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5335 token->type = CPP_PREPARSED_EXPR;
5336 token->u.value = expr;
5337 token->keyword = RID_MAX;
5338 cp_lexer_purge_tokens_after (parser->lexer, start);
5341 /* Like the above functions, but let the user modify the tokens. Used by
5342 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5343 later parses, so it makes sense to localize the effects of
5344 cp_parser_commit_to_tentative_parse. */
5346 struct tentative_firewall
5348 cp_parser *parser;
5349 bool set;
5351 tentative_firewall (cp_parser *p): parser(p)
5353 /* If we're currently parsing tentatively, start a committed level as a
5354 firewall and then an inner tentative parse. */
5355 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5357 cp_parser_parse_tentatively (parser);
5358 cp_parser_commit_to_topmost_tentative_parse (parser);
5359 cp_parser_parse_tentatively (parser);
5363 ~tentative_firewall()
5365 if (set)
5367 /* Finish the inner tentative parse and the firewall, propagating any
5368 uncommitted error state to the outer tentative parse. */
5369 bool err = cp_parser_error_occurred (parser);
5370 cp_parser_parse_definitely (parser);
5371 cp_parser_parse_definitely (parser);
5372 if (err)
5373 cp_parser_simulate_error (parser);
5378 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5379 This class is for tracking such a matching pair of symbols.
5380 In particular, it tracks the location of the first token,
5381 so that if the second token is missing, we can highlight the
5382 location of the first token when notifying the user about the
5383 problem. */
5385 template <typename traits_t>
5386 class token_pair
5388 public:
5389 /* token_pair's ctor. */
5390 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5392 /* If the next token is the opening symbol for this pair, consume it and
5393 return true.
5394 Otherwise, issue an error and return false.
5395 In either case, record the location of the opening token. */
5397 bool require_open (cp_parser *parser)
5399 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5400 return cp_parser_require (parser, traits_t::open_token_type,
5401 traits_t::required_token_open);
5404 /* Consume the next token from PARSER, recording its location as
5405 that of the opening token within the pair. */
5407 cp_token * consume_open (cp_parser *parser)
5409 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5410 gcc_assert (tok->type == traits_t::open_token_type);
5411 m_open_loc = tok->location;
5412 return tok;
5415 /* If the next token is the closing symbol for this pair, consume it
5416 and return it.
5417 Otherwise, issue an error, highlighting the location of the
5418 corresponding opening token, and return NULL. */
5420 cp_token *require_close (cp_parser *parser) const
5422 return cp_parser_require (parser, traits_t::close_token_type,
5423 traits_t::required_token_close,
5424 m_open_loc);
5427 location_t open_location () const { return m_open_loc; }
5429 private:
5430 location_t m_open_loc;
5433 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5435 struct matching_paren_traits
5437 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5438 static const enum required_token required_token_open = RT_OPEN_PAREN;
5439 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5440 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5443 /* "matching_parens" is a token_pair<T> class for tracking matching
5444 pairs of parentheses. */
5446 typedef token_pair<matching_paren_traits> matching_parens;
5448 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5450 struct matching_brace_traits
5452 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5453 static const enum required_token required_token_open = RT_OPEN_BRACE;
5454 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5455 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5458 /* "matching_braces" is a token_pair<T> class for tracking matching
5459 pairs of braces. */
5461 typedef token_pair<matching_brace_traits> matching_braces;
5464 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5465 enclosing parentheses. */
5467 static cp_expr
5468 cp_parser_statement_expr (cp_parser *parser)
5470 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5471 auto oas = make_temp_override (parser->omp_array_section_p, false);
5473 /* Consume the '('. */
5474 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5475 matching_parens parens;
5476 parens.consume_open (parser);
5477 /* Start the statement-expression. */
5478 tree expr = begin_stmt_expr ();
5479 /* Parse the compound-statement. */
5480 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5481 /* Finish up. */
5482 expr = finish_stmt_expr (expr, false);
5483 /* Consume the ')'. */
5484 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5485 if (!parens.require_close (parser))
5486 cp_parser_skip_to_end_of_statement (parser);
5488 cp_parser_end_tentative_firewall (parser, start, expr);
5489 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5490 return cp_expr (expr, combined_loc);
5493 /* Expressions [gram.expr] */
5495 /* Parse a fold-operator.
5497 fold-operator:
5498 - * / % ^ & | = < > << >>
5499 = -= *= /= %= ^= &= |= <<= >>=
5500 == != <= >= && || , .* ->*
5502 This returns the tree code corresponding to the matched operator
5503 as an int. When the current token matches a compound assignment
5504 operator, the resulting tree code is the negative value of the
5505 non-assignment operator. */
5507 static int
5508 cp_parser_fold_operator (cp_token *token)
5510 switch (token->type)
5512 case CPP_PLUS: return PLUS_EXPR;
5513 case CPP_MINUS: return MINUS_EXPR;
5514 case CPP_MULT: return MULT_EXPR;
5515 case CPP_DIV: return TRUNC_DIV_EXPR;
5516 case CPP_MOD: return TRUNC_MOD_EXPR;
5517 case CPP_XOR: return BIT_XOR_EXPR;
5518 case CPP_AND: return BIT_AND_EXPR;
5519 case CPP_OR: return BIT_IOR_EXPR;
5520 case CPP_LSHIFT: return LSHIFT_EXPR;
5521 case CPP_RSHIFT: return RSHIFT_EXPR;
5523 case CPP_EQ: return -NOP_EXPR;
5524 case CPP_PLUS_EQ: return -PLUS_EXPR;
5525 case CPP_MINUS_EQ: return -MINUS_EXPR;
5526 case CPP_MULT_EQ: return -MULT_EXPR;
5527 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5528 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5529 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5530 case CPP_AND_EQ: return -BIT_AND_EXPR;
5531 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5532 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5533 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5535 case CPP_EQ_EQ: return EQ_EXPR;
5536 case CPP_NOT_EQ: return NE_EXPR;
5537 case CPP_LESS: return LT_EXPR;
5538 case CPP_GREATER: return GT_EXPR;
5539 case CPP_LESS_EQ: return LE_EXPR;
5540 case CPP_GREATER_EQ: return GE_EXPR;
5542 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5543 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5545 case CPP_COMMA: return COMPOUND_EXPR;
5547 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5548 case CPP_DEREF_STAR: return MEMBER_REF;
5550 default: return ERROR_MARK;
5554 /* Returns true if CODE indicates a binary expression, which is not allowed in
5555 the LHS of a fold-expression. More codes will need to be added to use this
5556 function in other contexts. */
5558 static bool
5559 is_binary_op (tree_code code)
5561 switch (code)
5563 case PLUS_EXPR:
5564 case POINTER_PLUS_EXPR:
5565 case MINUS_EXPR:
5566 case MULT_EXPR:
5567 case TRUNC_DIV_EXPR:
5568 case TRUNC_MOD_EXPR:
5569 case BIT_XOR_EXPR:
5570 case BIT_AND_EXPR:
5571 case BIT_IOR_EXPR:
5572 case LSHIFT_EXPR:
5573 case RSHIFT_EXPR:
5575 case MODOP_EXPR:
5577 case EQ_EXPR:
5578 case NE_EXPR:
5579 case LE_EXPR:
5580 case GE_EXPR:
5581 case LT_EXPR:
5582 case GT_EXPR:
5584 case TRUTH_ANDIF_EXPR:
5585 case TRUTH_ORIF_EXPR:
5587 case COMPOUND_EXPR:
5589 case DOTSTAR_EXPR:
5590 case MEMBER_REF:
5591 return true;
5593 default:
5594 return false;
5598 /* If the next token is a suitable fold operator, consume it and return as
5599 the function above. */
5601 static int
5602 cp_parser_fold_operator (cp_parser *parser)
5604 cp_token* token = cp_lexer_peek_token (parser->lexer);
5605 int code = cp_parser_fold_operator (token);
5606 if (code != ERROR_MARK)
5607 cp_lexer_consume_token (parser->lexer);
5608 return code;
5611 /* Parse a fold-expression.
5613 fold-expression:
5614 ( ... folding-operator cast-expression)
5615 ( cast-expression folding-operator ... )
5616 ( cast-expression folding operator ... folding-operator cast-expression)
5618 Note that the '(' and ')' are matched in primary expression. */
5620 static cp_expr
5621 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5623 cp_id_kind pidk;
5624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5625 const cp_token *token = nullptr;
5627 // Left fold.
5628 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5630 if (expr1)
5631 return error_mark_node;
5632 cp_lexer_consume_token (parser->lexer);
5633 token = cp_lexer_peek_token (parser->lexer);
5634 int op = cp_parser_fold_operator (parser);
5635 if (op == ERROR_MARK)
5637 cp_parser_error (parser, "expected binary operator");
5638 return error_mark_node;
5641 tree expr = cp_parser_cast_expression (parser, false, false,
5642 false, &pidk);
5643 if (expr == error_mark_node)
5644 return error_mark_node;
5645 loc = make_location (token->location, loc, parser->lexer);
5646 return finish_left_unary_fold_expr (loc, expr, op);
5649 token = cp_lexer_peek_token (parser->lexer);
5650 int op = cp_parser_fold_operator (parser);
5651 if (op == ERROR_MARK)
5653 cp_parser_error (parser, "expected binary operator");
5654 return error_mark_node;
5657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5659 cp_parser_error (parser, "expected ...");
5660 return error_mark_node;
5662 cp_lexer_consume_token (parser->lexer);
5664 /* The operands of a fold-expression are cast-expressions, so binary or
5665 conditional expressions are not allowed. We check this here to avoid
5666 tentative parsing. */
5667 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5668 /* OK, the expression was parenthesized. */;
5669 else if (is_binary_op (TREE_CODE (expr1)))
5670 error_at (location_of (expr1),
5671 "binary expression in operand of fold-expression");
5672 else if (TREE_CODE (expr1) == COND_EXPR
5673 || (REFERENCE_REF_P (expr1)
5674 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5675 error_at (location_of (expr1),
5676 "conditional expression in operand of fold-expression");
5678 // Right fold.
5679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5681 loc = make_location (token->location, loc, parser->lexer);
5682 return finish_right_unary_fold_expr (loc, expr1, op);
5685 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5687 cp_parser_error (parser, "mismatched operator in fold-expression");
5688 return error_mark_node;
5690 cp_lexer_consume_token (parser->lexer);
5692 // Binary left or right fold.
5693 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5694 if (expr2 == error_mark_node)
5695 return error_mark_node;
5696 loc = make_location (token->location, loc, parser->lexer);
5697 return finish_binary_fold_expr (loc, expr1, expr2, op);
5700 /* Parse a primary-expression.
5702 primary-expression:
5703 literal
5704 this
5705 ( expression )
5706 id-expression
5707 lambda-expression (C++11)
5709 GNU Extensions:
5711 primary-expression:
5712 ( compound-statement )
5713 __builtin_va_arg ( assignment-expression , type-id )
5714 __builtin_offsetof ( type-id , offsetof-expression )
5716 C++ Extensions:
5717 __has_nothrow_assign ( type-id )
5718 __has_nothrow_constructor ( type-id )
5719 __has_nothrow_copy ( type-id )
5720 __has_trivial_assign ( type-id )
5721 __has_trivial_constructor ( type-id )
5722 __has_trivial_copy ( type-id )
5723 __has_trivial_destructor ( type-id )
5724 __has_virtual_destructor ( type-id )
5725 __is_abstract ( type-id )
5726 __is_base_of ( type-id , type-id )
5727 __is_class ( type-id )
5728 __is_empty ( type-id )
5729 __is_enum ( type-id )
5730 __is_final ( type-id )
5731 __is_literal_type ( type-id )
5732 __is_pod ( type-id )
5733 __is_polymorphic ( type-id )
5734 __is_std_layout ( type-id )
5735 __is_trivial ( type-id )
5736 __is_union ( type-id )
5738 Objective-C++ Extension:
5740 primary-expression:
5741 objc-expression
5743 literal:
5744 __null
5746 ADDRESS_P is true iff this expression was immediately preceded by
5747 "&" and therefore might denote a pointer-to-member. CAST_P is true
5748 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5749 true iff this expression is a template argument.
5751 Returns a representation of the expression. Upon return, *IDK
5752 indicates what kind of id-expression (if any) was present. */
5754 static cp_expr
5755 cp_parser_primary_expression (cp_parser *parser,
5756 bool address_p,
5757 bool cast_p,
5758 bool template_arg_p,
5759 bool decltype_p,
5760 cp_id_kind *idk)
5762 cp_token *token = NULL;
5764 /* Assume the primary expression is not an id-expression. */
5765 *idk = CP_ID_KIND_NONE;
5767 /* Peek at the next token. */
5768 token = cp_lexer_peek_token (parser->lexer);
5769 switch ((int) token->type)
5771 /* literal:
5772 integer-literal
5773 character-literal
5774 floating-literal
5775 string-literal
5776 boolean-literal
5777 pointer-literal
5778 user-defined-literal */
5779 case CPP_CHAR:
5780 case CPP_CHAR16:
5781 case CPP_CHAR32:
5782 case CPP_WCHAR:
5783 case CPP_UTF8CHAR:
5784 case CPP_NUMBER:
5785 case CPP_PREPARSED_EXPR:
5786 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5787 return cp_parser_userdef_numeric_literal (parser);
5788 token = cp_lexer_consume_token (parser->lexer);
5789 if (TREE_CODE (token->u.value) == FIXED_CST)
5791 error_at (token->location,
5792 "fixed-point types not supported in C++");
5793 return error_mark_node;
5795 /* Floating-point literals are only allowed in an integral
5796 constant expression if they are cast to an integral or
5797 enumeration type. */
5798 if ((TREE_CODE (token->u.value) == REAL_CST
5799 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5800 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5801 && parser->integral_constant_expression_p
5802 && pedantic)
5804 /* CAST_P will be set even in invalid code like "int(2.7 +
5805 ...)". Therefore, we have to check that the next token
5806 is sure to end the cast. */
5807 if (cast_p)
5809 cp_token *next_token;
5811 next_token = cp_lexer_peek_token (parser->lexer);
5812 if (/* The comma at the end of an
5813 enumerator-definition. */
5814 next_token->type != CPP_COMMA
5815 /* The curly brace at the end of an enum-specifier. */
5816 && next_token->type != CPP_CLOSE_BRACE
5817 /* The end of a statement. */
5818 && next_token->type != CPP_SEMICOLON
5819 /* The end of the cast-expression. */
5820 && next_token->type != CPP_CLOSE_PAREN
5821 /* The end of an array bound. */
5822 && next_token->type != CPP_CLOSE_SQUARE
5823 /* The closing ">" in a template-argument-list. */
5824 && (next_token->type != CPP_GREATER
5825 || parser->greater_than_is_operator_p)
5826 /* C++0x only: A ">>" treated like two ">" tokens,
5827 in a template-argument-list. */
5828 && (next_token->type != CPP_RSHIFT
5829 || (cxx_dialect == cxx98)
5830 || parser->greater_than_is_operator_p))
5831 cast_p = false;
5834 /* If we are within a cast, then the constraint that the
5835 cast is to an integral or enumeration type will be
5836 checked at that point. If we are not within a cast, then
5837 this code is invalid. */
5838 if (!cast_p)
5839 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5841 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5842 .maybe_add_location_wrapper ());
5844 case CPP_CHAR_USERDEF:
5845 case CPP_CHAR16_USERDEF:
5846 case CPP_CHAR32_USERDEF:
5847 case CPP_WCHAR_USERDEF:
5848 case CPP_UTF8CHAR_USERDEF:
5849 return cp_parser_userdef_char_literal (parser);
5851 case CPP_STRING:
5852 case CPP_STRING16:
5853 case CPP_STRING32:
5854 case CPP_WSTRING:
5855 case CPP_UTF8STRING:
5856 case CPP_STRING_USERDEF:
5857 case CPP_STRING16_USERDEF:
5858 case CPP_STRING32_USERDEF:
5859 case CPP_WSTRING_USERDEF:
5860 case CPP_UTF8STRING_USERDEF:
5861 /* ??? Should wide strings be allowed when parser->translate_strings_p
5862 is false (i.e. in attributes)? If not, we can kill the third
5863 argument to cp_parser_string_literal. */
5864 if (parser->translate_strings_p)
5865 return (cp_parser_userdef_string_literal (parser,
5866 /*lookup_udlit=*/true)
5867 .maybe_add_location_wrapper ());
5868 else
5869 return (cp_parser_string_literal (parser,
5870 /*translate=*/false,
5871 /*wide_ok=*/true)
5872 .maybe_add_location_wrapper ());
5874 case CPP_OPEN_PAREN:
5875 /* If we see `( { ' then we are looking at the beginning of
5876 a GNU statement-expression. */
5877 if (cp_parser_allow_gnu_extensions_p (parser)
5878 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5880 /* Statement-expressions are not allowed by the standard. */
5881 pedwarn (token->location, OPT_Wpedantic,
5882 "ISO C++ forbids braced-groups within expressions");
5884 /* And they're not allowed outside of a function-body; you
5885 cannot, for example, write:
5887 int i = ({ int j = 3; j + 1; });
5889 at class or namespace scope. */
5890 if (!parser->in_function_body
5891 || parser->in_template_argument_list_p)
5893 error_at (token->location,
5894 "statement-expressions are not allowed outside "
5895 "functions nor in template-argument lists");
5896 cp_parser_skip_to_end_of_block_or_statement (parser);
5897 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5898 cp_lexer_consume_token (parser->lexer);
5899 return error_mark_node;
5901 else
5902 return cp_parser_statement_expr (parser);
5904 /* Otherwise it's a normal parenthesized expression. */
5906 cp_expr expr;
5907 bool saved_greater_than_is_operator_p;
5909 location_t open_paren_loc = token->location;
5911 /* Consume the `('. */
5912 matching_parens parens;
5913 parens.consume_open (parser);
5914 /* Within a parenthesized expression, a `>' token is always
5915 the greater-than operator. */
5916 saved_greater_than_is_operator_p
5917 = parser->greater_than_is_operator_p;
5918 parser->greater_than_is_operator_p = true;
5920 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5921 /* Left fold expression. */
5922 expr = NULL_TREE;
5923 else
5924 /* Parse the parenthesized expression. */
5925 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5927 token = cp_lexer_peek_token (parser->lexer);
5928 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5930 expr = cp_parser_fold_expression (parser, expr);
5931 if (expr != error_mark_node
5932 && cxx_dialect < cxx17)
5933 pedwarn (input_location, OPT_Wc__17_extensions,
5934 "fold-expressions only available with %<-std=c++17%> "
5935 "or %<-std=gnu++17%>");
5937 else
5938 /* Let the front end know that this expression was
5939 enclosed in parentheses. This matters in case, for
5940 example, the expression is of the form `A::B', since
5941 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5942 not. */
5943 expr = finish_parenthesized_expr (expr);
5945 /* DR 705: Wrapping an unqualified name in parentheses
5946 suppresses arg-dependent lookup. We want to pass back
5947 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5948 (c++/37862), but none of the others. */
5949 if (*idk != CP_ID_KIND_QUALIFIED)
5950 *idk = CP_ID_KIND_NONE;
5952 /* The `>' token might be the end of a template-id or
5953 template-parameter-list now. */
5954 parser->greater_than_is_operator_p
5955 = saved_greater_than_is_operator_p;
5957 /* Consume the `)'. */
5958 token = cp_lexer_peek_token (parser->lexer);
5959 location_t close_paren_loc = token->location;
5960 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5961 expr.set_range (open_paren_loc, close_paren_loc);
5962 if (no_wparens)
5963 suppress_warning (expr, OPT_Wparentheses);
5964 if (!parens.require_close (parser)
5965 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5966 cp_parser_skip_to_end_of_statement (parser);
5968 return expr;
5971 case CPP_OPEN_SQUARE:
5973 if (c_dialect_objc ())
5975 /* We might have an Objective-C++ message. */
5976 cp_parser_parse_tentatively (parser);
5977 tree msg = cp_parser_objc_message_expression (parser);
5978 /* If that works out, we're done ... */
5979 if (cp_parser_parse_definitely (parser))
5980 return msg;
5981 /* ... else, fall though to see if it's a lambda. */
5983 cp_expr lam = cp_parser_lambda_expression (parser);
5984 /* Don't warn about a failed tentative parse. */
5985 if (cp_parser_error_occurred (parser))
5986 return error_mark_node;
5987 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5988 return lam;
5991 case CPP_OBJC_STRING:
5992 if (c_dialect_objc ())
5993 /* We have an Objective-C++ string literal. */
5994 return cp_parser_objc_expression (parser);
5995 cp_parser_error (parser, "expected primary-expression");
5996 return error_mark_node;
5998 case CPP_KEYWORD:
5999 switch (token->keyword)
6001 /* These two are the boolean literals. */
6002 case RID_TRUE:
6003 cp_lexer_consume_token (parser->lexer);
6004 return cp_expr (boolean_true_node, token->location);
6005 case RID_FALSE:
6006 cp_lexer_consume_token (parser->lexer);
6007 return cp_expr (boolean_false_node, token->location);
6009 /* The `__null' literal. */
6010 case RID_NULL:
6011 cp_lexer_consume_token (parser->lexer);
6012 return cp_expr (null_node, token->location);
6014 /* The `nullptr' literal. */
6015 case RID_NULLPTR:
6016 cp_lexer_consume_token (parser->lexer);
6017 return cp_expr (nullptr_node, token->location);
6019 /* Recognize the `this' keyword. */
6020 case RID_THIS:
6021 cp_lexer_consume_token (parser->lexer);
6022 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
6024 error_at (token->location,
6025 "%<this%> may not be used in this context");
6026 return error_mark_node;
6028 /* Pointers cannot appear in constant-expressions. */
6029 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
6030 return error_mark_node;
6031 return cp_expr (finish_this_expr (), token->location);
6033 /* The `operator' keyword can be the beginning of an
6034 id-expression. */
6035 case RID_OPERATOR:
6036 goto id_expression;
6038 case RID_FUNCTION_NAME:
6039 case RID_PRETTY_FUNCTION_NAME:
6040 case RID_C99_FUNCTION_NAME:
6042 non_integral_constant name;
6044 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
6045 __func__ are the names of variables -- but they are
6046 treated specially. Therefore, they are handled here,
6047 rather than relying on the generic id-expression logic
6048 below. Grammatically, these names are id-expressions.
6050 Consume the token. */
6051 token = cp_lexer_consume_token (parser->lexer);
6053 switch (token->keyword)
6055 case RID_FUNCTION_NAME:
6056 name = NIC_FUNC_NAME;
6057 break;
6058 case RID_PRETTY_FUNCTION_NAME:
6059 name = NIC_PRETTY_FUNC;
6060 break;
6061 case RID_C99_FUNCTION_NAME:
6062 name = NIC_C99_FUNC;
6063 break;
6064 default:
6065 gcc_unreachable ();
6068 if (cp_parser_non_integral_constant_expression (parser, name))
6069 return error_mark_node;
6071 /* Look up the name. */
6072 return finish_fname (token->u.value);
6075 case RID_VA_ARG:
6077 tree expression;
6078 tree type;
6079 location_t type_location;
6080 location_t start_loc
6081 = cp_lexer_peek_token (parser->lexer)->location;
6082 /* The `__builtin_va_arg' construct is used to handle
6083 `va_arg'. Consume the `__builtin_va_arg' token. */
6084 cp_lexer_consume_token (parser->lexer);
6085 /* Look for the opening `('. */
6086 matching_parens parens;
6087 parens.require_open (parser);
6088 /* Now, parse the assignment-expression. */
6089 expression = cp_parser_assignment_expression (parser);
6090 /* Look for the `,'. */
6091 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
6092 type_location = cp_lexer_peek_token (parser->lexer)->location;
6093 /* Parse the type-id. */
6095 type_id_in_expr_sentinel s (parser);
6096 type = cp_parser_type_id (parser);
6098 /* Look for the closing `)'. */
6099 location_t finish_loc
6100 = cp_lexer_peek_token (parser->lexer)->location;
6101 parens.require_close (parser);
6102 /* Using `va_arg' in a constant-expression is not
6103 allowed. */
6104 if (cp_parser_non_integral_constant_expression (parser,
6105 NIC_VA_ARG))
6106 return error_mark_node;
6107 /* Construct a location of the form:
6108 __builtin_va_arg (v, int)
6109 ~~~~~~~~~~~~~~~~~~~~~^~~~
6110 with the caret at the type, ranging from the start of the
6111 "__builtin_va_arg" token to the close paren. */
6112 location_t combined_loc
6113 = make_location (type_location, start_loc, finish_loc);
6114 return build_x_va_arg (combined_loc, expression, type);
6117 case RID_OFFSETOF:
6118 return cp_parser_builtin_offsetof (parser);
6120 // C++ concepts
6121 case RID_REQUIRES:
6122 return cp_parser_requires_expression (parser);
6124 /* Objective-C++ expressions. */
6125 case RID_AT_ENCODE:
6126 case RID_AT_PROTOCOL:
6127 case RID_AT_SELECTOR:
6128 return cp_parser_objc_expression (parser);
6130 case RID_OMP_ALL_MEMORY:
6131 gcc_assert (flag_openmp);
6132 cp_lexer_consume_token (parser->lexer);
6133 error_at (token->location,
6134 "%<omp_all_memory%> may only be used in OpenMP "
6135 "%<depend%> clause");
6136 return error_mark_node;
6138 case RID_TEMPLATE:
6139 if (parser->in_function_body
6140 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6141 == CPP_LESS))
6143 error_at (token->location,
6144 "a template declaration cannot appear at block scope");
6145 cp_parser_skip_to_end_of_block_or_statement (parser);
6146 return error_mark_node;
6148 /* FALLTHRU */
6149 default:
6150 cp_parser_error (parser, "expected primary-expression");
6151 return error_mark_node;
6154 /* An id-expression can start with either an identifier, a
6155 `::' as the beginning of a qualified-id, or the "operator"
6156 keyword. */
6157 case CPP_NAME:
6158 if (const cp_trait* trait = cp_lexer_peek_trait_expr (parser->lexer))
6159 return cp_parser_trait (parser, trait);
6160 /* FALLTHRU */
6161 case CPP_SCOPE:
6162 case CPP_TEMPLATE_ID:
6163 case CPP_NESTED_NAME_SPECIFIER:
6165 id_expression:
6166 cp_expr id_expression;
6167 cp_expr decl;
6168 const char *error_msg;
6169 bool template_p;
6170 bool done;
6171 cp_token *id_expr_token;
6173 /* Parse the id-expression. */
6174 id_expression
6175 = cp_parser_id_expression (parser,
6176 /*template_keyword_p=*/false,
6177 /*check_dependency_p=*/true,
6178 &template_p,
6179 /*declarator_p=*/false,
6180 /*optional_p=*/false);
6181 if (id_expression == error_mark_node)
6182 return error_mark_node;
6183 id_expr_token = token;
6184 token = cp_lexer_peek_token (parser->lexer);
6185 done = (token->type != CPP_OPEN_SQUARE
6186 && token->type != CPP_OPEN_PAREN
6187 && token->type != CPP_DOT
6188 && token->type != CPP_DEREF
6189 && token->type != CPP_PLUS_PLUS
6190 && token->type != CPP_MINUS_MINUS);
6191 /* If we have a template-id, then no further lookup is
6192 required. If the template-id was for a template-class, we
6193 will sometimes have a TYPE_DECL at this point. */
6194 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6195 || TREE_CODE (id_expression) == TYPE_DECL)
6196 decl = id_expression;
6197 /* Look up the name. */
6198 else
6200 tree ambiguous_decls;
6202 /* If we already know that this lookup is ambiguous, then
6203 we've already issued an error message; there's no reason
6204 to check again. */
6205 if (id_expr_token->type == CPP_NAME
6206 && id_expr_token->error_reported)
6208 cp_parser_simulate_error (parser);
6209 return error_mark_node;
6212 decl = cp_parser_lookup_name (parser, id_expression,
6213 none_type,
6214 template_p,
6215 /*is_namespace=*/false,
6216 /*check_dependency=*/true,
6217 &ambiguous_decls,
6218 id_expression.get_location ());
6219 /* If the lookup was ambiguous, an error will already have
6220 been issued. */
6221 if (ambiguous_decls)
6222 return error_mark_node;
6224 /* In Objective-C++, we may have an Objective-C 2.0
6225 dot-syntax for classes here. */
6226 if (c_dialect_objc ()
6227 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6228 && TREE_CODE (decl) == TYPE_DECL
6229 && objc_is_class_name (decl))
6231 tree component;
6232 cp_lexer_consume_token (parser->lexer);
6233 component = cp_parser_identifier (parser);
6234 if (component == error_mark_node)
6235 return error_mark_node;
6237 tree result = objc_build_class_component_ref (id_expression,
6238 component);
6239 /* Build a location of the form:
6240 expr.component
6241 ~~~~~^~~~~~~~~
6242 with caret at the start of the component name (at
6243 input_location), ranging from the start of the id_expression
6244 to the end of the component name. */
6245 location_t combined_loc
6246 = make_location (input_location, id_expression.get_start (),
6247 get_finish (input_location));
6248 protected_set_expr_location (result, combined_loc);
6249 return result;
6252 /* In Objective-C++, an instance variable (ivar) may be preferred
6253 to whatever cp_parser_lookup_name() found.
6254 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6255 rest of c-family, we have to do a little extra work to preserve
6256 any location information in cp_expr "decl". Given that
6257 objc_lookup_ivar is implemented in "c-family" and "objc", we
6258 have a trip through the pure "tree" type, rather than cp_expr.
6259 Naively copying it back to "decl" would implicitly give the
6260 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6261 store an EXPR_LOCATION. Hence we only update "decl" (and
6262 hence its location_t) if we get back a different tree node. */
6263 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6264 id_expression);
6265 if (decl_tree != decl.get_value ())
6266 decl = cp_expr (decl_tree);
6268 /* If name lookup gives us a SCOPE_REF, then the
6269 qualifying scope was dependent. */
6270 if (TREE_CODE (decl) == SCOPE_REF)
6272 /* At this point, we do not know if DECL is a valid
6273 integral constant expression. We assume that it is
6274 in fact such an expression, so that code like:
6276 template <int N> struct A {
6277 int a[B<N>::i];
6280 is accepted. At template-instantiation time, we
6281 will check that B<N>::i is actually a constant. */
6282 return decl;
6284 /* Check to see if DECL is a local variable in a context
6285 where that is forbidden. */
6286 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6287 && local_variable_p (decl)
6288 /* DR 2082 permits local variables in unevaluated contexts
6289 within a default argument. */
6290 && !cp_unevaluated_operand)
6292 const char *msg
6293 = (TREE_CODE (decl) == PARM_DECL
6294 ? G_("parameter %qD may not appear in this context")
6295 : G_("local variable %qD may not appear in this context"));
6296 error_at (id_expression.get_location (), msg,
6297 decl.get_value ());
6298 return error_mark_node;
6302 decl = (finish_id_expression
6303 (id_expression, decl, parser->scope,
6304 idk,
6305 parser->integral_constant_expression_p,
6306 parser->allow_non_integral_constant_expression_p,
6307 &parser->non_integral_constant_expression_p,
6308 template_p, done, address_p,
6309 template_arg_p,
6310 &error_msg,
6311 id_expression.get_location ()));
6312 if (error_msg)
6313 cp_parser_error (parser, error_msg);
6314 /* Build a location for an id-expression of the form:
6315 ::ns::id
6316 ~~~~~~^~
6320 i.e. from the start of the first token to the end of the final
6321 token, with the caret at the start of the unqualified-id. */
6322 location_t caret_loc = get_pure_location (id_expression.get_location ());
6323 location_t start_loc = get_start (id_expr_token->location);
6324 location_t finish_loc = get_finish (id_expression.get_location ());
6325 location_t combined_loc
6326 = make_location (caret_loc, start_loc, finish_loc);
6328 decl.set_location (combined_loc);
6329 return decl;
6332 /* Anything else is an error. */
6333 default:
6334 cp_parser_error (parser, "expected primary-expression");
6335 return error_mark_node;
6339 static inline cp_expr
6340 cp_parser_primary_expression (cp_parser *parser,
6341 bool address_p,
6342 bool cast_p,
6343 bool template_arg_p,
6344 cp_id_kind *idk)
6346 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6347 /*decltype*/false, idk);
6350 /* Complain about missing template keyword when naming a dependent
6351 member template. */
6353 static void
6354 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6356 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6357 return;
6359 gcc_rich_location richloc (loc);
6360 richloc.add_fixit_insert_before ("template");
6361 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6362 "expected %qs keyword before dependent "
6363 "template name", "template");
6364 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6367 /* Parse an id-expression.
6369 id-expression:
6370 unqualified-id
6371 qualified-id
6373 qualified-id:
6374 :: [opt] nested-name-specifier template [opt] unqualified-id
6375 :: identifier
6376 :: operator-function-id
6377 :: template-id
6379 Return a representation of the unqualified portion of the
6380 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6381 a `::' or nested-name-specifier.
6383 Often, if the id-expression was a qualified-id, the caller will
6384 want to make a SCOPE_REF to represent the qualified-id. This
6385 function does not do this in order to avoid wastefully creating
6386 SCOPE_REFs when they are not required.
6388 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6389 `template' keyword.
6391 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6392 uninstantiated templates.
6394 If *TEMPLATE_P is non-NULL, it is set to true iff the
6395 `template' keyword is used to explicitly indicate that the entity
6396 named is a template.
6398 If DECLARATOR_P is true, the id-expression is appearing as part of
6399 a declarator, rather than as part of an expression. */
6401 static cp_expr
6402 cp_parser_id_expression (cp_parser *parser,
6403 bool template_keyword_p,
6404 bool check_dependency_p,
6405 bool *template_p,
6406 bool declarator_p,
6407 bool optional_p)
6409 bool global_scope_p;
6410 bool nested_name_specifier_p;
6412 /* Assume the `template' keyword was not used. */
6413 if (template_p)
6414 *template_p = template_keyword_p;
6416 /* Look for the optional `::' operator. */
6417 global_scope_p
6418 = (!template_keyword_p
6419 && (cp_parser_global_scope_opt (parser,
6420 /*current_scope_valid_p=*/false)
6421 != NULL_TREE));
6423 /* Look for the optional nested-name-specifier. */
6424 nested_name_specifier_p
6425 = (cp_parser_nested_name_specifier_opt (parser,
6426 /*typename_keyword_p=*/false,
6427 check_dependency_p,
6428 /*type_p=*/false,
6429 declarator_p,
6430 template_keyword_p)
6431 != NULL_TREE);
6433 cp_expr id = NULL_TREE;
6434 tree scope = parser->scope;
6436 /* Peek at the next token. */
6437 cp_token *token = cp_lexer_peek_token (parser->lexer);
6439 /* If there is a nested-name-specifier, then we are looking at
6440 the first qualified-id production. */
6441 if (nested_name_specifier_p)
6443 tree saved_object_scope;
6444 tree saved_qualifying_scope;
6446 /* See if the next token is the `template' keyword. */
6447 if (!template_p)
6448 template_p = &template_keyword_p;
6449 *template_p = cp_parser_optional_template_keyword (parser);
6450 /* Name lookup we do during the processing of the
6451 unqualified-id might obliterate SCOPE. */
6452 saved_object_scope = parser->object_scope;
6453 saved_qualifying_scope = parser->qualifying_scope;
6454 /* Process the final unqualified-id. */
6455 id = cp_parser_unqualified_id (parser, *template_p,
6456 check_dependency_p,
6457 declarator_p,
6458 /*optional_p=*/false);
6459 /* Restore the SAVED_SCOPE for our caller. */
6460 parser->scope = scope;
6461 parser->object_scope = saved_object_scope;
6462 parser->qualifying_scope = saved_qualifying_scope;
6464 /* Otherwise, if we are in global scope, then we are looking at one
6465 of the other qualified-id productions. */
6466 else if (global_scope_p)
6468 /* If it's an identifier, and the next token is not a "<", then
6469 we can avoid the template-id case. This is an optimization
6470 for this common case. */
6471 if (token->type == CPP_NAME
6472 && !cp_parser_nth_token_starts_template_argument_list_p
6473 (parser, 2))
6474 return cp_parser_identifier (parser);
6476 cp_parser_parse_tentatively (parser);
6477 /* Try a template-id. */
6478 id = cp_parser_template_id_expr (parser,
6479 /*template_keyword_p=*/false,
6480 /*check_dependency_p=*/true,
6481 declarator_p);
6482 /* If that worked, we're done. */
6483 if (cp_parser_parse_definitely (parser))
6484 return id;
6486 /* Peek at the next token. (Changes in the token buffer may
6487 have invalidated the pointer obtained above.) */
6488 token = cp_lexer_peek_token (parser->lexer);
6490 switch (token->type)
6492 case CPP_NAME:
6493 id = cp_parser_identifier (parser);
6494 break;
6496 case CPP_KEYWORD:
6497 if (token->keyword == RID_OPERATOR)
6499 id = cp_parser_operator_function_id (parser);
6500 break;
6502 /* Fall through. */
6504 default:
6505 cp_parser_error (parser, "expected id-expression");
6506 return error_mark_node;
6509 else
6511 if (!scope)
6512 scope = parser->context->object_type;
6513 id = cp_parser_unqualified_id (parser, template_keyword_p,
6514 /*check_dependency_p=*/true,
6515 declarator_p,
6516 optional_p);
6519 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6520 && warn_missing_template_keyword
6521 && !template_keyword_p
6522 /* Don't warn if we're looking inside templates. */
6523 && check_dependency_p
6524 /* In a template argument list a > could be closing
6525 the enclosing targs. */
6526 && !parser->in_template_argument_list_p
6527 && scope && dependentish_scope_p (scope)
6528 /* Don't confuse an ill-formed constructor declarator for a missing
6529 template keyword in a return type. */
6530 && !(declarator_p && constructor_name_p (id, scope))
6531 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6532 && warning_enabled_at (token->location,
6533 OPT_Wmissing_template_keyword))
6535 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6536 if (cp_parser_skip_entire_template_parameter_list (parser)
6537 /* An operator after the > suggests that the > ends a
6538 template-id; a name or literal suggests that the > is an
6539 operator. */
6540 && (cp_lexer_peek_token (parser->lexer)->type
6541 <= CPP_LAST_PUNCTUATOR))
6542 missing_template_diag (token->location);
6545 return id;
6548 /* Parse an unqualified-id.
6550 unqualified-id:
6551 identifier
6552 operator-function-id
6553 conversion-function-id
6554 ~ class-name
6555 template-id
6557 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6558 keyword, in a construct like `A::template ...'.
6560 Returns a representation of unqualified-id. For the `identifier'
6561 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6562 production a BIT_NOT_EXPR is returned; the operand of the
6563 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6564 other productions, see the documentation accompanying the
6565 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6566 names are looked up in uninstantiated templates. If DECLARATOR_P
6567 is true, the unqualified-id is appearing as part of a declarator,
6568 rather than as part of an expression. */
6570 static cp_expr
6571 cp_parser_unqualified_id (cp_parser* parser,
6572 bool template_keyword_p,
6573 bool check_dependency_p,
6574 bool declarator_p,
6575 bool optional_p)
6577 cp_token *token;
6579 /* Peek at the next token. */
6580 token = cp_lexer_peek_token (parser->lexer);
6582 switch ((int) token->type)
6584 case CPP_NAME:
6586 tree id;
6588 /* We don't know yet whether or not this will be a
6589 template-id. */
6590 cp_parser_parse_tentatively (parser);
6591 /* Try a template-id. */
6592 id = cp_parser_template_id_expr (parser, template_keyword_p,
6593 check_dependency_p,
6594 declarator_p);
6595 /* If it worked, we're done. */
6596 if (cp_parser_parse_definitely (parser))
6597 return id;
6598 /* Otherwise, it's an ordinary identifier. */
6599 return cp_parser_identifier (parser);
6602 case CPP_TEMPLATE_ID:
6603 return cp_parser_template_id_expr (parser, template_keyword_p,
6604 check_dependency_p,
6605 declarator_p);
6607 case CPP_COMPL:
6609 tree type_decl;
6610 tree qualifying_scope;
6611 tree object_scope;
6612 tree scope;
6613 bool done;
6614 location_t tilde_loc = token->location;
6616 /* Consume the `~' token. */
6617 cp_lexer_consume_token (parser->lexer);
6618 /* Parse the class-name. The standard, as written, seems to
6619 say that:
6621 template <typename T> struct S { ~S (); };
6622 template <typename T> S<T>::~S() {}
6624 is invalid, since `~' must be followed by a class-name, but
6625 `S<T>' is dependent, and so not known to be a class.
6626 That's not right; we need to look in uninstantiated
6627 templates. A further complication arises from:
6629 template <typename T> void f(T t) {
6630 t.T::~T();
6633 Here, it is not possible to look up `T' in the scope of `T'
6634 itself. We must look in both the current scope, and the
6635 scope of the containing complete expression.
6637 Yet another issue is:
6639 struct S {
6640 int S;
6641 ~S();
6644 S::~S() {}
6646 The standard does not seem to say that the `S' in `~S'
6647 should refer to the type `S' and not the data member
6648 `S::S'. */
6650 /* DR 244 says that we look up the name after the "~" in the
6651 same scope as we looked up the qualifying name. That idea
6652 isn't fully worked out; it's more complicated than that. */
6653 scope = parser->scope;
6654 object_scope = parser->object_scope;
6655 qualifying_scope = parser->qualifying_scope;
6657 /* Check for invalid scopes. */
6658 if (scope == error_mark_node)
6660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6661 cp_lexer_consume_token (parser->lexer);
6662 return error_mark_node;
6664 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6666 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6667 error_at (token->location,
6668 "scope %qT before %<~%> is not a class-name",
6669 scope);
6670 cp_parser_simulate_error (parser);
6671 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6672 cp_lexer_consume_token (parser->lexer);
6673 return error_mark_node;
6675 if (template_keyword_p)
6677 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6678 error_at (tilde_loc, "%<template%> keyword not permitted in "
6679 "destructor name");
6680 cp_parser_simulate_error (parser);
6681 return error_mark_node;
6684 gcc_assert (!scope || TYPE_P (scope));
6686 token = cp_lexer_peek_token (parser->lexer);
6688 /* Create a location with caret == start at the tilde,
6689 finishing at the end of the peeked token, e.g:
6690 ~token
6691 ^~~~~~. */
6692 location_t loc
6693 = make_location (tilde_loc, tilde_loc, token->location);
6695 /* If the name is of the form "X::~X" it's OK even if X is a
6696 typedef. */
6698 if (scope
6699 && token->type == CPP_NAME
6700 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6701 != CPP_LESS)
6702 && (token->u.value == TYPE_IDENTIFIER (scope)
6703 || (CLASS_TYPE_P (scope)
6704 && constructor_name_p (token->u.value, scope))))
6706 cp_lexer_consume_token (parser->lexer);
6707 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6710 /* ~auto means the destructor of whatever the object is. */
6711 if (cp_parser_is_keyword (token, RID_AUTO))
6713 if (cxx_dialect < cxx14)
6714 pedwarn (loc, OPT_Wc__14_extensions,
6715 "%<~auto%> only available with "
6716 "%<-std=c++14%> or %<-std=gnu++14%>");
6717 cp_lexer_consume_token (parser->lexer);
6718 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6721 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6722 declarator-id of a constructor or destructor. */
6723 if (token->type == CPP_TEMPLATE_ID && declarator_p)
6725 auto_diagnostic_group d;
6726 bool w = false;
6727 if (cxx_dialect >= cxx20 && !cp_parser_simulate_error (parser))
6728 w = pedwarn (tilde_loc, OPT_Wtemplate_id_cdtor,
6729 "template-id not allowed for destructor in C++20");
6730 else if (cxx_dialect < cxx20
6731 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6732 w = warning_at (tilde_loc, OPT_Wtemplate_id_cdtor,
6733 "template-id not allowed for destructor in C++20");
6734 if (w)
6735 inform (tilde_loc, "remove the %qs", "< >");
6738 /* If there was an explicit qualification (S::~T), first look
6739 in the scope given by the qualification (i.e., S).
6741 Note: in the calls to cp_parser_class_name below we pass
6742 typename_type so that lookup finds the injected-class-name
6743 rather than the constructor. */
6744 done = false;
6745 type_decl = NULL_TREE;
6746 if (scope)
6748 cp_parser_parse_tentatively (parser);
6749 type_decl = cp_parser_class_name (parser,
6750 /*typename_keyword_p=*/false,
6751 /*template_keyword_p=*/false,
6752 typename_type,
6753 /*check_dependency=*/false,
6754 /*class_head_p=*/false,
6755 declarator_p);
6756 if (cp_parser_parse_definitely (parser))
6757 done = true;
6759 /* In "N::S::~S", look in "N" as well. */
6760 if (!done && scope && qualifying_scope)
6762 cp_parser_parse_tentatively (parser);
6763 parser->scope = qualifying_scope;
6764 parser->object_scope = NULL_TREE;
6765 parser->qualifying_scope = NULL_TREE;
6766 type_decl
6767 = cp_parser_class_name (parser,
6768 /*typename_keyword_p=*/false,
6769 /*template_keyword_p=*/false,
6770 typename_type,
6771 /*check_dependency=*/false,
6772 /*class_head_p=*/false,
6773 declarator_p);
6774 if (cp_parser_parse_definitely (parser))
6775 done = true;
6777 /* In "p->S::~T", look in the scope given by "*p" as well. */
6778 else if (!done && object_scope)
6780 cp_parser_parse_tentatively (parser);
6781 parser->scope = object_scope;
6782 parser->object_scope = NULL_TREE;
6783 parser->qualifying_scope = NULL_TREE;
6784 type_decl
6785 = cp_parser_class_name (parser,
6786 /*typename_keyword_p=*/false,
6787 /*template_keyword_p=*/false,
6788 typename_type,
6789 /*check_dependency=*/false,
6790 /*class_head_p=*/false,
6791 declarator_p);
6792 if (cp_parser_parse_definitely (parser))
6793 done = true;
6795 /* Look in the surrounding context. */
6796 if (!done)
6798 parser->scope = NULL_TREE;
6799 parser->object_scope = NULL_TREE;
6800 parser->qualifying_scope = NULL_TREE;
6801 if (processing_template_decl)
6802 cp_parser_parse_tentatively (parser);
6803 type_decl
6804 = cp_parser_class_name (parser,
6805 /*typename_keyword_p=*/false,
6806 /*template_keyword_p=*/false,
6807 typename_type,
6808 /*check_dependency=*/false,
6809 /*class_head_p=*/false,
6810 declarator_p);
6811 if (processing_template_decl
6812 && ! cp_parser_parse_definitely (parser))
6814 /* We couldn't find a type with this name. If we're parsing
6815 tentatively, fail and try something else. */
6816 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6818 cp_parser_simulate_error (parser);
6819 return error_mark_node;
6821 /* Otherwise, accept it and check for a match at instantiation
6822 time. */
6823 type_decl = cp_parser_identifier (parser);
6824 if (type_decl != error_mark_node)
6825 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6826 return type_decl;
6829 /* If an error occurred, assume that the name of the
6830 destructor is the same as the name of the qualifying
6831 class. That allows us to keep parsing after running
6832 into ill-formed destructor names. */
6833 if (type_decl == error_mark_node && scope)
6834 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6835 else if (type_decl == error_mark_node)
6836 return error_mark_node;
6838 /* Check that destructor name and scope match. */
6839 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6841 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6842 error_at (loc,
6843 "declaration of %<~%T%> as member of %qT",
6844 type_decl, scope);
6845 cp_parser_simulate_error (parser);
6846 return error_mark_node;
6849 /* [class.dtor]
6851 A typedef-name that names a class shall not be used as the
6852 identifier in the declarator for a destructor declaration. */
6853 if (declarator_p
6854 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6855 && !DECL_SELF_REFERENCE_P (type_decl)
6856 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6857 error_at (loc,
6858 "typedef-name %qD used as destructor declarator",
6859 type_decl);
6861 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6864 case CPP_KEYWORD:
6865 if (token->keyword == RID_OPERATOR)
6867 cp_expr id;
6869 /* This could be a template-id, so we try that first. */
6870 cp_parser_parse_tentatively (parser);
6871 /* Try a template-id. */
6872 id = cp_parser_template_id_expr (parser, template_keyword_p,
6873 /*check_dependency_p=*/true,
6874 declarator_p);
6875 /* If that worked, we're done. */
6876 if (cp_parser_parse_definitely (parser))
6877 return id;
6878 /* We still don't know whether we're looking at an
6879 operator-function-id or a conversion-function-id. */
6880 cp_parser_parse_tentatively (parser);
6881 /* Try an operator-function-id. */
6882 id = cp_parser_operator_function_id (parser);
6883 /* If that didn't work, try a conversion-function-id. */
6884 if (!cp_parser_parse_definitely (parser))
6885 id = cp_parser_conversion_function_id (parser);
6887 return id;
6889 /* Fall through. */
6891 default:
6892 if (optional_p)
6893 return NULL_TREE;
6894 cp_parser_error (parser, "expected unqualified-id");
6895 return error_mark_node;
6899 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6900 be a template-id or the name shall refer to a class template or an
6901 alias template. */
6903 static void
6904 check_template_keyword_in_nested_name_spec (tree name)
6906 if (CLASS_TYPE_P (name)
6907 && ((CLASSTYPE_USE_TEMPLATE (name)
6908 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6909 || CLASSTYPE_IS_TEMPLATE (name)))
6910 return;
6912 if (TREE_CODE (name) == TYPENAME_TYPE
6913 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6914 return;
6915 /* Alias templates are also OK. */
6916 else if (alias_template_specialization_p (name, nt_opaque))
6917 return;
6919 permerror (input_location, TYPE_P (name)
6920 ? G_("%qT is not a template")
6921 : G_("%qD is not a template"),
6922 name);
6925 /* Parse an (optional) nested-name-specifier.
6927 nested-name-specifier: [C++98]
6928 class-or-namespace-name :: nested-name-specifier [opt]
6929 class-or-namespace-name :: template nested-name-specifier [opt]
6931 nested-name-specifier: [C++0x]
6932 type-name ::
6933 namespace-name ::
6934 nested-name-specifier identifier ::
6935 nested-name-specifier template [opt] simple-template-id ::
6937 PARSER->SCOPE should be set appropriately before this function is
6938 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6939 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6940 in name lookups.
6942 Sets PARSER->SCOPE to the class (TYPE) or namespace
6943 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6944 it unchanged if there is no nested-name-specifier. Returns the new
6945 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6947 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6949 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6950 part of a declaration and/or decl-specifier. */
6952 static tree
6953 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6954 bool typename_keyword_p,
6955 bool check_dependency_p,
6956 bool type_p,
6957 bool is_declaration,
6958 bool template_keyword_p /* = false */)
6960 bool success = false;
6961 cp_token_position start = 0;
6962 cp_token *token;
6964 /* Remember where the nested-name-specifier starts. */
6965 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6966 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6968 start = cp_lexer_token_position (parser->lexer, false);
6969 push_deferring_access_checks (dk_deferred);
6972 while (true)
6974 tree new_scope;
6975 tree old_scope;
6976 tree saved_qualifying_scope;
6978 /* Spot cases that cannot be the beginning of a
6979 nested-name-specifier. */
6980 token = cp_lexer_peek_token (parser->lexer);
6982 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6983 the already parsed nested-name-specifier. */
6984 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6986 /* Grab the nested-name-specifier and continue the loop. */
6987 cp_parser_pre_parsed_nested_name_specifier (parser);
6988 /* If we originally encountered this nested-name-specifier
6989 with CHECK_DEPENDENCY_P set to true, we will not have
6990 resolved TYPENAME_TYPEs, so we must do so here. */
6991 if (is_declaration
6992 && !check_dependency_p
6993 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6995 new_scope = resolve_typename_type (parser->scope,
6996 /*only_current_p=*/false);
6997 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6998 parser->scope = new_scope;
7000 success = true;
7001 continue;
7004 /* Spot cases that cannot be the beginning of a
7005 nested-name-specifier. On the second and subsequent times
7006 through the loop, we look for the `template' keyword. */
7007 if (success && token->keyword == RID_TEMPLATE)
7009 /* A template-id can start a nested-name-specifier. */
7010 else if (token->type == CPP_TEMPLATE_ID)
7012 /* DR 743: decltype can be used in a nested-name-specifier. */
7013 else if (token_is_decltype (token))
7015 else
7017 /* If the next token is not an identifier, then it is
7018 definitely not a type-name or namespace-name. */
7019 if (token->type != CPP_NAME)
7020 break;
7021 /* If the following token is neither a `<' (to begin a
7022 template-id), nor a `::', then we are not looking at a
7023 nested-name-specifier. */
7024 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7026 if (token->type == CPP_COLON
7027 && parser->colon_corrects_to_scope_p
7028 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
7029 /* name:name is a valid sequence in an Objective C message. */
7030 && !parser->objective_c_message_context_p)
7032 gcc_rich_location richloc (token->location);
7033 richloc.add_fixit_replace ("::");
7034 error_at (&richloc,
7035 "found %<:%> in nested-name-specifier, "
7036 "expected %<::%>");
7037 token->type = CPP_SCOPE;
7040 if (token->type != CPP_SCOPE
7041 && !cp_parser_nth_token_starts_template_argument_list_p
7042 (parser, 2))
7043 break;
7046 /* The nested-name-specifier is optional, so we parse
7047 tentatively. */
7048 cp_parser_parse_tentatively (parser);
7050 /* Look for the optional `template' keyword, if this isn't the
7051 first time through the loop. */
7052 if (success)
7054 template_keyword_p = cp_parser_optional_template_keyword (parser);
7055 /* DR1710: "In a qualified-id used as the name in
7056 a typename-specifier, elaborated-type-specifier, using-declaration,
7057 or class-or-decltype, an optional keyword template appearing at
7058 the top level is ignored." */
7059 if (!template_keyword_p
7060 && typename_keyword_p
7061 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
7062 template_keyword_p = true;
7065 /* Save the old scope since the name lookup we are about to do
7066 might destroy it. */
7067 old_scope = parser->scope;
7068 saved_qualifying_scope = parser->qualifying_scope;
7069 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
7070 look up names in "X<T>::I" in order to determine that "Y" is
7071 a template. So, if we have a typename at this point, we make
7072 an effort to look through it. */
7073 if (is_declaration
7074 && !check_dependency_p
7075 && !typename_keyword_p
7076 && parser->scope
7077 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7078 parser->scope = resolve_typename_type (parser->scope,
7079 /*only_current_p=*/false);
7080 /* Parse the qualifying entity. */
7081 new_scope
7082 = cp_parser_qualifying_entity (parser,
7083 typename_keyword_p,
7084 template_keyword_p,
7085 check_dependency_p,
7086 type_p,
7087 is_declaration);
7088 /* Look for the `::' token. */
7089 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7091 /* If we found what we wanted, we keep going; otherwise, we're
7092 done. */
7093 if (!cp_parser_parse_definitely (parser))
7095 bool error_p = false;
7097 /* Restore the OLD_SCOPE since it was valid before the
7098 failed attempt at finding the last
7099 class-or-namespace-name. */
7100 parser->scope = old_scope;
7101 parser->qualifying_scope = saved_qualifying_scope;
7103 /* If the next token is a decltype, and the one after that is a
7104 `::', then the decltype has failed to resolve to a class or
7105 enumeration type. Give this error even when parsing
7106 tentatively since it can't possibly be valid--and we're going
7107 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7108 won't get another chance.*/
7109 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7110 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7111 == CPP_SCOPE))
7113 token = cp_lexer_consume_token (parser->lexer);
7114 tree dtype = token->u.tree_check_value->value;
7115 if (dtype != error_mark_node)
7116 error_at (token->location, "%<decltype%> evaluates to %qT, "
7117 "which is not a class or enumeration type",
7118 dtype);
7119 parser->scope = error_mark_node;
7120 error_p = true;
7121 /* As below. */
7122 success = true;
7123 cp_lexer_consume_token (parser->lexer);
7126 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7127 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7129 /* If we have a non-type template-id followed by ::, it can't
7130 possibly be valid. */
7131 token = cp_lexer_peek_token (parser->lexer);
7132 tree tid = token->u.tree_check_value->value;
7133 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7134 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7136 tree tmpl = NULL_TREE;
7137 if (is_overloaded_fn (tid))
7139 tree fns = get_fns (tid);
7140 if (OVL_SINGLE_P (fns))
7141 tmpl = OVL_FIRST (fns);
7142 if (function_concept_p (fns))
7143 error_at (token->location, "concept-id %qD "
7144 "in nested-name-specifier", tid);
7145 else
7146 error_at (token->location, "function template-id "
7147 "%qD in nested-name-specifier", tid);
7149 else
7151 tmpl = TREE_OPERAND (tid, 0);
7152 if (variable_concept_p (tmpl)
7153 || standard_concept_p (tmpl))
7154 error_at (token->location, "concept-id %qD "
7155 "in nested-name-specifier", tid);
7156 else
7158 /* Variable template. */
7159 gcc_assert (variable_template_p (tmpl));
7160 error_at (token->location, "variable template-id "
7161 "%qD in nested-name-specifier", tid);
7164 if (tmpl)
7165 inform (DECL_SOURCE_LOCATION (tmpl),
7166 "%qD declared here", tmpl);
7168 parser->scope = error_mark_node;
7169 error_p = true;
7170 /* As below. */
7171 success = true;
7172 cp_lexer_consume_token (parser->lexer);
7173 cp_lexer_consume_token (parser->lexer);
7177 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7178 break;
7179 /* If the next token is an identifier, and the one after
7180 that is a `::', then any valid interpretation would have
7181 found a class-or-namespace-name. */
7182 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7183 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7184 == CPP_SCOPE)
7185 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7186 != CPP_COMPL))
7188 token = cp_lexer_consume_token (parser->lexer);
7189 if (!error_p)
7191 if (!token->error_reported)
7193 tree decl;
7194 tree ambiguous_decls;
7196 decl = cp_parser_lookup_name (parser, token->u.value,
7197 none_type,
7198 /*is_template=*/false,
7199 /*is_namespace=*/false,
7200 /*check_dependency=*/true,
7201 &ambiguous_decls,
7202 token->location);
7203 if (TREE_CODE (decl) == TEMPLATE_DECL)
7204 error_at (token->location,
7205 "%qD used without template arguments",
7206 decl);
7207 else if (ambiguous_decls)
7209 // cp_parser_lookup_name has the same diagnostic,
7210 // thus make sure to emit it at most once.
7211 if (cp_parser_uncommitted_to_tentative_parse_p
7212 (parser))
7214 error_at (token->location,
7215 "reference to %qD is ambiguous",
7216 token->u.value);
7217 print_candidates (ambiguous_decls);
7219 decl = error_mark_node;
7221 else
7223 if (cxx_dialect != cxx98)
7224 cp_parser_name_lookup_error
7225 (parser, token->u.value, decl, NLE_NOT_CXX98,
7226 token->location);
7227 else
7228 cp_parser_name_lookup_error
7229 (parser, token->u.value, decl, NLE_CXX98,
7230 token->location);
7233 parser->scope = error_mark_node;
7234 error_p = true;
7235 /* Treat this as a successful nested-name-specifier
7236 due to:
7238 [basic.lookup.qual]
7240 If the name found is not a class-name (clause
7241 _class_) or namespace-name (_namespace.def_), the
7242 program is ill-formed. */
7243 success = true;
7245 cp_lexer_consume_token (parser->lexer);
7247 break;
7249 /* We've found one valid nested-name-specifier. */
7250 success = true;
7251 /* Name lookup always gives us a DECL. */
7252 if (TREE_CODE (new_scope) == TYPE_DECL)
7253 new_scope = TREE_TYPE (new_scope);
7254 /* Uses of "template" must be followed by actual templates. */
7255 if (template_keyword_p)
7256 check_template_keyword_in_nested_name_spec (new_scope);
7257 /* If it is a class scope, try to complete it; we are about to
7258 be looking up names inside the class. */
7259 if (TYPE_P (new_scope)
7260 /* Since checking types for dependency can be expensive,
7261 avoid doing it if the type is already complete. */
7262 && !COMPLETE_TYPE_P (new_scope)
7263 /* Do not try to complete dependent types. */
7264 && !dependent_type_p (new_scope))
7266 new_scope = complete_type (new_scope);
7267 /* If it is a typedef to current class, use the current
7268 class instead, as the typedef won't have any names inside
7269 it yet. */
7270 if (!COMPLETE_TYPE_P (new_scope)
7271 && currently_open_class (new_scope))
7272 new_scope = TYPE_MAIN_VARIANT (new_scope);
7274 /* Make sure we look in the right scope the next time through
7275 the loop. */
7276 parser->scope = new_scope;
7279 /* If parsing tentatively, replace the sequence of tokens that makes
7280 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7281 token. That way, should we re-parse the token stream, we will
7282 not have to repeat the effort required to do the parse, nor will
7283 we issue duplicate error messages. */
7284 if (success && start)
7286 cp_token *token;
7288 token = cp_lexer_token_at (parser->lexer, start);
7289 /* Reset the contents of the START token. */
7290 token->type = CPP_NESTED_NAME_SPECIFIER;
7291 /* Retrieve any deferred checks. Do not pop this access checks yet
7292 so the memory will not be reclaimed during token replacing below. */
7293 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7294 token->tree_check_p = true;
7295 token->u.tree_check_value->value = parser->scope;
7296 token->u.tree_check_value->checks = get_deferred_access_checks ();
7297 token->u.tree_check_value->qualifying_scope =
7298 parser->qualifying_scope;
7299 token->keyword = RID_MAX;
7301 /* Purge all subsequent tokens. */
7302 cp_lexer_purge_tokens_after (parser->lexer, start);
7305 if (start)
7306 pop_to_parent_deferring_access_checks ();
7308 return success ? parser->scope : NULL_TREE;
7311 /* Parse a nested-name-specifier. See
7312 cp_parser_nested_name_specifier_opt for details. This function
7313 behaves identically, except that it will an issue an error if no
7314 nested-name-specifier is present. */
7316 static tree
7317 cp_parser_nested_name_specifier (cp_parser *parser,
7318 bool typename_keyword_p,
7319 bool check_dependency_p,
7320 bool type_p,
7321 bool is_declaration)
7323 tree scope;
7325 /* Look for the nested-name-specifier. */
7326 scope = cp_parser_nested_name_specifier_opt (parser,
7327 typename_keyword_p,
7328 check_dependency_p,
7329 type_p,
7330 is_declaration);
7331 /* If it was not present, issue an error message. */
7332 if (!scope)
7334 cp_parser_error (parser, "expected nested-name-specifier");
7335 parser->scope = NULL_TREE;
7338 return scope;
7341 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7342 this is either a class-name or a namespace-name (which corresponds
7343 to the class-or-namespace-name production in the grammar). For
7344 C++0x, it can also be a type-name that refers to an enumeration
7345 type or a simple-template-id.
7347 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7348 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7349 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7350 TYPE_P is TRUE iff the next name should be taken as a class-name,
7351 even the same name is declared to be another entity in the same
7352 scope.
7354 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7355 specified by the class-or-namespace-name. If neither is found the
7356 ERROR_MARK_NODE is returned. */
7358 static tree
7359 cp_parser_qualifying_entity (cp_parser *parser,
7360 bool typename_keyword_p,
7361 bool template_keyword_p,
7362 bool check_dependency_p,
7363 bool type_p,
7364 bool is_declaration)
7366 tree saved_scope;
7367 tree saved_qualifying_scope;
7368 tree saved_object_scope;
7369 tree scope;
7370 bool only_class_p;
7371 bool successful_parse_p;
7373 /* DR 743: decltype can appear in a nested-name-specifier. */
7374 if (cp_lexer_next_token_is_decltype (parser->lexer))
7376 scope = cp_parser_decltype (parser);
7377 if (TREE_CODE (scope) != ENUMERAL_TYPE
7378 && !MAYBE_CLASS_TYPE_P (scope))
7380 cp_parser_simulate_error (parser);
7381 return error_mark_node;
7383 if (TYPE_NAME (scope))
7384 scope = TYPE_NAME (scope);
7385 return scope;
7388 /* Before we try to parse the class-name, we must save away the
7389 current PARSER->SCOPE since cp_parser_class_name will destroy
7390 it. */
7391 saved_scope = parser->scope;
7392 saved_qualifying_scope = parser->qualifying_scope;
7393 saved_object_scope = parser->object_scope;
7394 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7395 there is no need to look for a namespace-name. */
7396 only_class_p = template_keyword_p
7397 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7398 if (!only_class_p)
7399 cp_parser_parse_tentatively (parser);
7400 scope = cp_parser_class_name (parser,
7401 typename_keyword_p,
7402 template_keyword_p,
7403 type_p ? class_type : none_type,
7404 check_dependency_p,
7405 /*class_head_p=*/false,
7406 is_declaration,
7407 /*enum_ok=*/cxx_dialect > cxx98);
7408 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7409 /* If that didn't work, try for a namespace-name. */
7410 if (!only_class_p && !successful_parse_p)
7412 /* Restore the saved scope. */
7413 parser->scope = saved_scope;
7414 parser->qualifying_scope = saved_qualifying_scope;
7415 parser->object_scope = saved_object_scope;
7416 /* If we are not looking at an identifier followed by the scope
7417 resolution operator, then this is not part of a
7418 nested-name-specifier. (Note that this function is only used
7419 to parse the components of a nested-name-specifier.) */
7420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7421 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7422 return error_mark_node;
7423 scope = cp_parser_namespace_name (parser);
7426 return scope;
7429 /* Return true if we are looking at a compound-literal, false otherwise. */
7431 static bool
7432 cp_parser_compound_literal_p (cp_parser *parser)
7434 cp_lexer_save_tokens (parser->lexer);
7436 /* Skip tokens until the next token is a closing parenthesis.
7437 If we find the closing `)', and the next token is a `{', then
7438 we are looking at a compound-literal. */
7439 bool compound_literal_p
7440 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7441 /*consume_paren=*/true)
7442 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7444 /* Roll back the tokens we skipped. */
7445 cp_lexer_rollback_tokens (parser->lexer);
7447 return compound_literal_p;
7450 /* Return true if EXPR is the integer constant zero or a complex constant
7451 of zero, without any folding, but ignoring location wrappers. */
7453 bool
7454 literal_integer_zerop (const_tree expr)
7456 return (location_wrapper_p (expr)
7457 && integer_zerop (TREE_OPERAND (expr, 0)));
7460 /* Parse a postfix-expression.
7462 postfix-expression:
7463 primary-expression
7464 postfix-expression [ expression ]
7465 postfix-expression ( expression-list [opt] )
7466 simple-type-specifier ( expression-list [opt] )
7467 typename :: [opt] nested-name-specifier identifier
7468 ( expression-list [opt] )
7469 typename :: [opt] nested-name-specifier template [opt] template-id
7470 ( expression-list [opt] )
7471 postfix-expression . template [opt] id-expression
7472 postfix-expression -> template [opt] id-expression
7473 postfix-expression . pseudo-destructor-name
7474 postfix-expression -> pseudo-destructor-name
7475 postfix-expression ++
7476 postfix-expression --
7477 dynamic_cast < type-id > ( expression )
7478 static_cast < type-id > ( expression )
7479 reinterpret_cast < type-id > ( expression )
7480 const_cast < type-id > ( expression )
7481 typeid ( expression )
7482 typeid ( type-id )
7484 GNU Extension:
7486 postfix-expression:
7487 ( type-id ) { initializer-list , [opt] }
7489 This extension is a GNU version of the C99 compound-literal
7490 construct. (The C99 grammar uses `type-name' instead of `type-id',
7491 but they are essentially the same concept.)
7493 If ADDRESS_P is true, the postfix expression is the operand of the
7494 `&' operator. CAST_P is true if this expression is the target of a
7495 cast.
7497 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7498 class member access expressions [expr.ref].
7500 Returns a representation of the expression. */
7502 static cp_expr
7503 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7504 bool member_access_only_p, bool decltype_p,
7505 cp_id_kind * pidk_return)
7507 cp_token *token;
7508 location_t loc;
7509 enum rid keyword;
7510 cp_id_kind idk = CP_ID_KIND_NONE;
7511 cp_expr postfix_expression = NULL_TREE;
7512 bool is_member_access = false;
7514 /* Peek at the next token. */
7515 token = cp_lexer_peek_token (parser->lexer);
7516 loc = token->location;
7517 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7519 /* Some of the productions are determined by keywords. */
7520 keyword = token->keyword;
7521 switch (keyword)
7523 case RID_DYNCAST:
7524 case RID_STATCAST:
7525 case RID_REINTCAST:
7526 case RID_CONSTCAST:
7528 tree type;
7529 cp_expr expression;
7530 const char *saved_message;
7531 bool saved_in_type_id_in_expr_p;
7533 /* All of these can be handled in the same way from the point
7534 of view of parsing. Begin by consuming the token
7535 identifying the cast. */
7536 cp_lexer_consume_token (parser->lexer);
7538 /* New types cannot be defined in the cast. */
7539 saved_message = parser->type_definition_forbidden_message;
7540 parser->type_definition_forbidden_message
7541 = G_("types may not be defined in casts");
7543 /* Look for the opening `<'. */
7544 cp_parser_require (parser, CPP_LESS, RT_LESS);
7545 /* Parse the type to which we are casting. */
7546 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7547 parser->in_type_id_in_expr_p = true;
7548 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7549 NULL);
7550 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7551 /* Look for the closing `>'. */
7552 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7553 /* Restore the old message. */
7554 parser->type_definition_forbidden_message = saved_message;
7556 bool saved_greater_than_is_operator_p
7557 = parser->greater_than_is_operator_p;
7558 parser->greater_than_is_operator_p = true;
7560 /* And the expression which is being cast. */
7561 matching_parens parens;
7562 parens.require_open (parser);
7563 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7564 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7565 RT_CLOSE_PAREN);
7566 location_t end_loc = close_paren ?
7567 close_paren->location : UNKNOWN_LOCATION;
7569 parser->greater_than_is_operator_p
7570 = saved_greater_than_is_operator_p;
7572 /* Only type conversions to integral or enumeration types
7573 can be used in constant-expressions. */
7574 if (!cast_valid_in_integral_constant_expression_p (type)
7575 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7577 postfix_expression = error_mark_node;
7578 break;
7581 /* Construct a location e.g. :
7582 reinterpret_cast <int *> (expr)
7583 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7584 ranging from the start of the "*_cast" token to the final closing
7585 paren, with the caret at the start. */
7586 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7588 switch (keyword)
7590 case RID_DYNCAST:
7591 postfix_expression
7592 = build_dynamic_cast (cp_cast_loc, type, expression,
7593 tf_warning_or_error);
7594 break;
7595 case RID_STATCAST:
7596 postfix_expression
7597 = build_static_cast (cp_cast_loc, type, expression,
7598 tf_warning_or_error);
7599 break;
7600 case RID_REINTCAST:
7601 postfix_expression
7602 = build_reinterpret_cast (cp_cast_loc, type, expression,
7603 tf_warning_or_error);
7604 break;
7605 case RID_CONSTCAST:
7606 postfix_expression
7607 = build_const_cast (cp_cast_loc, type, expression,
7608 tf_warning_or_error);
7609 break;
7610 default:
7611 gcc_unreachable ();
7614 break;
7616 case RID_TYPEID:
7618 tree type;
7619 const char *saved_message;
7620 bool saved_in_type_id_in_expr_p;
7622 /* Consume the `typeid' token. */
7623 cp_lexer_consume_token (parser->lexer);
7624 /* Look for the `(' token. */
7625 matching_parens parens;
7626 parens.require_open (parser);
7627 /* Types cannot be defined in a `typeid' expression. */
7628 saved_message = parser->type_definition_forbidden_message;
7629 parser->type_definition_forbidden_message
7630 = G_("types may not be defined in a %<typeid%> expression");
7631 /* We can't be sure yet whether we're looking at a type-id or an
7632 expression. */
7633 cp_parser_parse_tentatively (parser);
7634 /* Try a type-id first. */
7635 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7636 parser->in_type_id_in_expr_p = true;
7637 type = cp_parser_type_id (parser);
7638 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7639 /* Look for the `)' token. Otherwise, we can't be sure that
7640 we're not looking at an expression: consider `typeid (int
7641 (3))', for example. */
7642 cp_token *close_paren = parens.require_close (parser);
7643 /* If all went well, simply lookup the type-id. */
7644 if (cp_parser_parse_definitely (parser))
7645 postfix_expression = get_typeid (type, tf_warning_or_error);
7646 /* Otherwise, fall back to the expression variant. */
7647 else
7649 tree expression;
7651 /* Look for an expression. */
7652 expression = cp_parser_expression (parser, & idk);
7653 /* Compute its typeid. */
7654 postfix_expression = build_typeid (expression, tf_warning_or_error);
7655 /* Look for the `)' token. */
7656 close_paren = parens.require_close (parser);
7658 /* Restore the saved message. */
7659 parser->type_definition_forbidden_message = saved_message;
7660 /* `typeid' may not appear in an integral constant expression. */
7661 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7662 postfix_expression = error_mark_node;
7664 /* Construct a location e.g. :
7665 typeid (expr)
7666 ^~~~~~~~~~~~~
7667 ranging from the start of the "typeid" token to the final closing
7668 paren, with the caret at the start. */
7669 if (close_paren)
7671 location_t typeid_loc
7672 = make_location (start_loc, start_loc, close_paren->location);
7673 postfix_expression.set_location (typeid_loc);
7674 postfix_expression.maybe_add_location_wrapper ();
7677 break;
7679 case RID_TYPENAME:
7681 tree type;
7682 /* The syntax permitted here is the same permitted for an
7683 elaborated-type-specifier. */
7684 ++parser->prevent_constrained_type_specifiers;
7685 type = cp_parser_elaborated_type_specifier (parser,
7686 /*is_friend=*/false,
7687 /*is_declaration=*/false);
7688 --parser->prevent_constrained_type_specifiers;
7689 postfix_expression = cp_parser_functional_cast (parser, type);
7691 break;
7693 case RID_ADDRESSOF:
7694 case RID_BUILTIN_SHUFFLE:
7695 case RID_BUILTIN_SHUFFLEVECTOR:
7696 case RID_BUILTIN_LAUNDER:
7697 case RID_BUILTIN_ASSOC_BARRIER:
7699 vec<tree, va_gc> *vec;
7701 cp_lexer_consume_token (parser->lexer);
7702 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7703 /*cast_p=*/false, /*allow_expansion_p=*/true,
7704 /*non_constant_p=*/NULL);
7705 if (vec == NULL)
7707 postfix_expression = error_mark_node;
7708 break;
7711 for (tree p : *vec)
7712 mark_exp_read (p);
7714 switch (keyword)
7716 case RID_ADDRESSOF:
7717 if (vec->length () == 1)
7718 postfix_expression
7719 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7720 else
7722 error_at (loc, "wrong number of arguments to "
7723 "%<__builtin_addressof%>");
7724 postfix_expression = error_mark_node;
7726 break;
7728 case RID_BUILTIN_LAUNDER:
7729 if (vec->length () == 1)
7730 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7731 tf_warning_or_error);
7732 else
7734 error_at (loc, "wrong number of arguments to "
7735 "%<__builtin_launder%>");
7736 postfix_expression = error_mark_node;
7738 break;
7740 case RID_BUILTIN_ASSOC_BARRIER:
7741 if (vec->length () == 1)
7742 postfix_expression = build1_loc (loc, PAREN_EXPR,
7743 TREE_TYPE ((*vec)[0]),
7744 (*vec)[0]);
7745 else
7747 error_at (loc, "wrong number of arguments to "
7748 "%<__builtin_assoc_barrier%>");
7749 postfix_expression = error_mark_node;
7751 break;
7753 case RID_BUILTIN_SHUFFLE:
7754 if (vec->length () == 2)
7755 postfix_expression
7756 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7757 (*vec)[1], tf_warning_or_error);
7758 else if (vec->length () == 3)
7759 postfix_expression
7760 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7761 (*vec)[2], tf_warning_or_error);
7762 else
7764 error_at (loc, "wrong number of arguments to "
7765 "%<__builtin_shuffle%>");
7766 postfix_expression = error_mark_node;
7768 break;
7770 case RID_BUILTIN_SHUFFLEVECTOR:
7771 if (vec->length () < 3)
7773 error_at (loc, "wrong number of arguments to "
7774 "%<__builtin_shufflevector%>");
7775 postfix_expression = error_mark_node;
7777 else
7779 postfix_expression
7780 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7782 break;
7784 default:
7785 gcc_unreachable ();
7787 break;
7790 case RID_BUILTIN_CONVERTVECTOR:
7792 tree expression;
7793 tree type;
7794 /* Consume the `__builtin_convertvector' token. */
7795 cp_lexer_consume_token (parser->lexer);
7796 /* Look for the opening `('. */
7797 matching_parens parens;
7798 parens.require_open (parser);
7799 /* Now, parse the assignment-expression. */
7800 expression = cp_parser_assignment_expression (parser);
7801 /* Look for the `,'. */
7802 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7803 location_t type_location
7804 = cp_lexer_peek_token (parser->lexer)->location;
7805 /* Parse the type-id. */
7807 type_id_in_expr_sentinel s (parser);
7808 type = cp_parser_type_id (parser);
7810 /* Look for the closing `)'. */
7811 parens.require_close (parser);
7812 postfix_expression
7813 = cp_build_vec_convert (expression, type_location, type,
7814 tf_warning_or_error);
7815 break;
7818 case RID_BUILTIN_BIT_CAST:
7820 tree expression;
7821 tree type;
7822 /* Consume the `__builtin_bit_cast' token. */
7823 cp_lexer_consume_token (parser->lexer);
7824 /* Look for the opening `('. */
7825 matching_parens parens;
7826 parens.require_open (parser);
7827 location_t type_location
7828 = cp_lexer_peek_token (parser->lexer)->location;
7829 /* Parse the type-id. */
7831 type_id_in_expr_sentinel s (parser);
7832 type = cp_parser_type_id (parser);
7834 /* Look for the `,'. */
7835 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7836 /* Now, parse the assignment-expression. */
7837 expression = cp_parser_assignment_expression (parser);
7838 /* Look for the closing `)'. */
7839 parens.require_close (parser);
7840 postfix_expression
7841 = cp_build_bit_cast (type_location, type, expression,
7842 tf_warning_or_error);
7843 break;
7846 default:
7848 tree type;
7850 /* If the next thing is a simple-type-specifier, we may be
7851 looking at a functional cast. We could also be looking at
7852 an id-expression. So, we try the functional cast, and if
7853 that doesn't work we fall back to the primary-expression. */
7854 cp_parser_parse_tentatively (parser);
7855 /* Look for the simple-type-specifier. */
7856 ++parser->prevent_constrained_type_specifiers;
7857 type = cp_parser_simple_type_specifier (parser,
7858 /*decl_specs=*/NULL,
7859 CP_PARSER_FLAGS_NONE);
7860 --parser->prevent_constrained_type_specifiers;
7861 /* Parse the cast itself. */
7862 if (!cp_parser_error_occurred (parser))
7863 postfix_expression
7864 = cp_parser_functional_cast (parser, type);
7865 /* If that worked, we're done. */
7866 if (cp_parser_parse_definitely (parser))
7867 break;
7869 /* If the functional-cast didn't work out, try a
7870 compound-literal. */
7871 if (cp_parser_allow_gnu_extensions_p (parser)
7872 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7874 cp_expr initializer = NULL_TREE;
7876 cp_parser_parse_tentatively (parser);
7878 matching_parens parens;
7879 parens.consume_open (parser);
7881 /* Avoid calling cp_parser_type_id pointlessly, see comment
7882 in cp_parser_cast_expression about c++/29234. */
7883 if (!cp_parser_compound_literal_p (parser))
7884 cp_parser_simulate_error (parser);
7885 else
7887 /* Parse the type. */
7888 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7889 parser->in_type_id_in_expr_p = true;
7890 type = cp_parser_type_id (parser);
7891 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7892 parens.require_close (parser);
7895 /* If things aren't going well, there's no need to
7896 keep going. */
7897 if (!cp_parser_error_occurred (parser))
7898 /* Parse the brace-enclosed initializer list. */
7899 initializer = cp_parser_braced_list (parser);
7900 /* If that worked, we're definitely looking at a
7901 compound-literal expression. */
7902 if (cp_parser_parse_definitely (parser))
7904 /* Warn the user that a compound literal is not
7905 allowed in standard C++. */
7906 pedwarn (input_location, OPT_Wpedantic,
7907 "ISO C++ forbids compound-literals");
7908 /* For simplicity, we disallow compound literals in
7909 constant-expressions. We could
7910 allow compound literals of integer type, whose
7911 initializer was a constant, in constant
7912 expressions. Permitting that usage, as a further
7913 extension, would not change the meaning of any
7914 currently accepted programs. (Of course, as
7915 compound literals are not part of ISO C++, the
7916 standard has nothing to say.) */
7917 if (cp_parser_non_integral_constant_expression (parser,
7918 NIC_NCC))
7920 postfix_expression = error_mark_node;
7921 break;
7923 /* Form the representation of the compound-literal. */
7924 postfix_expression
7925 = finish_compound_literal (type, initializer,
7926 tf_warning_or_error, fcl_c99);
7927 postfix_expression.set_location (initializer.get_location ());
7928 break;
7932 /* It must be a primary-expression. */
7933 postfix_expression
7934 = cp_parser_primary_expression (parser, address_p, cast_p,
7935 /*template_arg_p=*/false,
7936 decltype_p,
7937 &idk);
7939 break;
7942 /* Note that we don't need to worry about calling build_cplus_new on a
7943 class-valued CALL_EXPR in decltype when it isn't the end of the
7944 postfix-expression; unary_complex_lvalue will take care of that for
7945 all these cases. */
7947 /* Keep looping until the postfix-expression is complete. */
7948 while (true)
7950 if (idk == CP_ID_KIND_UNQUALIFIED
7951 && identifier_p (postfix_expression)
7952 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7953 /* It is not a Koenig lookup function call. */
7954 postfix_expression
7955 = unqualified_name_lookup_error (postfix_expression);
7957 /* Peek at the next token. */
7958 token = cp_lexer_peek_token (parser->lexer);
7960 switch (token->type)
7962 case CPP_OPEN_SQUARE:
7963 if (cp_next_tokens_can_be_std_attribute_p (parser))
7965 cp_parser_error (parser,
7966 "two consecutive %<[%> shall "
7967 "only introduce an attribute");
7968 return error_mark_node;
7970 postfix_expression
7971 = cp_parser_postfix_open_square_expression (parser,
7972 postfix_expression,
7973 false,
7974 decltype_p);
7975 postfix_expression.set_range (start_loc,
7976 postfix_expression.get_location ());
7978 idk = CP_ID_KIND_NONE;
7979 is_member_access = false;
7980 break;
7982 case CPP_OPEN_PAREN:
7983 /* postfix-expression ( expression-list [opt] ) */
7985 bool koenig_p;
7986 bool is_builtin_constant_p;
7987 bool saved_integral_constant_expression_p = false;
7988 bool saved_non_integral_constant_expression_p = false;
7989 tsubst_flags_t complain = complain_flags (decltype_p);
7990 vec<tree, va_gc> *args;
7991 location_t close_paren_loc = UNKNOWN_LOCATION;
7992 location_t combined_loc = UNKNOWN_LOCATION;
7994 is_member_access = false;
7996 tree stripped_expression
7997 = tree_strip_any_location_wrapper (postfix_expression);
7998 is_builtin_constant_p
7999 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
8000 if (is_builtin_constant_p)
8002 /* The whole point of __builtin_constant_p is to allow
8003 non-constant expressions to appear as arguments. */
8004 saved_integral_constant_expression_p
8005 = parser->integral_constant_expression_p;
8006 saved_non_integral_constant_expression_p
8007 = parser->non_integral_constant_expression_p;
8008 parser->integral_constant_expression_p = false;
8010 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
8011 && fndecl_built_in_p (stripped_expression,
8012 BUILT_IN_CLASSIFY_TYPE))
8014 /* __builtin_classify_type (type) */
8015 auto cl1 = make_temp_override
8016 (parser->type_definition_forbidden_message,
8017 G_("types may not be defined in "
8018 "%<__builtin_classify_type%> calls"));
8019 auto cl2 = make_temp_override
8020 (parser->type_definition_forbidden_message_arg,
8021 NULL);
8022 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
8023 true);
8024 cp_unevaluated uev;
8025 cp_parser_parse_tentatively (parser);
8026 matching_parens parens;
8027 parens.consume_open (parser);
8028 tree type = cp_parser_type_id (parser);
8029 parens.require_close (parser);
8030 if (cp_parser_parse_definitely (parser))
8032 if (dependent_type_p (type))
8034 postfix_expression = build_vl_exp (CALL_EXPR, 4);
8035 CALL_EXPR_FN (postfix_expression)
8036 = stripped_expression;
8037 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
8038 CALL_EXPR_ARG (postfix_expression, 0)
8039 = build_min (SIZEOF_EXPR, size_type_node, type);
8040 TREE_TYPE (postfix_expression) = integer_type_node;
8042 else
8044 postfix_expression
8045 = build_int_cst (integer_type_node,
8046 type_to_class (type));
8048 break;
8051 args = (cp_parser_parenthesized_expression_list
8052 (parser, non_attr,
8053 /*cast_p=*/false, /*allow_expansion_p=*/true,
8054 /*non_constant_p=*/NULL,
8055 /*close_paren_loc=*/&close_paren_loc,
8056 /*wrap_locations_p=*/true));
8057 if (is_builtin_constant_p)
8059 parser->integral_constant_expression_p
8060 = saved_integral_constant_expression_p;
8061 parser->non_integral_constant_expression_p
8062 = saved_non_integral_constant_expression_p;
8065 if (args == NULL)
8067 postfix_expression = error_mark_node;
8068 break;
8071 /* Function calls are not permitted in
8072 constant-expressions. */
8073 if (! builtin_valid_in_constant_expr_p (postfix_expression)
8074 && cp_parser_non_integral_constant_expression (parser,
8075 NIC_FUNC_CALL))
8077 postfix_expression = error_mark_node;
8078 release_tree_vector (args);
8079 break;
8082 koenig_p = false;
8083 if (idk == CP_ID_KIND_UNQUALIFIED
8084 || idk == CP_ID_KIND_TEMPLATE_ID)
8086 if (identifier_p (postfix_expression)
8087 /* In C++20, we may need to perform ADL for a template
8088 name. */
8089 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
8090 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
8092 if (!args->is_empty ())
8094 koenig_p = true;
8095 if (!any_type_dependent_arguments_p (args))
8096 postfix_expression
8097 = perform_koenig_lookup (postfix_expression, args,
8098 complain);
8100 else
8101 postfix_expression
8102 = unqualified_fn_lookup_error (postfix_expression);
8104 /* We do not perform argument-dependent lookup if
8105 normal lookup finds a non-function, in accordance
8106 with the expected resolution of DR 218. */
8107 else if (!args->is_empty ()
8108 && is_overloaded_fn (postfix_expression))
8110 /* Do not do argument dependent lookup if regular
8111 lookup finds a member function or a block-scope
8112 function declaration. [basic.lookup.argdep]/3 */
8113 bool do_adl_p = true;
8114 tree fns = get_fns (postfix_expression);
8115 for (lkp_iterator iter (fns); iter; ++iter)
8117 tree fn = STRIP_TEMPLATE (*iter);
8118 if ((TREE_CODE (fn) == USING_DECL
8119 && DECL_DEPENDENT_P (fn))
8120 || DECL_FUNCTION_MEMBER_P (fn)
8121 || DECL_LOCAL_DECL_P (fn))
8123 do_adl_p = false;
8124 break;
8128 if (do_adl_p)
8130 koenig_p = true;
8131 if (!any_type_dependent_arguments_p (args))
8132 postfix_expression
8133 = perform_koenig_lookup (postfix_expression, args,
8134 complain);
8139 /* Temporarily set input_location to the combined location
8140 with call expression range, as e.g. build_out_target_exprs
8141 called from convert_default_arg relies on input_location,
8142 so updating it only when the call is fully built results
8143 in inconsistencies between location handling in templates
8144 and outside of templates. */
8145 if (close_paren_loc != UNKNOWN_LOCATION)
8146 combined_loc = make_location (token->location, start_loc,
8147 close_paren_loc);
8148 iloc_sentinel ils (combined_loc);
8150 if (TREE_CODE (postfix_expression) == OFFSET_REF
8151 || TREE_CODE (postfix_expression) == MEMBER_REF
8152 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8153 postfix_expression = (build_offset_ref_call_from_tree
8154 (postfix_expression, &args,
8155 complain));
8156 else
8157 /* All other function calls. */
8159 if (DECL_P (postfix_expression)
8160 && parser->omp_for_parse_state
8161 && parser->omp_for_parse_state->in_intervening_code
8162 && omp_runtime_api_call (postfix_expression))
8164 error_at (loc, "calls to the OpenMP runtime API are "
8165 "not permitted in intervening code");
8166 parser->omp_for_parse_state->fail = true;
8168 bool disallow_virtual = (idk == CP_ID_KIND_QUALIFIED);
8169 postfix_expression
8170 = finish_call_expr (postfix_expression, &args,
8171 disallow_virtual,
8172 koenig_p,
8173 complain);
8176 if (close_paren_loc != UNKNOWN_LOCATION)
8177 postfix_expression.set_location (combined_loc);
8179 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8180 idk = CP_ID_KIND_NONE;
8182 release_tree_vector (args);
8184 break;
8186 case CPP_DOT:
8187 case CPP_DEREF:
8188 /* postfix-expression . template [opt] id-expression
8189 postfix-expression . pseudo-destructor-name
8190 postfix-expression -> template [opt] id-expression
8191 postfix-expression -> pseudo-destructor-name */
8193 /* Consume the `.' or `->' operator. */
8194 cp_lexer_consume_token (parser->lexer);
8196 postfix_expression
8197 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8198 postfix_expression,
8199 false, &idk, loc);
8201 is_member_access = true;
8202 break;
8204 case CPP_PLUS_PLUS:
8205 /* postfix-expression ++ */
8206 /* Consume the `++' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Generate a representation for the complete expression. */
8209 postfix_expression
8210 = finish_increment_expr (postfix_expression,
8211 POSTINCREMENT_EXPR);
8212 /* Increments may not appear in constant-expressions. */
8213 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8214 postfix_expression = error_mark_node;
8215 idk = CP_ID_KIND_NONE;
8216 is_member_access = false;
8217 break;
8219 case CPP_MINUS_MINUS:
8220 /* postfix-expression -- */
8221 /* Consume the `--' token. */
8222 cp_lexer_consume_token (parser->lexer);
8223 /* Generate a representation for the complete expression. */
8224 postfix_expression
8225 = finish_increment_expr (postfix_expression,
8226 POSTDECREMENT_EXPR);
8227 /* Decrements may not appear in constant-expressions. */
8228 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8229 postfix_expression = error_mark_node;
8230 idk = CP_ID_KIND_NONE;
8231 is_member_access = false;
8232 break;
8234 default:
8235 if (pidk_return != NULL)
8236 * pidk_return = idk;
8237 if (member_access_only_p)
8238 return is_member_access
8239 ? postfix_expression
8240 : cp_expr (error_mark_node);
8241 else
8242 return postfix_expression;
8247 /* Helper function for cp_parser_parenthesized_expression_list and
8248 cp_parser_postfix_open_square_expression. Parse a single element
8249 of parenthesized expression list. */
8251 static cp_expr
8252 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8253 bool allow_expansion_p,
8254 bool *non_constant_p)
8256 cp_expr expr (NULL_TREE);
8257 bool expr_non_constant_p;
8259 /* Parse the next assignment-expression. */
8260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8262 /* A braced-init-list. */
8263 cp_lexer_set_source_position (parser->lexer);
8264 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8265 expr = cp_parser_braced_list (parser,
8266 (non_constant_p != nullptr
8267 ? &expr_non_constant_p
8268 : nullptr));
8269 if (non_constant_p && expr_non_constant_p)
8270 *non_constant_p = true;
8272 else if (non_constant_p)
8274 expr = cp_parser_constant_expression (parser,
8275 /*allow_non_constant_p=*/true,
8276 &expr_non_constant_p);
8277 if (expr_non_constant_p)
8278 *non_constant_p = true;
8280 else
8281 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8283 /* If we have an ellipsis, then this is an expression expansion. */
8284 if (allow_expansion_p
8285 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8287 /* Consume the `...'. */
8288 cp_lexer_consume_token (parser->lexer);
8290 /* Build the argument pack. */
8291 expr = make_pack_expansion (expr);
8293 return expr;
8296 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8297 by cp_parser_builtin_offsetof. We're looking for
8299 postfix-expression [ expression ]
8300 postfix-expression [ braced-init-list ] (C++11)
8301 postfix-expression [ expression-list[opt] ] (C++23)
8303 FOR_OFFSETOF is set if we're being called in that context, which
8304 changes how we deal with integer constant expressions. */
8306 static tree
8307 cp_parser_postfix_open_square_expression (cp_parser *parser,
8308 tree postfix_expression,
8309 bool for_offsetof,
8310 bool decltype_p)
8312 tree index = NULL_TREE;
8313 releasing_vec expression_list = NULL;
8314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8315 bool saved_greater_than_is_operator_p;
8316 bool saved_colon_corrects_to_scope_p;
8318 /* Consume the `[' token. */
8319 cp_lexer_consume_token (parser->lexer);
8321 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8322 parser->greater_than_is_operator_p = true;
8324 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8325 if (parser->omp_array_section_p)
8326 parser->colon_corrects_to_scope_p = false;
8328 /* Parse the index expression. */
8329 /* ??? For offsetof, there is a question of what to allow here. If
8330 offsetof is not being used in an integral constant expression context,
8331 then we *could* get the right answer by computing the value at runtime.
8332 If we are in an integral constant expression context, then we might
8333 could accept any constant expression; hard to say without analysis.
8334 Rather than open the barn door too wide right away, allow only integer
8335 constant expressions here. */
8336 if (for_offsetof)
8337 index = cp_parser_constant_expression (parser);
8338 else if (!parser->omp_array_section_p
8339 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8341 if (cxx_dialect >= cxx23
8342 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8343 *&expression_list = make_tree_vector ();
8344 else if (cxx_dialect >= cxx23)
8346 while (true)
8348 cp_expr expr
8349 = cp_parser_parenthesized_expression_list_elt (parser,
8350 /*cast_p=*/
8351 false,
8352 /*allow_exp_p=*/
8353 true,
8354 /*non_cst_p=*/
8355 NULL);
8357 if (expr == error_mark_node)
8358 index = error_mark_node;
8359 else if (expression_list.get () == NULL
8360 && !PACK_EXPANSION_P (expr.get_value ()))
8361 index = expr.get_value ();
8362 else
8363 vec_safe_push (expression_list, expr.get_value ());
8365 /* If the next token isn't a `,', then we are done. */
8366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8367 break;
8369 if (expression_list.get () == NULL && index != error_mark_node)
8371 *&expression_list = make_tree_vector_single (index);
8372 index = NULL_TREE;
8375 /* Otherwise, consume the `,' and keep going. */
8376 cp_lexer_consume_token (parser->lexer);
8378 if (expression_list.get () && index == error_mark_node)
8379 expression_list.release ();
8381 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8383 cp_lexer_set_source_position (parser->lexer);
8384 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8385 index = cp_parser_braced_list (parser);
8387 else
8388 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8389 /*decltype_p=*/false,
8390 /*warn_comma_p=*/warn_comma_subscript);
8393 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8395 if (cxx_dialect >= cxx23
8396 && parser->omp_array_section_p
8397 && expression_list.get () != NULL
8398 && vec_safe_length (expression_list) > 1)
8400 error_at (loc, "cannot use multidimensional subscript in OpenMP array "
8401 "section");
8402 index = error_mark_node;
8404 if (parser->omp_array_section_p
8405 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8407 cp_lexer_consume_token (parser->lexer);
8408 tree length = NULL_TREE;
8409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8411 if (cxx_dialect >= cxx23)
8413 cp_expr expr
8414 = cp_parser_parenthesized_expression_list_elt (parser,
8415 /*cast_p=*/
8416 false,
8417 /*allow_exp_p=*/
8418 true,
8419 /*non_cst_p=*/
8420 NULL);
8422 if (expr == error_mark_node)
8423 length = error_mark_node;
8424 else
8425 length = expr.get_value ();
8427 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8429 error_at (loc, "cannot use multidimensional subscript in "
8430 "OpenMP array section");
8431 length = error_mark_node;
8434 else
8435 length
8436 = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8437 /*decltype_p=*/false,
8438 /*warn_comma_p=*/warn_comma_subscript);
8441 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8443 if (index == error_mark_node || length == error_mark_node)
8445 cp_parser_skip_to_closing_square_bracket (parser);
8446 return error_mark_node;
8448 else
8449 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8451 return grok_omp_array_section (input_location, postfix_expression, index,
8452 length);
8455 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8457 /* Look for the closing `]'. */
8458 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8460 /* Build the ARRAY_REF. */
8461 postfix_expression = grok_array_decl (loc, postfix_expression,
8462 index, &expression_list,
8463 tf_warning_or_error
8464 | (decltype_p ? tf_decltype : 0));
8466 /* When not doing offsetof, array references are not permitted in
8467 constant-expressions. */
8468 if (!for_offsetof
8469 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8470 postfix_expression = error_mark_node;
8472 return postfix_expression;
8475 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8476 dereference of incomplete type, returns true if error_mark_node should
8477 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8478 and *DEPENDENT_P. */
8480 bool
8481 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8482 bool *dependent_p)
8484 /* In a template, be permissive by treating an object expression
8485 of incomplete type as dependent (after a pedwarn). */
8486 diagnostic_t kind = (processing_template_decl
8487 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8489 switch (TREE_CODE (*postfix_expression))
8491 case CAST_EXPR:
8492 case REINTERPRET_CAST_EXPR:
8493 case CONST_CAST_EXPR:
8494 case STATIC_CAST_EXPR:
8495 case DYNAMIC_CAST_EXPR:
8496 case IMPLICIT_CONV_EXPR:
8497 case VIEW_CONVERT_EXPR:
8498 case NON_LVALUE_EXPR:
8499 kind = DK_ERROR;
8500 break;
8501 case OVERLOAD:
8502 /* Don't emit any diagnostic for OVERLOADs. */
8503 kind = DK_IGNORED;
8504 break;
8505 default:
8506 /* Avoid clobbering e.g. DECLs. */
8507 if (!EXPR_P (*postfix_expression))
8508 kind = DK_ERROR;
8509 break;
8512 if (kind == DK_IGNORED)
8513 return false;
8515 location_t exploc = location_of (*postfix_expression);
8516 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8517 if (!MAYBE_CLASS_TYPE_P (*scope))
8518 return true;
8519 if (kind == DK_ERROR)
8520 *scope = *postfix_expression = error_mark_node;
8521 else if (processing_template_decl)
8523 *dependent_p = true;
8524 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8526 return false;
8529 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8530 by cp_parser_builtin_offsetof. We're looking for
8532 postfix-expression . template [opt] id-expression
8533 postfix-expression . pseudo-destructor-name
8534 postfix-expression -> template [opt] id-expression
8535 postfix-expression -> pseudo-destructor-name
8537 FOR_OFFSETOF is set if we're being called in that context. That sorta
8538 limits what of the above we'll actually accept, but nevermind.
8539 TOKEN_TYPE is the "." or "->" token, which will already have been
8540 removed from the stream. */
8542 static tree
8543 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8544 enum cpp_ttype token_type,
8545 cp_expr postfix_expression,
8546 bool for_offsetof, cp_id_kind *idk,
8547 location_t location)
8549 tree name;
8550 bool dependent_p;
8551 bool pseudo_destructor_p;
8552 tree scope = NULL_TREE;
8553 location_t start_loc = postfix_expression.get_start ();
8555 /* If this is a `->' operator, dereference the pointer. */
8556 if (token_type == CPP_DEREF)
8557 postfix_expression = build_x_arrow (location, postfix_expression,
8558 tf_warning_or_error);
8559 /* Check to see whether or not the expression is type-dependent and
8560 not the current instantiation. */
8561 dependent_p = type_dependent_object_expression_p (postfix_expression);
8562 /* The identifier following the `->' or `.' is not qualified. */
8563 parser->scope = NULL_TREE;
8564 parser->qualifying_scope = NULL_TREE;
8565 parser->object_scope = NULL_TREE;
8566 *idk = CP_ID_KIND_NONE;
8568 /* Enter the scope corresponding to the type of the object
8569 given by the POSTFIX_EXPRESSION. */
8570 if (!dependent_p)
8572 scope = TREE_TYPE (postfix_expression);
8573 /* According to the standard, no expression should ever have
8574 reference type. Unfortunately, we do not currently match
8575 the standard in this respect in that our internal representation
8576 of an expression may have reference type even when the standard
8577 says it does not. Therefore, we have to manually obtain the
8578 underlying type here. */
8579 scope = non_reference (scope);
8580 /* The type of the POSTFIX_EXPRESSION must be complete. */
8581 /* Unlike the object expression in other contexts, *this is not
8582 required to be of complete type for purposes of class member
8583 access (5.2.5) outside the member function body. */
8584 if (postfix_expression != current_class_ref
8585 && scope != error_mark_node
8586 && !currently_open_class (scope))
8588 scope = complete_type (scope);
8589 if (!COMPLETE_TYPE_P (scope)
8590 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8591 &dependent_p))
8592 return error_mark_node;
8595 if (!dependent_p)
8597 /* Let the name lookup machinery know that we are processing a
8598 class member access expression. */
8599 parser->context->object_type = scope;
8600 /* If something went wrong, we want to be able to discern that case,
8601 as opposed to the case where there was no SCOPE due to the type
8602 of expression being dependent. */
8603 if (!scope)
8604 scope = error_mark_node;
8605 /* If the SCOPE was erroneous, make the various semantic analysis
8606 functions exit quickly -- and without issuing additional error
8607 messages. */
8608 if (scope == error_mark_node)
8609 postfix_expression = error_mark_node;
8613 if (dependent_p)
8615 tree type = TREE_TYPE (postfix_expression);
8616 /* If we don't have a (type-dependent) object of class type, use
8617 typeof to figure out the type of the object. */
8618 if (type == NULL_TREE || is_auto (type))
8619 type = finish_typeof (postfix_expression);
8620 parser->context->object_type = type;
8623 /* Assume this expression is not a pseudo-destructor access. */
8624 pseudo_destructor_p = false;
8626 /* If the SCOPE is a scalar type, then, if this is a valid program,
8627 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8628 is type dependent, it can be pseudo-destructor-name or something else.
8629 Try to parse it as pseudo-destructor-name first. */
8630 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8632 tree s;
8633 tree type;
8635 cp_parser_parse_tentatively (parser);
8636 /* Parse the pseudo-destructor-name. */
8637 s = NULL_TREE;
8638 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8639 &s, &type);
8640 if (dependent_p
8641 && (cp_parser_error_occurred (parser)
8642 || !SCALAR_TYPE_P (type)))
8643 cp_parser_abort_tentative_parse (parser);
8644 else if (cp_parser_parse_definitely (parser))
8646 pseudo_destructor_p = true;
8647 postfix_expression
8648 = finish_pseudo_destructor_expr (postfix_expression,
8649 s, type, location);
8653 if (!pseudo_destructor_p)
8655 /* If the SCOPE is not a scalar type, we are looking at an
8656 ordinary class member access expression, rather than a
8657 pseudo-destructor-name. */
8658 bool template_p;
8659 cp_token *token = cp_lexer_peek_token (parser->lexer);
8660 /* Parse the id-expression. */
8661 name = (cp_parser_id_expression
8662 (parser,
8663 cp_parser_optional_template_keyword (parser),
8664 /*check_dependency_p=*/true,
8665 &template_p,
8666 /*declarator_p=*/false,
8667 /*optional_p=*/false));
8668 /* In general, build a SCOPE_REF if the member name is qualified.
8669 However, if the name was not dependent and has already been
8670 resolved; there is no need to build the SCOPE_REF. For example;
8672 struct X { void f(); };
8673 template <typename T> void f(T* t) { t->X::f(); }
8675 Even though "t" is dependent, "X::f" is not and has been resolved
8676 to a BASELINK; there is no need to include scope information. */
8678 /* But we do need to remember that there was an explicit scope for
8679 virtual function calls. */
8680 if (parser->scope)
8681 *idk = CP_ID_KIND_QUALIFIED;
8683 /* If the name is a template-id that names a type, we will get a
8684 TYPE_DECL here. That is invalid code. */
8685 if (TREE_CODE (name) == TYPE_DECL)
8687 error_at (token->location, "invalid use of %qD", name);
8688 postfix_expression = error_mark_node;
8690 else
8692 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8694 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8696 error_at (token->location, "%<%D::%D%> is not a class member",
8697 parser->scope, name);
8698 postfix_expression = error_mark_node;
8700 else
8701 name = build_qualified_name (/*type=*/NULL_TREE,
8702 parser->scope,
8703 name,
8704 template_p);
8705 parser->scope = NULL_TREE;
8706 parser->qualifying_scope = NULL_TREE;
8707 parser->object_scope = NULL_TREE;
8709 if (parser->scope && name && BASELINK_P (name))
8710 adjust_result_of_qualified_name_lookup
8711 (name, parser->scope, scope);
8712 postfix_expression
8713 = finish_class_member_access_expr (postfix_expression, name,
8714 template_p,
8715 tf_warning_or_error);
8716 /* Build a location e.g.:
8717 ptr->access_expr
8718 ~~~^~~~~~~~~~~~~
8719 where the caret is at the deref token, ranging from
8720 the start of postfix_expression to the end of the access expr. */
8721 location_t combined_loc
8722 = make_location (input_location, start_loc, parser->lexer);
8723 protected_set_expr_location (postfix_expression, combined_loc);
8727 /* We no longer need to look up names in the scope of the object on
8728 the left-hand side of the `.' or `->' operator. */
8729 parser->context->object_type = NULL_TREE;
8731 /* Outside of offsetof, these operators may not appear in
8732 constant-expressions. */
8733 if (!for_offsetof
8734 && (cp_parser_non_integral_constant_expression
8735 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8736 postfix_expression = error_mark_node;
8738 return postfix_expression;
8741 /* Parse a parenthesized expression-list.
8743 expression-list:
8744 assignment-expression
8745 expression-list, assignment-expression
8747 attribute-list:
8748 expression-list
8749 identifier
8750 identifier, expression-list
8752 CAST_P is true if this expression is the target of a cast.
8754 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8755 argument pack.
8757 WRAP_LOCATIONS_P is true if expressions within this list for which
8758 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8759 their source locations.
8761 Returns a vector of trees. Each element is a representation of an
8762 assignment-expression. NULL is returned if the ( and or ) are
8763 missing. An empty, but allocated, vector is returned on no
8764 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8765 if we are parsing an attribute list for an attribute that wants a
8766 plain identifier argument, normal_attr for an attribute that wants
8767 an expression, or non_attr if we aren't parsing an attribute list. If
8768 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8769 not all of the expressions in the list were constant.
8770 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8771 will be written to with the location of the closing parenthesis. If
8772 an error occurs, it may or may not be written to. */
8774 static vec<tree, va_gc> *
8775 cp_parser_parenthesized_expression_list (cp_parser* parser,
8776 int is_attribute_list,
8777 bool cast_p,
8778 bool allow_expansion_p,
8779 bool *non_constant_p,
8780 location_t *close_paren_loc,
8781 bool wrap_locations_p)
8783 vec<tree, va_gc> *expression_list;
8784 bool saved_greater_than_is_operator_p;
8785 bool saved_omp_array_section_p;
8787 /* Assume all the expressions will be constant. */
8788 if (non_constant_p)
8789 *non_constant_p = false;
8791 matching_parens parens;
8792 if (!parens.require_open (parser))
8793 return NULL;
8795 expression_list = make_tree_vector ();
8797 /* Within a parenthesized expression, a `>' token is always
8798 the greater-than operator. */
8799 saved_greater_than_is_operator_p
8800 = parser->greater_than_is_operator_p;
8801 parser->greater_than_is_operator_p = true;
8803 saved_omp_array_section_p = parser->omp_array_section_p;
8804 parser->omp_array_section_p = false;
8806 cp_expr expr (NULL_TREE);
8808 /* Consume expressions until there are no more. */
8809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8810 while (true)
8812 /* At the beginning of attribute lists, check to see if the
8813 next token is an identifier. */
8814 if (is_attribute_list == id_attr
8815 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8816 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8817 else if (is_attribute_list == assume_attr)
8818 expr = cp_parser_conditional_expression (parser);
8819 else if (is_attribute_list == uneval_string_attr)
8820 expr = cp_parser_unevaluated_string_literal (parser);
8821 else
8822 expr
8823 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8824 allow_expansion_p,
8825 non_constant_p);
8827 if (wrap_locations_p)
8828 expr.maybe_add_location_wrapper ();
8830 /* Add it to the list. We add error_mark_node
8831 expressions to the list, so that we can still tell if
8832 the correct form for a parenthesized expression-list
8833 is found. That gives better errors. */
8834 vec_safe_push (expression_list, expr.get_value ());
8836 if (expr == error_mark_node)
8837 goto skip_comma;
8839 /* After the first item, attribute lists look the same as
8840 expression lists. */
8841 is_attribute_list = non_attr;
8843 get_comma:;
8844 /* If the next token isn't a `,', then we are done. */
8845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8846 break;
8848 /* Otherwise, consume the `,' and keep going. */
8849 cp_lexer_consume_token (parser->lexer);
8852 if (close_paren_loc)
8853 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8855 if (!parens.require_close (parser))
8857 int ending;
8859 skip_comma:;
8860 /* We try and resync to an unnested comma, as that will give the
8861 user better diagnostics. */
8862 ending = cp_parser_skip_to_closing_parenthesis (parser,
8863 /*recovering=*/true,
8864 /*or_comma=*/true,
8865 /*consume_paren=*/true);
8866 if (ending < 0)
8867 goto get_comma;
8868 if (!ending)
8870 parser->greater_than_is_operator_p
8871 = saved_greater_than_is_operator_p;
8872 parser->omp_array_section_p = saved_omp_array_section_p;
8873 return NULL;
8877 parser->greater_than_is_operator_p
8878 = saved_greater_than_is_operator_p;
8879 parser->omp_array_section_p = saved_omp_array_section_p;
8881 return expression_list;
8884 /* Parse a pseudo-destructor-name.
8886 pseudo-destructor-name:
8887 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8888 :: [opt] nested-name-specifier template template-id :: ~ type-name
8889 :: [opt] nested-name-specifier [opt] ~ type-name
8891 If either of the first two productions is used, sets *SCOPE to the
8892 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8893 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8894 or ERROR_MARK_NODE if the parse fails. */
8896 static void
8897 cp_parser_pseudo_destructor_name (cp_parser* parser,
8898 tree object,
8899 tree* scope,
8900 tree* type)
8902 bool nested_name_specifier_p;
8904 /* Handle ~auto. */
8905 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8906 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8907 && !type_dependent_expression_p (object))
8909 if (cxx_dialect < cxx14)
8910 pedwarn (input_location, OPT_Wc__14_extensions,
8911 "%<~auto%> only available with "
8912 "%<-std=c++14%> or %<-std=gnu++14%>");
8913 cp_lexer_consume_token (parser->lexer);
8914 cp_lexer_consume_token (parser->lexer);
8915 *scope = NULL_TREE;
8916 *type = TREE_TYPE (object);
8917 return;
8920 /* Assume that things will not work out. */
8921 *type = error_mark_node;
8923 /* Look for the optional `::' operator. */
8924 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8925 /* Look for the optional nested-name-specifier. */
8926 nested_name_specifier_p
8927 = (cp_parser_nested_name_specifier_opt (parser,
8928 /*typename_keyword_p=*/false,
8929 /*check_dependency_p=*/true,
8930 /*type_p=*/false,
8931 /*is_declaration=*/false)
8932 != NULL_TREE);
8933 /* Now, if we saw a nested-name-specifier, we might be doing the
8934 second production. */
8935 if (nested_name_specifier_p
8936 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8938 /* Consume the `template' keyword. */
8939 cp_lexer_consume_token (parser->lexer);
8940 /* Parse the template-id. */
8941 cp_parser_template_id (parser,
8942 /*template_keyword_p=*/true,
8943 /*check_dependency_p=*/false,
8944 class_type,
8945 /*is_declaration=*/true);
8946 /* Look for the `::' token. */
8947 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8949 /* If the next token is not a `~', then there might be some
8950 additional qualification. */
8951 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8953 /* At this point, we're looking for "type-name :: ~". The type-name
8954 must not be a class-name, since this is a pseudo-destructor. So,
8955 it must be either an enum-name, or a typedef-name -- both of which
8956 are just identifiers. So, we peek ahead to check that the "::"
8957 and "~" tokens are present; if they are not, then we can avoid
8958 calling type_name. */
8959 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8960 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8961 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8963 cp_parser_error (parser, "non-scalar type");
8964 return;
8967 /* Look for the type-name. */
8968 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8969 if (*scope == error_mark_node)
8970 return;
8972 /* Look for the `::' token. */
8973 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8975 else
8976 *scope = NULL_TREE;
8978 /* Look for the `~'. */
8979 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8981 /* Once we see the ~, this has to be a pseudo-destructor. */
8982 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8983 cp_parser_commit_to_topmost_tentative_parse (parser);
8985 /* Look for the type-name again. We are not responsible for
8986 checking that it matches the first type-name. */
8987 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8990 /* Parse a unary-expression.
8992 unary-expression:
8993 postfix-expression
8994 ++ cast-expression
8995 -- cast-expression
8996 await-expression
8997 unary-operator cast-expression
8998 sizeof unary-expression
8999 sizeof ( type-id )
9000 alignof ( type-id ) [C++0x]
9001 new-expression
9002 delete-expression
9004 GNU Extensions:
9006 unary-expression:
9007 __extension__ cast-expression
9008 __alignof__ unary-expression
9009 __alignof__ ( type-id )
9010 alignof unary-expression [C++0x]
9011 __real__ cast-expression
9012 __imag__ cast-expression
9013 && identifier
9014 sizeof ( type-id ) { initializer-list , [opt] }
9015 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
9016 __alignof__ ( type-id ) { initializer-list , [opt] }
9018 ADDRESS_P is true iff the unary-expression is appearing as the
9019 operand of the `&' operator. CAST_P is true if this expression is
9020 the target of a cast.
9022 Returns a representation of the expression. */
9024 static cp_expr
9025 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
9026 bool address_p, bool cast_p, bool decltype_p)
9028 cp_token *token;
9029 enum tree_code unary_operator;
9031 /* Peek at the next token. */
9032 token = cp_lexer_peek_token (parser->lexer);
9033 /* Some keywords give away the kind of expression. */
9034 if (token->type == CPP_KEYWORD)
9036 enum rid keyword = token->keyword;
9038 switch (keyword)
9040 case RID_ALIGNOF:
9041 case RID_SIZEOF:
9043 tree operand, ret;
9044 enum tree_code op;
9045 location_t start_loc = token->location;
9047 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
9048 bool std_alignof = id_equal (token->u.value, "alignof");
9050 /* Consume the token. */
9051 cp_lexer_consume_token (parser->lexer);
9052 /* Parse the operand. */
9053 operand = cp_parser_sizeof_operand (parser, keyword);
9055 /* Construct a location e.g. :
9056 alignof (expr)
9057 ^~~~~~~~~~~~~~
9058 with start == caret at the start of the "alignof"/"sizeof"
9059 token, with the endpoint at the final closing paren. */
9060 location_t compound_loc
9061 = make_location (start_loc, start_loc, parser->lexer);
9063 if (TYPE_P (operand))
9064 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
9065 std_alignof, true);
9066 else
9068 /* ISO C++ defines alignof only with types, not with
9069 expressions. So pedwarn if alignof is used with a non-
9070 type expression. However, __alignof__ is ok. */
9071 if (std_alignof)
9072 pedwarn (token->location, OPT_Wpedantic,
9073 "ISO C++ does not allow %<alignof%> "
9074 "with a non-type");
9076 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
9077 std_alignof, true);
9079 /* For SIZEOF_EXPR, just issue diagnostics, but keep
9080 SIZEOF_EXPR with the original operand. */
9081 if (op == SIZEOF_EXPR && ret != error_mark_node)
9083 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
9085 if (!processing_template_decl && TYPE_P (operand))
9087 ret = build_min (SIZEOF_EXPR, size_type_node,
9088 build1 (NOP_EXPR, operand,
9089 error_mark_node));
9090 SIZEOF_EXPR_TYPE_P (ret) = 1;
9092 else
9093 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
9094 TREE_SIDE_EFFECTS (ret) = 0;
9095 TREE_READONLY (ret) = 1;
9096 SET_EXPR_LOCATION (ret, compound_loc);
9100 cp_expr ret_expr (ret, compound_loc);
9101 ret_expr = ret_expr.maybe_add_location_wrapper ();
9102 return ret_expr;
9105 case RID_BUILTIN_HAS_ATTRIBUTE:
9106 return cp_parser_has_attribute_expression (parser);
9108 case RID_NEW:
9109 return cp_parser_new_expression (parser);
9111 case RID_DELETE:
9112 return cp_parser_delete_expression (parser);
9114 case RID_EXTENSION:
9116 /* The saved value of the PEDANTIC flag. */
9117 int saved_pedantic;
9118 tree expr;
9120 /* Save away the PEDANTIC flag. */
9121 cp_parser_extension_opt (parser, &saved_pedantic);
9122 /* Parse the cast-expression. */
9123 expr = cp_parser_simple_cast_expression (parser);
9124 /* Restore the PEDANTIC flag. */
9125 pedantic = saved_pedantic;
9127 return expr;
9130 case RID_REALPART:
9131 case RID_IMAGPART:
9133 tree expression;
9135 /* Consume the `__real__' or `__imag__' token. */
9136 cp_lexer_consume_token (parser->lexer);
9137 /* Parse the cast-expression. */
9138 expression = cp_parser_simple_cast_expression (parser);
9139 /* Create the complete representation. */
9140 return build_x_unary_op (token->location,
9141 (keyword == RID_REALPART
9142 ? REALPART_EXPR : IMAGPART_EXPR),
9143 expression, NULL_TREE,
9144 tf_warning_or_error);
9146 break;
9148 case RID_TRANSACTION_ATOMIC:
9149 case RID_TRANSACTION_RELAXED:
9150 return cp_parser_transaction_expression (parser, keyword);
9152 case RID_NOEXCEPT:
9154 tree expr;
9155 const char *saved_message;
9156 bool saved_integral_constant_expression_p;
9157 bool saved_non_integral_constant_expression_p;
9158 bool saved_greater_than_is_operator_p;
9160 location_t start_loc = token->location;
9162 cp_lexer_consume_token (parser->lexer);
9163 matching_parens parens;
9164 parens.require_open (parser);
9166 saved_message = parser->type_definition_forbidden_message;
9167 parser->type_definition_forbidden_message
9168 = G_("types may not be defined in %<noexcept%> expressions");
9170 saved_integral_constant_expression_p
9171 = parser->integral_constant_expression_p;
9172 saved_non_integral_constant_expression_p
9173 = parser->non_integral_constant_expression_p;
9174 parser->integral_constant_expression_p = false;
9176 saved_greater_than_is_operator_p
9177 = parser->greater_than_is_operator_p;
9178 parser->greater_than_is_operator_p = true;
9180 ++cp_unevaluated_operand;
9181 ++c_inhibit_evaluation_warnings;
9182 ++cp_noexcept_operand;
9183 expr = cp_parser_expression (parser);
9184 --cp_noexcept_operand;
9185 --c_inhibit_evaluation_warnings;
9186 --cp_unevaluated_operand;
9188 parser->greater_than_is_operator_p
9189 = saved_greater_than_is_operator_p;
9191 parser->integral_constant_expression_p
9192 = saved_integral_constant_expression_p;
9193 parser->non_integral_constant_expression_p
9194 = saved_non_integral_constant_expression_p;
9196 parser->type_definition_forbidden_message = saved_message;
9198 parens.require_close (parser);
9200 /* Construct a location of the form:
9201 noexcept (expr)
9202 ^~~~~~~~~~~~~~~
9203 with start == caret, finishing at the close-paren. */
9204 location_t noexcept_loc
9205 = make_location (start_loc, start_loc, parser->lexer);
9207 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9208 noexcept_loc);
9211 case RID_CO_AWAIT:
9213 tree expr;
9214 location_t kw_loc = token->location;
9216 /* Consume the `co_await' token. */
9217 cp_lexer_consume_token (parser->lexer);
9218 /* Parse its cast-expression. */
9219 expr = cp_parser_simple_cast_expression (parser);
9220 if (expr == error_mark_node)
9221 return error_mark_node;
9223 /* Handle [expr.await]. */
9224 return cp_expr (finish_co_await_expr (kw_loc, expr));
9227 default:
9228 break;
9232 /* Look for the `:: new' and `:: delete', which also signal the
9233 beginning of a new-expression, or delete-expression,
9234 respectively. If the next token is `::', then it might be one of
9235 these. */
9236 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9238 enum rid keyword;
9240 /* See if the token after the `::' is one of the keywords in
9241 which we're interested. */
9242 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9243 /* If it's `new', we have a new-expression. */
9244 if (keyword == RID_NEW)
9245 return cp_parser_new_expression (parser);
9246 /* Similarly, for `delete'. */
9247 else if (keyword == RID_DELETE)
9248 return cp_parser_delete_expression (parser);
9251 /* Look for a unary operator. */
9252 unary_operator = cp_parser_unary_operator (token);
9253 /* The `++' and `--' operators can be handled similarly, even though
9254 they are not technically unary-operators in the grammar. */
9255 if (unary_operator == ERROR_MARK)
9257 if (token->type == CPP_PLUS_PLUS)
9258 unary_operator = PREINCREMENT_EXPR;
9259 else if (token->type == CPP_MINUS_MINUS)
9260 unary_operator = PREDECREMENT_EXPR;
9261 /* Handle the GNU address-of-label extension. */
9262 else if (cp_parser_allow_gnu_extensions_p (parser)
9263 && token->type == CPP_AND_AND)
9265 tree identifier;
9266 tree expression;
9267 location_t start_loc = token->location;
9269 /* Consume the '&&' token. */
9270 cp_lexer_consume_token (parser->lexer);
9271 /* Look for the identifier. */
9272 identifier = cp_parser_identifier (parser);
9273 /* Construct a location of the form:
9274 &&label
9275 ^~~~~~~
9276 with caret==start at the "&&", finish at the end of the label. */
9277 location_t combined_loc
9278 = make_location (start_loc, start_loc, parser->lexer);
9279 /* Create an expression representing the address. */
9280 expression = finish_label_address_expr (identifier, combined_loc);
9281 if (TREE_CODE (expression) == ADDR_EXPR)
9282 mark_label_addressed (identifier);
9283 if (cp_parser_non_integral_constant_expression (parser,
9284 NIC_ADDR_LABEL))
9285 expression = error_mark_node;
9286 return expression;
9289 if (unary_operator != ERROR_MARK)
9291 cp_expr cast_expression;
9292 cp_expr expression = error_mark_node;
9293 non_integral_constant non_constant_p = NIC_NONE;
9294 location_t loc = token->location;
9295 tsubst_flags_t complain = complain_flags (decltype_p);
9297 /* Consume the operator token. */
9298 token = cp_lexer_consume_token (parser->lexer);
9299 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9301 /* Parse the cast-expression. */
9302 cast_expression
9303 = cp_parser_cast_expression (parser,
9304 unary_operator == ADDR_EXPR,
9305 /*cast_p=*/false,
9306 /*decltype*/false,
9307 pidk);
9309 /* Make a location:
9310 OP_TOKEN CAST_EXPRESSION
9311 ^~~~~~~~~~~~~~~~~~~~~~~~~
9312 with start==caret at the operator token, and
9313 extending to the end of the cast_expression. */
9314 loc = make_location (loc, loc, cast_expression.get_finish ());
9316 /* Now, build an appropriate representation. */
9317 switch (unary_operator)
9319 case INDIRECT_REF:
9320 non_constant_p = NIC_STAR;
9321 expression = build_x_indirect_ref (loc, cast_expression,
9322 RO_UNARY_STAR, NULL_TREE,
9323 complain);
9324 /* TODO: build_x_indirect_ref does not always honor the
9325 location, so ensure it is set. */
9326 expression.set_location (loc);
9327 break;
9329 case ADDR_EXPR:
9330 non_constant_p = NIC_ADDR;
9331 /* Fall through. */
9332 case BIT_NOT_EXPR:
9333 expression = build_x_unary_op (loc, unary_operator,
9334 cast_expression,
9335 NULL_TREE, complain);
9336 /* TODO: build_x_unary_op does not always honor the location,
9337 so ensure it is set. */
9338 expression.set_location (loc);
9339 break;
9341 case PREINCREMENT_EXPR:
9342 case PREDECREMENT_EXPR:
9343 non_constant_p = unary_operator == PREINCREMENT_EXPR
9344 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9345 /* Fall through. */
9346 case NEGATE_EXPR:
9347 /* Immediately fold negation of a constant, unless the constant is 0
9348 (since -0 == 0) or it would overflow. */
9349 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9351 tree stripped_expr
9352 = tree_strip_any_location_wrapper (cast_expression);
9353 if (CONSTANT_CLASS_P (stripped_expr)
9354 && !integer_zerop (stripped_expr)
9355 && !TREE_OVERFLOW (stripped_expr))
9357 tree folded = fold_build1 (unary_operator,
9358 TREE_TYPE (stripped_expr),
9359 stripped_expr);
9360 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9362 expression = maybe_wrap_with_location (folded, loc);
9363 break;
9367 /* Fall through. */
9368 case UNARY_PLUS_EXPR:
9369 case TRUTH_NOT_EXPR:
9370 expression = finish_unary_op_expr (loc, unary_operator,
9371 cast_expression, complain);
9372 break;
9374 default:
9375 gcc_unreachable ();
9378 if (non_constant_p != NIC_NONE
9379 && cp_parser_non_integral_constant_expression (parser,
9380 non_constant_p))
9381 expression = error_mark_node;
9383 return expression;
9386 return cp_parser_postfix_expression (parser, address_p, cast_p,
9387 /*member_access_only_p=*/false,
9388 decltype_p,
9389 pidk);
9392 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9393 unary-operator, the corresponding tree code is returned. */
9395 static enum tree_code
9396 cp_parser_unary_operator (cp_token* token)
9398 switch (token->type)
9400 case CPP_MULT:
9401 return INDIRECT_REF;
9403 case CPP_AND:
9404 return ADDR_EXPR;
9406 case CPP_PLUS:
9407 return UNARY_PLUS_EXPR;
9409 case CPP_MINUS:
9410 return NEGATE_EXPR;
9412 case CPP_NOT:
9413 return TRUTH_NOT_EXPR;
9415 case CPP_COMPL:
9416 return BIT_NOT_EXPR;
9418 default:
9419 return ERROR_MARK;
9423 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9424 Returns a representation of the expression. */
9426 static tree
9427 cp_parser_has_attribute_expression (cp_parser *parser)
9429 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9431 /* Consume the __builtin_has_attribute token. */
9432 cp_lexer_consume_token (parser->lexer);
9434 matching_parens parens;
9435 if (!parens.require_open (parser))
9436 return error_mark_node;
9438 /* Types cannot be defined in a `sizeof' expression. Save away the
9439 old message. */
9440 const char *saved_message = parser->type_definition_forbidden_message;
9441 const char *saved_message_arg
9442 = parser->type_definition_forbidden_message_arg;
9443 parser->type_definition_forbidden_message
9444 = G_("types may not be defined in %qs expressions");
9445 parser->type_definition_forbidden_message_arg
9446 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9448 /* The restrictions on constant-expressions do not apply inside
9449 sizeof expressions. */
9450 bool saved_integral_constant_expression_p
9451 = parser->integral_constant_expression_p;
9452 bool saved_non_integral_constant_expression_p
9453 = parser->non_integral_constant_expression_p;
9454 parser->integral_constant_expression_p = false;
9456 /* Do not actually evaluate the expression. */
9457 ++cp_unevaluated_operand;
9458 ++c_inhibit_evaluation_warnings;
9460 tree oper = NULL_TREE;
9462 /* We can't be sure yet whether we're looking at a type-id or an
9463 expression. */
9464 cp_parser_parse_tentatively (parser);
9466 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9467 parser->in_type_id_in_expr_p = true;
9468 /* Look for the type-id. */
9469 oper = cp_parser_type_id (parser);
9470 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9472 cp_parser_parse_definitely (parser);
9474 /* If the type-id production did not work out, then we must be
9475 looking at an expression. */
9476 if (!oper || oper == error_mark_node)
9477 oper = cp_parser_assignment_expression (parser);
9479 STRIP_ANY_LOCATION_WRAPPER (oper);
9481 /* Go back to evaluating expressions. */
9482 --cp_unevaluated_operand;
9483 --c_inhibit_evaluation_warnings;
9485 /* And restore the old one. */
9486 parser->type_definition_forbidden_message = saved_message;
9487 parser->type_definition_forbidden_message_arg = saved_message_arg;
9488 parser->integral_constant_expression_p
9489 = saved_integral_constant_expression_p;
9490 parser->non_integral_constant_expression_p
9491 = saved_non_integral_constant_expression_p;
9493 /* Consume the comma if it's there. */
9494 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9496 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9497 /*consume_paren=*/true);
9498 return error_mark_node;
9501 /* Parse the attribute specification. */
9502 bool ret = false;
9503 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9504 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9506 if (oper == error_mark_node)
9507 /* Nothing. */;
9508 else if (processing_template_decl && uses_template_parms (oper))
9509 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9510 "not supported yet");
9511 else
9513 /* Fold constant expressions used in attributes first. */
9514 cp_check_const_attributes (attr);
9516 /* Finally, see if OPER has been declared with ATTR. */
9517 ret = has_attribute (atloc, oper, attr, default_conversion);
9520 parens.require_close (parser);
9522 else
9524 error_at (atloc, "expected identifier");
9525 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9528 /* Construct a location e.g. :
9529 __builtin_has_attribute (oper, attr)
9530 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9531 with start == caret at the start of the built-in token,
9532 and with the endpoint at the final closing paren. */
9533 location_t compound_loc
9534 = make_location (start_loc, start_loc, parser->lexer);
9536 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9537 ret_expr.set_location (compound_loc);
9538 ret_expr = ret_expr.maybe_add_location_wrapper ();
9539 return ret_expr;
9542 /* Parse a new-expression.
9544 new-expression:
9545 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9546 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9548 Returns a representation of the expression. */
9550 static tree
9551 cp_parser_new_expression (cp_parser* parser)
9553 bool global_scope_p;
9554 vec<tree, va_gc> *placement;
9555 tree type;
9556 vec<tree, va_gc> *initializer;
9557 tree nelts = NULL_TREE;
9558 tree ret;
9560 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9562 /* Look for the optional `::' operator. */
9563 global_scope_p
9564 = (cp_parser_global_scope_opt (parser,
9565 /*current_scope_valid_p=*/false)
9566 != NULL_TREE);
9567 /* Look for the `new' operator. */
9568 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9569 /* There's no easy way to tell a new-placement from the
9570 `( type-id )' construct. */
9571 cp_parser_parse_tentatively (parser);
9572 /* Look for a new-placement. */
9573 placement = cp_parser_new_placement (parser);
9574 /* If that didn't work out, there's no new-placement. */
9575 if (!cp_parser_parse_definitely (parser))
9577 if (placement != NULL)
9578 release_tree_vector (placement);
9579 placement = NULL;
9582 /* If the next token is a `(', then we have a parenthesized
9583 type-id. */
9584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9586 cp_token *token;
9587 const char *saved_message = parser->type_definition_forbidden_message;
9589 /* Consume the `('. */
9590 matching_parens parens;
9591 parens.consume_open (parser);
9593 /* Parse the type-id. */
9594 parser->type_definition_forbidden_message
9595 = G_("types may not be defined in a new-expression");
9597 type_id_in_expr_sentinel s (parser);
9598 type = cp_parser_type_id (parser);
9600 parser->type_definition_forbidden_message = saved_message;
9602 /* Look for the closing `)'. */
9603 parens.require_close (parser);
9604 token = cp_lexer_peek_token (parser->lexer);
9605 /* There should not be a direct-new-declarator in this production,
9606 but GCC used to allowed this, so we check and emit a sensible error
9607 message for this case. */
9608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9610 error_at (token->location,
9611 "array bound forbidden after parenthesized type-id");
9612 inform (token->location,
9613 "try removing the parentheses around the type-id");
9614 cp_parser_direct_new_declarator (parser);
9617 /* Otherwise, there must be a new-type-id. */
9618 else
9619 type = cp_parser_new_type_id (parser, &nelts);
9621 /* If the next token is a `(' or '{', then we have a new-initializer. */
9622 cp_token *token = cp_lexer_peek_token (parser->lexer);
9623 if (token->type == CPP_OPEN_PAREN
9624 || token->type == CPP_OPEN_BRACE)
9625 initializer = cp_parser_new_initializer (parser);
9626 else
9627 initializer = NULL;
9629 /* A new-expression may not appear in an integral constant
9630 expression. */
9631 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9632 ret = error_mark_node;
9633 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9634 of a new-type-id or type-id of a new-expression, the new-expression shall
9635 contain a new-initializer of the form ( assignment-expression )".
9636 Additionally, consistently with the spirit of DR 1467, we want to accept
9637 'new auto { 2 }' too. */
9638 else if ((ret = type_uses_auto (type))
9639 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9640 && (vec_safe_length (initializer) != 1
9641 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9642 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9644 error_at (token->location,
9645 "initialization of new-expression for type %<auto%> "
9646 "requires exactly one element");
9647 ret = error_mark_node;
9649 else
9651 /* Construct a location e.g.:
9652 ptr = new int[100]
9653 ^~~~~~~~~~~~
9654 with caret == start at the start of the "new" token, and the end
9655 at the end of the final token we consumed. */
9656 location_t combined_loc = make_location (start_loc, start_loc,
9657 parser->lexer);
9658 /* Create a representation of the new-expression. */
9659 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9660 global_scope_p, tf_warning_or_error);
9663 if (placement != NULL)
9664 release_tree_vector (placement);
9665 if (initializer != NULL)
9666 release_tree_vector (initializer);
9668 return ret;
9671 /* Parse a new-placement.
9673 new-placement:
9674 ( expression-list )
9676 Returns the same representation as for an expression-list. */
9678 static vec<tree, va_gc> *
9679 cp_parser_new_placement (cp_parser* parser)
9681 vec<tree, va_gc> *expression_list;
9683 /* Parse the expression-list. */
9684 expression_list = (cp_parser_parenthesized_expression_list
9685 (parser, non_attr, /*cast_p=*/false,
9686 /*allow_expansion_p=*/true,
9687 /*non_constant_p=*/NULL));
9689 if (expression_list && expression_list->is_empty ())
9690 error ("expected expression-list or type-id");
9692 return expression_list;
9695 /* Parse a new-type-id.
9697 new-type-id:
9698 type-specifier-seq new-declarator [opt]
9700 Returns the TYPE allocated. If the new-type-id indicates an array
9701 type, *NELTS is set to the number of elements in the last array
9702 bound; the TYPE will not include the last array bound. */
9704 static tree
9705 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9707 cp_decl_specifier_seq type_specifier_seq;
9708 cp_declarator *new_declarator;
9709 cp_declarator *declarator;
9710 cp_declarator *outer_declarator;
9711 const char *saved_message;
9713 /* The type-specifier sequence must not contain type definitions.
9714 (It cannot contain declarations of new types either, but if they
9715 are not definitions we will catch that because they are not
9716 complete.) */
9717 saved_message = parser->type_definition_forbidden_message;
9718 parser->type_definition_forbidden_message
9719 = G_("types may not be defined in a new-type-id");
9720 /* Parse the type-specifier-seq. */
9721 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9722 /*is_declaration=*/false,
9723 /*is_trailing_return=*/false,
9724 &type_specifier_seq);
9725 /* Restore the old message. */
9726 parser->type_definition_forbidden_message = saved_message;
9728 if (type_specifier_seq.type == error_mark_node)
9729 return error_mark_node;
9731 /* Parse the new-declarator. */
9732 new_declarator = cp_parser_new_declarator_opt (parser);
9734 /* Determine the number of elements in the last array dimension, if
9735 any. */
9736 *nelts = NULL_TREE;
9737 /* Skip down to the last array dimension. */
9738 declarator = new_declarator;
9739 outer_declarator = NULL;
9740 while (declarator && (declarator->kind == cdk_pointer
9741 || declarator->kind == cdk_ptrmem))
9743 outer_declarator = declarator;
9744 declarator = declarator->declarator;
9746 while (declarator
9747 && declarator->kind == cdk_array
9748 && declarator->declarator
9749 && declarator->declarator->kind == cdk_array)
9751 outer_declarator = declarator;
9752 declarator = declarator->declarator;
9755 if (declarator && declarator->kind == cdk_array)
9757 *nelts = declarator->u.array.bounds;
9758 if (*nelts == error_mark_node)
9759 *nelts = integer_one_node;
9761 if (*nelts == NULL_TREE)
9762 /* Leave [] in the declarator. */;
9763 else if (outer_declarator)
9764 outer_declarator->declarator = declarator->declarator;
9765 else
9766 new_declarator = NULL;
9769 return groktypename (&type_specifier_seq, new_declarator, false);
9772 /* Parse an (optional) new-declarator.
9774 new-declarator:
9775 ptr-operator new-declarator [opt]
9776 direct-new-declarator
9778 Returns the declarator. */
9780 static cp_declarator *
9781 cp_parser_new_declarator_opt (cp_parser* parser)
9783 enum tree_code code;
9784 tree type, std_attributes = NULL_TREE;
9785 cp_cv_quals cv_quals;
9787 /* We don't know if there's a ptr-operator next, or not. */
9788 cp_parser_parse_tentatively (parser);
9789 /* Look for a ptr-operator. */
9790 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9791 /* If that worked, look for more new-declarators. */
9792 if (cp_parser_parse_definitely (parser))
9794 cp_declarator *declarator;
9796 /* Parse another optional declarator. */
9797 declarator = cp_parser_new_declarator_opt (parser);
9799 declarator = cp_parser_make_indirect_declarator
9800 (code, type, cv_quals, declarator, std_attributes);
9802 return declarator;
9805 /* If the next token is a `[', there is a direct-new-declarator. */
9806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9807 return cp_parser_direct_new_declarator (parser);
9809 return NULL;
9812 /* Parse a direct-new-declarator.
9814 direct-new-declarator:
9815 [ expression ]
9816 direct-new-declarator [constant-expression]
9820 static cp_declarator *
9821 cp_parser_direct_new_declarator (cp_parser* parser)
9823 cp_declarator *declarator = NULL;
9824 bool first_p = true;
9826 while (true)
9828 tree expression;
9829 cp_token *token;
9831 /* Look for the opening `['. */
9832 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9834 token = cp_lexer_peek_token (parser->lexer);
9835 if (token->type == CPP_CLOSE_SQUARE && first_p)
9836 expression = NULL_TREE;
9837 else
9838 expression = cp_parser_expression (parser);
9839 /* The standard requires that the expression have integral
9840 type. DR 74 adds enumeration types. We believe that the
9841 real intent is that these expressions be handled like the
9842 expression in a `switch' condition, which also allows
9843 classes with a single conversion to integral or
9844 enumeration type. */
9845 if (expression && !processing_template_decl)
9847 expression
9848 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9849 expression,
9850 /*complain=*/true);
9851 if (!expression)
9853 error_at (token->location,
9854 "expression in new-declarator must have integral "
9855 "or enumeration type");
9856 expression = error_mark_node;
9860 /* Look for the closing `]'. */
9861 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9863 /* Add this bound to the declarator. */
9864 declarator = make_array_declarator (declarator, expression);
9866 /* If the next token is not a `[', then there are no more
9867 bounds. */
9868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9869 break;
9870 first_p = false;
9873 return declarator;
9876 /* Parse a new-initializer.
9878 new-initializer:
9879 ( expression-list [opt] )
9880 braced-init-list
9882 Returns a representation of the expression-list. */
9884 static vec<tree, va_gc> *
9885 cp_parser_new_initializer (cp_parser* parser)
9887 vec<tree, va_gc> *expression_list;
9889 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9891 cp_lexer_set_source_position (parser->lexer);
9892 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9893 tree t = cp_parser_braced_list (parser);
9894 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9895 expression_list = make_tree_vector_single (t);
9897 else
9898 expression_list = (cp_parser_parenthesized_expression_list
9899 (parser, non_attr, /*cast_p=*/false,
9900 /*allow_expansion_p=*/true,
9901 /*non_constant_p=*/NULL));
9903 return expression_list;
9906 /* Parse a delete-expression.
9908 delete-expression:
9909 :: [opt] delete cast-expression
9910 :: [opt] delete [ ] cast-expression
9912 Returns a representation of the expression. */
9914 static tree
9915 cp_parser_delete_expression (cp_parser* parser)
9917 bool global_scope_p;
9918 bool array_p;
9919 tree expression;
9920 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9922 /* Look for the optional `::' operator. */
9923 global_scope_p
9924 = (cp_parser_global_scope_opt (parser,
9925 /*current_scope_valid_p=*/false)
9926 != NULL_TREE);
9927 /* Look for the `delete' keyword. */
9928 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9929 /* See if the array syntax is in use. */
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9932 /* Consume the `[' token. */
9933 cp_lexer_consume_token (parser->lexer);
9934 /* Look for the `]' token. */
9935 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9936 /* Remember that this is the `[]' construct. */
9937 array_p = true;
9939 else
9940 array_p = false;
9942 /* Parse the cast-expression. */
9943 expression = cp_parser_simple_cast_expression (parser);
9945 /* A delete-expression may not appear in an integral constant
9946 expression. */
9947 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9948 return error_mark_node;
9950 /* Construct a location e.g.:
9951 delete [ ] ptr
9952 ^~~~~~~~~~~~~~
9953 with caret == start at the start of the "delete" token, and
9954 the end at the end of the final token we consumed. */
9955 location_t combined_loc = make_location (start_loc, start_loc,
9956 parser->lexer);
9957 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9958 global_scope_p, tf_warning_or_error);
9960 return expression;
9963 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9964 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9965 0 otherwise. */
9967 static int
9968 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9970 cp_token *token = cp_lexer_peek_token (parser->lexer);
9971 switch (token->type)
9973 case CPP_COMMA:
9974 case CPP_SEMICOLON:
9975 case CPP_QUERY:
9976 case CPP_COLON:
9977 case CPP_CLOSE_SQUARE:
9978 case CPP_CLOSE_PAREN:
9979 case CPP_CLOSE_BRACE:
9980 case CPP_OPEN_BRACE:
9981 case CPP_DOT:
9982 case CPP_DOT_STAR:
9983 case CPP_DEREF:
9984 case CPP_DEREF_STAR:
9985 case CPP_DIV:
9986 case CPP_MOD:
9987 case CPP_LSHIFT:
9988 case CPP_RSHIFT:
9989 case CPP_LESS:
9990 case CPP_GREATER:
9991 case CPP_LESS_EQ:
9992 case CPP_GREATER_EQ:
9993 case CPP_EQ_EQ:
9994 case CPP_NOT_EQ:
9995 case CPP_EQ:
9996 case CPP_MULT_EQ:
9997 case CPP_DIV_EQ:
9998 case CPP_MOD_EQ:
9999 case CPP_PLUS_EQ:
10000 case CPP_MINUS_EQ:
10001 case CPP_RSHIFT_EQ:
10002 case CPP_LSHIFT_EQ:
10003 case CPP_AND_EQ:
10004 case CPP_XOR_EQ:
10005 case CPP_OR_EQ:
10006 case CPP_XOR:
10007 case CPP_OR:
10008 case CPP_OR_OR:
10009 case CPP_EOF:
10010 case CPP_ELLIPSIS:
10011 return 0;
10013 case CPP_OPEN_PAREN:
10014 /* In ((type ()) () the last () isn't a valid cast-expression,
10015 so the whole must be parsed as postfix-expression. */
10016 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
10017 != CPP_CLOSE_PAREN;
10019 case CPP_OPEN_SQUARE:
10020 /* '[' may start a primary-expression in obj-c++ and in C++11,
10021 as a lambda-expression, eg, '(void)[]{}'. */
10022 if (cxx_dialect >= cxx11)
10023 return -1;
10024 return c_dialect_objc ();
10026 case CPP_PLUS_PLUS:
10027 case CPP_MINUS_MINUS:
10028 /* '++' and '--' may or may not start a cast-expression:
10030 struct T { void operator++(int); };
10031 void f() { (T())++; }
10035 int a;
10036 (int)++a; */
10037 return -1;
10039 default:
10040 return 1;
10044 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
10045 in the order: const_cast, static_cast, reinterpret_cast.
10047 Don't suggest dynamic_cast.
10049 Return the first legal cast kind found, or NULL otherwise. */
10051 static const char *
10052 get_cast_suggestion (tree dst_type, tree orig_expr)
10054 tree trial;
10056 /* Reuse the parser logic by attempting to build the various kinds of
10057 cast, with "complain" disabled.
10058 Identify the first such cast that is valid. */
10060 /* Don't attempt to run such logic within template processing. */
10061 if (processing_template_decl)
10062 return NULL;
10064 /* First try const_cast. */
10065 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
10066 if (trial != error_mark_node)
10067 return "const_cast";
10069 /* If that fails, try static_cast. */
10070 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
10071 if (trial != error_mark_node)
10072 return "static_cast";
10074 /* Finally, try reinterpret_cast. */
10075 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
10076 tf_none);
10077 if (trial != error_mark_node)
10078 return "reinterpret_cast";
10080 /* No such cast possible. */
10081 return NULL;
10084 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
10085 suggesting how to convert a C-style cast of the form:
10087 (DST_TYPE)ORIG_EXPR
10089 to a C++-style cast.
10091 The primary range of RICHLOC is asssumed to be that of the original
10092 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
10093 of the parens in the C-style cast. */
10095 static void
10096 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
10097 location_t close_paren_loc, tree orig_expr,
10098 tree dst_type)
10100 /* This function is non-trivial, so bail out now if the warning isn't
10101 going to be emitted. */
10102 if (!warn_old_style_cast)
10103 return;
10105 /* Try to find a legal C++ cast, trying them in order:
10106 const_cast, static_cast, reinterpret_cast. */
10107 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
10108 if (!cast_suggestion)
10109 return;
10111 /* Replace the open paren with "CAST_SUGGESTION<". */
10112 pretty_printer pp;
10113 pp_string (&pp, cast_suggestion);
10114 pp_less (&pp);
10115 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
10117 /* Replace the close paren with "> (". */
10118 rich_loc->add_fixit_replace (close_paren_loc, "> (");
10120 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
10121 rich_loc->add_fixit_insert_after (")");
10125 /* Parse a cast-expression.
10127 cast-expression:
10128 unary-expression
10129 ( type-id ) cast-expression
10131 ADDRESS_P is true iff the unary-expression is appearing as the
10132 operand of the `&' operator. CAST_P is true if this expression is
10133 the target of a cast.
10135 Returns a representation of the expression. */
10137 static cp_expr
10138 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10139 bool decltype_p, cp_id_kind * pidk)
10141 /* If it's a `(', then we might be looking at a cast. */
10142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10144 tree type = NULL_TREE;
10145 cp_expr expr (NULL_TREE);
10146 int cast_expression = 0;
10147 const char *saved_message;
10149 /* There's no way to know yet whether or not this is a cast.
10150 For example, `(int (3))' is a unary-expression, while `(int)
10151 3' is a cast. So, we resort to parsing tentatively. */
10152 cp_parser_parse_tentatively (parser);
10153 /* Types may not be defined in a cast. */
10154 saved_message = parser->type_definition_forbidden_message;
10155 parser->type_definition_forbidden_message
10156 = G_("types may not be defined in casts");
10157 /* Consume the `('. */
10158 matching_parens parens;
10159 cp_token *open_paren = parens.consume_open (parser);
10160 location_t open_paren_loc = open_paren->location;
10161 location_t close_paren_loc = UNKNOWN_LOCATION;
10163 /* A very tricky bit is that `(struct S) { 3 }' is a
10164 compound-literal (which we permit in C++ as an extension).
10165 But, that construct is not a cast-expression -- it is a
10166 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10167 is legal; if the compound-literal were a cast-expression,
10168 you'd need an extra set of parentheses.) But, if we parse
10169 the type-id, and it happens to be a class-specifier, then we
10170 will commit to the parse at that point, because we cannot
10171 undo the action that is done when creating a new class. So,
10172 then we cannot back up and do a postfix-expression.
10174 Another tricky case is the following (c++/29234):
10176 struct S { void operator () (); };
10178 void foo ()
10180 ( S()() );
10183 As a type-id we parse the parenthesized S()() as a function
10184 returning a function, groktypename complains and we cannot
10185 back up in this case either.
10187 Therefore, we scan ahead to the closing `)', and check to see
10188 if the tokens after the `)' can start a cast-expression. Otherwise
10189 we are dealing with an unary-expression, a postfix-expression
10190 or something else.
10192 Yet another tricky case, in C++11, is the following (c++/54891):
10194 (void)[]{};
10196 The issue is that usually, besides the case of lambda-expressions,
10197 the parenthesized type-id cannot be followed by '[', and, eg, we
10198 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10199 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10200 we don't commit, we try a cast-expression, then an unary-expression.
10202 Save tokens so that we can put them back. */
10203 cp_lexer_save_tokens (parser->lexer);
10205 /* We may be looking at a cast-expression. */
10206 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10207 /*consume_paren=*/true))
10208 cast_expression
10209 = cp_parser_tokens_start_cast_expression (parser);
10211 /* Roll back the tokens we skipped. */
10212 cp_lexer_rollback_tokens (parser->lexer);
10213 /* If we aren't looking at a cast-expression, simulate an error so
10214 that the call to cp_parser_error_occurred below returns true. */
10215 if (!cast_expression)
10216 cp_parser_simulate_error (parser);
10217 else
10219 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10220 parser->in_type_id_in_expr_p = true;
10221 /* Look for the type-id. */
10222 type = cp_parser_type_id (parser);
10223 /* Look for the closing `)'. */
10224 cp_token *close_paren = parens.require_close (parser);
10225 if (close_paren)
10226 close_paren_loc = close_paren->location;
10227 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10230 /* Restore the saved message. */
10231 parser->type_definition_forbidden_message = saved_message;
10233 /* At this point this can only be either a cast or a
10234 parenthesized ctor such as `(T ())' that looks like a cast to
10235 function returning T. */
10236 if (!cp_parser_error_occurred (parser))
10238 /* Only commit if the cast-expression doesn't start with
10239 '++', '--', or '[' in C++11. */
10240 if (cast_expression > 0)
10241 cp_parser_commit_to_topmost_tentative_parse (parser);
10243 expr = cp_parser_cast_expression (parser,
10244 /*address_p=*/false,
10245 /*cast_p=*/true,
10246 /*decltype_p=*/false,
10247 pidk);
10249 if (cp_parser_parse_definitely (parser))
10251 /* Warn about old-style casts, if so requested. */
10252 if (warn_old_style_cast
10253 && !in_system_header_at (input_location)
10254 && !VOID_TYPE_P (type)
10255 && current_lang_name != lang_name_c)
10257 gcc_rich_location rich_loc (input_location);
10258 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10259 expr, type);
10260 warning_at (&rich_loc, OPT_Wold_style_cast,
10261 "use of old-style cast to %q#T", type);
10264 /* Only type conversions to integral or enumeration types
10265 can be used in constant-expressions. */
10266 if (!cast_valid_in_integral_constant_expression_p (type)
10267 && cp_parser_non_integral_constant_expression (parser,
10268 NIC_CAST))
10269 return error_mark_node;
10271 /* Perform the cast. */
10272 /* Make a location:
10273 (TYPE) EXPR
10274 ^~~~~~~~~~~
10275 with start==caret at the open paren, extending to the
10276 end of "expr". */
10277 location_t cast_loc = make_location (open_paren_loc,
10278 open_paren_loc,
10279 expr.get_finish ());
10280 expr = build_c_cast (cast_loc, type, expr);
10281 return expr;
10284 else
10285 cp_parser_abort_tentative_parse (parser);
10288 /* If we get here, then it's not a cast, so it must be a
10289 unary-expression. */
10290 return cp_parser_unary_expression (parser, pidk, address_p,
10291 cast_p, decltype_p);
10294 /* Parse a binary expression of the general form:
10296 pm-expression:
10297 cast-expression
10298 pm-expression .* cast-expression
10299 pm-expression ->* cast-expression
10301 multiplicative-expression:
10302 pm-expression
10303 multiplicative-expression * pm-expression
10304 multiplicative-expression / pm-expression
10305 multiplicative-expression % pm-expression
10307 additive-expression:
10308 multiplicative-expression
10309 additive-expression + multiplicative-expression
10310 additive-expression - multiplicative-expression
10312 shift-expression:
10313 additive-expression
10314 shift-expression << additive-expression
10315 shift-expression >> additive-expression
10317 relational-expression:
10318 shift-expression
10319 relational-expression < shift-expression
10320 relational-expression > shift-expression
10321 relational-expression <= shift-expression
10322 relational-expression >= shift-expression
10324 GNU Extension:
10326 relational-expression:
10327 relational-expression <? shift-expression
10328 relational-expression >? shift-expression
10330 equality-expression:
10331 relational-expression
10332 equality-expression == relational-expression
10333 equality-expression != relational-expression
10335 and-expression:
10336 equality-expression
10337 and-expression & equality-expression
10339 exclusive-or-expression:
10340 and-expression
10341 exclusive-or-expression ^ and-expression
10343 inclusive-or-expression:
10344 exclusive-or-expression
10345 inclusive-or-expression | exclusive-or-expression
10347 logical-and-expression:
10348 inclusive-or-expression
10349 logical-and-expression && inclusive-or-expression
10351 logical-or-expression:
10352 logical-and-expression
10353 logical-or-expression || logical-and-expression
10355 All these are implemented with a single function like:
10357 binary-expression:
10358 simple-cast-expression
10359 binary-expression <token> binary-expression
10361 CAST_P is true if this expression is the target of a cast.
10363 The binops_by_token map is used to get the tree codes for each <token> type.
10364 binary-expressions are associated according to a precedence table. */
10366 #define TOKEN_PRECEDENCE(token) \
10367 (((token->type == CPP_GREATER \
10368 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10369 && !parser->greater_than_is_operator_p) \
10370 ? PREC_NOT_OPERATOR \
10371 : binops_by_token[token->type].prec)
10373 static cp_expr
10374 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10375 bool no_toplevel_fold_p,
10376 bool decltype_p,
10377 enum cp_parser_prec prec,
10378 cp_id_kind * pidk)
10380 cp_parser_expression_stack stack;
10381 cp_parser_expression_stack_entry *sp = &stack[0];
10382 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10383 cp_parser_expression_stack_entry current;
10384 cp_expr rhs;
10385 cp_token *token;
10386 enum tree_code rhs_type;
10387 enum cp_parser_prec new_prec, lookahead_prec;
10388 tree overload;
10390 /* Parse the first expression. */
10391 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10392 ? TRUTH_NOT_EXPR : ERROR_MARK);
10393 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10394 cast_p, decltype_p, pidk);
10395 current.prec = prec;
10397 if (cp_parser_error_occurred (parser))
10398 return error_mark_node;
10400 for (;;)
10402 /* Get an operator token. */
10403 token = cp_lexer_peek_token (parser->lexer);
10405 if (warn_cxx11_compat
10406 && token->type == CPP_RSHIFT
10407 && !parser->greater_than_is_operator_p)
10409 if (warning_at (token->location, OPT_Wc__11_compat,
10410 "%<>>%> operator is treated"
10411 " as two right angle brackets in C++11"))
10412 inform (token->location,
10413 "suggest parentheses around %<>>%> expression");
10416 new_prec = TOKEN_PRECEDENCE (token);
10417 if (new_prec != PREC_NOT_OPERATOR
10418 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10419 /* This is a fold-expression; handle it later. */
10420 new_prec = PREC_NOT_OPERATOR;
10422 /* Popping an entry off the stack means we completed a subexpression:
10423 - either we found a token which is not an operator (`>' where it is not
10424 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10425 will happen repeatedly;
10426 - or, we found an operator which has lower priority. This is the case
10427 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10428 parsing `3 * 4'. */
10429 if (new_prec <= current.prec)
10431 if (sp == stack)
10432 break;
10433 else
10434 goto pop;
10437 get_rhs:
10438 current.tree_type = binops_by_token[token->type].tree_type;
10439 current.loc = token->location;
10440 current.flags = token->flags;
10442 /* We used the operator token. */
10443 cp_lexer_consume_token (parser->lexer);
10445 /* For "false && x" or "true || x", x will never be executed;
10446 disable warnings while evaluating it. */
10447 if ((current.tree_type == TRUTH_ANDIF_EXPR
10448 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10449 || (current.tree_type == TRUTH_ORIF_EXPR
10450 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10452 disable_warnings_sp = sp;
10453 ++c_inhibit_evaluation_warnings;
10456 /* Extract another operand. It may be the RHS of this expression
10457 or the LHS of a new, higher priority expression. */
10458 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10459 ? TRUTH_NOT_EXPR : ERROR_MARK);
10460 rhs = cp_parser_simple_cast_expression (parser);
10462 /* Get another operator token. Look up its precedence to avoid
10463 building a useless (immediately popped) stack entry for common
10464 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10465 token = cp_lexer_peek_token (parser->lexer);
10466 lookahead_prec = TOKEN_PRECEDENCE (token);
10467 if (lookahead_prec != PREC_NOT_OPERATOR
10468 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10469 lookahead_prec = PREC_NOT_OPERATOR;
10470 if (lookahead_prec > new_prec)
10472 /* ... and prepare to parse the RHS of the new, higher priority
10473 expression. Since precedence levels on the stack are
10474 monotonically increasing, we do not have to care about
10475 stack overflows. */
10476 *sp = current;
10477 ++sp;
10478 current.lhs = rhs;
10479 current.lhs_type = rhs_type;
10480 current.prec = new_prec;
10481 new_prec = lookahead_prec;
10482 goto get_rhs;
10484 pop:
10485 lookahead_prec = new_prec;
10486 /* If the stack is not empty, we have parsed into LHS the right side
10487 (`4' in the example above) of an expression we had suspended.
10488 We can use the information on the stack to recover the LHS (`3')
10489 from the stack together with the tree code (`MULT_EXPR'), and
10490 the precedence of the higher level subexpression
10491 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10492 which will be used to actually build the additive expression. */
10493 rhs = current.lhs;
10494 rhs_type = current.lhs_type;
10495 --sp;
10496 current = *sp;
10499 /* Undo the disabling of warnings done above. */
10500 if (sp == disable_warnings_sp)
10502 disable_warnings_sp = NULL;
10503 --c_inhibit_evaluation_warnings;
10506 if (warn_logical_not_paren
10507 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10508 && current.lhs_type == TRUTH_NOT_EXPR
10509 /* Avoid warning for !!x == y. */
10510 && (TREE_CODE (current.lhs) != NE_EXPR
10511 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10512 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10513 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10514 /* Avoid warning for !b == y where b is boolean. */
10515 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10516 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10517 != BOOLEAN_TYPE))))
10518 /* Avoid warning for !!b == y where b is boolean. */
10519 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10520 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10521 && DECL_P (tree_strip_any_location_wrapper
10522 (TREE_OPERAND (current.lhs, 0)))))
10523 || TREE_TYPE (current.lhs) == NULL_TREE
10524 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10525 warn_logical_not_parentheses (current.loc, current.tree_type,
10526 current.lhs, maybe_constant_value (rhs));
10528 if (warn_xor_used_as_pow
10529 && current.tree_type == BIT_XOR_EXPR
10530 /* Don't warn for named "xor" (as opposed to '^'). */
10531 && !(current.flags & NAMED_OP)
10532 && current.lhs.decimal_p ()
10533 && rhs.decimal_p ())
10534 check_for_xor_used_as_pow
10535 (current.lhs.get_location (),
10536 tree_strip_any_location_wrapper (current.lhs),
10537 current.loc,
10538 rhs.get_location (),
10539 tree_strip_any_location_wrapper (rhs));
10541 overload = NULL;
10543 location_t combined_loc = make_location (current.loc,
10544 current.lhs.get_start (),
10545 rhs.get_finish ());
10547 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10548 ERROR_MARK for everything that is not a binary expression.
10549 This makes warn_about_parentheses miss some warnings that
10550 involve unary operators. For unary expressions we should
10551 pass the correct tree_code unless the unary expression was
10552 surrounded by parentheses.
10554 if (no_toplevel_fold_p
10555 && lookahead_prec <= current.prec
10556 && sp == stack)
10558 if (current.lhs == error_mark_node || rhs == error_mark_node)
10559 current.lhs = error_mark_node;
10560 else
10562 current.lhs.maybe_add_location_wrapper ();
10563 rhs.maybe_add_location_wrapper ();
10564 current.lhs
10565 = build_min (current.tree_type,
10566 TREE_CODE_CLASS (current.tree_type)
10567 == tcc_comparison
10568 ? boolean_type_node : TREE_TYPE (current.lhs),
10569 current.lhs.get_value (), rhs.get_value ());
10570 SET_EXPR_LOCATION (current.lhs, combined_loc);
10573 else
10575 op_location_t op_loc (current.loc, combined_loc);
10576 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10577 current.lhs, current.lhs_type,
10578 rhs, rhs_type, NULL_TREE, &overload,
10579 complain_flags (decltype_p));
10580 /* TODO: build_x_binary_op doesn't always honor the location. */
10581 current.lhs.set_location (combined_loc);
10583 current.lhs_type = current.tree_type;
10585 /* If the binary operator required the use of an overloaded operator,
10586 then this expression cannot be an integral constant-expression.
10587 An overloaded operator can be used even if both operands are
10588 otherwise permissible in an integral constant-expression if at
10589 least one of the operands is of enumeration type. */
10591 if (overload
10592 && cp_parser_non_integral_constant_expression (parser,
10593 NIC_OVERLOADED))
10594 return error_mark_node;
10597 return current.lhs;
10600 static cp_expr
10601 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10602 bool no_toplevel_fold_p,
10603 enum cp_parser_prec prec,
10604 cp_id_kind * pidk)
10606 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10607 /*decltype*/false, prec, pidk);
10610 /* Parse the `? expression : assignment-expression' part of a
10611 conditional-expression. The LOGICAL_OR_EXPR is the
10612 logical-or-expression that started the conditional-expression.
10613 Returns a representation of the entire conditional-expression.
10615 This routine is used by cp_parser_assignment_expression
10616 and cp_parser_conditional_expression.
10618 ? expression : assignment-expression
10620 GNU Extensions:
10622 ? : assignment-expression */
10624 static tree
10625 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10627 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10628 cp_expr assignment_expr;
10629 struct cp_token *token;
10630 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10632 /* Consume the `?' token. */
10633 cp_lexer_consume_token (parser->lexer);
10634 token = cp_lexer_peek_token (parser->lexer);
10635 if (cp_parser_allow_gnu_extensions_p (parser)
10636 && token->type == CPP_COLON)
10638 pedwarn (token->location, OPT_Wpedantic,
10639 "ISO C++ does not allow %<?:%> with omitted middle operand");
10640 /* Implicit true clause. */
10641 expr = NULL_TREE;
10642 c_inhibit_evaluation_warnings +=
10643 folded_logical_or_expr == truthvalue_true_node;
10644 warn_for_omitted_condop (token->location, logical_or_expr);
10646 else
10648 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10649 parser->colon_corrects_to_scope_p = false;
10650 /* Parse the expression. */
10651 c_inhibit_evaluation_warnings +=
10652 folded_logical_or_expr == truthvalue_false_node;
10653 expr = cp_parser_expression (parser);
10654 c_inhibit_evaluation_warnings +=
10655 ((folded_logical_or_expr == truthvalue_true_node)
10656 - (folded_logical_or_expr == truthvalue_false_node));
10657 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10660 /* The next token should be a `:'. */
10661 cp_parser_require (parser, CPP_COLON, RT_COLON);
10662 /* Parse the assignment-expression. */
10663 assignment_expr = cp_parser_assignment_expression (parser);
10664 c_inhibit_evaluation_warnings -=
10665 folded_logical_or_expr == truthvalue_true_node;
10667 /* Make a location:
10668 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10669 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10670 with the caret at the "?", ranging from the start of
10671 the logical_or_expr to the end of the assignment_expr. */
10672 loc = make_location (loc,
10673 logical_or_expr.get_start (),
10674 assignment_expr.get_finish ());
10676 /* Build the conditional-expression. */
10677 return build_x_conditional_expr (loc, logical_or_expr,
10678 expr,
10679 assignment_expr,
10680 tf_warning_or_error);
10683 /* Parse a conditional-expression.
10685 conditional-expression:
10686 logical-or-expression
10687 logical-or-expression ? expression : assignment-expression
10689 GNU Extensions:
10691 logical-or-expression ? : assignment-expression */
10693 static cp_expr
10694 cp_parser_conditional_expression (cp_parser *parser)
10696 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10697 PREC_NOT_OPERATOR, NULL);
10698 /* If the next token is a `?' then we're actually looking at
10699 a conditional-expression; otherwise we're done. */
10700 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10701 return cp_parser_question_colon_clause (parser, expr);
10702 return expr;
10705 /* Parse an assignment-expression.
10707 assignment-expression:
10708 conditional-expression
10709 logical-or-expression assignment-operator assignment_expression
10710 throw-expression
10711 yield-expression
10713 CAST_P is true if this expression is the target of a cast.
10714 DECLTYPE_P is true if this expression is the operand of decltype.
10716 Returns a representation for the expression. */
10718 static cp_expr
10719 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10720 bool cast_p, bool decltype_p)
10722 cp_expr expr;
10724 /* If the next token is the `throw' keyword, then we're looking at
10725 a throw-expression. */
10726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10727 expr = cp_parser_throw_expression (parser);
10728 /* If the next token is the `co_yield' keyword, then we're looking at
10729 a yield-expression. */
10730 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10731 expr = cp_parser_yield_expression (parser);
10732 /* Otherwise, it must be that we are looking at a
10733 logical-or-expression. */
10734 else
10736 /* Parse the binary expressions (logical-or-expression). */
10737 expr = cp_parser_binary_expression (parser, cast_p, false,
10738 decltype_p,
10739 PREC_NOT_OPERATOR, pidk);
10740 /* If the next token is a `?' then we're actually looking at a
10741 conditional-expression. */
10742 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10743 return cp_parser_question_colon_clause (parser, expr);
10744 else
10746 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10748 /* If it's an assignment-operator, we're using the second
10749 production. */
10750 enum tree_code assignment_operator
10751 = cp_parser_assignment_operator_opt (parser);
10752 if (assignment_operator != ERROR_MARK)
10754 /* Parse the right-hand side of the assignment. */
10755 cp_expr rhs = cp_parser_initializer_clause (parser);
10757 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10760 /* An assignment may not appear in a
10761 constant-expression. */
10762 if (cp_parser_non_integral_constant_expression (parser,
10763 NIC_ASSIGNMENT))
10764 return error_mark_node;
10765 /* Build the assignment expression. Its default
10766 location:
10767 LHS = RHS
10768 ~~~~^~~~~
10769 is the location of the '=' token as the
10770 caret, ranging from the start of the lhs to the
10771 end of the rhs. */
10772 loc = make_location (loc,
10773 expr.get_start (),
10774 rhs.get_finish ());
10775 expr = build_x_modify_expr (loc, expr,
10776 assignment_operator,
10777 rhs, NULL_TREE,
10778 complain_flags (decltype_p));
10779 /* TODO: build_x_modify_expr doesn't honor the location,
10780 so we must set it here. */
10781 expr.set_location (loc);
10786 return expr;
10789 /* Parse an (optional) assignment-operator.
10791 assignment-operator: one of
10792 = *= /= %= += -= >>= <<= &= ^= |=
10794 GNU Extension:
10796 assignment-operator: one of
10797 <?= >?=
10799 If the next token is an assignment operator, the corresponding tree
10800 code is returned, and the token is consumed. For example, for
10801 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10802 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10803 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10804 operator, ERROR_MARK is returned. */
10806 static enum tree_code
10807 cp_parser_assignment_operator_opt (cp_parser* parser)
10809 enum tree_code op;
10810 cp_token *token;
10812 /* Peek at the next token. */
10813 token = cp_lexer_peek_token (parser->lexer);
10815 switch (token->type)
10817 case CPP_EQ:
10818 op = NOP_EXPR;
10819 break;
10821 case CPP_MULT_EQ:
10822 op = MULT_EXPR;
10823 break;
10825 case CPP_DIV_EQ:
10826 op = TRUNC_DIV_EXPR;
10827 break;
10829 case CPP_MOD_EQ:
10830 op = TRUNC_MOD_EXPR;
10831 break;
10833 case CPP_PLUS_EQ:
10834 op = PLUS_EXPR;
10835 break;
10837 case CPP_MINUS_EQ:
10838 op = MINUS_EXPR;
10839 break;
10841 case CPP_RSHIFT_EQ:
10842 op = RSHIFT_EXPR;
10843 break;
10845 case CPP_LSHIFT_EQ:
10846 op = LSHIFT_EXPR;
10847 break;
10849 case CPP_AND_EQ:
10850 op = BIT_AND_EXPR;
10851 break;
10853 case CPP_XOR_EQ:
10854 op = BIT_XOR_EXPR;
10855 break;
10857 case CPP_OR_EQ:
10858 op = BIT_IOR_EXPR;
10859 break;
10861 default:
10862 /* Nothing else is an assignment operator. */
10863 op = ERROR_MARK;
10866 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10867 if (op != ERROR_MARK
10868 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10869 op = ERROR_MARK;
10871 /* If it was an assignment operator, consume it. */
10872 if (op != ERROR_MARK)
10873 cp_lexer_consume_token (parser->lexer);
10875 return op;
10878 /* Parse an expression.
10880 expression:
10881 assignment-expression
10882 expression , assignment-expression
10884 CAST_P is true if this expression is the target of a cast.
10885 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10886 except possibly parenthesized or on the RHS of a comma (N3276).
10887 WARN_COMMA_P is true if a comma should be diagnosed.
10889 Returns a representation of the expression. */
10891 static cp_expr
10892 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10893 bool cast_p, bool decltype_p, bool warn_comma_p)
10895 cp_expr expression = NULL_TREE;
10896 location_t loc = UNKNOWN_LOCATION;
10898 while (true)
10900 cp_expr assignment_expression;
10902 /* Parse the next assignment-expression. */
10903 assignment_expression
10904 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10906 /* We don't create a temporary for a call that is the immediate operand
10907 of decltype or on the RHS of a comma. But when we see a comma, we
10908 need to create a temporary for a call on the LHS. */
10909 if (decltype_p && !processing_template_decl
10910 && TREE_CODE (assignment_expression) == CALL_EXPR
10911 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10912 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10913 assignment_expression
10914 = build_cplus_new (TREE_TYPE (assignment_expression),
10915 assignment_expression, tf_warning_or_error);
10917 /* If this is the first assignment-expression, we can just
10918 save it away. */
10919 if (!expression)
10920 expression = assignment_expression;
10921 else
10923 /* Create a location with caret at the comma, ranging
10924 from the start of the LHS to the end of the RHS. */
10925 loc = make_location (loc,
10926 expression.get_start (),
10927 assignment_expression.get_finish ());
10928 expression = build_x_compound_expr (loc, expression,
10929 assignment_expression, NULL_TREE,
10930 complain_flags (decltype_p));
10931 expression.set_location (loc);
10933 /* If the next token is not a comma, or we're in a fold-expression, then
10934 we are done with the expression. */
10935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10936 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10937 break;
10938 /* Consume the `,'. */
10939 loc = cp_lexer_peek_token (parser->lexer)->location;
10940 if (warn_comma_p)
10942 /* [depr.comma.subscript]: A comma expression appearing as
10943 the expr-or-braced-init-list of a subscripting expression
10944 is deprecated. A parenthesized comma expression is not
10945 deprecated. */
10946 warning_at (loc, OPT_Wcomma_subscript,
10947 "top-level comma expression in array subscript "
10948 "is deprecated");
10949 warn_comma_p = false;
10951 cp_lexer_consume_token (parser->lexer);
10952 /* A comma operator cannot appear in a constant-expression. */
10953 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10954 expression = error_mark_node;
10957 return expression;
10960 /* Parse a constant-expression.
10962 constant-expression:
10963 conditional-expression
10965 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10966 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10967 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10968 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10969 greater than 1, this isn't really a constant-expression, only a
10970 potentially constant-evaluated expression. If STRICT_P is true,
10971 only parse a conditional-expression, otherwise parse an
10972 assignment-expression. See below for rationale. */
10974 static cp_expr
10975 cp_parser_constant_expression (cp_parser* parser,
10976 int allow_non_constant_p /* = 0 */,
10977 bool *non_constant_p /* = NULL */,
10978 bool strict_p /* = false */)
10980 /* It might seem that we could simply parse the
10981 conditional-expression, and then check to see if it were
10982 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10983 one that the compiler can figure out is constant, possibly after
10984 doing some simplifications or optimizations. The standard has a
10985 precise definition of constant-expression, and we must honor
10986 that, even though it is somewhat more restrictive.
10988 For example:
10990 int i[(2, 3)];
10992 is not a legal declaration, because `(2, 3)' is not a
10993 constant-expression. The `,' operator is forbidden in a
10994 constant-expression. However, GCC's constant-folding machinery
10995 will fold this operation to an INTEGER_CST for `3'. */
10997 /* Save the old settings. */
10998 bool saved_integral_constant_expression_p
10999 = parser->integral_constant_expression_p;
11000 bool saved_allow_non_integral_constant_expression_p
11001 = parser->allow_non_integral_constant_expression_p;
11002 bool saved_non_integral_constant_expression_p
11003 = parser->non_integral_constant_expression_p;
11004 /* We are now parsing a constant-expression. */
11005 parser->integral_constant_expression_p = true;
11006 parser->allow_non_integral_constant_expression_p
11007 = (allow_non_constant_p || cxx_dialect >= cxx11);
11008 parser->non_integral_constant_expression_p = false;
11010 /* A manifestly constant-evaluated expression is evaluated even in an
11011 unevaluated operand. */
11012 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
11014 /* Although the grammar says "conditional-expression", when not STRICT_P,
11015 we parse an "assignment-expression", which also permits
11016 "throw-expression" and the use of assignment operators. In the case
11017 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
11018 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
11019 actually essential that we look for an assignment-expression.
11020 For example, cp_parser_initializer_clauses uses this function to
11021 determine whether a particular assignment-expression is in fact
11022 constant. */
11023 cp_expr expression;
11024 if (strict_p)
11025 expression = cp_parser_conditional_expression (parser);
11026 else
11027 expression = cp_parser_assignment_expression (parser);
11028 /* Restore the old settings. */
11029 parser->integral_constant_expression_p
11030 = saved_integral_constant_expression_p;
11031 parser->allow_non_integral_constant_expression_p
11032 = saved_allow_non_integral_constant_expression_p;
11033 if (cxx_dialect >= cxx11
11034 && (!allow_non_constant_p || non_constant_p))
11036 /* Require an rvalue constant expression here; that's what our
11037 callers expect. Reference constant expressions are handled
11038 separately in e.g. cp_parser_template_argument. */
11039 tree decay = expression;
11040 if (TREE_TYPE (expression)
11041 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
11042 decay = build_address (expression);
11043 bool is_const = is_rvalue_constant_expression (decay);
11044 parser->non_integral_constant_expression_p = !is_const;
11045 if (!is_const && !allow_non_constant_p)
11046 require_rvalue_constant_expression (decay);
11048 if (allow_non_constant_p && non_constant_p)
11049 *non_constant_p = parser->non_integral_constant_expression_p;
11050 parser->non_integral_constant_expression_p
11051 = saved_non_integral_constant_expression_p;
11053 return expression;
11056 /* Parse __builtin_offsetof.
11058 offsetof-expression:
11059 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
11061 offsetof-member-designator:
11062 id-expression
11063 | offsetof-member-designator "." id-expression
11064 | offsetof-member-designator "[" expression "]"
11065 | offsetof-member-designator "->" id-expression */
11067 static cp_expr
11068 cp_parser_builtin_offsetof (cp_parser *parser)
11070 int save_ice_p, save_non_ice_p;
11071 tree type;
11072 cp_expr expr;
11073 cp_id_kind dummy;
11074 cp_token *token;
11075 location_t finish_loc;
11077 /* We're about to accept non-integral-constant things, but will
11078 definitely yield an integral constant expression. Save and
11079 restore these values around our local parsing. */
11080 save_ice_p = parser->integral_constant_expression_p;
11081 save_non_ice_p = parser->non_integral_constant_expression_p;
11083 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11085 /* Consume the "__builtin_offsetof" token. */
11086 cp_lexer_consume_token (parser->lexer);
11087 /* Consume the opening `('. */
11088 matching_parens parens;
11089 parens.require_open (parser);
11090 /* Parse the type-id. */
11091 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11093 const char *saved_message = parser->type_definition_forbidden_message;
11094 parser->type_definition_forbidden_message
11095 = G_("types may not be defined within %<__builtin_offsetof%>");
11096 type = cp_parser_type_id (parser);
11097 parser->type_definition_forbidden_message = saved_message;
11099 /* Look for the `,'. */
11100 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11101 token = cp_lexer_peek_token (parser->lexer);
11103 /* Build the (type *)null that begins the traditional offsetof macro. */
11104 tree object_ptr
11105 = build_static_cast (input_location, build_pointer_type (type),
11106 null_pointer_node, tf_warning_or_error);
11108 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
11109 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
11110 true, &dummy, token->location);
11111 while (true)
11113 token = cp_lexer_peek_token (parser->lexer);
11114 switch (token->type)
11116 case CPP_OPEN_SQUARE:
11117 /* offsetof-member-designator "[" expression "]" */
11118 expr = cp_parser_postfix_open_square_expression (parser, expr,
11119 true, false);
11120 break;
11122 case CPP_DEREF:
11123 /* offsetof-member-designator "->" identifier */
11124 expr = grok_array_decl (token->location, expr, integer_zero_node,
11125 NULL, tf_warning_or_error);
11126 /* FALLTHRU */
11128 case CPP_DOT:
11129 /* offsetof-member-designator "." identifier */
11130 cp_lexer_consume_token (parser->lexer);
11131 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
11132 expr, true, &dummy,
11133 token->location);
11134 break;
11136 case CPP_CLOSE_PAREN:
11137 /* Consume the ")" token. */
11138 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11139 cp_lexer_consume_token (parser->lexer);
11140 goto success;
11142 default:
11143 /* Error. We know the following require will fail, but
11144 that gives the proper error message. */
11145 parens.require_close (parser);
11146 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11147 expr = error_mark_node;
11148 goto failure;
11152 success:
11153 /* Make a location of the form:
11154 __builtin_offsetof (struct s, f)
11155 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11156 with caret at the type-id, ranging from the start of the
11157 "_builtin_offsetof" token to the close paren. */
11158 loc = make_location (loc, start_loc, finish_loc);
11159 /* The result will be an INTEGER_CST, so we need to explicitly
11160 preserve the location. */
11161 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11163 failure:
11164 parser->integral_constant_expression_p = save_ice_p;
11165 parser->non_integral_constant_expression_p = save_non_ice_p;
11167 expr = expr.maybe_add_location_wrapper ();
11168 return expr;
11171 /* Parse a builtin trait expression or type. */
11173 static cp_expr
11174 cp_parser_trait (cp_parser* parser, const cp_trait* trait)
11176 const cp_trait_kind kind = trait->kind;
11177 tree type1, type2 = NULL_TREE;
11178 const bool binary = (trait->arity == 2);
11179 const bool variadic = (trait->arity == -1);
11180 const bool type = trait->type;
11182 /* Get location of initial token. */
11183 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11185 /* Consume the token. */
11186 cp_lexer_consume_token (parser->lexer);
11188 matching_parens parens;
11189 if (kind == CPTK_TYPE_PACK_ELEMENT)
11190 cp_parser_require (parser, CPP_LESS, RT_LESS);
11191 else
11192 parens.require_open (parser);
11194 if (kind == CPTK_IS_DEDUCIBLE)
11196 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11197 type1 = cp_parser_id_expression (parser,
11198 /*template_keyword_p=*/false,
11199 /*check_dependency_p=*/true,
11200 nullptr,
11201 /*declarator_p=*/false,
11202 /*optional_p=*/false);
11203 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11205 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11206 /* __type_pack_element takes an expression as its first argument and uses
11207 template-id syntax instead of function call syntax (for consistency
11208 with Clang). We special case these properties of __type_pack_element
11209 here and elsewhere. */
11210 type1 = cp_parser_constant_expression (parser);
11211 else
11213 type_id_in_expr_sentinel s (parser);
11214 type1 = cp_parser_type_id (parser);
11217 if (type1 == error_mark_node)
11218 return error_mark_node;
11220 if (kind == CPTK_TYPE_PACK_ELEMENT)
11222 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11223 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11224 for (tree elt : tree_vec_range (trailing))
11226 if (!TYPE_P (elt))
11228 error_at (cp_expr_loc_or_input_loc (elt),
11229 "trailing argument to %<__type_pack_element%> "
11230 "is not a type");
11231 return error_mark_node;
11234 type2 = trailing;
11236 else if (binary)
11238 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11241 type_id_in_expr_sentinel s (parser);
11242 type2 = cp_parser_type_id (parser);
11245 if (type2 == error_mark_node)
11246 return error_mark_node;
11248 else if (variadic)
11250 auto_vec<tree, 4> trailing;
11251 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11253 cp_lexer_consume_token (parser->lexer);
11254 tree elt = cp_parser_type_id (parser);
11255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11257 cp_lexer_consume_token (parser->lexer);
11258 elt = make_pack_expansion (elt);
11260 if (elt == error_mark_node)
11261 return error_mark_node;
11262 trailing.safe_push (elt);
11264 type2 = make_tree_vec (trailing.length ());
11265 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11266 TREE_VEC_ELT (type2, i) = trailing[i];
11269 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11270 if (kind == CPTK_TYPE_PACK_ELEMENT)
11271 /* cp_parser_enclosed_template_argument_list above already took care
11272 of parsing the closing '>'. */;
11273 else
11274 parens.require_close (parser);
11276 /* Construct a location of the form:
11277 __is_trivially_copyable(_Tp)
11278 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11279 with start == caret, finishing at the close-paren. */
11280 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11282 /* Complete the trait expression, which may mean either processing
11283 the trait expr now or saving it for template instantiation. */
11284 switch (kind)
11286 case CPTK_BASES:
11287 return cp_expr (finish_bases (type1, false), trait_loc);
11288 case CPTK_DIRECT_BASES:
11289 return cp_expr (finish_bases (type1, true), trait_loc);
11290 default:
11291 if (type)
11292 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11293 else
11294 return finish_trait_expr (trait_loc, kind, type1, type2);
11298 /* Parse a lambda expression.
11300 lambda-expression:
11301 lambda-introducer lambda-declarator [opt] compound-statement
11302 lambda-introducer < template-parameter-list > requires-clause [opt]
11303 lambda-declarator [opt] compound-statement
11305 Returns a representation of the expression. */
11307 static cp_expr
11308 cp_parser_lambda_expression (cp_parser* parser)
11310 tree lambda_expr = build_lambda_expr ();
11311 tree type;
11312 bool ok = true;
11313 cp_token *token = cp_lexer_peek_token (parser->lexer);
11314 cp_token_position start = 0;
11316 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11318 if (cxx_dialect >= cxx20)
11320 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11321 non-type parameter is nonsensical.
11323 Distinguish a lambda in the parameter type from a lambda in the
11324 default argument by looking at local_variables_forbidden_p, which is
11325 only set in default arguments. */
11326 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11328 error_at (token->location,
11329 "lambda-expression in template parameter type");
11330 token->error_reported = true;
11331 ok = false;
11334 else if (cp_unevaluated_operand)
11336 if (!token->error_reported)
11338 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11339 "lambda-expression in unevaluated context"
11340 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11341 token->error_reported = true;
11343 ok = false;
11345 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11347 if (!token->error_reported)
11349 error_at (token->location, "lambda-expression in template-argument"
11350 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11351 token->error_reported = true;
11353 ok = false;
11356 /* We may be in the middle of deferred access check. Disable
11357 it now. */
11358 push_deferring_access_checks (dk_no_deferred);
11360 cp_parser_lambda_introducer (parser, lambda_expr);
11361 if (cp_parser_error_occurred (parser))
11362 return error_mark_node;
11364 type = begin_lambda_type (lambda_expr);
11365 if (type == error_mark_node)
11366 return error_mark_node;
11368 record_lambda_scope (lambda_expr);
11369 record_lambda_scope_discriminator (lambda_expr);
11371 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11372 determine_visibility (TYPE_NAME (type));
11374 /* Now that we've started the type, add the capture fields for any
11375 explicit captures. */
11376 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11379 /* Inside the class, surrounding template-parameter-lists do not apply. */
11380 unsigned int saved_num_template_parameter_lists
11381 = parser->num_template_parameter_lists;
11382 unsigned char in_statement = parser->in_statement;
11383 bool in_switch_statement_p = parser->in_switch_statement_p;
11384 bool fully_implicit_function_template_p
11385 = parser->fully_implicit_function_template_p;
11386 tree implicit_template_parms = parser->implicit_template_parms;
11387 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11388 bool auto_is_implicit_function_template_parm_p
11389 = parser->auto_is_implicit_function_template_parm_p;
11390 bool saved_omp_array_section_p = parser->omp_array_section_p;
11392 parser->num_template_parameter_lists = 0;
11393 parser->in_statement = 0;
11394 parser->in_switch_statement_p = false;
11395 parser->fully_implicit_function_template_p = false;
11396 parser->implicit_template_parms = 0;
11397 parser->implicit_template_scope = 0;
11398 parser->auto_is_implicit_function_template_parm_p = false;
11399 parser->omp_array_section_p = false;
11401 /* The body of a lambda in a discarded statement is not discarded. */
11402 bool discarded = in_discarded_stmt;
11403 in_discarded_stmt = 0;
11405 /* Similarly the body of a lambda in immediate function context is not
11406 in immediate function context. */
11407 bool save_in_consteval_if_p = in_consteval_if_p;
11408 in_consteval_if_p = false;
11410 /* By virtue of defining a local class, a lambda expression has access to
11411 the private variables of enclosing classes. */
11413 if (cp_parser_start_tentative_firewall (parser))
11414 start = token;
11416 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11418 if (ok && cp_parser_error_occurred (parser))
11419 ok = false;
11421 if (ok)
11422 cp_parser_lambda_body (parser, lambda_expr);
11423 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11425 if (cp_parser_skip_to_closing_brace (parser))
11426 cp_lexer_consume_token (parser->lexer);
11429 /* The capture list was built up in reverse order; fix that now. */
11430 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11431 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11433 if (ok)
11434 maybe_add_lambda_conv_op (type);
11436 finish_struct (type, /*attributes=*/NULL_TREE);
11438 in_consteval_if_p = save_in_consteval_if_p;
11439 in_discarded_stmt = discarded;
11441 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11442 parser->in_statement = in_statement;
11443 parser->in_switch_statement_p = in_switch_statement_p;
11444 parser->fully_implicit_function_template_p
11445 = fully_implicit_function_template_p;
11446 parser->implicit_template_parms = implicit_template_parms;
11447 parser->implicit_template_scope = implicit_template_scope;
11448 parser->auto_is_implicit_function_template_parm_p
11449 = auto_is_implicit_function_template_parm_p;
11450 parser->omp_array_section_p = saved_omp_array_section_p;
11453 /* This field is only used during parsing of the lambda. */
11454 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11456 /* This lambda shouldn't have any proxies left at this point. */
11457 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11458 /* And now that we're done, push proxies for an enclosing lambda. */
11459 insert_pending_capture_proxies ();
11461 /* Update the lambda expression to a range. */
11462 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11463 token->location,
11464 parser->lexer);
11466 if (ok)
11467 lambda_expr = build_lambda_object (lambda_expr);
11468 else
11469 lambda_expr = error_mark_node;
11471 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11473 pop_deferring_access_checks ();
11475 return lambda_expr;
11478 /* Parse the beginning of a lambda expression.
11480 lambda-introducer:
11481 [ lambda-capture [opt] ]
11483 LAMBDA_EXPR is the current representation of the lambda expression. */
11485 static void
11486 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11488 /* Need commas after the first capture. */
11489 bool first = true;
11491 /* Eat the leading `['. */
11492 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11494 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11495 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11496 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11497 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11498 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11499 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11500 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11501 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11503 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11505 cp_lexer_consume_token (parser->lexer);
11506 first = false;
11508 if (!(at_function_scope_p () || parsing_nsdmi ()))
11509 error ("non-local lambda expression cannot have a capture-default");
11512 hash_set<tree, true> ids;
11513 tree first_capture_id = NULL_TREE;
11514 unsigned name_independent_cnt = 0;
11515 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11517 cp_token* capture_token;
11518 tree capture_id;
11519 tree capture_init_expr;
11520 cp_id_kind idk = CP_ID_KIND_NONE;
11521 bool explicit_init_p = false;
11523 enum capture_kind_type
11525 BY_COPY,
11526 BY_REFERENCE
11528 enum capture_kind_type capture_kind = BY_COPY;
11530 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11532 error ("expected end of capture-list");
11533 return;
11536 if (first)
11537 first = false;
11538 else
11539 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11541 /* Possibly capture `this'. */
11542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11544 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11545 if (cxx_dialect < cxx20 && pedantic
11546 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11547 pedwarn (loc, OPT_Wc__20_extensions,
11548 "explicit by-copy capture of %<this%> "
11549 "with by-copy capture default only available with "
11550 "%<-std=c++20%> or %<-std=gnu++20%>");
11551 cp_lexer_consume_token (parser->lexer);
11552 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11553 pedwarn (input_location, 0,
11554 "already captured %qD in lambda expression",
11555 this_identifier);
11556 else
11557 add_capture (lambda_expr, /*id=*/this_identifier,
11558 /*initializer=*/finish_this_expr (),
11559 /*by_reference_p=*/true, explicit_init_p, NULL);
11560 continue;
11563 /* Possibly capture `*this'. */
11564 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11565 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11567 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11568 if (cxx_dialect < cxx17)
11569 pedwarn (loc, OPT_Wc__17_extensions,
11570 "%<*this%> capture only available with "
11571 "%<-std=c++17%> or %<-std=gnu++17%>");
11572 cp_lexer_consume_token (parser->lexer);
11573 cp_lexer_consume_token (parser->lexer);
11574 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11575 pedwarn (input_location, 0,
11576 "already captured %qD in lambda expression",
11577 this_identifier);
11578 else
11579 add_capture (lambda_expr, /*id=*/this_identifier,
11580 /*initializer=*/finish_this_expr (),
11581 /*by_reference_p=*/false, explicit_init_p, NULL);
11582 continue;
11585 /* But reject `&this'. */
11586 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11587 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11589 error_at (cp_lexer_peek_token (parser->lexer)->location,
11590 "%<this%> cannot be captured by reference");
11591 cp_lexer_consume_token (parser->lexer);
11592 cp_lexer_consume_token (parser->lexer);
11593 continue;
11596 /* Remember whether we want to capture as a reference or not. */
11597 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11599 capture_kind = BY_REFERENCE;
11600 cp_lexer_consume_token (parser->lexer);
11603 bool init_pack_expansion = false;
11604 location_t ellipsis_loc = UNKNOWN_LOCATION;
11605 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11607 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11608 if (cxx_dialect < cxx20)
11609 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11610 "pack init-capture only available with "
11611 "%<-std=c++20%> or %<-std=gnu++20%>");
11612 cp_lexer_consume_token (parser->lexer);
11613 init_pack_expansion = true;
11616 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11617 if (init_pack_expansion && capture_kind != BY_REFERENCE
11618 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11620 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11621 0, "%<&%> should come before %<...%>");
11622 capture_kind = BY_REFERENCE;
11623 cp_lexer_consume_token (parser->lexer);
11626 /* Get the identifier. */
11627 capture_token = cp_lexer_peek_token (parser->lexer);
11628 capture_id = cp_parser_identifier (parser);
11630 if (capture_id == error_mark_node)
11631 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11632 delimiters, but I modified this to stop on unnested ']' as well. It
11633 was already changed to stop on unnested '}', so the
11634 "closing_parenthesis" name is no more misleading with my change. */
11636 cp_parser_skip_to_closing_parenthesis (parser,
11637 /*recovering=*/true,
11638 /*or_comma=*/true,
11639 /*consume_paren=*/true);
11640 break;
11643 /* Find the initializer for this capture. */
11644 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11645 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11646 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11648 /* An explicit initializer exists. */
11649 if (cxx_dialect < cxx14)
11650 pedwarn (input_location, OPT_Wc__14_extensions,
11651 "lambda capture initializers "
11652 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11653 capture_init_expr = cp_parser_initializer (parser,
11654 /*direct_init=*/nullptr,
11655 /*non_constant=*/nullptr,
11656 /*subexpression_p=*/true);
11657 explicit_init_p = true;
11658 if (capture_init_expr == NULL_TREE)
11660 error ("empty initializer for lambda init-capture");
11661 capture_init_expr = error_mark_node;
11663 if (init_pack_expansion)
11664 capture_init_expr = make_pack_expansion (capture_init_expr);
11666 else
11668 const char* error_msg;
11670 /* Turn the identifier into an id-expression. */
11671 capture_init_expr
11672 = cp_parser_lookup_name_simple (parser, capture_id,
11673 capture_token->location);
11675 if (capture_init_expr == error_mark_node)
11677 unqualified_name_lookup_error (capture_id);
11678 continue;
11680 else if (!VAR_P (capture_init_expr)
11681 && TREE_CODE (capture_init_expr) != PARM_DECL)
11683 error_at (capture_token->location,
11684 "capture of non-variable %qE",
11685 capture_init_expr);
11686 if (DECL_P (capture_init_expr))
11687 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11688 "%q#D declared here", capture_init_expr);
11689 continue;
11691 if (VAR_P (capture_init_expr)
11692 && decl_storage_duration (capture_init_expr) != dk_auto)
11694 if (pedwarn (capture_token->location, 0, "capture of variable "
11695 "%qD with non-automatic storage duration",
11696 capture_init_expr))
11697 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11698 "%q#D declared here", capture_init_expr);
11699 continue;
11702 capture_init_expr
11703 = finish_id_expression
11704 (capture_id,
11705 capture_init_expr,
11706 parser->scope,
11707 &idk,
11708 /*integral_constant_expression_p=*/false,
11709 /*allow_non_integral_constant_expression_p=*/false,
11710 /*non_integral_constant_expression_p=*/NULL,
11711 /*template_p=*/false,
11712 /*done=*/true,
11713 /*address_p=*/false,
11714 /*template_arg_p=*/false,
11715 &error_msg,
11716 capture_token->location);
11718 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11720 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11721 cp_lexer_consume_token (parser->lexer);
11722 capture_init_expr = make_pack_expansion (capture_init_expr);
11723 if (init_pack_expansion)
11725 /* If what follows is an initializer, the second '...' is
11726 invalid. But for cases like [...xs...], the first one
11727 is invalid. */
11728 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11729 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11730 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11731 ellipsis_loc = loc;
11732 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11733 continue;
11738 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11739 && !explicit_init_p)
11741 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11742 && capture_kind == BY_COPY)
11743 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11744 "of %qD redundant with by-copy capture default",
11745 capture_id);
11746 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11747 && capture_kind == BY_REFERENCE)
11748 pedwarn (capture_token->location, 0, "explicit by-reference "
11749 "capture of %qD redundant with by-reference capture "
11750 "default", capture_id);
11753 /* Check for duplicates.
11754 Optimize for the zero or one explicit captures cases and only create
11755 the hash_set after adding second capture. */
11756 bool found = false;
11757 if (!ids.is_empty ())
11758 found = ids.add (capture_id);
11759 else if (first_capture_id == NULL_TREE)
11760 first_capture_id = capture_id;
11761 else if (capture_id == first_capture_id)
11762 found = true;
11763 else
11765 ids.add (first_capture_id);
11766 ids.add (capture_id);
11768 if (found && explicit_init_p && id_equal (capture_id, "_"))
11769 found = false;
11770 if (found)
11771 pedwarn (input_location, 0,
11772 "already captured %qD in lambda expression", capture_id);
11773 else
11774 add_capture (lambda_expr, capture_id, capture_init_expr,
11775 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11776 explicit_init_p, &name_independent_cnt);
11778 /* If there is any qualification still in effect, clear it
11779 now; we will be starting fresh with the next capture. */
11780 parser->scope = NULL_TREE;
11781 parser->qualifying_scope = NULL_TREE;
11782 parser->object_scope = NULL_TREE;
11785 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11788 /* Parse the (optional) middle of a lambda expression.
11790 lambda-declarator:
11791 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11792 lambda-specifiers (C++23)
11794 lambda-specifiers:
11795 decl-specifier-seq [opt] noexcept-specifier [opt]
11796 attribute-specifier-seq [opt] trailing-return-type [opt]
11798 LAMBDA_EXPR is the current representation of the lambda expression. */
11800 static bool
11801 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11803 /* 5.1.1.4 of the standard says:
11804 If a lambda-expression does not include a lambda-declarator, it is as if
11805 the lambda-declarator were ().
11806 This means an empty parameter list, no attributes, and no exception
11807 specification. */
11808 tree param_list = void_list_node;
11809 tree std_attrs = NULL_TREE;
11810 tree gnu_attrs = NULL_TREE;
11811 tree exception_spec = NULL_TREE;
11812 tree template_param_list = NULL_TREE;
11813 tree tx_qual = NULL_TREE;
11814 tree return_type = NULL_TREE;
11815 tree trailing_requires_clause = NULL_TREE;
11816 bool has_param_list = false;
11817 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11818 cp_decl_specifier_seq lambda_specs;
11819 clear_decl_specs (&lambda_specs);
11820 /* A lambda op() is const unless explicitly 'mutable'. */
11821 cp_cv_quals quals = TYPE_QUAL_CONST;
11823 /* The template-parameter-list is optional, but must begin with
11824 an opening angle if present. */
11825 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11827 if (cxx_dialect < cxx20
11828 && (pedantic || cxx_dialect < cxx14))
11829 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11830 "lambda templates are only available with "
11831 "%<-std=c++20%> or %<-std=gnu++20%>");
11833 cp_lexer_consume_token (parser->lexer);
11835 template_param_list = cp_parser_template_parameter_list (parser);
11836 cp_parser_require_end_of_template_parameter_list (parser);
11838 /* We may have a constrained generic lambda; parse the requires-clause
11839 immediately after the template-parameter-list and combine with any
11840 shorthand constraints present. */
11841 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11842 if (flag_concepts)
11844 tree reqs = get_shorthand_constraints (current_template_parms);
11845 if (dreqs)
11846 reqs = combine_constraint_expressions (reqs, dreqs);
11847 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11850 /* We just processed one more parameter list. */
11851 ++parser->num_template_parameter_lists;
11854 /* Committee discussion supports allowing attributes here. */
11855 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11857 /* The parameter-declaration-clause is optional (unless
11858 template-parameter-list was given), but must begin with an
11859 opening parenthesis if present. */
11860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11862 matching_parens parens;
11863 parens.consume_open (parser);
11865 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11867 /* Parse parameters. */
11868 param_list
11869 = cp_parser_parameter_declaration_clause
11870 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11872 /* Default arguments shall not be specified in the
11873 parameter-declaration-clause of a lambda-declarator. */
11874 if (pedantic && cxx_dialect < cxx14)
11875 for (tree t = param_list; t; t = TREE_CHAIN (t))
11876 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11877 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11878 OPT_Wc__14_extensions,
11879 "default argument specified for lambda parameter");
11881 parens.require_close (parser);
11882 has_param_list = true;
11884 else if (cxx_dialect < cxx23)
11885 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11887 /* [expr.prim.lambda.general]
11888 lambda-specifier:
11889 consteval, constexpr, mutable, static
11890 [4] A lambda-specifier-seq shall contain at most one of each
11891 lambda-specifier and shall not contain both constexpr and consteval.
11892 The lambda-specifier-seq shall not contain both mutable and static. */
11893 int declares_class_or_enum;
11894 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11895 cp_parser_decl_specifier_seq (parser,
11896 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11897 &lambda_specs, &declares_class_or_enum);
11899 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11901 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11902 "parameter declaration before lambda declaration "
11903 "specifiers only optional with %<-std=c++2b%> or "
11904 "%<-std=gnu++2b%>");
11905 omitted_parms_loc = UNKNOWN_LOCATION;
11907 /* Peek at the params, see if we have an xobj parameter. */
11908 if (param_list && TREE_PURPOSE (param_list) == this_identifier)
11910 quals = TYPE_UNQUALIFIED;
11911 /* We still need grokdeclarator to see that this is an xobj function
11912 and finish the rest of the work, don't mutate it. */
11913 tree const xobj_param = TREE_VALUE (param_list);
11914 tree const param_type = TREE_TYPE (xobj_param);
11915 /* [expr.prim.lambda.closure-5]
11916 Given a lambda with a lambda-capture, the type of the explicit object
11917 parameter, if any, of the lambda's function call operator (possibly
11918 instantiated from a function call operator template) shall be either:
11919 -- the closure type,
11920 -- a class type derived from the closure type, or
11921 -- a reference to a possibly cv-qualified such type. */
11922 bool const unrelated_with_captures
11923 = (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11924 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11925 /* Since a lambda's type is anonymous, we can assume an xobj
11926 parameter is unrelated to the closure if it is non-dependent.
11927 If it is dependent we handle it at instantiation time. */
11928 && !WILDCARD_TYPE_P (non_reference (param_type));
11929 if (unrelated_with_captures)
11931 error_at (DECL_SOURCE_LOCATION (xobj_param),
11932 "a lambda with captures may not have an explicit object "
11933 "parameter of an unrelated type");
11934 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = NULL_TREE;
11937 /* [expr.prim.lambda.general-4]
11938 If the lambda-declarator contains an explicit object parameter
11939 ([dcl.fct]), then no lambda-specifier in the lambda-specifier-seq
11940 shall be mutable or static. */
11941 if (lambda_specs.storage_class == sc_mutable)
11943 auto_diagnostic_group d;
11944 error_at (lambda_specs.locations[ds_storage_class],
11945 "%<mutable%> lambda specifier "
11946 "with explicit object parameter");
11947 /* Tell the user how to do what they probably meant, maybe fixits
11948 would be appropriate later? */
11949 if (unrelated_with_captures)
11950 /* The following hints don't make sense when we already have an
11951 unrelated type with captures, don't emit them. */;
11952 else if (!TYPE_REF_P (param_type))
11953 inform (DECL_SOURCE_LOCATION (xobj_param),
11954 "the passed in closure object will not be mutated because "
11955 "it is taken by value");
11956 else if (TYPE_READONLY (TREE_TYPE (param_type)))
11957 inform (DECL_SOURCE_LOCATION (xobj_param),
11958 "declare the explicit object parameter as non-const "
11959 "reference instead");
11960 else
11961 inform (DECL_SOURCE_LOCATION (xobj_param),
11962 "explicit object parameter is already a mutable "
11963 "reference");
11965 else if (lambda_specs.storage_class == sc_static)
11967 auto_diagnostic_group d;
11968 error_at (lambda_specs.locations[ds_storage_class],
11969 "%<static%> lambda specifier "
11970 "with explicit object parameter");
11971 inform (DECL_SOURCE_LOCATION (xobj_param),
11972 "explicit object parameter declared here");
11975 else if (lambda_specs.storage_class == sc_mutable)
11977 quals = TYPE_UNQUALIFIED;
11979 else if (lambda_specs.storage_class == sc_static)
11981 /* [expr.prim.lambda.general-4]
11982 If the lambda-specifier-seq contains static, there shall be no
11983 lambda-capture. */
11984 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11985 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11986 error_at (lambda_specs.locations[ds_storage_class],
11987 "%<static%> lambda specifier with lambda capture");
11988 else
11990 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11991 quals = TYPE_UNQUALIFIED;
11995 tx_qual = cp_parser_tx_qualifier_opt (parser);
11996 if (omitted_parms_loc && tx_qual)
11998 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11999 "parameter declaration before lambda transaction "
12000 "qualifier only optional with %<-std=c++2b%> or "
12001 "%<-std=gnu++2b%>");
12002 omitted_parms_loc = UNKNOWN_LOCATION;
12005 /* Parse optional exception specification. */
12006 exception_spec
12007 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
12009 if (omitted_parms_loc && exception_spec)
12011 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12012 "parameter declaration before lambda exception "
12013 "specification only optional with %<-std=c++2b%> or "
12014 "%<-std=gnu++2b%>");
12015 omitted_parms_loc = UNKNOWN_LOCATION;
12018 /* GCC 8 accepted attributes here, and this is the place for standard C++11
12019 attributes that appertain to the function type. */
12020 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12021 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
12022 else
12023 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12025 /* Parse optional trailing return type. */
12026 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
12028 if (omitted_parms_loc)
12029 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12030 "parameter declaration before lambda trailing "
12031 "return type only optional with %<-std=c++2b%> or "
12032 "%<-std=gnu++2b%>");
12033 cp_lexer_consume_token (parser->lexer);
12034 return_type = cp_parser_trailing_type_id (parser);
12037 /* Also allow GNU attributes at the very end of the declaration, the usual
12038 place for GNU attributes. */
12039 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12040 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
12042 if (has_param_list)
12044 /* Parse optional trailing requires clause. */
12045 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
12047 /* The function parameters must be in scope all the way until after the
12048 trailing-return-type in case of decltype. */
12049 pop_bindings_and_leave_scope ();
12052 /* Create the function call operator.
12054 Messing with declarators like this is no uglier than building up the
12055 FUNCTION_DECL by hand, and this is less likely to get out of sync with
12056 other code. */
12058 cp_decl_specifier_seq return_type_specs;
12059 cp_declarator* declarator;
12060 tree fco;
12061 void *p;
12063 clear_decl_specs (&return_type_specs);
12064 return_type_specs.type = make_auto ();
12066 if (lambda_specs.locations[ds_constexpr])
12068 if (cxx_dialect >= cxx17)
12069 return_type_specs.locations[ds_constexpr]
12070 = lambda_specs.locations[ds_constexpr];
12071 else
12072 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
12073 "lambda only available with %<-std=c++17%> or "
12074 "%<-std=gnu++17%>");
12076 if (lambda_specs.locations[ds_consteval])
12077 return_type_specs.locations[ds_consteval]
12078 = lambda_specs.locations[ds_consteval];
12079 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
12081 return_type_specs.storage_class = sc_static;
12082 return_type_specs.locations[ds_storage_class]
12083 = lambda_specs.locations[ds_storage_class];
12086 p = obstack_alloc (&declarator_obstack, 0);
12088 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
12089 LAMBDA_EXPR_LOCATION (lambda_expr));
12091 declarator = make_call_declarator (declarator, param_list, quals,
12092 VIRT_SPEC_UNSPECIFIED,
12093 REF_QUAL_NONE,
12094 tx_qual,
12095 exception_spec,
12096 return_type,
12097 trailing_requires_clause,
12098 std_attrs,
12099 UNKNOWN_LOCATION);
12101 fco = grokmethod (&return_type_specs,
12102 declarator,
12103 chainon (gnu_attrs, lambda_specs.attributes));
12104 if (fco != error_mark_node)
12106 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
12107 DECL_ARTIFICIAL (fco) = 1;
12108 if (DECL_IOBJ_MEMBER_FUNCTION_P (fco))
12109 /* Give the object parameter a different name. */
12110 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
12111 DECL_SET_LAMBDA_FUNCTION (fco, true);
12113 if (template_param_list)
12115 fco = finish_member_template_decl (fco);
12116 finish_template_decl (template_param_list);
12117 --parser->num_template_parameter_lists;
12119 else if (parser->fully_implicit_function_template_p)
12120 fco = finish_fully_implicit_template (parser, fco);
12122 finish_member_declaration (fco);
12123 record_lambda_scope_sig_discriminator (lambda_expr, fco);
12125 obstack_free (&declarator_obstack, p);
12127 return (fco != error_mark_node);
12131 /* Parse the body of a lambda expression, which is simply
12133 compound-statement
12135 but which requires special handling.
12136 LAMBDA_EXPR is the current representation of the lambda expression. */
12138 static void
12139 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
12141 bool nested = (current_function_decl != NULL_TREE);
12142 unsigned char local_variables_forbidden_p
12143 = parser->local_variables_forbidden_p;
12144 bool in_function_body = parser->in_function_body;
12146 /* The body of a lambda-expression is not a subexpression of the enclosing
12147 expression. */
12148 cp_evaluated ev;
12150 if (nested)
12151 push_function_context ();
12152 else
12153 /* Still increment function_depth so that we don't GC in the
12154 middle of an expression. */
12155 ++function_depth;
12157 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
12158 auto ord = make_temp_override (parser->oacc_routine, NULL);
12159 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
12160 vec<tree> omp_privatization_save;
12161 save_omp_privatization_clauses (omp_privatization_save);
12162 /* Clear this in case we're in the middle of a default argument. */
12163 parser->local_variables_forbidden_p = 0;
12164 parser->in_function_body = true;
12167 local_specialization_stack s (lss_copy);
12168 tree fco = lambda_function (lambda_expr);
12169 tree body = start_lambda_function (fco, lambda_expr);
12171 /* Originally C++11 required us to peek for 'return expr'; and
12172 process it specially here to deduce the return type. N3638
12173 removed the need for that. */
12174 cp_parser_function_body (parser, false);
12176 finish_lambda_function (body);
12179 restore_omp_privatization_clauses (omp_privatization_save);
12180 parser->local_variables_forbidden_p = local_variables_forbidden_p;
12181 parser->in_function_body = in_function_body;
12182 if (nested)
12183 pop_function_context();
12184 else
12185 --function_depth;
12188 /* Statements [gram.stmt.stmt] */
12190 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
12192 static void
12193 add_debug_begin_stmt (location_t loc)
12195 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
12196 return;
12197 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
12198 /* A concept is never expanded normally. */
12199 return;
12201 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12202 SET_EXPR_LOCATION (stmt, loc);
12203 add_stmt (stmt);
12206 struct cp_omp_attribute_data
12208 cp_token_cache *tokens;
12209 const c_omp_directive *dir;
12210 c_omp_directive_kind kind;
12213 /* Handle omp::directive and omp::sequence attributes in ATTRS
12214 (if any) at the start of a statement or in attribute-declaration. */
12216 static tree
12217 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12219 if (!flag_openmp && !flag_openmp_simd)
12220 return attrs;
12222 auto_vec<cp_omp_attribute_data, 16> vec;
12223 int cnt = 0;
12224 int tokens = 0;
12225 bool bad = false;
12226 for (tree *pa = &attrs; *pa; )
12227 if (get_attribute_namespace (*pa) == omp_identifier
12228 && is_attribute_p ("directive", get_attribute_name (*pa)))
12230 cnt++;
12231 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12233 tree d = TREE_VALUE (a);
12234 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12235 cp_token *first = DEFPARSE_TOKENS (d)->first;
12236 cp_token *last = DEFPARSE_TOKENS (d)->last;
12237 if (parser->omp_attrs_forbidden_p)
12239 error_at (first->location,
12240 "mixing OpenMP directives with attribute and pragma "
12241 "syntax on the same statement");
12242 parser->omp_attrs_forbidden_p = false;
12243 bad = true;
12245 else if (TREE_PUBLIC (d))
12247 error_at (first->location,
12248 "OpenMP %<omp::decl%> attribute on a statement");
12249 bad = true;
12251 const char *directive[3] = {};
12252 for (int i = 0; i < 3; i++)
12254 tree id = NULL_TREE;
12255 if (first + i == last)
12256 break;
12257 if (first[i].type == CPP_NAME)
12258 id = first[i].u.value;
12259 else if (first[i].type == CPP_KEYWORD)
12260 id = ridpointers[(int) first[i].keyword];
12261 else
12262 break;
12263 directive[i] = IDENTIFIER_POINTER (id);
12265 const c_omp_directive *dir = NULL;
12266 if (directive[0])
12267 dir = c_omp_categorize_directive (directive[0], directive[1],
12268 directive[2]);
12269 if (dir == NULL)
12271 error_at (first->location,
12272 "unknown OpenMP directive name in %qs attribute "
12273 "argument",
12274 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12275 continue;
12277 c_omp_directive_kind kind = dir->kind;
12278 if (dir->id == PRAGMA_OMP_ORDERED)
12280 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12281 depend/doacross clause. */
12282 if (directive[1]
12283 && (strcmp (directive[1], "depend") == 0
12284 || strcmp (directive[1], "doacross") == 0))
12285 kind = C_OMP_DIR_STANDALONE;
12286 else if (first + 2 < last
12287 && first[1].type == CPP_COMMA
12288 && first[2].type == CPP_NAME
12289 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12290 "depend") == 0
12291 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12292 "doacross") == 0))
12293 kind = C_OMP_DIR_STANDALONE;
12295 else if (dir->id == PRAGMA_OMP_ERROR)
12297 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12298 int paren_depth = 0;
12299 for (int i = 1; first + i < last; i++)
12300 if (first[i].type == CPP_OPEN_PAREN)
12301 paren_depth++;
12302 else if (first[i].type == CPP_CLOSE_PAREN)
12303 paren_depth--;
12304 else if (paren_depth == 0
12305 && first + i + 2 < last
12306 && first[i].type == CPP_NAME
12307 && first[i + 1].type == CPP_OPEN_PAREN
12308 && first[i + 2].type == CPP_NAME
12309 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12310 "at")
12311 && !strcmp (IDENTIFIER_POINTER (first[i
12312 + 2].u.value),
12313 "execution"))
12315 kind = C_OMP_DIR_STANDALONE;
12316 break;
12319 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12320 vec.safe_push (v);
12321 if (flag_openmp || dir->simd)
12322 tokens += (last - first) + 1;
12324 cp_omp_attribute_data v = {};
12325 vec.safe_push (v);
12326 *pa = TREE_CHAIN (*pa);
12328 else
12329 pa = &TREE_CHAIN (*pa);
12331 if (bad)
12332 return attrs;
12334 unsigned int i;
12335 cp_omp_attribute_data *v;
12336 cp_omp_attribute_data *construct_seen = nullptr;
12337 cp_omp_attribute_data *standalone_seen = nullptr;
12338 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12339 FOR_EACH_VEC_ELT (vec, i, v)
12340 if (v->tokens)
12342 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12343 construct_seen = v;
12344 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12345 standalone_seen = v;
12347 else
12349 if (standalone_seen && !prev_standalone_seen)
12351 prev_standalone_seen = standalone_seen;
12352 standalone_seen = nullptr;
12356 if (cnt > 1 && construct_seen)
12358 error_at (construct_seen->tokens->first->location,
12359 "OpenMP construct among %<omp::directive%> attributes"
12360 " requires all %<omp::directive%> attributes on the"
12361 " same statement to be in the same %<omp::sequence%>");
12362 return attrs;
12364 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12366 error_at (standalone_seen->tokens->first->location,
12367 "multiple OpenMP standalone directives among"
12368 " %<omp::directive%> attributes must be all within the"
12369 " same %<omp::sequence%>");
12370 return attrs;
12373 if (prev_standalone_seen)
12374 standalone_seen = prev_standalone_seen;
12375 if (standalone_seen
12376 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12378 error_at (standalone_seen->tokens->first->location,
12379 "standalone OpenMP directives in %<omp::directive%> attribute"
12380 " can only appear on an empty statement");
12381 return attrs;
12383 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12385 cp_token *token = cp_lexer_peek_token (parser->lexer);
12386 enum pragma_kind kind = cp_parser_pragma_kind (token);
12387 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12389 error_at (token->location,
12390 "mixing OpenMP directives with attribute and pragma "
12391 "syntax on the same statement");
12392 return attrs;
12396 if (!tokens)
12397 return attrs;
12398 tokens++;
12399 cp_lexer *lexer = cp_lexer_alloc ();
12400 lexer->debugging_p = parser->lexer->debugging_p;
12401 vec_safe_reserve (lexer->buffer, tokens, true);
12402 FOR_EACH_VEC_ELT (vec, i, v)
12404 if (!v->tokens)
12405 continue;
12406 if (!flag_openmp && !v->dir->simd)
12407 continue;
12408 cp_token *first = v->tokens->first;
12409 cp_token *last = v->tokens->last;
12410 cp_token tok = {};
12411 tok.type = CPP_PRAGMA;
12412 tok.keyword = RID_MAX;
12413 tok.u.value = build_int_cst (NULL, v->dir->id);
12414 tok.location = first->location;
12415 lexer->buffer->quick_push (tok);
12416 while (++first < last)
12417 lexer->buffer->quick_push (*first);
12418 tok = {};
12419 tok.type = CPP_PRAGMA_EOL;
12420 tok.keyword = RID_MAX;
12421 tok.location = last->location;
12422 lexer->buffer->quick_push (tok);
12424 cp_token tok = {};
12425 tok.type = CPP_EOF;
12426 tok.keyword = RID_MAX;
12427 tok.location = lexer->buffer->last ().location;
12428 lexer->buffer->quick_push (tok);
12429 lexer->next = parser->lexer;
12430 lexer->next_token = lexer->buffer->address ();
12431 lexer->last_token = lexer->next_token
12432 + lexer->buffer->length ()
12433 - 1;
12434 lexer->in_omp_attribute_pragma = true;
12435 parser->lexer = lexer;
12436 /* Move the current source position to that of the first token in the
12437 new lexer. */
12438 cp_lexer_set_source_position_from_token (lexer->next_token);
12439 return attrs;
12442 /* True if and only if the name is one of the contract types. */
12444 static bool
12445 contract_attribute_p (const_tree id)
12447 return is_attribute_p ("assert", id)
12448 || is_attribute_p ("pre", id)
12449 || is_attribute_p ("post", id);
12452 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12453 (if any) at the start or after declaration-id of a declaration. */
12455 static void
12456 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12457 cp_omp_declare_simd_data *data,
12458 bool start)
12460 if (!flag_openmp && !flag_openmp_simd)
12461 return;
12463 int cnt = 0;
12464 bool bad = false;
12465 bool variant_p = false;
12466 location_t loc = UNKNOWN_LOCATION;
12467 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12468 if (get_attribute_namespace (pa) == omp_identifier
12469 && is_attribute_p ("directive", get_attribute_name (pa)))
12471 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12473 tree d = TREE_VALUE (a);
12474 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12475 cp_token *first = DEFPARSE_TOKENS (d)->first;
12476 cp_token *last = DEFPARSE_TOKENS (d)->last;
12477 const char *directive[3] = {};
12478 for (int i = 0; i < 3; i++)
12480 tree id = NULL_TREE;
12481 if (first + i == last)
12482 break;
12483 if (first[i].type == CPP_NAME)
12484 id = first[i].u.value;
12485 else if (first[i].type == CPP_KEYWORD)
12486 id = ridpointers[(int) first[i].keyword];
12487 else
12488 break;
12489 directive[i] = IDENTIFIER_POINTER (id);
12491 const c_omp_directive *dir = NULL;
12492 if (directive[0])
12493 dir = c_omp_categorize_directive (directive[0], directive[1],
12494 directive[2]);
12495 if (dir == NULL)
12496 continue;
12497 if (dir->id == PRAGMA_OMP_DECLARE
12498 && (strcmp (directive[1], "simd") == 0
12499 || strcmp (directive[1], "variant") == 0))
12501 if (cnt++ == 0)
12503 variant_p = strcmp (directive[1], "variant") == 0;
12504 loc = first->location;
12506 if (start && parser->omp_declare_simd && !bad)
12508 error_at (first->location,
12509 "mixing OpenMP directives with attribute and "
12510 "pragma syntax on the same declaration");
12511 bad = true;
12517 if (bad)
12519 for (tree *pa = pattrs; *pa; )
12520 if (get_attribute_namespace (*pa) == omp_identifier
12521 && is_attribute_p ("directive", get_attribute_name (*pa)))
12522 *pa = TREE_CHAIN (*pa);
12523 else
12524 pa = &TREE_CHAIN (*pa);
12525 return;
12527 if (cnt == 0)
12528 return;
12530 if (parser->omp_declare_simd == NULL)
12532 data->error_seen = false;
12533 data->fndecl_seen = false;
12534 data->variant_p = variant_p;
12535 data->loc = loc;
12536 data->tokens = vNULL;
12537 data->attribs[0] = NULL;
12538 data->attribs[1] = NULL;
12539 parser->omp_declare_simd = data;
12541 parser->omp_declare_simd->attribs[!start] = pattrs;
12544 /* Parse a statement.
12546 statement:
12547 labeled-statement
12548 expression-statement
12549 compound-statement
12550 selection-statement
12551 iteration-statement
12552 jump-statement
12553 declaration-statement
12554 try-block
12556 C++11:
12558 statement:
12559 labeled-statement
12560 attribute-specifier-seq (opt) expression-statement
12561 attribute-specifier-seq (opt) compound-statement
12562 attribute-specifier-seq (opt) selection-statement
12563 attribute-specifier-seq (opt) iteration-statement
12564 attribute-specifier-seq (opt) jump-statement
12565 declaration-statement
12566 attribute-specifier-seq (opt) try-block
12568 init-statement:
12569 expression-statement
12570 simple-declaration
12571 alias-declaration
12573 TM Extension:
12575 statement:
12576 atomic-statement
12578 IN_COMPOUND is true when the statement is nested inside a
12579 cp_parser_compound_statement.
12581 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12582 is a (possibly labeled) if statement which is not enclosed in braces
12583 and has an else clause. This is used to implement -Wparentheses.
12585 CHAIN is a vector of if-else-if conditions.
12587 Note that this version of parsing restricts assertions to be attached to
12588 empty statements. */
12590 static void
12591 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12592 const bool in_compound, bool *if_p, vec<tree> *chain,
12593 location_t *loc_after_labels)
12595 tree statement, std_attrs = NULL_TREE;
12596 cp_token *token;
12597 location_t statement_location, attrs_loc;
12598 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12599 bool has_std_attrs;
12600 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12601 This matters for certain pragmas. */
12602 bool in_compound_for_pragma = in_compound;
12604 restart:
12605 if (if_p != NULL)
12606 *if_p = false;
12607 /* There is no statement yet. */
12608 statement = NULL_TREE;
12610 saved_token_sentinel saved_tokens (parser->lexer);
12611 token = cp_lexer_peek_token (parser->lexer);
12612 attrs_loc = token->location;
12613 if (c_dialect_objc ())
12614 /* In obj-c++, seeing '[[' might be the either the beginning of
12615 c++11 attributes, or a nested objc-message-expression. So
12616 let's parse the c++11 attributes tentatively. */
12617 cp_parser_parse_tentatively (parser);
12618 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12619 if (std_attrs)
12620 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12621 if (c_dialect_objc ())
12623 if (!cp_parser_parse_definitely (parser))
12624 std_attrs = NULL_TREE;
12626 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12628 /* Peek at the next token. */
12629 token = cp_lexer_peek_token (parser->lexer);
12631 /* If we have contracts, check that they're valid in this context. */
12632 if (std_attrs != error_mark_node)
12634 if (tree pre = lookup_attribute ("pre", std_attrs))
12635 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12636 "preconditions cannot be statements");
12637 else if (tree post = lookup_attribute ("post", std_attrs))
12638 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12639 "postconditions cannot be statements");
12641 /* Check that assertions are null statements. */
12642 if (cp_contract_assertion_p (std_attrs))
12643 if (token->type != CPP_SEMICOLON)
12644 error_at (token->location, "assertions must be followed by %<;%>");
12647 bool omp_attrs_forbidden_p;
12648 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12650 if (std_attrs && (flag_openmp || flag_openmp_simd))
12652 bool handle_omp_attribs = false;
12653 if (token->type == CPP_KEYWORD)
12654 switch (token->keyword)
12656 case RID_IF:
12657 case RID_SWITCH:
12658 case RID_WHILE:
12659 case RID_DO:
12660 case RID_FOR:
12661 case RID_BREAK:
12662 case RID_CONTINUE:
12663 case RID_RETURN:
12664 case RID_CO_RETURN:
12665 case RID_GOTO:
12666 case RID_AT_TRY:
12667 case RID_AT_CATCH:
12668 case RID_AT_FINALLY:
12669 case RID_AT_SYNCHRONIZED:
12670 case RID_AT_THROW:
12671 case RID_TRY:
12672 case RID_TRANSACTION_ATOMIC:
12673 case RID_TRANSACTION_RELAXED:
12674 case RID_SYNCHRONIZED:
12675 case RID_ATOMIC_NOEXCEPT:
12676 case RID_ATOMIC_CANCEL:
12677 case RID_TRANSACTION_CANCEL:
12678 handle_omp_attribs = true;
12679 break;
12680 default:
12681 break;
12683 else if (token->type == CPP_SEMICOLON
12684 || token->type == CPP_OPEN_BRACE
12685 || token->type == CPP_PRAGMA)
12686 handle_omp_attribs = true;
12687 if (handle_omp_attribs)
12689 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12690 std_attrs);
12691 token = cp_lexer_peek_token (parser->lexer);
12694 parser->omp_attrs_forbidden_p = false;
12696 /* Remember the location of the first token in the statement. */
12697 cp_token *statement_token = token;
12698 statement_location = token->location;
12699 add_debug_begin_stmt (statement_location);
12700 /* If this is a keyword, then that will often determine what kind of
12701 statement we have. */
12702 if (token->type == CPP_KEYWORD)
12704 enum rid keyword = token->keyword;
12706 switch (keyword)
12708 case RID_CASE:
12709 case RID_DEFAULT:
12710 /* Looks like a labeled-statement with a case label.
12711 Parse the label, and then use tail recursion to parse
12712 the statement. */
12713 cp_parser_label_for_labeled_statement (parser, std_attrs);
12714 in_compound_for_pragma = false;
12715 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12716 goto restart;
12718 case RID_IF:
12719 case RID_SWITCH:
12720 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12721 statement = cp_parser_selection_statement (parser, if_p, chain);
12722 break;
12724 case RID_WHILE:
12725 case RID_DO:
12726 case RID_FOR:
12727 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12728 statement = cp_parser_iteration_statement (parser, if_p, false,
12729 NULL_TREE, false);
12730 break;
12732 case RID_BREAK:
12733 case RID_CONTINUE:
12734 case RID_RETURN:
12735 case RID_CO_RETURN:
12736 case RID_GOTO:
12737 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12738 statement = cp_parser_jump_statement (parser);
12739 break;
12741 /* Objective-C++ exception-handling constructs. */
12742 case RID_AT_TRY:
12743 case RID_AT_CATCH:
12744 case RID_AT_FINALLY:
12745 case RID_AT_SYNCHRONIZED:
12746 case RID_AT_THROW:
12747 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12748 statement = cp_parser_objc_statement (parser);
12749 break;
12751 case RID_TRY:
12752 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12753 statement = cp_parser_try_block (parser);
12754 break;
12756 case RID_NAMESPACE:
12757 /* This must be a namespace alias definition. */
12758 if (has_std_attrs)
12760 /* Attributes should be parsed as part of the
12761 declaration, so let's un-parse them. */
12762 saved_tokens.rollback();
12763 std_attrs = NULL_TREE;
12765 cp_parser_declaration_statement (parser);
12766 return;
12768 case RID_TRANSACTION_ATOMIC:
12769 case RID_TRANSACTION_RELAXED:
12770 case RID_SYNCHRONIZED:
12771 case RID_ATOMIC_NOEXCEPT:
12772 case RID_ATOMIC_CANCEL:
12773 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12774 statement = cp_parser_transaction (parser, token);
12775 break;
12776 case RID_TRANSACTION_CANCEL:
12777 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12778 statement = cp_parser_transaction_cancel (parser);
12779 break;
12781 default:
12782 /* It might be a keyword like `int' that can start a
12783 declaration-statement. */
12784 break;
12787 else if (token->type == CPP_NAME)
12789 /* If the next token is a `:', then we are looking at a
12790 labeled-statement. */
12791 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12792 if (token->type == CPP_COLON)
12794 /* Looks like a labeled-statement with an ordinary label.
12795 Parse the label, and then use tail recursion to parse
12796 the statement. */
12798 cp_parser_label_for_labeled_statement (parser, std_attrs);
12800 /* If there's no statement, it's not a labeled-statement, just
12801 a label. That's allowed in C++23, but only if we're at the
12802 end of a compound-statement. */
12803 if (in_compound
12804 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12806 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12807 if (cxx_dialect < cxx23)
12808 pedwarn (loc, OPT_Wc__23_extensions,
12809 "label at end of compound statement only available "
12810 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12811 return;
12813 in_compound_for_pragma = false;
12814 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12815 goto restart;
12818 /* Anything that starts with a `{' must be a compound-statement. */
12819 else if (token->type == CPP_OPEN_BRACE)
12821 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12822 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12824 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12825 a statement all its own. */
12826 else if (token->type == CPP_PRAGMA)
12828 do_pragma:;
12829 cp_lexer *lexer = parser->lexer;
12830 bool do_restart = false;
12831 /* Only certain OpenMP pragmas are attached to statements, and thus
12832 are considered statements themselves. All others are not. In
12833 the context of a compound, accept the pragma as a "statement" and
12834 return so that we can check for a close brace. Otherwise we
12835 require a real statement and must go back and read one. */
12836 if (in_compound_for_pragma)
12838 if (cp_parser_pragma (parser, pragma_compound, if_p)
12839 && parser->omp_for_parse_state)
12840 check_omp_intervening_code (parser);
12842 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12843 do_restart = true;
12844 else if (parser->omp_for_parse_state)
12845 check_omp_intervening_code (parser);
12846 if (parser->lexer != lexer
12847 && lexer->in_omp_attribute_pragma
12848 && (!in_omp_attribute_pragma || lexer->orphan_p))
12850 if (saved_tokens.lexer == lexer)
12852 if (saved_tokens.mode == STS_COMMIT)
12853 cp_lexer_commit_tokens (lexer);
12854 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12855 saved_tokens.lexer = parser->lexer;
12856 saved_tokens.mode = STS_DONOTHING;
12857 saved_tokens.len = parser->lexer->saved_tokens.length ();
12859 cp_lexer_destroy (lexer);
12860 lexer = parser->lexer;
12862 if (do_restart)
12863 goto restart;
12864 if (parser->lexer == lexer
12865 && lexer->in_omp_attribute_pragma
12866 && !in_omp_attribute_pragma)
12867 parser->lexer->orphan_p = true;
12868 return;
12870 else if (token->type == CPP_EOF)
12872 cp_parser_error (parser, "expected statement");
12873 return;
12876 /* Everything else must be a declaration-statement or an
12877 expression-statement. Try for the declaration-statement
12878 first, unless we are looking at a `;', in which case we know that
12879 we have an expression-statement. */
12880 if (!statement)
12882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12884 if (has_std_attrs)
12885 /* Attributes should be parsed as part of the declaration,
12886 so let's un-parse them. */
12887 saved_tokens.rollback();
12889 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12890 cp_parser_parse_tentatively (parser);
12891 /* Try to parse the declaration-statement. */
12892 cp_parser_declaration_statement (parser);
12893 parser->omp_attrs_forbidden_p = false;
12894 /* If that worked, we're done. */
12895 if (cp_parser_parse_definitely (parser))
12896 return;
12897 /* It didn't work, restore the post-attribute position. */
12898 if (has_std_attrs)
12900 cp_lexer_set_token_position (parser->lexer, statement_token);
12901 if (flag_openmp || flag_openmp_simd)
12903 size_t i = 1;
12904 bool handle_omp_attribs = true;
12905 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12906 == RID_EXTENSION)
12907 i++;
12908 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12910 case RID_ASM:
12911 case RID_NAMESPACE:
12912 case RID_USING:
12913 case RID_LABEL:
12914 case RID_STATIC_ASSERT:
12915 /* Don't handle OpenMP attribs on keywords that
12916 always start a declaration statement but don't
12917 accept attribute before it and therefore
12918 the tentative cp_parser_declaration_statement
12919 fails to parse because of that. */
12920 handle_omp_attribs = false;
12921 break;
12922 default:
12923 break;
12926 if (handle_omp_attribs)
12928 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12929 std_attrs
12930 = cp_parser_handle_statement_omp_attributes
12931 (parser, std_attrs);
12932 parser->omp_attrs_forbidden_p = false;
12933 token = cp_lexer_peek_token (parser->lexer);
12934 if (token->type == CPP_PRAGMA)
12935 goto do_pragma;
12940 /* All preceding labels have been parsed at this point. */
12941 if (loc_after_labels != NULL)
12942 *loc_after_labels = statement_location;
12944 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12946 /* Look for an expression-statement instead. */
12947 statement = cp_parser_expression_statement (parser, in_statement_expr);
12949 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12950 attrs_loc);
12952 /* Handle [[fallthrough]];. */
12953 if (attribute_fallthrough_p (std_attrs))
12955 /* The next token after the fallthrough attribute is ';'. */
12956 if (statement == NULL_TREE)
12958 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12959 statement = build_call_expr_internal_loc (statement_location,
12960 IFN_FALLTHROUGH,
12961 void_type_node, 0);
12962 finish_expr_stmt (statement);
12964 else
12965 warning_at (statement_location, OPT_Wattributes,
12966 "%<fallthrough%> attribute not followed by %<;%>");
12967 std_attrs = NULL_TREE;
12970 /* Handle [[assert: ...]]; */
12971 if (cp_contract_assertion_p (std_attrs))
12973 /* Add the assertion as a statement in the current block. */
12974 gcc_assert (!statement || statement == error_mark_node);
12975 emit_assertion (std_attrs);
12976 std_attrs = NULL_TREE;
12980 /* Set the line number for the statement. */
12981 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12982 SET_EXPR_LOCATION (statement, statement_location);
12984 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12985 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
12986 warning_at (attrs_loc, OPT_Wattributes,
12987 "attributes at the beginning of statement are ignored");
12990 /* Append ATTR to attribute list ATTRS. */
12992 tree
12993 attr_chainon (tree attrs, tree attr)
12995 if (attrs == error_mark_node)
12996 return error_mark_node;
12997 if (attr == error_mark_node)
12998 return error_mark_node;
12999 return chainon (attrs, attr);
13002 /* Parse the label for a labeled-statement, i.e.
13004 label:
13005 attribute-specifier-seq[opt] identifier :
13006 attribute-specifier-seq[opt] case constant-expression :
13007 attribute-specifier-seq[opt] default :
13009 labeled-statement:
13010 label statement
13012 GNU Extension:
13013 case constant-expression ... constant-expression : statement
13015 When a label is parsed without errors, the label is added to the
13016 parse tree by the finish_* functions, so this function doesn't
13017 have to return the label. */
13019 static void
13020 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
13022 cp_token *token;
13023 tree label = NULL_TREE;
13024 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13026 /* The next token should be an identifier. */
13027 token = cp_lexer_peek_token (parser->lexer);
13028 if (token->type != CPP_NAME
13029 && token->type != CPP_KEYWORD)
13031 cp_parser_error (parser, "expected labeled-statement");
13032 return;
13035 /* Remember whether this case or a user-defined label is allowed to fall
13036 through to. */
13037 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
13039 parser->colon_corrects_to_scope_p = false;
13040 switch (token->keyword)
13042 case RID_CASE:
13044 tree expr, expr_hi;
13045 cp_token *ellipsis;
13047 /* Consume the `case' token. */
13048 cp_lexer_consume_token (parser->lexer);
13049 /* Parse the constant-expression. */
13050 expr = cp_parser_constant_expression (parser);
13051 if (check_for_bare_parameter_packs (expr))
13052 expr = error_mark_node;
13054 ellipsis = cp_lexer_peek_token (parser->lexer);
13055 if (ellipsis->type == CPP_ELLIPSIS)
13057 /* Consume the `...' token. */
13058 cp_lexer_consume_token (parser->lexer);
13059 expr_hi = cp_parser_constant_expression (parser);
13060 if (check_for_bare_parameter_packs (expr_hi))
13061 expr_hi = error_mark_node;
13063 /* We don't need to emit warnings here, as the common code
13064 will do this for us. */
13066 else
13067 expr_hi = NULL_TREE;
13069 if (parser->in_switch_statement_p)
13071 tree l = finish_case_label (token->location, expr, expr_hi);
13072 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13074 label = CASE_LABEL (l);
13075 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13078 else
13079 error_at (token->location,
13080 "case label %qE not within a switch statement",
13081 expr);
13083 break;
13085 case RID_DEFAULT:
13086 /* Consume the `default' token. */
13087 cp_lexer_consume_token (parser->lexer);
13089 if (parser->in_switch_statement_p)
13091 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
13092 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13094 label = CASE_LABEL (l);
13095 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13098 else
13099 error_at (token->location, "case label not within a switch statement");
13100 break;
13102 default:
13103 /* Anything else must be an ordinary label. */
13104 label = finish_label_stmt (cp_parser_identifier (parser));
13105 if (label && TREE_CODE (label) == LABEL_DECL)
13106 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13107 break;
13110 /* Require the `:' token. */
13111 cp_parser_require (parser, CPP_COLON, RT_COLON);
13113 /* An ordinary label may optionally be followed by attributes.
13114 However, this is only permitted if the attributes are then
13115 followed by a semicolon. This is because, for backward
13116 compatibility, when parsing
13117 lab: __attribute__ ((unused)) int i;
13118 we want the attribute to attach to "i", not "lab". */
13119 if (label != NULL_TREE
13120 && cp_next_tokens_can_be_gnu_attribute_p (parser))
13122 tree attrs;
13123 cp_parser_parse_tentatively (parser);
13124 attrs = cp_parser_gnu_attributes_opt (parser);
13125 if (attrs == NULL_TREE
13126 /* And fallthrough always binds to the expression-statement. */
13127 || attribute_fallthrough_p (attrs)
13128 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13129 cp_parser_abort_tentative_parse (parser);
13130 else if (!cp_parser_parse_definitely (parser))
13132 else
13133 attributes = attr_chainon (attributes, attrs);
13136 if (attributes != NULL_TREE)
13137 cplus_decl_attributes (&label, attributes, 0);
13139 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13142 /* Parse an expression-statement.
13144 expression-statement:
13145 expression [opt] ;
13147 Returns the new EXPR_STMT -- or NULL_TREE if the expression
13148 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
13149 indicates whether this expression-statement is part of an
13150 expression statement. */
13152 static tree
13153 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
13155 tree statement = NULL_TREE;
13156 cp_token *token = cp_lexer_peek_token (parser->lexer);
13157 location_t loc = token->location;
13159 /* There might be attribute fallthrough. */
13160 tree attr = cp_parser_gnu_attributes_opt (parser);
13162 /* If the next token is a ';', then there is no expression
13163 statement. */
13164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13166 statement = cp_parser_expression (parser);
13167 if (statement == error_mark_node
13168 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13170 /* If we ran into a problem, make sure we complained. */
13171 gcc_assert (seen_error ());
13173 cp_parser_skip_to_end_of_block_or_statement (parser);
13174 return error_mark_node;
13178 attr = process_stmt_assume_attribute (attr, statement, loc);
13180 /* Handle [[fallthrough]];. */
13181 if (attribute_fallthrough_p (attr))
13183 /* The next token after the fallthrough attribute is ';'. */
13184 if (statement == NULL_TREE)
13185 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
13186 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
13187 void_type_node, 0);
13188 else
13189 warning_at (loc, OPT_Wattributes,
13190 "%<fallthrough%> attribute not followed by %<;%>");
13191 attr = NULL_TREE;
13194 /* Allow "[[fallthrough]];", but warn otherwise. */
13195 if (attr != NULL_TREE && any_nonignored_attribute_p (attr))
13196 warning_at (loc, OPT_Wattributes,
13197 "attributes at the beginning of statement are ignored");
13199 /* Give a helpful message for "A<T>::type t;" and the like. */
13200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13201 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13203 if (TREE_CODE (statement) == SCOPE_REF)
13204 error_at (token->location, "need %<typename%> before %qE because "
13205 "%qT is a dependent scope",
13206 statement, TREE_OPERAND (statement, 0));
13207 else if (is_overloaded_fn (statement)
13208 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13210 /* A::A a; */
13211 tree fn = get_first_fn (statement);
13212 error_at (token->location,
13213 "%<%T::%D%> names the constructor, not the type",
13214 DECL_CONTEXT (fn), DECL_NAME (fn));
13218 /* Consume the final `;'. */
13219 cp_parser_consume_semicolon_at_end_of_statement (parser);
13221 if (in_statement_expr
13222 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13223 /* This is the final expression statement of a statement
13224 expression. */
13225 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13226 else if (statement)
13227 statement = finish_expr_stmt (statement);
13229 return statement;
13232 /* Parse a compound-statement.
13234 compound-statement:
13235 { statement-seq [opt] label-seq [opt] }
13237 label-seq:
13238 label
13239 label-seq label
13241 GNU extension:
13243 compound-statement:
13244 { label-declaration-seq [opt] statement-seq [opt] }
13246 label-declaration-seq:
13247 label-declaration
13248 label-declaration-seq label-declaration
13250 Returns a tree representing the statement. */
13252 static tree
13253 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13254 int bcs_flags, bool function_body)
13256 tree compound_stmt;
13257 matching_braces braces;
13259 /* Consume the `{'. */
13260 if (!braces.require_open (parser))
13261 return error_mark_node;
13262 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13263 && !function_body && cxx_dialect < cxx14)
13264 pedwarn (input_location, OPT_Wpedantic,
13265 "compound-statement in %<constexpr%> function");
13266 /* Begin the compound-statement. */
13267 compound_stmt = begin_compound_stmt (bcs_flags);
13268 /* If the next keyword is `__label__' we have a label declaration. */
13269 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13270 cp_parser_label_declaration (parser);
13271 /* Parse an (optional) statement-seq. */
13272 cp_parser_statement_seq_opt (parser, in_statement_expr);
13274 /* Consume the `}'. */
13275 braces.require_close (parser);
13277 /* Finish the compound-statement. */
13278 finish_compound_stmt (compound_stmt);
13280 return compound_stmt;
13283 /* Diagnose errors related to imperfectly nested loops in an OMP
13284 loop construct. This function is called when such code is seen.
13285 Only issue one such diagnostic no matter how much invalid
13286 intervening code there is in the loop.
13287 FIXME: maybe the location associated with the diagnostic should
13288 be the current parser token instead of the location of the outer loop
13289 nest. */
13291 static void
13292 check_omp_intervening_code (cp_parser *parser)
13294 struct omp_for_parse_data *omp_for_parse_state
13295 = parser->omp_for_parse_state;
13296 gcc_assert (omp_for_parse_state);
13298 if (!omp_for_parse_state->in_intervening_code)
13299 return;
13300 omp_for_parse_state->saw_intervening_code = true;
13302 /* Only diagnose errors related to perfect nesting once. */
13303 if (!omp_for_parse_state->perfect_nesting_fail)
13305 if (omp_for_parse_state->code == OACC_LOOP)
13307 error_at (omp_for_parse_state->for_loc,
13308 "inner loops must be perfectly nested in "
13309 "%<#pragma acc loop%>");
13310 omp_for_parse_state->perfect_nesting_fail = true;
13312 else if (omp_for_parse_state->ordered)
13314 error_at (omp_for_parse_state->for_loc,
13315 "inner loops must be perfectly nested with "
13316 "%<ordered%> clause");
13317 omp_for_parse_state->perfect_nesting_fail = true;
13319 else if (omp_for_parse_state->inscan)
13321 error_at (omp_for_parse_state->for_loc,
13322 "inner loops must be perfectly nested with "
13323 "%<reduction%> %<inscan%> clause");
13324 omp_for_parse_state->perfect_nesting_fail = true;
13326 /* TODO: Also reject loops with TILE directive. */
13327 if (omp_for_parse_state->perfect_nesting_fail)
13328 omp_for_parse_state->fail = true;
13332 /* Parse an (optional) statement-seq.
13334 statement-seq:
13335 statement
13336 statement-seq [opt] statement */
13338 static void
13339 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13341 struct omp_for_parse_data *omp_for_parse_state
13342 = parser->omp_for_parse_state;
13343 bool in_omp_loop_block
13344 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13346 /* Scan statements until there aren't any more. */
13347 while (true)
13349 cp_token *token = cp_lexer_peek_token (parser->lexer);
13351 /* If we are looking at a `}', then we have run out of
13352 statements; the same is true if we have reached the end
13353 of file, or have stumbled upon a stray '@end'. */
13354 if (token->type == CPP_CLOSE_BRACE
13355 || token->type == CPP_EOF
13356 || token->type == CPP_PRAGMA_EOL
13357 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13358 break;
13360 /* If we are in a compound statement and find 'else' then
13361 something went wrong. */
13362 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13364 if (parser->in_statement & IN_IF_STMT)
13365 break;
13366 else
13368 token = cp_lexer_consume_token (parser->lexer);
13369 error_at (token->location, "%<else%> without a previous %<if%>");
13373 /* Handle special cases for OMP FOR canonical loop syntax. */
13374 else if (in_omp_loop_block)
13376 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13377 if (want_nested_loop
13378 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13380 /* Found the nested loop. */
13381 omp_for_parse_state->depth++;
13382 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13383 omp_for_parse_state->depth--;
13385 else if (token->type == CPP_SEMICOLON)
13387 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13388 loop form, GCC used to accept an empty statements as not
13389 being intervening code. Continue to do that, as an
13390 extension. */
13391 /* FIXME: Maybe issue a warning or something here? */
13392 cp_lexer_consume_token (parser->lexer);
13394 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13395 /* The nested compound statement may contain the next loop, or
13396 it might just be intervening code. */
13398 cp_parser_statement (parser, in_statement_expr, true, NULL);
13399 if (omp_for_parse_state->want_nested_loop)
13400 check_omp_intervening_code (parser);
13402 else
13404 /* This must be intervening code. */
13405 omp_for_parse_state->want_nested_loop = false;
13406 /* Defer calling check_omp_intervening_code on pragmas until
13407 cp_parser_statement, because we can't know until we parse
13408 it whether or not the pragma is a statement. */
13409 if (token->type != CPP_PRAGMA)
13410 check_omp_intervening_code (parser);
13411 cp_parser_statement (parser, in_statement_expr, true, NULL);
13412 omp_for_parse_state->want_nested_loop = want_nested_loop;
13414 continue;
13417 /* Parse the statement. */
13418 cp_parser_statement (parser, in_statement_expr, true, NULL);
13422 /* Return true if this is the C++20 version of range-based-for with
13423 init-statement. */
13425 static bool
13426 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13428 bool r = false;
13430 /* Save tokens so that we can put them back. */
13431 cp_lexer_save_tokens (parser->lexer);
13433 /* There has to be an unnested ; followed by an unnested :. */
13434 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13435 /*recovering=*/false,
13436 CPP_SEMICOLON,
13437 /*consume_paren=*/false) != -1)
13438 goto out;
13440 /* We found the semicolon, eat it now. */
13441 cp_lexer_consume_token (parser->lexer);
13443 /* Now look for ':' that is not nested in () or {}. */
13444 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13445 /*recovering=*/false,
13446 CPP_COLON,
13447 /*consume_paren=*/false) == -1);
13449 out:
13450 /* Roll back the tokens we skipped. */
13451 cp_lexer_rollback_tokens (parser->lexer);
13453 return r;
13456 /* Return true if we're looking at (init; cond), false otherwise. */
13458 static bool
13459 cp_parser_init_statement_p (cp_parser *parser)
13461 /* Save tokens so that we can put them back. */
13462 cp_lexer_save_tokens (parser->lexer);
13464 /* Look for ';' that is not nested in () or {}. */
13465 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13466 /*recovering=*/false,
13467 CPP_SEMICOLON,
13468 /*consume_paren=*/false);
13470 /* Roll back the tokens we skipped. */
13471 cp_lexer_rollback_tokens (parser->lexer);
13473 return ret == -1;
13476 /* Parse a selection-statement.
13478 selection-statement:
13479 if ( init-statement [opt] condition ) statement
13480 if ( init-statement [opt] condition ) statement else statement
13481 switch ( init-statement [opt] condition ) statement
13483 Returns the new IF_STMT or SWITCH_STMT.
13485 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13486 is a (possibly labeled) if statement which is not enclosed in
13487 braces and has an else clause. This is used to implement
13488 -Wparentheses.
13490 CHAIN is a vector of if-else-if conditions. This is used to implement
13491 -Wduplicated-cond. */
13493 static tree
13494 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13495 vec<tree> *chain)
13497 cp_token *token;
13498 enum rid keyword;
13499 token_indent_info guard_tinfo;
13501 if (if_p != NULL)
13502 *if_p = false;
13504 /* Peek at the next token. */
13505 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13506 guard_tinfo = get_token_indent_info (token);
13508 /* See what kind of keyword it is. */
13509 keyword = token->keyword;
13510 switch (keyword)
13512 case RID_IF:
13513 case RID_SWITCH:
13515 tree statement;
13516 tree condition;
13518 bool cx = false;
13519 if (keyword == RID_IF
13520 && cp_lexer_next_token_is_keyword (parser->lexer,
13521 RID_CONSTEXPR))
13523 cx = true;
13524 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13525 if (cxx_dialect < cxx17)
13526 pedwarn (tok->location, OPT_Wc__17_extensions,
13527 "%<if constexpr%> only available with "
13528 "%<-std=c++17%> or %<-std=gnu++17%>");
13530 int ce = 0;
13531 if (keyword == RID_IF && !cx)
13533 if (cp_lexer_next_token_is_keyword (parser->lexer,
13534 RID_CONSTEVAL))
13535 ce = 1;
13536 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13537 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13538 RID_CONSTEVAL))
13540 ce = -1;
13541 cp_lexer_consume_token (parser->lexer);
13544 if (ce)
13546 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13547 if (cxx_dialect < cxx23)
13548 pedwarn (tok->location, OPT_Wc__23_extensions,
13549 "%<if consteval%> only available with "
13550 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13552 bool save_in_consteval_if_p = in_consteval_if_p;
13553 statement = begin_if_stmt ();
13554 IF_STMT_CONSTEVAL_P (statement) = true;
13555 condition = finish_if_stmt_cond (boolean_false_node, statement);
13557 gcc_rich_location richloc (tok->location);
13558 bool non_compound_stmt_p = false;
13559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13561 non_compound_stmt_p = true;
13562 richloc.add_fixit_insert_after (tok->location, "{");
13565 in_consteval_if_p |= ce > 0;
13566 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13568 if (non_compound_stmt_p)
13570 location_t before_loc
13571 = cp_lexer_peek_token (parser->lexer)->location;
13572 richloc.add_fixit_insert_before (before_loc, "}");
13573 error_at (&richloc,
13574 "%<if consteval%> requires compound statement");
13575 non_compound_stmt_p = false;
13578 finish_then_clause (statement);
13580 /* If the next token is `else', parse the else-clause. */
13581 if (cp_lexer_next_token_is_keyword (parser->lexer,
13582 RID_ELSE))
13584 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13585 gcc_rich_location else_richloc (else_tok->location);
13586 guard_tinfo = get_token_indent_info (else_tok);
13587 /* Consume the `else' keyword. */
13588 cp_lexer_consume_token (parser->lexer);
13590 begin_else_clause (statement);
13592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13594 non_compound_stmt_p = true;
13595 else_richloc.add_fixit_insert_after (else_tok->location,
13596 "{");
13599 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13600 cp_parser_implicitly_scoped_statement (parser, NULL,
13601 guard_tinfo);
13603 if (non_compound_stmt_p)
13605 location_t before_loc
13606 = cp_lexer_peek_token (parser->lexer)->location;
13607 else_richloc.add_fixit_insert_before (before_loc, "}");
13608 error_at (&else_richloc,
13609 "%<if consteval%> requires compound statement");
13612 finish_else_clause (statement);
13615 in_consteval_if_p = save_in_consteval_if_p;
13616 if (ce < 0)
13618 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13619 if (THEN_CLAUSE (statement) == NULL_TREE)
13620 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13623 finish_if_stmt (statement);
13624 return statement;
13627 /* Look for the `('. */
13628 matching_parens parens;
13629 if (!parens.require_open (parser))
13631 cp_parser_skip_to_end_of_statement (parser);
13632 return error_mark_node;
13635 /* Begin the selection-statement. */
13636 if (keyword == RID_IF)
13638 statement = begin_if_stmt ();
13639 IF_STMT_CONSTEXPR_P (statement) = cx;
13641 else
13642 statement = begin_switch_stmt ();
13644 /* Parse the optional init-statement. */
13645 if (cp_parser_init_statement_p (parser))
13647 tree decl;
13648 if (cxx_dialect < cxx17)
13649 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13650 OPT_Wc__17_extensions,
13651 "init-statement in selection statements only available "
13652 "with %<-std=c++17%> or %<-std=gnu++17%>");
13653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13654 /* A non-empty init-statement can have arbitrary side
13655 effects. */
13656 vec_free (chain);
13657 cp_parser_init_statement (parser, &decl);
13660 /* Parse the condition. */
13661 condition = cp_parser_condition (parser);
13662 /* Look for the `)'. */
13663 if (!parens.require_close (parser))
13664 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13665 /*consume_paren=*/true);
13667 if (keyword == RID_IF)
13669 bool nested_if;
13670 unsigned char in_statement;
13672 /* Add the condition. */
13673 condition = finish_if_stmt_cond (condition, statement);
13675 if (warn_duplicated_cond)
13676 warn_duplicated_cond_add_or_warn (token->location, condition,
13677 &chain);
13679 /* Parse the then-clause. */
13680 in_statement = parser->in_statement;
13681 parser->in_statement |= IN_IF_STMT;
13683 /* Outside a template, the non-selected branch of a constexpr
13684 if is a 'discarded statement', i.e. unevaluated. */
13685 bool was_discarded = in_discarded_stmt;
13686 bool discard_then = (cx && !processing_template_decl
13687 && integer_zerop (condition));
13688 if (discard_then)
13690 in_discarded_stmt = true;
13691 ++c_inhibit_evaluation_warnings;
13694 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13695 guard_tinfo);
13697 parser->in_statement = in_statement;
13699 finish_then_clause (statement);
13701 if (discard_then)
13703 THEN_CLAUSE (statement) = NULL_TREE;
13704 in_discarded_stmt = was_discarded;
13705 --c_inhibit_evaluation_warnings;
13708 /* If the next token is `else', parse the else-clause. */
13709 if (cp_lexer_next_token_is_keyword (parser->lexer,
13710 RID_ELSE))
13712 bool discard_else = (cx && !processing_template_decl
13713 && integer_nonzerop (condition));
13714 if (discard_else)
13716 in_discarded_stmt = true;
13717 ++c_inhibit_evaluation_warnings;
13720 guard_tinfo
13721 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13722 /* Consume the `else' keyword. */
13723 cp_lexer_consume_token (parser->lexer);
13724 if (warn_duplicated_cond)
13726 if (cp_lexer_next_token_is_keyword (parser->lexer,
13727 RID_IF)
13728 && chain == NULL)
13730 /* We've got "if (COND) else if (COND2)". Start
13731 the condition chain and add COND as the first
13732 element. */
13733 chain = new vec<tree> ();
13734 if (!CONSTANT_CLASS_P (condition)
13735 && !TREE_SIDE_EFFECTS (condition))
13737 /* Wrap it in a NOP_EXPR so that we can set the
13738 location of the condition. */
13739 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13740 condition);
13741 SET_EXPR_LOCATION (e, token->location);
13742 chain->safe_push (e);
13745 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13746 RID_IF))
13747 /* This is if-else without subsequent if. Zap the
13748 condition chain; we would have already warned at
13749 this point. */
13750 vec_free (chain);
13752 begin_else_clause (statement);
13753 /* Parse the else-clause. */
13754 cp_parser_implicitly_scoped_statement (parser, NULL,
13755 guard_tinfo, chain);
13757 finish_else_clause (statement);
13759 /* If we are currently parsing a then-clause, then
13760 IF_P will not be NULL. We set it to true to
13761 indicate that this if statement has an else clause.
13762 This may trigger the Wparentheses warning below
13763 when we get back up to the parent if statement. */
13764 if (if_p != NULL)
13765 *if_p = true;
13767 if (discard_else)
13769 ELSE_CLAUSE (statement) = NULL_TREE;
13770 in_discarded_stmt = was_discarded;
13771 --c_inhibit_evaluation_warnings;
13774 else
13776 /* This if statement does not have an else clause. If
13777 NESTED_IF is true, then the then-clause has an if
13778 statement which does have an else clause. We warn
13779 about the potential ambiguity. */
13780 if (nested_if)
13781 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13782 "suggest explicit braces to avoid ambiguous"
13783 " %<else%>");
13784 if (warn_duplicated_cond)
13785 /* We don't need the condition chain anymore. */
13786 vec_free (chain);
13789 /* Now we're all done with the if-statement. */
13790 finish_if_stmt (statement);
13792 else
13794 bool in_switch_statement_p;
13795 unsigned char in_statement;
13797 /* Add the condition. */
13798 finish_switch_cond (condition, statement);
13800 /* Parse the body of the switch-statement. */
13801 in_switch_statement_p = parser->in_switch_statement_p;
13802 in_statement = parser->in_statement;
13803 parser->in_switch_statement_p = true;
13804 parser->in_statement |= IN_SWITCH_STMT;
13805 cp_parser_implicitly_scoped_statement (parser, if_p,
13806 guard_tinfo);
13807 parser->in_switch_statement_p = in_switch_statement_p;
13808 parser->in_statement = in_statement;
13810 /* Now we're all done with the switch-statement. */
13811 finish_switch_stmt (statement);
13814 return statement;
13816 break;
13818 default:
13819 cp_parser_error (parser, "expected selection-statement");
13820 return error_mark_node;
13824 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13825 If we have seen at least one decl-specifier, and the next token is not
13826 a parenthesis (after "int (" we might be looking at a functional cast)
13827 neither we are dealing with a concept-check expression then we must be
13828 looking at a declaration. */
13830 static void
13831 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13832 cp_decl_specifier_seq *decl_specs)
13834 if (decl_specs->any_specifiers_p
13835 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13836 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13837 && !cp_parser_error_occurred (parser)
13838 && !(decl_specs->type
13839 && TREE_CODE (decl_specs->type) == TYPE_DECL
13840 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13841 cp_parser_commit_to_tentative_parse (parser);
13844 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13845 The declarator shall not specify a function or an array. Returns
13846 TRUE if the declarator is valid, FALSE otherwise. */
13848 static bool
13849 cp_parser_check_condition_declarator (cp_parser* parser,
13850 cp_declarator *declarator,
13851 location_t loc)
13853 if (declarator == cp_error_declarator
13854 || function_declarator_p (declarator)
13855 || declarator->kind == cdk_array)
13857 if (declarator == cp_error_declarator)
13858 /* Already complained. */;
13859 else if (declarator->kind == cdk_array)
13860 error_at (loc, "condition declares an array");
13861 else
13862 error_at (loc, "condition declares a function");
13863 if (parser->fully_implicit_function_template_p)
13864 abort_fully_implicit_template (parser);
13865 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13866 /*or_comma=*/false,
13867 /*consume_paren=*/false);
13868 return false;
13870 else
13871 return true;
13874 /* Parse a condition.
13876 condition:
13877 expression
13878 type-specifier-seq declarator = initializer-clause
13879 type-specifier-seq declarator braced-init-list
13881 GNU Extension:
13883 condition:
13884 type-specifier-seq declarator asm-specification [opt]
13885 attributes [opt] = assignment-expression
13887 Returns the expression that should be tested. */
13889 static tree
13890 cp_parser_condition (cp_parser* parser)
13892 cp_decl_specifier_seq type_specifiers;
13893 const char *saved_message;
13894 int declares_class_or_enum;
13896 /* Try the declaration first. */
13897 cp_parser_parse_tentatively (parser);
13898 /* New types are not allowed in the type-specifier-seq for a
13899 condition. */
13900 saved_message = parser->type_definition_forbidden_message;
13901 parser->type_definition_forbidden_message
13902 = G_("types may not be defined in conditions");
13903 /* Parse the type-specifier-seq. */
13904 cp_parser_decl_specifier_seq (parser,
13905 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13906 &type_specifiers,
13907 &declares_class_or_enum);
13908 /* Restore the saved message. */
13909 parser->type_definition_forbidden_message = saved_message;
13911 /* Gather the attributes that were provided with the
13912 decl-specifiers. */
13913 tree prefix_attributes = type_specifiers.attributes;
13915 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13917 /* If all is well, we might be looking at a declaration. */
13918 if (!cp_parser_error_occurred (parser))
13920 tree decl;
13921 tree asm_specification;
13922 tree attributes;
13923 cp_declarator *declarator;
13924 tree initializer = NULL_TREE;
13925 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13927 /* Parse the declarator. */
13928 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13929 CP_PARSER_FLAGS_NONE,
13930 /*ctor_dtor_or_conv_p=*/NULL,
13931 /*parenthesized_p=*/NULL,
13932 /*member_p=*/false,
13933 /*friend_p=*/false,
13934 /*static_p=*/false);
13935 /* Parse the attributes. */
13936 attributes = cp_parser_attributes_opt (parser);
13937 /* Parse the asm-specification. */
13938 asm_specification = cp_parser_asm_specification_opt (parser);
13939 /* If the next token is not an `=' or '{', then we might still be
13940 looking at an expression. For example:
13942 if (A(a).x)
13944 looks like a decl-specifier-seq and a declarator -- but then
13945 there is no `=', so this is an expression. */
13946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13947 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13948 cp_parser_simulate_error (parser);
13950 /* If we did see an `=' or '{', then we are looking at a declaration
13951 for sure. */
13952 if (cp_parser_parse_definitely (parser))
13954 tree pushed_scope;
13955 bool non_constant_p = false;
13956 int flags = LOOKUP_ONLYCONVERTING;
13958 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13959 return error_mark_node;
13961 /* Create the declaration. */
13962 decl = start_decl (declarator, &type_specifiers,
13963 /*initialized_p=*/true,
13964 attributes, prefix_attributes,
13965 &pushed_scope);
13967 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13968 /* Parse the initializer. */
13969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13971 initializer = cp_parser_braced_list (parser, &non_constant_p);
13972 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13973 flags = 0;
13975 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13977 /* Consume the `='. */
13978 cp_lexer_consume_token (parser->lexer);
13979 initializer = cp_parser_initializer_clause (parser,
13980 &non_constant_p);
13982 else
13984 cp_parser_error (parser, "expected initializer");
13985 initializer = error_mark_node;
13987 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13988 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13990 /* Process the initializer. */
13991 cp_finish_decl (decl,
13992 initializer, !non_constant_p,
13993 asm_specification,
13994 flags);
13996 if (pushed_scope)
13997 pop_scope (pushed_scope);
13999 return convert_from_reference (decl);
14002 /* If we didn't even get past the declarator successfully, we are
14003 definitely not looking at a declaration. */
14004 else
14005 cp_parser_abort_tentative_parse (parser);
14007 /* Otherwise, we are looking at an expression. */
14008 return cp_parser_expression (parser);
14011 /* Parses a for-statement or range-for-statement until the closing ')',
14012 not included. */
14014 static tree
14015 cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
14017 tree init, scope, decl;
14018 bool is_range_for;
14020 /* Begin the for-statement. */
14021 scope = begin_for_scope (&init);
14023 /* Maybe parse the optional init-statement in a range-based for loop. */
14024 if (cp_parser_range_based_for_with_init_p (parser)
14025 /* Checked for diagnostic purposes only. */
14026 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14028 tree dummy;
14029 cp_parser_init_statement (parser, &dummy);
14030 if (cxx_dialect < cxx20)
14032 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14033 OPT_Wc__20_extensions,
14034 "range-based %<for%> loops with initializer only "
14035 "available with %<-std=c++20%> or %<-std=gnu++20%>");
14036 decl = error_mark_node;
14040 /* Parse the initialization. */
14041 is_range_for = cp_parser_init_statement (parser, &decl);
14043 if (is_range_for)
14044 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
14045 novector, false);
14046 else
14047 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
14050 static tree
14051 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
14052 tree unroll, bool novector)
14054 /* Normal for loop */
14055 tree condition = NULL_TREE;
14056 tree expression = NULL_TREE;
14057 tree stmt;
14059 stmt = begin_for_stmt (scope, init);
14060 /* The init-statement has already been parsed in
14061 cp_parser_init_statement, so no work is needed here. */
14062 finish_init_stmt (stmt);
14064 /* If there's a condition, process it. */
14065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14066 condition = cp_parser_condition (parser);
14067 else if (ivdep)
14069 cp_parser_error (parser, "missing loop condition in loop with "
14070 "%<GCC ivdep%> pragma");
14071 condition = error_mark_node;
14073 else if (unroll)
14075 cp_parser_error (parser, "missing loop condition in loop with "
14076 "%<GCC unroll%> pragma");
14077 condition = error_mark_node;
14079 finish_for_cond (condition, stmt, ivdep, unroll, novector);
14080 /* Look for the `;'. */
14081 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14083 /* If there's an expression, process it. */
14084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14085 expression = cp_parser_expression (parser);
14086 finish_for_expr (expression, stmt);
14088 return stmt;
14091 /* Tries to parse a range-based for-statement:
14093 range-based-for:
14094 decl-specifier-seq declarator : expression
14096 The decl-specifier-seq declarator and the `:' are already parsed by
14097 cp_parser_init_statement. If processing_template_decl it returns a
14098 newly created RANGE_FOR_STMT; if not, it is converted to a
14099 regular FOR_STMT. */
14101 static tree
14102 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
14103 bool ivdep, tree unroll, bool novector, bool is_omp)
14105 tree stmt, range_expr;
14106 auto_vec <cxx_binding *, 16> bindings;
14107 auto_vec <tree, 16> names;
14108 cp_decomp decomp_d, *decomp = NULL;
14110 /* Get the range declaration momentarily out of the way so that
14111 the range expression doesn't clash with it. */
14112 if (range_decl != error_mark_node)
14114 if (DECL_HAS_VALUE_EXPR_P (range_decl))
14116 tree v = DECL_VALUE_EXPR (range_decl);
14117 /* For decomposition declaration get all of the corresponding
14118 declarations out of the way. */
14119 if (TREE_CODE (v) == ARRAY_REF
14120 && VAR_P (TREE_OPERAND (v, 0))
14121 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
14123 tree d = range_decl;
14124 range_decl = TREE_OPERAND (v, 0);
14125 decomp = &decomp_d;
14126 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
14127 decomp->decl = d;
14128 for (unsigned int i = 0; i < decomp->count;
14129 i++, d = DECL_CHAIN (d))
14131 tree name = DECL_NAME (d);
14132 names.safe_push (name);
14133 bindings.safe_push (IDENTIFIER_BINDING (name));
14134 IDENTIFIER_BINDING (name)
14135 = IDENTIFIER_BINDING (name)->previous;
14139 if (names.is_empty ())
14141 tree name = DECL_NAME (range_decl);
14142 names.safe_push (name);
14143 bindings.safe_push (IDENTIFIER_BINDING (name));
14144 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
14148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14149 range_expr = cp_parser_braced_list (parser);
14150 else
14151 range_expr = cp_parser_expression (parser);
14153 /* Put the range declaration(s) back into scope. */
14154 for (unsigned int i = 0; i < names.length (); i++)
14156 cxx_binding *binding = bindings[i];
14157 binding->previous = IDENTIFIER_BINDING (names[i]);
14158 IDENTIFIER_BINDING (names[i]) = binding;
14161 /* finish_omp_for has its own code for the following, so just
14162 return the range_expr instead. */
14163 if (is_omp)
14164 return range_expr;
14166 /* If in template, STMT is converted to a normal for-statement
14167 at instantiation. If not, it is done just ahead. */
14168 if (processing_template_decl)
14170 if (check_for_bare_parameter_packs (range_expr))
14171 range_expr = error_mark_node;
14172 stmt = begin_range_for_stmt (scope, init);
14173 if (ivdep)
14174 RANGE_FOR_IVDEP (stmt) = 1;
14175 if (unroll)
14176 RANGE_FOR_UNROLL (stmt) = unroll;
14177 if (novector)
14178 RANGE_FOR_NOVECTOR (stmt) = 1;
14179 finish_range_for_decl (stmt, range_decl, range_expr);
14180 if (!type_dependent_expression_p (range_expr)
14181 /* do_auto_deduction doesn't mess with template init-lists. */
14182 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14183 do_range_for_auto_deduction (range_decl, range_expr, decomp);
14185 else
14187 stmt = begin_for_stmt (scope, init);
14188 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
14189 ivdep, unroll, novector);
14191 return stmt;
14194 /* Subroutine of cp_convert_range_for: given the initializer expression,
14195 builds up the range temporary. */
14197 static tree
14198 build_range_temp (tree range_expr)
14200 /* Find out the type deduced by the declaration
14201 `auto &&__range = range_expr'. */
14202 tree auto_node = make_auto ();
14203 tree range_type = cp_build_reference_type (auto_node, true);
14204 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14206 /* Create the __range variable. */
14207 tree range_temp = build_decl (input_location, VAR_DECL,
14208 for_range__identifier, range_type);
14209 TREE_USED (range_temp) = 1;
14210 DECL_ARTIFICIAL (range_temp) = 1;
14212 return range_temp;
14215 /* Used by cp_parser_range_for in template context: we aren't going to
14216 do a full conversion yet, but we still need to resolve auto in the
14217 type of the for-range-declaration if present. This is basically
14218 a shortcut version of cp_convert_range_for. */
14220 static void
14221 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14223 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14224 if (auto_node)
14226 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14227 range_temp = convert_from_reference (build_range_temp (range_expr));
14228 iter_type = (cp_parser_perform_range_for_lookup
14229 (range_temp, &begin_dummy, &end_dummy));
14230 if (iter_type)
14232 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14233 iter_type);
14234 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14235 RO_UNARY_STAR, NULL_TREE,
14236 tf_warning_or_error);
14237 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14238 iter_decl, auto_node,
14239 tf_warning_or_error,
14240 adc_variable_type);
14241 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
14242 cp_finish_decomp (decl, decomp);
14247 /* Warns when the loop variable should be changed to a reference type to
14248 avoid unnecessary copying. I.e., from
14250 for (const auto x : range)
14252 where range returns a reference, to
14254 for (const auto &x : range)
14256 if this version doesn't make a copy.
14258 This function also warns when the loop variable is initialized with
14259 a value of a different type resulting in a copy:
14261 int arr[10];
14262 for (const double &x : arr)
14264 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14265 This function is never called when processing_template_decl is on. */
14267 static void
14268 warn_for_range_copy (tree decl, tree expr)
14270 if (!warn_range_loop_construct
14271 || decl == error_mark_node)
14272 return;
14274 location_t loc = DECL_SOURCE_LOCATION (decl);
14275 tree type = TREE_TYPE (decl);
14277 if (from_macro_expansion_at (loc))
14278 return;
14280 if (TYPE_REF_P (type))
14282 if (glvalue_p (expr)
14283 && ref_conv_binds_to_temporary (type, expr).is_true ())
14285 auto_diagnostic_group d;
14286 if (warning_at (loc, OPT_Wrange_loop_construct,
14287 "loop variable %qD of type %qT binds to a temporary "
14288 "constructed from type %qT", decl, type,
14289 TREE_TYPE (expr)))
14291 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14292 TYPE_QUAL_CONST);
14293 ref = cp_build_reference_type (ref, /*rval*/false);
14294 inform (loc, "use non-reference type %qT to make the copy "
14295 "explicit or %qT to prevent copying",
14296 non_reference (type), ref);
14299 return;
14301 else if (!CP_TYPE_CONST_P (type))
14302 return;
14304 /* Since small trivially copyable types are cheap to copy, we suppress the
14305 warning for them. 64B is a common size of a cache line. */
14306 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14307 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14308 && trivially_copyable_p (type)))
14309 return;
14311 /* If we can initialize a reference directly, suggest that to avoid the
14312 copy. */
14313 tree rtype = cp_build_reference_type (type, /*rval*/false);
14314 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14316 auto_diagnostic_group d;
14317 if (warning_at (loc, OPT_Wrange_loop_construct,
14318 "loop variable %qD creates a copy from type %qT",
14319 decl, type))
14321 gcc_rich_location richloc (loc);
14322 richloc.add_fixit_insert_before ("&");
14323 inform (&richloc, "use reference type to prevent copying");
14328 /* Converts a range-based for-statement into a normal
14329 for-statement, as per the definition.
14331 for (RANGE_DECL : RANGE_EXPR)
14332 BLOCK
14334 should be equivalent to:
14337 auto &&__range = RANGE_EXPR;
14338 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14339 __begin != __end;
14340 ++__begin)
14342 RANGE_DECL = *__begin;
14343 BLOCK
14347 If RANGE_EXPR is an array:
14348 BEGIN_EXPR = __range
14349 END_EXPR = __range + ARRAY_SIZE(__range)
14350 Else if RANGE_EXPR has a member 'begin' or 'end':
14351 BEGIN_EXPR = __range.begin()
14352 END_EXPR = __range.end()
14353 Else:
14354 BEGIN_EXPR = begin(__range)
14355 END_EXPR = end(__range);
14357 If __range has a member 'begin' but not 'end', or vice versa, we must
14358 still use the second alternative (it will surely fail, however).
14359 When calling begin()/end() in the third alternative we must use
14360 argument dependent lookup, but always considering 'std' as an associated
14361 namespace. */
14363 tree
14364 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14365 cp_decomp *decomp, bool ivdep, tree unroll,
14366 bool novector)
14368 tree begin, end;
14369 tree iter_type, begin_expr, end_expr;
14370 tree condition, expression;
14372 range_expr = mark_lvalue_use (range_expr);
14374 if (range_decl == error_mark_node || range_expr == error_mark_node)
14375 /* If an error happened previously do nothing or else a lot of
14376 unhelpful errors would be issued. */
14377 begin_expr = end_expr = iter_type = error_mark_node;
14378 else
14380 tree range_temp;
14382 if (VAR_P (range_expr)
14383 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14384 /* Can't bind a reference to an array of runtime bound. */
14385 range_temp = range_expr;
14386 else
14388 range_temp = build_range_temp (range_expr);
14389 pushdecl (range_temp);
14390 cp_finish_decl (range_temp, range_expr,
14391 /*is_constant_init*/false, NULL_TREE,
14392 LOOKUP_ONLYCONVERTING);
14393 range_temp = convert_from_reference (range_temp);
14395 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14396 &begin_expr, &end_expr);
14399 /* The new for initialization statement. */
14400 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14401 iter_type);
14402 TREE_USED (begin) = 1;
14403 DECL_ARTIFICIAL (begin) = 1;
14404 pushdecl (begin);
14405 cp_finish_decl (begin, begin_expr,
14406 /*is_constant_init*/false, NULL_TREE,
14407 LOOKUP_ONLYCONVERTING);
14409 if (cxx_dialect >= cxx17)
14410 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14411 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14412 TREE_USED (end) = 1;
14413 DECL_ARTIFICIAL (end) = 1;
14414 pushdecl (end);
14415 cp_finish_decl (end, end_expr,
14416 /*is_constant_init*/false, NULL_TREE,
14417 LOOKUP_ONLYCONVERTING);
14419 finish_init_stmt (statement);
14421 /* The new for condition. */
14422 condition = build_x_binary_op (input_location, NE_EXPR,
14423 begin, ERROR_MARK,
14424 end, ERROR_MARK,
14425 NULL_TREE, NULL, tf_warning_or_error);
14426 finish_for_cond (condition, statement, ivdep, unroll, novector);
14428 /* The new increment expression. */
14429 expression = finish_unary_op_expr (input_location,
14430 PREINCREMENT_EXPR, begin,
14431 tf_warning_or_error);
14432 finish_for_expr (expression, statement);
14434 /* The declaration is initialized with *__begin inside the loop body. */
14435 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14436 NULL_TREE, tf_warning_or_error);
14437 cp_finish_decl (range_decl, deref_begin,
14438 /*is_constant_init*/false, NULL_TREE,
14439 LOOKUP_ONLYCONVERTING, decomp);
14440 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14441 cp_finish_decomp (range_decl, decomp);
14443 warn_for_range_copy (range_decl, deref_begin);
14445 return statement;
14448 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14449 We need to solve both at the same time because the method used
14450 depends on the existence of members begin or end.
14451 Returns the type deduced for the iterator expression. */
14453 static tree
14454 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14456 if (error_operand_p (range))
14458 *begin = *end = error_mark_node;
14459 return error_mark_node;
14462 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14464 error ("range-based %<for%> expression of type %qT "
14465 "has incomplete type", TREE_TYPE (range));
14466 *begin = *end = error_mark_node;
14467 return error_mark_node;
14469 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14471 /* If RANGE is an array, we will use pointer arithmetic. */
14472 *begin = decay_conversion (range, tf_warning_or_error);
14473 *end = build_binary_op (input_location, PLUS_EXPR,
14474 range,
14475 array_type_nelts_top (TREE_TYPE (range)),
14476 false);
14477 return TREE_TYPE (*begin);
14479 else
14481 /* If it is not an array, we must do a bit of magic. */
14482 tree id_begin, id_end;
14483 tree member_begin, member_end;
14485 *begin = *end = error_mark_node;
14487 id_begin = get_identifier ("begin");
14488 id_end = get_identifier ("end");
14489 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14490 /*protect=*/2, /*want_type=*/false,
14491 tf_warning_or_error);
14492 member_end = lookup_member (TREE_TYPE (range), id_end,
14493 /*protect=*/2, /*want_type=*/false,
14494 tf_warning_or_error);
14496 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14498 /* Use the member functions. */
14499 *begin = cp_parser_range_for_member_function (range, id_begin);
14500 *end = cp_parser_range_for_member_function (range, id_end);
14502 else
14504 /* Use global functions with ADL. */
14505 releasing_vec vec;
14507 vec_safe_push (vec, range);
14509 member_begin = perform_koenig_lookup (id_begin, vec,
14510 tf_warning_or_error);
14511 *begin = finish_call_expr (member_begin, &vec, false, true,
14512 tf_warning_or_error);
14513 member_end = perform_koenig_lookup (id_end, vec,
14514 tf_warning_or_error);
14515 *end = finish_call_expr (member_end, &vec, false, true,
14516 tf_warning_or_error);
14519 /* Last common checks. */
14520 if (*begin == error_mark_node || *end == error_mark_node)
14522 /* If one of the expressions is an error do no more checks. */
14523 *begin = *end = error_mark_node;
14524 return error_mark_node;
14526 else if (type_dependent_expression_p (*begin)
14527 || type_dependent_expression_p (*end))
14528 /* Can happen, when, eg, in a template context, Koenig lookup
14529 can't resolve begin/end (c++/58503). */
14530 return NULL_TREE;
14531 else
14533 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14534 /* The unqualified type of the __begin and __end temporaries should
14535 be the same, as required by the multiple auto declaration. */
14536 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14538 if (cxx_dialect >= cxx17
14539 && (build_x_binary_op (input_location, NE_EXPR,
14540 *begin, ERROR_MARK,
14541 *end, ERROR_MARK,
14542 NULL_TREE, NULL, tf_none)
14543 != error_mark_node))
14544 /* P0184R0 allows __begin and __end to have different types,
14545 but make sure they are comparable so we can give a better
14546 diagnostic. */;
14547 else
14548 error ("inconsistent begin/end types in range-based %<for%> "
14549 "statement: %qT and %qT",
14550 TREE_TYPE (*begin), TREE_TYPE (*end));
14552 return iter_type;
14557 /* Helper function for cp_parser_perform_range_for_lookup.
14558 Builds a tree for RANGE.IDENTIFIER(). */
14560 static tree
14561 cp_parser_range_for_member_function (tree range, tree identifier)
14563 tree member, res;
14565 member = finish_class_member_access_expr (range, identifier,
14566 false, tf_warning_or_error);
14567 if (member == error_mark_node)
14568 return error_mark_node;
14570 releasing_vec vec;
14571 res = finish_call_expr (member, &vec,
14572 /*disallow_virtual=*/false,
14573 /*koenig_p=*/false,
14574 tf_warning_or_error);
14575 return res;
14578 /* Parse an iteration-statement.
14580 iteration-statement:
14581 while ( condition ) statement
14582 do statement while ( expression ) ;
14583 for ( init-statement condition [opt] ; expression [opt] )
14584 statement
14586 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14588 static tree
14589 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14590 tree unroll, bool novector)
14592 cp_token *token;
14593 enum rid keyword;
14594 tree statement;
14595 unsigned char in_statement;
14596 token_indent_info guard_tinfo;
14598 /* Peek at the next token. */
14599 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14600 if (!token)
14601 return error_mark_node;
14603 guard_tinfo = get_token_indent_info (token);
14605 /* Remember whether or not we are already within an iteration
14606 statement. */
14607 in_statement = parser->in_statement;
14609 /* Special case for OMP loop intervening code. Parsing of permitted
14610 collapsed loop nests is handled elsewhere. */
14611 if (parser->omp_for_parse_state)
14613 error_at (token->location,
14614 "loop not permitted in intervening code in OpenMP loop body");
14615 parser->omp_for_parse_state->fail = true;
14618 /* See what kind of keyword it is. */
14619 keyword = token->keyword;
14620 switch (keyword)
14622 case RID_WHILE:
14624 tree condition;
14626 /* Begin the while-statement. */
14627 statement = begin_while_stmt ();
14628 /* Look for the `('. */
14629 matching_parens parens;
14630 parens.require_open (parser);
14631 /* Parse the condition. */
14632 condition = cp_parser_condition (parser);
14633 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14634 /* Look for the `)'. */
14635 parens.require_close (parser);
14636 /* Parse the dependent statement. */
14637 parser->in_statement = IN_ITERATION_STMT;
14638 bool prev = note_iteration_stmt_body_start ();
14639 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14640 note_iteration_stmt_body_end (prev);
14641 parser->in_statement = in_statement;
14642 /* We're done with the while-statement. */
14643 finish_while_stmt (statement);
14645 break;
14647 case RID_DO:
14649 tree expression;
14651 /* Begin the do-statement. */
14652 statement = begin_do_stmt ();
14653 /* Parse the body of the do-statement. */
14654 parser->in_statement = IN_ITERATION_STMT;
14655 bool prev = note_iteration_stmt_body_start ();
14656 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14657 note_iteration_stmt_body_end (prev);
14658 parser->in_statement = in_statement;
14659 finish_do_body (statement);
14660 /* Look for the `while' keyword. */
14661 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14662 /* Look for the `('. */
14663 matching_parens parens;
14664 parens.require_open (parser);
14665 /* Parse the expression. */
14666 expression = cp_parser_expression (parser);
14667 /* We're done with the do-statement. */
14668 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14669 /* Look for the `)'. */
14670 parens.require_close (parser);
14671 /* Look for the `;'. */
14672 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14674 break;
14676 case RID_FOR:
14678 /* Look for the `('. */
14679 matching_parens parens;
14680 parens.require_open (parser);
14682 statement = cp_parser_for (parser, ivdep, unroll, novector);
14684 /* Look for the `)'. */
14685 parens.require_close (parser);
14687 /* Parse the body of the for-statement. */
14688 parser->in_statement = IN_ITERATION_STMT;
14689 bool prev = note_iteration_stmt_body_start ();
14690 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14691 note_iteration_stmt_body_end (prev);
14692 parser->in_statement = in_statement;
14694 /* We're done with the for-statement. */
14695 finish_for_stmt (statement);
14697 break;
14699 default:
14700 cp_parser_error (parser, "expected iteration-statement");
14701 statement = error_mark_node;
14702 break;
14705 return statement;
14708 /* Parse an init-statement or the declarator of a range-based-for.
14709 Returns true if a range-based-for declaration is seen.
14711 init-statement:
14712 expression-statement
14713 simple-declaration
14714 alias-declaration */
14716 static bool
14717 cp_parser_init_statement (cp_parser *parser, tree *decl)
14719 /* If the next token is a `;', then we have an empty
14720 expression-statement. Grammatically, this is also a
14721 simple-declaration, but an invalid one, because it does not
14722 declare anything. Therefore, if we did not handle this case
14723 specially, we would issue an error message about an invalid
14724 declaration. */
14725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14727 bool is_range_for = false;
14728 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14730 /* A colon is used in range-based for. */
14731 parser->colon_corrects_to_scope_p = false;
14733 /* We're going to speculatively look for a declaration, falling back
14734 to an expression, if necessary. */
14735 cp_parser_parse_tentatively (parser);
14736 bool expect_semicolon_p = true;
14737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14739 cp_parser_alias_declaration (parser);
14740 expect_semicolon_p = false;
14741 if (cxx_dialect < cxx23
14742 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14743 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14744 OPT_Wc__23_extensions,
14745 "alias-declaration in init-statement only "
14746 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14748 else
14749 /* Parse the declaration. */
14750 cp_parser_simple_declaration (parser,
14751 /*function_definition_allowed_p=*/false,
14752 decl);
14753 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14754 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14756 /* It is a range-for, consume the ':'. */
14757 cp_lexer_consume_token (parser->lexer);
14758 is_range_for = true;
14759 if (cxx_dialect < cxx11)
14760 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14761 OPT_Wc__11_extensions,
14762 "range-based %<for%> loops only available with "
14763 "%<-std=c++11%> or %<-std=gnu++11%>");
14765 else if (expect_semicolon_p)
14766 /* The ';' is not consumed yet because we told
14767 cp_parser_simple_declaration not to. */
14768 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14770 if (cp_parser_parse_definitely (parser))
14771 return is_range_for;
14772 /* If the tentative parse failed, then we shall need to look for an
14773 expression-statement. */
14775 /* If we are here, it is an expression-statement. */
14776 cp_parser_expression_statement (parser, NULL_TREE);
14777 return false;
14780 /* Parse a jump-statement.
14782 jump-statement:
14783 break ;
14784 continue ;
14785 return expression [opt] ;
14786 return braced-init-list ;
14787 coroutine-return-statement;
14788 goto identifier ;
14790 GNU extension:
14792 jump-statement:
14793 goto * expression ;
14795 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14797 static tree
14798 cp_parser_jump_statement (cp_parser* parser)
14800 tree statement = error_mark_node;
14801 cp_token *token;
14802 enum rid keyword;
14803 unsigned char in_statement;
14805 /* Peek at the next token. */
14806 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14807 if (!token)
14808 return error_mark_node;
14810 /* See what kind of keyword it is. */
14811 keyword = token->keyword;
14812 switch (keyword)
14814 case RID_BREAK:
14815 in_statement = parser->in_statement & ~IN_IF_STMT;
14816 switch (in_statement)
14818 case 0:
14819 error_at (token->location, "break statement not within loop or switch");
14820 break;
14821 default:
14822 gcc_assert ((in_statement & IN_SWITCH_STMT)
14823 || in_statement == IN_ITERATION_STMT);
14824 statement = finish_break_stmt ();
14825 if (in_statement == IN_ITERATION_STMT)
14826 break_maybe_infinite_loop ();
14827 break;
14828 case IN_OMP_BLOCK:
14829 error_at (token->location, "invalid exit from OpenMP structured block");
14830 break;
14831 case IN_OMP_FOR:
14832 error_at (token->location, "break statement used with OpenMP for loop");
14833 break;
14835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14836 break;
14838 case RID_CONTINUE:
14839 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14841 case 0:
14842 error_at (token->location, "continue statement not within a loop");
14843 break;
14844 /* Fall through. */
14845 case IN_ITERATION_STMT:
14846 case IN_OMP_FOR:
14847 statement = finish_continue_stmt ();
14848 break;
14849 case IN_OMP_BLOCK:
14850 error_at (token->location, "invalid exit from OpenMP structured block");
14851 break;
14852 default:
14853 gcc_unreachable ();
14855 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14856 break;
14858 case RID_CO_RETURN:
14859 case RID_RETURN:
14861 tree expr;
14863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14865 cp_lexer_set_source_position (parser->lexer);
14866 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14867 expr = cp_parser_braced_list (parser);
14869 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14870 expr = cp_parser_expression (parser);
14871 else
14872 /* If the next token is a `;', then there is no
14873 expression. */
14874 expr = NULL_TREE;
14875 /* Build the return-statement, check co-return first, since type
14876 deduction is not valid there. */
14877 if (keyword == RID_CO_RETURN)
14878 statement = finish_co_return_stmt (token->location, expr);
14879 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14880 /* Don't deduce from a discarded return statement. */;
14881 else
14882 statement = finish_return_stmt (expr);
14883 /* Look for the final `;'. */
14884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14886 break;
14888 case RID_GOTO:
14889 if (parser->in_function_body
14890 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14891 && cxx_dialect < cxx23)
14893 error ("%<goto%> in %<constexpr%> function only available with "
14894 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14895 cp_function_chain->invalid_constexpr = true;
14898 /* Create the goto-statement. */
14899 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14901 /* Issue a warning about this use of a GNU extension. */
14902 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14903 /* Consume the '*' token. */
14904 cp_lexer_consume_token (parser->lexer);
14905 /* Parse the dependent expression. */
14906 finish_goto_stmt (cp_parser_expression (parser));
14908 else
14909 finish_goto_stmt (cp_parser_identifier (parser));
14910 /* Look for the final `;'. */
14911 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14912 break;
14914 default:
14915 cp_parser_error (parser, "expected jump-statement");
14916 break;
14919 return statement;
14922 /* Parse a declaration-statement.
14924 declaration-statement:
14925 block-declaration */
14927 static void
14928 cp_parser_declaration_statement (cp_parser* parser)
14930 void *p;
14932 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14933 p = obstack_alloc (&declarator_obstack, 0);
14935 /* Parse the block-declaration. */
14936 cp_parser_block_declaration (parser, /*statement_p=*/true);
14938 /* Free any declarators allocated. */
14939 obstack_free (&declarator_obstack, p);
14942 /* Some dependent statements (like `if (cond) statement'), are
14943 implicitly in their own scope. In other words, if the statement is
14944 a single statement (as opposed to a compound-statement), it is
14945 none-the-less treated as if it were enclosed in braces. Any
14946 declarations appearing in the dependent statement are out of scope
14947 after control passes that point. This function parses a statement,
14948 but ensures that is in its own scope, even if it is not a
14949 compound-statement.
14951 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14952 is a (possibly labeled) if statement which is not enclosed in
14953 braces and has an else clause. This is used to implement
14954 -Wparentheses.
14956 CHAIN is a vector of if-else-if conditions. This is used to implement
14957 -Wduplicated-cond.
14959 Returns the new statement. */
14961 static tree
14962 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14963 const token_indent_info &guard_tinfo,
14964 vec<tree> *chain)
14966 tree statement;
14967 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14968 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14969 token_indent_info body_tinfo
14970 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14972 if (if_p != NULL)
14973 *if_p = false;
14975 /* Mark if () ; with a special NOP_EXPR. */
14976 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14978 cp_lexer_consume_token (parser->lexer);
14979 statement = add_stmt (build_empty_stmt (body_loc));
14981 if (guard_tinfo.keyword == RID_IF
14982 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14983 warning_at (body_loc, OPT_Wempty_body,
14984 "suggest braces around empty body in an %<if%> statement");
14985 else if (guard_tinfo.keyword == RID_ELSE)
14986 warning_at (body_loc, OPT_Wempty_body,
14987 "suggest braces around empty body in an %<else%> statement");
14989 /* if a compound is opened, we simply parse the statement directly. */
14990 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14991 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14992 /* If the token is not a `{', then we must take special action. */
14993 else
14995 /* Create a compound-statement. */
14996 statement = begin_compound_stmt (0);
14997 /* Parse the dependent-statement. */
14998 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14999 &body_loc_after_labels);
15000 /* Finish the dummy compound-statement. */
15001 finish_compound_stmt (statement);
15004 token_indent_info next_tinfo
15005 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15006 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15008 if (body_loc_after_labels != UNKNOWN_LOCATION
15009 && next_tinfo.type != CPP_SEMICOLON)
15010 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
15011 guard_tinfo.location, guard_tinfo.keyword);
15013 /* Return the statement. */
15014 return statement;
15017 /* For some dependent statements (like `while (cond) statement'), we
15018 have already created a scope. Therefore, even if the dependent
15019 statement is a compound-statement, we do not want to create another
15020 scope. */
15022 static void
15023 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
15024 const token_indent_info &guard_tinfo)
15026 /* If the token is a `{', then we must take special action. */
15027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15029 token_indent_info body_tinfo
15030 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15031 location_t loc_after_labels = UNKNOWN_LOCATION;
15033 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
15034 &loc_after_labels);
15035 token_indent_info next_tinfo
15036 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15037 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15039 if (loc_after_labels != UNKNOWN_LOCATION
15040 && next_tinfo.type != CPP_SEMICOLON)
15041 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
15042 guard_tinfo.location,
15043 guard_tinfo.keyword);
15045 else
15047 /* Avoid calling cp_parser_compound_statement, so that we
15048 don't create a new scope. Do everything else by hand. */
15049 matching_braces braces;
15050 braces.require_open (parser);
15051 /* If the next keyword is `__label__' we have a label declaration. */
15052 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
15053 cp_parser_label_declaration (parser);
15054 /* Parse an (optional) statement-seq. */
15055 cp_parser_statement_seq_opt (parser, NULL_TREE);
15056 braces.require_close (parser);
15060 /* Modules */
15062 /* Parse a module-name or module-partition.
15064 module-name:
15065 module-name-qualifier [opt] identifier
15067 module-partition:
15068 : module-name-qualifier [opt] identifier
15070 module-name-qualifier:
15071 identifier .
15072 module-name-qualifier identifier .
15074 Returns a pointer to the module object, or NULL on failure.
15075 For PARTITION_P, PARENT is the module this is a partition of. */
15077 static module_state *
15078 cp_parser_module_name (cp_parser *parser, bool partition_p = false,
15079 module_state *parent = NULL)
15081 if (partition_p
15082 && cp_lexer_consume_token (parser->lexer)->type != CPP_COLON)
15083 return NULL;
15085 for (;;)
15087 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
15089 if (partition_p)
15090 cp_parser_error (parser, "expected module-partition");
15091 else
15092 cp_parser_error (parser, "expected module-name");
15093 return NULL;
15096 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
15097 parent = get_module (name, parent, partition_p);
15098 if (cp_lexer_peek_token (parser->lexer)->type != CPP_DOT)
15099 break;
15101 cp_lexer_consume_token (parser->lexer);
15104 return parent;
15107 /* Parse a module-partition. Defers to cp_parser_module_name. */
15109 static module_state *
15110 cp_parser_module_partition (cp_parser *parser, module_state *parent = NULL)
15112 return cp_parser_module_name (parser, /*partition_p=*/true, parent);
15115 /* Named module-declaration
15116 __module ; PRAGMA_EOL
15117 __module : private ; PRAGMA_EOL (unimplemented)
15118 [__export] __module module-name module-partition [opt]
15119 attr-spec-seq-opt ; PRAGMA_EOL
15122 static module_parse
15123 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
15124 bool exporting)
15126 /* We're a pseudo pragma. */
15127 parser->lexer->in_pragma = true;
15128 cp_token *token = cp_lexer_consume_token (parser->lexer);
15130 if (flag_header_unit)
15132 error_at (token->location,
15133 "module-declaration not permitted in header-unit");
15134 goto skip_eol;
15136 else if (mp_state == MP_FIRST && !exporting
15137 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15139 /* Start global module fragment. */
15140 cp_lexer_consume_token (parser->lexer);
15141 module_kind = MK_NAMED;
15142 mp_state = MP_GLOBAL;
15143 cp_parser_require_pragma_eol (parser, token);
15145 else if (!exporting
15146 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15147 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
15148 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
15150 cp_lexer_consume_token (parser->lexer);
15151 cp_lexer_consume_token (parser->lexer);
15152 cp_lexer_consume_token (parser->lexer);
15153 cp_parser_require_pragma_eol (parser, token);
15155 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
15156 && module_has_cmi_p ())
15158 mp_state = MP_PRIVATE_IMPORTS;
15159 sorry_at (token->location, "private module fragment");
15161 else
15162 error_at (token->location,
15163 "private module fragment only permitted in purview"
15164 " of module interface or partition");
15166 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
15168 /* Neither the first declaration, nor in a GMF. */
15169 error_at (token->location, "module-declaration only permitted as first"
15170 " declaration, or ending a global module fragment");
15171 skip_eol:
15172 cp_parser_skip_to_pragma_eol (parser, token);
15174 else
15176 module_state *mod = cp_parser_module_name (parser);
15177 if (mod && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
15178 mod = cp_parser_module_partition (parser, mod);
15179 tree attrs = cp_parser_attributes_opt (parser);
15181 if (mod)
15182 mp_state = MP_PURVIEW_IMPORTS;
15183 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15184 goto skip_eol;
15186 declare_module (mod, token->location, exporting, attrs, parse_in);
15187 cp_parser_require_pragma_eol (parser, token);
15190 return mp_state;
15193 /* Import-declaration
15194 __import module-name attr-spec-seq-opt ; PRAGMA_EOL
15195 __import module-partition attr-spec-seq-opt ; PRAGMA_EOL
15196 __import header-name attr-spec-seq-opt ; PRAGMA_EOL
15199 static void
15200 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
15201 bool exporting)
15203 /* We're a pseudo pragma. */
15204 parser->lexer->in_pragma = true;
15205 cp_token *token = cp_lexer_consume_token (parser->lexer);
15207 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
15209 error_at (token->location, "post-module-declaration"
15210 " imports must be contiguous");
15211 note_lexer:
15212 inform (token->location, "perhaps insert a line break, or other"
15213 " disambiguation, to prevent this being considered a"
15214 " module control-line");
15215 skip_eol:
15216 cp_parser_skip_to_pragma_eol (parser, token);
15218 else if (current_scope () != global_namespace)
15220 error_at (token->location, "import-declaration must be at global scope");
15221 goto note_lexer;
15223 else
15225 module_state *mod = NULL;
15226 cp_token *next = cp_lexer_peek_token (parser->lexer);
15227 if (next->type == CPP_HEADER_NAME)
15229 cp_lexer_consume_token (parser->lexer);
15230 mod = get_module (next->u.value);
15232 else if (next->type == CPP_COLON)
15234 /* An import specifying a module-partition shall only appear after the
15235 module-declaration in a module unit: [module.import]/4. */
15236 if (named_module_p ()
15237 && (mp_state == MP_PURVIEW_IMPORTS
15238 || mp_state == MP_PRIVATE_IMPORTS))
15239 mod = cp_parser_module_partition (parser);
15240 else
15241 error_at (next->location, "import specifying a module-partition"
15242 " must appear after a named module-declaration");
15244 else
15245 mod = cp_parser_module_name (parser);
15246 tree attrs = cp_parser_attributes_opt (parser);
15248 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15249 goto skip_eol;
15250 cp_parser_require_pragma_eol (parser, token);
15252 if (parser->in_unbraced_linkage_specification_p)
15253 error_at (token->location, "import cannot appear directly in"
15254 " a linkage-specification");
15256 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15258 /* Module-purview imports must not be from source inclusion
15259 [cpp.import]/7 */
15260 if (attrs
15261 && private_lookup_attribute ("__translated",
15262 strlen ("__translated"), attrs))
15263 error_at (token->location, "post-module-declaration imports"
15264 " must not be include-translated");
15265 else if (!token->main_source_p)
15266 error_at (token->location, "post-module-declaration imports"
15267 " must not be from header inclusion");
15270 import_module (mod, token->location, exporting, attrs, parse_in);
15274 /* export-declaration.
15276 export declaration
15277 export { declaration-seq-opt } */
15279 static void
15280 cp_parser_module_export (cp_parser *parser)
15282 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15283 cp_token *token = cp_lexer_consume_token (parser->lexer);
15285 if (!module_interface_p ())
15286 error_at (token->location,
15287 "%qE may only occur after a module interface declaration",
15288 token->u.value);
15290 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15292 unsigned mk = module_kind;
15293 if (module_exporting_p ())
15294 error_at (token->location,
15295 "%qE may only occur once in an export declaration",
15296 token->u.value);
15297 module_kind |= MK_EXPORTING;
15299 if (braced)
15301 cp_ensure_no_omp_declare_simd (parser);
15302 cp_ensure_no_oacc_routine (parser);
15304 cp_lexer_consume_token (parser->lexer);
15305 cp_parser_declaration_seq_opt (parser);
15306 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15308 else
15310 /* Explicitly check if the next tokens might be a
15311 module-directive line, so we can give a clearer error message
15312 about why the directive will be rejected. */
15313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15314 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15315 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15316 error_at (token->location, "%<export%> not part of following"
15317 " module-directive");
15318 cp_parser_declaration (parser, NULL_TREE);
15321 module_kind = mk;
15324 /* Declarations [gram.dcl.dcl] */
15326 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15327 is the top-level declaration sequence. That affects whether we
15328 deal with module-preamble.
15330 declaration-seq:
15331 declaration
15332 declaration-seq declaration */
15334 static void
15335 cp_parser_declaration_seq_opt (cp_parser* parser)
15337 while (true)
15339 cp_token *token = cp_lexer_peek_token (parser->lexer);
15341 if (token->type == CPP_CLOSE_BRACE
15342 || token->type == CPP_EOF)
15343 break;
15344 else
15345 cp_parser_toplevel_declaration (parser);
15349 /* Parse a declaration.
15351 declaration:
15352 block-declaration
15353 function-definition
15354 template-declaration
15355 explicit-instantiation
15356 explicit-specialization
15357 linkage-specification
15358 namespace-definition
15360 C++17:
15361 deduction-guide
15363 modules:
15364 (all these are only allowed at the outermost level, check
15365 that semantically, for better diagnostics)
15366 module-declaration
15367 module-export-declaration
15368 module-import-declaration
15369 export-declaration
15371 GNU extension:
15373 declaration:
15374 __extension__ declaration */
15376 static void
15377 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15379 int saved_pedantic;
15381 /* Check for the `__extension__' keyword. */
15382 if (cp_parser_extension_opt (parser, &saved_pedantic))
15384 /* Parse the qualified declaration. */
15385 cp_parser_declaration (parser, prefix_attrs);
15386 /* Restore the PEDANTIC flag. */
15387 pedantic = saved_pedantic;
15389 return;
15392 /* Try to figure out what kind of declaration is present. */
15393 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15394 cp_token *token2 = (token1->type == CPP_EOF
15395 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15397 if (token1->type == CPP_SEMICOLON)
15399 cp_lexer_consume_token (parser->lexer);
15400 /* A declaration consisting of a single semicolon is invalid
15401 * before C++11. Allow it unless we're being pedantic. */
15402 if (cxx_dialect < cxx11)
15403 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15404 return;
15406 else if (cp_lexer_nth_token_is (parser->lexer,
15407 cp_parser_skip_std_attribute_spec_seq (parser,
15409 CPP_SEMICOLON))
15411 location_t attrs_loc = token1->location;
15412 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15414 if (std_attrs && (flag_openmp || flag_openmp_simd))
15416 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15417 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15418 std_attrs);
15419 if (parser->lexer->in_omp_attribute_pragma)
15421 cp_lexer *lexer = parser->lexer;
15422 while (parser->lexer->in_omp_attribute_pragma)
15424 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15425 CPP_PRAGMA));
15426 cp_parser_pragma (parser, pragma_external, NULL);
15428 cp_lexer_destroy (lexer);
15432 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
15433 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15434 OPT_Wattributes, "attribute ignored");
15435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15436 cp_lexer_consume_token (parser->lexer);
15437 return;
15440 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15441 void *p = obstack_alloc (&declarator_obstack, 0);
15443 tree attributes = NULL_TREE;
15445 /* Conditionally, allow attributes to precede a linkage specification. */
15446 if (token1->keyword == RID_ATTRIBUTE)
15448 cp_lexer_save_tokens (parser->lexer);
15449 attributes = cp_parser_attributes_opt (parser);
15450 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15451 cp_token *t2 = (t1->type == CPP_EOF
15452 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15453 if (t1->keyword == RID_EXTERN
15454 && cp_parser_is_pure_string_literal (t2))
15456 cp_lexer_commit_tokens (parser->lexer);
15457 /* We might have already been here. */
15458 if (!c_dialect_objc ())
15460 location_t where = get_finish (t2->location);
15461 warning_at (token1->location, OPT_Wattributes, "attributes are"
15462 " not permitted in this position");
15463 where = linemap_position_for_loc_and_offset (line_table,
15464 where, 1);
15465 inform (where, "attributes may be inserted here");
15466 attributes = NULL_TREE;
15468 token1 = t1;
15469 token2 = t2;
15471 else
15473 cp_lexer_rollback_tokens (parser->lexer);
15474 attributes = NULL_TREE;
15477 /* If we already had some attributes, and we've added more, then prepend.
15478 Otherwise attributes just contains any that we just read. */
15479 if (prefix_attrs)
15481 if (attributes)
15482 TREE_CHAIN (prefix_attrs) = attributes;
15483 attributes = prefix_attrs;
15486 /* If the next token is `extern' and the following token is a string
15487 literal, then we have a linkage specification. */
15488 if (token1->keyword == RID_EXTERN
15489 && cp_parser_is_pure_string_literal (token2))
15490 cp_parser_linkage_specification (parser, attributes);
15491 /* If the next token is `template', then we have either a template
15492 declaration, an explicit instantiation, or an explicit
15493 specialization. */
15494 else if (token1->keyword == RID_TEMPLATE)
15496 /* `template <>' indicates a template specialization. */
15497 if (token2->type == CPP_LESS
15498 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15499 cp_parser_explicit_specialization (parser);
15500 /* `template <' indicates a template declaration. */
15501 else if (token2->type == CPP_LESS)
15502 cp_parser_template_declaration (parser, /*member_p=*/false);
15503 /* Anything else must be an explicit instantiation. */
15504 else
15505 cp_parser_explicit_instantiation (parser);
15507 /* If the next token is `export', it's new-style modules or
15508 old-style template. */
15509 else if (token1->keyword == RID_EXPORT)
15511 if (!modules_p ())
15512 cp_parser_template_declaration (parser, /*member_p=*/false);
15513 else
15514 cp_parser_module_export (parser);
15516 else if (cp_token_is_module_directive (token1))
15518 bool exporting = token1->keyword == RID__EXPORT;
15519 cp_token *next = exporting ? token2 : token1;
15520 if (exporting)
15521 cp_lexer_consume_token (parser->lexer);
15522 // In module purview this will be ill-formed.
15523 auto state = (!named_module_p () ? MP_NOT_MODULE
15524 : module_purview_p () ? MP_PURVIEW
15525 : MP_GLOBAL);
15526 if (next->keyword == RID__MODULE)
15527 cp_parser_module_declaration (parser, state, exporting);
15528 else
15529 cp_parser_import_declaration (parser, state, exporting);
15531 /* If the next token is `extern', 'static' or 'inline' and the one
15532 after that is `template', we have a GNU extended explicit
15533 instantiation directive. */
15534 else if (cp_parser_allow_gnu_extensions_p (parser)
15535 && token2->keyword == RID_TEMPLATE
15536 && (token1->keyword == RID_EXTERN
15537 || token1->keyword == RID_STATIC
15538 || token1->keyword == RID_INLINE))
15539 cp_parser_explicit_instantiation (parser);
15540 /* If the next token is `namespace', check for a named or unnamed
15541 namespace definition. */
15542 else if (token1->keyword == RID_NAMESPACE
15543 && (/* A named namespace definition. */
15544 (token2->type == CPP_NAME
15545 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15546 != CPP_EQ))
15547 || (token2->type == CPP_OPEN_SQUARE
15548 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15549 == CPP_OPEN_SQUARE)
15550 /* An unnamed namespace definition. */
15551 || token2->type == CPP_OPEN_BRACE
15552 || token2->keyword == RID_ATTRIBUTE))
15553 cp_parser_namespace_definition (parser);
15554 /* An inline (associated) namespace definition. */
15555 else if (token2->keyword == RID_NAMESPACE
15556 && token1->keyword == RID_INLINE)
15557 cp_parser_namespace_definition (parser);
15558 /* Objective-C++ declaration/definition. */
15559 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15560 cp_parser_objc_declaration (parser, attributes);
15561 else if (c_dialect_objc ()
15562 && token1->keyword == RID_ATTRIBUTE
15563 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15564 cp_parser_objc_declaration (parser, attributes);
15565 /* At this point we may have a template declared by a concept
15566 introduction. */
15567 else if (flag_concepts
15568 && cp_parser_template_declaration_after_export (parser,
15569 /*member_p=*/false))
15570 /* We did. */;
15571 else
15572 /* Try to parse a block-declaration, or a function-definition. */
15573 cp_parser_block_declaration (parser, /*statement_p=*/false);
15575 /* Free any declarators allocated. */
15576 obstack_free (&declarator_obstack, p);
15579 /* Parse a namespace-scope declaration. */
15581 static void
15582 cp_parser_toplevel_declaration (cp_parser* parser)
15584 cp_token *token = cp_lexer_peek_token (parser->lexer);
15586 if (token->type == CPP_PRAGMA)
15587 /* A top-level declaration can consist solely of a #pragma. A
15588 nested declaration cannot, so this is done here and not in
15589 cp_parser_declaration. (A #pragma at block scope is
15590 handled in cp_parser_statement.) */
15591 cp_parser_pragma (parser, pragma_external, NULL);
15592 else
15593 /* Parse the declaration itself. */
15594 cp_parser_declaration (parser, NULL_TREE);
15597 /* Parse a block-declaration.
15599 block-declaration:
15600 simple-declaration
15601 asm-definition
15602 namespace-alias-definition
15603 using-declaration
15604 using-directive
15606 GNU Extension:
15608 block-declaration:
15609 __extension__ block-declaration
15611 C++0x Extension:
15613 block-declaration:
15614 static_assert-declaration
15616 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15617 part of a declaration-statement. */
15619 static void
15620 cp_parser_block_declaration (cp_parser *parser,
15621 bool statement_p)
15623 int saved_pedantic;
15625 /* Check for the `__extension__' keyword. */
15626 if (cp_parser_extension_opt (parser, &saved_pedantic))
15628 /* Parse the qualified declaration. */
15629 cp_parser_block_declaration (parser, statement_p);
15630 /* Restore the PEDANTIC flag. */
15631 pedantic = saved_pedantic;
15633 return;
15636 /* Peek at the next token to figure out which kind of declaration is
15637 present. */
15638 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15640 /* If the next keyword is `asm', we have an asm-definition. */
15641 if (token1->keyword == RID_ASM)
15643 if (statement_p)
15644 cp_parser_commit_to_tentative_parse (parser);
15645 cp_parser_asm_definition (parser);
15647 /* If the next keyword is `namespace', we have a
15648 namespace-alias-definition. */
15649 else if (token1->keyword == RID_NAMESPACE)
15650 cp_parser_namespace_alias_definition (parser);
15651 /* If the next keyword is `using', we have a
15652 using-declaration, a using-directive, or an alias-declaration. */
15653 else if (token1->keyword == RID_USING)
15655 cp_token *token2;
15657 if (statement_p)
15658 cp_parser_commit_to_tentative_parse (parser);
15659 /* If the token after `using' is `namespace', then we have a
15660 using-directive. */
15661 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15662 if (token2->keyword == RID_NAMESPACE)
15663 cp_parser_using_directive (parser);
15664 else if (token2->keyword == RID_ENUM)
15665 cp_parser_using_enum (parser);
15666 /* If the second token after 'using' is '=', then we have an
15667 alias-declaration. */
15668 else if (cxx_dialect >= cxx11
15669 && token2->type == CPP_NAME
15670 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15671 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15672 cp_parser_alias_declaration (parser);
15673 /* Otherwise, it's a using-declaration. */
15674 else
15675 cp_parser_using_declaration (parser,
15676 /*access_declaration_p=*/false);
15678 /* If the next keyword is `__label__' we have a misplaced label
15679 declaration. */
15680 else if (token1->keyword == RID_LABEL)
15682 cp_lexer_consume_token (parser->lexer);
15683 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15684 cp_parser_skip_to_end_of_statement (parser);
15685 /* If the next token is now a `;', consume it. */
15686 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15687 cp_lexer_consume_token (parser->lexer);
15689 /* If the next token is `static_assert' we have a static assertion. */
15690 else if (token1->keyword == RID_STATIC_ASSERT)
15691 cp_parser_static_assert (parser, /*member_p=*/false);
15692 else
15694 size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1);
15695 cp_token *after_attr = NULL;
15696 if (attr_idx != 1)
15697 after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx);
15698 /* If the next tokens after attributes is `using namespace', then we have
15699 a using-directive. */
15700 if (after_attr
15701 && after_attr->keyword == RID_USING
15702 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15703 RID_NAMESPACE))
15705 if (statement_p)
15706 cp_parser_commit_to_tentative_parse (parser);
15707 cp_parser_using_directive (parser);
15709 /* If the next token after attributes is `asm', then we have
15710 an asm-definition. */
15711 else if (after_attr && after_attr->keyword == RID_ASM)
15713 if (statement_p)
15714 cp_parser_commit_to_tentative_parse (parser);
15715 cp_parser_asm_definition (parser);
15717 /* Anything else must be a simple-declaration. */
15718 else
15719 cp_parser_simple_declaration (parser, !statement_p,
15720 /*maybe_range_for_decl*/NULL);
15724 /* Parse a simple-declaration.
15726 simple-declaration:
15727 decl-specifier-seq [opt] init-declarator-list [opt] ;
15728 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15729 brace-or-equal-initializer ;
15731 init-declarator-list:
15732 init-declarator
15733 init-declarator-list , init-declarator
15735 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15736 function-definition as a simple-declaration.
15738 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15739 parsed declaration if it is an uninitialized single declarator not followed
15740 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15741 if present, will not be consumed. */
15743 static void
15744 cp_parser_simple_declaration (cp_parser* parser,
15745 bool function_definition_allowed_p,
15746 tree *maybe_range_for_decl)
15748 cp_decl_specifier_seq decl_specifiers;
15749 int declares_class_or_enum;
15750 bool saw_declarator;
15751 location_t comma_loc = UNKNOWN_LOCATION;
15752 location_t init_loc = UNKNOWN_LOCATION;
15754 if (maybe_range_for_decl)
15755 *maybe_range_for_decl = NULL_TREE;
15757 /* Defer access checks until we know what is being declared; the
15758 checks for names appearing in the decl-specifier-seq should be
15759 done as if we were in the scope of the thing being declared. */
15760 push_deferring_access_checks (dk_deferred);
15762 /* Parse the decl-specifier-seq. We have to keep track of whether
15763 or not the decl-specifier-seq declares a named class or
15764 enumeration type, since that is the only case in which the
15765 init-declarator-list is allowed to be empty.
15767 [dcl.dcl]
15769 In a simple-declaration, the optional init-declarator-list can be
15770 omitted only when declaring a class or enumeration, that is when
15771 the decl-specifier-seq contains either a class-specifier, an
15772 elaborated-type-specifier, or an enum-specifier. */
15773 cp_parser_decl_specifier_seq (parser,
15774 CP_PARSER_FLAGS_OPTIONAL,
15775 &decl_specifiers,
15776 &declares_class_or_enum);
15777 /* We no longer need to defer access checks. */
15778 stop_deferring_access_checks ();
15780 cp_omp_declare_simd_data odsd;
15781 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15782 cp_parser_handle_directive_omp_attributes (parser,
15783 &decl_specifiers.attributes,
15784 &odsd, true);
15786 /* In a block scope, a valid declaration must always have a
15787 decl-specifier-seq. By not trying to parse declarators, we can
15788 resolve the declaration/expression ambiguity more quickly. */
15789 if (!function_definition_allowed_p
15790 && !decl_specifiers.any_specifiers_p)
15792 cp_parser_error (parser, "expected declaration");
15793 goto done;
15796 /* If the next two tokens are both identifiers, the code is
15797 erroneous. The usual cause of this situation is code like:
15799 T t;
15801 where "T" should name a type -- but does not. */
15802 if (!decl_specifiers.any_type_specifiers_p
15803 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15805 /* If parsing tentatively, we should commit; we really are
15806 looking at a declaration. */
15807 cp_parser_commit_to_tentative_parse (parser);
15808 /* Give up. */
15809 goto done;
15812 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15814 /* Look for C++17 decomposition declaration. */
15815 for (size_t n = 1; ; n++)
15816 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15817 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15818 continue;
15819 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15820 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15821 && decl_specifiers.any_specifiers_p)
15823 tree decl
15824 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15825 maybe_range_for_decl,
15826 &init_loc);
15828 /* The next token should be either a `,' or a `;'. */
15829 cp_token *token = cp_lexer_peek_token (parser->lexer);
15830 /* If it's a `;', we are done. */
15831 if (token->type == CPP_SEMICOLON)
15832 goto finish;
15833 else if (maybe_range_for_decl)
15835 if (*maybe_range_for_decl == NULL_TREE)
15836 *maybe_range_for_decl = error_mark_node;
15837 goto finish;
15839 /* Anything else is an error. */
15840 else
15842 /* If we have already issued an error message we don't need
15843 to issue another one. */
15844 if ((decl != error_mark_node
15845 && DECL_INITIAL (decl) != error_mark_node)
15846 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15847 cp_parser_error (parser, "expected %<;%>");
15848 /* Skip tokens until we reach the end of the statement. */
15849 cp_parser_skip_to_end_of_statement (parser);
15850 /* If the next token is now a `;', consume it. */
15851 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15852 cp_lexer_consume_token (parser->lexer);
15853 goto done;
15856 else
15857 break;
15859 tree last_type;
15860 bool auto_specifier_p;
15861 /* NULL_TREE if both variable and function declaration are allowed,
15862 error_mark_node if function declaration are not allowed and
15863 a FUNCTION_DECL that should be diagnosed if it is followed by
15864 variable declarations. */
15865 tree auto_function_declaration;
15867 last_type = NULL_TREE;
15868 auto_specifier_p
15869 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15870 auto_function_declaration = NULL_TREE;
15872 /* Keep going until we hit the `;' at the end of the simple
15873 declaration. */
15874 saw_declarator = false;
15875 while (cp_lexer_next_token_is_not (parser->lexer,
15876 CPP_SEMICOLON))
15878 cp_token *token;
15879 bool function_definition_p;
15880 tree decl;
15881 tree auto_result = NULL_TREE;
15883 if (saw_declarator)
15885 /* If we are processing next declarator, comma is expected */
15886 token = cp_lexer_peek_token (parser->lexer);
15887 gcc_assert (token->type == CPP_COMMA);
15888 cp_lexer_consume_token (parser->lexer);
15889 if (maybe_range_for_decl)
15891 *maybe_range_for_decl = error_mark_node;
15892 if (comma_loc == UNKNOWN_LOCATION)
15893 comma_loc = token->location;
15896 else
15897 saw_declarator = true;
15899 /* Parse the init-declarator. */
15900 decl = cp_parser_init_declarator (parser,
15901 CP_PARSER_FLAGS_NONE,
15902 &decl_specifiers,
15903 /*checks=*/NULL,
15904 function_definition_allowed_p,
15905 /*member_p=*/false,
15906 declares_class_or_enum,
15907 &function_definition_p,
15908 maybe_range_for_decl,
15909 &init_loc,
15910 &auto_result);
15911 const bool fndecl_p = TREE_CODE (decl) == FUNCTION_DECL;
15912 /* If an error occurred while parsing tentatively, exit quickly.
15913 (That usually happens when in the body of a function; each
15914 statement is treated as a declaration-statement until proven
15915 otherwise.) */
15916 if (cp_parser_error_occurred (parser))
15917 goto done;
15919 if (auto_specifier_p && cxx_dialect >= cxx14)
15921 /* If the init-declarator-list contains more than one
15922 init-declarator, they shall all form declarations of
15923 variables. */
15924 if (auto_function_declaration == NULL_TREE)
15925 auto_function_declaration = fndecl_p ? decl : error_mark_node;
15926 else if (fndecl_p || auto_function_declaration != error_mark_node)
15928 error_at (decl_specifiers.locations[ds_type_spec],
15929 "non-variable %qD in declaration with more than one "
15930 "declarator with placeholder type",
15931 fndecl_p ? decl : auto_function_declaration);
15932 auto_function_declaration = error_mark_node;
15936 if (auto_result
15937 && (!processing_template_decl || !type_uses_auto (auto_result)))
15939 if (last_type
15940 && last_type != error_mark_node
15941 && !same_type_p (auto_result, last_type))
15943 /* If the list of declarators contains more than one declarator,
15944 the type of each declared variable is determined as described
15945 above. If the type deduced for the template parameter U is not
15946 the same in each deduction, the program is ill-formed. */
15947 error_at (decl_specifiers.locations[ds_type_spec],
15948 "inconsistent deduction for %qT: %qT and then %qT",
15949 decl_specifiers.type, last_type, auto_result);
15950 last_type = error_mark_node;
15952 else
15953 last_type = auto_result;
15956 /* Handle function definitions specially. */
15957 if (function_definition_p)
15959 /* If the next token is a `,', then we are probably
15960 processing something like:
15962 void f() {}, *p;
15964 which is erroneous. */
15965 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15967 cp_token *token = cp_lexer_peek_token (parser->lexer);
15968 error_at (token->location,
15969 "mixing"
15970 " declarations and function-definitions is forbidden");
15972 /* Otherwise, we're done with the list of declarators. */
15973 else
15975 pop_deferring_access_checks ();
15976 cp_finalize_omp_declare_simd (parser, &odsd);
15977 return;
15980 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15981 *maybe_range_for_decl = decl;
15982 /* The next token should be either a `,' or a `;'. */
15983 token = cp_lexer_peek_token (parser->lexer);
15984 /* If it's a `,', there are more declarators to come. */
15985 if (token->type == CPP_COMMA)
15986 /* will be consumed next time around */;
15987 /* If it's a `;', we are done. */
15988 else if (token->type == CPP_SEMICOLON)
15989 break;
15990 else if (maybe_range_for_decl)
15992 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15993 permerror (decl_specifiers.locations[ds_type_spec],
15994 "types may not be defined in a for-range-declaration");
15995 break;
15997 /* Anything else is an error. */
15998 else
16000 /* If we have already issued an error message we don't need
16001 to issue another one. */
16002 if ((decl != error_mark_node
16003 /* grokfndecl sets DECL_INITIAL to error_mark_node for
16004 functions. */
16005 && (fndecl_p || DECL_INITIAL (decl) != error_mark_node))
16006 || cp_parser_uncommitted_to_tentative_parse_p (parser))
16007 cp_parser_error (parser, "expected %<,%> or %<;%>");
16008 /* Skip tokens until we reach the end of the statement. */
16009 cp_parser_skip_to_end_of_statement (parser);
16010 /* If the next token is now a `;', consume it. */
16011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16012 cp_lexer_consume_token (parser->lexer);
16013 goto done;
16015 /* After the first time around, a function-definition is not
16016 allowed -- even if it was OK at first. For example:
16018 int i, f() {}
16020 is not valid. */
16021 function_definition_allowed_p = false;
16024 /* Issue an error message if no declarators are present, and the
16025 decl-specifier-seq does not itself declare a class or
16026 enumeration: [dcl.dcl]/3. */
16027 if (!saw_declarator)
16029 if (cp_parser_declares_only_class_p (parser))
16031 if (!declares_class_or_enum
16032 && decl_specifiers.type
16033 && OVERLOAD_TYPE_P (decl_specifiers.type))
16034 /* Ensure an error is issued anyway when finish_decltype_type,
16035 called via cp_parser_decl_specifier_seq, returns a class or
16036 an enumeration (c++/51786). */
16037 decl_specifiers.type = NULL_TREE;
16038 shadow_tag (&decl_specifiers);
16040 /* Perform any deferred access checks. */
16041 perform_deferred_access_checks (tf_warning_or_error);
16044 /* Consume the `;'. */
16045 finish:
16046 if (!maybe_range_for_decl)
16047 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16048 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16050 if (init_loc != UNKNOWN_LOCATION)
16051 error_at (init_loc, "initializer in range-based %<for%> loop");
16052 if (comma_loc != UNKNOWN_LOCATION)
16053 error_at (comma_loc,
16054 "multiple declarations in range-based %<for%> loop");
16057 done:
16058 pop_deferring_access_checks ();
16059 cp_finalize_omp_declare_simd (parser, &odsd);
16062 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
16063 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
16064 initializer ; */
16066 static tree
16067 cp_parser_decomposition_declaration (cp_parser *parser,
16068 cp_decl_specifier_seq *decl_specifiers,
16069 tree *maybe_range_for_decl,
16070 location_t *init_loc)
16072 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
16073 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16074 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
16076 /* Parse the identifier-list. */
16077 auto_vec<cp_expr, 10> v;
16078 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16079 while (true)
16081 cp_expr e = cp_parser_identifier (parser);
16082 if (e.get_value () == error_mark_node)
16083 break;
16084 v.safe_push (e);
16085 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16086 break;
16087 cp_lexer_consume_token (parser->lexer);
16090 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
16091 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16093 end_loc = UNKNOWN_LOCATION;
16094 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
16095 false);
16096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16097 cp_lexer_consume_token (parser->lexer);
16098 else
16100 cp_parser_skip_to_end_of_statement (parser);
16101 return error_mark_node;
16105 if (cxx_dialect < cxx17)
16106 pedwarn (loc, OPT_Wc__17_extensions,
16107 "structured bindings only available with "
16108 "%<-std=c++17%> or %<-std=gnu++17%>");
16110 tree pushed_scope;
16111 cp_declarator *declarator = make_declarator (cdk_decomp);
16112 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
16113 declarator->id_loc = loc;
16114 if (ref_qual != REF_QUAL_NONE)
16115 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
16116 ref_qual == REF_QUAL_RVALUE,
16117 NULL_TREE);
16118 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
16119 NULL_TREE, decl_specifiers->attributes,
16120 &pushed_scope);
16121 tree orig_decl = decl;
16123 unsigned int i;
16124 cp_expr e;
16125 cp_decl_specifier_seq decl_specs;
16126 clear_decl_specs (&decl_specs);
16127 decl_specs.type = make_auto ();
16128 if (decl_specifiers->storage_class == sc_static)
16129 decl_specs.storage_class = sc_static;
16130 tree prev = decl;
16131 FOR_EACH_VEC_ELT (v, i, e)
16133 if (i == 0)
16134 declarator = make_id_declarator (NULL_TREE, e.get_value (),
16135 sfk_none, e.get_location ());
16136 else
16138 declarator->u.id.unqualified_name = e.get_value ();
16139 declarator->id_loc = e.get_location ();
16141 tree elt_pushed_scope;
16142 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
16143 NULL_TREE, NULL_TREE, &elt_pushed_scope);
16144 if (decl2 == error_mark_node)
16145 decl = error_mark_node;
16146 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
16148 /* Ensure we've diagnosed redeclaration if we aren't creating
16149 a new VAR_DECL. */
16150 gcc_assert (errorcount);
16151 decl = error_mark_node;
16153 else
16154 prev = decl2;
16155 if (elt_pushed_scope)
16156 pop_scope (elt_pushed_scope);
16159 if (v.is_empty ())
16161 error_at (loc, "empty structured binding declaration");
16162 decl = error_mark_node;
16165 if (maybe_range_for_decl == NULL
16166 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16168 bool non_constant_p = false, is_direct_init = false;
16169 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
16170 tree initializer = cp_parser_initializer (parser, &is_direct_init,
16171 &non_constant_p);
16172 if (initializer == NULL_TREE
16173 || (TREE_CODE (initializer) == TREE_LIST
16174 && TREE_CHAIN (initializer))
16175 || (is_direct_init
16176 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
16177 && CONSTRUCTOR_NELTS (initializer) != 1))
16179 error_at (loc, "invalid initializer for structured binding "
16180 "declaration");
16181 initializer = error_mark_node;
16184 if (decl != error_mark_node)
16186 cp_decomp decomp = { prev, v.length () };
16187 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
16188 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
16189 &decomp);
16190 cp_finish_decomp (decl, &decomp);
16193 else if (decl != error_mark_node)
16195 *maybe_range_for_decl = prev;
16196 cp_decomp decomp = { prev, v.length () };
16197 /* Ensure DECL_VALUE_EXPR is created for all the decls but
16198 the underlying DECL. */
16199 cp_finish_decomp (decl, &decomp);
16202 if (pushed_scope)
16203 pop_scope (pushed_scope);
16205 if (decl == error_mark_node && DECL_P (orig_decl))
16207 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
16208 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
16211 return decl;
16214 /* Names of storage classes. */
16216 static const char *const
16217 cp_storage_class_name[] = {
16218 "", "auto", "register", "static", "extern", "mutable"
16221 /* Parse a decl-specifier-seq.
16223 decl-specifier-seq:
16224 decl-specifier-seq [opt] decl-specifier
16225 decl-specifier attribute-specifier-seq [opt] (C++11)
16227 decl-specifier:
16228 storage-class-specifier
16229 type-specifier
16230 function-specifier
16231 friend
16232 typedef
16234 GNU Extension:
16236 decl-specifier:
16237 attributes
16239 Concepts Extension:
16241 decl-specifier:
16242 concept
16244 Set *DECL_SPECS to a representation of the decl-specifier-seq.
16246 The parser flags FLAGS is used to control type-specifier parsing.
16248 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16249 flags:
16251 1: one of the decl-specifiers is an elaborated-type-specifier
16252 (i.e., a type declaration)
16253 2: one of the decl-specifiers is an enum-specifier or a
16254 class-specifier (i.e., a type definition)
16258 static void
16259 cp_parser_decl_specifier_seq (cp_parser* parser,
16260 cp_parser_flags flags,
16261 cp_decl_specifier_seq *decl_specs,
16262 int* declares_class_or_enum)
16264 bool constructor_possible_p = !parser->in_declarator_p;
16265 bool found_decl_spec = false;
16266 cp_token *start_token = NULL;
16267 cp_decl_spec ds;
16269 /* Clear DECL_SPECS. */
16270 clear_decl_specs (decl_specs);
16272 /* Assume no class or enumeration type is declared. */
16273 *declares_class_or_enum = 0;
16275 /* Keep a token that additionally will be used for diagnostics. */
16276 cp_token *first_specifier = NULL;
16277 /* Keep reading specifiers until there are no more to read. */
16278 while (true)
16280 bool constructor_p;
16281 cp_token *token;
16282 ds = ds_last;
16284 /* Peek at the next token. */
16285 token = cp_lexer_peek_token (parser->lexer);
16287 /* Save the first token of the decl spec list for error
16288 reporting. */
16289 if (!start_token)
16290 start_token = token;
16291 /* Handle attributes. */
16292 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16293 && cp_next_tokens_can_be_attribute_p (parser))
16295 /* Parse the attributes. */
16296 tree attrs = cp_parser_attributes_opt (parser);
16298 /* In a sequence of declaration specifiers, c++11 attributes
16299 appertain to the type that precede them. In that case
16300 [dcl.spec]/1 says:
16302 The attribute-specifier-seq affects the type only for
16303 the declaration it appears in, not other declarations
16304 involving the same type.
16306 But for now let's force the user to position the
16307 attribute either at the beginning of the declaration or
16308 after the declarator-id, which would clearly mean that it
16309 applies to the declarator. */
16310 if (cxx11_attribute_p (attrs))
16312 if (!found_decl_spec)
16313 /* The c++11 attribute is at the beginning of the
16314 declaration. It appertains to the entity being
16315 declared. */;
16316 else
16318 if (find_contract (attrs))
16320 diagnose_misapplied_contracts (attrs);
16321 attrs = NULL_TREE;
16323 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16325 /* This is an attribute following a
16326 class-specifier. */
16327 if (decl_specs->type_definition_p)
16328 warn_misplaced_attr_for_class_type (token->location,
16329 decl_specs->type);
16330 attrs = NULL_TREE;
16332 else
16334 decl_specs->std_attributes
16335 = attr_chainon (decl_specs->std_attributes, attrs);
16336 if (decl_specs->locations[ds_std_attribute] == 0)
16337 decl_specs->locations[ds_std_attribute] = token->location;
16339 continue;
16343 decl_specs->attributes
16344 = attr_chainon (decl_specs->attributes, attrs);
16345 if (decl_specs->locations[ds_attribute] == 0)
16346 decl_specs->locations[ds_attribute] = token->location;
16347 continue;
16349 /* We know by this point that the token is not part of an attribute. */
16350 if (!first_specifier)
16351 first_specifier = token;
16352 /* Special case for "this" specifier, indicating a parm is an xobj parm.
16353 The "this" specifier must be the first specifier in the declaration,
16354 after any attributes. */
16355 if (token->keyword == RID_THIS && (flags & CP_PARSER_FLAGS_PARAMETER))
16357 cp_lexer_consume_token (parser->lexer);
16358 if (token != first_specifier)
16360 /* Don't emit diagnostics if we have already seen "this",
16361 leave it for set_and_check_decl_spec_loc. */
16362 if (decl_specs->locations[ds_this] == 0)
16364 auto_diagnostic_group d;
16365 gcc_rich_location richloc (token->location);
16366 /* Works, need to add tests for it though. */
16367 richloc.add_fixit_remove ();
16368 richloc.add_fixit_insert_before (first_specifier->location,
16369 "this ");
16370 error_at (&richloc,
16371 "%<this%> must be the first specifier "
16372 "in a parameter declaration");
16375 set_and_check_decl_spec_loc (decl_specs, ds_this, token);
16376 continue;
16379 /* Assume we will find a decl-specifier keyword. */
16380 found_decl_spec = true;
16381 /* If the next token is an appropriate keyword, we can simply
16382 add it to the list. */
16383 switch (token->keyword)
16385 /* decl-specifier:
16386 friend
16387 constexpr
16388 constinit */
16389 case RID_FRIEND:
16390 if (!at_class_scope_p ())
16392 gcc_rich_location richloc (token->location);
16393 richloc.add_fixit_remove ();
16394 error_at (&richloc, "%<friend%> used outside of class");
16395 cp_lexer_purge_token (parser->lexer);
16397 else
16399 ds = ds_friend;
16400 /* Consume the token. */
16401 cp_lexer_consume_token (parser->lexer);
16403 break;
16405 case RID_CONSTEXPR:
16406 ds = ds_constexpr;
16407 cp_lexer_consume_token (parser->lexer);
16408 break;
16410 case RID_CONSTINIT:
16411 ds = ds_constinit;
16412 cp_lexer_consume_token (parser->lexer);
16413 break;
16415 case RID_CONSTEVAL:
16416 ds = ds_consteval;
16417 cp_lexer_consume_token (parser->lexer);
16418 break;
16420 case RID_CONCEPT:
16421 ds = ds_concept;
16422 cp_lexer_consume_token (parser->lexer);
16424 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16425 break;
16427 /* Warn for concept as a decl-specifier. We'll rewrite these as
16428 concept declarations later. */
16429 if (!flag_concepts_ts)
16431 cp_token *next = cp_lexer_peek_token (parser->lexer);
16432 if (next->keyword == RID_BOOL)
16433 permerror (next->location, "the %<bool%> keyword is not "
16434 "allowed in a C++20 concept definition");
16435 else
16436 error_at (token->location, "C++20 concept definition syntax "
16437 "is %<concept <name> = <expr>%>");
16440 /* In C++20 a concept definition is just 'concept name = expr;'
16441 Support that syntax as a TS extension by pretending we've seen
16442 the 'bool' specifier. */
16443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16444 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16445 && !decl_specs->any_type_specifiers_p)
16447 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16448 token, /*type_definition*/false);
16449 decl_specs->any_type_specifiers_p = true;
16451 break;
16453 /* function-specifier:
16454 inline
16455 virtual
16456 explicit */
16457 case RID_INLINE:
16458 case RID_VIRTUAL:
16459 case RID_EXPLICIT:
16460 cp_parser_function_specifier_opt (parser, decl_specs);
16461 break;
16463 /* decl-specifier:
16464 typedef */
16465 case RID_TYPEDEF:
16466 ds = ds_typedef;
16467 /* Consume the token. */
16468 cp_lexer_consume_token (parser->lexer);
16470 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16471 break;
16473 /* A constructor declarator cannot appear in a typedef. */
16474 constructor_possible_p = false;
16475 /* The "typedef" keyword can only occur in a declaration; we
16476 may as well commit at this point. */
16477 cp_parser_commit_to_tentative_parse (parser);
16479 if (decl_specs->storage_class != sc_none)
16481 if (decl_specs->conflicting_specifiers_p)
16482 break;
16483 gcc_rich_location richloc (token->location);
16484 location_t oloc = decl_specs->locations[ds_storage_class];
16485 richloc.add_location_if_nearby (oloc);
16486 error_at (&richloc,
16487 "%<typedef%> specifier conflicts with %qs",
16488 cp_storage_class_name[decl_specs->storage_class]);
16489 decl_specs->conflicting_specifiers_p = true;
16491 break;
16493 /* storage-class-specifier:
16494 auto
16495 register
16496 static
16497 extern
16498 mutable
16500 GNU Extension:
16501 thread */
16502 case RID_AUTO:
16503 if (cxx_dialect == cxx98)
16505 /* Consume the token. */
16506 cp_lexer_consume_token (parser->lexer);
16508 /* Complain about `auto' as a storage specifier, if
16509 we're complaining about C++0x compatibility. */
16510 gcc_rich_location richloc (token->location);
16511 richloc.add_fixit_remove ();
16512 warning_at (&richloc, OPT_Wc__11_compat,
16513 "%<auto%> changes meaning in C++11; "
16514 "please remove it");
16516 /* Set the storage class anyway. */
16517 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16518 token);
16520 else
16521 /* C++0x auto type-specifier. */
16522 found_decl_spec = false;
16523 break;
16525 case RID_REGISTER:
16526 case RID_STATIC:
16527 case RID_EXTERN:
16528 case RID_MUTABLE:
16529 /* Consume the token. */
16530 cp_lexer_consume_token (parser->lexer);
16531 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16532 token);
16533 break;
16534 case RID_THREAD:
16535 /* Consume the token. */
16536 ds = ds_thread;
16537 cp_lexer_consume_token (parser->lexer);
16538 break;
16540 default:
16541 /* We did not yet find a decl-specifier yet. */
16542 found_decl_spec = false;
16543 break;
16546 if (found_decl_spec
16547 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16548 && token->keyword != RID_CONSTEXPR)
16549 error ("%qD invalid in condition", ridpointers[token->keyword]);
16551 if (found_decl_spec
16552 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16553 && token->keyword != RID_MUTABLE
16554 && token->keyword != RID_CONSTEXPR
16555 && token->keyword != RID_CONSTEVAL)
16557 if (token->keyword != RID_STATIC)
16558 error_at (token->location, "%qD invalid in lambda",
16559 ridpointers[token->keyword]);
16560 else if (cxx_dialect < cxx23)
16561 pedwarn (token->location, OPT_Wc__23_extensions,
16562 "%qD only valid in lambda with %<-std=c++23%> or "
16563 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16566 if (ds != ds_last)
16567 set_and_check_decl_spec_loc (decl_specs, ds, token);
16569 /* Constructors are a special case. The `S' in `S()' is not a
16570 decl-specifier; it is the beginning of the declarator. */
16571 constructor_p
16572 = (!found_decl_spec
16573 && constructor_possible_p
16574 && (cp_parser_constructor_declarator_p
16575 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16576 ds_friend))));
16578 /* If we don't have a DECL_SPEC yet, then we must be looking at
16579 a type-specifier. */
16580 if (!found_decl_spec && !constructor_p)
16582 int decl_spec_declares_class_or_enum;
16583 bool is_cv_qualifier;
16584 tree type_spec;
16586 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16587 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16589 type_spec
16590 = cp_parser_type_specifier (parser, flags,
16591 decl_specs,
16592 /*is_declaration=*/true,
16593 &decl_spec_declares_class_or_enum,
16594 &is_cv_qualifier);
16595 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16597 /* If this type-specifier referenced a user-defined type
16598 (a typedef, class-name, etc.), then we can't allow any
16599 more such type-specifiers henceforth.
16601 [dcl.spec]
16603 The longest sequence of decl-specifiers that could
16604 possibly be a type name is taken as the
16605 decl-specifier-seq of a declaration. The sequence shall
16606 be self-consistent as described below.
16608 [dcl.type]
16610 As a general rule, at most one type-specifier is allowed
16611 in the complete decl-specifier-seq of a declaration. The
16612 only exceptions are the following:
16614 -- const or volatile can be combined with any other
16615 type-specifier.
16617 -- signed or unsigned can be combined with char, long,
16618 short, or int.
16620 -- ..
16622 Example:
16624 typedef char* Pc;
16625 void g (const int Pc);
16627 Here, Pc is *not* part of the decl-specifier seq; it's
16628 the declarator. Therefore, once we see a type-specifier
16629 (other than a cv-qualifier), we forbid any additional
16630 user-defined types. We *do* still allow things like `int
16631 int' to be considered a decl-specifier-seq, and issue the
16632 error message later. */
16633 if (type_spec && !is_cv_qualifier)
16634 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16635 /* A constructor declarator cannot follow a type-specifier. */
16636 if (type_spec)
16638 constructor_possible_p = false;
16639 found_decl_spec = true;
16640 if (!is_cv_qualifier)
16641 decl_specs->any_type_specifiers_p = true;
16643 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16644 error_at (token->location, "type-specifier invalid in lambda");
16648 /* If we still do not have a DECL_SPEC, then there are no more
16649 decl-specifiers. */
16650 if (!found_decl_spec)
16651 break;
16653 if (decl_specs->std_attributes)
16655 error_at (decl_specs->locations[ds_std_attribute],
16656 "standard attributes in middle of decl-specifiers");
16657 inform (decl_specs->locations[ds_std_attribute],
16658 "standard attributes must precede the decl-specifiers to "
16659 "apply to the declaration, or follow them to apply to "
16660 "the type");
16663 decl_specs->any_specifiers_p = true;
16664 /* After we see one decl-specifier, further decl-specifiers are
16665 always optional. */
16666 flags |= CP_PARSER_FLAGS_OPTIONAL;
16669 /* Don't allow a friend specifier with a class definition. */
16670 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16671 && (*declares_class_or_enum & 2))
16672 error_at (decl_specs->locations[ds_friend],
16673 "class definition may not be declared a friend");
16676 /* Parse an (optional) storage-class-specifier.
16678 storage-class-specifier:
16679 auto
16680 register
16681 static
16682 extern
16683 mutable
16685 GNU Extension:
16687 storage-class-specifier:
16688 thread
16690 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16692 static tree
16693 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16695 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16697 case RID_AUTO:
16698 if (cxx_dialect != cxx98)
16699 return NULL_TREE;
16700 /* Fall through for C++98. */
16701 gcc_fallthrough ();
16703 case RID_REGISTER:
16704 case RID_STATIC:
16705 case RID_EXTERN:
16706 case RID_MUTABLE:
16707 case RID_THREAD:
16708 /* Consume the token. */
16709 return cp_lexer_consume_token (parser->lexer)->u.value;
16711 default:
16712 return NULL_TREE;
16716 /* Parse an (optional) function-specifier.
16718 function-specifier:
16719 inline
16720 virtual
16721 explicit
16723 C++20 Extension:
16724 explicit(constant-expression)
16726 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16727 Updates DECL_SPECS, if it is non-NULL. */
16729 static tree
16730 cp_parser_function_specifier_opt (cp_parser* parser,
16731 cp_decl_specifier_seq *decl_specs)
16733 cp_token *token = cp_lexer_peek_token (parser->lexer);
16734 switch (token->keyword)
16736 case RID_INLINE:
16737 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16738 break;
16740 case RID_VIRTUAL:
16741 /* 14.5.2.3 [temp.mem]
16743 A member function template shall not be virtual. */
16744 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16745 && current_class_type)
16746 error_at (token->location, "templates may not be %<virtual%>");
16747 else
16748 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16749 break;
16751 case RID_EXPLICIT:
16753 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16754 /* If we see '(', it's C++20 explicit(bool). */
16755 tree expr;
16756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16758 matching_parens parens;
16759 parens.consume_open (parser);
16761 /* New types are not allowed in an explicit-specifier. */
16762 const char *saved_message
16763 = parser->type_definition_forbidden_message;
16764 parser->type_definition_forbidden_message
16765 = G_("types may not be defined in explicit-specifier");
16767 if (cxx_dialect < cxx20)
16768 pedwarn (token->location, OPT_Wc__20_extensions,
16769 "%<explicit(bool)%> only available with %<-std=c++20%> "
16770 "or %<-std=gnu++20%>");
16772 /* Parse the constant-expression. */
16773 expr = cp_parser_constant_expression (parser);
16775 /* Restore the saved message. */
16776 parser->type_definition_forbidden_message = saved_message;
16777 parens.require_close (parser);
16779 else
16780 /* The explicit-specifier explicit without a constant-expression is
16781 equivalent to the explicit-specifier explicit(true). */
16782 expr = boolean_true_node;
16784 /* [dcl.fct.spec]
16785 "the constant-expression, if supplied, shall be a contextually
16786 converted constant expression of type bool." */
16787 expr = build_explicit_specifier (expr, tf_warning_or_error);
16788 /* We could evaluate it -- mark the decl as appropriate. */
16789 if (expr == boolean_true_node)
16790 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16791 else if (expr == boolean_false_node)
16792 /* Don't mark the decl as explicit. */;
16793 else if (decl_specs)
16794 /* The expression was value-dependent. Remember it so that we can
16795 substitute it later. */
16796 decl_specs->explicit_specifier = expr;
16797 return id;
16800 default:
16801 return NULL_TREE;
16804 /* Consume the token. */
16805 return cp_lexer_consume_token (parser->lexer)->u.value;
16808 /* Parse a linkage-specification.
16810 linkage-specification:
16811 extern string-literal { declaration-seq [opt] }
16812 extern string-literal declaration */
16814 static void
16815 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16817 /* Look for the `extern' keyword. */
16818 cp_token *extern_token
16819 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16821 /* Look for the string-literal. */
16822 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16823 tree linkage;
16824 if (cxx_dialect >= cxx26)
16825 linkage = cp_parser_unevaluated_string_literal (parser);
16826 else
16827 linkage = cp_parser_string_literal (parser, /*translate=*/false,
16828 /*wide_ok=*/false);
16830 /* Transform the literal into an identifier. If the literal is a
16831 wide-character string, or contains embedded NULs, then we can't
16832 handle it as the user wants. */
16833 if (linkage == error_mark_node
16834 || strlen (TREE_STRING_POINTER (linkage))
16835 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16837 cp_parser_error (parser, "invalid linkage-specification");
16838 /* Assume C++ linkage. */
16839 linkage = lang_name_cplusplus;
16841 else
16842 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16844 /* We're now using the new linkage. */
16845 unsigned saved_module = module_kind;
16846 module_kind &= ~MK_ATTACH;
16847 push_lang_context (linkage);
16849 /* Preserve the location of the innermost linkage specification,
16850 tracking the locations of nested specifications via a local. */
16851 location_t saved_location
16852 = parser->innermost_linkage_specification_location;
16853 /* Construct a location ranging from the start of the "extern" to
16854 the end of the string-literal, with the caret at the start, e.g.:
16855 extern "C" {
16856 ^~~~~~~~~~
16858 parser->innermost_linkage_specification_location
16859 = make_location (extern_token->location,
16860 extern_token->location,
16861 get_finish (string_token->location));
16863 /* If the next token is a `{', then we're using the first
16864 production. */
16865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16867 cp_ensure_no_omp_declare_simd (parser);
16868 cp_ensure_no_oacc_routine (parser);
16870 /* Consume the `{' token. */
16871 matching_braces braces;
16872 braces.consume_open (parser);
16873 /* Parse the declarations. */
16874 cp_parser_declaration_seq_opt (parser);
16875 /* Look for the closing `}'. */
16876 braces.require_close (parser);
16878 /* Otherwise, there's just one declaration. */
16879 else
16881 bool saved_in_unbraced_linkage_specification_p;
16883 saved_in_unbraced_linkage_specification_p
16884 = parser->in_unbraced_linkage_specification_p;
16885 parser->in_unbraced_linkage_specification_p = true;
16886 cp_parser_declaration (parser, prefix_attr);
16887 parser->in_unbraced_linkage_specification_p
16888 = saved_in_unbraced_linkage_specification_p;
16891 /* We're done with the linkage-specification. */
16892 pop_lang_context ();
16893 module_kind = saved_module;
16895 /* Restore location of parent linkage specification, if any. */
16896 parser->innermost_linkage_specification_location = saved_location;
16899 /* Parse a static_assert-declaration.
16901 static_assert-declaration:
16902 static_assert ( constant-expression , string-literal ) ;
16903 static_assert ( constant-expression ) ; (C++17)
16904 static_assert ( constant-expression, conditional-expression ) ; (C++26)
16906 If MEMBER_P, this static_assert is a class member. */
16908 static void
16909 cp_parser_static_assert (cp_parser *parser, bool member_p)
16911 cp_expr condition;
16912 location_t token_loc;
16913 tree message;
16915 /* Peek at the `static_assert' token so we can keep track of exactly
16916 where the static assertion started. */
16917 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16919 /* Look for the `static_assert' keyword. */
16920 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16921 RT_STATIC_ASSERT))
16922 return;
16924 /* We know we are in a static assertion; commit to any tentative
16925 parse. */
16926 if (cp_parser_parsing_tentatively (parser))
16927 cp_parser_commit_to_tentative_parse (parser);
16929 /* Parse the `(' starting the static assertion condition. */
16930 matching_parens parens;
16931 parens.require_open (parser);
16933 /* Parse the constant-expression. Allow a non-constant expression
16934 here in order to give better diagnostics in finish_static_assert. */
16935 condition
16936 = cp_parser_constant_expression (parser,
16937 /*allow_non_constant_p=*/true,
16938 /*non_constant_p=*/nullptr);
16940 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16942 if (pedantic && cxx_dialect < cxx17)
16943 pedwarn (input_location, OPT_Wc__17_extensions,
16944 "%<static_assert%> without a message "
16945 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16946 /* Eat the ')' */
16947 cp_lexer_consume_token (parser->lexer);
16948 message = build_string (1, "");
16949 TREE_TYPE (message) = char_array_type_node;
16950 fix_string_type (message);
16952 else
16954 /* Parse the separating `,'. */
16955 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16957 /* Parse the message expression. */
16958 bool string_lit = true;
16959 for (unsigned int i = 1; ; ++i)
16961 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, i);
16962 if (cp_parser_is_pure_string_literal (tok))
16963 continue;
16964 else if (tok->type == CPP_CLOSE_PAREN)
16965 break;
16966 string_lit = false;
16967 break;
16969 if (!string_lit)
16971 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16972 if (cxx_dialect < cxx26)
16973 pedwarn (loc, OPT_Wc__26_extensions,
16974 "%<static_assert%> with non-string message only "
16975 "available with %<-std=c++2c%> or %<-std=gnu++2c%>");
16977 message = cp_parser_conditional_expression (parser);
16978 if (TREE_CODE (message) == STRING_CST)
16979 message = build1_loc (loc, PAREN_EXPR, TREE_TYPE (message),
16980 message);
16982 else if (cxx_dialect >= cxx26)
16983 message = cp_parser_unevaluated_string_literal (parser);
16984 else
16985 message = cp_parser_string_literal (parser, /*translate=*/false,
16986 /*wide_ok=*/true);
16988 /* A `)' completes the static assertion. */
16989 if (!parens.require_close (parser))
16990 cp_parser_skip_to_closing_parenthesis (parser,
16991 /*recovering=*/true,
16992 /*or_comma=*/false,
16993 /*consume_paren=*/true);
16996 /* A semicolon terminates the declaration. */
16997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16999 /* Get the location for the static assertion. Use that of the
17000 condition if available, otherwise, use that of the "static_assert"
17001 token. */
17002 location_t assert_loc = condition.get_location ();
17003 if (assert_loc == UNKNOWN_LOCATION)
17004 assert_loc = token_loc;
17006 /* Complete the static assertion, which may mean either processing
17007 the static assert now or saving it for template instantiation. */
17008 finish_static_assert (condition, message, assert_loc, member_p,
17009 /*show_expr_p=*/false);
17012 /* Parse the expression in decltype ( expression ). */
17014 static tree
17015 cp_parser_decltype_expr (cp_parser *parser,
17016 bool &id_expression_or_member_access_p)
17018 cp_token *id_expr_start_token;
17019 tree expr;
17021 /* First, try parsing an id-expression. */
17022 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
17023 cp_parser_parse_tentatively (parser);
17024 expr = cp_parser_id_expression (parser,
17025 /*template_keyword_p=*/false,
17026 /*check_dependency_p=*/true,
17027 /*template_p=*/NULL,
17028 /*declarator_p=*/false,
17029 /*optional_p=*/false);
17031 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
17033 bool non_integral_constant_expression_p = false;
17034 tree id_expression = expr;
17035 cp_id_kind idk;
17036 const char *error_msg;
17038 if (identifier_p (expr))
17039 /* Lookup the name we got back from the id-expression. */
17040 expr = cp_parser_lookup_name_simple (parser, expr,
17041 id_expr_start_token->location);
17043 if (expr
17044 && expr != error_mark_node
17045 && TREE_CODE (expr) != TYPE_DECL
17046 && (TREE_CODE (expr) != BIT_NOT_EXPR
17047 || !TYPE_P (TREE_OPERAND (expr, 0)))
17048 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17050 /* Complete lookup of the id-expression. */
17051 expr = (finish_id_expression
17052 (id_expression, expr, parser->scope, &idk,
17053 /*integral_constant_expression_p=*/false,
17054 /*allow_non_integral_constant_expression_p=*/true,
17055 &non_integral_constant_expression_p,
17056 /*template_p=*/false,
17057 /*done=*/true,
17058 /*address_p=*/false,
17059 /*template_arg_p=*/false,
17060 &error_msg,
17061 id_expr_start_token->location));
17063 if (error_msg)
17065 /* We found an id-expression, but it was something that we
17066 should not have found. This is an error, not something
17067 we can recover from, so report the error we found and
17068 we'll recover as gracefully as possible. */
17069 cp_parser_parse_definitely (parser);
17070 cp_parser_error (parser, error_msg);
17071 id_expression_or_member_access_p = true;
17072 return error_mark_node;
17076 if (expr
17077 && expr != error_mark_node
17078 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17079 /* We have an id-expression. */
17080 id_expression_or_member_access_p = true;
17083 if (!id_expression_or_member_access_p)
17085 /* Abort the id-expression parse. */
17086 cp_parser_abort_tentative_parse (parser);
17088 /* Parsing tentatively, again. */
17089 cp_parser_parse_tentatively (parser);
17091 /* Parse a class member access. */
17092 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
17093 /*cast_p=*/false, /*decltype*/true,
17094 /*member_access_only_p=*/true, NULL);
17096 if (expr
17097 && expr != error_mark_node
17098 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17099 /* We have an id-expression. */
17100 id_expression_or_member_access_p = true;
17103 if (id_expression_or_member_access_p)
17104 /* We have parsed the complete id-expression or member access. */
17105 cp_parser_parse_definitely (parser);
17106 else
17108 /* Abort our attempt to parse an id-expression or member access
17109 expression. */
17110 cp_parser_abort_tentative_parse (parser);
17112 /* Parse a full expression. */
17113 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
17114 /*decltype_p=*/true);
17117 return expr;
17120 /* Parse a `decltype' type. Returns the type.
17122 decltype-specifier:
17123 decltype ( expression )
17124 C++14:
17125 decltype ( auto ) */
17127 static tree
17128 cp_parser_decltype (cp_parser *parser)
17130 bool id_expression_or_member_access_p = false;
17131 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
17133 if (start_token->type == CPP_DECLTYPE)
17135 /* Already parsed. */
17136 cp_lexer_consume_token (parser->lexer);
17137 return saved_checks_value (start_token->u.tree_check_value);
17140 /* Look for the `decltype' token. */
17141 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
17142 return error_mark_node;
17144 /* Parse the opening `('. */
17145 matching_parens parens;
17146 if (!parens.require_open (parser))
17147 return error_mark_node;
17149 /* Since we're going to preserve any side-effects from this parse, set up a
17150 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17151 in the expression. */
17152 tentative_firewall firewall (parser);
17154 /* If in_declarator_p, a reparse as an expression might succeed (60361).
17155 Otherwise, commit now for better diagnostics. */
17156 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
17157 && !parser->in_declarator_p)
17158 cp_parser_commit_to_topmost_tentative_parse (parser);
17160 push_deferring_access_checks (dk_deferred);
17162 tree expr = NULL_TREE;
17164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
17165 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
17167 /* decltype (auto) */
17168 cp_lexer_consume_token (parser->lexer);
17169 if (cxx_dialect < cxx14)
17171 error_at (start_token->location,
17172 "%<decltype(auto)%> type specifier only available with "
17173 "%<-std=c++14%> or %<-std=gnu++14%>");
17174 expr = error_mark_node;
17177 else
17179 /* decltype (expression) */
17181 /* Types cannot be defined in a `decltype' expression. Save away the
17182 old message and set the new one. */
17183 const char *saved_message = parser->type_definition_forbidden_message;
17184 parser->type_definition_forbidden_message
17185 = G_("types may not be defined in %<decltype%> expressions");
17187 /* The restrictions on constant-expressions do not apply inside
17188 decltype expressions. */
17189 bool saved_integral_constant_expression_p
17190 = parser->integral_constant_expression_p;
17191 bool saved_non_integral_constant_expression_p
17192 = parser->non_integral_constant_expression_p;
17193 parser->integral_constant_expression_p = false;
17195 /* Within a parenthesized expression, a `>' token is always
17196 the greater-than operator. */
17197 bool saved_greater_than_is_operator_p
17198 = parser->greater_than_is_operator_p;
17199 parser->greater_than_is_operator_p = true;
17201 /* Don't synthesize an implicit template type parameter here. This
17202 could happen with C++23 code like
17204 void f(decltype(new auto{0}));
17206 where we want to deduce the auto right away so that the parameter
17207 is of type 'int *'. */
17208 auto cleanup = make_temp_override
17209 (parser->auto_is_implicit_function_template_parm_p, false);
17211 /* Do not actually evaluate the expression. */
17212 ++cp_unevaluated_operand;
17214 /* Do not warn about problems with the expression. */
17215 ++c_inhibit_evaluation_warnings;
17217 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
17218 STRIP_ANY_LOCATION_WRAPPER (expr);
17220 /* Go back to evaluating expressions. */
17221 --cp_unevaluated_operand;
17222 --c_inhibit_evaluation_warnings;
17224 /* The `>' token might be the end of a template-id or
17225 template-parameter-list now. */
17226 parser->greater_than_is_operator_p
17227 = saved_greater_than_is_operator_p;
17229 /* Restore the old message and the integral constant expression
17230 flags. */
17231 parser->type_definition_forbidden_message = saved_message;
17232 parser->integral_constant_expression_p
17233 = saved_integral_constant_expression_p;
17234 parser->non_integral_constant_expression_p
17235 = saved_non_integral_constant_expression_p;
17238 /* Parse to the closing `)'. */
17239 if (expr == error_mark_node || !parens.require_close (parser))
17241 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17242 /*consume_paren=*/true);
17243 expr = error_mark_node;
17246 /* If we got a parse error while tentative, bail out now. */
17247 if (cp_parser_error_occurred (parser))
17249 pop_deferring_access_checks ();
17250 return error_mark_node;
17253 if (!expr)
17254 /* Build auto. */
17255 expr = make_decltype_auto ();
17256 else
17257 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
17258 tf_warning_or_error);
17260 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
17261 it again. */
17262 start_token->type = CPP_DECLTYPE;
17263 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17264 start_token->tree_check_p = true;
17265 start_token->u.tree_check_value->value = expr;
17266 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
17267 start_token->keyword = RID_MAX;
17269 location_t loc = start_token->location;
17270 loc = make_location (loc, loc, parser->lexer);
17271 start_token->location = loc;
17273 cp_lexer_purge_tokens_after (parser->lexer, start_token);
17275 pop_to_parent_deferring_access_checks ();
17277 return expr;
17280 /* Special member functions [gram.special] */
17282 /* Parse a conversion-function-id.
17284 conversion-function-id:
17285 operator conversion-type-id
17287 Returns an IDENTIFIER_NODE representing the operator. */
17289 static tree
17290 cp_parser_conversion_function_id (cp_parser* parser)
17292 tree type;
17293 tree saved_scope;
17294 tree saved_qualifying_scope;
17295 tree saved_object_scope;
17296 tree pushed_scope = NULL_TREE;
17298 /* Look for the `operator' token. */
17299 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17300 return error_mark_node;
17301 /* When we parse the conversion-type-id, the current scope will be
17302 reset. However, we need that information in able to look up the
17303 conversion function later, so we save it here. */
17304 saved_scope = parser->scope;
17305 saved_qualifying_scope = parser->qualifying_scope;
17306 saved_object_scope = parser->object_scope;
17307 /* We must enter the scope of the class so that the names of
17308 entities declared within the class are available in the
17309 conversion-type-id. For example, consider:
17311 struct S {
17312 typedef int I;
17313 operator I();
17316 S::operator I() { ... }
17318 In order to see that `I' is a type-name in the definition, we
17319 must be in the scope of `S'. */
17320 if (saved_scope)
17321 pushed_scope = push_scope (saved_scope);
17322 /* Parse the conversion-type-id. */
17323 type = cp_parser_conversion_type_id (parser);
17324 /* Leave the scope of the class, if any. */
17325 if (pushed_scope)
17326 pop_scope (pushed_scope);
17327 /* Restore the saved scope. */
17328 parser->scope = saved_scope;
17329 parser->qualifying_scope = saved_qualifying_scope;
17330 parser->object_scope = saved_object_scope;
17331 /* If the TYPE is invalid, indicate failure. */
17332 if (type == error_mark_node)
17333 return error_mark_node;
17334 return make_conv_op_name (type);
17337 /* Parse a conversion-type-id:
17339 conversion-type-id:
17340 type-specifier-seq conversion-declarator [opt]
17342 Returns the TYPE specified. */
17344 static tree
17345 cp_parser_conversion_type_id (cp_parser* parser)
17347 tree attributes;
17348 cp_decl_specifier_seq type_specifiers;
17349 cp_declarator *declarator;
17350 tree type_specified;
17351 const char *saved_message;
17353 /* Parse the attributes. */
17354 attributes = cp_parser_attributes_opt (parser);
17356 saved_message = parser->type_definition_forbidden_message;
17357 parser->type_definition_forbidden_message
17358 = G_("types may not be defined in a conversion-type-id");
17360 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17361 optional in conversion-type-id. */
17362 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17363 /*is_declaration=*/false,
17364 /*is_trailing_return=*/false,
17365 &type_specifiers);
17367 parser->type_definition_forbidden_message = saved_message;
17369 /* If that didn't work, stop. */
17370 if (type_specifiers.type == error_mark_node)
17371 return error_mark_node;
17372 /* Parse the conversion-declarator. */
17373 declarator = cp_parser_conversion_declarator_opt (parser);
17375 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17376 /*initialized=*/0, &attributes);
17377 if (attributes)
17378 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17380 /* Don't give this error when parsing tentatively. This happens to
17381 work because we always parse this definitively once. */
17382 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17383 && type_uses_auto (type_specified))
17385 if (cxx_dialect < cxx14)
17387 error ("invalid use of %<auto%> in conversion operator");
17388 return error_mark_node;
17390 else if (template_parm_scope_p ())
17391 warning (0, "use of %<auto%> in member template "
17392 "conversion operator can never be deduced");
17395 return type_specified;
17398 /* Parse an (optional) conversion-declarator.
17400 conversion-declarator:
17401 ptr-operator conversion-declarator [opt]
17405 static cp_declarator *
17406 cp_parser_conversion_declarator_opt (cp_parser* parser)
17408 enum tree_code code;
17409 tree class_type, std_attributes = NULL_TREE;
17410 cp_cv_quals cv_quals;
17412 /* We don't know if there's a ptr-operator next, or not. */
17413 cp_parser_parse_tentatively (parser);
17414 /* Try the ptr-operator. */
17415 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17416 &std_attributes);
17417 /* If it worked, look for more conversion-declarators. */
17418 if (cp_parser_parse_definitely (parser))
17420 cp_declarator *declarator;
17422 /* Parse another optional declarator. */
17423 declarator = cp_parser_conversion_declarator_opt (parser);
17425 declarator = cp_parser_make_indirect_declarator
17426 (code, class_type, cv_quals, declarator, std_attributes);
17428 return declarator;
17431 return NULL;
17434 /* Parse an (optional) ctor-initializer.
17436 ctor-initializer:
17437 : mem-initializer-list */
17439 static void
17440 cp_parser_ctor_initializer_opt (cp_parser* parser)
17442 /* If the next token is not a `:', then there is no
17443 ctor-initializer. */
17444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17446 /* Do default initialization of any bases and members. */
17447 if (DECL_CONSTRUCTOR_P (current_function_decl))
17448 finish_mem_initializers (NULL_TREE);
17449 return;
17452 /* Consume the `:' token. */
17453 cp_lexer_consume_token (parser->lexer);
17454 /* And the mem-initializer-list. */
17455 cp_parser_mem_initializer_list (parser);
17458 /* Parse a mem-initializer-list.
17460 mem-initializer-list:
17461 mem-initializer ... [opt]
17462 mem-initializer ... [opt] , mem-initializer-list */
17464 static void
17465 cp_parser_mem_initializer_list (cp_parser* parser)
17467 tree mem_initializer_list = NULL_TREE;
17468 tree target_ctor = error_mark_node;
17469 cp_token *token = cp_lexer_peek_token (parser->lexer);
17471 /* Let the semantic analysis code know that we are starting the
17472 mem-initializer-list. */
17473 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17474 error_at (token->location,
17475 "only constructors take member initializers");
17477 /* Loop through the list. */
17478 while (true)
17480 tree mem_initializer;
17482 token = cp_lexer_peek_token (parser->lexer);
17483 /* Parse the mem-initializer. */
17484 mem_initializer = cp_parser_mem_initializer (parser);
17485 /* If the next token is a `...', we're expanding member initializers. */
17486 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17487 if (ellipsis
17488 || (mem_initializer != error_mark_node
17489 && check_for_bare_parameter_packs (TREE_PURPOSE
17490 (mem_initializer))))
17492 /* Consume the `...'. */
17493 if (ellipsis)
17494 cp_lexer_consume_token (parser->lexer);
17496 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17497 can be expanded but members cannot. */
17498 if (mem_initializer != error_mark_node
17499 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17501 error_at (token->location,
17502 "cannot expand initializer for member %qD",
17503 TREE_PURPOSE (mem_initializer));
17504 mem_initializer = error_mark_node;
17507 /* Construct the pack expansion type. */
17508 if (mem_initializer != error_mark_node)
17509 mem_initializer = make_pack_expansion (mem_initializer);
17511 if (target_ctor != error_mark_node
17512 && mem_initializer != error_mark_node)
17514 error ("mem-initializer for %qD follows constructor delegation",
17515 TREE_PURPOSE (mem_initializer));
17516 mem_initializer = error_mark_node;
17518 /* Look for a target constructor. */
17519 if (mem_initializer != error_mark_node
17520 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17521 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17523 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17524 if (mem_initializer_list)
17526 error ("constructor delegation follows mem-initializer for %qD",
17527 TREE_PURPOSE (mem_initializer_list));
17528 mem_initializer = error_mark_node;
17530 target_ctor = mem_initializer;
17532 /* Add it to the list, unless it was erroneous. */
17533 if (mem_initializer != error_mark_node)
17535 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17536 mem_initializer_list = mem_initializer;
17538 /* If the next token is not a `,', we're done. */
17539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17540 break;
17541 /* Consume the `,' token. */
17542 cp_lexer_consume_token (parser->lexer);
17545 /* Perform semantic analysis. */
17546 if (DECL_CONSTRUCTOR_P (current_function_decl))
17547 finish_mem_initializers (mem_initializer_list);
17550 /* Parse a mem-initializer.
17552 mem-initializer:
17553 mem-initializer-id ( expression-list [opt] )
17554 mem-initializer-id braced-init-list
17556 GNU extension:
17558 mem-initializer:
17559 ( expression-list [opt] )
17561 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17562 class) or FIELD_DECL (for a non-static data member) to initialize;
17563 the TREE_VALUE is the expression-list. An empty initialization
17564 list is represented by void_list_node. */
17566 static tree
17567 cp_parser_mem_initializer (cp_parser* parser)
17569 tree mem_initializer_id;
17570 tree expression_list;
17571 tree member;
17572 cp_token *token = cp_lexer_peek_token (parser->lexer);
17574 /* Find out what is being initialized. */
17575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17577 permerror (token->location,
17578 "anachronistic old-style base class initializer");
17579 mem_initializer_id = NULL_TREE;
17581 else
17583 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17584 if (mem_initializer_id == error_mark_node)
17585 return mem_initializer_id;
17587 member = expand_member_init (mem_initializer_id);
17588 if (member && !DECL_P (member))
17589 in_base_initializer = 1;
17591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17593 cp_lexer_set_source_position (parser->lexer);
17594 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17595 expression_list = cp_parser_braced_list (parser);
17596 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17597 expression_list = build_tree_list (NULL_TREE, expression_list);
17599 else
17601 vec<tree, va_gc> *vec;
17602 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17603 /*cast_p=*/false,
17604 /*allow_expansion_p=*/true,
17605 /*non_constant_p=*/NULL,
17606 /*close_paren_loc=*/NULL,
17607 /*wrap_locations_p=*/true);
17608 if (vec == NULL)
17609 return error_mark_node;
17610 expression_list = build_tree_list_vec (vec);
17611 release_tree_vector (vec);
17614 if (expression_list == error_mark_node)
17615 return error_mark_node;
17616 if (!expression_list)
17617 expression_list = void_type_node;
17619 in_base_initializer = 0;
17621 if (!member)
17622 return error_mark_node;
17623 tree node = build_tree_list (member, expression_list);
17625 /* We can't attach the source location of this initializer directly to
17626 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17627 within the TREE_TYPE of the list node. */
17628 location_t loc
17629 = make_location (token->location, token->location, parser->lexer);
17630 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17631 SET_EXPR_LOCATION (dummy, loc);
17632 TREE_TYPE (node) = dummy;
17634 return node;
17637 /* Parse a mem-initializer-id.
17639 mem-initializer-id:
17640 :: [opt] nested-name-specifier [opt] class-name
17641 decltype-specifier (C++11)
17642 identifier
17644 Returns a TYPE indicating the class to be initialized for the first
17645 production (and the second in C++11). Returns an IDENTIFIER_NODE
17646 indicating the data member to be initialized for the last production. */
17648 static tree
17649 cp_parser_mem_initializer_id (cp_parser* parser)
17651 bool global_scope_p;
17652 bool nested_name_specifier_p;
17653 bool template_p = false;
17654 tree id;
17656 cp_token *token = cp_lexer_peek_token (parser->lexer);
17658 /* `typename' is not allowed in this context ([temp.res]). */
17659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17661 error_at (token->location,
17662 "keyword %<typename%> not allowed in this context (a qualified "
17663 "member initializer is implicitly a type)");
17664 cp_lexer_consume_token (parser->lexer);
17666 /* Look for the optional `::' operator. */
17667 global_scope_p
17668 = (cp_parser_global_scope_opt (parser,
17669 /*current_scope_valid_p=*/false)
17670 != NULL_TREE);
17671 /* Look for the optional nested-name-specifier. The simplest way to
17672 implement:
17674 [temp.res]
17676 The keyword `typename' is not permitted in a base-specifier or
17677 mem-initializer; in these contexts a qualified name that
17678 depends on a template-parameter is implicitly assumed to be a
17679 type name.
17681 is to assume that we have seen the `typename' keyword at this
17682 point. */
17683 nested_name_specifier_p
17684 = (cp_parser_nested_name_specifier_opt (parser,
17685 /*typename_keyword_p=*/true,
17686 /*check_dependency_p=*/true,
17687 /*type_p=*/true,
17688 /*is_declaration=*/true)
17689 != NULL_TREE);
17690 if (nested_name_specifier_p)
17691 template_p = cp_parser_optional_template_keyword (parser);
17692 /* If there is a `::' operator or a nested-name-specifier, then we
17693 are definitely looking for a class-name. */
17694 if (global_scope_p || nested_name_specifier_p)
17695 return cp_parser_class_name (parser,
17696 /*typename_keyword_p=*/true,
17697 /*template_keyword_p=*/template_p,
17698 typename_type,
17699 /*check_dependency_p=*/true,
17700 /*class_head_p=*/false,
17701 /*is_declaration=*/true);
17702 /* Otherwise, we could also be looking for an ordinary identifier. */
17703 cp_parser_parse_tentatively (parser);
17704 if (cp_lexer_next_token_is_decltype (parser->lexer))
17705 /* Try a decltype-specifier. */
17706 id = cp_parser_decltype (parser);
17707 else
17708 /* Otherwise, try a class-name. */
17709 id = cp_parser_class_name (parser,
17710 /*typename_keyword_p=*/true,
17711 /*template_keyword_p=*/false,
17712 none_type,
17713 /*check_dependency_p=*/true,
17714 /*class_head_p=*/false,
17715 /*is_declaration=*/true);
17716 /* If we found one, we're done. */
17717 if (cp_parser_parse_definitely (parser))
17718 return id;
17719 /* Otherwise, look for an ordinary identifier. */
17720 return cp_parser_identifier (parser);
17723 /* Overloading [gram.over] */
17725 /* Parse an operator-function-id.
17727 operator-function-id:
17728 operator operator
17730 Returns an IDENTIFIER_NODE for the operator which is a
17731 human-readable spelling of the identifier, e.g., `operator +'. */
17733 static cp_expr
17734 cp_parser_operator_function_id (cp_parser* parser)
17736 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17737 /* Look for the `operator' keyword. */
17738 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17739 return error_mark_node;
17740 /* And then the name of the operator itself. */
17741 return cp_parser_operator (parser, start_loc);
17744 /* Return an identifier node for a user-defined literal operator.
17745 The suffix identifier is chained to the operator name identifier. */
17747 tree
17748 cp_literal_operator_id (const char* name)
17750 tree identifier;
17751 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17752 + strlen (name) + 10);
17753 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17754 identifier = get_identifier (buffer);
17755 XDELETEVEC (buffer);
17757 return identifier;
17760 /* Parse an operator.
17762 operator:
17763 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17764 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17765 || ++ -- , ->* -> () []
17767 GNU Extensions:
17769 operator:
17770 <? >? <?= >?=
17772 Returns an IDENTIFIER_NODE for the operator which is a
17773 human-readable spelling of the identifier, e.g., `operator +'. */
17775 static cp_expr
17776 cp_parser_operator (cp_parser* parser, location_t start_loc)
17778 tree id = NULL_TREE;
17779 cp_token *token;
17780 bool utf8 = false;
17782 /* Peek at the next token. */
17783 token = cp_lexer_peek_token (parser->lexer);
17785 location_t end_loc = token->location;
17787 /* Figure out which operator we have. */
17788 enum tree_code op = ERROR_MARK;
17789 bool assop = false;
17790 bool consumed = false;
17791 switch (token->type)
17793 case CPP_KEYWORD:
17795 /* The keyword should be either `new', `delete' or `co_await'. */
17796 if (token->keyword == RID_NEW)
17797 op = NEW_EXPR;
17798 else if (token->keyword == RID_DELETE)
17799 op = DELETE_EXPR;
17800 else if (token->keyword == RID_CO_AWAIT)
17801 op = CO_AWAIT_EXPR;
17802 else
17803 break;
17805 /* Consume the `new', `delete' or co_await token. */
17806 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17808 /* Peek at the next token. */
17809 token = cp_lexer_peek_token (parser->lexer);
17810 /* If it's a `[' token then this is the array variant of the
17811 operator. */
17812 if (token->type == CPP_OPEN_SQUARE
17813 && op != CO_AWAIT_EXPR)
17815 /* Consume the `[' token. */
17816 cp_lexer_consume_token (parser->lexer);
17817 /* Look for the `]' token. */
17818 if (cp_token *close_token
17819 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17820 end_loc = close_token->location;
17821 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17823 consumed = true;
17824 break;
17827 case CPP_PLUS:
17828 op = PLUS_EXPR;
17829 break;
17831 case CPP_MINUS:
17832 op = MINUS_EXPR;
17833 break;
17835 case CPP_MULT:
17836 op = MULT_EXPR;
17837 break;
17839 case CPP_DIV:
17840 op = TRUNC_DIV_EXPR;
17841 break;
17843 case CPP_MOD:
17844 op = TRUNC_MOD_EXPR;
17845 break;
17847 case CPP_XOR:
17848 op = BIT_XOR_EXPR;
17849 break;
17851 case CPP_AND:
17852 op = BIT_AND_EXPR;
17853 break;
17855 case CPP_OR:
17856 op = BIT_IOR_EXPR;
17857 break;
17859 case CPP_COMPL:
17860 op = BIT_NOT_EXPR;
17861 break;
17863 case CPP_NOT:
17864 op = TRUTH_NOT_EXPR;
17865 break;
17867 case CPP_EQ:
17868 assop = true;
17869 op = NOP_EXPR;
17870 break;
17872 case CPP_LESS:
17873 op = LT_EXPR;
17874 break;
17876 case CPP_GREATER:
17877 op = GT_EXPR;
17878 break;
17880 case CPP_PLUS_EQ:
17881 assop = true;
17882 op = PLUS_EXPR;
17883 break;
17885 case CPP_MINUS_EQ:
17886 assop = true;
17887 op = MINUS_EXPR;
17888 break;
17890 case CPP_MULT_EQ:
17891 assop = true;
17892 op = MULT_EXPR;
17893 break;
17895 case CPP_DIV_EQ:
17896 assop = true;
17897 op = TRUNC_DIV_EXPR;
17898 break;
17900 case CPP_MOD_EQ:
17901 assop = true;
17902 op = TRUNC_MOD_EXPR;
17903 break;
17905 case CPP_XOR_EQ:
17906 assop = true;
17907 op = BIT_XOR_EXPR;
17908 break;
17910 case CPP_AND_EQ:
17911 assop = true;
17912 op = BIT_AND_EXPR;
17913 break;
17915 case CPP_OR_EQ:
17916 assop = true;
17917 op = BIT_IOR_EXPR;
17918 break;
17920 case CPP_LSHIFT:
17921 op = LSHIFT_EXPR;
17922 break;
17924 case CPP_RSHIFT:
17925 op = RSHIFT_EXPR;
17926 break;
17928 case CPP_LSHIFT_EQ:
17929 assop = true;
17930 op = LSHIFT_EXPR;
17931 break;
17933 case CPP_RSHIFT_EQ:
17934 assop = true;
17935 op = RSHIFT_EXPR;
17936 break;
17938 case CPP_EQ_EQ:
17939 op = EQ_EXPR;
17940 break;
17942 case CPP_NOT_EQ:
17943 op = NE_EXPR;
17944 break;
17946 case CPP_LESS_EQ:
17947 op = LE_EXPR;
17948 break;
17950 case CPP_GREATER_EQ:
17951 op = GE_EXPR;
17952 break;
17954 case CPP_SPACESHIP:
17955 op = SPACESHIP_EXPR;
17956 break;
17958 case CPP_AND_AND:
17959 op = TRUTH_ANDIF_EXPR;
17960 break;
17962 case CPP_OR_OR:
17963 op = TRUTH_ORIF_EXPR;
17964 break;
17966 case CPP_PLUS_PLUS:
17967 op = POSTINCREMENT_EXPR;
17968 break;
17970 case CPP_MINUS_MINUS:
17971 op = PREDECREMENT_EXPR;
17972 break;
17974 case CPP_COMMA:
17975 op = COMPOUND_EXPR;
17976 break;
17978 case CPP_DEREF_STAR:
17979 op = MEMBER_REF;
17980 break;
17982 case CPP_DEREF:
17983 op = COMPONENT_REF;
17984 break;
17986 case CPP_QUERY:
17987 op = COND_EXPR;
17988 /* Consume the `?'. */
17989 cp_lexer_consume_token (parser->lexer);
17990 /* Look for the matching `:'. */
17991 cp_parser_require (parser, CPP_COLON, RT_COLON);
17992 consumed = true;
17993 break;
17995 case CPP_OPEN_PAREN:
17997 /* Consume the `('. */
17998 matching_parens parens;
17999 parens.consume_open (parser);
18000 /* Look for the matching `)'. */
18001 token = parens.require_close (parser);
18002 if (token)
18003 end_loc = token->location;
18004 op = CALL_EXPR;
18005 consumed = true;
18006 break;
18009 case CPP_OPEN_SQUARE:
18010 /* Consume the `['. */
18011 cp_lexer_consume_token (parser->lexer);
18012 /* Look for the matching `]'. */
18013 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18014 if (token)
18015 end_loc = token->location;
18016 op = ARRAY_REF;
18017 consumed = true;
18018 break;
18020 case CPP_UTF8STRING:
18021 case CPP_UTF8STRING_USERDEF:
18022 utf8 = true;
18023 /* FALLTHRU */
18024 case CPP_STRING:
18025 case CPP_WSTRING:
18026 case CPP_STRING16:
18027 case CPP_STRING32:
18028 case CPP_STRING_USERDEF:
18029 case CPP_WSTRING_USERDEF:
18030 case CPP_STRING16_USERDEF:
18031 case CPP_STRING32_USERDEF:
18033 tree string_tree;
18034 int sz, len;
18036 if (cxx_dialect == cxx98)
18037 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
18039 /* Consume the string. */
18040 cp_expr str = cp_parser_userdef_string_literal (parser,
18041 /*lookup_udlit=*/false);
18042 if (str == error_mark_node)
18043 return error_mark_node;
18044 else if (TREE_CODE (str) == USERDEF_LITERAL)
18046 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
18047 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
18048 end_loc = str.get_location ();
18050 else
18052 string_tree = str;
18053 /* Look for the suffix identifier. */
18054 token = cp_lexer_peek_token (parser->lexer);
18055 if (token->type == CPP_NAME)
18057 id = cp_parser_identifier (parser);
18058 end_loc = token->location;
18060 else if (token->type == CPP_KEYWORD)
18062 error ("unexpected keyword;"
18063 " remove space between quotes and suffix identifier");
18064 return error_mark_node;
18066 else
18068 error ("expected suffix identifier");
18069 return error_mark_node;
18072 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
18073 (TREE_TYPE (TREE_TYPE (string_tree))));
18074 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
18075 if (len != 0)
18077 error ("expected empty string after %<operator%> keyword");
18078 return error_mark_node;
18080 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
18081 != char_type_node)
18083 error ("invalid encoding prefix in literal operator");
18084 return error_mark_node;
18086 if (id != error_mark_node)
18088 const char *name = IDENTIFIER_POINTER (id);
18089 id = cp_literal_operator_id (name);
18091 /* Generate a location of the form:
18092 "" _suffix_identifier
18093 ^~~~~~~~~~~~~~~~~~~~~
18094 with caret == start at the start token, finish at the end of the
18095 suffix identifier. */
18096 location_t combined_loc
18097 = make_location (start_loc, start_loc, parser->lexer);
18098 return cp_expr (id, combined_loc);
18101 default:
18102 /* Anything else is an error. */
18103 break;
18106 /* If we have selected an identifier, we need to consume the
18107 operator token. */
18108 if (op != ERROR_MARK)
18110 id = ovl_op_identifier (assop, op);
18111 if (!consumed)
18112 cp_lexer_consume_token (parser->lexer);
18114 /* Otherwise, no valid operator name was present. */
18115 else
18117 cp_parser_error (parser, "expected operator");
18118 id = error_mark_node;
18121 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
18122 return cp_expr (id, start_loc);
18125 /* Parse a template-declaration.
18127 template-declaration:
18128 export [opt] template < template-parameter-list > declaration
18130 If MEMBER_P is TRUE, this template-declaration occurs within a
18131 class-specifier.
18133 The grammar rule given by the standard isn't correct. What
18134 is really meant is:
18136 template-declaration:
18137 export [opt] template-parameter-list-seq
18138 decl-specifier-seq [opt] init-declarator [opt] ;
18139 export [opt] template-parameter-list-seq
18140 function-definition
18142 template-parameter-list-seq:
18143 template-parameter-list-seq [opt]
18144 template < template-parameter-list >
18146 Concept Extensions:
18148 template-parameter-list-seq:
18149 template < template-parameter-list > requires-clause [opt]
18151 requires-clause:
18152 requires logical-or-expression */
18154 static void
18155 cp_parser_template_declaration (cp_parser* parser, bool member_p)
18157 /* Check for `export'. */
18158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
18160 /* Consume the `export' token. */
18161 cp_lexer_consume_token (parser->lexer);
18162 /* Warn that this use of export is deprecated. */
18163 if (cxx_dialect < cxx11)
18164 warning (0, "keyword %<export%> not implemented, and will be ignored");
18165 else if (cxx_dialect < cxx20)
18166 warning (0, "keyword %<export%> is deprecated, and is ignored");
18167 else
18168 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
18171 cp_parser_template_declaration_after_export (parser, member_p);
18174 /* Parse a template-parameter-list.
18176 template-parameter-list:
18177 template-parameter
18178 template-parameter-list , template-parameter
18180 Returns a TREE_LIST. Each node represents a template parameter.
18181 The nodes are connected via their TREE_CHAINs. */
18183 static tree
18184 cp_parser_template_parameter_list (cp_parser* parser)
18186 tree parameter_list = NULL_TREE;
18188 /* Don't create wrapper nodes within a template-parameter-list,
18189 since we don't want to have different types based on the
18190 spelling location of constants and decls within them. */
18191 auto_suppress_location_wrappers sentinel;
18193 begin_template_parm_list ();
18195 /* The loop below parses the template parms. We first need to know
18196 the total number of template parms to be able to compute proper
18197 canonical types of each dependent type. So after the loop, when
18198 we know the total number of template parms,
18199 end_template_parm_list computes the proper canonical types and
18200 fixes up the dependent types accordingly. */
18201 while (true)
18203 tree parameter;
18204 bool is_non_type;
18205 bool is_parameter_pack;
18206 location_t parm_loc;
18208 /* Parse the template-parameter. */
18209 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
18210 parameter = cp_parser_template_parameter (parser,
18211 &is_non_type,
18212 &is_parameter_pack);
18213 /* Add it to the list. */
18214 if (parameter != error_mark_node)
18215 parameter_list = process_template_parm (parameter_list,
18216 parm_loc,
18217 parameter,
18218 is_non_type,
18219 is_parameter_pack);
18220 else
18222 tree err_parm = build_tree_list (parameter, parameter);
18223 parameter_list = chainon (parameter_list, err_parm);
18226 /* If the next token is not a `,', we're done. */
18227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18228 break;
18229 /* Otherwise, consume the `,' token. */
18230 cp_lexer_consume_token (parser->lexer);
18233 return end_template_parm_list (parameter_list);
18236 /* Parse a introduction-list.
18238 introduction-list:
18239 introduced-parameter
18240 introduction-list , introduced-parameter
18242 introduced-parameter:
18243 ...[opt] identifier
18245 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
18246 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
18247 WILDCARD_DECL will also have DECL_NAME set and token location in
18248 DECL_SOURCE_LOCATION. */
18250 static tree
18251 cp_parser_introduction_list (cp_parser *parser)
18253 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
18255 while (true)
18257 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
18258 if (is_pack)
18259 cp_lexer_consume_token (parser->lexer);
18261 tree identifier = cp_parser_identifier (parser);
18262 if (identifier == error_mark_node)
18263 break;
18265 /* Build placeholder. */
18266 tree parm = build_nt (WILDCARD_DECL);
18267 DECL_SOURCE_LOCATION (parm)
18268 = cp_lexer_peek_token (parser->lexer)->location;
18269 DECL_NAME (parm) = identifier;
18270 WILDCARD_PACK_P (parm) = is_pack;
18271 vec_safe_push (introduction_vec, parm);
18273 /* If the next token is not a `,', we're done. */
18274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18275 break;
18276 /* Otherwise, consume the `,' token. */
18277 cp_lexer_consume_token (parser->lexer);
18280 /* Convert the vec into a TREE_VEC. */
18281 tree introduction_list = make_tree_vec (introduction_vec->length ());
18282 unsigned int n;
18283 tree parm;
18284 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
18285 TREE_VEC_ELT (introduction_list, n) = parm;
18287 release_tree_vector (introduction_vec);
18288 return introduction_list;
18291 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
18292 is an abstract declarator. */
18294 static inline cp_declarator*
18295 get_id_declarator (cp_declarator *declarator)
18297 cp_declarator *d = declarator;
18298 while (d && d->kind != cdk_id)
18299 d = d->declarator;
18300 return d;
18303 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
18304 is an abstract declarator. */
18306 static inline tree
18307 get_unqualified_id (cp_declarator *declarator)
18309 declarator = get_id_declarator (declarator);
18310 if (declarator)
18311 return declarator->u.id.unqualified_name;
18312 else
18313 return NULL_TREE;
18316 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18318 static inline bool
18319 is_constrained_parameter (tree type)
18321 return (type
18322 && TREE_CODE (type) == TYPE_DECL
18323 && CONSTRAINED_PARM_CONCEPT (type)
18324 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18327 /* Returns true if PARM declares a constrained-parameter. */
18329 static inline bool
18330 is_constrained_parameter (cp_parameter_declarator *parm)
18332 return is_constrained_parameter (parm->decl_specifiers.type);
18335 /* Check that the type parameter is only a declarator-id, and that its
18336 type is not cv-qualified. */
18338 bool
18339 cp_parser_check_constrained_type_parm (cp_parser *parser,
18340 cp_parameter_declarator *parm)
18342 if (!parm->declarator)
18343 return true;
18345 if (parm->declarator->kind != cdk_id)
18347 cp_parser_error (parser, "invalid constrained type parameter");
18348 return false;
18351 /* Don't allow cv-qualified type parameters. */
18352 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18353 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18355 cp_parser_error (parser, "cv-qualified type parameter");
18356 return false;
18359 return true;
18362 /* Finish parsing/processing a template type parameter and checking
18363 various restrictions. */
18365 static inline tree
18366 cp_parser_constrained_type_template_parm (cp_parser *parser,
18367 tree id,
18368 cp_parameter_declarator* parmdecl)
18370 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18371 return finish_template_type_parm (class_type_node, id);
18372 else
18373 return error_mark_node;
18376 static tree
18377 finish_constrained_template_template_parm (tree proto, tree id)
18379 /* FIXME: This should probably be copied, and we may need to adjust
18380 the template parameter depths. */
18381 tree saved_parms = current_template_parms;
18382 begin_template_parm_list ();
18383 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18384 end_template_parm_list ();
18386 tree parm = finish_template_template_parm (class_type_node, id);
18387 current_template_parms = saved_parms;
18389 return parm;
18392 /* Finish parsing/processing a template template parameter by borrowing
18393 the template parameter list from the prototype parameter. */
18395 static tree
18396 cp_parser_constrained_template_template_parm (cp_parser *parser,
18397 tree proto,
18398 tree id,
18399 cp_parameter_declarator *parmdecl)
18401 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18402 return error_mark_node;
18403 return finish_constrained_template_template_parm (proto, id);
18406 /* Create a new non-type template parameter from the given PARM
18407 declarator. */
18409 static tree
18410 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18411 cp_parameter_declarator *parm)
18413 *is_non_type = true;
18414 cp_declarator *decl = parm->declarator;
18415 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18416 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18417 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18420 /* Build a constrained template parameter based on the PARMDECL
18421 declarator. The type of PARMDECL is the constrained type, which
18422 refers to the prototype template parameter that ultimately
18423 specifies the type of the declared parameter. */
18425 static tree
18426 finish_constrained_parameter (cp_parser *parser,
18427 cp_parameter_declarator *parmdecl,
18428 bool *is_non_type)
18430 tree decl = parmdecl->decl_specifiers.type;
18431 tree id = get_unqualified_id (parmdecl->declarator);
18432 tree def = parmdecl->default_argument;
18433 tree proto = DECL_INITIAL (decl);
18435 /* Build the parameter. Return an error if the declarator was invalid. */
18436 tree parm;
18437 if (TREE_CODE (proto) == TYPE_DECL)
18438 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18439 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18440 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18441 parmdecl);
18442 else
18443 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18444 if (parm == error_mark_node)
18445 return error_mark_node;
18447 /* Finish the parameter decl and create a node attaching the
18448 default argument and constraint. */
18449 parm = build_tree_list (def, parm);
18450 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18452 return parm;
18455 /* Returns true if the parsed type actually represents the declaration
18456 of a type template-parameter. */
18458 static bool
18459 declares_constrained_type_template_parameter (tree type)
18461 return (is_constrained_parameter (type)
18462 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18465 /* Returns true if the parsed type actually represents the declaration of
18466 a template template-parameter. */
18468 static bool
18469 declares_constrained_template_template_parameter (tree type)
18471 return (is_constrained_parameter (type)
18472 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18475 /* Parse a default argument for a type template-parameter.
18476 Note that diagnostics are handled in cp_parser_template_parameter. */
18478 static tree
18479 cp_parser_default_type_template_argument (cp_parser *parser)
18481 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18483 /* Consume the `=' token. */
18484 cp_lexer_consume_token (parser->lexer);
18486 cp_token *token = cp_lexer_peek_token (parser->lexer);
18488 /* Tell cp_parser_lambda_expression this is a default argument. */
18489 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18490 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18492 /* Parse the default-argument. */
18493 push_deferring_access_checks (dk_no_deferred);
18494 tree default_argument = cp_parser_type_id (parser,
18495 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18496 NULL);
18497 pop_deferring_access_checks ();
18499 if (flag_concepts && type_uses_auto (default_argument))
18501 error_at (token->location,
18502 "invalid use of %<auto%> in default template argument");
18503 return error_mark_node;
18506 return default_argument;
18509 /* Parse a default argument for a template template-parameter. */
18511 static tree
18512 cp_parser_default_template_template_argument (cp_parser *parser)
18514 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18516 bool is_template;
18518 /* Consume the `='. */
18519 cp_lexer_consume_token (parser->lexer);
18520 /* Parse the id-expression. */
18521 push_deferring_access_checks (dk_no_deferred);
18522 /* save token before parsing the id-expression, for error
18523 reporting */
18524 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18525 tree default_argument
18526 = cp_parser_id_expression (parser,
18527 /*template_keyword_p=*/false,
18528 /*check_dependency_p=*/true,
18529 /*template_p=*/&is_template,
18530 /*declarator_p=*/false,
18531 /*optional_p=*/false);
18532 if (TREE_CODE (default_argument) == TYPE_DECL)
18533 /* If the id-expression was a template-id that refers to
18534 a template-class, we already have the declaration here,
18535 so no further lookup is needed. */
18537 else
18538 /* Look up the name. */
18539 default_argument
18540 = cp_parser_lookup_name (parser, default_argument,
18541 none_type,
18542 /*is_template=*/is_template,
18543 /*is_namespace=*/false,
18544 /*check_dependency=*/true,
18545 /*ambiguous_decls=*/NULL,
18546 token->location);
18547 /* See if the default argument is valid. */
18548 default_argument = check_template_template_default_arg (default_argument);
18549 pop_deferring_access_checks ();
18550 return default_argument;
18553 /* Parse a template-parameter.
18555 template-parameter:
18556 type-parameter
18557 parameter-declaration
18559 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18560 the parameter. The TREE_PURPOSE is the default value, if any.
18561 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18562 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18563 set to true iff this parameter is a parameter pack. */
18565 static tree
18566 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18567 bool *is_parameter_pack)
18569 cp_token *token;
18570 cp_parameter_declarator *parameter_declarator;
18571 tree parm;
18573 /* Assume it is a type parameter or a template parameter. */
18574 *is_non_type = false;
18575 /* Assume it not a parameter pack. */
18576 *is_parameter_pack = false;
18577 /* Peek at the next token. */
18578 token = cp_lexer_peek_token (parser->lexer);
18579 /* If it is `template', we have a type-parameter. */
18580 if (token->keyword == RID_TEMPLATE)
18581 return cp_parser_type_parameter (parser, is_parameter_pack);
18582 /* If it is `class' or `typename' we do not know yet whether it is a
18583 type parameter or a non-type parameter. Consider:
18585 template <typename T, typename T::X X> ...
18589 template <class C, class D*> ...
18591 Here, the first parameter is a type parameter, and the second is
18592 a non-type parameter. We can tell by looking at the token after
18593 the identifier -- if it is a `,', `=', or `>' then we have a type
18594 parameter. */
18595 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18597 /* Peek at the token after `class' or `typename'. */
18598 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18599 /* If it's an ellipsis, we have a template type parameter
18600 pack. */
18601 if (token->type == CPP_ELLIPSIS)
18602 return cp_parser_type_parameter (parser, is_parameter_pack);
18603 /* If it's an identifier, skip it. */
18604 if (token->type == CPP_NAME)
18605 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18606 /* Now, see if the token looks like the end of a template
18607 parameter. */
18608 if (token->type == CPP_COMMA
18609 || token->type == CPP_EQ
18610 || token->type == CPP_GREATER)
18611 return cp_parser_type_parameter (parser, is_parameter_pack);
18614 /* Otherwise, it is a non-type parameter or a constrained parameter.
18616 [temp.param]
18618 When parsing a default template-argument for a non-type
18619 template-parameter, the first non-nested `>' is taken as the end
18620 of the template parameter-list rather than a greater-than
18621 operator. */
18622 parameter_declarator
18623 = cp_parser_parameter_declaration (parser,
18624 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18625 /*template_parm_p=*/true,
18626 /*parenthesized_p=*/NULL);
18628 if (!parameter_declarator)
18629 return error_mark_node;
18631 /* If the parameter declaration is marked as a parameter pack, set
18632 *IS_PARAMETER_PACK to notify the caller. */
18633 if (parameter_declarator->template_parameter_pack_p)
18634 *is_parameter_pack = true;
18636 if (parameter_declarator->default_argument)
18638 /* Can happen in some cases of erroneous input (c++/34892). */
18639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18640 /* Consume the `...' for better error recovery. */
18641 cp_lexer_consume_token (parser->lexer);
18644 /* The parameter may have been constrained type parameter. */
18645 if (is_constrained_parameter (parameter_declarator))
18646 return finish_constrained_parameter (parser,
18647 parameter_declarator,
18648 is_non_type);
18650 // Now we're sure that the parameter is a non-type parameter.
18651 *is_non_type = true;
18653 parm = grokdeclarator (parameter_declarator->declarator,
18654 &parameter_declarator->decl_specifiers,
18655 TPARM, /*initialized=*/0,
18656 /*attrlist=*/NULL);
18657 if (parm == error_mark_node)
18658 return error_mark_node;
18660 return build_tree_list (parameter_declarator->default_argument, parm);
18663 /* Parse a type-parameter.
18665 type-parameter:
18666 class identifier [opt]
18667 class identifier [opt] = type-id
18668 typename identifier [opt]
18669 typename identifier [opt] = type-id
18670 template < template-parameter-list > class identifier [opt]
18671 template < template-parameter-list > class identifier [opt]
18672 = id-expression
18674 GNU Extension (variadic templates):
18676 type-parameter:
18677 class ... identifier [opt]
18678 typename ... identifier [opt]
18680 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18681 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18682 the declaration of the parameter.
18684 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18686 static tree
18687 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18689 cp_token *token;
18690 tree parameter;
18692 /* Look for a keyword to tell us what kind of parameter this is. */
18693 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18694 if (!token)
18695 return error_mark_node;
18697 switch (token->keyword)
18699 case RID_CLASS:
18700 case RID_TYPENAME:
18702 tree identifier;
18703 tree default_argument;
18705 /* If the next token is an ellipsis, we have a template
18706 argument pack. */
18707 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18709 /* Consume the `...' token. */
18710 cp_lexer_consume_token (parser->lexer);
18711 maybe_warn_variadic_templates ();
18713 *is_parameter_pack = true;
18716 /* If the next token is an identifier, then it names the
18717 parameter. */
18718 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18719 identifier = cp_parser_identifier (parser);
18720 else
18721 identifier = NULL_TREE;
18723 /* Create the parameter. */
18724 parameter = finish_template_type_parm (class_type_node, identifier);
18726 /* If the next token is an `=', we have a default argument. */
18727 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18729 default_argument
18730 = cp_parser_default_type_template_argument (parser);
18732 /* Template parameter packs cannot have default
18733 arguments. */
18734 if (*is_parameter_pack)
18736 if (identifier)
18737 error_at (token->location,
18738 "template parameter pack %qD cannot have a "
18739 "default argument", identifier);
18740 else
18741 error_at (token->location,
18742 "template parameter packs cannot have "
18743 "default arguments");
18744 default_argument = NULL_TREE;
18746 else if (check_for_bare_parameter_packs (default_argument))
18747 default_argument = error_mark_node;
18749 else
18750 default_argument = NULL_TREE;
18752 /* Create the combined representation of the parameter and the
18753 default argument. */
18754 parameter = build_tree_list (default_argument, parameter);
18756 break;
18758 case RID_TEMPLATE:
18760 tree identifier;
18761 tree default_argument;
18763 /* Look for the `<'. */
18764 cp_parser_require (parser, CPP_LESS, RT_LESS);
18765 /* Parse the template-parameter-list. */
18766 cp_parser_template_parameter_list (parser);
18767 /* Look for the `>'. */
18768 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18770 /* If template requirements are present, parse them. */
18771 if (flag_concepts)
18773 tree reqs = get_shorthand_constraints (current_template_parms);
18774 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18775 reqs = combine_constraint_expressions (reqs, dreqs);
18776 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18779 /* Look for the `class' or 'typename' keywords. */
18780 cp_parser_type_parameter_key (parser);
18781 /* If the next token is an ellipsis, we have a template
18782 argument pack. */
18783 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18785 /* Consume the `...' token. */
18786 cp_lexer_consume_token (parser->lexer);
18787 maybe_warn_variadic_templates ();
18789 *is_parameter_pack = true;
18791 /* If the next token is an `=', then there is a
18792 default-argument. If the next token is a `>', we are at
18793 the end of the parameter-list. If the next token is a `,',
18794 then we are at the end of this parameter. */
18795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18796 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18797 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18799 identifier = cp_parser_identifier (parser);
18800 /* Treat invalid names as if the parameter were nameless. */
18801 if (identifier == error_mark_node)
18802 identifier = NULL_TREE;
18804 else
18805 identifier = NULL_TREE;
18807 /* Create the template parameter. */
18808 parameter = finish_template_template_parm (class_type_node,
18809 identifier);
18811 /* If the next token is an `=', then there is a
18812 default-argument. */
18813 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18815 default_argument
18816 = cp_parser_default_template_template_argument (parser);
18818 /* Template parameter packs cannot have default
18819 arguments. */
18820 if (*is_parameter_pack)
18822 if (identifier)
18823 error_at (token->location,
18824 "template parameter pack %qD cannot "
18825 "have a default argument",
18826 identifier);
18827 else
18828 error_at (token->location, "template parameter packs cannot "
18829 "have default arguments");
18830 default_argument = NULL_TREE;
18833 else
18834 default_argument = NULL_TREE;
18836 /* Create the combined representation of the parameter and the
18837 default argument. */
18838 parameter = build_tree_list (default_argument, parameter);
18840 break;
18842 default:
18843 gcc_unreachable ();
18844 break;
18847 return parameter;
18850 /* Parse a template-id.
18852 template-id:
18853 template-name < template-argument-list [opt] >
18855 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18856 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18857 returned. Otherwise, if the template-name names a function, or set
18858 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18859 names a class, returns a TYPE_DECL for the specialization.
18861 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18862 uninstantiated templates. */
18864 static tree
18865 cp_parser_template_id (cp_parser *parser,
18866 bool template_keyword_p,
18867 bool check_dependency_p,
18868 enum tag_types tag_type,
18869 bool is_declaration)
18871 tree templ;
18872 tree arguments;
18873 tree template_id;
18874 cp_token_position start_of_id = 0;
18875 cp_token *next_token = NULL, *next_token_2 = NULL;
18876 bool is_identifier;
18878 /* If the next token corresponds to a template-id, there is no need
18879 to reparse it. */
18880 cp_token *token = cp_lexer_peek_token (parser->lexer);
18882 if (token->type == CPP_TEMPLATE_ID)
18884 cp_lexer_consume_token (parser->lexer);
18885 return saved_checks_value (token->u.tree_check_value);
18888 /* Avoid performing name lookup if there is no possibility of
18889 finding a template-id. */
18890 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18891 || (token->type == CPP_NAME
18892 && !cp_parser_nth_token_starts_template_argument_list_p
18893 (parser, 2)))
18895 cp_parser_error (parser, "expected template-id");
18896 return error_mark_node;
18899 /* Remember where the template-id starts. */
18900 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18901 start_of_id = cp_lexer_token_position (parser->lexer, false);
18903 push_deferring_access_checks (dk_deferred);
18905 /* Parse the template-name. */
18906 is_identifier = false;
18907 templ = cp_parser_template_name (parser, template_keyword_p,
18908 check_dependency_p,
18909 is_declaration,
18910 tag_type,
18911 &is_identifier);
18913 /* Push any access checks inside the firewall we're about to create. */
18914 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18915 pop_deferring_access_checks ();
18916 if (templ == error_mark_node || is_identifier)
18917 return templ;
18919 /* Since we're going to preserve any side-effects from this parse, set up a
18920 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18921 in the template arguments. */
18922 tentative_firewall firewall (parser);
18923 reopen_deferring_access_checks (checks);
18925 /* If we find the sequence `[:' after a template-name, it's probably
18926 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18927 parse correctly the argument list. */
18928 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18929 == CPP_OPEN_SQUARE)
18930 && next_token->flags & DIGRAPH
18931 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18932 == CPP_COLON)
18933 && !(next_token_2->flags & PREV_WHITE))
18935 cp_parser_parse_tentatively (parser);
18936 /* Change `:' into `::'. */
18937 next_token_2->type = CPP_SCOPE;
18938 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18939 CPP_LESS. */
18940 cp_lexer_consume_token (parser->lexer);
18942 /* Parse the arguments. */
18943 arguments = cp_parser_enclosed_template_argument_list (parser);
18944 if (!cp_parser_parse_definitely (parser))
18946 /* If we couldn't parse an argument list, then we revert our changes
18947 and return simply an error. Maybe this is not a template-id
18948 after all. */
18949 next_token_2->type = CPP_COLON;
18950 cp_parser_error (parser, "expected %<<%>");
18951 pop_deferring_access_checks ();
18952 return error_mark_node;
18954 /* Otherwise, emit an error about the invalid digraph, but continue
18955 parsing because we got our argument list. */
18956 if (permerror (next_token->location,
18957 "%<<::%> cannot begin a template-argument list"))
18959 static bool hint = false;
18960 inform (next_token->location,
18961 "%<<:%> is an alternate spelling for %<[%>."
18962 " Insert whitespace between %<<%> and %<::%>");
18963 if (!hint && !flag_permissive)
18965 inform (next_token->location, "(if you use %<-fpermissive%> "
18966 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18967 "accept your code)");
18968 hint = true;
18972 else
18974 /* Look for the `<' that starts the template-argument-list. */
18975 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18977 pop_deferring_access_checks ();
18978 return error_mark_node;
18980 /* Parse the arguments. */
18981 arguments = cp_parser_enclosed_template_argument_list (parser);
18983 if ((cxx_dialect > cxx17)
18984 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18985 && !template_keyword_p
18986 && (cp_parser_error_occurred (parser)
18987 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18989 /* This didn't go well. */
18990 if (TREE_CODE (templ) == FUNCTION_DECL)
18992 /* C++20 says that "function-name < a;" is now ill-formed. */
18993 if (cp_parser_error_occurred (parser))
18995 error_at (token->location, "invalid template-argument-list");
18996 inform (token->location, "function name as the left hand "
18997 "operand of %<<%> is ill-formed in C++20; wrap the "
18998 "function name in %<()%>");
19000 else
19001 /* We expect "f<targs>" to be followed by "(args)". */
19002 error_at (cp_lexer_peek_token (parser->lexer)->location,
19003 "expected %<(%> after template-argument-list");
19004 if (start_of_id)
19005 /* Purge all subsequent tokens. */
19006 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19008 else
19009 cp_parser_simulate_error (parser);
19010 pop_deferring_access_checks ();
19011 return error_mark_node;
19015 /* Set the location to be of the form:
19016 template-name < template-argument-list [opt] >
19017 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19018 with caret == start at the start of the template-name,
19019 ranging until the closing '>'. */
19020 location_t combined_loc
19021 = make_location (token->location, token->location, parser->lexer);
19023 /* Check for concepts autos where they don't belong. We could
19024 identify types in some cases of identifier TEMPL, looking ahead
19025 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
19026 types. We reject them in functions, but if what we have is an
19027 identifier, even with none_type we can't conclude it's NOT a
19028 type, we have to wait for template substitution. */
19029 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
19030 template_id = error_mark_node;
19031 /* Build a representation of the specialization. */
19032 else if (identifier_p (templ))
19033 template_id = build_min_nt_loc (combined_loc,
19034 TEMPLATE_ID_EXPR,
19035 templ, arguments);
19036 else if (DECL_TYPE_TEMPLATE_P (templ)
19037 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
19039 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
19040 template (rather than some instantiation thereof) only if
19041 is not nested within some other construct. For example, in
19042 "template <typename T> void f(T) { A<T>::", A<T> is just an
19043 instantiation of A. */
19044 bool entering_scope
19045 = (template_parm_scope_p ()
19046 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
19047 template_id
19048 = finish_template_type (templ, arguments, entering_scope);
19050 else if (concept_definition_p (templ))
19052 /* The caller will decide whether this is a concept check or type
19053 constraint. */
19054 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
19055 boolean_type_node, templ, arguments);
19057 else if (variable_template_p (templ))
19059 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
19060 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19061 SET_EXPR_LOCATION (template_id, combined_loc);
19063 else if (TREE_CODE (templ) == TYPE_DECL
19064 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
19066 /* Some type template in dependent scope. */
19067 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
19068 name = build_min_nt_loc (combined_loc,
19069 TEMPLATE_ID_EXPR,
19070 name, arguments);
19071 template_id = templ;
19073 else
19075 /* If it's not a class-template or a template-template, it should be
19076 a function-template. */
19077 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
19079 template_id = lookup_template_function (templ, arguments);
19080 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19081 SET_EXPR_LOCATION (template_id, combined_loc);
19084 /* If parsing tentatively, replace the sequence of tokens that makes
19085 up the template-id with a CPP_TEMPLATE_ID token. That way,
19086 should we re-parse the token stream, we will not have to repeat
19087 the effort required to do the parse, nor will we issue duplicate
19088 error messages about problems during instantiation of the
19089 template. */
19090 if (start_of_id
19091 /* Don't do this if we had a parse error in a declarator; re-parsing
19092 might succeed if a name changes meaning (60361). */
19093 && !(cp_parser_error_occurred (parser)
19094 && cp_parser_parsing_tentatively (parser)
19095 && parser->in_declarator_p))
19097 /* Reset the contents of the START_OF_ID token. */
19098 token->type = CPP_TEMPLATE_ID;
19099 token->location = combined_loc;
19101 /* Retrieve any deferred checks. Do not pop this access checks yet
19102 so the memory will not be reclaimed during token replacing below. */
19103 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
19104 token->tree_check_p = true;
19105 token->u.tree_check_value->value = template_id;
19106 token->u.tree_check_value->checks = get_deferred_access_checks ();
19107 token->keyword = RID_MAX;
19109 /* Purge all subsequent tokens. */
19110 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19112 /* ??? Can we actually assume that, if template_id ==
19113 error_mark_node, we will have issued a diagnostic to the
19114 user, as opposed to simply marking the tentative parse as
19115 failed? */
19116 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
19117 error_at (token->location, "parse error in template argument list");
19120 pop_to_parent_deferring_access_checks ();
19121 return template_id;
19124 /* Like cp_parser_template_id, called in non-type context. */
19126 static tree
19127 cp_parser_template_id_expr (cp_parser *parser,
19128 bool template_keyword_p,
19129 bool check_dependency_p,
19130 bool is_declaration)
19132 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
19133 none_type, is_declaration);
19134 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
19135 && concept_check_p (x))
19136 /* We didn't check the arguments in cp_parser_template_id; do that now. */
19137 return build_concept_id (x);
19138 return x;
19141 /* Parse a template-name.
19143 template-name:
19144 identifier
19146 The standard should actually say:
19148 template-name:
19149 identifier
19150 operator-function-id
19152 A defect report has been filed about this issue.
19154 A conversion-function-id cannot be a template name because they cannot
19155 be part of a template-id. In fact, looking at this code:
19157 a.operator K<int>()
19159 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
19160 It is impossible to call a templated conversion-function-id with an
19161 explicit argument list, since the only allowed template parameter is
19162 the type to which it is converting.
19164 If TEMPLATE_KEYWORD_P is true, then we have just seen the
19165 `template' keyword, in a construction like:
19167 T::template f<3>()
19169 In that case `f' is taken to be a template-name, even though there
19170 is no way of knowing for sure.
19172 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
19173 name refers to a set of overloaded functions, at least one of which
19174 is a template, or an IDENTIFIER_NODE with the name of the template,
19175 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
19176 names are looked up inside uninstantiated templates. */
19178 static tree
19179 cp_parser_template_name (cp_parser* parser,
19180 bool template_keyword_p,
19181 bool check_dependency_p,
19182 bool is_declaration,
19183 enum tag_types tag_type,
19184 bool *is_identifier)
19186 tree identifier;
19187 tree decl;
19188 cp_token *token = cp_lexer_peek_token (parser->lexer);
19190 /* If the next token is `operator', then we have either an
19191 operator-function-id or a conversion-function-id. */
19192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
19194 /* We don't know whether we're looking at an
19195 operator-function-id or a conversion-function-id. */
19196 cp_parser_parse_tentatively (parser);
19197 /* Try an operator-function-id. */
19198 identifier = cp_parser_operator_function_id (parser);
19199 /* If that didn't work, try a conversion-function-id. */
19200 if (!cp_parser_parse_definitely (parser))
19202 cp_parser_error (parser, "expected template-name");
19203 return error_mark_node;
19206 /* Look for the identifier. */
19207 else
19208 identifier = cp_parser_identifier (parser);
19210 /* If we didn't find an identifier, we don't have a template-id. */
19211 if (identifier == error_mark_node)
19212 return error_mark_node;
19214 /* If the name immediately followed the `template' keyword, then it
19215 is a template-name. However, if the next token is not `<', then
19216 we do not treat it as a template-name, since it is not being used
19217 as part of a template-id. This enables us to handle constructs
19218 like:
19220 template <typename T> struct S { S(); };
19221 template <typename T> S<T>::S();
19223 correctly. We would treat `S' as a template -- if it were `S<T>'
19224 -- but we do not if there is no `<'. */
19226 if (processing_template_decl
19227 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19229 /* In a declaration, in a dependent context, we pretend that the
19230 "template" keyword was present in order to improve error
19231 recovery. For example, given:
19233 template <typename T> void f(T::X<int>);
19235 we want to treat "X<int>" as a template-id. */
19236 if (is_declaration
19237 && !template_keyword_p
19238 && parser->scope && TYPE_P (parser->scope)
19239 && check_dependency_p
19240 && dependent_scope_p (parser->scope)
19241 /* Do not do this for dtors (or ctors), since they never
19242 need the template keyword before their name. */
19243 && !constructor_name_p (identifier, parser->scope))
19245 cp_token_position start = 0;
19247 /* Explain what went wrong. */
19248 error_at (token->location, "non-template %qD used as template",
19249 identifier);
19250 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
19251 parser->scope, identifier);
19252 /* If parsing tentatively, find the location of the "<" token. */
19253 if (cp_parser_simulate_error (parser))
19254 start = cp_lexer_token_position (parser->lexer, true);
19255 /* Parse the template arguments so that we can issue error
19256 messages about them. */
19257 cp_lexer_consume_token (parser->lexer);
19258 cp_parser_enclosed_template_argument_list (parser);
19259 /* Skip tokens until we find a good place from which to
19260 continue parsing. */
19261 cp_parser_skip_to_closing_parenthesis (parser,
19262 /*recovering=*/true,
19263 /*or_comma=*/true,
19264 /*consume_paren=*/false);
19265 /* If parsing tentatively, permanently remove the
19266 template argument list. That will prevent duplicate
19267 error messages from being issued about the missing
19268 "template" keyword. */
19269 if (start)
19270 cp_lexer_purge_tokens_after (parser->lexer, start);
19271 if (is_identifier)
19272 *is_identifier = true;
19273 parser->context->object_type = NULL_TREE;
19274 return identifier;
19277 /* If the "template" keyword is present, then there is generally
19278 no point in doing name-lookup, so we just return IDENTIFIER.
19279 But, if the qualifying scope is non-dependent then we can
19280 (and must) do name-lookup normally. */
19281 if (template_keyword_p)
19283 tree scope = (parser->scope ? parser->scope
19284 : parser->context->object_type);
19285 if (scope && TYPE_P (scope)
19286 && (!CLASS_TYPE_P (scope)
19287 || (check_dependency_p && dependent_scope_p (scope))))
19289 /* We're optimizing away the call to cp_parser_lookup_name, but
19290 we still need to do this. */
19291 parser->object_scope = parser->context->object_type;
19292 parser->context->object_type = NULL_TREE;
19293 return identifier;
19298 /* cp_parser_lookup_name clears OBJECT_TYPE. */
19299 tree scope = (parser->scope ? parser->scope
19300 : parser->context->object_type);
19302 /* Look up the name. */
19303 decl = cp_parser_lookup_name (parser, identifier,
19304 tag_type,
19305 /*is_template=*/1 + template_keyword_p,
19306 /*is_namespace=*/false,
19307 check_dependency_p,
19308 /*ambiguous_decls=*/NULL,
19309 token->location);
19311 decl = strip_using_decl (decl);
19313 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19314 template-argument-list if it follows a name that is not a
19315 conversion-function-id and
19316 - that follows the keyword template or a ~ after a nested-name-specifier or
19317 in a class member access expression, or
19318 - for which name lookup finds the injected-class-name of a class template
19319 or finds any declaration of a template, or
19320 - that is an unqualified name for which name lookup either finds one or
19321 more functions or finds nothing, or
19322 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19323 (9.3.4), or in a type-only context other than a nested-name-specifier
19324 (13.8). */
19326 /* Handle injected-class-name. */
19327 decl = maybe_get_template_decl_from_type_decl (decl);
19329 /* If DECL is a template, then the name was a template-name. */
19330 if (TREE_CODE (decl) == TEMPLATE_DECL)
19332 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19333 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19335 tree d = DECL_TEMPLATE_RESULT (decl);
19336 tree attr;
19337 if (TREE_CODE (d) == TYPE_DECL)
19338 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19339 else
19340 attr = DECL_ATTRIBUTES (d);
19341 if (TREE_UNAVAILABLE (decl))
19343 attr = lookup_attribute ("unavailable", attr);
19344 error_unavailable_use (decl, attr);
19346 else if (TREE_DEPRECATED (decl)
19347 && deprecated_state != DEPRECATED_SUPPRESS)
19349 attr = lookup_attribute ("deprecated", attr);
19350 warn_deprecated_use (decl, attr);
19354 else
19356 /* Look through an overload set for any templates. */
19357 bool found = false;
19359 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19360 !found && iter; ++iter)
19361 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19362 found = true;
19364 /* "an unqualified name for which name lookup either finds one or more
19365 functions or finds nothing". */
19366 if (!found
19367 && (cxx_dialect > cxx17)
19368 && !scope
19369 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19370 && tag_type == none_type)
19372 /* The "more functions" case. Just use the OVERLOAD as normally.
19373 We don't use is_overloaded_fn here to avoid considering
19374 BASELINKs. */
19375 if (TREE_CODE (decl) == OVERLOAD
19376 /* Name lookup found one function. */
19377 || TREE_CODE (decl) == FUNCTION_DECL
19378 /* Name lookup found nothing. */
19379 || decl == error_mark_node)
19380 found = true;
19383 /* "that follows the keyword template"..."in a type-only context" */
19384 if (!found && scope
19385 && (template_keyword_p || tag_type != none_type)
19386 && dependentish_scope_p (scope)
19387 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19388 found = true;
19390 if (!found)
19392 /* The name does not name a template. */
19393 cp_parser_error (parser, "expected template-name");
19394 return error_mark_node;
19396 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19397 || TREE_CODE (decl) == USING_DECL
19398 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19399 || (TREE_CODE (decl) == TYPE_DECL
19400 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19401 /* Repeat the lookup at instantiation time. */
19402 decl = identifier;
19405 return decl;
19408 /* Parse a template-argument-list.
19410 template-argument-list:
19411 template-argument ... [opt]
19412 template-argument-list , template-argument ... [opt]
19414 Returns a TREE_VEC containing the arguments. */
19416 static tree
19417 cp_parser_template_argument_list (cp_parser* parser)
19419 bool saved_in_template_argument_list_p;
19420 bool saved_ice_p;
19421 bool saved_non_ice_p;
19423 /* Don't create location wrapper nodes within a template-argument-list. */
19424 auto_suppress_location_wrappers sentinel;
19426 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19427 parser->in_template_argument_list_p = true;
19428 /* Even if the template-id appears in an integral
19429 constant-expression, the contents of the argument list do
19430 not. */
19431 saved_ice_p = parser->integral_constant_expression_p;
19432 parser->integral_constant_expression_p = false;
19433 saved_non_ice_p = parser->non_integral_constant_expression_p;
19434 parser->non_integral_constant_expression_p = false;
19436 /* Parse the arguments. */
19437 auto_vec<tree, 10> args;
19440 if (!args.is_empty ())
19441 /* Consume the comma. */
19442 cp_lexer_consume_token (parser->lexer);
19444 /* Parse the template-argument. */
19445 tree argument = cp_parser_template_argument (parser);
19447 /* If the next token is an ellipsis, we're expanding a template
19448 argument pack. */
19449 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19451 if (argument == error_mark_node)
19453 cp_token *token = cp_lexer_peek_token (parser->lexer);
19454 error_at (token->location,
19455 "expected parameter pack before %<...%>");
19457 /* Consume the `...' token. */
19458 cp_lexer_consume_token (parser->lexer);
19460 /* Make the argument into a TYPE_PACK_EXPANSION or
19461 EXPR_PACK_EXPANSION. */
19462 argument = make_pack_expansion (argument);
19465 args.safe_push (argument);
19467 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19469 int n_args = args.length ();
19470 tree vec = make_tree_vec (n_args);
19472 for (int i = 0; i < n_args; i++)
19473 TREE_VEC_ELT (vec, i) = args[i];
19475 parser->non_integral_constant_expression_p = saved_non_ice_p;
19476 parser->integral_constant_expression_p = saved_ice_p;
19477 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19478 if (CHECKING_P)
19479 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19480 return vec;
19483 /* Parse a template-argument.
19485 template-argument:
19486 assignment-expression
19487 type-id
19488 id-expression
19490 The representation is that of an assignment-expression, type-id, or
19491 id-expression -- except that the qualified id-expression is
19492 evaluated, so that the value returned is either a DECL or an
19493 OVERLOAD.
19495 Although the standard says "assignment-expression", it forbids
19496 throw-expressions or assignments in the template argument.
19497 Therefore, we use "conditional-expression" instead. */
19499 static tree
19500 cp_parser_template_argument (cp_parser* parser)
19502 tree argument;
19503 bool template_p;
19504 bool address_p;
19505 bool maybe_type_id = false;
19506 cp_token *token = NULL, *argument_start_token = NULL;
19507 location_t loc = 0;
19508 cp_id_kind idk;
19510 /* There's really no way to know what we're looking at, so we just
19511 try each alternative in order.
19513 [temp.arg]
19515 In a template-argument, an ambiguity between a type-id and an
19516 expression is resolved to a type-id, regardless of the form of
19517 the corresponding template-parameter.
19519 Therefore, we try a type-id first. */
19520 cp_parser_parse_tentatively (parser);
19521 argument = cp_parser_template_type_arg (parser);
19522 /* If there was no error parsing the type-id but the next token is a
19523 '>>', our behavior depends on which dialect of C++ we're
19524 parsing. In C++98, we probably found a typo for '> >'. But there
19525 are type-id which are also valid expressions. For instance:
19527 struct X { int operator >> (int); };
19528 template <int V> struct Foo {};
19529 Foo<X () >> 5> r;
19531 Here 'X()' is a valid type-id of a function type, but the user just
19532 wanted to write the expression "X() >> 5". Thus, we remember that we
19533 found a valid type-id, but we still try to parse the argument as an
19534 expression to see what happens.
19536 In C++0x, the '>>' will be considered two separate '>'
19537 tokens. */
19538 if (!cp_parser_error_occurred (parser)
19539 && ((cxx_dialect == cxx98
19540 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19541 /* Similarly for >= which
19542 cp_parser_next_token_ends_template_argument_p treats for
19543 diagnostics purposes as mistyped > =, but can be valid
19544 after a type-id. */
19545 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19547 maybe_type_id = true;
19548 cp_parser_abort_tentative_parse (parser);
19550 else
19552 /* If the next token isn't a `,' or a `>', then this argument wasn't
19553 really finished. This means that the argument is not a valid
19554 type-id. */
19555 if (!cp_parser_next_token_ends_template_argument_p (parser))
19556 cp_parser_error (parser, "expected template-argument");
19557 /* If that worked, we're done. */
19558 if (cp_parser_parse_definitely (parser))
19559 return argument;
19561 /* We're still not sure what the argument will be. */
19562 cp_parser_parse_tentatively (parser);
19563 /* Try a template. */
19564 argument_start_token = cp_lexer_peek_token (parser->lexer);
19565 argument = cp_parser_id_expression (parser,
19566 /*template_keyword_p=*/false,
19567 /*check_dependency_p=*/true,
19568 &template_p,
19569 /*declarator_p=*/false,
19570 /*optional_p=*/false);
19571 /* If the next token isn't a `,' or a `>', then this argument wasn't
19572 really finished. */
19573 if (!cp_parser_next_token_ends_template_argument_p (parser))
19574 cp_parser_error (parser, "expected template-argument");
19575 if (!cp_parser_error_occurred (parser))
19577 /* Figure out what is being referred to. If the id-expression
19578 was for a class template specialization, then we will have a
19579 TYPE_DECL at this point. There is no need to do name lookup
19580 at this point in that case. */
19581 if (TREE_CODE (argument) != TYPE_DECL)
19582 argument = cp_parser_lookup_name (parser, argument,
19583 none_type,
19584 /*is_template=*/template_p,
19585 /*is_namespace=*/false,
19586 /*check_dependency=*/true,
19587 /*ambiguous_decls=*/NULL,
19588 argument_start_token->location);
19589 if (TREE_CODE (argument) != TEMPLATE_DECL
19590 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19591 cp_parser_error (parser, "expected template-name");
19593 if (cp_parser_parse_definitely (parser))
19595 if (TREE_UNAVAILABLE (argument))
19596 error_unavailable_use (argument, NULL_TREE);
19597 else if (TREE_DEPRECATED (argument))
19598 warn_deprecated_use (argument, NULL_TREE);
19599 return argument;
19601 /* It must be a non-type argument. In C++17 any constant-expression is
19602 allowed. */
19603 if (cxx_dialect > cxx14)
19604 goto general_expr;
19606 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19608 -- an integral constant-expression of integral or enumeration
19609 type; or
19611 -- the name of a non-type template-parameter; or
19613 -- the name of an object or function with external linkage...
19615 -- the address of an object or function with external linkage...
19617 -- a pointer to member... */
19618 /* Look for a non-type template parameter. */
19619 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19621 cp_parser_parse_tentatively (parser);
19622 argument = cp_parser_primary_expression (parser,
19623 /*address_p=*/false,
19624 /*cast_p=*/false,
19625 /*template_arg_p=*/true,
19626 &idk);
19627 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19628 || !cp_parser_next_token_ends_template_argument_p (parser))
19629 cp_parser_simulate_error (parser);
19630 if (cp_parser_parse_definitely (parser))
19631 return argument;
19634 /* If the next token is "&", the argument must be the address of an
19635 object or function with external linkage. */
19636 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19637 if (address_p)
19639 loc = cp_lexer_peek_token (parser->lexer)->location;
19640 cp_lexer_consume_token (parser->lexer);
19642 /* See if we might have an id-expression. */
19643 token = cp_lexer_peek_token (parser->lexer);
19644 if (token->type == CPP_NAME
19645 || token->keyword == RID_OPERATOR
19646 || token->type == CPP_SCOPE
19647 || token->type == CPP_TEMPLATE_ID
19648 || token->type == CPP_NESTED_NAME_SPECIFIER)
19650 cp_parser_parse_tentatively (parser);
19651 argument = cp_parser_primary_expression (parser,
19652 address_p,
19653 /*cast_p=*/false,
19654 /*template_arg_p=*/true,
19655 &idk);
19656 if (cp_parser_error_occurred (parser)
19657 || !cp_parser_next_token_ends_template_argument_p (parser))
19658 cp_parser_abort_tentative_parse (parser);
19659 else
19661 tree probe;
19663 if (INDIRECT_REF_P (argument))
19665 /* Strip the dereference temporarily. */
19666 gcc_assert (REFERENCE_REF_P (argument));
19667 argument = TREE_OPERAND (argument, 0);
19670 /* If we're in a template, we represent a qualified-id referring
19671 to a static data member as a SCOPE_REF even if the scope isn't
19672 dependent so that we can check access control later. */
19673 probe = argument;
19674 if (TREE_CODE (probe) == SCOPE_REF)
19675 probe = TREE_OPERAND (probe, 1);
19676 if (VAR_P (probe))
19678 /* A variable without external linkage might still be a
19679 valid constant-expression, so no error is issued here
19680 if the external-linkage check fails. */
19681 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19682 cp_parser_simulate_error (parser);
19684 else if (is_overloaded_fn (argument))
19685 /* All overloaded functions are allowed; if the external
19686 linkage test does not pass, an error will be issued
19687 later. */
19689 else if (address_p
19690 && (TREE_CODE (argument) == OFFSET_REF
19691 || TREE_CODE (argument) == SCOPE_REF))
19692 /* A pointer-to-member. */
19694 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19696 else
19697 cp_parser_simulate_error (parser);
19699 if (cp_parser_parse_definitely (parser))
19701 if (address_p)
19702 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19703 NULL_TREE, tf_warning_or_error);
19704 else
19705 argument = convert_from_reference (argument);
19706 return argument;
19710 /* If the argument started with "&", there are no other valid
19711 alternatives at this point. */
19712 if (address_p)
19714 cp_parser_error (parser, "invalid non-type template argument");
19715 return error_mark_node;
19718 general_expr:
19719 /* If the argument wasn't successfully parsed as a type-id followed
19720 by '>>', the argument can only be a constant expression now.
19721 Otherwise, we try parsing the constant-expression tentatively,
19722 because the argument could really be a type-id. */
19723 if (maybe_type_id)
19724 cp_parser_parse_tentatively (parser);
19726 if (cxx_dialect <= cxx14)
19727 argument = cp_parser_constant_expression (parser);
19728 else
19730 /* In C++20, we can encounter a braced-init-list. */
19731 if (cxx_dialect >= cxx20
19732 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19733 return cp_parser_braced_list (parser);
19735 /* With C++17 generalized non-type template arguments we need to handle
19736 lvalue constant expressions, too. */
19737 argument = cp_parser_assignment_expression (parser);
19738 require_potential_constant_expression (argument);
19741 if (!maybe_type_id)
19742 return argument;
19743 if (!cp_parser_next_token_ends_template_argument_p (parser))
19744 cp_parser_error (parser, "expected template-argument");
19745 if (cp_parser_parse_definitely (parser))
19746 return argument;
19747 /* We did our best to parse the argument as a non type-id, but that
19748 was the only alternative that matched (albeit with a '>' after
19749 it). We can assume it's just a typo from the user, and a
19750 diagnostic will then be issued. */
19751 return cp_parser_template_type_arg (parser);
19754 /* Parse an explicit-instantiation.
19756 explicit-instantiation:
19757 template declaration
19759 Although the standard says `declaration', what it really means is:
19761 explicit-instantiation:
19762 template decl-specifier-seq [opt] declarator [opt] ;
19764 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19765 supposed to be allowed. A defect report has been filed about this
19766 issue.
19768 GNU Extension:
19770 explicit-instantiation:
19771 storage-class-specifier template
19772 decl-specifier-seq [opt] declarator [opt] ;
19773 function-specifier template
19774 decl-specifier-seq [opt] declarator [opt] ; */
19776 static void
19777 cp_parser_explicit_instantiation (cp_parser* parser)
19779 int declares_class_or_enum;
19780 cp_decl_specifier_seq decl_specifiers;
19781 tree extension_specifier = NULL_TREE;
19783 auto_timevar tv (TV_TEMPLATE_INST);
19785 /* Look for an (optional) storage-class-specifier or
19786 function-specifier. */
19787 if (cp_parser_allow_gnu_extensions_p (parser))
19789 extension_specifier
19790 = cp_parser_storage_class_specifier_opt (parser);
19791 if (!extension_specifier)
19792 extension_specifier
19793 = cp_parser_function_specifier_opt (parser,
19794 /*decl_specs=*/NULL);
19797 /* Look for the `template' keyword. */
19798 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19799 /* Let the front end know that we are processing an explicit
19800 instantiation. */
19801 begin_explicit_instantiation ();
19802 /* [temp.explicit] says that we are supposed to ignore access
19803 control while processing explicit instantiation directives. */
19804 push_deferring_access_checks (dk_no_check);
19805 /* Parse a decl-specifier-seq. */
19806 cp_parser_decl_specifier_seq (parser,
19807 CP_PARSER_FLAGS_OPTIONAL,
19808 &decl_specifiers,
19809 &declares_class_or_enum);
19811 cp_omp_declare_simd_data odsd;
19812 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19813 cp_parser_handle_directive_omp_attributes (parser,
19814 &decl_specifiers.attributes,
19815 &odsd, true);
19817 /* If there was exactly one decl-specifier, and it declared a class,
19818 and there's no declarator, then we have an explicit type
19819 instantiation. */
19820 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19822 tree type = check_tag_decl (&decl_specifiers,
19823 /*explicit_type_instantiation_p=*/true);
19824 /* Turn access control back on for names used during
19825 template instantiation. */
19826 pop_deferring_access_checks ();
19827 if (type)
19828 do_type_instantiation (type, extension_specifier,
19829 /*complain=*/tf_error);
19831 else
19833 cp_declarator *declarator;
19834 tree decl;
19836 /* Parse the declarator. */
19837 declarator
19838 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19839 CP_PARSER_FLAGS_NONE,
19840 /*ctor_dtor_or_conv_p=*/NULL,
19841 /*parenthesized_p=*/NULL,
19842 /*member_p=*/false,
19843 /*friend_p=*/false,
19844 /*static_p=*/false);
19845 if (declares_class_or_enum & 2)
19846 cp_parser_check_for_definition_in_return_type (declarator,
19847 decl_specifiers.type,
19848 decl_specifiers.locations[ds_type_spec]);
19849 if (declarator != cp_error_declarator)
19851 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19852 permerror (decl_specifiers.locations[ds_inline],
19853 "explicit instantiation shall not use"
19854 " %<inline%> specifier");
19855 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19856 permerror (decl_specifiers.locations[ds_constexpr],
19857 "explicit instantiation shall not use"
19858 " %<constexpr%> specifier");
19859 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19860 permerror (decl_specifiers.locations[ds_consteval],
19861 "explicit instantiation shall not use"
19862 " %<consteval%> specifier");
19864 decl = grokdeclarator (declarator, &decl_specifiers,
19865 NORMAL, 0, &decl_specifiers.attributes);
19866 /* Turn access control back on for names used during
19867 template instantiation. */
19868 pop_deferring_access_checks ();
19869 /* Do the explicit instantiation. */
19870 do_decl_instantiation (decl, extension_specifier);
19872 else
19874 pop_deferring_access_checks ();
19875 /* Skip the body of the explicit instantiation. */
19876 cp_parser_skip_to_end_of_statement (parser);
19879 /* We're done with the instantiation. */
19880 end_explicit_instantiation ();
19882 cp_parser_consume_semicolon_at_end_of_statement (parser);
19884 cp_finalize_omp_declare_simd (parser, &odsd);
19887 /* Parse an explicit-specialization.
19889 explicit-specialization:
19890 template < > declaration
19892 Although the standard says `declaration', what it really means is:
19894 explicit-specialization:
19895 template <> decl-specifier [opt] init-declarator [opt] ;
19896 template <> function-definition
19897 template <> explicit-specialization
19898 template <> template-declaration */
19900 static void
19901 cp_parser_explicit_specialization (cp_parser* parser)
19903 cp_token *token = cp_lexer_peek_token (parser->lexer);
19905 /* Look for the `template' keyword. */
19906 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19907 /* Look for the `<'. */
19908 cp_parser_require (parser, CPP_LESS, RT_LESS);
19909 /* Look for the `>'. */
19910 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19911 /* We have processed another parameter list. */
19912 ++parser->num_template_parameter_lists;
19914 /* [temp]
19916 A template ... explicit specialization ... shall not have C
19917 linkage. */
19918 bool need_lang_pop = current_lang_name == lang_name_c;
19919 if (need_lang_pop)
19921 error_at (token->location, "template specialization with C linkage");
19922 maybe_show_extern_c_location ();
19924 /* Give it C++ linkage to avoid confusing other parts of the
19925 front end. */
19926 push_lang_context (lang_name_cplusplus);
19929 /* Let the front end know that we are beginning a specialization. */
19930 if (begin_specialization ())
19932 /* If the next keyword is `template', we need to figure out
19933 whether or not we're looking a template-declaration. */
19934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19936 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19937 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19938 cp_parser_template_declaration_after_export (parser,
19939 /*member_p=*/false);
19940 else
19941 cp_parser_explicit_specialization (parser);
19943 else
19944 /* Parse the dependent declaration. */
19945 cp_parser_single_declaration (parser,
19946 /*checks=*/NULL,
19947 /*member_p=*/false,
19948 /*explicit_specialization_p=*/true,
19949 /*friend_p=*/NULL);
19952 /* We're done with the specialization. */
19953 end_specialization ();
19955 /* For the erroneous case of a template with C linkage, we pushed an
19956 implicit C++ linkage scope; exit that scope now. */
19957 if (need_lang_pop)
19958 pop_lang_context ();
19960 /* We're done with this parameter list. */
19961 --parser->num_template_parameter_lists;
19964 /* Preserve the attributes across a garbage collect (by making it a GC
19965 root), which can occur when parsing a member function. */
19967 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19969 /* Parse a type-specifier.
19971 type-specifier:
19972 simple-type-specifier
19973 class-specifier
19974 enum-specifier
19975 elaborated-type-specifier
19976 cv-qualifier
19978 GNU Extension:
19980 type-specifier:
19981 __complex__
19983 Returns a representation of the type-specifier. For a
19984 class-specifier, enum-specifier, or elaborated-type-specifier, a
19985 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19987 The parser flags FLAGS is used to control type-specifier parsing.
19989 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19990 in a decl-specifier-seq.
19992 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19993 class-specifier, enum-specifier, or elaborated-type-specifier, then
19994 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19995 if a type is declared; 2 if it is defined. Otherwise, it is set to
19996 zero.
19998 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19999 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
20000 is set to FALSE. */
20002 static tree
20003 cp_parser_type_specifier (cp_parser* parser,
20004 cp_parser_flags flags,
20005 cp_decl_specifier_seq *decl_specs,
20006 bool is_declaration,
20007 int* declares_class_or_enum,
20008 bool* is_cv_qualifier)
20010 tree type_spec = NULL_TREE;
20011 cp_token *token;
20012 enum rid keyword;
20013 cp_decl_spec ds = ds_last;
20015 /* Assume this type-specifier does not declare a new type. */
20016 if (declares_class_or_enum)
20017 *declares_class_or_enum = 0;
20018 /* And that it does not specify a cv-qualifier. */
20019 if (is_cv_qualifier)
20020 *is_cv_qualifier = false;
20021 /* Peek at the next token. */
20022 token = cp_lexer_peek_token (parser->lexer);
20024 /* If we're looking at a keyword, we can use that to guide the
20025 production we choose. */
20026 keyword = token->keyword;
20027 switch (keyword)
20029 case RID_ENUM:
20030 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20031 goto elaborated_type_specifier;
20033 /* Look for the enum-specifier. */
20034 type_spec = cp_parser_enum_specifier (parser);
20035 /* If that worked, we're done. */
20036 if (type_spec)
20038 if (declares_class_or_enum)
20039 *declares_class_or_enum = 2;
20040 if (decl_specs)
20041 cp_parser_set_decl_spec_type (decl_specs,
20042 type_spec,
20043 token,
20044 /*type_definition_p=*/true);
20045 return type_spec;
20047 else
20048 goto elaborated_type_specifier;
20050 /* Any of these indicate either a class-specifier, or an
20051 elaborated-type-specifier. */
20052 case RID_CLASS:
20053 case RID_STRUCT:
20054 case RID_UNION:
20055 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20056 goto elaborated_type_specifier;
20058 /* Parse tentatively so that we can back up if we don't find a
20059 class-specifier. */
20060 cp_parser_parse_tentatively (parser);
20061 if (decl_specs->attributes)
20062 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
20063 /* Look for the class-specifier. */
20064 type_spec = cp_parser_class_specifier (parser);
20065 if (decl_specs->attributes)
20066 cp_parser_decl_specs_attrs->pop ();
20067 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
20068 /* If that worked, we're done. */
20069 if (cp_parser_parse_definitely (parser))
20071 if (declares_class_or_enum)
20072 *declares_class_or_enum = 2;
20073 if (decl_specs)
20074 cp_parser_set_decl_spec_type (decl_specs,
20075 type_spec,
20076 token,
20077 /*type_definition_p=*/true);
20078 return type_spec;
20081 /* Fall through. */
20082 elaborated_type_specifier:
20083 /* We're declaring (not defining) a class or enum. */
20084 if (declares_class_or_enum)
20085 *declares_class_or_enum = 1;
20087 /* Fall through. */
20088 case RID_TYPENAME:
20089 /* Look for an elaborated-type-specifier. */
20090 type_spec
20091 = (cp_parser_elaborated_type_specifier
20092 (parser,
20093 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
20094 is_declaration));
20095 if (decl_specs)
20096 cp_parser_set_decl_spec_type (decl_specs,
20097 type_spec,
20098 token,
20099 /*type_definition_p=*/false);
20100 return type_spec;
20102 case RID_CONST:
20103 ds = ds_const;
20104 if (is_cv_qualifier)
20105 *is_cv_qualifier = true;
20106 break;
20108 case RID_VOLATILE:
20109 ds = ds_volatile;
20110 if (is_cv_qualifier)
20111 *is_cv_qualifier = true;
20112 break;
20114 case RID_RESTRICT:
20115 ds = ds_restrict;
20116 if (is_cv_qualifier)
20117 *is_cv_qualifier = true;
20118 break;
20120 case RID_COMPLEX:
20121 /* The `__complex__' keyword is a GNU extension. */
20122 ds = ds_complex;
20123 break;
20125 default:
20126 break;
20129 /* Handle simple keywords. */
20130 if (ds != ds_last)
20132 if (decl_specs)
20134 set_and_check_decl_spec_loc (decl_specs, ds, token);
20135 decl_specs->any_specifiers_p = true;
20137 return cp_lexer_consume_token (parser->lexer)->u.value;
20140 /* If we do not already have a type-specifier, assume we are looking
20141 at a simple-type-specifier. */
20142 type_spec = cp_parser_simple_type_specifier (parser,
20143 decl_specs,
20144 flags);
20146 /* If we didn't find a type-specifier, and a type-specifier was not
20147 optional in this context, issue an error message. */
20148 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20150 cp_parser_error (parser, "expected type specifier");
20151 return error_mark_node;
20154 return type_spec;
20157 /* Parse a simple-type-specifier.
20159 simple-type-specifier:
20160 :: [opt] nested-name-specifier [opt] type-name
20161 :: [opt] nested-name-specifier template template-id
20162 char
20163 wchar_t
20164 bool
20165 short
20167 long
20168 signed
20169 unsigned
20170 float
20171 double
20172 void
20174 C++11 Extension:
20176 simple-type-specifier:
20177 auto
20178 decltype ( expression )
20179 char16_t
20180 char32_t
20181 __underlying_type ( type-id )
20183 C++17 extension:
20185 nested-name-specifier(opt) template-name
20187 GNU Extension:
20189 simple-type-specifier:
20190 __int128
20191 __typeof__ unary-expression
20192 __typeof__ ( type-id )
20193 __typeof__ ( type-id ) { initializer-list , [opt] }
20195 Concepts Extension:
20197 simple-type-specifier:
20198 constrained-type-specifier
20200 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
20201 appropriately updated. */
20203 static tree
20204 cp_parser_simple_type_specifier (cp_parser* parser,
20205 cp_decl_specifier_seq *decl_specs,
20206 cp_parser_flags flags)
20208 tree type = NULL_TREE;
20209 cp_token *token;
20210 int idx;
20212 /* Peek at the next token. */
20213 token = cp_lexer_peek_token (parser->lexer);
20215 /* If we're looking at a keyword, things are easy. */
20216 switch (token->keyword)
20218 case RID_CHAR:
20219 if (decl_specs)
20220 decl_specs->explicit_char_p = true;
20221 type = char_type_node;
20222 break;
20223 case RID_CHAR8:
20224 type = char8_type_node;
20225 break;
20226 case RID_CHAR16:
20227 type = char16_type_node;
20228 break;
20229 case RID_CHAR32:
20230 type = char32_type_node;
20231 break;
20232 case RID_WCHAR:
20233 type = wchar_type_node;
20234 break;
20235 case RID_BOOL:
20236 type = boolean_type_node;
20237 break;
20238 case RID_SHORT:
20239 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
20240 type = short_integer_type_node;
20241 break;
20242 case RID_INT:
20243 if (decl_specs)
20244 decl_specs->explicit_int_p = true;
20245 type = integer_type_node;
20246 break;
20247 case RID_INT_N_0:
20248 case RID_INT_N_1:
20249 case RID_INT_N_2:
20250 case RID_INT_N_3:
20251 idx = token->keyword - RID_INT_N_0;
20252 if (! int_n_enabled_p [idx])
20253 break;
20254 if (decl_specs)
20256 decl_specs->explicit_intN_p = true;
20257 decl_specs->int_n_idx = idx;
20258 /* Check if the alternate "__intN__" form has been used instead of
20259 "__intN". */
20260 if (startswith (IDENTIFIER_POINTER (token->u.value)
20261 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
20262 decl_specs->int_n_alt = true;
20264 type = int_n_trees [idx].signed_type;
20265 break;
20266 case RID_LONG:
20267 if (decl_specs)
20268 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
20269 type = long_integer_type_node;
20270 break;
20271 case RID_SIGNED:
20272 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
20273 type = integer_type_node;
20274 break;
20275 case RID_UNSIGNED:
20276 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
20277 type = unsigned_type_node;
20278 break;
20279 case RID_FLOAT:
20280 type = float_type_node;
20281 break;
20282 case RID_DOUBLE:
20283 type = double_type_node;
20284 break;
20285 CASE_RID_FLOATN_NX:
20286 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
20287 if (type == NULL_TREE)
20288 error ("%<_Float%d%s%> is not supported on this target",
20289 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
20290 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
20291 ? "x" : "");
20292 break;
20293 case RID_VOID:
20294 type = void_type_node;
20295 break;
20297 case RID_AUTO:
20298 maybe_warn_cpp0x (CPP0X_AUTO);
20299 if (parser->auto_is_implicit_function_template_parm_p)
20301 /* The 'auto' might be the placeholder return type for a function decl
20302 with trailing return type. */
20303 bool have_trailing_return_fn_decl = false;
20305 cp_parser_parse_tentatively (parser);
20306 cp_lexer_consume_token (parser->lexer);
20307 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
20308 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
20309 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20310 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
20312 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20314 cp_lexer_consume_token (parser->lexer);
20315 cp_parser_skip_to_closing_parenthesis (parser,
20316 /*recovering*/false,
20317 /*or_comma*/false,
20318 /*consume_paren*/true);
20319 continue;
20322 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20324 have_trailing_return_fn_decl = true;
20325 break;
20328 cp_lexer_consume_token (parser->lexer);
20330 cp_parser_abort_tentative_parse (parser);
20332 if (have_trailing_return_fn_decl)
20334 type = make_auto ();
20335 break;
20338 if (cxx_dialect >= cxx14)
20340 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20341 type = TREE_TYPE (type);
20343 else
20344 type = error_mark_node;
20346 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20348 if (cxx_dialect < cxx14)
20349 error_at (token->location,
20350 "use of %<auto%> in lambda parameter declaration "
20351 "only available with "
20352 "%<-std=c++14%> or %<-std=gnu++14%>");
20354 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20355 pedwarn (token->location, 0,
20356 "use of %<auto%> in template argument "
20357 "only available with %<-fconcepts-ts%>");
20358 else if (!flag_concepts)
20359 pedwarn (token->location, 0,
20360 "use of %<auto%> in parameter declaration "
20361 "only available with %<-std=c++20%> or %<-fconcepts%>");
20362 else if (cxx_dialect < cxx14)
20363 error_at (token->location,
20364 "use of %<auto%> in parameter declaration "
20365 "only available with "
20366 "%<-std=c++14%> or %<-std=gnu++14%>");
20368 else
20369 type = make_auto ();
20370 break;
20372 case RID_DECLTYPE:
20373 /* Since DR 743, decltype can either be a simple-type-specifier by
20374 itself or begin a nested-name-specifier. Parsing it will replace
20375 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20376 handling below decide what to do. */
20377 cp_parser_decltype (parser);
20378 cp_lexer_set_token_position (parser->lexer, token);
20379 break;
20381 case RID_TYPEOF:
20382 /* Consume the `typeof' token. */
20383 cp_lexer_consume_token (parser->lexer);
20384 /* Parse the operand to `typeof'. */
20385 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20386 /* If it is not already a TYPE, take its type. */
20387 if (!TYPE_P (type))
20388 type = finish_typeof (type);
20390 if (decl_specs)
20391 cp_parser_set_decl_spec_type (decl_specs, type,
20392 token,
20393 /*type_definition_p=*/false);
20395 return type;
20397 default:
20398 /* If token is a type-yielding built-in traits, parse it. */
20399 const cp_trait* trait = cp_lexer_peek_trait_type (parser->lexer);
20400 if (trait)
20402 type = cp_parser_trait (parser, trait);
20403 if (decl_specs)
20404 cp_parser_set_decl_spec_type (decl_specs, type,
20405 token,
20406 /*type_definition_p=*/false);
20408 return type;
20411 break;
20414 /* If token is an already-parsed decltype not followed by ::,
20415 it's a simple-type-specifier. */
20416 if (token->type == CPP_DECLTYPE
20417 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20419 type = saved_checks_value (token->u.tree_check_value);
20420 if (decl_specs)
20422 cp_parser_set_decl_spec_type (decl_specs, type,
20423 token,
20424 /*type_definition_p=*/false);
20425 /* Remember that we are handling a decltype in order to
20426 implement the resolution of DR 1510 when the argument
20427 isn't instantiation dependent. */
20428 decl_specs->decltype_p = true;
20430 cp_lexer_consume_token (parser->lexer);
20431 return type;
20434 /* If the type-specifier was for a built-in type, we're done. */
20435 if (type)
20437 /* Record the type. */
20438 if (decl_specs
20439 && (token->keyword != RID_SIGNED
20440 && token->keyword != RID_UNSIGNED
20441 && token->keyword != RID_SHORT
20442 && token->keyword != RID_LONG))
20443 cp_parser_set_decl_spec_type (decl_specs,
20444 type,
20445 token,
20446 /*type_definition_p=*/false);
20447 if (decl_specs)
20448 decl_specs->any_specifiers_p = true;
20450 /* Consume the token. */
20451 cp_lexer_consume_token (parser->lexer);
20453 if (type == error_mark_node)
20454 return error_mark_node;
20456 /* There is no valid C++ program where a non-template type is
20457 followed by a "<". That usually indicates that the user thought
20458 that the type was a template. */
20459 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20460 token->location);
20462 return TYPE_NAME (type);
20465 /* The type-specifier must be a user-defined type. */
20466 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20468 bool qualified_p;
20469 bool global_p;
20470 const bool typename_p = (cxx_dialect >= cxx20
20471 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20473 /* Don't gobble tokens or issue error messages if this is an
20474 optional type-specifier. */
20475 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20476 cp_parser_parse_tentatively (parser);
20478 /* Remember current tentative parsing state -- if we know we need
20479 a type, we can give better diagnostics here. */
20480 bool tent = cp_parser_parsing_tentatively (parser);
20482 token = cp_lexer_peek_token (parser->lexer);
20484 /* Look for the optional `::' operator. */
20485 global_p
20486 = (cp_parser_global_scope_opt (parser,
20487 /*current_scope_valid_p=*/false)
20488 != NULL_TREE);
20489 /* Look for the nested-name specifier. */
20490 qualified_p
20491 = (cp_parser_nested_name_specifier_opt (parser,
20492 /*typename_keyword_p=*/false,
20493 /*check_dependency_p=*/true,
20494 /*type_p=*/false,
20495 /*is_declaration=*/false)
20496 != NULL_TREE);
20497 /* If we have seen a nested-name-specifier, and the next token
20498 is `template', then we are using the template-id production. */
20499 if (parser->scope
20500 && cp_parser_optional_template_keyword (parser))
20502 /* Look for the template-id. */
20503 type = cp_parser_template_id (parser,
20504 /*template_keyword_p=*/true,
20505 /*check_dependency_p=*/true,
20506 none_type,
20507 /*is_declaration=*/false);
20508 /* If the template-id did not name a type, we are out of
20509 luck. */
20510 if (TREE_CODE (type) != TYPE_DECL)
20512 /* ...unless we pretend we have seen 'typename'. */
20513 if (typename_p)
20514 type = cp_parser_make_typename_type (parser, type,
20515 token->location);
20516 else
20518 cp_parser_error (parser, "expected template-id for type");
20519 type = error_mark_node;
20523 /* DR 1812: A < following a qualified-id in a typename-specifier
20524 could safely be assumed to begin a template argument list, so
20525 the template keyword should be optional. */
20526 else if (parser->scope
20527 && qualified_p
20528 && typename_p
20529 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20531 cp_parser_parse_tentatively (parser);
20533 type = cp_parser_template_id (parser,
20534 /*template_keyword_p=*/true,
20535 /*check_dependency_p=*/true,
20536 none_type,
20537 /*is_declaration=*/false);
20538 /* This is handled below, so back off. */
20539 if (type && concept_check_p (type))
20540 cp_parser_simulate_error (parser);
20542 if (!cp_parser_parse_definitely (parser))
20543 type = NULL_TREE;
20544 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20545 type = make_typename_type (parser->scope, type, typename_type,
20546 /*complain=*/tf_error);
20547 else if (TREE_CODE (type) != TYPE_DECL)
20548 type = NULL_TREE;
20551 /* Otherwise, look for a type-name. */
20552 if (!type)
20554 if (cxx_dialect >= cxx17 || flag_concepts)
20555 cp_parser_parse_tentatively (parser);
20557 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20559 if ((cxx_dialect >= cxx17 || flag_concepts)
20560 && !cp_parser_parse_definitely (parser))
20561 type = NULL_TREE;
20564 if (!type && flag_concepts && decl_specs)
20566 /* Try for a type-constraint with template arguments. We check
20567 decl_specs here to avoid trying this for a functional cast. */
20569 cp_parser_parse_tentatively (parser);
20571 type = cp_parser_template_id (parser,
20572 /*template_keyword_p=*/false,
20573 /*check_dependency_p=*/true,
20574 none_type,
20575 /*is_declaration=*/false);
20576 if (type && concept_check_p (type))
20578 location_t loc = EXPR_LOCATION (type);
20579 type = cp_parser_placeholder_type_specifier (parser, loc,
20580 type, tent);
20581 if (tent && type == error_mark_node)
20582 /* Perhaps it's a concept-check expression. */
20583 cp_parser_simulate_error (parser);
20585 else
20586 cp_parser_simulate_error (parser);
20588 if (!cp_parser_parse_definitely (parser))
20589 type = NULL_TREE;
20592 if (!type && cxx_dialect >= cxx17)
20594 /* Try class template argument deduction or type-constraint without
20595 template arguments. */
20596 tree name = cp_parser_identifier (parser);
20597 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20598 && parser->scope != error_mark_node)
20600 location_t loc
20601 = cp_lexer_previous_token (parser->lexer)->location;
20602 tree tmpl = cp_parser_lookup_name (parser, name,
20603 none_type,
20604 /*is_template=*/false,
20605 /*is_namespace=*/false,
20606 /*check_dependency=*/true,
20607 /*ambiguous_decls=*/NULL,
20608 token->location);
20609 if (tmpl && tmpl != error_mark_node
20610 && ctad_template_p (tmpl))
20611 type = make_template_placeholder (tmpl);
20612 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20613 type = cp_parser_placeholder_type_specifier (parser, loc,
20614 tmpl, tent);
20615 else
20617 type = error_mark_node;
20618 if (!cp_parser_simulate_error (parser))
20619 cp_parser_name_lookup_error (parser, name, tmpl,
20620 NLE_TYPE, token->location);
20623 else
20624 type = error_mark_node;
20627 /* If it didn't work out, we don't have a TYPE. */
20628 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20629 && !cp_parser_parse_definitely (parser))
20630 type = NULL_TREE;
20632 /* Keep track of all name-lookups performed in class scopes. */
20633 if (type
20634 && !global_p
20635 && !qualified_p
20636 && TREE_CODE (type) == TYPE_DECL
20637 && identifier_p (DECL_NAME (type)))
20638 maybe_note_name_used_in_class (DECL_NAME (type), type);
20640 if (type && decl_specs)
20641 cp_parser_set_decl_spec_type (decl_specs, type,
20642 token,
20643 /*type_definition_p=*/false);
20646 /* If we didn't get a type-name, issue an error message. */
20647 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20649 cp_parser_error (parser, "expected type-name");
20650 return error_mark_node;
20653 if (type && type != error_mark_node)
20655 /* See if TYPE is an Objective-C type, and if so, parse and
20656 accept any protocol references following it. Do this before
20657 the cp_parser_check_for_invalid_template_id() call, because
20658 Objective-C types can be followed by '<...>' which would
20659 enclose protocol names rather than template arguments, and so
20660 everything is fine. */
20661 if (c_dialect_objc () && !parser->scope
20662 && (objc_is_id (type) || objc_is_class_name (type)))
20664 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20665 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20667 /* Clobber the "unqualified" type previously entered into
20668 DECL_SPECS with the new, improved protocol-qualified version. */
20669 if (decl_specs)
20670 decl_specs->type = qual_type;
20672 return qual_type;
20675 /* There is no valid C++ program where a non-template type is
20676 followed by a "<". That usually indicates that the user
20677 thought that the type was a template. */
20678 cp_parser_check_for_invalid_template_id (parser, type,
20679 none_type,
20680 token->location);
20683 return type;
20686 /* Parse the remainder of a placholder-type-specifier.
20688 placeholder-type-specifier:
20689 type-constraint_opt auto
20690 type-constraint_opt decltype(auto)
20692 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20693 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20694 parses the placeholder type, and performs some contextual syntactic analysis.
20696 LOC provides the location of the template name.
20698 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20699 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20700 might actually be a concept-check,
20702 Note that the Concepts TS allows the auto or decltype(auto) to be
20703 omitted in a constrained-type-specifier. */
20705 static tree
20706 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20707 tree tmpl, bool tentative)
20709 if (tmpl == error_mark_node)
20710 return error_mark_node;
20712 tree orig_tmpl = tmpl;
20714 /* Get the arguments as written for subsequent analysis. */
20715 tree args = NULL_TREE;
20716 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20718 args = TREE_OPERAND (tmpl, 1);
20719 tmpl = TREE_OPERAND (tmpl, 0);
20721 else
20722 /* A concept-name with no arguments can't be an expression. */
20723 tentative = false;
20725 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20727 /* Get the concept and prototype parameter for the constraint. */
20728 tree_pair info = finish_type_constraints (tmpl, args, complain);
20729 tree con = info.first;
20730 tree proto = info.second;
20731 if (con == error_mark_node)
20732 return error_mark_node;
20734 /* As per the standard, require auto or decltype(auto), except in some
20735 cases (template parameter lists, -fconcepts-ts enabled). */
20736 cp_token *placeholder = NULL, *close_paren = NULL;
20737 if (cxx_dialect >= cxx20)
20739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20740 placeholder = cp_lexer_consume_token (parser->lexer);
20741 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20743 placeholder = cp_lexer_consume_token (parser->lexer);
20744 matching_parens parens;
20745 parens.require_open (parser);
20746 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20747 close_paren = parens.require_close (parser);
20751 /* A type constraint constrains a contextually determined type or type
20752 parameter pack. However, the Concepts TS does allow concepts
20753 to introduce non-type and template template parameters. */
20754 if (TREE_CODE (proto) != TYPE_DECL)
20756 if (!flag_concepts_ts
20757 || !processing_template_parmlist)
20759 if (!tentative)
20761 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20762 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20764 return error_mark_node;
20768 /* In a template parameter list, a type-parameter can be introduced
20769 by type-constraints alone. */
20770 if (processing_template_parmlist && !placeholder)
20772 /* In a default argument we may not be creating new parameters. */
20773 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20775 /* If this assert turns out to be false, do error() instead. */
20776 gcc_assert (tentative);
20777 return error_mark_node;
20779 return build_constrained_parameter (con, proto, args);
20782 /* Diagnose issues placeholder issues. */
20783 if (!flag_concepts_ts
20784 && !parser->in_result_type_constraint_p
20785 && !placeholder)
20787 if (tentative)
20788 /* Perhaps it's a concept-check expression (c++/91073). */
20789 return error_mark_node;
20791 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20792 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20793 error_at (input_location,
20794 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20795 /* Fall through. This is an error of omission. */
20797 else if (parser->in_result_type_constraint_p && placeholder)
20799 /* A trailing return type only allows type-constraints. */
20800 error_at (input_location,
20801 "unexpected placeholder in constrained result type");
20804 /* In a parameter-declaration-clause, a placeholder-type-specifier
20805 results in an invented template parameter. */
20806 if (parser->auto_is_implicit_function_template_parm_p)
20808 if (close_paren)
20810 location_t loc = make_location (placeholder->location,
20811 placeholder->location,
20812 close_paren->location);
20813 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20814 return error_mark_node;
20816 tree parm = build_constrained_parameter (con, proto, args);
20817 return synthesize_implicit_template_parm (parser, parm);
20820 /* Determine if the type should be deduced using template argument
20821 deduction or decltype deduction. Note that the latter is always
20822 used for type-constraints in trailing return types. */
20823 bool decltype_p = placeholder
20824 ? placeholder->keyword == RID_DECLTYPE
20825 : parser->in_result_type_constraint_p;
20827 /* Otherwise, this is the type of a variable or return type. */
20828 if (decltype_p)
20829 return make_constrained_decltype_auto (con, args);
20830 else
20831 return make_constrained_auto (con, args);
20834 /* Parse a type-name.
20836 type-name:
20837 class-name
20838 enum-name
20839 typedef-name
20840 simple-template-id [in c++0x]
20842 enum-name:
20843 identifier
20845 typedef-name:
20846 identifier
20848 Concepts:
20850 type-name:
20851 concept-name
20852 partial-concept-id
20854 concept-name:
20855 identifier
20857 Returns a TYPE_DECL for the type. */
20859 static tree
20860 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20862 tree type_decl;
20864 /* We can't know yet whether it is a class-name or not. */
20865 cp_parser_parse_tentatively (parser);
20866 /* Try a class-name. */
20867 type_decl = cp_parser_class_name (parser,
20868 typename_keyword_p,
20869 /*template_keyword_p=*/false,
20870 none_type,
20871 /*check_dependency_p=*/true,
20872 /*class_head_p=*/false,
20873 /*is_declaration=*/false);
20874 /* If it's not a class-name, keep looking. */
20875 if (!cp_parser_parse_definitely (parser))
20877 if (cxx_dialect < cxx11)
20878 /* It must be a typedef-name or an enum-name. */
20879 return cp_parser_nonclass_name (parser);
20881 cp_parser_parse_tentatively (parser);
20882 /* It is either a simple-template-id representing an
20883 instantiation of an alias template... */
20884 type_decl = cp_parser_template_id (parser,
20885 /*template_keyword_p=*/false,
20886 /*check_dependency_p=*/true,
20887 none_type,
20888 /*is_declaration=*/false);
20889 /* Note that this must be an instantiation of an alias template
20890 because [temp.names]/6 says:
20892 A template-id that names an alias template specialization
20893 is a type-name.
20895 Whereas [temp.names]/7 says:
20897 A simple-template-id that names a class template
20898 specialization is a class-name.
20900 With concepts, this could also be a partial-concept-id that
20901 declares a non-type template parameter. */
20902 if (type_decl != NULL_TREE
20903 && TREE_CODE (type_decl) == TYPE_DECL
20904 && TYPE_DECL_ALIAS_P (type_decl))
20905 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20906 else
20907 cp_parser_simulate_error (parser);
20909 if (!cp_parser_parse_definitely (parser))
20910 /* ... Or a typedef-name or an enum-name. */
20911 return cp_parser_nonclass_name (parser);
20914 return type_decl;
20917 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20918 or a concept-name.
20920 enum-name:
20921 identifier
20923 typedef-name:
20924 identifier
20926 concept-name:
20927 identifier
20929 Returns a TYPE_DECL for the type. */
20931 static tree
20932 cp_parser_nonclass_name (cp_parser* parser)
20934 tree type_decl;
20935 tree identifier;
20937 cp_token *token = cp_lexer_peek_token (parser->lexer);
20938 identifier = cp_parser_identifier (parser);
20939 if (identifier == error_mark_node)
20940 return error_mark_node;
20942 /* Look up the type-name. */
20943 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20945 type_decl = strip_using_decl (type_decl);
20947 if (TREE_CODE (type_decl) != TYPE_DECL
20948 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20950 /* See if this is an Objective-C type. */
20951 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20952 tree type = objc_get_protocol_qualified_type (identifier, protos);
20953 if (type)
20954 type_decl = TYPE_NAME (type);
20957 /* Issue an error if we did not find a type-name. */
20958 if (TREE_CODE (type_decl) != TYPE_DECL
20959 /* In Objective-C, we have the complication that class names are
20960 normally type names and start declarations (eg, the
20961 "NSObject" in "NSObject *object;"), but can be used in an
20962 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20963 is an expression. So, a classname followed by a dot is not a
20964 valid type-name. */
20965 || (objc_is_class_name (TREE_TYPE (type_decl))
20966 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20968 if (!cp_parser_simulate_error (parser))
20969 cp_parser_name_lookup_error (parser, identifier, type_decl,
20970 NLE_TYPE, token->location);
20971 return error_mark_node;
20973 /* Remember that the name was used in the definition of the
20974 current class so that we can check later to see if the
20975 meaning would have been different after the class was
20976 entirely defined. */
20977 else if (type_decl != error_mark_node
20978 && !parser->scope)
20979 maybe_note_name_used_in_class (identifier, type_decl);
20981 return type_decl;
20984 /* Parse an elaborated-type-specifier. Note that the grammar given
20985 here incorporates the resolution to DR68.
20987 elaborated-type-specifier:
20988 class-key :: [opt] nested-name-specifier [opt] identifier
20989 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20990 enum-key :: [opt] nested-name-specifier [opt] identifier
20991 typename :: [opt] nested-name-specifier identifier
20992 typename :: [opt] nested-name-specifier template [opt]
20993 template-id
20995 GNU extension:
20997 elaborated-type-specifier:
20998 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20999 class-key attributes :: [opt] nested-name-specifier [opt]
21000 template [opt] template-id
21001 enum attributes :: [opt] nested-name-specifier [opt] identifier
21003 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
21004 declared `friend'. If IS_DECLARATION is TRUE, then this
21005 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
21006 something is being declared.
21008 Returns the TYPE specified. */
21010 static tree
21011 cp_parser_elaborated_type_specifier (cp_parser* parser,
21012 bool is_friend,
21013 bool is_declaration)
21015 enum tag_types tag_type;
21016 tree identifier;
21017 tree type = NULL_TREE;
21018 tree attributes = NULL_TREE;
21019 tree globalscope;
21020 cp_token *token = NULL;
21022 /* For class and enum types the location of the class-key or enum-key. */
21023 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
21024 /* For a scoped enum, the 'class' or 'struct' keyword id. */
21025 rid scoped_key = RID_MAX;
21027 /* See if we're looking at the `enum' keyword. */
21028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
21030 /* Consume the `enum' token. */
21031 cp_lexer_consume_token (parser->lexer);
21032 /* Remember that it's an enumeration type. */
21033 tag_type = enum_type;
21034 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
21035 enums) is used here. */
21036 cp_token *token = cp_lexer_peek_token (parser->lexer);
21037 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
21038 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
21040 location_t loc = token->location;
21041 gcc_rich_location richloc (loc);
21042 richloc.add_range (input_location);
21043 richloc.add_fixit_remove ();
21044 pedwarn (&richloc, 0, "elaborated-type-specifier for "
21045 "a scoped enum must not use the %qD keyword",
21046 token->u.value);
21047 /* Consume the `struct' or `class' and parse it anyway. */
21048 cp_lexer_consume_token (parser->lexer);
21049 /* Create a combined location for the whole scoped-enum-key. */
21050 key_loc = make_location (key_loc, key_loc, loc);
21052 else
21053 scoped_key = RID_MAX;
21055 /* Parse the attributes. */
21056 attributes = cp_parser_attributes_opt (parser);
21058 /* Or, it might be `typename'. */
21059 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21060 RID_TYPENAME))
21062 /* Consume the `typename' token. */
21063 cp_lexer_consume_token (parser->lexer);
21064 /* Remember that it's a `typename' type. */
21065 tag_type = typename_type;
21067 /* Otherwise it must be a class-key. */
21068 else
21070 key_loc = cp_lexer_peek_token (parser->lexer)->location;
21071 tag_type = cp_parser_class_key (parser);
21072 if (tag_type == none_type)
21073 return error_mark_node;
21074 /* Parse the attributes. */
21075 attributes = cp_parser_attributes_opt (parser);
21078 /* Look for the `::' operator. */
21079 globalscope = cp_parser_global_scope_opt (parser,
21080 /*current_scope_valid_p=*/false);
21081 /* Look for the nested-name-specifier. */
21082 tree nested_name_specifier;
21083 if (tag_type == typename_type && !globalscope)
21085 nested_name_specifier
21086 = cp_parser_nested_name_specifier (parser,
21087 /*typename_keyword_p=*/true,
21088 /*check_dependency_p=*/true,
21089 /*type_p=*/true,
21090 is_declaration);
21091 if (!nested_name_specifier)
21092 return error_mark_node;
21094 else
21095 /* Even though `typename' is not present, the proposed resolution
21096 to Core Issue 180 says that in `class A<T>::B', `B' should be
21097 considered a type-name, even if `A<T>' is dependent. */
21098 nested_name_specifier
21099 = cp_parser_nested_name_specifier_opt (parser,
21100 /*typename_keyword_p=*/true,
21101 /*check_dependency_p=*/true,
21102 /*type_p=*/true,
21103 is_declaration);
21104 /* For everything but enumeration types, consider a template-id.
21105 For an enumeration type, consider only a plain identifier. */
21106 if (tag_type != enum_type)
21108 bool template_p = false;
21109 tree decl;
21111 /* Allow the `template' keyword. */
21112 template_p = cp_parser_optional_template_keyword (parser);
21113 /* If we didn't see `template', we don't know if there's a
21114 template-id or not. */
21115 if (!template_p)
21116 cp_parser_parse_tentatively (parser);
21117 /* The `template' keyword must follow a nested-name-specifier. */
21118 else if (!nested_name_specifier && !globalscope)
21120 cp_parser_error (parser, "%<template%> must follow a nested-"
21121 "name-specifier");
21122 return error_mark_node;
21125 /* Parse the template-id. */
21126 token = cp_lexer_peek_token (parser->lexer);
21127 decl = cp_parser_template_id (parser, template_p,
21128 /*check_dependency_p=*/true,
21129 tag_type,
21130 is_declaration);
21131 /* If we didn't find a template-id, look for an ordinary
21132 identifier. */
21133 if (!template_p && !cp_parser_parse_definitely (parser))
21135 /* We can get here when cp_parser_template_id, called by
21136 cp_parser_class_name with tag_type == none_type, succeeds
21137 and caches a BASELINK. Then, when called again here,
21138 instead of failing and returning an error_mark_node
21139 returns it (see template/typename17.C in C++11).
21140 ??? Could we diagnose this earlier? */
21141 else if (tag_type == typename_type && BASELINK_P (decl))
21143 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
21144 type = error_mark_node;
21146 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
21147 in effect, then we must assume that, upon instantiation, the
21148 template will correspond to a class. */
21149 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21150 && tag_type == typename_type)
21151 type = make_typename_type (parser->scope, decl,
21152 typename_type,
21153 /*complain=*/tf_error);
21154 /* If the `typename' keyword is in effect and DECL is not a type
21155 decl, then type is non existent. */
21156 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
21158 else if (TREE_CODE (decl) == TYPE_DECL)
21160 type = check_elaborated_type_specifier (tag_type, decl,
21161 /*allow_template_p=*/true);
21163 /* If the next token is a semicolon, this must be a specialization,
21164 instantiation, or friend declaration. Check the scope while we
21165 still know whether or not we had a nested-name-specifier. */
21166 if (type != error_mark_node
21167 && !nested_name_specifier && !is_friend
21168 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21169 check_unqualified_spec_or_inst (type, token->location);
21171 else if (decl == error_mark_node)
21172 type = error_mark_node;
21175 if (!type)
21177 token = cp_lexer_peek_token (parser->lexer);
21178 identifier = cp_parser_identifier (parser);
21180 if (identifier == error_mark_node)
21182 parser->scope = NULL_TREE;
21183 return error_mark_node;
21186 /* For a `typename', we needn't call xref_tag. */
21187 if (tag_type == typename_type
21188 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21189 return cp_parser_make_typename_type (parser, identifier,
21190 token->location);
21192 /* Template parameter lists apply only if we are not within a
21193 function parameter list. */
21194 bool template_parm_lists_apply
21195 = parser->num_template_parameter_lists;
21196 if (template_parm_lists_apply)
21197 for (cp_binding_level *s = current_binding_level;
21198 s && s->kind != sk_template_parms;
21199 s = s->level_chain)
21200 if (s->kind == sk_function_parms)
21201 template_parm_lists_apply = false;
21203 /* Look up a qualified name in the usual way. */
21204 if (parser->scope)
21206 tree decl;
21207 tree ambiguous_decls;
21209 decl = cp_parser_lookup_name (parser, identifier,
21210 tag_type,
21211 /*is_template=*/false,
21212 /*is_namespace=*/false,
21213 /*check_dependency=*/true,
21214 &ambiguous_decls,
21215 token->location);
21217 /* If the lookup was ambiguous, an error will already have been
21218 issued. */
21219 if (ambiguous_decls)
21220 return error_mark_node;
21222 /* If we are parsing friend declaration, DECL may be a
21223 TEMPLATE_DECL tree node here. However, we need to check
21224 whether this TEMPLATE_DECL results in valid code. Consider
21225 the following example:
21227 namespace N {
21228 template <class T> class C {};
21230 class X {
21231 template <class T> friend class N::C; // #1, valid code
21233 template <class T> class Y {
21234 friend class N::C; // #2, invalid code
21237 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
21238 name lookup of `N::C'. We see that friend declaration must
21239 be template for the code to be valid. Note that
21240 processing_template_decl does not work here since it is
21241 always 1 for the above two cases. */
21243 decl = (cp_parser_maybe_treat_template_as_class
21244 (decl, /*tag_name_p=*/is_friend
21245 && template_parm_lists_apply));
21247 if (TREE_CODE (decl) != TYPE_DECL)
21249 cp_parser_diagnose_invalid_type_name (parser,
21250 identifier,
21251 token->location);
21252 return error_mark_node;
21255 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21257 bool allow_template = (template_parm_lists_apply
21258 || DECL_SELF_REFERENCE_P (decl));
21259 type = check_elaborated_type_specifier (tag_type, decl,
21260 allow_template);
21262 if (type == error_mark_node)
21263 return error_mark_node;
21266 /* Forward declarations of nested types, such as
21268 class C1::C2;
21269 class C1::C2::C3;
21271 are invalid unless all components preceding the final '::'
21272 are complete. If all enclosing types are complete, these
21273 declarations become merely pointless.
21275 Invalid forward declarations of nested types are errors
21276 caught elsewhere in parsing. Those that are pointless arrive
21277 here. */
21279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21280 && !is_friend && is_declaration
21281 && !processing_explicit_instantiation)
21282 warning (0, "declaration %qD does not declare anything", decl);
21284 type = TREE_TYPE (decl);
21286 else
21288 /* An elaborated-type-specifier sometimes introduces a new type and
21289 sometimes names an existing type. Normally, the rule is that it
21290 introduces a new type only if there is not an existing type of
21291 the same name already in scope. For example, given:
21293 struct S {};
21294 void f() { struct S s; }
21296 the `struct S' in the body of `f' is the same `struct S' as in
21297 the global scope; the existing definition is used. However, if
21298 there were no global declaration, this would introduce a new
21299 local class named `S'.
21301 An exception to this rule applies to the following code:
21303 namespace N { struct S; }
21305 Here, the elaborated-type-specifier names a new type
21306 unconditionally; even if there is already an `S' in the
21307 containing scope this declaration names a new type.
21308 This exception only applies if the elaborated-type-specifier
21309 forms the complete declaration:
21311 [class.name]
21313 A declaration consisting solely of `class-key identifier ;' is
21314 either a redeclaration of the name in the current scope or a
21315 forward declaration of the identifier as a class name. It
21316 introduces the name into the current scope.
21318 We are in this situation precisely when the next token is a `;'.
21320 An exception to the exception is that a `friend' declaration does
21321 *not* name a new type; i.e., given:
21323 struct S { friend struct T; };
21325 `T' is not a new type in the scope of `S'.
21327 Also, `new struct S' or `sizeof (struct S)' never results in the
21328 definition of a new type; a new type can only be declared in a
21329 declaration context. */
21331 TAG_how how;
21333 if (is_friend)
21334 /* Friends have special name lookup rules. */
21335 how = TAG_how::HIDDEN_FRIEND;
21336 else if (is_declaration
21337 && cp_lexer_next_token_is (parser->lexer,
21338 CPP_SEMICOLON))
21339 /* This is a `class-key identifier ;' */
21340 how = TAG_how::CURRENT_ONLY;
21341 else
21342 how = TAG_how::GLOBAL;
21344 bool template_p =
21345 (template_parm_lists_apply
21346 && (cp_parser_next_token_starts_class_definition_p (parser)
21347 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21348 /* An unqualified name was used to reference this type, so
21349 there were no qualifying templates. */
21350 if (template_parm_lists_apply
21351 && !cp_parser_check_template_parameters (parser,
21352 /*num_templates=*/0,
21353 /*template_id*/false,
21354 token->location,
21355 /*declarator=*/NULL))
21356 return error_mark_node;
21358 type = xref_tag (tag_type, identifier, how, template_p);
21362 if (type == error_mark_node)
21363 return error_mark_node;
21365 /* Allow attributes on forward declarations of classes. */
21366 if (attributes)
21368 if (TREE_CODE (type) == TYPENAME_TYPE)
21369 warning (OPT_Wattributes,
21370 "attributes ignored on uninstantiated type");
21371 else if (tag_type != enum_type
21372 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21373 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21374 && ! processing_explicit_instantiation)
21375 warning (OPT_Wattributes,
21376 "attributes ignored on template instantiation");
21377 else if (is_friend && cxx11_attribute_p (attributes))
21379 if (warning (OPT_Wattributes, "attribute ignored"))
21380 inform (input_location, "an attribute that appertains to a friend "
21381 "declaration that is not a definition is ignored");
21383 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21384 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21385 else
21386 warning (OPT_Wattributes,
21387 "attributes ignored on elaborated-type-specifier that is "
21388 "not a forward declaration");
21391 if (tag_type == enum_type)
21392 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21393 else
21395 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21396 for alias definition. */
21397 bool decl_class = (is_declaration
21398 && cp_parser_declares_only_class_p (parser));
21399 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21400 decl_class);
21402 /* Indicate whether this class was declared as a `class' or as a
21403 `struct'. */
21404 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21405 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21408 /* A "<" cannot follow an elaborated type specifier. If that
21409 happens, the user was probably trying to form a template-id. */
21410 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21411 token->location);
21413 return type;
21416 /* Parse an enum-specifier.
21418 enum-specifier:
21419 enum-head { enumerator-list [opt] }
21420 enum-head { enumerator-list , } [C++0x]
21422 enum-head:
21423 enum-key identifier [opt] enum-base [opt]
21424 enum-key nested-name-specifier identifier enum-base [opt]
21426 enum-key:
21427 enum
21428 enum class [C++0x]
21429 enum struct [C++0x]
21431 enum-base: [C++0x]
21432 : type-specifier-seq
21434 opaque-enum-specifier:
21435 enum-key identifier enum-base [opt] ;
21437 GNU Extensions:
21438 enum-key attributes[opt] identifier [opt] enum-base [opt]
21439 { enumerator-list [opt] }attributes[opt]
21440 enum-key attributes[opt] identifier [opt] enum-base [opt]
21441 { enumerator-list, }attributes[opt] [C++0x]
21443 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21444 if the token stream isn't an enum-specifier after all. */
21446 static tree
21447 cp_parser_enum_specifier (cp_parser* parser)
21449 tree identifier;
21450 tree type = NULL_TREE;
21451 tree prev_scope;
21452 tree nested_name_specifier = NULL_TREE;
21453 tree attributes;
21454 bool scoped_enum_p = false;
21455 bool has_underlying_type = false;
21456 bool nested_being_defined = false;
21457 bool new_value_list = false;
21458 bool is_new_type = false;
21459 bool is_unnamed = false;
21460 tree underlying_type = NULL_TREE;
21461 cp_token *type_start_token = NULL;
21462 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21464 /* Parse tentatively so that we can back up if we don't find a
21465 enum-specifier. */
21466 cp_parser_parse_tentatively (parser);
21468 /* Caller guarantees that the current token is 'enum', an identifier
21469 possibly follows, and the token after that is an opening brace.
21470 If we don't have an identifier, fabricate an anonymous name for
21471 the enumeration being defined. */
21472 cp_lexer_consume_token (parser->lexer);
21474 /* Parse the "class" or "struct", which indicates a scoped
21475 enumeration type in C++0x. */
21476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21477 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21479 if (cxx_dialect < cxx11)
21480 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21482 /* Consume the `struct' or `class' token. */
21483 cp_lexer_consume_token (parser->lexer);
21485 scoped_enum_p = true;
21488 attributes = cp_parser_attributes_opt (parser);
21490 /* Clear the qualification. */
21491 parser->scope = NULL_TREE;
21492 parser->qualifying_scope = NULL_TREE;
21493 parser->object_scope = NULL_TREE;
21495 /* Figure out in what scope the declaration is being placed. */
21496 prev_scope = current_scope ();
21498 type_start_token = cp_lexer_peek_token (parser->lexer);
21500 push_deferring_access_checks (dk_no_check);
21501 nested_name_specifier
21502 = cp_parser_nested_name_specifier_opt (parser,
21503 /*typename_keyword_p=*/true,
21504 /*check_dependency_p=*/false,
21505 /*type_p=*/false,
21506 /*is_declaration=*/false);
21508 if (nested_name_specifier)
21510 tree name;
21512 identifier = cp_parser_identifier (parser);
21513 name = cp_parser_lookup_name (parser, identifier,
21514 enum_type,
21515 /*is_template=*/false,
21516 /*is_namespace=*/false,
21517 /*check_dependency=*/true,
21518 /*ambiguous_decls=*/NULL,
21519 input_location);
21520 if (name && name != error_mark_node)
21522 type = TREE_TYPE (name);
21523 if (TREE_CODE (type) == TYPENAME_TYPE)
21525 /* Are template enums allowed in ISO? */
21526 if (template_parm_scope_p ())
21527 pedwarn (type_start_token->location, OPT_Wpedantic,
21528 "%qD is an enumeration template", name);
21529 /* ignore a typename reference, for it will be solved by name
21530 in start_enum. */
21531 type = NULL_TREE;
21534 else if (nested_name_specifier == error_mark_node)
21535 /* We already issued an error. */;
21536 else
21538 error_at (type_start_token->location,
21539 "%qD does not name an enumeration in %qT",
21540 identifier, nested_name_specifier);
21541 nested_name_specifier = error_mark_node;
21544 else
21546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21547 identifier = cp_parser_identifier (parser);
21548 else
21550 identifier = make_anon_name ();
21551 is_unnamed = true;
21552 if (scoped_enum_p)
21553 error_at (type_start_token->location,
21554 "unnamed scoped enum is not allowed");
21557 pop_deferring_access_checks ();
21559 /* Check for the `:' that denotes a specified underlying type in C++0x.
21560 Note that a ':' could also indicate a bitfield width, however. */
21561 location_t colon_loc = UNKNOWN_LOCATION;
21562 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21564 cp_decl_specifier_seq type_specifiers;
21566 /* Consume the `:'. */
21567 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21568 cp_lexer_consume_token (parser->lexer);
21570 auto tdf
21571 = make_temp_override (parser->type_definition_forbidden_message,
21572 G_("types may not be defined in enum-base"));
21574 /* Parse the type-specifier-seq. */
21575 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21576 /*is_declaration=*/false,
21577 /*is_trailing_return=*/false,
21578 &type_specifiers);
21580 /* At this point this is surely not elaborated type specifier. */
21581 if (!cp_parser_parse_definitely (parser))
21582 return NULL_TREE;
21584 if (cxx_dialect < cxx11)
21585 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21587 has_underlying_type = true;
21589 /* If that didn't work, stop. */
21590 if (type_specifiers.type != error_mark_node)
21592 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21593 /*initialized=*/0, NULL);
21594 if (underlying_type == error_mark_node
21595 || check_for_bare_parameter_packs (underlying_type))
21596 underlying_type = NULL_TREE;
21600 /* Look for the `{' but don't consume it yet. */
21601 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21603 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21605 if (has_underlying_type)
21606 cp_parser_commit_to_tentative_parse (parser);
21607 cp_parser_error (parser, "expected %<{%>");
21608 if (has_underlying_type)
21609 return error_mark_node;
21611 /* An opaque-enum-specifier must have a ';' here. */
21612 if ((scoped_enum_p || underlying_type)
21613 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21615 if (has_underlying_type)
21616 pedwarn (colon_loc,
21617 OPT_Welaborated_enum_base,
21618 "declaration of enumeration with "
21619 "fixed underlying type and no enumerator list is "
21620 "only permitted as a standalone declaration");
21621 else
21622 cp_parser_error (parser, "expected %<;%> or %<{%>");
21626 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21627 return NULL_TREE;
21629 if (nested_name_specifier)
21631 if (CLASS_TYPE_P (nested_name_specifier))
21633 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21634 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21635 push_scope (nested_name_specifier);
21637 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21638 push_nested_namespace (nested_name_specifier);
21641 /* Issue an error message if type-definitions are forbidden here. */
21642 if (!cp_parser_check_type_definition (parser))
21643 type = error_mark_node;
21644 else
21645 /* Create the new type. We do this before consuming the opening
21646 brace so the enum will be recorded as being on the line of its
21647 tag (or the 'enum' keyword, if there is no tag). */
21648 type = start_enum (identifier, type, underlying_type,
21649 attributes, scoped_enum_p, &is_new_type);
21651 /* If the next token is not '{' it is an opaque-enum-specifier or an
21652 elaborated-type-specifier. */
21653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21655 auto_timevar tv (TV_PARSE_ENUM);
21657 if (nested_name_specifier
21658 && nested_name_specifier != error_mark_node)
21660 /* The following catches invalid code such as:
21661 enum class S<int>::E { A, B, C }; */
21662 if (!processing_specialization
21663 && CLASS_TYPE_P (nested_name_specifier)
21664 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21665 error_at (type_start_token->location, "cannot add an enumerator "
21666 "list to a template instantiation");
21668 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21670 error_at (type_start_token->location,
21671 "%<%T::%E%> has not been declared",
21672 TYPE_CONTEXT (nested_name_specifier),
21673 nested_name_specifier);
21674 type = error_mark_node;
21676 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21677 && !CLASS_TYPE_P (nested_name_specifier))
21679 error_at (type_start_token->location, "nested name specifier "
21680 "%qT for enum declaration does not name a class "
21681 "or namespace", nested_name_specifier);
21682 type = error_mark_node;
21684 /* If that scope does not contain the scope in which the
21685 class was originally declared, the program is invalid. */
21686 else if (prev_scope && !is_ancestor (prev_scope,
21687 nested_name_specifier))
21689 if (at_namespace_scope_p ())
21690 error_at (type_start_token->location,
21691 "declaration of %qD in namespace %qD which does not "
21692 "enclose %qD",
21693 type, prev_scope, nested_name_specifier);
21694 else
21695 error_at (type_start_token->location,
21696 "declaration of %qD in %qD which does not "
21697 "enclose %qD",
21698 type, prev_scope, nested_name_specifier);
21699 type = error_mark_node;
21701 /* If that scope is the scope where the declaration is being placed
21702 the program is invalid. */
21703 else if (CLASS_TYPE_P (nested_name_specifier)
21704 && CLASS_TYPE_P (prev_scope)
21705 && same_type_p (nested_name_specifier, prev_scope))
21707 permerror (type_start_token->location,
21708 "extra qualification not allowed");
21709 nested_name_specifier = NULL_TREE;
21713 if (scoped_enum_p)
21714 begin_scope (sk_scoped_enum, type);
21716 /* Consume the opening brace. */
21717 matching_braces braces;
21718 braces.consume_open (parser);
21720 if (type == error_mark_node)
21721 ; /* Nothing to add */
21722 else if (OPAQUE_ENUM_P (type)
21723 || (cxx_dialect > cxx98 && processing_specialization))
21725 new_value_list = true;
21726 SET_OPAQUE_ENUM_P (type, false);
21727 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21729 else
21731 error_at (type_start_token->location,
21732 "multiple definition of %q#T", type);
21733 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21734 "previous definition here");
21735 type = error_mark_node;
21738 if (type == error_mark_node)
21739 cp_parser_skip_to_end_of_block_or_statement (parser);
21740 /* If the next token is not '}', then there are some enumerators. */
21741 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21743 if (is_unnamed && !scoped_enum_p
21744 /* Don't warn for enum {} a; here. */
21745 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21746 pedwarn (type_start_token->location, OPT_Wpedantic,
21747 "ISO C++ forbids empty unnamed enum");
21749 else
21751 /* We've seen a '{' so we know we're in an enum-specifier.
21752 Commit to any tentative parse to get syntax errors. */
21753 cp_parser_commit_to_tentative_parse (parser);
21754 cp_parser_enumerator_list (parser, type);
21757 /* Consume the final '}'. */
21758 braces.require_close (parser);
21760 if (scoped_enum_p)
21761 finish_scope ();
21763 else
21765 /* If a ';' follows, then it is an opaque-enum-specifier
21766 and additional restrictions apply. */
21767 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21769 if (is_unnamed)
21770 error_at (type_start_token->location,
21771 "opaque-enum-specifier without name");
21772 else if (nested_name_specifier)
21773 error_at (type_start_token->location,
21774 "opaque-enum-specifier must use a simple identifier");
21778 /* Look for trailing attributes to apply to this enumeration, and
21779 apply them if appropriate. */
21780 if (cp_parser_allow_gnu_extensions_p (parser))
21782 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21783 cplus_decl_attributes (&type,
21784 trailing_attr,
21785 (int) ATTR_FLAG_TYPE_IN_PLACE);
21788 /* Finish up the enumeration. */
21789 if (type != error_mark_node)
21791 if (new_value_list)
21792 finish_enum_value_list (type);
21793 if (is_new_type)
21794 finish_enum (type);
21797 if (nested_name_specifier)
21799 if (CLASS_TYPE_P (nested_name_specifier))
21801 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21802 pop_scope (nested_name_specifier);
21804 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21805 pop_nested_namespace (nested_name_specifier);
21807 return type;
21810 /* Parse an enumerator-list. The enumerators all have the indicated
21811 TYPE.
21813 enumerator-list:
21814 enumerator-definition
21815 enumerator-list , enumerator-definition */
21817 static void
21818 cp_parser_enumerator_list (cp_parser* parser, tree type)
21820 while (true)
21822 /* Parse an enumerator-definition. */
21823 cp_parser_enumerator_definition (parser, type);
21825 /* If the next token is not a ',', we've reached the end of
21826 the list. */
21827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21828 break;
21829 /* Otherwise, consume the `,' and keep going. */
21830 cp_lexer_consume_token (parser->lexer);
21831 /* If the next token is a `}', there is a trailing comma. */
21832 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21834 if (cxx_dialect < cxx11)
21835 pedwarn (input_location, OPT_Wpedantic,
21836 "comma at end of enumerator list");
21837 break;
21842 /* Parse an enumerator-definition. The enumerator has the indicated
21843 TYPE.
21845 enumerator-definition:
21846 enumerator
21847 enumerator = constant-expression
21849 enumerator:
21850 identifier
21852 GNU Extensions:
21854 enumerator-definition:
21855 enumerator attributes [opt]
21856 enumerator attributes [opt] = constant-expression */
21858 static void
21859 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21861 tree identifier;
21862 tree value;
21863 location_t loc;
21865 /* Save the input location because we are interested in the location
21866 of the identifier and not the location of the explicit value. */
21867 loc = cp_lexer_peek_token (parser->lexer)->location;
21869 /* Look for the identifier. */
21870 identifier = cp_parser_identifier (parser);
21871 if (identifier == error_mark_node)
21872 return;
21874 /* Parse any specified attributes. */
21875 tree attrs = cp_parser_attributes_opt (parser);
21877 /* If the next token is an '=', then there is an explicit value. */
21878 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21880 /* Consume the `=' token. */
21881 cp_lexer_consume_token (parser->lexer);
21882 /* Parse the value. */
21883 value = cp_parser_constant_expression (parser);
21885 else
21886 value = NULL_TREE;
21888 /* If we are processing a template, make sure the initializer of the
21889 enumerator doesn't contain any bare template parameter pack. */
21890 if (current_lambda_expr ())
21892 /* In a lambda it should work, but doesn't currently. */
21893 if (uses_parameter_packs (value))
21895 sorry ("unexpanded parameter pack in enumerator in lambda");
21896 value = error_mark_node;
21899 else if (check_for_bare_parameter_packs (value))
21900 value = error_mark_node;
21902 /* Create the enumerator. */
21903 build_enumerator (identifier, value, type, attrs, loc);
21906 /* Parse a namespace-name.
21908 namespace-name:
21909 original-namespace-name
21910 namespace-alias
21912 Returns the NAMESPACE_DECL for the namespace. */
21914 static tree
21915 cp_parser_namespace_name (cp_parser* parser)
21917 tree identifier;
21918 tree namespace_decl;
21920 cp_token *token = cp_lexer_peek_token (parser->lexer);
21922 /* Get the name of the namespace. */
21923 identifier = cp_parser_identifier (parser);
21924 if (identifier == error_mark_node)
21925 return error_mark_node;
21927 /* Look up the identifier in the currently active scope. Look only
21928 for namespaces, due to:
21930 [basic.lookup.udir]
21932 When looking up a namespace-name in a using-directive or alias
21933 definition, only namespace names are considered.
21935 And:
21937 [basic.lookup.qual]
21939 During the lookup of a name preceding the :: scope resolution
21940 operator, object, function, and enumerator names are ignored.
21942 (Note that cp_parser_qualifying_entity only calls this
21943 function if the token after the name is the scope resolution
21944 operator.) */
21945 namespace_decl = cp_parser_lookup_name (parser, identifier,
21946 none_type,
21947 /*is_template=*/false,
21948 /*is_namespace=*/true,
21949 /*check_dependency=*/true,
21950 /*ambiguous_decls=*/NULL,
21951 token->location);
21952 /* If it's not a namespace, issue an error. */
21953 if (namespace_decl == error_mark_node
21954 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21956 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21958 auto_diagnostic_group d;
21959 name_hint hint;
21960 if (namespace_decl == error_mark_node
21961 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21962 hint = suggest_alternative_in_explicit_scope (token->location,
21963 identifier,
21964 parser->scope);
21965 if (const char *suggestion = hint.suggestion ())
21967 gcc_rich_location richloc (token->location);
21968 richloc.add_fixit_replace (suggestion);
21969 error_at (&richloc,
21970 "%qD is not a namespace-name; did you mean %qs?",
21971 identifier, suggestion);
21973 else
21974 error_at (token->location, "%qD is not a namespace-name",
21975 identifier);
21977 else
21978 cp_parser_error (parser, "expected namespace-name");
21979 namespace_decl = error_mark_node;
21982 return namespace_decl;
21985 /* Parse a namespace-definition.
21987 namespace-definition:
21988 named-namespace-definition
21989 unnamed-namespace-definition
21991 named-namespace-definition:
21992 original-namespace-definition
21993 extension-namespace-definition
21995 original-namespace-definition:
21996 namespace identifier { namespace-body }
21998 extension-namespace-definition:
21999 namespace original-namespace-name { namespace-body }
22001 unnamed-namespace-definition:
22002 namespace { namespace-body } */
22004 static void
22005 cp_parser_namespace_definition (cp_parser* parser)
22007 tree identifier;
22008 int nested_definition_count = 0;
22010 cp_ensure_no_omp_declare_simd (parser);
22011 cp_ensure_no_oacc_routine (parser);
22013 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
22014 const bool topmost_inline_p = is_inline;
22016 if (is_inline)
22018 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
22019 cp_lexer_consume_token (parser->lexer);
22022 /* Look for the `namespace' keyword. */
22023 cp_token* token
22024 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22026 /* Parse any specified attributes before the identifier. */
22027 tree attribs = cp_parser_attributes_opt (parser);
22029 for (;;)
22031 identifier = NULL_TREE;
22033 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
22034 RID_INLINE);
22035 if (nested_inline_p && nested_definition_count != 0)
22037 if (pedantic && cxx_dialect < cxx20)
22038 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
22039 OPT_Wc__20_extensions, "nested inline namespace "
22040 "definitions only available with %<-std=c++20%> or "
22041 "%<-std=gnu++20%>");
22042 cp_lexer_consume_token (parser->lexer);
22045 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22047 identifier = cp_parser_identifier (parser);
22049 if (cp_next_tokens_can_be_std_attribute_p (parser))
22050 pedwarn (input_location, OPT_Wpedantic,
22051 "standard attributes on namespaces must precede "
22052 "the namespace name");
22054 /* Parse any attributes specified after the identifier. */
22055 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
22058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22060 /* Don't forget that the innermost namespace might have been
22061 marked as inline. Use |= because we cannot overwrite
22062 IS_INLINE in case the outermost namespace is inline, but
22063 there are no nested inlines. */
22064 is_inline |= nested_inline_p;
22065 break;
22068 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
22069 pedwarn (input_location, OPT_Wc__17_extensions,
22070 "nested namespace definitions only available with "
22071 "%<-std=c++17%> or %<-std=gnu++17%>");
22073 /* Nested namespace names can create new namespaces (unlike
22074 other qualified-ids). */
22075 if (int count = (identifier
22076 ? push_namespace (identifier, nested_inline_p)
22077 : 0))
22078 nested_definition_count += count;
22079 else
22080 cp_parser_error (parser, "nested namespace name required");
22081 cp_lexer_consume_token (parser->lexer);
22084 if (nested_definition_count && !identifier)
22085 cp_parser_error (parser, "namespace name required");
22087 if (nested_definition_count && attribs)
22088 error_at (token->location,
22089 "a nested namespace definition cannot have attributes");
22090 if (nested_definition_count && topmost_inline_p)
22091 error_at (token->location,
22092 "a nested namespace definition cannot be inline");
22094 /* Start the namespace. */
22095 nested_definition_count += push_namespace (identifier, is_inline);
22097 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
22099 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
22101 /* Look for the `{' to validate starting the namespace. */
22102 matching_braces braces;
22103 if (braces.require_open (parser))
22105 /* Parse the body of the namespace. */
22106 cp_parser_namespace_body (parser);
22108 /* Look for the final `}'. */
22109 braces.require_close (parser);
22112 if (has_visibility)
22113 pop_visibility (1);
22115 /* Pop the nested namespace definitions. */
22116 while (nested_definition_count--)
22117 pop_namespace ();
22120 /* Parse a namespace-body.
22122 namespace-body:
22123 declaration-seq [opt] */
22125 static void
22126 cp_parser_namespace_body (cp_parser* parser)
22128 cp_parser_declaration_seq_opt (parser);
22131 /* Parse a namespace-alias-definition.
22133 namespace-alias-definition:
22134 namespace identifier = qualified-namespace-specifier ; */
22136 static void
22137 cp_parser_namespace_alias_definition (cp_parser* parser)
22139 tree identifier;
22140 tree namespace_specifier;
22142 cp_token *token = cp_lexer_peek_token (parser->lexer);
22144 /* Look for the `namespace' keyword. */
22145 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22146 /* Look for the identifier. */
22147 identifier = cp_parser_identifier (parser);
22148 if (identifier == error_mark_node)
22149 return;
22150 /* Look for the `=' token. */
22151 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
22152 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22154 error_at (token->location, "%<namespace%> definition is not allowed here");
22155 /* Skip the definition. */
22156 cp_lexer_consume_token (parser->lexer);
22157 if (cp_parser_skip_to_closing_brace (parser))
22158 cp_lexer_consume_token (parser->lexer);
22159 return;
22161 cp_parser_require (parser, CPP_EQ, RT_EQ);
22162 /* Look for the qualified-namespace-specifier. */
22163 namespace_specifier
22164 = cp_parser_qualified_namespace_specifier (parser);
22165 cp_warn_deprecated_use_scopes (namespace_specifier);
22166 /* Look for the `;' token. */
22167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22169 /* Register the alias in the symbol table. */
22170 do_namespace_alias (identifier, namespace_specifier);
22173 /* Parse a qualified-namespace-specifier.
22175 qualified-namespace-specifier:
22176 :: [opt] nested-name-specifier [opt] namespace-name
22178 Returns a NAMESPACE_DECL corresponding to the specified
22179 namespace. */
22181 static tree
22182 cp_parser_qualified_namespace_specifier (cp_parser* parser)
22184 /* Look for the optional `::'. */
22185 cp_parser_global_scope_opt (parser,
22186 /*current_scope_valid_p=*/false);
22188 /* Look for the optional nested-name-specifier. */
22189 cp_parser_nested_name_specifier_opt (parser,
22190 /*typename_keyword_p=*/false,
22191 /*check_dependency_p=*/true,
22192 /*type_p=*/false,
22193 /*is_declaration=*/true);
22195 return cp_parser_namespace_name (parser);
22198 /* Subroutine of cp_parser_using_declaration. */
22200 static tree
22201 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
22203 tree decl = NULL_TREE;
22204 if (at_class_scope_p ())
22206 /* Create the USING_DECL. */
22207 decl = do_class_using_decl (qscope, identifier);
22209 if (check_for_bare_parameter_packs (decl))
22210 return error_mark_node;
22212 if (decl && typename_p)
22213 USING_DECL_TYPENAME_P (decl) = 1;
22215 /* Add it to the list of members in this class. */
22216 finish_member_declaration (decl);
22218 else
22219 finish_nonmember_using_decl (qscope, identifier);
22220 return decl;
22223 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
22224 access declaration.
22226 using-declaration:
22227 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
22228 using :: unqualified-id ;
22230 access-declaration:
22231 qualified-id ;
22235 static bool
22236 cp_parser_using_declaration (cp_parser* parser,
22237 bool access_declaration_p)
22239 cp_token *token;
22240 bool typename_p = false;
22241 bool global_scope_p;
22242 tree identifier;
22243 tree qscope;
22244 int oldcount = errorcount;
22245 cp_token *diag_token = NULL;
22247 if (access_declaration_p)
22249 diag_token = cp_lexer_peek_token (parser->lexer);
22250 cp_parser_parse_tentatively (parser);
22252 else
22254 /* Look for the `using' keyword. */
22255 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22257 again:
22258 /* Peek at the next token. */
22259 token = cp_lexer_peek_token (parser->lexer);
22260 /* See if it's `typename'. */
22261 if (token->keyword == RID_TYPENAME)
22263 /* Remember that we've seen it. */
22264 typename_p = true;
22265 /* Consume the `typename' token. */
22266 cp_lexer_consume_token (parser->lexer);
22270 /* Look for the optional global scope qualification. */
22271 global_scope_p
22272 = (cp_parser_global_scope_opt (parser,
22273 /*current_scope_valid_p=*/false)
22274 != NULL_TREE);
22276 /* If we saw `typename', or didn't see `::', then there must be a
22277 nested-name-specifier present. */
22278 if (typename_p || !global_scope_p)
22280 qscope = cp_parser_nested_name_specifier (parser, typename_p,
22281 /*check_dependency_p=*/true,
22282 /*type_p=*/false,
22283 /*is_declaration=*/true);
22284 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
22286 cp_parser_skip_to_end_of_block_or_statement (parser);
22287 return false;
22290 /* Otherwise, we could be in either of the two productions. In that
22291 case, treat the nested-name-specifier as optional. */
22292 else
22293 qscope = cp_parser_nested_name_specifier_opt (parser,
22294 /*typename_keyword_p=*/false,
22295 /*check_dependency_p=*/true,
22296 /*type_p=*/false,
22297 /*is_declaration=*/true);
22298 if (!qscope)
22299 qscope = global_namespace;
22301 cp_warn_deprecated_use_scopes (qscope);
22303 if (access_declaration_p
22304 && !MAYBE_CLASS_TYPE_P (qscope)
22305 && TREE_CODE (qscope) != ENUMERAL_TYPE)
22306 /* If the qualifying scope of an access-declaration isn't a class
22307 or enumeration type then it can't be valid. */
22308 cp_parser_simulate_error (parser);
22310 if (access_declaration_p && cp_parser_error_occurred (parser))
22311 /* Something has already gone wrong; there's no need to parse
22312 further. Since an error has occurred, the return value of
22313 cp_parser_parse_definitely will be false, as required. */
22314 return cp_parser_parse_definitely (parser);
22316 token = cp_lexer_peek_token (parser->lexer);
22317 /* Parse the unqualified-id. */
22318 identifier = cp_parser_unqualified_id (parser,
22319 /*template_keyword_p=*/false,
22320 /*check_dependency_p=*/true,
22321 /*declarator_p=*/true,
22322 /*optional_p=*/false);
22324 if (access_declaration_p)
22326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22327 cp_parser_simulate_error (parser);
22328 if (!cp_parser_parse_definitely (parser))
22329 return false;
22331 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22333 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22334 if (cxx_dialect < cxx17)
22335 pedwarn (ell->location, OPT_Wc__17_extensions,
22336 "pack expansion in using-declaration only available "
22337 "with %<-std=c++17%> or %<-std=gnu++17%>");
22339 /* A parameter pack can appear in the qualifying scope, and/or in the
22340 terminal name (if naming a conversion function). Logically they're
22341 part of a single pack expansion of the overall USING_DECL, but we
22342 express them as separate pack expansions within the USING_DECL since
22343 we can't create a pack expansion over a USING_DECL. */
22344 bool saw_parm_pack = false;
22345 if (uses_parameter_packs (qscope))
22347 qscope = make_pack_expansion (qscope);
22348 saw_parm_pack = true;
22350 if (identifier_p (identifier)
22351 && IDENTIFIER_CONV_OP_P (identifier)
22352 && uses_parameter_packs (TREE_TYPE (identifier)))
22354 identifier = make_conv_op_name (make_pack_expansion
22355 (TREE_TYPE (identifier)));
22356 saw_parm_pack = true;
22358 if (!saw_parm_pack)
22360 /* Issue an error in terms using a SCOPE_REF that includes both
22361 components. */
22362 tree name
22363 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22364 make_pack_expansion (name);
22365 gcc_assert (seen_error ());
22366 qscope = identifier = error_mark_node;
22370 /* The function we call to handle a using-declaration is different
22371 depending on what scope we are in. */
22372 if (qscope == error_mark_node || identifier == error_mark_node)
22374 else if (!identifier_p (identifier)
22375 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22376 /* [namespace.udecl]
22378 A using declaration shall not name a template-id. */
22379 error_at (token->location,
22380 "a template-id may not appear in a using-declaration");
22381 else
22383 tree decl = finish_using_decl (qscope, identifier, typename_p);
22385 if (decl == error_mark_node)
22387 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22388 return false;
22392 if (!access_declaration_p
22393 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22395 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22396 if (cxx_dialect < cxx17)
22397 pedwarn (comma->location, OPT_Wc__17_extensions,
22398 "comma-separated list in using-declaration only available "
22399 "with %<-std=c++17%> or %<-std=gnu++17%>");
22400 goto again;
22403 /* Look for the final `;'. */
22404 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22406 if (access_declaration_p && errorcount == oldcount)
22407 warning_at (diag_token->location, OPT_Wdeprecated,
22408 "access declarations are deprecated "
22409 "in favour of using-declarations; "
22410 "suggestion: add the %<using%> keyword");
22412 return true;
22415 /* C++20 using enum declaration.
22417 using-enum-declaration :
22418 using elaborated-enum-specifier ; */
22420 static void
22421 cp_parser_using_enum (cp_parser *parser)
22423 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22425 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22426 breaks one of the motivating examples in using-enum-5.C.
22427 cp_parser_simple_type_specifier seems to be closer to what we actually
22428 want, though that hasn't been properly specified yet. */
22430 /* Consume 'enum'. */
22431 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22432 cp_lexer_consume_token (parser->lexer);
22434 cp_token *start = cp_lexer_peek_token (parser->lexer);
22436 tree type = (cp_parser_simple_type_specifier
22437 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22439 cp_token *end = cp_lexer_previous_token (parser->lexer);
22441 if (type == error_mark_node
22442 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22444 cp_parser_skip_to_end_of_block_or_statement (parser);
22445 return;
22447 if (TREE_CODE (type) == TYPE_DECL)
22448 type = TREE_TYPE (type);
22450 /* The elaborated-enum-specifier shall not name a dependent type and the type
22451 shall have a reachable enum-specifier. */
22452 const char *msg = nullptr;
22453 if (cxx_dialect < cxx20)
22454 msg = G_("%<using enum%> "
22455 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22456 else if (dependent_type_p (type))
22457 msg = G_("%<using enum%> of dependent type %qT");
22458 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22459 msg = G_("%<using enum%> of non-enumeration type %q#T");
22460 else if (!COMPLETE_TYPE_P (type))
22461 msg = G_("%<using enum%> of incomplete type %qT");
22462 else if (OPAQUE_ENUM_P (type))
22463 msg = G_("%<using enum%> of %qT before its enum-specifier");
22464 if (msg)
22466 location_t loc = make_location (start, start, end);
22467 auto_diagnostic_group g;
22468 error_at (loc, msg, type);
22469 loc = location_of (type);
22470 if (cxx_dialect < cxx20 || loc == input_location)
22472 else if (OPAQUE_ENUM_P (type))
22473 inform (loc, "opaque-enum-declaration here");
22474 else
22475 inform (loc, "declared here");
22478 /* A using-enum-declaration introduces the enumerator names of the named
22479 enumeration as if by a using-declaration for each enumerator. */
22480 if (TREE_CODE (type) == ENUMERAL_TYPE)
22481 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22482 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22485 /* Parse an alias-declaration.
22487 alias-declaration:
22488 using identifier attribute-specifier-seq [opt] = type-id */
22490 static tree
22491 cp_parser_alias_declaration (cp_parser* parser)
22493 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22494 location_t id_location, type_location;
22495 cp_declarator *declarator;
22496 cp_decl_specifier_seq decl_specs;
22497 bool member_p;
22498 const char *saved_message = NULL;
22500 /* Look for the `using' keyword. */
22501 cp_token *using_token
22502 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22503 if (using_token == NULL)
22504 return error_mark_node;
22506 id_location = cp_lexer_peek_token (parser->lexer)->location;
22507 id = cp_parser_identifier (parser);
22508 if (id == error_mark_node)
22509 return error_mark_node;
22511 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22512 attributes = cp_parser_attributes_opt (parser);
22513 if (attributes == error_mark_node)
22514 return error_mark_node;
22516 cp_parser_require (parser, CPP_EQ, RT_EQ);
22518 if (cp_parser_error_occurred (parser))
22519 return error_mark_node;
22521 cp_parser_commit_to_tentative_parse (parser);
22523 /* Now we are going to parse the type-id of the declaration. */
22526 [dcl.type]/3 says:
22528 "A type-specifier-seq shall not define a class or enumeration
22529 unless it appears in the type-id of an alias-declaration (7.1.3) that
22530 is not the declaration of a template-declaration."
22532 In other words, if we currently are in an alias template, the
22533 type-id should not define a type.
22535 So let's set parser->type_definition_forbidden_message in that
22536 case; cp_parser_check_type_definition (called by
22537 cp_parser_class_specifier) will then emit an error if a type is
22538 defined in the type-id. */
22539 if (parser->num_template_parameter_lists)
22541 saved_message = parser->type_definition_forbidden_message;
22542 parser->type_definition_forbidden_message =
22543 G_("types may not be defined in alias template declarations");
22546 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22547 &type_location);
22549 /* Restore the error message if need be. */
22550 if (parser->num_template_parameter_lists)
22551 parser->type_definition_forbidden_message = saved_message;
22553 if (type == error_mark_node
22554 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22556 cp_parser_skip_to_end_of_block_or_statement (parser);
22557 return error_mark_node;
22560 /* A typedef-name can also be introduced by an alias-declaration. The
22561 identifier following the using keyword becomes a typedef-name. It has
22562 the same semantics as if it were introduced by the typedef
22563 specifier. In particular, it does not define a new type and it shall
22564 not appear in the type-id. */
22566 clear_decl_specs (&decl_specs);
22567 decl_specs.type = type;
22568 if (attributes != NULL_TREE)
22570 decl_specs.attributes = attributes;
22571 set_and_check_decl_spec_loc (&decl_specs,
22572 ds_attribute,
22573 attrs_token);
22575 set_and_check_decl_spec_loc (&decl_specs,
22576 ds_typedef,
22577 using_token);
22578 set_and_check_decl_spec_loc (&decl_specs,
22579 ds_alias,
22580 using_token);
22581 decl_specs.locations[ds_type_spec] = type_location;
22583 if (parser->num_template_parameter_lists
22584 && !cp_parser_check_template_parameters (parser,
22585 /*num_templates=*/0,
22586 /*template_id*/false,
22587 id_location,
22588 /*declarator=*/NULL))
22589 return error_mark_node;
22591 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22593 member_p = at_class_scope_p ();
22594 if (member_p)
22595 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22596 NULL_TREE, attributes);
22597 else
22598 decl = start_decl (declarator, &decl_specs, 0,
22599 attributes, NULL_TREE, &pushed_scope);
22600 if (decl == error_mark_node)
22601 return decl;
22603 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22605 if (pushed_scope)
22606 pop_scope (pushed_scope);
22608 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22609 added into the symbol table; otherwise, return the TYPE_DECL. */
22610 if (DECL_LANG_SPECIFIC (decl)
22611 && DECL_TEMPLATE_INFO (decl)
22612 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22614 decl = DECL_TI_TEMPLATE (decl);
22615 if (member_p)
22616 check_member_template (decl);
22619 return decl;
22622 /* Parse a using-directive.
22624 using-directive:
22625 attribute-specifier-seq [opt] using namespace :: [opt]
22626 nested-name-specifier [opt] namespace-name ; */
22628 static void
22629 cp_parser_using_directive (cp_parser* parser)
22631 tree namespace_decl;
22632 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22635 /* Error during attribute parsing that resulted in skipping
22636 to next semicolon. */
22637 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22638 return;
22641 /* Look for the `using' keyword. */
22642 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22643 /* And the `namespace' keyword. */
22644 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22645 /* Look for the optional `::' operator. */
22646 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22647 /* And the optional nested-name-specifier. */
22648 cp_parser_nested_name_specifier_opt (parser,
22649 /*typename_keyword_p=*/false,
22650 /*check_dependency_p=*/true,
22651 /*type_p=*/false,
22652 /*is_declaration=*/true);
22653 /* Get the namespace being used. */
22654 namespace_decl = cp_parser_namespace_name (parser);
22655 cp_warn_deprecated_use_scopes (namespace_decl);
22656 /* And any specified GNU attributes. */
22657 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22658 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22660 /* Update the symbol table. */
22661 finish_using_directive (namespace_decl, attribs);
22663 /* Look for the final `;'. */
22664 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22667 /* Parse an asm-definition.
22669 asm-qualifier:
22670 volatile
22671 inline
22672 goto
22674 asm-qualifier-list:
22675 asm-qualifier
22676 asm-qualifier-list asm-qualifier
22678 asm-definition:
22679 asm ( string-literal ) ;
22681 GNU Extension:
22683 asm-definition:
22684 asm asm-qualifier-list [opt] ( string-literal ) ;
22685 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22686 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22687 : asm-operand-list [opt] ) ;
22688 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22689 : asm-operand-list [opt]
22690 : asm-clobber-list [opt] ) ;
22691 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22692 : asm-clobber-list [opt]
22693 : asm-goto-list ) ;
22695 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22696 contains goto, and is the only allowed form in that case. No duplicates are
22697 allowed in an asm-qualifier-list. */
22699 static void
22700 cp_parser_asm_definition (cp_parser* parser)
22702 tree outputs = NULL_TREE;
22703 tree inputs = NULL_TREE;
22704 tree clobbers = NULL_TREE;
22705 tree labels = NULL_TREE;
22706 tree asm_stmt;
22707 bool extended_p = false;
22708 bool invalid_inputs_p = false;
22709 bool invalid_outputs_p = false;
22710 required_token missing = RT_NONE;
22711 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
22712 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22714 /* Look for the `asm' keyword. */
22715 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22717 /* In C++20, unevaluated inline assembly is permitted in constexpr
22718 functions. */
22719 if (parser->in_function_body
22720 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22721 && cxx_dialect < cxx20)
22722 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22723 "function only available with %<-std=c++20%> or "
22724 "%<-std=gnu++20%>");
22726 /* Handle the asm-qualifier-list. */
22727 location_t volatile_loc = UNKNOWN_LOCATION;
22728 location_t inline_loc = UNKNOWN_LOCATION;
22729 location_t goto_loc = UNKNOWN_LOCATION;
22730 location_t first_loc = UNKNOWN_LOCATION;
22732 if (cp_parser_allow_gnu_extensions_p (parser))
22733 for (;;)
22735 cp_token *token = cp_lexer_peek_token (parser->lexer);
22736 location_t loc = token->location;
22737 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22739 case RID_VOLATILE:
22740 if (volatile_loc)
22742 error_at (loc, "duplicate %<asm%> qualifier %qT",
22743 token->u.value);
22744 inform (volatile_loc, "first seen here");
22746 else
22748 if (!parser->in_function_body)
22749 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22750 "outside of function body", token->u.value);
22751 volatile_loc = loc;
22753 cp_lexer_consume_token (parser->lexer);
22754 continue;
22756 case RID_INLINE:
22757 if (inline_loc)
22759 error_at (loc, "duplicate %<asm%> qualifier %qT",
22760 token->u.value);
22761 inform (inline_loc, "first seen here");
22763 else
22764 inline_loc = loc;
22765 if (!first_loc)
22766 first_loc = loc;
22767 cp_lexer_consume_token (parser->lexer);
22768 continue;
22770 case RID_GOTO:
22771 if (goto_loc)
22773 error_at (loc, "duplicate %<asm%> qualifier %qT",
22774 token->u.value);
22775 inform (goto_loc, "first seen here");
22777 else
22778 goto_loc = loc;
22779 if (!first_loc)
22780 first_loc = loc;
22781 cp_lexer_consume_token (parser->lexer);
22782 continue;
22784 case RID_CONST:
22785 case RID_RESTRICT:
22786 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22787 cp_lexer_consume_token (parser->lexer);
22788 continue;
22790 default:
22791 break;
22793 break;
22796 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22797 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22798 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22800 if (!parser->in_function_body && (inline_p || goto_p))
22802 error_at (first_loc, "%<asm%> qualifier outside of function body");
22803 inline_p = goto_p = false;
22806 /* Look for the opening `('. */
22807 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22808 return;
22809 /* Look for the string. */
22810 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22811 /*wide_ok=*/false);
22812 if (string == error_mark_node)
22814 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22815 /*consume_paren=*/true);
22816 return;
22819 /* If we're allowing GNU extensions, check for the extended assembly
22820 syntax. Unfortunately, the `:' tokens need not be separated by
22821 a space in C, and so, for compatibility, we tolerate that here
22822 too. Doing that means that we have to treat the `::' operator as
22823 two `:' tokens. */
22824 if (cp_parser_allow_gnu_extensions_p (parser)
22825 && parser->in_function_body
22826 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22827 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22829 bool inputs_p = false;
22830 bool clobbers_p = false;
22831 bool labels_p = false;
22833 /* The extended syntax was used. */
22834 extended_p = true;
22836 /* Look for outputs. */
22837 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22839 /* Consume the `:'. */
22840 cp_lexer_consume_token (parser->lexer);
22841 /* Parse the output-operands. */
22842 if (cp_lexer_next_token_is_not (parser->lexer,
22843 CPP_COLON)
22844 && cp_lexer_next_token_is_not (parser->lexer,
22845 CPP_SCOPE)
22846 && cp_lexer_next_token_is_not (parser->lexer,
22847 CPP_CLOSE_PAREN))
22849 outputs = cp_parser_asm_operand_list (parser);
22850 if (outputs == error_mark_node)
22851 invalid_outputs_p = true;
22854 /* If the next token is `::', there are no outputs, and the
22855 next token is the beginning of the inputs. */
22856 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22857 /* The inputs are coming next. */
22858 inputs_p = true;
22860 /* Look for inputs. */
22861 if (inputs_p
22862 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22864 /* Consume the `:' or `::'. */
22865 cp_lexer_consume_token (parser->lexer);
22866 /* Parse the output-operands. */
22867 if (cp_lexer_next_token_is_not (parser->lexer,
22868 CPP_COLON)
22869 && cp_lexer_next_token_is_not (parser->lexer,
22870 CPP_SCOPE)
22871 && cp_lexer_next_token_is_not (parser->lexer,
22872 CPP_CLOSE_PAREN))
22874 inputs = cp_parser_asm_operand_list (parser);
22875 if (inputs == error_mark_node)
22876 invalid_inputs_p = true;
22879 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22880 /* The clobbers are coming next. */
22881 clobbers_p = true;
22883 /* Look for clobbers. */
22884 if (clobbers_p
22885 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22887 clobbers_p = true;
22888 /* Consume the `:' or `::'. */
22889 cp_lexer_consume_token (parser->lexer);
22890 /* Parse the clobbers. */
22891 if (cp_lexer_next_token_is_not (parser->lexer,
22892 CPP_COLON)
22893 && cp_lexer_next_token_is_not (parser->lexer,
22894 CPP_CLOSE_PAREN))
22895 clobbers = cp_parser_asm_clobber_list (parser);
22897 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22898 /* The labels are coming next. */
22899 labels_p = true;
22901 /* Look for labels. */
22902 if (labels_p
22903 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22905 labels_p = true;
22906 /* Consume the `:' or `::'. */
22907 cp_lexer_consume_token (parser->lexer);
22908 /* Parse the labels. */
22909 labels = cp_parser_asm_label_list (parser);
22912 if (goto_p && !labels_p)
22913 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22915 else if (goto_p)
22916 missing = RT_COLON_SCOPE;
22918 /* Look for the closing `)'. */
22919 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22920 missing ? missing : RT_CLOSE_PAREN))
22921 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22922 /*consume_paren=*/true);
22923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22925 if (!invalid_inputs_p && !invalid_outputs_p)
22927 /* Create the ASM_EXPR. */
22928 if (parser->in_function_body)
22930 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22931 inputs, clobbers, labels, inline_p);
22932 /* If the extended syntax was not used, mark the ASM_EXPR. */
22933 if (!extended_p)
22935 tree temp = asm_stmt;
22936 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22937 temp = TREE_OPERAND (temp, 0);
22939 ASM_INPUT_P (temp) = 1;
22942 else
22943 symtab->finalize_toplevel_asm (string);
22946 if (std_attrs && any_nonignored_attribute_p (std_attrs))
22947 warning_at (asm_loc, OPT_Wattributes,
22948 "attributes ignored on %<asm%> declaration");
22951 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22952 type that comes from the decl-specifier-seq. */
22954 static tree
22955 strip_declarator_types (tree type, cp_declarator *declarator)
22957 for (cp_declarator *d = declarator; d;)
22958 switch (d->kind)
22960 case cdk_id:
22961 case cdk_decomp:
22962 case cdk_error:
22963 d = NULL;
22964 break;
22966 default:
22967 if (TYPE_PTRMEMFUNC_P (type))
22968 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22969 type = TREE_TYPE (type);
22970 d = d->declarator;
22971 break;
22974 return type;
22977 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22978 a construct looks like a variable definition but is actually a function
22979 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22980 is the declarator for this function declaration. */
22982 static void
22983 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22984 const cp_declarator *declarator)
22986 /* Only warn if we are declaring a function at block scope. */
22987 if (!at_function_scope_p ())
22988 return;
22990 /* And only if there is no storage class specified. */
22991 if (decl_specifiers->storage_class != sc_none
22992 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22993 return;
22995 if (declarator->kind != cdk_function
22996 || !declarator->declarator
22997 || declarator->declarator->kind != cdk_id
22998 || !identifier_p (get_unqualified_id
22999 (const_cast<cp_declarator *>(declarator))))
23000 return;
23002 /* Don't warn when the whole declarator (not just the declarator-id!)
23003 was parenthesized. That is, don't warn for int(n()) but do warn
23004 for int(f)(). */
23005 if (declarator->parenthesized != UNKNOWN_LOCATION)
23006 return;
23008 tree type;
23009 if (decl_specifiers->type)
23011 type = decl_specifiers->type;
23012 if (TREE_CODE (type) == TYPE_DECL)
23013 type = TREE_TYPE (type);
23015 /* If the return type is void there is no ambiguity. */
23016 if (same_type_p (type, void_type_node))
23017 return;
23019 else if (decl_specifiers->any_type_specifiers_p)
23020 /* Code like long f(); will have null ->type. If we have any
23021 type-specifiers, pretend we've seen int. */
23022 type = integer_type_node;
23023 else
23024 return;
23026 auto_diagnostic_group d;
23027 location_t loc = declarator->u.function.parens_loc;
23028 tree params = declarator->u.function.parameters;
23029 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
23031 /* The T t() case. */
23032 if (params == void_list_node)
23034 if (warning_at (loc, OPT_Wvexing_parse,
23035 "empty parentheses were disambiguated as a function "
23036 "declaration"))
23038 /* () means value-initialization (C++03 and up); {} (C++11 and up)
23039 means value-initialization or aggregate-initialization, nothing
23040 means default-initialization. We can only suggest removing the
23041 parentheses/adding {} if T has a default constructor. */
23042 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
23044 gcc_rich_location iloc (loc);
23045 iloc.add_fixit_remove ();
23046 inform (&iloc, "remove parentheses to default-initialize "
23047 "a variable");
23048 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23050 if (CP_AGGREGATE_TYPE_P (type))
23051 inform (loc, "or replace parentheses with braces to "
23052 "aggregate-initialize a variable");
23053 else
23054 inform (loc, "or replace parentheses with braces to "
23055 "value-initialize a variable");
23059 return;
23062 /* If we had (...) or the parameter-list wasn't parenthesized,
23063 we're done. */
23064 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
23065 return;
23067 /* The T t(X()) case. */
23068 if (list_length (params) == 2)
23070 if (warning_at (loc, OPT_Wvexing_parse,
23071 "parentheses were disambiguated as a function "
23072 "declaration"))
23074 gcc_rich_location iloc (loc);
23075 /* {}-initialization means that we can use an initializer-list
23076 constructor if no default constructor is available, so don't
23077 suggest using {} for classes that have an initializer_list
23078 constructor. */
23079 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23081 iloc.add_fixit_replace (get_start (loc), "{");
23082 iloc.add_fixit_replace (get_finish (loc), "}");
23083 inform (&iloc, "replace parentheses with braces to declare a "
23084 "variable");
23086 else
23088 iloc.add_fixit_insert_after (get_start (loc), "(");
23089 iloc.add_fixit_insert_before (get_finish (loc), ")");
23090 inform (&iloc, "add parentheses to declare a variable");
23094 /* The T t(X(), X()) case. */
23095 else if (warning_at (loc, OPT_Wvexing_parse,
23096 "parentheses were disambiguated as a function "
23097 "declaration"))
23099 gcc_rich_location iloc (loc);
23100 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23102 iloc.add_fixit_replace (get_start (loc), "{");
23103 iloc.add_fixit_replace (get_finish (loc), "}");
23104 inform (&iloc, "replace parentheses with braces to declare a "
23105 "variable");
23110 /* If DECLARATOR with DECL_SPECS is a function declarator that has
23111 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
23112 has the same meaning as in cp_parser_declarator. */
23114 static void
23115 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
23116 cp_decl_specifier_seq *decl_specs,
23117 cp_declarator *declarator,
23118 int *ctor_dtor_or_conv_p)
23120 if (cxx_dialect >= cxx17
23121 && *ctor_dtor_or_conv_p <= 0
23122 && !decl_specs->type
23123 && !decl_specs->any_type_specifiers_p
23124 && function_declarator_p (declarator))
23126 cp_declarator *id = get_id_declarator (declarator);
23127 tree name = id->u.id.unqualified_name;
23128 parser->scope = id->u.id.qualifying_scope;
23129 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
23130 if (tmpl
23131 && (DECL_CLASS_TEMPLATE_P (tmpl)
23132 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
23134 id->u.id.unqualified_name = dguide_name (tmpl);
23135 id->u.id.sfk = sfk_deduction_guide;
23136 *ctor_dtor_or_conv_p = 1;
23141 /* Declarators [gram.dcl.decl] */
23143 /* Parse an init-declarator.
23145 init-declarator:
23146 declarator initializer [opt]
23148 GNU Extension:
23150 init-declarator:
23151 declarator asm-specification [opt] attributes [opt] initializer [opt]
23153 function-definition:
23154 decl-specifier-seq [opt] declarator ctor-initializer [opt]
23155 function-body
23156 decl-specifier-seq [opt] declarator function-try-block
23158 GNU Extension:
23160 function-definition:
23161 __extension__ function-definition
23163 TM Extension:
23165 function-definition:
23166 decl-specifier-seq [opt] declarator function-transaction-block
23168 The parser flags FLAGS is used to control type-specifier parsing.
23170 The DECL_SPECIFIERS apply to this declarator. Returns a
23171 representation of the entity declared. If MEMBER_P is TRUE, then
23172 this declarator appears in a class scope. The new DECL created by
23173 this declarator is returned.
23175 The CHECKS are access checks that should be performed once we know
23176 what entity is being declared (and, therefore, what classes have
23177 befriended it).
23179 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
23180 for a function-definition here as well. If the declarator is a
23181 declarator for a function-definition, *FUNCTION_DEFINITION_P will
23182 be TRUE upon return. By that point, the function-definition will
23183 have been completely parsed.
23185 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
23186 is FALSE.
23188 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
23189 parsed declaration if it is an uninitialized single declarator not followed
23190 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
23191 if present, will not be consumed. If returned, this declarator will be
23192 created with SD_INITIALIZED but will not call cp_finish_decl.
23194 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
23195 and there is an initializer, the pointed location_t is set to the
23196 location of the '=' or `(', or '{' in C++11 token introducing the
23197 initializer. */
23199 static tree
23200 cp_parser_init_declarator (cp_parser* parser,
23201 cp_parser_flags flags,
23202 cp_decl_specifier_seq *decl_specifiers,
23203 vec<deferred_access_check, va_gc> *checks,
23204 bool function_definition_allowed_p,
23205 bool member_p,
23206 int declares_class_or_enum,
23207 bool* function_definition_p,
23208 tree* maybe_range_for_decl,
23209 location_t* init_loc,
23210 tree* auto_result)
23212 cp_token *token = NULL, *asm_spec_start_token = NULL,
23213 *attributes_start_token = NULL;
23214 cp_declarator *declarator;
23215 tree prefix_attributes;
23216 tree attributes = NULL;
23217 tree asm_specification;
23218 tree initializer;
23219 tree decl = NULL_TREE;
23220 tree scope;
23221 int is_initialized;
23222 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
23223 initialized with "= ..", CPP_OPEN_PAREN if initialized with
23224 "(...)". */
23225 enum cpp_ttype initialization_kind;
23226 bool is_direct_init = false;
23227 bool is_non_constant_init;
23228 int ctor_dtor_or_conv_p;
23229 bool friend_p = cp_parser_friend_p (decl_specifiers);
23230 bool static_p = decl_specifiers->storage_class == sc_static;
23231 tree pushed_scope = NULL_TREE;
23232 bool range_for_decl_p = false;
23233 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23234 location_t tmp_init_loc = UNKNOWN_LOCATION;
23236 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
23237 flags |= CP_PARSER_FLAGS_CONSTEVAL;
23239 /* Assume that this is not the declarator for a function
23240 definition. */
23241 if (function_definition_p)
23242 *function_definition_p = false;
23244 /* Default arguments are only permitted for function parameters. */
23245 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23246 parser->default_arg_ok_p = false;
23248 /* Defer access checks while parsing the declarator; we cannot know
23249 what names are accessible until we know what is being
23250 declared. */
23251 resume_deferring_access_checks ();
23253 token = cp_lexer_peek_token (parser->lexer);
23255 /* Parse the declarator. */
23256 declarator
23257 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23258 flags, &ctor_dtor_or_conv_p,
23259 /*parenthesized_p=*/NULL,
23260 member_p, friend_p, static_p);
23261 /* Gather up the deferred checks. */
23262 stop_deferring_access_checks ();
23264 parser->default_arg_ok_p = saved_default_arg_ok_p;
23266 /* If the DECLARATOR was erroneous, there's no need to go
23267 further. */
23268 if (declarator == cp_error_declarator)
23269 return error_mark_node;
23271 /* Check that the number of template-parameter-lists is OK. */
23272 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
23273 token->location))
23274 return error_mark_node;
23276 if (declares_class_or_enum & 2)
23277 cp_parser_check_for_definition_in_return_type (declarator,
23278 decl_specifiers->type,
23279 decl_specifiers->locations[ds_type_spec]);
23281 /* Figure out what scope the entity declared by the DECLARATOR is
23282 located in. `grokdeclarator' sometimes changes the scope, so
23283 we compute it now. */
23284 scope = get_scope_of_declarator (declarator);
23286 /* Perform any lookups in the declared type which were thought to be
23287 dependent, but are not in the scope of the declarator. */
23288 decl_specifiers->type
23289 = maybe_update_decl_type (decl_specifiers->type, scope);
23291 /* If we're allowing GNU extensions, look for an
23292 asm-specification. */
23293 if (cp_parser_allow_gnu_extensions_p (parser))
23295 /* Look for an asm-specification. */
23296 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
23297 asm_specification = cp_parser_asm_specification_opt (parser);
23299 else
23300 asm_specification = NULL_TREE;
23302 /* Gather the attributes that were provided with the
23303 decl-specifiers. */
23304 prefix_attributes = decl_specifiers->attributes;
23306 /* Look for attributes. */
23307 attributes_start_token = cp_lexer_peek_token (parser->lexer);
23308 attributes = cp_parser_attributes_opt (parser);
23310 /* Peek at the next token. */
23311 token = cp_lexer_peek_token (parser->lexer);
23313 bool bogus_implicit_tmpl = false;
23315 if (function_declarator_p (declarator))
23317 /* Handle C++17 deduction guides. Note that class-scope
23318 non-template deduction guides are instead handled in
23319 cp_parser_member_declaration. */
23320 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23321 decl_specifiers,
23322 declarator,
23323 &ctor_dtor_or_conv_p);
23325 if (!member_p && !cp_parser_error_occurred (parser))
23326 warn_about_ambiguous_parse (decl_specifiers, declarator);
23328 /* Check to see if the token indicates the start of a
23329 function-definition. */
23330 if (cp_parser_token_starts_function_definition_p (token))
23332 if (!function_definition_allowed_p)
23334 /* If a function-definition should not appear here, issue an
23335 error message. */
23336 cp_parser_error (parser,
23337 "a function-definition is not allowed here");
23338 return error_mark_node;
23341 location_t func_brace_location
23342 = cp_lexer_peek_token (parser->lexer)->location;
23344 /* Neither attributes nor an asm-specification are allowed
23345 on a function-definition. */
23346 if (asm_specification)
23347 error_at (asm_spec_start_token->location,
23348 "an %<asm%> specification is not allowed "
23349 "on a function-definition");
23350 if (attributes)
23351 error_at (attributes_start_token->location,
23352 "attributes are not allowed "
23353 "on a function-definition");
23354 /* This is a function-definition. */
23355 *function_definition_p = true;
23357 /* Parse the function definition. */
23358 if (member_p)
23359 decl = cp_parser_save_member_function_body (parser,
23360 decl_specifiers,
23361 declarator,
23362 prefix_attributes);
23363 else
23364 decl =
23365 (cp_parser_function_definition_from_specifiers_and_declarator
23366 (parser, decl_specifiers, prefix_attributes, declarator));
23368 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23370 /* This is where the prologue starts... */
23371 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23372 = func_brace_location;
23375 return decl;
23378 else if (parser->fully_implicit_function_template_p)
23380 /* A non-template declaration involving a function parameter list
23381 containing an implicit template parameter will be made into a
23382 template. If the resulting declaration is not going to be an
23383 actual function then finish the template scope here to prevent it.
23384 An error message will be issued once we have a decl to talk about.
23386 FIXME probably we should do type deduction rather than create an
23387 implicit template, but the standard currently doesn't allow it. */
23388 bogus_implicit_tmpl = true;
23389 finish_fully_implicit_template (parser, NULL_TREE);
23392 /* [dcl.dcl]
23394 Only in function declarations for constructors, destructors, type
23395 conversions, and deduction guides can the decl-specifier-seq be omitted.
23397 We explicitly postpone this check past the point where we handle
23398 function-definitions because we tolerate function-definitions
23399 that are missing their return types in some modes. */
23400 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23402 cp_parser_error (parser,
23403 "expected constructor, destructor, or type conversion");
23404 return error_mark_node;
23407 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23408 an initializer as well. */
23409 if (token->type == CPP_EQ
23410 || token->type == CPP_OPEN_PAREN
23411 || token->type == CPP_OPEN_BRACE)
23413 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23414 It isn't; it's an expression. (Here '(i)' would have already been
23415 parsed as a declarator.) */
23416 if (token->type == CPP_OPEN_PAREN
23417 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23419 cp_lexer_save_tokens (parser->lexer);
23420 cp_lexer_consume_token (parser->lexer);
23421 cp_parser_skip_to_closing_parenthesis (parser,
23422 /*recovering*/false,
23423 /*or_comma*/false,
23424 /*consume_paren*/true);
23425 /* If this is an initializer, only a ',' or ';' can follow: either
23426 we have another init-declarator, or we're at the end of an
23427 init-declarator-list which can only be followed by a ';'. */
23428 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23429 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23430 cp_lexer_rollback_tokens (parser->lexer);
23431 if (UNLIKELY (!ok))
23432 /* Not an init-declarator. */
23433 return error_mark_node;
23435 is_initialized = SD_INITIALIZED;
23436 initialization_kind = token->type;
23437 declarator->init_loc = token->location;
23438 if (maybe_range_for_decl)
23439 *maybe_range_for_decl = error_mark_node;
23440 tmp_init_loc = token->location;
23441 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23442 *init_loc = tmp_init_loc;
23444 if (token->type == CPP_EQ
23445 && function_declarator_p (declarator))
23447 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23448 if (t2->keyword == RID_DEFAULT)
23449 is_initialized = SD_DEFAULTED;
23450 else if (t2->keyword == RID_DELETE)
23451 is_initialized = SD_DELETED;
23454 else
23456 /* If the init-declarator isn't initialized and isn't followed by a
23457 `,' or `;', it's not a valid init-declarator. */
23458 if (token->type != CPP_COMMA
23459 && token->type != CPP_SEMICOLON)
23461 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23462 range_for_decl_p = true;
23463 else
23465 if (!maybe_range_for_decl)
23466 cp_parser_error (parser, "expected initializer");
23467 return error_mark_node;
23470 is_initialized = SD_UNINITIALIZED;
23471 initialization_kind = CPP_EOF;
23474 /* Because start_decl has side-effects, we should only call it if we
23475 know we're going ahead. By this point, we know that we cannot
23476 possibly be looking at any other construct. */
23477 cp_parser_commit_to_tentative_parse (parser);
23479 /* Enter the newly declared entry in the symbol table. If we're
23480 processing a declaration in a class-specifier, we wait until
23481 after processing the initializer. */
23482 if (!member_p)
23484 if (parser->in_unbraced_linkage_specification_p)
23485 decl_specifiers->storage_class = sc_extern;
23486 decl = start_decl (declarator, decl_specifiers,
23487 range_for_decl_p? SD_INITIALIZED : is_initialized,
23488 attributes, prefix_attributes, &pushed_scope);
23489 cp_finalize_omp_declare_simd (parser, decl);
23490 cp_finalize_oacc_routine (parser, decl, false);
23491 /* Adjust location of decl if declarator->id_loc is more appropriate:
23492 set, and decl wasn't merged with another decl, in which case its
23493 location would be different from input_location, and more accurate. */
23494 if (DECL_P (decl)
23495 && declarator->id_loc != UNKNOWN_LOCATION
23496 && DECL_SOURCE_LOCATION (decl) == input_location)
23497 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23499 else if (scope)
23500 /* Enter the SCOPE. That way unqualified names appearing in the
23501 initializer will be looked up in SCOPE. */
23502 pushed_scope = push_scope (scope);
23504 /* Perform deferred access control checks, now that we know in which
23505 SCOPE the declared entity resides. */
23506 if (!member_p && decl)
23508 tree saved_current_function_decl = NULL_TREE;
23510 /* If the entity being declared is a function, pretend that we
23511 are in its scope. If it is a `friend', it may have access to
23512 things that would not otherwise be accessible. */
23513 if (TREE_CODE (decl) == FUNCTION_DECL)
23515 saved_current_function_decl = current_function_decl;
23516 current_function_decl = decl;
23519 /* Perform access checks for template parameters. */
23520 cp_parser_perform_template_parameter_access_checks (checks);
23522 /* Perform the access control checks for the declarator and the
23523 decl-specifiers. */
23524 perform_deferred_access_checks (tf_warning_or_error);
23526 /* Restore the saved value. */
23527 if (TREE_CODE (decl) == FUNCTION_DECL)
23528 current_function_decl = saved_current_function_decl;
23531 /* Parse the initializer. */
23532 initializer = NULL_TREE;
23533 is_direct_init = false;
23534 is_non_constant_init = true;
23535 if (is_initialized)
23537 if (function_declarator_p (declarator))
23539 if (initialization_kind == CPP_EQ)
23540 initializer = cp_parser_pure_specifier (parser);
23541 else
23543 /* If the declaration was erroneous, we don't really
23544 know what the user intended, so just silently
23545 consume the initializer. */
23546 if (decl != error_mark_node)
23547 error_at (tmp_init_loc, "initializer provided for function");
23548 cp_parser_skip_to_closing_parenthesis (parser,
23549 /*recovering=*/true,
23550 /*or_comma=*/false,
23551 /*consume_paren=*/true);
23554 else
23556 /* We want to record the extra mangling scope for in-class
23557 initializers of class members and initializers of static
23558 data member templates and namespace-scope initializers.
23559 The former involves deferring parsing of the initializer
23560 until end of class as with default arguments. So right
23561 here we only handle the latter two. */
23562 bool has_lambda_scope = false;
23564 if (decl != error_mark_node
23565 && !member_p
23566 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23567 has_lambda_scope = true;
23569 if (has_lambda_scope)
23570 start_lambda_scope (decl);
23571 initializer = cp_parser_initializer (parser,
23572 &is_direct_init,
23573 &is_non_constant_init);
23574 if (has_lambda_scope)
23575 finish_lambda_scope ();
23576 if (initializer == error_mark_node)
23577 cp_parser_skip_to_end_of_statement (parser);
23581 /* The old parser allows attributes to appear after a parenthesized
23582 initializer. Mark Mitchell proposed removing this functionality
23583 on the GCC mailing lists on 2002-08-13. This parser accepts the
23584 attributes -- but ignores them. Made a permerror in GCC 8. */
23585 if (cp_parser_allow_gnu_extensions_p (parser)
23586 && initialization_kind == CPP_OPEN_PAREN
23587 && cp_parser_attributes_opt (parser)
23588 && permerror (input_location,
23589 "attributes after parenthesized initializer ignored"))
23591 static bool hint;
23592 if (flag_permissive && !hint)
23594 hint = true;
23595 inform (input_location,
23596 "this flexibility is deprecated and will be removed");
23600 /* And now complain about a non-function implicit template. */
23601 if (bogus_implicit_tmpl && decl != error_mark_node)
23602 error_at (DECL_SOURCE_LOCATION (decl),
23603 "non-function %qD declared as implicit template", decl);
23605 /* For an in-class declaration, use `grokfield' to create the
23606 declaration. */
23607 if (member_p)
23609 if (pushed_scope)
23611 pop_scope (pushed_scope);
23612 pushed_scope = NULL_TREE;
23614 decl = grokfield (declarator, decl_specifiers,
23615 initializer, !is_non_constant_init,
23616 /*asmspec=*/NULL_TREE,
23617 attr_chainon (attributes, prefix_attributes));
23618 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23619 cp_parser_save_default_args (parser, decl);
23620 cp_finalize_omp_declare_simd (parser, decl);
23621 cp_finalize_oacc_routine (parser, decl, false);
23624 /* Finish processing the declaration. But, skip member
23625 declarations. */
23626 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23628 cp_finish_decl (decl,
23629 initializer, !is_non_constant_init,
23630 asm_specification,
23631 /* If the initializer is in parentheses, then this is
23632 a direct-initialization, which means that an
23633 `explicit' constructor is OK. Otherwise, an
23634 `explicit' constructor cannot be used. */
23635 ((is_direct_init || !is_initialized)
23636 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23638 else if ((cxx_dialect != cxx98) && friend_p
23639 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23640 /* Core issue #226 (C++0x only): A default template-argument
23641 shall not be specified in a friend class template
23642 declaration. */
23643 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23644 /*is_partial=*/false, /*is_friend_decl=*/1);
23646 if (!friend_p && pushed_scope)
23647 pop_scope (pushed_scope);
23649 if (function_declarator_p (declarator)
23650 && parser->fully_implicit_function_template_p)
23652 if (member_p)
23653 decl = finish_fully_implicit_template (parser, decl);
23654 else
23655 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23658 if (auto_result && is_initialized && decl_specifiers->type
23659 && type_uses_auto (decl_specifiers->type))
23660 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23662 return decl;
23665 /* Parse a declarator.
23667 declarator:
23668 direct-declarator
23669 ptr-operator declarator
23671 abstract-declarator:
23672 ptr-operator abstract-declarator [opt]
23673 direct-abstract-declarator
23675 GNU Extensions:
23677 declarator:
23678 attributes [opt] direct-declarator
23679 attributes [opt] ptr-operator declarator
23681 abstract-declarator:
23682 attributes [opt] ptr-operator abstract-declarator [opt]
23683 attributes [opt] direct-abstract-declarator
23685 The parser flags FLAGS is used to control type-specifier parsing.
23687 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23688 detect constructors, destructors, deduction guides, or conversion operators.
23689 It is set to -1 if the declarator is a name, and +1 if it is a
23690 function. Otherwise it is set to zero. Usually you just want to
23691 test for >0, but internally the negative value is used.
23693 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23694 a decl-specifier-seq unless it declares a constructor, destructor,
23695 or conversion. It might seem that we could check this condition in
23696 semantic analysis, rather than parsing, but that makes it difficult
23697 to handle something like `f()'. We want to notice that there are
23698 no decl-specifiers, and therefore realize that this is an
23699 expression, not a declaration.)
23701 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23702 the declarator is a direct-declarator of the form "(...)".
23704 MEMBER_P is true iff this declarator is a member-declarator.
23706 FRIEND_P is true iff this declarator is a friend.
23708 STATIC_P is true iff the keyword static was seen. */
23710 static cp_declarator *
23711 cp_parser_declarator (cp_parser* parser,
23712 cp_parser_declarator_kind dcl_kind,
23713 cp_parser_flags flags,
23714 int* ctor_dtor_or_conv_p,
23715 bool* parenthesized_p,
23716 bool member_p, bool friend_p, bool static_p)
23718 cp_declarator *declarator;
23719 enum tree_code code;
23720 cp_cv_quals cv_quals;
23721 tree class_type;
23722 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23724 /* Assume this is not a constructor, destructor, or type-conversion
23725 operator. */
23726 if (ctor_dtor_or_conv_p)
23727 *ctor_dtor_or_conv_p = 0;
23729 if (cp_parser_allow_gnu_extensions_p (parser))
23730 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23732 /* Check for the ptr-operator production. */
23733 cp_parser_parse_tentatively (parser);
23734 /* Parse the ptr-operator. */
23735 code = cp_parser_ptr_operator (parser,
23736 &class_type,
23737 &cv_quals,
23738 &std_attributes);
23740 /* If that worked, then we have a ptr-operator. */
23741 if (cp_parser_parse_definitely (parser))
23743 /* If a ptr-operator was found, then this declarator was not
23744 parenthesized. */
23745 if (parenthesized_p)
23746 *parenthesized_p = false;
23747 /* The dependent declarator is optional if we are parsing an
23748 abstract-declarator. */
23749 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23750 cp_parser_parse_tentatively (parser);
23752 /* Parse the dependent declarator. */
23753 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23754 /*ctor_dtor_or_conv_p=*/NULL,
23755 /*parenthesized_p=*/NULL,
23756 member_p, friend_p, static_p);
23758 /* If we are parsing an abstract-declarator, we must handle the
23759 case where the dependent declarator is absent. */
23760 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23761 && !cp_parser_parse_definitely (parser))
23762 declarator = NULL;
23764 declarator = cp_parser_make_indirect_declarator
23765 (code, class_type, cv_quals, declarator, std_attributes);
23767 /* Everything else is a direct-declarator. */
23768 else
23770 if (parenthesized_p)
23771 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23772 CPP_OPEN_PAREN);
23773 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23774 flags, ctor_dtor_or_conv_p,
23775 member_p, friend_p, static_p);
23778 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23779 declarator->attributes = gnu_attributes;
23780 return declarator;
23783 /* Parse a direct-declarator or direct-abstract-declarator.
23785 direct-declarator:
23786 declarator-id
23787 direct-declarator ( parameter-declaration-clause )
23788 cv-qualifier-seq [opt]
23789 ref-qualifier [opt]
23790 exception-specification [opt]
23791 direct-declarator [ constant-expression [opt] ]
23792 ( declarator )
23794 direct-abstract-declarator:
23795 direct-abstract-declarator [opt]
23796 ( parameter-declaration-clause )
23797 cv-qualifier-seq [opt]
23798 ref-qualifier [opt]
23799 exception-specification [opt]
23800 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23801 ( abstract-declarator )
23803 Returns a representation of the declarator. DCL_KIND is
23804 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23805 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23806 we are parsing a direct-declarator. It is
23807 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23808 of ambiguity we prefer an abstract declarator, as per
23809 [dcl.ambig.res].
23810 The parser flags FLAGS is used to control type-specifier parsing.
23811 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23812 as for cp_parser_declarator. */
23814 static cp_declarator *
23815 cp_parser_direct_declarator (cp_parser* parser,
23816 cp_parser_declarator_kind dcl_kind,
23817 cp_parser_flags flags,
23818 int* ctor_dtor_or_conv_p,
23819 bool member_p, bool friend_p, bool static_p)
23821 cp_token *token;
23822 cp_declarator *declarator = NULL;
23823 tree scope = NULL_TREE;
23824 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23825 bool saved_in_declarator_p = parser->in_declarator_p;
23826 bool first = true;
23827 tree pushed_scope = NULL_TREE;
23828 cp_token *open_paren = NULL, *close_paren = NULL;
23830 while (true)
23832 /* Peek at the next token. */
23833 token = cp_lexer_peek_token (parser->lexer);
23834 if (token->type == CPP_OPEN_PAREN)
23836 /* This is either a parameter-declaration-clause, or a
23837 parenthesized declarator. When we know we are parsing a
23838 named declarator, it must be a parenthesized declarator
23839 if FIRST is true. For instance, `(int)' is a
23840 parameter-declaration-clause, with an omitted
23841 direct-abstract-declarator. But `((*))', is a
23842 parenthesized abstract declarator. Finally, when T is a
23843 template parameter `(T)' is a
23844 parameter-declaration-clause, and not a parenthesized
23845 named declarator.
23847 We first try and parse a parameter-declaration-clause,
23848 and then try a nested declarator (if FIRST is true).
23850 It is not an error for it not to be a
23851 parameter-declaration-clause, even when FIRST is
23852 false. Consider,
23854 int i (int);
23855 int i (3);
23857 The first is the declaration of a function while the
23858 second is the definition of a variable, including its
23859 initializer.
23861 Having seen only the parenthesis, we cannot know which of
23862 these two alternatives should be selected. Even more
23863 complex are examples like:
23865 int i (int (a));
23866 int i (int (3));
23868 The former is a function-declaration; the latter is a
23869 variable initialization.
23871 Thus again, we try a parameter-declaration-clause, and if
23872 that fails, we back out and return. */
23874 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23876 tree params;
23877 bool is_declarator = false;
23879 open_paren = NULL;
23881 /* In a member-declarator, the only valid interpretation
23882 of a parenthesis is the start of a
23883 parameter-declaration-clause. (It is invalid to
23884 initialize a static data member with a parenthesized
23885 initializer; only the "=" form of initialization is
23886 permitted.) */
23887 if (!member_p)
23888 cp_parser_parse_tentatively (parser);
23890 /* Consume the `('. */
23891 const location_t parens_start = token->location;
23892 matching_parens parens;
23893 parens.consume_open (parser);
23894 if (first)
23896 /* If this is going to be an abstract declarator, we're
23897 in a declarator and we can't have default args. */
23898 parser->default_arg_ok_p = false;
23899 parser->in_declarator_p = true;
23902 begin_scope (sk_function_parms, NULL_TREE);
23904 /* Parse the parameter-declaration-clause. */
23905 params
23906 = cp_parser_parameter_declaration_clause (parser, flags);
23907 const location_t parens_end
23908 = cp_lexer_peek_token (parser->lexer)->location;
23910 /* Consume the `)'. */
23911 parens.require_close (parser);
23913 /* For code like
23914 int x(auto(42));
23915 A a(auto(i), 42);
23916 we have synthesized an implicit template parameter and marked
23917 what we thought was a function as an implicit function template.
23918 But now, having seen the whole parameter list, we know it's not
23919 a function declaration, so undo that. */
23920 if (cp_parser_error_occurred (parser)
23921 && parser->fully_implicit_function_template_p
23922 /* Don't do this for the inner (). */
23923 && parser->default_arg_ok_p)
23924 abort_fully_implicit_template (parser);
23926 /* If all went well, parse the cv-qualifier-seq,
23927 ref-qualifier and the exception-specification. */
23928 if (member_p || cp_parser_parse_definitely (parser))
23930 cp_cv_quals cv_quals;
23931 cp_virt_specifiers virt_specifiers;
23932 cp_ref_qualifier ref_qual;
23933 tree exception_specification;
23934 tree late_return;
23935 tree attrs;
23936 bool memfn = (member_p || (pushed_scope
23937 && CLASS_TYPE_P (pushed_scope)));
23938 unsigned char local_variables_forbidden_p
23939 = parser->local_variables_forbidden_p;
23940 /* 'this' is not allowed in static member functions. */
23941 if (static_p || friend_p)
23942 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23944 is_declarator = true;
23946 if (ctor_dtor_or_conv_p)
23947 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23948 first = false;
23950 /* Parse the cv-qualifier-seq. */
23951 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23952 /* Parse the ref-qualifier. */
23953 ref_qual = cp_parser_ref_qualifier_opt (parser);
23954 /* Parse the tx-qualifier. */
23955 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23957 tree save_ccp = current_class_ptr;
23958 tree save_ccr = current_class_ref;
23959 if (memfn && !friend_p && !static_p)
23960 /* DR 1207: 'this' is in scope after the cv-quals. */
23961 inject_this_parameter (current_class_type, cv_quals);
23963 /* If it turned out that this is e.g. a pointer to a
23964 function, we don't want to delay noexcept parsing. */
23965 if (declarator == NULL || declarator->kind != cdk_id)
23966 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23968 /* Parse the exception-specification. */
23969 exception_specification
23970 = cp_parser_exception_specification_opt (parser,
23971 flags);
23973 attrs = cp_parser_std_attribute_spec_seq (parser);
23975 cp_omp_declare_simd_data odsd;
23976 if ((flag_openmp || flag_openmp_simd)
23977 && declarator
23978 && declarator->std_attributes
23979 && declarator->kind == cdk_id)
23981 tree *pa = &declarator->std_attributes;
23982 cp_parser_handle_directive_omp_attributes (parser, pa,
23983 &odsd, false);
23986 /* In here, we handle cases where attribute is used after
23987 the function declaration. For example:
23988 void func (int x) __attribute__((vector(..))); */
23989 tree gnu_attrs = NULL_TREE;
23990 tree requires_clause = NULL_TREE;
23991 late_return
23992 = cp_parser_late_return_type_opt (parser, declarator,
23993 requires_clause);
23995 cp_finalize_omp_declare_simd (parser, &odsd);
23997 /* Parse the virt-specifier-seq. */
23998 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24000 location_t parens_loc = make_location (parens_start,
24001 parens_start,
24002 parens_end);
24003 /* Create the function-declarator. */
24004 declarator = make_call_declarator (declarator,
24005 params,
24006 cv_quals,
24007 virt_specifiers,
24008 ref_qual,
24009 tx_qual,
24010 exception_specification,
24011 late_return,
24012 requires_clause,
24013 attrs,
24014 parens_loc);
24015 declarator->attributes = gnu_attrs;
24016 /* Any subsequent parameter lists are to do with
24017 return type, so are not those of the declared
24018 function. */
24019 parser->default_arg_ok_p = false;
24021 current_class_ptr = save_ccp;
24022 current_class_ref = save_ccr;
24024 /* Restore the state of local_variables_forbidden_p. */
24025 parser->local_variables_forbidden_p
24026 = local_variables_forbidden_p;
24029 /* Remove the function parms from scope. */
24030 pop_bindings_and_leave_scope ();
24032 if (is_declarator)
24033 /* Repeat the main loop. */
24034 continue;
24037 /* If this is the first, we can try a parenthesized
24038 declarator. */
24039 if (first)
24041 bool saved_in_type_id_in_expr_p;
24043 parser->default_arg_ok_p = saved_default_arg_ok_p;
24044 parser->in_declarator_p = saved_in_declarator_p;
24046 open_paren = token;
24047 /* Consume the `('. */
24048 matching_parens parens;
24049 parens.consume_open (parser);
24050 /* Parse the nested declarator. */
24051 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24052 parser->in_type_id_in_expr_p = true;
24053 declarator
24054 = cp_parser_declarator (parser, dcl_kind, flags,
24055 ctor_dtor_or_conv_p,
24056 /*parenthesized_p=*/NULL,
24057 member_p, friend_p,
24058 /*static_p=*/false);
24059 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24060 first = false;
24061 /* Expect a `)'. */
24062 close_paren = cp_lexer_peek_token (parser->lexer);
24063 if (!parens.require_close (parser))
24064 declarator = cp_error_declarator;
24065 if (declarator == cp_error_declarator)
24066 break;
24068 goto handle_declarator;
24070 /* Otherwise, we must be done. */
24071 else
24072 break;
24074 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
24075 && token->type == CPP_OPEN_SQUARE
24076 && !cp_next_tokens_can_be_attribute_p (parser))
24078 /* Parse an array-declarator. */
24079 tree bounds, attrs;
24081 if (ctor_dtor_or_conv_p)
24082 *ctor_dtor_or_conv_p = 0;
24084 open_paren = NULL;
24085 first = false;
24086 parser->default_arg_ok_p = false;
24087 parser->in_declarator_p = true;
24088 /* Consume the `['. */
24089 cp_lexer_consume_token (parser->lexer);
24090 /* Peek at the next token. */
24091 token = cp_lexer_peek_token (parser->lexer);
24092 /* If the next token is `]', then there is no
24093 constant-expression. */
24094 if (token->type != CPP_CLOSE_SQUARE)
24096 bool non_constant_p;
24097 bounds
24098 = cp_parser_constant_expression (parser,
24099 /*allow_non_constant=*/true,
24100 &non_constant_p);
24101 if (!non_constant_p)
24102 /* OK */;
24103 else if (error_operand_p (bounds))
24104 /* Already gave an error. */;
24105 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24106 /* Let compute_array_index_type diagnose this. */;
24107 else if (!parser->in_function_body
24108 || parsing_function_declarator ())
24110 /* Normally, the array bound must be an integral constant
24111 expression. However, as an extension, we allow VLAs
24112 in function scopes as long as they aren't part of a
24113 parameter declaration. */
24114 cp_parser_error (parser,
24115 "array bound is not an integer constant");
24116 bounds = error_mark_node;
24118 else if (processing_template_decl
24119 && !type_dependent_expression_p (bounds))
24121 /* Remember this wasn't a constant-expression. */
24122 bounds = build_nop (TREE_TYPE (bounds), bounds);
24123 TREE_SIDE_EFFECTS (bounds) = 1;
24126 else
24127 bounds = NULL_TREE;
24128 /* Look for the closing `]'. */
24129 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24131 declarator = cp_error_declarator;
24132 break;
24135 attrs = cp_parser_std_attribute_spec_seq (parser);
24136 declarator = make_array_declarator (declarator, bounds);
24137 declarator->std_attributes = attrs;
24139 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
24142 tree qualifying_scope;
24143 tree unqualified_name;
24144 tree attrs;
24145 special_function_kind sfk;
24146 bool abstract_ok;
24147 bool pack_expansion_p = false;
24148 cp_token *declarator_id_start_token;
24150 /* Parse a declarator-id */
24151 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
24152 if (abstract_ok)
24154 cp_parser_parse_tentatively (parser);
24156 /* If we see an ellipsis, we should be looking at a
24157 parameter pack. */
24158 if (token->type == CPP_ELLIPSIS)
24160 /* Consume the `...' */
24161 cp_lexer_consume_token (parser->lexer);
24163 pack_expansion_p = true;
24167 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
24168 unqualified_name
24169 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
24170 qualifying_scope = parser->scope;
24171 if (abstract_ok)
24173 bool okay = false;
24175 if (!unqualified_name && pack_expansion_p)
24177 /* Check whether an error occurred. */
24178 okay = !cp_parser_error_occurred (parser);
24180 /* We already consumed the ellipsis to mark a
24181 parameter pack, but we have no way to report it,
24182 so abort the tentative parse. We will be exiting
24183 immediately anyway. */
24184 cp_parser_abort_tentative_parse (parser);
24186 else
24187 okay = cp_parser_parse_definitely (parser);
24189 if (!okay)
24190 unqualified_name = error_mark_node;
24191 else if (unqualified_name
24192 && (qualifying_scope
24193 || (!identifier_p (unqualified_name))))
24195 cp_parser_error (parser, "expected unqualified-id");
24196 unqualified_name = error_mark_node;
24200 if (!unqualified_name)
24201 return NULL;
24202 if (unqualified_name == error_mark_node)
24204 declarator = cp_error_declarator;
24205 pack_expansion_p = false;
24206 declarator->parameter_pack_p = false;
24207 break;
24210 attrs = cp_parser_std_attribute_spec_seq (parser);
24212 if (qualifying_scope && at_namespace_scope_p ()
24213 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
24215 /* In the declaration of a member of a template class
24216 outside of the class itself, the SCOPE will sometimes
24217 be a TYPENAME_TYPE. For example, given:
24219 template <typename T>
24220 int S<T>::R::i = 3;
24222 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
24223 this context, we must resolve S<T>::R to an ordinary
24224 type, rather than a typename type.
24226 The reason we normally avoid resolving TYPENAME_TYPEs
24227 is that a specialization of `S' might render
24228 `S<T>::R' not a type. However, if `S' is
24229 specialized, then this `i' will not be used, so there
24230 is no harm in resolving the types here. */
24231 tree type;
24233 /* Resolve the TYPENAME_TYPE. */
24234 type = resolve_typename_type (qualifying_scope,
24235 /*only_current_p=*/false);
24236 /* If that failed, the declarator is invalid. */
24237 if (TREE_CODE (type) == TYPENAME_TYPE)
24239 if (typedef_variant_p (type))
24240 error_at (declarator_id_start_token->location,
24241 "cannot define member of dependent typedef "
24242 "%qT", type);
24243 else
24244 error_at (declarator_id_start_token->location,
24245 "%<%T::%E%> is not a type",
24246 TYPE_CONTEXT (qualifying_scope),
24247 TYPE_IDENTIFIER (qualifying_scope));
24249 qualifying_scope = type;
24252 sfk = sfk_none;
24254 if (unqualified_name)
24256 tree class_type;
24258 if (qualifying_scope
24259 && CLASS_TYPE_P (qualifying_scope))
24260 class_type = qualifying_scope;
24261 else
24262 class_type = current_class_type;
24264 if (TREE_CODE (unqualified_name) == TYPE_DECL)
24266 tree name_type = TREE_TYPE (unqualified_name);
24268 if (!class_type || !same_type_p (name_type, class_type))
24270 /* We do not attempt to print the declarator
24271 here because we do not have enough
24272 information about its original syntactic
24273 form. */
24274 cp_parser_error (parser, "invalid declarator");
24275 declarator = cp_error_declarator;
24276 break;
24278 else if (qualifying_scope
24279 && CLASSTYPE_USE_TEMPLATE (name_type))
24281 error_at (declarator_id_start_token->location,
24282 "invalid use of constructor as a template");
24283 inform (declarator_id_start_token->location,
24284 "use %<%T::%D%> instead of %<%T::%D%> to "
24285 "name the constructor in a qualified name",
24286 class_type,
24287 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
24288 class_type, name_type);
24289 declarator = cp_error_declarator;
24290 break;
24292 unqualified_name = constructor_name (class_type);
24295 if (class_type)
24297 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
24298 sfk = sfk_destructor;
24299 else if (identifier_p (unqualified_name)
24300 && IDENTIFIER_CONV_OP_P (unqualified_name))
24301 sfk = sfk_conversion;
24302 else if (/* There's no way to declare a constructor
24303 for an unnamed type, even if the type
24304 got a name for linkage purposes. */
24305 !TYPE_WAS_UNNAMED (class_type)
24306 /* Handle correctly (c++/19200):
24308 struct S {
24309 struct T{};
24310 friend void S(T);
24313 and also:
24315 namespace N {
24316 void S();
24319 struct S {
24320 friend void N::S();
24321 }; */
24322 && (!friend_p || class_type == qualifying_scope)
24323 && constructor_name_p (unqualified_name,
24324 class_type))
24325 sfk = sfk_constructor;
24326 else if (is_overloaded_fn (unqualified_name)
24327 && DECL_CONSTRUCTOR_P (get_first_fn
24328 (unqualified_name)))
24329 sfk = sfk_constructor;
24331 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24332 *ctor_dtor_or_conv_p = -1;
24335 declarator = make_id_declarator (qualifying_scope,
24336 unqualified_name,
24337 sfk, token->location);
24338 declarator->std_attributes = attrs;
24339 declarator->parameter_pack_p = pack_expansion_p;
24341 if (pack_expansion_p)
24342 maybe_warn_variadic_templates ();
24344 /* We're looking for this case in [temp.res]:
24345 A qualified-id is assumed to name a type if [...]
24346 - it is a decl-specifier of the decl-specifier-seq of a
24347 parameter-declaration in a declarator of a function or
24348 function template declaration, ... */
24349 if (cxx_dialect >= cxx20
24350 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24351 && declarator->kind == cdk_id
24352 && !at_class_scope_p ()
24353 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24355 /* ...whose declarator-id is qualified. If it isn't, never
24356 assume the parameters to refer to types. */
24357 if (qualifying_scope == NULL_TREE)
24358 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24359 else
24361 /* Now we have something like
24362 template <typename T> int C::x(S::p);
24363 which can be a function template declaration or a
24364 variable template definition. If name lookup for
24365 the declarator-id C::x finds one or more function
24366 templates, assume S::p to name a type. Otherwise,
24367 don't. */
24368 tree decl
24369 = cp_parser_lookup_name (parser, unqualified_name,
24370 none_type,
24371 /*is_template=*/false,
24372 /*is_namespace=*/false,
24373 /*check_dependency=*/false,
24374 /*ambiguous_decls=*/NULL,
24375 token->location);
24377 if (!is_overloaded_fn (decl)
24378 /* Allow
24379 template<typename T>
24380 A<T>::A(T::type) { } */
24381 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24382 && constructor_name_p (unqualified_name,
24383 qualifying_scope)))
24384 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24389 handle_declarator:;
24390 scope = get_scope_of_declarator (declarator);
24391 if (scope)
24393 /* Any names that appear after the declarator-id for a
24394 member are looked up in the containing scope. */
24395 if (at_function_scope_p ())
24397 /* But declarations with qualified-ids can't appear in a
24398 function. */
24399 cp_parser_error (parser, "qualified-id in declaration");
24400 declarator = cp_error_declarator;
24401 break;
24403 pushed_scope = push_scope (scope);
24405 parser->in_declarator_p = true;
24406 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24407 || (declarator && declarator->kind == cdk_id))
24408 /* Default args are only allowed on function
24409 declarations. */
24410 parser->default_arg_ok_p = saved_default_arg_ok_p;
24411 else
24412 parser->default_arg_ok_p = false;
24414 first = false;
24416 /* We're done. */
24417 else
24418 break;
24421 /* For an abstract declarator, we might wind up with nothing at this
24422 point. That's an error; the declarator is not optional. */
24423 if (!declarator)
24424 cp_parser_error (parser, "expected declarator");
24425 else if (open_paren)
24427 /* Record overly parenthesized declarator so we can give a
24428 diagnostic about confusing decl/expr disambiguation. */
24429 if (declarator->kind == cdk_array)
24431 /* If the open and close parens are on different lines, this
24432 is probably a formatting thing, so ignore. */
24433 expanded_location open = expand_location (open_paren->location);
24434 expanded_location close = expand_location (close_paren->location);
24435 if (open.line != close.line || open.file != close.file)
24436 open_paren = NULL;
24438 if (open_paren)
24439 declarator->parenthesized = make_location (open_paren->location,
24440 open_paren->location,
24441 close_paren->location);
24444 /* If we entered a scope, we must exit it now. */
24445 if (pushed_scope)
24446 pop_scope (pushed_scope);
24448 parser->default_arg_ok_p = saved_default_arg_ok_p;
24449 parser->in_declarator_p = saved_in_declarator_p;
24451 return declarator;
24454 /* Parse a ptr-operator.
24456 ptr-operator:
24457 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24458 * cv-qualifier-seq [opt]
24460 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24461 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24463 GNU Extension:
24465 ptr-operator:
24466 & cv-qualifier-seq [opt]
24468 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24469 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24470 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24471 filled in with the TYPE containing the member. *CV_QUALS is
24472 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24473 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24474 Note that the tree codes returned by this function have nothing
24475 to do with the types of trees that will be eventually be created
24476 to represent the pointer or reference type being parsed. They are
24477 just constants with suggestive names. */
24478 static enum tree_code
24479 cp_parser_ptr_operator (cp_parser* parser,
24480 tree* type,
24481 cp_cv_quals *cv_quals,
24482 tree *attributes)
24484 enum tree_code code = ERROR_MARK;
24485 cp_token *token;
24486 tree attrs = NULL_TREE;
24488 /* Assume that it's not a pointer-to-member. */
24489 *type = NULL_TREE;
24490 /* And that there are no cv-qualifiers. */
24491 *cv_quals = TYPE_UNQUALIFIED;
24493 /* Peek at the next token. */
24494 token = cp_lexer_peek_token (parser->lexer);
24496 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24497 if (token->type == CPP_MULT)
24498 code = INDIRECT_REF;
24499 else if (token->type == CPP_AND)
24500 code = ADDR_EXPR;
24501 else if ((cxx_dialect != cxx98) &&
24502 token->type == CPP_AND_AND) /* C++0x only */
24503 code = NON_LVALUE_EXPR;
24505 if (code != ERROR_MARK)
24507 /* Consume the `*', `&' or `&&'. */
24508 cp_lexer_consume_token (parser->lexer);
24510 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24511 `&', if we are allowing GNU extensions. (The only qualifier
24512 that can legally appear after `&' is `restrict', but that is
24513 enforced during semantic analysis. */
24514 if (code == INDIRECT_REF
24515 || cp_parser_allow_gnu_extensions_p (parser))
24516 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24518 attrs = cp_parser_std_attribute_spec_seq (parser);
24519 if (attributes != NULL)
24520 *attributes = attrs;
24522 else
24524 /* Try the pointer-to-member case. */
24525 cp_parser_parse_tentatively (parser);
24526 /* Look for the optional `::' operator. */
24527 cp_parser_global_scope_opt (parser,
24528 /*current_scope_valid_p=*/false);
24529 /* Look for the nested-name specifier. */
24530 token = cp_lexer_peek_token (parser->lexer);
24531 cp_parser_nested_name_specifier (parser,
24532 /*typename_keyword_p=*/false,
24533 /*check_dependency_p=*/true,
24534 /*type_p=*/false,
24535 /*is_declaration=*/false);
24536 /* If we found it, and the next token is a `*', then we are
24537 indeed looking at a pointer-to-member operator. */
24538 if (!cp_parser_error_occurred (parser)
24539 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24541 /* Indicate that the `*' operator was used. */
24542 code = INDIRECT_REF;
24544 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24545 error_at (token->location, "%qD is a namespace", parser->scope);
24546 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24547 error_at (token->location, "cannot form pointer to member of "
24548 "non-class %q#T", parser->scope);
24549 else
24551 /* The type of which the member is a member is given by the
24552 current SCOPE. */
24553 *type = parser->scope;
24554 /* The next name will not be qualified. */
24555 parser->scope = NULL_TREE;
24556 parser->qualifying_scope = NULL_TREE;
24557 parser->object_scope = NULL_TREE;
24558 /* Look for optional c++11 attributes. */
24559 attrs = cp_parser_std_attribute_spec_seq (parser);
24560 if (attributes != NULL)
24561 *attributes = attrs;
24562 /* Look for the optional cv-qualifier-seq. */
24563 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24566 /* If that didn't work we don't have a ptr-operator. */
24567 if (!cp_parser_parse_definitely (parser))
24568 cp_parser_error (parser, "expected ptr-operator");
24571 return code;
24574 /* Parse an (optional) cv-qualifier-seq.
24576 cv-qualifier-seq:
24577 cv-qualifier cv-qualifier-seq [opt]
24579 cv-qualifier:
24580 const
24581 volatile
24583 GNU Extension:
24585 cv-qualifier:
24586 __restrict__
24588 Returns a bitmask representing the cv-qualifiers. */
24590 static cp_cv_quals
24591 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24593 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24595 while (true)
24597 cp_token *token;
24598 cp_cv_quals cv_qualifier;
24600 /* Peek at the next token. */
24601 token = cp_lexer_peek_token (parser->lexer);
24602 /* See if it's a cv-qualifier. */
24603 switch (token->keyword)
24605 case RID_CONST:
24606 cv_qualifier = TYPE_QUAL_CONST;
24607 break;
24609 case RID_VOLATILE:
24610 cv_qualifier = TYPE_QUAL_VOLATILE;
24611 break;
24613 case RID_RESTRICT:
24614 cv_qualifier = TYPE_QUAL_RESTRICT;
24615 break;
24617 default:
24618 cv_qualifier = TYPE_UNQUALIFIED;
24619 break;
24622 if (!cv_qualifier)
24623 break;
24625 if (cv_quals & cv_qualifier)
24627 gcc_rich_location richloc (token->location);
24628 richloc.add_fixit_remove ();
24629 error_at (&richloc, "duplicate cv-qualifier");
24630 cp_lexer_purge_token (parser->lexer);
24632 else
24634 cp_lexer_consume_token (parser->lexer);
24635 cv_quals |= cv_qualifier;
24639 return cv_quals;
24642 /* Parse an (optional) ref-qualifier
24644 ref-qualifier:
24648 Returns cp_ref_qualifier representing ref-qualifier. */
24650 static cp_ref_qualifier
24651 cp_parser_ref_qualifier_opt (cp_parser* parser)
24653 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24655 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24656 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24657 return ref_qual;
24659 while (true)
24661 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24662 cp_token *token = cp_lexer_peek_token (parser->lexer);
24664 switch (token->type)
24666 case CPP_AND:
24667 curr_ref_qual = REF_QUAL_LVALUE;
24668 break;
24670 case CPP_AND_AND:
24671 curr_ref_qual = REF_QUAL_RVALUE;
24672 break;
24674 default:
24675 curr_ref_qual = REF_QUAL_NONE;
24676 break;
24679 if (!curr_ref_qual)
24680 break;
24681 else if (ref_qual)
24683 error_at (token->location, "multiple ref-qualifiers");
24684 cp_lexer_purge_token (parser->lexer);
24686 else
24688 ref_qual = curr_ref_qual;
24689 cp_lexer_consume_token (parser->lexer);
24693 return ref_qual;
24696 /* Parse an optional tx-qualifier.
24698 tx-qualifier:
24699 transaction_safe
24700 transaction_safe_dynamic */
24702 static tree
24703 cp_parser_tx_qualifier_opt (cp_parser *parser)
24705 cp_token *token = cp_lexer_peek_token (parser->lexer);
24706 if (token->type == CPP_NAME)
24708 tree name = token->u.value;
24709 const char *p = IDENTIFIER_POINTER (name);
24710 const int len = strlen ("transaction_safe");
24711 if (startswith (p, "transaction_safe"))
24713 p += len;
24714 if (*p == '\0'
24715 || !strcmp (p, "_dynamic"))
24717 cp_lexer_consume_token (parser->lexer);
24718 if (!flag_tm)
24720 error ("%qE requires %<-fgnu-tm%>", name);
24721 return NULL_TREE;
24723 else
24724 return name;
24728 return NULL_TREE;
24731 /* Parse an (optional) virt-specifier-seq.
24733 virt-specifier-seq:
24734 virt-specifier virt-specifier-seq [opt]
24736 virt-specifier:
24737 override
24738 final
24740 Returns a bitmask representing the virt-specifiers. */
24742 static cp_virt_specifiers
24743 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24745 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24747 while (true)
24749 cp_token *token;
24750 cp_virt_specifiers virt_specifier;
24752 /* Peek at the next token. */
24753 token = cp_lexer_peek_token (parser->lexer);
24754 /* See if it's a virt-specifier-qualifier. */
24755 if (token->type != CPP_NAME)
24756 break;
24757 if (id_equal (token->u.value, "override"))
24759 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24760 virt_specifier = VIRT_SPEC_OVERRIDE;
24762 else if (id_equal (token->u.value, "final"))
24764 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24765 virt_specifier = VIRT_SPEC_FINAL;
24767 else if (id_equal (token->u.value, "__final"))
24769 virt_specifier = VIRT_SPEC_FINAL;
24771 else
24772 break;
24774 if (virt_specifiers & virt_specifier)
24776 gcc_rich_location richloc (token->location);
24777 richloc.add_fixit_remove ();
24778 error_at (&richloc, "duplicate virt-specifier");
24779 cp_lexer_purge_token (parser->lexer);
24781 else
24783 cp_lexer_consume_token (parser->lexer);
24784 virt_specifiers |= virt_specifier;
24787 return virt_specifiers;
24790 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24791 is in scope even though it isn't real. */
24793 void
24794 inject_this_parameter (tree ctype, cp_cv_quals quals)
24796 tree this_parm;
24798 if (current_class_ptr)
24800 /* We don't clear this between NSDMIs. Is it already what we want? */
24801 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24802 if (DECL_P (current_class_ptr)
24803 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24804 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24805 && cp_type_quals (type) == quals)
24806 return;
24809 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24810 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24811 current_class_ptr = NULL_TREE;
24812 current_class_ref
24813 = cp_build_fold_indirect_ref (this_parm);
24814 current_class_ptr = this_parm;
24817 /* Return true iff our current scope is a non-static data member
24818 initializer. */
24820 bool
24821 parsing_nsdmi (void)
24823 /* We recognize NSDMI context by the context-less 'this' pointer set up
24824 by the function above. */
24825 if (current_class_ptr
24826 && TREE_CODE (current_class_ptr) == PARM_DECL
24827 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24828 return true;
24829 return false;
24832 /* True if we're parsing a function declarator. */
24834 bool
24835 parsing_function_declarator ()
24837 /* this_entity is NULL for a function parameter scope while parsing the
24838 declarator; it is set when parsing the body of the function. */
24839 return (current_binding_level->kind == sk_function_parms
24840 && !current_binding_level->this_entity);
24843 /* Parse a late-specified return type, if any. This is not a separate
24844 non-terminal, but part of a function declarator, which looks like
24846 -> trailing-type-specifier-seq abstract-declarator(opt)
24848 Returns the type indicated by the type-id.
24850 In addition to this, parse any queued up #pragma omp declare simd
24851 clauses, and #pragma acc routine clauses.
24853 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24854 function. */
24856 static tree
24857 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24858 tree& requires_clause)
24860 cp_token *token;
24861 tree type = NULL_TREE;
24862 bool declare_simd_p = (parser->omp_declare_simd
24863 && declarator
24864 && declarator->kind == cdk_id);
24866 bool oacc_routine_p = (parser->oacc_routine
24867 && declarator
24868 && declarator->kind == cdk_id);
24870 /* Peek at the next token. */
24871 token = cp_lexer_peek_token (parser->lexer);
24872 /* A late-specified return type is indicated by an initial '->'. */
24873 if (token->type != CPP_DEREF
24874 && token->keyword != RID_REQUIRES
24875 && !(token->type == CPP_NAME
24876 && token->u.value == ridpointers[RID_REQUIRES])
24877 && !(declare_simd_p || oacc_routine_p))
24878 return NULL_TREE;
24880 if (token->type == CPP_DEREF)
24882 /* Consume the ->. */
24883 cp_lexer_consume_token (parser->lexer);
24885 type = cp_parser_trailing_type_id (parser);
24888 /* Function declarations may be followed by a trailing
24889 requires-clause. */
24890 requires_clause = cp_parser_requires_clause_opt (parser, false);
24892 if (declare_simd_p)
24893 declarator->attributes
24894 = cp_parser_late_parsing_omp_declare_simd (parser,
24895 declarator->attributes);
24896 if (oacc_routine_p)
24897 declarator->attributes
24898 = cp_parser_late_parsing_oacc_routine (parser,
24899 declarator->attributes);
24901 return type;
24904 /* Parse a declarator-id.
24906 declarator-id:
24907 id-expression
24908 :: [opt] nested-name-specifier [opt] type-name
24910 In the `id-expression' case, the value returned is as for
24911 cp_parser_id_expression if the id-expression was an unqualified-id.
24912 If the id-expression was a qualified-id, then a SCOPE_REF is
24913 returned. The first operand is the scope (either a NAMESPACE_DECL
24914 or TREE_TYPE), but the second is still just a representation of an
24915 unqualified-id. */
24917 static tree
24918 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24920 tree id;
24921 /* The expression must be an id-expression. Assume that qualified
24922 names are the names of types so that:
24924 template <class T>
24925 int S<T>::R::i = 3;
24927 will work; we must treat `S<T>::R' as the name of a type.
24928 Similarly, assume that qualified names are templates, where
24929 required, so that:
24931 template <class T>
24932 int S<T>::R<T>::i = 3;
24934 will work, too. */
24935 id = cp_parser_id_expression (parser,
24936 /*template_keyword_p=*/false,
24937 /*check_dependency_p=*/false,
24938 /*template_p=*/NULL,
24939 /*declarator_p=*/true,
24940 optional_p);
24941 if (id && BASELINK_P (id))
24942 id = BASELINK_FUNCTIONS (id);
24943 return id;
24946 /* Parse a type-id.
24948 type-id:
24949 type-specifier-seq abstract-declarator [opt]
24951 The parser flags FLAGS is used to control type-specifier parsing.
24953 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24955 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24956 i.e. we've just seen "->".
24958 Returns the TYPE specified. */
24960 static tree
24961 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24962 bool is_template_arg, bool is_trailing_return,
24963 location_t *type_location)
24965 cp_decl_specifier_seq type_specifier_seq;
24966 cp_declarator *abstract_declarator;
24968 /* Parse the type-specifier-seq. */
24969 cp_parser_type_specifier_seq (parser, flags,
24970 /*is_declaration=*/false,
24971 is_trailing_return,
24972 &type_specifier_seq);
24973 if (type_location)
24974 *type_location = type_specifier_seq.locations[ds_type_spec];
24976 if (is_template_arg && type_specifier_seq.type
24977 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24978 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24979 /* A bare template name as a template argument is a template template
24980 argument, not a placeholder, so fail parsing it as a type argument. */
24982 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24983 cp_parser_simulate_error (parser);
24984 return error_mark_node;
24986 if (type_specifier_seq.type == error_mark_node)
24987 return error_mark_node;
24989 /* There might or might not be an abstract declarator. */
24990 cp_parser_parse_tentatively (parser);
24991 /* Look for the declarator. */
24992 abstract_declarator
24993 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24994 CP_PARSER_FLAGS_NONE, NULL,
24995 /*parenthesized_p=*/NULL,
24996 /*member_p=*/false,
24997 /*friend_p=*/false,
24998 /*static_p=*/false);
24999 /* Check to see if there really was a declarator. */
25000 if (!cp_parser_parse_definitely (parser))
25001 abstract_declarator = NULL;
25003 bool auto_typeid_ok = false;
25004 /* The concepts TS allows 'auto' as a type-id. */
25005 if (flag_concepts_ts)
25006 auto_typeid_ok = !parser->in_type_id_in_expr_p;
25007 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
25008 outside the template-argument-list context here only for the sake of
25009 diagnostic: grokdeclarator then can emit a better error message for
25010 e.g. using T = auto. */
25011 else if (flag_concepts)
25012 auto_typeid_ok = (!parser->in_type_id_in_expr_p
25013 && !parser->in_template_argument_list_p);
25015 if (type_specifier_seq.type
25016 && !auto_typeid_ok
25017 /* None of the valid uses of 'auto' in C++14 involve the type-id
25018 nonterminal, but it is valid in a trailing-return-type. */
25019 && !(cxx_dialect >= cxx14 && is_trailing_return))
25020 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
25022 /* A type-id with type 'auto' is only ok if the abstract declarator
25023 is a function declarator with a late-specified return type.
25025 A type-id with 'auto' is also valid in a trailing-return-type
25026 in a compound-requirement. */
25027 if (abstract_declarator
25028 && abstract_declarator->kind == cdk_function
25029 && abstract_declarator->u.function.late_return_type)
25030 /* OK */;
25031 else if (parser->in_result_type_constraint_p)
25032 /* OK */;
25033 else
25035 if (!cp_parser_simulate_error (parser))
25037 location_t loc = type_specifier_seq.locations[ds_type_spec];
25038 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25040 auto_diagnostic_group g;
25041 gcc_rich_location richloc (loc);
25042 richloc.add_fixit_insert_after ("<>");
25043 error_at (&richloc, "missing template arguments after %qE",
25044 tmpl);
25045 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
25046 tmpl);
25048 else if (parser->in_template_argument_list_p)
25049 error_at (loc, "%qT not permitted in template argument",
25050 auto_node);
25051 else
25052 error_at (loc, "invalid use of %qT", auto_node);
25054 return error_mark_node;
25058 return groktypename (&type_specifier_seq, abstract_declarator,
25059 is_template_arg);
25062 /* Wrapper for cp_parser_type_id_1. */
25064 static tree
25065 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
25066 location_t *type_location)
25068 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
25071 /* Wrapper for cp_parser_type_id_1. */
25073 static tree
25074 cp_parser_template_type_arg (cp_parser *parser)
25076 const char *saved_message = parser->type_definition_forbidden_message;
25077 parser->type_definition_forbidden_message
25078 = G_("types may not be defined in template arguments");
25079 tree r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE,
25080 /*is_template_arg=*/true,
25081 /*is_trailing_return=*/false, nullptr);
25082 parser->type_definition_forbidden_message = saved_message;
25083 /* cp_parser_type_id_1 checks for auto, but only for
25084 ->auto_is_implicit_function_template_parm_p. */
25085 if (cxx_dialect >= cxx14 && !flag_concepts_ts && type_uses_auto (r))
25087 error ("invalid use of %<auto%> in template argument");
25088 r = error_mark_node;
25090 return r;
25093 /* Wrapper for cp_parser_type_id_1. */
25095 static tree
25096 cp_parser_trailing_type_id (cp_parser *parser)
25098 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
25099 false, true, NULL);
25102 /* Parse a type-specifier-seq.
25104 type-specifier-seq:
25105 type-specifier type-specifier-seq [opt]
25107 GNU extension:
25109 type-specifier-seq:
25110 attributes type-specifier-seq [opt]
25112 The parser flags FLAGS is used to control type-specifier parsing.
25114 If IS_DECLARATION is true, we are at the start of a "condition" or
25115 exception-declaration, so we might be followed by a declarator-id.
25117 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25118 i.e. we've just seen "->".
25120 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
25122 static void
25123 cp_parser_type_specifier_seq (cp_parser* parser,
25124 cp_parser_flags flags,
25125 bool is_declaration,
25126 bool is_trailing_return,
25127 cp_decl_specifier_seq *type_specifier_seq)
25129 bool seen_type_specifier = false;
25130 cp_token *start_token = NULL;
25132 /* Clear the TYPE_SPECIFIER_SEQ. */
25133 clear_decl_specs (type_specifier_seq);
25135 flags |= CP_PARSER_FLAGS_OPTIONAL;
25136 /* In the context of a trailing return type, enum E { } is an
25137 elaborated-type-specifier followed by a function-body, not an
25138 enum-specifier. */
25139 if (is_trailing_return)
25140 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
25142 /* Parse the type-specifiers and attributes. */
25143 while (true)
25145 tree type_specifier;
25146 bool is_cv_qualifier;
25148 /* Check for attributes first. */
25149 if (cp_next_tokens_can_be_attribute_p (parser))
25151 /* GNU attributes at the end of a declaration apply to the
25152 declaration as a whole, not to the trailing return type. So look
25153 ahead to see if these attributes are at the end. */
25154 if (seen_type_specifier && is_trailing_return
25155 && cp_next_tokens_can_be_gnu_attribute_p (parser))
25157 size_t n = cp_parser_skip_attributes_opt (parser, 1);
25158 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
25159 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
25160 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
25161 break;
25163 type_specifier_seq->attributes
25164 = attr_chainon (type_specifier_seq->attributes,
25165 cp_parser_attributes_opt (parser));
25166 continue;
25169 /* record the token of the beginning of the type specifier seq,
25170 for error reporting purposes*/
25171 if (!start_token)
25172 start_token = cp_lexer_peek_token (parser->lexer);
25174 /* Look for the type-specifier. */
25175 type_specifier = cp_parser_type_specifier (parser,
25176 flags,
25177 type_specifier_seq,
25178 /*is_declaration=*/false,
25179 NULL,
25180 &is_cv_qualifier);
25181 if (!type_specifier)
25183 /* If the first type-specifier could not be found, this is not a
25184 type-specifier-seq at all. */
25185 if (!seen_type_specifier)
25187 /* Set in_declarator_p to avoid skipping to the semicolon. */
25188 int in_decl = parser->in_declarator_p;
25189 parser->in_declarator_p = true;
25191 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
25192 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
25193 cp_parser_error (parser, "expected type-specifier");
25195 parser->in_declarator_p = in_decl;
25197 type_specifier_seq->type = error_mark_node;
25198 return;
25200 /* If subsequent type-specifiers could not be found, the
25201 type-specifier-seq is complete. */
25202 break;
25205 seen_type_specifier = true;
25206 /* The standard says that a condition can be:
25208 type-specifier-seq declarator = assignment-expression
25210 However, given:
25212 struct S {};
25213 if (int S = ...)
25215 we should treat the "S" as a declarator, not as a
25216 type-specifier. The standard doesn't say that explicitly for
25217 type-specifier-seq, but it does say that for
25218 decl-specifier-seq in an ordinary declaration. Perhaps it
25219 would be clearer just to allow a decl-specifier-seq here, and
25220 then add a semantic restriction that if any decl-specifiers
25221 that are not type-specifiers appear, the program is invalid. */
25222 if (is_declaration && !is_cv_qualifier)
25223 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
25227 /* Return whether the function currently being declared has an associated
25228 template parameter list. */
25230 static bool
25231 function_being_declared_is_template_p (cp_parser* parser)
25233 if (!current_template_parms || processing_template_parmlist)
25234 return false;
25236 if (parser->implicit_template_scope)
25237 return true;
25239 if (at_class_scope_p ()
25240 && TYPE_BEING_DEFINED (current_class_type))
25241 return parser->num_template_parameter_lists != 0;
25243 return ((int) parser->num_template_parameter_lists > template_class_depth
25244 (current_class_type));
25247 /* Parse a parameter-declaration-clause.
25249 parameter-declaration-clause:
25250 parameter-declaration-list [opt] ... [opt]
25251 parameter-declaration-list , ...
25253 The parser flags FLAGS is used to control type-specifier parsing.
25255 Returns a representation for the parameter declarations. A return
25256 value of NULL indicates a parameter-declaration-clause consisting
25257 only of an ellipsis. */
25259 static tree
25260 cp_parser_parameter_declaration_clause (cp_parser* parser,
25261 cp_parser_flags flags)
25263 tree parameters;
25264 cp_token *token;
25265 bool ellipsis_p;
25267 auto cleanup = make_temp_override
25268 (parser->auto_is_implicit_function_template_parm_p);
25270 if (!processing_specialization
25271 && !processing_template_parmlist
25272 && !processing_explicit_instantiation
25273 /* default_arg_ok_p tracks whether this is a parameter-clause for an
25274 actual function or a random abstract declarator. */
25275 && parser->default_arg_ok_p)
25276 if (!current_function_decl
25277 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
25278 parser->auto_is_implicit_function_template_parm_p = true;
25280 /* Peek at the next token. */
25281 token = cp_lexer_peek_token (parser->lexer);
25282 /* Check for trivial parameter-declaration-clauses. */
25283 if (token->type == CPP_ELLIPSIS)
25285 /* Consume the `...' token. */
25286 cp_lexer_consume_token (parser->lexer);
25287 return NULL_TREE;
25289 else if (token->type == CPP_CLOSE_PAREN)
25290 /* There are no parameters. */
25291 return void_list_node;
25292 /* Check for `(void)', too, which is a special case. */
25293 else if (token->keyword == RID_VOID
25294 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25295 == CPP_CLOSE_PAREN))
25297 /* Consume the `void' token. */
25298 cp_lexer_consume_token (parser->lexer);
25299 /* There are no parameters. */
25300 return explicit_void_list_node;
25303 /* A vector of parameters that haven't been pushed yet. */
25304 auto_vec<tree> pending_decls;
25306 /* Parse the parameter-declaration-list. */
25307 parameters = cp_parser_parameter_declaration_list (parser, flags,
25308 &pending_decls);
25309 /* If a parse error occurred while parsing the
25310 parameter-declaration-list, then the entire
25311 parameter-declaration-clause is erroneous. */
25312 if (parameters == error_mark_node)
25313 return NULL_TREE;
25315 /* Peek at the next token. */
25316 token = cp_lexer_peek_token (parser->lexer);
25317 /* If it's a `,', the clause should terminate with an ellipsis. */
25318 if (token->type == CPP_COMMA)
25320 /* Consume the `,'. */
25321 cp_lexer_consume_token (parser->lexer);
25322 /* Expect an ellipsis. */
25323 ellipsis_p
25324 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
25326 /* It might also be `...' if the optional trailing `,' was
25327 omitted. */
25328 else if (token->type == CPP_ELLIPSIS)
25330 /* Consume the `...' token. */
25331 cp_lexer_consume_token (parser->lexer);
25332 /* And remember that we saw it. */
25333 ellipsis_p = true;
25335 else
25336 ellipsis_p = false;
25338 /* A valid parameter-declaration-clause can only be followed by a ')'.
25339 So it's time to push all the parameters we have seen now that we
25340 know we have a valid declaration. Note that here we may not have
25341 committed yet, nor should we. Pushing here will detect the error
25342 of redefining a parameter. */
25343 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25345 for (tree p : pending_decls)
25346 pushdecl (p);
25348 /* Delayed checking of auto parameters. */
25349 if (!parser->auto_is_implicit_function_template_parm_p
25350 && cxx_dialect >= cxx14)
25351 for (tree p = parameters; p; p = TREE_CHAIN (p))
25352 if (type_uses_auto (TREE_TYPE (TREE_VALUE (p))))
25354 error_at (location_of (TREE_VALUE (p)),
25355 "%<auto%> parameter not permitted in this context");
25356 TREE_TYPE (TREE_VALUE (p)) = error_mark_node;
25360 /* Finish the parameter list. */
25361 if (!ellipsis_p)
25362 parameters = chainon (parameters, void_list_node);
25364 return parameters;
25367 /* Parse a parameter-declaration-list.
25369 parameter-declaration-list:
25370 parameter-declaration
25371 parameter-declaration-list , parameter-declaration
25373 The parser flags FLAGS is used to control type-specifier parsing.
25374 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25376 Returns a representation of the parameter-declaration-list, as for
25377 cp_parser_parameter_declaration_clause. However, the
25378 `void_list_node' is never appended to the list. */
25380 static tree
25381 cp_parser_parameter_declaration_list (cp_parser* parser,
25382 cp_parser_flags flags,
25383 auto_vec<tree> *pending_decls)
25385 tree parameters = NULL_TREE;
25386 tree *tail = &parameters;
25387 bool saved_in_unbraced_linkage_specification_p;
25388 int index = 0;
25390 /* The special considerations that apply to a function within an
25391 unbraced linkage specifications do not apply to the parameters
25392 to the function. */
25393 saved_in_unbraced_linkage_specification_p
25394 = parser->in_unbraced_linkage_specification_p;
25395 parser->in_unbraced_linkage_specification_p = false;
25397 /* Look for more parameters. */
25398 while (true)
25400 cp_parameter_declarator *parameter;
25401 tree decl = error_mark_node;
25402 bool parenthesized_p = false;
25404 /* Parse the parameter. */
25405 parameter
25406 = cp_parser_parameter_declaration (parser, flags,
25407 /*template_parm_p=*/false,
25408 &parenthesized_p);
25410 /* We don't know yet if the enclosing context is unavailable or deprecated,
25411 so wait and deal with it in grokparms if appropriate. */
25412 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25414 if (parameter && !cp_parser_error_occurred (parser))
25416 decl = grokdeclarator (parameter->declarator,
25417 &parameter->decl_specifiers,
25418 PARM,
25419 parameter->default_argument != NULL_TREE,
25420 &parameter->decl_specifiers.attributes);
25421 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25422 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25425 deprecated_state = DEPRECATED_NORMAL;
25427 /* If a parse error occurred parsing the parameter declaration,
25428 then the entire parameter-declaration-list is erroneous. */
25429 if (decl == error_mark_node)
25431 parameters = error_mark_node;
25432 break;
25435 if (parameter->decl_specifiers.attributes)
25436 cplus_decl_attributes (&decl,
25437 parameter->decl_specifiers.attributes,
25439 if (DECL_NAME (decl))
25441 /* We cannot always pushdecl while parsing tentatively because
25442 it may have side effects and we can't be sure yet if we're
25443 parsing a declaration, e.g.:
25445 S foo(int(x), int(x), int{x});
25447 where it's not clear if we're dealing with a constructor call
25448 or a function declaration until we've seen the last argument
25449 which breaks it up.
25450 It's safe to pushdecl so long as it doesn't result in a clash
25451 with an already-pushed parameter. But we don't delay pushing
25452 different parameters to handle
25454 S foo(int(i), decltype(i) j = 42);
25456 which is valid. */
25457 if (pending_decls
25458 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25459 /* See if PARAMETERS already contains a parameter with the same
25460 DECL_NAME as DECL. */
25461 && [parameters, decl] {
25462 for (tree p = parameters; p; p = TREE_CHAIN (p))
25463 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25464 return true;
25465 return false;
25466 }())
25467 pending_decls->safe_push (decl);
25468 else
25469 decl = pushdecl (decl);
25472 if (decl != error_mark_node)
25474 retrofit_lang_decl (decl);
25475 DECL_PARM_INDEX (decl) = ++index;
25476 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25479 /* Add the new parameter to the list. */
25480 *tail = build_tree_list (parameter->default_argument, decl);
25481 tail = &TREE_CHAIN (*tail);
25483 /* If the parameters were parenthesized, it's the case of
25484 T foo(X(x)) which looks like a variable definition but
25485 is a function declaration. */
25486 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25487 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25489 /* Peek at the next token. */
25490 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25491 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25492 /* These are for Objective-C++ */
25493 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25494 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25495 /* The parameter-declaration-list is complete. */
25496 break;
25497 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25499 cp_token *token;
25501 /* Peek at the next token. */
25502 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25503 /* If it's an ellipsis, then the list is complete. */
25504 if (token->type == CPP_ELLIPSIS)
25505 break;
25506 /* Otherwise, there must be more parameters. Consume the
25507 `,'. */
25508 cp_lexer_consume_token (parser->lexer);
25509 /* When parsing something like:
25511 int i(float f, double d)
25513 we can tell after seeing the declaration for "f" that we
25514 are not looking at an initialization of a variable "i",
25515 but rather at the declaration of a function "i".
25517 Due to the fact that the parsing of template arguments
25518 (as specified to a template-id) requires backtracking we
25519 cannot use this technique when inside a template argument
25520 list. */
25521 if (!parser->in_template_argument_list_p
25522 && !parser->in_type_id_in_expr_p
25523 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25524 /* However, a parameter-declaration of the form
25525 "float(f)" (which is a valid declaration of a
25526 parameter "f") can also be interpreted as an
25527 expression (the conversion of "f" to "float"). */
25528 && !parenthesized_p)
25529 cp_parser_commit_to_tentative_parse (parser);
25531 else
25533 cp_parser_error (parser, "expected %<,%> or %<...%>");
25534 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25535 cp_parser_skip_to_closing_parenthesis (parser,
25536 /*recovering=*/true,
25537 /*or_comma=*/false,
25538 /*consume_paren=*/false);
25539 break;
25543 parser->in_unbraced_linkage_specification_p
25544 = saved_in_unbraced_linkage_specification_p;
25546 /* Reset implicit_template_scope if we are about to leave the function
25547 parameter list that introduced it. Note that for out-of-line member
25548 definitions, there will be one or more class scopes before we get to
25549 the template parameter scope. */
25551 if (cp_binding_level *its = parser->implicit_template_scope)
25552 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25554 while (maybe_its->kind == sk_class)
25555 maybe_its = maybe_its->level_chain;
25556 if (maybe_its == its)
25558 parser->implicit_template_parms = 0;
25559 parser->implicit_template_scope = 0;
25563 return parameters;
25566 /* Parse a parameter declaration.
25568 parameter-declaration:
25569 decl-specifier-seq ... [opt] declarator
25570 decl-specifier-seq declarator = assignment-expression
25571 decl-specifier-seq ... [opt] abstract-declarator [opt]
25572 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25574 The parser flags FLAGS is used to control type-specifier parsing.
25576 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25577 declares a template parameter. (In that case, a non-nested `>'
25578 token encountered during the parsing of the assignment-expression
25579 is not interpreted as a greater-than operator.)
25581 Returns a representation of the parameter, or NULL if an error
25582 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25583 true iff the declarator is of the form "(p)". */
25585 static cp_parameter_declarator *
25586 cp_parser_parameter_declaration (cp_parser *parser,
25587 cp_parser_flags flags,
25588 bool template_parm_p,
25589 bool *parenthesized_p)
25591 int declares_class_or_enum;
25592 cp_decl_specifier_seq decl_specifiers;
25593 cp_declarator *declarator;
25594 tree default_argument;
25595 cp_token *token = NULL, *declarator_token_start = NULL;
25596 const char *saved_message;
25597 bool template_parameter_pack_p = false;
25599 /* In a template parameter, `>' is not an operator.
25601 [temp.param]
25603 When parsing a default template-argument for a non-type
25604 template-parameter, the first non-nested `>' is taken as the end
25605 of the template parameter-list rather than a greater-than
25606 operator. */
25608 /* Type definitions may not appear in parameter types. */
25609 saved_message = parser->type_definition_forbidden_message;
25610 parser->type_definition_forbidden_message
25611 = G_("types may not be defined in parameter types");
25613 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25614 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25615 (current_template_parms)) : 0);
25617 /* Parse the declaration-specifiers. */
25618 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25619 cp_parser_decl_specifier_seq (parser,
25620 flags | CP_PARSER_FLAGS_PARAMETER,
25621 &decl_specifiers,
25622 &declares_class_or_enum);
25624 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25625 type-constraint opt auto can be used as a decl-specifier of the
25626 decl-specifier-seq of a parameter-declaration of a function declaration
25627 or lambda-expression..." but we must not synthesize an implicit template
25628 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25629 we want to synthesize only the first auto. */
25630 auto cleanup = make_temp_override
25631 (parser->auto_is_implicit_function_template_parm_p, false);
25633 /* Complain about missing 'typename' or other invalid type names. */
25634 if (!decl_specifiers.any_type_specifiers_p
25635 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25636 decl_specifiers.type = error_mark_node;
25638 /* If an error occurred, there's no reason to attempt to parse the
25639 rest of the declaration. */
25640 if (cp_parser_error_occurred (parser))
25642 parser->type_definition_forbidden_message = saved_message;
25643 return NULL;
25646 /* Peek at the next token. */
25647 token = cp_lexer_peek_token (parser->lexer);
25649 /* If the next token is a `)', `,', `=', `>', or `...', then there
25650 is no declarator. However, when variadic templates are enabled,
25651 there may be a declarator following `...'. */
25652 if (token->type == CPP_CLOSE_PAREN
25653 || token->type == CPP_COMMA
25654 || token->type == CPP_EQ
25655 || token->type == CPP_GREATER)
25657 declarator = NULL;
25658 if (parenthesized_p)
25659 *parenthesized_p = false;
25661 /* Otherwise, there should be a declarator. */
25662 else
25664 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25665 parser->default_arg_ok_p = false;
25667 /* After seeing a decl-specifier-seq, if the next token is not a
25668 "(" or "{", there is no possibility that the code is a valid
25669 expression. Therefore, if parsing tentatively, we commit at
25670 this point. */
25671 if (!parser->in_template_argument_list_p
25672 /* In an expression context, having seen:
25674 (int((char ...
25676 we cannot be sure whether we are looking at a
25677 function-type (taking a "char" as a parameter) or a cast
25678 of some object of type "char" to "int". */
25679 && !parser->in_type_id_in_expr_p
25680 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25681 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25685 if (decl_specifiers.type
25686 && template_placeholder_p (decl_specifiers.type))
25687 /* This is a CTAD expression, not a parameter declaration. */
25688 cp_parser_simulate_error (parser);
25690 else
25691 cp_parser_commit_to_tentative_parse (parser);
25693 /* Parse the declarator. */
25694 declarator_token_start = token;
25695 declarator = cp_parser_declarator (parser,
25696 CP_PARSER_DECLARATOR_EITHER,
25697 CP_PARSER_FLAGS_NONE,
25698 /*ctor_dtor_or_conv_p=*/NULL,
25699 parenthesized_p,
25700 /*member_p=*/false,
25701 /*friend_p=*/false,
25702 /*static_p=*/false);
25703 parser->default_arg_ok_p = saved_default_arg_ok_p;
25704 /* After the declarator, allow more attributes. */
25705 decl_specifiers.attributes
25706 = attr_chainon (decl_specifiers.attributes,
25707 cp_parser_attributes_opt (parser));
25709 /* If the declarator is a template parameter pack, remember that and
25710 clear the flag in the declarator itself so we don't get errors
25711 from grokdeclarator. */
25712 if (template_parm_p && declarator && declarator->parameter_pack_p)
25714 declarator->parameter_pack_p = false;
25715 template_parameter_pack_p = true;
25719 /* If the next token is an ellipsis, and we have not seen a declarator
25720 name, and if either the type of the declarator contains parameter
25721 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25722 for, eg, abbreviated integral type names), then we actually have a
25723 parameter pack expansion expression. Otherwise, leave the ellipsis
25724 for a C-style variadic function. */
25725 token = cp_lexer_peek_token (parser->lexer);
25727 bool xobj_param_p
25728 = decl_spec_seq_has_spec_p (&decl_specifiers, ds_this);
25729 if (xobj_param_p && template_parm_p)
25731 error_at (decl_specifiers.locations[ds_this],
25732 "%<this%> specifier in template parameter declaration");
25733 xobj_param_p = false;
25734 decl_specifiers.locations[ds_this] = 0;
25737 if (xobj_param_p
25738 && ((declarator && declarator->parameter_pack_p)
25739 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)))
25741 location_t xobj_param
25742 = make_location (decl_specifiers.locations[ds_this],
25743 decl_spec_token_start->location,
25744 input_location);
25745 error_at (xobj_param,
25746 "an explicit object parameter cannot "
25747 "be a function parameter pack");
25748 /* Suppress errors that occur down the line. */
25749 if (declarator)
25750 declarator->parameter_pack_p = false;
25753 /* If a function parameter pack was specified and an implicit template
25754 parameter was introduced during cp_parser_parameter_declaration,
25755 change any implicit parameters introduced into packs. */
25756 if (parser->implicit_template_parms
25757 && ((token->type == CPP_ELLIPSIS
25758 && declarator_can_be_parameter_pack (declarator))
25759 || (declarator && declarator->parameter_pack_p)))
25761 int latest_template_parm_idx = TREE_VEC_LENGTH
25762 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25764 if (latest_template_parm_idx != template_parm_idx)
25765 decl_specifiers.type = convert_generic_types_to_packs
25766 (decl_specifiers.type,
25767 template_parm_idx, latest_template_parm_idx);
25770 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25772 tree type = decl_specifiers.type;
25774 if (type && DECL_P (type))
25775 type = TREE_TYPE (type);
25777 if (((type
25778 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25779 && (template_parm_p || uses_parameter_packs (type)))
25780 || (!type && template_parm_p))
25781 && declarator_can_be_parameter_pack (declarator))
25783 /* Consume the `...'. */
25784 cp_lexer_consume_token (parser->lexer);
25785 maybe_warn_variadic_templates ();
25787 /* Build a pack expansion type */
25788 if (template_parm_p)
25789 template_parameter_pack_p = true;
25790 else if (declarator)
25791 declarator->parameter_pack_p = true;
25792 else
25793 decl_specifiers.type = make_pack_expansion (type);
25797 /* The restriction on defining new types applies only to the type
25798 of the parameter, not to the default argument. */
25799 parser->type_definition_forbidden_message = saved_message;
25800 cp_token *eq_token = NULL;
25801 /* If the next token is `=', then process a default argument. */
25802 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25804 tree type = decl_specifiers.type;
25805 token = cp_lexer_peek_token (parser->lexer);
25806 /* Used for diagnostics with an xobj parameter. */
25807 eq_token = token;
25808 if (declarator)
25809 declarator->init_loc = token->location;
25810 /* If we are defining a class, then the tokens that make up the
25811 default argument must be saved and processed later. */
25812 if (!template_parm_p && at_class_scope_p ()
25813 && TYPE_BEING_DEFINED (current_class_type)
25814 && !LAMBDA_TYPE_P (current_class_type))
25815 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25817 /* A constrained-type-specifier may declare a type
25818 template-parameter. */
25819 else if (declares_constrained_type_template_parameter (type))
25820 default_argument
25821 = cp_parser_default_type_template_argument (parser);
25823 /* A constrained-type-specifier may declare a
25824 template-template-parameter. */
25825 else if (declares_constrained_template_template_parameter (type))
25826 default_argument
25827 = cp_parser_default_template_template_argument (parser);
25829 /* Outside of a class definition, we can just parse the
25830 assignment-expression. */
25831 else
25832 default_argument
25833 = cp_parser_default_argument (parser, template_parm_p);
25835 if (!parser->default_arg_ok_p)
25837 permerror (token->location,
25838 "default arguments are only "
25839 "permitted for function parameters");
25841 else if ((declarator && declarator->parameter_pack_p)
25842 || template_parameter_pack_p
25843 || (decl_specifiers.type
25844 && PACK_EXPANSION_P (decl_specifiers.type)))
25846 /* Find the name of the parameter pack. */
25847 cp_declarator *id_declarator = declarator;
25848 while (id_declarator && id_declarator->kind != cdk_id)
25849 id_declarator = id_declarator->declarator;
25851 if (id_declarator && id_declarator->kind == cdk_id)
25852 error_at (declarator_token_start->location,
25853 template_parm_p
25854 ? G_("template parameter pack %qD "
25855 "cannot have a default argument")
25856 : G_("parameter pack %qD cannot have "
25857 "a default argument"),
25858 id_declarator->u.id.unqualified_name);
25859 else
25860 error_at (declarator_token_start->location,
25861 template_parm_p
25862 ? G_("template parameter pack cannot have "
25863 "a default argument")
25864 : G_("parameter pack cannot have a "
25865 "default argument"));
25867 default_argument = NULL_TREE;
25870 else
25871 default_argument = NULL_TREE;
25873 if (default_argument)
25874 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25876 if (xobj_param_p)
25878 if (default_argument)
25880 /* If there is a default_argument, eq_token should always be set. */
25881 gcc_assert (eq_token);
25882 location_t param_with_init_loc
25883 = make_location (eq_token->location,
25884 decl_spec_token_start->location,
25885 input_location);
25886 error_at (param_with_init_loc,
25887 "an explicit object parameter "
25888 "may not have a default argument");
25890 /* Xobj parameters can not have default arguments, thus
25891 we can reuse the default argument field to flag the param as such. */
25892 default_argument = this_identifier;
25895 /* Generate a location for the parameter, ranging from the start of the
25896 initial token to the end of the final token (using input_location for
25897 the latter, set up by cp_lexer_set_source_position_from_token when
25898 consuming tokens).
25900 If we have a identifier, then use it for the caret location, e.g.
25902 extern int callee (int one, int (*two)(int, int), float three);
25903 ~~~~~~^~~~~~~~~~~~~~
25905 otherwise, reuse the start location for the caret location e.g.:
25907 extern int callee (int one, int (*)(int, int), float three);
25908 ^~~~~~~~~~~~~~~~~
25911 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25912 ? declarator->id_loc
25913 : decl_spec_token_start->location);
25914 location_t param_loc = make_location (caret_loc,
25915 decl_spec_token_start->location,
25916 input_location);
25918 return make_parameter_declarator (&decl_specifiers,
25919 declarator,
25920 default_argument,
25921 param_loc,
25922 template_parameter_pack_p);
25925 /* Parse a default argument and return it.
25927 TEMPLATE_PARM_P is true if this is a default argument for a
25928 non-type template parameter. */
25929 static tree
25930 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25932 tree default_argument = NULL_TREE;
25933 bool saved_greater_than_is_operator_p;
25934 unsigned char saved_local_variables_forbidden_p;
25936 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25937 set correctly. */
25938 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25939 parser->greater_than_is_operator_p = !template_parm_p;
25940 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25941 auto ord = make_temp_override (parser->oacc_routine, NULL);
25942 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25944 /* Local variable names (and the `this' keyword) may not
25945 appear in a default argument. */
25946 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25947 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25948 /* Parse the assignment-expression. */
25949 if (template_parm_p)
25950 push_deferring_access_checks (dk_no_deferred);
25951 tree saved_class_ptr = NULL_TREE;
25952 tree saved_class_ref = NULL_TREE;
25953 /* The "this" pointer is not valid in a default argument. */
25954 if (cfun)
25956 saved_class_ptr = current_class_ptr;
25957 cp_function_chain->x_current_class_ptr = NULL_TREE;
25958 saved_class_ref = current_class_ref;
25959 cp_function_chain->x_current_class_ref = NULL_TREE;
25961 default_argument = cp_parser_initializer (parser);
25962 /* Restore the "this" pointer. */
25963 if (cfun)
25965 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25966 cp_function_chain->x_current_class_ref = saved_class_ref;
25968 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25969 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25970 if (template_parm_p)
25971 pop_deferring_access_checks ();
25972 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25973 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25975 return default_argument;
25978 /* Parse a function-body.
25980 function-body:
25981 compound_statement */
25983 static void
25984 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25986 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25987 ? BCS_TRY_BLOCK : BCS_NORMAL),
25988 true);
25991 /* Parse a ctor-initializer-opt followed by a function-body. Return
25992 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25993 is true we are parsing a function-try-block. */
25995 static void
25996 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25997 bool in_function_try_block)
25999 tree body, list;
26000 const bool check_body_p
26001 = (DECL_CONSTRUCTOR_P (current_function_decl)
26002 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
26003 tree last = NULL;
26005 if (in_function_try_block
26006 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26007 && cxx_dialect < cxx20)
26009 if (DECL_CONSTRUCTOR_P (current_function_decl))
26010 pedwarn (input_location, OPT_Wc__20_extensions,
26011 "function-try-block body of %<constexpr%> constructor only "
26012 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26013 else
26014 pedwarn (input_location, OPT_Wc__20_extensions,
26015 "function-try-block body of %<constexpr%> function only "
26016 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26019 /* Begin the function body. */
26020 body = begin_function_body ();
26021 /* Parse the optional ctor-initializer. */
26022 cp_parser_ctor_initializer_opt (parser);
26024 /* If we're parsing a constexpr constructor definition, we need
26025 to check that the constructor body is indeed empty. However,
26026 before we get to cp_parser_function_body lot of junk has been
26027 generated, so we can't just check that we have an empty block.
26028 Rather we take a snapshot of the outermost block, and check whether
26029 cp_parser_function_body changed its state. */
26030 if (check_body_p)
26032 list = cur_stmt_list;
26033 if (STATEMENT_LIST_TAIL (list))
26034 last = STATEMENT_LIST_TAIL (list)->stmt;
26036 /* Parse the function-body. */
26037 cp_parser_function_body (parser, in_function_try_block);
26038 if (check_body_p)
26039 check_constexpr_ctor_body (last, list, /*complain=*/true);
26040 /* Finish the function body. */
26041 finish_function_body (body);
26044 /* Parse an initializer.
26046 initializer:
26047 = initializer-clause
26048 ( expression-list )
26050 Returns an expression representing the initializer. If no
26051 initializer is present, NULL_TREE is returned.
26053 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
26054 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
26055 set to TRUE if there is no initializer present. If there is an
26056 initializer, and it is not a constant-expression, *NON_CONSTANT_P
26057 is set to true; otherwise it is set to false. */
26059 static tree
26060 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
26061 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
26063 cp_token *token;
26064 tree init;
26066 /* Peek at the next token. */
26067 token = cp_lexer_peek_token (parser->lexer);
26069 /* Let our caller know whether or not this initializer was
26070 parenthesized. */
26071 if (is_direct_init)
26072 *is_direct_init = (token->type != CPP_EQ);
26073 /* Assume that the initializer is constant. */
26074 if (non_constant_p)
26075 *non_constant_p = false;
26077 if (token->type == CPP_EQ)
26079 /* Consume the `='. */
26080 cp_lexer_consume_token (parser->lexer);
26081 /* Parse the initializer-clause. */
26082 init = cp_parser_initializer_clause (parser, non_constant_p);
26084 else if (token->type == CPP_OPEN_PAREN)
26086 vec<tree, va_gc> *vec;
26087 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26088 /*cast_p=*/false,
26089 /*allow_expansion_p=*/true,
26090 non_constant_p);
26091 if (vec == NULL)
26092 return error_mark_node;
26093 init = build_tree_list_vec (vec);
26094 release_tree_vector (vec);
26096 else if (token->type == CPP_OPEN_BRACE)
26098 cp_lexer_set_source_position (parser->lexer);
26099 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26100 init = cp_parser_braced_list (parser, non_constant_p);
26101 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
26103 else
26105 /* Anything else is an error. */
26106 cp_parser_error (parser, "expected initializer");
26107 init = error_mark_node;
26110 if (!subexpression_p && check_for_bare_parameter_packs (init))
26111 init = error_mark_node;
26113 return init;
26116 /* Parse an initializer-clause.
26118 initializer-clause:
26119 assignment-expression
26120 braced-init-list
26122 Returns an expression representing the initializer.
26124 If the `assignment-expression' production is used the value
26125 returned is simply a representation for the expression.
26127 Otherwise, calls cp_parser_braced_list. */
26129 static cp_expr
26130 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
26132 cp_expr initializer;
26134 /* Assume the expression is constant. */
26135 if (non_constant_p)
26136 *non_constant_p = false;
26138 /* If it is not a `{', then we are looking at an
26139 assignment-expression. */
26140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26142 initializer
26143 = cp_parser_constant_expression (parser,
26144 /*allow_non_constant_p=*/2,
26145 non_constant_p);
26147 else
26148 initializer = cp_parser_braced_list (parser, non_constant_p);
26150 return initializer;
26153 /* Parse a brace-enclosed initializer list.
26155 braced-init-list:
26156 { initializer-list , [opt] }
26157 { designated-initializer-list , [opt] }
26160 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
26161 the elements of the initializer-list (or NULL, if the last
26162 production is used). The TREE_TYPE for the CONSTRUCTOR will be
26163 NULL_TREE. There is no way to detect whether or not the optional
26164 trailing `,' was provided. NON_CONSTANT_P is as for
26165 cp_parser_initializer. */
26167 static cp_expr
26168 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
26170 tree initializer;
26171 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26172 auto oas = make_temp_override (parser->omp_array_section_p, false);
26174 /* Consume the `{' token. */
26175 matching_braces braces;
26176 braces.require_open (parser);
26177 /* Create a CONSTRUCTOR to represent the braced-initializer. */
26178 initializer = make_node (CONSTRUCTOR);
26179 /* If it's not a `}', then there is a non-trivial initializer. */
26180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
26182 bool designated;
26183 /* Parse the initializer list. */
26184 CONSTRUCTOR_ELTS (initializer)
26185 = cp_parser_initializer_list (parser, non_constant_p, &designated);
26186 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
26187 /* A trailing `,' token is allowed. */
26188 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26189 cp_lexer_consume_token (parser->lexer);
26191 else if (non_constant_p)
26192 *non_constant_p = false;
26193 /* Now, there should be a trailing `}'. */
26194 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
26195 braces.require_close (parser);
26196 TREE_TYPE (initializer) = init_list_type_node;
26197 recompute_constructor_flags (initializer);
26199 cp_expr result (initializer);
26200 /* Build a location of the form:
26201 { ... }
26202 ^~~~~~~
26203 with caret==start at the open brace, finish at the close brace. */
26204 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26205 result.set_location (combined_loc);
26206 return result;
26209 /* Consume tokens up to, but not including, the next non-nested closing `]'.
26210 Returns true iff we found a closing `]'. */
26212 static bool
26213 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
26215 unsigned square_depth = 0;
26217 while (true)
26219 cp_token * token = cp_lexer_peek_token (parser->lexer);
26221 switch (token->type)
26223 case CPP_PRAGMA_EOL:
26224 if (!parser->lexer->in_pragma)
26225 break;
26226 /* FALLTHRU */
26228 case CPP_EOF:
26229 /* If we've run out of tokens, then there is no closing `]'. */
26230 return false;
26232 case CPP_OPEN_SQUARE:
26233 ++square_depth;
26234 break;
26236 case CPP_CLOSE_SQUARE:
26237 if (!square_depth--)
26238 return true;
26239 break;
26241 default:
26242 break;
26245 /* Consume the current token, skipping it. */
26246 cp_lexer_consume_token (parser->lexer);
26250 /* Consume tokens up to, and including, the next non-nested closing `]'.
26251 Returns true iff we found a closing `]'. */
26253 static bool
26254 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
26256 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
26257 if (found)
26258 cp_lexer_consume_token (parser->lexer);
26259 return found;
26262 /* Return true if we are looking at an array-designator, false otherwise. */
26264 static bool
26265 cp_parser_array_designator_p (cp_parser *parser)
26267 /* Consume the `['. */
26268 cp_lexer_consume_token (parser->lexer);
26270 cp_lexer_save_tokens (parser->lexer);
26272 /* Skip tokens until the next token is a closing square bracket.
26273 If we find the closing `]', and the next token is a `=', then
26274 we are looking at an array designator. */
26275 bool array_designator_p
26276 = (cp_parser_skip_to_closing_square_bracket (parser)
26277 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
26279 /* Roll back the tokens we skipped. */
26280 cp_lexer_rollback_tokens (parser->lexer);
26282 return array_designator_p;
26285 /* Parse an initializer-list.
26287 initializer-list:
26288 initializer-clause ... [opt]
26289 initializer-list , initializer-clause ... [opt]
26291 C++20 Extension:
26293 designated-initializer-list:
26294 designated-initializer-clause
26295 designated-initializer-list , designated-initializer-clause
26297 designated-initializer-clause:
26298 designator brace-or-equal-initializer
26300 designator:
26301 . identifier
26303 GNU Extension:
26305 initializer-list:
26306 designation initializer-clause ...[opt]
26307 initializer-list , designation initializer-clause ...[opt]
26309 designation:
26310 . identifier =
26311 identifier :
26312 [ constant-expression ] =
26314 Returns a vec of constructor_elt. The VALUE of each elt is an expression
26315 for the initializer. If the INDEX of the elt is non-NULL, it is the
26316 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
26317 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
26318 are any designators. */
26320 static vec<constructor_elt, va_gc> *
26321 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
26322 bool *designated)
26324 vec<constructor_elt, va_gc> *v = NULL;
26325 bool first_p = true;
26326 tree first_designator = NULL_TREE;
26328 /* Assume all of the expressions are constant. */
26329 if (non_constant_p)
26330 *non_constant_p = false;
26332 unsigned nelts = 0;
26333 int suppress = suppress_location_wrappers;
26335 /* Parse the rest of the list. */
26336 while (true)
26338 cp_token *token;
26339 tree designator;
26340 tree initializer;
26341 bool clause_non_constant_p;
26342 bool direct_p = false;
26343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26345 /* Handle the C++20 syntax, '. id ='. */
26346 if ((cxx_dialect >= cxx20
26347 || cp_parser_allow_gnu_extensions_p (parser))
26348 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
26349 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
26350 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
26351 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
26352 == CPP_OPEN_BRACE)))
26354 if (pedantic && cxx_dialect < cxx20)
26355 pedwarn (loc, OPT_Wc__20_extensions,
26356 "C++ designated initializers only available with "
26357 "%<-std=c++20%> or %<-std=gnu++20%>");
26358 /* Consume the `.'. */
26359 cp_lexer_consume_token (parser->lexer);
26360 /* Consume the identifier. */
26361 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26362 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26363 /* Consume the `='. */
26364 cp_lexer_consume_token (parser->lexer);
26365 else
26366 direct_p = true;
26368 /* Also, if the next token is an identifier and the following one is a
26369 colon, we are looking at the GNU designated-initializer
26370 syntax. */
26371 else if (cp_parser_allow_gnu_extensions_p (parser)
26372 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
26373 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26374 == CPP_COLON))
26376 /* Warn the user that they are using an extension. */
26377 pedwarn (loc, OPT_Wpedantic,
26378 "ISO C++ does not allow GNU designated initializers");
26379 /* Consume the identifier. */
26380 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26381 /* Consume the `:'. */
26382 cp_lexer_consume_token (parser->lexer);
26384 /* Also handle C99 array designators, '[ const ] ='. */
26385 else if (cp_parser_allow_gnu_extensions_p (parser)
26386 && !c_dialect_objc ()
26387 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26389 /* In C++11, [ could start a lambda-introducer. */
26390 bool non_const = false;
26392 cp_parser_parse_tentatively (parser);
26394 if (!cp_parser_array_designator_p (parser))
26396 cp_parser_simulate_error (parser);
26397 designator = NULL_TREE;
26399 else
26401 designator = cp_parser_constant_expression (parser, true,
26402 &non_const);
26403 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26404 cp_parser_require (parser, CPP_EQ, RT_EQ);
26407 if (!cp_parser_parse_definitely (parser))
26408 designator = NULL_TREE;
26409 else if (non_const
26410 && (!require_potential_rvalue_constant_expression
26411 (designator)))
26412 designator = NULL_TREE;
26413 if (designator)
26414 /* Warn the user that they are using an extension. */
26415 pedwarn (loc, OPT_Wpedantic,
26416 "ISO C++ does not allow C99 designated initializers");
26418 else
26419 designator = NULL_TREE;
26421 if (first_p)
26423 first_designator = designator;
26424 first_p = false;
26426 else if (cxx_dialect >= cxx20
26427 && first_designator != error_mark_node
26428 && (!first_designator != !designator))
26430 error_at (loc, "either all initializer clauses should be designated "
26431 "or none of them should be");
26432 first_designator = error_mark_node;
26434 else if (cxx_dialect < cxx20 && !first_designator)
26435 first_designator = designator;
26437 /* Parse the initializer. */
26438 initializer = cp_parser_initializer_clause (parser,
26439 (non_constant_p != nullptr
26440 ? &clause_non_constant_p
26441 : nullptr));
26442 /* If any clause is non-constant, so is the entire initializer. */
26443 if (non_constant_p && clause_non_constant_p)
26444 *non_constant_p = true;
26446 if (TREE_CODE (initializer) == CONSTRUCTOR)
26447 /* This uses |= rather than = because C_I_D_I could have been set in
26448 cp_parser_functional_cast so we must be careful not to clear the
26449 flag. */
26450 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26452 /* If we have an ellipsis, this is an initializer pack
26453 expansion. */
26454 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26456 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26458 /* Consume the `...'. */
26459 cp_lexer_consume_token (parser->lexer);
26461 if (designator && cxx_dialect >= cxx20)
26462 error_at (loc,
26463 "%<...%> not allowed in designated initializer list");
26465 /* Turn the initializer into an initializer expansion. */
26466 initializer = make_pack_expansion (initializer);
26469 /* Add it to the vector. */
26470 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26472 /* If the next token is not a comma, we have reached the end of
26473 the list. */
26474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26475 break;
26477 /* Peek at the next token. */
26478 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26479 /* If the next token is a `}', then we're still done. An
26480 initializer-clause can have a trailing `,' after the
26481 initializer-list and before the closing `}'. */
26482 if (token->type == CPP_CLOSE_BRACE)
26483 break;
26485 /* Suppress location wrappers in a long initializer to save memory
26486 (14179). The cutoff is chosen arbitrarily. */
26487 const unsigned loc_max = 256;
26488 unsigned incr = 1;
26489 if (TREE_CODE (initializer) == CONSTRUCTOR)
26490 /* Look one level down because it's easy. Looking deeper would require
26491 passing down a nelts pointer, and I don't think multi-level massive
26492 initializers are common enough to justify this. */
26493 incr = CONSTRUCTOR_NELTS (initializer);
26494 nelts += incr;
26495 if (nelts >= loc_max && (nelts - incr) < loc_max)
26496 ++suppress_location_wrappers;
26498 /* Consume the `,' token. */
26499 cp_lexer_consume_token (parser->lexer);
26502 /* The same identifier shall not appear in multiple designators
26503 of a designated-initializer-list. */
26504 if (first_designator)
26506 unsigned int i;
26507 tree designator, val;
26508 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26509 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26511 if (IDENTIFIER_MARKED (designator))
26513 error_at (cp_expr_loc_or_input_loc (val),
26514 "%<.%s%> designator used multiple times in "
26515 "the same initializer list",
26516 IDENTIFIER_POINTER (designator));
26517 (*v)[i].index = error_mark_node;
26519 else
26520 IDENTIFIER_MARKED (designator) = 1;
26522 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26523 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26524 IDENTIFIER_MARKED (designator) = 0;
26527 suppress_location_wrappers = suppress;
26529 *designated = first_designator != NULL_TREE;
26530 return v;
26533 /* Classes [gram.class] */
26535 /* Parse a class-name.
26537 class-name:
26538 identifier
26539 template-id
26541 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26542 to indicate that names looked up in dependent types should be
26543 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26544 keyword has been used to indicate that the name that appears next
26545 is a template. TAG_TYPE indicates the explicit tag given before
26546 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26547 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26548 is the class being defined in a class-head. If ENUM_OK is TRUE,
26549 enum-names are also accepted.
26551 Returns the TYPE_DECL representing the class. */
26553 static tree
26554 cp_parser_class_name (cp_parser *parser,
26555 bool typename_keyword_p,
26556 bool template_keyword_p,
26557 enum tag_types tag_type,
26558 bool check_dependency_p,
26559 bool class_head_p,
26560 bool is_declaration,
26561 bool enum_ok)
26563 tree decl;
26564 tree identifier = NULL_TREE;
26566 /* All class-names start with an identifier. */
26567 cp_token *token = cp_lexer_peek_token (parser->lexer);
26568 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26570 cp_parser_error (parser, "expected class-name");
26571 return error_mark_node;
26574 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26575 to a template-id, so we save it here. Consider object scope too,
26576 so that make_typename_type below can use it (cp_parser_template_name
26577 considers object scope also). This may happen with code like
26579 p->template A<T>::a()
26581 where we first want to look up A<T>::a in the class of the object
26582 expression, as per [basic.lookup.classref]. */
26583 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26584 /* This only checks parser->scope to avoid duplicate errors; if
26585 ->object_type is erroneous, go on to give a parse error. */
26586 if (parser->scope == error_mark_node)
26587 return error_mark_node;
26589 /* Any name names a type if we're following the `typename' keyword
26590 in a qualified name where the enclosing scope is type-dependent. */
26591 const bool typename_p = (typename_keyword_p
26592 && parser->scope
26593 && TYPE_P (parser->scope)
26594 && dependent_scope_p (parser->scope));
26595 /* Handle the common case (an identifier, but not a template-id)
26596 efficiently. */
26597 if (token->type == CPP_NAME
26598 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26600 cp_token *identifier_token;
26601 bool ambiguous_p;
26603 /* Look for the identifier. */
26604 identifier_token = cp_lexer_peek_token (parser->lexer);
26605 ambiguous_p = identifier_token->error_reported;
26606 identifier = cp_parser_identifier (parser);
26607 /* If the next token isn't an identifier, we are certainly not
26608 looking at a class-name. */
26609 if (identifier == error_mark_node)
26610 decl = error_mark_node;
26611 /* If we know this is a type-name, there's no need to look it
26612 up. */
26613 else if (typename_p)
26614 decl = identifier;
26615 else
26617 tree ambiguous_decls;
26618 /* If we already know that this lookup is ambiguous, then
26619 we've already issued an error message; there's no reason
26620 to check again. */
26621 if (ambiguous_p)
26623 cp_parser_simulate_error (parser);
26624 return error_mark_node;
26626 /* If the next token is a `::', then the name must be a type
26627 name.
26629 [basic.lookup.qual]
26631 During the lookup for a name preceding the :: scope
26632 resolution operator, object, function, and enumerator
26633 names are ignored. */
26634 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26635 tag_type = scope_type;
26636 /* Look up the name. */
26637 decl = cp_parser_lookup_name (parser, identifier,
26638 tag_type,
26639 /*is_template=*/false,
26640 /*is_namespace=*/false,
26641 check_dependency_p,
26642 &ambiguous_decls,
26643 identifier_token->location);
26644 if (ambiguous_decls)
26646 if (cp_parser_parsing_tentatively (parser))
26647 cp_parser_simulate_error (parser);
26648 return error_mark_node;
26652 else
26654 /* Try a template-id. */
26655 decl = cp_parser_template_id (parser, template_keyword_p,
26656 check_dependency_p,
26657 tag_type,
26658 is_declaration);
26659 if (decl == error_mark_node)
26660 return error_mark_node;
26663 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26665 /* If this is a typename, create a TYPENAME_TYPE. */
26666 if (typename_p && decl != error_mark_node)
26668 decl = make_typename_type (scope, decl, typename_type,
26669 /*complain=*/tf_error);
26670 if (decl != error_mark_node)
26671 decl = TYPE_NAME (decl);
26674 decl = strip_using_decl (decl);
26676 /* Check to see that it is really the name of a class. */
26677 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26678 && identifier_p (TREE_OPERAND (decl, 0))
26679 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26680 /* Situations like this:
26682 template <typename T> struct A {
26683 typename T::template X<int>::I i;
26686 are problematic. Is `T::template X<int>' a class-name? The
26687 standard does not seem to be definitive, but there is no other
26688 valid interpretation of the following `::'. Therefore, those
26689 names are considered class-names. */
26691 decl = make_typename_type (scope, decl, tag_type, tf_error);
26692 if (decl != error_mark_node)
26693 decl = TYPE_NAME (decl);
26695 else if (TREE_CODE (decl) != TYPE_DECL
26696 || TREE_TYPE (decl) == error_mark_node
26697 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26698 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26699 /* In Objective-C 2.0, a classname followed by '.' starts a
26700 dot-syntax expression, and it's not a type-name. */
26701 || (c_dialect_objc ()
26702 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26703 && objc_is_class_name (decl)))
26704 decl = error_mark_node;
26706 if (decl == error_mark_node)
26707 cp_parser_error (parser, "expected class-name");
26708 else if (identifier && !parser->scope)
26709 maybe_note_name_used_in_class (identifier, decl);
26711 return decl;
26714 /* Make sure that any member-function parameters are in scope.
26715 For instance, a function's noexcept-specifier can use the function's
26716 parameters:
26718 struct S {
26719 void fn (int p) noexcept(noexcept(p));
26722 so we need to make sure name lookup can find them. This is used
26723 when we delay parsing of the noexcept-specifier. */
26725 static void
26726 inject_parm_decls (tree decl)
26728 begin_scope (sk_function_parms, decl);
26729 tree args = DECL_ARGUMENTS (decl);
26731 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26733 if (args && is_this_parameter (args))
26735 gcc_checking_assert (current_class_ptr == NULL_TREE);
26736 current_class_ref = cp_build_fold_indirect_ref (args);
26737 current_class_ptr = args;
26741 /* Undo the effects of inject_parm_decls. */
26743 static void
26744 pop_injected_parms (void)
26746 pop_bindings_and_leave_scope ();
26747 current_class_ptr = current_class_ref = NULL_TREE;
26750 /* Parse a class-specifier.
26752 class-specifier:
26753 class-head { member-specification [opt] }
26755 Returns the TREE_TYPE representing the class. */
26757 tree
26758 cp_parser_class_specifier (cp_parser* parser)
26760 auto_timevar tv (TV_PARSE_STRUCT);
26762 tree type;
26763 tree attributes = NULL_TREE;
26764 bool nested_name_specifier_p;
26765 unsigned saved_num_template_parameter_lists;
26766 bool saved_in_function_body;
26767 unsigned char in_statement;
26768 bool in_switch_statement_p;
26769 bool saved_in_unbraced_linkage_specification_p;
26770 tree old_scope = NULL_TREE;
26771 tree scope = NULL_TREE;
26772 cp_token *closing_brace;
26774 push_deferring_access_checks (dk_no_deferred);
26776 /* Parse the class-head. */
26777 type = cp_parser_class_head (parser,
26778 &nested_name_specifier_p);
26779 /* If the class-head was a semantic disaster, skip the entire body
26780 of the class. */
26781 if (!type)
26783 cp_parser_skip_to_end_of_block_or_statement (parser);
26784 pop_deferring_access_checks ();
26785 return error_mark_node;
26788 /* Look for the `{'. */
26789 matching_braces braces;
26790 if (!braces.require_open (parser))
26792 pop_deferring_access_checks ();
26793 return error_mark_node;
26796 cp_ensure_no_omp_declare_simd (parser);
26797 cp_ensure_no_oacc_routine (parser);
26799 /* Issue an error message if type-definitions are forbidden here. */
26800 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26801 /* Remember that we are defining one more class. */
26802 ++parser->num_classes_being_defined;
26803 /* Inside the class, surrounding template-parameter-lists do not
26804 apply. */
26805 saved_num_template_parameter_lists
26806 = parser->num_template_parameter_lists;
26807 parser->num_template_parameter_lists = 0;
26808 /* We are not in a function body. */
26809 saved_in_function_body = parser->in_function_body;
26810 parser->in_function_body = false;
26811 /* Or in a loop. */
26812 in_statement = parser->in_statement;
26813 parser->in_statement = 0;
26814 /* Or in a switch. */
26815 in_switch_statement_p = parser->in_switch_statement_p;
26816 parser->in_switch_statement_p = false;
26817 /* We are not immediately inside an extern "lang" block. */
26818 saved_in_unbraced_linkage_specification_p
26819 = parser->in_unbraced_linkage_specification_p;
26820 parser->in_unbraced_linkage_specification_p = false;
26821 /* 'this' from an enclosing non-static member function is unavailable. */
26822 tree saved_ccp = current_class_ptr;
26823 tree saved_ccr = current_class_ref;
26824 current_class_ptr = NULL_TREE;
26825 current_class_ref = NULL_TREE;
26827 /* Start the class. */
26828 if (nested_name_specifier_p)
26830 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26831 /* SCOPE must be a scope nested inside current scope. */
26832 if (is_nested_namespace (current_namespace,
26833 decl_namespace_context (scope)))
26834 old_scope = push_inner_scope (scope);
26835 else
26836 nested_name_specifier_p = false;
26838 type = begin_class_definition (type);
26840 if (type == error_mark_node)
26841 /* If the type is erroneous, skip the entire body of the class. */
26842 cp_parser_skip_to_closing_brace (parser);
26843 else
26844 /* Parse the member-specification. */
26845 cp_parser_member_specification_opt (parser);
26847 /* Look for the trailing `}'. */
26848 closing_brace = braces.require_close (parser);
26849 /* Look for trailing attributes to apply to this class. */
26850 if (cp_parser_allow_gnu_extensions_p (parser))
26851 attributes = cp_parser_gnu_attributes_opt (parser);
26852 if (type != error_mark_node)
26853 type = finish_struct (type, attributes);
26854 if (nested_name_specifier_p)
26855 pop_inner_scope (old_scope, scope);
26857 /* We've finished a type definition. Check for the common syntax
26858 error of forgetting a semicolon after the definition. We need to
26859 be careful, as we can't just check for not-a-semicolon and be done
26860 with it; the user might have typed:
26862 class X { } c = ...;
26863 class X { } *p = ...;
26865 and so forth. Instead, enumerate all the possible tokens that
26866 might follow this production; if we don't see one of them, then
26867 complain and silently insert the semicolon. */
26869 cp_token *token = cp_lexer_peek_token (parser->lexer);
26870 bool want_semicolon = true;
26872 if (cp_next_tokens_can_be_std_attribute_p (parser))
26873 /* Don't try to parse c++11 attributes here. As per the
26874 grammar, that should be a task for
26875 cp_parser_decl_specifier_seq. */
26876 want_semicolon = false;
26878 switch (token->type)
26880 case CPP_NAME:
26881 case CPP_SEMICOLON:
26882 case CPP_MULT:
26883 case CPP_AND:
26884 case CPP_OPEN_PAREN:
26885 case CPP_CLOSE_PAREN:
26886 case CPP_COMMA:
26887 case CPP_SCOPE:
26888 want_semicolon = false;
26889 break;
26891 /* While it's legal for type qualifiers and storage class
26892 specifiers to follow type definitions in the grammar, only
26893 compiler testsuites contain code like that. Assume that if
26894 we see such code, then what we're really seeing is a case
26895 like:
26897 class X { }
26898 const <type> var = ...;
26902 class Y { }
26903 static <type> func (...) ...
26905 i.e. the qualifier or specifier applies to the next
26906 declaration. To do so, however, we need to look ahead one
26907 more token to see if *that* token is a type specifier.
26909 This code could be improved to handle:
26911 class Z { }
26912 static const <type> var = ...; */
26913 case CPP_KEYWORD:
26914 if (keyword_is_decl_specifier (token->keyword))
26916 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26918 /* Handling user-defined types here would be nice, but very
26919 tricky. */
26920 want_semicolon
26921 = (lookahead->type == CPP_KEYWORD
26922 && keyword_begins_type_specifier (lookahead->keyword));
26924 break;
26925 default:
26926 break;
26929 /* If we don't have a type, then something is very wrong and we
26930 shouldn't try to do anything clever. Likewise for not seeing the
26931 closing brace. */
26932 if (closing_brace && TYPE_P (type) && want_semicolon)
26934 /* Locate the closing brace. */
26935 cp_token_position prev
26936 = cp_lexer_previous_token_position (parser->lexer);
26937 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26938 location_t loc = prev_token->location;
26940 /* We want to suggest insertion of a ';' immediately *after* the
26941 closing brace, so, if we can, offset the location by 1 column. */
26942 location_t next_loc = loc;
26943 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26944 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26946 rich_location richloc (line_table, next_loc);
26948 /* If we successfully offset the location, suggest the fix-it. */
26949 if (next_loc != loc)
26950 richloc.add_fixit_insert_before (next_loc, ";");
26952 if (CLASSTYPE_DECLARED_CLASS (type))
26953 error_at (&richloc,
26954 "expected %<;%> after class definition");
26955 else if (TREE_CODE (type) == RECORD_TYPE)
26956 error_at (&richloc,
26957 "expected %<;%> after struct definition");
26958 else if (TREE_CODE (type) == UNION_TYPE)
26959 error_at (&richloc,
26960 "expected %<;%> after union definition");
26961 else
26962 gcc_unreachable ();
26964 /* Unget one token and smash it to look as though we encountered
26965 a semicolon in the input stream. */
26966 cp_lexer_set_token_position (parser->lexer, prev);
26967 token = cp_lexer_peek_token (parser->lexer);
26968 token->type = CPP_SEMICOLON;
26969 token->keyword = RID_MAX;
26973 /* If this class is not itself within the scope of another class,
26974 then we need to parse the bodies of all of the queued function
26975 definitions. Note that the queued functions defined in a class
26976 are not always processed immediately following the
26977 class-specifier for that class. Consider:
26979 struct A {
26980 struct B { void f() { sizeof (A); } };
26983 If `f' were processed before the processing of `A' were
26984 completed, there would be no way to compute the size of `A'.
26985 Note that the nesting we are interested in here is lexical --
26986 not the semantic nesting given by TYPE_CONTEXT. In particular,
26987 for:
26989 struct A { struct B; };
26990 struct A::B { void f() { } };
26992 there is no need to delay the parsing of `A::B::f'. */
26993 if (--parser->num_classes_being_defined == 0)
26995 tree decl;
26996 tree class_type = NULL_TREE;
26997 tree pushed_scope = NULL_TREE;
26998 unsigned ix;
26999 cp_default_arg_entry *e;
27001 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
27003 /* Skip default arguments, NSDMIs, etc, in order to improve
27004 error recovery (c++/71169, c++/71832). */
27005 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27006 vec_safe_truncate (unparsed_nsdmis, 0);
27007 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27010 /* In a first pass, parse default arguments to the functions.
27011 Then, in a second pass, parse the bodies of the functions.
27012 This two-phased approach handles cases like:
27014 struct S {
27015 void f() { g(); }
27016 void g(int i = 3);
27020 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
27022 decl = e->decl;
27023 /* If there are default arguments that have not yet been processed,
27024 take care of them now. */
27025 if (class_type != e->class_type)
27027 if (pushed_scope)
27028 pop_scope (pushed_scope);
27029 class_type = e->class_type;
27030 pushed_scope = push_scope (class_type);
27032 /* Make sure that any template parameters are in scope. */
27033 maybe_begin_member_template_processing (decl);
27034 /* Parse the default argument expressions. */
27035 cp_parser_late_parsing_default_args (parser, decl);
27036 /* Remove any template parameters from the symbol table. */
27037 maybe_end_member_template_processing ();
27039 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27041 /* If there are noexcept-specifiers that have not yet been processed,
27042 take care of them now. Do this before processing NSDMIs as they
27043 may depend on noexcept-specifiers already having been processed. */
27044 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
27046 tree ctx = DECL_CONTEXT (decl);
27047 if (class_type != ctx)
27049 if (pushed_scope)
27050 pop_scope (pushed_scope);
27051 class_type = ctx;
27052 pushed_scope = push_scope (class_type);
27055 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
27056 def_parse = TREE_PURPOSE (def_parse);
27058 /* Make sure that any template parameters are in scope. */
27059 maybe_begin_member_template_processing (decl);
27061 /* Make sure that any member-function parameters are in scope.
27062 This function doesn't expect ccp to be set. */
27063 current_class_ptr = current_class_ref = NULL_TREE;
27064 inject_parm_decls (decl);
27066 /* 'this' is not allowed in static member functions. */
27067 unsigned char local_variables_forbidden_p
27068 = parser->local_variables_forbidden_p;
27069 if (DECL_THIS_STATIC (decl))
27070 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27072 /* Now we can parse the noexcept-specifier. */
27073 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
27075 if (spec == error_mark_node)
27076 spec = NULL_TREE;
27078 /* Update the fn's type directly -- it might have escaped
27079 beyond this decl :( */
27080 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
27081 /* Update any instantiations we've already created. We must
27082 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
27083 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
27084 in the pattern. */
27085 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
27086 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
27087 = spec ? TREE_PURPOSE (spec) : error_mark_node;
27089 /* Restore the state of local_variables_forbidden_p. */
27090 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27092 /* The finish_struct call above performed various override checking,
27093 but it skipped unparsed noexcept-specifier operands. Now that we
27094 have resolved them, check again. */
27095 noexcept_override_late_checks (decl);
27097 /* Remove any member-function parameters from the symbol table. */
27098 pop_injected_parms ();
27100 /* Remove any template parameters from the symbol table. */
27101 maybe_end_member_template_processing ();
27103 vec_safe_truncate (unparsed_noexcepts, 0);
27105 /* Now parse any NSDMIs. */
27106 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
27108 tree ctx = type_context_for_name_lookup (decl);
27109 if (class_type != ctx)
27111 if (pushed_scope)
27112 pop_scope (pushed_scope);
27113 class_type = ctx;
27114 pushed_scope = push_scope (class_type);
27116 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
27117 cp_parser_late_parsing_nsdmi (parser, decl);
27119 vec_safe_truncate (unparsed_nsdmis, 0);
27121 /* Now contract attributes. */
27122 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
27124 tree ctx = DECL_CONTEXT (decl);
27125 if (class_type != ctx)
27127 if (pushed_scope)
27128 pop_scope (pushed_scope);
27129 class_type = ctx;
27130 pushed_scope = push_scope (class_type);
27133 temp_override<tree> cfd(current_function_decl, decl);
27135 /* Make sure that any template parameters are in scope. */
27136 maybe_begin_member_template_processing (decl);
27138 /* Make sure that any member-function parameters are in scope.
27139 This function doesn't expect ccp to be set. */
27140 current_class_ptr = current_class_ref = NULL_TREE;
27141 inject_parm_decls (decl);
27143 /* 'this' is not allowed in static member functions. */
27144 unsigned char local_variables_forbidden_p
27145 = parser->local_variables_forbidden_p;
27146 if (DECL_THIS_STATIC (decl))
27147 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27149 /* Now we can parse contract conditions. */
27150 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
27152 if (cxx_contract_attribute_p (a))
27153 cp_parser_late_contract_condition (parser, decl, a);
27156 /* Restore the state of local_variables_forbidden_p. */
27157 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27159 /* Remove any member-function parameters from the symbol table. */
27160 pop_injected_parms ();
27162 /* Remove any template parameters from the symbol table. */
27163 maybe_end_member_template_processing ();
27165 /* Perform any deferred contract matching. */
27166 match_deferred_contracts (decl);
27168 vec_safe_truncate (unparsed_contracts, 0);
27170 current_class_ptr = NULL_TREE;
27171 current_class_ref = NULL_TREE;
27172 if (pushed_scope)
27173 pop_scope (pushed_scope);
27175 /* Now parse the body of the functions. */
27176 if (flag_openmp)
27178 /* OpenMP UDRs need to be parsed before all other functions. */
27179 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27180 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
27181 cp_parser_late_parsing_for_member (parser, decl);
27182 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27183 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
27184 cp_parser_late_parsing_for_member (parser, decl);
27186 else
27187 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27188 cp_parser_late_parsing_for_member (parser, decl);
27189 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27192 /* Put back any saved access checks. */
27193 pop_deferring_access_checks ();
27195 /* Restore saved state. */
27196 parser->in_switch_statement_p = in_switch_statement_p;
27197 parser->in_statement = in_statement;
27198 parser->in_function_body = saved_in_function_body;
27199 parser->num_template_parameter_lists
27200 = saved_num_template_parameter_lists;
27201 parser->in_unbraced_linkage_specification_p
27202 = saved_in_unbraced_linkage_specification_p;
27203 current_class_ptr = saved_ccp;
27204 current_class_ref = saved_ccr;
27206 return type;
27209 /* Parse a class-head.
27211 class-head:
27212 class-key identifier [opt] base-clause [opt]
27213 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
27214 class-key nested-name-specifier [opt] template-id
27215 base-clause [opt]
27217 class-virt-specifier:
27218 final
27220 GNU Extensions:
27221 class-key attributes identifier [opt] base-clause [opt]
27222 class-key attributes nested-name-specifier identifier base-clause [opt]
27223 class-key attributes nested-name-specifier [opt] template-id
27224 base-clause [opt]
27226 Upon return BASES is initialized to the list of base classes (or
27227 NULL, if there are none) in the same form returned by
27228 cp_parser_base_clause.
27230 Returns the TYPE of the indicated class. Sets
27231 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
27232 involving a nested-name-specifier was used, and FALSE otherwise.
27234 Returns error_mark_node if this is not a class-head.
27236 Returns NULL_TREE if the class-head is syntactically valid, but
27237 semantically invalid in a way that means we should skip the entire
27238 body of the class. */
27240 static tree
27241 cp_parser_class_head (cp_parser* parser,
27242 bool* nested_name_specifier_p)
27244 tree nested_name_specifier;
27245 enum tag_types class_key;
27246 tree id = NULL_TREE;
27247 tree type = NULL_TREE;
27248 tree attributes;
27249 tree bases;
27250 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
27251 bool template_id_p = false;
27252 bool qualified_p = false;
27253 bool invalid_nested_name_p = false;
27254 bool invalid_explicit_specialization_p = false;
27255 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27256 tree pushed_scope = NULL_TREE;
27257 unsigned num_templates;
27258 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
27259 /* Assume no nested-name-specifier will be present. */
27260 *nested_name_specifier_p = false;
27261 /* Assume no template parameter lists will be used in defining the
27262 type. */
27263 num_templates = 0;
27264 parser->colon_corrects_to_scope_p = false;
27266 /* Look for the class-key. */
27267 class_key = cp_parser_class_key (parser);
27268 if (class_key == none_type)
27269 return error_mark_node;
27271 location_t class_head_start_location = input_location;
27273 /* Parse the attributes. */
27274 attributes = cp_parser_attributes_opt (parser);
27275 if (find_contract (attributes))
27276 diagnose_misapplied_contracts (attributes);
27278 /* If the next token is `::', that is invalid -- but sometimes
27279 people do try to write:
27281 struct ::S {};
27283 Handle this gracefully by accepting the extra qualifier, and then
27284 issuing an error about it later if this really is a
27285 class-head. If it turns out just to be an elaborated type
27286 specifier, remain silent. */
27287 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
27288 qualified_p = true;
27290 /* It is OK to define an inaccessible class; for example:
27292 class A { class B; };
27293 class A::B {};
27295 So we want to ignore access when parsing the class name.
27296 However, we might be tentatively parsing what is really an
27297 elaborated-type-specifier naming a template-id, e.g.
27299 struct C<&D::m> c;
27301 In this case the tentative parse as a class-head will fail, but not
27302 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
27303 Since dk_no_check is sticky, we must instead use dk_deferred so that
27304 any such CPP_TEMPLATE_ID token created during this tentative parse
27305 will correctly capture the access checks imposed by the template-id . */
27306 push_deferring_access_checks (dk_deferred);
27308 /* Determine the name of the class. Begin by looking for an
27309 optional nested-name-specifier. */
27310 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
27311 nested_name_specifier
27312 = cp_parser_nested_name_specifier_opt (parser,
27313 /*typename_keyword_p=*/false,
27314 /*check_dependency_p=*/false,
27315 /*type_p=*/true,
27316 /*is_declaration=*/false);
27317 /* If there was a nested-name-specifier, then there *must* be an
27318 identifier. */
27320 cp_token *bad_template_keyword = NULL;
27322 if (nested_name_specifier)
27324 type_start_token = cp_lexer_peek_token (parser->lexer);
27325 /* Although the grammar says `identifier', it really means
27326 `class-name' or `template-name'. You are only allowed to
27327 define a class that has already been declared with this
27328 syntax.
27330 The proposed resolution for Core Issue 180 says that wherever
27331 you see `class T::X' you should treat `X' as a type-name.
27333 We do not know if we will see a class-name, or a
27334 template-name. We look for a class-name first, in case the
27335 class-name is a template-id; if we looked for the
27336 template-name first we would stop after the template-name. */
27337 cp_parser_parse_tentatively (parser);
27338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27339 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
27340 type = cp_parser_class_name (parser,
27341 /*typename_keyword_p=*/false,
27342 /*template_keyword_p=*/false,
27343 class_type,
27344 /*check_dependency_p=*/false,
27345 /*class_head_p=*/true,
27346 /*is_declaration=*/false);
27347 /* If that didn't work, ignore the nested-name-specifier. */
27348 if (!cp_parser_parse_definitely (parser))
27350 invalid_nested_name_p = true;
27351 type_start_token = cp_lexer_peek_token (parser->lexer);
27352 id = cp_parser_identifier (parser);
27353 if (id == error_mark_node)
27354 id = NULL_TREE;
27356 /* If we could not find a corresponding TYPE, treat this
27357 declaration like an unqualified declaration. */
27358 if (type == error_mark_node)
27359 nested_name_specifier = NULL_TREE;
27360 /* Otherwise, count the number of templates used in TYPE and its
27361 containing scopes. */
27362 else
27363 num_templates = num_template_headers_for_class (TREE_TYPE (type));
27365 /* Otherwise, the identifier is optional. */
27366 else
27368 /* We don't know whether what comes next is a template-id,
27369 an identifier, or nothing at all. */
27370 cp_parser_parse_tentatively (parser);
27371 /* Check for a template-id. */
27372 type_start_token = cp_lexer_peek_token (parser->lexer);
27373 id = cp_parser_template_id (parser,
27374 /*template_keyword_p=*/false,
27375 /*check_dependency_p=*/true,
27376 class_key,
27377 /*is_declaration=*/true);
27378 /* If that didn't work, it could still be an identifier. */
27379 if (!cp_parser_parse_definitely (parser))
27381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27383 type_start_token = cp_lexer_peek_token (parser->lexer);
27384 id = cp_parser_identifier (parser);
27386 else
27387 id = NULL_TREE;
27389 else
27391 template_id_p = true;
27392 ++num_templates;
27396 pop_deferring_access_checks ();
27398 if (id)
27400 cp_parser_check_for_invalid_template_id (parser, id,
27401 class_key,
27402 type_start_token->location);
27404 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27406 /* If it's not a `:' or a `{' then we can't really be looking at a
27407 class-head, since a class-head only appears as part of a
27408 class-specifier. We have to detect this situation before calling
27409 xref_tag, since that has irreversible side-effects. */
27410 if (!cp_parser_next_token_starts_class_definition_p (parser))
27412 cp_parser_error (parser, "expected %<{%> or %<:%>");
27413 type = error_mark_node;
27414 goto out;
27417 /* At this point, we're going ahead with the class-specifier, even
27418 if some other problem occurs. */
27419 cp_parser_commit_to_tentative_parse (parser);
27420 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27422 cp_parser_error (parser,
27423 "cannot specify %<override%> for a class");
27424 type = error_mark_node;
27425 goto out;
27427 /* Issue the error about the overly-qualified name now. */
27428 if (qualified_p)
27430 cp_parser_error (parser,
27431 "global qualification of class name is invalid");
27432 type = error_mark_node;
27433 goto out;
27435 else if (invalid_nested_name_p)
27437 cp_parser_error (parser,
27438 "qualified name does not name a class");
27439 type = error_mark_node;
27440 goto out;
27442 else if (nested_name_specifier)
27444 tree scope;
27446 if (bad_template_keyword)
27447 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27448 keyword template shall not appear at the top level. */
27449 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27450 "keyword %<template%> not allowed in class-head-name");
27452 /* Reject typedef-names in class heads. */
27453 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27455 error_at (type_start_token->location,
27456 "invalid class name in declaration of %qD",
27457 type);
27458 type = NULL_TREE;
27459 goto done;
27462 /* Figure out in what scope the declaration is being placed. */
27463 scope = current_scope ();
27464 /* If that scope does not contain the scope in which the
27465 class was originally declared, the program is invalid. */
27466 if (scope && !is_ancestor (scope, nested_name_specifier))
27468 if (at_namespace_scope_p ())
27469 error_at (type_start_token->location,
27470 "declaration of %qD in namespace %qD which does not "
27471 "enclose %qD",
27472 type, scope, nested_name_specifier);
27473 else
27474 error_at (type_start_token->location,
27475 "declaration of %qD in %qD which does not enclose %qD",
27476 type, scope, nested_name_specifier);
27477 type = NULL_TREE;
27478 goto done;
27480 /* [dcl.meaning]
27482 A declarator-id shall not be qualified except for the
27483 definition of a ... nested class outside of its class
27484 ... [or] the definition or explicit instantiation of a
27485 class member of a namespace outside of its namespace. */
27486 if (scope == nested_name_specifier)
27487 permerror (nested_name_specifier_token_start->location,
27488 "extra qualification not allowed");
27490 /* An explicit-specialization must be preceded by "template <>". If
27491 it is not, try to recover gracefully. */
27492 if (at_namespace_scope_p ()
27493 && parser->num_template_parameter_lists == 0
27494 && !processing_template_parmlist
27495 && template_id_p)
27497 /* Build a location of this form:
27498 struct typename <ARGS>
27499 ^~~~~~~~~~~~~~~~~~~~~~
27500 with caret==start at the start token, and
27501 finishing at the end of the type. */
27502 location_t reported_loc
27503 = make_location (class_head_start_location,
27504 class_head_start_location,
27505 get_finish (type_start_token->location));
27506 rich_location richloc (line_table, reported_loc);
27507 richloc.add_fixit_insert_before (class_head_start_location,
27508 "template <> ");
27509 error_at (&richloc,
27510 "an explicit specialization must be preceded by"
27511 " %<template <>%>");
27512 invalid_explicit_specialization_p = true;
27513 /* Take the same action that would have been taken by
27514 cp_parser_explicit_specialization. */
27515 ++parser->num_template_parameter_lists;
27516 begin_specialization ();
27518 /* There must be no "return" statements between this point and the
27519 end of this function; set "type "to the correct return value and
27520 use "goto done;" to return. */
27521 /* Make sure that the right number of template parameters were
27522 present. */
27523 if (!cp_parser_check_template_parameters (parser, num_templates,
27524 template_id_p,
27525 type_start_token->location,
27526 /*declarator=*/NULL))
27528 /* If something went wrong, there is no point in even trying to
27529 process the class-definition. */
27530 type = NULL_TREE;
27531 goto done;
27534 /* Look up the type. */
27535 if (template_id_p)
27537 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27538 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27539 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27541 error_at (type_start_token->location,
27542 "function template %qD redeclared as a class template", id);
27543 type = error_mark_node;
27545 else
27547 type = TREE_TYPE (id);
27548 type = maybe_process_partial_specialization (type);
27550 /* Check the scope while we still know whether or not we had a
27551 nested-name-specifier. */
27552 if (type != error_mark_node)
27553 check_unqualified_spec_or_inst (type, type_start_token->location);
27555 if (nested_name_specifier)
27556 pushed_scope = push_scope (nested_name_specifier);
27558 else if (nested_name_specifier)
27560 type = TREE_TYPE (type);
27562 /* Given:
27564 template <typename T> struct S { struct T };
27565 template <typename T> struct S<T>::T { };
27567 we will get a TYPENAME_TYPE when processing the definition of
27568 `S::T'. We need to resolve it to the actual type before we
27569 try to define it. */
27570 if (TREE_CODE (type) == TYPENAME_TYPE)
27572 type = resolve_typename_type (type, /*only_current_p=*/false);
27573 if (TREE_CODE (type) == TYPENAME_TYPE)
27575 cp_parser_error (parser, "could not resolve typename type");
27576 type = error_mark_node;
27580 type = maybe_process_partial_specialization (type);
27581 if (type == error_mark_node)
27583 type = NULL_TREE;
27584 goto done;
27587 /* Enter the scope indicated by the nested-name-specifier. */
27588 pushed_scope = push_scope (nested_name_specifier);
27589 /* Get the canonical version of this type. */
27590 type = TYPE_MAIN_DECL (type);
27591 /* Call push_template_decl if it seems like we should be defining a
27592 template either from the template headers or the type we're
27593 defining, so that we diagnose both extra and missing headers. */
27594 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27595 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27596 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27598 type = push_template_decl (type);
27599 if (type == error_mark_node)
27601 type = NULL_TREE;
27602 goto done;
27606 type = TREE_TYPE (type);
27607 *nested_name_specifier_p = true;
27609 else /* The name is not a nested name. */
27611 /* If the class was unnamed, create a dummy name. */
27612 if (!id)
27613 id = make_anon_name ();
27614 TAG_how how = (parser->in_type_id_in_expr_p
27615 ? TAG_how::INNERMOST_NON_CLASS
27616 : TAG_how::CURRENT_ONLY);
27617 type = xref_tag (class_key, id, how,
27618 parser->num_template_parameter_lists);
27621 /* Diagnose class/struct/union mismatches. */
27622 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27623 true, true);
27625 /* Indicate whether this class was declared as a `class' or as a
27626 `struct'. */
27627 if (TREE_CODE (type) == RECORD_TYPE)
27628 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27630 /* If this type was already complete, and we see another definition,
27631 that's an error. Likewise if the type is already being defined:
27632 this can happen, eg, when it's defined from within an expression
27633 (c++/84605). */
27634 if (type != error_mark_node
27635 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27637 error_at (type_start_token->location, "redefinition of %q#T",
27638 type);
27639 inform (location_of (type), "previous definition of %q#T",
27640 type);
27641 type = NULL_TREE;
27642 goto done;
27644 else if (type == error_mark_node)
27645 type = NULL_TREE;
27647 if (type)
27649 if (current_lambda_expr ()
27650 && uses_parameter_packs (attributes))
27652 /* In a lambda this should work, but doesn't currently. */
27653 sorry ("unexpanded parameter pack in local class in lambda");
27654 attributes = NULL_TREE;
27657 /* Apply attributes now, before any use of the class as a template
27658 argument in its base list. */
27659 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27660 fixup_attribute_variants (type);
27663 /* Associate constraints with the type. */
27664 if (flag_concepts)
27665 type = associate_classtype_constraints (type);
27667 /* We will have entered the scope containing the class; the names of
27668 base classes should be looked up in that context. For example:
27670 struct A { struct B {}; struct C; };
27671 struct A::C : B {};
27673 is valid. */
27675 /* Get the list of base-classes, if there is one. Defer access checking
27676 until the entire list has been seen, as per [class.access.general]. */
27677 push_deferring_access_checks (dk_deferred);
27678 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27680 if (type)
27681 pushclass (type);
27682 bases = cp_parser_base_clause (parser);
27683 if (type)
27684 popclass ();
27686 else
27687 bases = NULL_TREE;
27689 /* If we're really defining a class, process the base classes.
27690 If they're invalid, fail. */
27691 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27692 xref_basetypes (type, bases);
27694 /* Now that all bases have been seen and attached to the class, check
27695 accessibility of the types named in the base-clause. This must be
27696 done relative to the class scope, so that we accept e.g.
27698 struct A { protected: struct B {}; };
27699 struct C : A::B, A {}; // OK: A::B is accessible via base A
27701 as per [class.access.general]. */
27702 if (type)
27703 pushclass (type);
27704 pop_to_parent_deferring_access_checks ();
27705 if (type)
27706 popclass ();
27708 done:
27709 /* Leave the scope given by the nested-name-specifier. We will
27710 enter the class scope itself while processing the members. */
27711 if (pushed_scope)
27712 pop_scope (pushed_scope);
27714 if (invalid_explicit_specialization_p)
27716 end_specialization ();
27717 --parser->num_template_parameter_lists;
27720 if (type)
27721 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27722 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27723 CLASSTYPE_FINAL (type) = 1;
27724 out:
27725 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27726 return type;
27729 /* Parse a class-key.
27731 class-key:
27732 class
27733 struct
27734 union
27736 Returns the kind of class-key specified, or none_type to indicate
27737 error. */
27739 static enum tag_types
27740 cp_parser_class_key (cp_parser* parser)
27742 cp_token *token;
27743 enum tag_types tag_type;
27745 /* Look for the class-key. */
27746 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27747 if (!token)
27748 return none_type;
27750 /* Check to see if the TOKEN is a class-key. */
27751 tag_type = cp_parser_token_is_class_key (token);
27752 if (!tag_type)
27753 cp_parser_error (parser, "expected class-key");
27754 return tag_type;
27757 /* Parse a type-parameter-key.
27759 type-parameter-key:
27760 class
27761 typename
27764 static void
27765 cp_parser_type_parameter_key (cp_parser* parser)
27767 /* Look for the type-parameter-key. */
27768 enum tag_types tag_type = none_type;
27769 cp_token *token = cp_lexer_peek_token (parser->lexer);
27770 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27772 cp_lexer_consume_token (parser->lexer);
27773 if (pedantic && tag_type == typename_type
27774 && cxx_dialect < cxx17)
27775 /* typename is not allowed in a template template parameter
27776 by the standard until C++17. */
27777 pedwarn (token->location, OPT_Wc__17_extensions,
27778 "ISO C++ forbids typename key in template template parameter;"
27779 " use %<-std=c++17%> or %<-std=gnu++17%>");
27781 else
27782 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27784 return;
27787 /* Parse an (optional) member-specification.
27789 member-specification:
27790 member-declaration member-specification [opt]
27791 access-specifier : member-specification [opt] */
27793 static void
27794 cp_parser_member_specification_opt (cp_parser* parser)
27796 while (true)
27798 cp_token *token;
27799 enum rid keyword;
27801 /* Peek at the next token. */
27802 token = cp_lexer_peek_token (parser->lexer);
27803 /* If it's a `}', or EOF then we've seen all the members. */
27804 if (token->type == CPP_CLOSE_BRACE
27805 || token->type == CPP_EOF
27806 || token->type == CPP_PRAGMA_EOL)
27807 break;
27809 /* See if this token is a keyword. */
27810 keyword = token->keyword;
27811 switch (keyword)
27813 case RID_PUBLIC:
27814 case RID_PROTECTED:
27815 case RID_PRIVATE:
27816 /* Consume the access-specifier. */
27817 cp_lexer_consume_token (parser->lexer);
27818 /* Remember which access-specifier is active. */
27819 current_access_specifier = token->u.value;
27820 /* Look for the `:'. */
27821 cp_parser_require (parser, CPP_COLON, RT_COLON);
27822 break;
27824 default:
27825 /* Accept #pragmas at class scope. */
27826 if (token->type == CPP_PRAGMA)
27828 cp_parser_pragma (parser, pragma_member, NULL);
27829 break;
27832 /* Otherwise, the next construction must be a
27833 member-declaration. */
27834 cp_parser_member_declaration (parser);
27839 /* Parse a member-declaration.
27841 member-declaration:
27842 decl-specifier-seq [opt] member-declarator-list [opt] ;
27843 function-definition ; [opt]
27844 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27845 using-declaration
27846 template-declaration
27847 alias-declaration
27849 member-declarator-list:
27850 member-declarator
27851 member-declarator-list , member-declarator
27853 member-declarator:
27854 declarator pure-specifier [opt]
27855 declarator constant-initializer [opt]
27856 identifier [opt] : constant-expression
27858 GNU Extensions:
27860 member-declaration:
27861 __extension__ member-declaration
27863 member-declarator:
27864 declarator attributes [opt] pure-specifier [opt]
27865 declarator attributes [opt] constant-initializer [opt]
27866 identifier [opt] attributes [opt] : constant-expression
27868 C++0x Extensions:
27870 member-declaration:
27871 static_assert-declaration */
27873 static void
27874 cp_parser_member_declaration (cp_parser* parser)
27876 cp_decl_specifier_seq decl_specifiers;
27877 tree prefix_attributes;
27878 tree decl;
27879 int declares_class_or_enum;
27880 bool friend_p;
27881 cp_token *token = NULL;
27882 cp_token *decl_spec_token_start = NULL;
27883 cp_token *initializer_token_start = NULL;
27884 int saved_pedantic;
27885 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27887 /* Check for the `__extension__' keyword. */
27888 if (cp_parser_extension_opt (parser, &saved_pedantic))
27890 /* Recurse. */
27891 cp_parser_member_declaration (parser);
27892 /* Restore the old value of the PEDANTIC flag. */
27893 pedantic = saved_pedantic;
27895 return;
27898 /* Check for a template-declaration. */
27899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27901 /* An explicit specialization here is an error condition, and we
27902 expect the specialization handler to detect and report this. */
27903 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27904 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27905 cp_parser_explicit_specialization (parser);
27906 else
27907 cp_parser_template_declaration (parser, /*member_p=*/true);
27909 return;
27911 /* Check for a template introduction. */
27912 else if (cp_parser_template_declaration_after_export (parser, true))
27913 return;
27915 /* Check for a using-declaration. */
27916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27918 if (cxx_dialect < cxx11)
27919 /* Parse the using-declaration. */
27920 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27921 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27922 cp_parser_using_enum (parser);
27923 else
27925 tree decl;
27926 bool alias_decl_expected;
27927 cp_parser_parse_tentatively (parser);
27928 decl = cp_parser_alias_declaration (parser);
27929 /* Note that if we actually see the '=' token after the
27930 identifier, cp_parser_alias_declaration commits the
27931 tentative parse. In that case, we really expect an
27932 alias-declaration. Otherwise, we expect a using
27933 declaration. */
27934 alias_decl_expected =
27935 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27936 cp_parser_parse_definitely (parser);
27938 if (alias_decl_expected)
27939 finish_member_declaration (decl);
27940 else
27941 cp_parser_using_declaration (parser,
27942 /*access_declaration_p=*/false);
27944 return;
27947 /* Check for @defs. */
27948 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27950 tree ivar, member;
27951 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27952 ivar = ivar_chains;
27953 while (ivar)
27955 member = ivar;
27956 ivar = TREE_CHAIN (member);
27957 TREE_CHAIN (member) = NULL_TREE;
27958 finish_member_declaration (member);
27960 return;
27963 /* If the next token is `static_assert' we have a static assertion. */
27964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27966 cp_parser_static_assert (parser, /*member_p=*/true);
27967 return;
27970 parser->colon_corrects_to_scope_p = false;
27972 cp_omp_declare_simd_data odsd;
27973 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27974 goto out;
27976 /* Parse the decl-specifier-seq. */
27977 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27978 cp_parser_decl_specifier_seq (parser,
27979 (CP_PARSER_FLAGS_OPTIONAL
27980 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27981 &decl_specifiers,
27982 &declares_class_or_enum);
27984 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27985 cp_parser_handle_directive_omp_attributes (parser,
27986 &decl_specifiers.attributes,
27987 &odsd, true);
27989 /* Check for an invalid type-name. */
27990 if (!decl_specifiers.any_type_specifiers_p
27991 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27992 goto out;
27993 /* If there is no declarator, then the decl-specifier-seq should
27994 specify a type. */
27995 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27997 /* If there was no decl-specifier-seq, and the next token is a
27998 `;', then we have something like:
28000 struct S { ; };
28002 [class.mem]
28004 Each member-declaration shall declare at least one member
28005 name of the class. */
28006 if (!decl_specifiers.any_specifiers_p)
28008 cp_token *token = cp_lexer_peek_token (parser->lexer);
28009 if (cxx_dialect < cxx11 && !in_system_header_at (token->location))
28011 gcc_rich_location richloc (token->location);
28012 richloc.add_fixit_remove ();
28013 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
28016 else
28018 /* See if this declaration is a friend. */
28019 friend_p = cp_parser_friend_p (&decl_specifiers);
28020 /* If there were decl-specifiers, check to see if there was
28021 a class-declaration. */
28022 tree type = check_tag_decl (&decl_specifiers,
28023 /*explicit_type_instantiation_p=*/false);
28024 /* Nested classes have already been added to the class, but
28025 a `friend' needs to be explicitly registered. */
28026 if (friend_p)
28028 /* If the `friend' keyword was present, the friend must
28029 be introduced with a class-key. */
28030 if (!declares_class_or_enum && cxx_dialect < cxx11)
28031 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
28032 "in C++03 a class-key must be used "
28033 "when declaring a friend");
28034 /* In this case:
28036 template <typename T> struct A {
28037 friend struct A<T>::B;
28040 A<T>::B will be represented by a TYPENAME_TYPE, and
28041 therefore not recognized by check_tag_decl. */
28042 if (!type)
28044 type = decl_specifiers.type;
28045 if (type && TREE_CODE (type) == TYPE_DECL)
28046 type = TREE_TYPE (type);
28048 /* Warn if an attribute cannot appear here, as per
28049 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
28050 we ignore attributes in elaborated-type-specifiers. */
28051 if (!declares_class_or_enum
28052 && cxx11_attribute_p (decl_specifiers.attributes))
28054 decl_specifiers.attributes = NULL_TREE;
28055 if (warning_at (decl_spec_token_start->location,
28056 OPT_Wattributes, "attribute ignored"))
28057 inform (decl_spec_token_start->location, "an attribute "
28058 "that appertains to a friend declaration that "
28059 "is not a definition is ignored");
28061 if (!type || !TYPE_P (type))
28062 error_at (decl_spec_token_start->location,
28063 "friend declaration does not name a class or "
28064 "function");
28065 else
28066 make_friend_class (current_class_type, type,
28067 /*complain=*/true);
28069 /* If there is no TYPE, an error message will already have
28070 been issued. */
28071 else if (!type || type == error_mark_node)
28073 /* An anonymous aggregate has to be handled specially; such
28074 a declaration really declares a data member (with a
28075 particular type), as opposed to a nested class. */
28076 else if (ANON_AGGR_TYPE_P (type))
28078 /* C++11 9.5/6. */
28079 if (decl_specifiers.storage_class != sc_none)
28080 error_at (decl_spec_token_start->location,
28081 "a storage class on an anonymous aggregate "
28082 "in class scope is not allowed");
28084 /* Remove constructors and such from TYPE, now that we
28085 know it is an anonymous aggregate. */
28086 fixup_anonymous_aggr (type);
28087 /* And make the corresponding data member. */
28088 decl = build_decl (decl_spec_token_start->location,
28089 FIELD_DECL, NULL_TREE, type);
28090 /* Add it to the class. */
28091 finish_member_declaration (decl);
28093 else
28094 cp_parser_check_access_in_redeclaration
28095 (TYPE_NAME (type),
28096 decl_spec_token_start->location);
28099 else
28101 bool assume_semicolon = false;
28103 /* Clear attributes from the decl_specifiers but keep them
28104 around as prefix attributes that apply them to the entity
28105 being declared. */
28106 prefix_attributes = decl_specifiers.attributes;
28107 decl_specifiers.attributes = NULL_TREE;
28108 if (parser->omp_declare_simd
28109 && (parser->omp_declare_simd->attribs[0]
28110 == &decl_specifiers.attributes))
28111 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
28113 /* See if these declarations will be friends. */
28114 friend_p = cp_parser_friend_p (&decl_specifiers);
28116 /* Keep going until we hit the `;' at the end of the
28117 declaration. */
28118 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28120 tree attributes = NULL_TREE;
28121 tree first_attribute;
28122 tree initializer;
28123 bool named_bitfld = false;
28125 /* Peek at the next token. */
28126 token = cp_lexer_peek_token (parser->lexer);
28128 /* The following code wants to know early if it is a bit-field
28129 or some other declaration. Attributes can appear before
28130 the `:' token. Skip over them without consuming any tokens
28131 to peek if they are followed by `:'. */
28132 if (cp_next_tokens_can_be_attribute_p (parser)
28133 || (token->type == CPP_NAME
28134 && cp_nth_tokens_can_be_attribute_p (parser, 2)
28135 && (named_bitfld = true)))
28137 size_t n
28138 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
28139 token = cp_lexer_peek_nth_token (parser->lexer, n);
28142 /* Check for a bitfield declaration. */
28143 if (token->type == CPP_COLON
28144 || (token->type == CPP_NAME
28145 && token == cp_lexer_peek_token (parser->lexer)
28146 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
28147 && (named_bitfld = true)))
28149 tree identifier;
28150 tree width;
28151 tree late_attributes = NULL_TREE;
28152 location_t id_location
28153 = cp_lexer_peek_token (parser->lexer)->location;
28155 if (named_bitfld)
28156 identifier = cp_parser_identifier (parser);
28157 else
28158 identifier = NULL_TREE;
28160 /* Look for attributes that apply to the bitfield. */
28161 attributes = cp_parser_attributes_opt (parser);
28163 /* Consume the `:' token. */
28164 cp_lexer_consume_token (parser->lexer);
28166 /* Get the width of the bitfield. */
28167 width = cp_parser_constant_expression (parser, false, NULL,
28168 cxx_dialect >= cxx11);
28170 /* In C++20 and as extension for C++11 and above we allow
28171 default member initializers for bit-fields. */
28172 initializer = NULL_TREE;
28173 if (cxx_dialect >= cxx11
28174 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
28175 || cp_lexer_next_token_is (parser->lexer,
28176 CPP_OPEN_BRACE)))
28178 location_t loc
28179 = cp_lexer_peek_token (parser->lexer)->location;
28180 if (cxx_dialect < cxx20
28181 && identifier != NULL_TREE)
28182 pedwarn (loc, OPT_Wc__20_extensions,
28183 "default member initializers for bit-fields "
28184 "only available with %<-std=c++20%> or "
28185 "%<-std=gnu++20%>");
28187 initializer = cp_parser_save_nsdmi (parser);
28188 if (identifier == NULL_TREE)
28190 error_at (loc, "default member initializer for "
28191 "unnamed bit-field");
28192 initializer = NULL_TREE;
28195 else
28197 /* Look for attributes that apply to the bitfield after
28198 the `:' token and width. This is where GCC used to
28199 parse attributes in the past, pedwarn if there is
28200 a std attribute. */
28201 if (cp_next_tokens_can_be_std_attribute_p (parser))
28202 pedwarn (input_location, OPT_Wpedantic,
28203 "ISO C++ allows bit-field attributes only "
28204 "before the %<:%> token");
28206 late_attributes = cp_parser_attributes_opt (parser);
28209 attributes = attr_chainon (attributes, late_attributes);
28211 /* Remember which attributes are prefix attributes and
28212 which are not. */
28213 first_attribute = attributes;
28214 /* Combine the attributes. */
28215 attributes = attr_chainon (prefix_attributes, attributes);
28217 /* Create the bitfield declaration. */
28218 decl = grokbitfield (identifier
28219 ? make_id_declarator (NULL_TREE,
28220 identifier,
28221 sfk_none,
28222 id_location)
28223 : NULL,
28224 &decl_specifiers,
28225 width, initializer,
28226 attributes);
28228 else
28230 cp_declarator *declarator;
28231 tree asm_specification;
28232 int ctor_dtor_or_conv_p;
28233 bool static_p = (decl_specifiers.storage_class == sc_static);
28234 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
28235 /* We can't delay parsing for friends,
28236 alias-declarations, and typedefs, even though the
28237 standard seems to require it. */
28238 if (!friend_p
28239 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28240 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
28242 /* Parse the declarator. */
28243 declarator
28244 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28245 flags,
28246 &ctor_dtor_or_conv_p,
28247 /*parenthesized_p=*/NULL,
28248 /*member_p=*/true,
28249 friend_p, static_p);
28251 /* If something went wrong parsing the declarator, make sure
28252 that we at least consume some tokens. */
28253 if (declarator == cp_error_declarator)
28255 /* Skip to the end of the statement. */
28256 cp_parser_skip_to_end_of_statement (parser);
28257 /* If the next token is not a semicolon, that is
28258 probably because we just skipped over the body of
28259 a function. So, we consume a semicolon if
28260 present, but do not issue an error message if it
28261 is not present. */
28262 if (cp_lexer_next_token_is (parser->lexer,
28263 CPP_SEMICOLON))
28264 cp_lexer_consume_token (parser->lexer);
28265 goto out;
28268 /* Handle class-scope non-template C++17 deduction guides. */
28269 cp_parser_maybe_adjust_declarator_for_dguide (parser,
28270 &decl_specifiers,
28271 declarator,
28272 &ctor_dtor_or_conv_p);
28274 if (declares_class_or_enum & 2)
28275 cp_parser_check_for_definition_in_return_type
28276 (declarator, decl_specifiers.type,
28277 decl_specifiers.locations[ds_type_spec]);
28279 /* Look for an asm-specification. */
28280 asm_specification = cp_parser_asm_specification_opt (parser);
28281 /* Look for attributes that apply to the declaration. */
28282 attributes = cp_parser_attributes_opt (parser);
28283 /* Remember which attributes are prefix attributes and
28284 which are not. */
28285 first_attribute = attributes;
28286 /* Combine the attributes. */
28287 attributes = attr_chainon (prefix_attributes, attributes);
28289 /* If it's an `=', then we have a constant-initializer or a
28290 pure-specifier. It is not correct to parse the
28291 initializer before registering the member declaration
28292 since the member declaration should be in scope while
28293 its initializer is processed. However, the rest of the
28294 front end does not yet provide an interface that allows
28295 us to handle this correctly. */
28296 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28298 /* In [class.mem]:
28300 A pure-specifier shall be used only in the declaration of
28301 a virtual function.
28303 A member-declarator can contain a constant-initializer
28304 only if it declares a static member of integral or
28305 enumeration type.
28307 Therefore, if the DECLARATOR is for a function, we look
28308 for a pure-specifier; otherwise, we look for a
28309 constant-initializer. When we call `grokfield', it will
28310 perform more stringent semantics checks. */
28311 initializer_token_start = cp_lexer_peek_token (parser->lexer);
28312 declarator->init_loc = initializer_token_start->location;
28313 if (function_declarator_p (declarator)
28314 || (decl_specifiers.type
28315 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
28316 && declarator->kind == cdk_id
28317 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
28318 == FUNCTION_TYPE)))
28319 initializer = cp_parser_pure_specifier (parser);
28320 else if (decl_specifiers.storage_class != sc_static)
28321 initializer = cp_parser_save_nsdmi (parser);
28322 else if (cxx_dialect >= cxx11)
28324 /* Don't require a constant rvalue in C++11, since we
28325 might want a reference constant. We'll enforce
28326 constancy later. */
28327 cp_lexer_consume_token (parser->lexer);
28328 /* Parse the initializer. */
28329 initializer = cp_parser_initializer_clause (parser);
28331 else
28332 /* Parse the initializer. */
28333 initializer = cp_parser_constant_initializer (parser);
28335 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
28336 && !function_declarator_p (declarator))
28338 declarator->init_loc
28339 = cp_lexer_peek_token (parser->lexer)->location;
28340 if (decl_specifiers.storage_class != sc_static)
28341 initializer = cp_parser_save_nsdmi (parser);
28342 else
28343 initializer = cp_parser_initializer (parser);
28345 /* Detect invalid bit-field cases such as
28347 int *p : 4;
28348 int &&r : 3;
28350 and similar. */
28351 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
28352 /* If there were no type specifiers, it was a
28353 constructor. */
28354 && decl_specifiers.any_type_specifiers_p)
28356 /* This is called for a decent diagnostic only. */
28357 tree d = grokdeclarator (declarator, &decl_specifiers,
28358 BITFIELD, /*initialized=*/false,
28359 &attributes);
28360 if (!error_operand_p (d))
28361 error_at (DECL_SOURCE_LOCATION (d),
28362 "bit-field %qD has non-integral type %qT",
28363 d, TREE_TYPE (d));
28364 cp_parser_skip_to_end_of_statement (parser);
28365 /* Avoid "extra ;" pedwarns. */
28366 if (cp_lexer_next_token_is (parser->lexer,
28367 CPP_SEMICOLON))
28368 cp_lexer_consume_token (parser->lexer);
28369 goto out;
28371 /* Otherwise, there is no initializer. */
28372 else
28373 initializer = NULL_TREE;
28375 /* See if we are probably looking at a function
28376 definition. We are certainly not looking at a
28377 member-declarator. Calling `grokfield' has
28378 side-effects, so we must not do it unless we are sure
28379 that we are looking at a member-declarator. */
28380 if (cp_parser_token_starts_function_definition_p
28381 (cp_lexer_peek_token (parser->lexer)))
28383 /* The grammar does not allow a pure-specifier to be
28384 used when a member function is defined. (It is
28385 possible that this fact is an oversight in the
28386 standard, since a pure function may be defined
28387 outside of the class-specifier. */
28388 if (initializer && initializer_token_start)
28389 error_at (initializer_token_start->location,
28390 "pure-specifier on function-definition");
28391 decl = cp_parser_save_member_function_body (parser,
28392 &decl_specifiers,
28393 declarator,
28394 attributes);
28396 if (parser->fully_implicit_function_template_p)
28397 decl = finish_fully_implicit_template (parser, decl);
28398 /* If the member was not a friend, declare it here. */
28399 if (!friend_p)
28400 finish_member_declaration (decl);
28401 /* Peek at the next token. */
28402 token = cp_lexer_peek_token (parser->lexer);
28403 /* If the next token is a semicolon, consume it. */
28404 if (token->type == CPP_SEMICOLON)
28406 location_t semicolon_loc
28407 = cp_lexer_consume_token (parser->lexer)->location;
28408 gcc_rich_location richloc (semicolon_loc);
28409 richloc.add_fixit_remove ();
28410 warning_at (&richloc, OPT_Wextra_semi,
28411 "extra %<;%> after in-class "
28412 "function definition");
28414 goto out;
28416 else
28417 if (declarator->kind == cdk_function)
28418 declarator->id_loc = token->location;
28420 /* Create the declaration. */
28421 decl = grokfield (declarator, &decl_specifiers,
28422 initializer, /*init_const_expr_p=*/true,
28423 asm_specification, attributes);
28425 if (parser->fully_implicit_function_template_p)
28427 if (friend_p)
28428 finish_fully_implicit_template (parser, 0);
28429 else
28430 decl = finish_fully_implicit_template (parser, decl);
28434 cp_finalize_omp_declare_simd (parser, decl);
28435 cp_finalize_oacc_routine (parser, decl, false);
28437 /* Reset PREFIX_ATTRIBUTES. */
28438 if (attributes != error_mark_node)
28440 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28441 attributes = TREE_CHAIN (attributes);
28442 if (attributes)
28443 TREE_CHAIN (attributes) = NULL_TREE;
28446 /* If there is any qualification still in effect, clear it
28447 now; we will be starting fresh with the next declarator. */
28448 parser->scope = NULL_TREE;
28449 parser->qualifying_scope = NULL_TREE;
28450 parser->object_scope = NULL_TREE;
28451 /* If it's a `,', then there are more declarators. */
28452 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28454 cp_lexer_consume_token (parser->lexer);
28455 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28457 cp_token *token = cp_lexer_previous_token (parser->lexer);
28458 gcc_rich_location richloc (token->location);
28459 richloc.add_fixit_remove ();
28460 error_at (&richloc, "stray %<,%> at end of "
28461 "member declaration");
28464 /* If the next token isn't a `;', then we have a parse error. */
28465 else if (cp_lexer_next_token_is_not (parser->lexer,
28466 CPP_SEMICOLON))
28468 /* The next token might be a ways away from where the
28469 actual semicolon is missing. Find the previous token
28470 and use that for our error position. */
28471 cp_token *token = cp_lexer_previous_token (parser->lexer);
28472 gcc_rich_location richloc (token->location);
28473 richloc.add_fixit_insert_after (";");
28474 error_at (&richloc, "expected %<;%> at end of "
28475 "member declaration");
28477 /* Assume that the user meant to provide a semicolon. If
28478 we were to cp_parser_skip_to_end_of_statement, we might
28479 skip to a semicolon inside a member function definition
28480 and issue nonsensical error messages. */
28481 assume_semicolon = true;
28484 if (decl)
28486 /* Add DECL to the list of members. */
28487 if (!friend_p
28488 /* Explicitly include, eg, NSDMIs, for better error
28489 recovery (c++/58650). */
28490 || !DECL_DECLARES_FUNCTION_P (decl))
28491 finish_member_declaration (decl);
28493 if (DECL_DECLARES_FUNCTION_P (decl))
28494 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28495 else if (TREE_CODE (decl) == FIELD_DECL
28496 && DECL_INITIAL (decl))
28497 /* Add DECL to the queue of NSDMI to be parsed later. */
28498 vec_safe_push (unparsed_nsdmis, decl);
28501 if (assume_semicolon)
28502 goto out;
28506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28507 out:
28508 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28509 cp_finalize_omp_declare_simd (parser, &odsd);
28512 /* Parse a pure-specifier.
28514 pure-specifier:
28517 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28518 Otherwise, ERROR_MARK_NODE is returned. */
28520 static tree
28521 cp_parser_pure_specifier (cp_parser* parser)
28523 cp_token *token;
28525 /* Look for the `=' token. */
28526 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28527 return error_mark_node;
28528 /* Look for the `0' token. */
28529 token = cp_lexer_peek_token (parser->lexer);
28531 if (token->type == CPP_EOF
28532 || token->type == CPP_PRAGMA_EOL)
28533 return error_mark_node;
28535 cp_lexer_consume_token (parser->lexer);
28537 /* Accept = default or = delete in c++0x mode. */
28538 if (token->keyword == RID_DEFAULT
28539 || token->keyword == RID_DELETE)
28541 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28542 return token->u.value;
28545 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28546 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28548 cp_parser_error (parser,
28549 "invalid pure specifier (only %<= 0%> is allowed)");
28550 cp_parser_skip_to_end_of_statement (parser);
28551 return error_mark_node;
28553 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28555 error_at (token->location, "templates may not be %<virtual%>");
28556 return error_mark_node;
28559 return integer_zero_node;
28562 /* Parse a constant-initializer.
28564 constant-initializer:
28565 = constant-expression
28567 Returns a representation of the constant-expression. */
28569 static tree
28570 cp_parser_constant_initializer (cp_parser* parser)
28572 /* Look for the `=' token. */
28573 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28574 return error_mark_node;
28576 /* It is invalid to write:
28578 struct S { static const int i = { 7 }; };
28581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28583 cp_parser_error (parser,
28584 "a brace-enclosed initializer is not allowed here");
28585 /* Consume the opening brace. */
28586 matching_braces braces;
28587 braces.consume_open (parser);
28588 /* Skip the initializer. */
28589 cp_parser_skip_to_closing_brace (parser);
28590 /* Look for the trailing `}'. */
28591 braces.require_close (parser);
28593 return error_mark_node;
28596 return cp_parser_constant_expression (parser);
28599 /* Derived classes [gram.class.derived] */
28601 /* Parse a base-clause.
28603 base-clause:
28604 : base-specifier-list
28606 base-specifier-list:
28607 base-specifier ... [opt]
28608 base-specifier-list , base-specifier ... [opt]
28610 Returns a TREE_LIST representing the base-classes, in the order in
28611 which they were declared. The representation of each node is as
28612 described by cp_parser_base_specifier.
28614 In the case that no bases are specified, this function will return
28615 NULL_TREE, not ERROR_MARK_NODE. */
28617 static tree
28618 cp_parser_base_clause (cp_parser* parser)
28620 tree bases = NULL_TREE;
28622 /* Look for the `:' that begins the list. */
28623 cp_parser_require (parser, CPP_COLON, RT_COLON);
28625 /* Scan the base-specifier-list. */
28626 while (true)
28628 cp_token *token;
28629 tree base;
28630 bool pack_expansion_p = false;
28632 /* Look for the base-specifier. */
28633 base = cp_parser_base_specifier (parser);
28634 /* Look for the (optional) ellipsis. */
28635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28637 /* Consume the `...'. */
28638 cp_lexer_consume_token (parser->lexer);
28640 pack_expansion_p = true;
28643 /* Add BASE to the front of the list. */
28644 if (base && base != error_mark_node)
28646 if (pack_expansion_p)
28647 /* Make this a pack expansion type. */
28648 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28650 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28652 TREE_CHAIN (base) = bases;
28653 bases = base;
28656 /* Peek at the next token. */
28657 token = cp_lexer_peek_token (parser->lexer);
28658 /* If it's not a comma, then the list is complete. */
28659 if (token->type != CPP_COMMA)
28660 break;
28661 /* Consume the `,'. */
28662 cp_lexer_consume_token (parser->lexer);
28665 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28666 base class had a qualified name. However, the next name that
28667 appears is certainly not qualified. */
28668 parser->scope = NULL_TREE;
28669 parser->qualifying_scope = NULL_TREE;
28670 parser->object_scope = NULL_TREE;
28672 return nreverse (bases);
28675 /* Parse a base-specifier.
28677 base-specifier:
28678 :: [opt] nested-name-specifier [opt] class-name
28679 virtual access-specifier [opt] :: [opt] nested-name-specifier
28680 [opt] class-name
28681 access-specifier virtual [opt] :: [opt] nested-name-specifier
28682 [opt] class-name
28684 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28685 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28686 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28687 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28689 static tree
28690 cp_parser_base_specifier (cp_parser* parser)
28692 cp_token *token;
28693 bool done = false;
28694 bool virtual_p = false;
28695 bool duplicate_virtual_error_issued_p = false;
28696 bool duplicate_access_error_issued_p = false;
28697 bool class_scope_p, template_p;
28698 tree access = access_default_node;
28699 tree type;
28701 /* Process the optional `virtual' and `access-specifier'. */
28702 while (!done)
28704 /* Peek at the next token. */
28705 token = cp_lexer_peek_token (parser->lexer);
28706 /* Process `virtual'. */
28707 switch (token->keyword)
28709 case RID_VIRTUAL:
28710 /* If `virtual' appears more than once, issue an error. */
28711 if (virtual_p && !duplicate_virtual_error_issued_p)
28713 cp_parser_error (parser,
28714 "%<virtual%> specified more than once in base-specifier");
28715 duplicate_virtual_error_issued_p = true;
28718 virtual_p = true;
28720 /* Consume the `virtual' token. */
28721 cp_lexer_consume_token (parser->lexer);
28723 break;
28725 case RID_PUBLIC:
28726 case RID_PROTECTED:
28727 case RID_PRIVATE:
28728 /* If more than one access specifier appears, issue an
28729 error. */
28730 if (access != access_default_node
28731 && !duplicate_access_error_issued_p)
28733 cp_parser_error (parser,
28734 "more than one access specifier in base-specifier");
28735 duplicate_access_error_issued_p = true;
28738 access = ridpointers[(int) token->keyword];
28740 /* Consume the access-specifier. */
28741 cp_lexer_consume_token (parser->lexer);
28743 break;
28745 default:
28746 done = true;
28747 break;
28750 /* It is not uncommon to see programs mechanically, erroneously, use
28751 the 'typename' keyword to denote (dependent) qualified types
28752 as base classes. */
28753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28755 token = cp_lexer_peek_token (parser->lexer);
28756 if (!processing_template_decl)
28757 error_at (token->location,
28758 "keyword %<typename%> not allowed outside of templates");
28759 else
28760 error_at (token->location,
28761 "keyword %<typename%> not allowed in this context "
28762 "(the base class is implicitly a type)");
28763 cp_lexer_consume_token (parser->lexer);
28766 /* Look for the optional `::' operator. */
28767 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28768 /* Look for the nested-name-specifier. The simplest way to
28769 implement:
28771 [temp.res]
28773 The keyword `typename' is not permitted in a base-specifier or
28774 mem-initializer; in these contexts a qualified name that
28775 depends on a template-parameter is implicitly assumed to be a
28776 type name.
28778 is to pretend that we have seen the `typename' keyword at this
28779 point. */
28780 cp_parser_nested_name_specifier_opt (parser,
28781 /*typename_keyword_p=*/true,
28782 /*check_dependency_p=*/true,
28783 /*type_p=*/true,
28784 /*is_declaration=*/true);
28785 /* If the base class is given by a qualified name, assume that names
28786 we see are type names or templates, as appropriate. */
28787 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28788 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28790 if (!parser->scope
28791 && cp_lexer_next_token_is_decltype (parser->lexer))
28792 /* DR 950 allows decltype as a base-specifier. */
28793 type = cp_parser_decltype (parser);
28794 else
28796 /* Otherwise, look for the class-name. */
28797 type = cp_parser_class_name (parser,
28798 class_scope_p,
28799 template_p,
28800 typename_type,
28801 /*check_dependency_p=*/true,
28802 /*class_head_p=*/false,
28803 /*is_declaration=*/true);
28804 type = TREE_TYPE (type);
28807 if (type == error_mark_node)
28808 return error_mark_node;
28810 return finish_base_specifier (type, access, virtual_p);
28813 /* Exception handling [gram.exception] */
28815 /* Save the tokens that make up the noexcept-specifier for a member-function.
28816 Returns a DEFERRED_PARSE. */
28818 static tree
28819 cp_parser_save_noexcept (cp_parser *parser)
28821 cp_token *first = parser->lexer->next_token;
28822 /* We want everything up to, including, the final ')'. */
28823 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28824 cp_token *last = parser->lexer->next_token;
28826 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28827 to carry the information we will need. */
28828 tree expr = make_node (DEFERRED_PARSE);
28829 /* Save away the noexcept-specifier; we will process it when the
28830 class is complete. */
28831 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28832 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28833 expr = build_tree_list (expr, NULL_TREE);
28834 return expr;
28837 /* Used for late processing of noexcept-specifiers of member-functions.
28838 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28839 we saved for later; parse it now. DECL is the declaration of the
28840 member function. */
28842 static tree
28843 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28845 /* Make sure we've gotten something that hasn't been parsed yet. */
28846 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28848 push_unparsed_function_queues (parser);
28850 /* Push the saved tokens for the noexcept-specifier onto the parser's
28851 lexer stack. */
28852 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28853 cp_parser_push_lexer_for_tokens (parser, tokens);
28855 /* Parse the cached noexcept-specifier. */
28856 tree parsed_arg
28857 = cp_parser_noexcept_specification_opt (parser,
28858 CP_PARSER_FLAGS_NONE,
28859 /*require_constexpr=*/true,
28860 /*consumed_expr=*/NULL,
28861 /*return_cond=*/false);
28863 /* Revert to the main lexer. */
28864 cp_parser_pop_lexer (parser);
28866 /* Restore the queue. */
28867 pop_unparsed_function_queues (parser);
28869 /* And we're done. */
28870 return parsed_arg;
28873 /* Perform late checking of overriding function with respect to their
28874 noexcept-specifiers. FNDECL is the member function that potentially
28875 overrides some virtual function with the same signature. */
28877 static void
28878 noexcept_override_late_checks (tree fndecl)
28880 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28881 tree base_binfo;
28883 if (DECL_STATIC_FUNCTION_P (fndecl))
28884 return;
28886 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28888 tree basetype = BINFO_TYPE (base_binfo);
28890 if (!TYPE_POLYMORPHIC_P (basetype))
28891 continue;
28893 tree fn = look_for_overrides_here (basetype, fndecl);
28894 if (fn)
28895 maybe_check_overriding_exception_spec (fndecl, fn);
28899 /* Parse an (optional) noexcept-specification.
28901 noexcept-specification:
28902 noexcept ( constant-expression ) [opt]
28904 If no noexcept-specification is present, returns NULL_TREE.
28905 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28906 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28907 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28908 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28909 in which case a boolean condition is returned instead. The parser flags
28910 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28911 the (member) function is `const'. */
28913 static tree
28914 cp_parser_noexcept_specification_opt (cp_parser* parser,
28915 cp_parser_flags flags,
28916 bool require_constexpr,
28917 bool* consumed_expr,
28918 bool return_cond)
28920 cp_token *token;
28921 const char *saved_message;
28923 /* Peek at the next token. */
28924 token = cp_lexer_peek_token (parser->lexer);
28926 /* Is it a noexcept-specification? */
28927 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28929 tree expr;
28931 /* [class.mem]/6 says that a noexcept-specifer (within the
28932 member-specification of the class) is a complete-class context of
28933 a class. So, if the noexcept-specifier has the optional expression,
28934 just save the tokens, and reparse this after we're done with the
28935 class. */
28937 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28938 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28939 /* No need to delay parsing for a number literal or true/false. */
28940 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28941 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28942 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28943 && at_class_scope_p ()
28944 && TYPE_BEING_DEFINED (current_class_type)
28945 && !LAMBDA_TYPE_P (current_class_type))
28946 return cp_parser_save_noexcept (parser);
28948 cp_lexer_consume_token (parser->lexer);
28950 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28952 matching_parens parens;
28953 parens.consume_open (parser);
28955 if (require_constexpr)
28957 /* Types may not be defined in an exception-specification. */
28958 saved_message = parser->type_definition_forbidden_message;
28959 parser->type_definition_forbidden_message
28960 = G_("types may not be defined in an exception-specification");
28962 bool non_constant_p;
28963 expr
28964 = cp_parser_constant_expression (parser,
28965 /*allow_non_constant=*/true,
28966 &non_constant_p);
28967 if (non_constant_p
28968 && !require_potential_rvalue_constant_expression (expr))
28970 expr = NULL_TREE;
28971 return_cond = true;
28974 /* Restore the saved message. */
28975 parser->type_definition_forbidden_message = saved_message;
28977 else
28979 expr = cp_parser_expression (parser);
28980 *consumed_expr = true;
28983 parens.require_close (parser);
28985 else
28987 expr = boolean_true_node;
28988 if (!require_constexpr)
28989 *consumed_expr = false;
28992 /* We cannot build a noexcept-spec right away because this will check
28993 that expr is a constexpr. */
28994 if (!return_cond)
28995 return build_noexcept_spec (expr, tf_warning_or_error);
28996 else
28997 return expr;
28999 else
29000 return NULL_TREE;
29003 /* Parse an (optional) exception-specification.
29005 exception-specification:
29006 throw ( type-id-list [opt] )
29008 Returns a TREE_LIST representing the exception-specification. The
29009 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
29010 control parsing. QUALS are qualifiers indicating whether the (member)
29011 function is `const'. */
29013 static tree
29014 cp_parser_exception_specification_opt (cp_parser* parser,
29015 cp_parser_flags flags)
29017 cp_token *token;
29018 tree type_id_list;
29019 const char *saved_message;
29021 /* Peek at the next token. */
29022 token = cp_lexer_peek_token (parser->lexer);
29024 /* Is it a noexcept-specification? */
29025 type_id_list
29026 = cp_parser_noexcept_specification_opt (parser, flags,
29027 /*require_constexpr=*/true,
29028 /*consumed_expr=*/NULL,
29029 /*return_cond=*/false);
29030 if (type_id_list != NULL_TREE)
29031 return type_id_list;
29033 /* If it's not `throw', then there's no exception-specification. */
29034 if (!cp_parser_is_keyword (token, RID_THROW))
29035 return NULL_TREE;
29037 location_t loc = token->location;
29039 /* Consume the `throw'. */
29040 cp_lexer_consume_token (parser->lexer);
29042 /* Look for the `('. */
29043 matching_parens parens;
29044 parens.require_open (parser);
29046 /* Peek at the next token. */
29047 token = cp_lexer_peek_token (parser->lexer);
29048 /* If it's not a `)', then there is a type-id-list. */
29049 if (token->type != CPP_CLOSE_PAREN)
29051 /* Types may not be defined in an exception-specification. */
29052 saved_message = parser->type_definition_forbidden_message;
29053 parser->type_definition_forbidden_message
29054 = G_("types may not be defined in an exception-specification");
29055 /* Parse the type-id-list. */
29056 type_id_list = cp_parser_type_id_list (parser);
29057 /* Restore the saved message. */
29058 parser->type_definition_forbidden_message = saved_message;
29060 if (cxx_dialect >= cxx17)
29062 error_at (loc, "ISO C++17 does not allow dynamic exception "
29063 "specifications");
29064 type_id_list = NULL_TREE;
29066 else if (cxx_dialect >= cxx11)
29067 warning_at (loc, OPT_Wdeprecated,
29068 "dynamic exception specifications are deprecated in "
29069 "C++11");
29071 /* In C++17, throw() is equivalent to noexcept (true). throw()
29072 is deprecated in C++11 and above as well, but is still widely used,
29073 so don't warn about it yet. */
29074 else if (cxx_dialect >= cxx17)
29075 type_id_list = noexcept_true_spec;
29076 else
29077 type_id_list = empty_except_spec;
29079 /* Look for the `)'. */
29080 parens.require_close (parser);
29082 return type_id_list;
29085 /* Parse an (optional) type-id-list.
29087 type-id-list:
29088 type-id ... [opt]
29089 type-id-list , type-id ... [opt]
29091 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
29092 in the order that the types were presented. */
29094 static tree
29095 cp_parser_type_id_list (cp_parser* parser)
29097 tree types = NULL_TREE;
29099 while (true)
29101 cp_token *token;
29102 tree type;
29104 token = cp_lexer_peek_token (parser->lexer);
29106 /* Get the next type-id. */
29107 type = cp_parser_type_id (parser);
29108 /* Check for invalid 'auto'. */
29109 if (flag_concepts && type_uses_auto (type))
29111 error_at (token->location,
29112 "invalid use of %<auto%> in exception-specification");
29113 type = error_mark_node;
29115 /* Parse the optional ellipsis. */
29116 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29118 /* Consume the `...'. */
29119 cp_lexer_consume_token (parser->lexer);
29121 /* Turn the type into a pack expansion expression. */
29122 type = make_pack_expansion (type);
29124 /* Add it to the list. */
29125 types = add_exception_specifier (types, type, /*complain=*/1);
29126 /* Peek at the next token. */
29127 token = cp_lexer_peek_token (parser->lexer);
29128 /* If it is not a `,', we are done. */
29129 if (token->type != CPP_COMMA)
29130 break;
29131 /* Consume the `,'. */
29132 cp_lexer_consume_token (parser->lexer);
29135 return nreverse (types);
29138 /* Parse a try-block.
29140 try-block:
29141 try compound-statement handler-seq */
29143 static tree
29144 cp_parser_try_block (cp_parser* parser)
29146 tree try_block;
29148 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
29149 if (parser->in_function_body
29150 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
29151 && cxx_dialect < cxx20)
29152 pedwarn (input_location, OPT_Wc__20_extensions,
29153 "%<try%> in %<constexpr%> function only "
29154 "available with %<-std=c++20%> or %<-std=gnu++20%>");
29156 try_block = begin_try_block ();
29157 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
29158 finish_try_block (try_block);
29159 cp_parser_handler_seq (parser);
29160 finish_handler_sequence (try_block);
29162 return try_block;
29165 /* Parse a function-try-block.
29167 function-try-block:
29168 try ctor-initializer [opt] function-body handler-seq */
29170 static void
29171 cp_parser_function_try_block (cp_parser* parser)
29173 tree compound_stmt;
29174 tree try_block;
29176 /* Look for the `try' keyword. */
29177 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
29178 return;
29179 /* Let the rest of the front end know where we are. */
29180 try_block = begin_function_try_block (&compound_stmt);
29181 /* Parse the function-body. */
29182 cp_parser_ctor_initializer_opt_and_function_body
29183 (parser, /*in_function_try_block=*/true);
29184 /* We're done with the `try' part. */
29185 finish_function_try_block (try_block);
29186 /* Parse the handlers. */
29187 cp_parser_handler_seq (parser);
29188 /* We're done with the handlers. */
29189 finish_function_handler_sequence (try_block, compound_stmt);
29192 /* Parse a handler-seq.
29194 handler-seq:
29195 handler handler-seq [opt] */
29197 static void
29198 cp_parser_handler_seq (cp_parser* parser)
29200 while (true)
29202 cp_token *token;
29204 /* Parse the handler. */
29205 cp_parser_handler (parser);
29206 /* Peek at the next token. */
29207 token = cp_lexer_peek_token (parser->lexer);
29208 /* If it's not `catch' then there are no more handlers. */
29209 if (!cp_parser_is_keyword (token, RID_CATCH))
29210 break;
29214 /* Parse a handler.
29216 handler:
29217 catch ( exception-declaration ) compound-statement */
29219 static void
29220 cp_parser_handler (cp_parser* parser)
29222 tree handler;
29223 tree declaration;
29225 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
29226 handler = begin_handler ();
29227 matching_parens parens;
29228 parens.require_open (parser);
29229 declaration = cp_parser_exception_declaration (parser);
29230 finish_handler_parms (declaration, handler);
29231 parens.require_close (parser);
29232 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29233 finish_handler (handler);
29236 /* Parse an exception-declaration.
29238 exception-declaration:
29239 type-specifier-seq declarator
29240 type-specifier-seq abstract-declarator
29241 type-specifier-seq
29244 Returns a VAR_DECL for the declaration, or NULL_TREE if the
29245 ellipsis variant is used. */
29247 static tree
29248 cp_parser_exception_declaration (cp_parser* parser)
29250 cp_decl_specifier_seq type_specifiers;
29251 cp_declarator *declarator;
29252 const char *saved_message;
29254 /* If it's an ellipsis, it's easy to handle. */
29255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29257 /* Consume the `...' token. */
29258 cp_lexer_consume_token (parser->lexer);
29259 return NULL_TREE;
29262 /* Types may not be defined in exception-declarations. */
29263 saved_message = parser->type_definition_forbidden_message;
29264 parser->type_definition_forbidden_message
29265 = G_("types may not be defined in exception-declarations");
29267 /* Parse the type-specifier-seq. */
29268 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
29269 /*is_declaration=*/true,
29270 /*is_trailing_return=*/false,
29271 &type_specifiers);
29272 /* If it's a `)', then there is no declarator. */
29273 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29274 declarator = NULL;
29275 else
29276 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
29277 CP_PARSER_FLAGS_NONE,
29278 /*ctor_dtor_or_conv_p=*/NULL,
29279 /*parenthesized_p=*/NULL,
29280 /*member_p=*/false,
29281 /*friend_p=*/false,
29282 /*static_p=*/false);
29284 /* Restore the saved message. */
29285 parser->type_definition_forbidden_message = saved_message;
29287 if (!type_specifiers.any_specifiers_p)
29288 return error_mark_node;
29290 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
29293 /* Parse a throw-expression.
29295 throw-expression:
29296 throw assignment-expression [opt]
29298 Returns a THROW_EXPR representing the throw-expression. */
29300 static tree
29301 cp_parser_throw_expression (cp_parser* parser)
29303 tree expression;
29304 cp_token* token;
29305 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29307 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
29308 token = cp_lexer_peek_token (parser->lexer);
29309 /* Figure out whether or not there is an assignment-expression
29310 following the "throw" keyword. */
29311 if (token->type == CPP_COMMA
29312 || token->type == CPP_SEMICOLON
29313 || token->type == CPP_CLOSE_PAREN
29314 || token->type == CPP_CLOSE_SQUARE
29315 || token->type == CPP_CLOSE_BRACE
29316 || token->type == CPP_COLON)
29317 expression = NULL_TREE;
29318 else
29319 expression = cp_parser_assignment_expression (parser);
29321 /* Construct a location e.g.:
29322 throw x
29323 ^~~~~~~
29324 with caret == start at the start of the "throw" token, and
29325 the end at the end of the final token we consumed. */
29326 location_t combined_loc = make_location (start_loc, start_loc,
29327 parser->lexer);
29328 expression = build_throw (combined_loc, expression, tf_warning_or_error);
29330 return expression;
29333 /* Parse a yield-expression.
29335 yield-expression:
29336 co_yield assignment-expression
29337 co_yield braced-init-list
29339 Returns a CO_YIELD_EXPR representing the yield-expression. */
29341 static tree
29342 cp_parser_yield_expression (cp_parser* parser)
29344 tree expr;
29346 cp_token *token = cp_lexer_peek_token (parser->lexer);
29347 location_t kw_loc = token->location; /* Save for later. */
29349 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
29351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29353 cp_lexer_set_source_position (parser->lexer);
29354 /* ??? : probably a moot point? */
29355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29356 expr = cp_parser_braced_list (parser);
29358 else
29359 expr = cp_parser_assignment_expression (parser);
29361 if (expr == error_mark_node)
29362 return expr;
29364 return finish_co_yield_expr (kw_loc, expr);
29367 /* GNU Extensions */
29369 /* Parse an (optional) asm-specification.
29371 asm-specification:
29372 asm ( string-literal )
29374 If the asm-specification is present, returns a STRING_CST
29375 corresponding to the string-literal. Otherwise, returns
29376 NULL_TREE. */
29378 static tree
29379 cp_parser_asm_specification_opt (cp_parser* parser)
29381 /* Peek at the next token. */
29382 cp_token *token = cp_lexer_peek_token (parser->lexer);
29383 /* If the next token isn't the `asm' keyword, then there's no
29384 asm-specification. */
29385 if (!cp_parser_is_keyword (token, RID_ASM))
29386 return NULL_TREE;
29388 /* Consume the `asm' token. */
29389 cp_lexer_consume_token (parser->lexer);
29390 /* Look for the `('. */
29391 matching_parens parens;
29392 parens.require_open (parser);
29394 /* Look for the string-literal. */
29395 tree asm_specification = cp_parser_string_literal (parser,
29396 /*translate=*/false,
29397 /*wide_ok=*/false);
29399 /* Look for the `)'. */
29400 parens.require_close (parser);
29402 return asm_specification;
29405 /* Parse an asm-operand-list.
29407 asm-operand-list:
29408 asm-operand
29409 asm-operand-list , asm-operand
29411 asm-operand:
29412 string-literal ( expression )
29413 [ string-literal ] string-literal ( expression )
29415 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29416 each node is the expression. The TREE_PURPOSE is itself a
29417 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29418 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29419 is a STRING_CST for the string literal before the parenthesis. Returns
29420 ERROR_MARK_NODE if any of the operands are invalid. */
29422 static tree
29423 cp_parser_asm_operand_list (cp_parser* parser)
29425 tree asm_operands = NULL_TREE;
29426 bool invalid_operands = false;
29428 while (true)
29430 tree name;
29432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29434 /* Consume the `[' token. */
29435 cp_lexer_consume_token (parser->lexer);
29436 /* Read the operand name. */
29437 name = cp_parser_identifier (parser);
29438 if (name != error_mark_node)
29439 name = build_string (IDENTIFIER_LENGTH (name),
29440 IDENTIFIER_POINTER (name));
29441 /* Look for the closing `]'. */
29442 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29444 else
29445 name = NULL_TREE;
29446 /* Look for the string-literal. */
29447 tree string_literal = cp_parser_string_literal (parser,
29448 /*translate=*/false,
29449 /*wide_ok=*/false);
29451 /* Look for the `('. */
29452 matching_parens parens;
29453 parens.require_open (parser);
29454 /* Parse the expression. */
29455 tree expression = cp_parser_expression (parser);
29456 /* Look for the `)'. */
29457 parens.require_close (parser);
29459 if (name == error_mark_node
29460 || string_literal == error_mark_node
29461 || expression == error_mark_node)
29462 invalid_operands = true;
29464 /* Add this operand to the list. */
29465 asm_operands = tree_cons (build_tree_list (name, string_literal),
29466 expression,
29467 asm_operands);
29468 /* If the next token is not a `,', there are no more
29469 operands. */
29470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29471 break;
29472 /* Consume the `,'. */
29473 cp_lexer_consume_token (parser->lexer);
29476 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29479 /* Parse an asm-clobber-list.
29481 asm-clobber-list:
29482 string-literal
29483 asm-clobber-list , string-literal
29485 Returns a TREE_LIST, indicating the clobbers in the order that they
29486 appeared. The TREE_VALUE of each node is a STRING_CST. */
29488 static tree
29489 cp_parser_asm_clobber_list (cp_parser* parser)
29491 tree clobbers = NULL_TREE;
29493 while (true)
29495 /* Look for the string literal. */
29496 tree string_literal = cp_parser_string_literal (parser,
29497 /*translate=*/false,
29498 /*wide_ok=*/false);
29499 /* Add it to the list. */
29500 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29501 /* If the next token is not a `,', then the list is
29502 complete. */
29503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29504 break;
29505 /* Consume the `,' token. */
29506 cp_lexer_consume_token (parser->lexer);
29509 return clobbers;
29512 /* Parse an asm-label-list.
29514 asm-label-list:
29515 identifier
29516 asm-label-list , identifier
29518 Returns a TREE_LIST, indicating the labels in the order that they
29519 appeared. The TREE_VALUE of each node is a label. */
29521 static tree
29522 cp_parser_asm_label_list (cp_parser* parser)
29524 tree labels = NULL_TREE;
29526 while (true)
29528 tree identifier, label, name;
29530 /* Look for the identifier. */
29531 identifier = cp_parser_identifier (parser);
29532 if (!error_operand_p (identifier))
29534 label = lookup_label (identifier);
29535 if (TREE_CODE (label) == LABEL_DECL)
29537 TREE_USED (label) = 1;
29538 check_goto (label);
29539 name = build_string (IDENTIFIER_LENGTH (identifier),
29540 IDENTIFIER_POINTER (identifier));
29541 labels = tree_cons (name, label, labels);
29544 /* If the next token is not a `,', then the list is
29545 complete. */
29546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29547 break;
29548 /* Consume the `,' token. */
29549 cp_lexer_consume_token (parser->lexer);
29552 return nreverse (labels);
29555 /* Return TRUE iff the next tokens in the stream are possibly the
29556 beginning of a GNU extension attribute. */
29558 static bool
29559 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29561 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29564 /* Return TRUE iff the next tokens in the stream are possibly the
29565 beginning of a standard C++-11 attribute specifier. */
29567 static bool
29568 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29570 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29573 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29574 beginning of a standard C++-11 attribute specifier. */
29576 static bool
29577 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29579 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29581 return (cxx_dialect >= cxx11
29582 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29583 || (token->type == CPP_OPEN_SQUARE
29584 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29585 && token->type == CPP_OPEN_SQUARE)));
29588 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29589 beginning of a GNU extension attribute. */
29591 static bool
29592 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29594 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29596 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29599 /* Return true iff the next tokens can be the beginning of either a
29600 GNU attribute list, or a standard C++11 attribute sequence. */
29602 static bool
29603 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29605 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29606 || cp_next_tokens_can_be_std_attribute_p (parser));
29609 /* Return true iff the next Nth tokens can be the beginning of either
29610 a GNU attribute list, or a standard C++11 attribute sequence. */
29612 static bool
29613 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29615 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29616 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29619 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29620 of GNU attributes, or return NULL. */
29622 static tree
29623 cp_parser_attributes_opt (cp_parser *parser)
29625 tree attrs = NULL_TREE;
29626 while (true)
29628 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29629 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29630 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29631 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29632 else
29633 break;
29635 return attrs;
29638 /* Parse an (optional) series of attributes.
29640 attributes:
29641 attributes attribute
29643 attribute:
29644 __attribute__ (( attribute-list [opt] ))
29646 The return value is as for cp_parser_gnu_attribute_list. */
29648 static tree
29649 cp_parser_gnu_attributes_opt (cp_parser* parser)
29651 tree attributes = NULL_TREE;
29653 auto cleanup = make_temp_override
29654 (parser->auto_is_implicit_function_template_parm_p, false);
29656 while (true)
29658 cp_token *token;
29659 tree attribute_list;
29660 bool ok = true;
29662 /* Peek at the next token. */
29663 token = cp_lexer_peek_token (parser->lexer);
29664 /* If it's not `__attribute__', then we're done. */
29665 if (token->keyword != RID_ATTRIBUTE)
29666 break;
29668 /* Consume the `__attribute__' keyword. */
29669 cp_lexer_consume_token (parser->lexer);
29670 /* Look for the two `(' tokens. */
29671 matching_parens outer_parens;
29672 if (!outer_parens.require_open (parser))
29673 ok = false;
29674 matching_parens inner_parens;
29675 if (!inner_parens.require_open (parser))
29676 ok = false;
29678 /* Peek at the next token. */
29679 token = cp_lexer_peek_token (parser->lexer);
29680 if (token->type != CPP_CLOSE_PAREN)
29681 /* Parse the attribute-list. */
29682 attribute_list = cp_parser_gnu_attribute_list (parser);
29683 else
29684 /* If the next token is a `)', then there is no attribute
29685 list. */
29686 attribute_list = NULL;
29688 /* Look for the two `)' tokens. */
29689 if (!inner_parens.require_close (parser))
29690 ok = false;
29691 if (!outer_parens.require_close (parser))
29692 ok = false;
29693 if (!ok)
29694 cp_parser_skip_to_end_of_statement (parser);
29696 /* Add these new attributes to the list. */
29697 attributes = attr_chainon (attributes, attribute_list);
29700 return attributes;
29703 /* Parse a GNU attribute-list.
29705 attribute-list:
29706 attribute
29707 attribute-list , attribute
29709 attribute:
29710 identifier
29711 identifier ( identifier )
29712 identifier ( identifier , expression-list )
29713 identifier ( expression-list )
29715 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29716 to an attribute. The TREE_PURPOSE of each node is the identifier
29717 indicating which attribute is in use. The TREE_VALUE represents
29718 the arguments, if any. */
29720 static tree
29721 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29723 tree attribute_list = NULL_TREE;
29724 bool save_translate_strings_p = parser->translate_strings_p;
29726 /* Don't create wrapper nodes within attributes: the
29727 handlers don't know how to handle them. */
29728 auto_suppress_location_wrappers sentinel;
29730 parser->translate_strings_p = false;
29731 while (true)
29733 cp_token *token;
29734 tree identifier;
29735 tree attribute;
29737 /* Look for the identifier. We also allow keywords here; for
29738 example `__attribute__ ((const))' is legal. */
29739 token = cp_lexer_peek_token (parser->lexer);
29740 if (token->type == CPP_NAME
29741 || token->type == CPP_KEYWORD)
29743 tree arguments = NULL_TREE;
29745 /* Consume the token, but save it since we need it for the
29746 SIMD enabled function parsing. */
29747 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29749 /* Save away the identifier that indicates which attribute
29750 this is. */
29751 identifier = (token->type == CPP_KEYWORD)
29752 /* For keywords, use the canonical spelling, not the
29753 parsed identifier. */
29754 ? ridpointers[(int) token->keyword]
29755 : id_token->u.value;
29757 identifier = canonicalize_attr_name (identifier);
29758 attribute = build_tree_list (identifier, NULL_TREE);
29760 /* Peek at the next token. */
29761 token = cp_lexer_peek_token (parser->lexer);
29762 /* If it's an `(', then parse the attribute arguments. */
29763 if (token->type == CPP_OPEN_PAREN)
29765 vec<tree, va_gc> *vec;
29766 int attr_flag = (attribute_takes_identifier_p (identifier)
29767 ? id_attr : normal_attr);
29768 if (is_attribute_p ("assume", identifier))
29769 attr_flag = assume_attr;
29770 vec = cp_parser_parenthesized_expression_list
29771 (parser, attr_flag, /*cast_p=*/false,
29772 /*allow_expansion_p=*/false,
29773 /*non_constant_p=*/NULL);
29774 if (vec == NULL)
29775 arguments = error_mark_node;
29776 else
29778 arguments = build_tree_list_vec (vec);
29779 release_tree_vector (vec);
29781 /* Save the arguments away. */
29782 TREE_VALUE (attribute) = arguments;
29785 if (arguments != error_mark_node)
29787 /* Add this attribute to the list. */
29788 TREE_CHAIN (attribute) = attribute_list;
29789 attribute_list = attribute;
29792 token = cp_lexer_peek_token (parser->lexer);
29794 /* Unless EXACTLY_ONE is set look for more attributes.
29795 If the next token isn't a `,', we're done. */
29796 if (exactly_one || token->type != CPP_COMMA)
29797 break;
29799 /* Consume the comma and keep going. */
29800 cp_lexer_consume_token (parser->lexer);
29802 parser->translate_strings_p = save_translate_strings_p;
29804 /* We built up the list in reverse order. */
29805 return nreverse (attribute_list);
29808 /* Parse arguments of omp::directive attribute.
29810 ( directive-name ,[opt] clause-list[opt] )
29812 For directive just remember the first/last tokens for subsequent
29813 parsing. */
29815 static void
29816 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
29818 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29819 if (first->type == CPP_CLOSE_PAREN)
29821 cp_lexer_consume_token (parser->lexer);
29822 error_at (first->location, "expected OpenMP directive name");
29823 cp_lexer_consume_token (parser->lexer);
29824 TREE_VALUE (attribute) = NULL_TREE;
29825 return;
29827 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29828 if (n == 1)
29830 cp_lexer_consume_token (parser->lexer);
29831 error_at (first->location, "expected attribute argument as balanced "
29832 "token sequence");
29833 TREE_VALUE (attribute) = NULL_TREE;
29834 return;
29836 for (n = n - 2; n; --n)
29837 cp_lexer_consume_token (parser->lexer);
29838 cp_token *last = cp_lexer_peek_token (parser->lexer);
29839 cp_lexer_consume_token (parser->lexer);
29840 tree arg = make_node (DEFERRED_PARSE);
29841 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29842 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29843 if (decl_p)
29844 TREE_PUBLIC (arg) = 1;
29845 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29848 /* Parse arguments of omp::sequence attribute.
29850 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29852 static void
29853 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29855 matching_parens parens;
29856 parens.consume_open (parser);
29859 cp_token *token = cp_lexer_peek_token (parser->lexer);
29860 if (token->type == CPP_NAME
29861 && token->u.value == omp_identifier
29862 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29864 cp_lexer_consume_token (parser->lexer);
29865 cp_lexer_consume_token (parser->lexer);
29866 token = cp_lexer_peek_token (parser->lexer);
29868 bool directive = false;
29869 const char *p;
29870 if (token->type != CPP_NAME)
29871 p = "";
29872 else
29873 p = IDENTIFIER_POINTER (token->u.value);
29874 if (strcmp (p, "directive") == 0)
29875 directive = true;
29876 else if (strcmp (p, "sequence") != 0)
29878 error_at (token->location, "expected %<directive%> or %<sequence%>");
29879 cp_parser_skip_to_closing_parenthesis (parser,
29880 /*recovering=*/true,
29881 /*or_comma=*/true,
29882 /*consume_paren=*/false);
29883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29884 break;
29885 cp_lexer_consume_token (parser->lexer);
29887 cp_lexer_consume_token (parser->lexer);
29888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29889 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29890 UNKNOWN_LOCATION);
29891 else if (directive)
29892 cp_parser_omp_directive_args (parser, attribute, false);
29893 else
29894 cp_parser_omp_sequence_args (parser, attribute);
29895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29896 break;
29897 cp_lexer_consume_token (parser->lexer);
29899 while (1);
29900 if (!parens.require_close (parser))
29901 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29902 /*consume_paren=*/true);
29905 /* Parse a standard C++11 attribute.
29907 The returned representation is a TREE_LIST which TREE_PURPOSE is
29908 the scoped name of the attribute, and the TREE_VALUE is its
29909 arguments list.
29911 Note that the scoped name of the attribute is itself a TREE_LIST
29912 which TREE_PURPOSE is the namespace of the attribute, and
29913 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29914 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29915 and which TREE_PURPOSE is directly the attribute name.
29917 Clients of the attribute code should use get_attribute_namespace
29918 and get_attribute_name to get the actual namespace and name of
29919 attributes, regardless of their being GNU or C++11 attributes.
29921 attribute:
29922 attribute-token attribute-argument-clause [opt]
29924 attribute-token:
29925 identifier
29926 attribute-scoped-token
29928 attribute-scoped-token:
29929 attribute-namespace :: identifier
29931 attribute-namespace:
29932 identifier
29934 attribute-argument-clause:
29935 ( balanced-token-seq )
29937 balanced-token-seq:
29938 balanced-token [opt]
29939 balanced-token-seq balanced-token
29941 balanced-token:
29942 ( balanced-token-seq )
29943 [ balanced-token-seq ]
29944 { balanced-token-seq }. */
29946 static tree
29947 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29949 tree attribute, attr_id = NULL_TREE, arguments;
29950 cp_token *token;
29952 auto cleanup = make_temp_override
29953 (parser->auto_is_implicit_function_template_parm_p, false);
29955 /* First, parse name of the attribute, a.k.a attribute-token. */
29957 token = cp_lexer_peek_token (parser->lexer);
29958 if (token->type == CPP_NAME)
29959 attr_id = token->u.value;
29960 else if (token->type == CPP_KEYWORD)
29961 attr_id = ridpointers[(int) token->keyword];
29962 else if (token->flags & NAMED_OP)
29963 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29965 if (attr_id == NULL_TREE)
29966 return NULL_TREE;
29968 cp_lexer_consume_token (parser->lexer);
29970 token = cp_lexer_peek_token (parser->lexer);
29971 if (token->type == CPP_SCOPE)
29973 /* We are seeing a scoped attribute token. */
29975 cp_lexer_consume_token (parser->lexer);
29976 if (attr_ns)
29977 error_at (token->location, "attribute using prefix used together "
29978 "with scoped attribute token");
29979 attr_ns = attr_id;
29981 token = cp_lexer_peek_token (parser->lexer);
29982 if (token->type == CPP_NAME)
29983 attr_id = token->u.value;
29984 else if (token->type == CPP_KEYWORD)
29985 attr_id = ridpointers[(int) token->keyword];
29986 else if (token->flags & NAMED_OP)
29987 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29988 else
29990 error_at (token->location,
29991 "expected an identifier for the attribute name");
29992 return error_mark_node;
29994 cp_lexer_consume_token (parser->lexer);
29996 attr_ns = canonicalize_attr_name (attr_ns);
29997 attr_id = canonicalize_attr_name (attr_id);
29998 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29999 NULL_TREE);
30000 token = cp_lexer_peek_token (parser->lexer);
30002 else if (attr_ns)
30004 attr_ns = canonicalize_attr_name (attr_ns);
30005 attr_id = canonicalize_attr_name (attr_id);
30006 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
30007 NULL_TREE);
30009 else
30011 attr_id = canonicalize_attr_name (attr_id);
30012 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
30013 NULL_TREE);
30015 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
30016 but no longer: we have to be able to tell [[noreturn]] and
30017 __attribute__((noreturn)) apart. */
30018 /* C++14 deprecated attribute is equivalent to GNU's. */
30019 if (is_attribute_p ("deprecated", attr_id))
30020 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30021 /* C++17 fallthrough attribute is equivalent to GNU's. */
30022 else if (is_attribute_p ("fallthrough", attr_id))
30023 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30024 /* C++23 assume attribute is equivalent to GNU's. */
30025 else if (is_attribute_p ("assume", attr_id))
30026 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30027 /* Transactional Memory TS optimize_for_synchronized attribute is
30028 equivalent to GNU transaction_callable. */
30029 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
30030 TREE_PURPOSE (attribute)
30031 = get_identifier ("transaction_callable");
30032 /* Transactional Memory attributes are GNU attributes. */
30033 else if (tm_attr_to_mask (attr_id))
30034 TREE_PURPOSE (attribute) = attr_id;
30037 /* Now parse the optional argument clause of the attribute. */
30039 if (token->type != CPP_OPEN_PAREN)
30041 if ((flag_openmp || flag_openmp_simd)
30042 && attr_ns == omp_identifier
30043 && (is_attribute_p ("directive", attr_id)
30044 || is_attribute_p ("sequence", attr_id)
30045 || is_attribute_p ("decl", attr_id)))
30047 error_at (token->location, "%<omp::%E%> attribute requires argument",
30048 attr_id);
30049 return NULL_TREE;
30051 return attribute;
30055 vec<tree, va_gc> *vec;
30056 int attr_flag = normal_attr;
30058 /* Maybe we don't expect to see any arguments for this attribute. */
30059 const attribute_spec *as
30060 = lookup_attribute_spec (TREE_PURPOSE (attribute));
30061 if (as && as->max_length == 0)
30063 error_at (token->location, "%qE attribute does not take any arguments",
30064 attr_id);
30065 cp_parser_skip_to_closing_parenthesis (parser,
30066 /*recovering=*/true,
30067 /*or_comma=*/false,
30068 /*consume_paren=*/true);
30069 return error_mark_node;
30072 if (is_attribute_p ("assume", attr_id)
30073 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
30074 /* The assume attribute needs special handling of the argument. */
30075 attr_flag = assume_attr;
30076 else if (attr_ns == gnu_identifier
30077 && attribute_takes_identifier_p (attr_id))
30078 /* A GNU attribute that takes an identifier in parameter. */
30079 attr_flag = id_attr;
30080 else if (attr_ns == NULL_TREE
30081 && cxx_dialect >= cxx26
30082 && (is_attribute_p ("deprecated", attr_id)
30083 || is_attribute_p ("nodiscard", attr_id)))
30084 attr_flag = uneval_string_attr;
30086 /* If this is a fake attribute created to handle -Wno-attributes,
30087 we must skip parsing the arguments. */
30088 if (as == NULL || attribute_ignored_p (as))
30090 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
30092 if (is_attribute_p ("directive", attr_id))
30094 cp_parser_omp_directive_args (parser, attribute, false);
30095 return attribute;
30097 else if (is_attribute_p ("decl", attr_id))
30099 TREE_VALUE (TREE_PURPOSE (attribute))
30100 = get_identifier ("directive");
30101 cp_parser_omp_directive_args (parser, attribute, true);
30102 return attribute;
30104 else if (is_attribute_p ("sequence", attr_id))
30106 TREE_VALUE (TREE_PURPOSE (attribute))
30107 = get_identifier ("directive");
30108 cp_parser_omp_sequence_args (parser, attribute);
30109 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
30110 return attribute;
30114 /* For unknown attributes, just skip balanced tokens instead of
30115 trying to parse the arguments. Set TREE_VALUE (attribute) to
30116 error_mark_node to distinguish skipped arguments from attributes
30117 with no arguments. */
30118 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
30119 cp_lexer_consume_token (parser->lexer);
30120 TREE_VALUE (attribute) = error_mark_node;
30121 return attribute;
30124 vec = cp_parser_parenthesized_expression_list
30125 (parser, attr_flag, /*cast_p=*/false,
30126 /*allow_expansion_p=*/true,
30127 /*non_constant_p=*/NULL);
30128 if (vec == NULL)
30129 arguments = error_mark_node;
30130 else
30132 if (vec->is_empty ())
30133 /* e.g. [[attr()]]. */
30134 error_at (token->location, "parentheses must be omitted if "
30135 "%qE attribute argument list is empty",
30136 attr_id);
30137 arguments = build_tree_list_vec (vec);
30138 release_tree_vector (vec);
30141 if (arguments == error_mark_node)
30142 attribute = error_mark_node;
30143 else
30144 TREE_VALUE (attribute) = arguments;
30147 return attribute;
30150 /* Warn if the attribute ATTRIBUTE appears more than once in the
30151 attribute-list ATTRIBUTES. This used to be enforced for certain
30152 attributes, but the restriction was removed in P2156.
30153 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
30154 found in ATTRIBUTES. */
30156 static bool
30157 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
30159 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
30160 "likely", "unlikely", "fallthrough",
30161 "no_unique_address", "carries_dependency" };
30162 if (attributes)
30163 for (const auto &a : alist)
30164 if (is_attribute_p (a, get_attribute_name (attribute))
30165 && is_attribute_namespace_p ("", attribute)
30166 && lookup_attribute ("", a, attributes))
30168 if (!from_macro_expansion_at (loc))
30169 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
30170 "multiple times", a);
30171 return false;
30173 return true;
30176 /* Parse a list of standard C++-11 attributes.
30178 attribute-list:
30179 attribute [opt]
30180 attribute-list , attribute[opt]
30181 attribute ...
30182 attribute-list , attribute ...
30185 static tree
30186 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
30188 tree attributes = NULL_TREE, attribute = NULL_TREE;
30189 cp_token *token = NULL;
30191 while (true)
30193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30194 attribute = cp_parser_std_attribute (parser, attr_ns);
30195 if (attribute == error_mark_node)
30196 break;
30197 if (attribute != NULL_TREE)
30199 if (cp_parser_check_std_attribute (loc, attributes, attribute))
30201 TREE_CHAIN (attribute) = attributes;
30202 attributes = attribute;
30205 token = cp_lexer_peek_token (parser->lexer);
30206 if (token->type == CPP_ELLIPSIS)
30208 cp_lexer_consume_token (parser->lexer);
30209 if (attribute == NULL_TREE)
30210 error_at (token->location,
30211 "expected attribute before %<...%>");
30212 else if (TREE_VALUE (attribute) == NULL_TREE)
30214 error_at (token->location, "attribute with no arguments "
30215 "contains no parameter packs");
30216 return error_mark_node;
30218 else if (TREE_VALUE (attribute) != error_mark_node)
30220 tree pack = make_pack_expansion (TREE_VALUE (attribute));
30221 if (pack == error_mark_node)
30222 return error_mark_node;
30223 TREE_VALUE (attribute) = pack;
30225 token = cp_lexer_peek_token (parser->lexer);
30227 if (token->type != CPP_COMMA)
30228 break;
30229 cp_lexer_consume_token (parser->lexer);
30231 attributes = nreverse (attributes);
30232 return attributes;
30235 /* Optionally parse a C++20 contract role. A NULL return means that no
30236 contract role was specified.
30238 contract-role:
30239 % default
30240 % identifier
30242 If the identifier does not name a known contract role, it will
30243 be assumed to be default. Returns the identifier for the role
30244 token. */
30246 static tree
30247 cp_parser_contract_role (cp_parser *parser)
30249 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
30250 cp_lexer_consume_token (parser->lexer);
30252 cp_token *token = cp_lexer_peek_token (parser->lexer);
30253 tree role_id = NULL_TREE;
30254 if (token->type == CPP_NAME)
30255 role_id = token->u.value;
30256 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30257 role_id = get_identifier ("default");
30258 else
30260 error_at (token->location, "expected contract-role");
30261 return error_mark_node;
30263 cp_lexer_consume_token (parser->lexer);
30265 /* FIXME: Warn about invalid/unknown roles? */
30266 return role_id;
30269 /* Parse an optional contract mode.
30271 contract-mode:
30272 contract-semantic
30273 [contract-level] [contract-role]
30275 contract-semantic:
30276 check_never_continue
30277 check_maybe_continue
30278 check_always_continue
30280 contract-level:
30281 default
30282 audit
30283 axiom
30285 contract-role:
30286 default
30287 identifier
30289 This grammar is taken from P1332R0. During parsing, this sets options
30290 on the MODE object to determine the configuration of the contract.
30292 Returns a tree containing the identifiers used in the configuration.
30293 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
30294 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
30295 contract-role, if any. NULL_TREE is returned if no information is
30296 given (i.e., all defaults selected). */
30298 static tree
30299 cp_parser_contract_mode_opt (cp_parser *parser,
30300 bool postcondition_p)
30302 /* The mode is empty; the level and role are default. */
30303 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30304 return NULL_TREE;
30306 /* There is only a role; the level is default. */
30307 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30309 tree role_id = cp_parser_contract_role (parser);
30310 return build_tree_list (role_id, get_identifier ("default"));
30313 /* Otherwise, match semantic or level. */
30314 cp_token *token = cp_lexer_peek_token (parser->lexer);
30315 contract_level level = CONTRACT_INVALID;
30316 contract_semantic semantic = CCS_INVALID;
30317 tree config_id;
30318 if (token->type == CPP_NAME)
30320 config_id = token->u.value;
30322 /* Either a named level, a concrete semantic, or an identifier
30323 for a postcondition. */
30324 const char *ident = IDENTIFIER_POINTER (token->u.value);
30325 level = map_contract_level (ident);
30326 semantic = map_contract_semantic (ident);
30328 /* The identifier is the return value for a postcondition. */
30329 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
30330 && postcondition_p)
30331 return NULL_TREE;
30333 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30335 config_id = get_identifier ("default");
30336 level = CONTRACT_DEFAULT;
30338 else
30340 /* We got some other token other than a ':'. */
30341 error_at (token->location, "expected contract semantic or level");
30342 return NULL_TREE;
30345 /* Consume the literal semantic or level token. */
30346 cp_lexer_consume_token (parser->lexer);
30348 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
30350 error_at (token->location,
30351 "expected contract level: "
30352 "%<default%>, %<audit%>, or %<axiom%>");
30353 return NULL_TREE;
30356 /* We matched an explicit semantic. */
30357 if (semantic != CCS_INVALID)
30359 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30361 error ("invalid use of contract role for explicit semantic");
30362 cp_lexer_consume_token (parser->lexer);
30363 cp_lexer_consume_token (parser->lexer);
30365 return config_id;
30368 /* We matched a level, there may be a role; otherwise this is default. */
30369 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30371 tree role_id = cp_parser_contract_role (parser);
30372 return build_tree_list (role_id, config_id);
30375 return build_tree_list (NULL_TREE, config_id);
30378 static tree
30379 find_error (tree *tp, int *, void *)
30381 if (*tp == error_mark_node)
30382 return *tp;
30383 return NULL_TREE;
30386 static bool
30387 contains_error_p (tree t)
30389 return walk_tree (&t, find_error, NULL, NULL);
30392 /* Parse a standard C++20 contract attribute specifier.
30394 contract-attribute-specifier:
30395 [ [ assert contract-level [opt] : conditional-expression ] ]
30396 [ [ pre contract-level [opt] : conditional-expression ] ]
30397 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
30399 For free functions, we cannot determine the type of the postcondition
30400 identifier because the we haven't called grokdeclarator yet. In those
30401 cases we parse the postcondition as if the identifier was declared as
30402 'auto <identifier>'. We then instantiate the postcondition once the
30403 return type is known.
30405 For member functions, contracts are in the complete-class context, so the
30406 parse is deferred. We also have the return type avaialable (unless it's
30407 deduced), so we don't need to parse the postcondition in terms of a
30408 placeholder. */
30410 static tree
30411 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30413 gcc_assert (contract_attribute_p (attribute));
30414 cp_token *token = cp_lexer_consume_token (parser->lexer);
30415 location_t loc = token->location;
30417 bool assertion_p = is_attribute_p ("assert", attribute);
30418 bool postcondition_p = is_attribute_p ("post", attribute);
30420 /* Parse the optional mode. */
30421 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30423 /* Check for postcondition identifiers. */
30424 cp_expr identifier;
30425 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30426 identifier = cp_parser_identifier (parser);
30427 if (identifier == error_mark_node)
30428 return error_mark_node;
30430 cp_parser_require (parser, CPP_COLON, RT_COLON);
30432 /* Defer the parsing of pre/post contracts inside class definitions. */
30433 tree contract;
30434 if (!assertion_p &&
30435 current_class_type &&
30436 TYPE_BEING_DEFINED (current_class_type))
30438 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30439 that doesn't close the attribute, return an error and let the attribute
30440 handling code emit an error for missing ']]'. */
30441 cp_token *first = cp_lexer_peek_token (parser->lexer);
30442 cp_parser_skip_to_closing_parenthesis_1 (parser,
30443 /*recovering=*/false,
30444 CPP_CLOSE_SQUARE,
30445 /*consume_paren=*/false);
30446 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30447 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30448 return error_mark_node;
30449 cp_token *last = cp_lexer_peek_token (parser->lexer);
30451 /* Build a deferred-parse node. */
30452 tree condition = make_node (DEFERRED_PARSE);
30453 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30454 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30456 /* And its corresponding contract. */
30457 contract = grok_contract (attribute, mode, identifier, condition, loc);
30459 else
30461 /* Enable location wrappers when parsing contracts. */
30462 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30464 /* Build a fake variable for the result identifier. */
30465 tree result = NULL_TREE;
30466 if (identifier)
30468 begin_scope (sk_block, NULL_TREE);
30469 result = make_postcondition_variable (identifier);
30470 ++processing_template_decl;
30473 /* Parse the condition, ensuring that parameters or the return variable
30474 aren't flagged for use outside the body of a function. */
30475 ++processing_contract_condition;
30476 cp_expr condition = cp_parser_conditional_expression (parser);
30477 --processing_contract_condition;
30479 /* Try to recover from errors by scanning up to the end of the
30480 attribute. Sometimes we get partially parsed expressions, so
30481 we need to search the condition for errors. */
30482 if (contains_error_p (condition))
30483 cp_parser_skip_up_to_closing_square_bracket (parser);
30485 /* Build the contract. */
30486 contract = grok_contract (attribute, mode, result, condition, loc);
30488 /* Leave our temporary scope for the postcondition result. */
30489 if (result)
30491 --processing_template_decl;
30492 pop_bindings_and_leave_scope ();
30496 if (!flag_contracts)
30498 error_at (loc, "contracts are only available with %<-fcontracts%>");
30499 return error_mark_node;
30502 return finish_contract_attribute (attribute, contract);
30505 /* Parse a contract condition for a deferred contract. */
30507 void cp_parser_late_contract_condition (cp_parser *parser,
30508 tree fn,
30509 tree attribute)
30511 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30513 /* Make sure we've gotten something that hasn't been parsed yet or that
30514 we're not parsing an invalid contract. */
30515 tree condition = CONTRACT_CONDITION (contract);
30516 if (TREE_CODE (condition) != DEFERRED_PARSE)
30517 return;
30519 tree identifier = NULL_TREE;
30520 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30521 identifier = POSTCONDITION_IDENTIFIER (contract);
30523 /* Build a fake variable for the result identifier. */
30524 tree result = NULL_TREE;
30525 if (identifier)
30527 /* TODO: Can we guarantee that the identifier has a location? */
30528 location_t loc = cp_expr_location (contract);
30529 tree type = TREE_TYPE (TREE_TYPE (fn));
30530 if (!check_postcondition_result (fn, type, loc))
30532 invalidate_contract (contract);
30533 return;
30536 begin_scope (sk_block, NULL_TREE);
30537 result = make_postcondition_variable (identifier, type);
30538 ++processing_template_decl;
30541 /* 'this' is not allowed in preconditions of constructors or in postconditions
30542 of destructors. Note that the previous value of this variable is
30543 established by the calling function, so we need to save it here. */
30544 tree saved_ccr = current_class_ref;
30545 tree saved_ccp = current_class_ptr;
30546 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30547 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30549 current_class_ref = current_class_ptr = NULL_TREE;
30550 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30553 push_unparsed_function_queues (parser);
30555 /* Push the saved tokens onto the parser's lexer stack. */
30556 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30557 cp_parser_push_lexer_for_tokens (parser, tokens);
30559 /* Parse the condition, ensuring that parameters or the return variable
30560 aren't flagged for use outside the body of a function. */
30561 ++processing_contract_condition;
30562 condition = cp_parser_conditional_expression (parser);
30563 --processing_contract_condition;
30565 /* Revert to the main lexer. */
30566 cp_parser_pop_lexer (parser);
30568 /* Restore the queue. */
30569 pop_unparsed_function_queues (parser);
30571 current_class_ref = saved_ccr;
30572 current_class_ptr = saved_ccp;
30574 /* Commit to changes. */
30575 update_late_contract (contract, result, condition);
30577 /* Leave our temporary scope for the postcondition result. */
30578 if (result)
30580 --processing_template_decl;
30581 pop_bindings_and_leave_scope ();
30585 /* Parse a standard C++-11 attribute specifier.
30587 attribute-specifier:
30588 [ [ attribute-using-prefix [opt] attribute-list ] ]
30589 contract-attribute-specifier
30590 alignment-specifier
30592 attribute-using-prefix:
30593 using attribute-namespace :
30595 alignment-specifier:
30596 alignas ( type-id ... [opt] )
30597 alignas ( alignment-expression ... [opt] ).
30599 Extensions for contracts:
30601 contract-attribute-specifier:
30602 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30603 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30604 [ [ post : contract-mode [opt] identifier [opt] :
30605 conditional-expression ] ]
30607 Return void_list_node if the current token doesn't start an
30608 attribute-specifier to differentiate from NULL_TREE returned e.g.
30609 for [ [ ] ]. */
30611 static tree
30612 cp_parser_std_attribute_spec (cp_parser *parser)
30614 tree attributes = NULL_TREE;
30615 cp_token *token = cp_lexer_peek_token (parser->lexer);
30617 if (token->type == CPP_OPEN_SQUARE
30618 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30620 tree attr_ns = NULL_TREE;
30621 tree attr_name = NULL_TREE;
30623 cp_lexer_consume_token (parser->lexer);
30624 cp_lexer_consume_token (parser->lexer);
30626 token = cp_lexer_peek_token (parser->lexer);
30627 if (token->type == CPP_NAME)
30629 attr_name = token->u.value;
30630 attr_name = canonicalize_attr_name (attr_name);
30633 /* Handle contract-attribute-specs specially. */
30634 if (attr_name && contract_attribute_p (attr_name))
30636 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30637 if (attrs != error_mark_node)
30638 attributes = attrs;
30639 goto finish_attrs;
30642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30644 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30645 if (token->type == CPP_NAME)
30646 attr_ns = token->u.value;
30647 else if (token->type == CPP_KEYWORD)
30648 attr_ns = ridpointers[(int) token->keyword];
30649 else if (token->flags & NAMED_OP)
30650 attr_ns = get_identifier (cpp_type2name (token->type,
30651 token->flags));
30652 if (attr_ns
30653 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30655 if (cxx_dialect < cxx17)
30656 pedwarn (input_location, OPT_Wc__17_extensions,
30657 "attribute using prefix only available "
30658 "with %<-std=c++17%> or %<-std=gnu++17%>");
30660 cp_lexer_consume_token (parser->lexer);
30661 cp_lexer_consume_token (parser->lexer);
30662 cp_lexer_consume_token (parser->lexer);
30664 else
30665 attr_ns = NULL_TREE;
30668 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30670 finish_attrs:
30671 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30672 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30673 cp_parser_skip_to_end_of_statement (parser);
30674 else
30675 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30676 when we are sure that we have actually parsed them. */
30677 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30679 else
30681 tree alignas_expr;
30683 /* Look for an alignment-specifier. */
30685 token = cp_lexer_peek_token (parser->lexer);
30687 if (token->type != CPP_KEYWORD
30688 || token->keyword != RID_ALIGNAS)
30689 return void_list_node;
30691 cp_lexer_consume_token (parser->lexer);
30692 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30694 matching_parens parens;
30695 if (!parens.require_open (parser))
30696 return error_mark_node;
30698 cp_parser_parse_tentatively (parser);
30699 alignas_expr = cp_parser_type_id (parser);
30701 if (!cp_parser_parse_definitely (parser))
30703 alignas_expr = cp_parser_assignment_expression (parser);
30704 if (alignas_expr == error_mark_node)
30705 cp_parser_skip_to_end_of_statement (parser);
30706 if (alignas_expr == NULL_TREE
30707 || alignas_expr == error_mark_node)
30708 return alignas_expr;
30711 alignas_expr = cxx_alignas_expr (alignas_expr);
30712 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30714 /* Handle alignas (pack...). */
30715 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30717 cp_lexer_consume_token (parser->lexer);
30718 alignas_expr = make_pack_expansion (alignas_expr);
30721 /* Something went wrong, so don't build the attribute. */
30722 if (alignas_expr == error_mark_node)
30723 return error_mark_node;
30725 /* Missing ')' means the code cannot possibly be valid; go ahead
30726 and commit to make sure we issue a hard error. */
30727 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30728 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30729 cp_parser_commit_to_tentative_parse (parser);
30731 if (!parens.require_close (parser))
30732 return error_mark_node;
30734 /* Build the C++-11 representation of an 'aligned'
30735 attribute. */
30736 attributes
30737 = build_tree_list (build_tree_list (gnu_identifier,
30738 aligned_identifier), alignas_expr);
30741 return attributes;
30744 /* Parse a standard C++-11 attribute-specifier-seq.
30746 attribute-specifier-seq:
30747 attribute-specifier-seq [opt] attribute-specifier */
30749 static tree
30750 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30752 tree attr_specs = NULL_TREE;
30753 tree attr_last = NULL_TREE;
30755 /* Don't create wrapper nodes within attributes: the
30756 handlers don't know how to handle them. */
30757 auto_suppress_location_wrappers sentinel;
30759 while (true)
30761 tree attr_spec = cp_parser_std_attribute_spec (parser);
30762 if (attr_spec == void_list_node)
30763 break;
30764 /* Accept [[]][[]]; for which cp_parser_std_attribute_spec
30765 returns NULL_TREE as there are no attributes. */
30766 if (attr_spec == NULL_TREE)
30767 continue;
30768 if (attr_spec == error_mark_node)
30769 return error_mark_node;
30771 if (attr_last)
30772 TREE_CHAIN (attr_last) = attr_spec;
30773 else
30774 attr_specs = attr_last = attr_spec;
30775 attr_last = tree_last (attr_last);
30778 return attr_specs;
30781 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30782 return index of the first token after balanced-token, or N on failure. */
30784 static size_t
30785 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30787 size_t orig_n = n;
30788 int nparens = 0, nbraces = 0, nsquares = 0;
30790 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30792 case CPP_PRAGMA_EOL:
30793 if (!parser->lexer->in_pragma)
30794 break;
30795 /* FALLTHRU */
30796 case CPP_EOF:
30797 /* Ran out of tokens. */
30798 return orig_n;
30799 case CPP_OPEN_PAREN:
30800 ++nparens;
30801 break;
30802 case CPP_OPEN_BRACE:
30803 ++nbraces;
30804 break;
30805 case CPP_OPEN_SQUARE:
30806 ++nsquares;
30807 break;
30808 case CPP_CLOSE_PAREN:
30809 --nparens;
30810 break;
30811 case CPP_CLOSE_BRACE:
30812 --nbraces;
30813 break;
30814 case CPP_CLOSE_SQUARE:
30815 --nsquares;
30816 break;
30817 default:
30818 break;
30820 while (nparens || nbraces || nsquares);
30821 return n;
30824 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30825 return index of the first token after the GNU attribute tokens, or N on
30826 failure. */
30828 static size_t
30829 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30831 while (true)
30833 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30834 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30835 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30836 break;
30838 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30839 if (n2 == n + 2)
30840 break;
30841 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30842 break;
30843 n = n2 + 1;
30845 return n;
30848 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30849 next token), return index of the first token after the standard C++11
30850 attribute tokens, or N on failure. */
30852 static size_t
30853 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30855 while (true)
30857 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30858 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30860 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30861 if (n2 == n + 1)
30862 break;
30863 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30864 break;
30865 n = n2 + 1;
30867 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30868 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30870 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30871 if (n2 == n + 1)
30872 break;
30873 n = n2;
30875 else
30876 break;
30878 return n;
30881 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30882 as the next token), return index of the first token after the attribute
30883 tokens, or N on failure. */
30885 static size_t
30886 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30888 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30889 return cp_parser_skip_gnu_attributes_opt (parser, n);
30890 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30893 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30894 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30895 current value of the PEDANTIC flag, regardless of whether or not
30896 the `__extension__' keyword is present. The caller is responsible
30897 for restoring the value of the PEDANTIC flag. */
30899 static bool
30900 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30902 /* Save the old value of the PEDANTIC flag. */
30903 *saved_pedantic = pedantic;
30905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30907 /* Consume the `__extension__' token. */
30908 cp_lexer_consume_token (parser->lexer);
30909 /* We're not being pedantic while the `__extension__' keyword is
30910 in effect. */
30911 pedantic = 0;
30913 return true;
30916 return false;
30919 /* Parse a label declaration.
30921 label-declaration:
30922 __label__ label-declarator-seq ;
30924 label-declarator-seq:
30925 identifier , label-declarator-seq
30926 identifier */
30928 static void
30929 cp_parser_label_declaration (cp_parser* parser)
30931 /* Look for the `__label__' keyword. */
30932 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30934 while (true)
30936 tree identifier;
30938 /* Look for an identifier. */
30939 identifier = cp_parser_identifier (parser);
30940 /* If we failed, stop. */
30941 if (identifier == error_mark_node)
30942 break;
30943 /* Declare it as a label. */
30944 finish_label_decl (identifier);
30945 /* If the next token is a `;', stop. */
30946 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30947 break;
30948 /* Look for the `,' separating the label declarations. */
30949 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30952 /* Look for the final `;'. */
30953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30956 // -------------------------------------------------------------------------- //
30957 // Concept definitions
30959 static tree
30960 cp_parser_concept_definition (cp_parser *parser)
30962 /* A concept definition is an unevaluated context. */
30963 cp_unevaluated u;
30965 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30966 cp_lexer_consume_token (parser->lexer);
30968 cp_expr id = cp_parser_identifier (parser);
30969 if (id == error_mark_node)
30971 cp_parser_skip_to_end_of_statement (parser);
30972 cp_parser_consume_semicolon_at_end_of_statement (parser);
30973 return NULL_TREE;
30976 tree attrs = cp_parser_attributes_opt (parser);
30978 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30980 cp_parser_skip_to_end_of_statement (parser);
30981 cp_parser_consume_semicolon_at_end_of_statement (parser);
30982 return error_mark_node;
30985 processing_constraint_expression_sentinel parsing_constraint;
30986 tree init = cp_parser_constraint_expression (parser);
30987 if (init == error_mark_node)
30988 cp_parser_skip_to_end_of_statement (parser);
30990 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30991 but continue as if it were. */
30992 cp_parser_consume_semicolon_at_end_of_statement (parser);
30994 return finish_concept_definition (id, init, attrs);
30997 // -------------------------------------------------------------------------- //
30998 // Requires Clause
31000 /* Diagnose an expression that should appear in ()'s within a requires-clause
31001 and suggest where to place those parentheses. */
31003 static void
31004 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
31006 error_at (loc, "expression must be enclosed in parentheses");
31009 static void
31010 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
31012 gcc_rich_location richloc (loc);
31013 richloc.add_fixit_insert_before ("(");
31014 richloc.add_fixit_insert_after (")");
31015 error_at (&richloc, "expression must be enclosed in parentheses");
31018 /* Characterizes the likely kind of expression intended by a mis-written
31019 primary constraint. */
31020 enum primary_constraint_error
31022 pce_ok,
31023 pce_maybe_operator,
31024 pce_maybe_postfix
31027 /* Returns true if the token(s) following a primary-expression in a
31028 constraint-logical-* expression would require parentheses. */
31030 static primary_constraint_error
31031 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
31033 cp_token *token = cp_lexer_peek_token (parser->lexer);
31034 switch (token->type)
31036 default:
31037 return pce_ok;
31039 case CPP_EQ:
31041 /* An equal sign may be part of the definition of a function,
31042 and not an assignment operator, when parsing the expression
31043 for a trailing requires-clause. For example:
31045 template<typename T>
31046 struct S {
31047 S() requires C<T> = default;
31050 Don't try to reparse this a binary operator. */
31051 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
31052 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
31053 return pce_ok;
31055 gcc_fallthrough ();
31058 /* Arithmetic operators. */
31059 case CPP_PLUS:
31060 case CPP_MINUS:
31061 case CPP_MULT:
31062 case CPP_DIV:
31063 case CPP_MOD:
31064 /* Bitwise operators. */
31065 case CPP_AND:
31066 case CPP_OR:
31067 case CPP_XOR:
31068 case CPP_RSHIFT:
31069 case CPP_LSHIFT:
31070 /* Relational operators. */
31071 case CPP_EQ_EQ:
31072 case CPP_NOT_EQ:
31073 case CPP_LESS:
31074 case CPP_GREATER:
31075 case CPP_LESS_EQ:
31076 case CPP_GREATER_EQ:
31077 case CPP_SPACESHIP:
31078 /* Pointer-to-member. */
31079 case CPP_DOT_STAR:
31080 case CPP_DEREF_STAR:
31081 /* Assignment operators. */
31082 case CPP_PLUS_EQ:
31083 case CPP_MINUS_EQ:
31084 case CPP_MULT_EQ:
31085 case CPP_DIV_EQ:
31086 case CPP_MOD_EQ:
31087 case CPP_AND_EQ:
31088 case CPP_OR_EQ:
31089 case CPP_XOR_EQ:
31090 case CPP_RSHIFT_EQ:
31091 case CPP_LSHIFT_EQ:
31092 /* Conditional operator */
31093 case CPP_QUERY:
31094 /* Unenclosed binary or conditional operator. */
31095 return pce_maybe_operator;
31097 case CPP_OPEN_PAREN:
31099 /* A primary constraint that precedes the parameter-list of a
31100 lambda expression is followed by an open paren.
31102 []<typename T> requires C (T a, T b) { ... }
31104 Don't try to re-parse this as a postfix expression. */
31105 if (lambda_p)
31106 return pce_ok;
31108 gcc_fallthrough ();
31110 case CPP_OPEN_SQUARE:
31112 /* A primary-constraint-expression followed by a '[[' is not a
31113 postfix expression. */
31114 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
31115 return pce_ok;
31117 gcc_fallthrough ();
31119 case CPP_PLUS_PLUS:
31120 case CPP_MINUS_MINUS:
31121 case CPP_DOT:
31122 /* Unenclosed postfix operator. */
31123 return pce_maybe_postfix;
31125 case CPP_DEREF:
31126 /* A primary constraint that precedes the lambda-declarator of a
31127 lambda expression is followed by trailing return type.
31129 []<typename T> requires C -> void {}
31131 Don't try to re-parse this as a postfix expression in
31132 C++23 and later. In C++20 ( needs to come in between but we
31133 allow it to be omitted with pedwarn. */
31134 if (lambda_p)
31135 return pce_ok;
31136 /* Unenclosed postfix operator. */
31137 return pce_maybe_postfix;
31141 /* Returns true if the next token begins a unary expression, preceded by
31142 an operator or keyword. */
31144 static bool
31145 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
31147 cp_token *token = cp_lexer_peek_token (parser->lexer);
31148 switch (token->type)
31150 case CPP_NOT:
31151 case CPP_PLUS:
31152 case CPP_MINUS:
31153 case CPP_MULT:
31154 case CPP_COMPL:
31155 case CPP_PLUS_PLUS:
31156 case CPP_MINUS_MINUS:
31157 return true;
31159 case CPP_KEYWORD:
31161 switch (token->keyword)
31163 case RID_STATCAST:
31164 case RID_DYNCAST:
31165 case RID_REINTCAST:
31166 case RID_CONSTCAST:
31167 case RID_TYPEID:
31168 case RID_SIZEOF:
31169 case RID_ALIGNOF:
31170 case RID_NOEXCEPT:
31171 case RID_NEW:
31172 case RID_DELETE:
31173 case RID_THROW:
31174 return true;
31176 default:
31177 break;
31181 default:
31182 break;
31185 return false;
31188 /* Parse a primary expression within a constraint. */
31190 static cp_expr
31191 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
31193 /* If this looks like a unary expression, parse it as such, but diagnose
31194 it as ill-formed; it requires parens. */
31195 if (cp_parser_unary_constraint_requires_parens (parser))
31197 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
31198 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
31199 return e;
31202 cp_lexer_save_tokens (parser->lexer);
31203 cp_id_kind idk;
31204 location_t loc = input_location;
31205 cp_expr expr = cp_parser_primary_expression (parser,
31206 /*address_p=*/false,
31207 /*cast_p=*/false,
31208 /*template_arg_p=*/false,
31209 &idk);
31210 expr.maybe_add_location_wrapper ();
31212 primary_constraint_error pce = pce_ok;
31213 if (expr != error_mark_node)
31215 /* The primary-expression could be part of an unenclosed non-logical
31216 compound expression. */
31217 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
31219 if (pce == pce_ok)
31221 cp_lexer_commit_tokens (parser->lexer);
31222 return finish_constraint_primary_expr (expr);
31225 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
31226 error, but return the expression as if it were valid. */
31227 cp_lexer_rollback_tokens (parser->lexer);
31228 cp_parser_parse_tentatively (parser);
31229 if (pce == pce_maybe_operator)
31230 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31231 else
31232 expr = cp_parser_simple_cast_expression (parser);
31233 if (cp_parser_parse_definitely (parser))
31235 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
31236 return expr;
31239 /* Otherwise, something has gone very wrong, and we can't generate a more
31240 meaningful diagnostic or recover. */
31241 cp_parser_diagnose_ungrouped_constraint_plain (loc);
31242 return error_mark_node;
31245 /* Parse a constraint-logical-and-expression.
31247 constraint-logical-and-expression:
31248 primary-expression
31249 constraint-logical-and-expression '&&' primary-expression */
31251 static cp_expr
31252 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
31254 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31255 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
31257 cp_token *op = cp_lexer_consume_token (parser->lexer);
31258 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31259 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
31261 return lhs;
31264 /* Parse a constraint-logical-or-expression.
31266 constraint-logical-or-expression:
31267 constraint-logical-and-expression
31268 constraint-logical-or-expression '||' constraint-logical-and-expression */
31270 static cp_expr
31271 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
31273 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31274 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
31276 cp_token *op = cp_lexer_consume_token (parser->lexer);
31277 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31278 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
31280 return lhs;
31283 /* Parse the expression after a requires-clause. This has a different grammar
31284 than that in the concepts TS. */
31286 static tree
31287 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
31289 processing_constraint_expression_sentinel parsing_constraint;
31290 ++processing_template_decl;
31291 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
31292 --processing_template_decl;
31293 if (check_for_bare_parameter_packs (expr))
31294 expr = error_mark_node;
31295 return expr;
31298 /* Parse a expression after a requires clause.
31300 constraint-expression:
31301 logical-or-expression
31303 The required logical-or-expression must be a constant expression. Note
31304 that we don't check that the expression is constepxr here. We defer until
31305 we analyze constraints and then, we only check atomic constraints. */
31307 static tree
31308 cp_parser_constraint_expression (cp_parser *parser)
31310 processing_constraint_expression_sentinel parsing_constraint;
31311 ++processing_template_decl;
31312 cp_expr expr = cp_parser_binary_expression (parser, false, true,
31313 PREC_NOT_OPERATOR, NULL);
31314 --processing_template_decl;
31315 if (check_for_bare_parameter_packs (expr))
31316 expr = error_mark_node;
31317 expr.maybe_add_location_wrapper ();
31318 return expr;
31321 /* Optionally parse a requires clause:
31323 requires-clause:
31324 `requires` constraint-logical-or-expression.
31325 [ConceptsTS]
31326 `requires constraint-expression.
31328 LAMBDA_P is true when the requires-clause is parsed before the
31329 parameter-list of a lambda-declarator. */
31331 static tree
31332 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
31334 /* A requires clause is an unevaluated context. */
31335 cp_unevaluated u;
31337 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31338 if (tok->keyword != RID_REQUIRES)
31340 if (!flag_concepts && tok->type == CPP_NAME
31341 && tok->u.value == ridpointers[RID_REQUIRES])
31343 error_at (cp_lexer_peek_token (parser->lexer)->location,
31344 "%<requires%> only available with "
31345 "%<-std=c++20%> or %<-fconcepts%>");
31346 /* Parse and discard the requires-clause. */
31347 cp_lexer_consume_token (parser->lexer);
31348 cp_parser_constraint_expression (parser);
31350 return NULL_TREE;
31353 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
31354 if (tok2->type == CPP_OPEN_BRACE)
31356 /* An opening brace following the start of a requires-clause is
31357 ill-formed; the user likely forgot the second `requires' that
31358 would start a requires-expression. */
31359 gcc_rich_location richloc (tok2->location);
31360 richloc.add_fixit_insert_after (tok->location, " requires");
31361 error_at (&richloc, "missing additional %<requires%> to start "
31362 "a requires-expression");
31363 /* Don't consume the `requires', so that it's reused as the start of a
31364 requires-expression. */
31366 else
31367 cp_lexer_consume_token (parser->lexer);
31369 if (!flag_concepts_ts)
31370 return cp_parser_requires_clause_expression (parser, lambda_p);
31371 else
31372 return cp_parser_constraint_expression (parser);
31375 /*---------------------------------------------------------------------------
31376 Requires expressions
31377 ---------------------------------------------------------------------------*/
31379 /* Parse a requires expression
31381 requirement-expression:
31382 'requires' requirement-parameter-list [opt] requirement-body */
31384 static tree
31385 cp_parser_requires_expression (cp_parser *parser)
31387 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31388 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
31390 /* Avoid committing to outer tentative parse. */
31391 tentative_firewall firewall (parser);
31393 /* This is definitely a requires-expression. */
31394 cp_parser_commit_to_tentative_parse (parser);
31396 tree parms, reqs;
31398 /* Local parameters are delared as variables within the scope
31399 of the expression. They are not visible past the end of
31400 the expression. Expressions within the requires-expression
31401 are unevaluated. */
31402 struct scope_sentinel
31404 scope_sentinel ()
31406 ++cp_unevaluated_operand;
31407 begin_scope (sk_function_parms, NULL_TREE);
31408 current_binding_level->requires_expression = true;
31411 ~scope_sentinel ()
31413 pop_bindings_and_leave_scope ();
31414 --cp_unevaluated_operand;
31416 } s;
31418 /* Parse the optional parameter list. */
31419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31421 parms = cp_parser_requirement_parameter_list (parser);
31422 if (parms == error_mark_node)
31423 return error_mark_node;
31425 else
31426 parms = NULL_TREE;
31428 /* Parse the requirement body. */
31429 ++processing_template_decl;
31430 reqs = cp_parser_requirement_body (parser);
31431 --processing_template_decl;
31432 if (reqs == error_mark_node)
31433 return error_mark_node;
31436 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31437 the parm chain. */
31438 grokparms (parms, &parms);
31439 loc = make_location (loc, loc, parser->lexer);
31440 tree expr = finish_requires_expr (loc, parms, reqs);
31441 if (!processing_template_decl)
31443 /* Perform semantic processing now to diagnose any invalid types and
31444 expressions. */
31445 int saved_errorcount = errorcount;
31446 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31447 if (errorcount > saved_errorcount)
31448 return error_mark_node;
31450 return expr;
31453 /* Parse a parameterized requirement.
31455 requirement-parameter-list:
31456 '(' parameter-declaration-clause ')' */
31458 static tree
31459 cp_parser_requirement_parameter_list (cp_parser *parser)
31461 matching_parens parens;
31462 if (!parens.require_open (parser))
31463 return error_mark_node;
31465 tree parms = (cp_parser_parameter_declaration_clause
31466 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31468 if (!parens.require_close (parser))
31469 return error_mark_node;
31471 /* Modify the declared parameters by removing their context
31472 so they don't refer to the enclosing scope and explicitly
31473 indicating that they are constraint variables. */
31474 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31476 if (parm == void_list_node || parm == explicit_void_list_node)
31477 break;
31478 tree decl = TREE_VALUE (parm);
31479 if (decl != error_mark_node)
31481 DECL_CONTEXT (decl) = NULL_TREE;
31482 CONSTRAINT_VAR_P (decl) = true;
31486 return parms;
31489 /* Parse the body of a requirement.
31491 requirement-body:
31492 '{' requirement-list '}' */
31493 static tree
31494 cp_parser_requirement_body (cp_parser *parser)
31496 matching_braces braces;
31497 if (!braces.require_open (parser))
31498 return error_mark_node;
31500 tree reqs = cp_parser_requirement_seq (parser);
31502 if (!braces.require_close (parser))
31503 return error_mark_node;
31505 return reqs;
31508 /* Parse a sequence of requirements.
31510 requirement-seq:
31511 requirement
31512 requirement-seq requirement */
31514 static tree
31515 cp_parser_requirement_seq (cp_parser *parser)
31517 tree result = NULL_TREE;
31520 tree req = cp_parser_requirement (parser);
31521 if (req != error_mark_node)
31522 result = tree_cons (NULL_TREE, req, result);
31524 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31525 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31527 /* If there are no valid requirements, this is not a valid expression. */
31528 if (!result)
31529 return error_mark_node;
31531 /* Reverse the order of requirements so they are analyzed in order. */
31532 return nreverse (result);
31535 /* Parse a syntactic requirement or type requirement.
31537 requirement:
31538 simple-requirement
31539 compound-requirement
31540 type-requirement
31541 nested-requirement */
31543 static tree
31544 cp_parser_requirement (cp_parser *parser)
31546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31547 return cp_parser_compound_requirement (parser);
31548 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31550 /* It's probably a type-requirement. */
31551 cp_parser_parse_tentatively (parser);
31552 tree req = cp_parser_type_requirement (parser);
31553 if (cp_parser_parse_definitely (parser))
31554 return req;
31555 /* No, maybe it's something like typename T::type(); */
31556 cp_parser_parse_tentatively (parser);
31557 req = cp_parser_simple_requirement (parser);
31558 if (cp_parser_parse_definitely (parser))
31559 return req;
31560 /* Non-tentative for the error. */
31561 return cp_parser_type_requirement (parser);
31563 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31564 return cp_parser_nested_requirement (parser);
31565 else
31566 return cp_parser_simple_requirement (parser);
31569 /* Parse a simple requirement.
31571 simple-requirement:
31572 expression ';' */
31574 static tree
31575 cp_parser_simple_requirement (cp_parser *parser)
31577 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31578 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31579 if (expr == error_mark_node)
31580 cp_parser_skip_to_end_of_statement (parser);
31582 cp_parser_consume_semicolon_at_end_of_statement (parser);
31584 if (!expr || expr == error_mark_node)
31585 return error_mark_node;
31587 /* Sometimes we don't get locations, so use the cached token location
31588 as a reasonable approximation. */
31589 if (expr.get_location() == UNKNOWN_LOCATION)
31590 expr.set_location (start);
31592 for (tree t = expr; ; )
31594 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31595 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31597 t = TREE_OPERAND (t, 0);
31598 continue;
31600 if (concept_check_p (t))
31602 gcc_rich_location richloc (get_start (start));
31603 richloc.add_fixit_insert_before (start, "requires ");
31604 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31605 "if a concept-id is a valid expression; add "
31606 "%<requires%> to check satisfaction");
31608 break;
31611 return finish_simple_requirement (expr.get_location (), expr);
31614 /* Parse a type requirement
31616 type-requirement
31617 nested-name-specifier [opt] required-type-name ';'
31619 required-type-name:
31620 type-name
31621 'template' [opt] simple-template-id */
31623 static tree
31624 cp_parser_type_requirement (cp_parser *parser)
31626 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31627 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31629 // Save the scope before parsing name specifiers.
31630 tree saved_scope = parser->scope;
31631 tree saved_object_scope = parser->object_scope;
31632 tree saved_qualifying_scope = parser->qualifying_scope;
31633 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31634 cp_parser_nested_name_specifier_opt (parser,
31635 /*typename_keyword_p=*/true,
31636 /*check_dependency_p=*/true,
31637 /*type_p=*/true,
31638 /*is_declaration=*/false);
31640 tree type;
31641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31643 cp_lexer_consume_token (parser->lexer);
31644 type = cp_parser_template_id (parser,
31645 /*template_keyword_p=*/true,
31646 /*check_dependency_p=*/true,
31647 /*tag_type=*/none_type,
31648 /*is_declaration=*/false);
31649 type = make_typename_type (parser->scope, type, typename_type,
31650 /*complain=*/tf_error);
31652 else
31653 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31655 if (TREE_CODE (type) == TYPE_DECL)
31656 type = TREE_TYPE (type);
31658 parser->scope = saved_scope;
31659 parser->object_scope = saved_object_scope;
31660 parser->qualifying_scope = saved_qualifying_scope;
31662 if (type == error_mark_node)
31663 cp_parser_skip_to_end_of_statement (parser);
31665 cp_parser_consume_semicolon_at_end_of_statement (parser);
31667 if (type == error_mark_node)
31668 return error_mark_node;
31670 loc = make_location (loc, start_tok->location, parser->lexer);
31671 return finish_type_requirement (loc, type);
31674 /* Parse a compound requirement
31676 compound-requirement:
31677 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31679 static tree
31680 cp_parser_compound_requirement (cp_parser *parser)
31682 /* Parse an expression enclosed in '{ }'s. */
31683 matching_braces braces;
31684 if (!braces.require_open (parser))
31685 return error_mark_node;
31687 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31689 tree expr = cp_parser_expression (parser, NULL, false, false);
31690 if (expr == error_mark_node)
31691 cp_parser_skip_to_closing_brace (parser);
31693 if (!braces.require_close (parser))
31695 cp_parser_skip_to_end_of_statement (parser);
31696 cp_parser_consume_semicolon_at_end_of_statement (parser);
31697 return error_mark_node;
31700 /* If the expression was invalid, skip the remainder of the requirement. */
31701 if (!expr || expr == error_mark_node)
31703 cp_parser_skip_to_end_of_statement (parser);
31704 cp_parser_consume_semicolon_at_end_of_statement (parser);
31705 return error_mark_node;
31708 /* Parse the optional noexcept. */
31709 bool noexcept_p = false;
31710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31712 cp_lexer_consume_token (parser->lexer);
31713 noexcept_p = true;
31716 /* Parse the optional trailing return type. */
31717 tree type = NULL_TREE;
31718 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31720 cp_lexer_consume_token (parser->lexer);
31721 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31723 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31724 parser->in_result_type_constraint_p = true;
31725 /* C++20 allows either a type-id or a type-constraint. Parsing
31726 a type-id will subsume the parsing for a type-constraint but
31727 allow for more syntactic forms (e.g., const C<T>*). */
31728 type = cp_parser_trailing_type_id (parser);
31729 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31730 if (type == error_mark_node)
31731 return error_mark_node;
31733 location_t type_loc = make_location (tok->location, tok->location,
31734 parser->lexer);
31736 /* Check that we haven't written something like 'const C<T>*'. */
31737 if (type_uses_auto (type))
31739 if (!is_auto (type))
31741 error_at (type_loc,
31742 "result type is not a plain type-constraint");
31743 cp_parser_consume_semicolon_at_end_of_statement (parser);
31744 return error_mark_node;
31747 else if (!flag_concepts_ts)
31748 /* P1452R2 removed the trailing-return-type option. */
31749 error_at (type_loc,
31750 "return-type-requirement is not a type-constraint");
31753 location_t loc = make_location (expr_token->location,
31754 braces.open_location (),
31755 parser->lexer);
31757 cp_parser_consume_semicolon_at_end_of_statement (parser);
31759 if (expr == error_mark_node || type == error_mark_node)
31760 return error_mark_node;
31762 return finish_compound_requirement (loc, expr, type, noexcept_p);
31765 /* Parse a nested requirement. This is the same as a requires clause.
31767 nested-requirement:
31768 requires-clause */
31770 static tree
31771 cp_parser_nested_requirement (cp_parser *parser)
31773 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31774 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31775 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31776 tree req = cp_parser_constraint_expression (parser);
31777 if (req == error_mark_node)
31778 cp_parser_skip_to_end_of_statement (parser);
31779 loc = make_location (loc, tok->location, parser->lexer);
31780 cp_parser_consume_semicolon_at_end_of_statement (parser);
31781 if (req == error_mark_node)
31782 return error_mark_node;
31783 return finish_nested_requirement (loc, req);
31786 /* Support Functions */
31788 /* Return the appropriate prefer_type argument for lookup_name based on
31789 tag_type. */
31791 static inline LOOK_want
31792 prefer_type_arg (tag_types tag_type)
31794 switch (tag_type)
31796 case none_type: return LOOK_want::NORMAL; // No preference.
31797 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31798 default: return LOOK_want::TYPE; // Type only.
31802 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31803 NAME should have one of the representations used for an
31804 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31805 is returned. If PARSER->SCOPE is a dependent type, then a
31806 SCOPE_REF is returned.
31808 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31809 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31810 was formed. Abstractly, such entities should not be passed to this
31811 function, because they do not need to be looked up, but it is
31812 simpler to check for this special case here, rather than at the
31813 call-sites.
31815 In cases not explicitly covered above, this function returns a
31816 DECL, OVERLOAD, or baselink representing the result of the lookup.
31817 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31818 is returned.
31820 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31821 (e.g., "struct") that was used. In that case bindings that do not
31822 refer to types are ignored.
31824 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31825 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31827 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31828 are ignored.
31830 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31831 types.
31833 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31834 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31835 NULL_TREE otherwise. */
31837 static cp_expr
31838 cp_parser_lookup_name (cp_parser *parser, tree name,
31839 enum tag_types tag_type,
31840 int is_template,
31841 bool is_namespace,
31842 bool check_dependency,
31843 tree *ambiguous_decls,
31844 location_t name_location)
31846 tree decl;
31847 tree object_type = parser->context->object_type;
31849 /* Assume that the lookup will be unambiguous. */
31850 if (ambiguous_decls)
31851 *ambiguous_decls = NULL_TREE;
31853 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31854 no longer valid. Note that if we are parsing tentatively, and
31855 the parse fails, OBJECT_TYPE will be automatically restored. */
31856 parser->context->object_type = NULL_TREE;
31858 if (name == error_mark_node)
31859 return error_mark_node;
31861 /* A template-id has already been resolved; there is no lookup to
31862 do. */
31863 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31864 return name;
31865 if (BASELINK_P (name))
31867 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31868 == TEMPLATE_ID_EXPR);
31869 return name;
31872 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31873 it should already have been checked to make sure that the name
31874 used matches the type being destroyed. */
31875 if (TREE_CODE (name) == BIT_NOT_EXPR)
31877 tree type;
31879 /* Figure out to which type this destructor applies. */
31880 if (parser->scope)
31881 type = parser->scope;
31882 else if (object_type)
31883 type = object_type;
31884 else
31885 type = current_class_type;
31886 /* If that's not a class type, there is no destructor. */
31887 if (!type || !CLASS_TYPE_P (type))
31888 return error_mark_node;
31890 /* In a non-static member function, check implicit this->. */
31891 if (current_class_ref)
31892 return lookup_destructor (current_class_ref, parser->scope, name,
31893 tf_warning_or_error);
31895 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31896 lazily_declare_fn (sfk_destructor, type);
31898 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31899 return dtor;
31901 return error_mark_node;
31904 /* By this point, the NAME should be an ordinary identifier. If
31905 the id-expression was a qualified name, the qualifying scope is
31906 stored in PARSER->SCOPE at this point. */
31907 gcc_assert (identifier_p (name));
31909 /* Perform the lookup. */
31910 if (parser->scope)
31912 bool dependent_p;
31914 if (parser->scope == error_mark_node)
31915 return error_mark_node;
31917 /* If the SCOPE is dependent, the lookup must be deferred until
31918 the template is instantiated -- unless we are explicitly
31919 looking up names in uninstantiated templates. Even then, we
31920 cannot look up the name if the scope is not a class type; it
31921 might, for example, be a template type parameter. */
31922 dependent_p = (TYPE_P (parser->scope)
31923 && dependent_scope_p (parser->scope));
31924 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31925 && dependent_p)
31926 /* Defer lookup. */
31927 decl = error_mark_node;
31928 else
31930 tree pushed_scope = NULL_TREE;
31932 /* If PARSER->SCOPE is a dependent type, then it must be a
31933 class type, and we must not be checking dependencies;
31934 otherwise, we would have processed this lookup above. So
31935 that PARSER->SCOPE is not considered a dependent base by
31936 lookup_member, we must enter the scope here. */
31937 if (dependent_p)
31938 pushed_scope = push_scope (parser->scope);
31940 /* If the PARSER->SCOPE is a template specialization, it
31941 may be instantiated during name lookup. In that case,
31942 errors may be issued. Even if we rollback the current
31943 tentative parse, those errors are valid. */
31944 decl = lookup_qualified_name (parser->scope, name,
31945 prefer_type_arg (tag_type),
31946 /*complain=*/true);
31948 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31949 lookup result and the nested-name-specifier nominates a class C:
31950 * if the name specified after the nested-name-specifier, when
31951 looked up in C, is the injected-class-name of C (Clause 9), or
31952 * if the name specified after the nested-name-specifier is the
31953 same as the identifier or the simple-template-id's template-
31954 name in the last component of the nested-name-specifier,
31955 the name is instead considered to name the constructor of
31956 class C. [ Note: for example, the constructor is not an
31957 acceptable lookup result in an elaborated-type-specifier so
31958 the constructor would not be used in place of the
31959 injected-class-name. --end note ] Such a constructor name
31960 shall be used only in the declarator-id of a declaration that
31961 names a constructor or in a using-declaration. */
31962 if (tag_type == none_type
31963 && DECL_SELF_REFERENCE_P (decl)
31964 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31965 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31966 prefer_type_arg (tag_type),
31967 /*complain=*/true);
31969 if (pushed_scope)
31970 pop_scope (pushed_scope);
31973 /* If the scope is a dependent type and either we deferred lookup or
31974 we did lookup but didn't find the name, rememeber the name. */
31975 if (decl == error_mark_node && TYPE_P (parser->scope)
31976 && dependent_type_p (parser->scope))
31978 if (tag_type)
31980 tree type;
31982 /* The resolution to Core Issue 180 says that `struct
31983 A::B' should be considered a type-name, even if `A'
31984 is dependent. */
31985 type = make_typename_type (parser->scope, name, tag_type,
31986 /*complain=*/tf_error);
31987 if (type != error_mark_node)
31988 decl = TYPE_NAME (type);
31990 else if (is_template
31991 && (cp_parser_next_token_ends_template_argument_p (parser)
31992 || cp_lexer_next_token_is (parser->lexer,
31993 CPP_CLOSE_PAREN)))
31994 decl = make_unbound_class_template (parser->scope,
31995 name, NULL_TREE,
31996 /*complain=*/tf_error);
31997 else
31998 decl = build_qualified_name (/*type=*/NULL_TREE,
31999 parser->scope, name,
32000 is_template);
32002 parser->qualifying_scope = parser->scope;
32003 parser->object_scope = NULL_TREE;
32005 else if (object_type)
32007 bool dep = dependent_scope_p (object_type);
32009 /* Look up the name in the scope of the OBJECT_TYPE, unless the
32010 OBJECT_TYPE is not a class. */
32011 if (!dep && CLASS_TYPE_P (object_type))
32012 /* If the OBJECT_TYPE is a template specialization, it may
32013 be instantiated during name lookup. In that case, errors
32014 may be issued. Even if we rollback the current tentative
32015 parse, those errors are valid. */
32016 decl = lookup_member (object_type,
32017 name,
32018 /*protect=*/0,
32019 /*prefer_type=*/tag_type != none_type,
32020 tf_warning_or_error);
32021 else
32022 decl = NULL_TREE;
32024 /* If we didn't find a member and have dependent bases, the member lookup
32025 is now dependent. */
32026 if (!dep && !decl && any_dependent_bases_p (object_type))
32027 dep = true;
32029 if (dep && is_template == 2)
32030 /* The template keyword specifies a dependent template. */;
32031 else if (!decl)
32032 /* Look it up in the enclosing context. DR 141: When looking for a
32033 template-name after -> or ., only consider class templates. */
32034 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32035 /* DR 141: When looking in the
32036 current enclosing context for a
32037 template-name after -> or ., only
32038 consider class templates. */
32039 : is_template ? LOOK_want::TYPE
32040 : prefer_type_arg (tag_type));
32042 /* If we did unqualified lookup of a dependent member-qualified name and
32043 found something, do we want to use it? P1787 clarified that we need
32044 to look in the object scope first even if it's dependent, but for now
32045 let's still use it in some cases.
32046 FIXME remember unqualified lookup result to use if member lookup fails
32047 at instantiation time. */
32048 if (decl && dep && is_template)
32050 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
32051 /* Only use the unqualified class template lookup if we're actually
32052 looking at a template arg list. */
32053 if (!cp_parser_skip_entire_template_parameter_list (parser))
32054 decl = NULL_TREE;
32057 /* If we know we're looking for a type (e.g. A in p->A::x),
32058 mock up a typename. */
32059 if (!decl && dep && tag_type != none_type)
32061 tree type = build_typename_type (object_type, name, name,
32062 typename_type);
32063 decl = TYPE_NAME (type);
32066 parser->object_scope = object_type;
32067 parser->qualifying_scope = NULL_TREE;
32069 else
32071 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32072 : prefer_type_arg (tag_type));
32073 parser->qualifying_scope = NULL_TREE;
32074 parser->object_scope = NULL_TREE;
32077 /* If the lookup failed, let our caller know. */
32078 if (!decl || decl == error_mark_node)
32079 return error_mark_node;
32081 /* If we have resolved the name of a member declaration, check to
32082 see if the declaration is accessible. When the name resolves to
32083 set of overloaded functions, accessibility is checked when
32084 overload resolution is done. If we have a TREE_LIST, then the lookup
32085 is either ambiguous or it found multiple injected-class-names, the
32086 accessibility of which is trivially satisfied.
32088 During an explicit instantiation, access is not checked at all,
32089 as per [temp.explicit]. */
32090 if (DECL_P (decl))
32091 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
32092 tf_warning_or_error);
32094 /* Pull out the template from an injected-class-name (or multiple). */
32095 if (is_template)
32096 decl = maybe_get_template_decl_from_type_decl (decl);
32098 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
32099 if (TREE_CODE (decl) == TREE_LIST)
32101 if (ambiguous_decls)
32102 *ambiguous_decls = decl;
32103 /* The error message we have to print is too complicated for
32104 cp_parser_error, so we incorporate its actions directly. */
32105 if (!cp_parser_simulate_error (parser))
32107 error_at (name_location, "reference to %qD is ambiguous",
32108 name);
32109 print_candidates (decl);
32111 return error_mark_node;
32114 gcc_assert (DECL_P (decl)
32115 || TREE_CODE (decl) == OVERLOAD
32116 || TREE_CODE (decl) == SCOPE_REF
32117 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
32118 || BASELINK_P (decl));
32120 maybe_record_typedef_use (decl);
32122 return cp_expr (decl, name_location);
32125 /* Like cp_parser_lookup_name, but for use in the typical case where
32126 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
32127 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
32129 static tree
32130 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
32132 return cp_parser_lookup_name (parser, name,
32133 none_type,
32134 /*is_template=*/false,
32135 /*is_namespace=*/false,
32136 /*check_dependency=*/true,
32137 /*ambiguous_decls=*/NULL,
32138 location);
32141 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
32142 the current context, return the TYPE_DECL. If TAG_NAME_P is
32143 true, the DECL indicates the class being defined in a class-head,
32144 or declared in an elaborated-type-specifier.
32146 Otherwise, return DECL. */
32148 static tree
32149 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
32151 /* If the TEMPLATE_DECL is being declared as part of a class-head,
32152 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
32154 struct A {
32155 template <typename T> struct B;
32158 template <typename T> struct A::B {};
32160 Similarly, in an elaborated-type-specifier:
32162 namespace N { struct X{}; }
32164 struct A {
32165 template <typename T> friend struct N::X;
32168 However, if the DECL refers to a class type, and we are in
32169 the scope of the class, then the name lookup automatically
32170 finds the TYPE_DECL created by build_self_reference rather
32171 than a TEMPLATE_DECL. For example, in:
32173 template <class T> struct S {
32174 S s;
32177 there is no need to handle such case. */
32179 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
32180 return DECL_TEMPLATE_RESULT (decl);
32182 return decl;
32185 /* If too many, or too few, template-parameter lists apply to the
32186 declarator, issue an error message. Returns TRUE if all went well,
32187 and FALSE otherwise. */
32189 static bool
32190 cp_parser_check_declarator_template_parameters (cp_parser* parser,
32191 cp_declarator *declarator,
32192 location_t declarator_location)
32194 switch (declarator->kind)
32196 case cdk_id:
32198 unsigned num_templates = 0;
32199 tree scope = declarator->u.id.qualifying_scope;
32200 bool template_id_p = false;
32202 if (scope)
32203 num_templates = num_template_headers_for_class (scope);
32204 else if (TREE_CODE (declarator->u.id.unqualified_name)
32205 == TEMPLATE_ID_EXPR)
32207 /* If the DECLARATOR has the form `X<y>' then it uses one
32208 additional level of template parameters. */
32209 ++num_templates;
32210 template_id_p = true;
32213 return cp_parser_check_template_parameters
32214 (parser, num_templates, template_id_p, declarator_location,
32215 declarator);
32218 case cdk_function:
32219 case cdk_array:
32220 case cdk_pointer:
32221 case cdk_reference:
32222 case cdk_ptrmem:
32223 return (cp_parser_check_declarator_template_parameters
32224 (parser, declarator->declarator, declarator_location));
32226 case cdk_decomp:
32227 case cdk_error:
32228 return true;
32230 default:
32231 gcc_unreachable ();
32233 return false;
32236 /* NUM_TEMPLATES were used in the current declaration. If that is
32237 invalid, return FALSE and issue an error messages. Otherwise,
32238 return TRUE. If DECLARATOR is non-NULL, then we are checking a
32239 declarator and we can print more accurate diagnostics. */
32241 static bool
32242 cp_parser_check_template_parameters (cp_parser* parser,
32243 unsigned num_templates,
32244 bool template_id_p,
32245 location_t location,
32246 cp_declarator *declarator)
32248 /* If there are the same number of template classes and parameter
32249 lists, that's OK. */
32250 if (parser->num_template_parameter_lists == num_templates)
32251 return true;
32252 /* If there are more, but only one more, and the name ends in an identifier,
32253 then we are declaring a primary template. That's OK too. */
32254 if (!template_id_p
32255 && parser->num_template_parameter_lists == num_templates + 1)
32256 return true;
32258 if (cp_parser_simulate_error (parser))
32259 return false;
32261 /* If there are more template classes than parameter lists, we have
32262 something like:
32264 template <class T> void S<T>::R<T>::f (); */
32265 if (parser->num_template_parameter_lists < num_templates)
32267 if (declarator && !current_function_decl)
32268 error_at (location, "specializing member %<%T::%E%> "
32269 "requires %<template<>%> syntax",
32270 declarator->u.id.qualifying_scope,
32271 declarator->u.id.unqualified_name);
32272 else if (declarator)
32273 error_at (location, "invalid declaration of %<%T::%E%>",
32274 declarator->u.id.qualifying_scope,
32275 declarator->u.id.unqualified_name);
32276 else
32277 error_at (location, "too few template-parameter-lists");
32278 return false;
32280 /* Otherwise, there are too many template parameter lists. We have
32281 something like:
32283 template <class T> template <class U> void S::f(); */
32284 error_at (location, "too many template-parameter-lists");
32285 return false;
32288 /* Parse an optional `::' token indicating that the following name is
32289 from the global namespace. If so, PARSER->SCOPE is set to the
32290 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
32291 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
32292 Returns the new value of PARSER->SCOPE, if the `::' token is
32293 present, and NULL_TREE otherwise. */
32295 static tree
32296 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
32298 cp_token *token;
32300 /* Peek at the next token. */
32301 token = cp_lexer_peek_token (parser->lexer);
32302 /* If we're looking at a `::' token then we're starting from the
32303 global namespace, not our current location. */
32304 if (token->type == CPP_SCOPE)
32306 /* Consume the `::' token. */
32307 cp_lexer_consume_token (parser->lexer);
32308 /* Set the SCOPE so that we know where to start the lookup. */
32309 parser->scope = global_namespace;
32310 parser->qualifying_scope = global_namespace;
32311 parser->object_scope = NULL_TREE;
32313 return parser->scope;
32315 else if (!current_scope_valid_p)
32317 parser->scope = NULL_TREE;
32318 parser->qualifying_scope = NULL_TREE;
32319 parser->object_scope = NULL_TREE;
32322 return NULL_TREE;
32325 /* Returns TRUE if the upcoming token sequence is the start of a
32326 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
32327 declarator is preceded by the `friend' specifier. The parser flags FLAGS
32328 is used to control type-specifier parsing. */
32330 static bool
32331 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
32332 bool friend_p)
32334 bool constructor_p;
32335 bool outside_class_specifier_p;
32336 tree nested_name_specifier;
32337 cp_token *next_token;
32339 /* The common case is that this is not a constructor declarator, so
32340 try to avoid doing lots of work if at all possible. It's not
32341 valid declare a constructor at function scope. */
32342 if (parser->in_function_body)
32343 return false;
32344 /* And only certain tokens can begin a constructor declarator. */
32345 next_token = cp_lexer_peek_token (parser->lexer);
32346 if (next_token->type != CPP_NAME
32347 && next_token->type != CPP_SCOPE
32348 && next_token->type != CPP_NESTED_NAME_SPECIFIER
32349 && next_token->type != CPP_TEMPLATE_ID)
32350 return false;
32352 /* Parse tentatively; we are going to roll back all of the tokens
32353 consumed here. */
32354 cp_parser_parse_tentatively (parser);
32355 /* Assume that we are looking at a constructor declarator. */
32356 constructor_p = true;
32358 /* Look for the optional `::' operator. */
32359 cp_parser_global_scope_opt (parser,
32360 /*current_scope_valid_p=*/false);
32361 /* Look for the nested-name-specifier. */
32362 nested_name_specifier
32363 = (cp_parser_nested_name_specifier_opt (parser,
32364 /*typename_keyword_p=*/false,
32365 /*check_dependency_p=*/false,
32366 /*type_p=*/false,
32367 /*is_declaration=*/false));
32369 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
32370 if (nested_name_specifier
32371 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
32373 tree s = resolve_typename_type (nested_name_specifier,
32374 /*only_current_p=*/false);
32375 if (TREE_CODE (s) != TYPENAME_TYPE)
32376 nested_name_specifier = s;
32379 outside_class_specifier_p = (!at_class_scope_p ()
32380 || !TYPE_BEING_DEFINED (current_class_type)
32381 || friend_p);
32383 /* Outside of a class-specifier, there must be a
32384 nested-name-specifier. Except in C++17 mode, where we
32385 might be declaring a guiding declaration. */
32386 if (!nested_name_specifier && outside_class_specifier_p
32387 && cxx_dialect < cxx17)
32388 constructor_p = false;
32389 else if (nested_name_specifier == error_mark_node)
32390 constructor_p = false;
32392 /* If we have a class scope, this is easy; DR 147 says that S::S always
32393 names the constructor, and no other qualified name could. */
32394 if (constructor_p && nested_name_specifier
32395 && CLASS_TYPE_P (nested_name_specifier))
32397 tree id = cp_parser_unqualified_id (parser,
32398 /*template_keyword_p=*/false,
32399 /*check_dependency_p=*/false,
32400 /*declarator_p=*/true,
32401 /*optional_p=*/false);
32402 if (is_overloaded_fn (id))
32403 id = DECL_NAME (get_first_fn (id));
32404 if (!constructor_name_p (id, nested_name_specifier))
32405 constructor_p = false;
32407 /* If we still think that this might be a constructor-declarator,
32408 look for a class-name. */
32409 else if (constructor_p)
32411 /* If we have:
32413 template <typename T> struct S {
32414 S();
32417 we must recognize that the nested `S' names a class. */
32418 if (cxx_dialect >= cxx17)
32419 cp_parser_parse_tentatively (parser);
32421 tree type_decl;
32422 type_decl = cp_parser_class_name (parser,
32423 /*typename_keyword_p=*/false,
32424 /*template_keyword_p=*/false,
32425 none_type,
32426 /*check_dependency_p=*/false,
32427 /*class_head_p=*/false,
32428 /*is_declaration=*/false);
32430 if (cxx_dialect >= cxx17
32431 && !cp_parser_parse_definitely (parser))
32433 type_decl = NULL_TREE;
32434 tree tmpl = cp_parser_template_name (parser,
32435 /*template_keyword*/false,
32436 /*check_dependency_p*/false,
32437 /*is_declaration*/false,
32438 none_type,
32439 /*is_identifier*/NULL);
32440 if (DECL_CLASS_TEMPLATE_P (tmpl)
32441 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32442 /* It's a deduction guide, return true. */;
32443 else
32444 cp_parser_simulate_error (parser);
32447 /* If there was no class-name, then this is not a constructor.
32448 Otherwise, if we are in a class-specifier and we aren't
32449 handling a friend declaration, check that its type matches
32450 current_class_type (c++/38313). Note: error_mark_node
32451 is left alone for error recovery purposes. */
32452 constructor_p = (!cp_parser_error_occurred (parser)
32453 && (outside_class_specifier_p
32454 || type_decl == NULL_TREE
32455 || type_decl == error_mark_node
32456 || same_type_p (current_class_type,
32457 TREE_TYPE (type_decl))));
32459 /* If we're still considering a constructor, we have to see a `(',
32460 to begin the parameter-declaration-clause, followed by either a
32461 `)', an `...', or a decl-specifier. We need to check for a
32462 type-specifier to avoid being fooled into thinking that:
32464 S (f) (int);
32466 is a constructor. (It is actually a function named `f' that
32467 takes one parameter (of type `int') and returns a value of type
32468 `S'. */
32469 if (constructor_p
32470 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32471 constructor_p = false;
32473 if (constructor_p
32474 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32475 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32476 /* A parameter declaration begins with a decl-specifier,
32477 which is either the "attribute" keyword, a storage class
32478 specifier, or (usually) a type-specifier. */
32479 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32480 /* GNU attributes can actually appear both at the start of
32481 a parameter and parenthesized declarator.
32482 S (__attribute__((unused)) int);
32483 is a constructor, but
32484 S (__attribute__((unused)) foo) (int);
32485 is a function declaration. [[attribute]] can appear in the
32486 first form too, but not in the second form. */
32487 && !cp_next_tokens_can_be_std_attribute_p (parser))
32489 tree type;
32490 tree pushed_scope = NULL_TREE;
32491 unsigned saved_num_template_parameter_lists;
32493 if (cp_parser_allow_gnu_extensions_p (parser)
32494 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32496 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32497 while (--n)
32498 cp_lexer_consume_token (parser->lexer);
32501 /* Names appearing in the type-specifier should be looked up
32502 in the scope of the class. */
32503 if (current_class_type)
32504 type = NULL_TREE;
32505 else if (type_decl)
32507 type = TREE_TYPE (type_decl);
32508 if (TREE_CODE (type) == TYPENAME_TYPE)
32510 type = resolve_typename_type (type,
32511 /*only_current_p=*/false);
32512 if (TREE_CODE (type) == TYPENAME_TYPE)
32514 cp_parser_abort_tentative_parse (parser);
32515 return false;
32518 pushed_scope = push_scope (type);
32521 /* Inside the constructor parameter list, surrounding
32522 template-parameter-lists do not apply. */
32523 saved_num_template_parameter_lists
32524 = parser->num_template_parameter_lists;
32525 parser->num_template_parameter_lists = 0;
32527 /* Look for the type-specifier. It's not optional, but its typename
32528 might be. Unless this is a friend declaration; we don't want to
32529 treat
32531 friend S (T::fn)(int);
32533 as a constructor, but with P0634, we might assume a type when
32534 looking for the type-specifier. It is actually a function named
32535 `T::fn' that takes one parameter (of type `int') and returns a
32536 value of type `S'. Constructors can be friends, but they must
32537 use a qualified name.
32539 Parse with an empty set of declaration specifiers since we're
32540 trying to match a decl-specifier-seq of the first parameter.
32541 This must be non-null so that cp_parser_simple_type_specifier
32542 will recognize a constrained placeholder type such as:
32543 'C<int> auto' where C is a type concept. */
32544 cp_decl_specifier_seq ctor_specs;
32545 clear_decl_specs (&ctor_specs);
32546 cp_parser_type_specifier (parser,
32547 (friend_p ? CP_PARSER_FLAGS_NONE
32548 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32549 /*decl_specs=*/&ctor_specs,
32550 /*is_declarator=*/true,
32551 /*declares_class_or_enum=*/NULL,
32552 /*is_cv_qualifier=*/NULL);
32554 parser->num_template_parameter_lists
32555 = saved_num_template_parameter_lists;
32557 /* Leave the scope of the class. */
32558 if (pushed_scope)
32559 pop_scope (pushed_scope);
32561 constructor_p = !cp_parser_error_occurred (parser);
32565 /* We did not really want to consume any tokens. */
32566 cp_parser_abort_tentative_parse (parser);
32568 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
32569 declarator-id of a constructor or destructor. */
32570 if (constructor_p
32571 && cp_lexer_peek_token (parser->lexer)->type == CPP_TEMPLATE_ID)
32573 auto_diagnostic_group d;
32574 if (emit_diagnostic (cxx_dialect >= cxx20 ? DK_PEDWARN : DK_WARNING,
32575 input_location, OPT_Wtemplate_id_cdtor,
32576 "template-id not allowed for constructor in C++20"))
32577 inform (input_location, "remove the %qs", "< >");
32580 return constructor_p;
32583 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32584 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32585 they must be performed once we are in the scope of the function.
32587 Returns the function defined. */
32589 static tree
32590 cp_parser_function_definition_from_specifiers_and_declarator
32591 (cp_parser* parser,
32592 cp_decl_specifier_seq *decl_specifiers,
32593 tree attributes,
32594 const cp_declarator *declarator)
32596 tree fn;
32597 bool success_p;
32599 /* Begin the function-definition. */
32600 success_p = start_function (decl_specifiers, declarator, attributes);
32602 /* The things we're about to see are not directly qualified by any
32603 template headers we've seen thus far. */
32604 reset_specialization ();
32606 /* If there were names looked up in the decl-specifier-seq that we
32607 did not check, check them now. We must wait until we are in the
32608 scope of the function to perform the checks, since the function
32609 might be a friend. */
32610 perform_deferred_access_checks (tf_warning_or_error);
32612 if (success_p)
32614 cp_finalize_omp_declare_simd (parser, current_function_decl);
32615 parser->omp_declare_simd = NULL;
32616 cp_finalize_oacc_routine (parser, current_function_decl, true);
32617 parser->oacc_routine = NULL;
32620 if (!success_p)
32622 /* Skip the entire function. */
32623 cp_parser_skip_to_end_of_block_or_statement (parser);
32624 fn = error_mark_node;
32626 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32628 /* Seen already, skip it. An error message has already been output. */
32629 cp_parser_skip_to_end_of_block_or_statement (parser);
32630 fn = current_function_decl;
32631 current_function_decl = NULL_TREE;
32632 /* If this is a function from a class, pop the nested class. */
32633 if (current_class_name)
32634 pop_nested_class ();
32636 else
32638 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32639 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32640 fn = cp_parser_function_definition_after_declarator (parser,
32641 /*inline_p=*/false);
32644 return fn;
32647 /* Parse the part of a function-definition that follows the
32648 declarator. INLINE_P is TRUE iff this function is an inline
32649 function defined within a class-specifier.
32651 Returns the function defined. */
32653 static tree
32654 cp_parser_function_definition_after_declarator (cp_parser* parser,
32655 bool inline_p)
32657 tree fn;
32658 bool saved_in_unbraced_linkage_specification_p;
32659 bool saved_in_function_body;
32660 unsigned saved_num_template_parameter_lists;
32661 cp_token *token;
32662 bool fully_implicit_function_template_p
32663 = parser->fully_implicit_function_template_p;
32664 parser->fully_implicit_function_template_p = false;
32665 tree implicit_template_parms
32666 = parser->implicit_template_parms;
32667 parser->implicit_template_parms = 0;
32668 cp_binding_level* implicit_template_scope
32669 = parser->implicit_template_scope;
32670 parser->implicit_template_scope = 0;
32672 saved_in_function_body = parser->in_function_body;
32673 parser->in_function_body = true;
32674 /* If the next token is `return', then the code may be trying to
32675 make use of the "named return value" extension that G++ used to
32676 support. */
32677 token = cp_lexer_peek_token (parser->lexer);
32678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32680 /* Consume the `return' keyword. */
32681 cp_lexer_consume_token (parser->lexer);
32682 /* Look for the identifier that indicates what value is to be
32683 returned. */
32684 cp_parser_identifier (parser);
32685 /* Issue an error message. */
32686 error_at (token->location,
32687 "named return values are no longer supported");
32688 /* Skip tokens until we reach the start of the function body. */
32689 while (true)
32691 cp_token *token = cp_lexer_peek_token (parser->lexer);
32692 if (token->type == CPP_OPEN_BRACE
32693 || token->type == CPP_EOF
32694 || token->type == CPP_PRAGMA_EOL)
32695 break;
32696 cp_lexer_consume_token (parser->lexer);
32699 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32700 anything declared inside `f'. */
32701 saved_in_unbraced_linkage_specification_p
32702 = parser->in_unbraced_linkage_specification_p;
32703 parser->in_unbraced_linkage_specification_p = false;
32704 /* Inside the function, surrounding template-parameter-lists do not
32705 apply. */
32706 saved_num_template_parameter_lists
32707 = parser->num_template_parameter_lists;
32708 parser->num_template_parameter_lists = 0;
32710 /* If the next token is `try', `__transaction_atomic', or
32711 `__transaction_relaxed`, then we are looking at either function-try-block
32712 or function-transaction-block. Note that all of these include the
32713 function-body. */
32714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32715 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32716 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32717 RID_TRANSACTION_RELAXED))
32718 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32719 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32720 cp_parser_function_try_block (parser);
32721 else
32722 cp_parser_ctor_initializer_opt_and_function_body
32723 (parser, /*in_function_try_block=*/false);
32725 /* Finish the function. */
32726 fn = finish_function (inline_p);
32728 if (modules_p ()
32729 && !inline_p
32730 && TYPE_P (DECL_CONTEXT (fn))
32731 && (DECL_DECLARED_INLINE_P (fn)
32732 || processing_template_decl))
32733 set_defining_module (fn);
32735 /* Generate code for it, if necessary. */
32736 expand_or_defer_fn (fn);
32738 /* Restore the saved values. */
32739 parser->in_unbraced_linkage_specification_p
32740 = saved_in_unbraced_linkage_specification_p;
32741 parser->num_template_parameter_lists
32742 = saved_num_template_parameter_lists;
32743 parser->in_function_body = saved_in_function_body;
32745 parser->fully_implicit_function_template_p
32746 = fully_implicit_function_template_p;
32747 parser->implicit_template_parms
32748 = implicit_template_parms;
32749 parser->implicit_template_scope
32750 = implicit_template_scope;
32752 if (parser->fully_implicit_function_template_p)
32753 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32755 return fn;
32758 /* Parse a template-declaration body (following argument list). */
32760 static void
32761 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32762 tree parameter_list,
32763 bool member_p)
32765 tree decl = NULL_TREE;
32766 bool friend_p = false;
32768 /* We just processed one more parameter list. */
32769 ++parser->num_template_parameter_lists;
32771 /* Get the deferred access checks from the parameter list. These
32772 will be checked once we know what is being declared, as for a
32773 member template the checks must be performed in the scope of the
32774 class containing the member. */
32775 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32777 /* Tentatively parse for a new template parameter list, which can either be
32778 the template keyword or a template introduction. */
32779 if (cp_parser_template_declaration_after_export (parser, member_p))
32780 /* OK */;
32781 else if (cxx_dialect >= cxx11
32782 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32783 decl = cp_parser_alias_declaration (parser);
32784 else if (flag_concepts
32785 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32786 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32787 /* -fconcept-ts 'concept bool' syntax is handled below, in
32788 cp_parser_single_declaration. */
32789 decl = cp_parser_concept_definition (parser);
32790 else
32792 cp_token *token = cp_lexer_peek_token (parser->lexer);
32793 decl = cp_parser_single_declaration (parser,
32794 checks,
32795 member_p,
32796 /*explicit_specialization_p=*/false,
32797 &friend_p);
32799 /* If this is a member template declaration, let the front
32800 end know. */
32801 if (member_p && !friend_p && decl)
32803 if (TREE_CODE (decl) == TYPE_DECL)
32804 cp_parser_check_access_in_redeclaration (decl, token->location);
32806 decl = finish_member_template_decl (decl);
32808 else if (friend_p && decl
32809 && DECL_DECLARES_TYPE_P (decl))
32810 make_friend_class (current_class_type, TREE_TYPE (decl),
32811 /*complain=*/true);
32813 /* We are done with the current parameter list. */
32814 --parser->num_template_parameter_lists;
32816 pop_deferring_access_checks ();
32818 /* Finish up. */
32819 finish_template_decl (parameter_list);
32821 /* Check the template arguments for a literal operator template. */
32822 if (decl
32823 && DECL_DECLARES_FUNCTION_P (decl)
32824 && UDLIT_OPER_P (DECL_NAME (decl)))
32826 bool ok = true;
32827 if (parameter_list == NULL_TREE)
32828 ok = false;
32829 else
32831 int num_parms = TREE_VEC_LENGTH (parameter_list);
32832 if (num_parms == 1)
32834 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32835 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32836 if (TREE_CODE (parm) != PARM_DECL)
32837 ok = false;
32838 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32839 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32840 /* OK, C++20 string literal operator template. We don't need
32841 to warn in lower dialects here because we will have already
32842 warned about the template parameter. */;
32843 else if (TREE_TYPE (parm) != char_type_node
32844 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32845 ok = false;
32847 else if (num_parms == 2 && cxx_dialect >= cxx14)
32849 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32850 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32851 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32852 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32853 if (TREE_CODE (parm) != PARM_DECL
32854 || TREE_TYPE (parm) != TREE_TYPE (type)
32855 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32856 ok = false;
32857 else
32858 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32859 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32860 "ISO C++ did not adopt string literal operator templa"
32861 "tes taking an argument pack of characters");
32863 else
32864 ok = false;
32866 if (!ok)
32868 if (cxx_dialect > cxx17)
32869 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32870 "template %qD has invalid parameter list; expected "
32871 "non-type template parameter pack %<<char...>%> or "
32872 "single non-type parameter of class type",
32873 decl);
32874 else
32875 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32876 "template %qD has invalid parameter list; expected "
32877 "non-type template parameter pack %<<char...>%>",
32878 decl);
32882 /* Register member declarations. */
32883 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32884 finish_member_declaration (decl);
32885 /* If DECL is a function template, we must return to parse it later.
32886 (Even though there is no definition, there might be default
32887 arguments that need handling.) */
32888 if (member_p && decl
32889 && DECL_DECLARES_FUNCTION_P (decl))
32890 vec_safe_push (unparsed_funs_with_definitions, decl);
32893 /* Parse a template introduction header for a template-declaration. Returns
32894 false if tentative parse fails. */
32896 static bool
32897 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32899 cp_parser_parse_tentatively (parser);
32901 tree saved_scope = parser->scope;
32902 tree saved_object_scope = parser->object_scope;
32903 tree saved_qualifying_scope = parser->qualifying_scope;
32904 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32906 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32908 /* In classes don't parse valid unnamed bitfields as invalid
32909 template introductions. */
32910 if (member_p)
32911 parser->colon_corrects_to_scope_p = false;
32913 /* Look for the optional `::' operator. */
32914 cp_parser_global_scope_opt (parser,
32915 /*current_scope_valid_p=*/false);
32916 /* Look for the nested-name-specifier. */
32917 cp_parser_nested_name_specifier_opt (parser,
32918 /*typename_keyword_p=*/false,
32919 /*check_dependency_p=*/true,
32920 /*type_p=*/false,
32921 /*is_declaration=*/false);
32923 cp_token *token = cp_lexer_peek_token (parser->lexer);
32924 tree concept_name = cp_parser_identifier (parser);
32926 /* Look up the concept for which we will be matching
32927 template parameters. */
32928 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32929 token->location);
32930 parser->scope = saved_scope;
32931 parser->object_scope = saved_object_scope;
32932 parser->qualifying_scope = saved_qualifying_scope;
32933 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32935 if (concept_name == error_mark_node
32936 || (seen_error () && !concept_definition_p (tmpl_decl)))
32937 cp_parser_simulate_error (parser);
32939 /* Look for opening brace for introduction. */
32940 matching_braces braces;
32941 braces.require_open (parser);
32942 location_t open_loc = input_location;
32944 if (!cp_parser_parse_definitely (parser))
32945 return false;
32947 push_deferring_access_checks (dk_deferred);
32949 /* Build vector of placeholder parameters and grab
32950 matching identifiers. */
32951 tree introduction_list = cp_parser_introduction_list (parser);
32953 /* Look for closing brace for introduction. */
32954 if (!braces.require_close (parser))
32955 return true;
32957 /* The introduction-list shall not be empty. */
32958 int nargs = TREE_VEC_LENGTH (introduction_list);
32959 if (nargs == 0)
32961 /* In cp_parser_introduction_list we have already issued an error. */
32962 return true;
32965 if (tmpl_decl == error_mark_node)
32967 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32968 token->location);
32969 return true;
32972 /* Build and associate the constraint. */
32973 location_t introduction_loc = make_location (open_loc,
32974 start_token->location,
32975 parser->lexer);
32976 tree parms = finish_template_introduction (tmpl_decl,
32977 introduction_list,
32978 introduction_loc);
32979 if (parms && parms != error_mark_node)
32981 if (!flag_concepts_ts)
32982 pedwarn (introduction_loc, 0, "template-introductions"
32983 " are not part of C++20 concepts; use %qs to enable",
32984 "-fconcepts-ts");
32986 cp_parser_template_declaration_after_parameters (parser, parms,
32987 member_p);
32988 return true;
32991 if (parms == NULL_TREE)
32992 error_at (token->location, "no matching concept for template-introduction");
32994 return true;
32997 /* Parse a normal template-declaration following the template keyword. */
32999 static void
33000 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
33002 tree parameter_list;
33003 bool need_lang_pop;
33004 location_t location = input_location;
33006 /* Look for the `<' token. */
33007 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
33008 return;
33009 if (at_class_scope_p () && current_function_decl)
33011 /* 14.5.2.2 [temp.mem]
33013 A local class shall not have member templates. */
33014 error_at (location,
33015 "invalid declaration of member template in local class");
33016 cp_parser_skip_to_end_of_block_or_statement (parser);
33017 return;
33019 /* [temp]
33021 A template ... shall not have C linkage. */
33022 if (current_lang_name == lang_name_c)
33024 error_at (location, "template with C linkage");
33025 maybe_show_extern_c_location ();
33026 /* Give it C++ linkage to avoid confusing other parts of the
33027 front end. */
33028 push_lang_context (lang_name_cplusplus);
33029 need_lang_pop = true;
33031 else
33032 need_lang_pop = false;
33034 /* We cannot perform access checks on the template parameter
33035 declarations until we know what is being declared, just as we
33036 cannot check the decl-specifier list. */
33037 push_deferring_access_checks (dk_deferred);
33039 /* If the next token is `>', then we have an invalid
33040 specialization. Rather than complain about an invalid template
33041 parameter, issue an error message here. */
33042 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
33044 cp_parser_error (parser, "invalid explicit specialization");
33045 begin_specialization ();
33046 parameter_list = NULL_TREE;
33048 else
33050 /* Parse the template parameters. */
33051 parameter_list = cp_parser_template_parameter_list (parser);
33054 /* Look for the `>'. */
33055 cp_parser_require_end_of_template_parameter_list (parser);
33057 /* Manage template requirements */
33058 if (flag_concepts)
33060 tree reqs = get_shorthand_constraints (current_template_parms);
33061 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
33062 reqs = combine_constraint_expressions (reqs, treqs);
33063 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
33066 cp_parser_template_declaration_after_parameters (parser, parameter_list,
33067 member_p);
33069 /* For the erroneous case of a template with C linkage, we pushed an
33070 implicit C++ linkage scope; exit that scope now. */
33071 if (need_lang_pop)
33072 pop_lang_context ();
33075 /* Parse a template-declaration, assuming that the `export' (and
33076 `extern') keywords, if present, has already been scanned. MEMBER_P
33077 is as for cp_parser_template_declaration. */
33079 static bool
33080 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
33082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33084 cp_lexer_consume_token (parser->lexer);
33085 cp_parser_explicit_template_declaration (parser, member_p);
33086 return true;
33088 else if (flag_concepts)
33089 return cp_parser_template_introduction (parser, member_p);
33091 return false;
33094 /* Perform the deferred access checks from a template-parameter-list.
33095 CHECKS is a TREE_LIST of access checks, as returned by
33096 get_deferred_access_checks. */
33098 static void
33099 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
33101 ++processing_template_parmlist;
33102 perform_access_checks (checks, tf_warning_or_error);
33103 --processing_template_parmlist;
33106 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
33107 `function-definition' sequence that follows a template header.
33108 If MEMBER_P is true, this declaration appears in a class scope.
33110 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
33111 *FRIEND_P is set to TRUE iff the declaration is a friend. */
33113 static tree
33114 cp_parser_single_declaration (cp_parser* parser,
33115 vec<deferred_access_check, va_gc> *checks,
33116 bool member_p,
33117 bool explicit_specialization_p,
33118 bool* friend_p)
33120 int declares_class_or_enum;
33121 tree decl = NULL_TREE;
33122 cp_decl_specifier_seq decl_specifiers;
33123 bool function_definition_p = false;
33124 cp_token *decl_spec_token_start;
33126 /* This function is only used when processing a template
33127 declaration. */
33128 gcc_assert (innermost_scope_kind () == sk_template_parms
33129 || innermost_scope_kind () == sk_template_spec);
33131 /* Defer access checks until we know what is being declared. */
33132 push_deferring_access_checks (dk_deferred);
33134 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
33135 alternative. */
33136 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
33137 cp_parser_decl_specifier_seq (parser,
33138 (CP_PARSER_FLAGS_OPTIONAL
33139 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
33140 &decl_specifiers,
33141 &declares_class_or_enum);
33143 cp_omp_declare_simd_data odsd;
33144 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
33145 cp_parser_handle_directive_omp_attributes (parser,
33146 &decl_specifiers.attributes,
33147 &odsd, true);
33149 if (friend_p)
33150 *friend_p = cp_parser_friend_p (&decl_specifiers);
33152 /* There are no template typedefs. */
33153 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
33155 error_at (decl_spec_token_start->location,
33156 "template declaration of %<typedef%>");
33157 decl = error_mark_node;
33160 /* Gather up the access checks that occurred the
33161 decl-specifier-seq. */
33162 stop_deferring_access_checks ();
33164 /* Check for the declaration of a template class. */
33165 if (declares_class_or_enum)
33167 if (cp_parser_declares_only_class_p (parser)
33168 || (declares_class_or_enum & 2))
33170 decl = shadow_tag (&decl_specifiers);
33172 /* In this case:
33174 struct C {
33175 friend template <typename T> struct A<T>::B;
33178 A<T>::B will be represented by a TYPENAME_TYPE, and
33179 therefore not recognized by shadow_tag. */
33180 if (friend_p && *friend_p
33181 && !decl
33182 && decl_specifiers.type
33183 && TYPE_P (decl_specifiers.type))
33184 decl = decl_specifiers.type;
33186 if (decl && decl != error_mark_node)
33187 decl = TYPE_NAME (decl);
33188 else
33189 decl = error_mark_node;
33191 /* If this is a declaration, but not a definition, associate
33192 any constraints with the type declaration. Constraints
33193 are associated with definitions in cp_parser_class_specifier. */
33194 if (declares_class_or_enum == 1)
33195 associate_classtype_constraints (TREE_TYPE (decl));
33197 /* Perform access checks for template parameters. */
33198 cp_parser_perform_template_parameter_access_checks (checks);
33200 /* Give a helpful diagnostic for
33201 template <class T> struct A { } a;
33202 if we aren't already recovering from an error. */
33203 if (!cp_parser_declares_only_class_p (parser)
33204 && !seen_error ())
33206 error_at (cp_lexer_peek_token (parser->lexer)->location,
33207 "a class template declaration must not declare "
33208 "anything else");
33209 cp_parser_skip_to_end_of_block_or_statement (parser);
33210 goto out;
33215 /* Complain about missing 'typename' or other invalid type names. */
33216 if (!decl_specifiers.any_type_specifiers_p
33217 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
33219 /* cp_parser_parse_and_diagnose_invalid_type_name calls
33220 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
33221 the rest of this declaration. */
33222 decl = error_mark_node;
33223 goto out;
33226 /* If it's not a template class, try for a template function. If
33227 the next token is a `;', then this declaration does not declare
33228 anything. But, if there were errors in the decl-specifiers, then
33229 the error might well have come from an attempted class-specifier.
33230 In that case, there's no need to warn about a missing declarator. */
33231 if (!decl
33232 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
33233 || decl_specifiers.type != error_mark_node))
33235 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
33236 /* We don't delay parsing for friends, though CWG 2510 may change
33237 that. */
33238 if (member_p && !(friend_p && *friend_p))
33239 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
33240 decl = cp_parser_init_declarator (parser,
33241 flags,
33242 &decl_specifiers,
33243 checks,
33244 /*function_definition_allowed_p=*/true,
33245 member_p,
33246 declares_class_or_enum,
33247 &function_definition_p,
33248 NULL, NULL, NULL);
33250 /* 7.1.1-1 [dcl.stc]
33252 A storage-class-specifier shall not be specified in an explicit
33253 specialization... */
33254 if (decl
33255 && explicit_specialization_p
33256 && decl_specifiers.storage_class != sc_none)
33258 error_at (decl_spec_token_start->location,
33259 "explicit template specialization cannot have a storage class");
33260 decl = error_mark_node;
33263 if (decl && VAR_P (decl))
33264 check_template_variable (decl);
33267 /* Look for a trailing `;' after the declaration. */
33268 if (!function_definition_p
33269 && (decl == error_mark_node
33270 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
33271 cp_parser_skip_to_end_of_block_or_statement (parser);
33273 out:
33274 pop_deferring_access_checks ();
33276 /* Clear any current qualification; whatever comes next is the start
33277 of something new. */
33278 parser->scope = NULL_TREE;
33279 parser->qualifying_scope = NULL_TREE;
33280 parser->object_scope = NULL_TREE;
33282 cp_finalize_omp_declare_simd (parser, &odsd);
33284 return decl;
33287 /* Parse a cast-expression that is not the operand of a unary "&". */
33289 static cp_expr
33290 cp_parser_simple_cast_expression (cp_parser *parser)
33292 return cp_parser_cast_expression (parser, /*address_p=*/false,
33293 /*cast_p=*/false, /*decltype*/false, NULL);
33296 /* Parse a functional cast to TYPE. Returns an expression
33297 representing the cast. */
33299 static cp_expr
33300 cp_parser_functional_cast (cp_parser* parser, tree type)
33302 vec<tree, va_gc> *vec;
33303 tree expression_list;
33304 cp_expr cast;
33306 location_t start_loc = input_location;
33308 if (!type)
33309 type = error_mark_node;
33311 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33313 cp_lexer_set_source_position (parser->lexer);
33314 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33315 expression_list = cp_parser_braced_list (parser);
33316 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
33317 if (TREE_CODE (type) == TYPE_DECL)
33318 type = TREE_TYPE (type);
33320 cast = finish_compound_literal (type, expression_list,
33321 tf_warning_or_error, fcl_functional);
33322 /* Create a location of the form:
33323 type_name{i, f}
33324 ^~~~~~~~~~~~~~~
33325 with caret == start at the start of the type name,
33326 finishing at the closing brace. */
33327 location_t combined_loc = make_location (start_loc, start_loc,
33328 parser->lexer);
33329 cast.set_location (combined_loc);
33330 return cast;
33334 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
33335 /*cast_p=*/true,
33336 /*allow_expansion_p=*/true,
33337 /*non_constant_p=*/NULL);
33338 if (vec == NULL)
33339 expression_list = error_mark_node;
33340 else
33342 expression_list = build_tree_list_vec (vec);
33343 release_tree_vector (vec);
33346 /* Create a location of the form:
33347 float(i)
33348 ^~~~~~~~
33349 with caret == start at the start of the type name,
33350 finishing at the closing paren. */
33351 location_t combined_loc = make_location (start_loc, start_loc,
33352 parser->lexer);
33353 cast = build_functional_cast (combined_loc, type, expression_list,
33354 tf_warning_or_error);
33356 /* [expr.const]/1: In an integral constant expression "only type
33357 conversions to integral or enumeration type can be used". */
33358 if (TREE_CODE (type) == TYPE_DECL)
33359 type = TREE_TYPE (type);
33360 if (cast != error_mark_node
33361 && !cast_valid_in_integral_constant_expression_p (type)
33362 && cp_parser_non_integral_constant_expression (parser,
33363 NIC_CONSTRUCTOR))
33364 return error_mark_node;
33366 return cast;
33369 /* Save the tokens that make up the body of a member function defined
33370 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
33371 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
33372 specifiers applied to the declaration. Returns the FUNCTION_DECL
33373 for the member function. */
33375 static tree
33376 cp_parser_save_member_function_body (cp_parser* parser,
33377 cp_decl_specifier_seq *decl_specifiers,
33378 cp_declarator *declarator,
33379 tree attributes)
33381 cp_token *first;
33382 cp_token *last;
33383 tree fn;
33384 bool function_try_block = false;
33386 /* Create the FUNCTION_DECL. */
33387 fn = grokmethod (decl_specifiers, declarator, attributes);
33388 cp_finalize_omp_declare_simd (parser, fn);
33389 cp_finalize_oacc_routine (parser, fn, true);
33390 /* If something went badly wrong, bail out now. */
33391 if (fn == error_mark_node)
33393 /* If there's a function-body, skip it. */
33394 if (cp_parser_token_starts_function_definition_p
33395 (cp_lexer_peek_token (parser->lexer)))
33396 cp_parser_skip_to_end_of_block_or_statement (parser);
33397 return error_mark_node;
33400 /* Remember it, if there are default args to post process. */
33401 cp_parser_save_default_args (parser, fn);
33403 /* Save away the tokens that make up the body of the
33404 function. */
33405 first = parser->lexer->next_token;
33407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
33408 cp_lexer_consume_token (parser->lexer);
33409 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33410 RID_TRANSACTION_ATOMIC))
33412 cp_lexer_consume_token (parser->lexer);
33413 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
33414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
33415 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
33416 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
33417 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
33418 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
33419 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
33421 cp_lexer_consume_token (parser->lexer);
33422 cp_lexer_consume_token (parser->lexer);
33423 cp_lexer_consume_token (parser->lexer);
33424 cp_lexer_consume_token (parser->lexer);
33425 cp_lexer_consume_token (parser->lexer);
33427 else
33428 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33429 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33431 cp_lexer_consume_token (parser->lexer);
33432 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33433 break;
33437 /* Handle function try blocks. */
33438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33440 cp_lexer_consume_token (parser->lexer);
33441 function_try_block = true;
33443 /* We can have braced-init-list mem-initializers before the fn body. */
33444 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33446 cp_lexer_consume_token (parser->lexer);
33447 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33449 /* cache_group will stop after an un-nested { } pair, too. */
33450 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33451 break;
33453 /* variadic mem-inits have ... after the ')'. */
33454 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33455 cp_lexer_consume_token (parser->lexer);
33458 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33459 /* Handle function try blocks. */
33460 if (function_try_block)
33461 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33462 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33463 last = parser->lexer->next_token;
33465 /* Save away the inline definition; we will process it when the
33466 class is complete. */
33467 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33468 DECL_PENDING_INLINE_P (fn) = 1;
33470 /* We need to know that this was defined in the class, so that
33471 friend templates are handled correctly. */
33472 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33474 /* Add FN to the queue of functions to be parsed later. */
33475 vec_safe_push (unparsed_funs_with_definitions, fn);
33477 return fn;
33480 /* Save the tokens that make up the in-class initializer for a non-static
33481 data member. Returns a DEFERRED_PARSE. */
33483 static tree
33484 cp_parser_save_nsdmi (cp_parser* parser)
33486 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33489 /* Parse a template-argument-list, as well as the trailing ">" (but
33490 not the opening "<"). See cp_parser_template_argument_list for the
33491 return value. */
33493 static tree
33494 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33496 tree arguments;
33497 tree saved_scope;
33498 tree saved_qualifying_scope;
33499 tree saved_object_scope;
33500 bool saved_greater_than_is_operator_p;
33502 /* [temp.names]
33504 When parsing a template-id, the first non-nested `>' is taken as
33505 the end of the template-argument-list rather than a greater-than
33506 operator. */
33507 saved_greater_than_is_operator_p
33508 = parser->greater_than_is_operator_p;
33509 parser->greater_than_is_operator_p = false;
33510 /* Parsing the argument list may modify SCOPE, so we save it
33511 here. */
33512 saved_scope = parser->scope;
33513 saved_qualifying_scope = parser->qualifying_scope;
33514 saved_object_scope = parser->object_scope;
33515 /* We need to evaluate the template arguments, even though this
33516 template-id may be nested within a "sizeof". */
33517 cp_evaluated ev;
33518 /* Parse the template-argument-list itself. */
33519 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33520 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33521 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33522 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33524 arguments = make_tree_vec (0);
33525 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33527 else
33528 arguments = cp_parser_template_argument_list (parser);
33529 /* Look for the `>' that ends the template-argument-list. If we find
33530 a '>>' instead, it's probably just a typo. */
33531 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33533 if (cxx_dialect != cxx98)
33535 /* In C++0x, a `>>' in a template argument list or cast
33536 expression is considered to be two separate `>'
33537 tokens. So, change the current token to a `>', but don't
33538 consume it: it will be consumed later when the outer
33539 template argument list (or cast expression) is parsed.
33540 Note that this replacement of `>' for `>>' is necessary
33541 even if we are parsing tentatively: in the tentative
33542 case, after calling
33543 cp_parser_enclosed_template_argument_list we will always
33544 throw away all of the template arguments and the first
33545 closing `>', either because the template argument list
33546 was erroneous or because we are replacing those tokens
33547 with a CPP_TEMPLATE_ID token. The second `>' (which will
33548 not have been thrown away) is needed either to close an
33549 outer template argument list or to complete a new-style
33550 cast. */
33551 cp_token *token = cp_lexer_peek_token (parser->lexer);
33552 token->type = CPP_GREATER;
33554 else if (!saved_greater_than_is_operator_p)
33556 /* If we're in a nested template argument list, the '>>' has
33557 to be a typo for '> >'. We emit the error message, but we
33558 continue parsing and we push a '>' as next token, so that
33559 the argument list will be parsed correctly. Note that the
33560 global source location is still on the token before the
33561 '>>', so we need to say explicitly where we want it. */
33562 cp_token *token = cp_lexer_peek_token (parser->lexer);
33563 gcc_rich_location richloc (token->location);
33564 richloc.add_fixit_replace ("> >");
33565 error_at (&richloc, "%<>>%> should be %<> >%> "
33566 "within a nested template argument list");
33568 token->type = CPP_GREATER;
33570 else
33572 /* If this is not a nested template argument list, the '>>'
33573 is a typo for '>'. Emit an error message and continue.
33574 Same deal about the token location, but here we can get it
33575 right by consuming the '>>' before issuing the diagnostic. */
33576 cp_token *token = cp_lexer_consume_token (parser->lexer);
33577 error_at (token->location,
33578 "spurious %<>>%>, use %<>%> to terminate "
33579 "a template argument list");
33582 /* Similarly for >>= and >=. */
33583 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33584 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33586 cp_token *token = cp_lexer_consume_token (parser->lexer);
33587 gcc_rich_location richloc (token->location);
33588 enum cpp_ttype new_type;
33589 const char *replacement;
33590 if (token->type == CPP_GREATER_EQ)
33592 replacement = "> =";
33593 new_type = CPP_EQ;
33595 else if (!saved_greater_than_is_operator_p)
33597 if (cxx_dialect != cxx98)
33598 replacement = ">> =";
33599 else
33600 replacement = "> > =";
33601 new_type = CPP_GREATER;
33603 else
33605 replacement = "> >=";
33606 new_type = CPP_GREATER_EQ;
33608 richloc.add_fixit_replace (replacement);
33609 error_at (&richloc, "%qs should be %qs to terminate a template "
33610 "argument list",
33611 cpp_type2name (token->type, token->flags), replacement);
33612 token->type = new_type;
33614 else
33615 cp_parser_require_end_of_template_parameter_list (parser);
33616 /* The `>' token might be a greater-than operator again now. */
33617 parser->greater_than_is_operator_p
33618 = saved_greater_than_is_operator_p;
33619 /* Restore the SAVED_SCOPE. */
33620 parser->scope = saved_scope;
33621 parser->qualifying_scope = saved_qualifying_scope;
33622 parser->object_scope = saved_object_scope;
33624 return arguments;
33627 /* MEMBER_FUNCTION is a member function, or a friend. If default
33628 arguments, or the body of the function have not yet been parsed,
33629 parse them now. */
33631 static void
33632 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33634 auto_timevar tv (TV_PARSE_INMETH);
33636 /* If this member is a template, get the underlying
33637 FUNCTION_DECL. */
33638 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33639 member_function = DECL_TEMPLATE_RESULT (member_function);
33641 /* There should not be any class definitions in progress at this
33642 point; the bodies of members are only parsed outside of all class
33643 definitions. */
33644 gcc_assert (parser->num_classes_being_defined == 0);
33645 /* While we're parsing the member functions we might encounter more
33646 classes. We want to handle them right away, but we don't want
33647 them getting mixed up with functions that are currently in the
33648 queue. */
33649 push_unparsed_function_queues (parser);
33651 /* Make sure that any template parameters are in scope. */
33652 maybe_begin_member_template_processing (member_function);
33654 /* If the body of the function has not yet been parsed, parse it
33655 now. Except if the tokens have been purged (PR c++/39751). */
33656 if (DECL_PENDING_INLINE_P (member_function)
33657 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33659 tree function_scope;
33660 cp_token_cache *tokens;
33662 /* The function is no longer pending; we are processing it. */
33663 tokens = DECL_PENDING_INLINE_INFO (member_function);
33664 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33665 DECL_PENDING_INLINE_P (member_function) = 0;
33667 /* If this is a local class, enter the scope of the containing
33668 function. */
33669 function_scope = current_function_decl;
33670 if (function_scope)
33671 push_function_context ();
33673 /* Push the body of the function onto the lexer stack. */
33674 cp_parser_push_lexer_for_tokens (parser, tokens);
33676 /* Let the front end know that we going to be defining this
33677 function. */
33678 start_preparsed_function (member_function, NULL_TREE,
33679 SF_PRE_PARSED | SF_INCLASS_INLINE);
33681 /* #pragma omp declare reduction needs special parsing. */
33682 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33684 parser->lexer->in_pragma = true;
33685 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33686 finish_function (/*inline_p=*/true);
33687 cp_check_omp_declare_reduction (member_function);
33689 else
33690 /* Now, parse the body of the function. */
33691 cp_parser_function_definition_after_declarator (parser,
33692 /*inline_p=*/true);
33694 /* Leave the scope of the containing function. */
33695 if (function_scope)
33696 pop_function_context ();
33697 cp_parser_pop_lexer (parser);
33700 /* Remove any template parameters from the symbol table. */
33701 maybe_end_member_template_processing ();
33703 /* Restore the queue. */
33704 pop_unparsed_function_queues (parser);
33707 /* If DECL contains any default args, remember it on the unparsed
33708 functions queue. */
33710 static void
33711 cp_parser_save_default_args (cp_parser* parser, tree decl)
33713 tree probe;
33715 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33716 probe;
33717 probe = TREE_CHAIN (probe))
33718 if (TREE_PURPOSE (probe))
33720 cp_default_arg_entry entry = {current_class_type, decl};
33721 vec_safe_push (unparsed_funs_with_default_args, entry);
33722 break;
33725 /* Remember if there is a noexcept-specifier to post process. */
33726 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33727 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33728 vec_safe_push (unparsed_noexcepts, decl);
33730 /* Contracts are deferred. */
33731 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33732 if (cxx_contract_attribute_p (attr))
33734 vec_safe_push (unparsed_contracts, decl);
33735 break;
33739 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33740 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33741 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33742 from the parameter-type-list. */
33744 static tree
33745 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33746 tree default_arg, tree parmtype)
33748 cp_token_cache *tokens;
33749 tree parsed_arg;
33751 if (default_arg == error_mark_node)
33752 return error_mark_node;
33754 /* Push the saved tokens for the default argument onto the parser's
33755 lexer stack. */
33756 tokens = DEFPARSE_TOKENS (default_arg);
33757 cp_parser_push_lexer_for_tokens (parser, tokens);
33759 start_lambda_scope (decl);
33761 /* Parse the default argument. */
33762 parsed_arg = cp_parser_initializer (parser);
33763 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33766 finish_lambda_scope ();
33768 if (parsed_arg == error_mark_node)
33769 cp_parser_skip_to_end_of_statement (parser);
33771 if (!processing_template_decl)
33773 /* In a non-template class, check conversions now. In a template,
33774 we'll wait and instantiate these as needed. */
33775 if (TREE_CODE (decl) == PARM_DECL)
33776 parsed_arg = check_default_argument (parmtype, parsed_arg,
33777 tf_warning_or_error);
33778 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33779 parsed_arg = error_mark_node;
33780 else
33781 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33784 /* If the token stream has not been completely used up, then
33785 there was extra junk after the end of the default
33786 argument. */
33787 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33789 if (TREE_CODE (decl) == PARM_DECL)
33790 cp_parser_error (parser, "expected %<,%>");
33791 else
33792 cp_parser_error (parser, "expected %<;%>");
33795 /* Revert to the main lexer. */
33796 cp_parser_pop_lexer (parser);
33798 return parsed_arg;
33801 /* FIELD is a non-static data member with an initializer which we saved for
33802 later; parse it now. */
33804 static void
33805 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33807 tree def;
33809 maybe_begin_member_template_processing (field);
33811 push_unparsed_function_queues (parser);
33812 def = cp_parser_late_parse_one_default_arg (parser, field,
33813 DECL_INITIAL (field),
33814 NULL_TREE);
33815 pop_unparsed_function_queues (parser);
33817 maybe_end_member_template_processing ();
33819 DECL_INITIAL (field) = def;
33822 /* FN is a FUNCTION_DECL which may contains a parameter with an
33823 unparsed DEFERRED_PARSE. Parse the default args now. This function
33824 assumes that the current scope is the scope in which the default
33825 argument should be processed. */
33827 static void
33828 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33830 unsigned char saved_local_variables_forbidden_p;
33832 /* While we're parsing the default args, we might (due to the
33833 statement expression extension) encounter more classes. We want
33834 to handle them right away, but we don't want them getting mixed
33835 up with default args that are currently in the queue. */
33836 push_unparsed_function_queues (parser);
33838 /* Local variable names (and the `this' keyword) may not appear
33839 in a default argument. */
33840 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33841 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33843 push_defarg_context (fn);
33845 begin_scope (sk_function_parms, fn);
33847 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33848 pushdecl clears DECL_CHAIN. */
33849 releasing_vec parms;
33850 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33851 parmdecl = DECL_CHAIN (parmdecl))
33852 vec_safe_push (parms, parmdecl);
33854 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33855 for (int i = 0;
33856 parm && parm != void_list_node;
33857 parm = TREE_CHAIN (parm),
33858 ++i)
33860 tree default_arg = TREE_PURPOSE (parm);
33861 tree parsed_arg;
33863 tree parmdecl = parms[i];
33864 pushdecl (parmdecl);
33866 if (!default_arg)
33867 continue;
33869 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33870 /* This can happen for a friend declaration for a function
33871 already declared with default arguments. */
33872 continue;
33874 parsed_arg
33875 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33876 default_arg,
33877 TREE_VALUE (parm));
33878 TREE_PURPOSE (parm) = parsed_arg;
33880 /* Update any instantiations we've already created. */
33881 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33882 TREE_PURPOSE (copy) = parsed_arg;
33885 pop_bindings_and_leave_scope ();
33887 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33888 parm = NULL_TREE;
33889 for (int i = parms->length () - 1; i >= 0; --i)
33891 DECL_CHAIN (parms[i]) = parm;
33892 parm = parms[i];
33895 pop_defarg_context ();
33897 /* Make sure no default arg is missing. */
33898 check_default_args (fn);
33900 /* Restore the state of local_variables_forbidden_p. */
33901 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33903 /* Restore the queue. */
33904 pop_unparsed_function_queues (parser);
33907 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33909 sizeof ... ( identifier )
33911 where the 'sizeof' token has already been consumed. */
33913 static tree
33914 cp_parser_sizeof_pack (cp_parser *parser)
33916 /* Consume the `...'. */
33917 cp_lexer_consume_token (parser->lexer);
33918 maybe_warn_variadic_templates ();
33920 matching_parens parens;
33921 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33922 if (paren)
33923 parens.consume_open (parser);
33924 else
33925 permerror (cp_lexer_peek_token (parser->lexer)->location,
33926 "%<sizeof...%> argument must be surrounded by parentheses");
33928 cp_token *token = cp_lexer_peek_token (parser->lexer);
33929 tree name = cp_parser_identifier (parser);
33930 if (name == error_mark_node)
33931 return error_mark_node;
33932 /* The name is not qualified. */
33933 parser->scope = NULL_TREE;
33934 parser->qualifying_scope = NULL_TREE;
33935 parser->object_scope = NULL_TREE;
33936 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33937 if (expr == error_mark_node)
33938 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33939 token->location);
33940 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33941 expr = TREE_TYPE (expr);
33942 else if (TREE_CODE (expr) == CONST_DECL)
33943 expr = DECL_INITIAL (expr);
33944 expr = make_pack_expansion (expr);
33945 if (expr != error_mark_node)
33946 PACK_EXPANSION_SIZEOF_P (expr) = true;
33948 if (paren)
33949 parens.require_close (parser);
33951 return expr;
33954 /* Parse the operand of `sizeof' (or a similar operator). Returns
33955 either a TYPE or an expression, depending on the form of the
33956 input. The KEYWORD indicates which kind of expression we have
33957 encountered. */
33959 static tree
33960 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33962 tree expr = NULL_TREE;
33963 const char *saved_message;
33964 const char *saved_message_arg;
33965 bool saved_integral_constant_expression_p;
33966 bool saved_non_integral_constant_expression_p;
33968 /* If it's a `...', then we are computing the length of a parameter
33969 pack. */
33970 if (keyword == RID_SIZEOF
33971 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33972 return cp_parser_sizeof_pack (parser);
33974 /* Types cannot be defined in a `sizeof' expression. Save away the
33975 old message. */
33976 saved_message = parser->type_definition_forbidden_message;
33977 saved_message_arg = parser->type_definition_forbidden_message_arg;
33978 parser->type_definition_forbidden_message
33979 = G_("types may not be defined in %qs expressions");
33980 parser->type_definition_forbidden_message_arg
33981 = IDENTIFIER_POINTER (ridpointers[keyword]);
33983 /* The restrictions on constant-expressions do not apply inside
33984 sizeof expressions. */
33985 saved_integral_constant_expression_p
33986 = parser->integral_constant_expression_p;
33987 saved_non_integral_constant_expression_p
33988 = parser->non_integral_constant_expression_p;
33989 parser->integral_constant_expression_p = false;
33991 auto cleanup = make_temp_override
33992 (parser->auto_is_implicit_function_template_parm_p, false);
33994 /* Do not actually evaluate the expression. */
33995 ++cp_unevaluated_operand;
33996 ++c_inhibit_evaluation_warnings;
33997 /* If it's a `(', then we might be looking at the type-id
33998 construction. */
33999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34001 tree type = NULL_TREE;
34003 tentative_firewall firewall (parser);
34005 /* We can't be sure yet whether we're looking at a type-id or an
34006 expression. */
34007 cp_parser_parse_tentatively (parser);
34009 matching_parens parens;
34010 parens.consume_open (parser);
34012 /* Note: as a GNU Extension, compound literals are considered
34013 postfix-expressions as they are in C99, so they are valid
34014 arguments to sizeof. See comment in cp_parser_cast_expression
34015 for details. */
34016 if (cp_parser_compound_literal_p (parser))
34017 cp_parser_simulate_error (parser);
34018 else
34020 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
34021 parser->in_type_id_in_expr_p = true;
34022 /* Look for the type-id. */
34023 type = cp_parser_type_id (parser);
34024 /* Look for the closing `)'. */
34025 parens.require_close (parser);
34026 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
34029 /* If all went well, then we're done. */
34030 if (cp_parser_parse_definitely (parser))
34031 expr = type;
34032 else
34034 /* Commit to the tentative_firewall so we get syntax errors. */
34035 cp_parser_commit_to_tentative_parse (parser);
34037 expr = cp_parser_unary_expression (parser);
34040 else
34041 expr = cp_parser_unary_expression (parser);
34043 /* Go back to evaluating expressions. */
34044 --cp_unevaluated_operand;
34045 --c_inhibit_evaluation_warnings;
34047 /* And restore the old one. */
34048 parser->type_definition_forbidden_message = saved_message;
34049 parser->type_definition_forbidden_message_arg = saved_message_arg;
34050 parser->integral_constant_expression_p
34051 = saved_integral_constant_expression_p;
34052 parser->non_integral_constant_expression_p
34053 = saved_non_integral_constant_expression_p;
34055 return expr;
34058 /* If the current declaration has no declarator, return true. */
34060 static bool
34061 cp_parser_declares_only_class_p (cp_parser *parser)
34063 /* If the next token is a `;' or a `,' then there is no
34064 declarator. */
34065 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34066 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
34069 /* Update the DECL_SPECS to reflect the storage class indicated by
34070 KEYWORD. */
34072 static void
34073 cp_parser_set_storage_class (cp_parser *parser,
34074 cp_decl_specifier_seq *decl_specs,
34075 enum rid keyword,
34076 cp_token *token)
34078 cp_storage_class storage_class;
34080 switch (keyword)
34082 case RID_AUTO:
34083 storage_class = sc_auto;
34084 break;
34085 case RID_REGISTER:
34086 storage_class = sc_register;
34087 break;
34088 case RID_STATIC:
34089 storage_class = sc_static;
34090 break;
34091 case RID_EXTERN:
34092 storage_class = sc_extern;
34093 break;
34094 case RID_MUTABLE:
34095 storage_class = sc_mutable;
34096 break;
34097 default:
34098 gcc_unreachable ();
34101 if (parser->in_unbraced_linkage_specification_p)
34103 error_at (token->location, "invalid use of %qD in linkage specification",
34104 ridpointers[keyword]);
34105 return;
34107 else if (decl_specs->storage_class != sc_none)
34109 if (decl_specs->conflicting_specifiers_p)
34110 return;
34111 gcc_rich_location richloc (token->location);
34112 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
34113 if (decl_specs->storage_class == storage_class)
34114 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
34115 else
34116 error_at (&richloc,
34117 "%qD specifier conflicts with %qs",
34118 ridpointers[keyword],
34119 cp_storage_class_name[decl_specs->storage_class]);
34120 decl_specs->conflicting_specifiers_p = true;
34121 return;
34124 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
34125 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
34126 && decl_specs->gnu_thread_keyword_p)
34128 pedwarn (decl_specs->locations[ds_thread], 0,
34129 "%<__thread%> before %qD", ridpointers[keyword]);
34132 decl_specs->storage_class = storage_class;
34133 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
34135 /* A storage class specifier cannot be applied alongside a typedef
34136 specifier. If there is a typedef specifier present then set
34137 conflicting_specifiers_p which will trigger an error later
34138 on in grokdeclarator. */
34139 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34140 && !decl_specs->conflicting_specifiers_p)
34142 gcc_rich_location richloc (token->location);
34143 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
34144 error_at (&richloc,
34145 "%qD specifier conflicts with %<typedef%>",
34146 ridpointers[keyword]);
34147 decl_specs->conflicting_specifiers_p = true;
34151 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
34152 is true, the type is a class or enum definition. */
34154 static void
34155 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
34156 tree type_spec,
34157 cp_token *token,
34158 bool type_definition_p)
34160 decl_specs->any_specifiers_p = true;
34162 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
34163 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
34164 this is what happened. In system headers, we ignore these
34165 declarations so that G++ can work with system headers that are not
34166 C++-safe. */
34167 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34168 && !type_definition_p
34169 && TYPE_P (type_spec)
34170 && (type_spec == boolean_type_node
34171 || type_spec == char8_type_node
34172 || type_spec == char16_type_node
34173 || type_spec == char32_type_node
34174 || extended_float_type_p (type_spec)
34175 || type_spec == wchar_type_node)
34176 && (decl_specs->type
34177 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
34178 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
34179 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
34180 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
34182 decl_specs->redefined_builtin_type = type_spec;
34183 set_and_check_decl_spec_loc (decl_specs,
34184 ds_redefined_builtin_type_spec,
34185 token);
34186 if (!decl_specs->type)
34188 decl_specs->type = type_spec;
34189 decl_specs->type_definition_p = false;
34190 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
34193 else if (decl_specs->type)
34194 decl_specs->multiple_types_p = true;
34195 else
34197 decl_specs->type = type_spec;
34198 decl_specs->type_definition_p = type_definition_p;
34199 decl_specs->redefined_builtin_type = NULL_TREE;
34200 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
34204 /* True iff TOKEN is the GNU keyword __thread. */
34206 static bool
34207 token_is__thread (cp_token *token)
34209 gcc_assert (token->keyword == RID_THREAD);
34210 return id_equal (token->u.value, "__thread");
34213 /* Set the location for a declarator specifier and check if it is
34214 duplicated.
34216 DECL_SPECS is the sequence of declarator specifiers onto which to
34217 set the location.
34219 DS is the single declarator specifier to set which location is to
34220 be set onto the existing sequence of declarators.
34222 LOCATION is the location for the declarator specifier to
34223 consider. */
34225 static void
34226 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
34227 cp_decl_spec ds, cp_token *token)
34229 gcc_assert (ds < ds_last);
34231 if (decl_specs == NULL)
34232 return;
34234 location_t location = token->location;
34236 if (decl_specs->locations[ds] == 0)
34238 decl_specs->locations[ds] = location;
34239 if (ds == ds_thread)
34240 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
34242 else
34244 if (ds == ds_long)
34246 if (decl_specs->locations[ds_long_long] != 0)
34247 error_at (location,
34248 "%<long long long%> is too long for GCC");
34249 else
34251 decl_specs->locations[ds_long_long] = location;
34252 pedwarn_cxx98 (location,
34253 OPT_Wlong_long,
34254 "ISO C++ 1998 does not support %<long long%>");
34257 else if (ds == ds_thread)
34259 bool gnu = token_is__thread (token);
34260 gcc_rich_location richloc (location);
34261 if (gnu != decl_specs->gnu_thread_keyword_p)
34263 richloc.add_range (decl_specs->locations[ds_thread]);
34264 error_at (&richloc,
34265 "both %<__thread%> and %<thread_local%> specified");
34267 else
34269 richloc.add_fixit_remove ();
34270 error_at (&richloc, "duplicate %qD", token->u.value);
34273 else
34275 /* These correspond to cp-tree.h:cp_decl_spec,
34276 changes here should also be reflected there. */
34277 static const char *const decl_spec_names[] = {
34278 "signed",
34279 "unsigned",
34280 "short",
34281 "long",
34282 "const",
34283 "volatile",
34284 "restrict",
34285 "inline",
34286 "virtual",
34287 "explicit",
34288 "friend",
34289 "typedef",
34290 "using",
34291 "constexpr",
34292 "__complex",
34293 "constinit",
34294 "consteval",
34295 "this"
34297 gcc_rich_location richloc (location);
34298 richloc.add_fixit_remove ();
34299 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
34304 /* Return true iff the declarator specifier DS is present in the
34305 sequence of declarator specifiers DECL_SPECS. */
34307 bool
34308 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
34309 cp_decl_spec ds)
34311 gcc_assert (ds < ds_last);
34313 if (decl_specs == NULL)
34314 return false;
34316 return decl_specs->locations[ds] != 0;
34319 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
34320 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
34322 static bool
34323 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
34325 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
34328 /* Issue an error message indicating that TOKEN_DESC was expected.
34329 If KEYWORD is true, it indicated this function is called by
34330 cp_parser_require_keword and the required token can only be
34331 a indicated keyword.
34333 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34334 within any error as the location of an "opening" token matching
34335 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34336 RT_CLOSE_PAREN). */
34338 static void
34339 cp_parser_required_error (cp_parser *parser,
34340 required_token token_desc,
34341 bool keyword,
34342 location_t matching_location)
34344 if (cp_parser_simulate_error (parser))
34345 return;
34347 const char *gmsgid = NULL;
34348 switch (token_desc)
34350 case RT_NEW:
34351 gmsgid = G_("expected %<new%>");
34352 break;
34353 case RT_DELETE:
34354 gmsgid = G_("expected %<delete%>");
34355 break;
34356 case RT_RETURN:
34357 gmsgid = G_("expected %<return%>");
34358 break;
34359 case RT_WHILE:
34360 gmsgid = G_("expected %<while%>");
34361 break;
34362 case RT_EXTERN:
34363 gmsgid = G_("expected %<extern%>");
34364 break;
34365 case RT_STATIC_ASSERT:
34366 gmsgid = G_("expected %<static_assert%>");
34367 break;
34368 case RT_DECLTYPE:
34369 gmsgid = G_("expected %<decltype%>");
34370 break;
34371 case RT_OPERATOR:
34372 gmsgid = G_("expected %<operator%>");
34373 break;
34374 case RT_CLASS:
34375 gmsgid = G_("expected %<class%>");
34376 break;
34377 case RT_TEMPLATE:
34378 gmsgid = G_("expected %<template%>");
34379 break;
34380 case RT_NAMESPACE:
34381 gmsgid = G_("expected %<namespace%>");
34382 break;
34383 case RT_USING:
34384 gmsgid = G_("expected %<using%>");
34385 break;
34386 case RT_ASM:
34387 gmsgid = G_("expected %<asm%>");
34388 break;
34389 case RT_TRY:
34390 gmsgid = G_("expected %<try%>");
34391 break;
34392 case RT_CATCH:
34393 gmsgid = G_("expected %<catch%>");
34394 break;
34395 case RT_THROW:
34396 gmsgid = G_("expected %<throw%>");
34397 break;
34398 case RT_AUTO:
34399 gmsgid = G_("expected %<auto%>");
34400 break;
34401 case RT_LABEL:
34402 gmsgid = G_("expected %<__label__%>");
34403 break;
34404 case RT_AT_TRY:
34405 gmsgid = G_("expected %<@try%>");
34406 break;
34407 case RT_AT_SYNCHRONIZED:
34408 gmsgid = G_("expected %<@synchronized%>");
34409 break;
34410 case RT_AT_THROW:
34411 gmsgid = G_("expected %<@throw%>");
34412 break;
34413 case RT_TRANSACTION_ATOMIC:
34414 gmsgid = G_("expected %<__transaction_atomic%>");
34415 break;
34416 case RT_TRANSACTION_RELAXED:
34417 gmsgid = G_("expected %<__transaction_relaxed%>");
34418 break;
34419 case RT_CO_YIELD:
34420 gmsgid = G_("expected %<co_yield%>");
34421 break;
34422 default:
34423 break;
34426 if (!gmsgid && !keyword)
34428 switch (token_desc)
34430 case RT_SEMICOLON:
34431 gmsgid = G_("expected %<;%>");
34432 break;
34433 case RT_OPEN_PAREN:
34434 gmsgid = G_("expected %<(%>");
34435 break;
34436 case RT_CLOSE_BRACE:
34437 gmsgid = G_("expected %<}%>");
34438 break;
34439 case RT_OPEN_BRACE:
34440 gmsgid = G_("expected %<{%>");
34441 break;
34442 case RT_CLOSE_SQUARE:
34443 gmsgid = G_("expected %<]%>");
34444 break;
34445 case RT_OPEN_SQUARE:
34446 gmsgid = G_("expected %<[%>");
34447 break;
34448 case RT_COMMA:
34449 gmsgid = G_("expected %<,%>");
34450 break;
34451 case RT_SCOPE:
34452 gmsgid = G_("expected %<::%>");
34453 break;
34454 case RT_LESS:
34455 gmsgid = G_("expected %<<%>");
34456 break;
34457 case RT_GREATER:
34458 gmsgid = G_("expected %<>%>");
34459 break;
34460 case RT_EQ:
34461 gmsgid = G_("expected %<=%>");
34462 break;
34463 case RT_ELLIPSIS:
34464 gmsgid = G_("expected %<...%>");
34465 break;
34466 case RT_MULT:
34467 gmsgid = G_("expected %<*%>");
34468 break;
34469 case RT_COMPL:
34470 gmsgid = G_("expected %<~%>");
34471 break;
34472 case RT_COLON:
34473 gmsgid = G_("expected %<:%>");
34474 break;
34475 case RT_COLON_SCOPE:
34476 gmsgid = G_("expected %<:%> or %<::%>");
34477 break;
34478 case RT_CLOSE_PAREN:
34479 gmsgid = G_("expected %<)%>");
34480 break;
34481 case RT_COMMA_CLOSE_PAREN:
34482 gmsgid = G_("expected %<,%> or %<)%>");
34483 break;
34484 case RT_PRAGMA_EOL:
34485 gmsgid = G_("expected end of line");
34486 break;
34487 case RT_NAME:
34488 gmsgid = G_("expected identifier");
34489 break;
34490 case RT_SELECT:
34491 gmsgid = G_("expected selection-statement");
34492 break;
34493 case RT_ITERATION:
34494 gmsgid = G_("expected iteration-statement");
34495 break;
34496 case RT_JUMP:
34497 gmsgid = G_("expected jump-statement");
34498 break;
34499 case RT_CLASS_KEY:
34500 gmsgid = G_("expected class-key");
34501 break;
34502 case RT_CLASS_TYPENAME_TEMPLATE:
34503 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34504 break;
34505 default:
34506 gcc_unreachable ();
34510 if (gmsgid)
34511 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34515 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34516 issue an error message indicating that TOKEN_DESC was expected.
34518 Returns the token consumed, if the token had the appropriate type.
34519 Otherwise, returns NULL.
34521 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34522 within any error as the location of an "opening" token matching
34523 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34524 RT_CLOSE_PAREN). */
34526 static cp_token *
34527 cp_parser_require (cp_parser* parser,
34528 enum cpp_ttype type,
34529 required_token token_desc,
34530 location_t matching_location)
34532 if (cp_lexer_next_token_is (parser->lexer, type))
34533 return cp_lexer_consume_token (parser->lexer);
34534 else
34536 /* Output the MESSAGE -- unless we're parsing tentatively. */
34537 if (!cp_parser_simulate_error (parser))
34538 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34539 matching_location);
34540 return NULL;
34544 /* Skip an entire parameter list from start to finish. The next token must
34545 be the initial "<" of the parameter list. Returns true on success and
34546 false otherwise. */
34548 static bool
34549 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34551 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34552 requires it. */
34553 cp_lexer_consume_token (parser->lexer);
34554 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34557 /* Ensure we are at the end of a template parameter list. If we are, return.
34558 If we are not, something has gone wrong, in which case issue an error and
34559 skip to end of the parameter list. */
34561 static void
34562 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34564 /* Are we ready, yet? If not, issue error message. */
34565 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34566 return;
34568 cp_parser_skip_to_end_of_template_parameter_list (parser);
34571 /* You should only call this function from inside a template parameter list
34572 (i.e. the current token should at least be the initial "<" of the
34573 parameter list). If you are skipping the entire list, it may be better to
34574 use cp_parser_skip_entire_template_parameter_list.
34576 Tokens are skipped until the final ">" is found, or if we see
34577 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34579 Returns true if we successfully reached the end, and false if
34580 something unexpected happened (e.g. end of file). */
34582 static bool
34583 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34585 /* Current level of '< ... >'. */
34586 unsigned level = 0;
34587 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34588 unsigned nesting_depth = 0;
34590 /* Skip tokens until the desired token is found. */
34591 while (true)
34593 /* Peek at the next token. */
34594 switch (cp_lexer_peek_token (parser->lexer)->type)
34596 case CPP_LESS:
34597 if (!nesting_depth)
34598 ++level;
34599 break;
34601 case CPP_RSHIFT:
34602 if (cxx_dialect == cxx98)
34603 /* C++0x views the `>>' operator as two `>' tokens, but
34604 C++98 does not. */
34605 break;
34606 else if (!nesting_depth && level-- == 0)
34608 /* We've hit a `>>' where the first `>' closes the
34609 template argument list, and the second `>' is
34610 spurious. Just consume the `>>' and stop; we've
34611 already produced at least one error. */
34612 cp_lexer_consume_token (parser->lexer);
34613 return false;
34615 /* Fall through for C++0x, so we handle the second `>' in
34616 the `>>'. */
34617 gcc_fallthrough ();
34619 case CPP_GREATER:
34620 if (!nesting_depth && level-- == 0)
34622 /* We've reached the token we want, consume it and stop. */
34623 cp_lexer_consume_token (parser->lexer);
34624 return true;
34626 break;
34628 case CPP_OPEN_PAREN:
34629 case CPP_OPEN_SQUARE:
34630 ++nesting_depth;
34631 break;
34633 case CPP_CLOSE_PAREN:
34634 case CPP_CLOSE_SQUARE:
34635 if (nesting_depth-- == 0)
34636 return false;
34637 break;
34639 case CPP_EOF:
34640 case CPP_PRAGMA_EOL:
34641 case CPP_SEMICOLON:
34642 case CPP_OPEN_BRACE:
34643 case CPP_CLOSE_BRACE:
34644 /* The '>' was probably forgotten, don't look further. */
34645 return false;
34647 default:
34648 break;
34651 /* Consume this token. */
34652 cp_lexer_consume_token (parser->lexer);
34656 /* If the next token is the indicated keyword, consume it. Otherwise,
34657 issue an error message indicating that TOKEN_DESC was expected.
34659 Returns the token consumed, if the token had the appropriate type.
34660 Otherwise, returns NULL. */
34662 static cp_token *
34663 cp_parser_require_keyword (cp_parser* parser,
34664 enum rid keyword,
34665 required_token token_desc)
34667 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34669 if (token && token->keyword != keyword)
34671 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34672 UNKNOWN_LOCATION);
34673 return NULL;
34676 return token;
34679 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34680 function-definition. */
34682 static bool
34683 cp_parser_token_starts_function_definition_p (cp_token* token)
34685 return (/* An ordinary function-body begins with an `{'. */
34686 token->type == CPP_OPEN_BRACE
34687 /* A ctor-initializer begins with a `:'. */
34688 || token->type == CPP_COLON
34689 /* A function-try-block begins with `try'. */
34690 || token->keyword == RID_TRY
34691 /* A function-transaction-block begins with `__transaction_atomic'
34692 or `__transaction_relaxed'. */
34693 || token->keyword == RID_TRANSACTION_ATOMIC
34694 || token->keyword == RID_TRANSACTION_RELAXED
34695 /* The named return value extension begins with `return'. */
34696 || token->keyword == RID_RETURN);
34699 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34700 definition. */
34702 static bool
34703 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34705 cp_token *token;
34707 token = cp_lexer_peek_token (parser->lexer);
34708 return (token->type == CPP_OPEN_BRACE
34709 || (token->type == CPP_COLON
34710 && !parser->colon_doesnt_start_class_def_p));
34713 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34714 C++0x) ending a template-argument. */
34716 static bool
34717 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34719 cp_token *token;
34721 token = cp_lexer_peek_token (parser->lexer);
34722 return (token->type == CPP_COMMA
34723 || token->type == CPP_GREATER
34724 || token->type == CPP_ELLIPSIS
34725 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34726 /* For better diagnostics, treat >>= like that too, that
34727 shouldn't appear non-nested in template arguments. */
34728 || token->type == CPP_RSHIFT_EQ);
34731 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34732 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34734 static bool
34735 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34736 size_t n)
34738 cp_token *token;
34740 token = cp_lexer_peek_nth_token (parser->lexer, n);
34741 if (token->type == CPP_LESS)
34742 return true;
34743 /* Check for the sequence `<::' in the original code. It would be lexed as
34744 `[:', where `[' is a digraph, and there is no whitespace before
34745 `:'. */
34746 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34748 cp_token *token2;
34749 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34750 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34751 return true;
34753 return false;
34756 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34757 or none_type otherwise. */
34759 static enum tag_types
34760 cp_parser_token_is_class_key (cp_token* token)
34762 switch (token->keyword)
34764 case RID_CLASS:
34765 return class_type;
34766 case RID_STRUCT:
34767 return record_type;
34768 case RID_UNION:
34769 return union_type;
34771 default:
34772 return none_type;
34776 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34777 or none_type otherwise or if the token is null. */
34779 static enum tag_types
34780 cp_parser_token_is_type_parameter_key (cp_token* token)
34782 if (!token)
34783 return none_type;
34785 switch (token->keyword)
34787 case RID_CLASS:
34788 return class_type;
34789 case RID_TYPENAME:
34790 return typename_type;
34792 default:
34793 return none_type;
34797 /* Diagnose redundant enum-keys. */
34799 static void
34800 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34801 tree type, rid scoped_key)
34803 if (!warn_redundant_tags)
34804 return;
34806 tree type_decl = TYPE_MAIN_DECL (type);
34807 tree name = DECL_NAME (type_decl);
34808 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34809 push_deferring_access_checks (dk_no_check);
34810 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34811 pop_deferring_access_checks ();
34813 /* The enum-key is redundant for uses of the TYPE that are not
34814 declarations and for which name lookup returns just the type
34815 itself. */
34816 if (decl != type_decl)
34817 return;
34819 if (scoped_key != RID_CLASS
34820 && scoped_key != RID_STRUCT
34821 && current_lang_name != lang_name_cplusplus
34822 && current_namespace == global_namespace)
34824 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34825 enum tag in shared C/C++ code in files (such as headers) included
34826 in the main source file. */
34827 const line_map_ordinary *map = NULL;
34828 linemap_resolve_location (line_table, key_loc,
34829 LRK_MACRO_DEFINITION_LOCATION,
34830 &map);
34831 if (!MAIN_FILE_P (map))
34832 return;
34835 gcc_rich_location richloc (key_loc);
34836 richloc.add_fixit_remove (key_loc);
34837 warning_at (&richloc, OPT_Wredundant_tags,
34838 "redundant enum-key %<enum%s%> in reference to %q#T",
34839 (scoped_key == RID_CLASS ? " class"
34840 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34843 /* Describes the set of declarations of a struct, class, or class template
34844 or its specializations. Used for -Wmismatched-tags. */
34846 class class_decl_loc_t
34848 public:
34850 class_decl_loc_t ()
34851 : locvec (), idxdef (), def_class_key ()
34853 locvec.create (4);
34856 /* Constructs an object for a single declaration of a class with
34857 CLASS_KEY at the current location in the current function (or
34858 at another scope). KEY_REDUNDANT is true if the class-key may
34859 be omitted in the current context without an ambiguity with
34860 another symbol with the same name.
34861 DEF_P is true for a class declaration that is a definition.
34862 CURLOC is the associated location. */
34863 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34864 location_t curloc = input_location)
34865 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34867 locvec.create (4);
34868 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34869 key_redundant);
34870 locvec.quick_push (ckl);
34873 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34874 isn't safely copyable and assignable and doesn't release storage
34875 on its own. */
34876 class_decl_loc_t (const class_decl_loc_t &rhs)
34877 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34878 def_class_key (rhs.def_class_key)
34881 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34883 if (this == &rhs)
34884 return *this;
34885 locvec.release ();
34886 locvec = rhs.locvec.copy ();
34887 idxdef = rhs.idxdef;
34888 def_class_key = rhs.def_class_key;
34889 return *this;
34892 ~class_decl_loc_t ()
34894 locvec.release ();
34897 /* Issues -Wmismatched-tags for a single class. */
34898 void diag_mismatched_tags (tree);
34900 /* Issues -Wmismatched-tags for all classes. */
34901 static void diag_mismatched_tags ();
34903 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34904 redundant tags (if -Wredundant-tags is enabled). */
34905 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34907 /* Either adds this decl to the collection of class decls
34908 or diagnoses it, whichever is appropriate. */
34909 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34911 private:
34913 tree function (unsigned i) const
34915 return locvec[i].func;
34918 location_t location (unsigned i) const
34920 return locvec[i].loc;
34923 bool key_redundant (unsigned i) const
34925 return locvec[i].key_redundant;
34928 tag_types class_key (unsigned i) const
34930 return locvec[i].class_key;
34933 /* True if a definition for the class has been seen. */
34934 bool def_p () const
34936 return idxdef < locvec.length ();
34939 /* The location of a single mention of a class type with the given
34940 class-key. */
34941 struct class_key_loc_t
34943 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34944 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34947 /* The function the type is mentioned in. */
34948 tree func;
34949 /* The exact location. */
34950 location_t loc;
34951 /* The class-key used in the mention of the type. */
34952 tag_types class_key;
34953 /* True when the class-key could be omitted at this location
34954 without an ambiguity with another symbol of the same name. */
34955 bool key_redundant;
34957 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34958 vec <class_key_loc_t> locvec;
34959 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34960 unsigned idxdef;
34961 /* The class-key the class was last declared with or none_type when
34962 it has been declared with a mismatched key. */
34963 tag_types def_class_key;
34965 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34966 description above. */
34967 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34968 static class_to_loc_map_t class2loc;
34971 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34973 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34974 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34975 is set for a declaration of class TYPE and clear for a reference to
34976 it that is not a declaration of it. */
34978 static void
34979 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34980 tag_types class_key, tree type, bool def_p,
34981 bool decl_p)
34983 if (type == error_mark_node)
34984 return;
34986 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34987 if (seen_as_union != (class_key == union_type))
34989 if (permerror (input_location, "%qs tag used in naming %q#T",
34990 class_key == union_type ? "union"
34991 : class_key == record_type ? "struct" : "class",
34992 type))
34993 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34994 "%q#T was previously declared here", type);
34995 return;
34998 if (!warn_mismatched_tags && !warn_redundant_tags)
34999 return;
35001 /* Only consider the true class-keys below and ignore typename_type,
35002 etc. that are not C++ class-keys. */
35003 if (class_key != class_type
35004 && class_key != record_type
35005 && class_key != union_type)
35006 return;
35008 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
35011 /* Returns the template or specialization of one to which the RECORD_TYPE
35012 TYPE corresponds. */
35014 static tree
35015 specialization_of (tree type)
35017 tree ret = type;
35019 /* Determine the template or its partial specialization to which TYPE
35020 corresponds. */
35021 if (tree ti = most_specialized_partial_spec (type, tf_none))
35022 if (ti != error_mark_node)
35023 ret = TREE_TYPE (TI_TEMPLATE (ti));
35025 if (ret == type)
35026 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
35028 return TYPE_MAIN_DECL (ret);
35032 /* Adds the class TYPE to the collection of class decls and diagnoses
35033 redundant tags (if -Wredundant-tags is enabled).
35034 DEF_P is expected to be set for a definition of class TYPE. DECL_P
35035 is set for a (likely, based on syntactic context) declaration of class
35036 TYPE and clear for a reference to it that is not a declaration of it. */
35038 void
35039 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
35040 tag_types class_key, tree type, bool def_p, bool decl_p)
35042 tree type_decl = TYPE_MAIN_DECL (type);
35043 tree name = DECL_NAME (type_decl);
35044 /* Look up the NAME to see if it unambiguously refers to the TYPE
35045 and set KEY_REDUNDANT if so. */
35046 push_deferring_access_checks (dk_no_check);
35047 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
35048 pop_deferring_access_checks ();
35050 /* The class-key is redundant for uses of the CLASS_TYPE that are
35051 neither definitions of it nor declarations, and for which name
35052 lookup returns just the type itself. */
35053 bool key_redundant = (!def_p && !decl_p
35054 && (decl == type_decl
35055 || TREE_CODE (decl) == TEMPLATE_DECL
35056 || (CLASS_TYPE_P (type)
35057 && TYPE_BEING_DEFINED (type))));
35059 if (key_redundant
35060 && class_key != class_type
35061 && current_lang_name != lang_name_cplusplus
35062 && current_namespace == global_namespace)
35064 /* Avoid issuing the diagnostic for apparently redundant struct
35065 and union class-keys in shared C/C++ code in files (such as
35066 headers) included in the main source file. */
35067 const line_map_ordinary *map = NULL;
35068 linemap_resolve_location (line_table, key_loc,
35069 LRK_MACRO_DEFINITION_LOCATION,
35070 &map);
35071 if (!MAIN_FILE_P (map))
35072 key_redundant = false;
35075 /* Set if a declaration of TYPE has previously been seen or if it must
35076 exist in a precompiled header. */
35077 bool exist;
35078 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
35079 if (!exist)
35081 tree type = TREE_TYPE (type_decl);
35082 if (def_p || !COMPLETE_TYPE_P (type))
35084 /* TYPE_DECL is the first declaration or definition of the type
35085 (outside precompiled headers -- see below). Just create
35086 a new entry for it and return unless it's a declaration
35087 involving a template that may need to be diagnosed by
35088 -Wredundant-tags. */
35089 *rdl = class_decl_loc_t (class_key, false, def_p);
35090 if (TREE_CODE (decl) != TEMPLATE_DECL)
35091 return;
35093 else
35095 /* TYPE was previously defined in some unknown precompiled header.
35096 Simply add a record of its definition at an unknown location and
35097 proceed below to add a reference to it at the current location.
35098 (Declarations in precompiled headers that are not definitions
35099 are ignored.) */
35100 tag_types def_key
35101 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
35102 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
35103 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
35104 exist = true;
35108 /* A prior declaration of TYPE_DECL has been seen. */
35110 if (key_redundant)
35112 gcc_rich_location richloc (key_loc);
35113 richloc.add_fixit_remove (key_loc);
35114 warning_at (&richloc, OPT_Wredundant_tags,
35115 "redundant class-key %qs in reference to %q#T",
35116 class_key == union_type ? "union"
35117 : class_key == record_type ? "struct" : "class",
35118 type);
35121 if (!exist)
35122 /* Do nothing if this is the first declaration of the type. */
35123 return;
35125 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
35126 /* Do nothing if the class-key in this declaration matches
35127 the definition. */
35128 return;
35130 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
35131 def_p);
35134 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
35135 of class decls or diagnoses it, whichever is appropriate. */
35137 void
35138 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
35139 tag_types class_key,
35140 bool redundant,
35141 bool def_p)
35143 /* Reset the CLASS_KEY associated with this type on mismatch.
35144 This is an optimization that lets the diagnostic code skip
35145 over classes that use the same class-key in all declarations. */
35146 if (def_class_key != class_key)
35147 def_class_key = none_type;
35149 /* Set IDXDEF to the index of the vector corresponding to
35150 the definition. */
35151 if (def_p)
35152 idxdef = locvec.length ();
35154 /* Append a record of this declaration to the vector. */
35155 class_key_loc_t ckl (current_function_decl, input_location, class_key,
35156 redundant);
35157 locvec.safe_push (ckl);
35159 if (idxdef == UINT_MAX)
35160 return;
35162 /* As a space optimization diagnose declarations of a class
35163 whose definition has been seen and purge the LOCVEC of
35164 all entries except the definition. */
35165 diag_mismatched_tags (type_decl);
35166 if (idxdef)
35168 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
35169 locvec.release ();
35170 locvec.reserve (2);
35171 locvec.safe_push (ent);
35172 idxdef = 0;
35174 else
35175 /* Pop the entry pushed above for this declaration. */
35176 locvec.pop ();
35179 /* Issues -Wmismatched-tags for a single class. */
35181 void
35182 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
35184 if (!warn_mismatched_tags)
35185 return;
35187 /* Number of uses of the class. */
35188 const unsigned ndecls = locvec.length ();
35190 /* The class (or template) declaration guiding the decisions about
35191 the diagnostic. For ordinary classes it's the same as THIS. For
35192 uses of instantiations of templates other than their declarations
35193 it points to the record for the declaration of the corresponding
35194 primary template or partial specialization. */
35195 class_decl_loc_t *cdlguide = this;
35197 tree type = TREE_TYPE (type_decl);
35198 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
35200 /* For implicit instantiations of a primary template look up
35201 the primary or partial specialization and use it as
35202 the expected class-key rather than using the class-key of
35203 the first reference to the instantiation. The primary must
35204 be (and inevitably is) at index zero. */
35205 tree spec = specialization_of (type);
35206 cdlguide = class2loc.get (spec);
35207 /* It's possible that we didn't find SPEC. Consider:
35209 template<typename T> struct A {
35210 template<typename U> struct W { };
35212 struct A<int>::W<int> w; // #1
35214 where while parsing A and #1 we've stashed
35215 A<T>
35216 A<T>::W<U>
35217 A<int>::W<int>
35218 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
35219 will yield A<int>::W<U> which may be in CLASS2LOC if we had
35220 an A<int> class specialization, but otherwise won't be in it.
35221 So try to look up A<T>::W<U>. */
35222 if (!cdlguide)
35224 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
35225 cdlguide = class2loc.get (spec);
35227 /* Now we really should have found something. */
35228 gcc_assert (cdlguide != NULL);
35230 /* Skip declarations that consistently use the same class-key. */
35231 else if (def_class_key != none_type)
35232 return;
35234 /* Set if a definition for the class has been seen. */
35235 const bool def_p = cdlguide->def_p ();
35237 /* The index of the declaration whose class-key this declaration
35238 is expected to match. It's either the class-key of the class
35239 definition if one exists or the first declaration otherwise. */
35240 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
35242 /* The class-key the class is expected to be declared with: it's
35243 either the key used in its definition or the first declaration
35244 if no definition has been provided.
35245 For implicit instantiations of a primary template it's
35246 the class-key used to declare the primary with. The primary
35247 must be at index zero. */
35248 const tag_types xpect_key = cdlguide->class_key (idxguide);
35250 unsigned idx = 0;
35251 /* Advance IDX to the first declaration that either is not
35252 a definition or that doesn't match the first declaration
35253 if no definition is provided. */
35254 while (class_key (idx) == xpect_key)
35255 if (++idx == ndecls)
35256 return;
35258 /* Save the current function before changing it below. */
35259 tree save_func = current_function_decl;
35260 /* Set the function declaration to print in diagnostic context. */
35261 current_function_decl = function (idx);
35263 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
35264 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
35266 location_t loc = location (idx);
35267 bool key_redundant_p = key_redundant (idx);
35268 auto_diagnostic_group d;
35269 /* Issue a warning for the first mismatched declaration.
35270 Avoid using "%#qT" since the class-key for the same type will
35271 be the same regardless of which one was used in the declaraion. */
35272 if (warning_at (loc, OPT_Wmismatched_tags,
35273 "%qT declared with a mismatched class-key %qs",
35274 type_decl, xmatchkstr))
35276 /* Suggest how to avoid the warning for each instance since
35277 the guidance may be different depending on context. */
35278 inform (loc,
35279 (key_redundant_p
35280 ? G_("remove the class-key or replace it with %qs")
35281 : G_("replace the class-key with %qs")),
35282 xpectkstr);
35284 /* Also point to the first declaration or definition that guided
35285 the decision to issue the warning above. */
35286 inform (cdlguide->location (idxguide),
35287 (def_p
35288 ? G_("%qT defined as %qs here")
35289 : G_("%qT first declared as %qs here")),
35290 type_decl, xpectkstr);
35293 /* Issue warnings for the remaining inconsistent declarations. */
35294 for (unsigned i = idx + 1; i != ndecls; ++i)
35296 tag_types clskey = class_key (i);
35297 /* Skip over the declarations that match either the definition
35298 if one was provided or the first declaration. */
35299 if (clskey == xpect_key)
35300 continue;
35302 loc = location (i);
35303 key_redundant_p = key_redundant (i);
35304 /* Set the function declaration to print in diagnostic context. */
35305 current_function_decl = function (i);
35306 if (warning_at (loc, OPT_Wmismatched_tags,
35307 "%qT declared with a mismatched class-key %qs",
35308 type_decl, xmatchkstr))
35309 /* Suggest how to avoid the warning for each instance since
35310 the guidance may be different depending on context. */
35311 inform (loc,
35312 (key_redundant_p
35313 ? G_("remove the class-key or replace it with %qs")
35314 : G_("replace the class-key with %qs")),
35315 xpectkstr);
35318 /* Restore the current function in case it was replaced above. */
35319 current_function_decl = save_func;
35322 /* Issues -Wmismatched-tags for all classes. Called at the end
35323 of processing a translation unit, after declarations of all class
35324 types and their uses have been recorded. */
35326 void
35327 class_decl_loc_t::diag_mismatched_tags ()
35329 /* CLASS2LOC should be empty if both -Wmismatched-tags and
35330 -Wredundant-tags are disabled. */
35331 gcc_assert (warn_mismatched_tags
35332 || warn_redundant_tags
35333 || class2loc.is_empty ());
35335 /* Save the current function before changing on return. It should
35336 be null at this point. */
35337 temp_override<tree> cleanup (current_function_decl);
35339 if (warn_mismatched_tags)
35341 /* Iterate over the collected class/struct/template declarations. */
35342 typedef class_to_loc_map_t::iterator iter_t;
35343 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
35345 tree type_decl = (*it).first;
35346 class_decl_loc_t &recloc = (*it).second;
35347 recloc.diag_mismatched_tags (type_decl);
35351 class2loc.empty ();
35354 /* Issue an error message if DECL is redeclared with different
35355 access than its original declaration [class.access.spec/3].
35356 This applies to nested classes, nested class templates and
35357 enumerations [class.mem/1]. */
35359 static void
35360 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
35362 if (!decl
35363 || (!(CLASS_TYPE_P (TREE_TYPE (decl))
35364 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
35365 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
35366 return;
35368 if ((TREE_PRIVATE (decl)
35369 != (current_access_specifier == access_private_node))
35370 || (TREE_PROTECTED (decl)
35371 != (current_access_specifier == access_protected_node)))
35372 error_at (location, "%qD redeclared with different access", decl);
35375 /* Look for the `template' keyword, as a syntactic disambiguator.
35376 Return TRUE iff it is present, in which case it will be
35377 consumed. */
35379 static bool
35380 cp_parser_optional_template_keyword (cp_parser *parser)
35382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
35384 /* In C++98 the `template' keyword can only be used within templates;
35385 outside templates the parser can always figure out what is a
35386 template and what is not. In C++11, per the resolution of DR 468,
35387 `template' is allowed in cases where it is not strictly necessary. */
35388 if (!processing_template_decl
35389 && pedantic && cxx_dialect == cxx98)
35391 cp_token *token = cp_lexer_peek_token (parser->lexer);
35392 pedwarn (token->location, OPT_Wpedantic,
35393 "in C++98 %<template%> (as a disambiguator) is only "
35394 "allowed within templates");
35395 /* If this part of the token stream is rescanned, the same
35396 error message would be generated. So, we purge the token
35397 from the stream. */
35398 cp_lexer_purge_token (parser->lexer);
35399 return false;
35401 else
35403 /* Consume the `template' keyword. */
35404 cp_lexer_consume_token (parser->lexer);
35405 return true;
35408 return false;
35411 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
35412 set PARSER->SCOPE, and perform other related actions. */
35414 static void
35415 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
35417 struct tree_check *check_value;
35419 /* Get the stored value. */
35420 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
35421 /* Set the scope from the stored value. */
35422 parser->scope = saved_checks_value (check_value);
35423 parser->qualifying_scope = check_value->qualifying_scope;
35424 parser->object_scope = parser->context->object_type;
35425 parser->context->object_type = NULL_TREE;
35428 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35429 encounter the end of a block before what we were looking for. */
35431 static bool
35432 cp_parser_cache_group (cp_parser *parser,
35433 enum cpp_ttype end,
35434 unsigned depth)
35436 while (true)
35438 cp_token *token = cp_lexer_peek_token (parser->lexer);
35440 /* Abort a parenthesized expression if we encounter a semicolon. */
35441 if ((end == CPP_CLOSE_PAREN || depth == 0)
35442 && token->type == CPP_SEMICOLON)
35443 return true;
35444 /* If we've reached the end of the file, stop. */
35445 if (token->type == CPP_EOF
35446 || (end != CPP_PRAGMA_EOL
35447 && token->type == CPP_PRAGMA_EOL))
35448 return true;
35449 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35450 /* We've hit the end of an enclosing block, so there's been some
35451 kind of syntax error. */
35452 return true;
35454 /* Consume the token. */
35455 cp_lexer_consume_token (parser->lexer);
35456 /* See if it starts a new group. */
35457 if (token->type == CPP_OPEN_BRACE)
35459 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35460 /* In theory this should probably check end == '}', but
35461 cp_parser_save_member_function_body needs it to exit
35462 after either '}' or ')' when called with ')'. */
35463 if (depth == 0)
35464 return false;
35466 else if (token->type == CPP_OPEN_PAREN)
35468 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35469 if (depth == 0 && end == CPP_CLOSE_PAREN)
35470 return false;
35472 else if (token->type == CPP_PRAGMA)
35473 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35474 else if (token->type == end)
35475 return false;
35479 /* Like above, for caching a default argument or NSDMI. Both of these are
35480 terminated by a non-nested comma, but it can be unclear whether or not a
35481 comma is nested in a template argument list unless we do more parsing.
35482 In order to handle this ambiguity, when we encounter a ',' after a '<'
35483 we try to parse what follows as a parameter-declaration-list (in the
35484 case of a default argument) or a member-declarator (in the case of an
35485 NSDMI). If that succeeds, then we stop caching. */
35487 static tree
35488 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35490 unsigned depth = 0;
35491 int maybe_template_id = 0;
35492 cp_token *first_token;
35493 cp_token *token;
35494 tree default_argument;
35496 /* Add tokens until we have processed the entire default
35497 argument. We add the range [first_token, token). */
35498 first_token = cp_lexer_peek_token (parser->lexer);
35499 if (first_token->type == CPP_OPEN_BRACE)
35501 /* For list-initialization, this is straightforward. */
35502 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35503 token = cp_lexer_peek_token (parser->lexer);
35505 else while (true)
35507 bool done = false;
35509 /* Peek at the next token. */
35510 token = cp_lexer_peek_token (parser->lexer);
35511 /* What we do depends on what token we have. */
35512 switch (token->type)
35514 /* In valid code, a default argument must be
35515 immediately followed by a `,' `)', or `...'. */
35516 case CPP_COMMA:
35517 if (depth == 0 && maybe_template_id)
35519 /* If we've seen a '<', we might be in a
35520 template-argument-list. Until Core issue 325 is
35521 resolved, we don't know how this situation ought
35522 to be handled, so try to DTRT. We check whether
35523 what comes after the comma is a valid parameter
35524 declaration list. If it is, then the comma ends
35525 the default argument; otherwise the default
35526 argument continues. */
35527 bool error = false;
35528 cp_token *peek;
35530 /* Set ITALP so cp_parser_parameter_declaration_list
35531 doesn't decide to commit to this parse. */
35532 bool saved_italp = parser->in_template_argument_list_p;
35533 parser->in_template_argument_list_p = true;
35535 cp_parser_parse_tentatively (parser);
35537 if (nsdmi)
35539 /* Parse declarators until we reach a non-comma or
35540 somthing that cannot be an initializer.
35541 Just checking whether we're looking at a single
35542 declarator is insufficient. Consider:
35543 int var = tuple<T,U>::x;
35544 The template parameter 'U' looks exactly like a
35545 declarator. */
35548 int ctor_dtor_or_conv_p;
35549 cp_lexer_consume_token (parser->lexer);
35550 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35551 CP_PARSER_FLAGS_NONE,
35552 &ctor_dtor_or_conv_p,
35553 /*parenthesized_p=*/NULL,
35554 /*member_p=*/true,
35555 /*friend_p=*/false,
35556 /*static_p=*/false);
35557 peek = cp_lexer_peek_token (parser->lexer);
35558 if (cp_parser_error_occurred (parser))
35559 break;
35561 while (peek->type == CPP_COMMA);
35562 /* If we met an '=' or ';' then the original comma
35563 was the end of the NSDMI. Otherwise assume
35564 we're still in the NSDMI. */
35565 error = (peek->type != CPP_EQ
35566 && peek->type != CPP_SEMICOLON);
35568 else
35570 cp_lexer_consume_token (parser->lexer);
35571 begin_scope (sk_function_parms, NULL_TREE);
35572 tree t = cp_parser_parameter_declaration_list
35573 (parser, CP_PARSER_FLAGS_NONE,
35574 /*pending_decls*/nullptr);
35575 if (t == error_mark_node)
35576 error = true;
35577 pop_bindings_and_leave_scope ();
35579 if (!cp_parser_error_occurred (parser) && !error)
35580 done = true;
35581 cp_parser_abort_tentative_parse (parser);
35583 parser->in_template_argument_list_p = saved_italp;
35584 break;
35586 /* FALLTHRU */
35587 case CPP_CLOSE_PAREN:
35588 case CPP_ELLIPSIS:
35589 /* If we run into a non-nested `;', `}', or `]',
35590 then the code is invalid -- but the default
35591 argument is certainly over. */
35592 case CPP_SEMICOLON:
35593 case CPP_CLOSE_BRACE:
35594 case CPP_CLOSE_SQUARE:
35595 if (depth == 0
35596 /* Handle correctly int n = sizeof ... ( p ); */
35597 && token->type != CPP_ELLIPSIS)
35598 done = true;
35599 /* Update DEPTH, if necessary. */
35600 else if (token->type == CPP_CLOSE_PAREN
35601 || token->type == CPP_CLOSE_BRACE
35602 || token->type == CPP_CLOSE_SQUARE)
35603 --depth;
35604 break;
35606 case CPP_OPEN_PAREN:
35607 case CPP_OPEN_SQUARE:
35608 case CPP_OPEN_BRACE:
35609 ++depth;
35610 break;
35612 case CPP_LESS:
35613 if (depth == 0)
35614 /* This might be the comparison operator, or it might
35615 start a template argument list. */
35616 ++maybe_template_id;
35617 break;
35619 case CPP_RSHIFT:
35620 if (cxx_dialect == cxx98)
35621 break;
35622 /* Fall through for C++0x, which treats the `>>'
35623 operator like two `>' tokens in certain
35624 cases. */
35625 gcc_fallthrough ();
35627 case CPP_GREATER:
35628 if (depth == 0)
35630 /* This might be an operator, or it might close a
35631 template argument list. But if a previous '<'
35632 started a template argument list, this will have
35633 closed it, so we can't be in one anymore. */
35634 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35635 if (maybe_template_id < 0)
35636 maybe_template_id = 0;
35638 break;
35640 /* If we run out of tokens, issue an error message. */
35641 case CPP_EOF:
35642 case CPP_PRAGMA_EOL:
35643 error_at (token->location, "file ends in default argument");
35644 return error_mark_node;
35646 case CPP_NAME:
35647 case CPP_SCOPE:
35648 /* In these cases, we should look for template-ids.
35649 For example, if the default argument is
35650 `X<int, double>()', we need to do name lookup to
35651 figure out whether or not `X' is a template; if
35652 so, the `,' does not end the default argument.
35654 That is not yet done. */
35655 break;
35657 default:
35658 break;
35661 /* If we've reached the end, stop. */
35662 if (done)
35663 break;
35665 /* Add the token to the token block. */
35666 token = cp_lexer_consume_token (parser->lexer);
35669 /* Create a DEFERRED_PARSE to represent the unparsed default
35670 argument. */
35671 default_argument = make_node (DEFERRED_PARSE);
35672 DEFPARSE_TOKENS (default_argument)
35673 = cp_token_cache_new (first_token, token);
35674 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35676 return default_argument;
35679 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35681 location_t
35682 defparse_location (tree default_argument)
35684 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35685 location_t start = tokens->first->location;
35686 location_t end = tokens->last->location;
35687 return make_location (start, start, end);
35690 /* Begin parsing tentatively. We always save tokens while parsing
35691 tentatively so that if the tentative parsing fails we can restore the
35692 tokens. */
35694 static void
35695 cp_parser_parse_tentatively (cp_parser* parser)
35697 /* Enter a new parsing context. */
35698 parser->context = cp_parser_context_new (parser->context);
35699 /* Begin saving tokens. */
35700 cp_lexer_save_tokens (parser->lexer);
35701 /* In order to avoid repetitive access control error messages,
35702 access checks are queued up until we are no longer parsing
35703 tentatively. */
35704 push_deferring_access_checks (dk_deferred);
35707 /* Commit to the currently active tentative parse. */
35709 static void
35710 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35712 cp_parser_context *context;
35713 cp_lexer *lexer;
35715 /* Mark all of the levels as committed. */
35716 lexer = parser->lexer;
35717 for (context = parser->context; context->next; context = context->next)
35719 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35720 break;
35721 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35722 while (!cp_lexer_saving_tokens (lexer))
35723 lexer = lexer->next;
35724 cp_lexer_commit_tokens (lexer);
35728 /* Commit to the topmost currently active tentative parse.
35730 Note that this function shouldn't be called when there are
35731 irreversible side-effects while in a tentative state. For
35732 example, we shouldn't create a permanent entry in the symbol
35733 table, or issue an error message that might not apply if the
35734 tentative parse is aborted. */
35736 static void
35737 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35739 cp_parser_context *context = parser->context;
35740 cp_lexer *lexer = parser->lexer;
35742 if (context)
35744 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35745 return;
35746 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35748 while (!cp_lexer_saving_tokens (lexer))
35749 lexer = lexer->next;
35750 cp_lexer_commit_tokens (lexer);
35754 /* Abort the currently active tentative parse. All consumed tokens
35755 will be rolled back, and no diagnostics will be issued. */
35757 static void
35758 cp_parser_abort_tentative_parse (cp_parser* parser)
35760 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35761 || errorcount > 0);
35762 cp_parser_simulate_error (parser);
35763 /* Now, pretend that we want to see if the construct was
35764 successfully parsed. */
35765 cp_parser_parse_definitely (parser);
35768 /* Stop parsing tentatively. If a parse error has occurred, restore the
35769 token stream. Otherwise, commit to the tokens we have consumed.
35770 Returns true if no error occurred; false otherwise. */
35772 static bool
35773 cp_parser_parse_definitely (cp_parser* parser)
35775 bool error_occurred;
35776 cp_parser_context *context;
35778 /* Remember whether or not an error occurred, since we are about to
35779 destroy that information. */
35780 error_occurred = cp_parser_error_occurred (parser);
35781 /* Remove the topmost context from the stack. */
35782 context = parser->context;
35783 parser->context = context->next;
35784 /* If no parse errors occurred, commit to the tentative parse. */
35785 if (!error_occurred)
35787 /* Commit to the tokens read tentatively, unless that was
35788 already done. */
35789 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35790 cp_lexer_commit_tokens (parser->lexer);
35792 pop_to_parent_deferring_access_checks ();
35794 /* Otherwise, if errors occurred, roll back our state so that things
35795 are just as they were before we began the tentative parse. */
35796 else
35798 cp_lexer_rollback_tokens (parser->lexer);
35799 pop_deferring_access_checks ();
35801 /* Add the context to the front of the free list. */
35802 context->next = cp_parser_context_free_list;
35803 cp_parser_context_free_list = context;
35805 return !error_occurred;
35808 /* Returns true if we are parsing tentatively and are not committed to
35809 this tentative parse. */
35811 static bool
35812 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35814 return (cp_parser_parsing_tentatively (parser)
35815 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35818 /* Returns nonzero iff an error has occurred during the most recent
35819 tentative parse. */
35821 static bool
35822 cp_parser_error_occurred (cp_parser* parser)
35824 return (cp_parser_parsing_tentatively (parser)
35825 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35828 /* Returns nonzero if GNU extensions are allowed. */
35830 static bool
35831 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35833 return parser->allow_gnu_extensions_p;
35836 /* Objective-C++ Productions */
35839 /* Parse an Objective-C expression, which feeds into a primary-expression
35840 above.
35842 objc-expression:
35843 objc-message-expression
35844 objc-string-literal
35845 objc-encode-expression
35846 objc-protocol-expression
35847 objc-selector-expression
35849 Returns a tree representation of the expression. */
35851 static cp_expr
35852 cp_parser_objc_expression (cp_parser* parser)
35854 /* Try to figure out what kind of declaration is present. */
35855 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35857 switch (kwd->type)
35859 case CPP_OPEN_SQUARE:
35860 return cp_parser_objc_message_expression (parser);
35862 case CPP_OBJC_STRING:
35863 kwd = cp_lexer_consume_token (parser->lexer);
35864 return objc_build_string_object (kwd->u.value);
35866 case CPP_KEYWORD:
35867 switch (kwd->keyword)
35869 case RID_AT_ENCODE:
35870 return cp_parser_objc_encode_expression (parser);
35872 case RID_AT_PROTOCOL:
35873 return cp_parser_objc_protocol_expression (parser);
35875 case RID_AT_SELECTOR:
35876 return cp_parser_objc_selector_expression (parser);
35878 default:
35879 break;
35881 /* FALLTHRU */
35882 default:
35883 error_at (kwd->location,
35884 "misplaced %<@%D%> Objective-C++ construct",
35885 kwd->u.value);
35886 cp_parser_skip_to_end_of_block_or_statement (parser);
35889 return error_mark_node;
35892 /* Parse an Objective-C message expression.
35894 objc-message-expression:
35895 [ objc-message-receiver objc-message-args ]
35897 Returns a representation of an Objective-C message. */
35899 static tree
35900 cp_parser_objc_message_expression (cp_parser* parser)
35902 tree receiver, messageargs;
35904 parser->objective_c_message_context_p = true;
35905 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35906 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35907 receiver = cp_parser_objc_message_receiver (parser);
35908 messageargs = cp_parser_objc_message_args (parser);
35909 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35910 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35912 tree result = objc_build_message_expr (receiver, messageargs);
35914 /* Construct a location e.g.
35915 [self func1:5]
35916 ^~~~~~~~~~~~~~
35917 ranging from the '[' to the ']', with the caret at the start. */
35918 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35919 protected_set_expr_location (result, combined_loc);
35921 parser->objective_c_message_context_p = false;
35922 return result;
35925 /* Parse an objc-message-receiver.
35927 objc-message-receiver:
35928 expression
35929 simple-type-specifier
35931 Returns a representation of the type or expression. */
35933 static tree
35934 cp_parser_objc_message_receiver (cp_parser* parser)
35936 tree rcv;
35938 /* An Objective-C message receiver may be either (1) a type
35939 or (2) an expression. */
35940 cp_parser_parse_tentatively (parser);
35941 rcv = cp_parser_expression (parser);
35943 /* If that worked out, fine. */
35944 if (cp_parser_parse_definitely (parser))
35945 return rcv;
35947 cp_parser_parse_tentatively (parser);
35948 rcv = cp_parser_simple_type_specifier (parser,
35949 /*decl_specs=*/NULL,
35950 CP_PARSER_FLAGS_NONE);
35952 if (cp_parser_parse_definitely (parser))
35953 return objc_get_class_reference (rcv);
35955 cp_parser_error (parser, "objective-c++ message receiver expected");
35956 return error_mark_node;
35959 /* Parse the arguments and selectors comprising an Objective-C message.
35961 objc-message-args:
35962 objc-selector
35963 objc-selector-args
35964 objc-selector-args , objc-comma-args
35966 objc-selector-args:
35967 objc-selector [opt] : assignment-expression
35968 objc-selector-args objc-selector [opt] : assignment-expression
35970 objc-comma-args:
35971 assignment-expression
35972 objc-comma-args , assignment-expression
35974 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35975 selector arguments and TREE_VALUE containing a list of comma
35976 arguments. */
35978 static tree
35979 cp_parser_objc_message_args (cp_parser* parser)
35981 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35982 bool maybe_unary_selector_p = true;
35983 cp_token *token = cp_lexer_peek_token (parser->lexer);
35985 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35987 tree selector = NULL_TREE, arg;
35989 if (token->type != CPP_COLON)
35990 selector = cp_parser_objc_selector (parser);
35992 /* Detect if we have a unary selector. */
35993 if (maybe_unary_selector_p
35994 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35995 return build_tree_list (selector, NULL_TREE);
35997 maybe_unary_selector_p = false;
35998 cp_parser_require (parser, CPP_COLON, RT_COLON);
35999 arg = cp_parser_assignment_expression (parser);
36001 sel_args
36002 = chainon (sel_args,
36003 build_tree_list (selector, arg));
36005 token = cp_lexer_peek_token (parser->lexer);
36008 /* Handle non-selector arguments, if any. */
36009 while (token->type == CPP_COMMA)
36011 tree arg;
36013 cp_lexer_consume_token (parser->lexer);
36014 arg = cp_parser_assignment_expression (parser);
36016 addl_args
36017 = chainon (addl_args,
36018 build_tree_list (NULL_TREE, arg));
36020 token = cp_lexer_peek_token (parser->lexer);
36023 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
36025 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
36026 return build_tree_list (error_mark_node, error_mark_node);
36029 return build_tree_list (sel_args, addl_args);
36032 /* Parse an Objective-C encode expression.
36034 objc-encode-expression:
36035 @encode objc-typename
36037 Returns an encoded representation of the type argument. */
36039 static cp_expr
36040 cp_parser_objc_encode_expression (cp_parser* parser)
36042 tree type;
36043 cp_token *token;
36044 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36046 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
36047 matching_parens parens;
36048 parens.require_open (parser);
36049 token = cp_lexer_peek_token (parser->lexer);
36050 type = complete_type (cp_parser_type_id (parser));
36051 parens.require_close (parser);
36053 if (!type)
36055 error_at (token->location,
36056 "%<@encode%> must specify a type as an argument");
36057 return error_mark_node;
36060 /* This happens if we find @encode(T) (where T is a template
36061 typename or something dependent on a template typename) when
36062 parsing a template. In that case, we can't compile it
36063 immediately, but we rather create an AT_ENCODE_EXPR which will
36064 need to be instantiated when the template is used.
36066 if (dependent_type_p (type))
36068 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
36069 TREE_READONLY (value) = 1;
36070 return value;
36074 /* Build a location of the form:
36075 @encode(int)
36076 ^~~~~~~~~~~~
36077 with caret==start at the @ token, finishing at the close paren. */
36078 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36080 return cp_expr (objc_build_encode_expr (type), combined_loc);
36083 /* Parse an Objective-C @defs expression. */
36085 static tree
36086 cp_parser_objc_defs_expression (cp_parser *parser)
36088 tree name;
36090 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
36091 matching_parens parens;
36092 parens.require_open (parser);
36093 name = cp_parser_identifier (parser);
36094 parens.require_close (parser);
36096 return objc_get_class_ivars (name);
36099 /* Parse an Objective-C protocol expression.
36101 objc-protocol-expression:
36102 @protocol ( identifier )
36104 Returns a representation of the protocol expression. */
36106 static tree
36107 cp_parser_objc_protocol_expression (cp_parser* parser)
36109 tree proto;
36110 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36112 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36113 matching_parens parens;
36114 parens.require_open (parser);
36115 proto = cp_parser_identifier (parser);
36116 parens.require_close (parser);
36118 /* Build a location of the form:
36119 @protocol(prot)
36120 ^~~~~~~~~~~~~~~
36121 with caret==start at the @ token, finishing at the close paren. */
36122 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36123 tree result = objc_build_protocol_expr (proto);
36124 protected_set_expr_location (result, combined_loc);
36125 return result;
36128 /* Parse an Objective-C selector expression.
36130 objc-selector-expression:
36131 @selector ( objc-method-signature )
36133 objc-method-signature:
36134 objc-selector
36135 objc-selector-seq
36137 objc-selector-seq:
36138 objc-selector :
36139 objc-selector-seq objc-selector :
36141 Returns a representation of the method selector. */
36143 static tree
36144 cp_parser_objc_selector_expression (cp_parser* parser)
36146 tree sel_seq = NULL_TREE;
36147 bool maybe_unary_selector_p = true;
36148 cp_token *token;
36149 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36151 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
36152 matching_parens parens;
36153 parens.require_open (parser);
36154 token = cp_lexer_peek_token (parser->lexer);
36156 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
36157 || token->type == CPP_SCOPE)
36159 tree selector = NULL_TREE;
36161 if (token->type != CPP_COLON
36162 || token->type == CPP_SCOPE)
36163 selector = cp_parser_objc_selector (parser);
36165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
36166 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
36168 /* Detect if we have a unary selector. */
36169 if (maybe_unary_selector_p)
36171 sel_seq = selector;
36172 goto finish_selector;
36174 else
36176 cp_parser_error (parser, "expected %<:%>");
36179 maybe_unary_selector_p = false;
36180 token = cp_lexer_consume_token (parser->lexer);
36182 if (token->type == CPP_SCOPE)
36184 sel_seq
36185 = chainon (sel_seq,
36186 build_tree_list (selector, NULL_TREE));
36187 sel_seq
36188 = chainon (sel_seq,
36189 build_tree_list (NULL_TREE, NULL_TREE));
36191 else
36192 sel_seq
36193 = chainon (sel_seq,
36194 build_tree_list (selector, NULL_TREE));
36196 token = cp_lexer_peek_token (parser->lexer);
36199 finish_selector:
36200 parens.require_close (parser);
36203 /* Build a location of the form:
36204 @selector(func)
36205 ^~~~~~~~~~~~~~~
36206 with caret==start at the @ token, finishing at the close paren. */
36207 location_t combined_loc = make_location (loc, loc, parser->lexer);
36208 tree result = objc_build_selector_expr (combined_loc, sel_seq);
36209 /* TODO: objc_build_selector_expr doesn't always honor the location. */
36210 protected_set_expr_location (result, combined_loc);
36211 return result;
36214 /* Parse a list of identifiers.
36216 objc-identifier-list:
36217 identifier
36218 objc-identifier-list , identifier
36220 Returns a TREE_LIST of identifier nodes. */
36222 static tree
36223 cp_parser_objc_identifier_list (cp_parser* parser)
36225 tree identifier;
36226 tree list;
36227 cp_token *sep;
36229 identifier = cp_parser_identifier (parser);
36230 if (identifier == error_mark_node)
36231 return error_mark_node;
36233 list = build_tree_list (NULL_TREE, identifier);
36234 sep = cp_lexer_peek_token (parser->lexer);
36236 while (sep->type == CPP_COMMA)
36238 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36239 identifier = cp_parser_identifier (parser);
36240 if (identifier == error_mark_node)
36241 return list;
36243 list = chainon (list, build_tree_list (NULL_TREE,
36244 identifier));
36245 sep = cp_lexer_peek_token (parser->lexer);
36248 return list;
36251 /* Parse an Objective-C alias declaration.
36253 objc-alias-declaration:
36254 @compatibility_alias identifier identifier ;
36256 This function registers the alias mapping with the Objective-C front end.
36257 It returns nothing. */
36259 static void
36260 cp_parser_objc_alias_declaration (cp_parser* parser)
36262 tree alias, orig;
36264 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
36265 alias = cp_parser_identifier (parser);
36266 orig = cp_parser_identifier (parser);
36267 objc_declare_alias (alias, orig);
36268 cp_parser_consume_semicolon_at_end_of_statement (parser);
36271 /* Parse an Objective-C class forward-declaration.
36273 objc-class-declaration:
36274 @class objc-identifier-list ;
36276 The function registers the forward declarations with the Objective-C
36277 front end. It returns nothing. */
36279 static void
36280 cp_parser_objc_class_declaration (cp_parser* parser)
36282 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
36283 while (true)
36285 tree id;
36287 id = cp_parser_identifier (parser);
36288 if (id == error_mark_node)
36289 break;
36291 objc_declare_class (id);
36293 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36294 cp_lexer_consume_token (parser->lexer);
36295 else
36296 break;
36298 cp_parser_consume_semicolon_at_end_of_statement (parser);
36301 /* Parse a list of Objective-C protocol references.
36303 objc-protocol-refs-opt:
36304 objc-protocol-refs [opt]
36306 objc-protocol-refs:
36307 < objc-identifier-list >
36309 Returns a TREE_LIST of identifiers, if any. */
36311 static tree
36312 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
36314 tree protorefs = NULL_TREE;
36316 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
36318 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
36319 protorefs = cp_parser_objc_identifier_list (parser);
36320 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
36323 return protorefs;
36326 /* Parse a Objective-C visibility specification. */
36328 static void
36329 cp_parser_objc_visibility_spec (cp_parser* parser)
36331 cp_token *vis = cp_lexer_peek_token (parser->lexer);
36333 switch (vis->keyword)
36335 case RID_AT_PRIVATE:
36336 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
36337 break;
36338 case RID_AT_PROTECTED:
36339 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
36340 break;
36341 case RID_AT_PUBLIC:
36342 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
36343 break;
36344 case RID_AT_PACKAGE:
36345 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
36346 break;
36347 default:
36348 return;
36351 /* Eat '@private'/'@protected'/'@public'. */
36352 cp_lexer_consume_token (parser->lexer);
36355 /* Parse an Objective-C method type. Return 'true' if it is a class
36356 (+) method, and 'false' if it is an instance (-) method. */
36358 static inline bool
36359 cp_parser_objc_method_type (cp_parser* parser)
36361 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
36362 return true;
36363 else
36364 return false;
36367 /* Parse an Objective-C protocol qualifier. */
36369 static tree
36370 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
36372 tree quals = NULL_TREE, node;
36373 cp_token *token = cp_lexer_peek_token (parser->lexer);
36375 node = token->u.value;
36377 while (node && identifier_p (node)
36378 && (node == ridpointers [(int) RID_IN]
36379 || node == ridpointers [(int) RID_OUT]
36380 || node == ridpointers [(int) RID_INOUT]
36381 || node == ridpointers [(int) RID_BYCOPY]
36382 || node == ridpointers [(int) RID_BYREF]
36383 || node == ridpointers [(int) RID_ONEWAY]))
36385 quals = tree_cons (NULL_TREE, node, quals);
36386 cp_lexer_consume_token (parser->lexer);
36387 token = cp_lexer_peek_token (parser->lexer);
36388 node = token->u.value;
36391 return quals;
36394 /* Parse an Objective-C typename. */
36396 static tree
36397 cp_parser_objc_typename (cp_parser* parser)
36399 tree type_name = NULL_TREE;
36401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36403 tree proto_quals, cp_type = NULL_TREE;
36405 matching_parens parens;
36406 parens.consume_open (parser); /* Eat '('. */
36407 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
36409 /* An ObjC type name may consist of just protocol qualifiers, in which
36410 case the type shall default to 'id'. */
36411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36413 cp_type = cp_parser_type_id (parser);
36415 /* If the type could not be parsed, an error has already
36416 been produced. For error recovery, behave as if it had
36417 not been specified, which will use the default type
36418 'id'. */
36419 if (cp_type == error_mark_node)
36421 cp_type = NULL_TREE;
36422 /* We need to skip to the closing parenthesis as
36423 cp_parser_type_id() does not seem to do it for
36424 us. */
36425 cp_parser_skip_to_closing_parenthesis (parser,
36426 /*recovering=*/true,
36427 /*or_comma=*/false,
36428 /*consume_paren=*/false);
36432 parens.require_close (parser);
36433 type_name = build_tree_list (proto_quals, cp_type);
36436 return type_name;
36439 /* Check to see if TYPE refers to an Objective-C selector name. */
36441 static bool
36442 cp_parser_objc_selector_p (enum cpp_ttype type)
36444 return (type == CPP_NAME || type == CPP_KEYWORD
36445 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36446 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36447 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36448 || type == CPP_XOR || type == CPP_XOR_EQ);
36451 /* Parse an Objective-C selector. */
36453 static tree
36454 cp_parser_objc_selector (cp_parser* parser)
36456 cp_token *token = cp_lexer_consume_token (parser->lexer);
36458 if (!cp_parser_objc_selector_p (token->type))
36460 error_at (token->location, "invalid Objective-C++ selector name");
36461 return error_mark_node;
36464 /* C++ operator names are allowed to appear in ObjC selectors. */
36465 switch (token->type)
36467 case CPP_AND_AND: return get_identifier ("and");
36468 case CPP_AND_EQ: return get_identifier ("and_eq");
36469 case CPP_AND: return get_identifier ("bitand");
36470 case CPP_OR: return get_identifier ("bitor");
36471 case CPP_COMPL: return get_identifier ("compl");
36472 case CPP_NOT: return get_identifier ("not");
36473 case CPP_NOT_EQ: return get_identifier ("not_eq");
36474 case CPP_OR_OR: return get_identifier ("or");
36475 case CPP_OR_EQ: return get_identifier ("or_eq");
36476 case CPP_XOR: return get_identifier ("xor");
36477 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36478 default: return token->u.value;
36482 /* Parse an Objective-C params list. */
36484 static tree
36485 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36487 tree params = NULL_TREE;
36488 bool maybe_unary_selector_p = true;
36489 cp_token *token = cp_lexer_peek_token (parser->lexer);
36491 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36493 tree selector = NULL_TREE, type_name, identifier;
36494 tree parm_attr = NULL_TREE;
36496 if (token->keyword == RID_ATTRIBUTE)
36497 break;
36499 if (token->type != CPP_COLON)
36500 selector = cp_parser_objc_selector (parser);
36502 /* Detect if we have a unary selector. */
36503 if (maybe_unary_selector_p
36504 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36506 params = selector; /* Might be followed by attributes. */
36507 break;
36510 maybe_unary_selector_p = false;
36511 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36513 /* Something went quite wrong. There should be a colon
36514 here, but there is not. Stop parsing parameters. */
36515 break;
36517 type_name = cp_parser_objc_typename (parser);
36518 /* New ObjC allows attributes on parameters too. */
36519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36520 parm_attr = cp_parser_attributes_opt (parser);
36521 identifier = cp_parser_identifier (parser);
36523 params
36524 = chainon (params,
36525 objc_build_keyword_decl (selector,
36526 type_name,
36527 identifier,
36528 parm_attr));
36530 token = cp_lexer_peek_token (parser->lexer);
36533 if (params == NULL_TREE)
36535 cp_parser_error (parser, "objective-c++ method declaration is expected");
36536 return error_mark_node;
36539 /* We allow tail attributes for the method. */
36540 if (token->keyword == RID_ATTRIBUTE)
36542 *attributes = cp_parser_attributes_opt (parser);
36543 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36544 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36545 return params;
36546 cp_parser_error (parser,
36547 "method attributes must be specified at the end");
36548 return error_mark_node;
36551 if (params == NULL_TREE)
36553 cp_parser_error (parser, "objective-c++ method declaration is expected");
36554 return error_mark_node;
36556 return params;
36559 /* Parse the non-keyword Objective-C params. */
36561 static tree
36562 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36563 tree* attributes)
36565 tree params = make_node (TREE_LIST);
36566 cp_token *token = cp_lexer_peek_token (parser->lexer);
36567 *ellipsisp = false; /* Initially, assume no ellipsis. */
36569 while (token->type == CPP_COMMA)
36571 cp_parameter_declarator *parmdecl;
36572 tree parm;
36574 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36575 token = cp_lexer_peek_token (parser->lexer);
36577 if (token->type == CPP_ELLIPSIS)
36579 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36580 *ellipsisp = true;
36581 token = cp_lexer_peek_token (parser->lexer);
36582 break;
36585 /* TODO: parse attributes for tail parameters. */
36586 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36587 false, NULL);
36588 parm = grokdeclarator (parmdecl->declarator,
36589 &parmdecl->decl_specifiers,
36590 PARM, /*initialized=*/0,
36591 /*attrlist=*/NULL);
36593 chainon (params, build_tree_list (NULL_TREE, parm));
36594 token = cp_lexer_peek_token (parser->lexer);
36597 /* We allow tail attributes for the method. */
36598 if (token->keyword == RID_ATTRIBUTE)
36600 if (*attributes == NULL_TREE)
36602 *attributes = cp_parser_attributes_opt (parser);
36603 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36604 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36605 return params;
36607 else
36608 /* We have an error, but parse the attributes, so that we can
36609 carry on. */
36610 *attributes = cp_parser_attributes_opt (parser);
36612 cp_parser_error (parser,
36613 "method attributes must be specified at the end");
36614 return error_mark_node;
36617 return params;
36620 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36622 static void
36623 cp_parser_objc_interstitial_code (cp_parser* parser)
36625 cp_token *token = cp_lexer_peek_token (parser->lexer);
36627 /* If the next token is `extern' and the following token is a string
36628 literal, then we have a linkage specification. */
36629 if (token->keyword == RID_EXTERN
36630 && cp_parser_is_pure_string_literal
36631 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36632 cp_parser_linkage_specification (parser, NULL_TREE);
36633 /* Handle #pragma, if any. */
36634 else if (token->type == CPP_PRAGMA)
36635 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36636 /* Allow stray semicolons. */
36637 else if (token->type == CPP_SEMICOLON)
36638 cp_lexer_consume_token (parser->lexer);
36639 /* Mark methods as optional or required, when building protocols. */
36640 else if (token->keyword == RID_AT_OPTIONAL)
36642 cp_lexer_consume_token (parser->lexer);
36643 objc_set_method_opt (true);
36645 else if (token->keyword == RID_AT_REQUIRED)
36647 cp_lexer_consume_token (parser->lexer);
36648 objc_set_method_opt (false);
36650 else if (token->keyword == RID_NAMESPACE)
36651 cp_parser_namespace_definition (parser);
36652 /* Other stray characters must generate errors. */
36653 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36655 cp_lexer_consume_token (parser->lexer);
36656 error ("stray %qs between Objective-C++ methods",
36657 token->type == CPP_OPEN_BRACE ? "{" : "}");
36659 /* Finally, try to parse a block-declaration, or a function-definition. */
36660 else
36661 cp_parser_block_declaration (parser, /*statement_p=*/false);
36664 /* Parse a method signature. */
36666 static tree
36667 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36669 tree rettype, kwdparms, optparms;
36670 bool ellipsis = false;
36671 bool is_class_method;
36673 is_class_method = cp_parser_objc_method_type (parser);
36674 rettype = cp_parser_objc_typename (parser);
36675 *attributes = NULL_TREE;
36676 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36677 if (kwdparms == error_mark_node)
36678 return error_mark_node;
36679 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36680 if (optparms == error_mark_node)
36681 return error_mark_node;
36683 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36686 static bool
36687 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36689 tree tattr;
36690 cp_lexer_save_tokens (parser->lexer);
36691 tattr = cp_parser_attributes_opt (parser);
36692 gcc_assert (tattr) ;
36694 /* If the attributes are followed by a method introducer, this is not allowed.
36695 Dump the attributes and flag the situation. */
36696 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36697 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36698 return true;
36700 /* Otherwise, the attributes introduce some interstitial code, possibly so
36701 rewind to allow that check. */
36702 cp_lexer_rollback_tokens (parser->lexer);
36703 return false;
36706 /* Parse an Objective-C method prototype list. */
36708 static void
36709 cp_parser_objc_method_prototype_list (cp_parser* parser)
36711 cp_token *token = cp_lexer_peek_token (parser->lexer);
36713 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36715 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36717 tree attributes, sig;
36718 bool is_class_method;
36719 if (token->type == CPP_PLUS)
36720 is_class_method = true;
36721 else
36722 is_class_method = false;
36723 sig = cp_parser_objc_method_signature (parser, &attributes);
36724 if (sig == error_mark_node)
36726 cp_parser_skip_to_end_of_block_or_statement (parser);
36727 token = cp_lexer_peek_token (parser->lexer);
36728 continue;
36730 objc_add_method_declaration (is_class_method, sig, attributes);
36731 cp_parser_consume_semicolon_at_end_of_statement (parser);
36733 else if (token->keyword == RID_AT_PROPERTY)
36734 cp_parser_objc_at_property_declaration (parser);
36735 else if (token->keyword == RID_ATTRIBUTE
36736 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36737 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36738 OPT_Wattributes,
36739 "prefix attributes are ignored for methods");
36740 else
36741 /* Allow for interspersed non-ObjC++ code. */
36742 cp_parser_objc_interstitial_code (parser);
36744 token = cp_lexer_peek_token (parser->lexer);
36747 if (token->type != CPP_EOF)
36748 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36749 else
36750 cp_parser_error (parser, "expected %<@end%>");
36752 objc_finish_interface ();
36755 /* Parse an Objective-C method definition list. */
36757 static void
36758 cp_parser_objc_method_definition_list (cp_parser* parser)
36760 for (;;)
36762 cp_token *token = cp_lexer_peek_token (parser->lexer);
36764 if (token->keyword == RID_AT_END)
36766 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36767 break;
36769 else if (token->type == CPP_EOF)
36771 cp_parser_error (parser, "expected %<@end%>");
36772 break;
36774 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36776 bool is_class_method = token->type == CPP_PLUS;
36778 push_deferring_access_checks (dk_deferred);
36779 tree attribute;
36780 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36781 if (sig == error_mark_node)
36782 cp_parser_skip_to_end_of_block_or_statement (parser);
36783 else
36785 objc_start_method_definition (is_class_method, sig,
36786 attribute, NULL_TREE);
36788 /* For historical reasons, we accept an optional semicolon. */
36789 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36790 cp_lexer_consume_token (parser->lexer);
36792 perform_deferred_access_checks (tf_warning_or_error);
36793 stop_deferring_access_checks ();
36794 tree meth
36795 = cp_parser_function_definition_after_declarator (parser, false);
36796 pop_deferring_access_checks ();
36797 objc_finish_method_definition (meth);
36800 /* The following case will be removed once @synthesize is
36801 completely implemented. */
36802 else if (token->keyword == RID_AT_PROPERTY)
36803 cp_parser_objc_at_property_declaration (parser);
36804 else if (token->keyword == RID_AT_SYNTHESIZE)
36805 cp_parser_objc_at_synthesize_declaration (parser);
36806 else if (token->keyword == RID_AT_DYNAMIC)
36807 cp_parser_objc_at_dynamic_declaration (parser);
36808 else if (token->keyword == RID_ATTRIBUTE
36809 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36810 warning_at (token->location, OPT_Wattributes,
36811 "prefix attributes are ignored for methods");
36812 else
36813 /* Allow for interspersed non-ObjC++ code. */
36814 cp_parser_objc_interstitial_code (parser);
36817 objc_finish_implementation ();
36820 /* Parse Objective-C ivars. */
36822 static void
36823 cp_parser_objc_class_ivars (cp_parser* parser)
36825 cp_token *token = cp_lexer_peek_token (parser->lexer);
36827 if (token->type != CPP_OPEN_BRACE)
36828 return; /* No ivars specified. */
36830 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36831 token = cp_lexer_peek_token (parser->lexer);
36833 while (token->type != CPP_CLOSE_BRACE
36834 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36836 cp_decl_specifier_seq declspecs;
36837 int decl_class_or_enum_p;
36838 tree prefix_attributes;
36840 cp_parser_objc_visibility_spec (parser);
36842 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36843 break;
36845 cp_parser_decl_specifier_seq (parser,
36846 CP_PARSER_FLAGS_OPTIONAL,
36847 &declspecs,
36848 &decl_class_or_enum_p);
36850 /* auto, register, static, extern, mutable. */
36851 if (declspecs.storage_class != sc_none)
36853 cp_parser_error (parser, "invalid type for instance variable");
36854 declspecs.storage_class = sc_none;
36857 /* thread_local. */
36858 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36860 cp_parser_error (parser, "invalid type for instance variable");
36861 declspecs.locations[ds_thread] = 0;
36864 /* typedef. */
36865 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36867 cp_parser_error (parser, "invalid type for instance variable");
36868 declspecs.locations[ds_typedef] = 0;
36871 prefix_attributes = declspecs.attributes;
36872 declspecs.attributes = NULL_TREE;
36874 /* Keep going until we hit the `;' at the end of the
36875 declaration. */
36876 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36878 tree width = NULL_TREE, attributes, first_attribute, decl;
36879 cp_declarator *declarator = NULL;
36880 int ctor_dtor_or_conv_p;
36882 /* Check for a (possibly unnamed) bitfield declaration. */
36883 token = cp_lexer_peek_token (parser->lexer);
36884 if (token->type == CPP_COLON)
36885 goto eat_colon;
36887 if (token->type == CPP_NAME
36888 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36889 == CPP_COLON))
36891 /* Get the name of the bitfield. */
36892 declarator = make_id_declarator (NULL_TREE,
36893 cp_parser_identifier (parser),
36894 sfk_none, token->location);
36896 eat_colon:
36897 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36898 /* Get the width of the bitfield. */
36899 width
36900 = cp_parser_constant_expression (parser);
36902 else
36904 /* Parse the declarator. */
36905 declarator
36906 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36907 CP_PARSER_FLAGS_NONE,
36908 &ctor_dtor_or_conv_p,
36909 /*parenthesized_p=*/NULL,
36910 /*member_p=*/false,
36911 /*friend_p=*/false,
36912 /*static_p=*/false);
36915 /* Look for attributes that apply to the ivar. */
36916 attributes = cp_parser_attributes_opt (parser);
36917 /* Remember which attributes are prefix attributes and
36918 which are not. */
36919 first_attribute = attributes;
36920 /* Combine the attributes. */
36921 attributes = attr_chainon (prefix_attributes, attributes);
36923 if (width)
36924 /* Create the bitfield declaration. */
36925 decl = grokbitfield (declarator, &declspecs,
36926 width, NULL_TREE, attributes);
36927 else
36928 decl = grokfield (declarator, &declspecs,
36929 NULL_TREE, /*init_const_expr_p=*/false,
36930 NULL_TREE, attributes);
36932 /* Add the instance variable. */
36933 if (decl != error_mark_node && decl != NULL_TREE)
36934 objc_add_instance_variable (decl);
36936 /* Reset PREFIX_ATTRIBUTES. */
36937 if (attributes != error_mark_node)
36939 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36940 attributes = TREE_CHAIN (attributes);
36941 if (attributes)
36942 TREE_CHAIN (attributes) = NULL_TREE;
36945 token = cp_lexer_peek_token (parser->lexer);
36947 if (token->type == CPP_COMMA)
36949 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36950 continue;
36952 break;
36955 cp_parser_consume_semicolon_at_end_of_statement (parser);
36956 token = cp_lexer_peek_token (parser->lexer);
36959 if (token->keyword == RID_AT_END)
36960 cp_parser_error (parser, "expected %<}%>");
36962 /* Do not consume the RID_AT_END, so it will be read again as terminating
36963 the @interface of @implementation. */
36964 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36965 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36967 /* For historical reasons, we accept an optional semicolon. */
36968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36969 cp_lexer_consume_token (parser->lexer);
36972 /* Parse an Objective-C protocol declaration. */
36974 static void
36975 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36977 tree proto, protorefs;
36978 cp_token *tok;
36980 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36983 tok = cp_lexer_peek_token (parser->lexer);
36984 error_at (tok->location, "identifier expected after %<@protocol%>");
36985 cp_parser_consume_semicolon_at_end_of_statement (parser);
36986 return;
36989 /* See if we have a forward declaration or a definition. */
36990 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36992 /* Try a forward declaration first. */
36993 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36995 while (true)
36997 tree id;
36999 id = cp_parser_identifier (parser);
37000 if (id == error_mark_node)
37001 break;
37003 objc_declare_protocol (id, attributes);
37005 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37006 cp_lexer_consume_token (parser->lexer);
37007 else
37008 break;
37010 cp_parser_consume_semicolon_at_end_of_statement (parser);
37013 /* Ok, we got a full-fledged definition (or at least should). */
37014 else
37016 proto = cp_parser_identifier (parser);
37017 protorefs = cp_parser_objc_protocol_refs_opt (parser);
37018 objc_start_protocol (proto, protorefs, attributes);
37019 cp_parser_objc_method_prototype_list (parser);
37023 /* Parse an Objective-C superclass or category. */
37025 static void
37026 cp_parser_objc_superclass_or_category (cp_parser *parser,
37027 bool iface_p,
37028 tree *super,
37029 tree *categ, bool *is_class_extension)
37031 cp_token *next = cp_lexer_peek_token (parser->lexer);
37033 *super = *categ = NULL_TREE;
37034 *is_class_extension = false;
37035 if (next->type == CPP_COLON)
37037 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
37038 *super = cp_parser_identifier (parser);
37040 else if (next->type == CPP_OPEN_PAREN)
37042 matching_parens parens;
37043 parens.consume_open (parser); /* Eat '('. */
37045 /* If there is no category name, and this is an @interface, we
37046 have a class extension. */
37047 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37049 *categ = NULL_TREE;
37050 *is_class_extension = true;
37052 else
37053 *categ = cp_parser_identifier (parser);
37055 parens.require_close (parser);
37059 /* Parse an Objective-C class interface. */
37061 static void
37062 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
37064 tree name, super, categ, protos;
37065 bool is_class_extension;
37067 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
37068 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
37069 name = cp_parser_identifier (parser);
37070 if (name == error_mark_node)
37072 /* It's hard to recover because even if valid @interface stuff
37073 is to follow, we can't compile it (or validate it) if we
37074 don't even know which class it refers to. Let's assume this
37075 was a stray '@interface' token in the stream and skip it.
37077 return;
37079 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
37080 &is_class_extension);
37081 protos = cp_parser_objc_protocol_refs_opt (parser);
37083 /* We have either a class or a category on our hands. */
37084 if (categ || is_class_extension)
37085 objc_start_category_interface (name, categ, protos, attributes);
37086 else
37088 objc_start_class_interface (name, nam_loc, super, protos, attributes);
37089 /* Handle instance variable declarations, if any. */
37090 cp_parser_objc_class_ivars (parser);
37091 objc_continue_interface ();
37094 cp_parser_objc_method_prototype_list (parser);
37097 /* Parse an Objective-C class implementation. */
37099 static void
37100 cp_parser_objc_class_implementation (cp_parser* parser)
37102 tree name, super, categ;
37103 bool is_class_extension;
37105 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
37106 name = cp_parser_identifier (parser);
37107 if (name == error_mark_node)
37109 /* It's hard to recover because even if valid @implementation
37110 stuff is to follow, we can't compile it (or validate it) if
37111 we don't even know which class it refers to. Let's assume
37112 this was a stray '@implementation' token in the stream and
37113 skip it.
37115 return;
37117 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
37118 &is_class_extension);
37120 /* We have either a class or a category on our hands. */
37121 if (categ)
37122 objc_start_category_implementation (name, categ);
37123 else
37125 objc_start_class_implementation (name, super);
37126 /* Handle instance variable declarations, if any. */
37127 cp_parser_objc_class_ivars (parser);
37128 objc_continue_implementation ();
37131 cp_parser_objc_method_definition_list (parser);
37134 /* Consume the @end token and finish off the implementation. */
37136 static void
37137 cp_parser_objc_end_implementation (cp_parser* parser)
37139 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37140 objc_finish_implementation ();
37143 /* Parse an Objective-C declaration. */
37145 static void
37146 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
37148 /* Try to figure out what kind of declaration is present. */
37149 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37151 if (attributes)
37152 switch (kwd->keyword)
37154 case RID_AT_ALIAS:
37155 case RID_AT_CLASS:
37156 case RID_AT_END:
37157 error_at (kwd->location, "attributes may not be specified before"
37158 " the %<@%D%> Objective-C++ keyword",
37159 kwd->u.value);
37160 attributes = NULL;
37161 break;
37162 case RID_AT_IMPLEMENTATION:
37163 warning_at (kwd->location, OPT_Wattributes,
37164 "prefix attributes are ignored before %<@%D%>",
37165 kwd->u.value);
37166 attributes = NULL;
37167 default:
37168 break;
37171 switch (kwd->keyword)
37173 case RID_AT_ALIAS:
37174 cp_parser_objc_alias_declaration (parser);
37175 break;
37176 case RID_AT_CLASS:
37177 cp_parser_objc_class_declaration (parser);
37178 break;
37179 case RID_AT_PROTOCOL:
37180 cp_parser_objc_protocol_declaration (parser, attributes);
37181 break;
37182 case RID_AT_INTERFACE:
37183 cp_parser_objc_class_interface (parser, attributes);
37184 break;
37185 case RID_AT_IMPLEMENTATION:
37186 cp_parser_objc_class_implementation (parser);
37187 break;
37188 case RID_AT_END:
37189 cp_parser_objc_end_implementation (parser);
37190 break;
37191 default:
37192 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37193 kwd->u.value);
37194 cp_parser_skip_to_end_of_block_or_statement (parser);
37198 /* Parse an Objective-C try-catch-finally statement.
37200 objc-try-catch-finally-stmt:
37201 @try compound-statement objc-catch-clause-seq [opt]
37202 objc-finally-clause [opt]
37204 objc-catch-clause-seq:
37205 objc-catch-clause objc-catch-clause-seq [opt]
37207 objc-catch-clause:
37208 @catch ( objc-exception-declaration ) compound-statement
37210 objc-finally-clause:
37211 @finally compound-statement
37213 objc-exception-declaration:
37214 parameter-declaration
37215 '...'
37217 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
37219 Returns NULL_TREE.
37221 PS: This function is identical to c_parser_objc_try_catch_finally_statement
37222 for C. Keep them in sync. */
37224 static tree
37225 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
37227 location_t location;
37228 tree stmt;
37230 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
37231 location = cp_lexer_peek_token (parser->lexer)->location;
37232 objc_maybe_warn_exceptions (location);
37233 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
37234 node, lest it get absorbed into the surrounding block. */
37235 stmt = push_stmt_list ();
37236 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37237 objc_begin_try_stmt (location, pop_stmt_list (stmt));
37239 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
37241 cp_parameter_declarator *parm;
37242 tree parameter_declaration = error_mark_node;
37243 bool seen_open_paren = false;
37244 matching_parens parens;
37246 cp_lexer_consume_token (parser->lexer);
37247 if (parens.require_open (parser))
37248 seen_open_paren = true;
37249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
37251 /* We have "@catch (...)" (where the '...' are literally
37252 what is in the code). Skip the '...'.
37253 parameter_declaration is set to NULL_TREE, and
37254 objc_being_catch_clauses() knows that that means
37255 '...'. */
37256 cp_lexer_consume_token (parser->lexer);
37257 parameter_declaration = NULL_TREE;
37259 else
37261 /* We have "@catch (NSException *exception)" or something
37262 like that. Parse the parameter declaration. */
37263 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
37264 false, NULL);
37265 if (parm == NULL)
37266 parameter_declaration = error_mark_node;
37267 else
37268 parameter_declaration = grokdeclarator (parm->declarator,
37269 &parm->decl_specifiers,
37270 PARM, /*initialized=*/0,
37271 /*attrlist=*/NULL);
37273 if (seen_open_paren)
37274 parens.require_close (parser);
37275 else
37277 /* If there was no open parenthesis, we are recovering from
37278 an error, and we are trying to figure out what mistake
37279 the user has made. */
37281 /* If there is an immediate closing parenthesis, the user
37282 probably forgot the opening one (ie, they typed "@catch
37283 NSException *e)". Parse the closing parenthesis and keep
37284 going. */
37285 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37286 cp_lexer_consume_token (parser->lexer);
37288 /* If these is no immediate closing parenthesis, the user
37289 probably doesn't know that parenthesis are required at
37290 all (ie, they typed "@catch NSException *e"). So, just
37291 forget about the closing parenthesis and keep going. */
37293 objc_begin_catch_clause (parameter_declaration);
37294 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37295 objc_finish_catch_clause ();
37297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
37299 cp_lexer_consume_token (parser->lexer);
37300 location = cp_lexer_peek_token (parser->lexer)->location;
37301 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
37302 node, lest it get absorbed into the surrounding block. */
37303 stmt = push_stmt_list ();
37304 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37305 objc_build_finally_clause (location, pop_stmt_list (stmt));
37308 return objc_finish_try_stmt ();
37311 /* Parse an Objective-C synchronized statement.
37313 objc-synchronized-stmt:
37314 @synchronized ( expression ) compound-statement
37316 Returns NULL_TREE. */
37318 static tree
37319 cp_parser_objc_synchronized_statement (cp_parser *parser)
37321 location_t location;
37322 tree lock, stmt;
37324 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
37326 location = cp_lexer_peek_token (parser->lexer)->location;
37327 objc_maybe_warn_exceptions (location);
37328 matching_parens parens;
37329 parens.require_open (parser);
37330 lock = cp_parser_expression (parser);
37331 parens.require_close (parser);
37333 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
37334 node, lest it get absorbed into the surrounding block. */
37335 stmt = push_stmt_list ();
37336 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37338 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
37341 /* Parse an Objective-C throw statement.
37343 objc-throw-stmt:
37344 @throw assignment-expression [opt] ;
37346 Returns a constructed '@throw' statement. */
37348 static tree
37349 cp_parser_objc_throw_statement (cp_parser *parser)
37351 tree expr = NULL_TREE;
37352 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37354 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
37356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37357 expr = cp_parser_expression (parser);
37359 cp_parser_consume_semicolon_at_end_of_statement (parser);
37361 return objc_build_throw_stmt (loc, expr);
37364 /* Parse an Objective-C statement. */
37366 static tree
37367 cp_parser_objc_statement (cp_parser * parser)
37369 /* Try to figure out what kind of declaration is present. */
37370 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37372 switch (kwd->keyword)
37374 case RID_AT_TRY:
37375 return cp_parser_objc_try_catch_finally_statement (parser);
37376 case RID_AT_SYNCHRONIZED:
37377 return cp_parser_objc_synchronized_statement (parser);
37378 case RID_AT_THROW:
37379 return cp_parser_objc_throw_statement (parser);
37380 default:
37381 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37382 kwd->u.value);
37383 cp_parser_skip_to_end_of_block_or_statement (parser);
37386 return error_mark_node;
37389 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
37390 look ahead to see if an objc keyword follows the attributes. This
37391 is to detect the use of prefix attributes on ObjC @interface and
37392 @protocol. */
37394 static bool
37395 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
37397 cp_lexer_save_tokens (parser->lexer);
37398 tree addon = cp_parser_attributes_opt (parser);
37399 if (addon
37400 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
37402 cp_lexer_commit_tokens (parser->lexer);
37403 if (*attrib)
37404 TREE_CHAIN (*attrib) = addon;
37405 else
37406 *attrib = addon;
37407 return true;
37409 cp_lexer_rollback_tokens (parser->lexer);
37410 return false;
37413 /* This routine is a minimal replacement for
37414 c_parser_struct_declaration () used when parsing the list of
37415 types/names or ObjC++ properties. For example, when parsing the
37416 code
37418 @property (readonly) int a, b, c;
37420 this function is responsible for parsing "int a, int b, int c" and
37421 returning the declarations as CHAIN of DECLs.
37423 TODO: Share this code with cp_parser_objc_class_ivars. It's very
37424 similar parsing. */
37425 static tree
37426 cp_parser_objc_struct_declaration (cp_parser *parser)
37428 tree decls = NULL_TREE;
37429 cp_decl_specifier_seq declspecs;
37430 int decl_class_or_enum_p;
37431 tree prefix_attributes;
37433 cp_parser_decl_specifier_seq (parser,
37434 CP_PARSER_FLAGS_NONE,
37435 &declspecs,
37436 &decl_class_or_enum_p);
37438 if (declspecs.type == error_mark_node)
37439 return error_mark_node;
37441 /* auto, register, static, extern, mutable. */
37442 if (declspecs.storage_class != sc_none)
37444 cp_parser_error (parser, "invalid type for property");
37445 declspecs.storage_class = sc_none;
37448 /* thread_local. */
37449 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37451 cp_parser_error (parser, "invalid type for property");
37452 declspecs.locations[ds_thread] = 0;
37455 /* typedef. */
37456 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37458 cp_parser_error (parser, "invalid type for property");
37459 declspecs.locations[ds_typedef] = 0;
37462 prefix_attributes = declspecs.attributes;
37463 declspecs.attributes = NULL_TREE;
37465 /* Keep going until we hit the `;' at the end of the declaration. */
37466 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37468 tree attributes, first_attribute, decl;
37469 cp_declarator *declarator;
37470 cp_token *token;
37472 /* Parse the declarator. */
37473 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37474 CP_PARSER_FLAGS_NONE,
37475 NULL, NULL, false, false, false);
37477 /* Look for attributes that apply to the ivar. */
37478 attributes = cp_parser_attributes_opt (parser);
37479 /* Remember which attributes are prefix attributes and
37480 which are not. */
37481 first_attribute = attributes;
37482 /* Combine the attributes. */
37483 attributes = attr_chainon (prefix_attributes, attributes);
37485 decl = grokfield (declarator, &declspecs,
37486 NULL_TREE, /*init_const_expr_p=*/false,
37487 NULL_TREE, attributes);
37489 if (decl == error_mark_node || decl == NULL_TREE)
37490 return error_mark_node;
37492 /* Reset PREFIX_ATTRIBUTES. */
37493 if (attributes != error_mark_node)
37495 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37496 attributes = TREE_CHAIN (attributes);
37497 if (attributes)
37498 TREE_CHAIN (attributes) = NULL_TREE;
37501 DECL_CHAIN (decl) = decls;
37502 decls = decl;
37504 token = cp_lexer_peek_token (parser->lexer);
37505 if (token->type == CPP_COMMA)
37507 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37508 continue;
37510 else
37511 break;
37513 return decls;
37516 /* Parse an Objective-C @property declaration. The syntax is:
37518 objc-property-declaration:
37519 '@property' objc-property-attributes[opt] struct-declaration ;
37521 objc-property-attributes:
37522 '(' objc-property-attribute-list ')'
37524 objc-property-attribute-list:
37525 objc-property-attribute
37526 objc-property-attribute-list, objc-property-attribute
37528 objc-property-attribute
37529 'getter' = identifier
37530 'setter' = identifier
37531 'readonly'
37532 'readwrite'
37533 'assign'
37534 'retain'
37535 'copy'
37536 'nonatomic'
37538 For example:
37539 @property NSString *name;
37540 @property (readonly) id object;
37541 @property (retain, nonatomic, getter=getTheName) id name;
37542 @property int a, b, c;
37544 PS: This function is identical to
37545 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37546 static void
37547 cp_parser_objc_at_property_declaration (cp_parser *parser)
37549 /* Parse the optional attribute list.
37551 A list of parsed, but not verified, attributes. */
37552 auto_delete_vec<property_attribute_info> prop_attr_list;
37553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37555 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37557 /* Parse the optional attribute list... */
37558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37560 /* Eat the '('. */
37561 matching_parens parens;
37562 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37563 parens.consume_open (parser);
37564 bool syntax_error = false;
37566 /* Allow empty @property attribute lists, but with a warning. */
37567 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37568 location_t attr_comb;
37569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37571 attr_comb = make_location (attr_end, attr_start, attr_end);
37572 warning_at (attr_comb, OPT_Wattributes,
37573 "empty property attribute list");
37575 else
37576 while (true)
37578 cp_token *token = cp_lexer_peek_token (parser->lexer);
37579 attr_start = token->location;
37580 attr_end = get_finish (token->location);
37581 attr_comb = make_location (attr_start, attr_start, attr_end);
37583 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37585 warning_at (attr_comb, OPT_Wattributes,
37586 "missing property attribute");
37587 if (token->type == CPP_CLOSE_PAREN)
37588 break;
37589 cp_lexer_consume_token (parser->lexer);
37590 continue;
37593 tree attr_name = NULL_TREE;
37594 if (identifier_p (token->u.value))
37595 attr_name = token->u.value;
37597 enum rid keyword;
37598 if (token->type == CPP_NAME)
37599 keyword = C_RID_CODE (token->u.value);
37600 else if (token->type == CPP_KEYWORD
37601 && token->keyword == RID_CLASS)
37602 /* Account for accepting the 'class' keyword in this context. */
37603 keyword = RID_CLASS;
37604 else
37605 keyword = RID_MAX; /* By definition, an unknown property. */
37606 cp_lexer_consume_token (parser->lexer);
37608 enum objc_property_attribute_kind prop_kind
37609 = objc_prop_attr_kind_for_rid (keyword);
37610 property_attribute_info *prop
37611 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37612 prop_attr_list.safe_push (prop);
37614 tree meth_name;
37615 switch (prop->prop_kind)
37617 default: break;
37618 case OBJC_PROPERTY_ATTR_UNKNOWN:
37619 if (attr_name)
37620 error_at (attr_start, "unknown property attribute %qE",
37621 attr_name);
37622 else
37623 error_at (attr_start, "unknown property attribute");
37624 prop->parse_error = syntax_error = true;
37625 break;
37627 case OBJC_PROPERTY_ATTR_GETTER:
37628 case OBJC_PROPERTY_ATTR_SETTER:
37629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37631 attr_comb = make_location (attr_end, attr_start, attr_end);
37632 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37633 attr_name);
37634 prop->parse_error = syntax_error = true;
37635 break;
37638 token = cp_lexer_peek_token (parser->lexer);
37639 attr_end = token->location;
37640 cp_lexer_consume_token (parser->lexer); /* eat the = */
37642 if (!cp_parser_objc_selector_p
37643 (cp_lexer_peek_token (parser->lexer)->type))
37645 attr_comb = make_location (attr_end, attr_start, attr_end);
37646 error_at (attr_comb, "expected %qE selector name",
37647 attr_name);
37648 prop->parse_error = syntax_error = true;
37649 break;
37652 /* Get the end of the method name, and consume the name. */
37653 token = cp_lexer_peek_token (parser->lexer);
37654 attr_end = get_finish (token->location);
37655 /* Because method names may contain C++ keywords, we have a
37656 routine to fetch them (this also consumes the token). */
37657 meth_name = cp_parser_objc_selector (parser);
37659 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37663 attr_comb = make_location (attr_end, attr_start,
37664 attr_end);
37665 error_at (attr_comb, "setter method names must"
37666 " terminate with %<:%>");
37667 prop->parse_error = syntax_error = true;
37669 else
37671 attr_end = get_finish (cp_lexer_peek_token
37672 (parser->lexer)->location);
37673 cp_lexer_consume_token (parser->lexer);
37675 attr_comb = make_location (attr_start, attr_start,
37676 attr_end);
37678 else
37679 attr_comb = make_location (attr_start, attr_start,
37680 attr_end);
37681 prop->ident = meth_name;
37682 /* Updated location including all that was successfully
37683 parsed. */
37684 prop->prop_loc = attr_comb;
37685 break;
37688 /* If we see a comma here, then keep going - even if we already
37689 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37690 this makes a more useful output and avoid spurious warnings
37691 about missing attributes that are, in fact, specified after the
37692 one with the syntax error. */
37693 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37694 cp_lexer_consume_token (parser->lexer);
37695 else
37696 break;
37699 if (syntax_error || !parens.require_close (parser))
37700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37701 /*or_comma=*/false,
37702 /*consume_paren=*/true);
37705 /* 'properties' is the list of properties that we read. Usually a
37706 single one, but maybe more (eg, in "@property int a, b, c;" there
37707 are three).
37708 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37709 that we can issue a meaningful and consistent (between C/C++) error
37710 message from objc_add_property_declaration (). */
37711 tree properties = cp_parser_objc_struct_declaration (parser);
37713 if (properties == error_mark_node)
37714 cp_parser_skip_to_end_of_statement (parser);
37715 else if (properties == NULL_TREE)
37716 cp_parser_error (parser, "expected identifier");
37717 else
37719 /* Comma-separated properties are chained together in reverse order;
37720 add them one by one. */
37721 properties = nreverse (properties);
37722 for (; properties; properties = TREE_CHAIN (properties))
37723 objc_add_property_declaration (loc, copy_node (properties),
37724 prop_attr_list);
37727 cp_parser_consume_semicolon_at_end_of_statement (parser);
37730 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37732 objc-synthesize-declaration:
37733 @synthesize objc-synthesize-identifier-list ;
37735 objc-synthesize-identifier-list:
37736 objc-synthesize-identifier
37737 objc-synthesize-identifier-list, objc-synthesize-identifier
37739 objc-synthesize-identifier
37740 identifier
37741 identifier = identifier
37743 For example:
37744 @synthesize MyProperty;
37745 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37747 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37748 for C. Keep them in sync.
37750 static void
37751 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37753 tree list = NULL_TREE;
37754 location_t loc;
37755 loc = cp_lexer_peek_token (parser->lexer)->location;
37757 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37758 while (true)
37760 tree property, ivar;
37761 property = cp_parser_identifier (parser);
37762 if (property == error_mark_node)
37764 cp_parser_consume_semicolon_at_end_of_statement (parser);
37765 return;
37767 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37769 cp_lexer_consume_token (parser->lexer);
37770 ivar = cp_parser_identifier (parser);
37771 if (ivar == error_mark_node)
37773 cp_parser_consume_semicolon_at_end_of_statement (parser);
37774 return;
37777 else
37778 ivar = NULL_TREE;
37779 list = chainon (list, build_tree_list (ivar, property));
37780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37781 cp_lexer_consume_token (parser->lexer);
37782 else
37783 break;
37785 cp_parser_consume_semicolon_at_end_of_statement (parser);
37786 objc_add_synthesize_declaration (loc, list);
37789 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37791 objc-dynamic-declaration:
37792 @dynamic identifier-list ;
37794 For example:
37795 @dynamic MyProperty;
37796 @dynamic MyProperty, AnotherProperty;
37798 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37799 for C. Keep them in sync.
37801 static void
37802 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37804 tree list = NULL_TREE;
37805 location_t loc;
37806 loc = cp_lexer_peek_token (parser->lexer)->location;
37808 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37809 while (true)
37811 tree property;
37812 property = cp_parser_identifier (parser);
37813 if (property == error_mark_node)
37815 cp_parser_consume_semicolon_at_end_of_statement (parser);
37816 return;
37818 list = chainon (list, build_tree_list (NULL, property));
37819 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37820 cp_lexer_consume_token (parser->lexer);
37821 else
37822 break;
37824 cp_parser_consume_semicolon_at_end_of_statement (parser);
37825 objc_add_dynamic_declaration (loc, list);
37829 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37831 /* Returns name of the next clause.
37832 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37833 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37834 returned and the token is consumed. */
37836 static pragma_omp_clause
37837 cp_parser_omp_clause_name (cp_parser *parser)
37839 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37842 result = PRAGMA_OACC_CLAUSE_AUTO;
37843 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37844 result = PRAGMA_OMP_CLAUSE_IF;
37845 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37846 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37847 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37848 result = PRAGMA_OACC_CLAUSE_DELETE;
37849 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37850 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37851 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37852 result = PRAGMA_OMP_CLAUSE_FOR;
37853 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37856 const char *p = IDENTIFIER_POINTER (id);
37858 switch (p[0])
37860 case 'a':
37861 if (!strcmp ("affinity", p))
37862 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37863 else if (!strcmp ("aligned", p))
37864 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37865 else if (!strcmp ("allocate", p))
37866 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37867 else if (!strcmp ("async", p))
37868 result = PRAGMA_OACC_CLAUSE_ASYNC;
37869 else if (!strcmp ("attach", p))
37870 result = PRAGMA_OACC_CLAUSE_ATTACH;
37871 break;
37872 case 'b':
37873 if (!strcmp ("bind", p))
37874 result = PRAGMA_OMP_CLAUSE_BIND;
37875 break;
37876 case 'c':
37877 if (!strcmp ("collapse", p))
37878 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37879 else if (!strcmp ("copy", p))
37880 result = PRAGMA_OACC_CLAUSE_COPY;
37881 else if (!strcmp ("copyin", p))
37882 result = PRAGMA_OMP_CLAUSE_COPYIN;
37883 else if (!strcmp ("copyout", p))
37884 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37885 else if (!strcmp ("copyprivate", p))
37886 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37887 else if (!strcmp ("create", p))
37888 result = PRAGMA_OACC_CLAUSE_CREATE;
37889 break;
37890 case 'd':
37891 if (!strcmp ("defaultmap", p))
37892 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37893 else if (!strcmp ("depend", p))
37894 result = PRAGMA_OMP_CLAUSE_DEPEND;
37895 else if (!strcmp ("detach", p))
37896 result = PRAGMA_OACC_CLAUSE_DETACH;
37897 else if (!strcmp ("device", p))
37898 result = PRAGMA_OMP_CLAUSE_DEVICE;
37899 else if (!strcmp ("deviceptr", p))
37900 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37901 else if (!strcmp ("device_resident", p))
37902 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37903 else if (!strcmp ("device_type", p))
37904 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37905 else if (!strcmp ("dist_schedule", p))
37906 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37907 else if (!strcmp ("doacross", p))
37908 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37909 break;
37910 case 'e':
37911 if (!strcmp ("enter", p))
37912 result = PRAGMA_OMP_CLAUSE_ENTER;
37913 break;
37914 case 'f':
37915 if (!strcmp ("filter", p))
37916 result = PRAGMA_OMP_CLAUSE_FILTER;
37917 else if (!strcmp ("final", p))
37918 result = PRAGMA_OMP_CLAUSE_FINAL;
37919 else if (!strcmp ("finalize", p))
37920 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37921 else if (!strcmp ("firstprivate", p))
37922 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37923 else if (!strcmp ("from", p))
37924 result = PRAGMA_OMP_CLAUSE_FROM;
37925 break;
37926 case 'g':
37927 if (!strcmp ("gang", p))
37928 result = PRAGMA_OACC_CLAUSE_GANG;
37929 else if (!strcmp ("grainsize", p))
37930 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37931 break;
37932 case 'h':
37933 if (!strcmp ("has_device_addr", p))
37934 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37935 else if (!strcmp ("hint", p))
37936 result = PRAGMA_OMP_CLAUSE_HINT;
37937 else if (!strcmp ("host", p))
37938 result = PRAGMA_OACC_CLAUSE_HOST;
37939 break;
37940 case 'i':
37941 if (!strcmp ("if_present", p))
37942 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37943 else if (!strcmp ("in_reduction", p))
37944 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37945 else if (!strcmp ("inbranch", p))
37946 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37947 else if (!strcmp ("independent", p))
37948 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37949 else if (!strcmp ("indirect", p))
37950 result = PRAGMA_OMP_CLAUSE_INDIRECT;
37951 else if (!strcmp ("is_device_ptr", p))
37952 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37953 break;
37954 case 'l':
37955 if (!strcmp ("lastprivate", p))
37956 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37957 else if (!strcmp ("linear", p))
37958 result = PRAGMA_OMP_CLAUSE_LINEAR;
37959 else if (!strcmp ("link", p))
37960 result = PRAGMA_OMP_CLAUSE_LINK;
37961 break;
37962 case 'm':
37963 if (!strcmp ("map", p))
37964 result = PRAGMA_OMP_CLAUSE_MAP;
37965 else if (!strcmp ("mergeable", p))
37966 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37967 break;
37968 case 'n':
37969 if (!strcmp ("no_create", p))
37970 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37971 else if (!strcmp ("nogroup", p))
37972 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37973 else if (!strcmp ("nohost", p))
37974 result = PRAGMA_OACC_CLAUSE_NOHOST;
37975 else if (!strcmp ("nontemporal", p))
37976 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37977 else if (!strcmp ("notinbranch", p))
37978 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37979 else if (!strcmp ("nowait", p))
37980 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37981 else if (!strcmp ("num_gangs", p))
37982 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37983 else if (!strcmp ("num_tasks", p))
37984 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37985 else if (!strcmp ("num_teams", p))
37986 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37987 else if (!strcmp ("num_threads", p))
37988 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37989 else if (!strcmp ("num_workers", p))
37990 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37991 break;
37992 case 'o':
37993 if (!strcmp ("ordered", p))
37994 result = PRAGMA_OMP_CLAUSE_ORDERED;
37995 else if (!strcmp ("order", p))
37996 result = PRAGMA_OMP_CLAUSE_ORDER;
37997 break;
37998 case 'p':
37999 if (!strcmp ("parallel", p))
38000 result = PRAGMA_OMP_CLAUSE_PARALLEL;
38001 else if (!strcmp ("present", p))
38002 result = PRAGMA_OACC_CLAUSE_PRESENT;
38003 else if (!strcmp ("present_or_copy", p)
38004 || !strcmp ("pcopy", p))
38005 result = PRAGMA_OACC_CLAUSE_COPY;
38006 else if (!strcmp ("present_or_copyin", p)
38007 || !strcmp ("pcopyin", p))
38008 result = PRAGMA_OACC_CLAUSE_COPYIN;
38009 else if (!strcmp ("present_or_copyout", p)
38010 || !strcmp ("pcopyout", p))
38011 result = PRAGMA_OACC_CLAUSE_COPYOUT;
38012 else if (!strcmp ("present_or_create", p)
38013 || !strcmp ("pcreate", p))
38014 result = PRAGMA_OACC_CLAUSE_CREATE;
38015 else if (!strcmp ("priority", p))
38016 result = PRAGMA_OMP_CLAUSE_PRIORITY;
38017 else if (!strcmp ("proc_bind", p))
38018 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
38019 break;
38020 case 'r':
38021 if (!strcmp ("reduction", p))
38022 result = PRAGMA_OMP_CLAUSE_REDUCTION;
38023 break;
38024 case 's':
38025 if (!strcmp ("safelen", p))
38026 result = PRAGMA_OMP_CLAUSE_SAFELEN;
38027 else if (!strcmp ("schedule", p))
38028 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
38029 else if (!strcmp ("sections", p))
38030 result = PRAGMA_OMP_CLAUSE_SECTIONS;
38031 else if (!strcmp ("self", p))
38032 result = PRAGMA_OACC_CLAUSE_SELF;
38033 else if (!strcmp ("seq", p))
38034 result = PRAGMA_OACC_CLAUSE_SEQ;
38035 else if (!strcmp ("shared", p))
38036 result = PRAGMA_OMP_CLAUSE_SHARED;
38037 else if (!strcmp ("simd", p))
38038 result = PRAGMA_OMP_CLAUSE_SIMD;
38039 else if (!strcmp ("simdlen", p))
38040 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
38041 break;
38042 case 't':
38043 if (!strcmp ("task_reduction", p))
38044 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
38045 else if (!strcmp ("taskgroup", p))
38046 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
38047 else if (!strcmp ("thread_limit", p))
38048 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
38049 else if (!strcmp ("threads", p))
38050 result = PRAGMA_OMP_CLAUSE_THREADS;
38051 else if (!strcmp ("tile", p))
38052 result = PRAGMA_OACC_CLAUSE_TILE;
38053 else if (!strcmp ("to", p))
38054 result = PRAGMA_OMP_CLAUSE_TO;
38055 break;
38056 case 'u':
38057 if (!strcmp ("uniform", p))
38058 result = PRAGMA_OMP_CLAUSE_UNIFORM;
38059 else if (!strcmp ("untied", p))
38060 result = PRAGMA_OMP_CLAUSE_UNTIED;
38061 else if (!strcmp ("use_device", p))
38062 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
38063 else if (!strcmp ("use_device_addr", p))
38064 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
38065 else if (!strcmp ("use_device_ptr", p))
38066 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
38067 break;
38068 case 'v':
38069 if (!strcmp ("vector", p))
38070 result = PRAGMA_OACC_CLAUSE_VECTOR;
38071 else if (!strcmp ("vector_length", p))
38072 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
38073 break;
38074 case 'w':
38075 if (!strcmp ("wait", p))
38076 result = PRAGMA_OACC_CLAUSE_WAIT;
38077 else if (!strcmp ("worker", p))
38078 result = PRAGMA_OACC_CLAUSE_WORKER;
38079 break;
38083 if (result != PRAGMA_OMP_CLAUSE_NONE)
38084 cp_lexer_consume_token (parser->lexer);
38086 return result;
38089 /* Validate that a clause of the given type does not already exist. */
38091 static void
38092 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
38093 const char *name, location_t location)
38095 if (omp_find_clause (clauses, code))
38096 error_at (location, "too many %qs clauses", name);
38099 /* OpenMP 2.5:
38100 variable-list:
38101 identifier
38102 variable-list , identifier
38104 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
38105 colon). An opening parenthesis will have been consumed by the caller.
38107 If KIND is nonzero, create the appropriate node and install the decl
38108 in OMP_CLAUSE_DECL and add the node to the head of the list.
38110 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
38111 return the list created.
38113 COLON can be NULL if only closing parenthesis should end the list,
38114 or pointer to bool which will receive false if the list is terminated
38115 by closing parenthesis or true if the list is terminated by colon.
38117 The optional ALLOW_DEREF argument is true if list items can use the deref
38118 (->) operator. */
38120 struct omp_dim
38122 tree low_bound, length;
38123 location_t loc;
38124 bool no_colon;
38125 omp_dim (tree lb, tree len, location_t lo, bool nc)
38126 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
38129 static tree
38130 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
38131 tree list, bool *colon,
38132 bool map_lvalue = false)
38134 auto_vec<omp_dim> dims;
38135 bool array_section_p;
38136 cp_token *token;
38137 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38138 if (colon)
38140 parser->colon_corrects_to_scope_p = false;
38141 *colon = false;
38143 while (1)
38145 tree name, decl;
38147 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38148 cp_parser_parse_tentatively (parser);
38149 /* This condition doesn't include OMP_CLAUSE_DEPEND or
38150 OMP_CLAUSE_AFFINITY since lvalue ("locator list") parsing for those is
38151 handled further down the function. */
38152 else if (map_lvalue
38153 && (kind == OMP_CLAUSE_MAP
38154 || kind == OMP_CLAUSE_TO
38155 || kind == OMP_CLAUSE_FROM))
38157 auto s = make_temp_override (parser->omp_array_section_p, true);
38158 token = cp_lexer_peek_token (parser->lexer);
38159 location_t loc = token->location;
38160 decl = cp_parser_assignment_expression (parser);
38162 /* This code rewrites a parsed expression containing various tree
38163 codes used to represent array accesses into a more uniform nest of
38164 OMP_ARRAY_SECTION nodes before it is processed by
38165 semantics.cc:handle_omp_array_sections_1. It might be more
38166 efficient to move this logic to that function instead, analysing
38167 the parsed expression directly rather than this preprocessed
38168 form. */
38169 dims.truncate (0);
38170 if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38172 while (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38174 tree low_bound = TREE_OPERAND (decl, 1);
38175 tree length = TREE_OPERAND (decl, 2);
38176 dims.safe_push (omp_dim (low_bound, length, loc, false));
38177 decl = TREE_OPERAND (decl, 0);
38180 while (TREE_CODE (decl) == ARRAY_REF
38181 || TREE_CODE (decl) == INDIRECT_REF
38182 || TREE_CODE (decl) == COMPOUND_EXPR)
38184 if (REFERENCE_REF_P (decl))
38185 break;
38187 if (TREE_CODE (decl) == COMPOUND_EXPR)
38189 decl = TREE_OPERAND (decl, 1);
38190 STRIP_NOPS (decl);
38192 else if (TREE_CODE (decl) == INDIRECT_REF)
38194 dims.safe_push (omp_dim (integer_zero_node,
38195 integer_one_node, loc, true));
38196 decl = TREE_OPERAND (decl, 0);
38198 else /* ARRAY_REF. */
38200 tree index = TREE_OPERAND (decl, 1);
38201 dims.safe_push (omp_dim (index, integer_one_node, loc,
38202 true));
38203 decl = TREE_OPERAND (decl, 0);
38207 /* Bare references have their own special handling, so remove
38208 the explicit dereference added by convert_from_reference. */
38209 if (REFERENCE_REF_P (decl))
38210 decl = TREE_OPERAND (decl, 0);
38212 for (int i = dims.length () - 1; i >= 0; i--)
38213 decl = grok_omp_array_section (loc, decl, dims[i].low_bound,
38214 dims[i].length);
38216 else if (TREE_CODE (decl) == INDIRECT_REF)
38218 bool ref_p = REFERENCE_REF_P (decl);
38220 /* If we have "*foo" and
38221 - it's an indirection of a reference, "unconvert" it, i.e.
38222 strip the indirection (to just "foo").
38223 - it's an indirection of a pointer, turn it into
38224 "foo[0:1]". */
38225 decl = TREE_OPERAND (decl, 0);
38226 STRIP_NOPS (decl);
38228 if (!ref_p)
38229 decl = grok_omp_array_section (loc, decl, integer_zero_node,
38230 integer_one_node);
38232 else if (TREE_CODE (decl) == ARRAY_REF)
38234 tree idx = TREE_OPERAND (decl, 1);
38236 decl = TREE_OPERAND (decl, 0);
38237 STRIP_NOPS (decl);
38239 decl = grok_omp_array_section (loc, decl, idx, integer_one_node);
38241 else if (TREE_CODE (decl) == NON_LVALUE_EXPR
38242 || CONVERT_EXPR_P (decl))
38243 decl = TREE_OPERAND (decl, 0);
38245 goto build_clause;
38247 token = cp_lexer_peek_token (parser->lexer);
38248 if (kind != 0
38249 && cp_parser_is_keyword (token, RID_THIS))
38251 decl = finish_this_expr ();
38252 if (TREE_CODE (decl) == NON_LVALUE_EXPR
38253 || CONVERT_EXPR_P (decl))
38254 decl = TREE_OPERAND (decl, 0);
38255 cp_lexer_consume_token (parser->lexer);
38257 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
38258 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
38259 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
38261 cp_id_kind idk;
38262 decl = cp_parser_primary_expression (parser, false, false, false,
38263 &idk);
38265 else if (kind == OMP_CLAUSE_DEPEND
38266 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
38267 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38268 || cp_lexer_nth_token_is (parser->lexer, 2,
38269 CPP_CLOSE_PAREN)))
38271 decl = ridpointers[RID_OMP_ALL_MEMORY];
38272 cp_lexer_consume_token (parser->lexer);
38274 else
38276 name = cp_parser_id_expression (parser, /*template_p=*/false,
38277 /*check_dependency_p=*/true,
38278 /*template_p=*/NULL,
38279 /*declarator_p=*/false,
38280 /*optional_p=*/false);
38281 if (name == error_mark_node)
38283 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38284 && cp_parser_simulate_error (parser))
38285 goto depend_lvalue;
38286 goto skip_comma;
38289 if (identifier_p (name))
38290 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38291 else
38292 decl = name;
38293 if (decl == error_mark_node)
38295 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38296 && cp_parser_simulate_error (parser))
38297 goto depend_lvalue;
38298 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38299 token->location);
38302 if (outer_automatic_var_p (decl))
38303 decl = process_outer_var_ref (decl, tf_warning_or_error);
38304 if (decl == error_mark_node)
38306 else if (kind != 0)
38308 switch (kind)
38310 case OMP_CLAUSE__CACHE_:
38311 /* The OpenACC cache directive explicitly only allows "array
38312 elements or subarrays". */
38313 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
38315 error_at (token->location, "expected %<[%>");
38316 decl = error_mark_node;
38317 break;
38319 /* FALLTHROUGH. */
38320 case OMP_CLAUSE_MAP:
38321 case OMP_CLAUSE_FROM:
38322 case OMP_CLAUSE_TO:
38323 start_component_ref:
38324 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38325 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
38327 cpp_ttype ttype
38328 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38329 ? CPP_DOT : CPP_DEREF;
38330 location_t loc
38331 = cp_lexer_peek_token (parser->lexer)->location;
38332 cp_id_kind idk = CP_ID_KIND_NONE;
38333 cp_lexer_consume_token (parser->lexer);
38334 decl = convert_from_reference (decl);
38335 decl = (cp_parser_postfix_dot_deref_expression
38336 (parser, ttype, cp_expr (decl, token->location),
38337 false, &idk, loc));
38339 /* FALLTHROUGH. */
38340 case OMP_CLAUSE_AFFINITY:
38341 case OMP_CLAUSE_DEPEND:
38342 case OMP_CLAUSE_REDUCTION:
38343 case OMP_CLAUSE_IN_REDUCTION:
38344 case OMP_CLAUSE_TASK_REDUCTION:
38345 case OMP_CLAUSE_HAS_DEVICE_ADDR:
38346 array_section_p = false;
38347 dims.truncate (0);
38348 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
38350 location_t loc = UNKNOWN_LOCATION;
38351 tree low_bound = NULL_TREE, length = NULL_TREE;
38352 bool no_colon = false;
38354 parser->colon_corrects_to_scope_p = false;
38355 cp_lexer_consume_token (parser->lexer);
38356 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38358 loc = cp_lexer_peek_token (parser->lexer)->location;
38359 low_bound = cp_parser_expression (parser);
38360 /* Later handling is not prepared to see through these. */
38361 gcc_checking_assert (!location_wrapper_p (low_bound));
38363 if (!colon)
38364 parser->colon_corrects_to_scope_p
38365 = saved_colon_corrects_to_scope_p;
38366 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
38368 length = integer_one_node;
38369 no_colon = true;
38371 else
38373 /* Look for `:'. */
38374 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38376 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38377 && cp_parser_simulate_error (parser))
38378 goto depend_lvalue;
38379 goto skip_comma;
38381 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38382 cp_parser_commit_to_tentative_parse (parser);
38383 else
38384 array_section_p = true;
38385 if (!cp_lexer_next_token_is (parser->lexer,
38386 CPP_CLOSE_SQUARE))
38388 length = cp_parser_expression (parser);
38389 /* Later handling is not prepared to see through these. */
38390 gcc_checking_assert (!location_wrapper_p (length));
38393 /* Look for the closing `]'. */
38394 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
38395 RT_CLOSE_SQUARE))
38397 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38398 && cp_parser_simulate_error (parser))
38399 goto depend_lvalue;
38400 goto skip_comma;
38403 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
38406 if ((kind == OMP_CLAUSE_MAP
38407 || kind == OMP_CLAUSE_FROM
38408 || kind == OMP_CLAUSE_TO)
38409 && !array_section_p
38410 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38411 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
38413 for (unsigned i = 0; i < dims.length (); i++)
38415 gcc_assert (dims[i].length == integer_one_node);
38416 decl = build_array_ref (dims[i].loc,
38417 decl, dims[i].low_bound);
38419 goto start_component_ref;
38421 else
38422 for (unsigned i = 0; i < dims.length (); i++)
38423 decl = build_omp_array_section (input_location, decl,
38424 dims[i].low_bound,
38425 dims[i].length);
38426 break;
38427 default:
38428 break;
38431 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38434 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
38435 && cp_parser_simulate_error (parser))
38437 depend_lvalue:
38438 cp_parser_abort_tentative_parse (parser);
38439 decl = cp_parser_assignment_expression (parser, NULL,
38440 false, false);
38442 else
38443 cp_parser_parse_definitely (parser);
38446 build_clause:
38447 tree u = build_omp_clause (token->location, kind);
38448 OMP_CLAUSE_DECL (u) = decl;
38449 OMP_CLAUSE_CHAIN (u) = list;
38450 list = u;
38452 else
38453 list = tree_cons (decl, NULL_TREE, list);
38455 get_comma:
38456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38457 break;
38458 cp_lexer_consume_token (parser->lexer);
38461 if (colon)
38462 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38464 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38466 *colon = true;
38467 cp_parser_require (parser, CPP_COLON, RT_COLON);
38468 return list;
38471 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38473 int ending;
38475 /* Try to resync to an unnested comma. Copied from
38476 cp_parser_parenthesized_expression_list. */
38477 skip_comma:
38478 if (colon)
38479 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38480 ending = cp_parser_skip_to_closing_parenthesis (parser,
38481 /*recovering=*/true,
38482 /*or_comma=*/true,
38483 /*consume_paren=*/true);
38484 if (ending < 0)
38485 goto get_comma;
38488 return list;
38491 /* Similarly, but expect leading and trailing parenthesis. This is a very
38492 common case for omp clauses. */
38494 static tree
38495 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
38496 bool map_lvalue = false)
38498 if (parser->lexer->in_omp_decl_attribute)
38500 if (kind)
38502 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38503 tree u = build_omp_clause (loc, kind);
38504 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
38505 OMP_CLAUSE_CHAIN (u) = list;
38506 return u;
38508 else
38509 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
38510 list);
38513 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38514 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
38515 map_lvalue);
38516 return list;
38519 /* OpenACC 2.0:
38520 copy ( variable-list )
38521 copyin ( variable-list )
38522 copyout ( variable-list )
38523 create ( variable-list )
38524 delete ( variable-list )
38525 present ( variable-list )
38527 OpenACC 2.6:
38528 no_create ( variable-list )
38529 attach ( variable-list )
38530 detach ( variable-list ) */
38532 static tree
38533 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38534 tree list)
38536 enum gomp_map_kind kind;
38537 switch (c_kind)
38539 case PRAGMA_OACC_CLAUSE_ATTACH:
38540 kind = GOMP_MAP_ATTACH;
38541 break;
38542 case PRAGMA_OACC_CLAUSE_COPY:
38543 kind = GOMP_MAP_TOFROM;
38544 break;
38545 case PRAGMA_OACC_CLAUSE_COPYIN:
38546 kind = GOMP_MAP_TO;
38547 break;
38548 case PRAGMA_OACC_CLAUSE_COPYOUT:
38549 kind = GOMP_MAP_FROM;
38550 break;
38551 case PRAGMA_OACC_CLAUSE_CREATE:
38552 kind = GOMP_MAP_ALLOC;
38553 break;
38554 case PRAGMA_OACC_CLAUSE_DELETE:
38555 kind = GOMP_MAP_RELEASE;
38556 break;
38557 case PRAGMA_OACC_CLAUSE_DETACH:
38558 kind = GOMP_MAP_DETACH;
38559 break;
38560 case PRAGMA_OACC_CLAUSE_DEVICE:
38561 kind = GOMP_MAP_FORCE_TO;
38562 break;
38563 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38564 kind = GOMP_MAP_DEVICE_RESIDENT;
38565 break;
38566 case PRAGMA_OACC_CLAUSE_LINK:
38567 kind = GOMP_MAP_LINK;
38568 break;
38569 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38570 kind = GOMP_MAP_IF_PRESENT;
38571 break;
38572 case PRAGMA_OACC_CLAUSE_PRESENT:
38573 kind = GOMP_MAP_FORCE_PRESENT;
38574 break;
38575 case PRAGMA_OACC_CLAUSE_SELF:
38576 /* "The 'host' clause is a synonym for the 'self' clause." */
38577 case PRAGMA_OACC_CLAUSE_HOST:
38578 kind = GOMP_MAP_FORCE_FROM;
38579 break;
38580 default:
38581 gcc_unreachable ();
38583 tree nl, c;
38584 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, false);
38586 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38587 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38589 return nl;
38592 /* OpenACC 2.0:
38593 deviceptr ( variable-list ) */
38595 static tree
38596 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38599 tree vars, t;
38601 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38602 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38603 variable-list must only allow for pointer variables. */
38604 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38605 for (t = vars; t; t = TREE_CHAIN (t))
38607 tree v = TREE_PURPOSE (t);
38608 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38609 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38610 OMP_CLAUSE_DECL (u) = v;
38611 OMP_CLAUSE_CHAIN (u) = list;
38612 list = u;
38615 return list;
38618 /* OpenACC 2.5:
38619 auto
38620 finalize
38621 independent
38622 nohost
38623 seq */
38625 static tree
38626 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38627 tree list)
38629 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38631 tree c = build_omp_clause (loc, code);
38632 OMP_CLAUSE_CHAIN (c) = list;
38634 return c;
38637 /* OpenACC:
38638 num_gangs ( expression )
38639 num_workers ( expression )
38640 vector_length ( expression ) */
38642 static tree
38643 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38644 const char *str, tree list)
38646 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38648 matching_parens parens;
38649 if (!parens.require_open (parser))
38650 return list;
38652 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38654 if (t == error_mark_node
38655 || !parens.require_close (parser))
38657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38658 /*or_comma=*/false,
38659 /*consume_paren=*/true);
38660 return list;
38663 check_no_duplicate_clause (list, code, str, loc);
38665 tree c = build_omp_clause (loc, code);
38666 OMP_CLAUSE_OPERAND (c, 0) = t;
38667 OMP_CLAUSE_CHAIN (c) = list;
38668 return c;
38671 /* OpenACC:
38673 gang [( gang-arg-list )]
38674 worker [( [num:] int-expr )]
38675 vector [( [length:] int-expr )]
38677 where gang-arg is one of:
38679 [num:] int-expr
38680 static: size-expr
38682 and size-expr may be:
38685 int-expr
38688 static tree
38689 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38690 omp_clause_code kind,
38691 const char *str, tree list)
38693 const char *id = "num";
38694 cp_lexer *lexer = parser->lexer;
38695 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38697 if (kind == OMP_CLAUSE_VECTOR)
38698 id = "length";
38700 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38702 matching_parens parens;
38703 parens.consume_open (parser);
38707 cp_token *next = cp_lexer_peek_token (lexer);
38708 int idx = 0;
38710 /* Gang static argument. */
38711 if (kind == OMP_CLAUSE_GANG
38712 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38714 cp_lexer_consume_token (lexer);
38716 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38717 goto cleanup_error;
38719 idx = 1;
38720 if (ops[idx] != NULL)
38722 cp_parser_error (parser, "too many %<static%> arguments");
38723 goto cleanup_error;
38726 /* Check for the '*' argument. */
38727 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38728 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38729 || cp_lexer_nth_token_is (parser->lexer, 2,
38730 CPP_CLOSE_PAREN)))
38732 cp_lexer_consume_token (lexer);
38733 ops[idx] = integer_minus_one_node;
38735 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38737 cp_lexer_consume_token (lexer);
38738 continue;
38740 else break;
38743 /* Worker num: argument and vector length: arguments. */
38744 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38745 && id_equal (next->u.value, id)
38746 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38748 cp_lexer_consume_token (lexer); /* id */
38749 cp_lexer_consume_token (lexer); /* ':' */
38752 /* Now collect the actual argument. */
38753 if (ops[idx] != NULL_TREE)
38755 cp_parser_error (parser, "unexpected argument");
38756 goto cleanup_error;
38759 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38760 false);
38761 if (expr == error_mark_node)
38762 goto cleanup_error;
38764 mark_exp_read (expr);
38765 ops[idx] = expr;
38767 if (kind == OMP_CLAUSE_GANG
38768 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38770 cp_lexer_consume_token (lexer);
38771 continue;
38773 break;
38775 while (1);
38777 if (!parens.require_close (parser))
38778 goto cleanup_error;
38781 check_no_duplicate_clause (list, kind, str, loc);
38783 c = build_omp_clause (loc, kind);
38785 if (ops[1])
38786 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38788 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38789 OMP_CLAUSE_CHAIN (c) = list;
38791 return c;
38793 cleanup_error:
38794 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38795 return list;
38798 /* OpenACC 2.0:
38799 tile ( size-expr-list ) */
38801 static tree
38802 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38804 tree c, expr = error_mark_node;
38805 tree tile = NULL_TREE;
38807 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38808 so, but the spec authors never considered such a case and have
38809 differing opinions on what it might mean, including 'not
38810 allowed'.) */
38811 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38812 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38813 clause_loc);
38815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38816 return list;
38820 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38821 return list;
38823 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38824 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38825 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38827 cp_lexer_consume_token (parser->lexer);
38828 expr = integer_zero_node;
38830 else
38831 expr = cp_parser_constant_expression (parser);
38833 tile = tree_cons (NULL_TREE, expr, tile);
38835 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38837 /* Consume the trailing ')'. */
38838 cp_lexer_consume_token (parser->lexer);
38840 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38841 tile = nreverse (tile);
38842 OMP_CLAUSE_TILE_LIST (c) = tile;
38843 OMP_CLAUSE_CHAIN (c) = list;
38844 return c;
38847 /* OpenACC 2.0
38848 Parse wait clause or directive parameters. */
38850 static tree
38851 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38853 vec<tree, va_gc> *args;
38854 tree t, args_tree;
38856 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38857 /*cast_p=*/false,
38858 /*allow_expansion_p=*/true,
38859 /*non_constant_p=*/NULL);
38861 if (args == NULL || args->length () == 0)
38863 if (args != NULL)
38865 cp_parser_error (parser, "expected integer expression list");
38866 release_tree_vector (args);
38868 return list;
38871 args_tree = build_tree_list_vec (args);
38873 release_tree_vector (args);
38875 for (t = args_tree; t; t = TREE_CHAIN (t))
38877 tree targ = TREE_VALUE (t);
38879 if (targ != error_mark_node)
38881 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38882 error ("%<wait%> expression must be integral");
38883 else
38885 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38887 targ = mark_rvalue_use (targ);
38888 OMP_CLAUSE_DECL (c) = targ;
38889 OMP_CLAUSE_CHAIN (c) = list;
38890 list = c;
38895 return list;
38898 /* OpenACC:
38899 wait [( int-expr-list )] */
38901 static tree
38902 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38904 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38906 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38907 list = cp_parser_oacc_wait_list (parser, location, list);
38908 else
38910 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38912 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38913 OMP_CLAUSE_CHAIN (c) = list;
38914 list = c;
38917 return list;
38920 /* OpenMP 3.0:
38921 collapse ( constant-expression ) */
38923 static tree
38924 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38926 tree c, num;
38927 location_t loc;
38928 HOST_WIDE_INT n;
38930 loc = cp_lexer_peek_token (parser->lexer)->location;
38931 matching_parens parens;
38932 if (!parens.require_open (parser))
38933 return list;
38935 num = cp_parser_constant_expression (parser);
38937 if (!parens.require_close (parser))
38938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38939 /*or_comma=*/false,
38940 /*consume_paren=*/true);
38942 if (num == error_mark_node)
38943 return list;
38944 num = fold_non_dependent_expr (num);
38945 if (!tree_fits_shwi_p (num)
38946 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38947 || (n = tree_to_shwi (num)) <= 0
38948 || (int) n != n)
38950 error_at (loc, "collapse argument needs positive constant integer expression");
38951 return list;
38954 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38955 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38956 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38957 OMP_CLAUSE_CHAIN (c) = list;
38958 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38960 return c;
38963 /* OpenMP 2.5:
38964 default ( none | shared )
38966 OpenMP 5.1:
38967 default ( private | firstprivate )
38969 OpenACC:
38970 default ( none | present ) */
38972 static tree
38973 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38974 location_t location, bool is_oacc)
38976 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38977 tree c;
38979 matching_parens parens;
38980 if (!parens.require_open (parser))
38981 return list;
38982 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38984 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38985 cp_lexer_consume_token (parser->lexer);
38987 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38990 const char *p = IDENTIFIER_POINTER (id);
38992 switch (p[0])
38994 case 'n':
38995 if (strcmp ("none", p) != 0)
38996 goto invalid_kind;
38997 kind = OMP_CLAUSE_DEFAULT_NONE;
38998 break;
39000 case 'p':
39001 if (strcmp ("present", p) != 0 || !is_oacc)
39002 goto invalid_kind;
39003 kind = OMP_CLAUSE_DEFAULT_PRESENT;
39004 break;
39006 case 'f':
39007 if (strcmp ("firstprivate", p) != 0 || is_oacc)
39008 goto invalid_kind;
39009 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
39010 break;
39012 case 's':
39013 if (strcmp ("shared", p) != 0 || is_oacc)
39014 goto invalid_kind;
39015 kind = OMP_CLAUSE_DEFAULT_SHARED;
39016 break;
39018 default:
39019 goto invalid_kind;
39022 cp_lexer_consume_token (parser->lexer);
39024 else
39026 invalid_kind:
39027 if (is_oacc)
39028 cp_parser_error (parser, "expected %<none%> or %<present%>");
39029 else
39030 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
39031 "%<private%> or %<firstprivate%>");
39034 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
39035 || !parens.require_close (parser))
39036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39037 /*or_comma=*/false,
39038 /*consume_paren=*/true);
39040 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
39041 return list;
39043 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
39044 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
39045 OMP_CLAUSE_CHAIN (c) = list;
39046 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
39048 return c;
39051 /* OpenMP 3.1:
39052 final ( expression ) */
39054 static tree
39055 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
39057 tree t, c;
39059 matching_parens parens;
39060 if (!parens.require_open (parser))
39061 return list;
39063 t = cp_parser_assignment_expression (parser);
39065 if (t == error_mark_node
39066 || !parens.require_close (parser))
39067 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39068 /*or_comma=*/false,
39069 /*consume_paren=*/true);
39071 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
39073 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
39074 OMP_CLAUSE_FINAL_EXPR (c) = t;
39075 OMP_CLAUSE_CHAIN (c) = list;
39077 return c;
39080 /* OpenMP 5.1:
39081 indirect [( expression )]
39084 static tree
39085 cp_parser_omp_clause_indirect (cp_parser *parser, tree list,
39086 location_t location)
39088 tree t;
39090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39092 matching_parens parens;
39093 if (!parens.require_open (parser))
39094 return list;
39096 bool non_constant_p;
39097 t = cp_parser_constant_expression (parser, true, &non_constant_p);
39099 if (t != error_mark_node && non_constant_p)
39100 error_at (location, "expected constant logical expression");
39102 if (t == error_mark_node
39103 || !parens.require_close (parser))
39104 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39105 /*or_comma=*/false,
39106 /*consume_paren=*/true);
39108 else
39109 t = integer_one_node;
39111 check_no_duplicate_clause (list, OMP_CLAUSE_INDIRECT, "indirect", location);
39113 tree c = build_omp_clause (location, OMP_CLAUSE_INDIRECT);
39114 OMP_CLAUSE_INDIRECT_EXPR (c) = t;
39115 OMP_CLAUSE_CHAIN (c) = list;
39117 return c;
39120 /* OpenMP 2.5:
39121 if ( expression )
39123 OpenMP 4.5:
39124 if ( directive-name-modifier : expression )
39126 directive-name-modifier:
39127 parallel | task | taskloop | target data | target | target update
39128 | target enter data | target exit data
39130 OpenMP 5.0:
39131 directive-name-modifier:
39132 ... | simd | cancel */
39134 static tree
39135 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
39136 bool is_omp)
39138 tree t, c;
39139 enum tree_code if_modifier = ERROR_MARK;
39141 matching_parens parens;
39142 if (!parens.require_open (parser))
39143 return list;
39145 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39148 const char *p = IDENTIFIER_POINTER (id);
39149 int n = 2;
39151 if (strcmp ("cancel", p) == 0)
39152 if_modifier = VOID_CST;
39153 else if (strcmp ("parallel", p) == 0)
39154 if_modifier = OMP_PARALLEL;
39155 else if (strcmp ("simd", p) == 0)
39156 if_modifier = OMP_SIMD;
39157 else if (strcmp ("task", p) == 0)
39158 if_modifier = OMP_TASK;
39159 else if (strcmp ("taskloop", p) == 0)
39160 if_modifier = OMP_TASKLOOP;
39161 else if (strcmp ("target", p) == 0)
39163 if_modifier = OMP_TARGET;
39164 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39166 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
39167 p = IDENTIFIER_POINTER (id);
39168 if (strcmp ("data", p) == 0)
39169 if_modifier = OMP_TARGET_DATA;
39170 else if (strcmp ("update", p) == 0)
39171 if_modifier = OMP_TARGET_UPDATE;
39172 else if (strcmp ("enter", p) == 0)
39173 if_modifier = OMP_TARGET_ENTER_DATA;
39174 else if (strcmp ("exit", p) == 0)
39175 if_modifier = OMP_TARGET_EXIT_DATA;
39176 if (if_modifier != OMP_TARGET)
39177 n = 3;
39178 else
39180 location_t loc
39181 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
39182 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
39183 "or %<exit%>");
39184 if_modifier = ERROR_MARK;
39186 if (if_modifier == OMP_TARGET_ENTER_DATA
39187 || if_modifier == OMP_TARGET_EXIT_DATA)
39189 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
39191 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39192 p = IDENTIFIER_POINTER (id);
39193 if (strcmp ("data", p) == 0)
39194 n = 4;
39196 if (n != 4)
39198 location_t loc
39199 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
39200 error_at (loc, "expected %<data%>");
39201 if_modifier = ERROR_MARK;
39206 if (if_modifier != ERROR_MARK)
39208 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
39210 while (n-- > 0)
39211 cp_lexer_consume_token (parser->lexer);
39213 else
39215 if (n > 2)
39217 location_t loc
39218 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
39219 error_at (loc, "expected %<:%>");
39221 if_modifier = ERROR_MARK;
39226 t = cp_parser_assignment_expression (parser);
39228 if (t == error_mark_node
39229 || !parens.require_close (parser))
39230 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39231 /*or_comma=*/false,
39232 /*consume_paren=*/true);
39234 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39235 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
39237 if (if_modifier != ERROR_MARK
39238 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39240 const char *p = NULL;
39241 switch (if_modifier)
39243 case VOID_CST: p = "cancel"; break;
39244 case OMP_PARALLEL: p = "parallel"; break;
39245 case OMP_SIMD: p = "simd"; break;
39246 case OMP_TASK: p = "task"; break;
39247 case OMP_TASKLOOP: p = "taskloop"; break;
39248 case OMP_TARGET_DATA: p = "target data"; break;
39249 case OMP_TARGET: p = "target"; break;
39250 case OMP_TARGET_UPDATE: p = "target update"; break;
39251 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
39252 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
39253 default: gcc_unreachable ();
39255 error_at (location, "too many %<if%> clauses with %qs modifier",
39257 return list;
39259 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39261 if (!is_omp)
39262 error_at (location, "too many %<if%> clauses");
39263 else
39264 error_at (location, "too many %<if%> clauses without modifier");
39265 return list;
39267 else if (if_modifier == ERROR_MARK
39268 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
39270 error_at (location, "if any %<if%> clause has modifier, then all "
39271 "%<if%> clauses have to use modifier");
39272 return list;
39276 c = build_omp_clause (location, OMP_CLAUSE_IF);
39277 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
39278 OMP_CLAUSE_IF_EXPR (c) = t;
39279 OMP_CLAUSE_CHAIN (c) = list;
39281 return c;
39284 /* OpenMP 3.1:
39285 mergeable */
39287 static tree
39288 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
39289 tree list, location_t location)
39291 tree c;
39293 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
39294 location);
39296 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
39297 OMP_CLAUSE_CHAIN (c) = list;
39298 return c;
39301 /* OpenMP 2.5:
39302 nowait */
39304 static tree
39305 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
39306 tree list, location_t location)
39308 tree c;
39310 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
39312 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
39313 OMP_CLAUSE_CHAIN (c) = list;
39314 return c;
39317 /* OpenMP 2.5:
39318 num_threads ( expression ) */
39320 static tree
39321 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
39322 location_t location)
39324 tree t, c;
39326 matching_parens parens;
39327 if (!parens.require_open (parser))
39328 return list;
39330 t = cp_parser_assignment_expression (parser);
39332 if (t == error_mark_node
39333 || !parens.require_close (parser))
39334 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39335 /*or_comma=*/false,
39336 /*consume_paren=*/true);
39338 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
39339 "num_threads", location);
39341 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
39342 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
39343 OMP_CLAUSE_CHAIN (c) = list;
39345 return c;
39348 /* OpenMP 4.5:
39349 num_tasks ( expression )
39351 OpenMP 5.1:
39352 num_tasks ( strict : expression ) */
39354 static tree
39355 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
39356 location_t location)
39358 tree t, c;
39360 matching_parens parens;
39361 if (!parens.require_open (parser))
39362 return list;
39364 bool strict = false;
39365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39366 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39368 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39369 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39371 strict = true;
39372 cp_lexer_consume_token (parser->lexer);
39373 cp_lexer_consume_token (parser->lexer);
39377 t = cp_parser_assignment_expression (parser);
39379 if (t == error_mark_node
39380 || !parens.require_close (parser))
39381 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39382 /*or_comma=*/false,
39383 /*consume_paren=*/true);
39385 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
39386 "num_tasks", location);
39388 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
39389 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
39390 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
39391 OMP_CLAUSE_CHAIN (c) = list;
39393 return c;
39396 /* OpenMP 4.5:
39397 grainsize ( expression )
39399 OpenMP 5.1:
39400 grainsize ( strict : expression ) */
39402 static tree
39403 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
39404 location_t location)
39406 tree t, c;
39408 matching_parens parens;
39409 if (!parens.require_open (parser))
39410 return list;
39412 bool strict = false;
39413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39414 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39416 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39417 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39419 strict = true;
39420 cp_lexer_consume_token (parser->lexer);
39421 cp_lexer_consume_token (parser->lexer);
39425 t = cp_parser_assignment_expression (parser);
39427 if (t == error_mark_node
39428 || !parens.require_close (parser))
39429 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39430 /*or_comma=*/false,
39431 /*consume_paren=*/true);
39433 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
39434 "grainsize", location);
39436 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
39437 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
39438 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
39439 OMP_CLAUSE_CHAIN (c) = list;
39441 return c;
39444 /* OpenMP 4.5:
39445 priority ( expression ) */
39447 static tree
39448 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
39449 location_t location)
39451 tree t, c;
39453 matching_parens parens;
39454 if (!parens.require_open (parser))
39455 return list;
39457 t = cp_parser_assignment_expression (parser);
39459 if (t == error_mark_node
39460 || !parens.require_close (parser))
39461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39462 /*or_comma=*/false,
39463 /*consume_paren=*/true);
39465 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
39466 "priority", location);
39468 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
39469 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
39470 OMP_CLAUSE_CHAIN (c) = list;
39472 return c;
39475 /* OpenMP 4.5:
39476 hint ( expression ) */
39478 static tree
39479 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
39481 tree t, c;
39483 matching_parens parens;
39484 if (!parens.require_open (parser))
39485 return list;
39487 t = cp_parser_assignment_expression (parser);
39489 if (t != error_mark_node)
39491 t = fold_non_dependent_expr (t);
39492 if (!value_dependent_expression_p (t)
39493 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
39494 || !tree_fits_shwi_p (t)
39495 || tree_int_cst_sgn (t) == -1))
39496 error_at (location, "expected constant integer expression with "
39497 "valid sync-hint value");
39499 if (t == error_mark_node
39500 || !parens.require_close (parser))
39501 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39502 /*or_comma=*/false,
39503 /*consume_paren=*/true);
39504 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
39506 c = build_omp_clause (location, OMP_CLAUSE_HINT);
39507 OMP_CLAUSE_HINT_EXPR (c) = t;
39508 OMP_CLAUSE_CHAIN (c) = list;
39510 return c;
39513 /* OpenMP 5.1:
39514 filter ( integer-expression ) */
39516 static tree
39517 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
39519 tree t, c;
39521 matching_parens parens;
39522 if (!parens.require_open (parser))
39523 return list;
39525 t = cp_parser_assignment_expression (parser);
39527 if (t == error_mark_node
39528 || !parens.require_close (parser))
39529 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39530 /*or_comma=*/false,
39531 /*consume_paren=*/true);
39532 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
39534 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
39535 OMP_CLAUSE_FILTER_EXPR (c) = t;
39536 OMP_CLAUSE_CHAIN (c) = list;
39538 return c;
39541 /* OpenMP 4.5:
39542 defaultmap ( tofrom : scalar )
39544 OpenMP 5.0:
39545 defaultmap ( implicit-behavior [ : variable-category ] ) */
39547 static tree
39548 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
39549 location_t location)
39551 tree c, id;
39552 const char *p;
39553 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39554 enum omp_clause_defaultmap_kind category
39555 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
39557 matching_parens parens;
39558 if (!parens.require_open (parser))
39559 return list;
39561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
39562 p = "default";
39563 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39565 invalid_behavior:
39566 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
39567 "%<tofrom%>, %<firstprivate%>, %<none%> "
39568 "or %<default%>");
39569 goto out_err;
39571 else
39573 id = cp_lexer_peek_token (parser->lexer)->u.value;
39574 p = IDENTIFIER_POINTER (id);
39577 switch (p[0])
39579 case 'a':
39580 if (strcmp ("alloc", p) == 0)
39581 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39582 else
39583 goto invalid_behavior;
39584 break;
39586 case 'd':
39587 if (strcmp ("default", p) == 0)
39588 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39589 else
39590 goto invalid_behavior;
39591 break;
39593 case 'f':
39594 if (strcmp ("firstprivate", p) == 0)
39595 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39596 else if (strcmp ("from", p) == 0)
39597 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39598 else
39599 goto invalid_behavior;
39600 break;
39602 case 'n':
39603 if (strcmp ("none", p) == 0)
39604 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39605 else
39606 goto invalid_behavior;
39607 break;
39609 case 'p':
39610 if (strcmp ("present", p) == 0)
39611 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39612 else
39613 goto invalid_behavior;
39614 break;
39616 case 't':
39617 if (strcmp ("tofrom", p) == 0)
39618 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39619 else if (strcmp ("to", p) == 0)
39620 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39621 else
39622 goto invalid_behavior;
39623 break;
39625 default:
39626 goto invalid_behavior;
39628 cp_lexer_consume_token (parser->lexer);
39630 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39632 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39633 goto out_err;
39635 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39637 invalid_category:
39638 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39639 "%<all%>");
39640 goto out_err;
39642 id = cp_lexer_peek_token (parser->lexer)->u.value;
39643 p = IDENTIFIER_POINTER (id);
39645 switch (p[0])
39647 case 'a':
39648 if (strcmp ("aggregate", p) == 0)
39649 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39650 else if (strcmp ("all", p) == 0)
39651 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39652 else
39653 goto invalid_category;
39654 break;
39656 case 'p':
39657 if (strcmp ("pointer", p) == 0)
39658 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39659 else
39660 goto invalid_category;
39661 break;
39663 case 's':
39664 if (strcmp ("scalar", p) == 0)
39665 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39666 else
39667 goto invalid_category;
39668 break;
39670 default:
39671 goto invalid_category;
39674 cp_lexer_consume_token (parser->lexer);
39676 if (!parens.require_close (parser))
39677 goto out_err;
39679 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39680 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39681 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39682 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39683 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39684 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39685 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39686 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39687 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39689 enum omp_clause_defaultmap_kind cat = category;
39690 location_t loc = OMP_CLAUSE_LOCATION (c);
39691 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39692 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39693 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39694 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39695 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39696 p = NULL;
39697 switch (cat)
39699 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39700 p = NULL;
39701 break;
39702 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39703 p = "all";
39704 break;
39705 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39706 p = "aggregate";
39707 break;
39708 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39709 p = "pointer";
39710 break;
39711 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39712 p = "scalar";
39713 break;
39714 default:
39715 gcc_unreachable ();
39717 if (p)
39718 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39720 else
39721 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39722 "category");
39723 break;
39726 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39727 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39728 OMP_CLAUSE_CHAIN (c) = list;
39729 return c;
39731 out_err:
39732 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39733 /*or_comma=*/false,
39734 /*consume_paren=*/true);
39735 return list;
39738 /* OpenMP 5.0:
39739 order ( concurrent )
39741 OpenMP 5.1:
39742 order ( order-modifier : concurrent )
39744 order-modifier:
39745 reproducible
39746 unconstrained */
39748 static tree
39749 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39751 tree c, id;
39752 const char *p;
39753 bool unconstrained = false;
39754 bool reproducible = false;
39756 matching_parens parens;
39757 if (!parens.require_open (parser))
39758 return list;
39760 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39761 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39763 id = cp_lexer_peek_token (parser->lexer)->u.value;
39764 p = IDENTIFIER_POINTER (id);
39765 if (strcmp (p, "unconstrained") == 0)
39766 unconstrained = true;
39767 else if (strcmp (p, "reproducible") == 0)
39768 reproducible = true;
39769 else
39771 cp_parser_error (parser, "expected %<reproducible%> or "
39772 "%<unconstrained%>");
39773 goto out_err;
39775 cp_lexer_consume_token (parser->lexer);
39776 cp_lexer_consume_token (parser->lexer);
39778 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39780 cp_parser_error (parser, "expected %<concurrent%>");
39781 goto out_err;
39783 else
39785 id = cp_lexer_peek_token (parser->lexer)->u.value;
39786 p = IDENTIFIER_POINTER (id);
39788 if (strcmp (p, "concurrent") != 0)
39790 cp_parser_error (parser, "expected %<concurrent%>");
39791 goto out_err;
39793 cp_lexer_consume_token (parser->lexer);
39794 if (!parens.require_close (parser))
39795 goto out_err;
39797 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39798 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39799 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39800 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39801 OMP_CLAUSE_CHAIN (c) = list;
39802 return c;
39804 out_err:
39805 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39806 /*or_comma=*/false,
39807 /*consume_paren=*/true);
39808 return list;
39811 /* OpenMP 5.0:
39812 bind ( teams | parallel | thread ) */
39814 static tree
39815 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39816 location_t location)
39818 tree c;
39819 const char *p;
39820 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39822 matching_parens parens;
39823 if (!parens.require_open (parser))
39824 return list;
39826 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39828 invalid:
39829 cp_parser_error (parser,
39830 "expected %<teams%>, %<parallel%> or %<thread%>");
39831 goto out_err;
39833 else
39835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39836 p = IDENTIFIER_POINTER (id);
39838 if (strcmp (p, "teams") == 0)
39839 kind = OMP_CLAUSE_BIND_TEAMS;
39840 else if (strcmp (p, "parallel") == 0)
39841 kind = OMP_CLAUSE_BIND_PARALLEL;
39842 else if (strcmp (p, "thread") != 0)
39843 goto invalid;
39844 cp_lexer_consume_token (parser->lexer);
39845 if (!parens.require_close (parser))
39846 goto out_err;
39848 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39849 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39850 OMP_CLAUSE_BIND_KIND (c) = kind;
39851 OMP_CLAUSE_CHAIN (c) = list;
39852 return c;
39854 out_err:
39855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39856 /*or_comma=*/false,
39857 /*consume_paren=*/true);
39858 return list;
39861 /* OpenMP 2.5:
39862 ordered
39864 OpenMP 4.5:
39865 ordered ( constant-expression ) */
39867 static tree
39868 cp_parser_omp_clause_ordered (cp_parser *parser,
39869 tree list, location_t location)
39871 tree c, num = NULL_TREE;
39872 HOST_WIDE_INT n;
39874 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39875 "ordered", location);
39877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39879 matching_parens parens;
39880 parens.consume_open (parser);
39882 num = cp_parser_constant_expression (parser);
39884 if (!parens.require_close (parser))
39885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39886 /*or_comma=*/false,
39887 /*consume_paren=*/true);
39889 if (num == error_mark_node)
39890 return list;
39891 num = fold_non_dependent_expr (num);
39892 if (!tree_fits_shwi_p (num)
39893 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39894 || (n = tree_to_shwi (num)) <= 0
39895 || (int) n != n)
39897 error_at (location,
39898 "ordered argument needs positive constant integer "
39899 "expression");
39900 return list;
39904 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39905 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39906 OMP_CLAUSE_CHAIN (c) = list;
39907 return c;
39910 /* OpenMP 2.5:
39911 reduction ( reduction-operator : variable-list )
39913 reduction-operator:
39914 One of: + * - & ^ | && ||
39916 OpenMP 3.1:
39918 reduction-operator:
39919 One of: + * - & ^ | && || min max
39921 OpenMP 4.0:
39923 reduction-operator:
39924 One of: + * - & ^ | && ||
39925 id-expression
39927 OpenMP 5.0:
39928 reduction ( reduction-modifier, reduction-operator : variable-list )
39929 in_reduction ( reduction-operator : variable-list )
39930 task_reduction ( reduction-operator : variable-list ) */
39932 static tree
39933 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39934 bool is_omp, tree list)
39936 enum tree_code code = ERROR_MARK;
39937 tree nlist, c, id = NULL_TREE;
39938 bool task = false;
39939 bool inscan = false;
39941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39942 return list;
39944 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39947 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39949 cp_lexer_consume_token (parser->lexer);
39950 cp_lexer_consume_token (parser->lexer);
39952 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39953 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39956 const char *p = IDENTIFIER_POINTER (id);
39957 if (strcmp (p, "task") == 0)
39958 task = true;
39959 else if (strcmp (p, "inscan") == 0)
39960 inscan = true;
39961 if (task || inscan)
39963 cp_lexer_consume_token (parser->lexer);
39964 cp_lexer_consume_token (parser->lexer);
39969 switch (cp_lexer_peek_token (parser->lexer)->type)
39971 case CPP_PLUS: code = PLUS_EXPR; break;
39972 case CPP_MULT: code = MULT_EXPR; break;
39973 case CPP_MINUS: code = MINUS_EXPR; break;
39974 case CPP_AND: code = BIT_AND_EXPR; break;
39975 case CPP_XOR: code = BIT_XOR_EXPR; break;
39976 case CPP_OR: code = BIT_IOR_EXPR; break;
39977 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39978 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39979 default: break;
39982 if (code != ERROR_MARK)
39983 cp_lexer_consume_token (parser->lexer);
39984 else
39986 bool saved_colon_corrects_to_scope_p;
39987 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39988 parser->colon_corrects_to_scope_p = false;
39989 id = cp_parser_id_expression (parser, /*template_p=*/false,
39990 /*check_dependency_p=*/true,
39991 /*template_p=*/NULL,
39992 /*declarator_p=*/false,
39993 /*optional_p=*/false);
39994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39995 if (identifier_p (id))
39997 const char *p = IDENTIFIER_POINTER (id);
39999 if (strcmp (p, "min") == 0)
40000 code = MIN_EXPR;
40001 else if (strcmp (p, "max") == 0)
40002 code = MAX_EXPR;
40003 else if (id == ovl_op_identifier (false, PLUS_EXPR))
40004 code = PLUS_EXPR;
40005 else if (id == ovl_op_identifier (false, MULT_EXPR))
40006 code = MULT_EXPR;
40007 else if (id == ovl_op_identifier (false, MINUS_EXPR))
40008 code = MINUS_EXPR;
40009 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
40010 code = BIT_AND_EXPR;
40011 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
40012 code = BIT_IOR_EXPR;
40013 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
40014 code = BIT_XOR_EXPR;
40015 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
40016 code = TRUTH_ANDIF_EXPR;
40017 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
40018 code = TRUTH_ORIF_EXPR;
40019 id = omp_reduction_id (code, id, NULL_TREE);
40020 tree scope = parser->scope;
40021 if (scope)
40022 id = build_qualified_name (NULL_TREE, scope, id, false);
40023 parser->scope = NULL_TREE;
40024 parser->qualifying_scope = NULL_TREE;
40025 parser->object_scope = NULL_TREE;
40027 else
40029 error ("invalid reduction-identifier");
40030 resync_fail:
40031 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40032 /*or_comma=*/false,
40033 /*consume_paren=*/true);
40034 return list;
40038 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40039 goto resync_fail;
40041 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
40042 NULL);
40043 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40045 OMP_CLAUSE_REDUCTION_CODE (c) = code;
40046 if (task)
40047 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
40048 else if (inscan)
40049 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
40050 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
40053 return nlist;
40056 /* OpenMP 2.5:
40057 schedule ( schedule-kind )
40058 schedule ( schedule-kind , expression )
40060 schedule-kind:
40061 static | dynamic | guided | runtime | auto
40063 OpenMP 4.5:
40064 schedule ( schedule-modifier : schedule-kind )
40065 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
40067 schedule-modifier:
40068 simd
40069 monotonic
40070 nonmonotonic */
40072 static tree
40073 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
40075 tree c, t;
40076 int modifiers = 0, nmodifiers = 0;
40078 matching_parens parens;
40079 if (!parens.require_open (parser))
40080 return list;
40082 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
40084 location_t comma = UNKNOWN_LOCATION;
40085 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40087 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40088 const char *p = IDENTIFIER_POINTER (id);
40089 if (strcmp ("simd", p) == 0)
40090 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
40091 else if (strcmp ("monotonic", p) == 0)
40092 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
40093 else if (strcmp ("nonmonotonic", p) == 0)
40094 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
40095 else
40096 break;
40097 comma = UNKNOWN_LOCATION;
40098 cp_lexer_consume_token (parser->lexer);
40099 if (nmodifiers++ == 0
40100 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40102 comma = cp_lexer_peek_token (parser->lexer)->location;
40103 cp_lexer_consume_token (parser->lexer);
40105 else
40107 cp_parser_require (parser, CPP_COLON, RT_COLON);
40108 break;
40111 if (comma != UNKNOWN_LOCATION)
40112 error_at (comma, "expected %<:%>");
40114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40116 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40117 const char *p = IDENTIFIER_POINTER (id);
40119 switch (p[0])
40121 case 'd':
40122 if (strcmp ("dynamic", p) != 0)
40123 goto invalid_kind;
40124 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
40125 break;
40127 case 'g':
40128 if (strcmp ("guided", p) != 0)
40129 goto invalid_kind;
40130 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
40131 break;
40133 case 'r':
40134 if (strcmp ("runtime", p) != 0)
40135 goto invalid_kind;
40136 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
40137 break;
40139 default:
40140 goto invalid_kind;
40143 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40144 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
40145 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
40146 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
40147 else
40148 goto invalid_kind;
40149 cp_lexer_consume_token (parser->lexer);
40151 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
40152 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40153 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
40154 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40156 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
40157 "specified");
40158 modifiers = 0;
40161 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40163 cp_token *token;
40164 cp_lexer_consume_token (parser->lexer);
40166 token = cp_lexer_peek_token (parser->lexer);
40167 t = cp_parser_assignment_expression (parser);
40169 if (t == error_mark_node)
40170 goto resync_fail;
40171 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
40172 error_at (token->location, "schedule %<runtime%> does not take "
40173 "a %<chunk_size%> parameter");
40174 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
40175 error_at (token->location, "schedule %<auto%> does not take "
40176 "a %<chunk_size%> parameter");
40177 else
40178 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
40180 if (!parens.require_close (parser))
40181 goto resync_fail;
40183 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40184 goto resync_fail;
40186 OMP_CLAUSE_SCHEDULE_KIND (c)
40187 = (enum omp_clause_schedule_kind)
40188 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
40190 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
40191 OMP_CLAUSE_CHAIN (c) = list;
40192 return c;
40194 invalid_kind:
40195 cp_parser_error (parser, "invalid schedule kind");
40196 resync_fail:
40197 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40198 /*or_comma=*/false,
40199 /*consume_paren=*/true);
40200 return list;
40203 /* OpenMP 3.0:
40204 untied */
40206 static tree
40207 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
40208 tree list, location_t location)
40210 tree c;
40212 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
40214 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
40215 OMP_CLAUSE_CHAIN (c) = list;
40216 return c;
40219 /* OpenMP 4.0:
40220 inbranch
40221 notinbranch */
40223 static tree
40224 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
40225 tree list, location_t location)
40227 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40228 tree c = build_omp_clause (location, code);
40229 OMP_CLAUSE_CHAIN (c) = list;
40230 return c;
40233 /* OpenMP 4.0:
40234 parallel
40236 sections
40237 taskgroup */
40239 static tree
40240 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
40241 enum omp_clause_code code,
40242 tree list, location_t location)
40244 tree c = build_omp_clause (location, code);
40245 OMP_CLAUSE_CHAIN (c) = list;
40246 return c;
40249 /* OpenMP 4.5:
40250 nogroup */
40252 static tree
40253 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
40254 tree list, location_t location)
40256 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
40257 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
40258 OMP_CLAUSE_CHAIN (c) = list;
40259 return c;
40262 /* OpenMP 4.5:
40263 simd
40264 threads */
40266 static tree
40267 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
40268 enum omp_clause_code code,
40269 tree list, location_t location)
40271 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40272 tree c = build_omp_clause (location, code);
40273 OMP_CLAUSE_CHAIN (c) = list;
40274 return c;
40277 /* OpenMP 4.0:
40278 num_teams ( expression )
40280 OpenMP 5.1:
40281 num_teams ( expression : expression ) */
40283 static tree
40284 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
40285 location_t location)
40287 tree upper, lower = NULL_TREE, c;
40289 matching_parens parens;
40290 if (!parens.require_open (parser))
40291 return list;
40293 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40294 parser->colon_corrects_to_scope_p = false;
40295 upper = cp_parser_assignment_expression (parser);
40296 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40298 if (upper != error_mark_node
40299 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40301 lower = upper;
40302 cp_lexer_consume_token (parser->lexer);
40303 upper = cp_parser_assignment_expression (parser);
40306 if (upper == error_mark_node
40307 || !parens.require_close (parser))
40308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40309 /*or_comma=*/false,
40310 /*consume_paren=*/true);
40312 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
40313 "num_teams", location);
40315 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
40316 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
40317 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
40318 OMP_CLAUSE_CHAIN (c) = list;
40320 return c;
40323 /* OpenMP 4.0:
40324 thread_limit ( expression ) */
40326 static tree
40327 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
40328 location_t location)
40330 tree t, c;
40332 matching_parens parens;
40333 if (!parens.require_open (parser))
40334 return list;
40336 t = cp_parser_assignment_expression (parser);
40338 if (t == error_mark_node
40339 || !parens.require_close (parser))
40340 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40341 /*or_comma=*/false,
40342 /*consume_paren=*/true);
40344 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
40345 "thread_limit", location);
40347 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
40348 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
40349 OMP_CLAUSE_CHAIN (c) = list;
40351 return c;
40354 /* OpenMP 4.0:
40355 aligned ( variable-list )
40356 aligned ( variable-list : constant-expression ) */
40358 static tree
40359 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
40361 tree nlist, c, alignment = NULL_TREE;
40362 bool colon;
40364 matching_parens parens;
40365 if (!parens.require_open (parser))
40366 return list;
40368 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
40369 &colon);
40371 if (colon)
40373 alignment = cp_parser_constant_expression (parser);
40375 if (!parens.require_close (parser))
40376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40377 /*or_comma=*/false,
40378 /*consume_paren=*/true);
40380 if (alignment == error_mark_node)
40381 alignment = NULL_TREE;
40384 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40385 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
40387 return nlist;
40390 /* OpenMP 5.0:
40391 allocate ( variable-list )
40392 allocate ( expression : variable-list )
40394 OpenMP 5.1:
40395 allocate ( allocator-modifier : variable-list )
40396 allocate ( allocator-modifier , allocator-modifier : variable-list )
40398 allocator-modifier:
40399 allocator ( expression )
40400 align ( expression ) */
40402 static tree
40403 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
40405 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
40406 bool colon, has_modifiers = false;
40408 matching_parens parens;
40409 if (!parens.require_open (parser))
40410 return list;
40412 cp_parser_parse_tentatively (parser);
40413 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40414 parser->colon_corrects_to_scope_p = false;
40415 for (int mod = 0; mod < 2; mod++)
40416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40417 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40419 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40420 const char *p = IDENTIFIER_POINTER (id);
40421 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
40422 break;
40423 cp_lexer_consume_token (parser->lexer);
40424 matching_parens parens2;
40425 if (!parens2.require_open (parser))
40426 break;
40427 if (strcmp (p, "allocator") == 0)
40429 if (allocator != NULL_TREE)
40430 break;
40431 allocator = cp_parser_assignment_expression (parser);
40433 else
40435 if (align != NULL_TREE)
40436 break;
40437 align = cp_parser_assignment_expression (parser);
40439 if (!parens2.require_close (parser))
40440 break;
40441 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40443 has_modifiers = true;
40444 break;
40446 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
40447 break;
40448 cp_lexer_consume_token (parser->lexer);
40450 else
40451 break;
40452 if (!has_modifiers)
40454 cp_parser_abort_tentative_parse (parser);
40455 align = NULL_TREE;
40456 allocator = NULL_TREE;
40457 cp_parser_parse_tentatively (parser);
40458 allocator = cp_parser_assignment_expression (parser);
40460 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40461 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40463 cp_parser_parse_definitely (parser);
40464 cp_lexer_consume_token (parser->lexer);
40465 if (allocator == error_mark_node)
40466 allocator = NULL_TREE;
40467 if (align == error_mark_node)
40468 align = NULL_TREE;
40470 else
40472 cp_parser_abort_tentative_parse (parser);
40473 allocator = NULL_TREE;
40474 align = NULL_TREE;
40477 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
40478 &colon);
40480 if (allocator || align)
40481 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40483 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40484 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
40487 return nlist;
40490 /* OpenMP 2.5:
40491 lastprivate ( variable-list )
40493 OpenMP 5.0:
40494 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
40496 static tree
40497 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
40499 bool conditional = false;
40501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40502 return list;
40504 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40505 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40508 const char *p = IDENTIFIER_POINTER (id);
40510 if (strcmp ("conditional", p) == 0)
40512 conditional = true;
40513 cp_lexer_consume_token (parser->lexer);
40514 cp_lexer_consume_token (parser->lexer);
40518 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
40519 list, NULL);
40521 if (conditional)
40522 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40523 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
40524 return nlist;
40527 /* OpenMP 4.0:
40528 linear ( variable-list )
40529 linear ( variable-list : expression )
40531 OpenMP 4.5:
40532 linear ( modifier ( variable-list ) )
40533 linear ( modifier ( variable-list ) : expression )
40535 modifier:
40538 uval
40540 OpenMP 5.2:
40541 linear ( variable-list : modifiers-list )
40543 modifiers:
40546 uval
40547 step ( expression ) */
40549 static tree
40550 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
40551 bool declare_simd)
40553 tree nlist, c, step = integer_one_node;
40554 bool colon;
40555 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
40556 bool old_linear_modifier = false;
40558 matching_parens parens;
40559 if (!parens.require_open (parser))
40560 return list;
40562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40565 const char *p = IDENTIFIER_POINTER (id);
40567 if (strcmp ("ref", p) == 0)
40568 kind = OMP_CLAUSE_LINEAR_REF;
40569 else if (strcmp ("val", p) == 0)
40570 kind = OMP_CLAUSE_LINEAR_VAL;
40571 else if (strcmp ("uval", p) == 0)
40572 kind = OMP_CLAUSE_LINEAR_UVAL;
40573 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40575 cp_lexer_consume_token (parser->lexer);
40576 old_linear_modifier = true;
40578 else
40579 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40582 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40583 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40584 &colon);
40585 else
40587 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40588 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40589 if (colon)
40590 cp_parser_require (parser, CPP_COLON, RT_COLON);
40591 else if (!parens.require_close (parser))
40592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40593 /*or_comma=*/false,
40594 /*consume_paren=*/true);
40597 if (colon)
40599 bool has_modifiers = false;
40600 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40601 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40603 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40604 const char *p = IDENTIFIER_POINTER (id);
40605 size_t pos = 0;
40606 if (strcmp ("ref", p) == 0
40607 || strcmp ("val", p) == 0
40608 || strcmp ("uval", p) == 0)
40609 pos = 2;
40610 else if (strcmp ("step", p) == 0
40611 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40613 pos = cp_parser_skip_balanced_tokens (parser, 2);
40614 if (pos == 2)
40615 pos = 0;
40617 if (pos != 0
40618 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
40619 || cp_lexer_nth_token_is (parser->lexer, pos,
40620 CPP_CLOSE_PAREN)))
40621 has_modifiers = true;
40624 step = NULL_TREE;
40625 if (has_modifiers)
40627 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40630 const char *p = IDENTIFIER_POINTER (id);
40631 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
40632 if (strcmp ("ref", p) == 0)
40633 nkind = OMP_CLAUSE_LINEAR_REF;
40634 else if (strcmp ("val", p) == 0)
40635 nkind = OMP_CLAUSE_LINEAR_VAL;
40636 else if (strcmp ("uval", p) == 0)
40637 nkind = OMP_CLAUSE_LINEAR_UVAL;
40638 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
40640 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
40641 error_at (cp_lexer_peek_token (parser->lexer)->location,
40642 "multiple linear modifiers");
40643 kind = nkind;
40644 cp_lexer_consume_token (parser->lexer);
40646 else if (strcmp ("step", p) == 0)
40648 location_t step_loc
40649 = cp_lexer_peek_token (parser->lexer)->location;
40650 cp_lexer_consume_token (parser->lexer);
40651 matching_parens parens2;
40652 if (parens2.require_open (parser))
40654 if (step)
40655 error_at (step_loc, "multiple %<step%> modifiers");
40656 if (declare_simd
40657 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40658 && cp_lexer_nth_token_is (parser->lexer, 2,
40659 CPP_CLOSE_PAREN))
40661 cp_token *token
40662 = cp_lexer_peek_token (parser->lexer);
40663 location_t tok_loc = token->location;
40664 cp_parser_parse_tentatively (parser);
40665 step = cp_parser_id_expression (parser, false, true,
40666 NULL, false, false);
40667 if (step != error_mark_node)
40668 step = cp_parser_lookup_name_simple (parser, step,
40669 tok_loc);
40670 if (step == error_mark_node)
40672 step = NULL_TREE;
40673 cp_parser_abort_tentative_parse (parser);
40675 else if (!cp_parser_parse_definitely (parser))
40676 step = NULL_TREE;
40678 if (!step)
40679 step = cp_parser_assignment_expression (parser);
40680 if (!parens2.require_close (parser))
40681 cp_parser_skip_to_closing_parenthesis (parser, true,
40682 false, true);
40684 else
40685 break;
40687 else
40688 break;
40689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40691 cp_lexer_consume_token (parser->lexer);
40692 continue;
40694 break;
40696 if (!step)
40697 step = integer_one_node;
40699 else if (declare_simd
40700 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40701 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40703 cp_token *token = cp_lexer_peek_token (parser->lexer);
40704 cp_parser_parse_tentatively (parser);
40705 step = cp_parser_id_expression (parser, /*template_p=*/false,
40706 /*check_dependency_p=*/true,
40707 /*template_p=*/NULL,
40708 /*declarator_p=*/false,
40709 /*optional_p=*/false);
40710 if (step != error_mark_node)
40711 step = cp_parser_lookup_name_simple (parser, step, token->location);
40712 if (step == error_mark_node)
40714 step = NULL_TREE;
40715 cp_parser_abort_tentative_parse (parser);
40717 else if (!cp_parser_parse_definitely (parser))
40718 step = NULL_TREE;
40720 if (!step)
40721 step = cp_parser_assignment_expression (parser);
40723 if (!parens.require_close (parser))
40724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40725 /*or_comma=*/false,
40726 /*consume_paren=*/true);
40728 if (step == error_mark_node)
40729 return list;
40732 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40734 OMP_CLAUSE_LINEAR_STEP (c) = step;
40735 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40736 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40739 return nlist;
40742 /* OpenMP 4.0:
40743 safelen ( constant-expression ) */
40745 static tree
40746 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40747 location_t location)
40749 tree t, c;
40751 matching_parens parens;
40752 if (!parens.require_open (parser))
40753 return list;
40755 t = cp_parser_constant_expression (parser);
40757 if (t == error_mark_node
40758 || !parens.require_close (parser))
40759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40760 /*or_comma=*/false,
40761 /*consume_paren=*/true);
40763 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40765 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40766 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40767 OMP_CLAUSE_CHAIN (c) = list;
40769 return c;
40772 /* OpenMP 4.0:
40773 simdlen ( constant-expression ) */
40775 static tree
40776 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40777 location_t location)
40779 tree t, c;
40781 matching_parens parens;
40782 if (!parens.require_open (parser))
40783 return list;
40785 t = cp_parser_constant_expression (parser);
40787 if (t == error_mark_node
40788 || !parens.require_close (parser))
40789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40790 /*or_comma=*/false,
40791 /*consume_paren=*/true);
40793 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40795 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40796 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40797 OMP_CLAUSE_CHAIN (c) = list;
40799 return c;
40802 /* OpenMP 4.5:
40803 vec:
40804 identifier [+/- integer]
40805 vec , identifier [+/- integer]
40808 static tree
40809 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40810 tree list, bool depend_p)
40812 tree vec = NULL;
40814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40816 cp_parser_error (parser, "expected identifier");
40817 return list;
40820 if (!depend_p)
40822 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40823 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40824 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40825 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40826 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40828 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40829 if (integer_onep (val))
40831 cp_lexer_consume_token (parser->lexer);
40832 cp_lexer_consume_token (parser->lexer);
40833 cp_lexer_consume_token (parser->lexer);
40834 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40835 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40836 OMP_CLAUSE_CHAIN (u) = list;
40837 return u;
40842 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40844 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40845 tree t, identifier = cp_parser_identifier (parser);
40846 tree addend = NULL;
40848 if (identifier == error_mark_node)
40849 t = error_mark_node;
40850 else
40852 t = cp_parser_lookup_name_simple
40853 (parser, identifier,
40854 cp_lexer_peek_token (parser->lexer)->location);
40855 if (t == error_mark_node)
40856 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40857 id_loc);
40860 bool neg = false;
40861 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40862 neg = true;
40863 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40865 addend = integer_zero_node;
40866 goto add_to_vector;
40868 cp_lexer_consume_token (parser->lexer);
40870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40872 cp_parser_error (parser, "expected integer");
40873 return list;
40876 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40877 if (TREE_CODE (addend) != INTEGER_CST)
40879 cp_parser_error (parser, "expected integer");
40880 return list;
40882 cp_lexer_consume_token (parser->lexer);
40884 add_to_vector:
40885 if (t != error_mark_node)
40887 vec = tree_cons (addend, t, vec);
40888 if (neg)
40889 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40893 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40894 break;
40896 cp_lexer_consume_token (parser->lexer);
40899 if (vec)
40901 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40902 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40903 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40904 OMP_CLAUSE_DECL (u) = nreverse (vec);
40905 OMP_CLAUSE_CHAIN (u) = list;
40906 return u;
40908 return list;
40911 /* OpenMP 5.0:
40912 detach ( event-handle ) */
40914 static tree
40915 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40917 matching_parens parens;
40919 if (!parens.require_open (parser))
40920 return list;
40922 cp_token *token;
40923 tree name, decl;
40925 token = cp_lexer_peek_token (parser->lexer);
40926 name = cp_parser_id_expression (parser, /*template_p=*/false,
40927 /*check_dependency_p=*/true,
40928 /*template_p=*/NULL,
40929 /*declarator_p=*/false,
40930 /*optional_p=*/false);
40931 if (name == error_mark_node)
40932 decl = error_mark_node;
40933 else
40935 if (identifier_p (name))
40936 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40937 else
40938 decl = name;
40939 if (decl == error_mark_node)
40940 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40941 token->location);
40944 if (decl == error_mark_node
40945 || !parens.require_close (parser))
40946 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40947 /*or_comma=*/false,
40948 /*consume_paren=*/true);
40950 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40951 OMP_CLAUSE_DECL (u) = decl;
40952 OMP_CLAUSE_CHAIN (u) = list;
40954 return u;
40957 /* OpenMP 5.0:
40958 iterators ( iterators-definition )
40960 iterators-definition:
40961 iterator-specifier
40962 iterator-specifier , iterators-definition
40964 iterator-specifier:
40965 identifier = range-specification
40966 iterator-type identifier = range-specification
40968 range-specification:
40969 begin : end
40970 begin : end : step */
40972 static tree
40973 cp_parser_omp_iterators (cp_parser *parser)
40975 tree ret = NULL_TREE, *last = &ret;
40976 cp_lexer_consume_token (parser->lexer);
40978 matching_parens parens;
40979 if (!parens.require_open (parser))
40980 return error_mark_node;
40982 bool saved_colon_corrects_to_scope_p
40983 = parser->colon_corrects_to_scope_p;
40984 bool saved_colon_doesnt_start_class_def_p
40985 = parser->colon_doesnt_start_class_def_p;
40989 tree iter_type;
40990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40991 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40992 iter_type = integer_type_node;
40993 else
40995 const char *saved_message
40996 = parser->type_definition_forbidden_message;
40997 parser->type_definition_forbidden_message
40998 = G_("types may not be defined in iterator type");
41000 iter_type = cp_parser_type_id (parser);
41002 parser->type_definition_forbidden_message = saved_message;
41005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41008 cp_parser_error (parser, "expected identifier");
41009 break;
41012 tree id = cp_parser_identifier (parser);
41013 if (id == error_mark_node)
41014 break;
41016 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41017 break;
41019 parser->colon_corrects_to_scope_p = false;
41020 parser->colon_doesnt_start_class_def_p = true;
41021 tree begin = cp_parser_assignment_expression (parser);
41023 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41024 break;
41026 tree end = cp_parser_assignment_expression (parser);
41028 tree step = integer_one_node;
41029 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41031 cp_lexer_consume_token (parser->lexer);
41032 step = cp_parser_assignment_expression (parser);
41035 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
41036 DECL_ARTIFICIAL (iter_var) = 1;
41037 DECL_CONTEXT (iter_var) = current_function_decl;
41038 pushdecl (iter_var);
41040 *last = make_tree_vec (6);
41041 TREE_VEC_ELT (*last, 0) = iter_var;
41042 TREE_VEC_ELT (*last, 1) = begin;
41043 TREE_VEC_ELT (*last, 2) = end;
41044 TREE_VEC_ELT (*last, 3) = step;
41045 last = &TREE_CHAIN (*last);
41047 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41049 cp_lexer_consume_token (parser->lexer);
41050 continue;
41052 break;
41054 while (1);
41056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41057 parser->colon_doesnt_start_class_def_p
41058 = saved_colon_doesnt_start_class_def_p;
41060 if (!parens.require_close (parser))
41061 cp_parser_skip_to_closing_parenthesis (parser,
41062 /*recovering=*/true,
41063 /*or_comma=*/false,
41064 /*consume_paren=*/true);
41066 return ret ? ret : error_mark_node;
41069 /* OpenMP 5.0:
41070 affinity ( [aff-modifier :] variable-list )
41071 aff-modifier:
41072 iterator ( iterators-definition ) */
41074 static tree
41075 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
41077 tree nlist, c, iterators = NULL_TREE;
41079 matching_parens parens;
41080 if (!parens.require_open (parser))
41081 return list;
41083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41086 const char *p = IDENTIFIER_POINTER (id);
41087 bool parse_iter = ((strcmp ("iterator", p) == 0)
41088 && (cp_lexer_nth_token_is (parser->lexer, 2,
41089 CPP_OPEN_PAREN)));
41090 if (parse_iter)
41092 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
41093 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
41095 if (parse_iter)
41097 begin_scope (sk_omp, NULL);
41098 iterators = cp_parser_omp_iterators (parser);
41099 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41101 if (iterators)
41102 poplevel (0, 1, 0);
41103 cp_parser_skip_to_closing_parenthesis (parser,
41104 /*recovering=*/true,
41105 /*or_comma=*/false,
41106 /*consume_paren=*/true);
41107 return list;
41111 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
41112 list, NULL);
41113 if (iterators)
41115 tree block = poplevel (1, 1, 0);
41116 if (iterators != error_mark_node)
41118 TREE_VEC_ELT (iterators, 5) = block;
41119 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41120 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
41121 OMP_CLAUSE_DECL (c));
41124 return nlist;
41127 /* OpenMP 4.0:
41128 depend ( depend-kind : variable-list )
41130 depend-kind:
41131 in | out | inout
41133 OpenMP 4.5:
41134 depend ( source )
41136 depend ( sink : vec )
41138 OpenMP 5.0:
41139 depend ( depend-modifier , depend-kind: variable-list )
41141 depend-kind:
41142 in | out | inout | mutexinoutset | depobj
41144 depend-modifier:
41145 iterator ( iterators-definition ) */
41147 static tree
41148 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
41150 tree nlist, c, iterators = NULL_TREE;
41151 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
41152 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
41154 matching_parens parens;
41155 if (!parens.require_open (parser))
41156 return list;
41160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41161 goto invalid_kind;
41163 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41164 const char *p = IDENTIFIER_POINTER (id);
41166 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
41168 begin_scope (sk_omp, NULL);
41169 iterators = cp_parser_omp_iterators (parser);
41170 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
41171 continue;
41173 if (strcmp ("in", p) == 0)
41174 kind = OMP_CLAUSE_DEPEND_IN;
41175 else if (strcmp ("inout", p) == 0)
41176 kind = OMP_CLAUSE_DEPEND_INOUT;
41177 else if (strcmp ("inoutset", p) == 0)
41178 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41179 else if (strcmp ("mutexinoutset", p) == 0)
41180 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41181 else if (strcmp ("out", p) == 0)
41182 kind = OMP_CLAUSE_DEPEND_OUT;
41183 else if (strcmp ("depobj", p) == 0)
41184 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
41185 else if (strcmp ("sink", p) == 0)
41186 dkind = OMP_CLAUSE_DOACROSS_SINK;
41187 else if (strcmp ("source", p) == 0)
41188 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
41189 else
41190 goto invalid_kind;
41191 break;
41193 while (1);
41195 cp_lexer_consume_token (parser->lexer);
41197 if (iterators
41198 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
41199 || dkind == OMP_CLAUSE_DOACROSS_SINK))
41201 poplevel (0, 1, 0);
41202 error_at (loc, "%<iterator%> modifier incompatible with %qs",
41203 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
41204 iterators = NULL_TREE;
41207 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
41209 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41210 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
41211 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
41212 OMP_CLAUSE_DECL (c) = NULL_TREE;
41213 OMP_CLAUSE_CHAIN (c) = list;
41214 if (!parens.require_close (parser))
41215 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41216 /*or_comma=*/false,
41217 /*consume_paren=*/true);
41218 return c;
41221 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41222 goto resync_fail;
41224 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
41226 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
41227 if (!parens.require_close (parser))
41228 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41229 /*or_comma=*/false,
41230 /*consume_paren=*/true);
41232 else
41234 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
41235 list, NULL);
41237 if (iterators)
41239 tree block = poplevel (1, 1, 0);
41240 if (iterators == error_mark_node)
41241 iterators = NULL_TREE;
41242 else
41243 TREE_VEC_ELT (iterators, 5) = block;
41246 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41248 OMP_CLAUSE_DEPEND_KIND (c) = kind;
41249 if (iterators)
41250 OMP_CLAUSE_DECL (c)
41251 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
41254 return nlist;
41256 invalid_kind:
41257 cp_parser_error (parser, "invalid depend kind");
41258 resync_fail:
41259 if (iterators)
41260 poplevel (0, 1, 0);
41261 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41262 /*or_comma=*/false,
41263 /*consume_paren=*/true);
41264 return list;
41267 /* OpenMP 5.2:
41268 doacross ( source : )
41269 doacross ( source : omp_cur_iteration )
41271 doacross ( sink : vec )
41272 doacross ( sink : omp_cur_iteration - logical_iteration ) */
41274 static tree
41275 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
41277 tree nlist;
41278 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
41280 matching_parens parens;
41281 if (!parens.require_open (parser))
41282 return list;
41284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41286 invalid_kind:
41287 cp_parser_error (parser, "invalid doacross kind");
41288 resync_fail:
41289 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41290 /*or_comma=*/false,
41291 /*consume_paren=*/true);
41292 return list;
41295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41296 const char *p = IDENTIFIER_POINTER (id);
41298 if (strcmp ("sink", p) == 0)
41299 kind = OMP_CLAUSE_DOACROSS_SINK;
41300 else if (strcmp ("source", p) == 0)
41301 kind = OMP_CLAUSE_DOACROSS_SOURCE;
41302 else
41303 goto invalid_kind;
41305 cp_lexer_consume_token (parser->lexer);
41307 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41308 goto resync_fail;
41310 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
41312 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41314 id = cp_lexer_peek_token (parser->lexer)->u.value;
41315 p = IDENTIFIER_POINTER (id);
41316 if (strcmp (p, "omp_cur_iteration") == 0)
41317 cp_lexer_consume_token (parser->lexer);
41319 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41320 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
41321 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
41322 OMP_CLAUSE_CHAIN (nlist) = list;
41324 else
41325 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
41327 if (!parens.require_close (parser))
41328 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41329 /*or_comma=*/false,
41330 /*consume_paren=*/true);
41331 return nlist;
41334 /* OpenMP 4.0:
41335 from ( variable-list )
41336 to ( variable-list )
41338 OpenMP 5.1:
41339 from ( [present :] variable-list )
41340 to ( [present :] variable-list ) */
41342 static tree
41343 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
41344 tree list)
41346 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41347 return list;
41349 bool present = false;
41350 cp_token *token = cp_lexer_peek_token (parser->lexer);
41352 if (token->type == CPP_NAME
41353 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
41354 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41356 present = true;
41357 cp_lexer_consume_token (parser->lexer);
41358 cp_lexer_consume_token (parser->lexer);
41361 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
41362 if (present)
41363 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
41364 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
41366 return nl;
41369 /* OpenMP 4.0:
41370 map ( map-kind : variable-list )
41371 map ( variable-list )
41373 map-kind:
41374 alloc | to | from | tofrom
41376 OpenMP 4.5:
41377 map-kind:
41378 alloc | to | from | tofrom | release | delete
41380 map ( always [,] map-kind: variable-list )
41382 OpenMP 5.0:
41383 map ( [map-type-modifier[,] ...] map-kind: variable-list )
41385 map-type-modifier:
41386 always | close */
41388 static tree
41389 cp_parser_omp_clause_map (cp_parser *parser, tree list)
41391 tree nlist, c;
41392 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
41394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41395 return list;
41397 int pos = 1;
41398 int map_kind_pos = 0;
41399 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
41400 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
41402 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
41404 map_kind_pos = pos;
41405 break;
41408 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
41409 pos++;
41410 pos++;
41413 bool always_modifier = false;
41414 bool close_modifier = false;
41415 bool present_modifier = false;
41416 for (int pos = 1; pos < map_kind_pos; ++pos)
41418 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41419 if (tok->type == CPP_COMMA)
41421 cp_lexer_consume_token (parser->lexer);
41422 continue;
41425 const char *p = IDENTIFIER_POINTER (tok->u.value);
41426 if (strcmp ("always", p) == 0)
41428 if (always_modifier)
41430 cp_parser_error (parser, "too many %<always%> modifiers");
41431 cp_parser_skip_to_closing_parenthesis (parser,
41432 /*recovering=*/true,
41433 /*or_comma=*/false,
41434 /*consume_paren=*/true);
41435 return list;
41437 always_modifier = true;
41439 else if (strcmp ("close", p) == 0)
41441 if (close_modifier)
41443 cp_parser_error (parser, "too many %<close%> modifiers");
41444 cp_parser_skip_to_closing_parenthesis (parser,
41445 /*recovering=*/true,
41446 /*or_comma=*/false,
41447 /*consume_paren=*/true);
41448 return list;
41450 close_modifier = true;
41452 else if (strcmp ("present", p) == 0)
41454 if (present_modifier)
41456 cp_parser_error (parser, "too many %<present%> modifiers");
41457 cp_parser_skip_to_closing_parenthesis (parser,
41458 /*recovering=*/true,
41459 /*or_comma=*/false,
41460 /*consume_paren=*/true);
41461 return list;
41463 present_modifier = true;
41465 else
41467 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
41468 " than %<always%>, %<close%> or %<present%>");
41469 cp_parser_skip_to_closing_parenthesis (parser,
41470 /*recovering=*/true,
41471 /*or_comma=*/false,
41472 /*consume_paren=*/true);
41473 return list;
41476 cp_lexer_consume_token (parser->lexer);
41479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41480 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41482 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41483 const char *p = IDENTIFIER_POINTER (id);
41484 int always_present_modifier = always_modifier && present_modifier;
41486 if (strcmp ("alloc", p) == 0)
41487 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
41488 else if (strcmp ("to", p) == 0)
41489 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
41490 : present_modifier ? GOMP_MAP_PRESENT_TO
41491 : always_modifier ? GOMP_MAP_ALWAYS_TO
41492 : GOMP_MAP_TO);
41493 else if (strcmp ("from", p) == 0)
41494 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
41495 : present_modifier ? GOMP_MAP_PRESENT_FROM
41496 : always_modifier ? GOMP_MAP_ALWAYS_FROM
41497 : GOMP_MAP_FROM);
41498 else if (strcmp ("tofrom", p) == 0)
41499 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
41500 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
41501 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
41502 : GOMP_MAP_TOFROM);
41503 else if (strcmp ("release", p) == 0)
41504 kind = GOMP_MAP_RELEASE;
41505 else
41507 cp_parser_error (parser, "invalid map kind");
41508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41509 /*or_comma=*/false,
41510 /*consume_paren=*/true);
41511 return list;
41513 cp_lexer_consume_token (parser->lexer);
41514 cp_lexer_consume_token (parser->lexer);
41516 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
41517 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41519 kind = GOMP_MAP_DELETE;
41520 cp_lexer_consume_token (parser->lexer);
41521 cp_lexer_consume_token (parser->lexer);
41524 /* We introduce a scope here so that errors parsing e.g. "always", "close"
41525 tokens do not propagate to later directives that might use them
41526 legally. */
41527 begin_scope (sk_omp, NULL);
41528 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
41529 NULL, true);
41530 finish_scope ();
41532 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41533 OMP_CLAUSE_SET_MAP_KIND (c, kind);
41535 return nlist;
41538 /* OpenMP 4.0:
41539 device ( expression )
41541 OpenMP 5.0:
41542 device ( [device-modifier :] integer-expression )
41544 device-modifier:
41545 ancestor | device_num */
41547 static tree
41548 cp_parser_omp_clause_device (cp_parser *parser, tree list,
41549 location_t location)
41551 tree t, c;
41552 bool ancestor = false;
41554 matching_parens parens;
41555 if (!parens.require_open (parser))
41556 return list;
41558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41559 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41561 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41562 const char *p = IDENTIFIER_POINTER (tok->u.value);
41563 if (strcmp ("ancestor", p) == 0)
41565 ancestor = true;
41567 /* A requires directive with the reverse_offload clause must be
41568 specified. */
41569 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
41571 error_at (tok->location, "%<ancestor%> device modifier not "
41572 "preceded by %<requires%> directive "
41573 "with %<reverse_offload%> clause");
41574 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41575 return list;
41578 else if (strcmp ("device_num", p) == 0)
41580 else
41582 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41583 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41584 return list;
41586 cp_lexer_consume_token (parser->lexer);
41587 cp_lexer_consume_token (parser->lexer);
41590 t = cp_parser_assignment_expression (parser);
41592 if (t == error_mark_node
41593 || !parens.require_close (parser))
41594 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41595 /*or_comma=*/false,
41596 /*consume_paren=*/true);
41598 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41599 "device", location);
41601 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41602 OMP_CLAUSE_DEVICE_ID (c) = t;
41603 OMP_CLAUSE_CHAIN (c) = list;
41604 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
41606 return c;
41609 /* OpenMP 4.0:
41610 dist_schedule ( static )
41611 dist_schedule ( static , expression ) */
41613 static tree
41614 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
41615 location_t location)
41617 tree c, t;
41619 matching_parens parens;
41620 if (!parens.require_open (parser))
41621 return list;
41623 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
41625 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
41626 goto invalid_kind;
41627 cp_lexer_consume_token (parser->lexer);
41629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41631 cp_lexer_consume_token (parser->lexer);
41633 t = cp_parser_assignment_expression (parser);
41635 if (t == error_mark_node)
41636 goto resync_fail;
41637 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
41639 if (!parens.require_close (parser))
41640 goto resync_fail;
41642 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41643 goto resync_fail;
41645 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
41646 "dist_schedule", location); */
41647 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
41648 warning_at (location, OPT_Wopenmp, "too many %qs clauses", "dist_schedule");
41649 OMP_CLAUSE_CHAIN (c) = list;
41650 return c;
41652 invalid_kind:
41653 cp_parser_error (parser, "invalid dist_schedule kind");
41654 resync_fail:
41655 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41656 /*or_comma=*/false,
41657 /*consume_paren=*/true);
41658 return list;
41661 /* OpenMP 4.0:
41662 proc_bind ( proc-bind-kind )
41664 proc-bind-kind:
41665 primary | master | close | spread
41666 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41668 static tree
41669 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41670 location_t location)
41672 tree c;
41673 enum omp_clause_proc_bind_kind kind;
41675 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41676 return list;
41678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41681 const char *p = IDENTIFIER_POINTER (id);
41683 if (strcmp ("primary", p) == 0)
41684 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41685 else if (strcmp ("master", p) == 0)
41686 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41687 else if (strcmp ("close", p) == 0)
41688 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41689 else if (strcmp ("spread", p) == 0)
41690 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41691 else
41692 goto invalid_kind;
41694 else
41695 goto invalid_kind;
41697 cp_lexer_consume_token (parser->lexer);
41698 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41699 goto resync_fail;
41701 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41702 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41703 location);
41704 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41705 OMP_CLAUSE_CHAIN (c) = list;
41706 return c;
41708 invalid_kind:
41709 cp_parser_error (parser, "invalid depend kind");
41710 resync_fail:
41711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41712 /*or_comma=*/false,
41713 /*consume_paren=*/true);
41714 return list;
41717 /* OpenMP 5.0:
41718 device_type ( host | nohost | any ) */
41720 static tree
41721 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41722 location_t location)
41724 tree c;
41725 enum omp_clause_device_type_kind kind;
41727 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41728 return list;
41730 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41732 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41733 const char *p = IDENTIFIER_POINTER (id);
41735 if (strcmp ("host", p) == 0)
41736 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41737 else if (strcmp ("nohost", p) == 0)
41738 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41739 else if (strcmp ("any", p) == 0)
41740 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41741 else
41742 goto invalid_kind;
41744 else
41745 goto invalid_kind;
41747 cp_lexer_consume_token (parser->lexer);
41748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41749 goto resync_fail;
41751 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41752 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41753 location);
41754 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41755 OMP_CLAUSE_CHAIN (c) = list;
41756 return c;
41758 invalid_kind:
41759 cp_parser_error (parser, "invalid depend kind");
41760 resync_fail:
41761 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41762 /*or_comma=*/false,
41763 /*consume_paren=*/true);
41764 return list;
41767 /* OpenACC:
41768 async [( int-expr )] */
41770 static tree
41771 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41773 tree c, t;
41774 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41776 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41778 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41780 matching_parens parens;
41781 parens.consume_open (parser);
41783 t = cp_parser_assignment_expression (parser);
41784 if (t == error_mark_node
41785 || !parens.require_close (parser))
41786 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41787 /*or_comma=*/false,
41788 /*consume_paren=*/true);
41791 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41793 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41794 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41795 OMP_CLAUSE_CHAIN (c) = list;
41796 list = c;
41798 return list;
41801 /* OpenACC 2.7:
41802 self [( expression )] */
41804 static tree
41805 cp_parser_oacc_compute_clause_self (cp_parser *parser, tree list)
41807 tree t;
41808 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41809 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41811 matching_parens parens;
41812 parens.consume_open (parser);
41813 t = cp_parser_assignment_expression (parser);
41814 if (t == error_mark_node
41815 || !parens.require_close (parser))
41817 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41818 /*or_comma=*/false,
41819 /*consume_paren=*/true);
41820 return list;
41823 else
41824 t = truthvalue_true_node;
41826 for (tree c = list; c; c = OMP_CLAUSE_CHAIN (c))
41827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SELF)
41829 error_at (location, "too many %<self%> clauses");
41830 return list;
41833 tree c = build_omp_clause (location, OMP_CLAUSE_SELF);
41834 OMP_CLAUSE_SELF_EXPR (c) = t;
41835 OMP_CLAUSE_CHAIN (c) = list;
41836 return c;
41839 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41840 is a bitmask in MASK. Return the list of clauses found. */
41842 static tree
41843 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41844 const char *where, cp_token *pragma_tok,
41845 bool finish_p = true, bool target_p = false)
41847 tree clauses = NULL;
41848 bool first = true;
41850 /* Don't create location wrapper nodes within OpenACC clauses. */
41851 auto_suppress_location_wrappers sentinel;
41853 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41855 location_t here;
41856 pragma_omp_clause c_kind;
41857 omp_clause_code code;
41858 const char *c_name;
41859 tree prev = clauses;
41861 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41862 cp_lexer_consume_token (parser->lexer);
41864 here = cp_lexer_peek_token (parser->lexer)->location;
41865 c_kind = cp_parser_omp_clause_name (parser);
41867 switch (c_kind)
41869 case PRAGMA_OACC_CLAUSE_ASYNC:
41870 clauses = cp_parser_oacc_clause_async (parser, clauses);
41871 c_name = "async";
41872 break;
41873 case PRAGMA_OACC_CLAUSE_AUTO:
41874 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
41875 clauses);
41876 c_name = "auto";
41877 break;
41878 case PRAGMA_OACC_CLAUSE_ATTACH:
41879 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41880 c_name = "attach";
41881 break;
41882 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41883 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41884 c_name = "collapse";
41885 break;
41886 case PRAGMA_OACC_CLAUSE_COPY:
41887 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41888 c_name = "copy";
41889 break;
41890 case PRAGMA_OACC_CLAUSE_COPYIN:
41891 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41892 c_name = "copyin";
41893 break;
41894 case PRAGMA_OACC_CLAUSE_COPYOUT:
41895 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41896 c_name = "copyout";
41897 break;
41898 case PRAGMA_OACC_CLAUSE_CREATE:
41899 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41900 c_name = "create";
41901 break;
41902 case PRAGMA_OACC_CLAUSE_DELETE:
41903 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41904 c_name = "delete";
41905 break;
41906 case PRAGMA_OMP_CLAUSE_DEFAULT:
41907 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41908 c_name = "default";
41909 break;
41910 case PRAGMA_OACC_CLAUSE_DETACH:
41911 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41912 c_name = "detach";
41913 break;
41914 case PRAGMA_OACC_CLAUSE_DEVICE:
41915 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41916 c_name = "device";
41917 break;
41918 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41919 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41920 c_name = "deviceptr";
41921 break;
41922 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41923 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41924 c_name = "device_resident";
41925 break;
41926 case PRAGMA_OACC_CLAUSE_FINALIZE:
41927 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41928 clauses);
41929 c_name = "finalize";
41930 break;
41931 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41932 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41933 clauses);
41934 c_name = "firstprivate";
41935 break;
41936 case PRAGMA_OACC_CLAUSE_GANG:
41937 c_name = "gang";
41938 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41939 c_name, clauses);
41940 break;
41941 case PRAGMA_OACC_CLAUSE_HOST:
41942 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41943 c_name = "host";
41944 break;
41945 case PRAGMA_OACC_CLAUSE_IF:
41946 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41947 c_name = "if";
41948 break;
41949 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41950 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41951 clauses);
41952 c_name = "if_present";
41953 break;
41954 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41955 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41956 clauses);
41957 c_name = "independent";
41958 break;
41959 case PRAGMA_OACC_CLAUSE_LINK:
41960 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41961 c_name = "link";
41962 break;
41963 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41964 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41965 c_name = "no_create";
41966 break;
41967 case PRAGMA_OACC_CLAUSE_NOHOST:
41968 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41969 clauses);
41970 c_name = "nohost";
41971 break;
41972 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41973 code = OMP_CLAUSE_NUM_GANGS;
41974 c_name = "num_gangs";
41975 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41976 clauses);
41977 break;
41978 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41979 c_name = "num_workers";
41980 code = OMP_CLAUSE_NUM_WORKERS;
41981 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41982 clauses);
41983 break;
41984 case PRAGMA_OACC_CLAUSE_PRESENT:
41985 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41986 c_name = "present";
41987 break;
41988 case PRAGMA_OACC_CLAUSE_PRIVATE:
41989 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41990 clauses);
41991 c_name = "private";
41992 break;
41993 case PRAGMA_OACC_CLAUSE_REDUCTION:
41994 clauses
41995 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41996 false, clauses);
41997 c_name = "reduction";
41998 break;
41999 case PRAGMA_OACC_CLAUSE_SELF:
42000 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)) == 0)
42001 /* OpenACC compute construct */
42002 clauses = cp_parser_oacc_compute_clause_self (parser, clauses);
42003 else
42004 /* OpenACC 'update' directive */
42005 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42006 c_name = "self";
42007 break;
42008 case PRAGMA_OACC_CLAUSE_SEQ:
42009 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
42010 clauses);
42011 c_name = "seq";
42012 break;
42013 case PRAGMA_OACC_CLAUSE_TILE:
42014 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
42015 c_name = "tile";
42016 break;
42017 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
42018 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42019 clauses);
42020 c_name = "use_device";
42021 break;
42022 case PRAGMA_OACC_CLAUSE_VECTOR:
42023 c_name = "vector";
42024 clauses = cp_parser_oacc_shape_clause (parser, here,
42025 OMP_CLAUSE_VECTOR,
42026 c_name, clauses);
42027 break;
42028 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
42029 c_name = "vector_length";
42030 code = OMP_CLAUSE_VECTOR_LENGTH;
42031 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42032 clauses);
42033 break;
42034 case PRAGMA_OACC_CLAUSE_WAIT:
42035 clauses = cp_parser_oacc_clause_wait (parser, clauses);
42036 c_name = "wait";
42037 break;
42038 case PRAGMA_OACC_CLAUSE_WORKER:
42039 c_name = "worker";
42040 clauses = cp_parser_oacc_shape_clause (parser, here,
42041 OMP_CLAUSE_WORKER,
42042 c_name, clauses);
42043 break;
42044 default:
42045 cp_parser_error (parser, "expected an OpenACC clause");
42046 goto saw_error;
42049 first = false;
42051 if (((mask >> c_kind) & 1) == 0)
42053 /* Remove the invalid clause(s) from the list to avoid
42054 confusing the rest of the compiler. */
42055 clauses = prev;
42056 error_at (here, "%qs is not valid for %qs", c_name, where);
42060 saw_error:
42061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42063 if (finish_p)
42064 return finish_omp_clauses (clauses, target_p ? C_ORT_ACC_TARGET
42065 : C_ORT_ACC);
42067 return clauses;
42070 /* Parse all OpenMP clauses. The set clauses allowed by the directive
42071 is a bitmask in MASK. Return the list of clauses found.
42072 FINISH_P set if finish_omp_clauses should be called.
42073 NESTED non-zero if clauses should be terminated by closing paren instead
42074 of end of pragma. If it is 2, additionally commas are required in between
42075 the clauses. */
42077 static tree
42078 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
42079 const char *where, cp_token *pragma_tok,
42080 bool finish_p = true, int nested = 0)
42082 tree clauses = NULL;
42083 bool first = true;
42084 cp_token *token = NULL;
42086 /* Don't create location wrapper nodes within OpenMP clauses. */
42087 auto_suppress_location_wrappers sentinel;
42089 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42091 pragma_omp_clause c_kind;
42092 const char *c_name;
42093 tree prev = clauses;
42095 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
42096 break;
42098 if (!first || nested != 2)
42100 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42101 cp_lexer_consume_token (parser->lexer);
42102 else if (nested == 2)
42103 error_at (cp_lexer_peek_token (parser->lexer)->location,
42104 "clauses in %<simd%> trait should be separated "
42105 "by %<,%>");
42108 token = cp_lexer_peek_token (parser->lexer);
42109 c_kind = cp_parser_omp_clause_name (parser);
42111 switch (c_kind)
42113 case PRAGMA_OMP_CLAUSE_BIND:
42114 clauses = cp_parser_omp_clause_bind (parser, clauses,
42115 token->location);
42116 c_name = "bind";
42117 break;
42118 case PRAGMA_OMP_CLAUSE_COLLAPSE:
42119 clauses = cp_parser_omp_clause_collapse (parser, clauses,
42120 token->location);
42121 c_name = "collapse";
42122 break;
42123 case PRAGMA_OMP_CLAUSE_COPYIN:
42124 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
42125 c_name = "copyin";
42126 break;
42127 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
42128 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
42129 clauses);
42130 c_name = "copyprivate";
42131 break;
42132 case PRAGMA_OMP_CLAUSE_DEFAULT:
42133 clauses = cp_parser_omp_clause_default (parser, clauses,
42134 token->location, false);
42135 c_name = "default";
42136 break;
42137 case PRAGMA_OMP_CLAUSE_FILTER:
42138 clauses = cp_parser_omp_clause_filter (parser, clauses,
42139 token->location);
42140 c_name = "filter";
42141 break;
42142 case PRAGMA_OMP_CLAUSE_FINAL:
42143 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
42144 c_name = "final";
42145 break;
42146 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
42147 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42148 clauses);
42149 c_name = "firstprivate";
42150 break;
42151 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
42152 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
42153 token->location);
42154 c_name = "grainsize";
42155 break;
42156 case PRAGMA_OMP_CLAUSE_HINT:
42157 clauses = cp_parser_omp_clause_hint (parser, clauses,
42158 token->location);
42159 c_name = "hint";
42160 break;
42161 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
42162 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
42163 token->location);
42164 c_name = "defaultmap";
42165 break;
42166 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
42167 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42168 clauses);
42169 c_name = "use_device_ptr";
42170 break;
42171 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
42172 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
42173 clauses);
42174 c_name = "use_device_addr";
42175 break;
42176 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
42177 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
42178 clauses);
42179 c_name = "is_device_ptr";
42180 break;
42181 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
42182 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
42183 clauses);
42184 c_name = "has_device_addr";
42185 break;
42186 case PRAGMA_OMP_CLAUSE_IF:
42187 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
42188 true);
42189 c_name = "if";
42190 break;
42191 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
42192 clauses
42193 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
42194 true, clauses);
42195 c_name = "in_reduction";
42196 break;
42197 case PRAGMA_OMP_CLAUSE_INDIRECT:
42198 clauses = cp_parser_omp_clause_indirect (parser, clauses,
42199 token->location);
42200 c_name = "indirect";
42201 break;
42202 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
42203 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
42204 c_name = "lastprivate";
42205 break;
42206 case PRAGMA_OMP_CLAUSE_MERGEABLE:
42207 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
42208 token->location);
42209 c_name = "mergeable";
42210 break;
42211 case PRAGMA_OMP_CLAUSE_NOWAIT:
42212 clauses = cp_parser_omp_clause_nowait (parser, clauses,
42213 token->location);
42214 c_name = "nowait";
42215 break;
42216 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
42217 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
42218 token->location);
42219 c_name = "num_tasks";
42220 break;
42221 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
42222 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
42223 token->location);
42224 c_name = "num_threads";
42225 break;
42226 case PRAGMA_OMP_CLAUSE_ORDER:
42227 clauses = cp_parser_omp_clause_order (parser, clauses,
42228 token->location);
42229 c_name = "order";
42230 break;
42231 case PRAGMA_OMP_CLAUSE_ORDERED:
42232 clauses = cp_parser_omp_clause_ordered (parser, clauses,
42233 token->location);
42234 c_name = "ordered";
42235 break;
42236 case PRAGMA_OMP_CLAUSE_PRIORITY:
42237 clauses = cp_parser_omp_clause_priority (parser, clauses,
42238 token->location);
42239 c_name = "priority";
42240 break;
42241 case PRAGMA_OMP_CLAUSE_PRIVATE:
42242 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42243 clauses);
42244 c_name = "private";
42245 break;
42246 case PRAGMA_OMP_CLAUSE_REDUCTION:
42247 clauses
42248 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42249 true, clauses);
42250 c_name = "reduction";
42251 break;
42252 case PRAGMA_OMP_CLAUSE_SCHEDULE:
42253 clauses = cp_parser_omp_clause_schedule (parser, clauses,
42254 token->location);
42255 c_name = "schedule";
42256 break;
42257 case PRAGMA_OMP_CLAUSE_SHARED:
42258 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
42259 clauses);
42260 c_name = "shared";
42261 break;
42262 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
42263 clauses
42264 = cp_parser_omp_clause_reduction (parser,
42265 OMP_CLAUSE_TASK_REDUCTION,
42266 true, clauses);
42267 c_name = "task_reduction";
42268 break;
42269 case PRAGMA_OMP_CLAUSE_UNTIED:
42270 clauses = cp_parser_omp_clause_untied (parser, clauses,
42271 token->location);
42272 c_name = "untied";
42273 break;
42274 case PRAGMA_OMP_CLAUSE_INBRANCH:
42275 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
42276 clauses, token->location);
42277 c_name = "inbranch";
42278 break;
42279 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
42280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
42281 clauses);
42282 c_name = "nontemporal";
42283 break;
42284 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
42285 clauses = cp_parser_omp_clause_branch (parser,
42286 OMP_CLAUSE_NOTINBRANCH,
42287 clauses, token->location);
42288 c_name = "notinbranch";
42289 break;
42290 case PRAGMA_OMP_CLAUSE_PARALLEL:
42291 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
42292 clauses, token->location);
42293 c_name = "parallel";
42294 if (!first)
42296 clause_not_first:
42297 error_at (token->location, "%qs must be the first clause of %qs",
42298 c_name, where);
42299 clauses = prev;
42301 break;
42302 case PRAGMA_OMP_CLAUSE_FOR:
42303 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
42304 clauses, token->location);
42305 c_name = "for";
42306 if (!first)
42307 goto clause_not_first;
42308 break;
42309 case PRAGMA_OMP_CLAUSE_SECTIONS:
42310 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
42311 clauses, token->location);
42312 c_name = "sections";
42313 if (!first)
42314 goto clause_not_first;
42315 break;
42316 case PRAGMA_OMP_CLAUSE_TASKGROUP:
42317 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
42318 clauses, token->location);
42319 c_name = "taskgroup";
42320 if (!first)
42321 goto clause_not_first;
42322 break;
42323 case PRAGMA_OMP_CLAUSE_LINK:
42324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
42325 c_name = "link";
42326 break;
42327 case PRAGMA_OMP_CLAUSE_TO:
42328 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
42330 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42331 clauses);
42332 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
42333 OMP_CLAUSE_ENTER_TO (c) = 1;
42334 clauses = nl;
42336 else
42337 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
42338 clauses);
42339 c_name = "to";
42340 break;
42341 case PRAGMA_OMP_CLAUSE_FROM:
42342 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
42343 clauses);
42344 c_name = "from";
42345 break;
42346 case PRAGMA_OMP_CLAUSE_UNIFORM:
42347 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
42348 clauses);
42349 c_name = "uniform";
42350 break;
42351 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
42352 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
42353 token->location);
42354 c_name = "num_teams";
42355 break;
42356 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
42357 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
42358 token->location);
42359 c_name = "thread_limit";
42360 break;
42361 case PRAGMA_OMP_CLAUSE_ALIGNED:
42362 clauses = cp_parser_omp_clause_aligned (parser, clauses);
42363 c_name = "aligned";
42364 break;
42365 case PRAGMA_OMP_CLAUSE_ALLOCATE:
42366 clauses = cp_parser_omp_clause_allocate (parser, clauses);
42367 c_name = "allocate";
42368 break;
42369 case PRAGMA_OMP_CLAUSE_LINEAR:
42371 bool declare_simd = false;
42372 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
42373 declare_simd = true;
42374 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
42376 c_name = "linear";
42377 break;
42378 case PRAGMA_OMP_CLAUSE_AFFINITY:
42379 clauses = cp_parser_omp_clause_affinity (parser, clauses);
42380 c_name = "affinity";
42381 break;
42382 case PRAGMA_OMP_CLAUSE_DEPEND:
42383 clauses = cp_parser_omp_clause_depend (parser, clauses,
42384 token->location);
42385 c_name = "depend";
42386 break;
42387 case PRAGMA_OMP_CLAUSE_DOACROSS:
42388 clauses = cp_parser_omp_clause_doacross (parser, clauses,
42389 token->location);
42390 c_name = "doacross";
42391 break;
42392 case PRAGMA_OMP_CLAUSE_DETACH:
42393 clauses = cp_parser_omp_clause_detach (parser, clauses);
42394 c_name = "detach";
42395 break;
42396 case PRAGMA_OMP_CLAUSE_MAP:
42397 clauses = cp_parser_omp_clause_map (parser, clauses);
42398 c_name = "map";
42399 break;
42400 case PRAGMA_OMP_CLAUSE_DEVICE:
42401 clauses = cp_parser_omp_clause_device (parser, clauses,
42402 token->location);
42403 c_name = "device";
42404 break;
42405 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
42406 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
42407 token->location);
42408 c_name = "dist_schedule";
42409 break;
42410 case PRAGMA_OMP_CLAUSE_PROC_BIND:
42411 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
42412 token->location);
42413 c_name = "proc_bind";
42414 break;
42415 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
42416 clauses = cp_parser_omp_clause_device_type (parser, clauses,
42417 token->location);
42418 c_name = "device_type";
42419 break;
42420 case PRAGMA_OMP_CLAUSE_SAFELEN:
42421 clauses = cp_parser_omp_clause_safelen (parser, clauses,
42422 token->location);
42423 c_name = "safelen";
42424 break;
42425 case PRAGMA_OMP_CLAUSE_SIMDLEN:
42426 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
42427 token->location);
42428 c_name = "simdlen";
42429 break;
42430 case PRAGMA_OMP_CLAUSE_NOGROUP:
42431 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
42432 token->location);
42433 c_name = "nogroup";
42434 break;
42435 case PRAGMA_OMP_CLAUSE_THREADS:
42436 clauses
42437 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
42438 clauses, token->location);
42439 c_name = "threads";
42440 break;
42441 case PRAGMA_OMP_CLAUSE_SIMD:
42442 clauses
42443 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
42444 clauses, token->location);
42445 c_name = "simd";
42446 break;
42447 case PRAGMA_OMP_CLAUSE_ENTER:
42448 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42449 clauses);
42450 c_name = "enter";
42451 break;
42452 default:
42453 cp_parser_error (parser, "expected an OpenMP clause");
42454 goto saw_error;
42457 first = false;
42459 if (((mask >> c_kind) & 1) == 0)
42461 /* Remove the invalid clause(s) from the list to avoid
42462 confusing the rest of the compiler. */
42463 clauses = prev;
42464 error_at (token->location, "%qs is not valid for %qs", c_name, where);
42467 saw_error:
42468 if (!nested)
42469 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42470 if (finish_p)
42472 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
42473 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
42474 else
42475 return finish_omp_clauses (clauses, C_ORT_OMP);
42477 return clauses;
42480 /* OpenMP 2.5:
42481 structured-block:
42482 statement
42484 In practice, we're also interested in adding the statement to an
42485 outer node. So it is convenient if we work around the fact that
42486 cp_parser_statement calls add_stmt. */
42488 static unsigned
42489 cp_parser_begin_omp_structured_block (cp_parser *parser)
42491 unsigned save = parser->in_statement;
42493 /* Only move the values to IN_OMP_BLOCK if they weren't false.
42494 This preserves the "not within loop or switch" style error messages
42495 for nonsense cases like
42496 void foo() {
42497 #pragma omp single
42498 break;
42501 if (parser->in_statement)
42502 parser->in_statement = IN_OMP_BLOCK;
42504 return save;
42507 static void
42508 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
42510 parser->in_statement = save;
42513 static tree
42514 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
42516 tree stmt = begin_omp_structured_block ();
42517 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42519 parser->omp_attrs_forbidden_p = true;
42520 cp_parser_statement (parser, NULL_TREE, false, if_p);
42522 cp_parser_end_omp_structured_block (parser, save);
42523 return finish_omp_structured_block (stmt);
42526 /* OpenMP 5.x:
42527 # pragma omp allocate (list) clauses
42529 OpenMP 5.0 clause:
42530 allocator (omp_allocator_handle_t expression)
42532 OpenMP 5.1 additional clause:
42533 align (constant-expression)] */
42535 static void
42536 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
42538 tree allocator = NULL_TREE;
42539 tree alignment = NULL_TREE;
42540 location_t loc = pragma_tok->location;
42541 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
42545 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42546 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42547 cp_lexer_consume_token (parser->lexer);
42549 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42550 break;
42551 matching_parens parens;
42552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42553 const char *p = IDENTIFIER_POINTER (id);
42554 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42555 cp_lexer_consume_token (parser->lexer);
42556 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
42558 error_at (cloc, "expected %<allocator%> or %<align%>");
42559 break;
42561 if (!parens.require_open (parser))
42562 break;
42563 tree expr = cp_parser_assignment_expression (parser);
42564 if (p[2] == 'i' && alignment)
42566 error_at (cloc, "too many %qs clauses", "align");
42567 break;
42569 else if (p[2] == 'i')
42571 if (expr != error_mark_node)
42572 alignment = expr;
42573 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
42574 if (alignment
42575 && !type_dependent_expression_p (alignment)
42576 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
42578 error_at (cloc, "%<align%> clause argument needs to be "
42579 "positive constant power of two integer "
42580 "expression");
42581 alignment = NULL_TREE;
42583 else if (alignment)
42585 alignment = mark_rvalue_use (alignment);
42586 if (!processing_template_decl)
42588 alignment = maybe_constant_value (alignment);
42589 if (TREE_CODE (alignment) != INTEGER_CST
42590 || !tree_fits_uhwi_p (alignment)
42591 || !integer_pow2p (alignment))
42593 error_at (cloc, "%<align%> clause argument needs to be "
42594 "positive constant power of two integer "
42595 "expression");
42596 alignment = NULL_TREE;
42601 else if (allocator)
42603 error_at (cloc, "too many %qs clauses", "allocator");
42604 break;
42606 else
42608 if (expr != error_mark_node)
42609 allocator = expr;
42611 parens.require_close (parser);
42612 } while (true);
42613 cp_parser_require_pragma_eol (parser, pragma_tok);
42615 if (allocator || alignment)
42616 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
42618 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
42619 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
42622 /* FIXME: When implementing properly, delete the align/allocate expr error
42623 check above and add one in semantics.cc (to properly handle templates).
42624 Base this on the allocator/align modifiers check for the 'allocate' clause
42625 in semantics.cc's finish_omp_clauses. */
42626 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
42629 /* OpenMP 2.5:
42630 # pragma omp atomic new-line
42631 expression-stmt
42633 expression-stmt:
42634 x binop= expr | x++ | ++x | x-- | --x
42635 binop:
42636 +, *, -, /, &, ^, |, <<, >>
42638 where x is an lvalue expression with scalar type.
42640 OpenMP 3.1:
42641 # pragma omp atomic new-line
42642 update-stmt
42644 # pragma omp atomic read new-line
42645 read-stmt
42647 # pragma omp atomic write new-line
42648 write-stmt
42650 # pragma omp atomic update new-line
42651 update-stmt
42653 # pragma omp atomic capture new-line
42654 capture-stmt
42656 # pragma omp atomic capture new-line
42657 capture-block
42659 read-stmt:
42660 v = x
42661 write-stmt:
42662 x = expr
42663 update-stmt:
42664 expression-stmt | x = x binop expr
42665 capture-stmt:
42666 v = expression-stmt
42667 capture-block:
42668 { v = x; update-stmt; } | { update-stmt; v = x; }
42670 OpenMP 4.0:
42671 update-stmt:
42672 expression-stmt | x = x binop expr | x = expr binop x
42673 capture-stmt:
42674 v = update-stmt
42675 capture-block:
42676 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
42678 OpenMP 5.1:
42679 # pragma omp atomic compare new-line
42680 conditional-update-atomic
42682 # pragma omp atomic compare capture new-line
42683 conditional-update-capture-atomic
42685 conditional-update-atomic:
42686 cond-expr-stmt | cond-update-stmt
42687 cond-expr-stmt:
42688 x = expr ordop x ? expr : x;
42689 x = x ordop expr ? expr : x;
42690 x = x == e ? d : x;
42691 cond-update-stmt:
42692 if (expr ordop x) { x = expr; }
42693 if (x ordop expr) { x = expr; }
42694 if (x == e) { x = d; }
42695 ordop:
42696 <, >
42697 conditional-update-capture-atomic:
42698 v = cond-expr-stmt
42699 { v = x; cond-expr-stmt }
42700 { cond-expr-stmt v = x; }
42701 { v = x; cond-update-stmt }
42702 { cond-update-stmt v = x; }
42703 if (x == e) { x = d; } else { v = x; }
42704 { r = x == e; if (r) { x = d; } }
42705 { r = x == e; if (r) { x = d; } else { v = x; } }
42707 where x, r and v are lvalue expressions with scalar type,
42708 expr, e and d are expressions with scalar type and e might be
42709 the same as v. */
42711 static void
42712 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42714 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42715 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42716 location_t loc = pragma_tok->location;
42717 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42718 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42719 bool structured_block = false;
42720 tree clauses = NULL_TREE;
42721 bool capture = false;
42722 bool compare = false;
42723 bool weak = false;
42724 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42725 bool no_semicolon = false;
42726 bool extra_scope = false;
42728 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42730 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42731 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42732 cp_lexer_consume_token (parser->lexer);
42734 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42736 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42737 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42738 const char *p = IDENTIFIER_POINTER (id);
42739 enum tree_code new_code = ERROR_MARK;
42740 enum omp_memory_order new_memory_order
42741 = OMP_MEMORY_ORDER_UNSPECIFIED;
42742 bool new_capture = false;
42743 bool new_compare = false;
42744 bool new_weak = false;
42745 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42747 if (!strcmp (p, "read"))
42748 new_code = OMP_ATOMIC_READ;
42749 else if (!strcmp (p, "write"))
42750 new_code = NOP_EXPR;
42751 else if (!strcmp (p, "update"))
42752 new_code = OMP_ATOMIC;
42753 else if (openacc && !strcmp (p, "capture"))
42754 new_code = OMP_ATOMIC_CAPTURE_NEW;
42755 else if (openacc)
42757 p = NULL;
42758 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42759 "or %<capture%> clause");
42761 else if (!strcmp (p, "capture"))
42762 new_capture = true;
42763 else if (!strcmp (p, "compare"))
42764 new_compare = true;
42765 else if (!strcmp (p, "weak"))
42766 new_weak = true;
42767 else if (!strcmp (p, "fail"))
42769 matching_parens parens;
42771 cp_lexer_consume_token (parser->lexer);
42772 if (!parens.require_open (parser))
42773 continue;
42775 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42777 id = cp_lexer_peek_token (parser->lexer)->u.value;
42778 const char *q = IDENTIFIER_POINTER (id);
42780 if (!strcmp (q, "seq_cst"))
42781 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42782 else if (!strcmp (q, "acquire"))
42783 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42784 else if (!strcmp (q, "relaxed"))
42785 new_fail = OMP_MEMORY_ORDER_RELAXED;
42788 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42790 cp_lexer_consume_token (parser->lexer);
42791 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42792 error_at (cloc, "too many %qs clauses", "fail");
42793 else
42794 fail = new_fail;
42796 else
42797 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42798 "or %<relaxed%>");
42799 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42800 || !parens.require_close (parser))
42801 cp_parser_skip_to_closing_parenthesis (parser,
42802 /*recovering=*/true,
42803 /*or_comma=*/false,
42804 /*consume_paren=*/true);
42805 continue;
42807 else if (!strcmp (p, "seq_cst"))
42808 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42809 else if (!strcmp (p, "acq_rel"))
42810 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42811 else if (!strcmp (p, "release"))
42812 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42813 else if (!strcmp (p, "acquire"))
42814 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42815 else if (!strcmp (p, "relaxed"))
42816 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42817 else if (!strcmp (p, "hint"))
42819 cp_lexer_consume_token (parser->lexer);
42820 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42821 continue;
42823 else
42825 p = NULL;
42826 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42827 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42828 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42829 "%<relaxed%> or %<hint%> clause");
42831 if (p)
42833 if (new_code != ERROR_MARK)
42835 /* OpenACC permits 'update capture'. */
42836 if (openacc
42837 && code == OMP_ATOMIC
42838 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42839 code = new_code;
42840 else if (code != ERROR_MARK)
42841 error_at (cloc, "too many atomic clauses");
42842 else
42843 code = new_code;
42845 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42847 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42848 error_at (cloc, "too many memory order clauses");
42849 else
42850 memory_order = new_memory_order;
42852 else if (new_capture)
42854 if (capture)
42855 error_at (cloc, "too many %qs clauses", "capture");
42856 else
42857 capture = true;
42859 else if (new_compare)
42861 if (compare)
42862 error_at (cloc, "too many %qs clauses", "compare");
42863 else
42864 compare = true;
42866 else if (new_weak)
42868 if (weak)
42869 error_at (cloc, "too many %qs clauses", "weak");
42870 else
42871 weak = true;
42873 cp_lexer_consume_token (parser->lexer);
42874 continue;
42877 break;
42879 cp_parser_require_pragma_eol (parser, pragma_tok);
42881 if (code == ERROR_MARK)
42882 code = OMP_ATOMIC;
42883 if (capture)
42885 if (code != OMP_ATOMIC)
42886 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42887 "clauses", "capture");
42888 else
42889 code = OMP_ATOMIC_CAPTURE_NEW;
42891 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42893 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42894 "clauses", "compare");
42895 compare = false;
42897 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42899 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42900 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42902 if (weak && !compare)
42904 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42905 weak = false;
42907 if (openacc)
42908 memory_order = OMP_MEMORY_ORDER_RELAXED;
42909 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42911 omp_requires_mask
42912 = (enum omp_requires) (omp_requires_mask
42913 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42914 switch ((enum omp_memory_order)
42915 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42917 case OMP_MEMORY_ORDER_UNSPECIFIED:
42918 case OMP_MEMORY_ORDER_RELAXED:
42919 memory_order = OMP_MEMORY_ORDER_RELAXED;
42920 break;
42921 case OMP_MEMORY_ORDER_SEQ_CST:
42922 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42923 break;
42924 case OMP_MEMORY_ORDER_ACQUIRE:
42925 if (code == NOP_EXPR) /* atomic write */
42927 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42928 "%<acquire%> clause implicitly provided by a "
42929 "%<requires%> directive");
42930 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42932 else
42933 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42934 break;
42935 case OMP_MEMORY_ORDER_RELEASE:
42936 if (code == OMP_ATOMIC_READ)
42938 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42939 "%<release%> clause implicitly provided by a "
42940 "%<requires%> directive");
42941 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42943 else
42944 memory_order = OMP_MEMORY_ORDER_RELEASE;
42945 break;
42946 case OMP_MEMORY_ORDER_ACQ_REL:
42947 switch (code)
42949 case OMP_ATOMIC_READ:
42950 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42951 break;
42952 case NOP_EXPR: /* atomic write */
42953 memory_order = OMP_MEMORY_ORDER_RELEASE;
42954 break;
42955 default:
42956 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42957 break;
42959 break;
42960 default:
42961 gcc_unreachable ();
42964 else
42965 switch (code)
42967 case OMP_ATOMIC_READ:
42968 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42970 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42971 "%<release%> clause");
42972 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42974 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42975 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42976 break;
42977 case NOP_EXPR: /* atomic write */
42978 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42980 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42981 "%<acquire%> clause");
42982 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42984 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42985 memory_order = OMP_MEMORY_ORDER_RELEASE;
42986 break;
42987 default:
42988 break;
42990 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42991 memory_order
42992 = (enum omp_memory_order) (memory_order
42993 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42995 switch (code)
42997 case OMP_ATOMIC_READ:
42998 case NOP_EXPR: /* atomic write */
42999 v = cp_parser_unary_expression (parser);
43000 if (v == error_mark_node)
43001 goto saw_error;
43002 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43003 goto saw_error;
43004 if (code == NOP_EXPR)
43005 lhs = cp_parser_expression (parser);
43006 else
43007 lhs = cp_parser_unary_expression (parser);
43008 if (lhs == error_mark_node)
43009 goto saw_error;
43010 if (code == NOP_EXPR)
43012 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
43013 opcode. */
43014 code = OMP_ATOMIC;
43015 rhs = lhs;
43016 lhs = v;
43017 v = NULL_TREE;
43019 goto done;
43020 case OMP_ATOMIC_CAPTURE_NEW:
43021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43023 cp_lexer_consume_token (parser->lexer);
43024 structured_block = true;
43026 else if (compare
43027 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43028 break;
43029 else
43031 v = cp_parser_unary_expression (parser);
43032 if (v == error_mark_node)
43033 goto saw_error;
43034 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43035 goto saw_error;
43036 if (compare
43037 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43039 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43040 error_at (eloc, "expected expression");
43041 goto saw_error;
43044 default:
43045 break;
43048 restart:
43049 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43051 cp_lexer_consume_token (parser->lexer);
43053 matching_parens parens;
43054 if (!parens.require_open (parser))
43055 goto saw_error;
43056 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43057 tree cmp_expr;
43058 if (r)
43059 cmp_expr = cp_parser_unary_expression (parser);
43060 else
43061 cmp_expr = cp_parser_binary_expression (parser, false, true,
43062 PREC_NOT_OPERATOR, NULL);
43063 if (!parens.require_close (parser))
43064 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
43065 if (cmp_expr == error_mark_node)
43066 goto saw_error;
43067 if (r)
43069 if (!cp_tree_equal (cmp_expr, r))
43070 goto bad_if;
43071 cmp_expr = rhs;
43072 rhs = NULL_TREE;
43073 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
43075 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43077 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43079 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43080 "expected %<==%> comparison in %<if%> condition");
43081 goto saw_error;
43083 else if (TREE_CODE (cmp_expr) != GT_EXPR
43084 && TREE_CODE (cmp_expr) != LT_EXPR)
43086 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43087 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
43088 "condition");
43089 goto saw_error;
43091 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43092 goto saw_error;
43094 extra_scope = true;
43095 eloc = cp_lexer_peek_token (parser->lexer)->location;
43096 lhs = cp_parser_unary_expression (parser);
43097 orig_lhs = lhs;
43098 if (lhs == error_mark_node)
43099 goto saw_error;
43100 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43102 cp_parser_error (parser, "expected %<=%>");
43103 goto saw_error;
43105 cp_lexer_consume_token (parser->lexer);
43106 eloc = cp_lexer_peek_token (parser->lexer)->location;
43107 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43108 rhs1 = cp_parser_expression (parser);
43109 else
43110 rhs1 = cp_parser_simple_cast_expression (parser);
43112 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43113 goto saw_error;
43115 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43116 goto saw_error;
43118 extra_scope = false;
43119 no_semicolon = true;
43121 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
43123 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43125 opcode = COND_EXPR;
43126 rhs = TREE_OPERAND (cmp_expr, 1);
43128 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
43130 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43131 ? MIN_EXPR : MAX_EXPR);
43132 rhs = rhs1;
43133 rhs1 = TREE_OPERAND (cmp_expr, 0);
43135 else
43136 goto bad_if;
43138 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
43139 goto bad_if;
43140 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
43141 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
43143 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43144 ? MAX_EXPR : MIN_EXPR);
43145 rhs = rhs1;
43146 rhs1 = TREE_OPERAND (cmp_expr, 1);
43148 else
43150 bad_if:
43151 cp_parser_error (parser,
43152 "invalid form of %<#pragma omp atomic compare%>");
43153 goto saw_error;
43156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
43158 if (code != OMP_ATOMIC_CAPTURE_NEW
43159 || (structured_block && r == NULL_TREE)
43160 || TREE_CODE (cmp_expr) != EQ_EXPR)
43162 eloc = cp_lexer_peek_token (parser->lexer)->location;
43163 error_at (eloc, "unexpected %<else%>");
43164 goto saw_error;
43167 cp_lexer_consume_token (parser->lexer);
43169 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43170 goto saw_error;
43172 extra_scope = true;
43173 v = cp_parser_unary_expression (parser);
43174 if (v == error_mark_node)
43175 goto saw_error;
43176 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43177 goto saw_error;
43179 tree expr = cp_parser_simple_cast_expression (parser);
43181 if (!cp_tree_equal (expr, lhs))
43182 goto bad_if;
43184 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43185 goto saw_error;
43187 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43188 goto saw_error;
43190 extra_scope = false;
43191 code = OMP_ATOMIC_CAPTURE_OLD;
43192 if (r == NULL_TREE)
43193 /* Signal to c_finish_omp_atomic that in
43194 if (x == e) { x = d; } else { v = x; }
43195 case the store to v should be conditional. */
43196 r = void_list_node;
43198 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43200 cp_parser_error (parser, "expected %<else%>");
43201 goto saw_error;
43203 else if (code == OMP_ATOMIC_CAPTURE_NEW
43204 && r != NULL_TREE
43205 && v == NULL_TREE)
43206 code = OMP_ATOMIC;
43207 goto stmt_done;
43209 lhs = cp_parser_unary_expression (parser);
43210 orig_lhs = lhs;
43211 switch (TREE_CODE (lhs))
43213 case ERROR_MARK:
43214 goto saw_error;
43216 case POSTINCREMENT_EXPR:
43217 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43218 code = OMP_ATOMIC_CAPTURE_OLD;
43219 /* FALLTHROUGH */
43220 case PREINCREMENT_EXPR:
43221 lhs = TREE_OPERAND (lhs, 0);
43222 opcode = PLUS_EXPR;
43223 rhs = integer_one_node;
43224 if (compare)
43225 goto invalid_compare;
43226 break;
43228 case POSTDECREMENT_EXPR:
43229 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43230 code = OMP_ATOMIC_CAPTURE_OLD;
43231 /* FALLTHROUGH */
43232 case PREDECREMENT_EXPR:
43233 lhs = TREE_OPERAND (lhs, 0);
43234 opcode = MINUS_EXPR;
43235 rhs = integer_one_node;
43236 if (compare)
43237 goto invalid_compare;
43238 break;
43240 case COMPOUND_EXPR:
43241 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
43242 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
43243 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
43244 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
43245 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
43246 (TREE_OPERAND (lhs, 1), 0), 0)))
43247 == BOOLEAN_TYPE)
43248 /* Undo effects of boolean_increment for post {in,de}crement. */
43249 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
43250 /* FALLTHRU */
43251 case MODIFY_EXPR:
43252 if (TREE_CODE (lhs) == MODIFY_EXPR
43253 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
43255 /* Undo effects of boolean_increment. */
43256 if (integer_onep (TREE_OPERAND (lhs, 1)))
43258 /* This is pre or post increment. */
43259 rhs = TREE_OPERAND (lhs, 1);
43260 lhs = TREE_OPERAND (lhs, 0);
43261 opcode = NOP_EXPR;
43262 if (code == OMP_ATOMIC_CAPTURE_NEW
43263 && !structured_block
43264 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
43265 code = OMP_ATOMIC_CAPTURE_OLD;
43266 if (compare)
43267 goto invalid_compare;
43268 break;
43271 /* FALLTHRU */
43272 default:
43273 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43275 cp_parser_error (parser, "expected %<=%>");
43276 goto saw_error;
43278 switch (cp_lexer_peek_token (parser->lexer)->type)
43280 case CPP_MULT_EQ:
43281 opcode = MULT_EXPR;
43282 break;
43283 case CPP_DIV_EQ:
43284 opcode = TRUNC_DIV_EXPR;
43285 break;
43286 case CPP_PLUS_EQ:
43287 opcode = PLUS_EXPR;
43288 break;
43289 case CPP_MINUS_EQ:
43290 opcode = MINUS_EXPR;
43291 break;
43292 case CPP_LSHIFT_EQ:
43293 opcode = LSHIFT_EXPR;
43294 break;
43295 case CPP_RSHIFT_EQ:
43296 opcode = RSHIFT_EXPR;
43297 break;
43298 case CPP_AND_EQ:
43299 opcode = BIT_AND_EXPR;
43300 break;
43301 case CPP_OR_EQ:
43302 opcode = BIT_IOR_EXPR;
43303 break;
43304 case CPP_XOR_EQ:
43305 opcode = BIT_XOR_EXPR;
43306 break;
43307 case CPP_EQ:
43308 enum cp_parser_prec oprec;
43309 cp_token *token;
43310 cp_lexer_consume_token (parser->lexer);
43311 cp_parser_parse_tentatively (parser);
43312 rhs1 = cp_parser_simple_cast_expression (parser);
43313 if (rhs1 == error_mark_node)
43315 cp_parser_abort_tentative_parse (parser);
43316 cp_parser_simple_cast_expression (parser);
43317 goto saw_error;
43319 token = cp_lexer_peek_token (parser->lexer);
43320 if (token->type != CPP_SEMICOLON
43321 && (!compare || token->type != CPP_QUERY)
43322 && !cp_tree_equal (lhs, rhs1))
43324 cp_parser_abort_tentative_parse (parser);
43325 cp_parser_parse_tentatively (parser);
43326 rhs = cp_parser_binary_expression (parser, false, true,
43327 PREC_NOT_OPERATOR, NULL);
43328 if (rhs == error_mark_node)
43330 cp_parser_abort_tentative_parse (parser);
43331 cp_parser_binary_expression (parser, false, true,
43332 PREC_NOT_OPERATOR, NULL);
43333 goto saw_error;
43335 switch (TREE_CODE (rhs))
43337 case MULT_EXPR:
43338 case TRUNC_DIV_EXPR:
43339 case RDIV_EXPR:
43340 case PLUS_EXPR:
43341 case MINUS_EXPR:
43342 case LSHIFT_EXPR:
43343 case RSHIFT_EXPR:
43344 case BIT_AND_EXPR:
43345 case BIT_IOR_EXPR:
43346 case BIT_XOR_EXPR:
43347 if (compare)
43348 break;
43349 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
43351 if (cp_parser_parse_definitely (parser))
43353 opcode = TREE_CODE (rhs);
43354 rhs1 = TREE_OPERAND (rhs, 0);
43355 rhs = TREE_OPERAND (rhs, 1);
43356 goto stmt_done;
43358 else
43359 goto saw_error;
43361 break;
43362 case EQ_EXPR:
43363 if (!compare
43364 || code != OMP_ATOMIC_CAPTURE_NEW
43365 || !structured_block
43366 || v
43367 || r)
43368 break;
43369 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
43370 && cp_lexer_nth_token_is_keyword (parser->lexer,
43371 2, RID_IF))
43373 if (cp_parser_parse_definitely (parser))
43375 r = lhs;
43376 lhs = NULL_TREE;
43377 rhs1 = NULL_TREE;
43378 cp_lexer_consume_token (parser->lexer);
43379 goto restart;
43382 break;
43383 case GT_EXPR:
43384 case LT_EXPR:
43385 if (compare
43386 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
43387 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
43388 && cp_parser_parse_definitely (parser))
43390 opcode = TREE_CODE (rhs);
43391 rhs1 = TREE_OPERAND (rhs, 0);
43392 rhs = TREE_OPERAND (rhs, 1);
43393 cond_expr:
43394 cp_lexer_consume_token (parser->lexer);
43395 bool saved_colon_corrects_to_scope_p
43396 = parser->colon_corrects_to_scope_p;
43397 parser->colon_corrects_to_scope_p = false;
43398 tree e1 = cp_parser_expression (parser);
43399 parser->colon_corrects_to_scope_p
43400 = saved_colon_corrects_to_scope_p;
43401 cp_parser_require (parser, CPP_COLON, RT_COLON);
43402 tree e2 = cp_parser_simple_cast_expression (parser);
43403 if (cp_tree_equal (lhs, e2))
43405 if (cp_tree_equal (lhs, rhs1))
43407 if (opcode == EQ_EXPR)
43409 opcode = COND_EXPR;
43410 rhs1 = e1;
43411 goto stmt_done;
43413 if (cp_tree_equal (rhs, e1))
43415 opcode
43416 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
43417 rhs = e1;
43418 goto stmt_done;
43421 else
43423 gcc_assert (opcode != EQ_EXPR);
43424 if (cp_tree_equal (rhs1, e1))
43426 opcode
43427 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
43428 rhs1 = rhs;
43429 rhs = e1;
43430 goto stmt_done;
43434 cp_parser_error (parser,
43435 "invalid form of "
43436 "%<#pragma omp atomic compare%>");
43437 goto saw_error;
43439 break;
43440 default:
43441 break;
43443 cp_parser_abort_tentative_parse (parser);
43444 if (structured_block
43445 && code == OMP_ATOMIC_CAPTURE_OLD
43446 && !compare)
43448 rhs = cp_parser_expression (parser);
43449 if (rhs == error_mark_node)
43450 goto saw_error;
43451 opcode = NOP_EXPR;
43452 rhs1 = NULL_TREE;
43453 goto stmt_done;
43455 cp_parser_error (parser,
43456 "invalid form of %<#pragma omp atomic%>");
43457 goto saw_error;
43459 if (!cp_parser_parse_definitely (parser))
43460 goto saw_error;
43461 switch (token->type)
43463 case CPP_SEMICOLON:
43464 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43466 code = OMP_ATOMIC_CAPTURE_OLD;
43467 v = lhs;
43468 lhs = NULL_TREE;
43469 lhs1 = rhs1;
43470 rhs1 = NULL_TREE;
43471 cp_lexer_consume_token (parser->lexer);
43472 goto restart;
43474 else if (structured_block && !compare)
43476 opcode = NOP_EXPR;
43477 rhs = rhs1;
43478 rhs1 = NULL_TREE;
43479 goto stmt_done;
43481 cp_parser_error (parser,
43482 "invalid form of %<#pragma omp atomic%>");
43483 goto saw_error;
43484 case CPP_MULT:
43485 opcode = MULT_EXPR;
43486 break;
43487 case CPP_DIV:
43488 opcode = TRUNC_DIV_EXPR;
43489 break;
43490 case CPP_PLUS:
43491 opcode = PLUS_EXPR;
43492 break;
43493 case CPP_MINUS:
43494 opcode = MINUS_EXPR;
43495 break;
43496 case CPP_LSHIFT:
43497 opcode = LSHIFT_EXPR;
43498 break;
43499 case CPP_RSHIFT:
43500 opcode = RSHIFT_EXPR;
43501 break;
43502 case CPP_AND:
43503 opcode = BIT_AND_EXPR;
43504 break;
43505 case CPP_OR:
43506 opcode = BIT_IOR_EXPR;
43507 break;
43508 case CPP_XOR:
43509 opcode = BIT_XOR_EXPR;
43510 break;
43511 case CPP_EQ_EQ:
43512 opcode = EQ_EXPR;
43513 break;
43514 case CPP_GREATER:
43515 opcode = GT_EXPR;
43516 break;
43517 case CPP_LESS:
43518 opcode = LT_EXPR;
43519 break;
43520 default:
43521 cp_parser_error (parser,
43522 "invalid operator for %<#pragma omp atomic%>");
43523 goto saw_error;
43525 if (compare
43526 && TREE_CODE_CLASS (opcode) != tcc_comparison)
43528 cp_parser_error (parser,
43529 "invalid form of "
43530 "%<#pragma omp atomic compare%>");
43531 goto saw_error;
43533 oprec = TOKEN_PRECEDENCE (token);
43534 gcc_assert (oprec != PREC_NOT_OPERATOR);
43535 if (commutative_tree_code (opcode))
43536 oprec = (enum cp_parser_prec) (oprec - 1);
43537 cp_lexer_consume_token (parser->lexer);
43538 rhs = cp_parser_binary_expression (parser, false, false,
43539 oprec, NULL);
43540 if (rhs == error_mark_node)
43541 goto saw_error;
43542 if (compare)
43544 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
43546 cp_parser_error (parser,
43547 "invalid form of "
43548 "%<#pragma omp atomic compare%>");
43549 goto saw_error;
43551 goto cond_expr;
43553 goto stmt_done;
43554 default:
43555 cp_parser_error (parser,
43556 "invalid operator for %<#pragma omp atomic%>");
43557 goto saw_error;
43559 cp_lexer_consume_token (parser->lexer);
43561 rhs = cp_parser_expression (parser);
43562 if (rhs == error_mark_node)
43563 goto saw_error;
43564 break;
43566 stmt_done:
43567 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
43569 if (!no_semicolon
43570 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43571 goto saw_error;
43572 no_semicolon = false;
43573 v = cp_parser_unary_expression (parser);
43574 if (v == error_mark_node)
43575 goto saw_error;
43576 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43577 goto saw_error;
43578 lhs1 = cp_parser_unary_expression (parser);
43579 if (lhs1 == error_mark_node)
43580 goto saw_error;
43582 if (structured_block)
43584 if (!no_semicolon)
43585 cp_parser_consume_semicolon_at_end_of_statement (parser);
43586 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
43588 done:
43589 if (weak && opcode != COND_EXPR)
43591 error_at (loc, "%<weak%> clause requires atomic equality comparison");
43592 weak = false;
43594 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43595 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
43596 rhs1, r, clauses, memory_order, weak);
43597 if (!structured_block && !no_semicolon)
43598 cp_parser_consume_semicolon_at_end_of_statement (parser);
43599 return;
43601 invalid_compare:
43602 error ("invalid form of %<pragma omp atomic compare%>");
43603 /* FALLTHRU */
43604 saw_error:
43605 cp_parser_skip_to_end_of_block_or_statement (parser);
43606 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43607 cp_lexer_consume_token (parser->lexer);
43608 if (structured_block)
43610 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43611 cp_lexer_consume_token (parser->lexer);
43612 else if (code == OMP_ATOMIC_CAPTURE_NEW)
43614 cp_parser_skip_to_end_of_block_or_statement (parser);
43615 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43616 cp_lexer_consume_token (parser->lexer);
43622 /* OpenMP 2.5:
43623 # pragma omp barrier new-line */
43625 static void
43626 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
43628 cp_parser_require_pragma_eol (parser, pragma_tok);
43629 finish_omp_barrier ();
43632 /* OpenMP 2.5:
43633 # pragma omp critical [(name)] new-line
43634 structured-block
43636 OpenMP 4.5:
43637 # pragma omp critical [(name) [hint(expression)]] new-line
43638 structured-block */
43640 #define OMP_CRITICAL_CLAUSE_MASK \
43641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
43643 static tree
43644 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43646 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
43648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43650 matching_parens parens;
43651 parens.consume_open (parser);
43653 name = cp_parser_identifier (parser);
43655 if (name == error_mark_node
43656 || !parens.require_close (parser))
43657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43658 /*or_comma=*/false,
43659 /*consume_paren=*/true);
43660 if (name == error_mark_node)
43661 name = NULL;
43663 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43664 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43665 cp_lexer_consume_token (parser->lexer);
43668 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
43669 "#pragma omp critical", pragma_tok);
43671 stmt = cp_parser_omp_structured_block (parser, if_p);
43672 return c_finish_omp_critical (input_location, stmt, name, clauses);
43675 /* OpenMP 5.0:
43676 # pragma omp depobj ( depobj ) depobj-clause new-line
43678 depobj-clause:
43679 depend (dependence-type : locator)
43680 destroy
43681 update (dependence-type)
43683 OpenMP 5.2 additionally:
43684 destroy ( depobj )
43686 dependence-type:
43689 inout
43690 mutexinout */
43692 static void
43693 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
43695 location_t loc = pragma_tok->location;
43696 matching_parens parens;
43697 if (!parens.require_open (parser))
43699 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43700 return;
43703 tree depobj = cp_parser_assignment_expression (parser);
43705 if (!parens.require_close (parser))
43706 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43707 /*or_comma=*/false,
43708 /*consume_paren=*/true);
43710 tree clause = NULL_TREE;
43711 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
43712 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43713 cp_lexer_consume_token (parser->lexer);
43714 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
43715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43718 const char *p = IDENTIFIER_POINTER (id);
43720 cp_lexer_consume_token (parser->lexer);
43721 if (!strcmp ("depend", p))
43723 /* Don't create location wrapper nodes within the depend clause. */
43724 auto_suppress_location_wrappers sentinel;
43725 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
43726 if (clause)
43727 clause = finish_omp_clauses (clause, C_ORT_OMP);
43728 if (!clause)
43729 clause = error_mark_node;
43731 else if (!strcmp ("destroy", p))
43733 kind = OMP_CLAUSE_DEPEND_LAST;
43734 matching_parens c_parens;
43735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43736 && c_parens.require_open (parser))
43738 tree destobj = cp_parser_assignment_expression (parser);
43739 if (depobj != error_mark_node
43740 && destobj != error_mark_node
43741 && !operand_equal_p (destobj, depobj, OEP_MATCH_SIDE_EFFECTS
43742 | OEP_LEXICOGRAPHIC))
43743 warning_at (EXPR_LOC_OR_LOC (destobj, c_loc), OPT_Wopenmp,
43744 "the %<destroy%> expression %qE should be the same "
43745 "as the %<depobj%> argument %qE", destobj, depobj);
43746 if (!c_parens.require_close (parser))
43747 cp_parser_skip_to_closing_parenthesis (parser,
43748 /*recovering=*/true,
43749 /*or_comma=*/false,
43750 /*consume_paren=*/true);
43753 else if (!strcmp ("update", p))
43755 matching_parens c_parens;
43756 if (c_parens.require_open (parser))
43758 location_t c2_loc
43759 = cp_lexer_peek_token (parser->lexer)->location;
43760 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43762 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43763 const char *p2 = IDENTIFIER_POINTER (id2);
43765 cp_lexer_consume_token (parser->lexer);
43766 if (!strcmp ("in", p2))
43767 kind = OMP_CLAUSE_DEPEND_IN;
43768 else if (!strcmp ("out", p2))
43769 kind = OMP_CLAUSE_DEPEND_OUT;
43770 else if (!strcmp ("inout", p2))
43771 kind = OMP_CLAUSE_DEPEND_INOUT;
43772 else if (!strcmp ("mutexinoutset", p2))
43773 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43774 else if (!strcmp ("inoutset", p2))
43775 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43777 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43779 clause = error_mark_node;
43780 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43781 "%<mutexinoutset%> or %<inoutset%>");
43783 if (!c_parens.require_close (parser))
43784 cp_parser_skip_to_closing_parenthesis (parser,
43785 /*recovering=*/true,
43786 /*or_comma=*/false,
43787 /*consume_paren=*/true);
43789 else
43790 clause = error_mark_node;
43793 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43795 clause = error_mark_node;
43796 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43798 cp_parser_require_pragma_eol (parser, pragma_tok);
43800 finish_omp_depobj (loc, depobj, kind, clause);
43804 /* OpenMP 2.5:
43805 # pragma omp flush flush-vars[opt] new-line
43807 flush-vars:
43808 ( variable-list )
43810 OpenMP 5.0:
43811 # pragma omp flush memory-order-clause new-line */
43813 static void
43814 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43816 enum memmodel mo = MEMMODEL_LAST;
43817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43818 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43819 cp_lexer_consume_token (parser->lexer);
43820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43822 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43823 const char *p = IDENTIFIER_POINTER (id);
43824 if (!strcmp (p, "seq_cst"))
43825 mo = MEMMODEL_SEQ_CST;
43826 else if (!strcmp (p, "acq_rel"))
43827 mo = MEMMODEL_ACQ_REL;
43828 else if (!strcmp (p, "release"))
43829 mo = MEMMODEL_RELEASE;
43830 else if (!strcmp (p, "acquire"))
43831 mo = MEMMODEL_ACQUIRE;
43832 else
43833 error_at (cp_lexer_peek_token (parser->lexer)->location,
43834 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43835 "%<acquire%>");
43836 cp_lexer_consume_token (parser->lexer);
43838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43840 if (mo != MEMMODEL_LAST)
43841 error_at (cp_lexer_peek_token (parser->lexer)->location,
43842 "%<flush%> list specified together with memory order "
43843 "clause");
43844 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43846 cp_parser_require_pragma_eol (parser, pragma_tok);
43848 finish_omp_flush (mo);
43851 /* Helper function, to parse omp for increment expression. */
43853 static tree
43854 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43856 tree cond = cp_parser_binary_expression (parser, false, true,
43857 PREC_NOT_OPERATOR, NULL);
43858 if (cond == error_mark_node
43859 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43861 cp_parser_skip_to_end_of_statement (parser);
43862 return error_mark_node;
43865 switch (TREE_CODE (cond))
43867 case GT_EXPR:
43868 case GE_EXPR:
43869 case LT_EXPR:
43870 case LE_EXPR:
43871 break;
43872 case NE_EXPR:
43873 if (code != OACC_LOOP)
43874 break;
43875 gcc_fallthrough ();
43876 default:
43877 return error_mark_node;
43880 /* If decl is an iterator, preserve LHS and RHS of the relational
43881 expr until finish_omp_for. */
43882 if (decl
43883 && (type_dependent_expression_p (decl)
43884 || CLASS_TYPE_P (TREE_TYPE (decl))))
43885 return cond;
43887 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
43888 TREE_CODE (cond),
43889 TREE_OPERAND (cond, 0), ERROR_MARK,
43890 TREE_OPERAND (cond, 1), ERROR_MARK,
43891 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
43894 /* Helper function, to parse omp for increment expression. */
43896 static tree
43897 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
43899 cp_token *token = cp_lexer_peek_token (parser->lexer);
43900 enum tree_code op;
43901 tree lhs, rhs;
43902 cp_id_kind idk;
43903 bool decl_first;
43905 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43907 op = (token->type == CPP_PLUS_PLUS
43908 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
43909 cp_lexer_consume_token (parser->lexer);
43910 lhs = cp_parser_simple_cast_expression (parser);
43911 if (lhs != decl
43912 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43913 return error_mark_node;
43914 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43917 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
43918 if (lhs != decl
43919 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43920 return error_mark_node;
43922 token = cp_lexer_peek_token (parser->lexer);
43923 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43925 op = (token->type == CPP_PLUS_PLUS
43926 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
43927 cp_lexer_consume_token (parser->lexer);
43928 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43931 op = cp_parser_assignment_operator_opt (parser);
43932 if (op == ERROR_MARK)
43933 return error_mark_node;
43935 if (op != NOP_EXPR)
43937 rhs = cp_parser_assignment_expression (parser);
43938 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43939 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43942 lhs = cp_parser_binary_expression (parser, false, false,
43943 PREC_ADDITIVE_EXPRESSION, NULL);
43944 token = cp_lexer_peek_token (parser->lexer);
43945 decl_first = (lhs == decl
43946 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43947 if (decl_first)
43948 lhs = NULL_TREE;
43949 if (token->type != CPP_PLUS
43950 && token->type != CPP_MINUS)
43951 return error_mark_node;
43955 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43956 cp_lexer_consume_token (parser->lexer);
43957 rhs = cp_parser_binary_expression (parser, false, false,
43958 PREC_ADDITIVE_EXPRESSION, NULL);
43959 token = cp_lexer_peek_token (parser->lexer);
43960 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43962 if (lhs == NULL_TREE)
43964 if (op == PLUS_EXPR)
43965 lhs = rhs;
43966 else
43967 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43968 NULL_TREE, tf_warning_or_error);
43970 else
43971 lhs = build_x_binary_op (input_location, op,
43972 lhs, ERROR_MARK,
43973 rhs, ERROR_MARK,
43974 NULL_TREE, NULL, tf_warning_or_error);
43977 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43979 if (!decl_first)
43981 if ((rhs != decl
43982 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43983 || op == MINUS_EXPR)
43984 return error_mark_node;
43985 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43987 else
43988 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43990 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43993 /* Parse the initialization statement of an OpenMP for loop. Range-for
43994 is handled separately in cp_convert_omp_range_for.
43996 On entry SL is the current statement list. Parsing of some forms
43997 of initialization pops this list and stores its contents in either INIT
43998 or THIS_PRE_BODY, and sets SL to null. Initialization for class
43999 iterators is added directly to SL and it is not popped until later.
44001 On return, DECL is set if the initialization is by binding the
44002 iteration variable. If the initialization is by assignment, REAL_DECL
44003 is set to point to a variable in an outer scope. ORIG_INIT is set
44004 if the iteration variable is of class type; this is a copy saved for
44005 error checking in finish_omp_for.
44007 Return true if the resulting construct should have an
44008 OMP_CLAUSE_PRIVATE added to it. */
44010 static tree
44011 cp_parser_omp_for_loop_init (cp_parser *parser,
44012 tree &this_pre_body,
44013 tree &sl,
44014 tree &init,
44015 tree &orig_init,
44016 tree &decl,
44017 tree &real_decl)
44019 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
44020 return NULL_TREE;
44022 tree add_private_clause = NULL_TREE;
44024 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
44026 init-expr:
44027 var = lb
44028 integer-type var = lb
44029 random-access-iterator-type var = lb
44030 pointer-type var = lb
44032 cp_decl_specifier_seq type_specifiers;
44034 /* First, try to parse as an initialized declaration. See
44035 cp_parser_condition, from whence the bulk of this is copied. */
44037 cp_parser_parse_tentatively (parser);
44038 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
44039 /*is_declaration=*/true,
44040 /*is_trailing_return=*/false,
44041 &type_specifiers);
44042 if (cp_parser_parse_definitely (parser))
44044 /* If parsing a type specifier seq succeeded, then this
44045 MUST be a initialized declaration. */
44046 tree asm_specification, attributes;
44047 cp_declarator *declarator;
44049 declarator = cp_parser_declarator (parser,
44050 CP_PARSER_DECLARATOR_NAMED,
44051 CP_PARSER_FLAGS_NONE,
44052 /*ctor_dtor_or_conv_p=*/NULL,
44053 /*parenthesized_p=*/NULL,
44054 /*member_p=*/false,
44055 /*friend_p=*/false,
44056 /*static_p=*/false);
44057 attributes = cp_parser_attributes_opt (parser);
44058 asm_specification = cp_parser_asm_specification_opt (parser);
44060 if (declarator == cp_error_declarator)
44061 cp_parser_skip_to_end_of_statement (parser);
44063 else
44065 tree pushed_scope, auto_node;
44067 decl = start_decl (declarator, &type_specifiers,
44068 SD_INITIALIZED, attributes,
44069 /*prefix_attributes=*/NULL_TREE,
44070 &pushed_scope);
44072 auto_node = type_uses_auto (TREE_TYPE (decl));
44073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
44075 if (cp_lexer_next_token_is (parser->lexer,
44076 CPP_OPEN_PAREN))
44077 error ("parenthesized initialization is not allowed in "
44078 "OpenMP %<for%> loop");
44079 else
44080 /* Trigger an error. */
44081 cp_parser_require (parser, CPP_EQ, RT_EQ);
44083 init = error_mark_node;
44084 cp_parser_skip_to_end_of_statement (parser);
44086 else if (CLASS_TYPE_P (TREE_TYPE (decl))
44087 || type_dependent_expression_p (decl)
44088 || auto_node)
44090 bool is_non_constant_init;
44092 init = cp_parser_initializer (parser,
44093 /*is_direct_init=*/nullptr,
44094 &is_non_constant_init);
44096 if (auto_node)
44098 TREE_TYPE (decl)
44099 = do_auto_deduction (TREE_TYPE (decl), init,
44100 auto_node);
44102 if (!CLASS_TYPE_P (TREE_TYPE (decl))
44103 && !type_dependent_expression_p (decl))
44104 goto non_class;
44107 cp_finish_decl (decl, init, !is_non_constant_init,
44108 asm_specification,
44109 LOOKUP_ONLYCONVERTING);
44110 orig_init = init;
44112 /* In the case of a class iterator, do not pop sl here.
44113 Both class initialization and finalization must happen in
44114 the enclosing init block scope. For now set the init
44115 expression to null; it'll be filled in properly in
44116 finish_omp_for before stuffing it in the OMP_FOR. */
44117 if (CLASS_TYPE_P (TREE_TYPE (decl)))
44118 init = NULL_TREE;
44119 else /* It is a parameterized type. */
44121 init = pop_stmt_list (sl);
44122 sl = NULL_TREE;
44123 if (init && TREE_CODE (init) == STATEMENT_LIST)
44125 tree_stmt_iterator i = tsi_start (init);
44126 /* Move lambda DECL_EXPRs to the enclosing block. */
44127 while (!tsi_end_p (i))
44129 tree t = tsi_stmt (i);
44130 if (TREE_CODE (t) == DECL_EXPR
44131 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
44133 tsi_delink (&i);
44134 add_stmt (t);
44135 continue;
44137 break;
44139 if (tsi_one_before_end_p (i))
44141 tree t = tsi_stmt (i);
44142 tsi_delink (&i);
44143 free_stmt_list (init);
44144 init = t;
44149 else
44150 /* This is an initialized declaration of non-class,
44151 non-parameterized type iteration variable. */
44153 /* Consume '='. */
44154 cp_lexer_consume_token (parser->lexer);
44155 init = cp_parser_assignment_expression (parser);
44157 non_class:
44158 if (TYPE_REF_P (TREE_TYPE (decl)))
44159 init = error_mark_node;
44160 else
44161 cp_finish_decl (decl, NULL_TREE,
44162 /*init_const_expr_p=*/false,
44163 asm_specification,
44164 LOOKUP_ONLYCONVERTING);
44165 this_pre_body = pop_stmt_list (sl);
44166 sl = NULL_TREE;
44169 if (pushed_scope)
44170 pop_scope (pushed_scope);
44173 else
44175 cp_id_kind idk;
44176 /* If parsing a type specifier sequence failed, then
44177 this MUST be a simple expression. */
44178 cp_parser_parse_tentatively (parser);
44179 decl = cp_parser_primary_expression (parser, false, false,
44180 false, &idk);
44181 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
44182 if (!cp_parser_error_occurred (parser)
44183 && decl
44184 && (TREE_CODE (decl) == COMPONENT_REF
44185 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
44187 cp_parser_abort_tentative_parse (parser);
44188 cp_parser_parse_tentatively (parser);
44189 cp_token *token = cp_lexer_peek_token (parser->lexer);
44190 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
44191 /*check_dependency_p=*/true,
44192 /*template_p=*/NULL,
44193 /*declarator_p=*/false,
44194 /*optional_p=*/false);
44195 if (name != error_mark_node
44196 && last_tok == cp_lexer_peek_token (parser->lexer))
44198 decl = cp_parser_lookup_name_simple (parser, name,
44199 token->location);
44200 if (TREE_CODE (decl) == FIELD_DECL)
44201 add_private_clause = omp_privatize_field (decl, false);
44203 cp_parser_abort_tentative_parse (parser);
44204 cp_parser_parse_tentatively (parser);
44205 decl = cp_parser_primary_expression (parser, false, false,
44206 false, &idk);
44208 if (!cp_parser_error_occurred (parser)
44209 && decl
44210 && DECL_P (decl)
44211 && CLASS_TYPE_P (TREE_TYPE (decl)))
44213 tree rhs;
44215 cp_parser_parse_definitely (parser);
44216 cp_parser_require (parser, CPP_EQ, RT_EQ);
44217 rhs = cp_parser_assignment_expression (parser);
44218 orig_init = rhs;
44219 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
44220 decl, NOP_EXPR,
44221 rhs, NULL_TREE,
44222 tf_warning_or_error));
44223 if (!add_private_clause)
44224 add_private_clause = decl;
44226 else
44228 decl = NULL;
44229 cp_parser_abort_tentative_parse (parser);
44230 init = cp_parser_expression (parser);
44231 if (init)
44233 if (TREE_CODE (init) == MODIFY_EXPR
44234 || TREE_CODE (init) == MODOP_EXPR)
44235 real_decl = TREE_OPERAND (init, 0);
44238 this_pre_body = pop_stmt_list (sl);
44239 sl = NULL_TREE;
44241 return add_private_clause;
44244 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
44245 including introducing new temporaries for the range start and end,
44246 doing auto deduction, and processing decomposition variables.
44248 This function is also called from pt.cc during template instantiation.
44249 In that case SL is NULL_TREE, otherwise it is the current statement
44250 list. */
44251 void
44252 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
44253 tree &decl, tree &orig_decl, tree &init,
44254 tree &orig_init, tree &cond, tree &incr)
44256 tree begin, end, range_temp_decl = NULL_TREE;
44257 tree iter_type, begin_expr, end_expr;
44258 bool clear_has_value_expr = false;
44260 if (processing_template_decl)
44262 if (check_for_bare_parameter_packs (init))
44263 init = error_mark_node;
44264 if (!type_dependent_expression_p (init)
44265 /* do_auto_deduction doesn't mess with template init-lists. */
44266 && !BRACE_ENCLOSED_INITIALIZER_P (init))
44268 tree d = decl;
44269 cp_decomp decomp_d, *decomp = NULL;
44270 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
44272 tree v = DECL_VALUE_EXPR (decl);
44273 if (TREE_CODE (v) == ARRAY_REF
44274 && VAR_P (TREE_OPERAND (v, 0))
44275 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44277 d = TREE_OPERAND (v, 0);
44278 decomp = &decomp_d;
44279 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44280 decomp->decl = decl;
44283 do_range_for_auto_deduction (d, init, decomp);
44285 cond = global_namespace;
44286 incr = NULL_TREE;
44287 orig_init = init;
44288 if (sl)
44290 this_pre_body = pop_stmt_list (sl);
44291 sl = NULL_TREE;
44293 return;
44296 init = mark_lvalue_use (init);
44298 if (decl == error_mark_node || init == error_mark_node)
44299 /* If an error happened previously do nothing or else a lot of
44300 unhelpful errors would be issued. */
44301 begin_expr = end_expr = iter_type = error_mark_node;
44302 else
44304 tree range_temp;
44306 if (VAR_P (init)
44307 && array_of_runtime_bound_p (TREE_TYPE (init)))
44308 /* Can't bind a reference to an array of runtime bound. */
44309 range_temp = init;
44310 else
44312 range_temp = build_range_temp (init);
44313 DECL_NAME (range_temp) = NULL_TREE;
44314 pushdecl (range_temp);
44315 cp_finish_decl (range_temp, init,
44316 /*is_constant_init*/false, NULL_TREE,
44317 LOOKUP_ONLYCONVERTING);
44318 range_temp_decl = range_temp;
44319 range_temp = convert_from_reference (range_temp);
44321 iter_type = cp_parser_perform_range_for_lookup (range_temp,
44322 &begin_expr, &end_expr);
44325 tree end_iter_type = iter_type;
44326 if (cxx_dialect >= cxx17)
44327 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
44328 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
44329 TREE_USED (end) = 1;
44330 DECL_ARTIFICIAL (end) = 1;
44331 pushdecl (end);
44332 cp_finish_decl (end, end_expr,
44333 /*is_constant_init*/false, NULL_TREE,
44334 LOOKUP_ONLYCONVERTING);
44336 /* The new for initialization statement. */
44337 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
44338 TREE_USED (begin) = 1;
44339 DECL_ARTIFICIAL (begin) = 1;
44340 pushdecl (begin);
44341 orig_init = init;
44342 if (CLASS_TYPE_P (iter_type))
44343 init = NULL_TREE;
44344 else
44346 init = begin_expr;
44347 begin_expr = NULL_TREE;
44349 cp_finish_decl (begin, begin_expr,
44350 /*is_constant_init*/false, NULL_TREE,
44351 LOOKUP_ONLYCONVERTING);
44353 /* The new for condition. */
44354 if (CLASS_TYPE_P (iter_type))
44355 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
44356 else
44357 cond = build_x_binary_op (input_location, NE_EXPR,
44358 begin, ERROR_MARK,
44359 end, ERROR_MARK,
44360 NULL_TREE, NULL, tf_warning_or_error);
44362 /* The new increment expression. */
44363 if (CLASS_TYPE_P (iter_type))
44364 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
44365 else
44366 incr = finish_unary_op_expr (input_location,
44367 PREINCREMENT_EXPR, begin,
44368 tf_warning_or_error);
44370 orig_decl = decl;
44371 decl = begin;
44372 /* Defer popping sl here. */
44374 cp_decomp decomp_d, *decomp = NULL;
44375 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
44377 tree v = DECL_VALUE_EXPR (orig_decl);
44378 if (TREE_CODE (v) == ARRAY_REF
44379 && VAR_P (TREE_OPERAND (v, 0))
44380 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44382 tree d = orig_decl;
44383 orig_decl = TREE_OPERAND (v, 0);
44384 decomp = &decomp_d;
44385 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44386 decomp->decl = d;
44390 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
44391 if (auto_node)
44393 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44394 NULL_TREE, tf_none);
44395 if (!error_operand_p (t))
44397 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
44398 t, auto_node);
44399 if (decomp)
44401 ++processing_template_decl;
44402 cp_finish_decomp (orig_decl, decomp);
44403 --processing_template_decl;
44404 if (!processing_template_decl)
44405 clear_has_value_expr = true;
44410 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
44411 that holds decls for variables implementing the iterator, represented
44412 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
44413 of the vector are decls of scratch variables for the range start and
44414 end that will eventually be bound in the implicit scope surrounding
44415 the whole loop nest. The remaining elements are decls of derived
44416 decomposition variables that are bound inside the loop body. This
44417 structure is further mangled by finish_omp_for into the form required
44418 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
44419 unsigned decomp_cnt = decomp ? decomp->count : 0;
44420 tree v = make_tree_vec (decomp_cnt + 3);
44421 TREE_VEC_ELT (v, 0) = range_temp_decl;
44422 TREE_VEC_ELT (v, 1) = end;
44423 TREE_VEC_ELT (v, 2) = orig_decl;
44424 if (clear_has_value_expr)
44425 TREE_PUBLIC (v) = 1;
44426 for (unsigned i = 0; i < decomp_cnt; i++)
44428 if (clear_has_value_expr)
44430 /* If cp_finish_decomp was called with processing_template_decl
44431 temporarily set to 1, then decomp names will have deduced
44432 name but the DECL_VALUE_EXPR will be dependent. Hide those
44433 from folding of other loop initializers e.g. for warning
44434 purposes until cp_finish_omp_range_for. */
44435 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
44436 || (TREE_TYPE (decomp->decl)
44437 == error_mark_node));
44438 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
44440 TREE_VEC_ELT (v, i + 3) = decomp->decl;
44441 decomp->decl = DECL_CHAIN (decomp->decl);
44443 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
44446 /* Helper for cp_parser_omp_for_loop, finalize part of range for
44447 inside of the collapsed body. */
44449 void
44450 cp_finish_omp_range_for (tree orig, tree begin)
44452 gcc_assert (TREE_CODE (orig) == TREE_LIST
44453 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
44454 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
44455 cp_decomp decomp_d, *decomp = NULL;
44457 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44459 decomp = &decomp_d;
44460 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
44461 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
44462 if (TREE_PUBLIC (TREE_CHAIN (orig)))
44464 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
44465 by cp_convert_omp_range_for above. */
44466 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
44467 tree d = decomp_d.decl;
44468 for (unsigned i = 0; i < decomp_d.count; i++)
44470 if (TREE_TYPE (d) != error_mark_node)
44471 DECL_HAS_VALUE_EXPR_P (d) = 1;
44472 d = DECL_CHAIN (d);
44477 /* The declaration is initialized with *__begin inside the loop body. */
44478 cp_finish_decl (decl,
44479 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44480 NULL_TREE, tf_warning_or_error),
44481 /*is_constant_init*/false, NULL_TREE,
44482 LOOKUP_ONLYCONVERTING, decomp);
44483 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44484 cp_finish_decomp (decl, decomp);
44487 /* Return true if next tokens contain a standard attribute that contains
44488 omp::directive (DIRECTIVE). */
44490 static bool
44491 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
44492 bool tentative)
44494 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
44495 if (n < 10)
44496 return false;
44497 for (i = 5; i < n - 4; i++)
44498 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
44499 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
44500 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
44502 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
44503 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
44504 if (strcmp (IDENTIFIER_POINTER (first), "directive")
44505 && strcmp (IDENTIFIER_POINTER (first), "__directive__"))
44506 continue;
44507 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
44508 break;
44510 if (i == n - 4)
44511 return false;
44512 cp_parser_parse_tentatively (parser);
44513 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
44514 location_t last_loc
44515 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
44516 location_t middle_loc = UNKNOWN_LOCATION;
44517 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
44518 int cnt = 0;
44519 bool seen = false;
44520 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
44521 if (get_attribute_namespace (attr) == omp_identifier
44522 && is_attribute_p ("directive", get_attribute_name (attr)))
44524 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
44526 tree d = TREE_VALUE (a);
44527 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
44528 cp_token *first = DEFPARSE_TOKENS (d)->first;
44529 cnt++;
44530 if (first->type == CPP_NAME
44531 && strcmp (IDENTIFIER_POINTER (first->u.value),
44532 directive) == 0)
44534 seen = true;
44535 if (middle_loc == UNKNOWN_LOCATION)
44536 middle_loc = first->location;
44540 if (!seen || tentative)
44542 cp_parser_abort_tentative_parse (parser);
44543 return seen;
44545 if (cnt != 1 || TREE_CHAIN (std_attrs))
44547 error_at (make_location (first_loc, last_loc, middle_loc),
44548 "%<[[omp::directive(%s)]]%> must be the only specified "
44549 "attribute on a statement", directive);
44550 cp_parser_abort_tentative_parse (parser);
44551 return false;
44553 if (!cp_parser_parse_definitely (parser))
44554 return false;
44555 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
44556 return true;
44559 /* Parse an OpenMP structured block sequence. KIND is the corresponding
44560 separating directive. */
44562 static tree
44563 cp_parser_omp_structured_block_sequence (cp_parser *parser,
44564 enum pragma_kind kind)
44566 tree stmt = begin_omp_structured_block ();
44567 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44569 cp_parser_statement (parser, NULL_TREE, false, NULL);
44570 while (true)
44572 cp_token *token = cp_lexer_peek_token (parser->lexer);
44574 if (token->type == CPP_CLOSE_BRACE
44575 || token->type == CPP_EOF
44576 || token->type == CPP_PRAGMA_EOL
44577 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
44578 || (kind != PRAGMA_NONE
44579 && cp_parser_pragma_kind (token) == kind))
44580 break;
44582 if (kind != PRAGMA_NONE
44583 && cp_parser_omp_section_scan (parser,
44584 kind == PRAGMA_OMP_SCAN
44585 ? "scan" : "section", false))
44586 break;
44588 cp_parser_statement (parser, NULL_TREE, false, NULL);
44591 cp_parser_end_omp_structured_block (parser, save);
44592 return finish_omp_structured_block (stmt);
44596 /* OpenMP 5.0:
44598 scan-loop-body:
44599 { structured-block scan-directive structured-block } */
44601 static void
44602 cp_parser_omp_scan_loop_body (cp_parser *parser)
44604 tree substmt, clauses = NULL_TREE;
44605 bool found_scan = false;
44607 matching_braces braces;
44608 if (!braces.require_open (parser))
44609 return;
44611 cp_token *tok = cp_lexer_peek_token (parser->lexer);
44612 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
44613 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
44614 else
44616 warning_at (tok->location, OPT_Wopenmp,
44617 "%<#pragma omp scan%> with zero preceding executable "
44618 "statements");
44619 substmt = build_empty_stmt (tok->location);
44621 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
44622 add_stmt (substmt);
44624 tok = cp_lexer_peek_token (parser->lexer);
44625 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
44627 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
44628 found_scan = true;
44630 cp_lexer_consume_token (parser->lexer);
44632 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44633 cp_lexer_consume_token (parser->lexer);
44635 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44637 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44638 const char *p = IDENTIFIER_POINTER (id);
44639 if (strcmp (p, "inclusive") == 0)
44640 clause = OMP_CLAUSE_INCLUSIVE;
44641 else if (strcmp (p, "exclusive") == 0)
44642 clause = OMP_CLAUSE_EXCLUSIVE;
44644 if (clause != OMP_CLAUSE_ERROR)
44646 cp_lexer_consume_token (parser->lexer);
44647 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
44649 else
44650 cp_parser_error (parser, "expected %<inclusive%> or "
44651 "%<exclusive%> clause");
44653 cp_parser_require_pragma_eol (parser, tok);
44655 else
44656 error ("expected %<#pragma omp scan%>");
44658 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
44659 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44660 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
44661 else
44663 if (found_scan)
44664 warning_at (tok->location, OPT_Wopenmp,
44665 "%<#pragma omp scan%> with zero succeeding executable "
44666 "statements");
44667 substmt = build_empty_stmt (tok->location);
44669 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
44670 clauses);
44671 add_stmt (substmt);
44673 braces.require_close (parser);
44677 /* This function parses a single level of a loop nest, invoking itself
44678 recursively if necessary.
44680 loop-nest :: for (...) loop-body
44681 loop-body :: loop-nest
44682 | { [intervening-code] loop-body [intervening-code] }
44683 | final-loop-body
44684 intervening-code :: structured-block-sequence
44685 final-loop-body :: structured-block
44687 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
44688 all the iterator information from the inner loops into vectors in the
44689 parser->omp_for_parse_state structure.
44691 In the "range for" case, it is transformed into a regular "for" iterator
44692 by introducing some temporary variables for the begin/end,
44693 as well as bindings of the actual iteration variables which are
44694 injected into the body of the loop.
44696 Initialization code for iterator variables may end up either in the
44697 init vector (simple assignments), in omp_for_parse_state->pre_body
44698 (decl_exprs for iterators bound in the for statement), or in the
44699 scope surrounding this level of loop initialization.
44701 The scopes of class iterator variables and their finalizers need to
44702 be adjusted after parsing so that all of the initialization happens
44703 in a scope surrounding all of the intervening and body code. For
44704 this reason we separately store the initialization and body blocks
44705 for each level of loops in the omp_for_parse_state structure and
44706 reassemble/reorder them in cp_parser_omp_for. See additional
44707 comments there about the use of placeholders, etc. */
44709 static tree
44710 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
44712 tree decl, cond, incr, init;
44713 tree orig_init, real_decl, orig_decl;
44714 tree init_block, body_block;
44715 tree init_placeholder, body_placeholder;
44716 tree init_scope;
44717 tree this_pre_body = NULL_TREE;
44718 bool moreloops;
44719 unsigned char save_in_statement;
44720 tree add_private_clause = NULL_TREE;
44721 location_t loc;
44722 bool is_range_for = false;
44723 tree sl = NULL_TREE;
44724 struct omp_for_parse_data *omp_for_parse_state
44725 = parser->omp_for_parse_state;
44726 gcc_assert (omp_for_parse_state);
44727 int depth = omp_for_parse_state->depth;
44729 /* We have already matched the FOR token but not consumed it yet. */
44730 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
44731 loc = cp_lexer_consume_token (parser->lexer)->location;
44733 /* Forbid break/continue in the loop initializer, condition, and
44734 increment expressions. */
44735 save_in_statement = parser->in_statement;
44736 parser->in_statement = IN_OMP_BLOCK;
44738 /* We are not in intervening code now. */
44739 omp_for_parse_state->in_intervening_code = false;
44741 /* Don't create location wrapper nodes within an OpenMP "for"
44742 statement. */
44743 auto_suppress_location_wrappers sentinel;
44745 matching_parens parens;
44746 if (!parens.require_open (parser))
44747 return NULL;
44749 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44751 init_placeholder = build_stmt (input_location, EXPR_STMT,
44752 integer_zero_node);
44753 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44755 /* The init_block acts as a container for this level of loop goo. */
44756 init_block = push_stmt_list ();
44757 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44759 /* Wrap a scope around this entire level of loop to hold bindings
44760 of loop iteration variables. We can't insert them directly
44761 in the containing scope because that would cause their visibility to
44762 be incorrect with respect to intervening code after this loop.
44763 We will combine the nested init_scopes in postprocessing after the
44764 entire loop is parsed. */
44765 init_scope = begin_compound_stmt (0);
44767 /* Now we need another level of statement list container to capture the
44768 initialization (and possible finalization) bits. In some cases this
44769 container may be popped off during initializer parsing to store code in
44770 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44771 we have a class iterator we will pop it at the end of parsing this
44772 level, so the cleanups are handled correctly. */
44773 sl = push_stmt_list ();
44775 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44777 /* Save tokens so that we can put them back. */
44778 cp_lexer_save_tokens (parser->lexer);
44780 /* Look for ':' that is not nested in () or {}. */
44781 is_range_for
44782 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44783 /*recovering=*/false,
44784 CPP_COLON,
44785 /*consume_paren=*/
44786 false) == -1);
44788 /* Roll back the tokens we skipped. */
44789 cp_lexer_rollback_tokens (parser->lexer);
44791 if (is_range_for)
44793 bool saved_colon_corrects_to_scope_p
44794 = parser->colon_corrects_to_scope_p;
44796 /* A colon is used in range-based for. */
44797 parser->colon_corrects_to_scope_p = false;
44799 /* Parse the declaration. */
44800 cp_parser_simple_declaration (parser,
44801 /*function_definition_allowed_p=*/
44802 false, &decl);
44803 parser->colon_corrects_to_scope_p
44804 = saved_colon_corrects_to_scope_p;
44806 cp_parser_require (parser, CPP_COLON, RT_COLON);
44808 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44809 false, NULL_TREE, false, true);
44811 cp_convert_omp_range_for (this_pre_body, sl, decl,
44812 orig_decl, init, orig_init,
44813 cond, incr);
44815 if (omp_for_parse_state->ordered_cl)
44816 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44817 "%<ordered%> clause with parameter on "
44818 "range-based %<for%> loop");
44820 goto parse_close_paren;
44824 add_private_clause
44825 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44826 init, orig_init, decl, real_decl);
44828 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44830 /* If the iteration variable was introduced via a declaration in the
44831 for statement, DECL points at it. Otherwise DECL is null and
44832 REAL_DECL is a variable previously declared in an outer scope.
44833 Make REAL_DECL point at the iteration variable no matter where it
44834 was introduced. */
44835 if (decl)
44836 real_decl = decl;
44838 /* Some clauses treat iterator variables specially. */
44839 if (omp_for_parse_state->cclauses != NULL
44840 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44841 && real_decl != NULL_TREE
44842 && omp_for_parse_state->code != OMP_LOOP)
44844 tree *c;
44845 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44846 *c ; )
44847 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44848 && OMP_CLAUSE_DECL (*c) == real_decl)
44850 error_at (loc, "iteration variable %qD"
44851 " should not be firstprivate", real_decl);
44852 *c = OMP_CLAUSE_CHAIN (*c);
44854 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44855 && OMP_CLAUSE_DECL (*c) == real_decl)
44857 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44858 tree l = *c;
44859 *c = OMP_CLAUSE_CHAIN (*c);
44860 if (omp_for_parse_state->code == OMP_SIMD)
44862 OMP_CLAUSE_CHAIN (l)
44863 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44864 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44866 else
44868 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
44869 omp_for_parse_state->clauses = l;
44871 add_private_clause = NULL_TREE;
44873 else
44875 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
44876 && OMP_CLAUSE_DECL (*c) == real_decl)
44877 add_private_clause = NULL_TREE;
44878 c = &OMP_CLAUSE_CHAIN (*c);
44882 if (add_private_clause)
44884 tree c;
44885 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
44887 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
44888 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
44889 && OMP_CLAUSE_DECL (c) == decl)
44890 break;
44891 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
44892 && OMP_CLAUSE_DECL (c) == decl)
44893 error_at (loc, "iteration variable %qD "
44894 "should not be firstprivate",
44895 decl);
44896 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
44897 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44898 && OMP_CLAUSE_DECL (c) == decl)
44899 error_at (loc, "iteration variable %qD should not be reduction",
44900 decl);
44902 if (c == NULL)
44904 if ((omp_for_parse_state->code == OMP_SIMD
44905 && omp_for_parse_state->count != 1)
44906 || omp_for_parse_state->code == OMP_LOOP)
44907 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
44908 else if (omp_for_parse_state->code != OMP_SIMD)
44909 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
44910 else
44911 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
44912 OMP_CLAUSE_DECL (c) = add_private_clause;
44913 c = finish_omp_clauses (c, C_ORT_OMP);
44914 if (c)
44916 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
44917 omp_for_parse_state->clauses = c;
44918 /* For linear, signal that we need to fill up
44919 the so far unknown linear step. */
44920 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
44921 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
44926 cond = NULL;
44927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44928 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
44929 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44931 incr = NULL;
44932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
44934 /* If decl is an iterator, preserve the operator on decl
44935 until finish_omp_for. */
44936 if (real_decl
44937 && ((processing_template_decl
44938 && (TREE_TYPE (real_decl) == NULL_TREE
44939 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
44940 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
44941 incr = cp_parser_omp_for_incr (parser, real_decl);
44942 else
44943 incr = cp_parser_expression (parser);
44944 protected_set_expr_location_if_unset (incr, input_location);
44947 parse_close_paren:
44948 if (!parens.require_close (parser))
44949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44950 /*or_comma=*/false,
44951 /*consume_paren=*/true);
44953 /* We've parsed all the for (...) stuff now. Store the bits. */
44954 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
44955 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
44956 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
44957 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
44958 if (orig_init)
44960 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
44961 omp_for_parse_state->orig_inits[depth] = orig_init;
44963 if (orig_decl)
44965 if (!omp_for_parse_state->orig_declv)
44966 omp_for_parse_state->orig_declv
44967 = copy_node (omp_for_parse_state->declv);
44968 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
44970 else if (omp_for_parse_state->orig_declv)
44971 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
44972 if (this_pre_body)
44973 append_to_statement_list_force (this_pre_body,
44974 &(omp_for_parse_state->pre_body));
44976 /* Start a nested block for the loop body. */
44977 body_placeholder = build_stmt (input_location, EXPR_STMT,
44978 integer_zero_node);
44979 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
44980 body_block = push_stmt_list ();
44981 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
44983 moreloops = depth < omp_for_parse_state->count - 1;
44984 omp_for_parse_state->want_nested_loop = moreloops;
44985 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44987 omp_for_parse_state->depth++;
44988 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
44989 omp_for_parse_state->depth--;
44991 else if (moreloops
44992 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44994 /* This is the open brace in the loop-body grammar production. Rather
44995 than trying to special-case braces, just parse it as a compound
44996 statement and handle the nested loop-body case there. Note that
44997 when we see a further open brace inside the compound statement
44998 loop-body, we don't know whether it is the start of intervening
44999 code that is a compound statement, or a level of braces
45000 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
45001 bit to ensure we have only one nested loop at each level. */
45003 omp_for_parse_state->in_intervening_code = true;
45004 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
45005 omp_for_parse_state->in_intervening_code = false;
45007 if (omp_for_parse_state->want_nested_loop)
45009 /* We have already parsed the whole loop body and not found a
45010 nested loop. */
45011 error_at (omp_for_parse_state->for_loc,
45012 "not enough nested loops");
45013 omp_for_parse_state->fail = true;
45015 if_p = NULL;
45017 else
45019 /* This is the final-loop-body case in the grammar: we have something
45020 that is not a FOR and not an open brace. */
45021 if (moreloops)
45023 /* If we were expecting a nested loop, give an error and mark
45024 that parsing has failed, and try to recover by parsing the
45025 body as regular code without further collapsing. */
45026 error_at (omp_for_parse_state->for_loc,
45027 "not enough nested loops");
45028 omp_for_parse_state->fail = true;
45030 parser->in_statement = IN_OMP_FOR;
45032 /* Generate the parts of range for that belong in the loop body,
45033 to be executed on every iteration. This includes setting the
45034 user-declared decomposition variables from the compiler-generated
45035 temporaries that are the real iteration variables for OMP_FOR.
45036 FIXME: Not sure if this is correct with respect to visibility
45037 of the variables from intervening code. However, putting this
45038 code in each level of loop instead of all around the innermost
45039 body also makes the decomposition variables visible to the
45040 inner for init/bound/step exressions, which is not supposed to
45041 happen and causes test failures. */
45042 if (omp_for_parse_state->orig_declv)
45043 for (int i = 0; i < omp_for_parse_state->count; i++)
45045 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
45046 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
45047 if (o != d)
45048 cp_finish_omp_range_for (o, d);
45051 /* Now parse the final-loop-body for the innermost loop. */
45052 parser->omp_for_parse_state = NULL;
45053 if (omp_for_parse_state->inscan)
45054 cp_parser_omp_scan_loop_body (parser);
45055 else
45056 cp_parser_statement (parser, NULL_TREE, false, if_p);
45057 parser->omp_for_parse_state = omp_for_parse_state;
45059 parser->in_statement = save_in_statement;
45060 omp_for_parse_state->want_nested_loop = false;
45061 omp_for_parse_state->in_intervening_code = true;
45063 /* Pop and remember the body block. Add the body placeholder
45064 to the surrounding statement list instead. This is just a unique
45065 token that will be replaced when we reassemble the generated
45066 code for the entire omp for statement. */
45067 body_block = pop_stmt_list (body_block);
45068 omp_for_parse_state->body_blockv[depth] = body_block;
45069 add_stmt (body_placeholder);
45071 /* Pop and remember the init block. */
45072 if (sl)
45073 add_stmt (pop_stmt_list (sl));
45074 finish_compound_stmt (init_scope);
45075 init_block = pop_stmt_list (init_block);
45076 omp_for_parse_state->init_blockv[depth] = init_block;
45078 /* Return the init placeholder rather than the remembered init block.
45079 Again, this is just a unique cookie that will be used to reassemble
45080 code pieces when the entire omp for statement has been parsed. */
45081 return init_placeholder;
45084 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
45085 and ITER is the element that is or contains a nested loop. This
45086 function moves the statements before and after ITER into
45087 OMP_STRUCTURED_BLOCKs and modifies *TP. */
45088 static void
45089 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
45091 tree sl = push_stmt_list ();
45092 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
45093 if (i == iter)
45095 sl = pop_stmt_list (sl);
45096 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45097 tsi_link_before (&i,
45098 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45099 TSI_SAME_STMT);
45100 i++;
45101 sl = push_stmt_list ();
45103 else
45105 tree s = tsi_stmt (i);
45106 tsi_delink (&i); /* Advances i to next statement. */
45107 add_stmt (s);
45109 sl = pop_stmt_list (sl);
45110 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45111 tsi_link_after (&iter,
45112 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45113 TSI_SAME_STMT);
45116 /* Helper to find and mark structured blocks in intervening code for a
45117 single loop level with markers for later error checking. *TP is the
45118 piece of code to be marked and INNER is the inner loop placeholder.
45119 Returns true if INNER was found (recursively) in *TP. */
45120 static bool
45121 find_structured_blocks (tree *tp, tree inner)
45123 if (*tp == inner)
45124 return true;
45125 else if (TREE_CODE (*tp) == BIND_EXPR)
45126 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
45127 else if (TREE_CODE (*tp) == STATEMENT_LIST)
45129 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
45131 tree *p = tsi_stmt_ptr (i);
45132 /* The normal case is that there is no intervening code and we
45133 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
45134 if (find_structured_blocks (p, inner))
45136 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
45137 insert_structured_blocks (tp, i);
45138 return true;
45141 return false;
45143 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
45144 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
45145 else if (TREE_CODE (*tp) == CLEANUP_STMT)
45146 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
45147 else
45148 return false;
45151 /* Helpers used for relinking tree structures: In tree rooted at
45152 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
45153 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
45155 struct sit_data {
45156 tree orig;
45157 tree repl;
45158 bool flatten;
45161 static tree
45162 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
45163 void *dp)
45165 struct sit_data *sit = (struct sit_data *)dp;
45166 if (*tp == sit->orig)
45168 *tp = sit->repl;
45169 return *tp;
45171 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
45172 trying to flatten. */
45173 else if (TREE_CODE (*tp) == BIND_EXPR
45174 && BIND_EXPR_BODY (*tp) == sit->orig
45175 && !BIND_EXPR_VARS (*tp)
45176 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
45178 *tp = sit->repl;
45179 return *tp;
45181 else if (sit->flatten
45182 && TREE_CODE (*tp) == BIND_EXPR
45183 && TREE_CODE (sit->repl) == BIND_EXPR)
45185 if (BIND_EXPR_BODY (*tp) == sit->orig)
45187 /* Merge binding lists for two directly nested BIND_EXPRs,
45188 keeping the outer one. */
45189 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45190 BIND_EXPR_VARS (sit->repl));
45191 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
45192 return *tp;
45194 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
45195 /* There might be a statement list containing cleanup_points
45196 etc between the two levels of BIND_EXPR. We can still merge
45197 them, again keeping the outer BIND_EXPR. */
45198 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
45199 !tsi_end_p (i); ++i)
45201 tree *p = tsi_stmt_ptr (i);
45202 if (*p == sit->orig)
45204 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45205 BIND_EXPR_VARS (sit->repl));
45206 *p = BIND_EXPR_BODY (sit->repl);
45207 return *tp;
45211 return NULL;
45214 static void
45215 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
45217 struct sit_data data;
45219 gcc_assert (*context && orig && repl);
45220 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
45221 repl = BIND_EXPR_BODY (repl);
45222 data.orig = orig;
45223 data.repl = repl;
45224 data.flatten = flatten;
45226 tree result = cp_walk_tree (context, substitute_in_tree_walker,
45227 (void *)&data, NULL);
45228 gcc_assert (result != NULL_TREE);
45231 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
45232 *DP is is the parent block. */
45234 static tree
45235 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
45237 tree superblock = *(tree *)dp;
45239 /* BIND_EXPR_BLOCK may be null if the expression is not a
45240 full-expression; if there's no block, no patching is necessary
45241 for this node. */
45242 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
45244 tree block = BIND_EXPR_BLOCK (*tp);
45245 if (superblock)
45247 BLOCK_SUPERCONTEXT (block) = superblock;
45248 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
45249 BLOCK_SUBBLOCKS (superblock) = block;
45251 BLOCK_SUBBLOCKS (block) = NULL_TREE;
45252 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
45253 (void *)&block, NULL);
45254 *walk_subtrees = 0;
45257 return NULL;
45260 /* Parse the restricted form of the for statement allowed by OpenMP. */
45262 static tree
45263 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
45264 tree *cclauses, bool *if_p)
45266 tree ret;
45267 tree cl, ordered_cl = NULL_TREE;
45268 int collapse = 1, ordered = 0;
45269 unsigned int count;
45270 bool tiling = false;
45271 bool inscan = false;
45272 struct omp_for_parse_data data;
45273 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
45274 tree result;
45275 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
45277 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
45278 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
45279 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
45280 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
45282 tiling = true;
45283 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
45285 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
45286 && OMP_CLAUSE_ORDERED_EXPR (cl))
45288 ordered_cl = cl;
45289 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
45291 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
45292 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
45293 && (code == OMP_SIMD || code == OMP_FOR))
45294 inscan = true;
45296 if (ordered && ordered < collapse)
45298 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
45299 "%<ordered%> clause parameter is less than %<collapse%>");
45300 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
45301 = build_int_cst (NULL_TREE, collapse);
45302 ordered = collapse;
45305 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
45306 count = ordered ? ordered : collapse;
45308 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45310 cp_parser_error (parser, "for statement expected");
45311 return NULL;
45314 /* Initialize parse state for recursive descent. */
45315 data.declv = make_tree_vec (count);
45316 data.initv = make_tree_vec (count);
45317 data.condv = make_tree_vec (count);
45318 data.incrv = make_tree_vec (count);
45319 data.pre_body = NULL_TREE;
45320 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
45321 data.count = count;
45322 data.depth = 0;
45323 data.want_nested_loop = true;
45324 data.ordered = ordered > 0;
45325 data.in_intervening_code = false;
45326 data.perfect_nesting_fail = false;
45327 data.fail = false;
45328 data.inscan = inscan;
45329 data.saw_intervening_code = false;
45330 data.code = code;
45331 data.orig_declv = NULL_TREE;
45332 data.clauses = clauses;
45333 data.cclauses = cclauses;
45334 data.ordered_cl = ordered_cl;
45335 parser->omp_for_parse_state = &data;
45337 cp_parser_omp_loop_nest (parser, if_p);
45339 /* Bomb out early if there was an error (not enough loops, etc). */
45340 if (data.fail || data.declv == NULL_TREE)
45342 parser->omp_for_parse_state = save_data;
45343 return NULL_TREE;
45346 /* Relink the init and body blocks that were built during parsing. At
45347 this point we have a structure nested like
45348 init 0
45349 body 0
45350 init 1
45351 body 1
45352 init 2
45353 body 2
45354 and we want to turn it into
45355 init 0
45356 init 1
45357 init 2
45358 omp_for
45359 body 0
45360 body 1
45361 body 2
45362 We also need to flatten the init blocks, as some code for later
45363 processing of combined directives gets confused otherwise. */
45365 gcc_assert (vec_safe_length (data.init_blockv) == count);
45366 gcc_assert (vec_safe_length (data.body_blockv) == count);
45367 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
45368 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
45370 /* First insert markers for structured blocks for intervening code in
45371 the loop bodies. */
45372 for (unsigned int i = 0; i < count - 1; i++)
45374 bool good = find_structured_blocks (&(data.body_blockv[i]),
45375 data.init_placeholderv[i+1]);
45376 gcc_assert (good);
45379 /* Do the substitution from the inside out. */
45380 for (unsigned int i = count - 1; i > 0; i--)
45382 substitute_in_tree (&(data.body_blockv[i-1]),
45383 data.init_placeholderv[i],
45384 data.body_blockv[i], false);
45385 substitute_in_tree (&(data.init_blockv[i-1]),
45386 data.body_placeholderv[i-1],
45387 data.init_blockv[i], true);
45390 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
45391 (and possibly other stuff) to the current statement list but
45392 returns a pointer to the OMP_FOR itself, or null in case of error. */
45393 result = push_stmt_list ();
45394 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
45395 data.initv, data.condv, data.incrv,
45396 data.body_blockv[0],
45397 data.pre_body, &data.orig_inits, data.clauses);
45398 result = pop_stmt_list (result);
45400 /* Check for errors involving lb/ub/incr expressions referencing
45401 variables declared in intervening code. */
45402 if (data.saw_intervening_code
45403 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
45404 ret = NULL_TREE;
45406 if (ret)
45408 /* Splice the omp_for into the nest of init blocks. */
45409 substitute_in_tree (&(data.init_blockv[0]),
45410 data.body_placeholderv[count - 1],
45411 result, true);
45413 /* Some later processing for combined directives assumes
45414 that the BIND_EXPR containing range for variables appears
45415 at top level in the OMP_FOR body. Fix that up if it's
45416 not the case, e.g. because there is intervening code. */
45417 if (code != OACC_LOOP)
45418 finish_omp_for_block (data.init_blockv[0], ret);
45420 /* Clean up the block subblock/superblock links. Per comment in
45421 begin_compound_stmt, "we don't build BLOCK nodes when processing
45422 templates", so skip this step in that case. */
45423 if (!processing_template_decl)
45425 tree superblock = NULL_TREE;
45426 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
45427 (void *)&superblock, NULL);
45430 /* Finally record the result. */
45431 add_stmt (data.init_blockv[0]);
45434 parser->omp_for_parse_state = save_data;
45435 return ret;
45438 /* Helper function for OpenMP parsing, split clauses and call
45439 finish_omp_clauses on each of the set of clauses afterwards. */
45441 static void
45442 cp_omp_split_clauses (location_t loc, enum tree_code code,
45443 omp_clause_mask mask, tree clauses, tree *cclauses)
45445 int i;
45446 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
45447 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
45448 if (cclauses[i])
45449 cclauses[i] = finish_omp_clauses (cclauses[i],
45450 i == C_OMP_CLAUSE_SPLIT_TARGET
45451 ? C_ORT_OMP_TARGET : C_ORT_OMP);
45454 /* OpenMP 5.0:
45455 #pragma omp loop loop-clause[optseq] new-line
45456 for-loop */
45458 #define OMP_LOOP_CLAUSE_MASK \
45459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
45464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45466 static tree
45467 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
45468 char *p_name, omp_clause_mask mask, tree *cclauses,
45469 bool *if_p)
45471 tree clauses, sb, ret;
45472 unsigned int save;
45473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45475 strcat (p_name, " loop");
45476 mask |= OMP_LOOP_CLAUSE_MASK;
45478 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45479 cclauses == NULL);
45480 if (cclauses)
45482 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
45483 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
45486 keep_next_level (true);
45487 sb = begin_omp_structured_block ();
45488 save = cp_parser_begin_omp_structured_block (parser);
45490 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
45492 cp_parser_end_omp_structured_block (parser, save);
45493 add_stmt (finish_omp_structured_block (sb));
45495 return ret;
45498 /* OpenMP 4.0:
45499 #pragma omp simd simd-clause[optseq] new-line
45500 for-loop */
45502 #define OMP_SIMD_CLAUSE_MASK \
45503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
45504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
45513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45515 static tree
45516 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
45517 char *p_name, omp_clause_mask mask, tree *cclauses,
45518 bool *if_p)
45520 tree clauses, sb, ret;
45521 unsigned int save;
45522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45524 strcat (p_name, " simd");
45525 mask |= OMP_SIMD_CLAUSE_MASK;
45527 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45528 cclauses == NULL);
45529 if (cclauses)
45531 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
45532 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
45535 keep_next_level (true);
45536 sb = begin_omp_structured_block ();
45537 save = cp_parser_begin_omp_structured_block (parser);
45539 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
45541 cp_parser_end_omp_structured_block (parser, save);
45542 add_stmt (finish_omp_structured_block (sb));
45544 return ret;
45547 /* OpenMP 2.5:
45548 #pragma omp for for-clause[optseq] new-line
45549 for-loop
45551 OpenMP 4.0:
45552 #pragma omp for simd for-simd-clause[optseq] new-line
45553 for-loop */
45555 #define OMP_FOR_CLAUSE_MASK \
45556 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
45562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
45563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45568 static tree
45569 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
45570 char *p_name, omp_clause_mask mask, tree *cclauses,
45571 bool *if_p)
45573 tree clauses, sb, ret;
45574 unsigned int save;
45575 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45577 strcat (p_name, " for");
45578 mask |= OMP_FOR_CLAUSE_MASK;
45579 /* parallel for{, simd} disallows nowait clause, but for
45580 target {teams distribute ,}parallel for{, simd} it should be accepted. */
45581 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
45582 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45583 /* Composite distribute parallel for{, simd} disallows ordered clause. */
45584 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45585 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
45587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45589 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45590 const char *p = IDENTIFIER_POINTER (id);
45592 if (strcmp (p, "simd") == 0)
45594 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45595 if (cclauses == NULL)
45596 cclauses = cclauses_buf;
45598 cp_lexer_consume_token (parser->lexer);
45599 if (!flag_openmp) /* flag_openmp_simd */
45600 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45601 cclauses, if_p);
45602 sb = begin_omp_structured_block ();
45603 save = cp_parser_begin_omp_structured_block (parser);
45604 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45605 cclauses, if_p);
45606 cp_parser_end_omp_structured_block (parser, save);
45607 tree body = finish_omp_structured_block (sb);
45608 if (ret == NULL)
45609 return ret;
45610 ret = make_node (OMP_FOR);
45611 TREE_TYPE (ret) = void_type_node;
45612 OMP_FOR_BODY (ret) = body;
45613 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45614 SET_EXPR_LOCATION (ret, loc);
45615 add_stmt (ret);
45616 return ret;
45619 if (!flag_openmp) /* flag_openmp_simd */
45621 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45622 return NULL_TREE;
45625 /* Composite distribute parallel for disallows linear clause. */
45626 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45627 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
45629 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45630 cclauses == NULL);
45631 if (cclauses)
45633 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
45634 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45637 keep_next_level (true);
45638 sb = begin_omp_structured_block ();
45639 save = cp_parser_begin_omp_structured_block (parser);
45641 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
45643 cp_parser_end_omp_structured_block (parser, save);
45644 add_stmt (finish_omp_structured_block (sb));
45646 return ret;
45649 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
45650 omp_clause_mask, tree *, bool *);
45652 /* OpenMP 2.5:
45653 # pragma omp master new-line
45654 structured-block */
45656 static tree
45657 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
45658 char *p_name, omp_clause_mask mask, tree *cclauses,
45659 bool *if_p)
45661 tree clauses, sb, ret;
45662 unsigned int save;
45663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45665 strcat (p_name, " master");
45667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45670 const char *p = IDENTIFIER_POINTER (id);
45672 if (strcmp (p, "taskloop") == 0)
45674 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45675 if (cclauses == NULL)
45676 cclauses = cclauses_buf;
45678 cp_lexer_consume_token (parser->lexer);
45679 if (!flag_openmp) /* flag_openmp_simd */
45680 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45681 cclauses, if_p);
45682 sb = begin_omp_structured_block ();
45683 save = cp_parser_begin_omp_structured_block (parser);
45684 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45685 cclauses, if_p);
45686 cp_parser_end_omp_structured_block (parser, save);
45687 tree body = finish_omp_structured_block (sb);
45688 if (ret == NULL)
45689 return ret;
45690 ret = c_finish_omp_master (loc, body);
45691 OMP_MASTER_COMBINED (ret) = 1;
45692 return ret;
45695 if (!flag_openmp) /* flag_openmp_simd */
45697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45698 return NULL_TREE;
45701 if (cclauses)
45703 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45704 false);
45705 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45707 else
45708 cp_parser_require_pragma_eol (parser, pragma_tok);
45710 return c_finish_omp_master (loc,
45711 cp_parser_omp_structured_block (parser, if_p));
45714 /* OpenMP 5.1:
45715 # pragma omp masked masked-clauses new-line
45716 structured-block */
45718 #define OMP_MASKED_CLAUSE_MASK \
45719 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
45721 static tree
45722 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
45723 char *p_name, omp_clause_mask mask, tree *cclauses,
45724 bool *if_p)
45726 tree clauses, sb, ret;
45727 unsigned int save;
45728 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45730 strcat (p_name, " masked");
45731 mask |= OMP_MASKED_CLAUSE_MASK;
45733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45735 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45736 const char *p = IDENTIFIER_POINTER (id);
45738 if (strcmp (p, "taskloop") == 0)
45740 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45741 if (cclauses == NULL)
45742 cclauses = cclauses_buf;
45744 cp_lexer_consume_token (parser->lexer);
45745 if (!flag_openmp) /* flag_openmp_simd */
45746 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45747 cclauses, if_p);
45748 sb = begin_omp_structured_block ();
45749 save = cp_parser_begin_omp_structured_block (parser);
45750 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45751 cclauses, if_p);
45752 cp_parser_end_omp_structured_block (parser, save);
45753 tree body = finish_omp_structured_block (sb);
45754 if (ret == NULL)
45755 return ret;
45756 ret = c_finish_omp_masked (loc, body,
45757 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45758 OMP_MASKED_COMBINED (ret) = 1;
45759 return ret;
45762 if (!flag_openmp) /* flag_openmp_simd */
45764 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45765 return NULL_TREE;
45768 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45769 cclauses == NULL);
45770 if (cclauses)
45772 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45773 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45776 return c_finish_omp_masked (loc,
45777 cp_parser_omp_structured_block (parser, if_p),
45778 clauses);
45781 /* OpenMP 2.5:
45782 # pragma omp ordered new-line
45783 structured-block
45785 OpenMP 4.5:
45786 # pragma omp ordered ordered-clauses new-line
45787 structured-block */
45789 #define OMP_ORDERED_CLAUSE_MASK \
45790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45793 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45797 static bool
45798 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45799 enum pragma_context context, bool *if_p)
45801 location_t loc = pragma_tok->location;
45802 int n = 1;
45804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45805 n = 2;
45807 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45809 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45810 const char *p = IDENTIFIER_POINTER (id);
45812 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45814 if (!flag_openmp) /* flag_openmp_simd */
45816 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45817 return false;
45819 if (context == pragma_stmt)
45821 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45822 "%qs clause may only be used in compound "
45823 "statements", p);
45824 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45825 return true;
45827 tree clauses
45828 = cp_parser_omp_all_clauses (parser,
45829 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45830 "#pragma omp ordered", pragma_tok);
45831 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45832 return false;
45836 tree clauses
45837 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45838 "#pragma omp ordered", pragma_tok);
45840 if (!flag_openmp /* flag_openmp_simd */
45841 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45842 return false;
45844 c_finish_omp_ordered (loc, clauses,
45845 cp_parser_omp_structured_block (parser, if_p));
45846 return true;
45849 /* OpenMP 2.5:
45851 section-scope:
45852 { section-sequence }
45854 section-sequence:
45855 section-directive[opt] structured-block
45856 section-sequence section-directive structured-block */
45858 static tree
45859 cp_parser_omp_sections_scope (cp_parser *parser)
45861 tree stmt, substmt;
45862 bool error_suppress = false;
45863 cp_token *tok;
45865 matching_braces braces;
45866 if (!braces.require_open (parser))
45867 return NULL_TREE;
45869 stmt = push_stmt_list ();
45871 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45872 != PRAGMA_OMP_SECTION
45873 && !cp_parser_omp_section_scan (parser, "section", true))
45875 substmt = cp_parser_omp_structured_block_sequence (parser,
45876 PRAGMA_OMP_SECTION);
45877 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45878 add_stmt (substmt);
45881 while (1)
45883 tok = cp_lexer_peek_token (parser->lexer);
45884 if (tok->type == CPP_CLOSE_BRACE)
45885 break;
45886 if (tok->type == CPP_EOF)
45887 break;
45889 if (cp_parser_omp_section_scan (parser, "section", false))
45890 tok = cp_lexer_peek_token (parser->lexer);
45891 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
45893 cp_lexer_consume_token (parser->lexer);
45894 cp_parser_require_pragma_eol (parser, tok);
45895 error_suppress = false;
45897 else if (!error_suppress)
45899 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
45900 error_suppress = true;
45903 substmt = cp_parser_omp_structured_block_sequence (parser,
45904 PRAGMA_OMP_SECTION);
45905 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45906 add_stmt (substmt);
45908 braces.require_close (parser);
45910 substmt = pop_stmt_list (stmt);
45912 stmt = make_node (OMP_SECTIONS);
45913 TREE_TYPE (stmt) = void_type_node;
45914 OMP_SECTIONS_BODY (stmt) = substmt;
45916 add_stmt (stmt);
45917 return stmt;
45920 /* OpenMP 2.5:
45921 # pragma omp sections sections-clause[optseq] newline
45922 sections-scope */
45924 #define OMP_SECTIONS_CLAUSE_MASK \
45925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45932 static tree
45933 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
45934 char *p_name, omp_clause_mask mask, tree *cclauses)
45936 tree clauses, ret;
45937 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45939 strcat (p_name, " sections");
45940 mask |= OMP_SECTIONS_CLAUSE_MASK;
45941 if (cclauses)
45942 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45944 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45945 cclauses == NULL);
45946 if (cclauses)
45948 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
45949 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
45952 ret = cp_parser_omp_sections_scope (parser);
45953 if (ret)
45954 OMP_SECTIONS_CLAUSES (ret) = clauses;
45956 return ret;
45959 /* OpenMP 2.5:
45960 # pragma omp parallel parallel-clause[optseq] new-line
45961 structured-block
45962 # pragma omp parallel for parallel-for-clause[optseq] new-line
45963 structured-block
45964 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
45965 structured-block
45967 OpenMP 4.0:
45968 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
45969 structured-block */
45971 #define OMP_PARALLEL_CLAUSE_MASK \
45972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
45978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
45980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
45983 static tree
45984 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
45985 char *p_name, omp_clause_mask mask, tree *cclauses,
45986 bool *if_p)
45988 tree stmt, clauses, block;
45989 unsigned int save;
45990 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45992 strcat (p_name, " parallel");
45993 mask |= OMP_PARALLEL_CLAUSE_MASK;
45994 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
45995 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
45996 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
45997 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
45999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
46001 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46002 if (cclauses == NULL)
46003 cclauses = cclauses_buf;
46005 cp_lexer_consume_token (parser->lexer);
46006 if (!flag_openmp) /* flag_openmp_simd */
46007 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46008 if_p);
46009 block = begin_omp_parallel ();
46010 save = cp_parser_begin_omp_structured_block (parser);
46011 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46012 if_p);
46013 cp_parser_end_omp_structured_block (parser, save);
46014 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46015 block);
46016 if (ret == NULL_TREE)
46017 return ret;
46018 OMP_PARALLEL_COMBINED (stmt) = 1;
46019 return stmt;
46021 /* When combined with distribute, parallel has to be followed by for.
46022 #pragma omp target parallel is allowed though. */
46023 else if (cclauses
46024 && (mask & (OMP_CLAUSE_MASK_1
46025 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
46027 error_at (loc, "expected %<for%> after %qs", p_name);
46028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46029 return NULL_TREE;
46031 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46034 const char *p = IDENTIFIER_POINTER (id);
46035 if (cclauses == NULL && strcmp (p, "masked") == 0)
46037 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46038 cclauses = cclauses_buf;
46040 cp_lexer_consume_token (parser->lexer);
46041 if (!flag_openmp) /* flag_openmp_simd */
46042 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46043 cclauses, if_p);
46044 block = begin_omp_parallel ();
46045 save = cp_parser_begin_omp_structured_block (parser);
46046 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46047 cclauses, if_p);
46048 cp_parser_end_omp_structured_block (parser, save);
46049 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46050 block);
46051 if (ret == NULL_TREE)
46052 return ret;
46053 /* masked does have just filter clause, but during gimplification
46054 isn't represented by a gimplification omp context, so for
46055 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
46056 so that
46057 #pragma omp parallel masked
46058 #pragma omp taskloop simd lastprivate (x)
46059 isn't confused with
46060 #pragma omp parallel masked taskloop simd lastprivate (x) */
46061 if (OMP_MASKED_COMBINED (ret))
46062 OMP_PARALLEL_COMBINED (stmt) = 1;
46063 return stmt;
46065 else if (cclauses == NULL && strcmp (p, "master") == 0)
46067 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46068 cclauses = cclauses_buf;
46070 cp_lexer_consume_token (parser->lexer);
46071 if (!flag_openmp) /* flag_openmp_simd */
46072 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46073 cclauses, if_p);
46074 block = begin_omp_parallel ();
46075 save = cp_parser_begin_omp_structured_block (parser);
46076 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46077 cclauses, if_p);
46078 cp_parser_end_omp_structured_block (parser, save);
46079 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46080 block);
46081 if (ret == NULL_TREE)
46082 return ret;
46083 /* master doesn't have any clauses and during gimplification
46084 isn't represented by a gimplification omp context, so for
46085 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
46086 so that
46087 #pragma omp parallel master
46088 #pragma omp taskloop simd lastprivate (x)
46089 isn't confused with
46090 #pragma omp parallel master taskloop simd lastprivate (x) */
46091 if (OMP_MASTER_COMBINED (ret))
46092 OMP_PARALLEL_COMBINED (stmt) = 1;
46093 return stmt;
46095 else if (strcmp (p, "loop") == 0)
46097 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46098 if (cclauses == NULL)
46099 cclauses = cclauses_buf;
46101 cp_lexer_consume_token (parser->lexer);
46102 if (!flag_openmp) /* flag_openmp_simd */
46103 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46104 cclauses, if_p);
46105 block = begin_omp_parallel ();
46106 save = cp_parser_begin_omp_structured_block (parser);
46107 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46108 cclauses, if_p);
46109 cp_parser_end_omp_structured_block (parser, save);
46110 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46111 block);
46112 if (ret == NULL_TREE)
46113 return ret;
46114 OMP_PARALLEL_COMBINED (stmt) = 1;
46115 return stmt;
46117 else if (!flag_openmp) /* flag_openmp_simd */
46119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46120 return NULL_TREE;
46122 else if (cclauses == NULL && strcmp (p, "sections") == 0)
46124 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46125 cclauses = cclauses_buf;
46127 cp_lexer_consume_token (parser->lexer);
46128 block = begin_omp_parallel ();
46129 save = cp_parser_begin_omp_structured_block (parser);
46130 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
46131 cp_parser_end_omp_structured_block (parser, save);
46132 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46133 block);
46134 OMP_PARALLEL_COMBINED (stmt) = 1;
46135 return stmt;
46138 else if (!flag_openmp) /* flag_openmp_simd */
46140 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46141 return NULL_TREE;
46144 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46145 cclauses == NULL);
46146 if (cclauses)
46148 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
46149 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
46152 block = begin_omp_parallel ();
46153 save = cp_parser_begin_omp_structured_block (parser);
46154 parser->omp_attrs_forbidden_p = true;
46155 cp_parser_statement (parser, NULL_TREE, false, if_p);
46156 cp_parser_end_omp_structured_block (parser, save);
46157 stmt = finish_omp_parallel (clauses, block);
46158 return stmt;
46161 /* OpenMP 2.5:
46162 # pragma omp single single-clause[optseq] new-line
46163 structured-block */
46165 #define OMP_SINGLE_CLAUSE_MASK \
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_COPYPRIVATE) \
46169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46172 static tree
46173 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46175 tree stmt = make_node (OMP_SINGLE);
46176 TREE_TYPE (stmt) = void_type_node;
46177 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46179 OMP_SINGLE_CLAUSES (stmt)
46180 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
46181 "#pragma omp single", pragma_tok);
46182 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46184 return add_stmt (stmt);
46187 /* OpenMP 5.1:
46188 # pragma omp scope scope-clause[optseq] new-line
46189 structured-block */
46191 #define OMP_SCOPE_CLAUSE_MASK \
46192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46198 static tree
46199 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46201 tree stmt = make_node (OMP_SCOPE);
46202 TREE_TYPE (stmt) = void_type_node;
46203 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46205 OMP_SCOPE_CLAUSES (stmt)
46206 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
46207 "#pragma omp scope", pragma_tok);
46208 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46210 return add_stmt (stmt);
46213 /* OpenMP 3.0:
46214 # pragma omp task task-clause[optseq] new-line
46215 structured-block */
46217 #define OMP_TASK_CLAUSE_MASK \
46218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
46231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
46233 static tree
46234 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46236 tree clauses, block;
46237 unsigned int save;
46239 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
46240 "#pragma omp task", pragma_tok);
46241 block = begin_omp_task ();
46242 save = cp_parser_begin_omp_structured_block (parser);
46243 parser->omp_attrs_forbidden_p = true;
46244 cp_parser_statement (parser, NULL_TREE, false, if_p);
46245 cp_parser_end_omp_structured_block (parser, save);
46246 return finish_omp_task (clauses, block);
46249 /* OpenMP 3.0:
46250 # pragma omp taskwait new-line
46252 OpenMP 5.0:
46253 # pragma omp taskwait taskwait-clause[opt] new-line */
46255 #define OMP_TASKWAIT_CLAUSE_MASK \
46256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46259 static void
46260 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
46262 tree clauses
46263 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
46264 "#pragma omp taskwait", pragma_tok);
46266 if (clauses)
46268 tree stmt = make_node (OMP_TASK);
46269 TREE_TYPE (stmt) = void_node;
46270 OMP_TASK_CLAUSES (stmt) = clauses;
46271 OMP_TASK_BODY (stmt) = NULL_TREE;
46272 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46273 add_stmt (stmt);
46275 else
46276 finish_omp_taskwait ();
46279 /* OpenMP 3.1:
46280 # pragma omp taskyield new-line */
46282 static void
46283 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
46285 cp_parser_require_pragma_eol (parser, pragma_tok);
46286 finish_omp_taskyield ();
46289 /* OpenMP 4.0:
46290 # pragma omp taskgroup new-line
46291 structured-block
46293 OpenMP 5.0:
46294 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
46296 #define OMP_TASKGROUP_CLAUSE_MASK \
46297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
46300 static tree
46301 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46303 tree clauses
46304 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
46305 "#pragma omp taskgroup", pragma_tok);
46306 return c_finish_omp_taskgroup (input_location,
46307 cp_parser_omp_structured_block (parser,
46308 if_p),
46309 clauses);
46313 /* OpenMP 2.5:
46314 # pragma omp threadprivate (variable-list) */
46316 static void
46317 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
46319 tree vars;
46321 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
46322 cp_parser_require_pragma_eol (parser, pragma_tok);
46324 finish_omp_threadprivate (vars);
46327 /* OpenMP 4.0:
46328 # pragma omp cancel cancel-clause[optseq] new-line */
46330 #define OMP_CANCEL_CLAUSE_MASK \
46331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
46335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
46337 static void
46338 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
46340 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
46341 "#pragma omp cancel", pragma_tok);
46342 finish_omp_cancel (clauses);
46345 /* OpenMP 4.0:
46346 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
46348 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
46349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
46354 static bool
46355 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
46356 enum pragma_context context)
46358 tree clauses;
46359 bool point_seen = false;
46361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46364 const char *p = IDENTIFIER_POINTER (id);
46366 if (strcmp (p, "point") == 0)
46368 cp_lexer_consume_token (parser->lexer);
46369 point_seen = true;
46372 if (!point_seen)
46374 cp_parser_error (parser, "expected %<point%>");
46375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46376 return false;
46379 if (context != pragma_compound)
46381 if (context == pragma_stmt)
46382 error_at (pragma_tok->location,
46383 "%<#pragma %s%> may only be used in compound statements",
46384 "omp cancellation point");
46385 else
46386 cp_parser_error (parser, "expected declaration specifiers");
46387 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46388 return true;
46391 clauses = cp_parser_omp_all_clauses (parser,
46392 OMP_CANCELLATION_POINT_CLAUSE_MASK,
46393 "#pragma omp cancellation point",
46394 pragma_tok);
46395 finish_omp_cancellation_point (clauses);
46396 return true;
46399 /* OpenMP 4.0:
46400 #pragma omp distribute distribute-clause[optseq] new-line
46401 for-loop */
46403 #define OMP_DISTRIBUTE_CLAUSE_MASK \
46404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
46408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46412 static tree
46413 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
46414 char *p_name, omp_clause_mask mask, tree *cclauses,
46415 bool *if_p)
46417 tree clauses, sb, ret;
46418 unsigned int save;
46419 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46421 strcat (p_name, " distribute");
46422 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
46424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46426 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46427 const char *p = IDENTIFIER_POINTER (id);
46428 bool simd = false;
46429 bool parallel = false;
46431 if (strcmp (p, "simd") == 0)
46432 simd = true;
46433 else
46434 parallel = strcmp (p, "parallel") == 0;
46435 if (parallel || simd)
46437 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46438 if (cclauses == NULL)
46439 cclauses = cclauses_buf;
46440 cp_lexer_consume_token (parser->lexer);
46441 if (!flag_openmp) /* flag_openmp_simd */
46443 if (simd)
46444 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46445 cclauses, if_p);
46446 else
46447 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46448 cclauses, if_p);
46450 sb = begin_omp_structured_block ();
46451 save = cp_parser_begin_omp_structured_block (parser);
46452 if (simd)
46453 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46454 cclauses, if_p);
46455 else
46456 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46457 cclauses, if_p);
46458 cp_parser_end_omp_structured_block (parser, save);
46459 tree body = finish_omp_structured_block (sb);
46460 if (ret == NULL)
46461 return ret;
46462 ret = make_node (OMP_DISTRIBUTE);
46463 TREE_TYPE (ret) = void_type_node;
46464 OMP_FOR_BODY (ret) = body;
46465 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46466 SET_EXPR_LOCATION (ret, loc);
46467 add_stmt (ret);
46468 return ret;
46471 if (!flag_openmp) /* flag_openmp_simd */
46473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46474 return NULL_TREE;
46477 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46478 cclauses == NULL);
46479 if (cclauses)
46481 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
46482 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46485 keep_next_level (true);
46486 sb = begin_omp_structured_block ();
46487 save = cp_parser_begin_omp_structured_block (parser);
46489 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
46491 cp_parser_end_omp_structured_block (parser, save);
46492 add_stmt (finish_omp_structured_block (sb));
46494 return ret;
46497 /* OpenMP 4.0:
46498 # pragma omp teams teams-clause[optseq] new-line
46499 structured-block */
46501 #define OMP_TEAMS_CLAUSE_MASK \
46502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
46507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
46511 static tree
46512 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
46513 char *p_name, omp_clause_mask mask, tree *cclauses,
46514 bool *if_p)
46516 tree clauses, sb, ret;
46517 unsigned int save;
46518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46520 strcat (p_name, " teams");
46521 mask |= OMP_TEAMS_CLAUSE_MASK;
46523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46525 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46526 const char *p = IDENTIFIER_POINTER (id);
46527 if (strcmp (p, "distribute") == 0)
46529 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46530 if (cclauses == NULL)
46531 cclauses = cclauses_buf;
46533 cp_lexer_consume_token (parser->lexer);
46534 if (!flag_openmp) /* flag_openmp_simd */
46535 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46536 cclauses, if_p);
46537 keep_next_level (true);
46538 sb = begin_omp_structured_block ();
46539 save = cp_parser_begin_omp_structured_block (parser);
46540 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46541 cclauses, if_p);
46542 cp_parser_end_omp_structured_block (parser, save);
46543 tree body = finish_omp_structured_block (sb);
46544 if (ret == NULL)
46545 return ret;
46546 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46547 ret = make_node (OMP_TEAMS);
46548 TREE_TYPE (ret) = void_type_node;
46549 OMP_TEAMS_CLAUSES (ret) = clauses;
46550 OMP_TEAMS_BODY (ret) = body;
46551 OMP_TEAMS_COMBINED (ret) = 1;
46552 SET_EXPR_LOCATION (ret, loc);
46553 return add_stmt (ret);
46555 else if (strcmp (p, "loop") == 0)
46557 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46558 if (cclauses == NULL)
46559 cclauses = cclauses_buf;
46561 cp_lexer_consume_token (parser->lexer);
46562 if (!flag_openmp) /* flag_openmp_simd */
46563 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46564 cclauses, if_p);
46565 keep_next_level (true);
46566 sb = begin_omp_structured_block ();
46567 save = cp_parser_begin_omp_structured_block (parser);
46568 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46569 cclauses, if_p);
46570 cp_parser_end_omp_structured_block (parser, save);
46571 tree body = finish_omp_structured_block (sb);
46572 if (ret == NULL)
46573 return ret;
46574 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46575 ret = make_node (OMP_TEAMS);
46576 TREE_TYPE (ret) = void_type_node;
46577 OMP_TEAMS_CLAUSES (ret) = clauses;
46578 OMP_TEAMS_BODY (ret) = body;
46579 OMP_TEAMS_COMBINED (ret) = 1;
46580 SET_EXPR_LOCATION (ret, loc);
46581 return add_stmt (ret);
46584 if (!flag_openmp) /* flag_openmp_simd */
46586 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46587 return NULL_TREE;
46590 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46591 cclauses == NULL);
46592 if (cclauses)
46594 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
46595 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46598 tree stmt = make_node (OMP_TEAMS);
46599 TREE_TYPE (stmt) = void_type_node;
46600 OMP_TEAMS_CLAUSES (stmt) = clauses;
46601 keep_next_level (true);
46602 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46603 SET_EXPR_LOCATION (stmt, loc);
46605 return add_stmt (stmt);
46608 /* OpenMP 4.0:
46609 # pragma omp target data target-data-clause[optseq] new-line
46610 structured-block */
46612 #define OMP_TARGET_DATA_CLAUSE_MASK \
46613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
46617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
46619 static tree
46620 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46622 if (flag_openmp)
46623 omp_requires_mask
46624 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46626 tree clauses
46627 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
46628 "#pragma omp target data", pragma_tok);
46629 c_omp_adjust_map_clauses (clauses, false);
46630 int map_seen = 0;
46631 for (tree *pc = &clauses; *pc;)
46633 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46634 switch (OMP_CLAUSE_MAP_KIND (*pc))
46636 case GOMP_MAP_TO:
46637 case GOMP_MAP_ALWAYS_TO:
46638 case GOMP_MAP_PRESENT_TO:
46639 case GOMP_MAP_ALWAYS_PRESENT_TO:
46640 case GOMP_MAP_FROM:
46641 case GOMP_MAP_ALWAYS_FROM:
46642 case GOMP_MAP_PRESENT_FROM:
46643 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46644 case GOMP_MAP_TOFROM:
46645 case GOMP_MAP_ALWAYS_TOFROM:
46646 case GOMP_MAP_PRESENT_TOFROM:
46647 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46648 case GOMP_MAP_ALLOC:
46649 case GOMP_MAP_PRESENT_ALLOC:
46650 map_seen = 3;
46651 break;
46652 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46653 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46654 case GOMP_MAP_ALWAYS_POINTER:
46655 case GOMP_MAP_ATTACH_DETACH:
46656 case GOMP_MAP_ATTACH:
46657 break;
46658 default:
46659 map_seen |= 1;
46660 error_at (OMP_CLAUSE_LOCATION (*pc),
46661 "%<#pragma omp target data%> with map-type other "
46662 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
46663 "on %<map%> clause");
46664 *pc = OMP_CLAUSE_CHAIN (*pc);
46665 continue;
46667 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
46668 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
46669 map_seen = 3;
46670 pc = &OMP_CLAUSE_CHAIN (*pc);
46673 if (map_seen != 3)
46675 if (map_seen == 0)
46676 error_at (pragma_tok->location,
46677 "%<#pragma omp target data%> must contain at least "
46678 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
46679 "clause");
46680 return NULL_TREE;
46683 tree stmt = make_node (OMP_TARGET_DATA);
46684 TREE_TYPE (stmt) = void_type_node;
46685 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
46687 keep_next_level (true);
46688 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46690 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46691 return add_stmt (stmt);
46694 /* OpenMP 4.5:
46695 # pragma omp target enter data target-enter-data-clause[optseq] new-line
46696 structured-block */
46698 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
46699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46705 static bool
46706 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
46707 enum pragma_context context)
46709 bool data_seen = false;
46710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46713 const char *p = IDENTIFIER_POINTER (id);
46715 if (strcmp (p, "data") == 0)
46717 cp_lexer_consume_token (parser->lexer);
46718 data_seen = true;
46721 if (!data_seen)
46723 cp_parser_error (parser, "expected %<data%>");
46724 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46725 return false;
46728 if (context == pragma_stmt)
46730 error_at (pragma_tok->location,
46731 "%<#pragma %s%> may only be used in compound statements",
46732 "omp target enter data");
46733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46734 return true;
46737 if (flag_openmp)
46738 omp_requires_mask
46739 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46741 tree clauses
46742 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
46743 "#pragma omp target enter data", pragma_tok);
46744 c_omp_adjust_map_clauses (clauses, false);
46745 int map_seen = 0;
46746 for (tree *pc = &clauses; *pc;)
46748 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46749 switch (OMP_CLAUSE_MAP_KIND (*pc))
46751 case GOMP_MAP_TO:
46752 case GOMP_MAP_ALWAYS_TO:
46753 case GOMP_MAP_PRESENT_TO:
46754 case GOMP_MAP_ALWAYS_PRESENT_TO:
46755 case GOMP_MAP_ALLOC:
46756 case GOMP_MAP_PRESENT_ALLOC:
46757 map_seen = 3;
46758 break;
46759 case GOMP_MAP_TOFROM:
46760 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46761 map_seen = 3;
46762 break;
46763 case GOMP_MAP_ALWAYS_TOFROM:
46764 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46765 map_seen = 3;
46766 break;
46767 case GOMP_MAP_PRESENT_TOFROM:
46768 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46769 map_seen = 3;
46770 break;
46771 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46772 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46773 map_seen = 3;
46774 break;
46775 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46776 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46777 case GOMP_MAP_ALWAYS_POINTER:
46778 case GOMP_MAP_ATTACH_DETACH:
46779 case GOMP_MAP_ATTACH:
46780 break;
46781 default:
46782 map_seen |= 1;
46783 error_at (OMP_CLAUSE_LOCATION (*pc),
46784 "%<#pragma omp target enter data%> with map-type other "
46785 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46786 *pc = OMP_CLAUSE_CHAIN (*pc);
46787 continue;
46789 pc = &OMP_CLAUSE_CHAIN (*pc);
46792 if (map_seen != 3)
46794 if (map_seen == 0)
46795 error_at (pragma_tok->location,
46796 "%<#pragma omp target enter data%> must contain at least "
46797 "one %<map%> clause");
46798 return true;
46801 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46802 TREE_TYPE (stmt) = void_type_node;
46803 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46804 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46805 add_stmt (stmt);
46806 return true;
46809 /* OpenMP 4.5:
46810 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46811 structured-block */
46813 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46814 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46820 static bool
46821 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46822 enum pragma_context context)
46824 bool data_seen = false;
46825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46828 const char *p = IDENTIFIER_POINTER (id);
46830 if (strcmp (p, "data") == 0)
46832 cp_lexer_consume_token (parser->lexer);
46833 data_seen = true;
46836 if (!data_seen)
46838 cp_parser_error (parser, "expected %<data%>");
46839 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46840 return false;
46843 if (context == pragma_stmt)
46845 error_at (pragma_tok->location,
46846 "%<#pragma %s%> may only be used in compound statements",
46847 "omp target exit data");
46848 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46849 return true;
46852 if (flag_openmp)
46853 omp_requires_mask
46854 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46856 tree clauses
46857 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46858 "#pragma omp target exit data", pragma_tok,
46859 false);
46860 clauses = finish_omp_clauses (clauses, C_ORT_OMP_EXIT_DATA);
46861 c_omp_adjust_map_clauses (clauses, false);
46862 int map_seen = 0;
46863 for (tree *pc = &clauses; *pc;)
46865 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46866 switch (OMP_CLAUSE_MAP_KIND (*pc))
46868 case GOMP_MAP_FROM:
46869 case GOMP_MAP_ALWAYS_FROM:
46870 case GOMP_MAP_PRESENT_FROM:
46871 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46872 case GOMP_MAP_RELEASE:
46873 case GOMP_MAP_DELETE:
46874 map_seen = 3;
46875 break;
46876 case GOMP_MAP_TOFROM:
46877 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
46878 map_seen = 3;
46879 break;
46880 case GOMP_MAP_ALWAYS_TOFROM:
46881 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
46882 map_seen = 3;
46883 break;
46884 case GOMP_MAP_PRESENT_TOFROM:
46885 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
46886 map_seen = 3;
46887 break;
46888 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46889 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
46890 map_seen = 3;
46891 break;
46892 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46893 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46894 case GOMP_MAP_ALWAYS_POINTER:
46895 case GOMP_MAP_ATTACH_DETACH:
46896 case GOMP_MAP_DETACH:
46897 break;
46898 default:
46899 map_seen |= 1;
46900 error_at (OMP_CLAUSE_LOCATION (*pc),
46901 "%<#pragma omp target exit data%> with map-type other "
46902 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
46903 "on %<map%> clause");
46904 *pc = OMP_CLAUSE_CHAIN (*pc);
46905 continue;
46907 pc = &OMP_CLAUSE_CHAIN (*pc);
46910 if (map_seen != 3)
46912 if (map_seen == 0)
46913 error_at (pragma_tok->location,
46914 "%<#pragma omp target exit data%> must contain at least "
46915 "one %<map%> clause");
46916 return true;
46919 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
46920 TREE_TYPE (stmt) = void_type_node;
46921 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
46922 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46923 add_stmt (stmt);
46924 return true;
46927 /* OpenMP 4.0:
46928 # pragma omp target update target-update-clause[optseq] new-line */
46930 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
46931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
46932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46938 static bool
46939 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
46940 enum pragma_context context)
46942 if (context == pragma_stmt)
46944 error_at (pragma_tok->location,
46945 "%<#pragma %s%> may only be used in compound statements",
46946 "omp target update");
46947 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46948 return true;
46951 tree clauses
46952 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
46953 "#pragma omp target update", pragma_tok);
46954 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
46955 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
46957 error_at (pragma_tok->location,
46958 "%<#pragma omp target update%> must contain at least one "
46959 "%<from%> or %<to%> clauses");
46960 return true;
46963 if (flag_openmp)
46964 omp_requires_mask
46965 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46967 tree stmt = make_node (OMP_TARGET_UPDATE);
46968 TREE_TYPE (stmt) = void_type_node;
46969 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
46970 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46971 add_stmt (stmt);
46972 return true;
46975 /* OpenMP 4.0:
46976 # pragma omp target target-clause[optseq] new-line
46977 structured-block */
46979 #define OMP_TARGET_CLAUSE_MASK \
46980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
46988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
46992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
46994 static bool
46995 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
46996 enum pragma_context context, bool *if_p)
46998 if (flag_openmp)
46999 omp_requires_mask
47000 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47005 const char *p = IDENTIFIER_POINTER (id);
47006 enum tree_code ccode = ERROR_MARK;
47008 if (strcmp (p, "teams") == 0)
47009 ccode = OMP_TEAMS;
47010 else if (strcmp (p, "parallel") == 0)
47011 ccode = OMP_PARALLEL;
47012 else if (strcmp (p, "simd") == 0)
47013 ccode = OMP_SIMD;
47014 if (ccode != ERROR_MARK)
47016 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
47017 char p_name[sizeof ("#pragma omp target teams distribute "
47018 "parallel for simd")];
47020 cp_lexer_consume_token (parser->lexer);
47021 strcpy (p_name, "#pragma omp target");
47022 if (!flag_openmp) /* flag_openmp_simd */
47024 tree stmt;
47025 switch (ccode)
47027 case OMP_TEAMS:
47028 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
47029 OMP_TARGET_CLAUSE_MASK,
47030 cclauses, if_p);
47031 break;
47032 case OMP_PARALLEL:
47033 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47034 OMP_TARGET_CLAUSE_MASK,
47035 cclauses, if_p);
47036 break;
47037 case OMP_SIMD:
47038 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
47039 OMP_TARGET_CLAUSE_MASK,
47040 cclauses, if_p);
47041 break;
47042 default:
47043 gcc_unreachable ();
47045 return stmt != NULL_TREE;
47047 keep_next_level (true);
47048 tree sb = begin_omp_structured_block (), ret;
47049 unsigned save = cp_parser_begin_omp_structured_block (parser);
47050 switch (ccode)
47052 case OMP_TEAMS:
47053 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
47054 OMP_TARGET_CLAUSE_MASK, cclauses,
47055 if_p);
47056 break;
47057 case OMP_PARALLEL:
47058 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47059 OMP_TARGET_CLAUSE_MASK, cclauses,
47060 if_p);
47061 break;
47062 case OMP_SIMD:
47063 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
47064 OMP_TARGET_CLAUSE_MASK, cclauses,
47065 if_p);
47066 break;
47067 default:
47068 gcc_unreachable ();
47070 cp_parser_end_omp_structured_block (parser, save);
47071 tree body = finish_omp_structured_block (sb);
47072 if (ret == NULL_TREE)
47073 return false;
47074 if (ccode == OMP_TEAMS && !processing_template_decl)
47075 /* For combined target teams, ensure the num_teams and
47076 thread_limit clause expressions are evaluated on the host,
47077 before entering the target construct. */
47078 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47079 c; c = OMP_CLAUSE_CHAIN (c))
47080 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
47081 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
47082 for (int i = 0;
47083 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
47084 if (OMP_CLAUSE_OPERAND (c, i)
47085 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
47087 tree expr = OMP_CLAUSE_OPERAND (c, i);
47088 expr = force_target_expr (TREE_TYPE (expr), expr,
47089 tf_none);
47090 if (expr == error_mark_node)
47091 continue;
47092 tree tmp = TARGET_EXPR_SLOT (expr);
47093 add_stmt (expr);
47094 OMP_CLAUSE_OPERAND (c, i) = expr;
47095 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
47096 OMP_CLAUSE_FIRSTPRIVATE);
47097 OMP_CLAUSE_DECL (tc) = tmp;
47098 OMP_CLAUSE_CHAIN (tc)
47099 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
47100 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
47102 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
47103 finish_omp_target (pragma_tok->location,
47104 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
47105 return true;
47107 else if (!flag_openmp) /* flag_openmp_simd */
47109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47110 return false;
47112 else if (strcmp (p, "data") == 0)
47114 cp_lexer_consume_token (parser->lexer);
47115 cp_parser_omp_target_data (parser, pragma_tok, if_p);
47116 return true;
47118 else if (strcmp (p, "enter") == 0)
47120 cp_lexer_consume_token (parser->lexer);
47121 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
47123 else if (strcmp (p, "exit") == 0)
47125 cp_lexer_consume_token (parser->lexer);
47126 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
47128 else if (strcmp (p, "update") == 0)
47130 cp_lexer_consume_token (parser->lexer);
47131 return cp_parser_omp_target_update (parser, pragma_tok, context);
47134 if (!flag_openmp) /* flag_openmp_simd */
47136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47137 return false;
47140 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
47141 "#pragma omp target", pragma_tok,
47142 false);
47143 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47144 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
47146 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
47147 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
47148 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
47149 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
47150 OMP_CLAUSE_CHAIN (c) = nc;
47152 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
47154 c_omp_adjust_map_clauses (clauses, true);
47155 keep_next_level (true);
47156 tree body = cp_parser_omp_structured_block (parser, if_p);
47158 finish_omp_target (pragma_tok->location, clauses, body, false);
47159 return true;
47162 /* OpenACC 2.0:
47163 # pragma acc cache (variable-list) new-line
47166 static tree
47167 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
47169 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
47170 clauses. */
47171 auto_suppress_location_wrappers sentinel;
47173 tree stmt, clauses;
47175 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
47176 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47178 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
47180 stmt = make_node (OACC_CACHE);
47181 TREE_TYPE (stmt) = void_type_node;
47182 OACC_CACHE_CLAUSES (stmt) = clauses;
47183 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47184 add_stmt (stmt);
47186 return stmt;
47189 /* OpenACC 2.0:
47190 # pragma acc data oacc-data-clause[optseq] new-line
47191 structured-block */
47193 #define OACC_DATA_CLAUSE_MASK \
47194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47206 static tree
47207 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47209 tree stmt, clauses, block;
47210 unsigned int save;
47212 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
47213 "#pragma acc data", pragma_tok);
47215 block = begin_omp_parallel ();
47216 save = cp_parser_begin_omp_structured_block (parser);
47217 cp_parser_statement (parser, NULL_TREE, false, if_p);
47218 cp_parser_end_omp_structured_block (parser, save);
47219 stmt = finish_oacc_data (clauses, block);
47220 return stmt;
47223 /* OpenACC 2.0:
47224 # pragma acc host_data <clauses> new-line
47225 structured-block */
47227 #define OACC_HOST_DATA_CLAUSE_MASK \
47228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
47229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
47232 static tree
47233 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47235 tree stmt, clauses, block;
47236 unsigned int save;
47238 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
47239 "#pragma acc host_data", pragma_tok,
47240 false);
47241 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
47243 error_at (pragma_tok->location,
47244 "%<host_data%> construct requires %<use_device%> clause");
47245 return error_mark_node;
47247 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47248 block = begin_omp_parallel ();
47249 save = cp_parser_begin_omp_structured_block (parser);
47250 cp_parser_statement (parser, NULL_TREE, false, if_p);
47251 cp_parser_end_omp_structured_block (parser, save);
47252 stmt = finish_oacc_host_data (clauses, block);
47253 return stmt;
47256 /* OpenACC 2.0:
47257 # pragma acc declare oacc-data-clause[optseq] new-line
47260 #define OACC_DECLARE_CLAUSE_MASK \
47261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
47267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
47268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47270 static tree
47271 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
47273 tree clauses, stmt;
47274 bool error = false;
47275 bool found_in_scope = global_bindings_p ();
47277 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
47278 "#pragma acc declare", pragma_tok);
47281 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47283 error_at (pragma_tok->location,
47284 "no valid clauses specified in %<#pragma acc declare%>");
47285 return NULL_TREE;
47288 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
47290 location_t loc = OMP_CLAUSE_LOCATION (t);
47291 tree decl = OMP_CLAUSE_DECL (t);
47292 if (!DECL_P (decl))
47294 error_at (loc, "array section in %<#pragma acc declare%>");
47295 error = true;
47296 continue;
47298 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
47299 switch (OMP_CLAUSE_MAP_KIND (t))
47301 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47302 case GOMP_MAP_ALLOC:
47303 case GOMP_MAP_TO:
47304 case GOMP_MAP_FORCE_DEVICEPTR:
47305 case GOMP_MAP_DEVICE_RESIDENT:
47306 break;
47308 case GOMP_MAP_LINK:
47309 if (!global_bindings_p ()
47310 && (TREE_STATIC (decl)
47311 || !DECL_EXTERNAL (decl)))
47313 error_at (loc,
47314 "%qD must be a global variable in "
47315 "%<#pragma acc declare link%>",
47316 decl);
47317 error = true;
47318 continue;
47320 break;
47322 default:
47323 if (global_bindings_p ())
47325 error_at (loc, "invalid OpenACC clause at file scope");
47326 error = true;
47327 continue;
47329 if (DECL_EXTERNAL (decl))
47331 error_at (loc,
47332 "invalid use of %<extern%> variable %qD "
47333 "in %<#pragma acc declare%>", decl);
47334 error = true;
47335 continue;
47337 else if (TREE_PUBLIC (decl))
47339 error_at (loc,
47340 "invalid use of %<global%> variable %qD "
47341 "in %<#pragma acc declare%>", decl);
47342 error = true;
47343 continue;
47345 break;
47348 if (!found_in_scope)
47349 /* This seems to ignore the existence of cleanup scopes?
47350 What is the meaning for local extern decls? The local
47351 extern is in this scope, but it is referring to a decl that
47352 is namespace scope. */
47353 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
47354 if (d == decl)
47356 found_in_scope = true;
47357 break;
47359 if (!found_in_scope)
47361 error_at (loc,
47362 "%qD must be a variable declared in the same scope as "
47363 "%<#pragma acc declare%>", decl);
47364 error = true;
47365 continue;
47368 if (!error)
47370 if (DECL_LOCAL_DECL_P (decl))
47371 /* We need to mark the aliased decl, as that is the entity
47372 that is being referred to. This won't work for
47373 dependent variables, but it didn't work for them before
47374 DECL_LOCAL_DECL_P was a thing either. But then
47375 dependent local extern variable decls are as rare as
47376 hen's teeth. */
47377 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
47378 if (alias != error_mark_node)
47379 decl = alias;
47381 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
47382 || lookup_attribute ("omp declare target link",
47383 DECL_ATTRIBUTES (decl)))
47385 error_at (loc, "variable %qD used more than once with "
47386 "%<#pragma acc declare%>", decl);
47387 error = true;
47388 continue;
47391 tree id;
47392 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
47393 id = get_identifier ("omp declare target link");
47394 else
47395 id = get_identifier ("omp declare target");
47397 DECL_ATTRIBUTES (decl)
47398 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
47399 if (current_binding_level->kind == sk_namespace)
47401 symtab_node *node = symtab_node::get (decl);
47402 if (node != NULL)
47404 node->offloadable = 1;
47405 if (ENABLE_OFFLOADING)
47407 g->have_offload = true;
47408 if (is_a <varpool_node *> (node))
47409 vec_safe_push (offload_vars, decl);
47416 if (error || current_binding_level->kind == sk_namespace)
47417 return NULL_TREE;
47419 stmt = make_node (OACC_DECLARE);
47420 TREE_TYPE (stmt) = void_type_node;
47421 OACC_DECLARE_CLAUSES (stmt) = clauses;
47422 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47424 add_stmt (stmt);
47426 return NULL_TREE;
47429 /* OpenACC 2.0:
47430 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
47434 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
47436 LOC is the location of the #pragma token.
47439 #define OACC_ENTER_DATA_CLAUSE_MASK \
47440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47447 #define OACC_EXIT_DATA_CLAUSE_MASK \
47448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
47452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
47454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47456 static tree
47457 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
47458 bool enter)
47460 location_t loc = pragma_tok->location;
47461 tree stmt, clauses;
47462 const char *p = "";
47464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47465 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47467 if (strcmp (p, "data") != 0)
47469 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
47470 enter ? "enter" : "exit");
47471 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47472 return NULL_TREE;
47475 cp_lexer_consume_token (parser->lexer);
47477 if (enter)
47478 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
47479 "#pragma acc enter data", pragma_tok);
47480 else
47481 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
47482 "#pragma acc exit data", pragma_tok);
47484 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47486 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
47487 enter ? "enter" : "exit");
47488 return NULL_TREE;
47491 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
47492 TREE_TYPE (stmt) = void_type_node;
47493 OMP_STANDALONE_CLAUSES (stmt) = clauses;
47494 SET_EXPR_LOCATION (stmt, loc);
47495 add_stmt (stmt);
47496 return stmt;
47499 /* OpenACC 2.0:
47500 # pragma acc loop oacc-loop-clause[optseq] new-line
47501 structured-block */
47503 #define OACC_LOOP_CLAUSE_MASK \
47504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
47505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
47511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
47512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
47515 static tree
47516 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
47517 omp_clause_mask mask, tree *cclauses, bool *if_p)
47519 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
47521 strcat (p_name, " loop");
47522 mask |= OACC_LOOP_CLAUSE_MASK;
47524 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47525 /*finish_p=*/cclauses == NULL,
47526 /*target=*/is_parallel);
47527 if (cclauses)
47529 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
47530 if (*cclauses)
47531 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC_TARGET);
47532 if (clauses)
47533 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47536 tree block = begin_omp_structured_block ();
47537 int save = cp_parser_begin_omp_structured_block (parser);
47538 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
47539 cp_parser_end_omp_structured_block (parser, save);
47541 /* Later processing of combined acc loop constructs gets confused
47542 by an extra level of empty nested BIND_EXPRs, so flatten them. */
47543 block = finish_omp_structured_block (block);
47544 if (TREE_CODE (block) == BIND_EXPR
47545 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
47546 && !BIND_EXPR_VARS (block))
47547 block = BIND_EXPR_BODY (block);
47548 add_stmt (block);
47550 return stmt;
47553 /* OpenACC 2.0:
47554 # pragma acc kernels oacc-kernels-clause[optseq] new-line
47555 structured-block
47559 # pragma acc parallel oacc-parallel-clause[optseq] new-line
47560 structured-block
47562 OpenACC 2.6:
47564 # pragma acc serial oacc-serial-clause[optseq] new-line
47567 #define OACC_KERNELS_CLAUSE_MASK \
47568 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47585 #define OACC_PARALLEL_CLAUSE_MASK \
47586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47606 #define OACC_SERIAL_CLAUSE_MASK \
47607 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47624 static tree
47625 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
47626 char *p_name, bool *if_p)
47628 omp_clause_mask mask;
47629 enum tree_code code;
47630 switch (cp_parser_pragma_kind (pragma_tok))
47632 case PRAGMA_OACC_KERNELS:
47633 strcat (p_name, " kernels");
47634 mask = OACC_KERNELS_CLAUSE_MASK;
47635 code = OACC_KERNELS;
47636 break;
47637 case PRAGMA_OACC_PARALLEL:
47638 strcat (p_name, " parallel");
47639 mask = OACC_PARALLEL_CLAUSE_MASK;
47640 code = OACC_PARALLEL;
47641 break;
47642 case PRAGMA_OACC_SERIAL:
47643 strcat (p_name, " serial");
47644 mask = OACC_SERIAL_CLAUSE_MASK;
47645 code = OACC_SERIAL;
47646 break;
47647 default:
47648 gcc_unreachable ();
47651 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47653 const char *p
47654 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47655 if (strcmp (p, "loop") == 0)
47657 cp_lexer_consume_token (parser->lexer);
47658 tree block = begin_omp_parallel ();
47659 tree clauses;
47660 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
47661 &clauses, if_p);
47662 protected_set_expr_location (stmt, pragma_tok->location);
47663 return finish_omp_construct (code, block, clauses);
47667 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47668 /*finish_p=*/true,
47669 /*target=*/true);
47671 tree block = begin_omp_parallel ();
47672 unsigned int save = cp_parser_begin_omp_structured_block (parser);
47673 cp_parser_statement (parser, NULL_TREE, false, if_p);
47674 cp_parser_end_omp_structured_block (parser, save);
47675 return finish_omp_construct (code, block, clauses);
47678 /* OpenACC 2.0:
47679 # pragma acc update oacc-update-clause[optseq] new-line
47682 #define OACC_UPDATE_CLAUSE_MASK \
47683 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
47685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
47686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
47688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
47691 static tree
47692 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
47694 tree stmt, clauses;
47696 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
47697 "#pragma acc update", pragma_tok);
47699 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47701 error_at (pragma_tok->location,
47702 "%<#pragma acc update%> must contain at least one "
47703 "%<device%> or %<host%> or %<self%> clause");
47704 return NULL_TREE;
47707 stmt = make_node (OACC_UPDATE);
47708 TREE_TYPE (stmt) = void_type_node;
47709 OACC_UPDATE_CLAUSES (stmt) = clauses;
47710 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47711 add_stmt (stmt);
47712 return stmt;
47715 /* OpenACC 2.0:
47716 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
47718 LOC is the location of the #pragma token.
47721 #define OACC_WAIT_CLAUSE_MASK \
47722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
47724 static tree
47725 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
47727 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
47728 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47730 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47731 list = cp_parser_oacc_wait_list (parser, loc, list);
47733 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
47734 "#pragma acc wait", pragma_tok);
47736 stmt = c_finish_oacc_wait (loc, list, clauses);
47737 stmt = finish_expr_stmt (stmt);
47739 return stmt;
47742 /* OpenMP 4.0:
47743 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
47745 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
47746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
47747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
47748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
47749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
47750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
47751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
47753 static void
47754 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
47755 enum pragma_context context,
47756 bool variant_p)
47758 bool first_p = parser->omp_declare_simd == NULL;
47759 cp_omp_declare_simd_data data;
47760 if (first_p)
47762 data.error_seen = false;
47763 data.fndecl_seen = false;
47764 data.variant_p = variant_p;
47765 data.tokens = vNULL;
47766 data.attribs[0] = NULL;
47767 data.attribs[1] = NULL;
47768 data.loc = UNKNOWN_LOCATION;
47769 /* It is safe to take the address of a local variable; it will only be
47770 used while this scope is live. */
47771 parser->omp_declare_simd = &data;
47773 else if (parser->omp_declare_simd->variant_p != variant_p)
47775 error_at (pragma_tok->location,
47776 "%<#pragma omp declare %s%> followed by "
47777 "%<#pragma omp declare %s%>",
47778 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47779 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47780 parser->omp_declare_simd->error_seen = true;
47783 /* Store away all pragma tokens. */
47784 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47785 cp_lexer_consume_token (parser->lexer);
47786 cp_parser_require_pragma_eol (parser, pragma_tok);
47787 struct cp_token_cache *cp
47788 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47789 parser->omp_declare_simd->tokens.safe_push (cp);
47791 if (first_p)
47793 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47794 cp_parser_pragma (parser, context, NULL);
47795 switch (context)
47797 case pragma_external:
47798 cp_parser_declaration (parser, NULL_TREE);
47799 break;
47800 case pragma_member:
47801 cp_parser_member_declaration (parser);
47802 break;
47803 case pragma_objc_icode:
47804 cp_parser_block_declaration (parser, /*statement_p=*/false);
47805 break;
47806 default:
47807 cp_parser_declaration_statement (parser);
47808 break;
47810 if (parser->omp_declare_simd
47811 && !parser->omp_declare_simd->error_seen
47812 && !parser->omp_declare_simd->fndecl_seen)
47813 error_at (pragma_tok->location,
47814 "%<#pragma omp declare %s%> not immediately followed by "
47815 "function declaration or definition",
47816 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47817 data.tokens.release ();
47818 parser->omp_declare_simd = NULL;
47822 /* OpenMP 5.0:
47824 trait-selector:
47825 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47827 trait-score:
47828 score(score-expression)
47830 Note that this function returns a list of trait selectors for the
47831 trait-selector-set SET. */
47833 static tree
47834 cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
47835 bool has_parms_p)
47837 tree ret = NULL_TREE;
47840 tree selector;
47841 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47842 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47843 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47844 else
47846 cp_parser_error (parser, "expected trait selector name");
47847 return error_mark_node;
47850 enum omp_ts_code sel
47851 = omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
47853 if (sel == OMP_TRAIT_INVALID)
47855 /* Per the spec, "Implementations can ignore specified selectors
47856 that are not those described in this section"; however, we
47857 must record such selectors because they cause match failures. */
47858 warning_at (cp_lexer_peek_token (parser->lexer)->location,
47859 OPT_Wopenmp,
47860 "unknown selector %qs for context selector set %qs",
47861 IDENTIFIER_POINTER (selector), omp_tss_map[set]);
47862 cp_lexer_consume_token (parser->lexer);
47863 ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
47864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47865 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47866 n; --n)
47867 cp_lexer_consume_token (parser->lexer);
47868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47870 cp_lexer_consume_token (parser->lexer);
47871 continue;
47873 else
47874 break;
47877 cp_lexer_consume_token (parser->lexer);
47879 tree properties = NULL_TREE;
47880 tree scoreval = NULL_TREE;
47881 enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
47882 bool allow_score = omp_ts_map[sel].allow_score;
47883 tree t;
47885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47887 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
47889 error ("selector %qs does not accept any properties",
47890 IDENTIFIER_POINTER (selector));
47891 return error_mark_node;
47894 matching_parens parens;
47895 parens.consume_open (parser);
47897 cp_token *token = cp_lexer_peek_token (parser->lexer);
47898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47899 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
47900 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
47902 cp_lexer_save_tokens (parser->lexer);
47903 cp_lexer_consume_token (parser->lexer);
47904 cp_lexer_consume_token (parser->lexer);
47905 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
47906 true)
47907 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
47909 cp_lexer_rollback_tokens (parser->lexer);
47910 cp_lexer_consume_token (parser->lexer);
47912 matching_parens parens2;
47913 parens2.require_open (parser);
47914 tree score = cp_parser_constant_expression (parser);
47915 if (!parens2.require_close (parser))
47916 cp_parser_skip_to_closing_parenthesis (parser, true,
47917 false, true);
47918 cp_parser_require (parser, CPP_COLON, RT_COLON);
47919 if (!allow_score)
47920 error_at (token->location,
47921 "%<score%> cannot be specified in traits "
47922 "in the %qs trait-selector-set",
47923 omp_tss_map[set]);
47924 else if (score != error_mark_node)
47926 score = fold_non_dependent_expr (score);
47927 if (value_dependent_expression_p (score))
47928 scoreval = score;
47929 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
47930 || TREE_CODE (score) != INTEGER_CST)
47931 error_at (token->location, "%<score%> argument must "
47932 "be constant integer expression");
47933 else if (tree_int_cst_sgn (score) < 0)
47934 error_at (token->location, "%<score%> argument must "
47935 "be non-negative");
47936 else
47937 scoreval = score;
47940 else
47941 cp_lexer_rollback_tokens (parser->lexer);
47943 token = cp_lexer_peek_token (parser->lexer);
47946 switch (property_kind)
47948 case OMP_TRAIT_PROPERTY_ID:
47949 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47950 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47952 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
47953 cp_lexer_consume_token (parser->lexer);
47954 properties = make_trait_property (prop, NULL_TREE,
47955 properties);
47957 else
47959 cp_parser_error (parser, "expected identifier");
47960 return error_mark_node;
47962 break;
47963 case OMP_TRAIT_PROPERTY_NAME_LIST:
47966 tree prop = OMP_TP_NAMELIST_NODE;
47967 tree value = NULL_TREE;
47968 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47969 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47971 value = cp_lexer_peek_token (parser->lexer)->u.value;
47972 cp_lexer_consume_token (parser->lexer);
47974 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
47975 value = cp_parser_string_literal (parser,
47976 /*translate=*/false,
47977 /*wide_ok=*/false);
47978 else
47980 cp_parser_error (parser, "expected identifier or "
47981 "string literal");
47982 return error_mark_node;
47985 properties = make_trait_property (prop, value, properties);
47987 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47988 cp_lexer_consume_token (parser->lexer);
47989 else
47990 break;
47992 while (1);
47993 break;
47994 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
47995 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
47996 /* FIXME: this is bogus, the expression need
47997 not be constant. */
47998 t = cp_parser_constant_expression (parser);
47999 if (t != error_mark_node)
48001 t = fold_non_dependent_expr (t);
48002 if (!value_dependent_expression_p (t)
48003 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
48004 || !tree_fits_shwi_p (t)))
48005 error_at (token->location, "property must be "
48006 "constant integer expression");
48007 else
48008 properties = make_trait_property (NULL_TREE, t,
48009 properties);
48011 else
48012 return error_mark_node;
48013 break;
48014 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
48015 if (sel == OMP_TRAIT_CONSTRUCT_SIMD)
48017 if (!has_parms_p)
48019 error_at (token->location, "properties for %<simd%> "
48020 "selector may not be specified in "
48021 "%<metadirective%>");
48022 return error_mark_node;
48024 properties
48025 = cp_parser_omp_all_clauses (parser,
48026 OMP_DECLARE_SIMD_CLAUSE_MASK,
48027 "simd", NULL, true, 2);
48029 else if (sel == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
48031 /* FIXME: The "requires" selector was added in OpenMP 5.1.
48032 Currently only the now-deprecated syntax
48033 from OpenMP 5.0 is supported. */
48034 sorry_at (token->location,
48035 "%<requires%> selector is not supported yet");
48036 return error_mark_node;
48038 else
48039 gcc_unreachable ();
48040 break;
48041 default:
48042 gcc_unreachable ();
48045 if (!parens.require_close (parser))
48046 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
48048 properties = nreverse (properties);
48050 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
48051 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
48052 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
48054 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
48055 return error_mark_node;
48058 ret = make_trait_selector (sel, scoreval, properties, ret);
48060 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48061 cp_lexer_consume_token (parser->lexer);
48062 else
48063 break;
48065 while (1);
48067 return nreverse (ret);
48070 /* OpenMP 5.0:
48072 trait-set-selector[,trait-set-selector[,...]]
48074 trait-set-selector:
48075 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
48077 trait-set-selector-name:
48078 constructor
48079 device
48080 implementation
48081 user */
48083 static tree
48084 cp_parser_omp_context_selector_specification (cp_parser *parser,
48085 bool has_parms_p)
48087 tree ret = NULL_TREE;
48090 const char *setp = "";
48091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48092 setp
48093 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48094 enum omp_tss_code set = omp_lookup_tss_code (setp);
48096 if (set == OMP_TRAIT_SET_INVALID)
48098 cp_parser_error (parser, "expected context selector set name");
48099 return error_mark_node;
48102 cp_lexer_consume_token (parser->lexer);
48104 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
48105 return error_mark_node;
48107 matching_braces braces;
48108 if (!braces.require_open (parser))
48109 return error_mark_node;
48111 tree selectors
48112 = cp_parser_omp_context_selector (parser, set, has_parms_p);
48113 if (selectors == error_mark_node)
48115 cp_parser_skip_to_closing_brace (parser);
48116 ret = error_mark_node;
48118 else if (ret != error_mark_node)
48119 ret = make_trait_set_selector (set, selectors, ret);
48121 braces.require_close (parser);
48123 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48124 cp_lexer_consume_token (parser->lexer);
48125 else
48126 break;
48128 while (1);
48130 if (ret == error_mark_node)
48131 return ret;
48132 return nreverse (ret);
48135 /* Assumption clauses:
48136 OpenMP 5.1
48137 absent (directive-name-list)
48138 contains (directive-name-list)
48139 holds (expression)
48140 no_openmp
48141 no_openmp_routines
48142 no_parallelism */
48144 static void
48145 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
48146 bool is_assume)
48148 bool no_openmp = false;
48149 bool no_openmp_routines = false;
48150 bool no_parallelism = false;
48151 bitmap_head absent_head, contains_head;
48153 bitmap_obstack_initialize (NULL);
48154 bitmap_initialize (&absent_head, &bitmap_default_obstack);
48155 bitmap_initialize (&contains_head, &bitmap_default_obstack);
48157 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48158 error_at (cp_lexer_peek_token (parser->lexer)->location,
48159 "expected at least one assumption clause");
48161 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48163 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48164 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48165 cp_lexer_consume_token (parser->lexer);
48167 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48168 break;
48170 const char *p
48171 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48172 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48174 if (!strcmp (p, "no_openmp"))
48176 cp_lexer_consume_token (parser->lexer);
48177 if (no_openmp)
48178 error_at (cloc, "too many %qs clauses", "no_openmp");
48179 no_openmp = true;
48181 else if (!strcmp (p, "no_openmp_routines"))
48183 cp_lexer_consume_token (parser->lexer);
48184 if (no_openmp_routines)
48185 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
48186 no_openmp_routines = true;
48188 else if (!strcmp (p, "no_parallelism"))
48190 cp_lexer_consume_token (parser->lexer);
48191 if (no_parallelism)
48192 error_at (cloc, "too many %qs clauses", "no_parallelism");
48193 no_parallelism = true;
48195 else if (!strcmp (p, "holds"))
48197 cp_lexer_consume_token (parser->lexer);
48198 matching_parens parens;
48199 if (parens.require_open (parser))
48201 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
48202 tree t = cp_parser_assignment_expression (parser);
48203 if (!type_dependent_expression_p (t))
48204 t = contextual_conv_bool (t, tf_warning_or_error);
48205 if (is_assume && !error_operand_p (t))
48206 finish_expr_stmt (build_assume_call (eloc, t));
48207 if (!parens.require_close (parser))
48208 cp_parser_skip_to_closing_parenthesis (parser,
48209 /*recovering=*/true,
48210 /*or_comma=*/false,
48211 /*consume_paren=*/true);
48214 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
48216 cp_lexer_consume_token (parser->lexer);
48217 matching_parens parens;
48218 if (parens.require_open (parser))
48222 const char *directive[3] = {};
48223 int i;
48224 location_t dloc
48225 = cp_lexer_peek_token (parser->lexer)->location;
48226 for (i = 0; i < 3; i++)
48228 tree id;
48229 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
48230 id = cp_lexer_peek_nth_token (parser->lexer,
48231 i + 1)->u.value;
48232 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
48233 CPP_KEYWORD))
48235 enum rid rid
48236 = cp_lexer_peek_nth_token (parser->lexer,
48237 i + 1)->keyword;
48238 id = ridpointers[rid];
48240 else
48241 break;
48242 directive[i] = IDENTIFIER_POINTER (id);
48244 if (i == 0)
48245 error_at (dloc, "expected directive name");
48246 else
48248 const struct c_omp_directive *dir
48249 = c_omp_categorize_directive (directive[0],
48250 directive[1],
48251 directive[2]);
48252 if (dir == NULL
48253 || dir->kind == C_OMP_DIR_DECLARATIVE
48254 || dir->kind == C_OMP_DIR_INFORMATIONAL
48255 || dir->id == PRAGMA_OMP_END
48256 || (!dir->second && directive[1])
48257 || (!dir->third && directive[2]))
48258 error_at (dloc, "unknown OpenMP directive name in "
48259 "%qs clause argument", p);
48260 else
48262 int id = dir - c_omp_directives;
48263 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
48264 : &absent_head, id))
48265 error_at (dloc, "%<%s%s%s%s%s%> directive "
48266 "mentioned in both %<absent%> and "
48267 "%<contains%> clauses",
48268 directive[0],
48269 directive[1] ? " " : "",
48270 directive[1] ? directive[1] : "",
48271 directive[2] ? " " : "",
48272 directive[2] ? directive[2] : "");
48273 else if (!bitmap_set_bit (p[0] == 'a'
48274 ? &absent_head
48275 : &contains_head, id))
48276 error_at (dloc, "%<%s%s%s%s%s%> directive "
48277 "mentioned multiple times in %qs "
48278 "clauses",
48279 directive[0],
48280 directive[1] ? " " : "",
48281 directive[1] ? directive[1] : "",
48282 directive[2] ? " " : "",
48283 directive[2] ? directive[2] : "", p);
48285 for (; i; --i)
48286 cp_lexer_consume_token (parser->lexer);
48288 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48289 cp_lexer_consume_token (parser->lexer);
48290 else
48291 break;
48293 while (1);
48294 if (!parens.require_close (parser))
48295 cp_parser_skip_to_closing_parenthesis (parser,
48296 /*recovering=*/true,
48297 /*or_comma=*/false,
48298 /*consume_paren=*/true);
48301 else if (startswith (p, "ext_"))
48303 warning_at (cloc, OPT_Wopenmp, "unknown assumption clause %qs", p);
48304 cp_lexer_consume_token (parser->lexer);
48305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48306 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48307 n; --n)
48308 cp_lexer_consume_token (parser->lexer);
48310 else
48312 cp_lexer_consume_token (parser->lexer);
48313 error_at (cloc, "expected assumption clause");
48314 break;
48317 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48320 /* OpenMP 5.1
48321 # pragma omp assume clauses[optseq] new-line */
48323 static void
48324 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48326 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
48327 add_stmt (cp_parser_omp_structured_block (parser, if_p));
48330 /* OpenMP 5.1
48331 # pragma omp assumes clauses[optseq] new-line */
48333 static bool
48334 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
48336 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48337 return false;
48340 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
48341 that into "omp declare variant base" attribute. */
48343 static tree
48344 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
48345 tree attrs)
48347 matching_parens parens;
48348 if (!parens.require_open (parser))
48350 fail:
48351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48352 return attrs;
48355 bool template_p;
48356 cp_id_kind idk = CP_ID_KIND_NONE;
48357 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
48358 cp_expr varid
48359 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
48360 /*check_dependency_p=*/true,
48361 /*template_p=*/&template_p,
48362 /*declarator_p=*/false,
48363 /*optional_p=*/false);
48364 parens.require_close (parser);
48366 tree variant;
48367 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
48368 || TREE_CODE (varid) == TYPE_DECL
48369 || varid == error_mark_node)
48370 variant = varid;
48371 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
48372 variant = NULL_TREE;
48373 else
48375 tree ambiguous_decls;
48376 variant = cp_parser_lookup_name (parser, varid, none_type,
48377 template_p, /*is_namespace=*/false,
48378 /*check_dependency=*/true,
48379 &ambiguous_decls,
48380 varid.get_location ());
48381 if (ambiguous_decls)
48382 variant = NULL_TREE;
48384 if (variant == NULL_TREE)
48385 variant = error_mark_node;
48386 else if (TREE_CODE (variant) != SCOPE_REF)
48388 const char *error_msg;
48389 variant
48390 = finish_id_expression (varid, variant, parser->scope,
48391 &idk, false, true,
48392 &parser->non_integral_constant_expression_p,
48393 template_p, true, false, false, &error_msg,
48394 varid.get_location ());
48395 if (error_msg)
48396 cp_parser_error (parser, error_msg);
48398 location_t caret_loc = get_pure_location (varid.get_location ());
48399 location_t start_loc = get_start (varid_token->location);
48400 location_t finish_loc = get_finish (varid.get_location ());
48401 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
48403 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48404 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48405 cp_lexer_consume_token (parser->lexer);
48407 const char *clause = "";
48408 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
48409 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48410 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48411 if (strcmp (clause, "match"))
48413 cp_parser_error (parser, "expected %<match%>");
48414 goto fail;
48417 cp_lexer_consume_token (parser->lexer);
48419 if (!parens.require_open (parser))
48420 goto fail;
48422 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
48423 if (ctx == error_mark_node)
48424 goto fail;
48425 ctx = omp_check_context_selector (match_loc, ctx);
48426 if (ctx != error_mark_node && variant != error_mark_node)
48428 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
48429 match_loc);
48430 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
48431 loc_node = tree_cons (match_loc_node,
48432 build_int_cst (integer_type_node, idk),
48433 build_tree_list (loc_node, integer_zero_node));
48434 attrs = tree_cons (get_identifier ("omp declare variant base"),
48435 tree_cons (variant, ctx, loc_node), attrs);
48436 if (processing_template_decl)
48437 ATTR_IS_DEPENDENT (attrs) = 1;
48440 parens.require_close (parser);
48441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48442 return attrs;
48446 /* Finalize #pragma omp declare simd clauses after direct declarator has
48447 been parsed, and put that into "omp declare simd" attribute. */
48449 static tree
48450 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
48452 struct cp_token_cache *ce;
48453 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
48454 int i;
48456 if (!data->error_seen && data->fndecl_seen)
48458 error ("%<#pragma omp declare %s%> not immediately followed by "
48459 "a single function declaration or definition",
48460 data->variant_p ? "variant" : "simd");
48461 data->error_seen = true;
48463 if (data->error_seen)
48464 return attrs;
48466 FOR_EACH_VEC_ELT (data->tokens, i, ce)
48468 tree c, cl;
48470 cp_parser_push_lexer_for_tokens (parser, ce);
48471 parser->lexer->in_pragma = true;
48472 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48473 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48475 const char *kind = IDENTIFIER_POINTER (id);
48476 cp_lexer_consume_token (parser->lexer);
48477 if (strcmp (kind, "simd") == 0)
48479 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
48480 "#pragma omp declare simd",
48481 pragma_tok);
48482 if (cl)
48483 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48484 c = build_tree_list (get_identifier ("omp declare simd"), cl);
48485 TREE_CHAIN (c) = attrs;
48486 if (processing_template_decl)
48487 ATTR_IS_DEPENDENT (c) = 1;
48488 attrs = c;
48490 else
48492 gcc_assert (strcmp (kind, "variant") == 0);
48493 attrs
48494 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
48496 cp_parser_pop_lexer (parser);
48499 cp_lexer *lexer = NULL;
48500 for (int i = 0; i < 2; i++)
48502 if (data->attribs[i] == NULL)
48503 continue;
48504 for (tree *pa = data->attribs[i]; *pa; )
48505 if (get_attribute_namespace (*pa) == omp_identifier
48506 && is_attribute_p ("directive", get_attribute_name (*pa)))
48508 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
48510 tree d = TREE_VALUE (a);
48511 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48512 cp_token *first = DEFPARSE_TOKENS (d)->first;
48513 cp_token *last = DEFPARSE_TOKENS (d)->last;
48514 const char *directive[3] = {};
48515 for (int j = 0; j < 3; j++)
48517 tree id = NULL_TREE;
48518 if (first + j == last)
48519 break;
48520 if (first[j].type == CPP_NAME)
48521 id = first[j].u.value;
48522 else if (first[j].type == CPP_KEYWORD)
48523 id = ridpointers[(int) first[j].keyword];
48524 else
48525 break;
48526 directive[j] = IDENTIFIER_POINTER (id);
48528 const c_omp_directive *dir = NULL;
48529 if (directive[0])
48530 dir = c_omp_categorize_directive (directive[0], directive[1],
48531 directive[2]);
48532 if (dir == NULL)
48534 error_at (first->location,
48535 "unknown OpenMP directive name in "
48536 "%qs attribute argument",
48537 TREE_PUBLIC (d)
48538 ? "omp::decl" : "omp::directive");
48539 continue;
48541 if (dir->id != PRAGMA_OMP_DECLARE
48542 || (strcmp (directive[1], "simd") != 0
48543 && strcmp (directive[1], "variant") != 0))
48545 error_at (first->location,
48546 "OpenMP directive other than %<declare simd%> "
48547 "or %<declare variant%> appertains to a "
48548 "declaration");
48549 continue;
48552 if (parser->omp_attrs_forbidden_p)
48554 error_at (first->location,
48555 "mixing OpenMP directives with attribute and "
48556 "pragma syntax on the same statement");
48557 parser->omp_attrs_forbidden_p = false;
48560 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
48561 continue;
48562 if (lexer == NULL)
48564 lexer = cp_lexer_alloc ();
48565 lexer->debugging_p = parser->lexer->debugging_p;
48567 vec_safe_reserve (lexer->buffer, (last - first) + 2);
48568 cp_token tok = {};
48569 tok.type = CPP_PRAGMA;
48570 tok.keyword = RID_MAX;
48571 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
48572 tok.location = first->location;
48573 lexer->buffer->quick_push (tok);
48574 while (++first < last)
48575 lexer->buffer->quick_push (*first);
48576 tok = {};
48577 tok.type = CPP_PRAGMA_EOL;
48578 tok.keyword = RID_MAX;
48579 tok.location = last->location;
48580 lexer->buffer->quick_push (tok);
48581 tok = {};
48582 tok.type = CPP_EOF;
48583 tok.keyword = RID_MAX;
48584 tok.location = last->location;
48585 lexer->buffer->quick_push (tok);
48586 lexer->next = parser->lexer;
48587 lexer->next_token = lexer->buffer->address ();
48588 lexer->last_token = lexer->next_token
48589 + lexer->buffer->length ()
48590 - 1;
48591 lexer->in_omp_attribute_pragma = true;
48592 parser->lexer = lexer;
48593 /* Move the current source position to that of the first token
48594 in the new lexer. */
48595 cp_lexer_set_source_position_from_token (lexer->next_token);
48597 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48599 const char *kind = IDENTIFIER_POINTER (id);
48600 cp_lexer_consume_token (parser->lexer);
48602 tree c, cl;
48603 if (strcmp (kind, "simd") == 0)
48605 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48606 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48607 cp_lexer_consume_token (parser->lexer);
48609 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
48610 cl = cp_parser_omp_all_clauses (parser, mask,
48611 "#pragma omp declare simd",
48612 pragma_tok);
48613 if (cl)
48614 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48615 c = build_tree_list (get_identifier ("omp declare simd"),
48616 cl);
48617 TREE_CHAIN (c) = attrs;
48618 if (processing_template_decl)
48619 ATTR_IS_DEPENDENT (c) = 1;
48620 attrs = c;
48622 else
48624 gcc_assert (strcmp (kind, "variant") == 0);
48625 attrs
48626 = cp_finish_omp_declare_variant (parser, pragma_tok,
48627 attrs);
48629 gcc_assert (parser->lexer != lexer);
48630 vec_safe_truncate (lexer->buffer, 0);
48632 *pa = TREE_CHAIN (*pa);
48634 else
48635 pa = &TREE_CHAIN (*pa);
48637 if (lexer)
48638 cp_lexer_destroy (lexer);
48640 data->fndecl_seen = true;
48641 return attrs;
48644 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
48645 a threadprivate, groupprivate, allocate or declare target directive,
48646 return true and parse it for DECL. */
48648 bool
48649 cp_maybe_parse_omp_decl (tree decl, tree d)
48651 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48652 cp_token *first = DEFPARSE_TOKENS (d)->first;
48653 cp_token *last = DEFPARSE_TOKENS (d)->last;
48654 const char *directive[3] = {};
48655 for (int j = 0; j < 3; j++)
48657 tree id = NULL_TREE;
48658 if (first + j == last)
48659 break;
48660 if (first[j].type == CPP_NAME)
48661 id = first[j].u.value;
48662 else if (first[j].type == CPP_KEYWORD)
48663 id = ridpointers[(int) first[j].keyword];
48664 else
48665 break;
48666 directive[j] = IDENTIFIER_POINTER (id);
48668 const c_omp_directive *dir = NULL;
48669 if (directive[0])
48670 dir = c_omp_categorize_directive (directive[0], directive[1],
48671 directive[2]);
48672 if (dir == NULL)
48674 error_at (first->location,
48675 "unknown OpenMP directive name in "
48676 "%qs attribute argument", "omp::decl");
48677 return false;
48679 if (dir->id != PRAGMA_OMP_THREADPRIVATE
48680 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
48681 && dir->id != PRAGMA_OMP_ALLOCATE
48682 && (dir->id != PRAGMA_OMP_DECLARE
48683 || strcmp (directive[1], "target") != 0))
48684 return false;
48686 if (!flag_openmp && !dir->simd)
48687 return true;
48689 cp_parser *parser = the_parser;
48690 cp_lexer *lexer = cp_lexer_alloc ();
48691 lexer->debugging_p = parser->lexer->debugging_p;
48692 lexer->in_omp_decl_attribute = decl;
48693 vec_safe_reserve (lexer->buffer, last - first + 3, true);
48694 cp_token tok = {};
48695 tok.type = CPP_PRAGMA;
48696 tok.keyword = RID_MAX;
48697 tok.u.value = build_int_cst (NULL, dir->id);
48698 tok.location = first->location;
48699 lexer->buffer->quick_push (tok);
48700 while (++first < last)
48701 lexer->buffer->quick_push (*first);
48702 tok = {};
48703 tok.type = CPP_PRAGMA_EOL;
48704 tok.keyword = RID_MAX;
48705 tok.location = last->location;
48706 lexer->buffer->quick_push (tok);
48707 tok = {};
48708 tok.type = CPP_EOF;
48709 tok.keyword = RID_MAX;
48710 tok.location = last->location;
48711 lexer->buffer->quick_push (tok);
48712 lexer->next = parser->lexer;
48713 lexer->next_token = lexer->buffer->address ();
48714 lexer->last_token = lexer->next_token
48715 + lexer->buffer->length ()
48716 - 1;
48717 lexer->in_omp_attribute_pragma = true;
48718 parser->lexer = lexer;
48719 /* Move the current source position to that of the first token in the
48720 new lexer. */
48721 cp_lexer_set_source_position_from_token (lexer->next_token);
48722 cp_parser_pragma (parser, pragma_external, NULL);
48724 return true;
48727 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
48728 on #pragma omp declare target. Return false if errors were reported. */
48730 static bool
48731 handle_omp_declare_target_clause (tree c, tree t, int device_type,
48732 bool indirect)
48734 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
48735 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
48736 tree id;
48737 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
48739 id = get_identifier ("omp declare target link");
48740 std::swap (at1, at2);
48742 else
48743 id = get_identifier ("omp declare target");
48744 if (at2)
48746 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
48747 error_at (OMP_CLAUSE_LOCATION (c),
48748 "%qD specified both in declare target %<link%> and %qs"
48749 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
48750 else
48751 error_at (OMP_CLAUSE_LOCATION (c),
48752 "%qD specified both in declare target %<link%> and "
48753 "%<to%> or %<enter%> clauses", t);
48754 return false;
48756 if (!at1)
48758 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48759 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
48760 return true;
48762 symtab_node *node = symtab_node::get (t);
48763 if (node != NULL)
48765 node->offloadable = 1;
48766 if (ENABLE_OFFLOADING)
48768 g->have_offload = true;
48769 if (is_a <varpool_node *> (node))
48770 vec_safe_push (offload_vars, t);
48774 if (TREE_CODE (t) != FUNCTION_DECL)
48775 return true;
48776 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
48778 tree at3 = lookup_attribute ("omp declare target host",
48779 DECL_ATTRIBUTES (t));
48780 if (at3 == NULL_TREE)
48782 id = get_identifier ("omp declare target host");
48783 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48786 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48788 tree at3 = lookup_attribute ("omp declare target nohost",
48789 DECL_ATTRIBUTES (t));
48790 if (at3 == NULL_TREE)
48792 id = get_identifier ("omp declare target nohost");
48793 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48796 if (indirect)
48798 tree at4 = lookup_attribute ("omp declare target indirect",
48799 DECL_ATTRIBUTES (t));
48800 if (at4 == NULL_TREE)
48802 id = get_identifier ("omp declare target indirect");
48803 DECL_ATTRIBUTES (t)
48804 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48807 return true;
48810 /* OpenMP 4.0:
48811 # pragma omp declare target new-line
48812 declarations and definitions
48813 # pragma omp end declare target new-line
48815 OpenMP 4.5:
48816 # pragma omp declare target ( extended-list ) new-line
48818 # pragma omp declare target declare-target-clauses[seq] new-line */
48820 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48827 static void
48828 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48830 tree clauses = NULL_TREE;
48831 int device_type = 0;
48832 bool indirect = false;
48833 bool only_device_type_or_indirect = true;
48834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48835 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48836 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48837 clauses
48838 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48839 "#pragma omp declare target", pragma_tok);
48840 else if (parser->lexer->in_omp_decl_attribute
48841 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48843 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48844 clauses);
48845 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
48846 cp_parser_require_pragma_eol (parser, pragma_tok);
48848 else
48850 cp_omp_declare_target_attr a
48851 = { parser->lexer->in_omp_attribute_pragma, -1, false };
48852 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48853 cp_parser_require_pragma_eol (parser, pragma_tok);
48854 return;
48856 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48858 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48859 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48860 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48861 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48863 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48865 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE
48866 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48867 continue;
48868 tree t = OMP_CLAUSE_DECL (c);
48869 only_device_type_or_indirect = false;
48870 if (!handle_omp_declare_target_clause (c, t, device_type, indirect))
48871 continue;
48872 if (VAR_OR_FUNCTION_DECL_P (t)
48873 && DECL_LOCAL_DECL_P (t)
48874 && DECL_LANG_SPECIFIC (t)
48875 && DECL_LOCAL_DECL_ALIAS (t)
48876 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
48877 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
48878 device_type, indirect);
48880 if ((device_type || indirect) && only_device_type_or_indirect)
48881 error_at (OMP_CLAUSE_LOCATION (clauses),
48882 "directive with only %<device_type%> or %<indirect%> clauses");
48883 if (indirect && device_type && device_type != OMP_CLAUSE_DEVICE_TYPE_ANY)
48884 error_at (OMP_CLAUSE_LOCATION (clauses),
48885 "%<device_type%> clause must specify 'any' when used with "
48886 "an %<indirect%> clause");
48889 /* OpenMP 5.1
48890 # pragma omp begin assumes clauses[optseq] new-line
48892 # pragma omp begin declare target clauses[optseq] new-line */
48894 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
48895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48898 static void
48899 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
48901 const char *p = "";
48902 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48903 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48905 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48906 p = IDENTIFIER_POINTER (id);
48908 if (strcmp (p, "declare") == 0)
48910 cp_lexer_consume_token (parser->lexer);
48911 p = "";
48912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48914 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48915 p = IDENTIFIER_POINTER (id);
48917 if (strcmp (p, "target") == 0)
48919 cp_lexer_consume_token (parser->lexer);
48920 tree clauses
48921 = cp_parser_omp_all_clauses (parser,
48922 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
48923 "#pragma omp begin declare target",
48924 pragma_tok);
48925 int device_type = 0;
48926 bool indirect = 0;
48927 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48929 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48930 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48931 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48932 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48934 cp_omp_declare_target_attr a
48935 = { in_omp_attribute_pragma, device_type, indirect };
48936 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48938 else
48940 cp_parser_error (parser, "expected %<target%>");
48941 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48944 else if (strcmp (p, "assumes") == 0)
48946 cp_lexer_consume_token (parser->lexer);
48947 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48948 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
48949 vec_safe_push (scope_chain->omp_begin_assumes, a);
48951 else
48953 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
48954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48958 /* OpenMP 4.0:
48959 # pragma omp end declare target new-line
48961 OpenMP 5.1:
48962 # pragma omp end assumes new-line */
48964 static void
48965 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
48967 const char *p = "";
48968 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48972 p = IDENTIFIER_POINTER (id);
48974 if (strcmp (p, "declare") == 0)
48976 cp_lexer_consume_token (parser->lexer);
48977 p = "";
48978 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48980 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48981 p = IDENTIFIER_POINTER (id);
48983 if (strcmp (p, "target") == 0)
48984 cp_lexer_consume_token (parser->lexer);
48985 else
48987 cp_parser_error (parser, "expected %<target%>");
48988 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48989 return;
48991 cp_parser_require_pragma_eol (parser, pragma_tok);
48992 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
48993 error_at (pragma_tok->location,
48994 "%<#pragma omp end declare target%> without corresponding "
48995 "%<#pragma omp declare target%> or "
48996 "%<#pragma omp begin declare target%>");
48997 else
48999 cp_omp_declare_target_attr
49000 a = scope_chain->omp_declare_target_attribute->pop ();
49001 if (a.attr_syntax != in_omp_attribute_pragma)
49003 if (a.attr_syntax)
49004 error_at (pragma_tok->location,
49005 "%qs in attribute syntax terminated "
49006 "with %qs in pragma syntax",
49007 a.device_type >= 0 ? "begin declare target"
49008 : "declare target",
49009 "end declare target");
49010 else
49011 error_at (pragma_tok->location,
49012 "%qs in pragma syntax terminated "
49013 "with %qs in attribute syntax",
49014 a.device_type >= 0 ? "begin declare target"
49015 : "declare target",
49016 "end declare target");
49020 else if (strcmp (p, "assumes") == 0)
49022 cp_lexer_consume_token (parser->lexer);
49023 cp_parser_require_pragma_eol (parser, pragma_tok);
49024 if (!vec_safe_length (scope_chain->omp_begin_assumes))
49025 error_at (pragma_tok->location,
49026 "%qs without corresponding %qs",
49027 "#pragma omp end assumes", "#pragma omp begin assumes");
49028 else
49030 cp_omp_begin_assumes_data
49031 a = scope_chain->omp_begin_assumes->pop ();
49032 if (a.attr_syntax != in_omp_attribute_pragma)
49034 if (a.attr_syntax)
49035 error_at (pragma_tok->location,
49036 "%qs in attribute syntax terminated "
49037 "with %qs in pragma syntax",
49038 "begin assumes", "end assumes");
49039 else
49040 error_at (pragma_tok->location,
49041 "%qs in pragma syntax terminated "
49042 "with %qs in attribute syntax",
49043 "begin assumes", "end assumes");
49047 else
49049 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
49050 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49051 return;
49055 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
49056 expression and optional initializer clause of
49057 #pragma omp declare reduction. We store the expression(s) as
49058 either 3, 6 or 7 special statements inside of the artificial function's
49059 body. The first two statements are DECL_EXPRs for the artificial
49060 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
49061 expression that uses those variables.
49062 If there was any INITIALIZER clause, this is followed by further statements,
49063 the fourth and fifth statements are DECL_EXPRs for the artificial
49064 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
49065 constructor variant (first token after open paren is not omp_priv),
49066 then the sixth statement is a statement with the function call expression
49067 that uses the OMP_PRIV and optionally OMP_ORIG variable.
49068 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
49069 to initialize the OMP_PRIV artificial variable and there is seventh
49070 statement, a DECL_EXPR of the OMP_PRIV statement again. */
49072 static bool
49073 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
49075 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
49076 gcc_assert (TYPE_REF_P (type));
49077 type = TREE_TYPE (type);
49078 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
49079 DECL_ARTIFICIAL (omp_out) = 1;
49080 pushdecl (omp_out);
49081 add_decl_expr (omp_out);
49082 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
49083 DECL_ARTIFICIAL (omp_in) = 1;
49084 pushdecl (omp_in);
49085 add_decl_expr (omp_in);
49086 tree combiner;
49087 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
49089 keep_next_level (true);
49090 tree block = begin_omp_structured_block ();
49091 combiner = cp_parser_expression (parser);
49092 finish_expr_stmt (combiner);
49093 block = finish_omp_structured_block (block);
49094 if (processing_template_decl)
49095 block = build_stmt (input_location, EXPR_STMT, block);
49096 add_stmt (block);
49098 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
49099 return false;
49101 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49102 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49103 cp_lexer_consume_token (parser->lexer);
49105 const char *p = "";
49106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49108 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49109 p = IDENTIFIER_POINTER (id);
49112 if (strcmp (p, "initializer") == 0)
49114 cp_lexer_consume_token (parser->lexer);
49115 matching_parens parens;
49116 if (!parens.require_open (parser))
49117 return false;
49119 p = "";
49120 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49123 p = IDENTIFIER_POINTER (id);
49126 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
49127 DECL_ARTIFICIAL (omp_priv) = 1;
49128 pushdecl (omp_priv);
49129 add_decl_expr (omp_priv);
49130 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
49131 DECL_ARTIFICIAL (omp_orig) = 1;
49132 pushdecl (omp_orig);
49133 add_decl_expr (omp_orig);
49135 keep_next_level (true);
49136 block = begin_omp_structured_block ();
49138 bool ctor = false;
49139 if (strcmp (p, "omp_priv") == 0)
49141 bool is_non_constant_init;
49142 ctor = true;
49143 cp_lexer_consume_token (parser->lexer);
49144 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
49145 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
49146 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49147 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
49148 == CPP_CLOSE_PAREN
49149 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
49150 == CPP_CLOSE_PAREN))
49152 finish_omp_structured_block (block);
49153 error ("invalid initializer clause");
49154 return false;
49156 initializer = cp_parser_initializer (parser,
49157 /*is_direct_init=*/nullptr,
49158 &is_non_constant_init);
49159 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
49160 NULL_TREE, LOOKUP_ONLYCONVERTING);
49162 else
49164 cp_parser_parse_tentatively (parser);
49165 /* Don't create location wrapper nodes here. */
49166 auto_suppress_location_wrappers sentinel;
49167 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
49168 /*check_dependency_p=*/true,
49169 /*template_p=*/NULL,
49170 /*declarator_p=*/false,
49171 /*optional_p=*/false);
49172 vec<tree, va_gc> *args;
49173 if (fn_name == error_mark_node
49174 || cp_parser_error_occurred (parser)
49175 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49176 || ((args = cp_parser_parenthesized_expression_list
49177 (parser, non_attr, /*cast_p=*/false,
49178 /*allow_expansion_p=*/true,
49179 /*non_constant_p=*/NULL)),
49180 cp_parser_error_occurred (parser)))
49182 finish_omp_structured_block (block);
49183 cp_parser_abort_tentative_parse (parser);
49184 cp_parser_error (parser, "expected id-expression (arguments)");
49185 return false;
49187 unsigned int i;
49188 tree arg;
49189 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
49190 if (arg == omp_priv
49191 || (TREE_CODE (arg) == ADDR_EXPR
49192 && TREE_OPERAND (arg, 0) == omp_priv))
49193 break;
49194 cp_parser_abort_tentative_parse (parser);
49195 if (arg == NULL_TREE)
49196 error ("one of the initializer call arguments should be %<omp_priv%>"
49197 " or %<&omp_priv%>");
49198 initializer = cp_parser_postfix_expression (parser, false, false, false,
49199 false, NULL);
49200 finish_expr_stmt (initializer);
49203 block = finish_omp_structured_block (block);
49204 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
49205 if (processing_template_decl)
49206 block = build_stmt (input_location, EXPR_STMT, block);
49207 add_stmt (block);
49209 if (ctor)
49210 add_decl_expr (omp_orig);
49212 if (!parens.require_close (parser))
49213 return false;
49216 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
49217 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
49218 UNKNOWN_LOCATION);
49220 return true;
49223 /* OpenMP 4.0
49224 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49225 initializer-clause[opt] new-line
49227 initializer-clause:
49228 initializer (omp_priv initializer)
49229 initializer (function-name (argument-list)) */
49231 static void
49232 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
49233 enum pragma_context)
49235 auto_vec<tree> types;
49236 enum tree_code reduc_code = ERROR_MARK;
49237 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
49238 unsigned int i;
49239 cp_token *first_token;
49240 cp_token_cache *cp;
49241 int errs;
49242 void *p;
49244 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
49245 p = obstack_alloc (&declarator_obstack, 0);
49247 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
49248 goto fail;
49250 switch (cp_lexer_peek_token (parser->lexer)->type)
49252 case CPP_PLUS:
49253 reduc_code = PLUS_EXPR;
49254 break;
49255 case CPP_MULT:
49256 reduc_code = MULT_EXPR;
49257 break;
49258 case CPP_MINUS:
49259 reduc_code = MINUS_EXPR;
49260 break;
49261 case CPP_AND:
49262 reduc_code = BIT_AND_EXPR;
49263 break;
49264 case CPP_XOR:
49265 reduc_code = BIT_XOR_EXPR;
49266 break;
49267 case CPP_OR:
49268 reduc_code = BIT_IOR_EXPR;
49269 break;
49270 case CPP_AND_AND:
49271 reduc_code = TRUTH_ANDIF_EXPR;
49272 break;
49273 case CPP_OR_OR:
49274 reduc_code = TRUTH_ORIF_EXPR;
49275 break;
49276 case CPP_NAME:
49277 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
49278 break;
49279 default:
49280 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
49281 "%<|%>, %<&&%>, %<||%> or identifier");
49282 goto fail;
49285 if (reduc_code != ERROR_MARK)
49286 cp_lexer_consume_token (parser->lexer);
49288 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
49289 if (reduc_id == error_mark_node)
49290 goto fail;
49292 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
49293 goto fail;
49295 /* Types may not be defined in declare reduction type list. */
49296 const char *saved_message;
49297 saved_message = parser->type_definition_forbidden_message;
49298 parser->type_definition_forbidden_message
49299 = G_("types may not be defined in declare reduction type list");
49300 bool saved_colon_corrects_to_scope_p;
49301 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
49302 parser->colon_corrects_to_scope_p = false;
49303 bool saved_colon_doesnt_start_class_def_p;
49304 saved_colon_doesnt_start_class_def_p
49305 = parser->colon_doesnt_start_class_def_p;
49306 parser->colon_doesnt_start_class_def_p = true;
49308 while (true)
49310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49311 type = cp_parser_type_id (parser);
49312 if (type == error_mark_node)
49314 else if (ARITHMETIC_TYPE_P (type)
49315 && (orig_reduc_id == NULL_TREE
49316 || (TREE_CODE (type) != COMPLEX_TYPE
49317 && (id_equal (orig_reduc_id, "min")
49318 || id_equal (orig_reduc_id, "max")))))
49319 error_at (loc, "predeclared arithmetic type %qT in "
49320 "%<#pragma omp declare reduction%>", type);
49321 else if (FUNC_OR_METHOD_TYPE_P (type)
49322 || TREE_CODE (type) == ARRAY_TYPE)
49323 error_at (loc, "function or array type %qT in "
49324 "%<#pragma omp declare reduction%>", type);
49325 else if (TYPE_REF_P (type))
49326 error_at (loc, "reference type %qT in "
49327 "%<#pragma omp declare reduction%>", type);
49328 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
49329 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
49330 "type %qT in %<#pragma omp declare reduction%>", type);
49331 else
49332 types.safe_push (type);
49334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
49335 cp_lexer_consume_token (parser->lexer);
49336 else
49337 break;
49340 /* Restore the saved message. */
49341 parser->type_definition_forbidden_message = saved_message;
49342 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
49343 parser->colon_doesnt_start_class_def_p
49344 = saved_colon_doesnt_start_class_def_p;
49346 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
49347 || types.is_empty ())
49349 fail:
49350 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49351 goto done;
49354 first_token = cp_lexer_peek_token (parser->lexer);
49355 cp = NULL;
49356 errs = errorcount;
49357 FOR_EACH_VEC_ELT (types, i, type)
49359 tree fntype
49360 = build_function_type_list (void_type_node,
49361 cp_build_reference_type (type, false),
49362 NULL_TREE);
49363 tree this_reduc_id = reduc_id;
49364 if (!dependent_type_p (type))
49365 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
49366 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
49367 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
49368 DECL_ARTIFICIAL (fndecl) = 1;
49369 DECL_EXTERNAL (fndecl) = 1;
49370 DECL_DECLARED_INLINE_P (fndecl) = 1;
49371 DECL_IGNORED_P (fndecl) = 1;
49372 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
49373 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
49374 DECL_ATTRIBUTES (fndecl)
49375 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
49376 DECL_ATTRIBUTES (fndecl));
49377 bool block_scope = false;
49378 if (current_function_decl)
49380 block_scope = true;
49381 DECL_CONTEXT (fndecl) = current_function_decl;
49382 DECL_LOCAL_DECL_P (fndecl) = true;
49385 if (processing_template_decl)
49386 fndecl = push_template_decl (fndecl);
49388 if (block_scope)
49390 if (!processing_template_decl)
49391 pushdecl (fndecl);
49393 else if (current_class_type)
49395 if (cp == NULL)
49397 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49398 cp_lexer_consume_token (parser->lexer);
49399 cp = cp_token_cache_new (first_token,
49400 cp_lexer_peek_nth_token (parser->lexer,
49401 2));
49403 DECL_STATIC_FUNCTION_P (fndecl) = 1;
49404 finish_member_declaration (fndecl);
49405 DECL_PENDING_INLINE_INFO (fndecl) = cp;
49406 DECL_PENDING_INLINE_P (fndecl) = 1;
49407 vec_safe_push (unparsed_funs_with_definitions, fndecl);
49408 continue;
49410 else
49412 DECL_CONTEXT (fndecl) = current_namespace;
49413 tree d = pushdecl (fndecl);
49414 /* We should never meet a matched duplicate decl. */
49415 gcc_checking_assert (d == error_mark_node || d == fndecl);
49418 tree block = NULL_TREE;
49419 if (!block_scope)
49420 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
49421 else
49422 block = begin_omp_structured_block ();
49423 if (cp)
49425 cp_parser_push_lexer_for_tokens (parser, cp);
49426 parser->lexer->in_pragma = true;
49429 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
49431 if (cp)
49432 cp_parser_pop_lexer (parser);
49433 if (!block_scope)
49434 finish_function (/*inline_p=*/false);
49435 else
49437 DECL_CONTEXT (fndecl) = current_function_decl;
49438 if (DECL_TEMPLATE_INFO (fndecl))
49439 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
49441 if (!ok)
49442 goto fail;
49444 if (block_scope)
49446 block = finish_omp_structured_block (block);
49447 if (TREE_CODE (block) == BIND_EXPR)
49448 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
49449 else if (TREE_CODE (block) == STATEMENT_LIST)
49450 DECL_SAVED_TREE (fndecl) = block;
49451 if (processing_template_decl)
49452 add_decl_expr (fndecl);
49455 cp_check_omp_declare_reduction (fndecl);
49456 if (cp == NULL && types.length () > 1)
49457 cp = cp_token_cache_new (first_token,
49458 cp_lexer_peek_nth_token (parser->lexer, 2));
49459 if (errs != errorcount)
49460 break;
49463 cp_parser_require_pragma_eol (parser, pragma_tok);
49465 done:
49466 /* Free any declarators allocated. */
49467 obstack_free (&declarator_obstack, p);
49470 /* OpenMP 4.0
49471 #pragma omp declare simd declare-simd-clauses[optseq] new-line
49472 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49473 initializer-clause[opt] new-line
49474 #pragma omp declare target new-line
49476 OpenMP 5.0
49477 #pragma omp declare variant (identifier) match (context-selector) */
49479 static bool
49480 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
49481 enum pragma_context context)
49483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49486 const char *p = IDENTIFIER_POINTER (id);
49488 if (strcmp (p, "simd") == 0)
49490 cp_lexer_consume_token (parser->lexer);
49491 cp_parser_omp_declare_simd (parser, pragma_tok,
49492 context, false);
49493 return true;
49495 if (flag_openmp && strcmp (p, "variant") == 0)
49497 cp_lexer_consume_token (parser->lexer);
49498 cp_parser_omp_declare_simd (parser, pragma_tok,
49499 context, true);
49500 return true;
49502 cp_ensure_no_omp_declare_simd (parser);
49503 if (strcmp (p, "reduction") == 0)
49505 cp_lexer_consume_token (parser->lexer);
49506 cp_parser_omp_declare_reduction (parser, pragma_tok,
49507 context);
49508 return false;
49510 if (!flag_openmp) /* flag_openmp_simd */
49512 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49513 return false;
49515 if (strcmp (p, "target") == 0)
49517 cp_lexer_consume_token (parser->lexer);
49518 cp_parser_omp_declare_target (parser, pragma_tok);
49519 return false;
49522 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
49523 "%<target%> or %<variant%>");
49524 cp_parser_require_pragma_eol (parser, pragma_tok);
49525 return false;
49528 /* OpenMP 5.0
49529 #pragma omp requires clauses[optseq] new-line */
49531 static bool
49532 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
49534 enum omp_requires new_req = (enum omp_requires) 0;
49536 location_t loc = pragma_tok->location;
49537 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49540 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49541 cp_lexer_consume_token (parser->lexer);
49543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49546 const char *p = IDENTIFIER_POINTER (id);
49547 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49548 enum omp_requires this_req = (enum omp_requires) 0;
49550 if (!strcmp (p, "unified_address"))
49551 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
49552 else if (!strcmp (p, "unified_shared_memory"))
49553 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
49554 else if (!strcmp (p, "dynamic_allocators"))
49555 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
49556 else if (!strcmp (p, "reverse_offload"))
49557 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
49558 else if (!strcmp (p, "atomic_default_mem_order"))
49560 cp_lexer_consume_token (parser->lexer);
49562 matching_parens parens;
49563 if (parens.require_open (parser))
49565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49567 id = cp_lexer_peek_token (parser->lexer)->u.value;
49568 p = IDENTIFIER_POINTER (id);
49570 if (!strcmp (p, "seq_cst"))
49571 this_req
49572 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
49573 else if (!strcmp (p, "relaxed"))
49574 this_req
49575 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
49576 else if (!strcmp (p, "release"))
49577 this_req
49578 = (enum omp_requires) OMP_MEMORY_ORDER_RELEASE;
49579 else if (!strcmp (p, "acq_rel"))
49580 this_req
49581 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
49582 else if (!strcmp (p, "acquire"))
49583 this_req
49584 = (enum omp_requires) OMP_MEMORY_ORDER_ACQUIRE;
49586 if (this_req == 0)
49588 error_at (cp_lexer_peek_token (parser->lexer)->location,
49589 "expected %<acq_rel%>, %<acquire%>, "
49590 "%<relaxed%>, %<release%> or %<seq_cst%>");
49591 switch (cp_lexer_peek_token (parser->lexer)->type)
49593 case CPP_EOF:
49594 case CPP_PRAGMA_EOL:
49595 case CPP_CLOSE_PAREN:
49596 break;
49597 default:
49598 if (cp_lexer_nth_token_is (parser->lexer, 2,
49599 CPP_CLOSE_PAREN))
49600 cp_lexer_consume_token (parser->lexer);
49601 break;
49604 else
49605 cp_lexer_consume_token (parser->lexer);
49607 if (!parens.require_close (parser))
49608 cp_parser_skip_to_closing_parenthesis (parser,
49609 /*recovering=*/true,
49610 /*or_comma=*/false,
49611 /*consume_paren=*/
49612 true);
49614 if (this_req == 0)
49616 cp_parser_require_pragma_eol (parser, pragma_tok);
49617 return false;
49620 p = NULL;
49622 else
49624 error_at (cloc, "expected %<unified_address%>, "
49625 "%<unified_shared_memory%>, "
49626 "%<dynamic_allocators%>, "
49627 "%<reverse_offload%> "
49628 "or %<atomic_default_mem_order%> clause");
49629 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49630 return false;
49632 if (p)
49633 cp_lexer_consume_token (parser->lexer);
49634 if (this_req)
49636 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49638 if ((this_req & new_req) != 0)
49639 error_at (cloc, "too many %qs clauses", p);
49640 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
49641 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
49642 error_at (cloc, "%qs clause used lexically after first "
49643 "target construct or offloading API", p);
49645 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49647 error_at (cloc, "too many %qs clauses",
49648 "atomic_default_mem_order");
49649 this_req = (enum omp_requires) 0;
49651 else if ((omp_requires_mask
49652 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49654 error_at (cloc, "more than one %<atomic_default_mem_order%>"
49655 " clause in a single compilation unit");
49656 this_req
49657 = (enum omp_requires)
49658 (omp_requires_mask
49659 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
49661 else if ((omp_requires_mask
49662 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
49663 error_at (cloc, "%<atomic_default_mem_order%> clause used "
49664 "lexically after first %<atomic%> construct "
49665 "without memory order clause");
49666 new_req = (enum omp_requires) (new_req | this_req);
49667 omp_requires_mask
49668 = (enum omp_requires) (omp_requires_mask | this_req);
49669 continue;
49672 break;
49674 cp_parser_require_pragma_eol (parser, pragma_tok);
49676 if (new_req == 0)
49677 error_at (loc, "%<pragma omp requires%> requires at least one clause");
49678 return false;
49682 /* OpenMP 5.1:
49683 #pragma omp nothing new-line */
49685 static void
49686 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
49688 cp_parser_require_pragma_eol (parser, pragma_tok);
49692 /* OpenMP 5.1
49693 #pragma omp error clauses[optseq] new-line */
49695 static bool
49696 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
49697 enum pragma_context context)
49699 int at_compilation = -1;
49700 int severity_fatal = -1;
49701 tree message = NULL_TREE;
49702 bool bad = false;
49703 location_t loc = pragma_tok->location;
49705 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49707 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49708 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49709 cp_lexer_consume_token (parser->lexer);
49711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
49712 break;
49714 const char *p
49715 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49716 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49717 static const char *args[] = {
49718 "execution", "compilation", "warning", "fatal"
49720 int *v = NULL;
49721 int idx = 0, n = -1;
49722 tree m = NULL_TREE;
49724 if (!strcmp (p, "at"))
49725 v = &at_compilation;
49726 else if (!strcmp (p, "severity"))
49728 v = &severity_fatal;
49729 idx += 2;
49731 else if (strcmp (p, "message"))
49733 error_at (cloc,
49734 "expected %<at%>, %<severity%> or %<message%> clause");
49735 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49736 return false;
49739 cp_lexer_consume_token (parser->lexer);
49741 matching_parens parens;
49742 if (parens.require_open (parser))
49744 if (v == NULL)
49746 m = cp_parser_assignment_expression (parser);
49747 if (type_dependent_expression_p (m))
49748 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
49749 else
49750 m = perform_implicit_conversion_flags (const_string_type_node, m,
49751 tf_warning_or_error,
49752 LOOKUP_NORMAL);
49754 else
49756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49758 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
49759 const char *q = IDENTIFIER_POINTER (val);
49761 if (!strcmp (q, args[idx]))
49762 n = 0;
49763 else if (!strcmp (q, args[idx + 1]))
49764 n = 1;
49766 if (n == -1)
49768 error_at (cp_lexer_peek_token (parser->lexer)->location,
49769 "expected %qs or %qs", args[idx], args[idx + 1]);
49770 bad = true;
49771 switch (cp_lexer_peek_token (parser->lexer)->type)
49773 case CPP_EOF:
49774 case CPP_PRAGMA_EOL:
49775 case CPP_CLOSE_PAREN:
49776 break;
49777 default:
49778 if (cp_lexer_nth_token_is (parser->lexer, 2,
49779 CPP_CLOSE_PAREN))
49780 cp_lexer_consume_token (parser->lexer);
49781 break;
49784 else
49785 cp_lexer_consume_token (parser->lexer);
49788 if (!parens.require_close (parser))
49789 cp_parser_skip_to_closing_parenthesis (parser,
49790 /*recovering=*/true,
49791 /*or_comma=*/false,
49792 /*consume_paren=*/
49793 true);
49795 if (v == NULL)
49797 if (message)
49799 error_at (cloc, "too many %qs clauses", p);
49800 bad = true;
49802 else
49803 message = m;
49805 else if (n != -1)
49807 if (*v != -1)
49809 error_at (cloc, "too many %qs clauses", p);
49810 bad = true;
49812 else
49813 *v = n;
49816 else
49817 bad = true;
49819 cp_parser_require_pragma_eol (parser, pragma_tok);
49820 if (bad)
49821 return true;
49823 if (at_compilation == -1)
49824 at_compilation = 1;
49825 if (severity_fatal == -1)
49826 severity_fatal = 1;
49827 if (!at_compilation)
49829 if (context != pragma_compound)
49831 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49832 "may only be used in compound statements");
49833 return true;
49835 tree fndecl
49836 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49837 : BUILT_IN_GOMP_WARNING);
49838 if (!message)
49839 message = build_zero_cst (const_string_type_node);
49840 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49841 build_all_ones_cst (size_type_node));
49842 add_stmt (stmt);
49843 return true;
49846 if (in_discarded_stmt)
49847 return false;
49849 const char *msg = NULL;
49850 if (message)
49852 msg = c_getstr (fold_for_warn (message));
49853 if (msg == NULL)
49854 msg = _("<message unknown at compile time>");
49856 if (msg)
49857 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49858 "%<pragma omp error%> encountered: %s", msg);
49859 else
49860 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49861 "%<pragma omp error%> encountered");
49862 return false;
49865 /* OpenMP 4.5:
49866 #pragma omp taskloop taskloop-clause[optseq] new-line
49867 for-loop
49869 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
49870 for-loop */
49872 #define OMP_TASKLOOP_CLAUSE_MASK \
49873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
49874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
49875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
49876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
49877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
49878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
49879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
49880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
49881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
49882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
49883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
49884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
49885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
49886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
49887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
49888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
49889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
49891 static tree
49892 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
49893 char *p_name, omp_clause_mask mask, tree *cclauses,
49894 bool *if_p)
49896 tree clauses, sb, ret;
49897 unsigned int save;
49898 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49900 strcat (p_name, " taskloop");
49901 mask |= OMP_TASKLOOP_CLAUSE_MASK;
49902 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
49903 clause. */
49904 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
49905 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
49907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49910 const char *p = IDENTIFIER_POINTER (id);
49912 if (strcmp (p, "simd") == 0)
49914 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
49915 if (cclauses == NULL)
49916 cclauses = cclauses_buf;
49918 cp_lexer_consume_token (parser->lexer);
49919 if (!flag_openmp) /* flag_openmp_simd */
49920 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49921 cclauses, if_p);
49922 sb = begin_omp_structured_block ();
49923 save = cp_parser_begin_omp_structured_block (parser);
49924 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49925 cclauses, if_p);
49926 cp_parser_end_omp_structured_block (parser, save);
49927 tree body = finish_omp_structured_block (sb);
49928 if (ret == NULL)
49929 return ret;
49930 ret = make_node (OMP_TASKLOOP);
49931 TREE_TYPE (ret) = void_type_node;
49932 OMP_FOR_BODY (ret) = body;
49933 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49934 SET_EXPR_LOCATION (ret, loc);
49935 add_stmt (ret);
49936 return ret;
49939 if (!flag_openmp) /* flag_openmp_simd */
49941 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49942 return NULL_TREE;
49945 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
49946 cclauses == NULL);
49947 if (cclauses)
49949 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
49950 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49953 keep_next_level (true);
49954 sb = begin_omp_structured_block ();
49955 save = cp_parser_begin_omp_structured_block (parser);
49957 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
49958 if_p);
49960 cp_parser_end_omp_structured_block (parser, save);
49961 add_stmt (finish_omp_structured_block (sb));
49963 return ret;
49967 /* OpenACC 2.0:
49968 # pragma acc routine oacc-routine-clause[optseq] new-line
49969 function-definition
49971 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
49974 #define OACC_ROUTINE_CLAUSE_MASK \
49975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
49976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
49977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
49978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
49979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
49981 /* Parse the OpenACC routine pragma. This has an optional '( name )'
49982 component, which must resolve to a declared namespace-scope
49983 function. The clauses are either processed directly (for a named
49984 function), or defered until the immediatley following declaration
49985 is parsed. */
49987 static void
49988 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
49989 enum pragma_context context)
49991 gcc_checking_assert (context == pragma_external);
49992 /* The checking for "another pragma following this one" in the "no optional
49993 '( name )'" case makes sure that we dont re-enter. */
49994 gcc_checking_assert (parser->oacc_routine == NULL);
49996 cp_oacc_routine_data data;
49997 data.error_seen = false;
49998 data.fndecl_seen = false;
49999 data.tokens = vNULL;
50000 data.clauses = NULL_TREE;
50001 data.loc = pragma_tok->location;
50002 /* It is safe to take the address of a local variable; it will only be
50003 used while this scope is live. */
50004 parser->oacc_routine = &data;
50006 /* Look for optional '( name )'. */
50007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
50009 matching_parens parens;
50010 parens.consume_open (parser); /* '(' */
50012 /* We parse the name as an id-expression. If it resolves to
50013 anything other than a non-overloaded function at namespace
50014 scope, it's an error. */
50015 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
50016 tree name = cp_parser_id_expression (parser,
50017 /*template_keyword_p=*/false,
50018 /*check_dependency_p=*/false,
50019 /*template_p=*/NULL,
50020 /*declarator_p=*/false,
50021 /*optional_p=*/false);
50022 tree decl = (identifier_p (name)
50023 ? cp_parser_lookup_name_simple (parser, name, name_loc)
50024 : name);
50025 if (name != error_mark_node && decl == error_mark_node)
50026 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
50028 if (decl == error_mark_node
50029 || !parens.require_close (parser))
50031 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50032 parser->oacc_routine = NULL;
50033 return;
50036 data.clauses
50037 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50038 "#pragma acc routine",
50039 cp_lexer_peek_token (parser->lexer));
50040 /* The clauses are in reverse order; fix that to make later diagnostic
50041 emission easier. */
50042 data.clauses = nreverse (data.clauses);
50044 if (decl && is_overloaded_fn (decl)
50045 && (TREE_CODE (decl) != FUNCTION_DECL
50046 || DECL_FUNCTION_TEMPLATE_P (decl)))
50048 error_at (name_loc,
50049 "%<#pragma acc routine%> names a set of overloads");
50050 parser->oacc_routine = NULL;
50051 return;
50054 /* Perhaps we should use the same rule as declarations in different
50055 namespaces? */
50056 if (!DECL_NAMESPACE_SCOPE_P (decl))
50058 error_at (name_loc,
50059 "%qD does not refer to a namespace scope function", decl);
50060 parser->oacc_routine = NULL;
50061 return;
50064 if (TREE_CODE (decl) != FUNCTION_DECL)
50066 error_at (name_loc, "%qD does not refer to a function", decl);
50067 parser->oacc_routine = NULL;
50068 return;
50071 cp_finalize_oacc_routine (parser, decl, false);
50072 parser->oacc_routine = NULL;
50074 else /* No optional '( name )'. */
50076 /* Store away all pragma tokens. */
50077 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50078 cp_lexer_consume_token (parser->lexer);
50079 cp_parser_require_pragma_eol (parser, pragma_tok);
50080 struct cp_token_cache *cp
50081 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
50082 parser->oacc_routine->tokens.safe_push (cp);
50084 /* Emit a helpful diagnostic if there's another pragma following this
50085 one. */
50086 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
50088 cp_ensure_no_oacc_routine (parser);
50089 data.tokens.release ();
50090 /* ..., and then just keep going. */
50091 return;
50094 /* We only have to consider the pragma_external case here. */
50095 cp_parser_declaration (parser, NULL_TREE);
50096 if (parser->oacc_routine
50097 && !parser->oacc_routine->fndecl_seen)
50098 cp_ensure_no_oacc_routine (parser);
50099 else
50100 parser->oacc_routine = NULL;
50101 data.tokens.release ();
50105 /* Finalize #pragma acc routine clauses after direct declarator has
50106 been parsed. */
50108 static tree
50109 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
50111 struct cp_token_cache *ce;
50112 cp_oacc_routine_data *data = parser->oacc_routine;
50114 if (!data->error_seen && data->fndecl_seen)
50116 error_at (data->loc,
50117 "%<#pragma acc routine%> not immediately followed by "
50118 "a single function declaration or definition");
50119 data->error_seen = true;
50121 if (data->error_seen)
50122 return attrs;
50124 gcc_checking_assert (data->tokens.length () == 1);
50125 ce = data->tokens[0];
50127 cp_parser_push_lexer_for_tokens (parser, ce);
50128 parser->lexer->in_pragma = true;
50129 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
50131 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
50132 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
50133 parser->oacc_routine->clauses
50134 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50135 "#pragma acc routine", pragma_tok);
50136 /* The clauses are in reverse order; fix that to make later diagnostic
50137 emission easier. */
50138 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
50139 cp_parser_pop_lexer (parser);
50140 /* Later, cp_finalize_oacc_routine will process the clauses. */
50141 parser->oacc_routine->fndecl_seen = true;
50143 return attrs;
50146 /* Apply any saved OpenACC routine clauses to a just-parsed
50147 declaration. */
50149 static void
50150 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
50152 if (UNLIKELY (parser->oacc_routine != NULL))
50154 /* Keep going if we're in error reporting mode. */
50155 if (parser->oacc_routine->error_seen
50156 || fndecl == error_mark_node)
50157 return;
50159 if (TREE_CODE (fndecl) != FUNCTION_DECL)
50161 if (parser->oacc_routine->fndecl_seen)
50163 error_at (parser->oacc_routine->loc,
50164 "%<#pragma acc routine%> not immediately followed by"
50165 " a single function declaration or definition");
50166 parser->oacc_routine = NULL;
50167 return;
50170 cp_ensure_no_oacc_routine (parser);
50171 return;
50174 int compatible
50175 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
50176 parser->oacc_routine->loc,
50177 "#pragma acc routine");
50178 if (compatible < 0)
50180 parser->oacc_routine = NULL;
50181 return;
50183 if (compatible > 0)
50186 else
50188 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
50190 error_at (parser->oacc_routine->loc,
50191 TREE_USED (fndecl)
50192 ? G_("%<#pragma acc routine%> must be applied before"
50193 " use")
50194 : G_("%<#pragma acc routine%> must be applied before"
50195 " definition"));
50196 parser->oacc_routine = NULL;
50197 return;
50200 /* Set the routine's level of parallelism. */
50201 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
50202 oacc_replace_fn_attrib (fndecl, dims);
50204 /* Add an "omp declare target" attribute. */
50205 DECL_ATTRIBUTES (fndecl)
50206 = tree_cons (get_identifier ("omp declare target"),
50207 parser->oacc_routine->clauses,
50208 DECL_ATTRIBUTES (fndecl));
50213 /* Main entry point to OpenMP statement pragmas. */
50215 static void
50216 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
50218 tree stmt;
50219 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
50220 omp_clause_mask mask (0);
50222 switch (cp_parser_pragma_kind (pragma_tok))
50224 case PRAGMA_OACC_ATOMIC:
50225 cp_parser_omp_atomic (parser, pragma_tok, true);
50226 return;
50227 case PRAGMA_OACC_CACHE:
50228 stmt = cp_parser_oacc_cache (parser, pragma_tok);
50229 break;
50230 case PRAGMA_OACC_DATA:
50231 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
50232 break;
50233 case PRAGMA_OACC_ENTER_DATA:
50234 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
50235 break;
50236 case PRAGMA_OACC_EXIT_DATA:
50237 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
50238 break;
50239 case PRAGMA_OACC_HOST_DATA:
50240 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
50241 break;
50242 case PRAGMA_OACC_KERNELS:
50243 case PRAGMA_OACC_PARALLEL:
50244 case PRAGMA_OACC_SERIAL:
50245 strcpy (p_name, "#pragma acc");
50246 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
50247 break;
50248 case PRAGMA_OACC_LOOP:
50249 strcpy (p_name, "#pragma acc");
50250 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
50251 if_p);
50252 break;
50253 case PRAGMA_OACC_UPDATE:
50254 stmt = cp_parser_oacc_update (parser, pragma_tok);
50255 break;
50256 case PRAGMA_OACC_WAIT:
50257 stmt = cp_parser_oacc_wait (parser, pragma_tok);
50258 break;
50259 case PRAGMA_OMP_ALLOCATE:
50260 cp_parser_omp_allocate (parser, pragma_tok);
50261 return;
50262 case PRAGMA_OMP_ATOMIC:
50263 cp_parser_omp_atomic (parser, pragma_tok, false);
50264 return;
50265 case PRAGMA_OMP_CRITICAL:
50266 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
50267 break;
50268 case PRAGMA_OMP_DISTRIBUTE:
50269 strcpy (p_name, "#pragma omp");
50270 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
50271 if_p);
50272 break;
50273 case PRAGMA_OMP_FOR:
50274 strcpy (p_name, "#pragma omp");
50275 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
50276 if_p);
50277 break;
50278 case PRAGMA_OMP_LOOP:
50279 strcpy (p_name, "#pragma omp");
50280 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
50281 if_p);
50282 break;
50283 case PRAGMA_OMP_MASKED:
50284 strcpy (p_name, "#pragma omp");
50285 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
50286 if_p);
50287 break;
50288 case PRAGMA_OMP_MASTER:
50289 strcpy (p_name, "#pragma omp");
50290 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
50291 if_p);
50292 break;
50293 case PRAGMA_OMP_PARALLEL:
50294 strcpy (p_name, "#pragma omp");
50295 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
50296 if_p);
50297 break;
50298 case PRAGMA_OMP_SCOPE:
50299 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
50300 break;
50301 case PRAGMA_OMP_SECTIONS:
50302 strcpy (p_name, "#pragma omp");
50303 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
50304 break;
50305 case PRAGMA_OMP_SIMD:
50306 strcpy (p_name, "#pragma omp");
50307 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
50308 if_p);
50309 break;
50310 case PRAGMA_OMP_SINGLE:
50311 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
50312 break;
50313 case PRAGMA_OMP_TASK:
50314 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
50315 break;
50316 case PRAGMA_OMP_TASKGROUP:
50317 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
50318 break;
50319 case PRAGMA_OMP_TASKLOOP:
50320 strcpy (p_name, "#pragma omp");
50321 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
50322 if_p);
50323 break;
50324 case PRAGMA_OMP_TEAMS:
50325 strcpy (p_name, "#pragma omp");
50326 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
50327 if_p);
50328 break;
50329 case PRAGMA_OMP_ASSUME:
50330 cp_parser_omp_assume (parser, pragma_tok, if_p);
50331 return;
50332 default:
50333 gcc_unreachable ();
50336 protected_set_expr_location (stmt, pragma_tok->location);
50339 /* Transactional Memory parsing routines. */
50341 /* Parse a transaction attribute.
50343 txn-attribute:
50344 attribute
50345 [ [ identifier ] ]
50347 We use this instead of cp_parser_attributes_opt for transactions to avoid
50348 the pedwarn in C++98 mode. */
50350 static tree
50351 cp_parser_txn_attribute_opt (cp_parser *parser)
50353 cp_token *token;
50354 tree attr_name, attr = NULL;
50356 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
50357 return cp_parser_attributes_opt (parser);
50359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
50360 return NULL_TREE;
50361 cp_lexer_consume_token (parser->lexer);
50362 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
50363 goto error1;
50365 token = cp_lexer_peek_token (parser->lexer);
50366 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
50368 token = cp_lexer_consume_token (parser->lexer);
50370 attr_name = (token->type == CPP_KEYWORD
50371 /* For keywords, use the canonical spelling,
50372 not the parsed identifier. */
50373 ? ridpointers[(int) token->keyword]
50374 : token->u.value);
50375 attr = build_tree_list (attr_name, NULL_TREE);
50377 else
50378 cp_parser_error (parser, "expected identifier");
50380 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50381 error1:
50382 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50383 return attr;
50386 /* Parse a __transaction_atomic or __transaction_relaxed statement.
50388 transaction-statement:
50389 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
50390 compound-statement
50391 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
50394 static tree
50395 cp_parser_transaction (cp_parser *parser, cp_token *token)
50397 unsigned char old_in = parser->in_transaction;
50398 unsigned char this_in = 1, new_in;
50399 enum rid keyword = token->keyword;
50400 tree stmt, attrs, noex;
50402 cp_lexer_consume_token (parser->lexer);
50404 if (keyword == RID_TRANSACTION_RELAXED
50405 || keyword == RID_SYNCHRONIZED)
50406 this_in |= TM_STMT_ATTR_RELAXED;
50407 else
50409 attrs = cp_parser_txn_attribute_opt (parser);
50410 if (attrs)
50411 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50414 /* Parse a noexcept specification. */
50415 if (keyword == RID_ATOMIC_NOEXCEPT)
50416 noex = boolean_true_node;
50417 else if (keyword == RID_ATOMIC_CANCEL)
50419 /* cancel-and-throw is unimplemented. */
50420 sorry ("%<atomic_cancel%>");
50421 noex = NULL_TREE;
50423 else
50424 noex = cp_parser_noexcept_specification_opt (parser,
50425 CP_PARSER_FLAGS_NONE,
50426 /*require_constexpr=*/true,
50427 /*consumed_expr=*/NULL,
50428 /*return_cond=*/true);
50430 /* Keep track if we're in the lexical scope of an outer transaction. */
50431 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
50433 stmt = begin_transaction_stmt (token->location, NULL, this_in);
50435 parser->in_transaction = new_in;
50436 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
50437 parser->in_transaction = old_in;
50439 finish_transaction_stmt (stmt, NULL, this_in, noex);
50441 return stmt;
50444 /* Parse a __transaction_atomic or __transaction_relaxed expression.
50446 transaction-expression:
50447 __transaction_atomic txn-noexcept-spec[opt] ( expression )
50448 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
50451 static tree
50452 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
50454 unsigned char old_in = parser->in_transaction;
50455 unsigned char this_in = 1;
50456 cp_token *token;
50457 tree expr, noex;
50458 bool noex_expr;
50459 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50461 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50462 || keyword == RID_TRANSACTION_RELAXED);
50464 if (!flag_tm)
50465 error_at (loc,
50466 keyword == RID_TRANSACTION_RELAXED
50467 ? G_("%<__transaction_relaxed%> without transactional memory "
50468 "support enabled")
50469 : G_("%<__transaction_atomic%> without transactional memory "
50470 "support enabled"));
50472 token = cp_parser_require_keyword (parser, keyword,
50473 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50474 : RT_TRANSACTION_RELAXED));
50475 gcc_assert (token != NULL);
50477 if (keyword == RID_TRANSACTION_RELAXED)
50478 this_in |= TM_STMT_ATTR_RELAXED;
50480 /* Set this early. This might mean that we allow transaction_cancel in
50481 an expression that we find out later actually has to be a constexpr.
50482 However, we expect that cxx_constant_value will be able to deal with
50483 this; also, if the noexcept has no constexpr, then what we parse next
50484 really is a transaction's body. */
50485 parser->in_transaction = this_in;
50487 /* Parse a noexcept specification. */
50488 noex = cp_parser_noexcept_specification_opt (parser,
50489 CP_PARSER_FLAGS_NONE,
50490 /*require_constexpr=*/false,
50491 &noex_expr,
50492 /*return_cond=*/true);
50494 if (!noex || !noex_expr
50495 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
50497 matching_parens parens;
50498 parens.require_open (parser);
50500 expr = cp_parser_expression (parser);
50501 expr = finish_parenthesized_expr (expr);
50503 parens.require_close (parser);
50505 else
50507 /* The only expression that is available got parsed for the noexcept
50508 already. noexcept is true then. */
50509 expr = noex;
50510 noex = boolean_true_node;
50513 expr = build_transaction_expr (token->location, expr, this_in, noex);
50514 parser->in_transaction = old_in;
50516 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
50517 return error_mark_node;
50519 return (flag_tm ? expr : error_mark_node);
50522 /* Parse a function-transaction-block.
50524 function-transaction-block:
50525 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
50526 function-body
50527 __transaction_atomic txn-attribute[opt] function-try-block
50528 __transaction_relaxed ctor-initializer[opt] function-body
50529 __transaction_relaxed function-try-block
50532 static void
50533 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
50535 unsigned char old_in = parser->in_transaction;
50536 unsigned char new_in = 1;
50537 tree compound_stmt, stmt, attrs;
50538 cp_token *token;
50540 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50541 || keyword == RID_TRANSACTION_RELAXED);
50542 token = cp_parser_require_keyword (parser, keyword,
50543 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50544 : RT_TRANSACTION_RELAXED));
50545 gcc_assert (token != NULL);
50547 if (keyword == RID_TRANSACTION_RELAXED)
50548 new_in |= TM_STMT_ATTR_RELAXED;
50549 else
50551 attrs = cp_parser_txn_attribute_opt (parser);
50552 if (attrs)
50553 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50556 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
50558 parser->in_transaction = new_in;
50560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
50561 cp_parser_function_try_block (parser);
50562 else
50563 cp_parser_ctor_initializer_opt_and_function_body
50564 (parser, /*in_function_try_block=*/false);
50566 parser->in_transaction = old_in;
50568 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
50571 /* Parse a __transaction_cancel statement.
50573 cancel-statement:
50574 __transaction_cancel txn-attribute[opt] ;
50575 __transaction_cancel txn-attribute[opt] throw-expression ;
50577 ??? Cancel and throw is not yet implemented. */
50579 static tree
50580 cp_parser_transaction_cancel (cp_parser *parser)
50582 cp_token *token;
50583 bool is_outer = false;
50584 tree stmt, attrs;
50586 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
50587 RT_TRANSACTION_CANCEL);
50588 gcc_assert (token != NULL);
50590 attrs = cp_parser_txn_attribute_opt (parser);
50591 if (attrs)
50592 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
50594 /* ??? Parse cancel-and-throw here. */
50596 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
50598 if (!flag_tm)
50600 error_at (token->location, "%<__transaction_cancel%> without "
50601 "transactional memory support enabled");
50602 return error_mark_node;
50604 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
50606 error_at (token->location, "%<__transaction_cancel%> within a "
50607 "%<__transaction_relaxed%>");
50608 return error_mark_node;
50610 else if (is_outer)
50612 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
50613 && !is_tm_may_cancel_outer (current_function_decl))
50615 error_at (token->location, "outer %<__transaction_cancel%> not "
50616 "within outer %<__transaction_atomic%>");
50617 error_at (token->location,
50618 " or a %<transaction_may_cancel_outer%> function");
50619 return error_mark_node;
50622 else if (parser->in_transaction == 0)
50624 error_at (token->location, "%<__transaction_cancel%> not within "
50625 "%<__transaction_atomic%>");
50626 return error_mark_node;
50629 stmt = build_tm_abort_call (token->location, is_outer);
50630 add_stmt (stmt);
50632 return stmt;
50636 /* Special handling for the first token or line in the file. The first
50637 thing in the file might be #pragma GCC pch_preprocess, which loads a
50638 PCH file, which is a GC collection point. So we need to handle this
50639 first pragma without benefit of an existing lexer structure.
50641 Always returns one token to the caller in *FIRST_TOKEN. This is
50642 either the true first token of the file, or the first token after
50643 the initial pragma. */
50645 static void
50646 cp_parser_initial_pragma (cp_token *first_token)
50648 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
50649 return;
50651 cp_lexer_get_preprocessor_token (0, first_token);
50653 tree name = NULL;
50654 if (first_token->type == CPP_STRING)
50656 name = first_token->u.value;
50658 cp_lexer_get_preprocessor_token (0, first_token);
50661 /* Skip to the end of the pragma. */
50662 if (first_token->type != CPP_PRAGMA_EOL)
50664 error_at (first_token->location,
50665 "malformed %<#pragma GCC pch_preprocess%>");
50667 cp_lexer_get_preprocessor_token (0, first_token);
50668 while (first_token->type != CPP_PRAGMA_EOL);
50671 /* Now actually load the PCH file. */
50672 if (name)
50673 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
50675 /* Read one more token to return to our caller. We have to do this
50676 after reading the PCH file in, since its pointers have to be
50677 live. */
50678 cp_lexer_get_preprocessor_token (0, first_token);
50681 /* Parse a pragma GCC ivdep. */
50683 static bool
50684 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
50686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50687 return true;
50690 /* Parse a pragma GCC unroll. */
50692 static tree
50693 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
50695 location_t location = cp_lexer_peek_token (parser->lexer)->location;
50696 tree unroll = cp_parser_constant_expression (parser);
50697 unroll = cp_check_pragma_unroll (location, fold_non_dependent_expr (unroll));
50698 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50699 return unroll;
50702 /* Parse a pragma GCC novector. */
50704 static bool
50705 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
50707 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50708 return true;
50711 /* Normal parsing of a pragma token. Here we can (and must) use the
50712 regular lexer. */
50714 static bool
50715 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
50717 cp_token *pragma_tok;
50718 unsigned int id;
50719 tree stmt;
50720 bool ret = false;
50722 pragma_tok = cp_lexer_consume_token (parser->lexer);
50723 gcc_assert (pragma_tok->type == CPP_PRAGMA);
50724 parser->lexer->in_pragma = true;
50726 id = cp_parser_pragma_kind (pragma_tok);
50727 if (parser->omp_for_parse_state
50728 && parser->omp_for_parse_state->in_intervening_code
50729 && id >= PRAGMA_OMP__START_
50730 && id <= PRAGMA_OMP__LAST_)
50732 error_at (pragma_tok->location,
50733 "intervening code must not contain OpenMP directives");
50734 parser->omp_for_parse_state->fail = true;
50735 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50736 return false;
50738 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
50739 cp_ensure_no_omp_declare_simd (parser);
50740 switch (id)
50742 case PRAGMA_GCC_PCH_PREPROCESS:
50743 error_at (pragma_tok->location,
50744 "%<#pragma GCC pch_preprocess%> must be first");
50745 break;
50747 case PRAGMA_OMP_BARRIER:
50748 switch (context)
50750 case pragma_compound:
50751 cp_parser_omp_barrier (parser, pragma_tok);
50752 return false;
50753 case pragma_stmt:
50754 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50755 "used in compound statements", "omp barrier");
50756 ret = true;
50757 break;
50758 default:
50759 goto bad_stmt;
50761 break;
50763 case PRAGMA_OMP_DEPOBJ:
50764 switch (context)
50766 case pragma_compound:
50767 cp_parser_omp_depobj (parser, pragma_tok);
50768 return false;
50769 case pragma_stmt:
50770 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50771 "used in compound statements", "omp depobj");
50772 ret = true;
50773 break;
50774 default:
50775 goto bad_stmt;
50777 break;
50779 case PRAGMA_OMP_FLUSH:
50780 switch (context)
50782 case pragma_compound:
50783 cp_parser_omp_flush (parser, pragma_tok);
50784 return false;
50785 case pragma_stmt:
50786 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50787 "used in compound statements", "omp flush");
50788 ret = true;
50789 break;
50790 default:
50791 goto bad_stmt;
50793 break;
50795 case PRAGMA_OMP_TASKWAIT:
50796 switch (context)
50798 case pragma_compound:
50799 cp_parser_omp_taskwait (parser, pragma_tok);
50800 return false;
50801 case pragma_stmt:
50802 error_at (pragma_tok->location,
50803 "%<#pragma %s%> may only be used in compound statements",
50804 "omp taskwait");
50805 ret = true;
50806 break;
50807 default:
50808 goto bad_stmt;
50810 break;
50812 case PRAGMA_OMP_TASKYIELD:
50813 switch (context)
50815 case pragma_compound:
50816 cp_parser_omp_taskyield (parser, pragma_tok);
50817 return false;
50818 case pragma_stmt:
50819 error_at (pragma_tok->location,
50820 "%<#pragma %s%> may only be used in compound statements",
50821 "omp taskyield");
50822 ret = true;
50823 break;
50824 default:
50825 goto bad_stmt;
50827 break;
50829 case PRAGMA_OMP_CANCEL:
50830 switch (context)
50832 case pragma_compound:
50833 cp_parser_omp_cancel (parser, pragma_tok);
50834 return false;
50835 case pragma_stmt:
50836 error_at (pragma_tok->location,
50837 "%<#pragma %s%> may only be used in compound statements",
50838 "omp cancel");
50839 ret = true;
50840 break;
50841 default:
50842 goto bad_stmt;
50844 break;
50846 case PRAGMA_OMP_CANCELLATION_POINT:
50847 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
50849 case PRAGMA_OMP_THREADPRIVATE:
50850 cp_parser_omp_threadprivate (parser, pragma_tok);
50851 return false;
50853 case PRAGMA_OMP_DECLARE:
50854 return cp_parser_omp_declare (parser, pragma_tok, context);
50856 case PRAGMA_OACC_DECLARE:
50857 cp_parser_oacc_declare (parser, pragma_tok);
50858 return false;
50860 case PRAGMA_OACC_ENTER_DATA:
50861 if (context == pragma_stmt)
50863 error_at (pragma_tok->location,
50864 "%<#pragma %s%> may only be used in compound statements",
50865 "acc enter data");
50866 ret = true;
50867 break;
50869 else if (context != pragma_compound)
50870 goto bad_stmt;
50871 cp_parser_omp_construct (parser, pragma_tok, if_p);
50872 return true;
50874 case PRAGMA_OACC_EXIT_DATA:
50875 if (context == pragma_stmt)
50877 error_at (pragma_tok->location,
50878 "%<#pragma %s%> may only be used in compound statements",
50879 "acc exit data");
50880 ret = true;
50881 break;
50883 else if (context != pragma_compound)
50884 goto bad_stmt;
50885 cp_parser_omp_construct (parser, pragma_tok, if_p);
50886 return true;
50888 case PRAGMA_OACC_ROUTINE:
50889 if (context != pragma_external)
50891 error_at (pragma_tok->location,
50892 "%<#pragma acc routine%> must be at file scope");
50893 ret = true;
50894 break;
50896 cp_parser_oacc_routine (parser, pragma_tok, context);
50897 return false;
50899 case PRAGMA_OACC_UPDATE:
50900 if (context == pragma_stmt)
50902 error_at (pragma_tok->location,
50903 "%<#pragma %s%> may only be used in compound statements",
50904 "acc update");
50905 ret = true;
50906 break;
50908 else if (context != pragma_compound)
50909 goto bad_stmt;
50910 cp_parser_omp_construct (parser, pragma_tok, if_p);
50911 return true;
50913 case PRAGMA_OACC_WAIT:
50914 if (context == pragma_stmt)
50916 error_at (pragma_tok->location,
50917 "%<#pragma %s%> may only be used in compound statements",
50918 "acc wait");
50919 ret = true;
50920 break;
50922 else if (context != pragma_compound)
50923 goto bad_stmt;
50924 cp_parser_omp_construct (parser, pragma_tok, if_p);
50925 return true;
50926 case PRAGMA_OMP_ALLOCATE:
50927 cp_parser_omp_allocate (parser, pragma_tok);
50928 return false;
50929 case PRAGMA_OACC_ATOMIC:
50930 case PRAGMA_OACC_CACHE:
50931 case PRAGMA_OACC_DATA:
50932 case PRAGMA_OACC_HOST_DATA:
50933 case PRAGMA_OACC_KERNELS:
50934 case PRAGMA_OACC_LOOP:
50935 case PRAGMA_OACC_PARALLEL:
50936 case PRAGMA_OACC_SERIAL:
50937 case PRAGMA_OMP_ASSUME:
50938 case PRAGMA_OMP_ATOMIC:
50939 case PRAGMA_OMP_CRITICAL:
50940 case PRAGMA_OMP_DISTRIBUTE:
50941 case PRAGMA_OMP_FOR:
50942 case PRAGMA_OMP_LOOP:
50943 case PRAGMA_OMP_MASKED:
50944 case PRAGMA_OMP_MASTER:
50945 case PRAGMA_OMP_PARALLEL:
50946 case PRAGMA_OMP_SCOPE:
50947 case PRAGMA_OMP_SECTIONS:
50948 case PRAGMA_OMP_SIMD:
50949 case PRAGMA_OMP_SINGLE:
50950 case PRAGMA_OMP_TASK:
50951 case PRAGMA_OMP_TASKGROUP:
50952 case PRAGMA_OMP_TASKLOOP:
50953 case PRAGMA_OMP_TEAMS:
50954 if (context != pragma_stmt && context != pragma_compound)
50955 goto bad_stmt;
50956 stmt = push_omp_privatization_clauses (false);
50957 cp_parser_omp_construct (parser, pragma_tok, if_p);
50958 pop_omp_privatization_clauses (stmt);
50959 return true;
50961 case PRAGMA_OMP_REQUIRES:
50962 if (context != pragma_external)
50964 error_at (pragma_tok->location,
50965 "%<#pragma omp requires%> may only be used at file or "
50966 "namespace scope");
50967 ret = true;
50968 break;
50970 return cp_parser_omp_requires (parser, pragma_tok);
50972 case PRAGMA_OMP_ASSUMES:
50973 if (context != pragma_external)
50975 error_at (pragma_tok->location,
50976 "%<#pragma omp assumes%> may only be used at file or "
50977 "namespace scope");
50978 ret = true;
50979 break;
50981 return cp_parser_omp_assumes (parser, pragma_tok);
50983 case PRAGMA_OMP_NOTHING:
50984 cp_parser_omp_nothing (parser, pragma_tok);
50985 return false;
50987 case PRAGMA_OMP_ERROR:
50988 return cp_parser_omp_error (parser, pragma_tok, context);
50990 case PRAGMA_OMP_ORDERED:
50991 if (context != pragma_stmt && context != pragma_compound)
50992 goto bad_stmt;
50993 stmt = push_omp_privatization_clauses (false);
50994 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
50995 pop_omp_privatization_clauses (stmt);
50996 return ret;
50998 case PRAGMA_OMP_TARGET:
50999 if (context != pragma_stmt && context != pragma_compound)
51000 goto bad_stmt;
51001 stmt = push_omp_privatization_clauses (false);
51002 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
51003 pop_omp_privatization_clauses (stmt);
51004 return ret;
51006 case PRAGMA_OMP_BEGIN:
51007 cp_parser_omp_begin (parser, pragma_tok);
51008 return false;
51010 case PRAGMA_OMP_END:
51011 cp_parser_omp_end (parser, pragma_tok);
51012 return false;
51014 case PRAGMA_OMP_SCAN:
51015 error_at (pragma_tok->location,
51016 "%<#pragma omp scan%> may only be used in "
51017 "a loop construct with %<inscan%> %<reduction%> clause");
51018 break;
51020 case PRAGMA_OMP_SECTION:
51021 error_at (pragma_tok->location,
51022 "%<#pragma omp section%> may only be used in "
51023 "%<#pragma omp sections%> construct");
51024 break;
51026 case PRAGMA_IVDEP:
51027 case PRAGMA_UNROLL:
51028 case PRAGMA_NOVECTOR:
51030 bool ivdep = false;
51031 tree unroll = NULL_TREE;
51032 bool novector = false;
51033 const char *pragma_str;
51035 switch (id)
51037 case PRAGMA_IVDEP:
51038 pragma_str = "ivdep";
51039 break;
51040 case PRAGMA_UNROLL:
51041 pragma_str = "unroll";
51042 break;
51043 case PRAGMA_NOVECTOR:
51044 pragma_str = "novector";
51045 break;
51046 default:
51047 gcc_unreachable ();
51050 if (context == pragma_external)
51052 error_at (pragma_tok->location,
51053 "%<#pragma GCC %s%> must be inside a function",
51054 pragma_str);
51055 break;
51058 cp_token *tok = pragma_tok;
51059 bool has_more = true;
51062 switch (cp_parser_pragma_kind (tok))
51064 case PRAGMA_IVDEP:
51066 if (tok != pragma_tok)
51067 tok = cp_lexer_consume_token (parser->lexer);
51068 ivdep = cp_parser_pragma_ivdep (parser, tok);
51069 break;
51071 case PRAGMA_UNROLL:
51073 if (tok != pragma_tok)
51074 tok = cp_lexer_consume_token (parser->lexer);
51075 unroll = cp_parser_pragma_unroll (parser, tok);
51076 break;
51078 case PRAGMA_NOVECTOR:
51080 if (tok != pragma_tok)
51081 tok = cp_lexer_consume_token (parser->lexer);
51082 novector = cp_parser_pragma_novector (parser, tok);
51083 break;
51085 default:
51086 has_more = false;
51087 break;
51089 tok = cp_lexer_peek_token (the_parser->lexer);
51090 has_more = has_more && tok->type == CPP_PRAGMA;
51092 while (has_more);
51094 if (tok->type != CPP_KEYWORD
51095 || (tok->keyword != RID_FOR
51096 && tok->keyword != RID_WHILE
51097 && tok->keyword != RID_DO))
51099 cp_parser_error (parser, "for, while or do statement expected");
51100 return false;
51102 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
51103 return true;
51106 default:
51107 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
51108 c_invoke_pragma_handler (id);
51109 break;
51111 bad_stmt:
51112 cp_parser_error (parser, "expected declaration specifiers");
51113 break;
51116 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51117 return ret;
51120 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
51121 populated the lexer with any tokens (the tokens rather being read by
51122 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
51123 a pragma. */
51124 static void
51125 maybe_read_tokens_for_pragma_lex ()
51127 const auto lexer = the_parser->lexer;
51128 if (!lexer->buffer->is_empty ())
51129 return;
51131 /* Read the rest of the tokens comprising the pragma line. */
51132 cp_token *tok;
51135 tok = vec_safe_push (lexer->buffer, cp_token ());
51136 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
51137 gcc_assert (tok->type != CPP_EOF);
51138 } while (tok->type != CPP_PRAGMA_EOL);
51139 lexer->next_token = lexer->buffer->address ();
51140 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
51143 /* The interface the pragma parsers have to the lexer. */
51145 enum cpp_ttype
51146 pragma_lex (tree *value, location_t *loc)
51148 if (flag_preprocess_only)
51149 maybe_read_tokens_for_pragma_lex ();
51151 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
51152 enum cpp_ttype ret = tok->type;
51154 *value = tok->u.value;
51155 if (loc)
51156 *loc = tok->location;
51158 if (ret == CPP_PRAGMA_EOL)
51159 ret = CPP_EOF;
51160 else if (ret == CPP_STRING)
51161 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
51162 /*wide_ok=*/false);
51163 else
51165 if (ret == CPP_KEYWORD)
51166 ret = CPP_NAME;
51167 cp_lexer_consume_token (the_parser->lexer);
51170 return ret;
51173 void
51174 pragma_lex_discard_to_eol ()
51176 /* We have already read all the tokens, so we just need to discard
51177 them here. */
51178 const auto lexer = the_parser->lexer;
51179 lexer->next_token = lexer->last_token;
51180 lexer->buffer->truncate (0);
51184 /* External interface. */
51186 /* Parse one entire translation unit. */
51188 void
51189 c_parse_file (void)
51191 static bool already_called = false;
51193 if (already_called)
51194 fatal_error (input_location,
51195 "multi-source compilation not implemented for C++");
51196 already_called = true;
51198 /* cp_lexer_new_main is called before doing any GC allocation
51199 because tokenization might load a PCH file. */
51200 cp_lexer_new_main ();
51202 cp_parser_translation_unit (the_parser);
51203 class_decl_loc_t::diag_mismatched_tags ();
51205 the_parser = NULL;
51207 finish_translation_unit ();
51210 /* Create an identifier for a generic parameter type (a synthesized
51211 template parameter implied by `auto' or a concept identifier). */
51213 static GTY(()) int generic_parm_count;
51214 static tree
51215 make_generic_type_name ()
51217 char buf[32];
51218 sprintf (buf, "auto:%d", ++generic_parm_count);
51219 return get_identifier (buf);
51222 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
51223 (creating a new template parameter list if necessary). Returns the newly
51224 created template type parm. */
51226 static tree
51227 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
51229 /* A requires-clause is not a function and cannot have placeholders. */
51230 if (current_binding_level->requires_expression)
51232 error ("placeholder type not allowed in this context");
51233 return error_mark_node;
51236 gcc_assert (current_binding_level->kind == sk_function_parms);
51238 /* We are either continuing a function template that already contains implicit
51239 template parameters, creating a new fully-implicit function template, or
51240 extending an existing explicit function template with implicit template
51241 parameters. */
51243 cp_binding_level *const entry_scope = current_binding_level;
51245 bool become_template = false;
51246 cp_binding_level *parent_scope = 0;
51248 if (parser->implicit_template_scope)
51250 gcc_assert (parser->implicit_template_parms);
51252 current_binding_level = parser->implicit_template_scope;
51254 else
51256 /* Roll back to the existing template parameter scope (in the case of
51257 extending an explicit function template) or introduce a new template
51258 parameter scope ahead of the function parameter scope (or class scope
51259 in the case of out-of-line member definitions). The function scope is
51260 added back after template parameter synthesis below. */
51262 cp_binding_level *scope = entry_scope;
51264 while (scope->kind == sk_function_parms)
51266 parent_scope = scope;
51267 scope = scope->level_chain;
51269 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
51271 /* If not defining a class, then any class scope is a scope level in
51272 an out-of-line member definition. In this case simply wind back
51273 beyond the first such scope to inject the template parameter list.
51274 Otherwise wind back to the class being defined. The latter can
51275 occur in class member friend declarations such as:
51277 class A {
51278 void foo (auto);
51280 class B {
51281 friend void A::foo (auto);
51284 The template parameter list synthesized for the friend declaration
51285 must be injected in the scope of 'B'. This can also occur in
51286 erroneous cases such as:
51288 struct A {
51289 struct B {
51290 void foo (auto);
51292 void B::foo (auto) {}
51295 Here the attempted definition of 'B::foo' within 'A' is ill-formed
51296 but, nevertheless, the template parameter list synthesized for the
51297 declarator should be injected into the scope of 'A' as if the
51298 ill-formed template was specified explicitly. */
51300 while (scope->kind == sk_class && !scope->defining_class_p)
51302 parent_scope = scope;
51303 scope = scope->level_chain;
51307 current_binding_level = scope;
51309 if (scope->kind != sk_template_parms
51310 || !function_being_declared_is_template_p (parser))
51312 /* Introduce a new template parameter list for implicit template
51313 parameters. */
51315 become_template = true;
51317 parser->implicit_template_scope
51318 = begin_scope (sk_template_parms, NULL);
51320 ++processing_template_decl;
51322 parser->fully_implicit_function_template_p = true;
51323 ++parser->num_template_parameter_lists;
51325 else
51327 /* Synthesize implicit template parameters at the end of the explicit
51328 template parameter list. */
51330 gcc_assert (current_template_parms);
51332 parser->implicit_template_scope = scope;
51334 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51335 parser->implicit_template_parms
51336 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
51340 /* Synthesize a new template parameter and track the current template
51341 parameter chain with implicit_template_parms. */
51343 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
51344 tree synth_id = make_generic_type_name ();
51345 bool non_type = false;
51347 /* Synthesize the type template parameter. */
51348 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
51349 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
51351 if (become_template)
51352 current_template_parms = tree_cons (size_int (current_template_depth + 1),
51353 NULL_TREE, current_template_parms);
51355 /* Attach the constraint to the parm before processing. */
51356 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
51357 TREE_TYPE (node) = constr;
51358 tree new_parm
51359 = process_template_parm (parser->implicit_template_parms,
51360 input_location,
51361 node,
51362 /*non_type=*/non_type,
51363 /*param_pack=*/false);
51364 // Process_template_parm returns the list of parms, and
51365 // parser->implicit_template_parms holds the final node of the parm
51366 // list. We really want to manipulate the newly appended element.
51367 gcc_checking_assert (!parser->implicit_template_parms
51368 || parser->implicit_template_parms == new_parm);
51369 if (parser->implicit_template_parms)
51370 new_parm = TREE_CHAIN (new_parm);
51371 gcc_checking_assert (!TREE_CHAIN (new_parm));
51373 // Record the last implicit parm node
51374 parser->implicit_template_parms = new_parm;
51376 /* Mark the synthetic declaration "virtual". This is used when
51377 comparing template-heads to determine if whether an abbreviated
51378 function template is equivalent to an explicit template.
51380 Note that DECL_ARTIFICIAL is used elsewhere for template
51381 parameters. */
51382 if (TREE_VALUE (new_parm) != error_mark_node)
51383 DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (new_parm)) = true;
51385 tree new_decl = get_local_decls ();
51386 if (non_type)
51387 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
51388 new_decl = DECL_INITIAL (new_decl);
51390 /* If creating a fully implicit function template, start the new implicit
51391 template parameter list with this synthesized type, otherwise grow the
51392 current template parameter list. */
51394 if (become_template)
51396 parent_scope->level_chain = current_binding_level;
51398 tree new_parms = make_tree_vec (1);
51399 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
51400 TREE_VALUE (current_template_parms) = new_parms;
51402 else
51404 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51405 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
51406 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
51407 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
51410 /* If the new parameter was constrained, we need to add that to the
51411 constraints in the template parameter list. */
51412 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
51414 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
51415 reqs = combine_constraint_expressions (reqs, req);
51416 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
51419 current_binding_level = entry_scope;
51421 return new_decl;
51424 /* Finish the declaration of a fully implicit function template. Such a
51425 template has no explicit template parameter list so has not been through the
51426 normal template head and tail processing. synthesize_implicit_template_parm
51427 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
51428 provided if the declaration is a class member such that its template
51429 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
51430 form is returned. Otherwise NULL_TREE is returned. */
51432 static tree
51433 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
51435 gcc_assert (parser->fully_implicit_function_template_p);
51437 if (member_decl_opt && member_decl_opt != error_mark_node
51438 && DECL_VIRTUAL_P (member_decl_opt))
51440 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
51441 "implicit templates may not be %<virtual%>");
51442 DECL_VIRTUAL_P (member_decl_opt) = false;
51445 if (member_decl_opt)
51446 member_decl_opt = finish_member_template_decl (member_decl_opt);
51447 end_template_decl ();
51449 parser->fully_implicit_function_template_p = false;
51450 parser->implicit_template_parms = 0;
51451 parser->implicit_template_scope = 0;
51452 --parser->num_template_parameter_lists;
51454 return member_decl_opt;
51457 /* Like finish_fully_implicit_template, but to be used in error
51458 recovery, rearranging scopes so that we restore the state we had
51459 before synthesize_implicit_template_parm inserted the implement
51460 template parms scope. */
51462 static void
51463 abort_fully_implicit_template (cp_parser *parser)
51465 cp_binding_level *return_to_scope = current_binding_level;
51467 if (parser->implicit_template_scope
51468 && return_to_scope != parser->implicit_template_scope)
51470 cp_binding_level *child = return_to_scope;
51471 for (cp_binding_level *scope = child->level_chain;
51472 scope != parser->implicit_template_scope;
51473 scope = child->level_chain)
51474 child = scope;
51475 child->level_chain = parser->implicit_template_scope->level_chain;
51476 parser->implicit_template_scope->level_chain = return_to_scope;
51477 current_binding_level = parser->implicit_template_scope;
51479 else
51480 return_to_scope = return_to_scope->level_chain;
51482 finish_fully_implicit_template (parser, NULL);
51484 gcc_assert (current_binding_level == return_to_scope);
51487 /* Helper function for diagnostics that have complained about things
51488 being used with 'extern "C"' linkage.
51490 Attempt to issue a note showing where the 'extern "C"' linkage began. */
51492 void
51493 maybe_show_extern_c_location (void)
51495 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
51496 inform (the_parser->innermost_linkage_specification_location,
51497 "%<extern \"C\"%> linkage started here");
51500 #include "gt-cp-parser.h"